slip.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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. 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. /* TTY layer expects 0 on success */
  743. return 0;
  744. err_free_bufs:
  745. sl_free_bufs(sl);
  746. err_free_chan:
  747. sl->tty = NULL;
  748. tty->disc_data = NULL;
  749. clear_bit(SLF_INUSE, &sl->flags);
  750. err_exit:
  751. rtnl_unlock();
  752. /* Count references from TTY module */
  753. return err;
  754. }
  755. /*
  756. * Close down a SLIP channel.
  757. * This means flushing out any pending queues, and then returning. This
  758. * call is serialized against other ldisc functions.
  759. *
  760. * We also use this method fo a hangup event
  761. */
  762. static void slip_close(struct tty_struct *tty)
  763. {
  764. struct slip *sl = tty->disc_data;
  765. /* First make sure we're connected. */
  766. if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
  767. return;
  768. tty->disc_data = NULL;
  769. sl->tty = NULL;
  770. if (!sl->leased)
  771. sl->line = 0;
  772. /* VSV = very important to remove timers */
  773. #ifdef CONFIG_SLIP_SMART
  774. del_timer_sync(&sl->keepalive_timer);
  775. del_timer_sync(&sl->outfill_timer);
  776. #endif
  777. /* Flush network side */
  778. unregister_netdev(sl->dev);
  779. /* This will complete via sl_free_netdev */
  780. }
  781. static int slip_hangup(struct tty_struct *tty)
  782. {
  783. slip_close(tty);
  784. return 0;
  785. }
  786. /************************************************************************
  787. * STANDARD SLIP ENCAPSULATION *
  788. ************************************************************************/
  789. static int slip_esc(unsigned char *s, unsigned char *d, int len)
  790. {
  791. unsigned char *ptr = d;
  792. unsigned char c;
  793. /*
  794. * Send an initial END character to flush out any
  795. * data that may have accumulated in the receiver
  796. * due to line noise.
  797. */
  798. *ptr++ = END;
  799. /*
  800. * For each byte in the packet, send the appropriate
  801. * character sequence, according to the SLIP protocol.
  802. */
  803. while (len-- > 0) {
  804. switch (c = *s++) {
  805. case END:
  806. *ptr++ = ESC;
  807. *ptr++ = ESC_END;
  808. break;
  809. case ESC:
  810. *ptr++ = ESC;
  811. *ptr++ = ESC_ESC;
  812. break;
  813. default:
  814. *ptr++ = c;
  815. break;
  816. }
  817. }
  818. *ptr++ = END;
  819. return ptr - d;
  820. }
  821. static void slip_unesc(struct slip *sl, unsigned char s)
  822. {
  823. switch (s) {
  824. case END:
  825. #ifdef CONFIG_SLIP_SMART
  826. /* drop keeptest bit = VSV */
  827. if (test_bit(SLF_KEEPTEST, &sl->flags))
  828. clear_bit(SLF_KEEPTEST, &sl->flags);
  829. #endif
  830. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  831. (sl->rcount > 2))
  832. sl_bump(sl);
  833. clear_bit(SLF_ESCAPE, &sl->flags);
  834. sl->rcount = 0;
  835. return;
  836. case ESC:
  837. set_bit(SLF_ESCAPE, &sl->flags);
  838. return;
  839. case ESC_ESC:
  840. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
  841. s = ESC;
  842. break;
  843. case ESC_END:
  844. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
  845. s = END;
  846. break;
  847. }
  848. if (!test_bit(SLF_ERROR, &sl->flags)) {
  849. if (sl->rcount < sl->buffsize) {
  850. sl->rbuff[sl->rcount++] = s;
  851. return;
  852. }
  853. sl->dev->stats.rx_over_errors++;
  854. set_bit(SLF_ERROR, &sl->flags);
  855. }
  856. }
  857. #ifdef CONFIG_SLIP_MODE_SLIP6
  858. /************************************************************************
  859. * 6 BIT SLIP ENCAPSULATION *
  860. ************************************************************************/
  861. static int slip_esc6(unsigned char *s, unsigned char *d, int len)
  862. {
  863. unsigned char *ptr = d;
  864. unsigned char c;
  865. int i;
  866. unsigned short v = 0;
  867. short bits = 0;
  868. /*
  869. * Send an initial END character to flush out any
  870. * data that may have accumulated in the receiver
  871. * due to line noise.
  872. */
  873. *ptr++ = 0x70;
  874. /*
  875. * Encode the packet into printable ascii characters
  876. */
  877. for (i = 0; i < len; ++i) {
  878. v = (v << 8) | s[i];
  879. bits += 8;
  880. while (bits >= 6) {
  881. bits -= 6;
  882. c = 0x30 + ((v >> bits) & 0x3F);
  883. *ptr++ = c;
  884. }
  885. }
  886. if (bits) {
  887. c = 0x30 + ((v << (6 - bits)) & 0x3F);
  888. *ptr++ = c;
  889. }
  890. *ptr++ = 0x70;
  891. return ptr - d;
  892. }
  893. static void slip_unesc6(struct slip *sl, unsigned char s)
  894. {
  895. unsigned char c;
  896. if (s == 0x70) {
  897. #ifdef CONFIG_SLIP_SMART
  898. /* drop keeptest bit = VSV */
  899. if (test_bit(SLF_KEEPTEST, &sl->flags))
  900. clear_bit(SLF_KEEPTEST, &sl->flags);
  901. #endif
  902. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  903. (sl->rcount > 2))
  904. sl_bump(sl);
  905. sl->rcount = 0;
  906. sl->xbits = 0;
  907. sl->xdata = 0;
  908. } else if (s >= 0x30 && s < 0x70) {
  909. sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
  910. sl->xbits += 6;
  911. if (sl->xbits >= 8) {
  912. sl->xbits -= 8;
  913. c = (unsigned char)(sl->xdata >> sl->xbits);
  914. if (!test_bit(SLF_ERROR, &sl->flags)) {
  915. if (sl->rcount < sl->buffsize) {
  916. sl->rbuff[sl->rcount++] = c;
  917. return;
  918. }
  919. sl->dev->stats.rx_over_errors++;
  920. set_bit(SLF_ERROR, &sl->flags);
  921. }
  922. }
  923. }
  924. }
  925. #endif /* CONFIG_SLIP_MODE_SLIP6 */
  926. /* Perform I/O control on an active SLIP channel. */
  927. static int slip_ioctl(struct tty_struct *tty, struct file *file,
  928. unsigned int cmd, unsigned long arg)
  929. {
  930. struct slip *sl = tty->disc_data;
  931. unsigned int tmp;
  932. int __user *p = (int __user *)arg;
  933. /* First make sure we're connected. */
  934. if (!sl || sl->magic != SLIP_MAGIC)
  935. return -EINVAL;
  936. switch (cmd) {
  937. case SIOCGIFNAME:
  938. tmp = strlen(sl->dev->name) + 1;
  939. if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
  940. return -EFAULT;
  941. return 0;
  942. case SIOCGIFENCAP:
  943. if (put_user(sl->mode, p))
  944. return -EFAULT;
  945. return 0;
  946. case SIOCSIFENCAP:
  947. if (get_user(tmp, p))
  948. return -EFAULT;
  949. #ifndef SL_INCLUDE_CSLIP
  950. if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE))
  951. return -EINVAL;
  952. #else
  953. if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
  954. (SL_MODE_ADAPTIVE | SL_MODE_CSLIP))
  955. /* return -EINVAL; */
  956. tmp &= ~SL_MODE_ADAPTIVE;
  957. #endif
  958. #ifndef CONFIG_SLIP_MODE_SLIP6
  959. if (tmp & SL_MODE_SLIP6)
  960. return -EINVAL;
  961. #endif
  962. sl->mode = tmp;
  963. sl->dev->type = ARPHRD_SLIP + sl->mode;
  964. return 0;
  965. case SIOCSIFHWADDR:
  966. return -EINVAL;
  967. #ifdef CONFIG_SLIP_SMART
  968. /* VSV changes start here */
  969. case SIOCSKEEPALIVE:
  970. if (get_user(tmp, p))
  971. return -EFAULT;
  972. if (tmp > 255) /* max for unchar */
  973. return -EINVAL;
  974. spin_lock_bh(&sl->lock);
  975. if (!sl->tty) {
  976. spin_unlock_bh(&sl->lock);
  977. return -ENODEV;
  978. }
  979. sl->keepalive = (u8)tmp;
  980. if (sl->keepalive != 0) {
  981. mod_timer(&sl->keepalive_timer,
  982. jiffies + sl->keepalive * HZ);
  983. set_bit(SLF_KEEPTEST, &sl->flags);
  984. } else
  985. del_timer(&sl->keepalive_timer);
  986. spin_unlock_bh(&sl->lock);
  987. return 0;
  988. case SIOCGKEEPALIVE:
  989. if (put_user(sl->keepalive, p))
  990. return -EFAULT;
  991. return 0;
  992. case SIOCSOUTFILL:
  993. if (get_user(tmp, p))
  994. return -EFAULT;
  995. if (tmp > 255) /* max for unchar */
  996. return -EINVAL;
  997. spin_lock_bh(&sl->lock);
  998. if (!sl->tty) {
  999. spin_unlock_bh(&sl->lock);
  1000. return -ENODEV;
  1001. }
  1002. sl->outfill = (u8)tmp;
  1003. if (sl->outfill != 0) {
  1004. mod_timer(&sl->outfill_timer,
  1005. jiffies + sl->outfill * HZ);
  1006. set_bit(SLF_OUTWAIT, &sl->flags);
  1007. } else
  1008. del_timer(&sl->outfill_timer);
  1009. spin_unlock_bh(&sl->lock);
  1010. return 0;
  1011. case SIOCGOUTFILL:
  1012. if (put_user(sl->outfill, p))
  1013. return -EFAULT;
  1014. return 0;
  1015. /* VSV changes end */
  1016. #endif
  1017. default:
  1018. return tty_mode_ioctl(tty, file, cmd, arg);
  1019. }
  1020. }
  1021. #ifdef CONFIG_COMPAT
  1022. static long slip_compat_ioctl(struct tty_struct *tty, struct file *file,
  1023. unsigned int cmd, unsigned long arg)
  1024. {
  1025. switch (cmd) {
  1026. case SIOCGIFNAME:
  1027. case SIOCGIFENCAP:
  1028. case SIOCSIFENCAP:
  1029. case SIOCSIFHWADDR:
  1030. case SIOCSKEEPALIVE:
  1031. case SIOCGKEEPALIVE:
  1032. case SIOCSOUTFILL:
  1033. case SIOCGOUTFILL:
  1034. return slip_ioctl(tty, file, cmd,
  1035. (unsigned long)compat_ptr(arg));
  1036. }
  1037. return -ENOIOCTLCMD;
  1038. }
  1039. #endif
  1040. /* VSV changes start here */
  1041. #ifdef CONFIG_SLIP_SMART
  1042. /* function do_ioctl called from net/core/dev.c
  1043. to allow get/set outfill/keepalive parameter
  1044. by ifconfig */
  1045. static int sl_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1046. {
  1047. struct slip *sl = netdev_priv(dev);
  1048. unsigned long *p = (unsigned long *)&rq->ifr_ifru;
  1049. if (sl == NULL) /* Allocation failed ?? */
  1050. return -ENODEV;
  1051. spin_lock_bh(&sl->lock);
  1052. if (!sl->tty) {
  1053. spin_unlock_bh(&sl->lock);
  1054. return -ENODEV;
  1055. }
  1056. switch (cmd) {
  1057. case SIOCSKEEPALIVE:
  1058. /* max for unchar */
  1059. if ((unsigned)*p > 255) {
  1060. spin_unlock_bh(&sl->lock);
  1061. return -EINVAL;
  1062. }
  1063. sl->keepalive = (u8)*p;
  1064. if (sl->keepalive != 0) {
  1065. sl->keepalive_timer.expires =
  1066. jiffies + sl->keepalive * HZ;
  1067. mod_timer(&sl->keepalive_timer,
  1068. jiffies + sl->keepalive * HZ);
  1069. set_bit(SLF_KEEPTEST, &sl->flags);
  1070. } else
  1071. del_timer(&sl->keepalive_timer);
  1072. break;
  1073. case SIOCGKEEPALIVE:
  1074. *p = sl->keepalive;
  1075. break;
  1076. case SIOCSOUTFILL:
  1077. if ((unsigned)*p > 255) { /* max for unchar */
  1078. spin_unlock_bh(&sl->lock);
  1079. return -EINVAL;
  1080. }
  1081. sl->outfill = (u8)*p;
  1082. if (sl->outfill != 0) {
  1083. mod_timer(&sl->outfill_timer,
  1084. jiffies + sl->outfill * HZ);
  1085. set_bit(SLF_OUTWAIT, &sl->flags);
  1086. } else
  1087. del_timer(&sl->outfill_timer);
  1088. break;
  1089. case SIOCGOUTFILL:
  1090. *p = sl->outfill;
  1091. break;
  1092. case SIOCSLEASE:
  1093. /* Resolve race condition, when ioctl'ing hanged up
  1094. and opened by another process device.
  1095. */
  1096. if (sl->tty != current->signal->tty &&
  1097. sl->pid != current->pid) {
  1098. spin_unlock_bh(&sl->lock);
  1099. return -EPERM;
  1100. }
  1101. sl->leased = 0;
  1102. if (*p)
  1103. sl->leased = 1;
  1104. break;
  1105. case SIOCGLEASE:
  1106. *p = sl->leased;
  1107. }
  1108. spin_unlock_bh(&sl->lock);
  1109. return 0;
  1110. }
  1111. #endif
  1112. /* VSV changes end */
  1113. static struct tty_ldisc_ops sl_ldisc = {
  1114. .owner = THIS_MODULE,
  1115. .magic = TTY_LDISC_MAGIC,
  1116. .name = "slip",
  1117. .open = slip_open,
  1118. .close = slip_close,
  1119. .hangup = slip_hangup,
  1120. .ioctl = slip_ioctl,
  1121. #ifdef CONFIG_COMPAT
  1122. .compat_ioctl = slip_compat_ioctl,
  1123. #endif
  1124. .receive_buf = slip_receive_buf,
  1125. .write_wakeup = slip_write_wakeup,
  1126. };
  1127. static int __init slip_init(void)
  1128. {
  1129. int status;
  1130. if (slip_maxdev < 4)
  1131. slip_maxdev = 4; /* Sanity */
  1132. printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
  1133. #ifdef CONFIG_SLIP_MODE_SLIP6
  1134. " (6 bit encapsulation enabled)"
  1135. #endif
  1136. ".\n",
  1137. SLIP_VERSION, slip_maxdev);
  1138. #if defined(SL_INCLUDE_CSLIP)
  1139. printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
  1140. #endif
  1141. #ifdef CONFIG_SLIP_SMART
  1142. printk(KERN_INFO "SLIP linefill/keepalive option.\n");
  1143. #endif
  1144. slip_devs = kzalloc(sizeof(struct net_device *)*slip_maxdev,
  1145. GFP_KERNEL);
  1146. if (!slip_devs) {
  1147. printk(KERN_ERR "SLIP: Can't allocate slip devices array.\n");
  1148. return -ENOMEM;
  1149. }
  1150. /* Fill in our line protocol discipline, and register it */
  1151. status = tty_register_ldisc(N_SLIP, &sl_ldisc);
  1152. if (status != 0) {
  1153. printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
  1154. kfree(slip_devs);
  1155. }
  1156. return status;
  1157. }
  1158. static void __exit slip_exit(void)
  1159. {
  1160. int i;
  1161. struct net_device *dev;
  1162. struct slip *sl;
  1163. unsigned long timeout = jiffies + HZ;
  1164. int busy = 0;
  1165. if (slip_devs == NULL)
  1166. return;
  1167. /* First of all: check for active disciplines and hangup them.
  1168. */
  1169. do {
  1170. if (busy)
  1171. msleep_interruptible(100);
  1172. busy = 0;
  1173. for (i = 0; i < slip_maxdev; i++) {
  1174. dev = slip_devs[i];
  1175. if (!dev)
  1176. continue;
  1177. sl = netdev_priv(dev);
  1178. spin_lock_bh(&sl->lock);
  1179. if (sl->tty) {
  1180. busy++;
  1181. tty_hangup(sl->tty);
  1182. }
  1183. spin_unlock_bh(&sl->lock);
  1184. }
  1185. } while (busy && time_before(jiffies, timeout));
  1186. /* FIXME: hangup is async so we should wait when doing this second
  1187. phase */
  1188. for (i = 0; i < slip_maxdev; i++) {
  1189. dev = slip_devs[i];
  1190. if (!dev)
  1191. continue;
  1192. slip_devs[i] = NULL;
  1193. sl = netdev_priv(dev);
  1194. if (sl->tty) {
  1195. printk(KERN_ERR "%s: tty discipline still running\n",
  1196. dev->name);
  1197. /* Intentionally leak the control block. */
  1198. dev->destructor = NULL;
  1199. }
  1200. unregister_netdev(dev);
  1201. }
  1202. kfree(slip_devs);
  1203. slip_devs = NULL;
  1204. i = tty_unregister_ldisc(N_SLIP);
  1205. if (i != 0)
  1206. printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
  1207. }
  1208. module_init(slip_init);
  1209. module_exit(slip_exit);
  1210. #ifdef CONFIG_SLIP_SMART
  1211. /*
  1212. * This is start of the code for multislip style line checking
  1213. * added by Stanislav Voronyi. All changes before marked VSV
  1214. */
  1215. static void sl_outfill(unsigned long sls)
  1216. {
  1217. struct slip *sl = (struct slip *)sls;
  1218. spin_lock(&sl->lock);
  1219. if (sl->tty == NULL)
  1220. goto out;
  1221. if (sl->outfill) {
  1222. if (test_bit(SLF_OUTWAIT, &sl->flags)) {
  1223. /* no packets were transmitted, do outfill */
  1224. #ifdef CONFIG_SLIP_MODE_SLIP6
  1225. unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
  1226. #else
  1227. unsigned char s = END;
  1228. #endif
  1229. /* put END into tty queue. Is it right ??? */
  1230. if (!netif_queue_stopped(sl->dev)) {
  1231. /* if device busy no outfill */
  1232. sl->tty->ops->write(sl->tty, &s, 1);
  1233. }
  1234. } else
  1235. set_bit(SLF_OUTWAIT, &sl->flags);
  1236. mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
  1237. }
  1238. out:
  1239. spin_unlock(&sl->lock);
  1240. }
  1241. static void sl_keepalive(unsigned long sls)
  1242. {
  1243. struct slip *sl = (struct slip *)sls;
  1244. spin_lock(&sl->lock);
  1245. if (sl->tty == NULL)
  1246. goto out;
  1247. if (sl->keepalive) {
  1248. if (test_bit(SLF_KEEPTEST, &sl->flags)) {
  1249. /* keepalive still high :(, we must hangup */
  1250. if (sl->outfill)
  1251. /* outfill timer must be deleted too */
  1252. (void)del_timer(&sl->outfill_timer);
  1253. printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
  1254. /* this must hangup tty & close slip */
  1255. tty_hangup(sl->tty);
  1256. /* I think we need not something else */
  1257. goto out;
  1258. } else
  1259. set_bit(SLF_KEEPTEST, &sl->flags);
  1260. mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
  1261. }
  1262. out:
  1263. spin_unlock(&sl->lock);
  1264. }
  1265. #endif
  1266. MODULE_LICENSE("GPL");
  1267. MODULE_ALIAS_LDISC(N_SLIP);