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 __asm__("clc\n"
433 "1:\t"
434 "lodsl\n\t"
435 "adcl %%eax, %0\n\t"
436 "decl %2\n\t"
437 "jne 1b\n\t"
438 "adcl $0, %0\n\t"
439 "movl %0, %%eax\n\t"
440 "shrl $16, %%eax\n\t"
441 "addw %%ax, %w0\n\t"
442 "adcw $0, %w0"
443 : "=r" (sum), "=S" (buff), "=r" (wlen)
444 : "0" (sum), "1" (buff), "2" (wlen)
445 : "ax" );
446 return (~sum) & 0xffff;
447 }
448
449
450
451
452
453 unsigned short
454 ip_compute_csum(unsigned char * buff, int len)
455 {
456 unsigned long sum = 0;
457
458
459 if (len > 3) {
460 __asm__("clc\n"
461 "1:\t"
462 "lodsl\n\t"
463 "adcl %%eax, %%ebx\n\t"
464 "loop 1b\n\t"
465 "adcl $0, %%ebx\n\t"
466 "movl %%ebx, %%eax\n\t"
467 "shrl $16, %%eax\n\t"
468 "addw %%ax, %%bx\n\t"
469 "adcw $0, %%bx"
470 : "=b" (sum) , "=S" (buff)
471 : "0" (sum), "c" (len >> 2) ,"1" (buff)
472 : "ax", "cx", "si", "bx" );
473 }
474 if (len & 2) {
475 __asm__("lodsw\n\t"
476 "addw %%ax, %%bx\n\t"
477 "adcw $0, %%bx"
478 : "=b" (sum), "=S" (buff)
479 : "0" (sum), "1" (buff)
480 : "bx", "ax", "si");
481 }
482 if (len & 1) {
483 __asm__("lodsb\n\t"
484 "movb $0, %%ah\n\t"
485 "addw %%ax, %%bx\n\t"
486 "adcw $0, %%bx"
487 : "=b" (sum), "=S" (buff)
488 : "0" (sum), "1" (buff)
489 : "bx", "ax", "si");
490 }
491 sum =~sum;
492 return(sum & 0xffff);
493 }
494
495
496 int
497 ip_csum(struct iphdr *iph)
498 {
499 return ip_fast_csum((unsigned char *)iph, iph->ihl);
500 }
501
502
503 static void
504 ip_send_check(struct iphdr *iph)
505 {
506 iph->check = 0;
507 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
508 }
509
510
511
512 static struct ipq *ipqueue = NULL;
513
514 static struct ipfrag *ip_frag_create(int offset, int end, struct sk_buff *skb, unsigned char *ptr)
515 {
516 struct ipfrag *fp;
517
518 fp = (struct ipfrag *) kmalloc(sizeof(struct ipfrag), GFP_ATOMIC);
519 if (fp == NULL)
520 {
521 printk("IP: frag_create: no memory left !\n");
522 return(NULL);
523 }
524 memset(fp, 0, sizeof(struct ipfrag));
525
526
527 fp->offset = offset;
528 fp->end = end;
529 fp->len = end - offset;
530 fp->skb = skb;
531 fp->ptr = ptr;
532
533 return(fp);
534 }
535
536
537
538
539
540
541 static struct ipq *ip_find(struct iphdr *iph)
542 {
543 struct ipq *qp;
544 struct ipq *qplast;
545
546 cli();
547 qplast = NULL;
548 for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next)
549 {
550 if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
551 iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol)
552 {
553 del_timer(&qp->timer);
554 sti();
555 return(qp);
556 }
557 }
558 sti();
559 return(NULL);
560 }
561
562
563
564
565
566
567
568
569 static void ip_free(struct ipq *qp)
570 {
571 struct ipfrag *fp;
572 struct ipfrag *xp;
573
574
575
576 del_timer(&qp->timer);
577
578
579 cli();
580 if (qp->prev == NULL)
581 {
582 ipqueue = qp->next;
583 if (ipqueue != NULL)
584 ipqueue->prev = NULL;
585 }
586 else
587 {
588 qp->prev->next = qp->next;
589 if (qp->next != NULL)
590 qp->next->prev = qp->prev;
591 }
592
593
594
595 fp = qp->fragments;
596 while (fp != NULL)
597 {
598 xp = fp->next;
599 IS_SKB(fp->skb);
600 kfree_skb(fp->skb,FREE_READ);
601 kfree_s(fp, sizeof(struct ipfrag));
602 fp = xp;
603 }
604
605
606
607
608 kfree_s(qp->mac, qp->maclen);
609
610
611 kfree_s(qp->iph, qp->ihlen + 8);
612
613
614 kfree_s(qp, sizeof(struct ipq));
615
616 sti();
617 }
618
619
620
621
622 static void ip_expire(unsigned long arg)
623 {
624 struct ipq *qp;
625
626 qp = (struct ipq *)arg;
627 DPRINTF((DBG_IP, "IP: queue_expire: fragment queue 0x%X timed out!\n", qp));
628
629
630 #if 0
631 icmp_send(qp->iph->ip_src.s_addr, ICMP_TIME_EXCEEDED,
632 ICMP_EXC_FRAGTIME, qp->iph);
633 #endif
634 if(qp->fragments!=NULL)
635 icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
636 ICMP_EXC_FRAGTIME, qp->dev);
637
638
639 ip_free(qp);
640 }
641
642
643
644
645
646
647
648
649
650 static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev)
651 {
652 struct ipq *qp;
653 int maclen;
654 int ihlen;
655
656 qp = (struct ipq *) kmalloc(sizeof(struct ipq), GFP_ATOMIC);
657 if (qp == NULL)
658 {
659 printk("IP: create: no memory left !\n");
660 return(NULL);
661 }
662 memset(qp, 0, sizeof(struct ipq));
663
664
665 maclen = ((unsigned long) iph) - ((unsigned long) (skb + 1));
666 qp->mac = (unsigned char *) kmalloc(maclen, GFP_ATOMIC);
667 if (qp->mac == NULL)
668 {
669 printk("IP: create: no memory left !\n");
670 kfree_s(qp, sizeof(struct ipq));
671 return(NULL);
672 }
673
674
675 ihlen = (iph->ihl * sizeof(unsigned long));
676 qp->iph = (struct iphdr *) kmalloc(ihlen + 8, GFP_ATOMIC);
677 if (qp->iph == NULL)
678 {
679 printk("IP: create: no memory left !\n");
680 kfree_s(qp->mac, maclen);
681 kfree_s(qp, sizeof(struct ipq));
682 return(NULL);
683 }
684
685
686 memcpy(qp->mac, (skb + 1), maclen);
687 memcpy(qp->iph, iph, ihlen + 8);
688 qp->len = 0;
689 qp->ihlen = ihlen;
690 qp->maclen = maclen;
691 qp->fragments = NULL;
692 qp->dev = dev;
693
694
695
696 qp->timer.expires = IP_FRAG_TIME;
697 qp->timer.data = (unsigned long) qp;
698 qp->timer.function = ip_expire;
699 add_timer(&qp->timer);
700
701
702 qp->prev = NULL;
703 cli();
704 qp->next = ipqueue;
705 if (qp->next != NULL)
706 qp->next->prev = qp;
707 ipqueue = qp;
708 sti();
709 return(qp);
710 }
711
712
713
714 static int ip_done(struct ipq *qp)
715 {
716 struct ipfrag *fp;
717 int offset;
718
719
720 if (qp->len == 0)
721 return(0);
722
723
724 fp = qp->fragments;
725 offset = 0;
726 while (fp != NULL)
727 {
728 if (fp->offset > offset)
729 return(0);
730 offset = fp->end;
731 fp = fp->next;
732 }
733
734
735 return(1);
736 }
737
738
739
740 static struct sk_buff *ip_glue(struct ipq *qp)
741 {
742 struct sk_buff *skb;
743 struct iphdr *iph;
744 struct ipfrag *fp;
745 unsigned char *ptr;
746 int count, len;
747
748
749 len = sizeof(struct sk_buff)+qp->maclen + qp->ihlen + qp->len;
750 if ((skb = alloc_skb(len,GFP_ATOMIC)) == NULL)
751 {
752 printk("IP: queue_glue: no memory for glueing queue 0x%X\n", (int) qp);
753 ip_free(qp);
754 return(NULL);
755 }
756
757
758 skb->len = (len - qp->maclen);
759 skb->h.raw = (unsigned char *) (skb + 1);
760 skb->free = 1;
761 skb->lock = 1;
762
763
764 ptr = (unsigned char *) skb->h.raw;
765 memcpy(ptr, ((unsigned char *) qp->mac), qp->maclen);
766
767 ptr += qp->maclen;
768 memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen);
769
770 ptr += qp->ihlen;
771 skb->h.raw += qp->maclen;
772
773
774 count = 0;
775
776
777 fp = qp->fragments;
778 while(fp != NULL)
779 {
780 if(count+fp->len>skb->len)
781 {
782 printk("Invalid fragment list: Fragment over size.\n");
783 kfree_skb(skb,FREE_WRITE);
784 return NULL;
785 }
786
787 memcpy((ptr + fp->offset), fp->ptr, fp->len);
788 count += fp->len;
789 fp = fp->next;
790 }
791
792
793 ip_free(qp);
794
795
796 iph = skb->h.iph;
797 iph->frag_off = 0;
798 iph->tot_len = htons((iph->ihl * sizeof(unsigned long)) + count);
799 return(skb);
800 }
801
802
803
804 static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
805 {
806 struct ipfrag *prev, *next;
807 struct ipfrag *tfp;
808 struct ipq *qp;
809 struct sk_buff *skb2;
810 unsigned char *ptr;
811 int flags, offset;
812 int i, ihl, end;
813
814
815 qp = ip_find(iph);
816
817
818 offset = ntohs(iph->frag_off);
819 flags = offset & ~IP_OFFSET;
820 offset &= IP_OFFSET;
821 if (((flags & IP_MF) == 0) && (offset == 0))
822 {
823 if (qp != NULL)
824 ip_free(qp);
825 return(skb);
826 }
827 offset <<= 3;
828
829
830
831
832
833
834 if (qp != NULL)
835 {
836 del_timer(&qp->timer);
837 qp->timer.expires = IP_FRAG_TIME;
838 qp->timer.data = (unsigned long) qp;
839 qp->timer.function = ip_expire;
840 add_timer(&qp->timer);
841 }
842 else
843 {
844 if ((qp = ip_create(skb, iph, dev)) == NULL)
845 return(NULL);
846 }
847
848
849 ihl = (iph->ihl * sizeof(unsigned long));
850 end = offset + ntohs(iph->tot_len) - ihl;
851
852
853 ptr = ((unsigned char *) (skb + 1)) + dev->hard_header_len + ihl;
854
855
856 if ((flags & IP_MF) == 0)
857 qp->len = end;
858
859
860
861
862
863
864 prev = NULL;
865 for(next = qp->fragments; next != NULL; next = next->next)
866 {
867 if (next->offset > offset)
868 break;
869 prev = next;
870 }
871
872
873
874
875
876
877 if (prev != NULL && offset < prev->end)
878 {
879 i = prev->end - offset;
880 offset += i;
881 ptr += i;
882 DPRINTF((DBG_IP, "IP: defrag: fixed low overlap %d bytes\n", i));
883 }
884
885
886
887
888
889
890 for(; next != NULL; next = tfp)
891 {
892 tfp = next->next;
893 if (next->offset >= end)
894 break;
895
896 i = end - next->offset;
897 next->len -= i;
898 next->offset += i;
899 next->ptr += i;
900
901
902 if (next->len <= 0)
903 {
904 DPRINTF((DBG_IP, "IP: defrag: removing frag 0x%X (len %d)\n",
905 next, next->len));
906 if (next->prev != NULL)
907 next->prev->next = next->next;
908 else
909 qp->fragments = next->next;
910
911 if (tfp->next != NULL)
912 next->next->prev = next->prev;
913
914 kfree_s(next, sizeof(struct ipfrag));
915 }
916 DPRINTF((DBG_IP, "IP: defrag: fixed high overlap %d bytes\n", i));
917 }
918
919
920 tfp = NULL;
921 tfp = ip_frag_create(offset, end, skb, ptr);
922 tfp->prev = prev;
923 tfp->next = next;
924 if (prev != NULL)
925 prev->next = tfp;
926 else
927 qp->fragments = tfp;
928
929 if (next != NULL)
930 next->prev = tfp;
931
932
933
934
935
936
937
938 if (ip_done(qp))
939 {
940 skb2 = ip_glue(qp);
941 return(skb2);
942 }
943 return(NULL);
944 }
945
946
947
948
949
950
951
952
953
954
955 void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
956 {
957 struct iphdr *iph;
958 unsigned char *raw;
959 unsigned char *ptr;
960 struct sk_buff *skb2;
961 int left, mtu, hlen, len;
962 int offset;
963
964
965 raw = (unsigned char *) (skb + 1);
966 iph = (struct iphdr *) (raw + dev->hard_header_len);
967
968
969 hlen = (iph->ihl * sizeof(unsigned long));
970 left = ntohs(iph->tot_len) - hlen;
971 hlen += dev->hard_header_len;
972 mtu = (dev->mtu - hlen);
973 ptr = (raw + hlen);
974
975 DPRINTF((DBG_IP, "IP: Fragmentation Desired\n"));
976 DPRINTF((DBG_IP, " DEV=%s, MTU=%d, LEN=%d SRC=%s",
977 dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
978 DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
979
980
981 if (ntohs(iph->frag_off) & IP_DF)
982 {
983 DPRINTF((DBG_IP, "IP: Fragmentation Desired, but DF set !\n"));
984 DPRINTF((DBG_IP, " DEV=%s, MTU=%d, LEN=%d SRC=%s",
985 dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
986 DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
987
988
989
990
991
992
993 icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev);
994 return;
995 }
996
997
998 if (is_frag & 2)
999 offset = (ntohs(iph->frag_off) & 0x1fff) << 3;
1000 else
1001 offset = 0;
1002 while(left > 0)
1003 {
1004 len = left;
1005 if (len+8 > mtu)
1006 len = (dev->mtu - hlen - 8);
1007 if ((left - len) >= 8)
1008 {
1009 len /= 8;
1010 len *= 8;
1011 }
1012 DPRINTF((DBG_IP,"IP: frag: creating fragment of %d bytes (%d total)\n",
1013 len, len + hlen));
1014
1015
1016 if ((skb2 = alloc_skb(sizeof(struct sk_buff) + len + hlen,GFP_KERNEL)) == NULL)
1017 {
1018 printk("IP: frag: no memory for new fragment!\n");
1019 return;
1020 }
1021 skb2->arp = skb->arp;
1022 skb2->free = skb->free;
1023 skb2->len = len + hlen;
1024 skb2->h.raw=(char *)(skb2+1);
1025
1026 if (sk)
1027 sk->wmem_alloc += skb2->mem_len;
1028
1029
1030 memcpy(skb2->h.raw, raw, hlen);
1031
1032
1033 memcpy(skb2->h.raw + hlen, ptr, len);
1034 left -= len;
1035
1036 skb2->h.raw+=dev->hard_header_len;
1037
1038 iph = (struct iphdr *)(skb2->h.raw);
1039 iph->frag_off = htons((offset >> 3));
1040
1041
1042 if (left > 0 || (is_frag & 1))
1043 iph->frag_off |= htons(IP_MF);
1044 ptr += len;
1045 offset += len;
1046
1047
1048
1049 ip_queue_xmit(sk, dev, skb2, 1);
1050
1051 }
1052 }
1053
1054
1055
1056 #ifdef CONFIG_IP_FORWARD
1057
1058
1059 static void
1060 ip_forward(struct sk_buff *skb, struct device *dev, int is_frag)
1061 {
1062 struct device *dev2;
1063 struct iphdr *iph;
1064 struct sk_buff *skb2;
1065 struct rtable *rt;
1066 unsigned char *ptr;
1067 unsigned long raddr;
1068
1069
1070
1071
1072
1073
1074 if(dev->flags&IFF_PROMISC)
1075 {
1076 if(memcmp((char *)&skb[1],dev->dev_addr,dev->addr_len))
1077 return;
1078 }
1079
1080
1081
1082
1083
1084
1085 iph = skb->h.iph;
1086 iph->ttl--;
1087 if (iph->ttl <= 0) {
1088 DPRINTF((DBG_IP, "\nIP: *** datagram expired: TTL=0 (ignored) ***\n"));
1089 DPRINTF((DBG_IP, " SRC = %s ", in_ntoa(iph->saddr)));
1090 DPRINTF((DBG_IP, " DST = %s (ignored)\n", in_ntoa(iph->daddr)));
1091
1092
1093 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, dev);
1094 return;
1095 }
1096
1097
1098 ip_send_check(iph);
1099
1100
1101
1102
1103
1104 rt = rt_route(iph->daddr, NULL);
1105 if (rt == NULL) {
1106 DPRINTF((DBG_IP, "\nIP: *** routing (phase I) failed ***\n"));
1107
1108
1109 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, dev);
1110 return;
1111 }
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121 raddr = rt->rt_gateway;
1122 if (raddr != 0) {
1123 rt = rt_route(raddr, NULL);
1124 if (rt == NULL) {
1125 DPRINTF((DBG_IP, "\nIP: *** routing (phase II) failed ***\n"));
1126
1127
1128 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, dev);
1129 return;
1130 }
1131 if (rt->rt_gateway != 0) raddr = rt->rt_gateway;
1132 } else raddr = iph->daddr;
1133 dev2 = rt->rt_dev;
1134
1135
1136 if (dev == dev2)
1137 return;
1138
1139
1140
1141
1142 DPRINTF((DBG_IP, "\nIP: *** fwd %s -> ", in_ntoa(iph->saddr)));
1143 DPRINTF((DBG_IP, "%s (via %s), LEN=%d\n",
1144 in_ntoa(raddr), dev2->name, skb->len));
1145
1146 if (dev2->flags & IFF_UP) {
1147 skb2 = (struct sk_buff *) alloc_skb(sizeof(struct sk_buff) +
1148 dev2->hard_header_len + skb->len, GFP_ATOMIC);
1149 if (skb2 == NULL) {
1150 printk("\nIP: No memory available for IP forward\n");
1151 return;
1152 }
1153 ptr = (unsigned char *)(skb2 + 1);
1154 skb2->sk = NULL;
1155 skb2->free = 1;
1156 skb2->len = skb->len + dev2->hard_header_len;
1157 skb2->mem_addr = skb2;
1158 skb2->mem_len = sizeof(struct sk_buff) + skb2->len;
1159 skb2->next = NULL;
1160 skb2->h.raw = ptr;
1161
1162
1163 memcpy(ptr + dev2->hard_header_len, skb->h.raw, skb->len);
1164
1165
1166 (void) ip_send(skb2, raddr, skb->len, dev2, dev2->pa_addr);
1167
1168 if(skb2->len > dev2->mtu)
1169 {
1170 ip_fragment(NULL,skb2,dev2, is_frag);
1171 kfree_skb(skb2,FREE_WRITE);
1172 }
1173 else
1174 dev2->queue_xmit(skb2, dev2, SOPRI_NORMAL);
1175 }
1176 }
1177
1178
1179 #endif
1180
1181
1182 int
1183 ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
1184 {
1185 struct iphdr *iph = skb->h.iph;
1186 unsigned char hash;
1187 unsigned char flag = 0;
1188 unsigned char opts_p = 0;
1189 struct inet_protocol *ipprot;
1190 static struct options opt;
1191
1192 int brd;
1193 int is_frag=0;
1194
1195 DPRINTF((DBG_IP, "<<\n"));
1196
1197
1198 if (skb->len<sizeof(struct iphdr) || iph->ihl<5 || iph->version != 4 || ip_fast_csum((unsigned char *)iph, iph->ihl) !=0) {
1199 DPRINTF((DBG_IP, "\nIP: *** datagram error ***\n"));
1200 DPRINTF((DBG_IP, " SRC = %s ", in_ntoa(iph->saddr)));
1201 DPRINTF((DBG_IP, " DST = %s (ignored)\n", in_ntoa(iph->daddr)));
1202 skb->sk = NULL;
1203 kfree_skb(skb, FREE_WRITE);
1204 return(0);
1205 }
1206
1207 if (iph->ihl != 5) {
1208 ip_print(iph);
1209 memset((char *) &opt, 0, sizeof(opt));
1210 if (do_options(iph, &opt) != 0)
1211 return 0;
1212 opts_p = 1;
1213 }
1214
1215 if (iph->frag_off & 0x0020)
1216 is_frag|=1;
1217 if (ntohs(iph->frag_off) & 0x1fff)
1218 is_frag|=2;
1219
1220
1221 if ((brd = chk_addr(iph->daddr)) == 0) {
1222 #ifdef CONFIG_IP_FORWARD
1223 ip_forward(skb, dev, is_frag);
1224 #else
1225 printk("Machine %x tried to use us as a forwarder to %x but we have forwarding disabled!\n",
1226 iph->saddr,iph->daddr);
1227 #endif
1228 skb->sk = NULL;
1229 kfree_skb(skb, FREE_WRITE);
1230 return(0);
1231 }
1232
1233
1234
1235
1236
1237 if(is_frag)
1238 {
1239 #ifdef CONFIG_IP_DEFRAG
1240 skb=ip_defrag(iph,skb,dev);
1241 if(skb==NULL)
1242 {
1243 return 0;
1244 }
1245 iph=skb->h.iph;
1246 #else
1247 printk("\nIP: *** datagram fragmentation not yet implemented ***\n");
1248 printk(" SRC = %s ", in_ntoa(iph->saddr));
1249 printk(" DST = %s (ignored)\n", in_ntoa(iph->daddr));
1250 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1251 skb->sk = NULL;
1252 kfree_skb(skb, FREE_WRITE);
1253 return(0);
1254 #endif
1255 }
1256
1257
1258
1259 if(brd==IS_INVBCAST)
1260 {
1261
1262
1263 skb->sk=NULL;
1264 kfree_skb(skb,FREE_WRITE);
1265 return(0);
1266 }
1267
1268
1269 skb->h.raw += iph->ihl*4;
1270 hash = iph->protocol & (MAX_INET_PROTOS -1);
1271 for (ipprot = (struct inet_protocol *)inet_protos[hash];
1272 ipprot != NULL;
1273 ipprot=(struct inet_protocol *)ipprot->next)
1274 {
1275 struct sk_buff *skb2;
1276
1277 if (ipprot->protocol != iph->protocol) continue;
1278 DPRINTF((DBG_IP, "Using protocol = %X:\n", ipprot));
1279 print_ipprot(ipprot);
1280
1281
1282
1283
1284
1285
1286 if (ipprot->copy) {
1287 skb2 = alloc_skb(skb->mem_len, GFP_ATOMIC);
1288 if (skb2 == NULL)
1289 continue;
1290 memcpy(skb2, skb, skb->mem_len);
1291 skb2->mem_addr = skb2;
1292 skb2->h.raw = (unsigned char *)(
1293 (unsigned long)skb2 +
1294 (unsigned long) skb->h.raw -
1295 (unsigned long)skb);
1296 skb2->free=1;
1297 } else {
1298 skb2 = skb;
1299 }
1300 flag = 1;
1301
1302
1303
1304
1305
1306
1307 ipprot->handler(skb2, dev, opts_p ? &opt : 0, iph->daddr,
1308 (ntohs(iph->tot_len) - (iph->ihl * 4)),
1309 iph->saddr, 0, ipprot);
1310
1311 }
1312
1313
1314
1315
1316
1317
1318
1319 if (!flag) {
1320 if (brd != IS_BROADCAST)
1321 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1322 skb->sk = NULL;
1323 kfree_skb(skb, FREE_WRITE);
1324 }
1325
1326 return(0);
1327 }
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337 void
1338 ip_queue_xmit(struct sock *sk, struct device *dev,
1339 struct sk_buff *skb, int free)
1340 {
1341 struct iphdr *iph;
1342 unsigned char *ptr;
1343
1344 if (sk == NULL) free = 1;
1345 if (dev == NULL) {
1346 printk("IP: ip_queue_xmit dev = NULL\n");
1347 return;
1348 }
1349 IS_SKB(skb);
1350 skb->free = free;
1351 skb->dev = dev;
1352 skb->when = jiffies;
1353
1354 DPRINTF((DBG_IP, ">>\n"));
1355 ptr = (unsigned char *)(skb + 1);
1356 ptr += dev->hard_header_len;
1357 iph = (struct iphdr *)ptr;
1358 iph->tot_len = ntohs(skb->len-dev->hard_header_len);
1359
1360 if(skb->len > dev->mtu)
1361 {
1362
1363 ip_fragment(sk,skb,dev,0);
1364 IS_SKB(skb);
1365 kfree_skb(skb,FREE_WRITE);
1366 return;
1367 }
1368
1369 ip_send_check(iph);
1370 ip_print(iph);
1371 skb->next = NULL;
1372
1373
1374 skb->magic = 1;
1375 if (!free) {
1376 skb->link3 = NULL;
1377 sk->packets_out++;
1378 cli();
1379 if (sk->send_head == NULL) {
1380 sk->send_tail = skb;
1381 sk->send_head = skb;
1382 } else {
1383
1384 if (sk->send_tail == NULL) {
1385 printk("IP: ***bug sk->send_tail == NULL != sk->send_head\n");
1386 sort_send(sk);
1387 } else {
1388 sk->send_tail->link3 = skb;
1389 sk->send_tail = skb;
1390 }
1391 }
1392 sti();
1393 reset_timer(sk, TIME_WRITE,
1394 backoff(sk->backoff) * (2 * sk->mdev + sk->rtt));
1395 } else {
1396 skb->sk = sk;
1397 }
1398
1399
1400 if (dev->flags & IFF_UP) {
1401 if (sk != NULL) {
1402 dev->queue_xmit(skb, dev, sk->priority);
1403 }
1404 else {
1405 dev->queue_xmit(skb, dev, SOPRI_NORMAL);
1406 }
1407 } else {
1408 if (free) kfree_skb(skb, FREE_WRITE);
1409 }
1410 }
1411
1412
1413 void
1414 ip_retransmit(struct sock *sk, int all)
1415 {
1416 struct sk_buff * skb;
1417 struct proto *prot;
1418 struct device *dev;
1419
1420 prot = sk->prot;
1421 skb = sk->send_head;
1422 while (skb != NULL) {
1423 dev = skb->dev;
1424
1425 if(dev==NULL)
1426 {
1427 printk("ip_retransmit: NULL device bug!\n");
1428 goto oops;
1429 }
1430
1431 IS_SKB(skb);
1432
1433
1434
1435
1436
1437
1438 cli();
1439
1440
1441 if (!skb->arp) {
1442 if (dev->rebuild_header(skb+1, dev)) {
1443 sti();
1444 if (!all) break;
1445 skb = (struct sk_buff *)skb->link3;
1446 continue;
1447 }
1448 }
1449 skb->arp = 1;
1450 sti();
1451 skb->when = jiffies;
1452
1453
1454 if (dev->flags & IFF_UP) {
1455 if (sk) dev->queue_xmit(skb, dev, sk->priority);
1456
1457 }
1458
1459 oops: sk->retransmits++;
1460 sk->prot->retransmits ++;
1461 if (!all) break;
1462
1463
1464 if (sk->retransmits > sk->cong_window) break;
1465 skb = (struct sk_buff *)skb->link3;
1466 }
1467
1468
1469
1470
1471
1472
1473
1474 sk->backoff++;
1475 reset_timer(sk, TIME_WRITE, backoff(sk->backoff) * (2 * sk->mdev + sk->rtt));
1476 }
1477
1478
1479 int backoff(int n)
1480 {
1481
1482
1483
1484
1485
1486 if(n<0)
1487 {
1488 printk("Backoff < 0!\n");
1489 return 16;
1490 }
1491
1492 if(n <= 4)
1493 return 1 << n;
1494 else
1495 {
1496 if(n<255)
1497 return n * n;
1498 else
1499 {
1500 printk("Overloaded backoff!\n");
1501 return 255*255;
1502 }
1503 }
1504 }
1505