slip.c 33 KB

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