slip.c 33 KB

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