slip.c 33 KB

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