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