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