slip.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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 mtu upwards.
  15. * Driver now spots this and grows/shrinks its buffers(hack!).
  16. * Memory leak if you run out of memory setting up a slip driver fixed.
  17. * Matt Dillon : Printable slip (borrowed from NET2E)
  18. * Pauline Middelink : Slip driver fixes.
  19. * Alan Cox : Honours the old SL_COMPRESSED flag
  20. * Alan Cox : KISS AX.25 and AXUI IP support
  21. * Michael Riepe : Automatic CSLIP recognition added
  22. * Charles Hedrick : CSLIP header length problem fix.
  23. * Alan Cox : Corrected non-IP cases of the above.
  24. * Alan Cox : Now uses hardware type as per FvK.
  25. * Alan Cox : Default to 192.168.0.0 (RFC 1597)
  26. * A.N.Kuznetsov : dev_tint() recursion fix.
  27. * Dmitry Gorodchanin : SLIP memory leaks
  28. * Dmitry Gorodchanin : Code cleanup. Reduce tty driver
  29. * buffering from 4096 to 256 bytes.
  30. * Improving SLIP response time.
  31. * CONFIG_SLIP_MODE_SLIP6.
  32. * ifconfig sl? up & down now works correctly.
  33. * Modularization.
  34. * Alan Cox : Oops - fix AX.25 buffer lengths
  35. * Dmitry Gorodchanin : Even more cleanups. Preserve CSLIP
  36. * statistics. Include CSLIP code only
  37. * if it really needed.
  38. * Alan Cox : Free slhc buffers in the right place.
  39. * Alan Cox : Allow for digipeated IP over AX.25
  40. * Matti Aarnio : Dynamic SLIP devices, with ideas taken
  41. * from Jim Freeman's <jfree@caldera.com>
  42. * dynamic PPP devices. We do NOT kfree()
  43. * device entries, just reg./unreg. them
  44. * as they are needed. We kfree() them
  45. * at module cleanup.
  46. * With MODULE-loading ``insmod'', user can
  47. * issue parameter: slip_maxdev=1024
  48. * (Or how much he/she wants.. Default is 256)
  49. * * Stanislav Voronyi : Slip line checking, with ideas taken
  50. * from multislip BSDI driver which was written
  51. * by Igor Chechik, RELCOM Corp. Only algorithms
  52. * have been ported to Linux SLIP driver.
  53. * Vitaly E. Lavrov : Sane behaviour on tty hangup.
  54. * Alexey Kuznetsov : Cleanup interfaces to tty&netdevice modules.
  55. */
  56. #define SL_CHECK_TRANSMIT
  57. #include <linux/config.h>
  58. #include <linux/module.h>
  59. #include <linux/moduleparam.h>
  60. #include <asm/system.h>
  61. #include <asm/uaccess.h>
  62. #include <linux/bitops.h>
  63. #include <linux/string.h>
  64. #include <linux/mm.h>
  65. #include <linux/interrupt.h>
  66. #include <linux/in.h>
  67. #include <linux/tty.h>
  68. #include <linux/errno.h>
  69. #include <linux/netdevice.h>
  70. #include <linux/etherdevice.h>
  71. #include <linux/skbuff.h>
  72. #include <linux/rtnetlink.h>
  73. #include <linux/if_arp.h>
  74. #include <linux/if_slip.h>
  75. #include <linux/init.h>
  76. #include "slip.h"
  77. #ifdef CONFIG_INET
  78. #include <linux/ip.h>
  79. #include <linux/tcp.h>
  80. #include <net/slhc_vj.h>
  81. #endif
  82. #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY"
  83. static struct net_device **slip_devs;
  84. static int slip_maxdev = SL_NRUNIT;
  85. module_param(slip_maxdev, int, 0);
  86. MODULE_PARM_DESC(slip_maxdev, "Maximum number of slip devices");
  87. static int slip_esc(unsigned char *p, unsigned char *d, int len);
  88. static void slip_unesc(struct slip *sl, unsigned char c);
  89. #ifdef CONFIG_SLIP_MODE_SLIP6
  90. static int slip_esc6(unsigned char *p, unsigned char *d, int len);
  91. static void slip_unesc6(struct slip *sl, unsigned char c);
  92. #endif
  93. #ifdef CONFIG_SLIP_SMART
  94. static void sl_keepalive(unsigned long sls);
  95. static void sl_outfill(unsigned long sls);
  96. static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd);
  97. #endif
  98. /********************************
  99. * Buffer administration routines:
  100. * sl_alloc_bufs()
  101. * sl_free_bufs()
  102. * sl_realloc_bufs()
  103. *
  104. * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because
  105. * sl_realloc_bufs provides strong atomicity and reallocation
  106. * on actively running device.
  107. *********************************/
  108. /*
  109. Allocate channel buffers.
  110. */
  111. static int
  112. sl_alloc_bufs(struct slip *sl, int mtu)
  113. {
  114. int err = -ENOBUFS;
  115. unsigned long len;
  116. char * rbuff = NULL;
  117. char * xbuff = NULL;
  118. #ifdef SL_INCLUDE_CSLIP
  119. char * cbuff = NULL;
  120. struct slcompress *slcomp = NULL;
  121. #endif
  122. /*
  123. * Allocate the SLIP frame buffers:
  124. *
  125. * rbuff Receive buffer.
  126. * xbuff Transmit buffer.
  127. * cbuff Temporary compression buffer.
  128. */
  129. len = mtu * 2;
  130. /*
  131. * allow for arrival of larger UDP packets, even if we say not to
  132. * also fixes a bug in which SunOS sends 512-byte packets even with
  133. * an MSS of 128
  134. */
  135. if (len < 576 * 2)
  136. len = 576 * 2;
  137. rbuff = kmalloc(len + 4, GFP_KERNEL);
  138. if (rbuff == NULL)
  139. goto err_exit;
  140. xbuff = kmalloc(len + 4, GFP_KERNEL);
  141. if (xbuff == NULL)
  142. goto err_exit;
  143. #ifdef SL_INCLUDE_CSLIP
  144. cbuff = kmalloc(len + 4, GFP_KERNEL);
  145. if (cbuff == NULL)
  146. goto err_exit;
  147. slcomp = slhc_init(16, 16);
  148. if (slcomp == NULL)
  149. goto err_exit;
  150. #endif
  151. spin_lock_bh(&sl->lock);
  152. if (sl->tty == NULL) {
  153. spin_unlock_bh(&sl->lock);
  154. err = -ENODEV;
  155. goto err_exit;
  156. }
  157. sl->mtu = mtu;
  158. sl->buffsize = len;
  159. sl->rcount = 0;
  160. sl->xleft = 0;
  161. rbuff = xchg(&sl->rbuff, rbuff);
  162. xbuff = xchg(&sl->xbuff, xbuff);
  163. #ifdef SL_INCLUDE_CSLIP
  164. cbuff = xchg(&sl->cbuff, cbuff);
  165. slcomp = xchg(&sl->slcomp, slcomp);
  166. #ifdef CONFIG_SLIP_MODE_SLIP6
  167. sl->xdata = 0;
  168. sl->xbits = 0;
  169. #endif
  170. #endif
  171. spin_unlock_bh(&sl->lock);
  172. err = 0;
  173. /* Cleanup */
  174. err_exit:
  175. #ifdef SL_INCLUDE_CSLIP
  176. kfree(cbuff);
  177. if (slcomp)
  178. slhc_free(slcomp);
  179. #endif
  180. kfree(xbuff);
  181. kfree(rbuff);
  182. return err;
  183. }
  184. /* Free a SLIP channel buffers. */
  185. static void
  186. sl_free_bufs(struct slip *sl)
  187. {
  188. void * tmp;
  189. /* Free all SLIP frame buffers. */
  190. tmp = xchg(&sl->rbuff, NULL);
  191. kfree(tmp);
  192. tmp = xchg(&sl->xbuff, NULL);
  193. kfree(tmp);
  194. #ifdef SL_INCLUDE_CSLIP
  195. tmp = xchg(&sl->cbuff, NULL);
  196. kfree(tmp);
  197. if ((tmp = xchg(&sl->slcomp, NULL)) != NULL)
  198. slhc_free(tmp);
  199. #endif
  200. }
  201. /*
  202. Reallocate slip channel buffers.
  203. */
  204. static int sl_realloc_bufs(struct slip *sl, int mtu)
  205. {
  206. int err = 0;
  207. struct net_device *dev = sl->dev;
  208. unsigned char *xbuff, *rbuff;
  209. #ifdef SL_INCLUDE_CSLIP
  210. unsigned char *cbuff;
  211. #endif
  212. int len = mtu * 2;
  213. /*
  214. * allow for arrival of larger UDP packets, even if we say not to
  215. * also fixes a bug in which SunOS sends 512-byte packets even with
  216. * an MSS of 128
  217. */
  218. if (len < 576 * 2)
  219. len = 576 * 2;
  220. xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
  221. rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
  222. #ifdef SL_INCLUDE_CSLIP
  223. cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
  224. #endif
  225. #ifdef SL_INCLUDE_CSLIP
  226. if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
  227. #else
  228. if (xbuff == NULL || rbuff == NULL) {
  229. #endif
  230. if (mtu >= sl->mtu) {
  231. printk(KERN_WARNING "%s: unable to grow slip buffers, MTU change cancelled.\n",
  232. dev->name);
  233. err = -ENOBUFS;
  234. }
  235. goto done;
  236. }
  237. spin_lock_bh(&sl->lock);
  238. err = -ENODEV;
  239. if (sl->tty == NULL)
  240. goto done_on_bh;
  241. xbuff = xchg(&sl->xbuff, xbuff);
  242. rbuff = xchg(&sl->rbuff, rbuff);
  243. #ifdef SL_INCLUDE_CSLIP
  244. cbuff = xchg(&sl->cbuff, cbuff);
  245. #endif
  246. if (sl->xleft) {
  247. if (sl->xleft <= len) {
  248. memcpy(sl->xbuff, sl->xhead, sl->xleft);
  249. } else {
  250. sl->xleft = 0;
  251. sl->tx_dropped++;
  252. }
  253. }
  254. sl->xhead = sl->xbuff;
  255. if (sl->rcount) {
  256. if (sl->rcount <= len) {
  257. memcpy(sl->rbuff, rbuff, sl->rcount);
  258. } else {
  259. sl->rcount = 0;
  260. sl->rx_over_errors++;
  261. set_bit(SLF_ERROR, &sl->flags);
  262. }
  263. }
  264. sl->mtu = mtu;
  265. dev->mtu = mtu;
  266. sl->buffsize = len;
  267. err = 0;
  268. done_on_bh:
  269. spin_unlock_bh(&sl->lock);
  270. done:
  271. kfree(xbuff);
  272. kfree(rbuff);
  273. #ifdef SL_INCLUDE_CSLIP
  274. kfree(cbuff);
  275. #endif
  276. return err;
  277. }
  278. /* Set the "sending" flag. This must be atomic hence the set_bit. */
  279. static inline void
  280. 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
  286. sl_unlock(struct slip *sl)
  287. {
  288. netif_wake_queue(sl->dev);
  289. }
  290. /* Send one completely decapsulated IP datagram to the IP layer. */
  291. static void
  292. sl_bump(struct slip *sl)
  293. {
  294. struct sk_buff *skb;
  295. int count;
  296. count = sl->rcount;
  297. #ifdef SL_INCLUDE_CSLIP
  298. if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
  299. unsigned char c;
  300. if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
  301. /* ignore compressed packets when CSLIP is off */
  302. if (!(sl->mode & SL_MODE_CSLIP)) {
  303. printk(KERN_WARNING "%s: compressed packet ignored\n", sl->dev->name);
  304. return;
  305. }
  306. /* make sure we've reserved enough space for uncompress to use */
  307. if (count + 80 > sl->buffsize) {
  308. sl->rx_over_errors++;
  309. return;
  310. }
  311. count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
  312. if (count <= 0) {
  313. return;
  314. }
  315. } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
  316. if (!(sl->mode & SL_MODE_CSLIP)) {
  317. /* turn on header compression */
  318. sl->mode |= SL_MODE_CSLIP;
  319. sl->mode &= ~SL_MODE_ADAPTIVE;
  320. printk(KERN_INFO "%s: header compression turned on\n", sl->dev->name);
  321. }
  322. sl->rbuff[0] &= 0x4f;
  323. if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
  324. return;
  325. }
  326. }
  327. }
  328. #endif /* SL_INCLUDE_CSLIP */
  329. sl->rx_bytes+=count;
  330. skb = dev_alloc_skb(count);
  331. if (skb == NULL) {
  332. printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n", sl->dev->name);
  333. sl->rx_dropped++;
  334. return;
  335. }
  336. skb->dev = sl->dev;
  337. memcpy(skb_put(skb,count), sl->rbuff, count);
  338. skb->mac.raw=skb->data;
  339. skb->protocol=htons(ETH_P_IP);
  340. netif_rx(skb);
  341. sl->dev->last_rx = jiffies;
  342. sl->rx_packets++;
  343. }
  344. /* Encapsulate one IP datagram and stuff into a TTY queue. */
  345. static void
  346. sl_encaps(struct slip *sl, unsigned char *icp, int len)
  347. {
  348. unsigned char *p;
  349. int actual, count;
  350. if (len > sl->mtu) { /* Sigh, shouldn't occur BUT ... */
  351. printk(KERN_WARNING "%s: truncating oversized transmit packet!\n", sl->dev->name);
  352. sl->tx_dropped++;
  353. sl_unlock(sl);
  354. return;
  355. }
  356. p = icp;
  357. #ifdef SL_INCLUDE_CSLIP
  358. if (sl->mode & SL_MODE_CSLIP) {
  359. len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
  360. }
  361. #endif
  362. #ifdef CONFIG_SLIP_MODE_SLIP6
  363. if(sl->mode & SL_MODE_SLIP6)
  364. count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
  365. else
  366. #endif
  367. count = slip_esc(p, (unsigned char *) sl->xbuff, len);
  368. /* Order of next two lines is *very* important.
  369. * When we are sending a little amount of data,
  370. * the transfer may be completed inside driver.write()
  371. * routine, because it's running with interrupts enabled.
  372. * In this case we *never* got WRITE_WAKEUP event,
  373. * if we did not request it before write operation.
  374. * 14 Oct 1994 Dmitry Gorodchanin.
  375. */
  376. sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
  377. actual = sl->tty->driver->write(sl->tty, sl->xbuff, count);
  378. #ifdef SL_CHECK_TRANSMIT
  379. sl->dev->trans_start = jiffies;
  380. #endif
  381. sl->xleft = count - actual;
  382. sl->xhead = sl->xbuff + actual;
  383. #ifdef CONFIG_SLIP_SMART
  384. /* VSV */
  385. clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
  386. #endif
  387. }
  388. /*
  389. * Called by the driver when there's room for more data. If we have
  390. * more packets to send, we send them here.
  391. */
  392. static void slip_write_wakeup(struct tty_struct *tty)
  393. {
  394. int actual;
  395. struct slip *sl = (struct slip *) tty->disc_data;
  396. /* First make sure we're connected. */
  397. if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) {
  398. return;
  399. }
  400. if (sl->xleft <= 0) {
  401. /* Now serial buffer is almost free & we can start
  402. * transmission of another packet */
  403. sl->tx_packets++;
  404. tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  405. sl_unlock(sl);
  406. return;
  407. }
  408. actual = tty->driver->write(tty, sl->xhead, sl->xleft);
  409. sl->xleft -= actual;
  410. sl->xhead += actual;
  411. }
  412. static void sl_tx_timeout(struct net_device *dev)
  413. {
  414. struct slip *sl = netdev_priv(dev);
  415. spin_lock(&sl->lock);
  416. if (netif_queue_stopped(dev)) {
  417. if (!netif_running(dev))
  418. goto out;
  419. /* May be we must check transmitter timeout here ?
  420. * 14 Oct 1994 Dmitry Gorodchanin.
  421. */
  422. #ifdef SL_CHECK_TRANSMIT
  423. if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
  424. /* 20 sec timeout not reached */
  425. goto out;
  426. }
  427. printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
  428. (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft) ?
  429. "bad line quality" : "driver error");
  430. sl->xleft = 0;
  431. sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  432. sl_unlock(sl);
  433. #endif
  434. }
  435. out:
  436. spin_unlock(&sl->lock);
  437. }
  438. /* Encapsulate an IP datagram and kick it into a TTY queue. */
  439. static int
  440. sl_xmit(struct sk_buff *skb, struct net_device *dev)
  441. {
  442. struct slip *sl = netdev_priv(dev);
  443. spin_lock(&sl->lock);
  444. if (!netif_running(dev)) {
  445. spin_unlock(&sl->lock);
  446. printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name);
  447. dev_kfree_skb(skb);
  448. return 0;
  449. }
  450. if (sl->tty == NULL) {
  451. spin_unlock(&sl->lock);
  452. dev_kfree_skb(skb);
  453. return 0;
  454. }
  455. sl_lock(sl);
  456. sl->tx_bytes+=skb->len;
  457. sl_encaps(sl, skb->data, skb->len);
  458. spin_unlock(&sl->lock);
  459. dev_kfree_skb(skb);
  460. return 0;
  461. }
  462. /******************************************
  463. * Routines looking at netdevice side.
  464. ******************************************/
  465. /* Netdevice UP -> DOWN routine */
  466. static int
  467. sl_close(struct net_device *dev)
  468. {
  469. struct slip *sl = netdev_priv(dev);
  470. spin_lock_bh(&sl->lock);
  471. if (sl->tty) {
  472. /* TTY discipline is running. */
  473. sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
  474. }
  475. netif_stop_queue(dev);
  476. sl->rcount = 0;
  477. sl->xleft = 0;
  478. spin_unlock_bh(&sl->lock);
  479. return 0;
  480. }
  481. /* Netdevice DOWN -> UP routine */
  482. static int sl_open(struct net_device *dev)
  483. {
  484. struct slip *sl = netdev_priv(dev);
  485. if (sl->tty==NULL)
  486. return -ENODEV;
  487. sl->flags &= (1 << SLF_INUSE);
  488. netif_start_queue(dev);
  489. return 0;
  490. }
  491. /* Netdevice change MTU request */
  492. static int sl_change_mtu(struct net_device *dev, int new_mtu)
  493. {
  494. struct slip *sl = netdev_priv(dev);
  495. if (new_mtu < 68 || new_mtu > 65534)
  496. return -EINVAL;
  497. if (new_mtu != dev->mtu)
  498. return sl_realloc_bufs(sl, new_mtu);
  499. return 0;
  500. }
  501. /* Netdevice get statistics request */
  502. static struct net_device_stats *
  503. sl_get_stats(struct net_device *dev)
  504. {
  505. static struct net_device_stats stats;
  506. struct slip *sl = netdev_priv(dev);
  507. #ifdef SL_INCLUDE_CSLIP
  508. struct slcompress *comp;
  509. #endif
  510. memset(&stats, 0, sizeof(struct net_device_stats));
  511. stats.rx_packets = sl->rx_packets;
  512. stats.tx_packets = sl->tx_packets;
  513. stats.rx_bytes = sl->rx_bytes;
  514. stats.tx_bytes = sl->tx_bytes;
  515. stats.rx_dropped = sl->rx_dropped;
  516. stats.tx_dropped = sl->tx_dropped;
  517. stats.tx_errors = sl->tx_errors;
  518. stats.rx_errors = sl->rx_errors;
  519. stats.rx_over_errors = sl->rx_over_errors;
  520. #ifdef SL_INCLUDE_CSLIP
  521. stats.rx_fifo_errors = sl->rx_compressed;
  522. stats.tx_fifo_errors = sl->tx_compressed;
  523. stats.collisions = sl->tx_misses;
  524. comp = sl->slcomp;
  525. if (comp) {
  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 /* CONFIG_INET */
  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->tx_timeout = sl_tx_timeout;
  545. dev->watchdog_timeo = 20*HZ;
  546. #endif
  547. return 0;
  548. }
  549. static void sl_uninit(struct net_device *dev)
  550. {
  551. struct slip *sl = netdev_priv(dev);
  552. sl_free_bufs(sl);
  553. }
  554. static void sl_setup(struct net_device *dev)
  555. {
  556. dev->init = sl_init;
  557. dev->uninit = sl_uninit;
  558. dev->open = sl_open;
  559. dev->destructor = free_netdev;
  560. dev->stop = sl_close;
  561. dev->get_stats = sl_get_stats;
  562. dev->change_mtu = sl_change_mtu;
  563. dev->hard_start_xmit = sl_xmit;
  564. #ifdef CONFIG_SLIP_SMART
  565. dev->do_ioctl = sl_ioctl;
  566. #endif
  567. dev->hard_header_len = 0;
  568. dev->addr_len = 0;
  569. dev->tx_queue_len = 10;
  570. SET_MODULE_OWNER(dev);
  571. /* New-style flags. */
  572. dev->flags = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
  573. }
  574. /******************************************
  575. Routines looking at TTY side.
  576. ******************************************/
  577. static int slip_receive_room(struct tty_struct *tty)
  578. {
  579. return 65536; /* We can handle an infinite amount of data. :-) */
  580. }
  581. /*
  582. * Handle the 'receiver data ready' interrupt.
  583. * This function is called by the 'tty_io' module in the kernel when
  584. * a block of SLIP data has been received, which can now be decapsulated
  585. * and sent on to some IP layer for further processing. This will not
  586. * be re-entered while running but other ldisc functions may be called
  587. * in parallel
  588. */
  589. static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
  590. {
  591. struct slip *sl = (struct slip *) tty->disc_data;
  592. if (!sl || sl->magic != SLIP_MAGIC ||
  593. !netif_running(sl->dev))
  594. return;
  595. /* Read the characters out of the buffer */
  596. while (count--) {
  597. if (fp && *fp++) {
  598. if (!test_and_set_bit(SLF_ERROR, &sl->flags)) {
  599. sl->rx_errors++;
  600. }
  601. cp++;
  602. continue;
  603. }
  604. #ifdef CONFIG_SLIP_MODE_SLIP6
  605. if (sl->mode & SL_MODE_SLIP6)
  606. slip_unesc6(sl, *cp++);
  607. else
  608. #endif
  609. slip_unesc(sl, *cp++);
  610. }
  611. }
  612. /************************************
  613. * slip_open helper routines.
  614. ************************************/
  615. /* Collect hanged up channels */
  616. static void sl_sync(void)
  617. {
  618. int i;
  619. struct net_device *dev;
  620. struct slip *sl;
  621. for (i = 0; i < slip_maxdev; i++) {
  622. if ((dev = slip_devs[i]) == NULL)
  623. break;
  624. sl = netdev_priv(dev);
  625. if (sl->tty || sl->leased)
  626. continue;
  627. if (dev->flags&IFF_UP)
  628. dev_close(dev);
  629. }
  630. }
  631. /* Find a free SLIP channel, and link in this `tty' line. */
  632. static struct slip *
  633. sl_alloc(dev_t line)
  634. {
  635. int i;
  636. int sel = -1;
  637. int score = -1;
  638. struct net_device *dev = NULL;
  639. struct slip *sl;
  640. if (slip_devs == NULL)
  641. return NULL; /* Master array missing ! */
  642. for (i = 0; i < slip_maxdev; i++) {
  643. dev = slip_devs[i];
  644. if (dev == NULL)
  645. break;
  646. sl = netdev_priv(dev);
  647. if (sl->leased) {
  648. if (sl->line != line)
  649. continue;
  650. if (sl->tty)
  651. return NULL;
  652. /* Clear ESCAPE & ERROR flags */
  653. sl->flags &= (1 << SLF_INUSE);
  654. return sl;
  655. }
  656. if (sl->tty)
  657. continue;
  658. if (current->pid == sl->pid) {
  659. if (sl->line == line && score < 3) {
  660. sel = i;
  661. score = 3;
  662. continue;
  663. }
  664. if (score < 2) {
  665. sel = i;
  666. score = 2;
  667. }
  668. continue;
  669. }
  670. if (sl->line == line && score < 1) {
  671. sel = i;
  672. score = 1;
  673. continue;
  674. }
  675. if (score < 0) {
  676. sel = i;
  677. score = 0;
  678. }
  679. }
  680. if (sel >= 0) {
  681. i = sel;
  682. dev = slip_devs[i];
  683. if (score > 1) {
  684. sl = netdev_priv(dev);
  685. sl->flags &= (1 << SLF_INUSE);
  686. return sl;
  687. }
  688. }
  689. /* Sorry, too many, all slots in use */
  690. if (i >= slip_maxdev)
  691. return NULL;
  692. if (dev) {
  693. sl = netdev_priv(dev);
  694. if (test_bit(SLF_INUSE, &sl->flags)) {
  695. unregister_netdevice(dev);
  696. dev = NULL;
  697. slip_devs[i] = NULL;
  698. }
  699. }
  700. if (!dev) {
  701. char name[IFNAMSIZ];
  702. sprintf(name, "sl%d", i);
  703. dev = alloc_netdev(sizeof(*sl), name, sl_setup);
  704. if (!dev)
  705. return NULL;
  706. dev->base_addr = i;
  707. }
  708. sl = netdev_priv(dev);
  709. /* Initialize channel control data */
  710. sl->magic = SLIP_MAGIC;
  711. sl->dev = dev;
  712. spin_lock_init(&sl->lock);
  713. sl->mode = SL_MODE_DEFAULT;
  714. #ifdef CONFIG_SLIP_SMART
  715. init_timer(&sl->keepalive_timer); /* initialize timer_list struct */
  716. sl->keepalive_timer.data=(unsigned long)sl;
  717. sl->keepalive_timer.function=sl_keepalive;
  718. init_timer(&sl->outfill_timer);
  719. sl->outfill_timer.data=(unsigned long)sl;
  720. sl->outfill_timer.function=sl_outfill;
  721. #endif
  722. slip_devs[i] = dev;
  723. return sl;
  724. }
  725. /*
  726. * Open the high-level part of the SLIP channel.
  727. * This function is called by the TTY module when the
  728. * SLIP line discipline is called for. Because we are
  729. * sure the tty line exists, we only have to link it to
  730. * a free SLIP channel...
  731. *
  732. * Called in process context serialized from other ldisc calls.
  733. */
  734. static int slip_open(struct tty_struct *tty)
  735. {
  736. struct slip *sl;
  737. int err;
  738. if(!capable(CAP_NET_ADMIN))
  739. return -EPERM;
  740. /* RTnetlink lock is misused here to serialize concurrent
  741. opens of slip channels. There are better ways, but it is
  742. the simplest one.
  743. */
  744. rtnl_lock();
  745. /* Collect hanged up channels. */
  746. sl_sync();
  747. sl = (struct slip *) tty->disc_data;
  748. err = -EEXIST;
  749. /* First make sure we're not already connected. */
  750. if (sl && sl->magic == SLIP_MAGIC)
  751. goto err_exit;
  752. /* OK. Find a free SLIP channel to use. */
  753. err = -ENFILE;
  754. if ((sl = sl_alloc(tty_devnum(tty))) == NULL)
  755. goto err_exit;
  756. sl->tty = tty;
  757. tty->disc_data = sl;
  758. sl->line = tty_devnum(tty);
  759. sl->pid = current->pid;
  760. /* FIXME: already done before we were called - seems this can go */
  761. if (tty->driver->flush_buffer)
  762. tty->driver->flush_buffer(tty);
  763. if (!test_bit(SLF_INUSE, &sl->flags)) {
  764. /* Perform the low-level SLIP initialization. */
  765. if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
  766. goto err_free_chan;
  767. set_bit(SLF_INUSE, &sl->flags);
  768. if ((err = register_netdevice(sl->dev)))
  769. goto err_free_bufs;
  770. }
  771. #ifdef CONFIG_SLIP_SMART
  772. if (sl->keepalive) {
  773. sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
  774. add_timer (&sl->keepalive_timer);
  775. }
  776. if (sl->outfill) {
  777. sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
  778. add_timer (&sl->outfill_timer);
  779. }
  780. #endif
  781. /* Done. We have linked the TTY line to a channel. */
  782. rtnl_unlock();
  783. return sl->dev->base_addr;
  784. err_free_bufs:
  785. sl_free_bufs(sl);
  786. err_free_chan:
  787. sl->tty = NULL;
  788. tty->disc_data = NULL;
  789. clear_bit(SLF_INUSE, &sl->flags);
  790. err_exit:
  791. rtnl_unlock();
  792. /* Count references from TTY module */
  793. return err;
  794. }
  795. /*
  796. FIXME: 1,2 are fixed 3 was never true anyway.
  797. Let me to blame a bit.
  798. 1. TTY module calls this funstion on soft interrupt.
  799. 2. TTY module calls this function WITH MASKED INTERRUPTS!
  800. 3. TTY module does not notify us about line discipline
  801. shutdown,
  802. Seems, now it is clean. The solution is to consider netdevice and
  803. line discipline sides as two independent threads.
  804. By-product (not desired): sl? does not feel hangups and remains open.
  805. It is supposed, that user level program (dip, diald, slattach...)
  806. will catch SIGHUP and make the rest of work.
  807. I see no way to make more with current tty code. --ANK
  808. */
  809. /*
  810. * Close down a SLIP channel.
  811. * This means flushing out any pending queues, and then returning. This
  812. * call is serialized against other ldisc functions.
  813. */
  814. static void
  815. slip_close(struct tty_struct *tty)
  816. {
  817. struct slip *sl = (struct slip *) tty->disc_data;
  818. /* First make sure we're connected. */
  819. if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
  820. return;
  821. tty->disc_data = NULL;
  822. sl->tty = NULL;
  823. if (!sl->leased)
  824. sl->line = 0;
  825. /* VSV = very important to remove timers */
  826. #ifdef CONFIG_SLIP_SMART
  827. del_timer_sync(&sl->keepalive_timer);
  828. del_timer_sync(&sl->outfill_timer);
  829. #endif
  830. /* Count references from TTY module */
  831. }
  832. /************************************************************************
  833. * STANDARD SLIP ENCAPSULATION *
  834. ************************************************************************/
  835. int
  836. slip_esc(unsigned char *s, unsigned char *d, int len)
  837. {
  838. unsigned char *ptr = d;
  839. unsigned char c;
  840. /*
  841. * Send an initial END character to flush out any
  842. * data that may have accumulated in the receiver
  843. * due to line noise.
  844. */
  845. *ptr++ = END;
  846. /*
  847. * For each byte in the packet, send the appropriate
  848. * character sequence, according to the SLIP protocol.
  849. */
  850. while (len-- > 0) {
  851. switch(c = *s++) {
  852. case END:
  853. *ptr++ = ESC;
  854. *ptr++ = ESC_END;
  855. break;
  856. case ESC:
  857. *ptr++ = ESC;
  858. *ptr++ = ESC_ESC;
  859. break;
  860. default:
  861. *ptr++ = c;
  862. break;
  863. }
  864. }
  865. *ptr++ = END;
  866. return (ptr - d);
  867. }
  868. static void slip_unesc(struct slip *sl, unsigned char s)
  869. {
  870. switch(s) {
  871. case END:
  872. #ifdef CONFIG_SLIP_SMART
  873. /* drop keeptest bit = VSV */
  874. if (test_bit(SLF_KEEPTEST, &sl->flags))
  875. clear_bit(SLF_KEEPTEST, &sl->flags);
  876. #endif
  877. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
  878. sl_bump(sl);
  879. }
  880. clear_bit(SLF_ESCAPE, &sl->flags);
  881. sl->rcount = 0;
  882. return;
  883. case ESC:
  884. set_bit(SLF_ESCAPE, &sl->flags);
  885. return;
  886. case ESC_ESC:
  887. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
  888. s = ESC;
  889. }
  890. break;
  891. case ESC_END:
  892. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
  893. s = END;
  894. }
  895. break;
  896. }
  897. if (!test_bit(SLF_ERROR, &sl->flags)) {
  898. if (sl->rcount < sl->buffsize) {
  899. sl->rbuff[sl->rcount++] = s;
  900. return;
  901. }
  902. sl->rx_over_errors++;
  903. set_bit(SLF_ERROR, &sl->flags);
  904. }
  905. }
  906. #ifdef CONFIG_SLIP_MODE_SLIP6
  907. /************************************************************************
  908. * 6 BIT SLIP ENCAPSULATION *
  909. ************************************************************************/
  910. int
  911. slip_esc6(unsigned char *s, unsigned char *d, int len)
  912. {
  913. unsigned char *ptr = d;
  914. unsigned char c;
  915. int i;
  916. unsigned short v = 0;
  917. short bits = 0;
  918. /*
  919. * Send an initial END character to flush out any
  920. * data that may have accumulated in the receiver
  921. * due to line noise.
  922. */
  923. *ptr++ = 0x70;
  924. /*
  925. * Encode the packet into printable ascii characters
  926. */
  927. for (i = 0; i < len; ++i) {
  928. v = (v << 8) | s[i];
  929. bits += 8;
  930. while (bits >= 6) {
  931. bits -= 6;
  932. c = 0x30 + ((v >> bits) & 0x3F);
  933. *ptr++ = c;
  934. }
  935. }
  936. if (bits) {
  937. c = 0x30 + ((v << (6 - bits)) & 0x3F);
  938. *ptr++ = c;
  939. }
  940. *ptr++ = 0x70;
  941. return ptr - d;
  942. }
  943. void
  944. slip_unesc6(struct slip *sl, unsigned char s)
  945. {
  946. unsigned char c;
  947. if (s == 0x70) {
  948. #ifdef CONFIG_SLIP_SMART
  949. /* drop keeptest bit = VSV */
  950. if (test_bit(SLF_KEEPTEST, &sl->flags))
  951. clear_bit(SLF_KEEPTEST, &sl->flags);
  952. #endif
  953. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
  954. sl_bump(sl);
  955. }
  956. sl->rcount = 0;
  957. sl->xbits = 0;
  958. sl->xdata = 0;
  959. } else if (s >= 0x30 && s < 0x70) {
  960. sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
  961. sl->xbits += 6;
  962. if (sl->xbits >= 8) {
  963. sl->xbits -= 8;
  964. c = (unsigned char)(sl->xdata >> sl->xbits);
  965. if (!test_bit(SLF_ERROR, &sl->flags)) {
  966. if (sl->rcount < sl->buffsize) {
  967. sl->rbuff[sl->rcount++] = c;
  968. return;
  969. }
  970. sl->rx_over_errors++;
  971. set_bit(SLF_ERROR, &sl->flags);
  972. }
  973. }
  974. }
  975. }
  976. #endif /* CONFIG_SLIP_MODE_SLIP6 */
  977. /* Perform I/O control on an active SLIP channel. */
  978. static int slip_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  979. {
  980. struct slip *sl = (struct slip *) tty->disc_data;
  981. unsigned int tmp;
  982. int __user *p = (int __user *)arg;
  983. /* First make sure we're connected. */
  984. if (!sl || sl->magic != SLIP_MAGIC) {
  985. return -EINVAL;
  986. }
  987. switch(cmd) {
  988. case SIOCGIFNAME:
  989. tmp = strlen(sl->dev->name) + 1;
  990. if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
  991. return -EFAULT;
  992. return 0;
  993. case SIOCGIFENCAP:
  994. if (put_user(sl->mode, p))
  995. return -EFAULT;
  996. return 0;
  997. case SIOCSIFENCAP:
  998. if (get_user(tmp, p))
  999. return -EFAULT;
  1000. #ifndef SL_INCLUDE_CSLIP
  1001. if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) {
  1002. return -EINVAL;
  1003. }
  1004. #else
  1005. if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
  1006. (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
  1007. /* return -EINVAL; */
  1008. tmp &= ~SL_MODE_ADAPTIVE;
  1009. }
  1010. #endif
  1011. #ifndef CONFIG_SLIP_MODE_SLIP6
  1012. if (tmp & SL_MODE_SLIP6) {
  1013. return -EINVAL;
  1014. }
  1015. #endif
  1016. sl->mode = tmp;
  1017. sl->dev->type = ARPHRD_SLIP+sl->mode;
  1018. return 0;
  1019. case SIOCSIFHWADDR:
  1020. return -EINVAL;
  1021. #ifdef CONFIG_SLIP_SMART
  1022. /* VSV changes start here */
  1023. case SIOCSKEEPALIVE:
  1024. if (get_user(tmp, p))
  1025. return -EFAULT;
  1026. if (tmp > 255) /* max for unchar */
  1027. return -EINVAL;
  1028. spin_lock_bh(&sl->lock);
  1029. if (!sl->tty) {
  1030. spin_unlock_bh(&sl->lock);
  1031. return -ENODEV;
  1032. }
  1033. if ((sl->keepalive = (unchar) tmp) != 0) {
  1034. mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
  1035. set_bit(SLF_KEEPTEST, &sl->flags);
  1036. } else {
  1037. del_timer (&sl->keepalive_timer);
  1038. }
  1039. spin_unlock_bh(&sl->lock);
  1040. return 0;
  1041. case SIOCGKEEPALIVE:
  1042. if (put_user(sl->keepalive, p))
  1043. return -EFAULT;
  1044. return 0;
  1045. case SIOCSOUTFILL:
  1046. if (get_user(tmp, p))
  1047. return -EFAULT;
  1048. if (tmp > 255) /* max for unchar */
  1049. return -EINVAL;
  1050. spin_lock_bh(&sl->lock);
  1051. if (!sl->tty) {
  1052. spin_unlock_bh(&sl->lock);
  1053. return -ENODEV;
  1054. }
  1055. if ((sl->outfill = (unchar) tmp) != 0){
  1056. mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
  1057. set_bit(SLF_OUTWAIT, &sl->flags);
  1058. } else {
  1059. del_timer (&sl->outfill_timer);
  1060. }
  1061. spin_unlock_bh(&sl->lock);
  1062. return 0;
  1063. case SIOCGOUTFILL:
  1064. if (put_user(sl->outfill, p))
  1065. return -EFAULT;
  1066. return 0;
  1067. /* VSV changes end */
  1068. #endif
  1069. /* Allow stty to read, but not set, the serial port */
  1070. case TCGETS:
  1071. case TCGETA:
  1072. return n_tty_ioctl(tty, file, cmd, arg);
  1073. default:
  1074. return -ENOIOCTLCMD;
  1075. }
  1076. }
  1077. /* VSV changes start here */
  1078. #ifdef CONFIG_SLIP_SMART
  1079. /* function do_ioctl called from net/core/dev.c
  1080. to allow get/set outfill/keepalive parameter
  1081. by ifconfig */
  1082. static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd)
  1083. {
  1084. struct slip *sl = netdev_priv(dev);
  1085. unsigned long *p = (unsigned long *)&rq->ifr_ifru;
  1086. if (sl == NULL) /* Allocation failed ?? */
  1087. return -ENODEV;
  1088. spin_lock_bh(&sl->lock);
  1089. if (!sl->tty) {
  1090. spin_unlock_bh(&sl->lock);
  1091. return -ENODEV;
  1092. }
  1093. switch(cmd){
  1094. case SIOCSKEEPALIVE:
  1095. /* max for unchar */
  1096. if ((unsigned)*p > 255) {
  1097. spin_unlock_bh(&sl->lock);
  1098. return -EINVAL;
  1099. }
  1100. sl->keepalive = (unchar) *p;
  1101. if (sl->keepalive != 0) {
  1102. sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
  1103. mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
  1104. set_bit(SLF_KEEPTEST, &sl->flags);
  1105. } else {
  1106. del_timer(&sl->keepalive_timer);
  1107. }
  1108. break;
  1109. case SIOCGKEEPALIVE:
  1110. *p = sl->keepalive;
  1111. break;
  1112. case SIOCSOUTFILL:
  1113. if ((unsigned)*p > 255) { /* max for unchar */
  1114. spin_unlock_bh(&sl->lock);
  1115. return -EINVAL;
  1116. }
  1117. if ((sl->outfill = (unchar)*p) != 0){
  1118. mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
  1119. set_bit(SLF_OUTWAIT, &sl->flags);
  1120. } else {
  1121. del_timer (&sl->outfill_timer);
  1122. }
  1123. break;
  1124. case SIOCGOUTFILL:
  1125. *p = sl->outfill;
  1126. break;
  1127. case SIOCSLEASE:
  1128. /* Resolve race condition, when ioctl'ing hanged up
  1129. and opened by another process device.
  1130. */
  1131. if (sl->tty != current->signal->tty && sl->pid != current->pid) {
  1132. spin_unlock_bh(&sl->lock);
  1133. return -EPERM;
  1134. }
  1135. sl->leased = 0;
  1136. if (*p)
  1137. sl->leased = 1;
  1138. break;
  1139. case SIOCGLEASE:
  1140. *p = sl->leased;
  1141. };
  1142. spin_unlock_bh(&sl->lock);
  1143. return 0;
  1144. }
  1145. #endif
  1146. /* VSV changes end */
  1147. static struct tty_ldisc sl_ldisc = {
  1148. .owner = THIS_MODULE,
  1149. .magic = TTY_LDISC_MAGIC,
  1150. .name = "slip",
  1151. .open = slip_open,
  1152. .close = slip_close,
  1153. .ioctl = slip_ioctl,
  1154. .receive_buf = slip_receive_buf,
  1155. .receive_room = slip_receive_room,
  1156. .write_wakeup = slip_write_wakeup,
  1157. };
  1158. static int __init slip_init(void)
  1159. {
  1160. int status;
  1161. if (slip_maxdev < 4)
  1162. slip_maxdev = 4; /* Sanity */
  1163. printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
  1164. #ifdef CONFIG_SLIP_MODE_SLIP6
  1165. " (6 bit encapsulation enabled)"
  1166. #endif
  1167. ".\n",
  1168. SLIP_VERSION, slip_maxdev );
  1169. #if defined(SL_INCLUDE_CSLIP)
  1170. printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
  1171. #endif
  1172. #ifdef CONFIG_SLIP_SMART
  1173. printk(KERN_INFO "SLIP linefill/keepalive option.\n");
  1174. #endif
  1175. slip_devs = kmalloc(sizeof(struct net_device *)*slip_maxdev, GFP_KERNEL);
  1176. if (!slip_devs) {
  1177. printk(KERN_ERR "SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n");
  1178. return -ENOMEM;
  1179. }
  1180. /* Clear the pointer array, we allocate devices when we need them */
  1181. memset(slip_devs, 0, sizeof(struct net_device *)*slip_maxdev);
  1182. /* Fill in our line protocol discipline, and register it */
  1183. if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
  1184. printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
  1185. kfree(slip_devs);
  1186. }
  1187. return status;
  1188. }
  1189. static void __exit slip_exit(void)
  1190. {
  1191. int i;
  1192. struct net_device *dev;
  1193. struct slip *sl;
  1194. unsigned long timeout = jiffies + HZ;
  1195. int busy = 0;
  1196. if (slip_devs == NULL)
  1197. return;
  1198. /* First of all: check for active disciplines and hangup them.
  1199. */
  1200. do {
  1201. if (busy) {
  1202. set_current_state(TASK_INTERRUPTIBLE);
  1203. schedule_timeout(HZ / 10);
  1204. }
  1205. busy = 0;
  1206. for (i = 0; i < slip_maxdev; i++) {
  1207. dev = slip_devs[i];
  1208. if (!dev)
  1209. continue;
  1210. sl = netdev_priv(dev);
  1211. spin_lock_bh(&sl->lock);
  1212. if (sl->tty) {
  1213. busy++;
  1214. tty_hangup(sl->tty);
  1215. }
  1216. spin_unlock_bh(&sl->lock);
  1217. }
  1218. } while (busy && time_before(jiffies, timeout));
  1219. for (i = 0; i < slip_maxdev; i++) {
  1220. dev = slip_devs[i];
  1221. if (!dev)
  1222. continue;
  1223. slip_devs[i] = NULL;
  1224. sl = netdev_priv(dev);
  1225. if (sl->tty) {
  1226. printk(KERN_ERR "%s: tty discipline still running\n",
  1227. dev->name);
  1228. /* Intentionally leak the control block. */
  1229. dev->destructor = NULL;
  1230. }
  1231. unregister_netdev(dev);
  1232. }
  1233. kfree(slip_devs);
  1234. slip_devs = NULL;
  1235. if ((i = tty_register_ldisc(N_SLIP, NULL)))
  1236. {
  1237. printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
  1238. }
  1239. }
  1240. module_init(slip_init);
  1241. module_exit(slip_exit);
  1242. #ifdef CONFIG_SLIP_SMART
  1243. /*
  1244. * This is start of the code for multislip style line checking
  1245. * added by Stanislav Voronyi. All changes before marked VSV
  1246. */
  1247. static void sl_outfill(unsigned long sls)
  1248. {
  1249. struct slip *sl=(struct slip *)sls;
  1250. spin_lock(&sl->lock);
  1251. if (sl->tty == NULL)
  1252. goto out;
  1253. if(sl->outfill)
  1254. {
  1255. if( test_bit(SLF_OUTWAIT, &sl->flags) )
  1256. {
  1257. /* no packets were transmitted, do outfill */
  1258. #ifdef CONFIG_SLIP_MODE_SLIP6
  1259. unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
  1260. #else
  1261. unsigned char s = END;
  1262. #endif
  1263. /* put END into tty queue. Is it right ??? */
  1264. if (!netif_queue_stopped(sl->dev))
  1265. {
  1266. /* if device busy no outfill */
  1267. sl->tty->driver->write(sl->tty, &s, 1);
  1268. }
  1269. }
  1270. else
  1271. set_bit(SLF_OUTWAIT, &sl->flags);
  1272. mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
  1273. }
  1274. out:
  1275. spin_unlock(&sl->lock);
  1276. }
  1277. static void sl_keepalive(unsigned long sls)
  1278. {
  1279. struct slip *sl=(struct slip *)sls;
  1280. spin_lock(&sl->lock);
  1281. if (sl->tty == NULL)
  1282. goto out;
  1283. if( sl->keepalive)
  1284. {
  1285. if(test_bit(SLF_KEEPTEST, &sl->flags))
  1286. {
  1287. /* keepalive still high :(, we must hangup */
  1288. if( sl->outfill ) /* outfill timer must be deleted too */
  1289. (void)del_timer(&sl->outfill_timer);
  1290. printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
  1291. tty_hangup(sl->tty); /* this must hangup tty & close slip */
  1292. /* I think we need not something else */
  1293. goto out;
  1294. }
  1295. else
  1296. set_bit(SLF_KEEPTEST, &sl->flags);
  1297. mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
  1298. }
  1299. out:
  1300. spin_unlock(&sl->lock);
  1301. }
  1302. #endif
  1303. MODULE_LICENSE("GPL");
  1304. MODULE_ALIAS_LDISC(N_SLIP);