slip.c 34 KB

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