slip.c 34 KB

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