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