slip.c 33 KB

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