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