slip.c 34 KB

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