slip.c 34 KB

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