slip.c 33 KB

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