slip.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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. #ifdef CONFIG_SLIP_MODE_SLIP6
  175. sl->xdata = 0;
  176. sl->xbits = 0;
  177. #endif
  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. if (slcomp)
  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. memcpy(skb_put(skb, count), 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, (unsigned char *) sl->xbuff, len);
  361. else
  362. #endif
  363. count = slip_esc(p, (unsigned char *) 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. sl->dev->trans_start = jiffies;
  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. /*
  385. * Called by the driver when there's room for more data. If we have
  386. * more packets to send, we send them here.
  387. */
  388. static void slip_write_wakeup(struct tty_struct *tty)
  389. {
  390. int actual;
  391. struct slip *sl = tty->disc_data;
  392. /* First make sure we're connected. */
  393. if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev))
  394. return;
  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, &tty->flags);
  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. }
  407. static void sl_tx_timeout(struct net_device *dev)
  408. {
  409. struct slip *sl = netdev_priv(dev);
  410. spin_lock(&sl->lock);
  411. if (netif_queue_stopped(dev)) {
  412. if (!netif_running(dev))
  413. goto out;
  414. /* May be we must check transmitter timeout here ?
  415. * 14 Oct 1994 Dmitry Gorodchanin.
  416. */
  417. #ifdef SL_CHECK_TRANSMIT
  418. if (time_before(jiffies, dev_trans_start(dev) + 20 * HZ)) {
  419. /* 20 sec timeout not reached */
  420. goto out;
  421. }
  422. printk(KERN_WARNING "%s: transmit timed out, %s?\n",
  423. dev->name,
  424. (tty_chars_in_buffer(sl->tty) || sl->xleft) ?
  425. "bad line quality" : "driver error");
  426. sl->xleft = 0;
  427. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  428. sl_unlock(sl);
  429. #endif
  430. }
  431. out:
  432. spin_unlock(&sl->lock);
  433. }
  434. /* Encapsulate an IP datagram and kick it into a TTY queue. */
  435. static netdev_tx_t
  436. sl_xmit(struct sk_buff *skb, struct net_device *dev)
  437. {
  438. struct slip *sl = netdev_priv(dev);
  439. spin_lock(&sl->lock);
  440. if (!netif_running(dev)) {
  441. spin_unlock(&sl->lock);
  442. printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name);
  443. dev_kfree_skb(skb);
  444. return NETDEV_TX_OK;
  445. }
  446. if (sl->tty == NULL) {
  447. spin_unlock(&sl->lock);
  448. dev_kfree_skb(skb);
  449. return NETDEV_TX_OK;
  450. }
  451. sl_lock(sl);
  452. dev->stats.tx_bytes += skb->len;
  453. sl_encaps(sl, skb->data, skb->len);
  454. spin_unlock(&sl->lock);
  455. dev_kfree_skb(skb);
  456. return NETDEV_TX_OK;
  457. }
  458. /******************************************
  459. * Routines looking at netdevice side.
  460. ******************************************/
  461. /* Netdevice UP -> DOWN routine */
  462. static int
  463. sl_close(struct net_device *dev)
  464. {
  465. struct slip *sl = netdev_priv(dev);
  466. spin_lock_bh(&sl->lock);
  467. if (sl->tty)
  468. /* TTY discipline is running. */
  469. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  470. netif_stop_queue(dev);
  471. sl->rcount = 0;
  472. sl->xleft = 0;
  473. spin_unlock_bh(&sl->lock);
  474. return 0;
  475. }
  476. /* Netdevice DOWN -> UP routine */
  477. static int sl_open(struct net_device *dev)
  478. {
  479. struct slip *sl = netdev_priv(dev);
  480. if (sl->tty == NULL)
  481. return -ENODEV;
  482. sl->flags &= (1 << SLF_INUSE);
  483. netif_start_queue(dev);
  484. return 0;
  485. }
  486. /* Netdevice change MTU request */
  487. static int sl_change_mtu(struct net_device *dev, int new_mtu)
  488. {
  489. struct slip *sl = netdev_priv(dev);
  490. if (new_mtu < 68 || new_mtu > 65534)
  491. return -EINVAL;
  492. if (new_mtu != dev->mtu)
  493. return sl_realloc_bufs(sl, new_mtu);
  494. return 0;
  495. }
  496. /* Netdevice get statistics request */
  497. static struct rtnl_link_stats64 *
  498. sl_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  499. {
  500. struct net_device_stats *devstats = &dev->stats;
  501. unsigned long c_rx_dropped = 0;
  502. #ifdef SL_INCLUDE_CSLIP
  503. unsigned long c_rx_fifo_errors = 0;
  504. unsigned long c_tx_fifo_errors = 0;
  505. unsigned long c_collisions = 0;
  506. struct slip *sl = netdev_priv(dev);
  507. struct slcompress *comp = sl->slcomp;
  508. if (comp) {
  509. c_rx_fifo_errors = comp->sls_i_compressed;
  510. c_rx_dropped = comp->sls_i_tossed;
  511. c_tx_fifo_errors = comp->sls_o_compressed;
  512. c_collisions = comp->sls_o_misses;
  513. }
  514. stats->rx_fifo_errors = sl->rx_compressed + c_rx_fifo_errors;
  515. stats->tx_fifo_errors = sl->tx_compressed + c_tx_fifo_errors;
  516. stats->collisions = sl->tx_misses + c_collisions;
  517. #endif
  518. stats->rx_packets = devstats->rx_packets;
  519. stats->tx_packets = devstats->tx_packets;
  520. stats->rx_bytes = devstats->rx_bytes;
  521. stats->tx_bytes = devstats->tx_bytes;
  522. stats->rx_dropped = devstats->rx_dropped + c_rx_dropped;
  523. stats->tx_dropped = devstats->tx_dropped;
  524. stats->tx_errors = devstats->tx_errors;
  525. stats->rx_errors = devstats->rx_errors;
  526. stats->rx_over_errors = devstats->rx_over_errors;
  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. struct net_device *dev = NULL;
  636. struct slip *sl;
  637. if (slip_devs == NULL)
  638. return NULL; /* Master array missing ! */
  639. for (i = 0; i < slip_maxdev; i++) {
  640. dev = slip_devs[i];
  641. if (dev == NULL)
  642. break;
  643. }
  644. /* Sorry, too many, all slots in use */
  645. if (i >= slip_maxdev)
  646. return NULL;
  647. if (dev) {
  648. sl = netdev_priv(dev);
  649. if (test_bit(SLF_INUSE, &sl->flags)) {
  650. unregister_netdevice(dev);
  651. dev = NULL;
  652. slip_devs[i] = NULL;
  653. }
  654. }
  655. if (!dev) {
  656. char name[IFNAMSIZ];
  657. sprintf(name, "sl%d", i);
  658. dev = alloc_netdev(sizeof(*sl), name, sl_setup);
  659. if (!dev)
  660. return NULL;
  661. dev->base_addr = i;
  662. }
  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. sl->mode = SL_MODE_DEFAULT;
  669. #ifdef CONFIG_SLIP_SMART
  670. /* initialize timer_list struct */
  671. init_timer(&sl->keepalive_timer);
  672. sl->keepalive_timer.data = (unsigned long)sl;
  673. sl->keepalive_timer.function = sl_keepalive;
  674. init_timer(&sl->outfill_timer);
  675. sl->outfill_timer.data = (unsigned long)sl;
  676. sl->outfill_timer.function = sl_outfill;
  677. #endif
  678. slip_devs[i] = dev;
  679. return sl;
  680. }
  681. /*
  682. * Open the high-level part of the SLIP channel.
  683. * This function is called by the TTY module when the
  684. * SLIP line discipline is called for. Because we are
  685. * sure the tty line exists, we only have to link it to
  686. * a free SLIP channel...
  687. *
  688. * Called in process context serialized from other ldisc calls.
  689. */
  690. static int slip_open(struct tty_struct *tty)
  691. {
  692. struct slip *sl;
  693. int err;
  694. if (!capable(CAP_NET_ADMIN))
  695. return -EPERM;
  696. if (tty->ops->write == NULL)
  697. return -EOPNOTSUPP;
  698. /* RTnetlink lock is misused here to serialize concurrent
  699. opens of slip channels. There are better ways, but it is
  700. the simplest one.
  701. */
  702. rtnl_lock();
  703. /* Collect hanged up channels. */
  704. sl_sync();
  705. sl = tty->disc_data;
  706. err = -EEXIST;
  707. /* First make sure we're not already connected. */
  708. if (sl && sl->magic == SLIP_MAGIC)
  709. goto err_exit;
  710. /* OK. Find a free SLIP channel to use. */
  711. err = -ENFILE;
  712. sl = sl_alloc(tty_devnum(tty));
  713. if (sl == NULL)
  714. goto err_exit;
  715. sl->tty = tty;
  716. tty->disc_data = sl;
  717. sl->line = tty_devnum(tty);
  718. sl->pid = current->pid;
  719. if (!test_bit(SLF_INUSE, &sl->flags)) {
  720. /* Perform the low-level SLIP initialization. */
  721. err = sl_alloc_bufs(sl, SL_MTU);
  722. if (err)
  723. goto err_free_chan;
  724. set_bit(SLF_INUSE, &sl->flags);
  725. err = register_netdevice(sl->dev);
  726. if (err)
  727. goto err_free_bufs;
  728. }
  729. #ifdef CONFIG_SLIP_SMART
  730. if (sl->keepalive) {
  731. sl->keepalive_timer.expires = jiffies + sl->keepalive * HZ;
  732. add_timer(&sl->keepalive_timer);
  733. }
  734. if (sl->outfill) {
  735. sl->outfill_timer.expires = jiffies + sl->outfill * HZ;
  736. add_timer(&sl->outfill_timer);
  737. }
  738. #endif
  739. /* Done. We have linked the TTY line to a channel. */
  740. rtnl_unlock();
  741. tty->receive_room = 65536; /* We don't flow control */
  742. return sl->dev->base_addr;
  743. err_free_bufs:
  744. sl_free_bufs(sl);
  745. err_free_chan:
  746. sl->tty = NULL;
  747. tty->disc_data = NULL;
  748. clear_bit(SLF_INUSE, &sl->flags);
  749. err_exit:
  750. rtnl_unlock();
  751. /* Count references from TTY module */
  752. return err;
  753. }
  754. /*
  755. * Close down a SLIP channel.
  756. * This means flushing out any pending queues, and then returning. This
  757. * call is serialized against other ldisc functions.
  758. *
  759. * We also use this method fo a hangup event
  760. */
  761. static void slip_close(struct tty_struct *tty)
  762. {
  763. struct slip *sl = tty->disc_data;
  764. /* First make sure we're connected. */
  765. if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
  766. return;
  767. tty->disc_data = NULL;
  768. sl->tty = NULL;
  769. if (!sl->leased)
  770. sl->line = 0;
  771. /* VSV = very important to remove timers */
  772. #ifdef CONFIG_SLIP_SMART
  773. del_timer_sync(&sl->keepalive_timer);
  774. del_timer_sync(&sl->outfill_timer);
  775. #endif
  776. /* Flush network side */
  777. unregister_netdev(sl->dev);
  778. /* This will complete via sl_free_netdev */
  779. }
  780. static int slip_hangup(struct tty_struct *tty)
  781. {
  782. slip_close(tty);
  783. return 0;
  784. }
  785. /************************************************************************
  786. * STANDARD SLIP ENCAPSULATION *
  787. ************************************************************************/
  788. static int slip_esc(unsigned char *s, unsigned char *d, int len)
  789. {
  790. unsigned char *ptr = d;
  791. unsigned char c;
  792. /*
  793. * Send an initial END character to flush out any
  794. * data that may have accumulated in the receiver
  795. * due to line noise.
  796. */
  797. *ptr++ = END;
  798. /*
  799. * For each byte in the packet, send the appropriate
  800. * character sequence, according to the SLIP protocol.
  801. */
  802. while (len-- > 0) {
  803. switch (c = *s++) {
  804. case END:
  805. *ptr++ = ESC;
  806. *ptr++ = ESC_END;
  807. break;
  808. case ESC:
  809. *ptr++ = ESC;
  810. *ptr++ = ESC_ESC;
  811. break;
  812. default:
  813. *ptr++ = c;
  814. break;
  815. }
  816. }
  817. *ptr++ = END;
  818. return ptr - d;
  819. }
  820. static void slip_unesc(struct slip *sl, unsigned char s)
  821. {
  822. switch (s) {
  823. case END:
  824. #ifdef CONFIG_SLIP_SMART
  825. /* drop keeptest bit = VSV */
  826. if (test_bit(SLF_KEEPTEST, &sl->flags))
  827. clear_bit(SLF_KEEPTEST, &sl->flags);
  828. #endif
  829. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  830. (sl->rcount > 2))
  831. sl_bump(sl);
  832. clear_bit(SLF_ESCAPE, &sl->flags);
  833. sl->rcount = 0;
  834. return;
  835. case ESC:
  836. set_bit(SLF_ESCAPE, &sl->flags);
  837. return;
  838. case ESC_ESC:
  839. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
  840. s = ESC;
  841. break;
  842. case ESC_END:
  843. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
  844. s = END;
  845. break;
  846. }
  847. if (!test_bit(SLF_ERROR, &sl->flags)) {
  848. if (sl->rcount < sl->buffsize) {
  849. sl->rbuff[sl->rcount++] = s;
  850. return;
  851. }
  852. sl->dev->stats.rx_over_errors++;
  853. set_bit(SLF_ERROR, &sl->flags);
  854. }
  855. }
  856. #ifdef CONFIG_SLIP_MODE_SLIP6
  857. /************************************************************************
  858. * 6 BIT SLIP ENCAPSULATION *
  859. ************************************************************************/
  860. static int slip_esc6(unsigned char *s, unsigned char *d, int len)
  861. {
  862. unsigned char *ptr = d;
  863. unsigned char c;
  864. int i;
  865. unsigned short v = 0;
  866. short bits = 0;
  867. /*
  868. * Send an initial END character to flush out any
  869. * data that may have accumulated in the receiver
  870. * due to line noise.
  871. */
  872. *ptr++ = 0x70;
  873. /*
  874. * Encode the packet into printable ascii characters
  875. */
  876. for (i = 0; i < len; ++i) {
  877. v = (v << 8) | s[i];
  878. bits += 8;
  879. while (bits >= 6) {
  880. bits -= 6;
  881. c = 0x30 + ((v >> bits) & 0x3F);
  882. *ptr++ = c;
  883. }
  884. }
  885. if (bits) {
  886. c = 0x30 + ((v << (6 - bits)) & 0x3F);
  887. *ptr++ = c;
  888. }
  889. *ptr++ = 0x70;
  890. return ptr - d;
  891. }
  892. static void slip_unesc6(struct slip *sl, unsigned char s)
  893. {
  894. unsigned char c;
  895. if (s == 0x70) {
  896. #ifdef CONFIG_SLIP_SMART
  897. /* drop keeptest bit = VSV */
  898. if (test_bit(SLF_KEEPTEST, &sl->flags))
  899. clear_bit(SLF_KEEPTEST, &sl->flags);
  900. #endif
  901. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  902. (sl->rcount > 2))
  903. sl_bump(sl);
  904. sl->rcount = 0;
  905. sl->xbits = 0;
  906. sl->xdata = 0;
  907. } else if (s >= 0x30 && s < 0x70) {
  908. sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
  909. sl->xbits += 6;
  910. if (sl->xbits >= 8) {
  911. sl->xbits -= 8;
  912. c = (unsigned char)(sl->xdata >> sl->xbits);
  913. if (!test_bit(SLF_ERROR, &sl->flags)) {
  914. if (sl->rcount < sl->buffsize) {
  915. sl->rbuff[sl->rcount++] = c;
  916. return;
  917. }
  918. sl->dev->stats.rx_over_errors++;
  919. set_bit(SLF_ERROR, &sl->flags);
  920. }
  921. }
  922. }
  923. }
  924. #endif /* CONFIG_SLIP_MODE_SLIP6 */
  925. /* Perform I/O control on an active SLIP channel. */
  926. static int slip_ioctl(struct tty_struct *tty, struct file *file,
  927. unsigned int cmd, unsigned long arg)
  928. {
  929. struct slip *sl = tty->disc_data;
  930. unsigned int tmp;
  931. int __user *p = (int __user *)arg;
  932. /* First make sure we're connected. */
  933. if (!sl || sl->magic != SLIP_MAGIC)
  934. return -EINVAL;
  935. switch (cmd) {
  936. case SIOCGIFNAME:
  937. tmp = strlen(sl->dev->name) + 1;
  938. if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
  939. return -EFAULT;
  940. return 0;
  941. case SIOCGIFENCAP:
  942. if (put_user(sl->mode, p))
  943. return -EFAULT;
  944. return 0;
  945. case SIOCSIFENCAP:
  946. if (get_user(tmp, p))
  947. return -EFAULT;
  948. #ifndef SL_INCLUDE_CSLIP
  949. if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE))
  950. return -EINVAL;
  951. #else
  952. if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
  953. (SL_MODE_ADAPTIVE | SL_MODE_CSLIP))
  954. /* return -EINVAL; */
  955. tmp &= ~SL_MODE_ADAPTIVE;
  956. #endif
  957. #ifndef CONFIG_SLIP_MODE_SLIP6
  958. if (tmp & SL_MODE_SLIP6)
  959. return -EINVAL;
  960. #endif
  961. sl->mode = tmp;
  962. sl->dev->type = ARPHRD_SLIP + sl->mode;
  963. return 0;
  964. case SIOCSIFHWADDR:
  965. return -EINVAL;
  966. #ifdef CONFIG_SLIP_SMART
  967. /* VSV changes start here */
  968. case SIOCSKEEPALIVE:
  969. if (get_user(tmp, p))
  970. return -EFAULT;
  971. if (tmp > 255) /* max for unchar */
  972. return -EINVAL;
  973. spin_lock_bh(&sl->lock);
  974. if (!sl->tty) {
  975. spin_unlock_bh(&sl->lock);
  976. return -ENODEV;
  977. }
  978. sl->keepalive = (u8)tmp;
  979. if (sl->keepalive != 0) {
  980. mod_timer(&sl->keepalive_timer,
  981. jiffies + sl->keepalive * HZ);
  982. set_bit(SLF_KEEPTEST, &sl->flags);
  983. } else
  984. del_timer(&sl->keepalive_timer);
  985. spin_unlock_bh(&sl->lock);
  986. return 0;
  987. case SIOCGKEEPALIVE:
  988. if (put_user(sl->keepalive, p))
  989. return -EFAULT;
  990. return 0;
  991. case SIOCSOUTFILL:
  992. if (get_user(tmp, p))
  993. return -EFAULT;
  994. if (tmp > 255) /* max for unchar */
  995. return -EINVAL;
  996. spin_lock_bh(&sl->lock);
  997. if (!sl->tty) {
  998. spin_unlock_bh(&sl->lock);
  999. return -ENODEV;
  1000. }
  1001. sl->outfill = (u8)tmp;
  1002. if (sl->outfill != 0) {
  1003. mod_timer(&sl->outfill_timer,
  1004. jiffies + sl->outfill * HZ);
  1005. set_bit(SLF_OUTWAIT, &sl->flags);
  1006. } else
  1007. del_timer(&sl->outfill_timer);
  1008. spin_unlock_bh(&sl->lock);
  1009. return 0;
  1010. case SIOCGOUTFILL:
  1011. if (put_user(sl->outfill, p))
  1012. return -EFAULT;
  1013. return 0;
  1014. /* VSV changes end */
  1015. #endif
  1016. default:
  1017. return tty_mode_ioctl(tty, file, cmd, arg);
  1018. }
  1019. }
  1020. #ifdef CONFIG_COMPAT
  1021. static long slip_compat_ioctl(struct tty_struct *tty, struct file *file,
  1022. unsigned int cmd, unsigned long arg)
  1023. {
  1024. switch (cmd) {
  1025. case SIOCGIFNAME:
  1026. case SIOCGIFENCAP:
  1027. case SIOCSIFENCAP:
  1028. case SIOCSIFHWADDR:
  1029. case SIOCSKEEPALIVE:
  1030. case SIOCGKEEPALIVE:
  1031. case SIOCSOUTFILL:
  1032. case SIOCGOUTFILL:
  1033. return slip_ioctl(tty, file, cmd,
  1034. (unsigned long)compat_ptr(arg));
  1035. }
  1036. return -ENOIOCTLCMD;
  1037. }
  1038. #endif
  1039. /* VSV changes start here */
  1040. #ifdef CONFIG_SLIP_SMART
  1041. /* function do_ioctl called from net/core/dev.c
  1042. to allow get/set outfill/keepalive parameter
  1043. by ifconfig */
  1044. static int sl_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1045. {
  1046. struct slip *sl = netdev_priv(dev);
  1047. unsigned long *p = (unsigned long *)&rq->ifr_ifru;
  1048. if (sl == NULL) /* Allocation failed ?? */
  1049. return -ENODEV;
  1050. spin_lock_bh(&sl->lock);
  1051. if (!sl->tty) {
  1052. spin_unlock_bh(&sl->lock);
  1053. return -ENODEV;
  1054. }
  1055. switch (cmd) {
  1056. case SIOCSKEEPALIVE:
  1057. /* max for unchar */
  1058. if ((unsigned)*p > 255) {
  1059. spin_unlock_bh(&sl->lock);
  1060. return -EINVAL;
  1061. }
  1062. sl->keepalive = (u8)*p;
  1063. if (sl->keepalive != 0) {
  1064. sl->keepalive_timer.expires =
  1065. jiffies + sl->keepalive * HZ;
  1066. mod_timer(&sl->keepalive_timer,
  1067. jiffies + sl->keepalive * HZ);
  1068. set_bit(SLF_KEEPTEST, &sl->flags);
  1069. } else
  1070. del_timer(&sl->keepalive_timer);
  1071. break;
  1072. case SIOCGKEEPALIVE:
  1073. *p = sl->keepalive;
  1074. break;
  1075. case SIOCSOUTFILL:
  1076. if ((unsigned)*p > 255) { /* max for unchar */
  1077. spin_unlock_bh(&sl->lock);
  1078. return -EINVAL;
  1079. }
  1080. sl->outfill = (u8)*p;
  1081. if (sl->outfill != 0) {
  1082. mod_timer(&sl->outfill_timer,
  1083. jiffies + sl->outfill * HZ);
  1084. set_bit(SLF_OUTWAIT, &sl->flags);
  1085. } else
  1086. del_timer(&sl->outfill_timer);
  1087. break;
  1088. case SIOCGOUTFILL:
  1089. *p = sl->outfill;
  1090. break;
  1091. case SIOCSLEASE:
  1092. /* Resolve race condition, when ioctl'ing hanged up
  1093. and opened by another process device.
  1094. */
  1095. if (sl->tty != current->signal->tty &&
  1096. sl->pid != current->pid) {
  1097. spin_unlock_bh(&sl->lock);
  1098. return -EPERM;
  1099. }
  1100. sl->leased = 0;
  1101. if (*p)
  1102. sl->leased = 1;
  1103. break;
  1104. case SIOCGLEASE:
  1105. *p = sl->leased;
  1106. }
  1107. spin_unlock_bh(&sl->lock);
  1108. return 0;
  1109. }
  1110. #endif
  1111. /* VSV changes end */
  1112. static struct tty_ldisc_ops sl_ldisc = {
  1113. .owner = THIS_MODULE,
  1114. .magic = TTY_LDISC_MAGIC,
  1115. .name = "slip",
  1116. .open = slip_open,
  1117. .close = slip_close,
  1118. .hangup = slip_hangup,
  1119. .ioctl = slip_ioctl,
  1120. #ifdef CONFIG_COMPAT
  1121. .compat_ioctl = slip_compat_ioctl,
  1122. #endif
  1123. .receive_buf = slip_receive_buf,
  1124. .write_wakeup = slip_write_wakeup,
  1125. };
  1126. static int __init slip_init(void)
  1127. {
  1128. int status;
  1129. if (slip_maxdev < 4)
  1130. slip_maxdev = 4; /* Sanity */
  1131. printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
  1132. #ifdef CONFIG_SLIP_MODE_SLIP6
  1133. " (6 bit encapsulation enabled)"
  1134. #endif
  1135. ".\n",
  1136. SLIP_VERSION, slip_maxdev);
  1137. #if defined(SL_INCLUDE_CSLIP)
  1138. printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
  1139. #endif
  1140. #ifdef CONFIG_SLIP_SMART
  1141. printk(KERN_INFO "SLIP linefill/keepalive option.\n");
  1142. #endif
  1143. slip_devs = kzalloc(sizeof(struct net_device *)*slip_maxdev,
  1144. GFP_KERNEL);
  1145. if (!slip_devs) {
  1146. printk(KERN_ERR "SLIP: Can't allocate slip devices array.\n");
  1147. return -ENOMEM;
  1148. }
  1149. /* Fill in our line protocol discipline, and register it */
  1150. status = tty_register_ldisc(N_SLIP, &sl_ldisc);
  1151. if (status != 0) {
  1152. printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
  1153. kfree(slip_devs);
  1154. }
  1155. return status;
  1156. }
  1157. static void __exit slip_exit(void)
  1158. {
  1159. int i;
  1160. struct net_device *dev;
  1161. struct slip *sl;
  1162. unsigned long timeout = jiffies + HZ;
  1163. int busy = 0;
  1164. if (slip_devs == NULL)
  1165. return;
  1166. /* First of all: check for active disciplines and hangup them.
  1167. */
  1168. do {
  1169. if (busy)
  1170. msleep_interruptible(100);
  1171. busy = 0;
  1172. for (i = 0; i < slip_maxdev; i++) {
  1173. dev = slip_devs[i];
  1174. if (!dev)
  1175. continue;
  1176. sl = netdev_priv(dev);
  1177. spin_lock_bh(&sl->lock);
  1178. if (sl->tty) {
  1179. busy++;
  1180. tty_hangup(sl->tty);
  1181. }
  1182. spin_unlock_bh(&sl->lock);
  1183. }
  1184. } while (busy && time_before(jiffies, timeout));
  1185. /* FIXME: hangup is async so we should wait when doing this second
  1186. phase */
  1187. for (i = 0; i < slip_maxdev; i++) {
  1188. dev = slip_devs[i];
  1189. if (!dev)
  1190. continue;
  1191. slip_devs[i] = NULL;
  1192. sl = netdev_priv(dev);
  1193. if (sl->tty) {
  1194. printk(KERN_ERR "%s: tty discipline still running\n",
  1195. dev->name);
  1196. /* Intentionally leak the control block. */
  1197. dev->destructor = NULL;
  1198. }
  1199. unregister_netdev(dev);
  1200. }
  1201. kfree(slip_devs);
  1202. slip_devs = NULL;
  1203. i = tty_unregister_ldisc(N_SLIP);
  1204. if (i != 0)
  1205. printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
  1206. }
  1207. module_init(slip_init);
  1208. module_exit(slip_exit);
  1209. #ifdef CONFIG_SLIP_SMART
  1210. /*
  1211. * This is start of the code for multislip style line checking
  1212. * added by Stanislav Voronyi. All changes before marked VSV
  1213. */
  1214. static void sl_outfill(unsigned long sls)
  1215. {
  1216. struct slip *sl = (struct slip *)sls;
  1217. spin_lock(&sl->lock);
  1218. if (sl->tty == NULL)
  1219. goto out;
  1220. if (sl->outfill) {
  1221. if (test_bit(SLF_OUTWAIT, &sl->flags)) {
  1222. /* no packets were transmitted, do outfill */
  1223. #ifdef CONFIG_SLIP_MODE_SLIP6
  1224. unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
  1225. #else
  1226. unsigned char s = END;
  1227. #endif
  1228. /* put END into tty queue. Is it right ??? */
  1229. if (!netif_queue_stopped(sl->dev)) {
  1230. /* if device busy no outfill */
  1231. sl->tty->ops->write(sl->tty, &s, 1);
  1232. }
  1233. } else
  1234. set_bit(SLF_OUTWAIT, &sl->flags);
  1235. mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
  1236. }
  1237. out:
  1238. spin_unlock(&sl->lock);
  1239. }
  1240. static void sl_keepalive(unsigned long sls)
  1241. {
  1242. struct slip *sl = (struct slip *)sls;
  1243. spin_lock(&sl->lock);
  1244. if (sl->tty == NULL)
  1245. goto out;
  1246. if (sl->keepalive) {
  1247. if (test_bit(SLF_KEEPTEST, &sl->flags)) {
  1248. /* keepalive still high :(, we must hangup */
  1249. if (sl->outfill)
  1250. /* outfill timer must be deleted too */
  1251. (void)del_timer(&sl->outfill_timer);
  1252. printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
  1253. /* this must hangup tty & close slip */
  1254. tty_hangup(sl->tty);
  1255. /* I think we need not something else */
  1256. goto out;
  1257. } else
  1258. set_bit(SLF_KEEPTEST, &sl->flags);
  1259. mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
  1260. }
  1261. out:
  1262. spin_unlock(&sl->lock);
  1263. }
  1264. #endif
  1265. MODULE_LICENSE("GPL");
  1266. MODULE_ALIAS_LDISC(N_SLIP);