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