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