slip.c 34 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/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 <linux/workqueue.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 (IS_ERR(slcomp))
  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_ni(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, sl->xbuff, len);
  360. else
  361. #endif
  362. count = slip_esc(p, 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. /* Write out any remaining transmit buffer. Scheduled when tty is writable */
  384. static void slip_transmit(struct work_struct *work)
  385. {
  386. struct slip *sl = container_of(work, struct slip, tx_work);
  387. int actual;
  388. spin_lock_bh(&sl->lock);
  389. /* First make sure we're connected. */
  390. if (!sl->tty || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) {
  391. spin_unlock_bh(&sl->lock);
  392. return;
  393. }
  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, &sl->tty->flags);
  399. spin_unlock_bh(&sl->lock);
  400. sl_unlock(sl);
  401. return;
  402. }
  403. actual = sl->tty->ops->write(sl->tty, sl->xhead, sl->xleft);
  404. sl->xleft -= actual;
  405. sl->xhead += actual;
  406. spin_unlock_bh(&sl->lock);
  407. }
  408. /*
  409. * Called by the driver when there's room for more data.
  410. * Schedule the transmit.
  411. */
  412. static void slip_write_wakeup(struct tty_struct *tty)
  413. {
  414. struct slip *sl = tty->disc_data;
  415. schedule_work(&sl->tx_work);
  416. }
  417. static void sl_tx_timeout(struct net_device *dev)
  418. {
  419. struct slip *sl = netdev_priv(dev);
  420. spin_lock(&sl->lock);
  421. if (netif_queue_stopped(dev)) {
  422. if (!netif_running(dev))
  423. goto out;
  424. /* May be we must check transmitter timeout here ?
  425. * 14 Oct 1994 Dmitry Gorodchanin.
  426. */
  427. #ifdef SL_CHECK_TRANSMIT
  428. if (time_before(jiffies, dev_trans_start(dev) + 20 * HZ)) {
  429. /* 20 sec timeout not reached */
  430. goto out;
  431. }
  432. printk(KERN_WARNING "%s: transmit timed out, %s?\n",
  433. dev->name,
  434. (tty_chars_in_buffer(sl->tty) || sl->xleft) ?
  435. "bad line quality" : "driver error");
  436. sl->xleft = 0;
  437. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  438. sl_unlock(sl);
  439. #endif
  440. }
  441. out:
  442. spin_unlock(&sl->lock);
  443. }
  444. /* Encapsulate an IP datagram and kick it into a TTY queue. */
  445. static netdev_tx_t
  446. sl_xmit(struct sk_buff *skb, struct net_device *dev)
  447. {
  448. struct slip *sl = netdev_priv(dev);
  449. spin_lock(&sl->lock);
  450. if (!netif_running(dev)) {
  451. spin_unlock(&sl->lock);
  452. printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name);
  453. dev_kfree_skb(skb);
  454. return NETDEV_TX_OK;
  455. }
  456. if (sl->tty == NULL) {
  457. spin_unlock(&sl->lock);
  458. dev_kfree_skb(skb);
  459. return NETDEV_TX_OK;
  460. }
  461. sl_lock(sl);
  462. dev->stats.tx_bytes += skb->len;
  463. sl_encaps(sl, skb->data, skb->len);
  464. spin_unlock(&sl->lock);
  465. dev_kfree_skb(skb);
  466. return NETDEV_TX_OK;
  467. }
  468. /******************************************
  469. * Routines looking at netdevice side.
  470. ******************************************/
  471. /* Netdevice UP -> DOWN routine */
  472. static int
  473. sl_close(struct net_device *dev)
  474. {
  475. struct slip *sl = netdev_priv(dev);
  476. spin_lock_bh(&sl->lock);
  477. if (sl->tty)
  478. /* TTY discipline is running. */
  479. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  480. netif_stop_queue(dev);
  481. sl->rcount = 0;
  482. sl->xleft = 0;
  483. spin_unlock_bh(&sl->lock);
  484. return 0;
  485. }
  486. /* Netdevice DOWN -> UP routine */
  487. static int sl_open(struct net_device *dev)
  488. {
  489. struct slip *sl = netdev_priv(dev);
  490. if (sl->tty == NULL)
  491. return -ENODEV;
  492. sl->flags &= (1 << SLF_INUSE);
  493. netif_start_queue(dev);
  494. return 0;
  495. }
  496. /* Netdevice change MTU request */
  497. static int sl_change_mtu(struct net_device *dev, int new_mtu)
  498. {
  499. struct slip *sl = netdev_priv(dev);
  500. if (new_mtu < 68 || new_mtu > 65534)
  501. return -EINVAL;
  502. if (new_mtu != dev->mtu)
  503. return sl_realloc_bufs(sl, new_mtu);
  504. return 0;
  505. }
  506. /* Netdevice get statistics request */
  507. static struct rtnl_link_stats64 *
  508. sl_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  509. {
  510. struct net_device_stats *devstats = &dev->stats;
  511. #ifdef SL_INCLUDE_CSLIP
  512. struct slip *sl = netdev_priv(dev);
  513. struct slcompress *comp = sl->slcomp;
  514. #endif
  515. stats->rx_packets = devstats->rx_packets;
  516. stats->tx_packets = devstats->tx_packets;
  517. stats->rx_bytes = devstats->rx_bytes;
  518. stats->tx_bytes = devstats->tx_bytes;
  519. stats->rx_dropped = devstats->rx_dropped;
  520. stats->tx_dropped = devstats->tx_dropped;
  521. stats->tx_errors = devstats->tx_errors;
  522. stats->rx_errors = devstats->rx_errors;
  523. stats->rx_over_errors = devstats->rx_over_errors;
  524. #ifdef SL_INCLUDE_CSLIP
  525. if (comp) {
  526. /* Generic compressed statistics */
  527. stats->rx_compressed = comp->sls_i_compressed;
  528. stats->tx_compressed = comp->sls_o_compressed;
  529. /* Are we really still needs this? */
  530. stats->rx_fifo_errors += comp->sls_i_compressed;
  531. stats->rx_dropped += comp->sls_i_tossed;
  532. stats->tx_fifo_errors += comp->sls_o_compressed;
  533. stats->collisions += comp->sls_o_misses;
  534. }
  535. #endif
  536. return stats;
  537. }
  538. /* Netdevice register callback */
  539. static int sl_init(struct net_device *dev)
  540. {
  541. struct slip *sl = netdev_priv(dev);
  542. /*
  543. * Finish setting up the DEVICE info.
  544. */
  545. dev->mtu = sl->mtu;
  546. dev->type = ARPHRD_SLIP + sl->mode;
  547. #ifdef SL_CHECK_TRANSMIT
  548. dev->watchdog_timeo = 20*HZ;
  549. #endif
  550. return 0;
  551. }
  552. static void sl_uninit(struct net_device *dev)
  553. {
  554. struct slip *sl = netdev_priv(dev);
  555. sl_free_bufs(sl);
  556. }
  557. /* Hook the destructor so we can free slip devices at the right point in time */
  558. static void sl_free_netdev(struct net_device *dev)
  559. {
  560. int i = dev->base_addr;
  561. free_netdev(dev);
  562. slip_devs[i] = NULL;
  563. }
  564. static const struct net_device_ops sl_netdev_ops = {
  565. .ndo_init = sl_init,
  566. .ndo_uninit = sl_uninit,
  567. .ndo_open = sl_open,
  568. .ndo_stop = sl_close,
  569. .ndo_start_xmit = sl_xmit,
  570. .ndo_get_stats64 = sl_get_stats64,
  571. .ndo_change_mtu = sl_change_mtu,
  572. .ndo_tx_timeout = sl_tx_timeout,
  573. #ifdef CONFIG_SLIP_SMART
  574. .ndo_do_ioctl = sl_ioctl,
  575. #endif
  576. };
  577. static void sl_setup(struct net_device *dev)
  578. {
  579. dev->netdev_ops = &sl_netdev_ops;
  580. dev->destructor = sl_free_netdev;
  581. dev->hard_header_len = 0;
  582. dev->addr_len = 0;
  583. dev->tx_queue_len = 10;
  584. /* New-style flags. */
  585. dev->flags = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
  586. }
  587. /******************************************
  588. Routines looking at TTY side.
  589. ******************************************/
  590. /*
  591. * Handle the 'receiver data ready' interrupt.
  592. * This function is called by the 'tty_io' module in the kernel when
  593. * a block of SLIP data has been received, which can now be decapsulated
  594. * and sent on to some IP layer for further processing. This will not
  595. * be re-entered while running but other ldisc functions may be called
  596. * in parallel
  597. */
  598. static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  599. char *fp, int count)
  600. {
  601. struct slip *sl = tty->disc_data;
  602. if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev))
  603. return;
  604. /* Read the characters out of the buffer */
  605. while (count--) {
  606. if (fp && *fp++) {
  607. if (!test_and_set_bit(SLF_ERROR, &sl->flags))
  608. sl->dev->stats.rx_errors++;
  609. cp++;
  610. continue;
  611. }
  612. #ifdef CONFIG_SLIP_MODE_SLIP6
  613. if (sl->mode & SL_MODE_SLIP6)
  614. slip_unesc6(sl, *cp++);
  615. else
  616. #endif
  617. slip_unesc(sl, *cp++);
  618. }
  619. }
  620. /************************************
  621. * slip_open helper routines.
  622. ************************************/
  623. /* Collect hanged up channels */
  624. static void sl_sync(void)
  625. {
  626. int i;
  627. struct net_device *dev;
  628. struct slip *sl;
  629. for (i = 0; i < slip_maxdev; i++) {
  630. dev = slip_devs[i];
  631. if (dev == NULL)
  632. break;
  633. sl = netdev_priv(dev);
  634. if (sl->tty || sl->leased)
  635. continue;
  636. if (dev->flags & IFF_UP)
  637. dev_close(dev);
  638. }
  639. }
  640. /* Find a free SLIP channel, and link in this `tty' line. */
  641. static struct slip *sl_alloc(dev_t line)
  642. {
  643. int i;
  644. char name[IFNAMSIZ];
  645. struct net_device *dev = NULL;
  646. struct slip *sl;
  647. for (i = 0; i < slip_maxdev; i++) {
  648. dev = slip_devs[i];
  649. if (dev == NULL)
  650. break;
  651. }
  652. /* Sorry, too many, all slots in use */
  653. if (i >= slip_maxdev)
  654. return NULL;
  655. sprintf(name, "sl%d", i);
  656. dev = alloc_netdev(sizeof(*sl), name, NET_NAME_UNKNOWN, sl_setup);
  657. if (!dev)
  658. return NULL;
  659. dev->base_addr = i;
  660. sl = netdev_priv(dev);
  661. /* Initialize channel control data */
  662. sl->magic = SLIP_MAGIC;
  663. sl->dev = dev;
  664. spin_lock_init(&sl->lock);
  665. INIT_WORK(&sl->tx_work, slip_transmit);
  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->pid = current->pid;
  716. if (!test_bit(SLF_INUSE, &sl->flags)) {
  717. /* Perform the low-level SLIP initialization. */
  718. err = sl_alloc_bufs(sl, SL_MTU);
  719. if (err)
  720. goto err_free_chan;
  721. set_bit(SLF_INUSE, &sl->flags);
  722. err = register_netdevice(sl->dev);
  723. if (err)
  724. goto err_free_bufs;
  725. }
  726. #ifdef CONFIG_SLIP_SMART
  727. if (sl->keepalive) {
  728. sl->keepalive_timer.expires = jiffies + sl->keepalive * HZ;
  729. add_timer(&sl->keepalive_timer);
  730. }
  731. if (sl->outfill) {
  732. sl->outfill_timer.expires = jiffies + sl->outfill * HZ;
  733. add_timer(&sl->outfill_timer);
  734. }
  735. #endif
  736. /* Done. We have linked the TTY line to a channel. */
  737. rtnl_unlock();
  738. tty->receive_room = 65536; /* We don't flow control */
  739. /* TTY layer expects 0 on success */
  740. return 0;
  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. spin_lock_bh(&sl->lock);
  766. tty->disc_data = NULL;
  767. sl->tty = NULL;
  768. spin_unlock_bh(&sl->lock);
  769. flush_work(&sl->tx_work);
  770. /* VSV = very important to remove timers */
  771. #ifdef CONFIG_SLIP_SMART
  772. del_timer_sync(&sl->keepalive_timer);
  773. del_timer_sync(&sl->outfill_timer);
  774. #endif
  775. /* Flush network side */
  776. unregister_netdev(sl->dev);
  777. /* This will complete via sl_free_netdev */
  778. }
  779. static int slip_hangup(struct tty_struct *tty)
  780. {
  781. slip_close(tty);
  782. return 0;
  783. }
  784. /************************************************************************
  785. * STANDARD SLIP ENCAPSULATION *
  786. ************************************************************************/
  787. static int slip_esc(unsigned char *s, unsigned char *d, int len)
  788. {
  789. unsigned char *ptr = d;
  790. unsigned char c;
  791. /*
  792. * Send an initial END character to flush out any
  793. * data that may have accumulated in the receiver
  794. * due to line noise.
  795. */
  796. *ptr++ = END;
  797. /*
  798. * For each byte in the packet, send the appropriate
  799. * character sequence, according to the SLIP protocol.
  800. */
  801. while (len-- > 0) {
  802. switch (c = *s++) {
  803. case END:
  804. *ptr++ = ESC;
  805. *ptr++ = ESC_END;
  806. break;
  807. case ESC:
  808. *ptr++ = ESC;
  809. *ptr++ = ESC_ESC;
  810. break;
  811. default:
  812. *ptr++ = c;
  813. break;
  814. }
  815. }
  816. *ptr++ = END;
  817. return ptr - d;
  818. }
  819. static void slip_unesc(struct slip *sl, unsigned char s)
  820. {
  821. switch (s) {
  822. case END:
  823. #ifdef CONFIG_SLIP_SMART
  824. /* drop keeptest bit = VSV */
  825. if (test_bit(SLF_KEEPTEST, &sl->flags))
  826. clear_bit(SLF_KEEPTEST, &sl->flags);
  827. #endif
  828. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  829. (sl->rcount > 2))
  830. sl_bump(sl);
  831. clear_bit(SLF_ESCAPE, &sl->flags);
  832. sl->rcount = 0;
  833. return;
  834. case ESC:
  835. set_bit(SLF_ESCAPE, &sl->flags);
  836. return;
  837. case ESC_ESC:
  838. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
  839. s = ESC;
  840. break;
  841. case ESC_END:
  842. if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
  843. s = END;
  844. break;
  845. }
  846. if (!test_bit(SLF_ERROR, &sl->flags)) {
  847. if (sl->rcount < sl->buffsize) {
  848. sl->rbuff[sl->rcount++] = s;
  849. return;
  850. }
  851. sl->dev->stats.rx_over_errors++;
  852. set_bit(SLF_ERROR, &sl->flags);
  853. }
  854. }
  855. #ifdef CONFIG_SLIP_MODE_SLIP6
  856. /************************************************************************
  857. * 6 BIT SLIP ENCAPSULATION *
  858. ************************************************************************/
  859. static int slip_esc6(unsigned char *s, unsigned char *d, int len)
  860. {
  861. unsigned char *ptr = d;
  862. unsigned char c;
  863. int i;
  864. unsigned short v = 0;
  865. short bits = 0;
  866. /*
  867. * Send an initial END character to flush out any
  868. * data that may have accumulated in the receiver
  869. * due to line noise.
  870. */
  871. *ptr++ = 0x70;
  872. /*
  873. * Encode the packet into printable ascii characters
  874. */
  875. for (i = 0; i < len; ++i) {
  876. v = (v << 8) | s[i];
  877. bits += 8;
  878. while (bits >= 6) {
  879. bits -= 6;
  880. c = 0x30 + ((v >> bits) & 0x3F);
  881. *ptr++ = c;
  882. }
  883. }
  884. if (bits) {
  885. c = 0x30 + ((v << (6 - bits)) & 0x3F);
  886. *ptr++ = c;
  887. }
  888. *ptr++ = 0x70;
  889. return ptr - d;
  890. }
  891. static void slip_unesc6(struct slip *sl, unsigned char s)
  892. {
  893. unsigned char c;
  894. if (s == 0x70) {
  895. #ifdef CONFIG_SLIP_SMART
  896. /* drop keeptest bit = VSV */
  897. if (test_bit(SLF_KEEPTEST, &sl->flags))
  898. clear_bit(SLF_KEEPTEST, &sl->flags);
  899. #endif
  900. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  901. (sl->rcount > 2))
  902. sl_bump(sl);
  903. sl->rcount = 0;
  904. sl->xbits = 0;
  905. sl->xdata = 0;
  906. } else if (s >= 0x30 && s < 0x70) {
  907. sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
  908. sl->xbits += 6;
  909. if (sl->xbits >= 8) {
  910. sl->xbits -= 8;
  911. c = (unsigned char)(sl->xdata >> sl->xbits);
  912. if (!test_bit(SLF_ERROR, &sl->flags)) {
  913. if (sl->rcount < sl->buffsize) {
  914. sl->rbuff[sl->rcount++] = c;
  915. return;
  916. }
  917. sl->dev->stats.rx_over_errors++;
  918. set_bit(SLF_ERROR, &sl->flags);
  919. }
  920. }
  921. }
  922. }
  923. #endif /* CONFIG_SLIP_MODE_SLIP6 */
  924. /* Perform I/O control on an active SLIP channel. */
  925. static int slip_ioctl(struct tty_struct *tty, struct file *file,
  926. unsigned int cmd, unsigned long arg)
  927. {
  928. struct slip *sl = tty->disc_data;
  929. unsigned int tmp;
  930. int __user *p = (int __user *)arg;
  931. /* First make sure we're connected. */
  932. if (!sl || sl->magic != SLIP_MAGIC)
  933. return -EINVAL;
  934. switch (cmd) {
  935. case SIOCGIFNAME:
  936. tmp = strlen(sl->dev->name) + 1;
  937. if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
  938. return -EFAULT;
  939. return 0;
  940. case SIOCGIFENCAP:
  941. if (put_user(sl->mode, p))
  942. return -EFAULT;
  943. return 0;
  944. case SIOCSIFENCAP:
  945. if (get_user(tmp, p))
  946. return -EFAULT;
  947. #ifndef SL_INCLUDE_CSLIP
  948. if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE))
  949. return -EINVAL;
  950. #else
  951. if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
  952. (SL_MODE_ADAPTIVE | SL_MODE_CSLIP))
  953. /* return -EINVAL; */
  954. tmp &= ~SL_MODE_ADAPTIVE;
  955. #endif
  956. #ifndef CONFIG_SLIP_MODE_SLIP6
  957. if (tmp & SL_MODE_SLIP6)
  958. return -EINVAL;
  959. #endif
  960. sl->mode = tmp;
  961. sl->dev->type = ARPHRD_SLIP + sl->mode;
  962. return 0;
  963. case SIOCSIFHWADDR:
  964. return -EINVAL;
  965. #ifdef CONFIG_SLIP_SMART
  966. /* VSV changes start here */
  967. case SIOCSKEEPALIVE:
  968. if (get_user(tmp, p))
  969. return -EFAULT;
  970. if (tmp > 255) /* max for unchar */
  971. return -EINVAL;
  972. spin_lock_bh(&sl->lock);
  973. if (!sl->tty) {
  974. spin_unlock_bh(&sl->lock);
  975. return -ENODEV;
  976. }
  977. sl->keepalive = (u8)tmp;
  978. if (sl->keepalive != 0) {
  979. mod_timer(&sl->keepalive_timer,
  980. jiffies + sl->keepalive * HZ);
  981. set_bit(SLF_KEEPTEST, &sl->flags);
  982. } else
  983. del_timer(&sl->keepalive_timer);
  984. spin_unlock_bh(&sl->lock);
  985. return 0;
  986. case SIOCGKEEPALIVE:
  987. if (put_user(sl->keepalive, p))
  988. return -EFAULT;
  989. return 0;
  990. case SIOCSOUTFILL:
  991. if (get_user(tmp, p))
  992. return -EFAULT;
  993. if (tmp > 255) /* max for unchar */
  994. return -EINVAL;
  995. spin_lock_bh(&sl->lock);
  996. if (!sl->tty) {
  997. spin_unlock_bh(&sl->lock);
  998. return -ENODEV;
  999. }
  1000. sl->outfill = (u8)tmp;
  1001. if (sl->outfill != 0) {
  1002. mod_timer(&sl->outfill_timer,
  1003. jiffies + sl->outfill * HZ);
  1004. set_bit(SLF_OUTWAIT, &sl->flags);
  1005. } else
  1006. del_timer(&sl->outfill_timer);
  1007. spin_unlock_bh(&sl->lock);
  1008. return 0;
  1009. case SIOCGOUTFILL:
  1010. if (put_user(sl->outfill, p))
  1011. return -EFAULT;
  1012. return 0;
  1013. /* VSV changes end */
  1014. #endif
  1015. default:
  1016. return tty_mode_ioctl(tty, file, cmd, arg);
  1017. }
  1018. }
  1019. #ifdef CONFIG_COMPAT
  1020. static long slip_compat_ioctl(struct tty_struct *tty, struct file *file,
  1021. unsigned int cmd, unsigned long arg)
  1022. {
  1023. switch (cmd) {
  1024. case SIOCGIFNAME:
  1025. case SIOCGIFENCAP:
  1026. case SIOCSIFENCAP:
  1027. case SIOCSIFHWADDR:
  1028. case SIOCSKEEPALIVE:
  1029. case SIOCGKEEPALIVE:
  1030. case SIOCSOUTFILL:
  1031. case SIOCGOUTFILL:
  1032. return slip_ioctl(tty, file, cmd,
  1033. (unsigned long)compat_ptr(arg));
  1034. }
  1035. return -ENOIOCTLCMD;
  1036. }
  1037. #endif
  1038. /* VSV changes start here */
  1039. #ifdef CONFIG_SLIP_SMART
  1040. /* function do_ioctl called from net/core/dev.c
  1041. to allow get/set outfill/keepalive parameter
  1042. by ifconfig */
  1043. static int sl_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1044. {
  1045. struct slip *sl = netdev_priv(dev);
  1046. unsigned long *p = (unsigned long *)&rq->ifr_ifru;
  1047. if (sl == NULL) /* Allocation failed ?? */
  1048. return -ENODEV;
  1049. spin_lock_bh(&sl->lock);
  1050. if (!sl->tty) {
  1051. spin_unlock_bh(&sl->lock);
  1052. return -ENODEV;
  1053. }
  1054. switch (cmd) {
  1055. case SIOCSKEEPALIVE:
  1056. /* max for unchar */
  1057. if ((unsigned)*p > 255) {
  1058. spin_unlock_bh(&sl->lock);
  1059. return -EINVAL;
  1060. }
  1061. sl->keepalive = (u8)*p;
  1062. if (sl->keepalive != 0) {
  1063. sl->keepalive_timer.expires =
  1064. jiffies + sl->keepalive * HZ;
  1065. mod_timer(&sl->keepalive_timer,
  1066. jiffies + sl->keepalive * HZ);
  1067. set_bit(SLF_KEEPTEST, &sl->flags);
  1068. } else
  1069. del_timer(&sl->keepalive_timer);
  1070. break;
  1071. case SIOCGKEEPALIVE:
  1072. *p = sl->keepalive;
  1073. break;
  1074. case SIOCSOUTFILL:
  1075. if ((unsigned)*p > 255) { /* max for unchar */
  1076. spin_unlock_bh(&sl->lock);
  1077. return -EINVAL;
  1078. }
  1079. sl->outfill = (u8)*p;
  1080. if (sl->outfill != 0) {
  1081. mod_timer(&sl->outfill_timer,
  1082. jiffies + sl->outfill * HZ);
  1083. set_bit(SLF_OUTWAIT, &sl->flags);
  1084. } else
  1085. del_timer(&sl->outfill_timer);
  1086. break;
  1087. case SIOCGOUTFILL:
  1088. *p = sl->outfill;
  1089. break;
  1090. case SIOCSLEASE:
  1091. /* Resolve race condition, when ioctl'ing hanged up
  1092. and opened by another process device.
  1093. */
  1094. if (sl->tty != current->signal->tty &&
  1095. sl->pid != current->pid) {
  1096. spin_unlock_bh(&sl->lock);
  1097. return -EPERM;
  1098. }
  1099. sl->leased = 0;
  1100. if (*p)
  1101. sl->leased = 1;
  1102. break;
  1103. case SIOCGLEASE:
  1104. *p = sl->leased;
  1105. }
  1106. spin_unlock_bh(&sl->lock);
  1107. return 0;
  1108. }
  1109. #endif
  1110. /* VSV changes end */
  1111. static struct tty_ldisc_ops sl_ldisc = {
  1112. .owner = THIS_MODULE,
  1113. .magic = TTY_LDISC_MAGIC,
  1114. .name = "slip",
  1115. .open = slip_open,
  1116. .close = slip_close,
  1117. .hangup = slip_hangup,
  1118. .ioctl = slip_ioctl,
  1119. #ifdef CONFIG_COMPAT
  1120. .compat_ioctl = slip_compat_ioctl,
  1121. #endif
  1122. .receive_buf = slip_receive_buf,
  1123. .write_wakeup = slip_write_wakeup,
  1124. };
  1125. static int __init slip_init(void)
  1126. {
  1127. int status;
  1128. if (slip_maxdev < 4)
  1129. slip_maxdev = 4; /* Sanity */
  1130. printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
  1131. #ifdef CONFIG_SLIP_MODE_SLIP6
  1132. " (6 bit encapsulation enabled)"
  1133. #endif
  1134. ".\n",
  1135. SLIP_VERSION, slip_maxdev);
  1136. #if defined(SL_INCLUDE_CSLIP)
  1137. printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
  1138. #endif
  1139. #ifdef CONFIG_SLIP_SMART
  1140. printk(KERN_INFO "SLIP linefill/keepalive option.\n");
  1141. #endif
  1142. slip_devs = kzalloc(sizeof(struct net_device *)*slip_maxdev,
  1143. GFP_KERNEL);
  1144. if (!slip_devs)
  1145. return -ENOMEM;
  1146. /* Fill in our line protocol discipline, and register it */
  1147. status = tty_register_ldisc(N_SLIP, &sl_ldisc);
  1148. if (status != 0) {
  1149. printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
  1150. kfree(slip_devs);
  1151. }
  1152. return status;
  1153. }
  1154. static void __exit slip_exit(void)
  1155. {
  1156. int i;
  1157. struct net_device *dev;
  1158. struct slip *sl;
  1159. unsigned long timeout = jiffies + HZ;
  1160. int busy = 0;
  1161. if (slip_devs == NULL)
  1162. return;
  1163. /* First of all: check for active disciplines and hangup them.
  1164. */
  1165. do {
  1166. if (busy)
  1167. msleep_interruptible(100);
  1168. busy = 0;
  1169. for (i = 0; i < slip_maxdev; i++) {
  1170. dev = slip_devs[i];
  1171. if (!dev)
  1172. continue;
  1173. sl = netdev_priv(dev);
  1174. spin_lock_bh(&sl->lock);
  1175. if (sl->tty) {
  1176. busy++;
  1177. tty_hangup(sl->tty);
  1178. }
  1179. spin_unlock_bh(&sl->lock);
  1180. }
  1181. } while (busy && time_before(jiffies, timeout));
  1182. /* FIXME: hangup is async so we should wait when doing this second
  1183. phase */
  1184. for (i = 0; i < slip_maxdev; i++) {
  1185. dev = slip_devs[i];
  1186. if (!dev)
  1187. continue;
  1188. slip_devs[i] = NULL;
  1189. sl = netdev_priv(dev);
  1190. if (sl->tty) {
  1191. printk(KERN_ERR "%s: tty discipline still running\n",
  1192. dev->name);
  1193. /* Intentionally leak the control block. */
  1194. dev->destructor = NULL;
  1195. }
  1196. unregister_netdev(dev);
  1197. }
  1198. kfree(slip_devs);
  1199. slip_devs = NULL;
  1200. i = tty_unregister_ldisc(N_SLIP);
  1201. if (i != 0)
  1202. printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
  1203. }
  1204. module_init(slip_init);
  1205. module_exit(slip_exit);
  1206. #ifdef CONFIG_SLIP_SMART
  1207. /*
  1208. * This is start of the code for multislip style line checking
  1209. * added by Stanislav Voronyi. All changes before marked VSV
  1210. */
  1211. static void sl_outfill(unsigned long sls)
  1212. {
  1213. struct slip *sl = (struct slip *)sls;
  1214. spin_lock(&sl->lock);
  1215. if (sl->tty == NULL)
  1216. goto out;
  1217. if (sl->outfill) {
  1218. if (test_bit(SLF_OUTWAIT, &sl->flags)) {
  1219. /* no packets were transmitted, do outfill */
  1220. #ifdef CONFIG_SLIP_MODE_SLIP6
  1221. unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
  1222. #else
  1223. unsigned char s = END;
  1224. #endif
  1225. /* put END into tty queue. Is it right ??? */
  1226. if (!netif_queue_stopped(sl->dev)) {
  1227. /* if device busy no outfill */
  1228. sl->tty->ops->write(sl->tty, &s, 1);
  1229. }
  1230. } else
  1231. set_bit(SLF_OUTWAIT, &sl->flags);
  1232. mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
  1233. }
  1234. out:
  1235. spin_unlock(&sl->lock);
  1236. }
  1237. static void sl_keepalive(unsigned long sls)
  1238. {
  1239. struct slip *sl = (struct slip *)sls;
  1240. spin_lock(&sl->lock);
  1241. if (sl->tty == NULL)
  1242. goto out;
  1243. if (sl->keepalive) {
  1244. if (test_bit(SLF_KEEPTEST, &sl->flags)) {
  1245. /* keepalive still high :(, we must hangup */
  1246. if (sl->outfill)
  1247. /* outfill timer must be deleted too */
  1248. (void)del_timer(&sl->outfill_timer);
  1249. printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
  1250. /* this must hangup tty & close slip */
  1251. tty_hangup(sl->tty);
  1252. /* I think we need not something else */
  1253. goto out;
  1254. } else
  1255. set_bit(SLF_KEEPTEST, &sl->flags);
  1256. mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
  1257. }
  1258. out:
  1259. spin_unlock(&sl->lock);
  1260. }
  1261. #endif
  1262. MODULE_LICENSE("GPL");
  1263. MODULE_ALIAS_LDISC(N_SLIP);