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