1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57#define SL_CHECK_TRANSMIT
58#include <linux/module.h>
59#include <linux/moduleparam.h>
60
61#include <asm/system.h>
62#include <asm/uaccess.h>
63#include <linux/bitops.h>
64#include <linux/string.h>
65#include <linux/mm.h>
66#include <linux/interrupt.h>
67#include <linux/in.h>
68#include <linux/tty.h>
69#include <linux/errno.h>
70#include <linux/netdevice.h>
71#include <linux/etherdevice.h>
72#include <linux/skbuff.h>
73#include <linux/rtnetlink.h>
74#include <linux/if_arp.h>
75#include <linux/if_slip.h>
76#include <linux/delay.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
87static struct net_device **slip_devs;
88
89static int slip_maxdev = SL_NRUNIT;
90module_param(slip_maxdev, int, 0);
91MODULE_PARM_DESC(slip_maxdev, "Maximum number of slip devices");
92
93static int slip_esc(unsigned char *p, unsigned char *d, int len);
94static void slip_unesc(struct slip *sl, unsigned char c);
95#ifdef CONFIG_SLIP_MODE_SLIP6
96static int slip_esc6(unsigned char *p, unsigned char *d, int len);
97static void slip_unesc6(struct slip *sl, unsigned char c);
98#endif
99#ifdef CONFIG_SLIP_SMART
100static void sl_keepalive(unsigned long sls);
101static void sl_outfill(unsigned long sls);
102static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd);
103#endif
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120static int
121sl_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
134
135
136
137
138
139 len = mtu * 2;
140
141
142
143
144
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
186err_exit:
187#ifdef SL_INCLUDE_CSLIP
188 kfree(cbuff);
189 if (slcomp)
190 slhc_free(slcomp);
191#endif
192 kfree(xbuff);
193 kfree(rbuff);
194 return err;
195}
196
197
198static void
199sl_free_bufs(struct slip *sl)
200{
201
202 kfree(xchg(&sl->rbuff, NULL));
203 kfree(xchg(&sl->xbuff, NULL));
204#ifdef SL_INCLUDE_CSLIP
205 kfree(xchg(&sl->cbuff, NULL));
206 slhc_free(xchg(&sl->slcomp, NULL));
207#endif
208}
209
210
211
212
213
214static int sl_realloc_bufs(struct slip *sl, int mtu)
215{
216 int err = 0;
217 struct net_device *dev = sl->dev;
218 unsigned char *xbuff, *rbuff;
219#ifdef SL_INCLUDE_CSLIP
220 unsigned char *cbuff;
221#endif
222 int len = mtu * 2;
223
224
225
226
227
228
229 if (len < 576 * 2)
230 len = 576 * 2;
231
232 xbuff = kmalloc(len + 4, GFP_ATOMIC);
233 rbuff = kmalloc(len + 4, GFP_ATOMIC);
234#ifdef SL_INCLUDE_CSLIP
235 cbuff = kmalloc(len + 4, GFP_ATOMIC);
236#endif
237
238
239#ifdef SL_INCLUDE_CSLIP
240 if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
241#else
242 if (xbuff == NULL || rbuff == NULL) {
243#endif
244 if (mtu >= sl->mtu) {
245 printk(KERN_WARNING "%s: unable to grow slip buffers, MTU change cancelled.\n",
246 dev->name);
247 err = -ENOBUFS;
248 }
249 goto done;
250 }
251
252 spin_lock_bh(&sl->lock);
253
254 err = -ENODEV;
255 if (sl->tty == NULL)
256 goto done_on_bh;
257
258 xbuff = xchg(&sl->xbuff, xbuff);
259 rbuff = xchg(&sl->rbuff, rbuff);
260#ifdef SL_INCLUDE_CSLIP
261 cbuff = xchg(&sl->cbuff, cbuff);
262#endif
263 if (sl->xleft) {
264 if (sl->xleft <= len) {
265 memcpy(sl->xbuff, sl->xhead, sl->xleft);
266 } else {
267 sl->xleft = 0;
268 sl->tx_dropped++;
269 }
270 }
271 sl->xhead = sl->xbuff;
272
273 if (sl->rcount) {
274 if (sl->rcount <= len) {
275 memcpy(sl->rbuff, rbuff, sl->rcount);
276 } else {
277 sl->rcount = 0;
278 sl->rx_over_errors++;
279 set_bit(SLF_ERROR, &sl->flags);
280 }
281 }
282 sl->mtu = mtu;
283 dev->mtu = mtu;
284 sl->buffsize = len;
285 err = 0;
286
287done_on_bh:
288 spin_unlock_bh(&sl->lock);
289
290done:
291 kfree(xbuff);
292 kfree(rbuff);
293#ifdef SL_INCLUDE_CSLIP
294 kfree(cbuff);
295#endif
296 return err;
297}
298
299
300
301static inline void
302sl_lock(struct slip *sl)
303{
304 netif_stop_queue(sl->dev);
305}
306
307
308
309static inline void
310sl_unlock(struct slip *sl)
311{
312 netif_wake_queue(sl->dev);
313}
314
315
316static void
317sl_bump(struct slip *sl)
318{
319 struct sk_buff *skb;
320 int count;
321
322 count = sl->rcount;
323#ifdef SL_INCLUDE_CSLIP
324 if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
325 unsigned char c;
326 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
327
328 if (!(sl->mode & SL_MODE_CSLIP)) {
329 printk(KERN_WARNING "%s: compressed packet ignored\n", sl->dev->name);
330 return;
331 }
332
333 if (count + 80 > sl->buffsize) {
334 sl->rx_over_errors++;
335 return;
336 }
337 count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
338 if (count <= 0) {
339 return;
340 }
341 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
342 if (!(sl->mode & SL_MODE_CSLIP)) {
343
344 sl->mode |= SL_MODE_CSLIP;
345 sl->mode &= ~SL_MODE_ADAPTIVE;
346 printk(KERN_INFO "%s: header compression turned on\n", sl->dev->name);
347 }
348 sl->rbuff[0] &= 0x4f;
349 if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
350 return;
351 }
352 }
353 }
354#endif
355
356 sl->rx_bytes+=count;
357
358 skb = dev_alloc_skb(count);
359 if (skb == NULL) {
360 printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n", sl->dev->name);
361 sl->rx_dropped++;
362 return;
363 }
364 skb->dev = sl->dev;
365 memcpy(skb_put(skb,count), sl->rbuff, count);
366 skb_reset_mac_header(skb);
367 skb->protocol=htons(ETH_P_IP);
368 netif_rx(skb);
369 sl->dev->last_rx = jiffies;
370 sl->rx_packets++;
371}
372
373
374static void
375sl_encaps(struct slip *sl, unsigned char *icp, int len)
376{
377 unsigned char *p;
378 int actual, count;
379
380 if (len > sl->mtu) {
381 printk(KERN_WARNING "%s: truncating oversized transmit packet!\n", sl->dev->name);
382 sl->tx_dropped++;
383 sl_unlock(sl);
384 return;
385 }
386
387 p = icp;
388#ifdef SL_INCLUDE_CSLIP
389 if (sl->mode & SL_MODE_CSLIP) {
390 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
391 }
392#endif
393#ifdef CONFIG_SLIP_MODE_SLIP6
394 if(sl->mode & SL_MODE_SLIP6)
395 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
396 else
397#endif
398 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
399
400
401
402
403
404
405
406
407
408 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
409 actual = sl->tty->driver->write(sl->tty, sl->xbuff, count);
410#ifdef SL_CHECK_TRANSMIT
411 sl->dev->trans_start = jiffies;
412#endif
413 sl->xleft = count - actual;
414 sl->xhead = sl->xbuff + actual;
415#ifdef CONFIG_SLIP_SMART
416
417 clear_bit(SLF_OUTWAIT, &sl->flags);
418#endif
419}
420
421
422
423
424
425static void slip_write_wakeup(struct tty_struct *tty)
426{
427 int actual;
428 struct slip *sl = (struct slip *) tty->disc_data;
429
430
431 if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) {
432 return;
433 }
434 if (sl->xleft <= 0) {
435
436
437 sl->tx_packets++;
438 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
439 sl_unlock(sl);
440 return;
441 }
442
443 actual = tty->driver->write(tty, sl->xhead, sl->xleft);
444 sl->xleft -= actual;
445 sl->xhead += actual;
446}
447
448static void sl_tx_timeout(struct net_device *dev)
449{
450 struct slip *sl = netdev_priv(dev);
451
452 spin_lock(&sl->lock);
453
454 if (netif_queue_stopped(dev)) {
455 if (!netif_running(dev))
456 goto out;
457
458
459
460
461#ifdef SL_CHECK_TRANSMIT
462 if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
463
464 goto out;
465 }
466 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
467 (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft) ?
468 "bad line quality" : "driver error");
469 sl->xleft = 0;
470 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
471 sl_unlock(sl);
472#endif
473 }
474
475out:
476 spin_unlock(&sl->lock);
477}
478
479
480
481static int
482sl_xmit(struct sk_buff *skb, struct net_device *dev)
483{
484 struct slip *sl = netdev_priv(dev);
485
486 spin_lock(&sl->lock);
487 if (!netif_running(dev)) {
488 spin_unlock(&sl->lock);
489 printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name);
490 dev_kfree_skb(skb);
491 return 0;
492 }
493 if (sl->tty == NULL) {
494 spin_unlock(&sl->lock);
495 dev_kfree_skb(skb);
496 return 0;
497 }
498
499 sl_lock(sl);
500 sl->tx_bytes+=skb->len;
501 sl_encaps(sl, skb->data, skb->len);
502 spin_unlock(&sl->lock);
503
504 dev_kfree_skb(skb);
505 return 0;
506}
507
508
509
510
511
512
513
514
515static int
516sl_close(struct net_device *dev)
517{
518 struct slip *sl = netdev_priv(dev);
519
520 spin_lock_bh(&sl->lock);
521 if (sl->tty) {
522
523 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
524 }
525 netif_stop_queue(dev);
526 sl->rcount = 0;
527 sl->xleft = 0;
528 spin_unlock_bh(&sl->lock);
529
530 return 0;
531}
532
533
534
535static int sl_open(struct net_device *dev)
536{
537 struct slip *sl = netdev_priv(dev);
538
539 if (sl->tty==NULL)
540 return -ENODEV;
541
542 sl->flags &= (1 << SLF_INUSE);
543 netif_start_queue(dev);
544 return 0;
545}
546
547
548
549static int sl_change_mtu(struct net_device *dev, int new_mtu)
550{
551 struct slip *sl = netdev_priv(dev);
552
553 if (new_mtu < 68 || new_mtu > 65534)
554 return -EINVAL;
555
556 if (new_mtu != dev->mtu)
557 return sl_realloc_bufs(sl, new_mtu);
558 return 0;
559}
560
561
562
563static struct net_device_stats *
564sl_get_stats(struct net_device *dev)
565{
566 static struct net_device_stats stats;
567 struct slip *sl = netdev_priv(dev);
568#ifdef SL_INCLUDE_CSLIP
569 struct slcompress *comp;
570#endif
571
572 memset(&stats, 0, sizeof(struct net_device_stats));
573
574 stats.rx_packets = sl->rx_packets;
575 stats.tx_packets = sl->tx_packets;
576 stats.rx_bytes = sl->rx_bytes;
577 stats.tx_bytes = sl->tx_bytes;
578 stats.rx_dropped = sl->rx_dropped;
579 stats.tx_dropped = sl->tx_dropped;
580 stats.tx_errors = sl->tx_errors;
581 stats.rx_errors = sl->rx_errors;
582 stats.rx_over_errors = sl->rx_over_errors;
583#ifdef SL_INCLUDE_CSLIP
584 stats.rx_fifo_errors = sl->rx_compressed;
585 stats.tx_fifo_errors = sl->tx_compressed;
586 stats.collisions = sl->tx_misses;
587 comp = sl->slcomp;
588 if (comp) {
589 stats.rx_fifo_errors += comp->sls_i_compressed;
590 stats.rx_dropped += comp->sls_i_tossed;
591 stats.tx_fifo_errors += comp->sls_o_compressed;
592 stats.collisions += comp->sls_o_misses;
593 }
594#endif
595 return (&stats);
596}
597
598
599
600static int sl_init(struct net_device *dev)
601{
602 struct slip *sl = netdev_priv(dev);
603
604
605
606
607
608 dev->mtu = sl->mtu;
609 dev->type = ARPHRD_SLIP + sl->mode;
610#ifdef SL_CHECK_TRANSMIT
611 dev->tx_timeout = sl_tx_timeout;
612 dev->watchdog_timeo = 20*HZ;
613#endif
614 return 0;
615}
616
617
618static void sl_uninit(struct net_device *dev)
619{
620 struct slip *sl = netdev_priv(dev);
621
622 sl_free_bufs(sl);
623}
624
625static void sl_setup(struct net_device *dev)
626{
627 dev->init = sl_init;
628 dev->uninit = sl_uninit;
629 dev->open = sl_open;
630 dev->destructor = free_netdev;
631 dev->stop = sl_close;
632 dev->get_stats = sl_get_stats;
633 dev->change_mtu = sl_change_mtu;
634 dev->hard_start_xmit = sl_xmit;
635#ifdef CONFIG_SLIP_SMART
636 dev->do_ioctl = sl_ioctl;
637#endif
638 dev->hard_header_len = 0;
639 dev->addr_len = 0;
640 dev->tx_queue_len = 10;
641
642
643 dev->flags = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
644}
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
661{
662 struct slip *sl = (struct slip *) tty->disc_data;
663
664 if (!sl || sl->magic != SLIP_MAGIC ||
665 !netif_running(sl->dev))
666 return;
667
668
669 while (count--) {
670 if (fp && *fp++) {
671 if (!test_and_set_bit(SLF_ERROR, &sl->flags)) {
672 sl->rx_errors++;
673 }
674 cp++;
675 continue;
676 }
677#ifdef CONFIG_SLIP_MODE_SLIP6
678 if (sl->mode & SL_MODE_SLIP6)
679 slip_unesc6(sl, *cp++);
680 else
681#endif
682 slip_unesc(sl, *cp++);
683 }
684}
685
686
687
688
689
690
691
692static void sl_sync(void)
693{
694 int i;
695 struct net_device *dev;
696 struct slip *sl;
697
698 for (i = 0; i < slip_maxdev; i++) {
699 if ((dev = slip_devs[i]) == NULL)
700 break;
701
702 sl = netdev_priv(dev);
703 if (sl->tty || sl->leased)
704 continue;
705 if (dev->flags&IFF_UP)
706 dev_close(dev);
707 }
708}
709
710
711
712static struct slip *
713sl_alloc(dev_t line)
714{
715 int i;
716 int sel = -1;
717 int score = -1;
718 struct net_device *dev = NULL;
719 struct slip *sl;
720
721 if (slip_devs == NULL)
722 return NULL;
723
724 for (i = 0; i < slip_maxdev; i++) {
725 dev = slip_devs[i];
726 if (dev == NULL)
727 break;
728
729 sl = netdev_priv(dev);
730 if (sl->leased) {
731 if (sl->line != line)
732 continue;
733 if (sl->tty)
734 return NULL;
735
736
737 sl->flags &= (1 << SLF_INUSE);
738 return sl;
739 }
740
741 if (sl->tty)
742 continue;
743
744 if (current->pid == sl->pid) {
745 if (sl->line == line && score < 3) {
746 sel = i;
747 score = 3;
748 continue;
749 }
750 if (score < 2) {
751 sel = i;
752 score = 2;
753 }
754 continue;
755 }
756 if (sl->line == line && score < 1) {
757 sel = i;
758 score = 1;
759 continue;
760 }
761 if (score < 0) {
762 sel = i;
763 score = 0;
764 }
765 }
766
767 if (sel >= 0) {
768 i = sel;
769 dev = slip_devs[i];
770 if (score > 1) {
771 sl = netdev_priv(dev);
772 sl->flags &= (1 << SLF_INUSE);
773 return sl;
774 }
775 }
776
777
778 if (i >= slip_maxdev)
779 return NULL;
780
781 if (dev) {
782 sl = netdev_priv(dev);
783 if (test_bit(SLF_INUSE, &sl->flags)) {
784 unregister_netdevice(dev);
785 dev = NULL;
786 slip_devs[i] = NULL;
787 }
788 }
789
790 if (!dev) {
791 char name[IFNAMSIZ];
792 sprintf(name, "sl%d", i);
793
794 dev = alloc_netdev(sizeof(*sl), name, sl_setup);
795 if (!dev)
796 return NULL;
797 dev->base_addr = i;
798 }
799
800 sl = netdev_priv(dev);
801
802
803 sl->magic = SLIP_MAGIC;
804 sl->dev = dev;
805 spin_lock_init(&sl->lock);
806 sl->mode = SL_MODE_DEFAULT;
807#ifdef CONFIG_SLIP_SMART
808 init_timer(&sl->keepalive_timer);
809 sl->keepalive_timer.data=(unsigned long)sl;
810 sl->keepalive_timer.function=sl_keepalive;
811 init_timer(&sl->outfill_timer);
812 sl->outfill_timer.data=(unsigned long)sl;
813 sl->outfill_timer.function=sl_outfill;
814#endif
815 slip_devs[i] = dev;
816
817 return sl;
818}
819
820
821
822
823
824
825
826
827
828
829
830static int slip_open(struct tty_struct *tty)
831{
832 struct slip *sl;
833 int err;
834
835 if(!capable(CAP_NET_ADMIN))
836 return -EPERM;
837
838
839
840
841
842 rtnl_lock();
843
844
845 sl_sync();
846
847 sl = (struct slip *) tty->disc_data;
848
849 err = -EEXIST;
850
851 if (sl && sl->magic == SLIP_MAGIC)
852 goto err_exit;
853
854
855 err = -ENFILE;
856 if ((sl = sl_alloc(tty_devnum(tty))) == NULL)
857 goto err_exit;
858
859 sl->tty = tty;
860 tty->disc_data = sl;
861 sl->line = tty_devnum(tty);
862 sl->pid = current->pid;
863
864 if (!test_bit(SLF_INUSE, &sl->flags)) {
865
866 if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
867 goto err_free_chan;
868
869 set_bit(SLF_INUSE, &sl->flags);
870
871 if ((err = register_netdevice(sl->dev)))
872 goto err_free_bufs;
873 }
874
875#ifdef CONFIG_SLIP_SMART
876 if (sl->keepalive) {
877 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
878 add_timer (&sl->keepalive_timer);
879 }
880 if (sl->outfill) {
881 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
882 add_timer (&sl->outfill_timer);
883 }
884#endif
885
886
887 rtnl_unlock();
888 tty->receive_room = 65536;
889 return sl->dev->base_addr;
890
891err_free_bufs:
892 sl_free_bufs(sl);
893
894err_free_chan:
895 sl->tty = NULL;
896 tty->disc_data = NULL;
897 clear_bit(SLF_INUSE, &sl->flags);
898
899err_exit:
900 rtnl_unlock();
901
902
903 return err;
904}
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931static void
932slip_close(struct tty_struct *tty)
933{
934 struct slip *sl = (struct slip *) tty->disc_data;
935
936
937 if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
938 return;
939
940 tty->disc_data = NULL;
941 sl->tty = NULL;
942 if (!sl->leased)
943 sl->line = 0;
944
945
946#ifdef CONFIG_SLIP_SMART
947 del_timer_sync(&sl->keepalive_timer);
948 del_timer_sync(&sl->outfill_timer);
949#endif
950
951
952}
953
954
955
956
957
958static int
959slip_esc(unsigned char *s, unsigned char *d, int len)
960{
961 unsigned char *ptr = d;
962 unsigned char c;
963
964
965
966
967
968
969
970 *ptr++ = END;
971
972
973
974
975
976
977 while (len-- > 0) {
978 switch(c = *s++) {
979 case END:
980 *ptr++ = ESC;
981 *ptr++ = ESC_END;
982 break;
983 case ESC:
984 *ptr++ = ESC;
985 *ptr++ = ESC_ESC;
986 break;
987 default:
988 *ptr++ = c;
989 break;
990 }
991 }
992 *ptr++ = END;
993 return (ptr - d);
994}
995
996static void slip_unesc(struct slip *sl, unsigned char s)
997{
998
999 switch(s) {
1000 case END:
1001#ifdef CONFIG_SLIP_SMART
1002
1003 if (test_bit(SLF_KEEPTEST, &sl->flags))
1004 clear_bit(SLF_KEEPTEST, &sl->flags);
1005#endif
1006
1007 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1008 sl_bump(sl);
1009 }
1010 clear_bit(SLF_ESCAPE, &sl->flags);
1011 sl->rcount = 0;
1012 return;
1013
1014 case ESC:
1015 set_bit(SLF_ESCAPE, &sl->flags);
1016 return;
1017 case ESC_ESC:
1018 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1019 s = ESC;
1020 }
1021 break;
1022 case ESC_END:
1023 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1024 s = END;
1025 }
1026 break;
1027 }
1028 if (!test_bit(SLF_ERROR, &sl->flags)) {
1029 if (sl->rcount < sl->buffsize) {
1030 sl->rbuff[sl->rcount++] = s;
1031 return;
1032 }
1033 sl->rx_over_errors++;
1034 set_bit(SLF_ERROR, &sl->flags);
1035 }
1036}
1037
1038
1039#ifdef CONFIG_SLIP_MODE_SLIP6
1040
1041
1042
1043
1044int
1045slip_esc6(unsigned char *s, unsigned char *d, int len)
1046{
1047 unsigned char *ptr = d;
1048 unsigned char c;
1049 int i;
1050 unsigned short v = 0;
1051 short bits = 0;
1052
1053
1054
1055
1056
1057
1058
1059 *ptr++ = 0x70;
1060
1061
1062
1063
1064
1065 for (i = 0; i < len; ++i) {
1066 v = (v << 8) | s[i];
1067 bits += 8;
1068 while (bits >= 6) {
1069 bits -= 6;
1070 c = 0x30 + ((v >> bits) & 0x3F);
1071 *ptr++ = c;
1072 }
1073 }
1074 if (bits) {
1075 c = 0x30 + ((v << (6 - bits)) & 0x3F);
1076 *ptr++ = c;
1077 }
1078 *ptr++ = 0x70;
1079 return ptr - d;
1080}
1081
1082void
1083slip_unesc6(struct slip *sl, unsigned char s)
1084{
1085 unsigned char c;
1086
1087 if (s == 0x70) {
1088#ifdef CONFIG_SLIP_SMART
1089
1090 if (test_bit(SLF_KEEPTEST, &sl->flags))
1091 clear_bit(SLF_KEEPTEST, &sl->flags);
1092#endif
1093
1094 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1095 sl_bump(sl);
1096 }
1097 sl->rcount = 0;
1098 sl->xbits = 0;
1099 sl->xdata = 0;
1100 } else if (s >= 0x30 && s < 0x70) {
1101 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
1102 sl->xbits += 6;
1103 if (sl->xbits >= 8) {
1104 sl->xbits -= 8;
1105 c = (unsigned char)(sl->xdata >> sl->xbits);
1106 if (!test_bit(SLF_ERROR, &sl->flags)) {
1107 if (sl->rcount < sl->buffsize) {
1108 sl->rbuff[sl->rcount++] = c;
1109 return;
1110 }
1111 sl->rx_over_errors++;
1112 set_bit(SLF_ERROR, &sl->flags);
1113 }
1114 }
1115 }
1116}
1117#endif
1118
1119
1120static int slip_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1121{
1122 struct slip *sl = (struct slip *) tty->disc_data;
1123 unsigned int tmp;
1124 int __user *p = (int __user *)arg;
1125
1126
1127 if (!sl || sl->magic != SLIP_MAGIC) {
1128 return -EINVAL;
1129 }
1130
1131 switch(cmd) {
1132 case SIOCGIFNAME:
1133 tmp = strlen(sl->dev->name) + 1;
1134 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1135 return -EFAULT;
1136 return 0;
1137
1138 case SIOCGIFENCAP:
1139 if (put_user(sl->mode, p))
1140 return -EFAULT;
1141 return 0;
1142
1143 case SIOCSIFENCAP:
1144 if (get_user(tmp, p))
1145 return -EFAULT;
1146#ifndef SL_INCLUDE_CSLIP
1147 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) {
1148 return -EINVAL;
1149 }
1150#else
1151 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1152 (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
1153
1154 tmp &= ~SL_MODE_ADAPTIVE;
1155 }
1156#endif
1157#ifndef CONFIG_SLIP_MODE_SLIP6
1158 if (tmp & SL_MODE_SLIP6) {
1159 return -EINVAL;
1160 }
1161#endif
1162 sl->mode = tmp;
1163 sl->dev->type = ARPHRD_SLIP+sl->mode;
1164 return 0;
1165
1166 case SIOCSIFHWADDR:
1167 return -EINVAL;
1168
1169#ifdef CONFIG_SLIP_SMART
1170
1171 case SIOCSKEEPALIVE:
1172 if (get_user(tmp, p))
1173 return -EFAULT;
1174 if (tmp > 255)
1175 return -EINVAL;
1176
1177 spin_lock_bh(&sl->lock);
1178 if (!sl->tty) {
1179 spin_unlock_bh(&sl->lock);
1180 return -ENODEV;
1181 }
1182 if ((sl->keepalive = (unchar) tmp) != 0) {
1183 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1184 set_bit(SLF_KEEPTEST, &sl->flags);
1185 } else {
1186 del_timer (&sl->keepalive_timer);
1187 }
1188 spin_unlock_bh(&sl->lock);
1189 return 0;
1190
1191 case SIOCGKEEPALIVE:
1192 if (put_user(sl->keepalive, p))
1193 return -EFAULT;
1194 return 0;
1195
1196 case SIOCSOUTFILL:
1197 if (get_user(tmp, p))
1198 return -EFAULT;
1199 if (tmp > 255)
1200 return -EINVAL;
1201 spin_lock_bh(&sl->lock);
1202 if (!sl->tty) {
1203 spin_unlock_bh(&sl->lock);
1204 return -ENODEV;
1205 }
1206 if ((sl->outfill = (unchar) tmp) != 0){
1207 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1208 set_bit(SLF_OUTWAIT, &sl->flags);
1209 } else {
1210 del_timer (&sl->outfill_timer);
1211 }
1212 spin_unlock_bh(&sl->lock);
1213 return 0;
1214
1215 case SIOCGOUTFILL:
1216 if (put_user(sl->outfill, p))
1217 return -EFAULT;
1218 return 0;
1219
1220#endif
1221 default:
1222 return tty_mode_ioctl(tty, file, cmd, arg);
1223 }
1224}
1225
1226
1227#ifdef CONFIG_SLIP_SMART
1228
1229
1230
1231
1232static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd)
1233{
1234 struct slip *sl = netdev_priv(dev);
1235 unsigned long *p = (unsigned long *)&rq->ifr_ifru;
1236
1237 if (sl == NULL)
1238 return -ENODEV;
1239
1240 spin_lock_bh(&sl->lock);
1241
1242 if (!sl->tty) {
1243 spin_unlock_bh(&sl->lock);
1244 return -ENODEV;
1245 }
1246
1247 switch(cmd){
1248 case SIOCSKEEPALIVE:
1249
1250 if ((unsigned)*p > 255) {
1251 spin_unlock_bh(&sl->lock);
1252 return -EINVAL;
1253 }
1254 sl->keepalive = (unchar) *p;
1255 if (sl->keepalive != 0) {
1256 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1257 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1258 set_bit(SLF_KEEPTEST, &sl->flags);
1259 } else {
1260 del_timer(&sl->keepalive_timer);
1261 }
1262 break;
1263
1264 case SIOCGKEEPALIVE:
1265 *p = sl->keepalive;
1266 break;
1267
1268 case SIOCSOUTFILL:
1269 if ((unsigned)*p > 255) {
1270 spin_unlock_bh(&sl->lock);
1271 return -EINVAL;
1272 }
1273 if ((sl->outfill = (unchar)*p) != 0){
1274 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1275 set_bit(SLF_OUTWAIT, &sl->flags);
1276 } else {
1277 del_timer (&sl->outfill_timer);
1278 }
1279 break;
1280
1281 case SIOCGOUTFILL:
1282 *p = sl->outfill;
1283 break;
1284
1285 case SIOCSLEASE:
1286
1287
1288
1289 if (sl->tty != current->signal->tty && sl->pid != current->pid) {
1290 spin_unlock_bh(&sl->lock);
1291 return -EPERM;
1292 }
1293 sl->leased = 0;
1294 if (*p)
1295 sl->leased = 1;
1296 break;
1297
1298 case SIOCGLEASE:
1299 *p = sl->leased;
1300 };
1301 spin_unlock_bh(&sl->lock);
1302 return 0;
1303}
1304#endif
1305
1306
1307static struct tty_ldisc sl_ldisc = {
1308 .owner = THIS_MODULE,
1309 .magic = TTY_LDISC_MAGIC,
1310 .name = "slip",
1311 .open = slip_open,
1312 .close = slip_close,
1313 .ioctl = slip_ioctl,
1314 .receive_buf = slip_receive_buf,
1315 .write_wakeup = slip_write_wakeup,
1316};
1317
1318static int __init slip_init(void)
1319{
1320 int status;
1321
1322 if (slip_maxdev < 4)
1323 slip_maxdev = 4;
1324
1325 printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
1326#ifdef CONFIG_SLIP_MODE_SLIP6
1327 " (6 bit encapsulation enabled)"
1328#endif
1329 ".\n",
1330 SLIP_VERSION, slip_maxdev );
1331#if defined(SL_INCLUDE_CSLIP)
1332 printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
1333#endif
1334#ifdef CONFIG_SLIP_SMART
1335 printk(KERN_INFO "SLIP linefill/keepalive option.\n");
1336#endif
1337
1338 slip_devs = kzalloc(sizeof(struct net_device *)*slip_maxdev, GFP_KERNEL);
1339 if (!slip_devs) {
1340 printk(KERN_ERR "SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n");
1341 return -ENOMEM;
1342 }
1343
1344
1345 if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
1346 printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
1347 kfree(slip_devs);
1348 }
1349 return status;
1350}
1351
1352static void __exit slip_exit(void)
1353{
1354 int i;
1355 struct net_device *dev;
1356 struct slip *sl;
1357 unsigned long timeout = jiffies + HZ;
1358 int busy = 0;
1359
1360 if (slip_devs == NULL)
1361 return;
1362
1363
1364
1365 do {
1366 if (busy)
1367 msleep_interruptible(100);
1368
1369 busy = 0;
1370 for (i = 0; i < slip_maxdev; i++) {
1371 dev = slip_devs[i];
1372 if (!dev)
1373 continue;
1374 sl = netdev_priv(dev);
1375 spin_lock_bh(&sl->lock);
1376 if (sl->tty) {
1377 busy++;
1378 tty_hangup(sl->tty);
1379 }
1380 spin_unlock_bh(&sl->lock);
1381 }
1382 } while (busy && time_before(jiffies, timeout));
1383
1384
1385 for (i = 0; i < slip_maxdev; i++) {
1386 dev = slip_devs[i];
1387 if (!dev)
1388 continue;
1389 slip_devs[i] = NULL;
1390
1391 sl = netdev_priv(dev);
1392 if (sl->tty) {
1393 printk(KERN_ERR "%s: tty discipline still running\n",
1394 dev->name);
1395
1396 dev->destructor = NULL;
1397 }
1398
1399 unregister_netdev(dev);
1400 }
1401
1402 kfree(slip_devs);
1403 slip_devs = NULL;
1404
1405 if ((i = tty_unregister_ldisc(N_SLIP)))
1406 {
1407 printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
1408 }
1409}
1410
1411module_init(slip_init);
1412module_exit(slip_exit);
1413
1414#ifdef CONFIG_SLIP_SMART
1415
1416
1417
1418
1419
1420static void sl_outfill(unsigned long sls)
1421{
1422 struct slip *sl=(struct slip *)sls;
1423
1424 spin_lock(&sl->lock);
1425
1426 if (sl->tty == NULL)
1427 goto out;
1428
1429 if(sl->outfill)
1430 {
1431 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1432 {
1433
1434#ifdef CONFIG_SLIP_MODE_SLIP6
1435 unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1436#else
1437 unsigned char s = END;
1438#endif
1439
1440 if (!netif_queue_stopped(sl->dev))
1441 {
1442
1443 sl->tty->driver->write(sl->tty, &s, 1);
1444 }
1445 }
1446 else
1447 set_bit(SLF_OUTWAIT, &sl->flags);
1448
1449 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1450 }
1451out:
1452 spin_unlock(&sl->lock);
1453}
1454
1455static void sl_keepalive(unsigned long sls)
1456{
1457 struct slip *sl=(struct slip *)sls;
1458
1459 spin_lock(&sl->lock);
1460
1461 if (sl->tty == NULL)
1462 goto out;
1463
1464 if( sl->keepalive)
1465 {
1466 if(test_bit(SLF_KEEPTEST, &sl->flags))
1467 {
1468
1469 if( sl->outfill )
1470 (void)del_timer(&sl->outfill_timer);
1471 printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1472 tty_hangup(sl->tty);
1473
1474 goto out;
1475 }
1476 else
1477 set_bit(SLF_KEEPTEST, &sl->flags);
1478
1479 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1480 }
1481
1482out:
1483 spin_unlock(&sl->lock);
1484}
1485
1486#endif
1487MODULE_LICENSE("GPL");
1488MODULE_ALIAS_LDISC(N_SLIP);
1489