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