1 /* 2 * INET An implementation of the TCP/IP protocol suite for the LINUX 3 * operating system. INET is implemented using the BSD Socket 4 * interface as the means of communication with the user level. 5 * 6 * The Internet Protocol (IP) module. 7 * 8 * Version: @(#)ip.c 1.0.16b 9/1/93 9 * 10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu> 11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 12 * Donald Becker, <becker@super.org> 13 * Alan Cox, <gw4pts@gw4pts.ampr.org> 14 * Richard Underwood 15 * 16 * Fixes: 17 * Alan Cox : Commented a couple of minor bits of surplus code 18 * Alan Cox : Undefining IP_FORWARD doesn't include the code 19 * (just stops a compiler warning). 20 * Alan Cox : Frames with >=MAX_ROUTE record routes, strict routes or loose routes 21 * are junked rather than corrupting things. 22 * Alan Cox : Frames to bad broadcast subnets are dumped 23 * We used to process them non broadcast and 24 * boy could that cause havoc. 25 * Alan Cox : ip_forward sets the free flag on the 26 * new frame it queues. Still crap because 27 * it copies the frame but at least it 28 * doesn't eat memory too. 29 * Alan Cox : Generic queue code and memory fixes. 30 * Fred Van Kempen : IP fragment support (borrowed from NET2E) 31 * Gerhard Koerting: Forward fragmented frames correctly. 32 * Gerhard Koerting: Fixes to my fix of the above 8-). 33 * Gerhard Koerting: IP interface addressing fix. 34 * Linus Torvalds : More robustness checks 35 * Alan Cox : Even more checks: Still not as robust as it ought to be 36 * Alan Cox : Save IP header pointer for later 37 * Alan Cox : ip option setting 38 * Alan Cox : Use ip_tos/ip_ttl settings 39 * Alan Cox : Fragmentation bogosity removed 40 * (Thanks to Mark.Bush@prg.ox.ac.uk) 41 * Dmitry Gorodchanin : Send of a raw packet crash fix. 42 * Alan Cox : Silly ip bug when an overlength 43 * fragment turns up. Now frees the 44 * queue. 45 * Linus Torvalds/ : Memory leakage on fragmentation 46 * Alan Cox : handling. 47 * Gerhard Koerting: Forwarding uses IP priority hints 48 * Teemu Rantanen : Fragment problems. 49 * Alan Cox : General cleanup, comments and reformat 50 * Alan Cox : SNMP statistics 51 * Alan Cox : BSD address rule semantics. Also see 52 * UDP as there is a nasty checksum issue 53 * if you do things the wrong way. 54 * Alan Cox : Always defrag, moved IP_FORWARD to the config.in file 55 * Alan Cox : IP options adjust sk->priority. 56 * Pedro Roque : Fix mtu/length error in ip_forward. 57 * Alan Cox : Avoid ip_chk_addr when possible. 58 * Richard Underwood : IP multicasting. 59 * Alan Cox : Cleaned up multicast handlers. 60 * Alan Cox : RAW sockets demultiplex in the BSD style. 61 * Gunther Mayer : Fix the SNMP reporting typo 62 * Alan Cox : Always in group 224.0.0.1 63 * Alan Cox : Multicast loopback error for 224.0.0.1 64 * Alan Cox : IP_MULTICAST_LOOP option. 65 * Alan Cox : Use notifiers. 66 * 67 * To Fix: 68 * IP option processing is mostly not needed. ip_forward needs to know about routing rules 69 * and time stamp but that's about all. Use the route mtu field here too 70 * IP fragmentation wants rewriting cleanly. The RFC815 algorithm is much more efficient 71 * and could be made very efficient with the addition of some virtual memory hacks to permit 72 * the allocation of a buffer that can then be 'grown' by twiddling page tables. 73 * Output fragmentation wants updating along with the buffer management to use a single 74 * interleaved copy algorithm so that fragmenting has a one copy overhead. Actual packet 75 * output should probably do its own fragmentation at the UDP/RAW layer. TCP shouldn't cause 76 * fragmentation anyway. 77 * 78 * This program is free software; you can redistribute it and/or 79 * modify it under the terms of the GNU General Public License 80 * as published by the Free Software Foundation; either version 81 * 2 of the License, or (at your option) any later version. 82 */ 83
84 #include <asm/segment.h>
85 #include <asm/system.h>
86 #include <linux/types.h>
87 #include <linux/kernel.h>
88 #include <linux/sched.h>
89 #include <linux/string.h>
90 #include <linux/errno.h>
91 #include <linux/config.h>
92
93 #include <linux/socket.h>
94 #include <linux/sockios.h>
95 #include <linux/in.h>
96 #include <linux/inet.h>
97 #include <linux/netdevice.h>
98 #include <linux/etherdevice.h>
99
100 #include "snmp.h"
101 #include "ip.h"
102 #include "protocol.h"
103 #include "route.h"
104 #include "tcp.h"
105 #include <linux/skbuff.h>
106 #include "sock.h"
107 #include "arp.h"
108 #include "icmp.h"
109 #include "raw.h"
110 #include "igmp.h"
111 #include <linux/ip_fw.h>
112
113 #define CONFIG_IP_DEFRAG
114
115 externintlast_retran;
116 externvoid sort_send(structsock *sk);
117
118 #definemin(a,b) ((a)<(b)?(a):(b))
119 #defineLOOPBACK(x) (((x) & htonl(0xff000000)) == htonl(0x7f000000))
120
121 /* 122 * SNMP management statistics 123 */ 124
125 #ifdefCONFIG_IP_FORWARD 126 structip_mibip_statistics={1,64,}; /* Forwarding=Yes, Default TTL=64 */ 127 #else 128 structip_mibip_statistics={0,64,}; /* Forwarding=No, Default TTL=64 */ 129 #endif 130
131 /* 132 * Handle the issuing of an ioctl() request 133 * for the ip device. This is scheduled to 134 * disappear 135 */ 136
137 intip_ioctl(structsock *sk, intcmd, unsignedlongarg)
/* */ 138 { 139 switch(cmd)
140 { 141 default:
142 return(-EINVAL);
143 } 144 } 145
146
147 /* these two routines will do routing. */ 148
149 staticvoid 150 strict_route(structiphdr *iph, structoptions *opt)
/* */ 151 { 152 } 153
154
155 staticvoid 156 loose_route(structiphdr *iph, structoptions *opt)
/* */ 157 { 158 } 159
160
161
162
163 /* This routine will check to see if we have lost a gateway. */ 164 void 165 ip_route_check(unsignedlongdaddr)
/* */ 166 { 167 } 168
169
170 #if 0
171 /* this routine puts the options at the end of an ip header. */ 172 staticint 173 build_options(structiphdr *iph, structoptions *opt)
/* */ 174 { 175 unsignedchar *ptr;
176 /* currently we don't support any options. */ 177 ptr = (unsignedchar *)(iph+1);
178 *ptr = 0;
179 return (4);
180 } 181 #endif 182
183
184 /* 185 * Take an skb, and fill in the MAC header. 186 */ 187
188 staticintip_send(structsk_buff *skb, unsignedlongdaddr, intlen, structdevice *dev, unsignedlongsaddr)
/* */ 189 { 190 intmac = 0;
191
192 skb->dev = dev;
193 skb->arp = 1;
194 if (dev->hard_header)
195 { 196 /* 197 * Build a hardware header. Source address is our mac, destination unknown 198 * (rebuild header will sort this out) 199 */ 200 mac = dev->hard_header(skb->data, dev, ETH_P_IP, NULL, NULL, len, skb);
201 if (mac < 0)
202 { 203 mac = -mac;
204 skb->arp = 0;
205 skb->raddr = daddr; /* next routing address */ 206 } 207 } 208 returnmac;
209 } 210
211 intip_id_count = 0;
212
213 /* 214 * This routine builds the appropriate hardware/IP headers for 215 * the routine. It assumes that if *dev != NULL then the 216 * protocol knows what it's doing, otherwise it uses the 217 * routing/ARP tables to select a device struct. 218 */ 219 intip_build_header(structsk_buff *skb, unsignedlongsaddr, unsignedlongdaddr,
/* */ 220 structdevice **dev, inttype, structoptions *opt, intlen, inttos, intttl)
221 { 222 staticstructoptionsoptmem;
223 structiphdr *iph;
224 structrtable *rt;
225 unsignedchar *buff;
226 unsignedlongraddr;
227 inttmp;
228 unsignedlongsrc;
229
230 buff = skb->data;
231
232 /* 233 * See if we need to look up the device. 234 */ 235
236 #ifdef CONFIG_INET_MULTICAST
237 if(MULTICAST(daddr) && *dev==NULL && skb->sk && *skb->sk->ip_mc_name)
238 *dev=dev_get(skb->sk->ip_mc_name);
239 #endif 240 if (*dev == NULL)
241 { 242 if(skb->localroute)
243 rt = ip_rt_local(daddr, &optmem, &src);
244 else 245 rt = ip_rt_route(daddr, &optmem, &src);
246 if (rt == NULL)
247 { 248 ip_statistics.IpOutNoRoutes++;
249 return(-ENETUNREACH);
250 } 251
252 *dev = rt->rt_dev;
253 /* 254 * If the frame is from us and going off machine it MUST MUST MUST 255 * have the output device ip address and never the loopback 256 */ 257 if (LOOPBACK(saddr) && !LOOPBACK(daddr))
258 saddr = src;/*rt->rt_dev->pa_addr;*/ 259 raddr = rt->rt_gateway;
260
261 opt = &optmem;
262 } 263 else 264 { 265 /* 266 * We still need the address of the first hop. 267 */ 268 if(skb->localroute)
269 rt = ip_rt_local(daddr, &optmem, &src);
270 else 271 rt = ip_rt_route(daddr, &optmem, &src);
272 /* 273 * If the frame is from us and going off machine it MUST MUST MUST 274 * have the output device ip address and never the loopback 275 */ 276 if (LOOPBACK(saddr) && !LOOPBACK(daddr))
277 saddr = src;/*rt->rt_dev->pa_addr;*/ 278
279 raddr = (rt == NULL) ? 0 : rt->rt_gateway;
280 } 281
282 /* 283 * No source addr so make it our addr 284 */ 285 if (saddr == 0)
286 saddr = src;
287
288 /* 289 * No gateway so aim at the real destination 290 */ 291 if (raddr == 0)
292 raddr = daddr;
293
294 /* 295 * Now build the MAC header. 296 */ 297
298 tmp = ip_send(skb, raddr, len, *dev, saddr);
299 buff += tmp;
300 len -= tmp;
301
302 /* 303 * Book keeping 304 */ 305
306 skb->dev = *dev;
307 skb->saddr = saddr;
308 if (skb->sk)
309 skb->sk->saddr = saddr;
310
311 /* 312 * Now build the IP header. 313 */ 314
315 /* 316 * If we are using IPPROTO_RAW, then we don't need an IP header, since 317 * one is being supplied to us by the user 318 */ 319
320 if(type == IPPROTO_RAW)
321 return (tmp);
322
323 iph = (structiphdr *)buff;
324 iph->version = 4;
325 iph->tos = tos;
326 iph->frag_off = 0;
327 iph->ttl = ttl;
328 iph->daddr = daddr;
329 iph->saddr = saddr;
330 iph->protocol = type;
331 iph->ihl = 5;
332 skb->ip_hdr = iph;
333
334 /* Setup the IP options. */ 335 #ifdef Not_Yet_Avail
336 build_options(iph, opt);
337 #endif 338
339 return(20 + tmp); /* IP header plus MAC header size */ 340 } 341
342
343 staticint 344 do_options(structiphdr *iph, structoptions *opt)
/* */ 345 { 346 unsignedchar *buff;
347 intdone = 0;
348 inti, len = sizeof(structiphdr);
349
350 /* Zero out the options. */ 351 opt->record_route.route_size = 0;
352 opt->loose_route.route_size = 0;
353 opt->strict_route.route_size = 0;
354 opt->tstamp.ptr = 0;
355 opt->security = 0;
356 opt->compartment = 0;
357 opt->handling = 0;
358 opt->stream = 0;
359 opt->tcc = 0;
360 return(0);
361
362 /* Advance the pointer to start at the options. */ 363 buff = (unsignedchar *)(iph + 1);
364
365 /* Now start the processing. */ 366 while (!done && len < iph->ihl*4) switch(*buff) { 367 caseIPOPT_END:
368 done = 1;
369 break;
370 caseIPOPT_NOOP:
371 buff++;
372 len++;
373 break;
374 caseIPOPT_SEC:
375 buff++;
376 if (*buff != 11) return(1);
377 buff++;
378 opt->security = ntohs(*(unsignedshort *)buff);
379 buff += 2;
380 opt->compartment = ntohs(*(unsignedshort *)buff);
381 buff += 2;
382 opt->handling = ntohs(*(unsignedshort *)buff);
383 buff += 2;
384 opt->tcc = ((*buff) << 16) + ntohs(*(unsignedshort *)(buff+1));
385 buff += 3;
386 len += 11;
387 break;
388 caseIPOPT_LSRR:
389 buff++;
390 if ((*buff - 3)% 4 != 0) return(1);
391 len += *buff;
392 opt->loose_route.route_size = (*buff -3)/4;
393 buff++;
394 if (*buff % 4 != 0) return(1);
395 opt->loose_route.pointer = *buff/4 - 1;
396 buff++;
397 buff++;
398 for (i = 0; i < opt->loose_route.route_size; i++) { 399 if(i>=MAX_ROUTE)
400 return(1);
401 opt->loose_route.route[i] = *(unsignedlong *)buff;
402 buff += 4;
403 } 404 break;
405 caseIPOPT_SSRR:
406 buff++;
407 if ((*buff - 3)% 4 != 0) return(1);
408 len += *buff;
409 opt->strict_route.route_size = (*buff -3)/4;
410 buff++;
411 if (*buff % 4 != 0) return(1);
412 opt->strict_route.pointer = *buff/4 - 1;
413 buff++;
414 buff++;
415 for (i = 0; i < opt->strict_route.route_size; i++) { 416 if(i>=MAX_ROUTE)
417 return(1);
418 opt->strict_route.route[i] = *(unsignedlong *)buff;
419 buff += 4;
420 } 421 break;
422 caseIPOPT_RR:
423 buff++;
424 if ((*buff - 3)% 4 != 0) return(1);
425 len += *buff;
426 opt->record_route.route_size = (*buff -3)/4;
427 buff++;
428 if (*buff % 4 != 0) return(1);
429 opt->record_route.pointer = *buff/4 - 1;
430 buff++;
431 buff++;
432 for (i = 0; i < opt->record_route.route_size; i++) { 433 if(i>=MAX_ROUTE)
434 return 1;
435 opt->record_route.route[i] = *(unsignedlong *)buff;
436 buff += 4;
437 } 438 break;
439 caseIPOPT_SID:
440 len += 4;
441 buff +=2;
442 opt->stream = *(unsignedshort *)buff;
443 buff += 2;
444 break;
445 caseIPOPT_TIMESTAMP:
446 buff++;
447 len += *buff;
448 if (*buff % 4 != 0) return(1);
449 opt->tstamp.len = *buff / 4 - 1;
450 buff++;
451 if ((*buff - 1) % 4 != 0) return(1);
452 opt->tstamp.ptr = (*buff-1)/4;
453 buff++;
454 opt->tstamp.x.full_char = *buff;
455 buff++;
456 for (i = 0; i < opt->tstamp.len; i++) { 457 opt->tstamp.data[i] = *(unsignedlong *)buff;
458 buff += 4;
459 } 460 break;
461 default:
462 return(1);
463 } 464
465 if (opt->record_route.route_size == 0) { 466 if (opt->strict_route.route_size != 0) { 467 memcpy(&(opt->record_route), &(opt->strict_route),
468 sizeof(opt->record_route));
469 }elseif (opt->loose_route.route_size != 0) { 470 memcpy(&(opt->record_route), &(opt->loose_route),
471 sizeof(opt->record_route));
472 } 473 } 474
475 if (opt->strict_route.route_size != 0 &&
476 opt->strict_route.route_size != opt->strict_route.pointer) { 477 strict_route(iph, opt);
478 return(0);
479 } 480
481 if (opt->loose_route.route_size != 0 &&
482 opt->loose_route.route_size != opt->loose_route.pointer) { 483 loose_route(iph, opt);
484 return(0);
485 } 486
487 return(0);
488 } 489
490 /* 491 * This is a version of ip_compute_csum() optimized for IP headers, which 492 * always checksum on 4 octet boundaries. 493 */ 494
495 staticinlineunsignedshortip_fast_csum(unsignedchar * buff, intwlen)
/* */ 496 { 497 unsignedlongsum = 0;
498
499 if (wlen)
500 { 501 unsignedlongbogus;
502 __asm__("clc\n"
503 "1:\t"
504 "lodsl\n\t"
505 "adcl %3, %0\n\t"
506 "decl %2\n\t"
507 "jne 1b\n\t"
508 "adcl $0, %0\n\t"
509 "movl %0, %3\n\t"
510 "shrl $16, %3\n\t"
511 "addw %w3, %w0\n\t"
512 "adcw $0, %w0"
513 : "=r" (sum), "=S" (buff), "=r" (wlen), "=a" (bogus)
514 : "0" (sum), "1" (buff), "2" (wlen));
515 } 516 return (~sum) & 0xffff;
517 } 518
519 /* 520 * This routine does all the checksum computations that don't 521 * require anything special (like copying or special headers). 522 */ 523
524 unsignedshortip_compute_csum(unsignedchar * buff, intlen)
/* */ 525 { 526 unsignedlongsum = 0;
527
528 /* Do the first multiple of 4 bytes and convert to 16 bits. */ 529 if (len > 3)
530 { 531 __asm__("clc\n"
532 "1:\t"
533 "lodsl\n\t"
534 "adcl %%eax, %%ebx\n\t"
535 "loop 1b\n\t"
536 "adcl $0, %%ebx\n\t"
537 "movl %%ebx, %%eax\n\t"
538 "shrl $16, %%eax\n\t"
539 "addw %%ax, %%bx\n\t"
540 "adcw $0, %%bx"
541 : "=b" (sum) , "=S" (buff)
542 : "0" (sum), "c" (len >> 2) ,"1" (buff)
543 : "ax", "cx", "si", "bx" );
544 } 545 if (len & 2)
546 { 547 __asm__("lodsw\n\t"
548 "addw %%ax, %%bx\n\t"
549 "adcw $0, %%bx"
550 : "=b" (sum), "=S" (buff)
551 : "0" (sum), "1" (buff)
552 : "bx", "ax", "si");
553 } 554 if (len & 1)
555 { 556 __asm__("lodsb\n\t"
557 "movb $0, %%ah\n\t"
558 "addw %%ax, %%bx\n\t"
559 "adcw $0, %%bx"
560 : "=b" (sum), "=S" (buff)
561 : "0" (sum), "1" (buff)
562 : "bx", "ax", "si");
563 } 564 sum =~sum;
565 return(sum & 0xffff);
566 } 567
568 /* 569 * Check the header of an incoming IP datagram. This version is still used in slhc.c. 570 */ 571
572 intip_csum(structiphdr *iph)
/* */ 573 { 574 returnip_fast_csum((unsignedchar *)iph, iph->ihl);
575 } 576
577 /* 578 * Generate a checksum for an outgoing IP datagram. 579 */ 580
581 voidip_send_check(structiphdr *iph)
/* */ 582 { 583 iph->check = 0;
584 iph->check = ip_fast_csum((unsignedchar *)iph, iph->ihl);
585 } 586
587 /************************ Fragment Handlers From NET2E **********************************/ 588
589
590 /* 591 * This fragment handler is a bit of a heap. On the other hand it works quite 592 * happily and handles things quite well. 593 */ 594
595 staticstructipq *ipqueue = NULL; /* IP fragment queue */ 596
597 /* 598 * Create a new fragment entry. 599 */ 600
601 staticstructipfrag *ip_frag_create(intoffset, intend, structsk_buff *skb, unsignedchar *ptr)
/* */ 602 { 603 structipfrag *fp;
604
605 fp = (structipfrag *) kmalloc(sizeof(structipfrag), GFP_ATOMIC);
606 if (fp == NULL)
607 { 608 printk("IP: frag_create: no memory left !\n");
609 return(NULL);
610 } 611 memset(fp, 0, sizeof(structipfrag));
612
613 /* Fill in the structure. */ 614 fp->offset = offset;
615 fp->end = end;
616 fp->len = end - offset;
617 fp->skb = skb;
618 fp->ptr = ptr;
619
620 return(fp);
621 } 622
623
624 /* 625 * Find the correct entry in the "incomplete datagrams" queue for 626 * this IP datagram, and return the queue entry address if found. 627 */ 628
629 staticstructipq *ip_find(structiphdr *iph)
/* */ 630 { 631 structipq *qp;
632 structipq *qplast;
633
634 cli();
635 qplast = NULL;
636 for(qp = ipqueue; qp != NULL; qplast = qp, qp = qp->next)
637 { 638 if (iph->id== qp->iph->id && iph->saddr == qp->iph->saddr &&
639 iph->daddr == qp->iph->daddr && iph->protocol == qp->iph->protocol)
640 { 641 del_timer(&qp->timer); /* So it doesn't vanish on us. The timer will be reset anyway */ 642 sti();
643 return(qp);
644 } 645 } 646 sti();
647 return(NULL);
648 } 649
650
651 /* 652 * Remove an entry from the "incomplete datagrams" queue, either 653 * because we completed, reassembled and processed it, or because 654 * it timed out. 655 */ 656
657 staticvoidip_free(structipq *qp)
/* */ 658 { 659 structipfrag *fp;
660 structipfrag *xp;
661
662 /* 663 * Stop the timer for this entry. 664 */ 665
666 del_timer(&qp->timer);
667
668 /* Remove this entry from the "incomplete datagrams" queue. */ 669 cli();
670 if (qp->prev == NULL)
671 { 672 ipqueue = qp->next;
673 if (ipqueue != NULL)
674 ipqueue->prev = NULL;
675 } 676 else 677 { 678 qp->prev->next = qp->next;
679 if (qp->next != NULL)
680 qp->next->prev = qp->prev;
681 } 682
683 /* Release all fragment data. */ 684
685 fp = qp->fragments;
686 while (fp != NULL)
687 { 688 xp = fp->next;
689 IS_SKB(fp->skb);
690 kfree_skb(fp->skb,FREE_READ);
691 kfree_s(fp, sizeof(structipfrag));
692 fp = xp;
693 } 694
695 /* Release the MAC header. */ 696 kfree_s(qp->mac, qp->maclen);
697
698 /* Release the IP header. */ 699 kfree_s(qp->iph, qp->ihlen + 8);
700
701 /* Finally, release the queue descriptor itself. */ 702 kfree_s(qp, sizeof(structipq));
703 sti();
704 } 705
706
707 /* 708 * Oops- a fragment queue timed out. Kill it and send an ICMP reply. 709 */ 710
711 staticvoidip_expire(unsignedlongarg)
/* */ 712 { 713 structipq *qp;
714
715 qp = (structipq *)arg;
716
717 /* 718 * Send an ICMP "Fragment Reassembly Timeout" message. 719 */ 720
721 ip_statistics.IpReasmTimeout++;
722 ip_statistics.IpReasmFails++;
723 /* This if is always true... shrug */ 724 if(qp->fragments!=NULL)
725 icmp_send(qp->fragments->skb,ICMP_TIME_EXCEEDED,
726 ICMP_EXC_FRAGTIME, qp->dev);
727
728 /* 729 * Nuke the fragment queue. 730 */ 731 ip_free(qp);
732 } 733
734
735 /* 736 * Add an entry to the 'ipq' queue for a newly received IP datagram. 737 * We will (hopefully :-) receive all other fragments of this datagram 738 * in time, so we just create a queue for this datagram, in which we 739 * will insert the received fragments at their respective positions. 740 */ 741
742 staticstructipq *ip_create(structsk_buff *skb, structiphdr *iph, structdevice *dev)
/* */ 743 { 744 structipq *qp;
745 intmaclen;
746 intihlen;
747
748 qp = (structipq *) kmalloc(sizeof(structipq), GFP_ATOMIC);
749 if (qp == NULL)
750 { 751 printk("IP: create: no memory left !\n");
752 return(NULL);
753 skb->dev = qp->dev;
754 } 755 memset(qp, 0, sizeof(structipq));
756
757 /* 758 * Allocate memory for the MAC header. 759 * 760 * FIXME: We have a maximum MAC address size limit and define 761 * elsewhere. We should use it here and avoid the 3 kmalloc() calls 762 */ 763
764 maclen = ((unsignedlong) iph) - ((unsignedlong) skb->data);
765 qp->mac = (unsignedchar *) kmalloc(maclen, GFP_ATOMIC);
766 if (qp->mac == NULL)
767 { 768 printk("IP: create: no memory left !\n");
769 kfree_s(qp, sizeof(structipq));
770 return(NULL);
771 } 772
773 /* 774 * Allocate memory for the IP header (plus 8 octets for ICMP). 775 */ 776
777 ihlen = (iph->ihl * sizeof(unsignedlong));
778 qp->iph = (structiphdr *) kmalloc(ihlen + 8, GFP_ATOMIC);
779 if (qp->iph == NULL)
780 { 781 printk("IP: create: no memory left !\n");
782 kfree_s(qp->mac, maclen);
783 kfree_s(qp, sizeof(structipq));
784 return(NULL);
785 } 786
787 /* Fill in the structure. */ 788 memcpy(qp->mac, skb->data, maclen);
789 memcpy(qp->iph, iph, ihlen + 8);
790 qp->len = 0;
791 qp->ihlen = ihlen;
792 qp->maclen = maclen;
793 qp->fragments = NULL;
794 qp->dev = dev;
795
796 /* Start a timer for this entry. */ 797 qp->timer.expires = IP_FRAG_TIME; /* about 30 seconds */ 798 qp->timer.data = (unsignedlong) qp; /* pointer to queue */ 799 qp->timer.function = ip_expire; /* expire function */ 800 add_timer(&qp->timer);
801
802 /* Add this entry to the queue. */ 803 qp->prev = NULL;
804 cli();
805 qp->next = ipqueue;
806 if (qp->next != NULL)
807 qp->next->prev = qp;
808 ipqueue = qp;
809 sti();
810 return(qp);
811 } 812
813
814 /* 815 * See if a fragment queue is complete. 816 */ 817
818 staticintip_done(structipq *qp)
/* */ 819 { 820 structipfrag *fp;
821 intoffset;
822
823 /* Only possible if we received the final fragment. */ 824 if (qp->len == 0)
825 return(0);
826
827 /* Check all fragment offsets to see if they connect. */ 828 fp = qp->fragments;
829 offset = 0;
830 while (fp != NULL)
831 { 832 if (fp->offset > offset)
833 return(0); /* fragment(s) missing */ 834 offset = fp->end;
835 fp = fp->next;
836 } 837
838 /* All fragments are present. */ 839 return(1);
840 } 841
842
843 /* 844 * Build a new IP datagram from all its fragments. 845 * 846 * FIXME: We copy here because we lack an effective way of handling lists 847 * of bits on input. Until the new skb data handling is in I'm not going 848 * to touch this with a bargepole. This also causes a 4Kish limit on 849 * packet sizes. 850 */ 851
852 staticstructsk_buff *ip_glue(structipq *qp)
/* */ 853 { 854 structsk_buff *skb;
855 structiphdr *iph;
856 structipfrag *fp;
857 unsignedchar *ptr;
858 intcount, len;
859
860 /* 861 * Allocate a new buffer for the datagram. 862 */ 863
864 len = qp->maclen + qp->ihlen + qp->len;
865
866 if ((skb = alloc_skb(len,GFP_ATOMIC)) == NULL)
867 { 868 ip_statistics.IpReasmFails++;
869 printk("IP: queue_glue: no memory for gluing queue 0x%X\n", (int) qp);
870 ip_free(qp);
871 return(NULL);
872 } 873
874 /* Fill in the basic details. */ 875 skb->len = (len - qp->maclen);
876 skb->h.raw = skb->data;
877 skb->free = 1;
878
879 /* Copy the original MAC and IP headers into the new buffer. */ 880 ptr = (unsignedchar *) skb->h.raw;
881 memcpy(ptr, ((unsignedchar *) qp->mac), qp->maclen);
882 ptr += qp->maclen;
883 memcpy(ptr, ((unsignedchar *) qp->iph), qp->ihlen);
884 ptr += qp->ihlen;
885 skb->h.raw += qp->maclen;
886
887 count = 0;
888
889 /* Copy the data portions of all fragments into the new buffer. */ 890 fp = qp->fragments;
891 while(fp != NULL)
892 { 893 if(count+fp->len > skb->len)
894 { 895 printk("Invalid fragment list: Fragment over size.\n");
896 ip_free(qp);
897 kfree_skb(skb,FREE_WRITE);
898 ip_statistics.IpReasmFails++;
899 returnNULL;
900 } 901 memcpy((ptr + fp->offset), fp->ptr, fp->len);
902 count += fp->len;
903 fp = fp->next;
904 } 905
906 /* We glued together all fragments, so remove the queue entry. */ 907 ip_free(qp);
908
909 /* Done with all fragments. Fixup the new IP header. */ 910 iph = skb->h.iph;
911 iph->frag_off = 0;
912 iph->tot_len = htons((iph->ihl * sizeof(unsignedlong)) + count);
913 skb->ip_hdr = iph;
914
915 ip_statistics.IpReasmOKs++;
916 return(skb);
917 } 918
919
920 /* 921 * Process an incoming IP datagram fragment. 922 */ 923
924 staticstructsk_buff *ip_defrag(structiphdr *iph, structsk_buff *skb, structdevice *dev)
/* */ 925 { 926 structipfrag *prev, *next;
927 structipfrag *tfp;
928 structipq *qp;
929 structsk_buff *skb2;
930 unsignedchar *ptr;
931 intflags, offset;
932 inti, ihl, end;
933
934 ip_statistics.IpReasmReqds++;
935
936 /* Find the entry of this IP datagram in the "incomplete datagrams" queue. */ 937 qp = ip_find(iph);
938
939 /* Is this a non-fragmented datagram? */ 940 offset = ntohs(iph->frag_off);
941 flags = offset & ~IP_OFFSET;
942 offset &= IP_OFFSET;
943 if (((flags & IP_MF) == 0) && (offset == 0))
944 { 945 if (qp != NULL)
946 ip_free(qp); /* Huh? How could this exist?? */ 947 return(skb);
948 } 949
950 offset <<= 3; /* offset is in 8-byte chunks */ 951
952 /* 953 * If the queue already existed, keep restarting its timer as long 954 * as we still are receiving fragments. Otherwise, create a fresh 955 * queue entry. 956 */ 957
958 if (qp != NULL)
959 { 960 del_timer(&qp->timer);
961 qp->timer.expires = IP_FRAG_TIME; /* about 30 seconds */ 962 qp->timer.data = (unsignedlong) qp; /* pointer to queue */ 963 qp->timer.function = ip_expire; /* expire function */ 964 add_timer(&qp->timer);
965 } 966 else 967 { 968 /* 969 * If we failed to create it, then discard the frame 970 */ 971 if ((qp = ip_create(skb, iph, dev)) == NULL)
972 { 973 skb->sk = NULL;
974 kfree_skb(skb, FREE_READ);
975 ip_statistics.IpReasmFails++;
976 returnNULL;
977 } 978 } 979
980 /* 981 * Determine the position of this fragment. 982 */ 983
984 ihl = (iph->ihl * sizeof(unsignedlong));
985 end = offset + ntohs(iph->tot_len) - ihl;
986
987 /* 988 * Point into the IP datagram 'data' part. 989 */ 990
991 ptr = skb->data + dev->hard_header_len + ihl;
992
993 /* 994 * Is this the final fragment? 995 */ 996
997 if ((flags & IP_MF) == 0)
998 qp->len = end;
999
1000 /*1001 * Find out which fragments are in front and at the back of us1002 * in the chain of fragments so far. We must know where to put1003 * this fragment, right?1004 */1005
1006 prev = NULL;
1007 for(next = qp->fragments; next != NULL; next = next->next)
1008 {1009 if (next->offset > offset)
1010 break; /* bingo! */1011 prev = next;
1012 }1013
1014 /*1015 * We found where to put this one.1016 * Check for overlap with preceding fragment, and, if needed,1017 * align things so that any overlaps are eliminated.1018 */1019 if (prev != NULL && offset < prev->end)
1020 {1021 i = prev->end - offset;
1022 offset += i; /* ptr into datagram */1023 ptr += i; /* ptr into fragment data */1024 }1025
1026 /*1027 * Look for overlap with succeeding segments.1028 * If we can merge fragments, do it.1029 */1030
1031 for(; next != NULL; next = tfp)
1032 {1033 tfp = next->next;
1034 if (next->offset >= end)
1035 break; /* no overlaps at all */1036
1037 i = end - next->offset; /* overlap is 'i' bytes */1038 next->len -= i; /* so reduce size of */1039 next->offset += i; /* next fragment */1040 next->ptr += i;
1041
1042 /*1043 * If we get a frag size of <= 0, remove it and the packet1044 * that it goes with.1045 */1046 if (next->len <= 0)
1047 {1048 if (next->prev != NULL)
1049 next->prev->next = next->next;
1050 else1051 qp->fragments = next->next;
1052
1053 if (tfp->next != NULL)
1054 next->next->prev = next->prev;
1055
1056 kfree_skb(next->skb,FREE_READ);
1057 kfree_s(next, sizeof(structipfrag));
1058 }1059 }1060
1061 /*1062 * Insert this fragment in the chain of fragments.1063 */1064
1065 tfp = NULL;
1066 tfp = ip_frag_create(offset, end, skb, ptr);
1067
1068 /*1069 * No memory to save the fragment - so throw the lot1070 */1071
1072 if (!tfp)
1073 {1074 skb->sk = NULL;
1075 kfree_skb(skb, FREE_READ);
1076 returnNULL;
1077 }1078 tfp->prev = prev;
1079 tfp->next = next;
1080 if (prev != NULL)
1081 prev->next = tfp;
1082 else1083 qp->fragments = tfp;
1084
1085 if (next != NULL)
1086 next->prev = tfp;
1087
1088 /*1089 * OK, so we inserted this new fragment into the chain.1090 * Check if we now have a full IP datagram which we can1091 * bump up to the IP layer...1092 */1093
1094 if (ip_done(qp))
1095 {1096 skb2 = ip_glue(qp); /* glue together the fragments */1097 return(skb2);
1098 }1099 return(NULL);
1100 }1101
1102
1103 /*1104 * This IP datagram is too large to be sent in one piece. Break it up into1105 * smaller pieces (each of size equal to the MAC header plus IP header plus1106 * a block of the data of the original IP data part) that will yet fit in a1107 * single device frame, and queue such a frame for sending by calling the1108 * ip_queue_xmit(). Note that this is recursion, and bad things will happen1109 * if this function causes a loop...1110 *1111 * Yes this is inefficient, feel free to submit a quicker one.1112 *1113 * **Protocol Violation**1114 * We copy all the options to each fragment. !FIXME!1115 */1116 voidip_fragment(structsock *sk, structsk_buff *skb, structdevice *dev, intis_frag)
/* */1117 {1118 structiphdr *iph;
1119 unsignedchar *raw;
1120 unsignedchar *ptr;
1121 structsk_buff *skb2;
1122 intleft, mtu, hlen, len;
1123 intoffset;
1124 unsignedlongflags;
1125
1126 /*1127 * Point into the IP datagram header.1128 */1129
1130 raw = skb->data;
1131 iph = (structiphdr *) (raw + dev->hard_header_len);
1132
1133 skb->ip_hdr = iph;
1134
1135 /*1136 * Setup starting values.1137 */1138
1139 hlen = (iph->ihl * sizeof(unsignedlong));
1140 left = ntohs(iph->tot_len) - hlen; /* Space per frame */1141 hlen += dev->hard_header_len; /* Total header size */1142 mtu = (dev->mtu - hlen); /* Size of data space */1143 ptr = (raw + hlen); /* Where to start from */1144
1145 /*1146 * Check for any "DF" flag. [DF means do not fragment]1147 */1148
1149 if (ntohs(iph->frag_off) & IP_DF)
1150 {1151 ip_statistics.IpFragFails++;
1152 icmp_send(skb,ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, dev);
1153 return;
1154 }1155
1156 /*1157 * The protocol doesn't seem to say what to do in the case that the1158 * frame + options doesn't fit the mtu. As it used to fall down dead1159 * in this case we were fortunate it didn't happen1160 */1161
1162 if(mtu<8)
1163 {1164 /* It's wrong but its better than nothing */1165 icmp_send(skb,ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED,dev);
1166 ip_statistics.IpFragFails++;
1167 return;
1168 }1169
1170 /*1171 * Fragment the datagram.1172 */1173
1174 /*1175 * The initial offset is 0 for a complete frame. When1176 * fragmenting fragments its wherever this one starts.1177 */1178
1179 if (is_frag & 2)
1180 offset = (ntohs(iph->frag_off) & 0x1fff) << 3;
1181 else1182 offset = 0;
1183
1184
1185 /*1186 * Keep copying data until we run out.1187 */1188
1189 while(left > 0)
1190 {1191 len = left;
1192 /* IF: it doesn't fit, use 'mtu' - the data space left */1193 if (len > mtu)
1194 len = mtu;
1195 /* IF: we are not sending upto and including the packet end1196 then align the next start on an eight byte boundary */1197 if (len < left)
1198 {1199 len/=8;
1200 len*=8;
1201 }1202 /*1203 * Allocate buffer.1204 */1205
1206 if ((skb2 = alloc_skb(len + hlen,GFP_ATOMIC)) == NULL)
1207 {1208 printk("IP: frag: no memory for new fragment!\n");
1209 ip_statistics.IpFragFails++;
1210 return;
1211 }1212
1213 /*1214 * Set up data on packet1215 */1216
1217 skb2->arp = skb->arp;
1218 if(skb->free==0)
1219 printk("IP fragmenter: BUG free!=1 in fragmenter\n");
1220 skb2->free = 1;
1221 skb2->len = len + hlen;
1222 skb2->h.raw=(char *) skb2->data;
1223 /*1224 * Charge the memory for the fragment to any owner1225 * it might possess1226 */1227
1228 save_flags(flags);
1229 if (sk)
1230 {1231 cli();
1232 sk->wmem_alloc += skb2->mem_len;
1233 skb2->sk=sk;
1234 }1235 restore_flags(flags);
1236 skb2->raddr = skb->raddr; /* For rebuild_header - must be here */1237
1238 /*1239 * Copy the packet header into the new buffer.1240 */1241
1242 memcpy(skb2->h.raw, raw, hlen);
1243
1244 /*1245 * Copy a block of the IP datagram.1246 */1247 memcpy(skb2->h.raw + hlen, ptr, len);
1248 left -= len;
1249
1250 skb2->h.raw+=dev->hard_header_len;
1251
1252 /*1253 * Fill in the new header fields.1254 */1255 iph = (structiphdr *)(skb2->h.raw/*+dev->hard_header_len*/);
1256 iph->frag_off = htons((offset >> 3));
1257 /*1258 * Added AC : If we are fragmenting a fragment thats not the1259 * last fragment then keep MF on each bit1260 */1261 if (left > 0 || (is_frag & 1))
1262 iph->frag_off |= htons(IP_MF);
1263 ptr += len;
1264 offset += len;
1265
1266 /*1267 * Put this fragment into the sending queue.1268 */1269
1270 ip_statistics.IpFragCreates++;
1271
1272 ip_queue_xmit(sk, dev, skb2, 2);
1273 }1274 ip_statistics.IpFragOKs++;
1275 }1276
1277
1278
1279 #ifdefCONFIG_IP_FORWARD1280
1281 /*1282 * Forward an IP datagram to its next destination.1283 */1284
1285 staticvoidip_forward(structsk_buff *skb, structdevice *dev, intis_frag)
/* */1286 {1287 structdevice *dev2; /* Output device */1288 structiphdr *iph; /* Our header */1289 structsk_buff *skb2; /* Output packet */1290 structrtable *rt; /* Route we use */1291 unsignedchar *ptr; /* Data pointer */1292 unsignedlongraddr; /* Router IP address */1293
1294 /* 1295 * See if we are allowed to forward this.1296 */1297
1298 #ifdefCONFIG_IP_FIREWALL1299 if(!ip_fw_chk(skb->h.iph, ip_fw_fwd_chain))
1300 {1301 return;
1302 }1303 #endif1304 /*1305 * According to the RFC, we must first decrease the TTL field. If1306 * that reaches zero, we must reply an ICMP control message telling1307 * that the packet's lifetime expired.1308 *1309 * Exception:1310 * We may not generate an ICMP for an ICMP. icmp_send does the1311 * enforcement of this so we can forget it here. It is however1312 * sometimes VERY important.1313 */1314
1315 iph = skb->h.iph;
1316 iph->ttl--;
1317 if (iph->ttl <= 0)
1318 {1319 /* Tell the sender its packet died... */1320 icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, dev);
1321 return;
1322 }1323
1324 /*1325 * Re-compute the IP header checksum.1326 * This is inefficient. We know what has happened to the header1327 * and could thus adjust the checksum as Phil Karn does in KA9Q1328 */1329
1330 ip_send_check(iph);
1331
1332 /*1333 * OK, the packet is still valid. Fetch its destination address,1334 * and give it to the IP sender for further processing.1335 */1336
1337 rt = ip_rt_route(iph->daddr, NULL, NULL);
1338 if (rt == NULL)
1339 {1340 /*1341 * Tell the sender its packet cannot be delivered. Again1342 * ICMP is screened later.1343 */1344 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_UNREACH, dev);
1345 return;
1346 }1347
1348
1349 /*1350 * Gosh. Not only is the packet valid; we even know how to1351 * forward it onto its final destination. Can we say this1352 * is being plain lucky?1353 * If the router told us that there is no GW, use the dest.1354 * IP address itself- we seem to be connected directly...1355 */1356
1357 raddr = rt->rt_gateway;
1358
1359 if (raddr != 0)
1360 {1361 /*1362 * There is a gateway so find the correct route for it.1363 * Gateways cannot in turn be gatewayed.1364 */1365 rt = ip_rt_route(raddr, NULL, NULL);
1366 if (rt == NULL)
1367 {1368 /*1369 * Tell the sender its packet cannot be delivered...1370 */1371 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, dev);
1372 return;
1373 }1374 if (rt->rt_gateway != 0)
1375 raddr = rt->rt_gateway;
1376 }1377 else1378 raddr = iph->daddr;
1379
1380 /*1381 * Having picked a route we can now send the frame out.1382 */1383
1384 dev2 = rt->rt_dev;
1385
1386 /*1387 * In IP you never forward a frame on the interface that it arrived1388 * upon. We should generate an ICMP HOST REDIRECT giving the route1389 * we calculated.1390 * For now just dropping the packet is an acceptable compromise.1391 */1392
1393 if (dev == dev2)
1394 return;
1395
1396 /*1397 * We now allocate a new buffer, and copy the datagram into it.1398 * If the indicated interface is up and running, kick it.1399 */1400
1401 if (dev2->flags & IFF_UP)
1402 {1403
1404 /*1405 * Current design decrees we copy the packet. For identical header1406 * lengths we could avoid it. The new skb code will let us push1407 * data so the problem goes away then.1408 */1409
1410 skb2 = alloc_skb(dev2->hard_header_len + skb->len, GFP_ATOMIC);
1411 /*1412 * This is rare and since IP is tolerant of network failures1413 * quite harmless.1414 */1415 if (skb2 == NULL)
1416 {1417 printk("\nIP: No memory available for IP forward\n");
1418 return;
1419 }1420 ptr = skb2->data;
1421 skb2->free = 1;
1422 skb2->len = skb->len + dev2->hard_header_len;
1423 skb2->h.raw = ptr;
1424
1425 /*1426 * Copy the packet data into the new buffer.1427 */1428 memcpy(ptr + dev2->hard_header_len, skb->h.raw, skb->len);
1429
1430 /* Now build the MAC header. */1431 (void) ip_send(skb2, raddr, skb->len, dev2, dev2->pa_addr);
1432
1433 ip_statistics.IpForwDatagrams++;
1434
1435 /*1436 * See if it needs fragmenting. Note in ip_rcv we tagged1437 * the fragment type. This must be right so that1438 * the fragmenter does the right thing.1439 */1440
1441 if(skb2->len > dev2->mtu + dev2->hard_header_len)
1442 {1443 ip_fragment(NULL,skb2,dev2, is_frag);
1444 kfree_skb(skb2,FREE_WRITE);
1445 }1446 else1447 {1448 #ifdefCONFIG_IP_ACCT1449 /*1450 * Count mapping we shortcut1451 */1452
1453 ip_acct_cnt(iph,ip_acct_chain,1);
1454 #endif1455
1456 /*1457 * Map service types to priority. We lie about1458 * throughput being low priority, but its a good1459 * choice to help improve general usage.1460 */1461 if(iph->tos & IPTOS_LOWDELAY)
1462 dev_queue_xmit(skb2, dev2, SOPRI_INTERACTIVE);
1463 elseif(iph->tos & IPTOS_THROUGHPUT)
1464 dev_queue_xmit(skb2, dev2, SOPRI_BACKGROUND);
1465 else1466 dev_queue_xmit(skb2, dev2, SOPRI_NORMAL);
1467 }1468 }1469 }1470
1471
1472 #endif1473
1474 /*1475 * This function receives all incoming IP datagrams.1476 */1477
1478 intip_rcv(structsk_buff *skb, structdevice *dev, structpacket_type *pt)
/* */1479 {1480 structiphdr *iph = skb->h.iph;
1481 structsock *raw_sk=NULL;
1482 unsignedcharhash;
1483 unsignedcharflag = 0;
1484 unsignedcharopts_p = 0; /* Set iff the packet has options. */1485 structinet_protocol *ipprot;
1486 staticstructoptionsopt; /* since we don't use these yet, and they1487 take up stack space. */1488 intbrd=IS_MYADDR;
1489 intis_frag=0;
1490
1491 ip_statistics.IpInReceives++;
1492
1493 /*1494 * Tag the ip header of this packet so we can find it1495 */1496
1497 skb->ip_hdr = iph;
1498
1499 /*1500 * Is the datagram acceptable?1501 *1502 * 1. Length at least the size of an ip header1503 * 2. Version of 41504 * 3. Checksums correctly. [Speed optimisation for later, skip loopback checksums]1505 * (4. We ought to check for IP multicast addresses and undefined types.. does this matter ?)1506 */1507
1508 if (skb->len<sizeof(structiphdr) || iph->ihl<5 || iph->version != 4 || ip_fast_csum((unsignedchar *)iph, iph->ihl) !=0)
1509 {1510 ip_statistics.IpInHdrErrors++;
1511 kfree_skb(skb, FREE_WRITE);
1512 return(0);
1513 }1514
1515 /*1516 * See if the firewall wants to dispose of the packet. 1517 */1518
1519 #ifdefCONFIG_IP_FIREWALL1520
1521 if(!LOOPBACK(iph->daddr) && !ip_fw_chk(iph,ip_fw_blk_chain))
1522 {1523 kfree_skb(skb, FREE_WRITE);
1524 return 0;
1525 }1526
1527 #endif1528
1529 /*1530 * Our transport medium may have padded the buffer out. Now we know it1531 * is IP we can trim to the true length of the frame.1532 */1533
1534 skb->len=ntohs(iph->tot_len);
1535
1536 /*1537 * Next analyse the packet for options. Studies show under one packet in1538 * a thousand have options....1539 */1540
1541 if (iph->ihl != 5)
1542 {/* Fast path for the typical optionless IP packet. */1543 memset((char *) &opt, 0, sizeof(opt));
1544 if (do_options(iph, &opt) != 0)
1545 return 0;
1546 opts_p = 1;
1547 }1548
1549 /*1550 * Remember if the frame is fragmented.1551 */1552
1553 if(iph->frag_off)
1554 {1555 if (iph->frag_off & 0x0020)
1556 is_frag|=1;
1557 /*1558 * Last fragment ?1559 */1560
1561 if (ntohs(iph->frag_off) & 0x1fff)
1562 is_frag|=2;
1563 }1564
1565 /*1566 * Do any IP forwarding required. chk_addr() is expensive -- avoid it someday.1567 *1568 * This is inefficient. While finding out if it is for us we could also compute1569 * the routing table entry. This is where the great unified cache theory comes1570 * in as and when someone implements it1571 *1572 * For most hosts over 99% of packets match the first conditional1573 * and don't go via ip_chk_addr. Note: brd is set to IS_MYADDR at1574 * function entry.1575 */1576
1577 if ( iph->daddr != skb->dev->pa_addr && (brd = ip_chk_addr(iph->daddr)) == 0)
1578 {1579 /*1580 * Don't forward multicast or broadcast frames.1581 */1582
1583 if(skb->pkt_type!=PACKET_HOST || brd==IS_BROADCAST)
1584 {1585 kfree_skb(skb,FREE_WRITE);
1586 return 0;
1587 }1588
1589 /*1590 * The packet is for another target. Forward the frame1591 */1592
1593 #ifdefCONFIG_IP_FORWARD1594 ip_forward(skb, dev, is_frag);
1595 #else1596 /* printk("Machine %lx tried to use us as a forwarder to %lx but we have forwarding disabled!\n",1597 iph->saddr,iph->daddr);*/1598 ip_statistics.IpInAddrErrors++;
1599 #endif1600 /*1601 * The forwarder is inefficient and copies the packet. We1602 * free the original now.1603 */1604
1605 kfree_skb(skb, FREE_WRITE);
1606 return(0);
1607 }1608
1609 #ifdefCONFIG_IP_MULTICAST1610
1611 if(brd==IS_MULTICAST && iph->daddr!=IGMP_ALL_HOSTS && !(dev->flags&IFF_LOOPBACK))
1612 {1613 /*1614 * Check it is for one of our groups1615 */1616 structip_mc_list *ip_mc=dev->ip_mc_list;
1617 do1618 {1619 if(ip_mc==NULL)
1620 {1621 kfree_skb(skb, FREE_WRITE);
1622 return 0;
1623 }1624 if(ip_mc->multiaddr==iph->daddr)
1625 break;
1626 ip_mc=ip_mc->next;
1627 }1628 while(1);
1629 }1630 #endif1631 /*1632 * Account for the packet1633 */1634
1635 #ifdefCONFIG_IP_ACCT1636 ip_acct_cnt(iph,ip_acct_chain,1);
1637 #endif1638
1639 /*1640 * Reassemble IP fragments.1641 */1642
1643 if(is_frag)
1644 {1645 /* Defragment. Obtain the complete packet if there is one */1646 skb=ip_defrag(iph,skb,dev);
1647 if(skb==NULL)
1648 return 0;
1649 skb->dev = dev;
1650 iph=skb->h.iph;
1651 }1652
1653
1654
1655 /*1656 * Point into the IP datagram, just past the header.1657 */1658
1659 skb->ip_hdr = iph;
1660 skb->h.raw += iph->ihl*4;
1661
1662 /*1663 * Deliver to raw sockets. This is fun as to avoid copies we want to make no surplus copies.1664 */1665
1666 hash = iph->protocol & (SOCK_ARRAY_SIZE-1);
1667
1668 /* If there maybe a raw socket we must check - if not we don't care less */1669 if((raw_sk=raw_prot.sock_array[hash])!=NULL)
1670 {1671 structsock *sknext=NULL;
1672 structsk_buff *skb1;
1673 raw_sk=get_sock_raw(raw_sk, hash, iph->saddr, iph->daddr);
1674 if(raw_sk) /* Any raw sockets */1675 {1676 do1677 {1678 /* Find the next */1679 sknext=get_sock_raw(raw_sk->next, hash, iph->saddr, iph->daddr);
1680 if(sknext)
1681 skb1=skb_clone(skb, GFP_ATOMIC);
1682 else1683 break; /* One pending raw socket left */1684 if(skb1)
1685 raw_rcv(raw_sk, skb1, dev, iph->saddr,iph->daddr);
1686 raw_sk=sknext;
1687 }1688 while(raw_sk!=NULL);
1689 /* Here either raw_sk is the last raw socket, or NULL if none */1690 /* We deliver to the last raw socket AFTER the protocol checks as it avoids a surplus copy */1691 }1692 }1693
1694 /*1695 * skb->h.raw now points at the protocol beyond the IP header.1696 */1697
1698 hash = iph->protocol & (MAX_INET_PROTOS -1);
1699 for (ipprot = (structinet_protocol *)inet_protos[hash];ipprot != NULL;ipprot=(structinet_protocol *)ipprot->next)
1700 {1701 structsk_buff *skb2;
1702
1703 if (ipprot->protocol != iph->protocol)
1704 continue;
1705 /*1706 * See if we need to make a copy of it. This will1707 * only be set if more than one protocol wants it.1708 * and then not for the last one. If there is a pending1709 * raw delivery wait for that1710 */1711 if (ipprot->copy || raw_sk)
1712 {1713 skb2 = skb_clone(skb, GFP_ATOMIC);
1714 if(skb2==NULL)
1715 continue;
1716 }1717 else1718 {1719 skb2 = skb;
1720 }1721 flag = 1;
1722
1723 /*1724 * Pass on the datagram to each protocol that wants it,1725 * based on the datagram protocol. We should really1726 * check the protocol handler's return values here...1727 */1728 ipprot->handler(skb2, dev, opts_p ? &opt : 0, iph->daddr,
1729 (ntohs(iph->tot_len) - (iph->ihl * 4)),
1730 iph->saddr, 0, ipprot);
1731
1732 }1733
1734 /*1735 * All protocols checked.1736 * If this packet was a broadcast, we may *not* reply to it, since that1737 * causes (proven, grin) ARP storms and a leakage of memory (i.e. all1738 * ICMP reply messages get queued up for transmission...)1739 */1740
1741 if(raw_sk!=NULL) /* Shift to last raw user */1742 raw_rcv(raw_sk, skb, dev, iph->saddr, iph->daddr);
1743 elseif (!flag) /* Free and report errors */1744 {1745 if (brd != IS_BROADCAST && brd!=IS_MULTICAST)
1746 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, dev);
1747 kfree_skb(skb, FREE_WRITE);
1748 }1749
1750 return(0);
1751 }1752
1753 /*1754 * Loop a packet back to the sender.1755 */1756
1757 staticvoidip_loopback(structdevice *old_dev, structsk_buff *skb)
/* */1758 {1759 externstructdeviceloopback_dev;
1760 structdevice *dev=&loopback_dev;
1761 intlen=skb->len-old_dev->hard_header_len;
1762 structsk_buff *newskb=alloc_skb(len+dev->hard_header_len, GFP_ATOMIC);
1763
1764 if(newskb==NULL)
1765 return;
1766
1767 newskb->link3=NULL;
1768 newskb->sk=NULL;
1769 newskb->dev=dev;
1770 newskb->saddr=skb->saddr;
1771 newskb->daddr=skb->daddr;
1772 newskb->raddr=skb->raddr;
1773 newskb->free=1;
1774 newskb->lock=0;
1775 newskb->users=0;
1776 newskb->pkt_type=skb->pkt_type;
1777 newskb->len=len+dev->hard_header_len;
1778
1779
1780 newskb->ip_hdr=(structiphdr *)(newskb->data+ip_send(newskb, skb->ip_hdr->daddr, len, dev, skb->ip_hdr->saddr));
1781 memcpy(newskb->ip_hdr,skb->ip_hdr,len);
1782
1783 /* Recurse. The device check against IFF_LOOPBACK will stop infinite recursion */1784
1785 /*printk("Loopback output queued [%lX to %lX].\n", newskb->ip_hdr->saddr,newskb->ip_hdr->daddr);*/1786 ip_queue_xmit(NULL, dev, newskb, 1);
1787 }1788
1789
1790 /*1791 * Queues a packet to be sent, and starts the transmitter1792 * if necessary. if free = 1 then we free the block after1793 * transmit, otherwise we don't. If free==2 we not only1794 * free the block but also don't assign a new ip seq number.1795 * This routine also needs to put in the total length,1796 * and compute the checksum1797 */1798
1799 voidip_queue_xmit(structsock *sk, structdevice *dev,
/* */1800 structsk_buff *skb, intfree)
1801 {1802 structiphdr *iph;
1803 unsignedchar *ptr;
1804
1805 /* Sanity check */1806 if (dev == NULL)
1807 {1808 printk("IP: ip_queue_xmit dev = NULL\n");
1809 return;
1810 }1811
1812 IS_SKB(skb);
1813
1814 /*1815 * Do some book-keeping in the packet for later1816 */1817
1818
1819 skb->dev = dev;
1820 skb->when = jiffies;
1821
1822 /*1823 * Find the IP header and set the length. This is bad1824 * but once we get the skb data handling code in the1825 * hardware will push its header sensibly and we will1826 * set skb->ip_hdr to avoid this mess and the fixed1827 * header length problem1828 */1829
1830 ptr = skb->data;
1831 ptr += dev->hard_header_len;
1832 iph = (structiphdr *)ptr;
1833 skb->ip_hdr = iph;
1834 iph->tot_len = ntohs(skb->len-dev->hard_header_len);
1835
1836 /*1837 * No reassigning numbers to fragments...1838 */1839
1840 if(free!=2)
1841 iph->id = htons(ip_id_count++);
1842 else1843 free=1;
1844
1845 /* All buffers without an owner socket get freed */1846 if (sk == NULL)
1847 free = 1;
1848
1849 skb->free = free;
1850
1851 /*1852 * Do we need to fragment. Again this is inefficient.1853 * We need to somehow lock the original buffer and use1854 * bits of it.1855 */1856
1857 if(skb->len > dev->mtu + dev->hard_header_len)
1858 {1859 ip_fragment(sk,skb,dev,0);
1860 IS_SKB(skb);
1861 kfree_skb(skb,FREE_WRITE);
1862 return;
1863 }1864
1865 /*1866 * Add an IP checksum1867 */1868
1869 ip_send_check(iph);
1870
1871 /*1872 * Print the frame when debugging1873 */1874
1875 /*1876 * More debugging. You cannot queue a packet already on a list1877 * Spot this and moan loudly.1878 */1879 if (skb->next != NULL)
1880 {1881 printk("ip_queue_xmit: next != NULL\n");
1882 skb_unlink(skb);
1883 }1884
1885 /*1886 * If a sender wishes the packet to remain unfreed1887 * we add it to his send queue. This arguably belongs1888 * in the TCP level since nobody else uses it. BUT1889 * remember IPng might change all the rules.1890 */1891
1892 if (!free)
1893 {1894 unsignedlongflags;
1895 /* The socket now has more outstanding blocks */1896
1897 sk->packets_out++;
1898
1899 /* Protect the list for a moment */1900 save_flags(flags);
1901 cli();
1902
1903 if (skb->link3 != NULL)
1904 {1905 printk("ip.c: link3 != NULL\n");
1906 skb->link3 = NULL;
1907 }1908 if (sk->send_head == NULL)
1909 {1910 sk->send_tail = skb;
1911 sk->send_head = skb;
1912 }1913 else1914 {1915 sk->send_tail->link3 = skb;
1916 sk->send_tail = skb;
1917 }1918 /* skb->link3 is NULL */1919
1920 /* Interrupt restore */1921 restore_flags(flags);
1922 }1923 else1924 /* Remember who owns the buffer */1925 skb->sk = sk;
1926
1927 /*1928 * If the indicated interface is up and running, send the packet.1929 */1930
1931 ip_statistics.IpOutRequests++;
1932 #ifdefCONFIG_IP_ACCT1933 ip_acct_cnt(iph,ip_acct_chain,1);
1934 #endif1935
1936 #ifdefCONFIG_IP_MULTICAST1937
1938 /*1939 * Multicasts are looped back for other local users1940 */1941
1942 if (MULTICAST(iph->daddr) && !(dev->flags&IFF_LOOPBACK))
1943 {1944 if(sk==NULL || sk->ip_mc_loop)
1945 {1946 if(iph->daddr==IGMP_ALL_HOSTS)
1947 ip_loopback(dev,skb);
1948 else1949 {1950 structip_mc_list *imc=dev->ip_mc_list;
1951 while(imc!=NULL)
1952 {1953 if(imc->multiaddr==iph->daddr)
1954 {1955 ip_loopback(dev,skb);
1956 break;
1957 }1958 imc=imc->next;
1959 }1960 }1961 }1962 /* Multicasts with ttl 0 must not go beyond the host */1963
1964 if(skb->ip_hdr->ttl==0)
1965 {1966 kfree_skb(skb, FREE_READ);
1967 return;
1968 }1969 }1970 #endif1971 if((dev->flags&IFF_BROADCAST) && iph->daddr==dev->pa_brdaddr && !(dev->flags&IFF_LOOPBACK))
1972 ip_loopback(dev,skb);
1973
1974 if (dev->flags & IFF_UP)
1975 {1976 /*1977 * If we have an owner use its priority setting,1978 * otherwise use NORMAL1979 */1980
1981 if (sk != NULL)
1982 {1983 dev_queue_xmit(skb, dev, sk->priority);
1984 }1985 else1986 {1987 dev_queue_xmit(skb, dev, SOPRI_NORMAL);
1988 }1989 }1990 else1991 {1992 ip_statistics.IpOutDiscards++;
1993 if (free)
1994 kfree_skb(skb, FREE_WRITE);
1995 }1996 }1997
1998
1999
2000 #ifdefCONFIG_IP_MULTICAST2001
2002 /*2003 * Write an multicast group list table for the IGMP daemon to2004 * read.2005 */2006
2007 intip_mc_procinfo(char *buffer, char **start, off_toffset, intlength)
/* */2008 {2009 off_tpos=0, begin=0;
2010 structip_mc_list *im;
2011 unsignedlongflags;
2012 intlen=0;
2013 structdevice *dev;
2014
2015 len=sprintf(buffer,"Device : Count\tGroup Users Timer\n");
2016 save_flags(flags);
2017 cli();
2018
2019 for(dev = dev_base; dev; dev = dev->next)
2020 {2021 if((dev->flags&IFF_UP)&&(dev->flags&IFF_MULTICAST))
2022 {2023 len+=sprintf(buffer+len,"%-10s: %5d\n",
2024 dev->name, dev->mc_count);
2025 for(im = dev->ip_mc_list; im; im = im->next)
2026 {2027 len+=sprintf(buffer+len,
2028 "\t\t\t%08lX %5d %d:%08lX\n",
2029 im->multiaddr, im->users,
2030 im->tm_running, im->timer.expires);
2031 pos=begin+len;
2032 if(pos<offset)
2033 {2034 len=0;
2035 begin=pos;
2036 }2037 if(pos>offset+length)
2038 break;
2039 }2040 }2041 }2042 restore_flags(flags);
2043 *start=buffer+(offset-begin);
2044 len-=(offset-begin);
2045 if(len>length)
2046 len=length;
2047 returnlen;
2048 }2049
2050
2051 #endif2052 /*2053 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on2054 * an IP socket.2055 *2056 * We implement IP_TOS (type of service), IP_TTL (time to live).2057 *2058 * Next release we will sort out IP_OPTIONS since for some people are kind of important.2059 */2060
2061 intip_setsockopt(structsock *sk, intlevel, intoptname, char *optval, intoptlen)
/* */2062 {2063 intval,err;
2064 #ifdefined(CONFIG_IP_FIREWALL) || defined(CONFIG_IP_ACCT)
2065 structip_fwtmp_fw;
2066 #endif2067 if (optval == NULL)
2068 return(-EINVAL);
2069
2070 err=verify_area(VERIFY_READ, optval, sizeof(int));
2071 if(err)
2072 returnerr;
2073
2074 val = get_fs_long((unsignedlong *)optval);
2075
2076 if(level!=SOL_IP)
2077 return -EOPNOTSUPP;
2078
2079 #ifdefCONFIG_IP_MULTICAST2080 if(optname==IP_MULTICAST_TTL)
2081 {2082 unsignedcharucval;
2083 ucval=get_fs_byte((unsignedchar *)optval);
2084 printk("MC TTL %d\n", ucval);
2085 if(ucval<1||ucval>255)
2086 return -EINVAL;
2087 sk->ip_mc_ttl=(int)ucval;
2088 return 0;
2089 }2090 #endif2091
2092 switch(optname)
2093 {2094 caseIP_TOS:
2095 if(val<0||val>255)
2096 return -EINVAL;
2097 sk->ip_tos=val;
2098 if(val==IPTOS_LOWDELAY)
2099 sk->priority=SOPRI_INTERACTIVE;
2100 if(val==IPTOS_THROUGHPUT)
2101 sk->priority=SOPRI_BACKGROUND;
2102 return 0;
2103 caseIP_TTL:
2104 if(val<1||val>255)
2105 return -EINVAL;
2106 sk->ip_ttl=val;
2107 return 0;
2108 #ifdefCONFIG_IP_MULTICAST2109 #ifdef GCC_WORKS
2110 caseIP_MULTICAST_TTL:
2111 {2112 unsignedcharucval;
2113
2114 ucval=get_fs_byte((unsignedchar *)optval);
2115 printk("MC TTL %d\n", ucval);
2116 if(ucval<1||ucval>255)
2117 return -EINVAL;
2118 sk->ip_mc_ttl=(int)ucval;
2119 return 0;
2120 }2121 #endif2122 caseIP_MULTICAST_LOOP:
2123 {2124 unsignedcharucval;
2125
2126 ucval=get_fs_byte((unsignedchar *)optval);
2127 if(ucval!=0 && ucval!=1)
2128 return -EINVAL;
2129 sk->ip_mc_loop=(int)ucval;
2130 return 0;
2131 }2132 caseIP_MULTICAST_IF:
2133 {2134 /* Not fully tested */2135 structin_addraddr;
2136 structdevice *dev=NULL;
2137
2138 /*2139 * Check the arguments are allowable2140 */2141
2142 err=verify_area(VERIFY_READ, optval, sizeof(addr));
2143 if(err)
2144 returnerr;
2145
2146 memcpy_fromfs(&addr,optval,sizeof(addr));
2147
2148 printk("MC bind %s\n", in_ntoa(addr.s_addr));
2149
2150 /*2151 * What address has been requested2152 */2153
2154 if(addr.s_addr==INADDR_ANY) /* Default */2155 {2156 sk->ip_mc_name[0]=0;
2157 return 0;
2158 }2159
2160 /*2161 * Find the device2162 */2163
2164 for(dev = dev_base; dev; dev = dev->next)
2165 {2166 if((dev->flags&IFF_UP)&&(dev->flags&IFF_MULTICAST)&&
2167 (dev->pa_addr==addr.s_addr))
2168 break;
2169 }2170
2171 /*2172 * Did we find one2173 */2174
2175 if(dev)
2176 {2177 strcpy(sk->ip_mc_name,dev->name);
2178 return 0;
2179 }2180 return -EADDRNOTAVAIL;
2181 }2182
2183 caseIP_ADD_MEMBERSHIP:
2184 {2185
2186 /*2187 * FIXME: Add/Del membership should have a semaphore protecting them from re-entry2188 */2189 structip_mreqmreq;
2190 staticstructoptionsoptmem;
2191 unsignedlongroute_src;
2192 structrtable *rt;
2193 structdevice *dev=NULL;
2194
2195 /*2196 * Check the arguments.2197 */2198
2199 err=verify_area(VERIFY_READ, optval, sizeof(mreq));
2200 if(err)
2201 returnerr;
2202
2203 memcpy_fromfs(&mreq,optval,sizeof(mreq));
2204
2205 /* 2206 * Get device for use later2207 */2208
2209 if(mreq.imr_interface.s_addr==INADDR_ANY)
2210 {2211 /*2212 * Not set so scan.2213 */2214 if((rt=ip_rt_route(mreq.imr_multiaddr.s_addr,&optmem, &route_src))!=NULL)
2215 {2216 dev=rt->rt_dev;
2217 rt->rt_use--;
2218 }2219 }2220 else2221 {2222 /*2223 * Find a suitable device.2224 */2225 for(dev = dev_base; dev; dev = dev->next)
2226 {2227 if((dev->flags&IFF_UP)&&(dev->flags&IFF_MULTICAST)&&
2228 (dev->pa_addr==mreq.imr_interface.s_addr))
2229 break;
2230 }2231 }2232
2233 /*2234 * No device, no cookies.2235 */2236
2237 if(!dev)
2238 return -ENODEV;
2239
2240 /*2241 * Join group.2242 */2243
2244 returnip_mc_join_group(sk,dev,mreq.imr_multiaddr.s_addr);
2245 }2246
2247 caseIP_DROP_MEMBERSHIP:
2248 {2249 structip_mreqmreq;
2250 structrtable *rt;
2251 staticstructoptionsoptmem;
2252 unsignedlongroute_src;
2253 structdevice *dev=NULL;
2254
2255 /*2256 * Check the arguments2257 */2258
2259 err=verify_area(VERIFY_READ, optval, sizeof(mreq));
2260 if(err)
2261 returnerr;
2262
2263 memcpy_fromfs(&mreq,optval,sizeof(mreq));
2264
2265 /*2266 * Get device for use later 2267 */2268
2269 if(mreq.imr_interface.s_addr==INADDR_ANY)
2270 {2271 if((rt=ip_rt_route(mreq.imr_multiaddr.s_addr,&optmem, &route_src))!=NULL)
2272 {2273 dev=rt->rt_dev;
2274 rt->rt_use--;
2275 }2276 }2277 else2278 {2279 for(dev = dev_base; dev; dev = dev->next)
2280 {2281 if((dev->flags&IFF_UP)&& (dev->flags&IFF_MULTICAST)&&
2282 (dev->pa_addr==mreq.imr_interface.s_addr))
2283 break;
2284 }2285 }2286
2287 /*2288 * Did we find a suitable device.2289 */2290
2291 if(!dev)
2292 return -ENODEV;
2293
2294 /*2295 * Leave group2296 */2297
2298 returnip_mc_leave_group(sk,dev,mreq.imr_multiaddr.s_addr);
2299 }2300 #endif2301 #ifdefCONFIG_IP_FIREWALL2302 caseIP_FW_ADD_BLK:
2303 caseIP_FW_DEL_BLK:
2304 caseIP_FW_ADD_FWD:
2305 caseIP_FW_DEL_FWD:
2306 caseIP_FW_CHK_BLK:
2307 caseIP_FW_CHK_FWD:
2308 caseIP_FW_FLUSH:
2309 caseIP_FW_POLICY:
2310 if(!suser())
2311 return -EPERM;
2312 if(optlen>sizeof(tmp_fw) || optlen<1)
2313 return -EINVAL;
2314 err=verify_area(VERIFY_READ,optval,optlen);
2315 if(err)
2316 returnerr;
2317 memcpy_fromfs(&tmp_fw,optval,optlen);
2318 err=ip_fw_ctl(optname, &tmp_fw,optlen);
2319 return -err; /* -0 is 0 after all */2320
2321 #endif2322 #ifdefCONFIG_IP_ACCT2323 caseIP_ACCT_DEL:
2324 caseIP_ACCT_ADD:
2325 caseIP_ACCT_FLUSH:
2326 caseIP_ACCT_ZERO:
2327 if(!suser())
2328 return -EPERM;
2329 if(optlen>sizeof(tmp_fw) || optlen<1)
2330 return -EINVAL;
2331 err=verify_area(VERIFY_READ,optval,optlen);
2332 if(err)
2333 returnerr;
2334 memcpy_fromfs(&tmp_fw, optval,optlen);
2335 err=ip_acct_ctl(optname, &tmp_fw,optlen);
2336 return -err; /* -0 is 0 after all */2337 #endif2338 /* IP_OPTIONS and friends go here eventually */2339 default:
2340 return(-ENOPROTOOPT);
2341 }2342 }2343
2344 /*2345 * Get the options. Note for future reference. The GET of IP options gets the2346 * _received_ ones. The set sets the _sent_ ones.2347 */2348
2349 intip_getsockopt(structsock *sk, intlevel, intoptname, char *optval, int *optlen)
/* */2350 {2351 intval,err;
2352 #ifdefCONFIG_IP_MULTICAST2353 intlen;
2354 #endif2355
2356 if(level!=SOL_IP)
2357 return -EOPNOTSUPP;
2358
2359 switch(optname)
2360 {2361 caseIP_TOS:
2362 val=sk->ip_tos;
2363 break;
2364 caseIP_TTL:
2365 val=sk->ip_ttl;
2366 break;
2367 #ifdefCONFIG_IP_MULTICAST2368 caseIP_MULTICAST_TTL:
2369 val=sk->ip_mc_ttl;
2370 break;
2371 caseIP_MULTICAST_LOOP:
2372 val=sk->ip_mc_loop;
2373 break;
2374 caseIP_MULTICAST_IF:
2375 err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
2376 if(err)
2377 returnerr;
2378 len=strlen(sk->ip_mc_name);
2379 err=verify_area(VERIFY_WRITE, optval, len);
2380 if(err)
2381 returnerr;
2382 put_fs_long(len,(unsignedlong *) optlen);
2383 memcpy_tofs((void *)optval,sk->ip_mc_name, len);
2384 return 0;
2385 #endif2386 default:
2387 return(-ENOPROTOOPT);
2388 }2389 err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
2390 if(err)
2391 returnerr;
2392 put_fs_long(sizeof(int),(unsignedlong *) optlen);
2393
2394 err=verify_area(VERIFY_WRITE, optval, sizeof(int));
2395 if(err)
2396 returnerr;
2397 put_fs_long(val,(unsignedlong *)optval);
2398
2399 return(0);
2400 }2401
2402 /*2403 * IP protocol layer initialiser2404 */2405
2406 staticstructpacket_typeip_packet_type =
2407 {2408 0, /* MUTTER ntohs(ETH_P_IP),*/2409 NULL, /* All devices */2410 ip_rcv,
2411 NULL,
2412 NULL,
2413 };
2414
2415 /*2416 * Device notifier2417 */2418
2419 staticintip_rt_event(unsignedlongevent, void *ptr)
/* */2420 {2421 if(event==NETDEV_DOWN)
2422 ip_rt_flush(ptr);
2423 returnNOTIFY_DONE;
2424 }2425
2426 structnotifier_blockip_rt_notifier={2427 ip_rt_event,
2428 NULL,
2429 0
2430 };
2431
2432 /*2433 * IP registers the packet type and then calls the subprotocol initialisers2434 */2435
2436 voidip_init(void)
/* */2437 {2438 ip_packet_type.type=htons(ETH_P_IP);
2439 dev_add_pack(&ip_packet_type);
2440
2441 /* So we flush routes when a device is downed */2442 register_netdevice_notifier(&ip_rt_notifier);
2443 /* ip_raw_init();2444 ip_packet_init();2445 ip_tcp_init();2446 ip_udp_init();*/2447 }