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_frag_create
- ip_find
- ip_free
- ip_expire
- ip_create
- ip_done
- ip_glue
- ip_defrag
- ip_fragment
- 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
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 #include <asm/segment.h>
45 #include <asm/system.h>
46 #include <linux/types.h>
47 #include <linux/kernel.h>
48 #include <linux/sched.h>
49 #include <linux/string.h>
50 #include <linux/errno.h>
51 #include <linux/socket.h>
52 #include <linux/sockios.h>
53 #include <linux/in.h>
54 #include "inet.h"
55 #include "dev.h"
56 #include "eth.h"
57 #include "ip.h"
58 #include "protocol.h"
59 #include "route.h"
60 #include "tcp.h"
61 #include "skbuff.h"
62 #include "sock.h"
63 #include "arp.h"
64 #include "icmp.h"
65
66 #define CONFIG_IP_FORWARD
67 #define CONFIG_IP_DEFRAG
68
69 extern int last_retran;
70 extern void sort_send(struct sock *sk);
71
72 void
73 ip_print(struct iphdr *ip)
74 {
75 unsigned char buff[32];
76 unsigned char *ptr;
77 int addr, len, i;
78
79 if (inet_debug != DBG_IP) return;
80
81
82 printk("IP: ihl=%d, version=%d, tos=%d, tot_len=%d\n",
83 ip->ihl, ip->version, ip->tos, ntohs(ip->tot_len));
84 printk(" id=%X, ttl=%d, prot=%d, check=%X\n",
85 ip->id, ip->ttl, ip->protocol, ip->check);
86 printk(" frag_off=%d\n", ip->frag_off);
87 printk(" soucre=%s ", in_ntoa(ip->saddr));
88 printk("dest=%s\n", in_ntoa(ip->daddr));
89 printk(" ----\n");
90
91
92 ptr = (unsigned char *)(ip + 1);
93 addr = 0;
94 len = ntohs(ip->tot_len) - (4 * ip->ihl);
95 while (len > 0) {
96 printk(" %04X: ", addr);
97 for(i = 0; i < 16; i++) {
98 if (len > 0) {
99 printk("%02X ", (*ptr & 0xFF));
100 buff[i] = *ptr++;
101 if (buff[i] < 32 || buff[i] > 126) buff[i] = '.';
102 } else {
103 printk(" ");
104 buff[i] = ' ';
105 }
106 addr++;
107 len--;
108 };
109 buff[i] = '\0';
110 printk(" \"%s\"\n", buff);
111 }
112 printk(" ----\n\n");
113 }
114
115
116 int
117 ip_ioctl(struct sock *sk, int cmd, unsigned long arg)
118 {
119 switch(cmd) {
120 case DDIOCSDBG:
121 return(dbg_ioctl((void *) arg, DBG_IP));
122 default:
123 return(-EINVAL);
124 }
125 }
126
127
128
129 static void
130 strict_route(struct iphdr *iph, struct options *opt)
131 {
132 }
133
134
135 static void
136 loose_route(struct iphdr *iph, struct options *opt)
137 {
138 }
139
140
141 static void
142 print_ipprot(struct inet_protocol *ipprot)
143 {
144 DPRINTF((DBG_IP, "handler = %X, protocol = %d, copy=%d \n",
145 ipprot->handler, ipprot->protocol, ipprot->copy));
146 }
147
148
149
150 void
151 ip_route_check(unsigned long daddr)
152 {
153 }
154
155
156 #if 0
157
158 static int
159 build_options(struct iphdr *iph, struct options *opt)
160 {
161 unsigned char *ptr;
162
163 ptr = (unsigned char *)(iph+1);
164 *ptr = 0;
165 return (4);
166 }
167 #endif
168
169
170
171 static int
172 ip_send(struct sk_buff *skb, unsigned long daddr, int len, struct device *dev,
173 unsigned long saddr)
174 {
175 unsigned char *ptr;
176 int mac;
177
178 ptr = (unsigned char *)(skb + 1);
179 mac = 0;
180 skb->arp = 1;
181 if (dev->hard_header) {
182 mac = dev->hard_header(ptr, dev, ETH_P_IP, daddr, saddr, len);
183 }
184 if (mac < 0) {
185 mac = -mac;
186 skb->arp = 0;
187 }
188 skb->dev = dev;
189 return(mac);
190 }
191
192
193
194
195
196
197
198
199 int
200 ip_build_header(struct sk_buff *skb, unsigned long saddr, unsigned long daddr,
201 struct device **dev, int type, struct options *opt, int len)
202 {
203 static struct options optmem;
204 struct iphdr *iph;
205 struct rtable *rt;
206 unsigned char *buff;
207 unsigned long raddr;
208 static int count = 0;
209 int tmp;
210
211 if (saddr == 0)
212 saddr = my_addr();
213
214 DPRINTF((DBG_IP, "ip_build_header (skb=%X, saddr=%X, daddr=%X, *dev=%X,\n"
215 " type=%d, opt=%X, len = %d)\n",
216 skb, saddr, daddr, *dev, type, opt, len));
217
218 buff = (unsigned char *)(skb + 1);
219
220
221 if (*dev == NULL) {
222 rt = rt_route(daddr, &optmem);
223 if (rt == NULL)
224 return(-ENETUNREACH);
225
226 *dev = rt->rt_dev;
227 if (saddr == 0x0100007FL && daddr != 0x0100007FL)
228 saddr = rt->rt_dev->pa_addr;
229 raddr = rt->rt_gateway;
230
231 DPRINTF((DBG_IP, "ip_build_header: saddr set to %s\n", in_ntoa(saddr)));
232 opt = &optmem;
233 } else {
234
235 rt = rt_route(daddr, &optmem);
236 raddr = (rt == NULL) ? 0 : rt->rt_gateway;
237 }
238 if (raddr == 0)
239 raddr = daddr;
240
241
242 tmp = ip_send(skb, raddr, len, *dev, saddr);
243 buff += tmp;
244 len -= tmp;
245
246 skb->dev = *dev;
247 skb->saddr = saddr;
248 if (skb->sk) skb->sk->saddr = saddr;
249
250
251
252
253
254
255 if(type == IPPROTO_RAW) return (tmp);
256
257 iph = (struct iphdr *)buff;
258 iph->version = 4;
259 iph->tos = 0;
260 iph->frag_off = 0;
261 iph->ttl = 32;
262 iph->daddr = daddr;
263 iph->saddr = saddr;
264 iph->protocol = type;
265 iph->ihl = 5;
266 iph->id = htons(count++);
267
268
269 #ifdef Not_Yet_Avail
270 build_options(iph, opt);
271 #endif
272
273 return(20 + tmp);
274 }
275
276
277 static int
278 do_options(struct iphdr *iph, struct options *opt)
279 {
280 unsigned char *buff;
281 int done = 0;
282 int i, len = sizeof(struct iphdr);
283
284
285 opt->record_route.route_size = 0;
286 opt->loose_route.route_size = 0;
287 opt->strict_route.route_size = 0;
288 opt->tstamp.ptr = 0;
289 opt->security = 0;
290 opt->compartment = 0;
291 opt->handling = 0;
292 opt->stream = 0;
293 opt->tcc = 0;
294 return(0);
295
296
297 buff = (unsigned char *)(iph + 1);
298
299
300 while (!done && len < iph->ihl*4) switch(*buff) {
301 case IPOPT_END:
302 done = 1;
303 break;
304 case IPOPT_NOOP:
305 buff++;
306 len++;
307 break;
308 case IPOPT_SEC:
309 buff++;
310 if (*buff != 11) return(1);
311 buff++;
312 opt->security = ntohs(*(unsigned short *)buff);
313 buff += 2;
314 opt->compartment = ntohs(*(unsigned short *)buff);
315 buff += 2;
316 opt->handling = ntohs(*(unsigned short *)buff);
317 buff += 2;
318 opt->tcc = ((*buff) << 16) + ntohs(*(unsigned short *)(buff+1));
319 buff += 3;
320 len += 11;
321 break;
322 case IPOPT_LSRR:
323 buff++;
324 if ((*buff - 3)% 4 != 0) return(1);
325 len += *buff;
326 opt->loose_route.route_size = (*buff -3)/4;
327 buff++;
328 if (*buff % 4 != 0) return(1);
329 opt->loose_route.pointer = *buff/4 - 1;
330 buff++;
331 buff++;
332 for (i = 0; i < opt->loose_route.route_size; i++) {
333 if(i>=MAX_ROUTE)
334 return(1);
335 opt->loose_route.route[i] = *(unsigned long *)buff;
336 buff += 4;
337 }
338 break;
339 case IPOPT_SSRR:
340 buff++;
341 if ((*buff - 3)% 4 != 0) return(1);
342 len += *buff;
343 opt->strict_route.route_size = (*buff -3)/4;
344 buff++;
345 if (*buff % 4 != 0) return(1);
346 opt->strict_route.pointer = *buff/4 - 1;
347 buff++;
348 buff++;
349 for (i = 0; i < opt->strict_route.route_size; i++) {
350 if(i>=MAX_ROUTE)
351 return(1);
352 opt->strict_route.route[i] = *(unsigned long *)buff;
353 buff += 4;
354 }
355 break;
356 case IPOPT_RR:
357 buff++;
358 if ((*buff - 3)% 4 != 0) return(1);
359 len += *buff;
360 opt->record_route.route_size = (*buff -3)/4;
361 buff++;
362 if (*buff % 4 != 0) return(1);
363 opt->record_route.pointer = *buff/4 - 1;
364 buff++;
365 buff++;
366 for (i = 0; i < opt->record_route.route_size; i++) {
367 if(i>=MAX_ROUTE)
368 return 1;
369 opt->record_route.route[i] = *(unsigned long *)buff;
370 buff += 4;
371 }
372 break;
373 case IPOPT_SID:
374 len += 4;
375 buff +=2;
376 opt->stream = *(unsigned short *)buff;
377 buff += 2;
378 break;
379 case IPOPT_TIMESTAMP:
380 buff++;
381 len += *buff;
382 if (*buff % 4 != 0) return(1);
383 opt->tstamp.len = *buff / 4 - 1;
384 buff++;
385 if ((*buff - 1) % 4 != 0) return(1);
386 opt->tstamp.ptr = (*buff-1)/4;
387 buff++;
388 opt->tstamp.x.full_char = *buff;
389 buff++;
390 for (i = 0; i < opt->tstamp.len; i++) {
391 opt->tstamp.data[i] = *(unsigned long *)buff;
392 buff += 4;
393 }
394 break;
395 default:
396 return(1);
397 }
398
399 if (opt->record_route.route_size == 0) {
400 if (opt->strict_route.route_size != 0) {
401 memcpy(&(opt->record_route), &(opt->strict_route),
402 sizeof(opt->record_route));
403 } else if (opt->loose_route.route_size != 0) {
404 memcpy(&(opt->record_route), &(opt->loose_route),
405 sizeof(opt->record_route));
406 }
407 }
408
409 if (opt->strict_route.route_size != 0 &&
410 opt->strict_route.route_size != opt->strict_route.pointer) {
411 strict_route(iph, opt);
412 return(0);
413 }
414
415 if (opt->loose_route.route_size != 0 &&
416 opt->loose_route.route_size != opt->loose_route.pointer) {
417 loose_route(iph, opt);
418 return(0);
419 }
420
421 return(0);
422 }
423
424
425
426 static inline unsigned short
427 ip_fast_csum(unsigned char * buff, int wlen)
428 {
429 unsigned long sum = 0;
430
431 if (wlen) {
432 unsigned long bogus;
433 __asm__("clc\n"
434 "1:\t"
435 "lodsl\n\t"
436 "adcl %3, %0\n\t"
437 "decl %2\n\t"
438 "jne 1b\n\t"
439 "adcl $0, %0\n\t"
440 "movl %0, %3\n\t"
441 "shrl $16, %3\n\t"
442 "addw %w3, %w0\n\t"
443 "adcw $0, %w0"
444 : "=r" (sum), "=S" (buff), "=r" (wlen), "=a" (bogus)
445 : "0" (sum), "1" (buff), "2" (wlen));
446 }
447 return (~sum) & 0xffff;
448 }
449
450
451
452
453
454 unsigned short
455 ip_compute_csum(unsigned char * buff, int len)
456 {
457 unsigned long sum = 0;
458
459
460 if (len > 3) {
461 __asm__("clc\n"
462 "1:\t"
463 "lodsl\n\t"
464 "adcl %%eax, %%ebx\n\t"
465 "loop 1b\n\t"
466 "adcl $0, %%ebx\n\t"
467 "movl %%ebx, %%eax\n\t"
468 "shrl $16, %%eax\n\t"
469 "addw %%ax, %%bx\n\t"
470 "adcw $0, %%bx"
471 : "=b" (sum) , "=S" (buff)
472 : "0" (sum), "c" (len >> 2) ,"1" (buff)
473 : "ax", "cx", "si", "bx" );
474 }
475 if (len & 2) {
476 __asm__("lodsw\n\t"
477 "addw %%ax, %%bx\n\t"
478 "adcw $0, %%bx"
479 : "=b" (sum), "=S" (buff)
480 : "0" (sum), "1" (buff)
481 : "bx", "ax", "si");
482 }
483 if (len & 1) {
484 __asm__("lodsb\n\t"
485 "movb $0, %%ah\n\t"
486 "addw %%ax, %%bx\n\t"
487 "adcw $0, %%bx"
488 : "=b" (sum), "=S" (buff)
489 : "0" (sum), "1" (buff)
490 : "bx", "ax", "si");
491 }
492 sum =~sum;
493 return(sum & 0xffff);
494 }
495
496
497 int
498 ip_csum(struct iphdr *iph)
499 {
500 return ip_fast_csum((unsigned char *)iph, iph->ihl);
501 }
502
503
504 static void
505 ip_send_check(struct iphdr *iph)
506 {
507 iph->check = 0;
508 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
509 }
510
511
512
513 static struct ipq *ipqueue = NULL;
514
515 static struct ipfrag *ip_frag_create(int offset, int end, struct sk_buff *skb, unsigned char *ptr)
516 {
517 struct ipfrag *fp;
518
519 fp = (struct ipfrag *) kmalloc(sizeof(struct ipfrag), GFP_ATOMIC);
520 if (fp == NULL)
521 {
522 printk("IP: frag_create: no memory left !\n");
523 return(NULL);
524 }
525 memset(fp, 0, sizeof(struct ipfrag));
526
527
528 fp->offset = offset;
529 fp->end = end;
530 fp->len = end - offset;
531 fp->skb = skb;
532 fp->ptr = ptr;
533
534 return(fp);
535 }
536
537
538
539
540
541
542 static struct ipq *ip_find(struct iphdr *iph)
543 {
544 struct ipq *qp;
545 struct ipq *qplast;
546
547 cli();
548 qplast = NULL;
549 for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next)
550 {
551 if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
552 iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol)
553 {
554 del_timer(&qp->timer);
555 sti();
556 return(qp);
557 }
558 }
559 sti();
560 return(NULL);
561 }
562
563
564
565
566
567
568
569
570 static void ip_free(struct ipq *qp)
571 {
572 struct ipfrag *fp;
573 struct ipfrag *xp;
574
575
576
577 del_timer(&qp->timer);
578
579
580 cli();
581 if (qp->prev == NULL)
582 {
583 ipqueue = qp->next;
584 if (ipqueue != NULL)
585 ipqueue->prev = NULL;
586 }
587 else
588 {
589 qp->prev->next = qp->next;
590 if (qp->next != NULL)
591 qp->next->prev = qp->prev;
592 }
593
594
595
596 fp = qp->fragments;
597 while (fp != NULL)
598 {
599 xp = fp->next;
600 IS_SKB(fp->skb);
601 kfree_skb(fp->skb,FREE_READ);
602 kfree_s(fp, sizeof(struct ipfrag));
603 fp = xp;
604 }
605
606
607
608
609 kfree_s(qp->mac, qp->maclen);
610
611
612 kfree_s(qp->iph, qp->ihlen + 8);
613
614
615 kfree_s(qp, sizeof(struct ipq));
616
617 sti();
618 }
619
620
621
622
623 static void ip_expire(unsigned long arg)
624 {
625 struct ipq *qp;
626
627 qp = (struct ipq *)arg;
628 DPRINTF((DBG_IP, "IP: queue_expire: fragment queue 0x%X timed out!\n", qp));
629
630
631 #if 0
632 icmp_send(qp->iph->ip_src.s_addr, ICMP_TIME_EXCEEDED,
633 ICMP_EXC_FRAGTIME, qp->iph);
634 #endif
635 if(qp->fragments!=NULL)
636 icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
637 ICMP_EXC_FRAGTIME, qp->dev);
638
639
640 ip_free(qp);
641 }
642
643
644
645
646
647
648
649
650
651 static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev)
652 {
653 struct ipq *qp;
654 int maclen;
655 int ihlen;
656
657 qp = (struct ipq *) kmalloc(sizeof(struct ipq), GFP_ATOMIC);
658 if (qp == NULL)
659 {
660 printk("IP: create: no memory left !\n");
661 return(NULL);
662 }
663 memset(qp, 0, sizeof(struct ipq));
664
665
666 maclen = ((unsigned long) iph) - ((unsigned long) (skb + 1));
667 qp->mac = (unsigned char *) kmalloc(maclen, GFP_ATOMIC);
668 if (qp->mac == NULL)
669 {
670 printk("IP: create: no memory left !\n");
671 kfree_s(qp, sizeof(struct ipq));
672 return(NULL);
673 }
674
675
676 ihlen = (iph->ihl * sizeof(unsigned long));
677 qp->iph = (struct iphdr *) kmalloc(ihlen + 8, GFP_ATOMIC);
678 if (qp->iph == NULL)
679 {
680 printk("IP: create: no memory left !\n");
681 kfree_s(qp->mac, maclen);
682 kfree_s(qp, sizeof(struct ipq));
683 return(NULL);
684 }
685
686
687 memcpy(qp->mac, (skb + 1), maclen);
688 memcpy(qp->iph, iph, ihlen + 8);
689 qp->len = 0;
690 qp->ihlen = ihlen;
691 qp->maclen = maclen;
692 qp->fragments = NULL;
693 qp->dev = dev;
694
695
696
697 qp->timer.expires = IP_FRAG_TIME;
698 qp->timer.data = (unsigned long) qp;
699 qp->timer.function = ip_expire;
700 add_timer(&qp->timer);
701
702
703 qp->prev = NULL;
704 cli();
705 qp->next = ipqueue;
706 if (qp->next != NULL)
707 qp->next->prev = qp;
708 ipqueue = qp;
709 sti();
710 return(qp);
711 }
712
713
714
715 static int ip_done(struct ipq *qp)
716 {
717 struct ipfrag *fp;
718 int offset;
719
720
721 if (qp->len == 0)
722 return(0);
723
724
725 fp = qp->fragments;
726 offset = 0;
727 while (fp != NULL)
728 {
729 if (fp->offset > offset)
730 return(0);
731 offset = fp->end;
732 fp = fp->next;
733 }
734
735
736 return(1);
737 }
738
739
740
741 static struct sk_buff *ip_glue(struct ipq *qp)
742 {
743 struct sk_buff *skb;
744 struct iphdr *iph;
745 struct ipfrag *fp;
746 unsigned char *ptr;
747 int count, len;
748
749
750 len = sizeof(struct sk_buff)+qp->maclen + qp->ihlen + qp->len;
751 if ((skb = alloc_skb(len,GFP_ATOMIC)) == NULL)
752 {
753 printk("IP: queue_glue: no memory for glueing queue 0x%X\n", (int) qp);
754 ip_free(qp);
755 return(NULL);
756 }
757
758
759 skb->len = (len - qp->maclen);
760 skb->h.raw = (unsigned char *) (skb + 1);
761 skb->free = 1;
762 skb->lock = 1;
763
764
765 ptr = (unsigned char *) skb->h.raw;
766 memcpy(ptr, ((unsigned char *) qp->mac), qp->maclen);
767
768 ptr += qp->maclen;
769 memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen);
770
771 ptr += qp->ihlen;
772 skb->h.raw += qp->maclen;
773
774
775 count = 0;
776
777
778 fp = qp->fragments;
779 while(fp != NULL)
780 {
781 if(count+fp->len>skb->len)
782 {
783 printk("Invalid fragment list: Fragment over size.\n");
784 kfree_skb(skb,FREE_WRITE);
785 return NULL;
786 }
787
788 memcpy((ptr + fp->offset), fp->ptr, fp->len);
789 count += fp->len;
790 fp = fp->next;
791 }
792
793
794 ip_free(qp);
795
796
797 iph = skb->h.iph;
798 iph->frag_off = 0;
799 iph->tot_len = htons((iph->ihl * sizeof(unsigned long)) + count);
800 return(skb);
801 }
802
803
804
805 static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
806 {
807 struct ipfrag *prev, *next;
808 struct ipfrag *tfp;
809 struct ipq *qp;
810 struct sk_buff *skb2;
811 unsigned char *ptr;
812 int flags, offset;
813 int i, ihl, end;
814
815
816 qp = ip_find(iph);
817
818
819 offset = ntohs(iph->frag_off);
820 flags = offset & ~IP_OFFSET;
821 offset &= IP_OFFSET;
822 if (((flags & IP_MF) == 0) && (offset == 0))
823 {
824 if (qp != NULL)
825 ip_free(qp);
826 return(skb);
827 }
828 offset <<= 3;
829
830
831
832
833
834
835 if (qp != NULL)
836 {
837 del_timer(&qp->timer);
838 qp->timer.expires = IP_FRAG_TIME;
839 qp->timer.data = (unsigned long) qp;
840 qp->timer.function = ip_expire;
841 add_timer(&qp->timer);
842 }
843 else
844 {
845 if ((qp = ip_create(skb, iph, dev)) == NULL)
846 return(NULL);
847 }
848
849
850 ihl = (iph->ihl * sizeof(unsigned long));
851 end = offset + ntohs(iph->tot_len) - ihl;
852
853
854 ptr = ((unsigned char *) (skb + 1)) + dev->hard_header_len + ihl;
855
856
857 if ((flags & IP_MF) == 0)
858 qp->len = end;
859
860
861
862
863
864
865 prev = NULL;
866 for(next = qp->fragments; next != NULL; next = next->next)
867 {
868 if (next->offset > offset)
869 break;
870 prev = next;
871 }
872
873
874
875
876
877
878 if (prev != NULL && offset < prev->end)
879 {
880 i = prev->end - offset;
881 offset += i;
882 ptr += i;
883 DPRINTF((DBG_IP, "IP: defrag: fixed low overlap %d bytes\n", i));
884 }
885
886
887
888
889
890
891 for(; next != NULL; next = tfp)
892 {
893 tfp = next->next;
894 if (next->offset >= end)
895 break;
896
897 i = end - next->offset;
898 next->len -= i;
899 next->offset += i;
900 next->ptr += i;
901
902
903 if (next->len <= 0)
904 {
905 DPRINTF((DBG_IP, "IP: defrag: removing frag 0x%X (len %d)\n",
906 next, next->len));
907 if (next->prev != NULL)
908 next->prev->next = next->next;
909 else
910 qp->fragments = next->next;
911
912 if (tfp->next != NULL)
913 next->next->prev = next->prev;
914
915 kfree_s(next, sizeof(struct ipfrag));
916 }
917 DPRINTF((DBG_IP, "IP: defrag: fixed high overlap %d bytes\n", i));
918 }
919
920
921 tfp = NULL;
922 tfp = ip_frag_create(offset, end, skb, ptr);
923 tfp->prev = prev;
924 tfp->next = next;
925 if (prev != NULL)
926 prev->next = tfp;
927 else
928 qp->fragments = tfp;
929
930 if (next != NULL)
931 next->prev = tfp;
932
933
934
935
936
937
938
939 if (ip_done(qp))
940 {
941 skb2 = ip_glue(qp);
942 return(skb2);
943 }
944 return(NULL);
945 }
946
947
948
949
950
951
952
953
954
955
956 void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
957 {
958 struct iphdr *iph;
959 unsigned char *raw;
960 unsigned char *ptr;
961 struct sk_buff *skb2;
962 int left, mtu, hlen, len;
963 int offset;
964
965
966 raw = (unsigned char *) (skb + 1);
967 iph = (struct iphdr *) (raw + dev->hard_header_len);
968
969
970 hlen = (iph->ihl * sizeof(unsigned long));
971 left = ntohs(iph->tot_len) - hlen;
972 hlen += dev->hard_header_len;
973 mtu = (dev->mtu - hlen);
974 ptr = (raw + hlen);
975
976 DPRINTF((DBG_IP, "IP: Fragmentation Desired\n"));
977 DPRINTF((DBG_IP, " DEV=%s, MTU=%d, LEN=%d SRC=%s",
978 dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
979 DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
980
981
982 if (ntohs(iph->frag_off) & IP_DF)
983 {
984 DPRINTF((DBG_IP, "IP: Fragmentation Desired, but DF set !\n"));
985 DPRINTF((DBG_IP, " DEV=%s, MTU=%d, LEN=%d SRC=%s",
986 dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
987 DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
988
989
990
991
992
993
994 icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev);
995 return;
996 }
997
998
999 if (is_frag & 2)
1000 offset = (ntohs(iph->frag_off) & 0x1fff) << 3;
1001 else
1002 offset = 0;
1003 while(left > 0)
1004 {
1005 len = left;
1006 if (len+8 > mtu)
1007 len = (dev->mtu - hlen - 8);
1008 if ((left - len) >= 8)
1009 {
1010 len /= 8;
1011 len *= 8;
1012 }
1013 DPRINTF((DBG_IP,"IP: frag: creating fragment of %d bytes (%d total)\n",
1014 len, len + hlen));
1015
1016
1017 if ((skb2 = alloc_skb(sizeof(struct sk_buff) + len + hlen,GFP_KERNEL)) == NULL)
1018 {
1019 printk("IP: frag: no memory for new fragment!\n");
1020 return;
1021 }
1022 skb2->arp = skb->arp;
1023 skb2->free = skb->free;
1024 skb2->len = len + hlen;
1025 skb2->h.raw=(char *)(skb2+1);
1026
1027 if (sk)
1028 sk->wmem_alloc += skb2->mem_len;
1029
1030
1031 memcpy(skb2->h.raw, raw, hlen);
1032
1033
1034 memcpy(skb2->h.raw + hlen, ptr, len);
1035 left -= len;
1036
1037 skb2->h.raw+=dev->hard_header_len;
1038
1039 iph = (struct iphdr *)(skb2->h.raw);
1040 iph->frag_off = htons((offset >> 3));
1041
1042
1043 if (left > 0 || (is_frag & 1))
1044 iph->frag_off |= htons(IP_MF);
1045 ptr += len;
1046 offset += len;
1047
1048
1049
1050 ip_queue_xmit(sk, dev, skb2, 1);
1051
1052 }
1053 }
1054
1055
1056
1057 #ifdef CONFIG_IP_FORWARD
1058
1059
1060 static void
1061 ip_forward(struct sk_buff *skb, struct device *dev, int is_frag)
1062 {
1063 struct device *dev2;
1064 struct iphdr *iph;
1065 struct sk_buff *skb2;
1066 struct rtable *rt;
1067 unsigned char *ptr;
1068 unsigned long raddr;
1069
1070
1071
1072
1073
1074
1075 if(dev->flags&IFF_PROMISC)
1076 {
1077 if(memcmp((char *)&skb[1],dev->dev_addr,dev->addr_len))
1078 return;
1079 }
1080
1081
1082
1083
1084
1085
1086 iph = skb->h.iph;
1087 iph->ttl--;
1088 if (iph->ttl <= 0) {
1089 DPRINTF((DBG_IP, "\nIP: *** datagram expired: TTL=0 (ignored) ***\n"));
1090 DPRINTF((DBG_IP, " SRC = %s ", in_ntoa(iph->saddr)));
1091 DPRINTF((DBG_IP, " DST = %s (ignored)\n", in_ntoa(iph->daddr)));
1092
1093
1094 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, dev);
1095 return;
1096 }
1097
1098
1099 ip_send_check(iph);
1100
1101
1102
1103
1104
1105 rt = rt_route(iph->daddr, NULL);
1106 if (rt == NULL) {
1107 DPRINTF((DBG_IP, "\nIP: *** routing (phase I) failed ***\n"));
1108
1109
1110 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, dev);
1111 return;
1112 }
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122 raddr = rt->rt_gateway;
1123 if (raddr != 0) {
1124 rt = rt_route(raddr, NULL);
1125 if (rt == NULL) {
1126 DPRINTF((DBG_IP, "\nIP: *** routing (phase II) failed ***\n"));
1127
1128
1129 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, dev);
1130 return;
1131 }
1132 if (rt->rt_gateway != 0) raddr = rt->rt_gateway;
1133 } else raddr = iph->daddr;
1134 dev2 = rt->rt_dev;
1135
1136
1137 if (dev == dev2)
1138 return;
1139
1140
1141
1142
1143 DPRINTF((DBG_IP, "\nIP: *** fwd %s -> ", in_ntoa(iph->saddr)));
1144 DPRINTF((DBG_IP, "%s (via %s), LEN=%d\n",
1145 in_ntoa(raddr), dev2->name, skb->len));
1146
1147 if (dev2->flags & IFF_UP) {
1148 skb2 = (struct sk_buff *) alloc_skb(sizeof(struct sk_buff) +
1149 dev2->hard_header_len + skb->len, GFP_ATOMIC);
1150 if (skb2 == NULL) {
1151 printk("\nIP: No memory available for IP forward\n");
1152 return;
1153 }
1154 ptr = (unsigned char *)(skb2 + 1);
1155 skb2->sk = NULL;
1156 skb2->free = 1;
1157 skb2->len = skb->len + dev2->hard_header_len;
1158 skb2->mem_addr = skb2;
1159 skb2->mem_len = sizeof(struct sk_buff) + skb2->len;
1160 skb2->next = NULL;
1161 skb2->h.raw = ptr;
1162
1163
1164 memcpy(ptr + dev2->hard_header_len, skb->h.raw, skb->len);
1165
1166
1167 (void) ip_send(skb2, raddr, skb->len, dev2, dev2->pa_addr);
1168
1169 if(skb2->len > dev2->mtu)
1170 {
1171 ip_fragment(NULL,skb2,dev2, is_frag);
1172 kfree_skb(skb2,FREE_WRITE);
1173 }
1174 else
1175 dev2->queue_xmit(skb2, dev2, SOPRI_NORMAL);
1176 }
1177 }
1178
1179
1180 #endif
1181
1182
1183 int
1184 ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
1185 {
1186 struct iphdr *iph = skb->h.iph;
1187 unsigned char hash;
1188 unsigned char flag = 0;
1189 unsigned char opts_p = 0;
1190 struct inet_protocol *ipprot;
1191 static struct options opt;
1192
1193 int brd;
1194 int is_frag=0;
1195
1196 DPRINTF((DBG_IP, "<<\n"));
1197
1198
1199 if (skb->len<sizeof(struct iphdr) || iph->ihl<5 || iph->version != 4 || ip_fast_csum((unsigned char *)iph, iph->ihl) !=0) {
1200 DPRINTF((DBG_IP, "\nIP: *** datagram error ***\n"));
1201 DPRINTF((DBG_IP, " SRC = %s ", in_ntoa(iph->saddr)));
1202 DPRINTF((DBG_IP, " DST = %s (ignored)\n", in_ntoa(iph->daddr)));
1203 skb->sk = NULL;
1204 kfree_skb(skb, FREE_WRITE);
1205 return(0);
1206 }
1207
1208 if (iph->ihl != 5) {
1209 ip_print(iph);
1210 memset((char *) &opt, 0, sizeof(opt));
1211 if (do_options(iph, &opt) != 0)
1212 return 0;
1213 opts_p = 1;
1214 }
1215
1216 if (iph->frag_off & 0x0020)
1217 is_frag|=1;
1218 if (ntohs(iph->frag_off) & 0x1fff)
1219 is_frag|=2;
1220
1221
1222 if ((brd = chk_addr(iph->daddr)) == 0) {
1223 #ifdef CONFIG_IP_FORWARD
1224 ip_forward(skb, dev, is_frag);
1225 #else
1226 printk("Machine %x tried to use us as a forwarder to %x but we have forwarding disabled!\n",
1227 iph->saddr,iph->daddr);
1228 #endif
1229 skb->sk = NULL;
1230 kfree_skb(skb, FREE_WRITE);
1231 return(0);
1232 }
1233
1234
1235
1236
1237
1238 if(is_frag)
1239 {
1240 #ifdef CONFIG_IP_DEFRAG
1241 skb=ip_defrag(iph,skb,dev);
1242 if(skb==NULL)
1243 {
1244 return 0;
1245 }
1246 iph=skb->h.iph;
1247 #else
1248 printk("\nIP: *** datagram fragmentation not yet implemented ***\n");
1249 printk(" SRC = %s ", in_ntoa(iph->saddr));
1250 printk(" DST = %s (ignored)\n", in_ntoa(iph->daddr));
1251 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1252 skb->sk = NULL;
1253 kfree_skb(skb, FREE_WRITE);
1254 return(0);
1255 #endif
1256 }
1257
1258
1259
1260 if(brd==IS_INVBCAST)
1261 {
1262
1263
1264 skb->sk=NULL;
1265 kfree_skb(skb,FREE_WRITE);
1266 return(0);
1267 }
1268
1269
1270 skb->h.raw += iph->ihl*4;
1271 hash = iph->protocol & (MAX_INET_PROTOS -1);
1272 for (ipprot = (struct inet_protocol *)inet_protos[hash];
1273 ipprot != NULL;
1274 ipprot=(struct inet_protocol *)ipprot->next)
1275 {
1276 struct sk_buff *skb2;
1277
1278 if (ipprot->protocol != iph->protocol) continue;
1279 DPRINTF((DBG_IP, "Using protocol = %X:\n", ipprot));
1280 print_ipprot(ipprot);
1281
1282
1283
1284
1285
1286
1287 if (ipprot->copy) {
1288 skb2 = alloc_skb(skb->mem_len, GFP_ATOMIC);
1289 if (skb2 == NULL)
1290 continue;
1291 memcpy(skb2, skb, skb->mem_len);
1292 skb2->mem_addr = skb2;
1293 skb2->h.raw = (unsigned char *)(
1294 (unsigned long)skb2 +
1295 (unsigned long) skb->h.raw -
1296 (unsigned long)skb);
1297 skb2->free=1;
1298 } else {
1299 skb2 = skb;
1300 }
1301 flag = 1;
1302
1303
1304
1305
1306
1307
1308 ipprot->handler(skb2, dev, opts_p ? &opt : 0, iph->daddr,
1309 (ntohs(iph->tot_len) - (iph->ihl * 4)),
1310 iph->saddr, 0, ipprot);
1311
1312 }
1313
1314
1315
1316
1317
1318
1319
1320 if (!flag) {
1321 if (brd != IS_BROADCAST)
1322 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1323 skb->sk = NULL;
1324 kfree_skb(skb, FREE_WRITE);
1325 }
1326
1327 return(0);
1328 }
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338 void
1339 ip_queue_xmit(struct sock *sk, struct device *dev,
1340 struct sk_buff *skb, int free)
1341 {
1342 struct iphdr *iph;
1343 unsigned char *ptr;
1344
1345 if (sk == NULL) free = 1;
1346 if (dev == NULL) {
1347 printk("IP: ip_queue_xmit dev = NULL\n");
1348 return;
1349 }
1350 IS_SKB(skb);
1351 skb->free = free;
1352 skb->dev = dev;
1353 skb->when = jiffies;
1354
1355 DPRINTF((DBG_IP, ">>\n"));
1356 ptr = (unsigned char *)(skb + 1);
1357 ptr += dev->hard_header_len;
1358 iph = (struct iphdr *)ptr;
1359 iph->tot_len = ntohs(skb->len-dev->hard_header_len);
1360
1361 if(skb->len > dev->mtu)
1362 {
1363
1364 ip_fragment(sk,skb,dev,0);
1365 IS_SKB(skb);
1366 kfree_skb(skb,FREE_WRITE);
1367 return;
1368 }
1369
1370 ip_send_check(iph);
1371 ip_print(iph);
1372 skb->next = NULL;
1373
1374
1375 skb->magic = 1;
1376 if (!free) {
1377 skb->link3 = NULL;
1378 sk->packets_out++;
1379 cli();
1380 if (sk->send_head == NULL) {
1381 sk->send_tail = skb;
1382 sk->send_head = skb;
1383 } else {
1384
1385 if (sk->send_tail == NULL) {
1386 printk("IP: ***bug sk->send_tail == NULL != sk->send_head\n");
1387 sort_send(sk);
1388 } else {
1389 sk->send_tail->link3 = skb;
1390 sk->send_tail = skb;
1391 }
1392 }
1393 sti();
1394 reset_timer(sk, TIME_WRITE,
1395 backoff(sk->backoff) * (2 * sk->mdev + sk->rtt));
1396 } else {
1397 skb->sk = sk;
1398 }
1399
1400
1401 if (dev->flags & IFF_UP) {
1402 if (sk != NULL) {
1403 dev->queue_xmit(skb, dev, sk->priority);
1404 }
1405 else {
1406 dev->queue_xmit(skb, dev, SOPRI_NORMAL);
1407 }
1408 } else {
1409 if (free) kfree_skb(skb, FREE_WRITE);
1410 }
1411 }
1412
1413
1414 void
1415 ip_retransmit(struct sock *sk, int all)
1416 {
1417 struct sk_buff * skb;
1418 struct proto *prot;
1419 struct device *dev;
1420
1421 prot = sk->prot;
1422 skb = sk->send_head;
1423 while (skb != NULL) {
1424 dev = skb->dev;
1425
1426 if(dev==NULL)
1427 {
1428 printk("ip_retransmit: NULL device bug!\n");
1429 goto oops;
1430 }
1431
1432 IS_SKB(skb);
1433
1434
1435
1436
1437
1438
1439 cli();
1440
1441
1442 if (!skb->arp) {
1443 if (dev->rebuild_header(skb+1, dev)) {
1444 sti();
1445 if (!all) break;
1446 skb = (struct sk_buff *)skb->link3;
1447 continue;
1448 }
1449 }
1450 skb->arp = 1;
1451 sti();
1452 skb->when = jiffies;
1453
1454
1455 if (dev->flags & IFF_UP) {
1456 if (sk) dev->queue_xmit(skb, dev, sk->priority);
1457
1458 }
1459
1460 oops: sk->retransmits++;
1461 sk->prot->retransmits ++;
1462 if (!all) break;
1463
1464
1465 if (sk->retransmits > sk->cong_window) break;
1466 skb = (struct sk_buff *)skb->link3;
1467 }
1468
1469
1470
1471
1472
1473
1474
1475 sk->backoff++;
1476 reset_timer(sk, TIME_WRITE, backoff(sk->backoff) * (2 * sk->mdev + sk->rtt));
1477 }
1478
1479
1480 int backoff(int n)
1481 {
1482
1483
1484
1485
1486
1487 if(n<0)
1488 {
1489 printk("Backoff < 0!\n");
1490 return 16;
1491 }
1492
1493 if(n <= 4)
1494 return 1 << n;
1495 else
1496 {
1497 if(n<255)
1498 return n * n;
1499 else
1500 {
1501 printk("Overloaded backoff!\n");
1502 return 255*255;
1503 }
1504 }
1505 }
1506