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