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