ip_input.c revision 1.192
1/* $NetBSD: ip_input.c,v 1.192 2003/12/08 02:23:27 jonathan Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32/*- 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the NetBSD 51 * Foundation, Inc. and its contributors. 52 * 4. Neither the name of The NetBSD Foundation nor the names of its 53 * contributors may be used to endorse or promote products derived 54 * from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69/* 70 * Copyright (c) 1982, 1986, 1988, 1993 71 * The Regents of the University of California. All rights reserved. 72 * 73 * Redistribution and use in source and binary forms, with or without 74 * modification, are permitted provided that the following conditions 75 * are met: 76 * 1. Redistributions of source code must retain the above copyright 77 * notice, this list of conditions and the following disclaimer. 78 * 2. Redistributions in binary form must reproduce the above copyright 79 * notice, this list of conditions and the following disclaimer in the 80 * documentation and/or other materials provided with the distribution. 81 * 3. Neither the name of the University nor the names of its contributors 82 * may be used to endorse or promote products derived from this software 83 * without specific prior written permission. 84 * 85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 95 * SUCH DAMAGE. 96 * 97 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 98 */ 99 100#include <sys/cdefs.h> 101__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.192 2003/12/08 02:23:27 jonathan Exp $"); 102 103#include "opt_inet.h" 104#include "opt_gateway.h" 105#include "opt_pfil_hooks.h" 106#include "opt_ipsec.h" 107#include "opt_mrouting.h" 108#include "opt_mbuftrace.h" 109#include "opt_inet_csum.h" 110 111#include <sys/param.h> 112#include <sys/systm.h> 113#include <sys/malloc.h> 114#include <sys/mbuf.h> 115#include <sys/domain.h> 116#include <sys/protosw.h> 117#include <sys/socket.h> 118#include <sys/socketvar.h> 119#include <sys/errno.h> 120#include <sys/time.h> 121#include <sys/kernel.h> 122#include <sys/pool.h> 123#include <sys/sysctl.h> 124 125#include <net/if.h> 126#include <net/if_dl.h> 127#include <net/route.h> 128#include <net/pfil.h> 129 130#include <netinet/in.h> 131#include <netinet/in_systm.h> 132#include <netinet/ip.h> 133#include <netinet/in_pcb.h> 134#include <netinet/in_var.h> 135#include <netinet/ip_var.h> 136#include <netinet/ip_icmp.h> 137/* just for gif_ttl */ 138#include <netinet/in_gif.h> 139#include "gif.h" 140#include <net/if_gre.h> 141#include "gre.h" 142 143#ifdef MROUTING 144#include <netinet/ip_mroute.h> 145#endif 146 147#ifdef IPSEC 148#include <netinet6/ipsec.h> 149#include <netkey/key.h> 150#endif 151#ifdef FAST_IPSEC 152#include <netipsec/ipsec.h> 153#include <netipsec/key.h> 154#endif /* FAST_IPSEC*/ 155 156#ifndef IPFORWARDING 157#ifdef GATEWAY 158#define IPFORWARDING 1 /* forward IP packets not for us */ 159#else /* GATEWAY */ 160#define IPFORWARDING 0 /* don't forward IP packets not for us */ 161#endif /* GATEWAY */ 162#endif /* IPFORWARDING */ 163#ifndef IPSENDREDIRECTS 164#define IPSENDREDIRECTS 1 165#endif 166#ifndef IPFORWSRCRT 167#define IPFORWSRCRT 1 /* forward source-routed packets */ 168#endif 169#ifndef IPALLOWSRCRT 170#define IPALLOWSRCRT 1 /* allow source-routed packets */ 171#endif 172#ifndef IPMTUDISC 173#define IPMTUDISC 1 174#endif 175#ifndef IPMTUDISCTIMEOUT 176#define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */ 177#endif 178 179/* 180 * Note: DIRECTED_BROADCAST is handled this way so that previous 181 * configuration using this option will Just Work. 182 */ 183#ifndef IPDIRECTEDBCAST 184#ifdef DIRECTED_BROADCAST 185#define IPDIRECTEDBCAST 1 186#else 187#define IPDIRECTEDBCAST 0 188#endif /* DIRECTED_BROADCAST */ 189#endif /* IPDIRECTEDBCAST */ 190int ipforwarding = IPFORWARDING; 191int ipsendredirects = IPSENDREDIRECTS; 192int ip_defttl = IPDEFTTL; 193int ip_forwsrcrt = IPFORWSRCRT; 194int ip_directedbcast = IPDIRECTEDBCAST; 195int ip_allowsrcrt = IPALLOWSRCRT; 196int ip_mtudisc = IPMTUDISC; 197int ip_mtudisc_timeout = IPMTUDISCTIMEOUT; 198#ifdef DIAGNOSTIC 199int ipprintfs = 0; 200#endif 201 202int ip_do_randomid = 0; 203 204/* 205 * XXX - Setting ip_checkinterface mostly implements the receive side of 206 * the Strong ES model described in RFC 1122, but since the routing table 207 * and transmit implementation do not implement the Strong ES model, 208 * setting this to 1 results in an odd hybrid. 209 * 210 * XXX - ip_checkinterface currently must be disabled if you use ipnat 211 * to translate the destination address to another local interface. 212 * 213 * XXX - ip_checkinterface must be disabled if you add IP aliases 214 * to the loopback interface instead of the interface where the 215 * packets for those addresses are received. 216 */ 217int ip_checkinterface = 0; 218 219 220struct rttimer_queue *ip_mtudisc_timeout_q = NULL; 221 222extern struct domain inetdomain; 223int ipqmaxlen = IFQ_MAXLEN; 224u_long in_ifaddrhash; /* size of hash table - 1 */ 225int in_ifaddrentries; /* total number of addrs */ 226struct in_ifaddrhead in_ifaddrhead; 227struct in_ifaddrhashhead *in_ifaddrhashtbl; 228u_long in_multihash; /* size of hash table - 1 */ 229int in_multientries; /* total number of addrs */ 230struct in_multihashhead *in_multihashtbl; 231struct ifqueue ipintrq; 232struct ipstat ipstat; 233uint16_t ip_id; 234 235#ifdef PFIL_HOOKS 236struct pfil_head inet_pfil_hook; 237#endif 238 239/* IP datagram reassembly queues (hashed) */ 240#define IPREASS_NHASH_LOG2 6 241#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) 242#define IPREASS_HMASK (IPREASS_NHASH - 1) 243#define IPREASS_HASH(x,y) \ 244 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) 245struct ipqhead ipq[IPREASS_NHASH]; 246int ipq_locked; 247int ip_nfragpackets = 0; 248int ip_maxfragpackets = 200; 249int ip_nfrags = 0; /* total fragments in reass queues */ 250 251static __inline int ipq_lock_try __P((void)); 252static __inline void ipq_unlock __P((void)); 253 254static __inline int 255ipq_lock_try() 256{ 257 int s; 258 259 /* 260 * Use splvm() -- we're blocking things that would cause 261 * mbuf allocation. 262 */ 263 s = splvm(); 264 if (ipq_locked) { 265 splx(s); 266 return (0); 267 } 268 ipq_locked = 1; 269 splx(s); 270 return (1); 271} 272 273static __inline void 274ipq_unlock() 275{ 276 int s; 277 278 s = splvm(); 279 ipq_locked = 0; 280 splx(s); 281} 282 283#ifdef DIAGNOSTIC 284#define IPQ_LOCK() \ 285do { \ 286 if (ipq_lock_try() == 0) { \ 287 printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \ 288 panic("ipq_lock"); \ 289 } \ 290} while (/*CONSTCOND*/ 0) 291#define IPQ_LOCK_CHECK() \ 292do { \ 293 if (ipq_locked == 0) { \ 294 printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \ 295 panic("ipq lock check"); \ 296 } \ 297} while (/*CONSTCOND*/ 0) 298#else 299#define IPQ_LOCK() (void) ipq_lock_try() 300#define IPQ_LOCK_CHECK() /* nothing */ 301#endif 302 303#define IPQ_UNLOCK() ipq_unlock() 304 305struct pool inmulti_pool; 306struct pool ipqent_pool; 307 308#ifdef INET_CSUM_COUNTERS 309#include <sys/device.h> 310 311struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 312 NULL, "inet", "hwcsum bad"); 313struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 314 NULL, "inet", "hwcsum ok"); 315struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 316 NULL, "inet", "swcsum"); 317 318#define INET_CSUM_COUNTER_INCR(ev) (ev)->ev_count++ 319 320#else 321 322#define INET_CSUM_COUNTER_INCR(ev) /* nothing */ 323 324#endif /* INET_CSUM_COUNTERS */ 325 326/* 327 * We need to save the IP options in case a protocol wants to respond 328 * to an incoming packet over the same route if the packet got here 329 * using IP source routing. This allows connection establishment and 330 * maintenance when the remote end is on a network that is not known 331 * to us. 332 */ 333int ip_nhops = 0; 334static struct ip_srcrt { 335 struct in_addr dst; /* final destination */ 336 char nop; /* one NOP to align */ 337 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 338 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 339} ip_srcrt; 340 341static void save_rte __P((u_char *, struct in_addr)); 342 343#ifdef MBUFTRACE 344struct mowner ip_rx_mowner = { "internet", "rx" }; 345struct mowner ip_tx_mowner = { "internet", "tx" }; 346#endif 347 348/* 349 * IP initialization: fill in IP protocol switch table. 350 * All protocols not implemented in kernel go to raw IP protocol handler. 351 */ 352void 353ip_init() 354{ 355 struct protosw *pr; 356 int i; 357 358 pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl", 359 NULL); 360 pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl", 361 NULL); 362 363 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 364 if (pr == 0) 365 panic("ip_init"); 366 for (i = 0; i < IPPROTO_MAX; i++) 367 ip_protox[i] = pr - inetsw; 368 for (pr = inetdomain.dom_protosw; 369 pr < inetdomain.dom_protoswNPROTOSW; pr++) 370 if (pr->pr_domain->dom_family == PF_INET && 371 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 372 ip_protox[pr->pr_protocol] = pr - inetsw; 373 374 for (i = 0; i < IPREASS_NHASH; i++) 375 LIST_INIT(&ipq[i]); 376 377 ip_id = time.tv_sec & 0xfffff; 378 ipintrq.ifq_maxlen = ipqmaxlen; 379 TAILQ_INIT(&in_ifaddrhead); 380 in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IFADDR, 381 M_WAITOK, &in_ifaddrhash); 382 in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IPMADDR, 383 M_WAITOK, &in_multihash); 384 ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout); 385#ifdef GATEWAY 386 ipflow_init(); 387#endif 388 389#ifdef PFIL_HOOKS 390 /* Register our Packet Filter hook. */ 391 inet_pfil_hook.ph_type = PFIL_TYPE_AF; 392 inet_pfil_hook.ph_af = AF_INET; 393 i = pfil_head_register(&inet_pfil_hook); 394 if (i != 0) 395 printf("ip_init: WARNING: unable to register pfil hook, " 396 "error %d\n", i); 397#endif /* PFIL_HOOKS */ 398 399#ifdef INET_CSUM_COUNTERS 400 evcnt_attach_static(&ip_hwcsum_bad); 401 evcnt_attach_static(&ip_hwcsum_ok); 402 evcnt_attach_static(&ip_swcsum); 403#endif /* INET_CSUM_COUNTERS */ 404 405#ifdef MBUFTRACE 406 MOWNER_ATTACH(&ip_tx_mowner); 407 MOWNER_ATTACH(&ip_rx_mowner); 408#endif /* MBUFTRACE */ 409} 410 411struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 412struct route ipforward_rt; 413 414/* 415 * IP software interrupt routine 416 */ 417void 418ipintr() 419{ 420 int s; 421 struct mbuf *m; 422 423 while (1) { 424 s = splnet(); 425 IF_DEQUEUE(&ipintrq, m); 426 splx(s); 427 if (m == 0) 428 return; 429 MCLAIM(m, &ip_rx_mowner); 430 ip_input(m); 431 } 432} 433 434/* 435 * Ip input routine. Checksum and byte swap header. If fragmented 436 * try to reassemble. Process options. Pass to next level. 437 */ 438void 439ip_input(struct mbuf *m) 440{ 441 struct ip *ip = NULL; 442 struct ipq *fp; 443 struct in_ifaddr *ia; 444 struct ifaddr *ifa; 445 struct ipqent *ipqe; 446 int hlen = 0, mff, len; 447 int downmatch; 448 int checkif; 449 int srcrt = 0; 450 u_int hash; 451#ifdef FAST_IPSEC 452 struct m_tag *mtag; 453 struct tdb_ident *tdbi; 454 struct secpolicy *sp; 455 int s, error; 456#endif /* FAST_IPSEC */ 457 458 MCLAIM(m, &ip_rx_mowner); 459#ifdef DIAGNOSTIC 460 if ((m->m_flags & M_PKTHDR) == 0) 461 panic("ipintr no HDR"); 462#endif 463 464 /* 465 * If no IP addresses have been set yet but the interfaces 466 * are receiving, can't do anything with incoming packets yet. 467 */ 468 if (TAILQ_FIRST(&in_ifaddrhead) == 0) 469 goto bad; 470 ipstat.ips_total++; 471 /* 472 * If the IP header is not aligned, slurp it up into a new 473 * mbuf with space for link headers, in the event we forward 474 * it. Otherwise, if it is aligned, make sure the entire 475 * base IP header is in the first mbuf of the chain. 476 */ 477 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { 478 if ((m = m_copyup(m, sizeof(struct ip), 479 (max_linkhdr + 3) & ~3)) == NULL) { 480 /* XXXJRT new stat, please */ 481 ipstat.ips_toosmall++; 482 return; 483 } 484 } else if (__predict_false(m->m_len < sizeof (struct ip))) { 485 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) { 486 ipstat.ips_toosmall++; 487 return; 488 } 489 } 490 ip = mtod(m, struct ip *); 491 if (ip->ip_v != IPVERSION) { 492 ipstat.ips_badvers++; 493 goto bad; 494 } 495 hlen = ip->ip_hl << 2; 496 if (hlen < sizeof(struct ip)) { /* minimum header length */ 497 ipstat.ips_badhlen++; 498 goto bad; 499 } 500 if (hlen > m->m_len) { 501 if ((m = m_pullup(m, hlen)) == 0) { 502 ipstat.ips_badhlen++; 503 return; 504 } 505 ip = mtod(m, struct ip *); 506 } 507 508 /* 509 * RFC1122: packets with a multicast source address are 510 * not allowed. 511 */ 512 if (IN_MULTICAST(ip->ip_src.s_addr)) { 513 ipstat.ips_badaddr++; 514 goto bad; 515 } 516 517 /* 127/8 must not appear on wire - RFC1122 */ 518 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 519 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 520 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 521 ipstat.ips_badaddr++; 522 goto bad; 523 } 524 } 525 526 switch (m->m_pkthdr.csum_flags & 527 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) | 528 M_CSUM_IPv4_BAD)) { 529 case M_CSUM_IPv4|M_CSUM_IPv4_BAD: 530 INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); 531 goto badcsum; 532 533 case M_CSUM_IPv4: 534 /* Checksum was okay. */ 535 INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); 536 break; 537 538 default: 539 /* Must compute it ourselves. */ 540 INET_CSUM_COUNTER_INCR(&ip_swcsum); 541 if (in_cksum(m, hlen) != 0) 542 goto bad; 543 break; 544 } 545 546 /* Retrieve the packet length. */ 547 len = ntohs(ip->ip_len); 548 549 /* 550 * Check for additional length bogosity 551 */ 552 if (len < hlen) { 553 ipstat.ips_badlen++; 554 goto bad; 555 } 556 557 /* 558 * Check that the amount of data in the buffers 559 * is as at least much as the IP header would have us expect. 560 * Trim mbufs if longer than we expect. 561 * Drop packet if shorter than we expect. 562 */ 563 if (m->m_pkthdr.len < len) { 564 ipstat.ips_tooshort++; 565 goto bad; 566 } 567 if (m->m_pkthdr.len > len) { 568 if (m->m_len == m->m_pkthdr.len) { 569 m->m_len = len; 570 m->m_pkthdr.len = len; 571 } else 572 m_adj(m, len - m->m_pkthdr.len); 573 } 574 575#if defined(IPSEC) || defined(FAST_IPSEC) 576 /* ipflow (IP fast forwarding) is not compatible with IPsec. */ 577 m->m_flags &= ~M_CANFASTFWD; 578#else 579 /* 580 * Assume that we can create a fast-forward IP flow entry 581 * based on this packet. 582 */ 583 m->m_flags |= M_CANFASTFWD; 584#endif 585 586#ifdef PFIL_HOOKS 587 /* 588 * Run through list of hooks for input packets. If there are any 589 * filters which require that additional packets in the flow are 590 * not fast-forwarded, they must clear the M_CANFASTFWD flag. 591 * Note that filters must _never_ set this flag, as another filter 592 * in the list may have previously cleared it. 593 */ 594 /* 595 * let ipfilter look at packet on the wire, 596 * not the decapsulated packet. 597 */ 598#ifdef IPSEC 599 if (!ipsec_getnhist(m)) 600#elif defined(FAST_IPSEC) 601 if (!ipsec_indone(m)) 602#else 603 if (1) 604#endif 605 { 606 struct in_addr odst; 607 608 odst = ip->ip_dst; 609 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, 610 PFIL_IN) != 0) 611 return; 612 if (m == NULL) 613 return; 614 ip = mtod(m, struct ip *); 615 hlen = ip->ip_hl << 2; 616 srcrt = (odst.s_addr != ip->ip_dst.s_addr); 617 } 618#endif /* PFIL_HOOKS */ 619 620#ifdef ALTQ 621 /* XXX Temporary until ALTQ is changed to use a pfil hook */ 622 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) { 623 /* packet dropped by traffic conditioner */ 624 return; 625 } 626#endif 627 628 /* 629 * Process options and, if not destined for us, 630 * ship it on. ip_dooptions returns 1 when an 631 * error was detected (causing an icmp message 632 * to be sent and the original packet to be freed). 633 */ 634 ip_nhops = 0; /* for source routed packets */ 635 if (hlen > sizeof (struct ip) && ip_dooptions(m)) 636 return; 637 638 /* 639 * Enable a consistency check between the destination address 640 * and the arrival interface for a unicast packet (the RFC 1122 641 * strong ES model) if IP forwarding is disabled and the packet 642 * is not locally generated. 643 * 644 * XXX - Checking also should be disabled if the destination 645 * address is ipnat'ed to a different interface. 646 * 647 * XXX - Checking is incompatible with IP aliases added 648 * to the loopback interface instead of the interface where 649 * the packets are received. 650 * 651 * XXX - We need to add a per ifaddr flag for this so that 652 * we get finer grain control. 653 */ 654 checkif = ip_checkinterface && (ipforwarding == 0) && 655 (m->m_pkthdr.rcvif != NULL) && 656 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0); 657 658 /* 659 * Check our list of addresses, to see if the packet is for us. 660 * 661 * Traditional 4.4BSD did not consult IFF_UP at all. 662 * The behavior here is to treat addresses on !IFF_UP interface 663 * as not mine. 664 */ 665 downmatch = 0; 666 LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 667 if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) { 668 if (checkif && ia->ia_ifp != m->m_pkthdr.rcvif) 669 continue; 670 if ((ia->ia_ifp->if_flags & IFF_UP) != 0) 671 break; 672 else 673 downmatch++; 674 } 675 } 676 if (ia != NULL) 677 goto ours; 678 if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 679 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) { 680 if (ifa->ifa_addr->sa_family != AF_INET) 681 continue; 682 ia = ifatoia(ifa); 683 if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) || 684 in_hosteq(ip->ip_dst, ia->ia_netbroadcast) || 685 /* 686 * Look for all-0's host part (old broadcast addr), 687 * either for subnet or net. 688 */ 689 ip->ip_dst.s_addr == ia->ia_subnet || 690 ip->ip_dst.s_addr == ia->ia_net) 691 goto ours; 692 /* 693 * An interface with IP address zero accepts 694 * all packets that arrive on that interface. 695 */ 696 if (in_nullhost(ia->ia_addr.sin_addr)) 697 goto ours; 698 } 699 } 700 if (IN_MULTICAST(ip->ip_dst.s_addr)) { 701 struct in_multi *inm; 702#ifdef MROUTING 703 extern struct socket *ip_mrouter; 704 705 if (M_READONLY(m)) { 706 if ((m = m_pullup(m, hlen)) == 0) { 707 ipstat.ips_toosmall++; 708 return; 709 } 710 ip = mtod(m, struct ip *); 711 } 712 713 if (ip_mrouter) { 714 /* 715 * If we are acting as a multicast router, all 716 * incoming multicast packets are passed to the 717 * kernel-level multicast forwarding function. 718 * The packet is returned (relatively) intact; if 719 * ip_mforward() returns a non-zero value, the packet 720 * must be discarded, else it may be accepted below. 721 * 722 * (The IP ident field is put in the same byte order 723 * as expected when ip_mforward() is called from 724 * ip_output().) 725 */ 726 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { 727 ipstat.ips_cantforward++; 728 m_freem(m); 729 return; 730 } 731 732 /* 733 * The process-level routing demon needs to receive 734 * all multicast IGMP packets, whether or not this 735 * host belongs to their destination groups. 736 */ 737 if (ip->ip_p == IPPROTO_IGMP) 738 goto ours; 739 ipstat.ips_forward++; 740 } 741#endif 742 /* 743 * See if we belong to the destination multicast group on the 744 * arrival interface. 745 */ 746 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 747 if (inm == NULL) { 748 ipstat.ips_cantforward++; 749 m_freem(m); 750 return; 751 } 752 goto ours; 753 } 754 if (ip->ip_dst.s_addr == INADDR_BROADCAST || 755 in_nullhost(ip->ip_dst)) 756 goto ours; 757 758 /* 759 * Not for us; forward if possible and desirable. 760 */ 761 if (ipforwarding == 0) { 762 ipstat.ips_cantforward++; 763 m_freem(m); 764 } else { 765 /* 766 * If ip_dst matched any of my address on !IFF_UP interface, 767 * and there's no IFF_UP interface that matches ip_dst, 768 * send icmp unreach. Forwarding it will result in in-kernel 769 * forwarding loop till TTL goes to 0. 770 */ 771 if (downmatch) { 772 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 773 ipstat.ips_cantforward++; 774 return; 775 } 776#ifdef IPSEC 777 if (ipsec4_in_reject(m, NULL)) { 778 ipsecstat.in_polvio++; 779 goto bad; 780 } 781#endif 782#ifdef FAST_IPSEC 783 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 784 s = splsoftnet(); 785 if (mtag != NULL) { 786 tdbi = (struct tdb_ident *)(mtag + 1); 787 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 788 } else { 789 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 790 IP_FORWARDING, &error); 791 } 792 if (sp == NULL) { /* NB: can happen if error */ 793 splx(s); 794 /*XXX error stat???*/ 795 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/ 796 goto bad; 797 } 798 799 /* 800 * Check security policy against packet attributes. 801 */ 802 error = ipsec_in_reject(sp, m); 803 KEY_FREESP(&sp); 804 splx(s); 805 if (error) { 806 ipstat.ips_cantforward++; 807 goto bad; 808 } 809#endif /* FAST_IPSEC */ 810 811 ip_forward(m, srcrt); 812 } 813 return; 814 815ours: 816 /* 817 * If offset or IP_MF are set, must reassemble. 818 * Otherwise, nothing need be done. 819 * (We could look in the reassembly queue to see 820 * if the packet was previously fragmented, 821 * but it's not worth the time; just let them time out.) 822 */ 823 if (ip->ip_off & ~htons(IP_DF|IP_RF)) { 824 if (M_READONLY(m)) { 825 if ((m = m_pullup(m, hlen)) == NULL) { 826 ipstat.ips_toosmall++; 827 goto bad; 828 } 829 ip = mtod(m, struct ip *); 830 } 831 832 /* 833 * Look for queue of fragments 834 * of this datagram. 835 */ 836 IPQ_LOCK(); 837 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 838 /* XXX LIST_FOREACH(fp, &ipq[hash], ipq_q) */ 839 for (fp = LIST_FIRST(&ipq[hash]); fp != NULL; 840 fp = LIST_NEXT(fp, ipq_q)) { 841 if (ip->ip_id == fp->ipq_id && 842 in_hosteq(ip->ip_src, fp->ipq_src) && 843 in_hosteq(ip->ip_dst, fp->ipq_dst) && 844 ip->ip_p == fp->ipq_p) 845 goto found; 846 847 } 848 fp = 0; 849found: 850 851 /* 852 * Adjust ip_len to not reflect header, 853 * set ipqe_mff if more fragments are expected, 854 * convert offset of this to bytes. 855 */ 856 ip->ip_len = htons(ntohs(ip->ip_len) - hlen); 857 mff = (ip->ip_off & htons(IP_MF)) != 0; 858 if (mff) { 859 /* 860 * Make sure that fragments have a data length 861 * that's a non-zero multiple of 8 bytes. 862 */ 863 if (ntohs(ip->ip_len) == 0 || 864 (ntohs(ip->ip_len) & 0x7) != 0) { 865 ipstat.ips_badfrags++; 866 IPQ_UNLOCK(); 867 goto bad; 868 } 869 } 870 ip->ip_off = htons((ntohs(ip->ip_off) & IP_OFFMASK) << 3); 871 872 /* 873 * If datagram marked as having more fragments 874 * or if this is not the first fragment, 875 * attempt reassembly; if it succeeds, proceed. 876 */ 877 if (mff || ip->ip_off != htons(0)) { 878 ipstat.ips_fragments++; 879 ipqe = pool_get(&ipqent_pool, PR_NOWAIT); 880 if (ipqe == NULL) { 881 ipstat.ips_rcvmemdrop++; 882 IPQ_UNLOCK(); 883 goto bad; 884 } 885 ipqe->ipqe_mff = mff; 886 ipqe->ipqe_m = m; 887 ipqe->ipqe_ip = ip; 888 m = ip_reass(ipqe, fp, &ipq[hash]); 889 if (m == 0) { 890 IPQ_UNLOCK(); 891 return; 892 } 893 ipstat.ips_reassembled++; 894 ip = mtod(m, struct ip *); 895 hlen = ip->ip_hl << 2; 896 ip->ip_len = htons(ntohs(ip->ip_len) + hlen); 897 } else 898 if (fp) 899 ip_freef(fp); 900 IPQ_UNLOCK(); 901 } 902 903#if defined(IPSEC) 904 /* 905 * enforce IPsec policy checking if we are seeing last header. 906 * note that we do not visit this with protocols with pcb layer 907 * code - like udp/tcp/raw ip. 908 */ 909 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 && 910 ipsec4_in_reject(m, NULL)) { 911 ipsecstat.in_polvio++; 912 goto bad; 913 } 914#endif 915#if FAST_IPSEC 916 /* 917 * enforce IPsec policy checking if we are seeing last header. 918 * note that we do not visit this with protocols with pcb layer 919 * code - like udp/tcp/raw ip. 920 */ 921 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) { 922 /* 923 * Check if the packet has already had IPsec processing 924 * done. If so, then just pass it along. This tag gets 925 * set during AH, ESP, etc. input handling, before the 926 * packet is returned to the ip input queue for delivery. 927 */ 928 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 929 s = splsoftnet(); 930 if (mtag != NULL) { 931 tdbi = (struct tdb_ident *)(mtag + 1); 932 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 933 } else { 934 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 935 IP_FORWARDING, &error); 936 } 937 if (sp != NULL) { 938 /* 939 * Check security policy against packet attributes. 940 */ 941 error = ipsec_in_reject(sp, m); 942 KEY_FREESP(&sp); 943 } else { 944 /* XXX error stat??? */ 945 error = EINVAL; 946DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/ 947 goto bad; 948 } 949 splx(s); 950 if (error) 951 goto bad; 952 } 953#endif /* FAST_IPSEC */ 954 955 /* 956 * Switch out to protocol's input routine. 957 */ 958#if IFA_STATS 959 if (ia && ip) 960 ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len); 961#endif 962 ipstat.ips_delivered++; 963 { 964 int off = hlen, nh = ip->ip_p; 965 966 (*inetsw[ip_protox[nh]].pr_input)(m, off, nh); 967 return; 968 } 969bad: 970 m_freem(m); 971 return; 972 973badcsum: 974 ipstat.ips_badsum++; 975 m_freem(m); 976} 977 978/* 979 * Take incoming datagram fragment and try to 980 * reassemble it into whole datagram. If a chain for 981 * reassembly of this datagram already exists, then it 982 * is given as fp; otherwise have to make a chain. 983 */ 984struct mbuf * 985ip_reass(ipqe, fp, ipqhead) 986 struct ipqent *ipqe; 987 struct ipq *fp; 988 struct ipqhead *ipqhead; 989{ 990 struct mbuf *m = ipqe->ipqe_m; 991 struct ipqent *nq, *p, *q; 992 struct ip *ip; 993 struct mbuf *t; 994 int hlen = ipqe->ipqe_ip->ip_hl << 2; 995 int i, next; 996 997 IPQ_LOCK_CHECK(); 998 999 /* 1000 * Presence of header sizes in mbufs 1001 * would confuse code below. 1002 */ 1003 m->m_data += hlen; 1004 m->m_len -= hlen; 1005 1006 /* 1007 * We are about to add a fragment; increment frag count. 1008 */ 1009 ip_nfrags++; 1010 1011 /* 1012 * If first fragment to arrive, create a reassembly queue. 1013 */ 1014 if (fp == 0) { 1015 /* 1016 * Enforce upper bound on number of fragmented packets 1017 * for which we attempt reassembly; 1018 * If maxfrag is 0, never accept fragments. 1019 * If maxfrag is -1, accept all fragments without limitation. 1020 */ 1021 if (ip_maxfragpackets < 0) 1022 ; 1023 else if (ip_nfragpackets >= ip_maxfragpackets) 1024 goto dropfrag; 1025 ip_nfragpackets++; 1026 MALLOC(fp, struct ipq *, sizeof (struct ipq), 1027 M_FTABLE, M_NOWAIT); 1028 if (fp == NULL) 1029 goto dropfrag; 1030 LIST_INSERT_HEAD(ipqhead, fp, ipq_q); 1031 fp->ipq_nfrags = 1; 1032 fp->ipq_ttl = IPFRAGTTL; 1033 fp->ipq_p = ipqe->ipqe_ip->ip_p; 1034 fp->ipq_id = ipqe->ipqe_ip->ip_id; 1035 TAILQ_INIT(&fp->ipq_fragq); 1036 fp->ipq_src = ipqe->ipqe_ip->ip_src; 1037 fp->ipq_dst = ipqe->ipqe_ip->ip_dst; 1038 p = NULL; 1039 goto insert; 1040 } else { 1041 fp->ipq_nfrags++; 1042 } 1043 1044 /* 1045 * Find a segment which begins after this one does. 1046 */ 1047 for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; 1048 p = q, q = TAILQ_NEXT(q, ipqe_q)) 1049 if (ntohs(q->ipqe_ip->ip_off) > ntohs(ipqe->ipqe_ip->ip_off)) 1050 break; 1051 1052 /* 1053 * If there is a preceding segment, it may provide some of 1054 * our data already. If so, drop the data from the incoming 1055 * segment. If it provides all of our data, drop us. 1056 */ 1057 if (p != NULL) { 1058 i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) - 1059 ntohs(ipqe->ipqe_ip->ip_off); 1060 if (i > 0) { 1061 if (i >= ntohs(ipqe->ipqe_ip->ip_len)) 1062 goto dropfrag; 1063 m_adj(ipqe->ipqe_m, i); 1064 ipqe->ipqe_ip->ip_off = 1065 htons(ntohs(ipqe->ipqe_ip->ip_off) + i); 1066 ipqe->ipqe_ip->ip_len = 1067 htons(ntohs(ipqe->ipqe_ip->ip_len) - i); 1068 } 1069 } 1070 1071 /* 1072 * While we overlap succeeding segments trim them or, 1073 * if they are completely covered, dequeue them. 1074 */ 1075 for (; q != NULL && 1076 ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len) > 1077 ntohs(q->ipqe_ip->ip_off); q = nq) { 1078 i = (ntohs(ipqe->ipqe_ip->ip_off) + 1079 ntohs(ipqe->ipqe_ip->ip_len)) - ntohs(q->ipqe_ip->ip_off); 1080 if (i < ntohs(q->ipqe_ip->ip_len)) { 1081 q->ipqe_ip->ip_len = 1082 htons(ntohs(q->ipqe_ip->ip_len) - i); 1083 q->ipqe_ip->ip_off = 1084 htons(ntohs(q->ipqe_ip->ip_off) + i); 1085 m_adj(q->ipqe_m, i); 1086 break; 1087 } 1088 nq = TAILQ_NEXT(q, ipqe_q); 1089 m_freem(q->ipqe_m); 1090 TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q); 1091 pool_put(&ipqent_pool, q); 1092 fp->ipq_nfrags--; 1093 ip_nfrags--; 1094 } 1095 1096insert: 1097 /* 1098 * Stick new segment in its place; 1099 * check for complete reassembly. 1100 */ 1101 if (p == NULL) { 1102 TAILQ_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q); 1103 } else { 1104 TAILQ_INSERT_AFTER(&fp->ipq_fragq, p, ipqe, ipqe_q); 1105 } 1106 next = 0; 1107 for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; 1108 p = q, q = TAILQ_NEXT(q, ipqe_q)) { 1109 if (ntohs(q->ipqe_ip->ip_off) != next) 1110 return (0); 1111 next += ntohs(q->ipqe_ip->ip_len); 1112 } 1113 if (p->ipqe_mff) 1114 return (0); 1115 1116 /* 1117 * Reassembly is complete. Check for a bogus message size and 1118 * concatenate fragments. 1119 */ 1120 q = TAILQ_FIRST(&fp->ipq_fragq); 1121 ip = q->ipqe_ip; 1122 if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) { 1123 ipstat.ips_toolong++; 1124 ip_freef(fp); 1125 return (0); 1126 } 1127 m = q->ipqe_m; 1128 t = m->m_next; 1129 m->m_next = 0; 1130 m_cat(m, t); 1131 nq = TAILQ_NEXT(q, ipqe_q); 1132 pool_put(&ipqent_pool, q); 1133 for (q = nq; q != NULL; q = nq) { 1134 t = q->ipqe_m; 1135 nq = TAILQ_NEXT(q, ipqe_q); 1136 pool_put(&ipqent_pool, q); 1137 m_cat(m, t); 1138 } 1139 ip_nfrags -= fp->ipq_nfrags; 1140 1141 /* 1142 * Create header for new ip packet by 1143 * modifying header of first packet; 1144 * dequeue and discard fragment reassembly header. 1145 * Make header visible. 1146 */ 1147 ip->ip_len = htons(next); 1148 ip->ip_src = fp->ipq_src; 1149 ip->ip_dst = fp->ipq_dst; 1150 LIST_REMOVE(fp, ipq_q); 1151 FREE(fp, M_FTABLE); 1152 ip_nfragpackets--; 1153 m->m_len += (ip->ip_hl << 2); 1154 m->m_data -= (ip->ip_hl << 2); 1155 /* some debugging cruft by sklower, below, will go away soon */ 1156 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 1157 int plen = 0; 1158 for (t = m; t; t = t->m_next) 1159 plen += t->m_len; 1160 m->m_pkthdr.len = plen; 1161 } 1162 return (m); 1163 1164dropfrag: 1165 if (fp != 0) 1166 fp->ipq_nfrags--; 1167 ip_nfrags--; 1168 ipstat.ips_fragdropped++; 1169 m_freem(m); 1170 pool_put(&ipqent_pool, ipqe); 1171 return (0); 1172} 1173 1174/* 1175 * Free a fragment reassembly header and all 1176 * associated datagrams. 1177 */ 1178void 1179ip_freef(fp) 1180 struct ipq *fp; 1181{ 1182 struct ipqent *q, *p; 1183 u_int nfrags = 0; 1184 1185 IPQ_LOCK_CHECK(); 1186 1187 for (q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; q = p) { 1188 p = TAILQ_NEXT(q, ipqe_q); 1189 m_freem(q->ipqe_m); 1190 nfrags++; 1191 TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q); 1192 pool_put(&ipqent_pool, q); 1193 } 1194 1195 if (nfrags != fp->ipq_nfrags) 1196 printf("ip_freef: nfrags %d != %d\n", fp->ipq_nfrags, nfrags); 1197 ip_nfrags -= nfrags; 1198 LIST_REMOVE(fp, ipq_q); 1199 FREE(fp, M_FTABLE); 1200 ip_nfragpackets--; 1201} 1202 1203/* 1204 * IP timer processing; 1205 * if a timer expires on a reassembly 1206 * queue, discard it. 1207 */ 1208void 1209ip_slowtimo() 1210{ 1211 static u_int dropscanidx = 0; 1212 u_int i; 1213 struct ipq *fp, *nfp; 1214 int s = splsoftnet(); 1215 1216 IPQ_LOCK(); 1217 for (i = 0; i < IPREASS_NHASH; i++) { 1218 for (fp = LIST_FIRST(&ipq[i]); fp != NULL; fp = nfp) { 1219 nfp = LIST_NEXT(fp, ipq_q); 1220 if (--fp->ipq_ttl == 0) { 1221 ipstat.ips_fragtimeout++; 1222 ip_freef(fp); 1223 } 1224 } 1225 } 1226 /* 1227 * If we are over the maximum number of fragments 1228 * (due to the limit being lowered), drain off 1229 * enough to get down to the new limit. Start draining 1230 * from the reassembly hashqueue most recently drained. 1231 */ 1232 if (ip_maxfragpackets < 0) 1233 ; 1234 else { 1235 int wrapped = 0; 1236 1237 i = dropscanidx; 1238 while (ip_nfragpackets > ip_maxfragpackets && wrapped == 0) { 1239 while (LIST_FIRST(&ipq[i]) != NULL) 1240 ip_freef(LIST_FIRST(&ipq[i])); 1241 if (++i >= IPREASS_NHASH) { 1242 i = 0; 1243 } 1244 /* 1245 * Dont scan forever even if fragment counters are 1246 * wrong: stop after scanning entire reassembly queue. 1247 */ 1248 if (i == dropscanidx) 1249 wrapped = 1; 1250 } 1251 dropscanidx = i; 1252 } 1253 IPQ_UNLOCK(); 1254#ifdef GATEWAY 1255 ipflow_slowtimo(); 1256#endif 1257 splx(s); 1258} 1259 1260/* 1261 * Drain off all datagram fragments. 1262 */ 1263void 1264ip_drain() 1265{ 1266 int i; 1267 1268 /* 1269 * We may be called from a device's interrupt context. If 1270 * the ipq is already busy, just bail out now. 1271 */ 1272 if (ipq_lock_try() == 0) 1273 return; 1274 1275 for (i = 0; i < IPREASS_NHASH; i++) { 1276 struct ipqhead *ipqh = &ipq[i]; 1277 struct ipq *fp, *nfp; 1278 for (fp = LIST_FIRST(ipqh); fp != NULL; fp = nfp) { 1279 nfp = LIST_NEXT(fp, ipq_q); 1280 ip_freef(fp); 1281 ipstat.ips_fragdropped++; 1282 } 1283 } 1284 1285 IPQ_UNLOCK(); 1286} 1287 1288/* 1289 * Do option processing on a datagram, 1290 * possibly discarding it if bad options are encountered, 1291 * or forwarding it if source-routed. 1292 * Returns 1 if packet has been forwarded/freed, 1293 * 0 if the packet should be processed further. 1294 */ 1295int 1296ip_dooptions(m) 1297 struct mbuf *m; 1298{ 1299 struct ip *ip = mtod(m, struct ip *); 1300 u_char *cp, *cp0; 1301 struct ip_timestamp *ipt; 1302 struct in_ifaddr *ia; 1303 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1304 struct in_addr dst; 1305 n_time ntime; 1306 1307 dst = ip->ip_dst; 1308 cp = (u_char *)(ip + 1); 1309 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1310 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1311 opt = cp[IPOPT_OPTVAL]; 1312 if (opt == IPOPT_EOL) 1313 break; 1314 if (opt == IPOPT_NOP) 1315 optlen = 1; 1316 else { 1317 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1318 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1319 goto bad; 1320 } 1321 optlen = cp[IPOPT_OLEN]; 1322 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1323 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1324 goto bad; 1325 } 1326 } 1327 switch (opt) { 1328 1329 default: 1330 break; 1331 1332 /* 1333 * Source routing with record. 1334 * Find interface with current destination address. 1335 * If none on this machine then drop if strictly routed, 1336 * or do nothing if loosely routed. 1337 * Record interface address and bring up next address 1338 * component. If strictly routed make sure next 1339 * address is on directly accessible net. 1340 */ 1341 case IPOPT_LSRR: 1342 case IPOPT_SSRR: 1343 if (ip_allowsrcrt == 0) { 1344 type = ICMP_UNREACH; 1345 code = ICMP_UNREACH_NET_PROHIB; 1346 goto bad; 1347 } 1348 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1349 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1350 goto bad; 1351 } 1352 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1353 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1354 goto bad; 1355 } 1356 ipaddr.sin_addr = ip->ip_dst; 1357 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))); 1358 if (ia == 0) { 1359 if (opt == IPOPT_SSRR) { 1360 type = ICMP_UNREACH; 1361 code = ICMP_UNREACH_SRCFAIL; 1362 goto bad; 1363 } 1364 /* 1365 * Loose routing, and not at next destination 1366 * yet; nothing to do except forward. 1367 */ 1368 break; 1369 } 1370 off--; /* 0 origin */ 1371 if ((off + sizeof(struct in_addr)) > optlen) { 1372 /* 1373 * End of source route. Should be for us. 1374 */ 1375 save_rte(cp, ip->ip_src); 1376 break; 1377 } 1378 /* 1379 * locate outgoing interface 1380 */ 1381 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 1382 sizeof(ipaddr.sin_addr)); 1383 if (opt == IPOPT_SSRR) 1384 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))); 1385 else 1386 ia = ip_rtaddr(ipaddr.sin_addr); 1387 if (ia == 0) { 1388 type = ICMP_UNREACH; 1389 code = ICMP_UNREACH_SRCFAIL; 1390 goto bad; 1391 } 1392 ip->ip_dst = ipaddr.sin_addr; 1393 bcopy((caddr_t)&ia->ia_addr.sin_addr, 1394 (caddr_t)(cp + off), sizeof(struct in_addr)); 1395 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1396 /* 1397 * Let ip_intr's mcast routing check handle mcast pkts 1398 */ 1399 forward = !IN_MULTICAST(ip->ip_dst.s_addr); 1400 break; 1401 1402 case IPOPT_RR: 1403 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1404 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1405 goto bad; 1406 } 1407 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1408 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1409 goto bad; 1410 } 1411 /* 1412 * If no space remains, ignore. 1413 */ 1414 off--; /* 0 origin */ 1415 if ((off + sizeof(struct in_addr)) > optlen) 1416 break; 1417 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 1418 sizeof(ipaddr.sin_addr)); 1419 /* 1420 * locate outgoing interface; if we're the destination, 1421 * use the incoming interface (should be same). 1422 */ 1423 if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))) 1424 == NULL && 1425 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) { 1426 type = ICMP_UNREACH; 1427 code = ICMP_UNREACH_HOST; 1428 goto bad; 1429 } 1430 bcopy((caddr_t)&ia->ia_addr.sin_addr, 1431 (caddr_t)(cp + off), sizeof(struct in_addr)); 1432 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1433 break; 1434 1435 case IPOPT_TS: 1436 code = cp - (u_char *)ip; 1437 ipt = (struct ip_timestamp *)cp; 1438 if (ipt->ipt_len < 4 || ipt->ipt_len > 40) { 1439 code = (u_char *)&ipt->ipt_len - (u_char *)ip; 1440 goto bad; 1441 } 1442 if (ipt->ipt_ptr < 5) { 1443 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip; 1444 goto bad; 1445 } 1446 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) { 1447 if (++ipt->ipt_oflw == 0) { 1448 code = (u_char *)&ipt->ipt_ptr - 1449 (u_char *)ip; 1450 goto bad; 1451 } 1452 break; 1453 } 1454 cp0 = (cp + ipt->ipt_ptr - 1); 1455 switch (ipt->ipt_flg) { 1456 1457 case IPOPT_TS_TSONLY: 1458 break; 1459 1460 case IPOPT_TS_TSANDADDR: 1461 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1462 sizeof(struct in_addr) > ipt->ipt_len) { 1463 code = (u_char *)&ipt->ipt_ptr - 1464 (u_char *)ip; 1465 goto bad; 1466 } 1467 ipaddr.sin_addr = dst; 1468 ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr), 1469 m->m_pkthdr.rcvif)); 1470 if (ia == 0) 1471 continue; 1472 bcopy(&ia->ia_addr.sin_addr, 1473 cp0, sizeof(struct in_addr)); 1474 ipt->ipt_ptr += sizeof(struct in_addr); 1475 break; 1476 1477 case IPOPT_TS_PRESPEC: 1478 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1479 sizeof(struct in_addr) > ipt->ipt_len) { 1480 code = (u_char *)&ipt->ipt_ptr - 1481 (u_char *)ip; 1482 goto bad; 1483 } 1484 bcopy(cp0, &ipaddr.sin_addr, 1485 sizeof(struct in_addr)); 1486 if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))) 1487 == NULL) 1488 continue; 1489 ipt->ipt_ptr += sizeof(struct in_addr); 1490 break; 1491 1492 default: 1493 /* XXX can't take &ipt->ipt_flg */ 1494 code = (u_char *)&ipt->ipt_ptr - 1495 (u_char *)ip + 1; 1496 goto bad; 1497 } 1498 ntime = iptime(); 1499 cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */ 1500 bcopy(cp0, (caddr_t)cp + ipt->ipt_ptr - 1, 1501 sizeof(n_time)); 1502 ipt->ipt_ptr += sizeof(n_time); 1503 } 1504 } 1505 if (forward) { 1506 if (ip_forwsrcrt == 0) { 1507 type = ICMP_UNREACH; 1508 code = ICMP_UNREACH_SRCFAIL; 1509 goto bad; 1510 } 1511 ip_forward(m, 1); 1512 return (1); 1513 } 1514 return (0); 1515bad: 1516 icmp_error(m, type, code, 0, 0); 1517 ipstat.ips_badoptions++; 1518 return (1); 1519} 1520 1521/* 1522 * Given address of next destination (final or next hop), 1523 * return internet address info of interface to be used to get there. 1524 */ 1525struct in_ifaddr * 1526ip_rtaddr(dst) 1527 struct in_addr dst; 1528{ 1529 struct sockaddr_in *sin; 1530 1531 sin = satosin(&ipforward_rt.ro_dst); 1532 1533 if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) { 1534 if (ipforward_rt.ro_rt) { 1535 RTFREE(ipforward_rt.ro_rt); 1536 ipforward_rt.ro_rt = 0; 1537 } 1538 sin->sin_family = AF_INET; 1539 sin->sin_len = sizeof(*sin); 1540 sin->sin_addr = dst; 1541 1542 rtalloc(&ipforward_rt); 1543 } 1544 if (ipforward_rt.ro_rt == 0) 1545 return ((struct in_ifaddr *)0); 1546 return (ifatoia(ipforward_rt.ro_rt->rt_ifa)); 1547} 1548 1549/* 1550 * Save incoming source route for use in replies, 1551 * to be picked up later by ip_srcroute if the receiver is interested. 1552 */ 1553void 1554save_rte(option, dst) 1555 u_char *option; 1556 struct in_addr dst; 1557{ 1558 unsigned olen; 1559 1560 olen = option[IPOPT_OLEN]; 1561#ifdef DIAGNOSTIC 1562 if (ipprintfs) 1563 printf("save_rte: olen %d\n", olen); 1564#endif /* 0 */ 1565 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1566 return; 1567 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 1568 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1569 ip_srcrt.dst = dst; 1570} 1571 1572/* 1573 * Retrieve incoming source route for use in replies, 1574 * in the same form used by setsockopt. 1575 * The first hop is placed before the options, will be removed later. 1576 */ 1577struct mbuf * 1578ip_srcroute() 1579{ 1580 struct in_addr *p, *q; 1581 struct mbuf *m; 1582 1583 if (ip_nhops == 0) 1584 return ((struct mbuf *)0); 1585 m = m_get(M_DONTWAIT, MT_SOOPTS); 1586 if (m == 0) 1587 return ((struct mbuf *)0); 1588 1589 MCLAIM(m, &inetdomain.dom_mowner); 1590#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1591 1592 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1593 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1594 OPTSIZ; 1595#ifdef DIAGNOSTIC 1596 if (ipprintfs) 1597 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1598#endif 1599 1600 /* 1601 * First save first hop for return route 1602 */ 1603 p = &ip_srcrt.route[ip_nhops - 1]; 1604 *(mtod(m, struct in_addr *)) = *p--; 1605#ifdef DIAGNOSTIC 1606 if (ipprintfs) 1607 printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr)); 1608#endif 1609 1610 /* 1611 * Copy option fields and padding (nop) to mbuf. 1612 */ 1613 ip_srcrt.nop = IPOPT_NOP; 1614 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1615 bcopy((caddr_t)&ip_srcrt.nop, 1616 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 1617 q = (struct in_addr *)(mtod(m, caddr_t) + 1618 sizeof(struct in_addr) + OPTSIZ); 1619#undef OPTSIZ 1620 /* 1621 * Record return path as an IP source route, 1622 * reversing the path (pointers are now aligned). 1623 */ 1624 while (p >= ip_srcrt.route) { 1625#ifdef DIAGNOSTIC 1626 if (ipprintfs) 1627 printf(" %x", ntohl(q->s_addr)); 1628#endif 1629 *q++ = *p--; 1630 } 1631 /* 1632 * Last hop goes to final destination. 1633 */ 1634 *q = ip_srcrt.dst; 1635#ifdef DIAGNOSTIC 1636 if (ipprintfs) 1637 printf(" %x\n", ntohl(q->s_addr)); 1638#endif 1639 return (m); 1640} 1641 1642/* 1643 * Strip out IP options, at higher 1644 * level protocol in the kernel. 1645 * Second argument is buffer to which options 1646 * will be moved, and return value is their length. 1647 * XXX should be deleted; last arg currently ignored. 1648 */ 1649void 1650ip_stripoptions(m, mopt) 1651 struct mbuf *m; 1652 struct mbuf *mopt; 1653{ 1654 int i; 1655 struct ip *ip = mtod(m, struct ip *); 1656 caddr_t opts; 1657 int olen; 1658 1659 olen = (ip->ip_hl << 2) - sizeof (struct ip); 1660 opts = (caddr_t)(ip + 1); 1661 i = m->m_len - (sizeof (struct ip) + olen); 1662 bcopy(opts + olen, opts, (unsigned)i); 1663 m->m_len -= olen; 1664 if (m->m_flags & M_PKTHDR) 1665 m->m_pkthdr.len -= olen; 1666 ip->ip_len = htons(ntohs(ip->ip_len) - olen); 1667 ip->ip_hl = sizeof (struct ip) >> 2; 1668} 1669 1670const int inetctlerrmap[PRC_NCMDS] = { 1671 0, 0, 0, 0, 1672 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1673 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1674 EMSGSIZE, EHOSTUNREACH, 0, 0, 1675 0, 0, 0, 0, 1676 ENOPROTOOPT 1677}; 1678 1679/* 1680 * Forward a packet. If some error occurs return the sender 1681 * an icmp packet. Note we can't always generate a meaningful 1682 * icmp message because icmp doesn't have a large enough repertoire 1683 * of codes and types. 1684 * 1685 * If not forwarding, just drop the packet. This could be confusing 1686 * if ipforwarding was zero but some routing protocol was advancing 1687 * us as a gateway to somewhere. However, we must let the routing 1688 * protocol deal with that. 1689 * 1690 * The srcrt parameter indicates whether the packet is being forwarded 1691 * via a source route. 1692 */ 1693void 1694ip_forward(m, srcrt) 1695 struct mbuf *m; 1696 int srcrt; 1697{ 1698 struct ip *ip = mtod(m, struct ip *); 1699 struct sockaddr_in *sin; 1700 struct rtentry *rt; 1701 int error, type = 0, code = 0; 1702 struct mbuf *mcopy; 1703 n_long dest; 1704 struct ifnet *destifp; 1705#if defined(IPSEC) || defined(FAST_IPSEC) 1706 struct ifnet dummyifp; 1707#endif 1708 1709 /* 1710 * We are now in the output path. 1711 */ 1712 MCLAIM(m, &ip_tx_mowner); 1713 1714 /* 1715 * Clear any in-bound checksum flags for this packet. 1716 */ 1717 m->m_pkthdr.csum_flags = 0; 1718 1719 dest = 0; 1720#ifdef DIAGNOSTIC 1721 if (ipprintfs) 1722 printf("forward: src %2.2x dst %2.2x ttl %x\n", 1723 ntohl(ip->ip_src.s_addr), 1724 ntohl(ip->ip_dst.s_addr), ip->ip_ttl); 1725#endif 1726 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1727 ipstat.ips_cantforward++; 1728 m_freem(m); 1729 return; 1730 } 1731 if (ip->ip_ttl <= IPTTLDEC) { 1732 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1733 return; 1734 } 1735 ip->ip_ttl -= IPTTLDEC; 1736 1737 sin = satosin(&ipforward_rt.ro_dst); 1738 if ((rt = ipforward_rt.ro_rt) == 0 || 1739 !in_hosteq(ip->ip_dst, sin->sin_addr)) { 1740 if (ipforward_rt.ro_rt) { 1741 RTFREE(ipforward_rt.ro_rt); 1742 ipforward_rt.ro_rt = 0; 1743 } 1744 sin->sin_family = AF_INET; 1745 sin->sin_len = sizeof(struct sockaddr_in); 1746 sin->sin_addr = ip->ip_dst; 1747 1748 rtalloc(&ipforward_rt); 1749 if (ipforward_rt.ro_rt == 0) { 1750 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1751 return; 1752 } 1753 rt = ipforward_rt.ro_rt; 1754 } 1755 1756 /* 1757 * Save at most 68 bytes of the packet in case 1758 * we need to generate an ICMP message to the src. 1759 * Pullup to avoid sharing mbuf cluster between m and mcopy. 1760 */ 1761 mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT); 1762 if (mcopy) 1763 mcopy = m_pullup(mcopy, ip->ip_hl << 2); 1764 1765 /* 1766 * If forwarding packet using same interface that it came in on, 1767 * perhaps should send a redirect to sender to shortcut a hop. 1768 * Only send redirect if source is sending directly to us, 1769 * and if packet was not source routed (or has any options). 1770 * Also, don't send redirect if forwarding using a default route 1771 * or a route modified by a redirect. 1772 */ 1773 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1774 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1775 !in_nullhost(satosin(rt_key(rt))->sin_addr) && 1776 ipsendredirects && !srcrt) { 1777 if (rt->rt_ifa && 1778 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) == 1779 ifatoia(rt->rt_ifa)->ia_subnet) { 1780 if (rt->rt_flags & RTF_GATEWAY) 1781 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1782 else 1783 dest = ip->ip_dst.s_addr; 1784 /* 1785 * Router requirements says to only send host 1786 * redirects. 1787 */ 1788 type = ICMP_REDIRECT; 1789 code = ICMP_REDIRECT_HOST; 1790#ifdef DIAGNOSTIC 1791 if (ipprintfs) 1792 printf("redirect (%d) to %x\n", code, 1793 (u_int32_t)dest); 1794#endif 1795 } 1796 } 1797 1798 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1799 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 1800 (struct ip_moptions *)NULL, (struct socket *)NULL); 1801 1802 if (error) 1803 ipstat.ips_cantforward++; 1804 else { 1805 ipstat.ips_forward++; 1806 if (type) 1807 ipstat.ips_redirectsent++; 1808 else { 1809 if (mcopy) { 1810#ifdef GATEWAY 1811 if (mcopy->m_flags & M_CANFASTFWD) 1812 ipflow_create(&ipforward_rt, mcopy); 1813#endif 1814 m_freem(mcopy); 1815 } 1816 return; 1817 } 1818 } 1819 if (mcopy == NULL) 1820 return; 1821 destifp = NULL; 1822 1823 switch (error) { 1824 1825 case 0: /* forwarded, but need redirect */ 1826 /* type, code set above */ 1827 break; 1828 1829 case ENETUNREACH: /* shouldn't happen, checked above */ 1830 case EHOSTUNREACH: 1831 case ENETDOWN: 1832 case EHOSTDOWN: 1833 default: 1834 type = ICMP_UNREACH; 1835 code = ICMP_UNREACH_HOST; 1836 break; 1837 1838 case EMSGSIZE: 1839 type = ICMP_UNREACH; 1840 code = ICMP_UNREACH_NEEDFRAG; 1841#if !defined(IPSEC) && !defined(FAST_IPSEC) 1842 if (ipforward_rt.ro_rt) 1843 destifp = ipforward_rt.ro_rt->rt_ifp; 1844#else 1845 /* 1846 * If the packet is routed over IPsec tunnel, tell the 1847 * originator the tunnel MTU. 1848 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1849 * XXX quickhack!!! 1850 */ 1851 if (ipforward_rt.ro_rt) { 1852 struct secpolicy *sp; 1853 int ipsecerror; 1854 size_t ipsechdr; 1855 struct route *ro; 1856 1857 sp = ipsec4_getpolicybyaddr(mcopy, 1858 IPSEC_DIR_OUTBOUND, IP_FORWARDING, 1859 &ipsecerror); 1860 1861 if (sp == NULL) 1862 destifp = ipforward_rt.ro_rt->rt_ifp; 1863 else { 1864 /* count IPsec header size */ 1865 ipsechdr = ipsec4_hdrsiz(mcopy, 1866 IPSEC_DIR_OUTBOUND, NULL); 1867 1868 /* 1869 * find the correct route for outer IPv4 1870 * header, compute tunnel MTU. 1871 * 1872 * XXX BUG ALERT 1873 * The "dummyifp" code relies upon the fact 1874 * that icmp_error() touches only ifp->if_mtu. 1875 */ 1876 /*XXX*/ 1877 destifp = NULL; 1878 if (sp->req != NULL 1879 && sp->req->sav != NULL 1880 && sp->req->sav->sah != NULL) { 1881 ro = &sp->req->sav->sah->sa_route; 1882 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1883 dummyifp.if_mtu = 1884 ro->ro_rt->rt_rmx.rmx_mtu ? 1885 ro->ro_rt->rt_rmx.rmx_mtu : 1886 ro->ro_rt->rt_ifp->if_mtu; 1887 dummyifp.if_mtu -= ipsechdr; 1888 destifp = &dummyifp; 1889 } 1890 } 1891 1892#ifdef IPSEC 1893 key_freesp(sp); 1894#else 1895 KEY_FREESP(&sp); 1896#endif 1897 } 1898 } 1899#endif /*IPSEC*/ 1900 ipstat.ips_cantfrag++; 1901 break; 1902 1903 case ENOBUFS: 1904#if 1 1905 /* 1906 * a router should not generate ICMP_SOURCEQUENCH as 1907 * required in RFC1812 Requirements for IP Version 4 Routers. 1908 * source quench could be a big problem under DoS attacks, 1909 * or if the underlying interface is rate-limited. 1910 */ 1911 if (mcopy) 1912 m_freem(mcopy); 1913 return; 1914#else 1915 type = ICMP_SOURCEQUENCH; 1916 code = 0; 1917 break; 1918#endif 1919 } 1920 icmp_error(mcopy, type, code, dest, destifp); 1921} 1922 1923void 1924ip_savecontrol(inp, mp, ip, m) 1925 struct inpcb *inp; 1926 struct mbuf **mp; 1927 struct ip *ip; 1928 struct mbuf *m; 1929{ 1930 1931 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1932 struct timeval tv; 1933 1934 microtime(&tv); 1935 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1936 SCM_TIMESTAMP, SOL_SOCKET); 1937 if (*mp) 1938 mp = &(*mp)->m_next; 1939 } 1940 if (inp->inp_flags & INP_RECVDSTADDR) { 1941 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1942 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1943 if (*mp) 1944 mp = &(*mp)->m_next; 1945 } 1946#ifdef notyet 1947 /* 1948 * XXX 1949 * Moving these out of udp_input() made them even more broken 1950 * than they already were. 1951 * - fenner@parc.xerox.com 1952 */ 1953 /* options were tossed already */ 1954 if (inp->inp_flags & INP_RECVOPTS) { 1955 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1956 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1957 if (*mp) 1958 mp = &(*mp)->m_next; 1959 } 1960 /* ip_srcroute doesn't do what we want here, need to fix */ 1961 if (inp->inp_flags & INP_RECVRETOPTS) { 1962 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1963 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1964 if (*mp) 1965 mp = &(*mp)->m_next; 1966 } 1967#endif 1968 if (inp->inp_flags & INP_RECVIF) { 1969 struct sockaddr_dl sdl; 1970 1971 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]); 1972 sdl.sdl_family = AF_LINK; 1973 sdl.sdl_index = m->m_pkthdr.rcvif ? 1974 m->m_pkthdr.rcvif->if_index : 0; 1975 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0; 1976 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len, 1977 IP_RECVIF, IPPROTO_IP); 1978 if (*mp) 1979 mp = &(*mp)->m_next; 1980 } 1981} 1982 1983/* 1984 * sysctl helper routine for net.inet.ip.mtudisctimeout. checks the 1985 * range of the new value and tweaks timers if it changes. 1986 */ 1987static int 1988sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS) 1989{ 1990 int error, tmp; 1991 struct sysctlnode node; 1992 1993 node = *rnode; 1994 tmp = ip_mtudisc_timeout; 1995 node.sysctl_data = &tmp; 1996 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1997 if (error || newp == NULL) 1998 return (error); 1999 if (tmp < 0) 2000 return (EINVAL); 2001 2002 ip_mtudisc_timeout = tmp; 2003 rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout); 2004 2005 return (0); 2006} 2007 2008#ifdef GATEWAY 2009/* 2010 * sysctl helper routine for net.inet.ip.maxflows. apparently if 2011 * maxflows is even looked up, we "reap flows". 2012 */ 2013static int 2014sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS) 2015{ 2016 int s; 2017 2018 s = sysctl_lookup(SYSCTLFN_CALL(rnode)); 2019 if (s) 2020 return (s); 2021 2022 s = splsoftnet(); 2023 ipflow_reap(0); 2024 splx(s); 2025 2026 return (0); 2027} 2028#endif /* GATEWAY */ 2029 2030 2031SYSCTL_SETUP(sysctl_net_inet_ip_setup, "sysctl net.inet.ip subtree setup") 2032{ 2033 extern int subnetsarelocal, hostzeroisbroadcast; 2034 2035 sysctl_createv(SYSCTL_PERMANENT, 2036 CTLTYPE_NODE, "net", NULL, 2037 NULL, 0, NULL, 0, 2038 CTL_NET, CTL_EOL); 2039 sysctl_createv(SYSCTL_PERMANENT, 2040 CTLTYPE_NODE, "inet", NULL, 2041 NULL, 0, NULL, 0, 2042 CTL_NET, PF_INET, CTL_EOL); 2043 sysctl_createv(SYSCTL_PERMANENT, 2044 CTLTYPE_NODE, "ip", NULL, 2045 NULL, 0, NULL, 0, 2046 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL); 2047 2048 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2049 CTLTYPE_INT, "forwarding", NULL, 2050 NULL, 0, &ipforwarding, 0, 2051 CTL_NET, PF_INET, IPPROTO_IP, 2052 IPCTL_FORWARDING, CTL_EOL); 2053 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2054 CTLTYPE_INT, "redirect", NULL, 2055 NULL, 0, &ipsendredirects, 0, 2056 CTL_NET, PF_INET, IPPROTO_IP, 2057 IPCTL_SENDREDIRECTS, CTL_EOL); 2058 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2059 CTLTYPE_INT, "ttl", NULL, 2060 NULL, 0, &ip_defttl, 0, 2061 CTL_NET, PF_INET, IPPROTO_IP, 2062 IPCTL_DEFTTL, CTL_EOL); 2063#ifdef IPCTL_DEFMTU 2064 sysctl_createv(SYSCTL_PERMANENT /* |SYSCTL_READWRITE? */, 2065 CTLTYPE_INT, "mtu", NULL, 2066 NULL, 0, &ip_mtu, 0, 2067 CTL_NET, PF_INET, IPPROTO_IP, 2068 IPCTL_DEFMTU, CTL_EOL); 2069#endif /* IPCTL_DEFMTU */ 2070 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READONLY1, 2071 CTLTYPE_INT, "forwsrcrt", NULL, 2072 NULL, 0, &ip_forwsrcrt, 0, 2073 CTL_NET, PF_INET, IPPROTO_IP, 2074 IPCTL_FORWSRCRT, CTL_EOL); 2075 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2076 CTLTYPE_INT, "directed-broadcast", NULL, 2077 NULL, 0, &ip_directedbcast, 0, 2078 CTL_NET, PF_INET, IPPROTO_IP, 2079 IPCTL_DIRECTEDBCAST, CTL_EOL); 2080 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2081 CTLTYPE_INT, "allowsrcrt", NULL, 2082 NULL, 0, &ip_allowsrcrt, 0, 2083 CTL_NET, PF_INET, IPPROTO_IP, 2084 IPCTL_ALLOWSRCRT, CTL_EOL); 2085 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2086 CTLTYPE_INT, "subnetsarelocal", NULL, 2087 NULL, 0, &subnetsarelocal, 0, 2088 CTL_NET, PF_INET, IPPROTO_IP, 2089 IPCTL_SUBNETSARELOCAL, CTL_EOL); 2090 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2091 CTLTYPE_INT, "mtudisc", NULL, 2092 NULL, 0, &ip_mtudisc, 0, 2093 CTL_NET, PF_INET, IPPROTO_IP, 2094 IPCTL_MTUDISC, CTL_EOL); 2095 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2096 CTLTYPE_INT, "anonportmin", NULL, 2097 sysctl_net_inet_ip_ports, 0, &anonportmin, 0, 2098 CTL_NET, PF_INET, IPPROTO_IP, 2099 IPCTL_ANONPORTMIN, CTL_EOL); 2100 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2101 CTLTYPE_INT, "anonportmax", NULL, 2102 sysctl_net_inet_ip_ports, 0, &anonportmax, 0, 2103 CTL_NET, PF_INET, IPPROTO_IP, 2104 IPCTL_ANONPORTMAX, CTL_EOL); 2105 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2106 CTLTYPE_INT, "mtudisctimeout", NULL, 2107 sysctl_net_inet_ip_pmtudto, 0, &ip_mtudisc_timeout, 0, 2108 CTL_NET, PF_INET, IPPROTO_IP, 2109 IPCTL_MTUDISCTIMEOUT, CTL_EOL); 2110#ifdef GATEWAY 2111 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2112 CTLTYPE_INT, "maxflows", NULL, 2113 sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0, 2114 CTL_NET, PF_INET, IPPROTO_IP, 2115 IPCTL_MAXFLOWS, CTL_EOL); 2116#endif /* GATEWAY */ 2117 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2118 CTLTYPE_INT, "hostzerobroadcast", NULL, 2119 NULL, 0, &hostzeroisbroadcast, 0, 2120 CTL_NET, PF_INET, IPPROTO_IP, 2121 IPCTL_HOSTZEROBROADCAST, CTL_EOL); 2122#if NGIF > 0 2123 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2124 CTLTYPE_INT, "gifttl", NULL, 2125 NULL, 0, &ip_gif_ttl, 0, 2126 CTL_NET, PF_INET, IPPROTO_IP, 2127 IPCTL_GIF_TTL, CTL_EOL); 2128#endif /* NGIF */ 2129#ifndef IPNOPRIVPORTS 2130 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2131 CTLTYPE_INT, "lowportmin", NULL, 2132 sysctl_net_inet_ip_ports, 0, &lowportmin, 0, 2133 CTL_NET, PF_INET, IPPROTO_IP, 2134 IPCTL_LOWPORTMIN, CTL_EOL); 2135 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2136 CTLTYPE_INT, "lowportmax", NULL, 2137 sysctl_net_inet_ip_ports, 0, &lowportmax, 0, 2138 CTL_NET, PF_INET, IPPROTO_IP, 2139 IPCTL_LOWPORTMAX, CTL_EOL); 2140#endif /* IPNOPRIVPORTS */ 2141 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2142 CTLTYPE_INT, "maxfragpackets", NULL, 2143 NULL, 0, &ip_maxfragpackets, 0, 2144 CTL_NET, PF_INET, IPPROTO_IP, 2145 IPCTL_MAXFRAGPACKETS, CTL_EOL); 2146#if NGRE > 0 2147 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2148 CTLTYPE_INT, "grettl", NULL, 2149 NULL, 0, &ip_gre_ttl, 0, 2150 CTL_NET, PF_INET, IPPROTO_IP, 2151 IPCTL_GRE_TTL, CTL_EOL); 2152#endif /* NGRE */ 2153 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2154 CTLTYPE_INT, "checkinterface", NULL, 2155 NULL, 0, &ip_checkinterface, 0, 2156 CTL_NET, PF_INET, IPPROTO_IP, 2157 IPCTL_CHECKINTERFACE, CTL_EOL); 2158 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 2159 CTLTYPE_INT, "random_id", NULL, 2160 NULL, 0, &ip_do_randomid, 0, 2161 CTL_NET, PF_INET, IPPROTO_IP, 2162 IPCTL_RANDOMID, CTL_EOL); 2163} 2164