This source file includes following definitions.
- ip_print
- ip_ioctl
- strict_route
- loose_route
- print_ipprot
- ip_route_check
- build_options
- ip_send
- ip_build_header
- do_options
- ip_fast_csum
- ip_compute_csum
- ip_csum
- ip_send_check
- ip_forward
- ip_rcv
- ip_queue_xmit
- ip_retransmit
- backoff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <asm/segment.h>
20 #include <asm/system.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
28 #include <linux/in.h>
29 #include "inet.h"
30 #include "dev.h"
31 #include "eth.h"
32 #include "ip.h"
33 #include "protocol.h"
34 #include "route.h"
35 #include "tcp.h"
36 #include "skbuff.h"
37 #include "sock.h"
38 #include "arp.h"
39 #include "icmp.h"
40
41 #define CONFIG_IP_FORWARD
42
43 extern int last_retran;
44 extern void sort_send(struct sock *sk);
45
46 void
47 ip_print(struct iphdr *ip)
48 {
49 unsigned char buff[32];
50 unsigned char *ptr;
51 int addr, len, i;
52
53 if (inet_debug != DBG_IP) return;
54
55
56 printk("IP: ihl=%d, version=%d, tos=%d, tot_len=%d\n",
57 ip->ihl, ip->version, ip->tos, ntohs(ip->tot_len));
58 printk(" id=%X, ttl=%d, prot=%d, check=%X\n",
59 ip->id, ip->ttl, ip->protocol, ip->check);
60 printk(" frag_off=%d\n", ip->frag_off);
61 printk(" soucre=%s ", in_ntoa(ip->saddr));
62 printk("dest=%s\n", in_ntoa(ip->daddr));
63 printk(" ----\n");
64
65
66 ptr = (unsigned char *)(ip + 1);
67 addr = 0;
68 len = ntohs(ip->tot_len) - (4 * ip->ihl);
69 while (len > 0) {
70 printk(" %04X: ", addr);
71 for(i = 0; i < 16; i++) {
72 if (len > 0) {
73 printk("%02X ", (*ptr & 0xFF));
74 buff[i] = *ptr++;
75 if (buff[i] < 32 || buff[i] > 126) buff[i] = '.';
76 } else {
77 printk(" ");
78 buff[i] = ' ';
79 }
80 addr++;
81 len--;
82 };
83 buff[i] = '\0';
84 printk(" \"%s\"\n", buff);
85 }
86 printk(" ----\n\n");
87 }
88
89
90 int
91 ip_ioctl(struct sock *sk, int cmd, unsigned long arg)
92 {
93 switch(cmd) {
94 case DDIOCSDBG:
95 return(dbg_ioctl((void *) arg, DBG_IP));
96 default:
97 return(-EINVAL);
98 }
99 }
100
101
102
103 static void
104 strict_route(struct iphdr *iph, struct options *opt)
105 {
106 }
107
108
109 static void
110 loose_route(struct iphdr *iph, struct options *opt)
111 {
112 }
113
114
115 static void
116 print_ipprot(struct inet_protocol *ipprot)
117 {
118 DPRINTF((DBG_IP, "handler = %X, protocol = %d, copy=%d \n",
119 ipprot->handler, ipprot->protocol, ipprot->copy));
120 }
121
122
123
124 void
125 ip_route_check(unsigned long daddr)
126 {
127 }
128
129
130 #if 0
131
132 static int
133 build_options(struct iphdr *iph, struct options *opt)
134 {
135 unsigned char *ptr;
136
137 ptr = (unsigned char *)(iph+1);
138 *ptr = 0;
139 return (4);
140 }
141 #endif
142
143
144
145 static int
146 ip_send(struct sk_buff *skb, unsigned long daddr, int len, struct device *dev,
147 unsigned long saddr)
148 {
149 unsigned char *ptr;
150 int mac;
151
152 ptr = (unsigned char *)(skb + 1);
153 mac = 0;
154 skb->arp = 1;
155 if (dev->hard_header) {
156 mac = dev->hard_header(ptr, dev, ETH_P_IP, daddr, saddr, len);
157 }
158 if (mac < 0) {
159 mac = -mac;
160 skb->arp = 0;
161 }
162 skb->dev = dev;
163 return(mac);
164 }
165
166
167
168
169
170
171
172
173 int
174 ip_build_header(struct sk_buff *skb, unsigned long saddr, unsigned long daddr,
175 struct device **dev, int type, struct options *opt, int len)
176 {
177 static struct options optmem;
178 struct iphdr *iph;
179 struct rtable *rt;
180 unsigned char *buff;
181 unsigned long raddr;
182 static int count = 0;
183 int tmp;
184
185 if (saddr == 0) saddr = my_addr();
186 DPRINTF((DBG_IP, "ip_build_header (skb=%X, saddr=%X, daddr=%X, *dev=%X,\n"
187 " type=%d, opt=%X, len = %d)\n",
188 skb, saddr, daddr, *dev, type, opt, len));
189 buff = (unsigned char *)(skb + 1);
190
191
192 if (*dev == NULL) {
193 rt = rt_route(daddr, &optmem);
194 if (rt == NULL) return(-ENETUNREACH);
195
196 *dev = rt->rt_dev;
197 if (daddr != 0x0100007F) saddr = rt->rt_dev->pa_addr;
198 raddr = rt->rt_gateway;
199
200 DPRINTF((DBG_IP, "ip_build_header: saddr set to %s\n", in_ntoa(saddr)));
201 opt = &optmem;
202 } else {
203
204 rt = rt_route(daddr, &optmem);
205 raddr = (rt == NULL) ? 0 : rt->rt_gateway;
206 }
207 if (raddr == 0) raddr = daddr;
208
209
210 tmp = ip_send(skb, raddr, len, *dev, saddr);
211 buff += tmp;
212 len -= tmp;
213
214 skb->dev = *dev;
215 skb->saddr = saddr;
216 if (skb->sk) skb->sk->saddr = saddr;
217
218
219
220
221
222
223 if(type == IPPROTO_RAW) return (tmp);
224
225 iph = (struct iphdr *)buff;
226 iph->version = 4;
227 iph->tos = 0;
228 iph->frag_off = 0;
229 iph->ttl = 32;
230 iph->daddr = daddr;
231 iph->saddr = saddr;
232 iph->protocol = type;
233 iph->ihl = 5;
234 iph->id = htons(count++);
235
236
237 #ifdef Not_Yet_Avail
238 build_options(iph, opt);
239 #endif
240
241 return(20 + tmp);
242 }
243
244
245 static int
246 do_options(struct iphdr *iph, struct options *opt)
247 {
248 unsigned char *buff;
249 int done = 0;
250 int i, len = sizeof(struct iphdr);
251
252
253 opt->record_route.route_size = 0;
254 opt->loose_route.route_size = 0;
255 opt->strict_route.route_size = 0;
256 opt->tstamp.ptr = 0;
257 opt->security = 0;
258 opt->compartment = 0;
259 opt->handling = 0;
260 opt->stream = 0;
261 opt->tcc = 0;
262 return(0);
263
264
265 buff = (unsigned char *)(iph + 1);
266
267
268 while (!done && len < iph->ihl*4) switch(*buff) {
269 case IPOPT_END:
270 done = 1;
271 break;
272 case IPOPT_NOOP:
273 buff++;
274 len++;
275 break;
276 case IPOPT_SEC:
277 buff++;
278 if (*buff != 11) return(1);
279 buff++;
280 opt->security = ntohs(*(unsigned short *)buff);
281 buff += 2;
282 opt->compartment = ntohs(*(unsigned short *)buff);
283 buff += 2;
284 opt->handling = ntohs(*(unsigned short *)buff);
285 buff += 2;
286 opt->tcc = ((*buff) << 16) + ntohs(*(unsigned short *)(buff+1));
287 buff += 3;
288 len += 11;
289 break;
290 case IPOPT_LSRR:
291 buff++;
292 if ((*buff - 3)% 4 != 0) return(1);
293 len += *buff;
294 opt->loose_route.route_size = (*buff -3)/4;
295 buff++;
296 if (*buff % 4 != 0) return(1);
297 opt->loose_route.pointer = *buff/4 - 1;
298 buff++;
299 buff++;
300 for (i = 0; i < opt->loose_route.route_size; i++) {
301 opt->loose_route.route[i] = *(unsigned long *)buff;
302 buff += 4;
303 }
304 break;
305 case IPOPT_SSRR:
306 buff++;
307 if ((*buff - 3)% 4 != 0) return(1);
308 len += *buff;
309 opt->strict_route.route_size = (*buff -3)/4;
310 buff++;
311 if (*buff % 4 != 0) return(1);
312 opt->strict_route.pointer = *buff/4 - 1;
313 buff++;
314 buff++;
315 for (i = 0; i < opt->strict_route.route_size; i++) {
316 opt->strict_route.route[i] = *(unsigned long *)buff;
317 buff += 4;
318 }
319 break;
320 case IPOPT_RR:
321 buff++;
322 if ((*buff - 3)% 4 != 0) return(1);
323 len += *buff;
324 opt->record_route.route_size = (*buff -3)/4;
325 buff++;
326 if (*buff % 4 != 0) return(1);
327 opt->record_route.pointer = *buff/4 - 1;
328 buff++;
329 buff++;
330 for (i = 0; i < opt->record_route.route_size; i++) {
331 opt->record_route.route[i] = *(unsigned long *)buff;
332 buff += 4;
333 }
334 break;
335 case IPOPT_SID:
336 len += 4;
337 buff +=2;
338 opt->stream = *(unsigned short *)buff;
339 buff += 2;
340 break;
341 case IPOPT_TIMESTAMP:
342 buff++;
343 len += *buff;
344 if (*buff % 4 != 0) return(1);
345 opt->tstamp.len = *buff / 4 - 1;
346 buff++;
347 if ((*buff - 1) % 4 != 0) return(1);
348 opt->tstamp.ptr = (*buff-1)/4;
349 buff++;
350 opt->tstamp.x.full_char = *buff;
351 buff++;
352 for (i = 0; i < opt->tstamp.len; i++) {
353 opt->tstamp.data[i] = *(unsigned long *)buff;
354 buff += 4;
355 }
356 break;
357 default:
358 return(1);
359 }
360
361 if (opt->record_route.route_size == 0) {
362 if (opt->strict_route.route_size != 0) {
363 memcpy(&(opt->record_route), &(opt->strict_route),
364 sizeof(opt->record_route));
365 } else if (opt->loose_route.route_size != 0) {
366 memcpy(&(opt->record_route), &(opt->loose_route),
367 sizeof(opt->record_route));
368 }
369 }
370
371 if (opt->strict_route.route_size != 0 &&
372 opt->strict_route.route_size != opt->strict_route.pointer) {
373 strict_route(iph, opt);
374 return(0);
375 }
376
377 if (opt->loose_route.route_size != 0 &&
378 opt->loose_route.route_size != opt->loose_route.pointer) {
379 loose_route(iph, opt);
380 return(0);
381 }
382
383 return(0);
384 }
385
386
387
388 static inline unsigned short
389 ip_fast_csum(unsigned char * buff, int wlen)
390 {
391 unsigned long sum = 0;
392 __asm__("\t clc\n"
393 "1:\n"
394 "\t lodsl\n"
395 "\t adcl %%eax, %%ebx\n"
396 "\t loop 1b\n"
397 "\t adcl $0, %%ebx\n"
398 "\t movl %%ebx, %%eax\n"
399 "\t shrl $16, %%eax\n"
400 "\t addw %%ax, %%bx\n"
401 "\t adcw $0, %%bx\n"
402 : "=b" (sum) , "=S" (buff)
403 : "0" (sum), "c" (wlen) ,"1" (buff)
404 : "ax", "cx", "si", "bx" );
405 return (~sum) & 0xffff;
406 }
407
408
409
410
411
412 unsigned short
413 ip_compute_csum(unsigned char * buff, int len)
414 {
415 unsigned long sum = 0;
416
417
418 if (len > 3) {
419 __asm__("\t clc\n"
420 "1:\n"
421 "\t lodsl\n"
422 "\t adcl %%eax, %%ebx\n"
423 "\t loop 1b\n"
424 "\t adcl $0, %%ebx\n"
425 "\t movl %%ebx, %%eax\n"
426 "\t shrl $16, %%eax\n"
427 "\t addw %%ax, %%bx\n"
428 "\t adcw $0, %%bx\n"
429 : "=b" (sum) , "=S" (buff)
430 : "0" (sum), "c" (len >> 2) ,"1" (buff)
431 : "ax", "cx", "si", "bx" );
432 }
433 if (len & 2) {
434 __asm__("\t lodsw\n"
435 "\t addw %%ax, %%bx\n"
436 "\t adcw $0, %%bx\n"
437 : "=b" (sum), "=S" (buff)
438 : "0" (sum), "1" (buff)
439 : "bx", "ax", "si");
440 }
441 if (len & 1) {
442 __asm__("\t lodsb\n"
443 "\t movb $0, %%ah\n"
444 "\t addw %%ax, %%bx\n"
445 "\t adcw $0, %%bx\n"
446 : "=b" (sum), "=S" (buff)
447 : "0" (sum), "1" (buff)
448 : "bx", "ax", "si");
449 }
450 sum =~sum;
451 return(sum & 0xffff);
452 }
453
454
455 int
456 ip_csum(struct iphdr *iph)
457 {
458 if (iph->check == 0 || ip_fast_csum((unsigned char *)iph, iph->ihl) == 0)
459 return(0);
460 return(1);
461 }
462
463
464 static void
465 ip_send_check(struct iphdr *iph)
466 {
467 iph->check = 0;
468 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
469 }
470
471
472
473 static void
474 ip_forward(struct sk_buff *skb, struct device *dev)
475 {
476 struct device *dev2;
477 struct iphdr *iph;
478 struct sk_buff *skb2;
479 struct rtable *rt;
480 unsigned char *ptr;
481 unsigned long raddr;
482
483
484
485
486
487
488 iph = skb->h.iph;
489 iph->ttl--;
490 if (iph->ttl <= 0) {
491 DPRINTF((DBG_IP, "\nIP: *** datagram expired: TTL=0 (ignored) ***\n"));
492 DPRINTF((DBG_IP, " SRC = %s ", in_ntoa(iph->saddr)));
493 DPRINTF((DBG_IP, " DST = %s (ignored)\n", in_ntoa(iph->daddr)));
494
495
496 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, dev);
497 return;
498 }
499
500
501 ip_send_check(iph);
502
503
504
505
506
507 rt = rt_route(iph->daddr, NULL);
508 if (rt == NULL) {
509 DPRINTF((DBG_IP, "\nIP: *** routing (phase I) failed ***\n"));
510
511
512 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, dev);
513 return;
514 }
515
516
517
518
519
520
521
522
523 raddr = rt->rt_gateway;
524 if (raddr != 0) {
525 rt = rt_route(raddr, NULL);
526 if (rt == NULL) {
527 DPRINTF((DBG_IP, "\nIP: *** routing (phase II) failed ***\n"));
528
529
530 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, dev);
531 return;
532 }
533 if (rt->rt_gateway != 0) raddr = rt->rt_gateway;
534 } else raddr = iph->daddr;
535 dev2 = rt->rt_dev;
536
537
538
539
540
541 DPRINTF((DBG_IP, "\nIP: *** fwd %s -> ", in_ntoa(iph->saddr)));
542 DPRINTF((DBG_IP, "%s (via %s), LEN=%d\n",
543 in_ntoa(raddr), dev2->name, skb->len));
544
545 if (dev2->flags & IFF_UP) {
546 skb2 = (struct sk_buff *) kmalloc(sizeof(struct sk_buff) +
547 dev2->hard_header_len + skb->len, GFP_ATOMIC);
548 if (skb2 == NULL) {
549 printk("\nIP: No memory available for IP forward\n");
550 return;
551 }
552 ptr = (unsigned char *)(skb2 + 1);
553 skb2->lock = 0;
554 skb2->sk = NULL;
555 skb2->len = skb->len + dev2->hard_header_len;
556 skb2->mem_addr = skb2;
557 skb2->mem_len = sizeof(struct sk_buff) + skb2->len;
558 skb2->next = NULL;
559 skb2->h.raw = ptr;
560
561
562 skb2->h.raw = ptr;
563 memcpy(ptr + dev2->hard_header_len, skb->h.raw, skb->len);
564
565
566 (void) ip_send(skb2, raddr, skb->len, dev2, dev2->pa_addr);
567
568 dev2->queue_xmit(skb2, dev2, SOPRI_NORMAL);
569 }
570 }
571
572
573
574 int
575 ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
576 {
577 struct iphdr *iph = skb->h.iph;
578 unsigned char hash;
579 unsigned char flag = 0;
580 unsigned char opts_p = 0;
581 struct inet_protocol *ipprot;
582 static struct options opt;
583
584 int brd;
585
586 DPRINTF((DBG_IP, "<<\n"));
587
588
589 if (iph->version != 4
590 || (iph->check != 0 && ip_fast_csum((unsigned char *)iph, iph->ihl) !=0)) {
591 DPRINTF((DBG_IP, "\nIP: *** datagram error ***\n"));
592 DPRINTF((DBG_IP, " SRC = %s ", in_ntoa(iph->saddr)));
593 DPRINTF((DBG_IP, " DST = %s (ignored)\n", in_ntoa(iph->daddr)));
594 skb->sk = NULL;
595 kfree_skb(skb, FREE_WRITE);
596 return(0);
597 }
598
599 if (iph->ihl != 5) {
600 ip_print(iph);
601 memset((char *) &opt, 0, sizeof(opt));
602 if (do_options(iph, &opt) != 0)
603 return 0;
604 opts_p = 1;
605 }
606
607
608 if ((brd = chk_addr(iph->daddr)) == 0) {
609 #ifdef CONFIG_IP_FORWARD
610 ip_forward(skb, dev);
611 #endif
612 skb->sk = NULL;
613 kfree_skb(skb, FREE_WRITE);
614 return(0);
615 }
616
617
618
619
620 if ((iph->frag_off & 0x0020) || (ntohs(iph->frag_off) & 0x1fff)) {
621 #ifdef CONFIG_IP_DEFRAG
622 ip_defrag(skb);
623 #else
624 printk("\nIP: *** datagram fragmentation not yet implemented ***\n");
625 printk(" SRC = %s ", in_ntoa(iph->saddr));
626 printk(" DST = %s (ignored)\n", in_ntoa(iph->daddr));
627 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
628 skb->sk = NULL;
629 kfree_skb(skb, FREE_WRITE);
630 return(0);
631 #endif
632 }
633
634
635 skb->h.raw += iph->ihl*4;
636 hash = iph->protocol & (MAX_INET_PROTOS -1);
637 for (ipprot = (struct inet_protocol *)inet_protos[hash];
638 ipprot != NULL;
639 ipprot=(struct inet_protocol *)ipprot->next)
640 {
641 struct sk_buff *skb2;
642
643 if (ipprot->protocol != iph->protocol) continue;
644 DPRINTF((DBG_IP, "Using protocol = %X:\n", ipprot));
645 print_ipprot(ipprot);
646
647
648
649
650
651
652 if (ipprot->copy) {
653 skb2 = (struct sk_buff *) kmalloc (skb->mem_len, GFP_ATOMIC);
654 if (skb2 == NULL) continue;
655 memcpy(skb2, skb, skb->mem_len);
656 skb2->mem_addr = skb2;
657 skb2->lock = 0;
658 skb2->h.raw = (unsigned char *)(
659 (unsigned long)skb2 +
660 (unsigned long) skb->h.raw -
661 (unsigned long)skb);
662 } else {
663 skb2 = skb;
664 }
665 flag = 1;
666
667
668
669
670
671
672 ipprot->handler(skb2, dev, opts_p ? &opt : 0, iph->daddr,
673 (ntohs(iph->tot_len) - (iph->ihl * 4)),
674 iph->saddr, 0, ipprot);
675
676 }
677
678
679
680
681
682
683
684 if (!flag) {
685 if (brd != IS_BROADCAST)
686 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
687 skb->sk = NULL;
688 kfree_skb(skb, FREE_WRITE);
689 }
690
691 return(0);
692 }
693
694
695
696
697
698
699
700
701
702 void
703 ip_queue_xmit(struct sock *sk, struct device *dev,
704 struct sk_buff *skb, int free)
705 {
706 struct iphdr *iph;
707 unsigned char *ptr;
708
709 if (sk == NULL) free = 1;
710 if (dev == NULL) {
711 printk("IP: ip_queue_xmit dev = NULL\n");
712 return;
713 }
714 skb->free = free;
715 skb->dev = dev;
716 skb->when = jiffies;
717
718 DPRINTF((DBG_IP, ">>\n"));
719 ptr = (unsigned char *)(skb + 1);
720 ptr += dev->hard_header_len;
721 iph = (struct iphdr *)ptr;
722 iph->tot_len = ntohs(skb->len - dev->hard_header_len);
723 ip_send_check(iph);
724 ip_print(iph);
725 skb->next = NULL;
726
727
728 skb->magic = 1;
729 if (!free) {
730 skb->link3 = NULL;
731 sk->packets_out++;
732 cli();
733 if (sk->send_head == NULL) {
734 sk->send_tail = skb;
735 sk->send_head = skb;
736 } else {
737
738 if (sk->send_tail == NULL) {
739 printk("IP: ***bug sk->send_tail == NULL != sk->send_head\n");
740 sort_send(sk);
741 } else {
742 sk->send_tail->link3 = skb;
743 sk->send_tail = skb;
744 }
745 }
746 sti();
747 reset_timer(sk, TIME_WRITE,
748 backoff(sk->backoff) * (2 * sk->mdev + sk->rtt));
749 } else {
750 skb->sk = sk;
751 }
752
753
754 if (dev->flags & IFF_UP) {
755 if (sk != NULL) {
756 dev->queue_xmit(skb, dev, sk->priority);
757 } else {
758 dev->queue_xmit(skb, dev, SOPRI_NORMAL);
759 }
760 } else {
761 if (free) kfree_skb(skb, FREE_WRITE);
762 }
763 }
764
765
766 void
767 ip_retransmit(struct sock *sk, int all)
768 {
769 struct sk_buff * skb;
770 struct proto *prot;
771 struct device *dev;
772
773 prot = sk->prot;
774 skb = sk->send_head;
775 while (skb != NULL) {
776 dev = skb->dev;
777
778 if(dev==NULL)
779 {
780 printk("ip_forward: NULL device bug!\n");
781 goto oops;
782 }
783
784
785
786
787
788
789 if (!skb->arp) {
790 if (dev->rebuild_header((struct enet_header *)(skb+1),dev)) {
791 if (!all) break;
792 skb = (struct sk_buff *)skb->link3;
793 continue;
794 }
795 }
796 skb->arp = 1;
797 skb->when = jiffies;
798
799
800 if (dev->flags & IFF_UP) {
801 if (sk) dev->queue_xmit(skb, dev, sk->priority);
802 else dev->queue_xmit(skb, dev, SOPRI_NORMAL );
803 }
804
805 oops: sk->retransmits++;
806 sk->prot->retransmits ++;
807 if (!all) break;
808
809
810 if (sk->retransmits > sk->cong_window) break;
811 skb = (struct sk_buff *)skb->link3;
812 }
813
814
815
816
817
818
819
820 sk->backoff++;
821 reset_timer(sk, TIME_WRITE, backoff(sk->backoff) * (2 * sk->mdev + sk->rtt));
822 }
823
824
825 int backoff(int n)
826 {
827
828
829
830
831
832 if(n <= 4)
833 return 1 << n;
834 else
835 return n * n;
836 }