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