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 sl_unlock(sl);
475 mark_bh(NET_BH);
476 return;
477 }
478
479 actual = tty->driver.write(tty, 0, sl->xhead, sl->xleft);
480 sl->xleft -= actual;
481 sl->xhead += actual;
482 }
483
484
485 static int
486 sl_xmit(struct sk_buff *skb, struct device *dev)
487 {
488 struct slip *sl = (struct slip*)(dev->priv);
489
490 if (!dev->start) {
491 printk("%s: xmit call when iface is down\n", dev->name);
492 return 1;
493 }
494
495
496
497
498
499
500
501
502
503
504 if (dev->tbusy) {
505
506
507
508 #ifdef SL_CHECK_TRANSMIT
509 if (jiffies - dev->trans_start < 20 * HZ) {
510
511 return 1;
512 }
513 printk("%s: transmit timed out, %s?\n", dev->name,
514 (sl->tty->driver.chars_in_buffer(sl->tty) || sl->xleft) ?
515 "bad line quality" : "driver error");
516 sl->xleft = 0;
517 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
518 sl_unlock(sl);
519 #else
520 return 1;
521 #endif
522 }
523
524
525 if (skb != NULL) {
526 sl_lock(sl);
527 sl_encaps(sl, skb->data, skb->len);
528 dev_kfree_skb(skb, FREE_WRITE);
529 }
530 return 0;
531 }
532
533
534
535
536
537 static int
538 sl_header(struct sk_buff *skb, struct device *dev, unsigned short type,
539 void *daddr, void *saddr, unsigned len)
540 {
541 #ifdef CONFIG_AX25
542 #ifdef CONFIG_INET
543 struct slip *sl = (struct slip*)(dev->priv);
544
545 if (sl->mode & SL_MODE_AX25 && type != htons(ETH_P_AX25)) {
546 return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
547 }
548 #endif
549 #endif
550 return 0;
551 }
552
553
554
555 static int
556 sl_rebuild_header(void *buff, struct device *dev, unsigned long raddr,
557 struct sk_buff *skb)
558 {
559 #ifdef CONFIG_AX25
560 #ifdef CONFIG_INET
561 struct slip *sl = (struct slip*)(dev->priv);
562
563 if (sl->mode & SL_MODE_AX25) {
564 return ax25_rebuild_header(buff, dev, raddr, skb);
565 }
566 #endif
567 #endif
568 return 0;
569 }
570
571
572
573 static int
574 sl_open(struct device *dev)
575 {
576 struct slip *sl = (struct slip*)(dev->priv);
577 unsigned long len;
578
579 if (sl->tty == NULL) {
580 return -ENODEV;
581 }
582
583
584
585
586
587
588
589
590 len = dev->mtu * 2;
591
592
593
594
595
596 if (len < 576 * 2) {
597 len = 576 * 2;
598 }
599 sl->rbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
600 if (sl->rbuff == NULL) {
601 goto norbuff;
602 }
603 sl->xbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
604 if (sl->xbuff == NULL) {
605 goto noxbuff;
606 }
607 #ifdef SL_INCLUDE_CSLIP
608 sl->cbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
609 if (sl->cbuff == NULL) {
610 goto nocbuff;
611 }
612 sl->slcomp = slhc_init(16, 16);
613 if (sl->slcomp == NULL) {
614 goto noslcomp;
615 }
616 #endif
617
618 #ifdef CONFIG_AX25
619 sl->mtu = dev->mtu + 73;
620 #else
621 sl->mtu = dev->mtu;
622 #endif
623 sl->buffsize = len;
624 sl->rcount = 0;
625 sl->xleft = 0;
626 #ifdef CONFIG_SLIP_MODE_SLIP6
627 sl->xdata = 0;
628 sl->xbits = 0;
629 #endif
630 sl->flags &= (1 << SLF_INUSE);
631 #ifdef CONFIG_SLIP_SMART
632 sl->keepalive=0;
633 init_timer(&sl->keepalive_timer);
634 sl->keepalive_timer.data=(unsigned long)sl;
635 sl->keepalive_timer.function=sl_keepalive;
636 sl->outfill=0;
637 init_timer(&sl->outfill_timer);
638 sl->outfill_timer.data=(unsigned long)sl;
639 sl->outfill_timer.function=sl_outfill;
640 #endif
641
642 if (dev->pa_addr == 0) {
643 dev->pa_addr=ntohl(0xC0A80001);
644 }
645 dev->tbusy = 0;
646
647 dev->start = 1;
648
649 return 0;
650
651
652 #ifdef SL_INCLUDE_CSLIP
653 noslcomp:
654 kfree(sl->cbuff);
655 nocbuff:
656 #endif
657 kfree(sl->xbuff);
658 noxbuff:
659 kfree(sl->rbuff);
660 norbuff:
661 return -ENOMEM;
662 }
663
664
665
666 static int
667 sl_close(struct device *dev)
668 {
669 struct slip *sl = (struct slip*)(dev->priv);
670
671 if (sl->tty == NULL) {
672 return -EBUSY;
673 }
674 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
675 dev->tbusy = 1;
676 dev->start = 0;
677
678
679
680 return 0;
681 }
682
683 static int
684 slip_receive_room(struct tty_struct *tty)
685 {
686 return 65536;
687 }
688
689
690
691
692
693
694
695 static void
696 slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
697 {
698 struct slip *sl = (struct slip *) tty->disc_data;
699
700 if (!sl || sl->magic != SLIP_MAGIC || !sl->dev->start)
701 return;
702
703
704
705
706
707 #ifdef CONFIG_AX25
708 if (sl->mtu != sl->dev->mtu + 73) {
709 #else
710 if (sl->mtu != sl->dev->mtu) {
711 #endif
712 sl_changedmtu(sl);
713 }
714
715
716 while (count--) {
717 if (fp && *fp++) {
718 if (!set_bit(SLF_ERROR, &sl->flags)) {
719 sl->rx_errors++;
720 }
721 cp++;
722 continue;
723 }
724 #ifdef CONFIG_SLIP_MODE_SLIP6
725 if (sl->mode & SL_MODE_SLIP6)
726 slip_unesc6(sl, *cp++);
727 else
728 #endif
729 slip_unesc(sl, *cp++);
730 }
731 }
732
733
734
735
736
737
738
739
740 static int
741 slip_open(struct tty_struct *tty)
742 {
743 struct slip *sl = (struct slip *) tty->disc_data;
744 int err;
745
746
747 if (sl && sl->magic == SLIP_MAGIC) {
748 return -EEXIST;
749 }
750
751
752 if ((sl = sl_alloc()) == NULL) {
753 return -ENFILE;
754 }
755
756 sl->tty = tty;
757 tty->disc_data = sl;
758 if (tty->driver.flush_buffer) {
759 tty->driver.flush_buffer(tty);
760 }
761 if (tty->ldisc.flush_buffer) {
762 tty->ldisc.flush_buffer(tty);
763 }
764
765
766 sl->mode = SL_MODE_DEFAULT;
767 sl->dev->type = ARPHRD_SLIP + sl->mode;
768 #ifdef CONFIG_AX25
769 if (sl->dev->type == 260) {
770 sl->dev->type = ARPHRD_AX25;
771 }
772 #endif
773
774 if ((err = sl_open(sl->dev))) {
775 return err;
776 }
777
778 MOD_INC_USE_COUNT;
779
780
781 return sl->dev->base_addr;
782 }
783
784
785
786
787
788
789
790
791 static void
792 slip_close(struct tty_struct *tty)
793 {
794 struct slip *sl = (struct slip *) tty->disc_data;
795
796
797 if (!sl || sl->magic != SLIP_MAGIC) {
798 return;
799 }
800
801 (void) dev_close(sl->dev);
802
803 tty->disc_data = 0;
804 sl->tty = NULL;
805
806 #ifdef CONFIG_SLIP_SMART
807 if (sl->keepalive)
808 (void)del_timer (&sl->keepalive_timer);
809 if (sl->outfill)
810 (void)del_timer (&sl->outfill_timer);
811 #endif
812 sl_free(sl);
813 unregister_netdev(sl->dev);
814 MOD_DEC_USE_COUNT;
815 }
816
817
818 static struct enet_statistics *
819 sl_get_stats(struct device *dev)
820 {
821 static struct enet_statistics stats;
822 struct slip *sl = (struct slip*)(dev->priv);
823 #ifdef SL_INCLUDE_CSLIP
824 struct slcompress *comp;
825 #endif
826
827 memset(&stats, 0, sizeof(struct enet_statistics));
828
829 stats.rx_packets = sl->rx_packets;
830 stats.tx_packets = sl->tx_packets;
831 stats.rx_dropped = sl->rx_dropped;
832 stats.tx_dropped = sl->tx_dropped;
833 stats.tx_errors = sl->tx_errors;
834 stats.rx_errors = sl->rx_errors;
835 stats.rx_over_errors = sl->rx_over_errors;
836 #ifdef SL_INCLUDE_CSLIP
837 stats.rx_fifo_errors = sl->rx_compressed;
838 stats.tx_fifo_errors = sl->tx_compressed;
839 stats.collisions = sl->tx_misses;
840 comp = sl->slcomp;
841 if (comp) {
842 stats.rx_fifo_errors += comp->sls_i_compressed;
843 stats.rx_dropped += comp->sls_i_tossed;
844 stats.tx_fifo_errors += comp->sls_o_compressed;
845 stats.collisions += comp->sls_o_misses;
846 }
847 #endif
848 return (&stats);
849 }
850
851
852
853
854
855
856 int
857 slip_esc(unsigned char *s, unsigned char *d, int len)
858 {
859 unsigned char *ptr = d;
860 unsigned char c;
861
862
863
864
865
866
867
868 *ptr++ = END;
869
870
871
872
873
874
875 while (len-- > 0) {
876 switch(c = *s++) {
877 case END:
878 *ptr++ = ESC;
879 *ptr++ = ESC_END;
880 break;
881 case ESC:
882 *ptr++ = ESC;
883 *ptr++ = ESC_ESC;
884 break;
885 default:
886 *ptr++ = c;
887 break;
888 }
889 }
890 *ptr++ = END;
891 return (ptr - d);
892 }
893
894 static void
895 slip_unesc(struct slip *sl, unsigned char s)
896 {
897
898 switch(s) {
899 case END:
900
901 if (test_bit(SLF_KEEPTEST, &sl->flags))
902 clear_bit(SLF_KEEPTEST, &sl->flags);
903
904 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
905 sl_bump(sl);
906 }
907 clear_bit(SLF_ESCAPE, &sl->flags);
908 sl->rcount = 0;
909 return;
910
911 case ESC:
912 set_bit(SLF_ESCAPE, &sl->flags);
913 return;
914 case ESC_ESC:
915 if (clear_bit(SLF_ESCAPE, &sl->flags)) {
916 s = ESC;
917 }
918 break;
919 case ESC_END:
920 if (clear_bit(SLF_ESCAPE, &sl->flags)) {
921 s = END;
922 }
923 break;
924 }
925 if (!test_bit(SLF_ERROR, &sl->flags)) {
926 if (sl->rcount < sl->buffsize) {
927 sl->rbuff[sl->rcount++] = s;
928 return;
929 }
930 sl->rx_over_errors++;
931 set_bit(SLF_ERROR, &sl->flags);
932 }
933 }
934
935
936 #ifdef CONFIG_SLIP_MODE_SLIP6
937
938
939
940
941 int
942 slip_esc6(unsigned char *s, unsigned char *d, int len)
943 {
944 unsigned char *ptr = d;
945 unsigned char c;
946 int i;
947 unsigned short v = 0;
948 short bits = 0;
949
950
951
952
953
954
955
956 *ptr++ = 0x70;
957
958
959
960
961
962 for (i = 0; i < len; ++i) {
963 v = (v << 8) | s[i];
964 bits += 8;
965 while (bits >= 6) {
966 bits -= 6;
967 c = 0x30 + ((v >> bits) & 0x3F);
968 *ptr++ = c;
969 }
970 }
971 if (bits) {
972 c = 0x30 + ((v << (6 - bits)) & 0x3F);
973 *ptr++ = c;
974 }
975 *ptr++ = 0x70;
976 return ptr - d;
977 }
978
979 void
980 slip_unesc6(struct slip *sl, unsigned char s)
981 {
982 unsigned char c;
983
984 if (s == 0x70) {
985
986 if (test_bit(SLF_KEEPTEST, &sl->flags))
987 clear_bit(SLF_KEEPTEST, &sl->flags);
988
989 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
990 sl_bump(sl);
991 }
992 sl->rcount = 0;
993 sl->xbits = 0;
994 sl->xdata = 0;
995 } else if (s >= 0x30 && s < 0x70) {
996 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
997 sl->xbits += 6;
998 if (sl->xbits >= 8) {
999 sl->xbits -= 8;
1000 c = (unsigned char)(sl->xdata >> sl->xbits);
1001 if (!test_bit(SLF_ERROR, &sl->flags)) {
1002 if (sl->rcount < sl->buffsize) {
1003 sl->rbuff[sl->rcount++] = c;
1004 return;
1005 }
1006 sl->rx_over_errors++;
1007 set_bit(SLF_ERROR, &sl->flags);
1008 }
1009 }
1010 }
1011 }
1012 #endif
1013
1014 #ifdef CONFIG_AX25
1015 int
1016 sl_set_mac_address(struct device *dev, void *addr)
1017 {
1018 int err;
1019
1020 err = verify_area(VERIFY_READ, addr, AX25_ADDR_LEN);
1021 if (err) {
1022 return err;
1023 }
1024
1025 memcpy_fromfs(dev->dev_addr, addr, AX25_ADDR_LEN);
1026
1027 return 0;
1028 }
1029
1030 static int
1031 sl_set_dev_mac_address(struct device *dev, void *addr)
1032 {
1033 struct sockaddr *sa=addr;
1034 memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
1035 return 0;
1036 }
1037 #endif
1038
1039
1040
1041 static int
1042 slip_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
1043 {
1044 struct slip *sl = (struct slip *) tty->disc_data;
1045 int err;
1046 unsigned int tmp;
1047
1048
1049 if (!sl || sl->magic != SLIP_MAGIC) {
1050 return -EINVAL;
1051 }
1052
1053 switch(cmd) {
1054 case SIOCGIFNAME:
1055 err = verify_area(VERIFY_WRITE, arg, strlen(sl->dev->name) + 1);
1056 if (err) {
1057 return err;
1058 }
1059 memcpy_tofs(arg, sl->dev->name, strlen(sl->dev->name) + 1);
1060 return 0;
1061
1062 case SIOCGIFENCAP:
1063 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1064 if (err) {
1065 return err;
1066 }
1067 put_user(sl->mode, (int *)arg);
1068 return 0;
1069
1070 case SIOCSIFENCAP:
1071 err = verify_area(VERIFY_READ, arg, sizeof(int));
1072 if (err) {
1073 return err;
1074 }
1075 tmp = get_user((int *)arg);
1076 #ifndef SL_INCLUDE_CSLIP
1077 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) {
1078 return -EINVAL;
1079 }
1080 #else
1081 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1082 (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
1083
1084 tmp &= ~SL_MODE_ADAPTIVE;
1085 }
1086 #endif
1087 #ifndef CONFIG_SLIP_MODE_SLIP6
1088 if (tmp & SL_MODE_SLIP6) {
1089 return -EINVAL;
1090 }
1091 #endif
1092 #ifndef CONFIG_AX25
1093 if (tmp & SL_MODE_AX25) {
1094 return -EINVAL;
1095 }
1096 #else
1097 if (tmp & SL_MODE_AX25) {
1098 sl->dev->addr_len=AX25_ADDR_LEN;
1099 sl->dev->hard_header_len=AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
1100 } else {
1101 sl->dev->addr_len=0;
1102 sl->dev->hard_header_len=0;
1103 }
1104 #endif
1105 sl->mode = tmp;
1106 sl->dev->type = ARPHRD_SLIP+sl->mode;
1107 #ifdef CONFIG_AX25
1108 if (sl->dev->type == 260) {
1109 sl->dev->type = ARPHRD_AX25;
1110 }
1111 #endif
1112 return 0;
1113
1114 case SIOCSIFHWADDR:
1115 #ifdef CONFIG_AX25
1116 return sl_set_mac_address(sl->dev, arg);
1117 #else
1118 return -EINVAL;
1119 #endif
1120
1121 #ifdef CONFIG_SLIP_SMART
1122
1123 case SIOCSKEEPALIVE:
1124 if (sl->keepalive)
1125 (void)del_timer (&sl->keepalive_timer);
1126 err = verify_area(VERIFY_READ, arg, sizeof(int));
1127 if (err) {
1128 return -err;
1129 }
1130 tmp = get_user((int *)arg);
1131 if (tmp > 255)
1132 return -EINVAL;
1133 if ((sl->keepalive = (unchar) tmp) != 0) {
1134 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1135 add_timer(&sl->keepalive_timer);
1136 set_bit(SLF_KEEPTEST, &sl->flags);
1137 }
1138 return 0;
1139
1140 case SIOCGKEEPALIVE:
1141 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1142 if (err) {
1143 return -err;
1144 }
1145 put_user(sl->keepalive, (int *)arg);
1146 return 0;
1147
1148 case SIOCSOUTFILL:
1149 if (sl->outfill)
1150 (void)del_timer (&sl->outfill_timer);
1151 err = verify_area(VERIFY_READ, arg, sizeof(int));
1152 if (err) {
1153 return -err;
1154 }
1155 tmp = get_user((int *)arg);
1156 if (tmp > 255)
1157 return -EINVAL;
1158 if ((sl->outfill = (unchar) tmp) != 0){
1159 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1160 add_timer(&sl->outfill_timer);
1161 set_bit(SLF_OUTWAIT, &sl->flags);
1162 }
1163 return 0;
1164
1165 case SIOCGOUTFILL:
1166 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1167 if (err) {
1168 return -err;
1169 }
1170 put_user(sl->outfill, (int *)arg);
1171 return 0;
1172
1173 #endif
1174
1175
1176 case TCGETS:
1177 case TCGETA:
1178 return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
1179
1180 default:
1181 return -ENOIOCTLCMD;
1182 }
1183 }
1184
1185 static int sl_open_dev(struct device *dev)
1186 {
1187 struct slip *sl = (struct slip*)(dev->priv);
1188 if(sl->tty==NULL)
1189 return -ENODEV;
1190 return 0;
1191 }
1192
1193
1194 #ifdef MODULE
1195 static int slip_init_ctrl_dev(void)
1196 #else
1197 int slip_init_ctrl_dev(struct device *dummy)
1198 #endif
1199 {
1200 int status;
1201
1202 if (slip_maxdev < 4) slip_maxdev = 4;
1203
1204 printk("SLIP: version %s (dynamic channels, max=%d)"
1205 #ifdef CONFIG_SLIP_MODE_SLIP6
1206 " (6 bit encapsulation enabled)"
1207 #endif
1208 ".\n",
1209 SLIP_VERSION, slip_maxdev );
1210 #if defined(SL_INCLUDE_CSLIP) && !defined(MODULE)
1211 printk("CSLIP: code copyright 1989 Regents of the University of California.\n");
1212 #endif
1213 #ifdef CONFIG_AX25
1214 printk("AX25: KISS encapsulation enabled.\n");
1215 #endif
1216 #ifdef CONFIG_SLIP_SMART
1217 printk("SLIP linefill/keepalive option.\n");
1218 #endif
1219
1220 slip_ctrls = (slip_ctrl_t **) kmalloc(sizeof(void*)*slip_maxdev, GFP_KERNEL);
1221 if (slip_ctrls == NULL)
1222 {
1223 printk("SLIP: Can't allocate slip_ctrls[] array! Uaargh! (-> No SLIP available)\n");
1224 return -ENOMEM;
1225 }
1226
1227
1228 memset(slip_ctrls, 0, sizeof(void*)*slip_maxdev);
1229
1230
1231 memset(&sl_ldisc, 0, sizeof(sl_ldisc));
1232 sl_ldisc.magic = TTY_LDISC_MAGIC;
1233 sl_ldisc.flags = 0;
1234 sl_ldisc.open = slip_open;
1235 sl_ldisc.close = slip_close;
1236 sl_ldisc.read = NULL;
1237 sl_ldisc.write = NULL;
1238 sl_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
1239 unsigned int, unsigned long)) slip_ioctl;
1240 sl_ldisc.select = NULL;
1241 sl_ldisc.receive_buf = slip_receive_buf;
1242 sl_ldisc.receive_room = slip_receive_room;
1243 sl_ldisc.write_wakeup = slip_write_wakeup;
1244 if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
1245 printk("SLIP: can't register line discipline (err = %d)\n", status);
1246 }
1247
1248
1249 #ifdef MODULE
1250 return status;
1251 #else
1252
1253
1254
1255 return ENODEV;
1256 #endif
1257 }
1258
1259
1260
1261 int
1262 slip_init(struct device *dev)
1263 {
1264 struct slip *sl = (struct slip*)(dev->priv);
1265 int i;
1266 #ifdef CONFIG_AX25
1267 static char ax25_bcast[AX25_ADDR_LEN] =
1268 {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
1269 static char ax25_test[AX25_ADDR_LEN] =
1270 {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
1271 #endif
1272
1273 if (sl == NULL)
1274 return -ENODEV;
1275
1276
1277
1278 memset(sl, 0, sizeof (struct slip));
1279 sl->magic = SLIP_MAGIC;
1280 sl->dev = dev;
1281
1282
1283 dev->mtu = SL_MTU;
1284 dev->hard_start_xmit = sl_xmit;
1285 dev->open = sl_open_dev;
1286 dev->stop = sl_close;
1287 dev->hard_header = sl_header;
1288 dev->get_stats = sl_get_stats;
1289 #ifdef HAVE_SET_MAC_ADDR
1290 #ifdef CONFIG_AX25
1291 dev->set_mac_address = sl_set_dev_mac_address;
1292 #endif
1293 #endif
1294 dev->hard_header_len = 0;
1295 dev->addr_len = 0;
1296 dev->type = ARPHRD_SLIP + SL_MODE_DEFAULT;
1297 dev->tx_queue_len = 10;
1298 #ifdef CONFIG_AX25
1299 if (sl->dev->type == 260) {
1300 sl->dev->type = ARPHRD_AX25;
1301 }
1302 memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
1303 memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);
1304 #endif
1305 dev->rebuild_header = sl_rebuild_header;
1306
1307 for (i = 0; i < DEV_NUMBUFFS; i++) {
1308 skb_queue_head_init(&dev->buffs[i]);
1309 }
1310
1311
1312 dev->flags = 0;
1313 dev->family = AF_INET;
1314 dev->pa_addr = 0;
1315 dev->pa_brdaddr = 0;
1316 dev->pa_mask = 0;
1317 dev->pa_alen = 4;
1318
1319 return 0;
1320 }
1321 #ifdef MODULE
1322
1323 int
1324 init_module(void)
1325 {
1326 return slip_init_ctrl_dev();
1327 }
1328
1329 void
1330 cleanup_module(void)
1331 {
1332 int i;
1333
1334 if (slip_ctrls != NULL)
1335 {
1336 for (i = 0; i < slip_maxdev; i++)
1337 {
1338 if (slip_ctrls[i]->dev.start)
1339
1340
1341 {
1342 unregister_netdev(&(slip_ctrls[i]->dev));
1343 kfree(slip_ctrls[i]);
1344 }
1345 }
1346 kfree(slip_ctrls);
1347 slip_ctrls = NULL;
1348 }
1349 if ((i = tty_register_ldisc(N_SLIP, NULL)))
1350 {
1351 printk("SLIP: can't unregister line discipline (err = %d)\n", i);
1352 }
1353 }
1354 #endif
1355
1356 #ifdef CONFIG_SLIP_SMART
1357
1358
1359
1360
1361
1362 static void sl_outfill(unsigned long sls)
1363 {
1364 struct slip *sl=(struct slip *)sls;
1365
1366 if(sls==0L)
1367 return;
1368
1369 if(sl->outfill)
1370 {
1371 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1372 {
1373
1374 #ifdef CONFIG_SLIP_MODE_SLIP6
1375 unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1376 #else
1377 unsigned char s = END;
1378 #endif
1379
1380 if (!test_bit(0, (void *) &sl->dev->tbusy))
1381 {
1382
1383 sl->tty->driver.write(sl->tty, 0, &s, 1);
1384 }
1385 }
1386 else
1387 set_bit(SLF_OUTWAIT, &sl->flags);
1388 (void)del_timer(&sl->outfill_timer);
1389 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1390 add_timer(&sl->outfill_timer);
1391 }
1392 else
1393 del_timer(&sl->outfill_timer);
1394 }
1395
1396 static void sl_keepalive(unsigned long sls)
1397 {
1398 struct slip *sl=(struct slip *)sls;
1399
1400 if(sls==NULL)
1401 return;
1402
1403 if( sl->keepalive)
1404 {
1405 if(test_bit(SLF_KEEPTEST, &sl->flags))
1406 {
1407
1408 (void)del_timer(&sl->keepalive_timer);
1409 if( sl->outfill )
1410 (void)del_timer(&sl->outfill_timer);
1411 printk("%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1412 tty_hangup(sl->tty);
1413
1414 return;
1415 }
1416 else
1417 set_bit(SLF_KEEPTEST, &sl->flags);
1418 (void)del_timer(&sl->keepalive_timer);
1419 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1420 add_timer(&sl->keepalive_timer);
1421 }
1422 else
1423 (void)del_timer(&sl->keepalive_timer);
1424 }
1425
1426 #endif