slip.c 33 KB

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