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 "slip.h"
80 #ifdef CONFIG_INET
81 #include <linux/ip.h>
82 #include <linux/tcp.h>
83 #include "slhc.h"
84 #endif
85
86 #ifdef MODULE
87 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY-MODULAR"
88 #else
89 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY"
90 #endif
91
92
93 typedef struct slip_ctrl {
94 char if_name[8];
95 struct slip ctrl;
96 struct device dev;
97 } slip_ctrl_t;
98 static slip_ctrl_t **slip_ctrls = NULL;
99 int slip_maxdev = SL_NRUNIT;
100
101 static struct tty_ldisc sl_ldisc;
102
103 static int slip_esc(unsigned char *p, unsigned char *d, int len);
104 static void slip_unesc(struct slip *sl, unsigned char c);
105 #ifdef CONFIG_SLIP_MODE_SLIP6
106 static int slip_esc6(unsigned char *p, unsigned char *d, int len);
107 static void slip_unesc6(struct slip *sl, unsigned char c);
108 #endif
109 #ifdef CONFIG_SLIP_SMART
110 static void sl_keepalive(unsigned long sls);
111 static void sl_outfill(unsigned long sls);
112 #endif
113
114
115 static inline struct slip *
116 sl_alloc(void)
117 {
118 slip_ctrl_t *slp;
119 int i;
120
121 if (slip_ctrls == NULL) return NULL;
122
123 for (i = 0; i < slip_maxdev; i++) {
124 slp = slip_ctrls[i];
125
126 if (slp == NULL)
127 break;
128
129 if (!set_bit(SLF_INUSE, &slp->ctrl.flags))
130 break;
131 }
132
133
134
135 if (i >= slip_maxdev) return NULL;
136
137
138 if (!slp &&
139 (slip_ctrls[i] = (slip_ctrl_t *)kmalloc(sizeof(slip_ctrl_t),
140 GFP_KERNEL)) != NULL) {
141 slp = slip_ctrls[i];
142 memset(slp, 0, sizeof(slip_ctrl_t));
143
144
145 set_bit(SLF_INUSE, &slp->ctrl.flags);
146 slp->ctrl.tty = NULL;
147 sprintf(slp->if_name, "sl%d", i);
148 slp->dev.name = slp->if_name;
149 slp->dev.base_addr = i;
150 slp->dev.priv = (void*)&(slp->ctrl);
151 slp->dev.next = NULL;
152 slp->dev.init = slip_init;
153
154
155 }
156 if (slp != NULL) {
157
158
159
160
161
162 if (register_netdev(&(slp->dev)) == 0) {
163
164 set_bit(SLF_INUSE, &slp->ctrl.flags);
165 slp->ctrl.dev = &(slp->dev);
166 slp->dev.priv = (void*)&(slp->ctrl);
167
168
169
170
171 return (&(slp->ctrl));
172
173 } else {
174 clear_bit(SLF_INUSE,&(slp->ctrl.flags));
175 printk("sl_alloc() - register_netdev() failure.\n");
176 }
177 }
178
179 return NULL;
180 }
181
182
183
184 static inline void
185 sl_free(struct slip *sl)
186 {
187
188 if (sl->rbuff) {
189 kfree(sl->rbuff);
190 }
191 sl->rbuff = NULL;
192 if (sl->xbuff) {
193 kfree(sl->xbuff);
194 }
195 sl->xbuff = NULL;
196 #ifdef SL_INCLUDE_CSLIP
197
198 if (sl->slcomp) {
199 sl->rx_compressed += sl->slcomp->sls_i_compressed;
200 sl->rx_dropped += sl->slcomp->sls_i_tossed;
201 sl->tx_compressed += sl->slcomp->sls_o_compressed;
202 sl->tx_misses += sl->slcomp->sls_o_misses;
203 }
204 if (sl->cbuff) {
205 kfree(sl->cbuff);
206 }
207 sl->cbuff = NULL;
208 if(sl->slcomp)
209 slhc_free(sl->slcomp);
210 sl->slcomp = NULL;
211 #endif
212
213 if (!clear_bit(SLF_INUSE, &sl->flags)) {
214 printk("%s: sl_free for already free unit.\n", sl->dev->name);
215 }
216 }
217
218
219
220
221
222 static void sl_changedmtu(struct slip *sl)
223 {
224 struct device *dev = sl->dev;
225 unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
226 #ifdef SL_INCLUDE_CSLIP
227 unsigned char *cbuff, *ocbuff;
228 #endif
229 int len;
230 unsigned long flags;
231
232 len = dev->mtu * 2;
233
234
235
236
237
238 if (len < 576 * 2) {
239 len = 576 * 2;
240 }
241
242 xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
243 rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
244 #ifdef SL_INCLUDE_CSLIP
245 cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
246 #endif
247
248 #ifdef SL_INCLUDE_CSLIP
249 if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
250 #else
251 if (xbuff == NULL || rbuff == NULL) {
252 #endif
253 printk("%s: unable to grow slip buffers, MTU change cancelled.\n",
254 sl->dev->name);
255 dev->mtu = sl->mtu;
256 if (xbuff != NULL) {
257 kfree(xbuff);
258 }
259 if (rbuff != NULL) {
260 kfree(rbuff);
261 }
262 #ifdef SL_INCLUDE_CSLIP
263 if (cbuff != NULL) {
264 kfree(cbuff);
265 }
266 #endif
267 return;
268 }
269
270 save_flags(flags); cli();
271
272 oxbuff = sl->xbuff;
273 sl->xbuff = xbuff;
274 orbuff = sl->rbuff;
275 sl->rbuff = rbuff;
276 #ifdef SL_INCLUDE_CSLIP
277 ocbuff = sl->cbuff;
278 sl->cbuff = cbuff;
279 #endif
280 if (sl->xleft) {
281 if (sl->xleft <= len) {
282 memcpy(sl->xbuff, sl->xhead, sl->xleft);
283 } else {
284 sl->xleft = 0;
285 sl->tx_dropped++;
286 }
287 }
288 sl->xhead = sl->xbuff;
289
290 if (sl->rcount) {
291 if (sl->rcount <= len) {
292 memcpy(sl->rbuff, orbuff, sl->rcount);
293 } else {
294 sl->rcount = 0;
295 sl->rx_over_errors++;
296 set_bit(SLF_ERROR, &sl->flags);
297 }
298 }
299 #ifdef CONFIG_AX25
300 sl->mtu = dev->mtu + 73;
301 #else
302 sl->mtu = dev->mtu;
303 #endif
304 sl->buffsize = len;
305
306 restore_flags(flags);
307
308 if (oxbuff != NULL) {
309 kfree(oxbuff);
310 }
311 if (orbuff != NULL) {
312 kfree(orbuff);
313 }
314 #ifdef SL_INCLUDE_CSLIP
315 if (ocbuff != NULL) {
316 kfree(ocbuff);
317 }
318 #endif
319 }
320
321
322
323 static inline void
324 sl_lock(struct slip *sl)
325 {
326 if (set_bit(0, (void *) &sl->dev->tbusy)) {
327 printk("%s: trying to lock already locked device!\n", sl->dev->name);
328 }
329 }
330
331
332
333 static inline void
334 sl_unlock(struct slip *sl)
335 {
336 if (!clear_bit(0, (void *)&sl->dev->tbusy)) {
337 printk("%s: trying to unlock already unlocked device!\n", sl->dev->name);
338 }
339 }
340
341
342 static void
343 sl_bump(struct slip *sl)
344 {
345 struct sk_buff *skb;
346 int count;
347
348 count = sl->rcount;
349 #ifdef SL_INCLUDE_CSLIP
350 if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
351 unsigned char c;
352 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
353
354 if (!(sl->mode & SL_MODE_CSLIP)) {
355 printk("%s: compressed packet ignored\n", sl->dev->name);
356 return;
357 }
358
359 if (count + 80 > sl->buffsize) {
360 sl->rx_over_errors++;
361 return;
362 }
363 count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
364 if (count <= 0) {
365 return;
366 }
367 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
368 if (!(sl->mode & SL_MODE_CSLIP)) {
369
370 sl->mode |= SL_MODE_CSLIP;
371 sl->mode &= ~SL_MODE_ADAPTIVE;
372 printk("%s: header compression turned on\n", sl->dev->name);
373 }
374 sl->rbuff[0] &= 0x4f;
375 if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
376 return;
377 }
378 }
379 }
380 #endif
381
382 skb = dev_alloc_skb(count);
383 if (skb == NULL) {
384 printk("%s: memory squeeze, dropping packet.\n", sl->dev->name);
385 sl->rx_dropped++;
386 return;
387 }
388 skb->dev = sl->dev;
389 memcpy(skb_put(skb,count), sl->rbuff, count);
390 skb->mac.raw=skb->data;
391 if(sl->mode & SL_MODE_AX25)
392 skb->protocol=htons(ETH_P_AX25);
393 else
394 skb->protocol=htons(ETH_P_IP);
395 netif_rx(skb);
396 sl->rx_packets++;
397 }
398
399
400 static void
401 sl_encaps(struct slip *sl, unsigned char *icp, int len)
402 {
403 unsigned char *p;
404 int actual, count;
405
406
407 #ifdef CONFIG_AX25
408 if (sl->mtu != sl->dev->mtu + 73) {
409 #else
410 if (sl->mtu != sl->dev->mtu) {
411 #endif
412 sl_changedmtu(sl);
413 }
414
415 if (len > sl->mtu) {
416 len = sl->mtu;
417 printk ("%s: truncating oversized transmit packet!\n", sl->dev->name);
418 sl->tx_dropped++;
419 sl_unlock(sl);
420 return;
421 }
422
423 p = icp;
424 #ifdef SL_INCLUDE_CSLIP
425 if (sl->mode & SL_MODE_CSLIP) {
426 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
427 }
428 #endif
429 #ifdef CONFIG_SLIP_MODE_SLIP6
430 if(sl->mode & SL_MODE_SLIP6)
431 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
432 else
433 #endif
434 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
435
436
437
438
439
440
441
442
443
444 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
445 actual = sl->tty->driver.write(sl->tty, 0, sl->xbuff, count);
446 #ifdef SL_CHECK_TRANSMIT
447 sl->dev->trans_start = jiffies;
448 #endif
449 sl->xleft = count - actual;
450 sl->xhead = sl->xbuff + actual;
451
452 clear_bit(SLF_OUTWAIT, &sl->flags);
453 }
454
455
456
457
458
459 static void slip_write_wakeup(struct tty_struct *tty)
460 {
461 int actual;
462 struct slip *sl = (struct slip *) tty->disc_data;
463
464
465 if (!sl || sl->magic != SLIP_MAGIC || !sl->dev->start) {
466 return;
467 }
468 if (sl->xleft <= 0) {
469
470
471 sl->tx_packets++;
472 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
473 if (test_bit(0, (void *) &sl->dev->tbusy))
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 sl_free(sl);
806 unregister_netdev(sl->dev);
807 MOD_DEC_USE_COUNT;
808 }
809
810
811 static struct enet_statistics *
812 sl_get_stats(struct device *dev)
813 {
814 static struct enet_statistics stats;
815 struct slip *sl = (struct slip*)(dev->priv);
816 #ifdef SL_INCLUDE_CSLIP
817 struct slcompress *comp;
818 #endif
819
820 memset(&stats, 0, sizeof(struct enet_statistics));
821
822 stats.rx_packets = sl->rx_packets;
823 stats.tx_packets = sl->tx_packets;
824 stats.rx_dropped = sl->rx_dropped;
825 stats.tx_dropped = sl->tx_dropped;
826 stats.tx_errors = sl->tx_errors;
827 stats.rx_errors = sl->rx_errors;
828 stats.rx_over_errors = sl->rx_over_errors;
829 #ifdef SL_INCLUDE_CSLIP
830 stats.rx_fifo_errors = sl->rx_compressed;
831 stats.tx_fifo_errors = sl->tx_compressed;
832 stats.collisions = sl->tx_misses;
833 comp = sl->slcomp;
834 if (comp) {
835 stats.rx_fifo_errors += comp->sls_i_compressed;
836 stats.rx_dropped += comp->sls_i_tossed;
837 stats.tx_fifo_errors += comp->sls_o_compressed;
838 stats.collisions += comp->sls_o_misses;
839 }
840 #endif
841 return (&stats);
842 }
843
844
845
846
847
848
849 int
850 slip_esc(unsigned char *s, unsigned char *d, int len)
851 {
852 unsigned char *ptr = d;
853 unsigned char c;
854
855
856
857
858
859
860
861 *ptr++ = END;
862
863
864
865
866
867
868 while (len-- > 0) {
869 switch(c = *s++) {
870 case END:
871 *ptr++ = ESC;
872 *ptr++ = ESC_END;
873 break;
874 case ESC:
875 *ptr++ = ESC;
876 *ptr++ = ESC_ESC;
877 break;
878 default:
879 *ptr++ = c;
880 break;
881 }
882 }
883 *ptr++ = END;
884 return (ptr - d);
885 }
886
887 static void
888 slip_unesc(struct slip *sl, unsigned char s)
889 {
890
891 switch(s) {
892 case END:
893
894 if (test_bit(SLF_KEEPTEST, &sl->flags))
895 clear_bit(SLF_KEEPTEST, &sl->flags);
896
897 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
898 sl_bump(sl);
899 }
900 clear_bit(SLF_ESCAPE, &sl->flags);
901 sl->rcount = 0;
902 return;
903
904 case ESC:
905 set_bit(SLF_ESCAPE, &sl->flags);
906 return;
907 case ESC_ESC:
908 if (clear_bit(SLF_ESCAPE, &sl->flags)) {
909 s = ESC;
910 }
911 break;
912 case ESC_END:
913 if (clear_bit(SLF_ESCAPE, &sl->flags)) {
914 s = END;
915 }
916 break;
917 }
918 if (!test_bit(SLF_ERROR, &sl->flags)) {
919 if (sl->rcount < sl->buffsize) {
920 sl->rbuff[sl->rcount++] = s;
921 return;
922 }
923 sl->rx_over_errors++;
924 set_bit(SLF_ERROR, &sl->flags);
925 }
926 }
927
928
929 #ifdef CONFIG_SLIP_MODE_SLIP6
930
931
932
933
934 int
935 slip_esc6(unsigned char *s, unsigned char *d, int len)
936 {
937 unsigned char *ptr = d;
938 unsigned char c;
939 int i;
940 unsigned short v = 0;
941 short bits = 0;
942
943
944
945
946
947
948
949 *ptr++ = 0x70;
950
951
952
953
954
955 for (i = 0; i < len; ++i) {
956 v = (v << 8) | s[i];
957 bits += 8;
958 while (bits >= 6) {
959 bits -= 6;
960 c = 0x30 + ((v >> bits) & 0x3F);
961 *ptr++ = c;
962 }
963 }
964 if (bits) {
965 c = 0x30 + ((v << (6 - bits)) & 0x3F);
966 *ptr++ = c;
967 }
968 *ptr++ = 0x70;
969 return ptr - d;
970 }
971
972 void
973 slip_unesc6(struct slip *sl, unsigned char s)
974 {
975 unsigned char c;
976
977 if (s == 0x70) {
978
979 if (test_bit(SLF_KEEPTEST, &sl->flags))
980 clear_bit(SLF_KEEPTEST, &sl->flags);
981
982 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
983 sl_bump(sl);
984 }
985 sl->rcount = 0;
986 sl->xbits = 0;
987 sl->xdata = 0;
988 } else if (s >= 0x30 && s < 0x70) {
989 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
990 sl->xbits += 6;
991 if (sl->xbits >= 8) {
992 sl->xbits -= 8;
993 c = (unsigned char)(sl->xdata >> sl->xbits);
994 if (!test_bit(SLF_ERROR, &sl->flags)) {
995 if (sl->rcount < sl->buffsize) {
996 sl->rbuff[sl->rcount++] = c;
997 return;
998 }
999 sl->rx_over_errors++;
1000 set_bit(SLF_ERROR, &sl->flags);
1001 }
1002 }
1003 }
1004 }
1005 #endif
1006
1007 #ifdef CONFIG_AX25
1008 int
1009 sl_set_mac_address(struct device *dev, void *addr)
1010 {
1011 int err;
1012
1013 err = verify_area(VERIFY_READ, addr, AX25_ADDR_LEN);
1014 if (err) {
1015 return err;
1016 }
1017
1018 memcpy_fromfs(dev->dev_addr, addr, AX25_ADDR_LEN);
1019
1020 return 0;
1021 }
1022
1023 static int
1024 sl_set_dev_mac_address(struct device *dev, void *addr)
1025 {
1026 struct sockaddr *sa=addr;
1027 memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
1028 return 0;
1029 }
1030 #endif
1031
1032
1033
1034 static int
1035 slip_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
1036 {
1037 struct slip *sl = (struct slip *) tty->disc_data;
1038 int err;
1039 unsigned int tmp;
1040
1041
1042 if (!sl || sl->magic != SLIP_MAGIC) {
1043 return -EINVAL;
1044 }
1045
1046 switch(cmd) {
1047 case SIOCGIFNAME:
1048 err = verify_area(VERIFY_WRITE, arg, strlen(sl->dev->name) + 1);
1049 if (err) {
1050 return err;
1051 }
1052 memcpy_tofs(arg, sl->dev->name, strlen(sl->dev->name) + 1);
1053 return 0;
1054
1055 case SIOCGIFENCAP:
1056 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1057 if (err) {
1058 return err;
1059 }
1060 put_user(sl->mode, (int *)arg);
1061 return 0;
1062
1063 case SIOCSIFENCAP:
1064 err = verify_area(VERIFY_READ, arg, sizeof(int));
1065 if (err) {
1066 return err;
1067 }
1068 tmp = get_user((int *)arg);
1069 #ifndef SL_INCLUDE_CSLIP
1070 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) {
1071 return -EINVAL;
1072 }
1073 #else
1074 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1075 (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
1076
1077 tmp &= ~SL_MODE_ADAPTIVE;
1078 }
1079 #endif
1080 #ifndef CONFIG_SLIP_MODE_SLIP6
1081 if (tmp & SL_MODE_SLIP6) {
1082 return -EINVAL;
1083 }
1084 #endif
1085 #ifndef CONFIG_AX25
1086 if (tmp & SL_MODE_AX25) {
1087 return -EINVAL;
1088 }
1089 #else
1090 if (tmp & SL_MODE_AX25) {
1091 sl->dev->addr_len=AX25_ADDR_LEN;
1092 sl->dev->hard_header_len=AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
1093 } else {
1094 sl->dev->addr_len=0;
1095 sl->dev->hard_header_len=0;
1096 }
1097 #endif
1098 sl->mode = tmp;
1099 sl->dev->type = ARPHRD_SLIP+sl->mode;
1100 #ifdef CONFIG_AX25
1101 if (sl->dev->type == 260) {
1102 sl->dev->type = ARPHRD_AX25;
1103 }
1104 #endif
1105 return 0;
1106
1107 case SIOCSIFHWADDR:
1108 #ifdef CONFIG_AX25
1109 return sl_set_mac_address(sl->dev, arg);
1110 #else
1111 return -EINVAL;
1112 #endif
1113
1114 #ifdef CONFIG_SLIP_SMART
1115
1116 case SIOCSKEEPALIVE:
1117 if (sl->keepalive)
1118 del_timer (&sl->keepalive_timer);
1119 err = verify_area(VERIFY_READ, arg, sizeof(int));
1120 if (err) {
1121 return -err;
1122 }
1123 tmp = get_user((int *)arg);
1124 if (tmp > 255)
1125 return -EINVAL;
1126 if ((sl->keepalive = (unchar) tmp) != 0) {
1127 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1128 add_timer(&sl->keepalive_timer);
1129 set_bit(SLF_KEEPTEST, &sl->flags);
1130 }
1131 return 0;
1132
1133 case SIOCGKEEPALIVE:
1134 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1135 if (err) {
1136 return -err;
1137 }
1138 put_user(sl->keepalive, (int *)arg);
1139 return 0;
1140
1141 case SIOCSOUTFILL:
1142 if (sl->outfill)
1143 (void)del_timer (&sl->outfill_timer);
1144 err = verify_area(VERIFY_READ, arg, sizeof(int));
1145 if (err) {
1146 return -err;
1147 }
1148 tmp = get_user((int *)arg);
1149 if (tmp > 255)
1150 return -EINVAL;
1151 if ((sl->outfill = (unchar) tmp) != 0){
1152 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1153 add_timer(&sl->outfill_timer);
1154 set_bit(SLF_OUTWAIT, &sl->flags);
1155 }
1156 return 0;
1157
1158 case SIOCGOUTFILL:
1159 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1160 if (err) {
1161 return -err;
1162 }
1163 put_user(sl->outfill, (int *)arg);
1164 return 0;
1165
1166 #endif
1167
1168
1169 case TCGETS:
1170 case TCGETA:
1171 return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
1172
1173 default:
1174 return -ENOIOCTLCMD;
1175 }
1176 }
1177
1178 static int sl_open_dev(struct device *dev)
1179 {
1180 struct slip *sl = (struct slip*)(dev->priv);
1181 if(sl->tty==NULL)
1182 return -ENODEV;
1183 return 0;
1184 }
1185
1186
1187 #ifdef MODULE
1188 static int slip_init_ctrl_dev(void)
1189 #else
1190 int slip_init_ctrl_dev(struct device *dummy)
1191 #endif
1192 {
1193 int status;
1194
1195 if (slip_maxdev < 4) slip_maxdev = 4;
1196
1197 printk("SLIP: version %s (dynamic channels, max=%d)"
1198 #ifdef CONFIG_SLIP_MODE_SLIP6
1199 " (6 bit encapsulation enabled)"
1200 #endif
1201 ".\n",
1202 SLIP_VERSION, slip_maxdev );
1203 #if defined(SL_INCLUDE_CSLIP) && !defined(MODULE)
1204 printk("CSLIP: code copyright 1989 Regents of the University of California.\n");
1205 #endif
1206 #ifdef CONFIG_AX25
1207 printk("AX25: KISS encapsulation enabled.\n");
1208 #endif
1209 #ifdef CONFIG_SLIP_SMART
1210 printk("SLIP linefill/keepalive option.\n");
1211 #endif
1212
1213 slip_ctrls = (slip_ctrl_t **) kmalloc(sizeof(void*)*slip_maxdev, GFP_KERNEL);
1214 if (slip_ctrls == NULL)
1215 {
1216 printk("SLIP: Can't allocate slip_ctrls[] array! Uaargh! (-> No SLIP available)\n");
1217 return -ENOMEM;
1218 }
1219
1220
1221 memset(slip_ctrls, 0, sizeof(void*)*slip_maxdev);
1222
1223
1224 memset(&sl_ldisc, 0, sizeof(sl_ldisc));
1225 sl_ldisc.magic = TTY_LDISC_MAGIC;
1226 sl_ldisc.flags = 0;
1227 sl_ldisc.open = slip_open;
1228 sl_ldisc.close = slip_close;
1229 sl_ldisc.read = NULL;
1230 sl_ldisc.write = NULL;
1231 sl_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
1232 unsigned int, unsigned long)) slip_ioctl;
1233 sl_ldisc.select = NULL;
1234 sl_ldisc.receive_buf = slip_receive_buf;
1235 sl_ldisc.receive_room = slip_receive_room;
1236 sl_ldisc.write_wakeup = slip_write_wakeup;
1237 if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
1238 printk("SLIP: can't register line discipline (err = %d)\n", status);
1239 }
1240
1241
1242 #ifdef MODULE
1243 return status;
1244 #else
1245
1246
1247
1248 return ENODEV;
1249 #endif
1250 }
1251
1252
1253
1254 int
1255 slip_init(struct device *dev)
1256 {
1257 struct slip *sl = (struct slip*)(dev->priv);
1258 int i;
1259 #ifdef CONFIG_AX25
1260 static char ax25_bcast[AX25_ADDR_LEN] =
1261 {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
1262 static char ax25_test[AX25_ADDR_LEN] =
1263 {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
1264 #endif
1265
1266 if (sl == NULL)
1267 return -ENODEV;
1268
1269
1270
1271 memset(sl, 0, sizeof (struct slip));
1272 sl->magic = SLIP_MAGIC;
1273 sl->dev = dev;
1274
1275
1276 dev->mtu = SL_MTU;
1277 dev->hard_start_xmit = sl_xmit;
1278 dev->open = sl_open_dev;
1279 dev->stop = sl_close;
1280 dev->hard_header = sl_header;
1281 dev->get_stats = sl_get_stats;
1282 #ifdef HAVE_SET_MAC_ADDR
1283 #ifdef CONFIG_AX25
1284 dev->set_mac_address = sl_set_dev_mac_address;
1285 #endif
1286 #endif
1287 dev->hard_header_len = 0;
1288 dev->addr_len = 0;
1289 dev->type = ARPHRD_SLIP + SL_MODE_DEFAULT;
1290 dev->tx_queue_len = 10;
1291 #ifdef CONFIG_AX25
1292 if (sl->dev->type == 260) {
1293 sl->dev->type = ARPHRD_AX25;
1294 }
1295 memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
1296 memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);
1297 #endif
1298 dev->rebuild_header = sl_rebuild_header;
1299
1300 for (i = 0; i < DEV_NUMBUFFS; i++) {
1301 skb_queue_head_init(&dev->buffs[i]);
1302 }
1303
1304
1305 dev->flags = 0;
1306 dev->family = AF_INET;
1307 dev->pa_addr = 0;
1308 dev->pa_brdaddr = 0;
1309 dev->pa_mask = 0;
1310 dev->pa_alen = 4;
1311
1312 return 0;
1313 }
1314 #ifdef MODULE
1315
1316 int
1317 init_module(void)
1318 {
1319 return slip_init_ctrl_dev();
1320 }
1321
1322 void
1323 cleanup_module(void)
1324 {
1325 int i;
1326
1327 if (slip_ctrls != NULL)
1328 {
1329 for (i = 0; i < slip_maxdev; i++)
1330 {
1331 if (slip_ctrls[i] != NULL)
1332 {
1333 unregister_netdev(&(slip_ctrls[i]->dev));
1334 kfree(slip_ctrls[i]);
1335 }
1336 }
1337 kfree(slip_ctrls);
1338 slip_ctrls = NULL;
1339 }
1340 if ((i = tty_register_ldisc(N_SLIP, NULL)))
1341 {
1342 printk("SLIP: can't unregister line discipline (err = %d)\n", i);
1343 }
1344 }
1345 #endif
1346
1347 #ifdef CONFIG_SLIP_SMART
1348
1349
1350
1351
1352
1353 static void sl_outfill(unsigned long sls)
1354 {
1355 struct slip *sl=(struct slip *)sls;
1356
1357 if(sls==NULL)
1358 return;
1359
1360 if(sl->outfill)
1361 {
1362 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1363 {
1364
1365 #ifdef CONFIG_SLIP_MODE_SLIP6
1366 unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1367 #else
1368 unsigned char s = END;
1369 #endif
1370
1371 if (!test_bit(0, (void *) &sl->dev->tbusy))
1372 {
1373
1374 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1375 sl->tty->driver.write(sl->tty, 0, &s, 1);
1376 }
1377 }
1378 else
1379 set_bit(SLF_OUTWAIT, &sl->flags);
1380 (void)del_timer(&sl->outfill_timer);
1381 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1382 add_timer(&sl->outfill_timer);
1383 }
1384 else
1385 del_timer(&sl->outfill_timer);
1386 }
1387
1388 static void sl_keepalive(unsigned long sls)
1389 {
1390 struct slip *sl=(struct slip *)sls;
1391
1392 if(sls==NULL)
1393 return;
1394
1395 if( sl->keepalive)
1396 {
1397 if(test_bit(SLF_KEEPTEST, &sl->flags))
1398 {
1399
1400 (void)del_timer(&sl->keepalive_timer);
1401 if( sl->outfill )
1402 (void)del_timer(&sl->outfill_timer);
1403 printk("%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1404 tty_hangup(sl->tty);
1405
1406 return;
1407 }
1408 else
1409 set_bit(SLF_KEEPTEST, &sl->flags);
1410 (void)del_timer(&sl->keepalive_timer);
1411 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1412 add_timer(&sl->keepalive_timer);
1413 }
1414 else
1415 (void)del_timer(&sl->keepalive_timer);
1416 }
1417
1418 #endif