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
768
769 ptr = (unsigned char *) skb->h.raw;
770 memcpy(ptr, ((unsigned char *) qp->mac), qp->maclen);
771
772 ptr += qp->maclen;
773 memcpy(ptr, ((unsigned char *) qp->iph), qp->ihlen);
774
775 ptr += qp->ihlen;
776 skb->h.raw += qp->maclen;
777
778
779 count = 0;
780
781
782 fp = qp->fragments;
783 while(fp != NULL)
784 {
785 if(count+fp->len>skb->len)
786 {
787 printk("Invalid fragment list: Fragment over size.\n");
788 kfree_skb(skb,FREE_WRITE);
789 return NULL;
790 }
791
792 memcpy((ptr + fp->offset), fp->ptr, fp->len);
793 count += fp->len;
794 fp = fp->next;
795 }
796
797
798 ip_free(qp);
799
800
801 iph = skb->h.iph;
802 iph->frag_off = 0;
803 iph->tot_len = htons((iph->ihl * sizeof(unsigned long)) + count);
804 skb->ip_hdr = iph;
805 return(skb);
806 }
807
808
809
810 static struct sk_buff *ip_defrag(struct iphdr *iph, struct sk_buff *skb, struct device *dev)
811 {
812 struct ipfrag *prev, *next;
813 struct ipfrag *tfp;
814 struct ipq *qp;
815 struct sk_buff *skb2;
816 unsigned char *ptr;
817 int flags, offset;
818 int i, ihl, end;
819
820
821 qp = ip_find(iph);
822
823
824 offset = ntohs(iph->frag_off);
825 flags = offset & ~IP_OFFSET;
826 offset &= IP_OFFSET;
827 if (((flags & IP_MF) == 0) && (offset == 0))
828 {
829 if (qp != NULL)
830 ip_free(qp);
831 return(skb);
832 }
833 offset <<= 3;
834
835
836
837
838
839
840 if (qp != NULL)
841 {
842 del_timer(&qp->timer);
843 qp->timer.expires = IP_FRAG_TIME;
844 qp->timer.data = (unsigned long) qp;
845 qp->timer.function = ip_expire;
846 add_timer(&qp->timer);
847 }
848 else
849 {
850 if ((qp = ip_create(skb, iph, dev)) == NULL)
851 return(NULL);
852 }
853
854
855 ihl = (iph->ihl * sizeof(unsigned long));
856 end = offset + ntohs(iph->tot_len) - ihl;
857
858
859 ptr = skb->data + dev->hard_header_len + ihl;
860
861
862 if ((flags & IP_MF) == 0)
863 qp->len = end;
864
865
866
867
868
869
870 prev = NULL;
871 for(next = qp->fragments; next != NULL; next = next->next)
872 {
873 if (next->offset > offset)
874 break;
875 prev = next;
876 }
877
878
879
880
881
882
883 if (prev != NULL && offset < prev->end)
884 {
885 i = prev->end - offset;
886 offset += i;
887 ptr += i;
888 DPRINTF((DBG_IP, "IP: defrag: fixed low overlap %d bytes\n", i));
889 }
890
891
892
893
894
895
896 for(; next != NULL; next = tfp)
897 {
898 tfp = next->next;
899 if (next->offset >= end)
900 break;
901
902 i = end - next->offset;
903 next->len -= i;
904 next->offset += i;
905 next->ptr += i;
906
907
908 if (next->len <= 0)
909 {
910 DPRINTF((DBG_IP, "IP: defrag: removing frag 0x%X (len %d)\n",
911 next, next->len));
912 if (next->prev != NULL)
913 next->prev->next = next->next;
914 else
915 qp->fragments = next->next;
916
917 if (tfp->next != NULL)
918 next->next->prev = next->prev;
919
920 kfree_s(next, sizeof(struct ipfrag));
921 }
922 DPRINTF((DBG_IP, "IP: defrag: fixed high overlap %d bytes\n", i));
923 }
924
925
926 tfp = NULL;
927 tfp = ip_frag_create(offset, end, skb, ptr);
928 tfp->prev = prev;
929 tfp->next = next;
930 if (prev != NULL)
931 prev->next = tfp;
932 else
933 qp->fragments = tfp;
934
935 if (next != NULL)
936 next->prev = tfp;
937
938
939
940
941
942
943
944 if (ip_done(qp))
945 {
946 skb2 = ip_glue(qp);
947 return(skb2);
948 }
949 return(NULL);
950 }
951
952
953
954
955
956
957
958
959
960
961 void ip_fragment(struct sock *sk, struct sk_buff *skb, struct device *dev, int is_frag)
962 {
963 struct iphdr *iph;
964 unsigned char *raw;
965 unsigned char *ptr;
966 struct sk_buff *skb2;
967 int left, mtu, hlen, len;
968 int offset;
969
970
971 raw = skb->data;
972 iph = (struct iphdr *) (raw + dev->hard_header_len);
973
974
975 hlen = (iph->ihl * sizeof(unsigned long));
976 left = ntohs(iph->tot_len) - hlen;
977 hlen += dev->hard_header_len;
978 mtu = (dev->mtu - hlen);
979 ptr = (raw + hlen);
980
981 DPRINTF((DBG_IP, "IP: Fragmentation Desired\n"));
982 DPRINTF((DBG_IP, " DEV=%s, MTU=%d, LEN=%d SRC=%s",
983 dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
984 DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
985
986
987 if (ntohs(iph->frag_off) & IP_DF)
988 {
989 DPRINTF((DBG_IP, "IP: Fragmentation Desired, but DF set !\n"));
990 DPRINTF((DBG_IP, " DEV=%s, MTU=%d, LEN=%d SRC=%s",
991 dev->name, dev->mtu, left, in_ntoa(iph->saddr)));
992 DPRINTF((DBG_IP, " DST=%s\n", in_ntoa(iph->daddr)));
993
994
995
996
997
998
999 icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev);
1000 return;
1001 }
1002
1003
1004 if (is_frag & 2)
1005 offset = (ntohs(iph->frag_off) & 0x1fff) << 3;
1006 else
1007 offset = 0;
1008 while(left > 0)
1009 {
1010 len = left;
1011 if (len+8 > mtu)
1012 len = (dev->mtu - hlen - 8);
1013 if ((left - len) >= 8)
1014 {
1015 len /= 8;
1016 len *= 8;
1017 }
1018 DPRINTF((DBG_IP,"IP: frag: creating fragment of %d bytes (%d total)\n",
1019 len, len + hlen));
1020
1021
1022 if ((skb2 = alloc_skb(sizeof(struct sk_buff) + len + hlen,GFP_KERNEL)) == NULL)
1023 {
1024 printk("IP: frag: no memory for new fragment!\n");
1025 return;
1026 }
1027 skb2->arp = skb->arp;
1028 skb2->free = skb->free;
1029 skb2->len = len + hlen;
1030 skb2->h.raw=(char *) skb2->data;
1031
1032 if (sk)
1033 sk->wmem_alloc += skb2->mem_len;
1034
1035
1036 memcpy(skb2->h.raw, raw, hlen);
1037
1038
1039 memcpy(skb2->h.raw + hlen, ptr, len);
1040 left -= len;
1041
1042 skb2->h.raw+=dev->hard_header_len;
1043
1044 iph = (struct iphdr *)(skb2->h.raw);
1045 iph->frag_off = htons((offset >> 3));
1046
1047
1048 if (left > 0 || (is_frag & 1))
1049 iph->frag_off |= htons(IP_MF);
1050 ptr += len;
1051 offset += len;
1052
1053
1054
1055 ip_queue_xmit(sk, dev, skb2, 1);
1056
1057 }
1058 }
1059
1060
1061
1062 #ifdef CONFIG_IP_FORWARD
1063
1064
1065 static void
1066 ip_forward(struct sk_buff *skb, struct device *dev, int is_frag)
1067 {
1068 struct device *dev2;
1069 struct iphdr *iph;
1070 struct sk_buff *skb2;
1071 struct rtable *rt;
1072 unsigned char *ptr;
1073 unsigned long raddr;
1074
1075
1076
1077
1078
1079
1080 if(dev->flags&IFF_PROMISC)
1081 {
1082 if(memcmp((char *)&skb[1],dev->dev_addr,dev->addr_len))
1083 return;
1084 }
1085
1086
1087
1088
1089
1090
1091 iph = skb->h.iph;
1092 iph->ttl--;
1093 if (iph->ttl <= 0) {
1094 DPRINTF((DBG_IP, "\nIP: *** datagram expired: TTL=0 (ignored) ***\n"));
1095 DPRINTF((DBG_IP, " SRC = %s ", in_ntoa(iph->saddr)));
1096 DPRINTF((DBG_IP, " DST = %s (ignored)\n", in_ntoa(iph->daddr)));
1097
1098
1099 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, dev);
1100 return;
1101 }
1102
1103
1104 ip_send_check(iph);
1105
1106
1107
1108
1109
1110 rt = rt_route(iph->daddr, NULL);
1111 if (rt == NULL) {
1112 DPRINTF((DBG_IP, "\nIP: *** routing (phase I) failed ***\n"));
1113
1114
1115 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, dev);
1116 return;
1117 }
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127 raddr = rt->rt_gateway;
1128 if (raddr != 0) {
1129 rt = rt_route(raddr, NULL);
1130 if (rt == NULL) {
1131 DPRINTF((DBG_IP, "\nIP: *** routing (phase II) failed ***\n"));
1132
1133
1134 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, dev);
1135 return;
1136 }
1137 if (rt->rt_gateway != 0) raddr = rt->rt_gateway;
1138 } else raddr = iph->daddr;
1139 dev2 = rt->rt_dev;
1140
1141
1142 if (dev == dev2)
1143 return;
1144
1145
1146
1147
1148 DPRINTF((DBG_IP, "\nIP: *** fwd %s -> ", in_ntoa(iph->saddr)));
1149 DPRINTF((DBG_IP, "%s (via %s), LEN=%d\n",
1150 in_ntoa(raddr), dev2->name, skb->len));
1151
1152 if (dev2->flags & IFF_UP) {
1153 skb2 = (struct sk_buff *) alloc_skb(sizeof(struct sk_buff) +
1154 dev2->hard_header_len + skb->len, GFP_ATOMIC);
1155 if (skb2 == NULL) {
1156 printk("\nIP: No memory available for IP forward\n");
1157 return;
1158 }
1159 ptr = skb2->data;
1160 skb2->sk = NULL;
1161 skb2->free = 1;
1162 skb2->len = skb->len + dev2->hard_header_len;
1163 skb2->mem_addr = skb2;
1164 skb2->mem_len = sizeof(struct sk_buff) + skb2->len;
1165 skb2->next = NULL;
1166 skb2->h.raw = ptr;
1167
1168
1169 memcpy(ptr + dev2->hard_header_len, skb->h.raw, skb->len);
1170
1171
1172 (void) ip_send(skb2, raddr, skb->len, dev2, dev2->pa_addr);
1173
1174 if(skb2->len > dev2->mtu)
1175 {
1176 ip_fragment(NULL,skb2,dev2, is_frag);
1177 kfree_skb(skb2,FREE_WRITE);
1178 }
1179 else
1180 dev2->queue_xmit(skb2, dev2, SOPRI_NORMAL);
1181 }
1182 }
1183
1184
1185 #endif
1186
1187
1188 int
1189 ip_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
1190 {
1191 struct iphdr *iph = skb->h.iph;
1192 unsigned char hash;
1193 unsigned char flag = 0;
1194 unsigned char opts_p = 0;
1195 struct inet_protocol *ipprot;
1196 static struct options opt;
1197
1198 int brd;
1199 int is_frag=0;
1200
1201 DPRINTF((DBG_IP, "<<\n"));
1202
1203 skb->ip_hdr = iph;
1204
1205 if (skb->len<sizeof(struct iphdr) || iph->ihl<5 || iph->version != 4 || ip_fast_csum((unsigned char *)iph, iph->ihl) !=0) {
1206 DPRINTF((DBG_IP, "\nIP: *** datagram error ***\n"));
1207 DPRINTF((DBG_IP, " SRC = %s ", in_ntoa(iph->saddr)));
1208 DPRINTF((DBG_IP, " DST = %s (ignored)\n", in_ntoa(iph->daddr)));
1209 skb->sk = NULL;
1210 kfree_skb(skb, FREE_WRITE);
1211 return(0);
1212 }
1213
1214 if (iph->ihl != 5) {
1215 ip_print(iph);
1216 memset((char *) &opt, 0, sizeof(opt));
1217 if (do_options(iph, &opt) != 0)
1218 return 0;
1219 opts_p = 1;
1220 }
1221
1222 if (iph->frag_off & 0x0020)
1223 is_frag|=1;
1224 if (ntohs(iph->frag_off) & 0x1fff)
1225 is_frag|=2;
1226
1227
1228 if ((brd = chk_addr(iph->daddr)) == 0) {
1229 #ifdef CONFIG_IP_FORWARD
1230 ip_forward(skb, dev, is_frag);
1231 #else
1232 printk("Machine %x tried to use us as a forwarder to %x but we have forwarding disabled!\n",
1233 iph->saddr,iph->daddr);
1234 #endif
1235 skb->sk = NULL;
1236 kfree_skb(skb, FREE_WRITE);
1237 return(0);
1238 }
1239
1240
1241
1242
1243
1244 if(is_frag)
1245 {
1246 #ifdef CONFIG_IP_DEFRAG
1247 skb=ip_defrag(iph,skb,dev);
1248 if(skb==NULL)
1249 {
1250 return 0;
1251 }
1252 iph=skb->h.iph;
1253 #else
1254 printk("\nIP: *** datagram fragmentation not yet implemented ***\n");
1255 printk(" SRC = %s ", in_ntoa(iph->saddr));
1256 printk(" DST = %s (ignored)\n", in_ntoa(iph->daddr));
1257 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1258 skb->sk = NULL;
1259 kfree_skb(skb, FREE_WRITE);
1260 return(0);
1261 #endif
1262 }
1263
1264
1265
1266 if(brd==IS_INVBCAST)
1267 {
1268
1269
1270 skb->sk=NULL;
1271 kfree_skb(skb,FREE_WRITE);
1272 return(0);
1273 }
1274
1275
1276
1277 skb->ip_hdr = iph;
1278 skb->h.raw += iph->ihl*4;
1279 hash = iph->protocol & (MAX_INET_PROTOS -1);
1280 for (ipprot = (struct inet_protocol *)inet_protos[hash];
1281 ipprot != NULL;
1282 ipprot=(struct inet_protocol *)ipprot->next)
1283 {
1284 struct sk_buff *skb2;
1285
1286 if (ipprot->protocol != iph->protocol) continue;
1287 DPRINTF((DBG_IP, "Using protocol = %X:\n", ipprot));
1288 print_ipprot(ipprot);
1289
1290
1291
1292
1293
1294
1295 if (ipprot->copy) {
1296 skb2 = alloc_skb(skb->mem_len, GFP_ATOMIC);
1297 if (skb2 == NULL)
1298 continue;
1299 memcpy(skb2, skb, skb->mem_len);
1300 skb2->mem_addr = skb2;
1301 skb2->ip_hdr = (struct iphdr *)(
1302 (unsigned long)skb2 +
1303 (unsigned long) skb->ip_hdr -
1304 (unsigned long)skb);
1305 skb2->h.raw = (unsigned char *)(
1306 (unsigned long)skb2 +
1307 (unsigned long) skb->h.raw -
1308 (unsigned long)skb);
1309 skb2->free=1;
1310 } else {
1311 skb2 = skb;
1312 }
1313 flag = 1;
1314
1315
1316
1317
1318
1319
1320 ipprot->handler(skb2, dev, opts_p ? &opt : 0, iph->daddr,
1321 (ntohs(iph->tot_len) - (iph->ihl * 4)),
1322 iph->saddr, 0, ipprot);
1323
1324 }
1325
1326
1327
1328
1329
1330
1331
1332 if (!flag) {
1333 if (brd != IS_BROADCAST)
1334 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1335 skb->sk = NULL;
1336 kfree_skb(skb, FREE_WRITE);
1337 }
1338
1339 return(0);
1340 }
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350 void
1351 ip_queue_xmit(struct sock *sk, struct device *dev,
1352 struct sk_buff *skb, int free)
1353 {
1354 struct iphdr *iph;
1355 unsigned char *ptr;
1356
1357 if (sk == NULL) free = 1;
1358 if (dev == NULL) {
1359 printk("IP: ip_queue_xmit dev = NULL\n");
1360 return;
1361 }
1362 IS_SKB(skb);
1363 skb->free = free;
1364 skb->dev = dev;
1365 skb->when = jiffies;
1366
1367 DPRINTF((DBG_IP, ">>\n"));
1368 ptr = skb->data;
1369 ptr += dev->hard_header_len;
1370 iph = (struct iphdr *)ptr;
1371 iph->tot_len = ntohs(skb->len-dev->hard_header_len);
1372
1373 if(skb->len > dev->mtu)
1374 {
1375
1376 ip_fragment(sk,skb,dev,0);
1377 IS_SKB(skb);
1378 kfree_skb(skb,FREE_WRITE);
1379 return;
1380 }
1381
1382 ip_send_check(iph);
1383 ip_print(iph);
1384 skb->next = NULL;
1385
1386
1387 skb->magic = 1;
1388 if (!free) {
1389 skb->link3 = NULL;
1390 sk->packets_out++;
1391 cli();
1392 if (sk->send_head == NULL) {
1393 sk->send_tail = skb;
1394 sk->send_head = skb;
1395 } else {
1396
1397 if (sk->send_tail == NULL) {
1398 printk("IP: ***bug sk->send_tail == NULL != sk->send_head\n");
1399 sort_send(sk);
1400 } else {
1401 sk->send_tail->link3 = skb;
1402 sk->send_tail = skb;
1403 }
1404 }
1405 sti();
1406 reset_timer(sk, TIME_WRITE, sk->rto);
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_do_retransmit(struct sock *sk, int all)
1427 {
1428 struct sk_buff * skb;
1429 struct proto *prot;
1430 struct device *dev;
1431 int retransmits;
1432
1433 prot = sk->prot;
1434 skb = sk->send_head;
1435 retransmits = sk->retransmits;
1436 while (skb != NULL) {
1437 dev = skb->dev;
1438
1439 if(dev==NULL)
1440 {
1441 printk("ip_retransmit: NULL device bug!\n");
1442 goto oops;
1443 }
1444
1445 IS_SKB(skb);
1446
1447
1448
1449
1450
1451
1452 cli();
1453
1454
1455 if (!skb->arp) {
1456 if (dev->rebuild_header(skb->data, dev)) {
1457 sti();
1458 if (!all) break;
1459 skb = (struct sk_buff *)skb->link3;
1460 continue;
1461 }
1462 }
1463 skb->arp = 1;
1464 sti();
1465 skb->when = jiffies;
1466
1467
1468 if (dev->flags & IFF_UP) {
1469 if (sk && !skb_device_locked(skb))
1470 dev->queue_xmit(skb, dev, sk->priority);
1471
1472 }
1473
1474 oops: retransmits++;
1475 sk->prot->retransmits ++;
1476 if (!all) break;
1477
1478
1479 if (sk->retransmits > sk->cong_window) break;
1480 skb = (struct sk_buff *)skb->link3;
1481 }
1482 }
1483
1484
1485
1486
1487
1488
1489
1490
1491 void
1492 ip_retransmit(struct sock *sk, int all)
1493 {
1494 ip_do_retransmit(sk, all);
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509 sk->retransmits++;
1510 sk->backoff++;
1511 sk->rto = min(sk->rto << 1, 120*HZ);
1512 reset_timer(sk, TIME_WRITE, sk->rto);
1513 }
1514
1515
1516
1517
1518
1519
1520 int ip_setsockopt(struct sock *sk, int level, int optname, char *optval, int optlen)
1521 {
1522 int val,err;
1523
1524 if (optval == NULL)
1525 return(-EINVAL);
1526
1527 err=verify_area(VERIFY_READ, optval, sizeof(int));
1528 if(err)
1529 return err;
1530
1531 val = get_fs_long((unsigned long *)optval);
1532
1533 if(level!=SOL_IP)
1534 return -EOPNOTSUPP;
1535
1536 switch(optname)
1537 {
1538 case IP_TOS:
1539 if(val<0||val>255)
1540 return -EINVAL;
1541 sk->ip_tos=val;
1542 return 0;
1543 case IP_TTL:
1544 if(val<1||val>255)
1545 return -EINVAL;
1546 sk->ip_ttl=val;
1547 return 0;
1548
1549 default:
1550 return(-ENOPROTOOPT);
1551 }
1552 }
1553
1554 int ip_getsockopt(struct sock *sk, int level, int optname, char *optval, int *optlen)
1555 {
1556 int val,err;
1557
1558 if(level!=SOL_IP)
1559 return -EOPNOTSUPP;
1560
1561 switch(optname)
1562 {
1563 case IP_TOS:
1564 val=sk->ip_tos;
1565 break;
1566 case IP_TTL:
1567 val=sk->ip_ttl;
1568 break;
1569 default:
1570 return(-ENOPROTOOPT);
1571 }
1572 err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
1573 if(err)
1574 return err;
1575 put_fs_long(sizeof(int),(unsigned long *) optlen);
1576
1577 err=verify_area(VERIFY_WRITE, optval, sizeof(int));
1578 if(err)
1579 return err;
1580 put_fs_long(val,(unsigned long *)optval);
1581
1582 return(0);
1583 }