slip.c 33 KB

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