slip.c 33 KB

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