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