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