This source file includes following definitions.
- sl_alloc
- sl_free
- sl_changedmtu
- sl_lock
- sl_unlock
- sl_bump
- sl_encaps
- slip_write_wakeup
- sl_xmit
- sl_header
- sl_rebuild_header
- sl_open
- sl_close
- slip_receive_room
- slip_receive_buf
- slip_open
- slip_close
- sl_get_stats
- slip_esc
- slip_unesc
- slip_esc6
- slip_unesc6
- sl_set_mac_address
- sl_set_dev_mac_address
- slip_ioctl
- sl_open_dev
- slip_init_ctrl_dev
- slip_init
- init_module
- cleanup_module
- sl_outfill
- sl_keepalive
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 #define SL_CHECK_TRANSMIT
56 #include <linux/config.h>
57 #include <linux/module.h>
58
59
60 #define CONFIG_SLIP_MODE_SLIP6
61
62 #include <asm/system.h>
63 #include <asm/segment.h>
64 #include <asm/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 #ifdef CONFIG_AX25
73 #include <linux/timer.h>
74 #include <net/ax25.h>
75 #endif
76 #include <linux/etherdevice.h>
77 #include <linux/skbuff.h>
78 #include <linux/if_arp.h>
79 #include <linux/if_slip.h>
80 #include "slip.h"
81 #ifdef CONFIG_INET
82 #include <linux/ip.h>
83 #include <linux/tcp.h>
84 #include "slhc.h"
85 #endif
86
87 #ifdef MODULE
88 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY-MODULAR"
89 #else
90 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY"
91 #endif
92
93
94 typedef struct slip_ctrl {
95 char if_name[8];
96 struct slip ctrl;
97 struct device dev;
98 } slip_ctrl_t;
99 static slip_ctrl_t **slip_ctrls = NULL;
100 int slip_maxdev = SL_NRUNIT;
101
102 static struct tty_ldisc sl_ldisc;
103
104 static int slip_esc(unsigned char *p, unsigned char *d, int len);
105 static void slip_unesc(struct slip *sl, unsigned char c);
106 #ifdef CONFIG_SLIP_MODE_SLIP6
107 static int slip_esc6(unsigned char *p, unsigned char *d, int len);
108 static void slip_unesc6(struct slip *sl, unsigned char c);
109 #endif
110 #ifdef CONFIG_SLIP_SMART
111 static void sl_keepalive(unsigned long sls);
112 static void sl_outfill(unsigned long sls);
113 #endif
114
115
116 static inline struct slip *
117 sl_alloc(void)
118 {
119 slip_ctrl_t *slp;
120 int i;
121
122 if (slip_ctrls == NULL) return NULL;
123
124 for (i = 0; i < slip_maxdev; i++) {
125 slp = slip_ctrls[i];
126
127 if (slp == NULL)
128 break;
129
130 if (!set_bit(SLF_INUSE, &slp->ctrl.flags))
131 break;
132 }
133
134
135
136 if (i >= slip_maxdev) return NULL;
137
138
139 if (!slp &&
140 (slip_ctrls[i] = (slip_ctrl_t *)kmalloc(sizeof(slip_ctrl_t),
141 GFP_KERNEL)) != NULL) {
142 slp = slip_ctrls[i];
143 memset(slp, 0, sizeof(slip_ctrl_t));
144
145
146 set_bit(SLF_INUSE, &slp->ctrl.flags);
147 slp->ctrl.tty = NULL;
148 sprintf(slp->if_name, "sl%d", i);
149 slp->dev.name = slp->if_name;
150 slp->dev.base_addr = i;
151 slp->dev.priv = (void*)&(slp->ctrl);
152 slp->dev.next = NULL;
153 slp->dev.init = slip_init;
154
155
156 }
157 if (slp != NULL) {
158
159
160
161
162
163 if (register_netdev(&(slp->dev)) == 0) {
164
165 set_bit(SLF_INUSE, &slp->ctrl.flags);
166 slp->ctrl.dev = &(slp->dev);
167 slp->dev.priv = (void*)&(slp->ctrl);
168
169
170
171
172 return (&(slp->ctrl));
173
174 } else {
175 clear_bit(SLF_INUSE,&(slp->ctrl.flags));
176 printk("sl_alloc() - register_netdev() failure.\n");
177 }
178 }
179
180 return NULL;
181 }
182
183
184
185 static inline void
186 sl_free(struct slip *sl)
187 {
188
189 if (sl->rbuff) {
190 kfree(sl->rbuff);
191 }
192 sl->rbuff = NULL;
193 if (sl->xbuff) {
194 kfree(sl->xbuff);
195 }
196 sl->xbuff = NULL;
197 #ifdef SL_INCLUDE_CSLIP
198
199 if (sl->slcomp) {
200 sl->rx_compressed += sl->slcomp->sls_i_compressed;
201 sl->rx_dropped += sl->slcomp->sls_i_tossed;
202 sl->tx_compressed += sl->slcomp->sls_o_compressed;
203 sl->tx_misses += sl->slcomp->sls_o_misses;
204 }
205 if (sl->cbuff) {
206 kfree(sl->cbuff);
207 }
208 sl->cbuff = NULL;
209 if(sl->slcomp)
210 slhc_free(sl->slcomp);
211 sl->slcomp = NULL;
212 #endif
213
214 if (!clear_bit(SLF_INUSE, &sl->flags)) {
215 printk("%s: sl_free for already free unit.\n", sl->dev->name);
216 }
217 }
218
219
220
221
222
223 static void sl_changedmtu(struct slip *sl)
224 {
225 struct device *dev = sl->dev;
226 unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
227 #ifdef SL_INCLUDE_CSLIP
228 unsigned char *cbuff, *ocbuff;
229 #endif
230 int len;
231 unsigned long flags;
232
233 len = dev->mtu * 2;
234
235
236
237
238
239 if (len < 576 * 2) {
240 len = 576 * 2;
241 }
242
243 xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
244 rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
245 #ifdef SL_INCLUDE_CSLIP
246 cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
247 #endif
248
249 #ifdef SL_INCLUDE_CSLIP
250 if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
251 #else
252 if (xbuff == NULL || rbuff == NULL) {
253 #endif
254 printk("%s: unable to grow slip buffers, MTU change cancelled.\n",
255 sl->dev->name);
256 dev->mtu = sl->mtu;
257 if (xbuff != NULL) {
258 kfree(xbuff);
259 }
260 if (rbuff != NULL) {
261 kfree(rbuff);
262 }
263 #ifdef SL_INCLUDE_CSLIP
264 if (cbuff != NULL) {
265 kfree(cbuff);
266 }
267 #endif
268 return;
269 }
270
271 save_flags(flags); cli();
272
273 oxbuff = sl->xbuff;
274 sl->xbuff = xbuff;
275 orbuff = sl->rbuff;
276 sl->rbuff = rbuff;
277 #ifdef SL_INCLUDE_CSLIP
278 ocbuff = sl->cbuff;
279 sl->cbuff = cbuff;
280 #endif
281 if (sl->xleft) {
282 if (sl->xleft <= len) {
283 memcpy(sl->xbuff, sl->xhead, sl->xleft);
284 } else {
285 sl->xleft = 0;
286 sl->tx_dropped++;
287 }
288 }
289 sl->xhead = sl->xbuff;
290
291 if (sl->rcount) {
292 if (sl->rcount <= len) {
293 memcpy(sl->rbuff, orbuff, sl->rcount);
294 } else {
295 sl->rcount = 0;
296 sl->rx_over_errors++;
297 set_bit(SLF_ERROR, &sl->flags);
298 }
299 }
300 #ifdef CONFIG_AX25
301 sl->mtu = dev->mtu + 73;
302 #else
303 sl->mtu = dev->mtu;
304 #endif
305 sl->buffsize = len;
306
307 restore_flags(flags);
308
309 if (oxbuff != NULL) {
310 kfree(oxbuff);
311 }
312 if (orbuff != NULL) {
313 kfree(orbuff);
314 }
315 #ifdef SL_INCLUDE_CSLIP
316 if (ocbuff != NULL) {
317 kfree(ocbuff);
318 }
319 #endif
320 }
321
322
323
324 static inline void
325 sl_lock(struct slip *sl)
326 {
327 if (set_bit(0, (void *) &sl->dev->tbusy)) {
328 printk("%s: trying to lock already locked device!\n", sl->dev->name);
329 }
330 }
331
332
333
334 static inline void
335 sl_unlock(struct slip *sl)
336 {
337 if (!clear_bit(0, (void *)&sl->dev->tbusy)) {
338 printk("%s: trying to unlock already unlocked device!\n", sl->dev->name);
339 }
340 }
341
342
343 static void
344 sl_bump(struct slip *sl)
345 {
346 struct sk_buff *skb;
347 int count;
348
349 count = sl->rcount;
350 #ifdef SL_INCLUDE_CSLIP
351 if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
352 unsigned char c;
353 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
354
355 if (!(sl->mode & SL_MODE_CSLIP)) {
356 printk("%s: compressed packet ignored\n", sl->dev->name);
357 return;
358 }
359
360 if (count + 80 > sl->buffsize) {
361 sl->rx_over_errors++;
362 return;
363 }
364 count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
365 if (count <= 0) {
366 return;
367 }
368 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
369 if (!(sl->mode & SL_MODE_CSLIP)) {
370
371 sl->mode |= SL_MODE_CSLIP;
372 sl->mode &= ~SL_MODE_ADAPTIVE;
373 printk("%s: header compression turned on\n", sl->dev->name);
374 }
375 sl->rbuff[0] &= 0x4f;
376 if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
377 return;
378 }
379 }
380 }
381 #endif
382
383 skb = dev_alloc_skb(count);
384 if (skb == NULL) {
385 printk("%s: memory squeeze, dropping packet.\n", sl->dev->name);
386 sl->rx_dropped++;
387 return;
388 }
389 skb->dev = sl->dev;
390 memcpy(skb_put(skb,count), sl->rbuff, count);
391 skb->mac.raw=skb->data;
392 if(sl->mode & SL_MODE_AX25)
393 skb->protocol=htons(ETH_P_AX25);
394 else
395 skb->protocol=htons(ETH_P_IP);
396 netif_rx(skb);
397 sl->rx_packets++;
398 }
399
400
401 static void
402 sl_encaps(struct slip *sl, unsigned char *icp, int len)
403 {
404 unsigned char *p;
405 int actual, count;
406
407
408 #ifdef CONFIG_AX25
409 if (sl->mtu != sl->dev->mtu + 73) {
410 #else
411 if (sl->mtu != sl->dev->mtu) {
412 #endif
413 sl_changedmtu(sl);
414 }
415
416 if (len > sl->mtu) {
417 len = sl->mtu;
418 printk ("%s: truncating oversized transmit packet!\n", sl->dev->name);
419 sl->tx_dropped++;
420 sl_unlock(sl);
421 return;
422 }
423
424 p = icp;
425 #ifdef SL_INCLUDE_CSLIP
426 if (sl->mode & SL_MODE_CSLIP) {
427 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
428 }
429 #endif
430 #ifdef CONFIG_SLIP_MODE_SLIP6
431 if(sl->mode & SL_MODE_SLIP6)
432 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
433 else
434 #endif
435 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
436
437
438
439
440
441
442
443
444
445 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
446 actual = sl->tty->driver.write(sl->tty, 0, sl->xbuff, count);
447 #ifdef SL_CHECK_TRANSMIT
448 sl->dev->trans_start = jiffies;
449 #endif
450 sl->xleft = count - actual;
451 sl->xhead = sl->xbuff + actual;
452
453 clear_bit(SLF_OUTWAIT, &sl->flags);
454 }
455
456
457
458
459
460 static void slip_write_wakeup(struct tty_struct *tty)
461 {
462 int actual;
463 struct slip *sl = (struct slip *) tty->disc_data;
464
465
466 if (!sl || sl->magic != SLIP_MAGIC || !sl->dev->start) {
467 return;
468 }
469 if (sl->xleft <= 0) {
470
471
472 sl->tx_packets++;
473 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
474 if (test_bit(0, (void *) &sl->dev->tbusy))
475 sl_unlock(sl);
476 mark_bh(NET_BH);
477 return;
478 }
479
480 actual = tty->driver.write(tty, 0, sl->xhead, sl->xleft);
481 sl->xleft -= actual;
482 sl->xhead += actual;
483 }
484
485
486 static int
487 sl_xmit(struct sk_buff *skb, struct device *dev)
488 {
489 struct slip *sl = (struct slip*)(dev->priv);
490
491 if (!dev->start) {
492 printk("%s: xmit call when iface is down\n", dev->name);
493 return 1;
494 }
495
496
497
498
499
500
501
502
503
504
505 if (dev->tbusy) {
506
507
508
509 #ifdef SL_CHECK_TRANSMIT
510 if (jiffies - dev->trans_start < 20 * HZ) {
511
512 return 1;
513 }
514 printk("%s: transmit timed out, %s?\n", dev->name,
515 (sl->tty->driver.chars_in_buffer(sl->tty) || sl->xleft) ?
516 "bad line quality" : "driver error");
517 sl->xleft = 0;
518 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
519 sl_unlock(sl);
520 #else
521 return 1;
522 #endif
523 }
524
525
526 if (skb != NULL) {
527 sl_lock(sl);
528 sl_encaps(sl, skb->data, skb->len);
529 dev_kfree_skb(skb, FREE_WRITE);
530 }
531 return 0;
532 }
533
534
535
536
537
538 static int
539 sl_header(struct sk_buff *skb, struct device *dev, unsigned short type,
540 void *daddr, void *saddr, unsigned len)
541 {
542 #ifdef CONFIG_AX25
543 #ifdef CONFIG_INET
544 struct slip *sl = (struct slip*)(dev->priv);
545
546 if (sl->mode & SL_MODE_AX25 && type != htons(ETH_P_AX25)) {
547 return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
548 }
549 #endif
550 #endif
551 return 0;
552 }
553
554
555
556 static int
557 sl_rebuild_header(void *buff, struct device *dev, unsigned long raddr,
558 struct sk_buff *skb)
559 {
560 #ifdef CONFIG_AX25
561 #ifdef CONFIG_INET
562 struct slip *sl = (struct slip*)(dev->priv);
563
564 if (sl->mode & SL_MODE_AX25) {
565 return ax25_rebuild_header(buff, dev, raddr, skb);
566 }
567 #endif
568 #endif
569 return 0;
570 }
571
572
573
574 static int
575 sl_open(struct device *dev)
576 {
577 struct slip *sl = (struct slip*)(dev->priv);
578 unsigned long len;
579
580 if (sl->tty == NULL) {
581 return -ENODEV;
582 }
583
584
585
586
587
588
589
590
591 len = dev->mtu * 2;
592
593
594
595
596
597 if (len < 576 * 2) {
598 len = 576 * 2;
599 }
600 sl->rbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
601 if (sl->rbuff == NULL) {
602 goto norbuff;
603 }
604 sl->xbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
605 if (sl->xbuff == NULL) {
606 goto noxbuff;
607 }
608 #ifdef SL_INCLUDE_CSLIP
609 sl->cbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
610 if (sl->cbuff == NULL) {
611 goto nocbuff;
612 }
613 sl->slcomp = slhc_init(16, 16);
614 if (sl->slcomp == NULL) {
615 goto noslcomp;
616 }
617 #endif
618
619 #ifdef CONFIG_AX25
620 sl->mtu = dev->mtu + 73;
621 #else
622 sl->mtu = dev->mtu;
623 #endif
624 sl->buffsize = len;
625 sl->rcount = 0;
626 sl->xleft = 0;
627 #ifdef CONFIG_SLIP_MODE_SLIP6
628 sl->xdata = 0;
629 sl->xbits = 0;
630 #endif
631 sl->flags &= (1 << SLF_INUSE);
632 #ifdef CONFIG_SLIP_SMART
633 sl->keepalive=0;
634 init_timer(&sl->keepalive_timer);
635 sl->keepalive_timer.data=(unsigned long)sl;
636 sl->keepalive_timer.function=sl_keepalive;
637 sl->outfill=0;
638 init_timer(&sl->outfill_timer);
639 sl->outfill_timer.data=(unsigned long)sl;
640 sl->outfill_timer.function=sl_outfill;
641 #endif
642
643 if (dev->pa_addr == 0) {
644 dev->pa_addr=ntohl(0xC0A80001);
645 }
646 dev->tbusy = 0;
647
648 dev->start = 1;
649
650 return 0;
651
652
653 #ifdef SL_INCLUDE_CSLIP
654 noslcomp:
655 kfree(sl->cbuff);
656 nocbuff:
657 #endif
658 kfree(sl->xbuff);
659 noxbuff:
660 kfree(sl->rbuff);
661 norbuff:
662 return -ENOMEM;
663 }
664
665
666
667 static int
668 sl_close(struct device *dev)
669 {
670 struct slip *sl = (struct slip*)(dev->priv);
671
672 if (sl->tty == NULL) {
673 return -EBUSY;
674 }
675 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
676 dev->tbusy = 1;
677 dev->start = 0;
678
679
680
681 return 0;
682 }
683
684 static int
685 slip_receive_room(struct tty_struct *tty)
686 {
687 return 65536;
688 }
689
690
691
692
693
694
695
696 static void
697 slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
698 {
699 struct slip *sl = (struct slip *) tty->disc_data;
700
701 if (!sl || sl->magic != SLIP_MAGIC || !sl->dev->start)
702 return;
703
704
705
706
707
708 #ifdef CONFIG_AX25
709 if (sl->mtu != sl->dev->mtu + 73) {
710 #else
711 if (sl->mtu != sl->dev->mtu) {
712 #endif
713 sl_changedmtu(sl);
714 }
715
716
717 while (count--) {
718 if (fp && *fp++) {
719 if (!set_bit(SLF_ERROR, &sl->flags)) {
720 sl->rx_errors++;
721 }
722 cp++;
723 continue;
724 }
725 #ifdef CONFIG_SLIP_MODE_SLIP6
726 if (sl->mode & SL_MODE_SLIP6)
727 slip_unesc6(sl, *cp++);
728 else
729 #endif
730 slip_unesc(sl, *cp++);
731 }
732 }
733
734
735
736
737
738
739
740
741 static int
742 slip_open(struct tty_struct *tty)
743 {
744 struct slip *sl = (struct slip *) tty->disc_data;
745 int err;
746
747
748 if (sl && sl->magic == SLIP_MAGIC) {
749 return -EEXIST;
750 }
751
752
753 if ((sl = sl_alloc()) == NULL) {
754 return -ENFILE;
755 }
756
757 sl->tty = tty;
758 tty->disc_data = sl;
759 if (tty->driver.flush_buffer) {
760 tty->driver.flush_buffer(tty);
761 }
762 if (tty->ldisc.flush_buffer) {
763 tty->ldisc.flush_buffer(tty);
764 }
765
766
767 sl->mode = SL_MODE_DEFAULT;
768 sl->dev->type = ARPHRD_SLIP + sl->mode;
769 #ifdef CONFIG_AX25
770 if (sl->dev->type == 260) {
771 sl->dev->type = ARPHRD_AX25;
772 }
773 #endif
774
775 if ((err = sl_open(sl->dev))) {
776 return err;
777 }
778
779 MOD_INC_USE_COUNT;
780
781
782 return sl->dev->base_addr;
783 }
784
785
786
787
788
789
790
791
792 static void
793 slip_close(struct tty_struct *tty)
794 {
795 struct slip *sl = (struct slip *) tty->disc_data;
796
797
798 if (!sl || sl->magic != SLIP_MAGIC) {
799 return;
800 }
801
802 (void) dev_close(sl->dev);
803
804 tty->disc_data = 0;
805 sl->tty = NULL;
806 sl_free(sl);
807 unregister_netdev(sl->dev);
808 MOD_DEC_USE_COUNT;
809 }
810
811
812 static struct enet_statistics *
813 sl_get_stats(struct device *dev)
814 {
815 static struct enet_statistics stats;
816 struct slip *sl = (struct slip*)(dev->priv);
817 #ifdef SL_INCLUDE_CSLIP
818 struct slcompress *comp;
819 #endif
820
821 memset(&stats, 0, sizeof(struct enet_statistics));
822
823 stats.rx_packets = sl->rx_packets;
824 stats.tx_packets = sl->tx_packets;
825 stats.rx_dropped = sl->rx_dropped;
826 stats.tx_dropped = sl->tx_dropped;
827 stats.tx_errors = sl->tx_errors;
828 stats.rx_errors = sl->rx_errors;
829 stats.rx_over_errors = sl->rx_over_errors;
830 #ifdef SL_INCLUDE_CSLIP
831 stats.rx_fifo_errors = sl->rx_compressed;
832 stats.tx_fifo_errors = sl->tx_compressed;
833 stats.collisions = sl->tx_misses;
834 comp = sl->slcomp;
835 if (comp) {
836 stats.rx_fifo_errors += comp->sls_i_compressed;
837 stats.rx_dropped += comp->sls_i_tossed;
838 stats.tx_fifo_errors += comp->sls_o_compressed;
839 stats.collisions += comp->sls_o_misses;
840 }
841 #endif
842 return (&stats);
843 }
844
845
846
847
848
849
850 int
851 slip_esc(unsigned char *s, unsigned char *d, int len)
852 {
853 unsigned char *ptr = d;
854 unsigned char c;
855
856
857
858
859
860
861
862 *ptr++ = END;
863
864
865
866
867
868
869 while (len-- > 0) {
870 switch(c = *s++) {
871 case END:
872 *ptr++ = ESC;
873 *ptr++ = ESC_END;
874 break;
875 case ESC:
876 *ptr++ = ESC;
877 *ptr++ = ESC_ESC;
878 break;
879 default:
880 *ptr++ = c;
881 break;
882 }
883 }
884 *ptr++ = END;
885 return (ptr - d);
886 }
887
888 static void
889 slip_unesc(struct slip *sl, unsigned char s)
890 {
891
892 switch(s) {
893 case END:
894
895 if (test_bit(SLF_KEEPTEST, &sl->flags))
896 clear_bit(SLF_KEEPTEST, &sl->flags);
897
898 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
899 sl_bump(sl);
900 }
901 clear_bit(SLF_ESCAPE, &sl->flags);
902 sl->rcount = 0;
903 return;
904
905 case ESC:
906 set_bit(SLF_ESCAPE, &sl->flags);
907 return;
908 case ESC_ESC:
909 if (clear_bit(SLF_ESCAPE, &sl->flags)) {
910 s = ESC;
911 }
912 break;
913 case ESC_END:
914 if (clear_bit(SLF_ESCAPE, &sl->flags)) {
915 s = END;
916 }
917 break;
918 }
919 if (!test_bit(SLF_ERROR, &sl->flags)) {
920 if (sl->rcount < sl->buffsize) {
921 sl->rbuff[sl->rcount++] = s;
922 return;
923 }
924 sl->rx_over_errors++;
925 set_bit(SLF_ERROR, &sl->flags);
926 }
927 }
928
929
930 #ifdef CONFIG_SLIP_MODE_SLIP6
931
932
933
934
935 int
936 slip_esc6(unsigned char *s, unsigned char *d, int len)
937 {
938 unsigned char *ptr = d;
939 unsigned char c;
940 int i;
941 unsigned short v = 0;
942 short bits = 0;
943
944
945
946
947
948
949
950 *ptr++ = 0x70;
951
952
953
954
955
956 for (i = 0; i < len; ++i) {
957 v = (v << 8) | s[i];
958 bits += 8;
959 while (bits >= 6) {
960 bits -= 6;
961 c = 0x30 + ((v >> bits) & 0x3F);
962 *ptr++ = c;
963 }
964 }
965 if (bits) {
966 c = 0x30 + ((v << (6 - bits)) & 0x3F);
967 *ptr++ = c;
968 }
969 *ptr++ = 0x70;
970 return ptr - d;
971 }
972
973 void
974 slip_unesc6(struct slip *sl, unsigned char s)
975 {
976 unsigned char c;
977
978 if (s == 0x70) {
979
980 if (test_bit(SLF_KEEPTEST, &sl->flags))
981 clear_bit(SLF_KEEPTEST, &sl->flags);
982
983 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
984 sl_bump(sl);
985 }
986 sl->rcount = 0;
987 sl->xbits = 0;
988 sl->xdata = 0;
989 } else if (s >= 0x30 && s < 0x70) {
990 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
991 sl->xbits += 6;
992 if (sl->xbits >= 8) {
993 sl->xbits -= 8;
994 c = (unsigned char)(sl->xdata >> sl->xbits);
995 if (!test_bit(SLF_ERROR, &sl->flags)) {
996 if (sl->rcount < sl->buffsize) {
997 sl->rbuff[sl->rcount++] = c;
998 return;
999 }
1000 sl->rx_over_errors++;
1001 set_bit(SLF_ERROR, &sl->flags);
1002 }
1003 }
1004 }
1005 }
1006 #endif
1007
1008 #ifdef CONFIG_AX25
1009 int
1010 sl_set_mac_address(struct device *dev, void *addr)
1011 {
1012 int err;
1013
1014 err = verify_area(VERIFY_READ, addr, AX25_ADDR_LEN);
1015 if (err) {
1016 return err;
1017 }
1018
1019 memcpy_fromfs(dev->dev_addr, addr, AX25_ADDR_LEN);
1020
1021 return 0;
1022 }
1023
1024 static int
1025 sl_set_dev_mac_address(struct device *dev, void *addr)
1026 {
1027 struct sockaddr *sa=addr;
1028 memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
1029 return 0;
1030 }
1031 #endif
1032
1033
1034
1035 static int
1036 slip_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
1037 {
1038 struct slip *sl = (struct slip *) tty->disc_data;
1039 int err;
1040 unsigned int tmp;
1041
1042
1043 if (!sl || sl->magic != SLIP_MAGIC) {
1044 return -EINVAL;
1045 }
1046
1047 switch(cmd) {
1048 case SIOCGIFNAME:
1049 err = verify_area(VERIFY_WRITE, arg, strlen(sl->dev->name) + 1);
1050 if (err) {
1051 return err;
1052 }
1053 memcpy_tofs(arg, sl->dev->name, strlen(sl->dev->name) + 1);
1054 return 0;
1055
1056 case SIOCGIFENCAP:
1057 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1058 if (err) {
1059 return err;
1060 }
1061 put_user(sl->mode, (int *)arg);
1062 return 0;
1063
1064 case SIOCSIFENCAP:
1065 err = verify_area(VERIFY_READ, arg, sizeof(int));
1066 if (err) {
1067 return err;
1068 }
1069 tmp = get_user((int *)arg);
1070 #ifndef SL_INCLUDE_CSLIP
1071 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) {
1072 return -EINVAL;
1073 }
1074 #else
1075 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1076 (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
1077
1078 tmp &= ~SL_MODE_ADAPTIVE;
1079 }
1080 #endif
1081 #ifndef CONFIG_SLIP_MODE_SLIP6
1082 if (tmp & SL_MODE_SLIP6) {
1083 return -EINVAL;
1084 }
1085 #endif
1086 #ifndef CONFIG_AX25
1087 if (tmp & SL_MODE_AX25) {
1088 return -EINVAL;
1089 }
1090 #else
1091 if (tmp & SL_MODE_AX25) {
1092 sl->dev->addr_len=AX25_ADDR_LEN;
1093 sl->dev->hard_header_len=AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
1094 } else {
1095 sl->dev->addr_len=0;
1096 sl->dev->hard_header_len=0;
1097 }
1098 #endif
1099 sl->mode = tmp;
1100 sl->dev->type = ARPHRD_SLIP+sl->mode;
1101 #ifdef CONFIG_AX25
1102 if (sl->dev->type == 260) {
1103 sl->dev->type = ARPHRD_AX25;
1104 }
1105 #endif
1106 return 0;
1107
1108 case SIOCSIFHWADDR:
1109 #ifdef CONFIG_AX25
1110 return sl_set_mac_address(sl->dev, arg);
1111 #else
1112 return -EINVAL;
1113 #endif
1114
1115 #ifdef CONFIG_SLIP_SMART
1116
1117 case SIOCSKEEPALIVE:
1118 if (sl->keepalive)
1119 del_timer (&sl->keepalive_timer);
1120 err = verify_area(VERIFY_READ, arg, sizeof(int));
1121 if (err) {
1122 return -err;
1123 }
1124 tmp = get_user((int *)arg);
1125 if (tmp > 255)
1126 return -EINVAL;
1127 if ((sl->keepalive = (unchar) tmp) != 0) {
1128 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1129 add_timer(&sl->keepalive_timer);
1130 set_bit(SLF_KEEPTEST, &sl->flags);
1131 }
1132 return 0;
1133
1134 case SIOCGKEEPALIVE:
1135 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1136 if (err) {
1137 return -err;
1138 }
1139 put_user(sl->keepalive, (int *)arg);
1140 return 0;
1141
1142 case SIOCSOUTFILL:
1143 if (sl->outfill)
1144 (void)del_timer (&sl->outfill_timer);
1145 err = verify_area(VERIFY_READ, arg, sizeof(int));
1146 if (err) {
1147 return -err;
1148 }
1149 tmp = get_user((int *)arg);
1150 if (tmp > 255)
1151 return -EINVAL;
1152 if ((sl->outfill = (unchar) tmp) != 0){
1153 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1154 add_timer(&sl->outfill_timer);
1155 set_bit(SLF_OUTWAIT, &sl->flags);
1156 }
1157 return 0;
1158
1159 case SIOCGOUTFILL:
1160 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1161 if (err) {
1162 return -err;
1163 }
1164 put_user(sl->outfill, (int *)arg);
1165 return 0;
1166
1167 #endif
1168
1169
1170 case TCGETS:
1171 case TCGETA:
1172 return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
1173
1174 default:
1175 return -ENOIOCTLCMD;
1176 }
1177 }
1178
1179 static int sl_open_dev(struct device *dev)
1180 {
1181 struct slip *sl = (struct slip*)(dev->priv);
1182 if(sl->tty==NULL)
1183 return -ENODEV;
1184 return 0;
1185 }
1186
1187
1188 #ifdef MODULE
1189 static int slip_init_ctrl_dev(void)
1190 #else
1191 int slip_init_ctrl_dev(struct device *dummy)
1192 #endif
1193 {
1194 int status;
1195
1196 if (slip_maxdev < 4) slip_maxdev = 4;
1197
1198 printk("SLIP: version %s (dynamic channels, max=%d)"
1199 #ifdef CONFIG_SLIP_MODE_SLIP6
1200 " (6 bit encapsulation enabled)"
1201 #endif
1202 ".\n",
1203 SLIP_VERSION, slip_maxdev );
1204 #if defined(SL_INCLUDE_CSLIP) && !defined(MODULE)
1205 printk("CSLIP: code copyright 1989 Regents of the University of California.\n");
1206 #endif
1207 #ifdef CONFIG_AX25
1208 printk("AX25: KISS encapsulation enabled.\n");
1209 #endif
1210 #ifdef CONFIG_SLIP_SMART
1211 printk("SLIP linefill/keepalive option.\n");
1212 #endif
1213
1214 slip_ctrls = (slip_ctrl_t **) kmalloc(sizeof(void*)*slip_maxdev, GFP_KERNEL);
1215 if (slip_ctrls == NULL)
1216 {
1217 printk("SLIP: Can't allocate slip_ctrls[] array! Uaargh! (-> No SLIP available)\n");
1218 return -ENOMEM;
1219 }
1220
1221
1222 memset(slip_ctrls, 0, sizeof(void*)*slip_maxdev);
1223
1224
1225 memset(&sl_ldisc, 0, sizeof(sl_ldisc));
1226 sl_ldisc.magic = TTY_LDISC_MAGIC;
1227 sl_ldisc.flags = 0;
1228 sl_ldisc.open = slip_open;
1229 sl_ldisc.close = slip_close;
1230 sl_ldisc.read = NULL;
1231 sl_ldisc.write = NULL;
1232 sl_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
1233 unsigned int, unsigned long)) slip_ioctl;
1234 sl_ldisc.select = NULL;
1235 sl_ldisc.receive_buf = slip_receive_buf;
1236 sl_ldisc.receive_room = slip_receive_room;
1237 sl_ldisc.write_wakeup = slip_write_wakeup;
1238 if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
1239 printk("SLIP: can't register line discipline (err = %d)\n", status);
1240 }
1241
1242
1243 #ifdef MODULE
1244 return status;
1245 #else
1246
1247
1248
1249 return ENODEV;
1250 #endif
1251 }
1252
1253
1254
1255 int
1256 slip_init(struct device *dev)
1257 {
1258 struct slip *sl = (struct slip*)(dev->priv);
1259 int i;
1260 #ifdef CONFIG_AX25
1261 static char ax25_bcast[AX25_ADDR_LEN] =
1262 {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
1263 static char ax25_test[AX25_ADDR_LEN] =
1264 {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
1265 #endif
1266
1267 if (sl == NULL)
1268 return -ENODEV;
1269
1270
1271
1272 memset(sl, 0, sizeof (struct slip));
1273 sl->magic = SLIP_MAGIC;
1274 sl->dev = dev;
1275
1276
1277 dev->mtu = SL_MTU;
1278 dev->hard_start_xmit = sl_xmit;
1279 dev->open = sl_open_dev;
1280 dev->stop = sl_close;
1281 dev->hard_header = sl_header;
1282 dev->get_stats = sl_get_stats;
1283 #ifdef HAVE_SET_MAC_ADDR
1284 #ifdef CONFIG_AX25
1285 dev->set_mac_address = sl_set_dev_mac_address;
1286 #endif
1287 #endif
1288 dev->hard_header_len = 0;
1289 dev->addr_len = 0;
1290 dev->type = ARPHRD_SLIP + SL_MODE_DEFAULT;
1291 dev->tx_queue_len = 10;
1292 #ifdef CONFIG_AX25
1293 if (sl->dev->type == 260) {
1294 sl->dev->type = ARPHRD_AX25;
1295 }
1296 memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
1297 memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);
1298 #endif
1299 dev->rebuild_header = sl_rebuild_header;
1300
1301 for (i = 0; i < DEV_NUMBUFFS; i++) {
1302 skb_queue_head_init(&dev->buffs[i]);
1303 }
1304
1305
1306 dev->flags = 0;
1307 dev->family = AF_INET;
1308 dev->pa_addr = 0;
1309 dev->pa_brdaddr = 0;
1310 dev->pa_mask = 0;
1311 dev->pa_alen = 4;
1312
1313 return 0;
1314 }
1315 #ifdef MODULE
1316
1317 int
1318 init_module(void)
1319 {
1320 return slip_init_ctrl_dev();
1321 }
1322
1323 void
1324 cleanup_module(void)
1325 {
1326 int i;
1327
1328 if (slip_ctrls != NULL)
1329 {
1330 for (i = 0; i < slip_maxdev; i++)
1331 {
1332 if (slip_ctrls[i] != NULL)
1333 {
1334 unregister_netdev(&(slip_ctrls[i]->dev));
1335 kfree(slip_ctrls[i]);
1336 }
1337 }
1338 kfree(slip_ctrls);
1339 slip_ctrls = NULL;
1340 }
1341 if ((i = tty_register_ldisc(N_SLIP, NULL)))
1342 {
1343 printk("SLIP: can't unregister line discipline (err = %d)\n", i);
1344 }
1345 }
1346 #endif
1347
1348 #ifdef CONFIG_SLIP_SMART
1349
1350
1351
1352
1353
1354 static void sl_outfill(unsigned long sls)
1355 {
1356 struct slip *sl=(struct slip *)sls;
1357
1358 if(sls==NULL)
1359 return;
1360
1361 if(sl->outfill)
1362 {
1363 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1364 {
1365
1366 #ifdef CONFIG_SLIP_MODE_SLIP6
1367 unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1368 #else
1369 unsigned char s = END;
1370 #endif
1371
1372 if (!test_bit(0, (void *) &sl->dev->tbusy))
1373 {
1374
1375 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1376 sl->tty->driver.write(sl->tty, 0, &s, 1);
1377 }
1378 }
1379 else
1380 set_bit(SLF_OUTWAIT, &sl->flags);
1381 (void)del_timer(&sl->outfill_timer);
1382 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1383 add_timer(&sl->outfill_timer);
1384 }
1385 else
1386 del_timer(&sl->outfill_timer);
1387 }
1388
1389 static void sl_keepalive(unsigned long sls)
1390 {
1391 struct slip *sl=(struct slip *)sls;
1392
1393 if(sls==NULL)
1394 return;
1395
1396 if( sl->keepalive)
1397 {
1398 if(test_bit(SLF_KEEPTEST, &sl->flags))
1399 {
1400
1401 (void)del_timer(&sl->keepalive_timer);
1402 if( sl->outfill )
1403 (void)del_timer(&sl->outfill_timer);
1404 printk("%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1405 tty_hangup(sl->tty);
1406
1407 return;
1408 }
1409 else
1410 set_bit(SLF_KEEPTEST, &sl->flags);
1411 (void)del_timer(&sl->keepalive_timer);
1412 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1413 add_timer(&sl->keepalive_timer);
1414 }
1415 else
1416 (void)del_timer(&sl->keepalive_timer);
1417 }
1418
1419 #endif