slip.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. /*
  2. * slip.c This module implements the SLIP protocol for kernel-based
  3. * devices like TTY. It interfaces between a raw TTY, and the
  4. * kernel's INET protocol layers.
  5. *
  6. * Version: @(#)slip.c 0.8.3 12/24/94
  7. *
  8. * Authors: Laurence Culhane, <loz@holmes.demon.co.uk>
  9. * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  10. *
  11. * Fixes:
  12. * Alan Cox : Sanity checks and avoid tx overruns.
  13. * Has a new sl->mtu field.
  14. * Alan Cox : Found cause of overrun. ifconfig sl0
  15. * mtu upwards. Driver now spots this
  16. * and grows/shrinks its buffers(hack!).
  17. * Memory leak if you run out of memory
  18. * setting up a slip driver fixed.
  19. * Matt Dillon : Printable slip (borrowed from NET2E)
  20. * Pauline Middelink : Slip driver fixes.
  21. * Alan Cox : Honours the old SL_COMPRESSED flag
  22. * Alan Cox : KISS AX.25 and AXUI IP support
  23. * Michael Riepe : Automatic CSLIP recognition added
  24. * Charles Hedrick : CSLIP header length problem fix.
  25. * Alan Cox : Corrected non-IP cases of the above.
  26. * Alan Cox : Now uses hardware type as per FvK.
  27. * Alan Cox : Default to 192.168.0.0 (RFC 1597)
  28. * A.N.Kuznetsov : dev_tint() recursion fix.
  29. * Dmitry Gorodchanin : SLIP memory leaks
  30. * Dmitry Gorodchanin : Code cleanup. Reduce tty driver
  31. * buffering from 4096 to 256 bytes.
  32. * Improving SLIP response time.
  33. * CONFIG_SLIP_MODE_SLIP6.
  34. * ifconfig sl? up & down now works
  35. * correctly.
  36. * Modularization.
  37. * Alan Cox : Oops - fix AX.25 buffer lengths
  38. * Dmitry Gorodchanin : Even more cleanups. Preserve CSLIP
  39. * statistics. Include CSLIP code only
  40. * if it really needed.
  41. * Alan Cox : Free slhc buffers in the right place.
  42. * Alan Cox : Allow for digipeated IP over AX.25
  43. * Matti Aarnio : Dynamic SLIP devices, with ideas taken
  44. * from Jim Freeman's <jfree@caldera.com>
  45. * dynamic PPP devices. We do NOT kfree()
  46. * device entries, just reg./unreg. them
  47. * as they are needed. We kfree() them
  48. * at module cleanup.
  49. * With MODULE-loading ``insmod'', user
  50. * can issue parameter: slip_maxdev=1024
  51. * (Or how much he/she wants.. Default
  52. * is 256)
  53. * Stanislav Voronyi : Slip line checking, with ideas taken
  54. * from multislip BSDI driver which was
  55. * written by Igor Chechik, RELCOM Corp.
  56. * Only algorithms have been ported to
  57. * Linux SLIP driver.
  58. * Vitaly E. Lavrov : Sane behaviour on tty hangup.
  59. * Alexey Kuznetsov : Cleanup interfaces to tty & netdevice
  60. * modules.
  61. */
  62. #define SL_CHECK_TRANSMIT
  63. #include <linux/module.h>
  64. #include <linux/moduleparam.h>
  65. #include <linux/uaccess.h>
  66. #include <linux/bitops.h>
  67. #include <linux/sched/signal.h>
  68. #include <linux/string.h>
  69. #include <linux/mm.h>
  70. #include <linux/interrupt.h>
  71. #include <linux/in.h>
  72. #include <linux/tty.h>
  73. #include <linux/errno.h>
  74. #include <linux/netdevice.h>
  75. #include <linux/etherdevice.h>
  76. #include <linux/skbuff.h>
  77. #include <linux/rtnetlink.h>
  78. #include <linux/if_arp.h>
  79. #include <linux/if_slip.h>
  80. #include <linux/compat.h>
  81. #include <linux/delay.h>
  82. #include <linux/init.h>
  83. #include <linux/slab.h>
  84. #include <linux/workqueue.h>
  85. #include "slip.h"
  86. #ifdef CONFIG_INET
  87. #include <linux/ip.h>
  88. #include <linux/tcp.h>
  89. #include <net/slhc_vj.h>
  90. #endif
  91. #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY"
  92. static struct net_device **slip_devs;
  93. static int slip_maxdev = SL_NRUNIT;
  94. module_param(slip_maxdev, int, 0);
  95. MODULE_PARM_DESC(slip_maxdev, "Maximum number of slip devices");
  96. static int slip_esc(unsigned char *p, unsigned char *d, int len);
  97. static void slip_unesc(struct slip *sl, unsigned char c);
  98. #ifdef CONFIG_SLIP_MODE_SLIP6
  99. static int slip_esc6(unsigned char *p, unsigned char *d, int len);
  100. static void slip_unesc6(struct slip *sl, unsigned char c);
  101. #endif
  102. #ifdef CONFIG_SLIP_SMART
  103. static void sl_keepalive(struct timer_list *t);
  104. static void sl_outfill(struct timer_list *t);
  105. static int sl_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  106. #endif
  107. /********************************
  108. * Buffer administration routines:
  109. * sl_alloc_bufs()
  110. * sl_free_bufs()
  111. * sl_realloc_bufs()
  112. *
  113. * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because
  114. * sl_realloc_bufs provides strong atomicity and reallocation
  115. * on actively running device.
  116. *********************************/
  117. /*
  118. Allocate channel buffers.
  119. */
  120. static int sl_alloc_bufs(struct slip *sl, int mtu)
  121. {
  122. int err = -ENOBUFS;
  123. unsigned long len;
  124. char *rbuff = NULL;
  125. char *xbuff = NULL;
  126. #ifdef SL_INCLUDE_CSLIP
  127. char *cbuff = NULL;
  128. struct slcompress *slcomp = NULL;
  129. #endif
  130. /*
  131. * Allocate the SLIP frame buffers:
  132. *
  133. * rbuff Receive buffer.
  134. * xbuff Transmit buffer.
  135. * cbuff Temporary compression buffer.
  136. */
  137. len = mtu * 2;
  138. /*
  139. * allow for arrival of larger UDP packets, even if we say not to
  140. * also fixes a bug in which SunOS sends 512-byte packets even with
  141. * an MSS of 128
  142. */
  143. if (len < 576 * 2)
  144. len = 576 * 2;
  145. rbuff = kmalloc(len + 4, GFP_KERNEL);
  146. if (rbuff == NULL)
  147. goto err_exit;
  148. xbuff = kmalloc(len + 4, GFP_KERNEL);
  149. if (xbuff == NULL)
  150. goto err_exit;
  151. #ifdef SL_INCLUDE_CSLIP
  152. cbuff = kmalloc(len + 4, GFP_KERNEL);
  153. if (cbuff == NULL)
  154. goto err_exit;
  155. slcomp = slhc_init(16, 16);
  156. if (IS_ERR(slcomp))
  157. goto err_exit;
  158. #endif
  159. spin_lock_bh(&sl->lock);
  160. if (sl->tty == NULL) {
  161. spin_unlock_bh(&sl->lock);
  162. err = -ENODEV;
  163. goto err_exit;
  164. }
  165. sl->mtu = mtu;
  166. sl->buffsize = len;
  167. sl->rcount = 0;
  168. sl->xleft = 0;
  169. rbuff = xchg(&sl->rbuff, rbuff);
  170. xbuff = xchg(&sl->xbuff, xbuff);
  171. #ifdef SL_INCLUDE_CSLIP
  172. cbuff = xchg(&sl->cbuff, cbuff);
  173. slcomp = xchg(&sl->slcomp, slcomp);
  174. #endif
  175. #ifdef CONFIG_SLIP_MODE_SLIP6
  176. sl->xdata = 0;
  177. sl->xbits = 0;
  178. #endif
  179. spin_unlock_bh(&sl->lock);
  180. err = 0;
  181. /* Cleanup */
  182. err_exit:
  183. #ifdef SL_INCLUDE_CSLIP
  184. kfree(cbuff);
  185. slhc_free(slcomp);
  186. #endif
  187. kfree(xbuff);
  188. kfree(rbuff);
  189. return err;
  190. }
  191. /* Free a SLIP channel buffers. */
  192. static void sl_free_bufs(struct slip *sl)
  193. {
  194. /* Free all SLIP frame buffers. */
  195. kfree(xchg(&sl->rbuff, NULL));
  196. kfree(xchg(&sl->xbuff, NULL));
  197. #ifdef SL_INCLUDE_CSLIP
  198. kfree(xchg(&sl->cbuff, NULL));
  199. slhc_free(xchg(&sl->slcomp, NULL));
  200. #endif
  201. }
  202. /*
  203. Reallocate slip channel buffers.
  204. */
  205. static int sl_realloc_bufs(struct slip *sl, int mtu)
  206. {
  207. int err = 0;
  208. struct net_device *dev = sl->dev;
  209. unsigned char *xbuff, *rbuff;
  210. #ifdef SL_INCLUDE_CSLIP
  211. unsigned char *cbuff;
  212. #endif
  213. int len = mtu * 2;
  214. /*
  215. * allow for arrival of larger UDP packets, even if we say not to
  216. * also fixes a bug in which SunOS sends 512-byte packets even with
  217. * an MSS of 128
  218. */
  219. if (len < 576 * 2)
  220. len = 576 * 2;
  221. xbuff = kmalloc(len + 4, GFP_ATOMIC);
  222. rbuff = kmalloc(len + 4, GFP_ATOMIC);
  223. #ifdef SL_INCLUDE_CSLIP
  224. cbuff = kmalloc(len + 4, GFP_ATOMIC);
  225. #endif
  226. #ifdef SL_INCLUDE_CSLIP
  227. if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
  228. #else
  229. if (xbuff == NULL || rbuff == NULL) {
  230. #endif
  231. if (mtu > sl->mtu) {
  232. printk(KERN_WARNING "%s: unable to grow slip buffers, MTU change cancelled.\n",
  233. dev->name);
  234. err = -ENOBUFS;
  235. }
  236. goto done;
  237. }
  238. spin_lock_bh(&sl->lock);
  239. err = -ENODEV;
  240. if (sl->tty == NULL)
  241. goto done_on_bh;
  242. xbuff = xchg(&sl->xbuff, xbuff);
  243. rbuff = xchg(&sl->rbuff, rbuff);
  244. #ifdef SL_INCLUDE_CSLIP
  245. cbuff = xchg(&sl->cbuff, cbuff);
  246. #endif
  247. if (sl->xleft) {
  248. if (sl->xleft <= len) {
  249. memcpy(sl->xbuff, sl->xhead, sl->xleft);
  250. } else {
  251. sl->xleft = 0;
  252. dev->stats.tx_dropped++;
  253. }
  254. }
  255. sl->xhead = sl->xbuff;
  256. if (sl->rcount) {
  257. if (sl->rcount <= len) {
  258. memcpy(sl->rbuff, rbuff, sl->rcount);
  259. } else {
  260. sl->rcount = 0;
  261. dev->stats.rx_over_errors++;
  262. set_bit(SLF_ERROR, &sl->flags);
  263. }
  264. }
  265. sl->mtu = mtu;
  266. dev->mtu = mtu;
  267. sl->buffsize = len;
  268. err = 0;
  269. done_on_bh:
  270. spin_unlock_bh(&sl->lock);
  271. done:
  272. kfree(xbuff);
  273. kfree(rbuff);
  274. #ifdef SL_INCLUDE_CSLIP
  275. kfree(cbuff);
  276. #endif
  277. return err;
  278. }
  279. /* Set the "sending" flag. This must be atomic hence the set_bit. */
  280. static inline void sl_lock(struct slip *sl)
  281. {
  282. netif_stop_queue(sl->dev);
  283. }
  284. /* Clear the "sending" flag. This must be atomic, hence the ASM. */
  285. static inline void sl_unlock(struct slip *sl)
  286. {
  287. netif_wake_queue(sl->dev);
  288. }
  289. /* Send one completely decapsulated IP datagram to the IP layer. */
  290. static void sl_bump(struct slip *sl)
  291. {
  292. struct net_device *dev = sl->dev;
  293. struct sk_buff *skb;
  294. int count;
  295. count = sl->rcount;
  296. #ifdef SL_INCLUDE_CSLIP
  297. if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
  298. unsigned char c = sl->rbuff[0];
  299. if (c & SL_TYPE_COMPRESSED_TCP) {
  300. /* ignore compressed packets when CSLIP is off */
  301. if (!(sl->mode & SL_MODE_CSLIP)) {
  302. printk(KERN_WARNING "%s: compressed packet ignored\n", dev->name);
  303. return;
  304. }
  305. /* make sure we've reserved enough space for uncompress
  306. to use */
  307. if (count + 80 > sl->buffsize) {
  308. dev->stats.rx_over_errors++;
  309. return;
  310. }
  311. count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
  312. if (count <= 0)
  313. return;
  314. } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
  315. if (!(sl->mode & SL_MODE_CSLIP)) {
  316. /* turn on header compression */
  317. sl->mode |= SL_MODE_CSLIP;
  318. sl->mode &= ~SL_MODE_ADAPTIVE;
  319. printk(KERN_INFO "%s: header compression turned on\n", dev->name);
  320. }
  321. sl->rbuff[0] &= 0x4f;
  322. if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0)
  323. return;
  324. }
  325. }
  326. #endif /* SL_INCLUDE_CSLIP */
  327. dev->stats.rx_bytes += count;
  328. skb = dev_alloc_skb(count);
  329. if (skb == NULL) {
  330. printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n", dev->name);
  331. dev->stats.rx_dropped++;
  332. return;
  333. }
  334. skb->dev = dev;
  335. skb_put_data(skb, sl->rbuff, count);
  336. skb_reset_mac_header(skb);
  337. skb->protocol = htons(ETH_P_IP);
  338. netif_rx_ni(skb);
  339. dev->stats.rx_packets++;
  340. }
  341. /* Encapsulate one IP datagram and stuff into a TTY queue. */
  342. static void sl_encaps(struct slip *sl, unsigned char *icp, int len)
  343. {
  344. unsigned char *p;
  345. int actual, count;
  346. if (len > sl->mtu) { /* Sigh, shouldn't occur BUT ... */
  347. printk(KERN_WARNING "%s: truncating oversized transmit packet!\n", sl->dev->name);
  348. sl->dev->stats.tx_dropped++;
  349. sl_unlock(sl);
  350. return;
  351. }
  352. p = icp;
  353. #ifdef SL_INCLUDE_CSLIP
  354. if (sl->mode & SL_MODE_CSLIP)
  355. len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
  356. #endif
  357. #ifdef CONFIG_SLIP_MODE_SLIP6
  358. if (sl->mode & SL_MODE_SLIP6)
  359. count = slip_esc6(p, sl->xbuff, len);
  360. else
  361. #endif
  362. count = slip_esc(p, sl->xbuff, len);
  363. /* Order of next two lines is *very* important.
  364. * When we are sending a little amount of data,
  365. * the transfer may be completed inside the ops->write()
  366. * routine, because it's running with interrupts enabled.
  367. * In this case we *never* got WRITE_WAKEUP event,
  368. * if we did not request it before write operation.
  369. * 14 Oct 1994 Dmitry Gorodchanin.
  370. */
  371. set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  372. actual = sl->tty->ops->write(sl->tty, sl->xbuff, count);
  373. #ifdef SL_CHECK_TRANSMIT
  374. netif_trans_update(sl->dev);
  375. #endif
  376. sl->xleft = count - actual;
  377. sl->xhead = sl->xbuff + actual;
  378. #ifdef CONFIG_SLIP_SMART
  379. /* VSV */
  380. clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
  381. #endif
  382. }
  383. /* Write out any remaining transmit buffer. Scheduled when tty is writable */
  384. static void slip_transmit(struct work_struct *work)
  385. {
  386. struct slip *sl = container_of(work, struct slip, tx_work);
  387. int actual;
  388. spin_lock_bh(&sl->lock);
  389. /* First make sure we're connected. */
  390. if (!sl->tty || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) {
  391. spin_unlock_bh(&sl->lock);
  392. return;
  393. }
  394. if (sl->xleft <= 0) {
  395. /* Now serial buffer is almost free & we can start
  396. * transmission of another packet */
  397. sl->dev->stats.tx_packets++;
  398. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  399. spin_unlock_bh(&sl->lock);
  400. sl_unlock(sl);
  401. return;
  402. }
  403. actual = sl->tty->ops->write(sl->tty, sl->xhead, sl->xleft);
  404. sl->xleft -= actual;
  405. sl->xhead += actual;
  406. spin_unlock_bh(&sl->lock);
  407. }
  408. /*
  409. * Called by the driver when there's room for more data.
  410. * Schedule the transmit.
  411. */
  412. static void slip_write_wakeup(struct tty_struct *tty)
  413. {
  414. struct slip *sl;
  415. rcu_read_lock();
  416. sl = rcu_dereference(tty->disc_data);
  417. if (!sl)
  418. goto out;
  419. schedule_work(&sl->tx_work);
  420. out:
  421. rcu_read_unlock();
  422. }
  423. static void sl_tx_timeout(struct net_device *dev)
  424. {
  425. struct slip *sl = netdev_priv(dev);
  426. spin_lock(&sl->lock);
  427. if (netif_queue_stopped(dev)) {
  428. if (!netif_running(dev))
  429. goto out;
  430. /* May be we must check transmitter timeout here ?
  431. * 14 Oct 1994 Dmitry Gorodchanin.
  432. */
  433. #ifdef SL_CHECK_TRANSMIT
  434. if (time_before(jiffies, dev_trans_start(dev) + 20 * HZ)) {
  435. /* 20 sec timeout not reached */
  436. goto out;
  437. }
  438. printk(KERN_WARNING "%s: transmit timed out, %s?\n",
  439. dev->name,
  440. (tty_chars_in_buffer(sl->tty) || sl->xleft) ?
  441. "bad line quality" : "driver error");
  442. sl->xleft = 0;
  443. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  444. sl_unlock(sl);
  445. #endif
  446. }
  447. out:
  448. spin_unlock(&sl->lock);
  449. }
  450. /* Encapsulate an IP datagram and kick it into a TTY queue. */
  451. static netdev_tx_t
  452. sl_xmit(struct sk_buff *skb, struct net_device *dev)
  453. {
  454. struct slip *sl = netdev_priv(dev);
  455. spin_lock(&sl->lock);
  456. if (!netif_running(dev)) {
  457. spin_unlock(&sl->lock);
  458. printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name);
  459. dev_kfree_skb(skb);
  460. return NETDEV_TX_OK;
  461. }
  462. if (sl->tty == NULL) {
  463. spin_unlock(&sl->lock);
  464. dev_kfree_skb(skb);
  465. return NETDEV_TX_OK;
  466. }
  467. sl_lock(sl);
  468. dev->stats.tx_bytes += skb->len;
  469. sl_encaps(sl, skb->data, skb->len);
  470. spin_unlock(&sl->lock);
  471. dev_kfree_skb(skb);
  472. return NETDEV_TX_OK;
  473. }
  474. /******************************************
  475. * Routines looking at netdevice side.
  476. ******************************************/
  477. /* Netdevice UP -> DOWN routine */
  478. static int
  479. sl_close(struct net_device *dev)
  480. {
  481. struct slip *sl = netdev_priv(dev);
  482. spin_lock_bh(&sl->lock);
  483. if (sl->tty)
  484. /* TTY discipline is running. */
  485. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  486. netif_stop_queue(dev);
  487. sl->rcount = 0;
  488. sl->xleft = 0;
  489. spin_unlock_bh(&sl->lock);
  490. return 0;
  491. }
  492. /* Netdevice DOWN -> UP routine */
  493. static int sl_open(struct net_device *dev)
  494. {
  495. struct slip *sl = netdev_priv(dev);
  496. if (sl->tty == NULL)
  497. return -ENODEV;
  498. sl->flags &= (1 << SLF_INUSE);
  499. netif_start_queue(dev);
  500. return 0;
  501. }
  502. /* Netdevice change MTU request */
  503. static int sl_change_mtu(struct net_device *dev, int new_mtu)
  504. {
  505. struct slip *sl = netdev_priv(dev);
  506. return sl_realloc_bufs(sl, new_mtu);
  507. }
  508. /* Netdevice get statistics request */
  509. static void
  510. sl_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  511. {
  512. struct net_device_stats *devstats = &dev->stats;
  513. #ifdef SL_INCLUDE_CSLIP
  514. struct slip *sl = netdev_priv(dev);
  515. struct slcompress *comp = sl->slcomp;
  516. #endif
  517. stats->rx_packets = devstats->rx_packets;
  518. stats->tx_packets = devstats->tx_packets;
  519. stats->rx_bytes = devstats->rx_bytes;
  520. stats->tx_bytes = devstats->tx_bytes;
  521. stats->rx_dropped = devstats->rx_dropped;
  522. stats->tx_dropped = devstats->tx_dropped;
  523. stats->tx_errors = devstats->tx_errors;
  524. stats->rx_errors = devstats->rx_errors;
  525. stats->rx_over_errors = devstats->rx_over_errors;
  526. #ifdef SL_INCLUDE_CSLIP
  527. if (comp) {
  528. /* Generic compressed statistics */
  529. stats->rx_compressed = comp->sls_i_compressed;
  530. stats->tx_compressed = comp->sls_o_compressed;
  531. /* Are we really still needs this? */
  532. stats->rx_fifo_errors += comp->sls_i_compressed;
  533. stats->rx_dropped += comp->sls_i_tossed;
  534. stats->tx_fifo_errors += comp->sls_o_compressed;
  535. stats->collisions += comp->sls_o_misses;
  536. }
  537. #endif
  538. }
  539. /* Netdevice register callback */
  540. static int sl_init(struct net_device *dev)
  541. {
  542. struct slip *sl = netdev_priv(dev);
  543. /*
  544. * Finish setting up the DEVICE info.
  545. */
  546. dev->mtu = sl->mtu;
  547. dev->type = ARPHRD_SLIP + sl->mode;
  548. #ifdef SL_CHECK_TRANSMIT
  549. dev->watchdog_timeo = 20*HZ;
  550. #endif
  551. return 0;
  552. }
  553. static void sl_uninit(struct net_device *dev)
  554. {
  555. struct slip *sl = netdev_priv(dev);
  556. sl_free_bufs(sl);
  557. }
  558. /* Hook the destructor so we can free slip devices at the right point in time */
  559. static void sl_free_netdev(struct net_device *dev)
  560. {
  561. int i = dev->base_addr;
  562. slip_devs[i] = NULL;
  563. }
  564. static const struct net_device_ops sl_netdev_ops = {
  565. .ndo_init = sl_init,
  566. .ndo_uninit = sl_uninit,
  567. .ndo_open = sl_open,
  568. .ndo_stop = sl_close,
  569. .ndo_start_xmit = sl_xmit,
  570. .ndo_get_stats64 = sl_get_stats64,
  571. .ndo_change_mtu = sl_change_mtu,
  572. .ndo_tx_timeout = sl_tx_timeout,
  573. #ifdef CONFIG_SLIP_SMART
  574. .ndo_do_ioctl = sl_ioctl,
  575. #endif
  576. };
  577. static void sl_setup(struct net_device *dev)
  578. {
  579. dev->netdev_ops = &sl_netdev_ops;
  580. dev->needs_free_netdev = true;
  581. dev->priv_destructor = sl_free_netdev;
  582. dev->hard_header_len = 0;
  583. dev->addr_len = 0;
  584. dev->tx_queue_len = 10;
  585. /* MTU range: 68 - 65534 */
  586. dev->min_mtu = 68;
  587. dev->max_mtu = 65534;
  588. /* New-style flags. */
  589. dev->flags = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
  590. }
  591. /******************************************
  592. Routines looking at TTY side.
  593. ******************************************/
  594. /*
  595. * Handle the 'receiver data ready' interrupt.
  596. * This function is called by the 'tty_io' module in the kernel when
  597. * a block of SLIP data has been received, which can now be decapsulated
  598. * and sent on to some IP layer for further processing. This will not
  599. * be re-entered while running but other ldisc functions may be called
  600. * in parallel
  601. */
  602. static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  603. char *fp, int count)
  604. {
  605. struct slip *sl = tty->disc_data;
  606. if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev))
  607. return;
  608. /* Read the characters out of the buffer */
  609. while (count--) {
  610. if (fp && *fp++) {
  611. if (!test_and_set_bit(SLF_ERROR, &sl->flags))
  612. sl->dev->stats.rx_errors++;
  613. cp++;
  614. continue;
  615. }
  616. #ifdef CONFIG_SLIP_MODE_SLIP6
  617. if (sl->mode & SL_MODE_SLIP6)
  618. slip_unesc6(sl, *cp++);
  619. else
  620. #endif
  621. slip_unesc(sl, *cp++);
  622. }
  623. }
  624. /************************************
  625. * slip_open helper routines.
  626. ************************************/
  627. /* Collect hanged up channels */
  628. static void sl_sync(void)
  629. {
  630. int i;
  631. struct net_device *dev;
  632. struct slip *sl;
  633. for (i = 0; i < slip_maxdev; i++) {
  634. dev = slip_devs[i];
  635. if (dev == NULL)
  636. break;
  637. sl = netdev_priv(dev);
  638. if (sl->tty || sl->leased)
  639. continue;
  640. if (dev->flags & IFF_UP)
  641. dev_close(dev);
  642. }
  643. }
  644. /* Find a free SLIP channel, and link in this `tty' line. */
  645. static struct slip *sl_alloc(void)
  646. {
  647. int i;
  648. char name[IFNAMSIZ];
  649. struct net_device *dev = NULL;
  650. struct slip *sl;
  651. for (i = 0; i < slip_maxdev; i++) {
  652. dev = slip_devs[i];
  653. if (dev == NULL)
  654. break;
  655. }
  656. /* Sorry, too many, all slots in use */
  657. if (i >= slip_maxdev)
  658. return NULL;
  659. sprintf(name, "sl%d", i);
  660. dev = alloc_netdev(sizeof(*sl), name, NET_NAME_UNKNOWN, sl_setup);
  661. if (!dev)
  662. return NULL;
  663. dev->base_addr = i;
  664. sl = netdev_priv(dev);
  665. /* Initialize channel control data */
  666. sl->magic = SLIP_MAGIC;
  667. sl->dev = dev;
  668. spin_lock_init(&sl->lock);
  669. INIT_WORK(&sl->tx_work, slip_transmit);
  670. sl->mode = SL_MODE_DEFAULT;
  671. #ifdef CONFIG_SLIP_SMART
  672. /* initialize timer_list struct */
  673. timer_setup(&sl->keepalive_timer, sl_keepalive, 0);
  674. timer_setup(&sl->outfill_timer, sl_outfill, 0);
  675. #endif
  676. slip_devs[i] = dev;
  677. return sl;
  678. }
  679. /*
  680. * Open the high-level part of the SLIP channel.
  681. * This function is called by the TTY module when the
  682. * SLIP line discipline is called for. Because we are
  683. * sure the tty line exists, we only have to link it to
  684. * a free SLIP channel...
  685. *
  686. * Called in process context serialized from other ldisc calls.
  687. */
  688. static int slip_open(struct tty_struct *tty)
  689. {
  690. struct slip *sl;
  691. int err;
  692. if (!capable(CAP_NET_ADMIN))
  693. return -EPERM;
  694. if (tty->ops->write == NULL)
  695. return -EOPNOTSUPP;
  696. /* RTnetlink lock is misused here to serialize concurrent
  697. opens of slip channels. There are better ways, but it is
  698. the simplest one.
  699. */
  700. rtnl_lock();
  701. /* Collect hanged up channels. */
  702. sl_sync();
  703. sl = tty->disc_data;
  704. err = -EEXIST;
  705. /* First make sure we're not already connected. */
  706. if (sl && sl->magic == SLIP_MAGIC)
  707. goto err_exit;
  708. /* OK. Find a free SLIP channel to use. */
  709. err = -ENFILE;
  710. sl = sl_alloc();
  711. if (sl == NULL)
  712. goto err_exit;
  713. sl->tty = tty;
  714. tty->disc_data = sl;
  715. sl->pid = current->pid;
  716. if (!test_bit(SLF_INUSE, &sl->flags)) {
  717. /* Perform the low-level SLIP initialization. */
  718. err = sl_alloc_bufs(sl, SL_MTU);
  719. if (err)
  720. goto err_free_chan;
  721. set_bit(SLF_INUSE, &sl->flags);
  722. err = register_netdevice(sl->dev);
  723. if (err)
  724. goto err_free_bufs;
  725. }
  726. #ifdef CONFIG_SLIP_SMART
  727. if (sl->keepalive) {
  728. sl->keepalive_timer.expires = jiffies + sl->keepalive * HZ;
  729. add_timer(&sl->keepalive_timer);
  730. }
  731. if (sl->outfill) {
  732. sl->outfill_timer.expires = jiffies + sl->outfill * HZ;
  733. add_timer(&sl->outfill_timer);
  734. }
  735. #endif
  736. /* Done. We have linked the TTY line to a channel. */
  737. rtnl_unlock();
  738. tty->receive_room = 65536; /* We don't flow control */
  739. /* TTY layer expects 0 on success */
  740. return 0;
  741. err_free_bufs:
  742. sl_free_bufs(sl);
  743. err_free_chan:
  744. sl->tty = NULL;
  745. tty->disc_data = NULL;
  746. clear_bit(SLF_INUSE, &sl->flags);
  747. sl_free_netdev(sl->dev);
  748. /* do not call free_netdev before rtnl_unlock */
  749. rtnl_unlock();
  750. free_netdev(sl->dev);
  751. return err;
  752. err_exit:
  753. rtnl_unlock();
  754. /* Count references from TTY module */
  755. return err;
  756. }
  757. /*
  758. * Close down a SLIP channel.
  759. * This means flushing out any pending queues, and then returning. This
  760. * call is serialized against other ldisc functions.
  761. *
  762. * We also use this method fo a hangup event
  763. */
  764. static void slip_close(struct tty_struct *tty)
  765. {
  766. struct slip *sl = tty->disc_data;
  767. /* First make sure we're connected. */
  768. if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
  769. return;
  770. spin_lock_bh(&sl->lock);
  771. rcu_assign_pointer(tty->disc_data, NULL);
  772. sl->tty = NULL;
  773. spin_unlock_bh(&sl->lock);
  774. synchronize_rcu();
  775. flush_work(&sl->tx_work);
  776. /* VSV = very important to remove timers */
  777. #ifdef CONFIG_SLIP_SMART
  778. del_timer_sync(&sl->keepalive_timer);
  779. del_timer_sync(&sl->outfill_timer);
  780. #endif
  781. /* Flush network side */
  782. unregister_netdev(sl->dev);
  783. /* This will complete via sl_free_netdev */
  784. }
  785. static int slip_hangup(struct tty_struct *tty)
  786. {
  787. slip_close(tty);
  788. return 0;
  789. }
  790. /************************************************************************
  791. * STANDARD SLIP ENCAPSULATION *
  792. ************************************************************************/
  793. static int slip_esc(unsigned char *s, unsigned char *d, int len)
  794. {
  795. unsigned char *ptr = d;
  796. unsigned char c;
  797. /*
  798. * Send an initial END character to flush out any
  799. * data that may have accumulated in the receiver
  800. * due to line noise.
  801. */
  802. *ptr++ = END;
  803. /*
  804. * For each byte in the packet, send the appropriate
  805. * character sequence, according to the SLIP protocol.
  806. */
  807. while (len-- > 0) {
  808. switch (c = *s++) {
  809. case END:
  810. *ptr++ = ESC;
  811. *ptr++ = ESC_END;
  812. break;
  813. case ESC:
  814. *ptr++ = ESC;
  815. *ptr++ = ESC_ESC;
  816. break;
  817. default:
  818. *ptr++ = c;
  819. break;
  820. }
  821. }
  822. *ptr++ = END;
  823. return ptr - d;
  824. }
  825. static void slip_unesc(struct slip *sl, unsigned char s)
  826. {
  827. switch (s) {
  828. case END:
  829. #ifdef CONFIG_SLIP_SMART
  830. /* drop keeptest bit = VSV */
  831. if (test_bit(SLF_KEEPTEST, &sl->flags))
  832. clear_bit(SLF_KEEPTEST, &sl->flags);
  833. #endif
  834. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  835. (sl->rcount > 2))
  836. sl_bump(sl);
  837. clear_bit(SLF_ESCAPE, &sl->flags);
  838. sl->rcount = 0;
  839. return;
  840. case ESC:
  841. set_bit(SLF_ESCAPE, &sl->flags);
  842. return;
  843. case ESC_ESC:
  844. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
  845. s = ESC;
  846. break;
  847. case ESC_END:
  848. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
  849. s = END;
  850. break;
  851. }
  852. if (!test_bit(SLF_ERROR, &sl->flags)) {
  853. if (sl->rcount < sl->buffsize) {
  854. sl->rbuff[sl->rcount++] = s;
  855. return;
  856. }
  857. sl->dev->stats.rx_over_errors++;
  858. set_bit(SLF_ERROR, &sl->flags);
  859. }
  860. }
  861. #ifdef CONFIG_SLIP_MODE_SLIP6
  862. /************************************************************************
  863. * 6 BIT SLIP ENCAPSULATION *
  864. ************************************************************************/
  865. static int slip_esc6(unsigned char *s, unsigned char *d, int len)
  866. {
  867. unsigned char *ptr = d;
  868. unsigned char c;
  869. int i;
  870. unsigned short v = 0;
  871. short bits = 0;
  872. /*
  873. * Send an initial END character to flush out any
  874. * data that may have accumulated in the receiver
  875. * due to line noise.
  876. */
  877. *ptr++ = 0x70;
  878. /*
  879. * Encode the packet into printable ascii characters
  880. */
  881. for (i = 0; i < len; ++i) {
  882. v = (v << 8) | s[i];
  883. bits += 8;
  884. while (bits >= 6) {
  885. bits -= 6;
  886. c = 0x30 + ((v >> bits) & 0x3F);
  887. *ptr++ = c;
  888. }
  889. }
  890. if (bits) {
  891. c = 0x30 + ((v << (6 - bits)) & 0x3F);
  892. *ptr++ = c;
  893. }
  894. *ptr++ = 0x70;
  895. return ptr - d;
  896. }
  897. static void slip_unesc6(struct slip *sl, unsigned char s)
  898. {
  899. unsigned char c;
  900. if (s == 0x70) {
  901. #ifdef CONFIG_SLIP_SMART
  902. /* drop keeptest bit = VSV */
  903. if (test_bit(SLF_KEEPTEST, &sl->flags))
  904. clear_bit(SLF_KEEPTEST, &sl->flags);
  905. #endif
  906. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  907. (sl->rcount > 2))
  908. sl_bump(sl);
  909. sl->rcount = 0;
  910. sl->xbits = 0;
  911. sl->xdata = 0;
  912. } else if (s >= 0x30 && s < 0x70) {
  913. sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
  914. sl->xbits += 6;
  915. if (sl->xbits >= 8) {
  916. sl->xbits -= 8;
  917. c = (unsigned char)(sl->xdata >> sl->xbits);
  918. if (!test_bit(SLF_ERROR, &sl->flags)) {
  919. if (sl->rcount < sl->buffsize) {
  920. sl->rbuff[sl->rcount++] = c;
  921. return;
  922. }
  923. sl->dev->stats.rx_over_errors++;
  924. set_bit(SLF_ERROR, &sl->flags);
  925. }
  926. }
  927. }
  928. }
  929. #endif /* CONFIG_SLIP_MODE_SLIP6 */
  930. /* Perform I/O control on an active SLIP channel. */
  931. static int slip_ioctl(struct tty_struct *tty, struct file *file,
  932. unsigned int cmd, unsigned long arg)
  933. {
  934. struct slip *sl = tty->disc_data;
  935. unsigned int tmp;
  936. int __user *p = (int __user *)arg;
  937. /* First make sure we're connected. */
  938. if (!sl || sl->magic != SLIP_MAGIC)
  939. return -EINVAL;
  940. switch (cmd) {
  941. case SIOCGIFNAME:
  942. tmp = strlen(sl->dev->name) + 1;
  943. if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
  944. return -EFAULT;
  945. return 0;
  946. case SIOCGIFENCAP:
  947. if (put_user(sl->mode, p))
  948. return -EFAULT;
  949. return 0;
  950. case SIOCSIFENCAP:
  951. if (get_user(tmp, p))
  952. return -EFAULT;
  953. #ifndef SL_INCLUDE_CSLIP
  954. if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE))
  955. return -EINVAL;
  956. #else
  957. if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
  958. (SL_MODE_ADAPTIVE | SL_MODE_CSLIP))
  959. /* return -EINVAL; */
  960. tmp &= ~SL_MODE_ADAPTIVE;
  961. #endif
  962. #ifndef CONFIG_SLIP_MODE_SLIP6
  963. if (tmp & SL_MODE_SLIP6)
  964. return -EINVAL;
  965. #endif
  966. sl->mode = tmp;
  967. sl->dev->type = ARPHRD_SLIP + sl->mode;
  968. return 0;
  969. case SIOCSIFHWADDR:
  970. return -EINVAL;
  971. #ifdef CONFIG_SLIP_SMART
  972. /* VSV changes start here */
  973. case SIOCSKEEPALIVE:
  974. if (get_user(tmp, p))
  975. return -EFAULT;
  976. if (tmp > 255) /* max for unchar */
  977. return -EINVAL;
  978. spin_lock_bh(&sl->lock);
  979. if (!sl->tty) {
  980. spin_unlock_bh(&sl->lock);
  981. return -ENODEV;
  982. }
  983. sl->keepalive = (u8)tmp;
  984. if (sl->keepalive != 0) {
  985. mod_timer(&sl->keepalive_timer,
  986. jiffies + sl->keepalive * HZ);
  987. set_bit(SLF_KEEPTEST, &sl->flags);
  988. } else
  989. del_timer(&sl->keepalive_timer);
  990. spin_unlock_bh(&sl->lock);
  991. return 0;
  992. case SIOCGKEEPALIVE:
  993. if (put_user(sl->keepalive, p))
  994. return -EFAULT;
  995. return 0;
  996. case SIOCSOUTFILL:
  997. if (get_user(tmp, p))
  998. return -EFAULT;
  999. if (tmp > 255) /* max for unchar */
  1000. return -EINVAL;
  1001. spin_lock_bh(&sl->lock);
  1002. if (!sl->tty) {
  1003. spin_unlock_bh(&sl->lock);
  1004. return -ENODEV;
  1005. }
  1006. sl->outfill = (u8)tmp;
  1007. if (sl->outfill != 0) {
  1008. mod_timer(&sl->outfill_timer,
  1009. jiffies + sl->outfill * HZ);
  1010. set_bit(SLF_OUTWAIT, &sl->flags);
  1011. } else
  1012. del_timer(&sl->outfill_timer);
  1013. spin_unlock_bh(&sl->lock);
  1014. return 0;
  1015. case SIOCGOUTFILL:
  1016. if (put_user(sl->outfill, p))
  1017. return -EFAULT;
  1018. return 0;
  1019. /* VSV changes end */
  1020. #endif
  1021. default:
  1022. return tty_mode_ioctl(tty, file, cmd, arg);
  1023. }
  1024. }
  1025. #ifdef CONFIG_COMPAT
  1026. static long slip_compat_ioctl(struct tty_struct *tty, struct file *file,
  1027. unsigned int cmd, unsigned long arg)
  1028. {
  1029. switch (cmd) {
  1030. case SIOCGIFNAME:
  1031. case SIOCGIFENCAP:
  1032. case SIOCSIFENCAP:
  1033. case SIOCSIFHWADDR:
  1034. case SIOCSKEEPALIVE:
  1035. case SIOCGKEEPALIVE:
  1036. case SIOCSOUTFILL:
  1037. case SIOCGOUTFILL:
  1038. return slip_ioctl(tty, file, cmd,
  1039. (unsigned long)compat_ptr(arg));
  1040. }
  1041. return -ENOIOCTLCMD;
  1042. }
  1043. #endif
  1044. /* VSV changes start here */
  1045. #ifdef CONFIG_SLIP_SMART
  1046. /* function do_ioctl called from net/core/dev.c
  1047. to allow get/set outfill/keepalive parameter
  1048. by ifconfig */
  1049. static int sl_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1050. {
  1051. struct slip *sl = netdev_priv(dev);
  1052. unsigned long *p = (unsigned long *)&rq->ifr_ifru;
  1053. if (sl == NULL) /* Allocation failed ?? */
  1054. return -ENODEV;
  1055. spin_lock_bh(&sl->lock);
  1056. if (!sl->tty) {
  1057. spin_unlock_bh(&sl->lock);
  1058. return -ENODEV;
  1059. }
  1060. switch (cmd) {
  1061. case SIOCSKEEPALIVE:
  1062. /* max for unchar */
  1063. if ((unsigned)*p > 255) {
  1064. spin_unlock_bh(&sl->lock);
  1065. return -EINVAL;
  1066. }
  1067. sl->keepalive = (u8)*p;
  1068. if (sl->keepalive != 0) {
  1069. sl->keepalive_timer.expires =
  1070. jiffies + sl->keepalive * HZ;
  1071. mod_timer(&sl->keepalive_timer,
  1072. jiffies + sl->keepalive * HZ);
  1073. set_bit(SLF_KEEPTEST, &sl->flags);
  1074. } else
  1075. del_timer(&sl->keepalive_timer);
  1076. break;
  1077. case SIOCGKEEPALIVE:
  1078. *p = sl->keepalive;
  1079. break;
  1080. case SIOCSOUTFILL:
  1081. if ((unsigned)*p > 255) { /* max for unchar */
  1082. spin_unlock_bh(&sl->lock);
  1083. return -EINVAL;
  1084. }
  1085. sl->outfill = (u8)*p;
  1086. if (sl->outfill != 0) {
  1087. mod_timer(&sl->outfill_timer,
  1088. jiffies + sl->outfill * HZ);
  1089. set_bit(SLF_OUTWAIT, &sl->flags);
  1090. } else
  1091. del_timer(&sl->outfill_timer);
  1092. break;
  1093. case SIOCGOUTFILL:
  1094. *p = sl->outfill;
  1095. break;
  1096. case SIOCSLEASE:
  1097. /* Resolve race condition, when ioctl'ing hanged up
  1098. and opened by another process device.
  1099. */
  1100. if (sl->tty != current->signal->tty &&
  1101. sl->pid != current->pid) {
  1102. spin_unlock_bh(&sl->lock);
  1103. return -EPERM;
  1104. }
  1105. sl->leased = 0;
  1106. if (*p)
  1107. sl->leased = 1;
  1108. break;
  1109. case SIOCGLEASE:
  1110. *p = sl->leased;
  1111. }
  1112. spin_unlock_bh(&sl->lock);
  1113. return 0;
  1114. }
  1115. #endif
  1116. /* VSV changes end */
  1117. static struct tty_ldisc_ops sl_ldisc = {
  1118. .owner = THIS_MODULE,
  1119. .magic = TTY_LDISC_MAGIC,
  1120. .name = "slip",
  1121. .open = slip_open,
  1122. .close = slip_close,
  1123. .hangup = slip_hangup,
  1124. .ioctl = slip_ioctl,
  1125. #ifdef CONFIG_COMPAT
  1126. .compat_ioctl = slip_compat_ioctl,
  1127. #endif
  1128. .receive_buf = slip_receive_buf,
  1129. .write_wakeup = slip_write_wakeup,
  1130. };
  1131. static int __init slip_init(void)
  1132. {
  1133. int status;
  1134. if (slip_maxdev < 4)
  1135. slip_maxdev = 4; /* Sanity */
  1136. printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
  1137. #ifdef CONFIG_SLIP_MODE_SLIP6
  1138. " (6 bit encapsulation enabled)"
  1139. #endif
  1140. ".\n",
  1141. SLIP_VERSION, slip_maxdev);
  1142. #if defined(SL_INCLUDE_CSLIP)
  1143. printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
  1144. #endif
  1145. #ifdef CONFIG_SLIP_SMART
  1146. printk(KERN_INFO "SLIP linefill/keepalive option.\n");
  1147. #endif
  1148. slip_devs = kcalloc(slip_maxdev, sizeof(struct net_device *),
  1149. GFP_KERNEL);
  1150. if (!slip_devs)
  1151. return -ENOMEM;
  1152. /* Fill in our line protocol discipline, and register it */
  1153. status = tty_register_ldisc(N_SLIP, &sl_ldisc);
  1154. if (status != 0) {
  1155. printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
  1156. kfree(slip_devs);
  1157. }
  1158. return status;
  1159. }
  1160. static void __exit slip_exit(void)
  1161. {
  1162. int i;
  1163. struct net_device *dev;
  1164. struct slip *sl;
  1165. unsigned long timeout = jiffies + HZ;
  1166. int busy = 0;
  1167. if (slip_devs == NULL)
  1168. return;
  1169. /* First of all: check for active disciplines and hangup them.
  1170. */
  1171. do {
  1172. if (busy)
  1173. msleep_interruptible(100);
  1174. busy = 0;
  1175. for (i = 0; i < slip_maxdev; i++) {
  1176. dev = slip_devs[i];
  1177. if (!dev)
  1178. continue;
  1179. sl = netdev_priv(dev);
  1180. spin_lock_bh(&sl->lock);
  1181. if (sl->tty) {
  1182. busy++;
  1183. tty_hangup(sl->tty);
  1184. }
  1185. spin_unlock_bh(&sl->lock);
  1186. }
  1187. } while (busy && time_before(jiffies, timeout));
  1188. /* FIXME: hangup is async so we should wait when doing this second
  1189. phase */
  1190. for (i = 0; i < slip_maxdev; i++) {
  1191. dev = slip_devs[i];
  1192. if (!dev)
  1193. continue;
  1194. slip_devs[i] = NULL;
  1195. sl = netdev_priv(dev);
  1196. if (sl->tty) {
  1197. printk(KERN_ERR "%s: tty discipline still running\n",
  1198. dev->name);
  1199. }
  1200. unregister_netdev(dev);
  1201. }
  1202. kfree(slip_devs);
  1203. slip_devs = NULL;
  1204. i = tty_unregister_ldisc(N_SLIP);
  1205. if (i != 0)
  1206. printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
  1207. }
  1208. module_init(slip_init);
  1209. module_exit(slip_exit);
  1210. #ifdef CONFIG_SLIP_SMART
  1211. /*
  1212. * This is start of the code for multislip style line checking
  1213. * added by Stanislav Voronyi. All changes before marked VSV
  1214. */
  1215. static void sl_outfill(struct timer_list *t)
  1216. {
  1217. struct slip *sl = from_timer(sl, t, outfill_timer);
  1218. spin_lock(&sl->lock);
  1219. if (sl->tty == NULL)
  1220. goto out;
  1221. if (sl->outfill) {
  1222. if (test_bit(SLF_OUTWAIT, &sl->flags)) {
  1223. /* no packets were transmitted, do outfill */
  1224. #ifdef CONFIG_SLIP_MODE_SLIP6
  1225. unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
  1226. #else
  1227. unsigned char s = END;
  1228. #endif
  1229. /* put END into tty queue. Is it right ??? */
  1230. if (!netif_queue_stopped(sl->dev)) {
  1231. /* if device busy no outfill */
  1232. sl->tty->ops->write(sl->tty, &s, 1);
  1233. }
  1234. } else
  1235. set_bit(SLF_OUTWAIT, &sl->flags);
  1236. mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
  1237. }
  1238. out:
  1239. spin_unlock(&sl->lock);
  1240. }
  1241. static void sl_keepalive(struct timer_list *t)
  1242. {
  1243. struct slip *sl = from_timer(sl, t, keepalive_timer);
  1244. spin_lock(&sl->lock);
  1245. if (sl->tty == NULL)
  1246. goto out;
  1247. if (sl->keepalive) {
  1248. if (test_bit(SLF_KEEPTEST, &sl->flags)) {
  1249. /* keepalive still high :(, we must hangup */
  1250. if (sl->outfill)
  1251. /* outfill timer must be deleted too */
  1252. (void)del_timer(&sl->outfill_timer);
  1253. printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
  1254. /* this must hangup tty & close slip */
  1255. tty_hangup(sl->tty);
  1256. /* I think we need not something else */
  1257. goto out;
  1258. } else
  1259. set_bit(SLF_KEEPTEST, &sl->flags);
  1260. mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
  1261. }
  1262. out:
  1263. spin_unlock(&sl->lock);
  1264. }
  1265. #endif
  1266. MODULE_LICENSE("GPL");
  1267. MODULE_ALIAS_LDISC(N_SLIP);