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