ip_input.c revision 1.313
1/* $NetBSD: ip_input.c,v 1.313 2014/05/23 19:35:24 rmind 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 * 49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 59 * POSSIBILITY OF SUCH DAMAGE. 60 */ 61 62/* 63 * Copyright (c) 1982, 1986, 1988, 1993 64 * The Regents of the University of California. All rights reserved. 65 * 66 * Redistribution and use in source and binary forms, with or without 67 * modification, are permitted provided that the following conditions 68 * are met: 69 * 1. Redistributions of source code must retain the above copyright 70 * notice, this list of conditions and the following disclaimer. 71 * 2. Redistributions in binary form must reproduce the above copyright 72 * notice, this list of conditions and the following disclaimer in the 73 * documentation and/or other materials provided with the distribution. 74 * 3. Neither the name of the University nor the names of its contributors 75 * may be used to endorse or promote products derived from this software 76 * without specific prior written permission. 77 * 78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 88 * SUCH DAMAGE. 89 * 90 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 91 */ 92 93#include <sys/cdefs.h> 94__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.313 2014/05/23 19:35:24 rmind Exp $"); 95 96#include "opt_inet.h" 97#include "opt_compat_netbsd.h" 98#include "opt_gateway.h" 99#include "opt_ipsec.h" 100#include "opt_mrouting.h" 101#include "opt_mbuftrace.h" 102#include "opt_inet_csum.h" 103 104#include <sys/param.h> 105#include <sys/systm.h> 106#include <sys/mbuf.h> 107#include <sys/domain.h> 108#include <sys/protosw.h> 109#include <sys/socket.h> 110#include <sys/socketvar.h> 111#include <sys/errno.h> 112#include <sys/time.h> 113#include <sys/kernel.h> 114#include <sys/pool.h> 115#include <sys/sysctl.h> 116#include <sys/kauth.h> 117 118#include <net/if.h> 119#include <net/if_dl.h> 120#include <net/route.h> 121#include <net/pfil.h> 122 123#include <netinet/in.h> 124#include <netinet/in_systm.h> 125#include <netinet/ip.h> 126#include <netinet/in_pcb.h> 127#include <netinet/in_proto.h> 128#include <netinet/in_var.h> 129#include <netinet/ip_var.h> 130#include <netinet/ip_private.h> 131#include <netinet/ip_icmp.h> 132/* just for gif_ttl */ 133#include <netinet/in_gif.h> 134#include "gif.h" 135#include <net/if_gre.h> 136#include "gre.h" 137 138#ifdef MROUTING 139#include <netinet/ip_mroute.h> 140#endif 141#include <netinet/portalgo.h> 142 143#ifdef IPSEC 144#include <netipsec/ipsec.h> 145#endif 146 147#ifndef IPFORWARDING 148#ifdef GATEWAY 149#define IPFORWARDING 1 /* forward IP packets not for us */ 150#else /* GATEWAY */ 151#define IPFORWARDING 0 /* don't forward IP packets not for us */ 152#endif /* GATEWAY */ 153#endif /* IPFORWARDING */ 154#ifndef IPSENDREDIRECTS 155#define IPSENDREDIRECTS 1 156#endif 157#ifndef IPFORWSRCRT 158#define IPFORWSRCRT 1 /* forward source-routed packets */ 159#endif 160#ifndef IPALLOWSRCRT 161#define IPALLOWSRCRT 1 /* allow source-routed packets */ 162#endif 163#ifndef IPMTUDISC 164#define IPMTUDISC 1 165#endif 166#ifndef IPMTUDISCTIMEOUT 167#define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */ 168#endif 169 170#ifdef COMPAT_50 171#include <compat/sys/time.h> 172#include <compat/sys/socket.h> 173#endif 174 175/* 176 * Note: DIRECTED_BROADCAST is handled this way so that previous 177 * configuration using this option will Just Work. 178 */ 179#ifndef IPDIRECTEDBCAST 180#ifdef DIRECTED_BROADCAST 181#define IPDIRECTEDBCAST 1 182#else 183#define IPDIRECTEDBCAST 0 184#endif /* DIRECTED_BROADCAST */ 185#endif /* IPDIRECTEDBCAST */ 186int ipforwarding = IPFORWARDING; 187int ipsendredirects = IPSENDREDIRECTS; 188int ip_defttl = IPDEFTTL; 189int ip_forwsrcrt = IPFORWSRCRT; 190int ip_directedbcast = IPDIRECTEDBCAST; 191int ip_allowsrcrt = IPALLOWSRCRT; 192int ip_mtudisc = IPMTUDISC; 193int ip_mtudisc_timeout = IPMTUDISCTIMEOUT; 194#ifdef DIAGNOSTIC 195int ipprintfs = 0; 196#endif 197 198int ip_do_randomid = 0; 199 200/* 201 * XXX - Setting ip_checkinterface mostly implements the receive side of 202 * the Strong ES model described in RFC 1122, but since the routing table 203 * and transmit implementation do not implement the Strong ES model, 204 * setting this to 1 results in an odd hybrid. 205 * 206 * XXX - ip_checkinterface currently must be disabled if you use ipnat 207 * to translate the destination address to another local interface. 208 * 209 * XXX - ip_checkinterface must be disabled if you add IP aliases 210 * to the loopback interface instead of the interface where the 211 * packets for those addresses are received. 212 */ 213int ip_checkinterface = 0; 214 215struct rttimer_queue *ip_mtudisc_timeout_q = NULL; 216 217struct ifqueue ipintrq; 218 219ipid_state_t * ip_ids; 220uint16_t ip_id; 221 222percpu_t *ipstat_percpu; 223 224pfil_head_t *inet_pfil_hook; 225 226#ifdef INET_CSUM_COUNTERS 227#include <sys/device.h> 228 229struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 230 NULL, "inet", "hwcsum bad"); 231struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 232 NULL, "inet", "hwcsum ok"); 233struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 234 NULL, "inet", "swcsum"); 235 236#define INET_CSUM_COUNTER_INCR(ev) (ev)->ev_count++ 237 238EVCNT_ATTACH_STATIC(ip_hwcsum_bad); 239EVCNT_ATTACH_STATIC(ip_hwcsum_ok); 240EVCNT_ATTACH_STATIC(ip_swcsum); 241 242#else 243 244#define INET_CSUM_COUNTER_INCR(ev) /* nothing */ 245 246#endif /* INET_CSUM_COUNTERS */ 247 248/* 249 * We need to save the IP options in case a protocol wants to respond 250 * to an incoming packet over the same route if the packet got here 251 * using IP source routing. This allows connection establishment and 252 * maintenance when the remote end is on a network that is not known 253 * to us. 254 */ 255 256static int ip_nhops = 0; 257 258static struct ip_srcrt { 259 struct in_addr dst; /* final destination */ 260 char nop; /* one NOP to align */ 261 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 262 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 263} ip_srcrt; 264 265static int ip_drainwanted; 266 267static void save_rte(u_char *, struct in_addr); 268 269#ifdef MBUFTRACE 270struct mowner ip_rx_mowner = MOWNER_INIT("internet", "rx"); 271struct mowner ip_tx_mowner = MOWNER_INIT("internet", "tx"); 272#endif 273 274static void ip_input(struct mbuf *); 275static void ip_forward(struct mbuf *, int); 276static bool ip_dooptions(struct mbuf *); 277static struct in_ifaddr *ip_rtaddr(struct in_addr); 278static void sysctl_net_inet_ip_setup(struct sysctllog **); 279 280/* 281 * IP initialization: fill in IP protocol switch table. 282 * All protocols not implemented in kernel go to raw IP protocol handler. 283 */ 284void 285ip_init(void) 286{ 287 const struct protosw *pr; 288 289 in_init(); 290 sysctl_net_inet_ip_setup(NULL); 291 292 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 293 KASSERT(pr != NULL); 294 295 for (u_int i = 0; i < IPPROTO_MAX; i++) { 296 ip_protox[i] = pr - inetsw; 297 } 298 for (pr = inetdomain.dom_protosw; 299 pr < inetdomain.dom_protoswNPROTOSW; pr++) 300 if (pr->pr_domain->dom_family == PF_INET && 301 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 302 ip_protox[pr->pr_protocol] = pr - inetsw; 303 304 ip_reass_init(); 305 306 ip_ids = ip_id_init(); 307 ip_id = time_second & 0xfffff; 308 309 ipintrq.ifq_maxlen = IFQ_MAXLEN; 310 311 ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout); 312#ifdef GATEWAY 313 ipflow_init(); 314#endif 315 316 /* Register our Packet Filter hook. */ 317 inet_pfil_hook = pfil_head_create(PFIL_TYPE_AF, (void *)AF_INET); 318 KASSERT(inet_pfil_hook != NULL); 319 320#ifdef MBUFTRACE 321 MOWNER_ATTACH(&ip_tx_mowner); 322 MOWNER_ATTACH(&ip_rx_mowner); 323#endif /* MBUFTRACE */ 324 325 ipstat_percpu = percpu_alloc(sizeof(uint64_t) * IP_NSTATS); 326} 327 328struct sockaddr_in ipaddr = { 329 .sin_len = sizeof(ipaddr), 330 .sin_family = AF_INET, 331}; 332 333static struct route ipforward_rt; 334 335/* 336 * IP software interrupt routine 337 */ 338void 339ipintr(void) 340{ 341 int s; 342 struct mbuf *m; 343 struct ifqueue lcl_intrq; 344 345 memset(&lcl_intrq, 0, sizeof(lcl_intrq)); 346 347 mutex_enter(softnet_lock); 348 KERNEL_LOCK(1, NULL); 349 if (!IF_IS_EMPTY(&ipintrq)) { 350 s = splnet(); 351 352 /* Take existing queue onto stack */ 353 lcl_intrq = ipintrq; 354 355 /* Zero out global queue, preserving maxlen and drops */ 356 ipintrq.ifq_head = NULL; 357 ipintrq.ifq_tail = NULL; 358 ipintrq.ifq_len = 0; 359 ipintrq.ifq_maxlen = lcl_intrq.ifq_maxlen; 360 ipintrq.ifq_drops = lcl_intrq.ifq_drops; 361 362 splx(s); 363 } 364 KERNEL_UNLOCK_ONE(NULL); 365 while (!IF_IS_EMPTY(&lcl_intrq)) { 366 IF_DEQUEUE(&lcl_intrq, m); 367 if (m == NULL) 368 break; 369 ip_input(m); 370 } 371 mutex_exit(softnet_lock); 372} 373 374/* 375 * Ip input routine. Checksum and byte swap header. If fragmented 376 * try to reassemble. Process options. Pass to next level. 377 */ 378static void 379ip_input(struct mbuf *m) 380{ 381 struct ip *ip = NULL; 382 struct in_ifaddr *ia; 383 struct ifaddr *ifa; 384 int hlen = 0, len; 385 int downmatch; 386 int checkif; 387 int srcrt = 0; 388 389 MCLAIM(m, &ip_rx_mowner); 390 KASSERT((m->m_flags & M_PKTHDR) != 0); 391 392 /* 393 * If no IP addresses have been set yet but the interfaces 394 * are receiving, can't do anything with incoming packets yet. 395 */ 396 if (TAILQ_FIRST(&in_ifaddrhead) == 0) 397 goto bad; 398 IP_STATINC(IP_STAT_TOTAL); 399 /* 400 * If the IP header is not aligned, slurp it up into a new 401 * mbuf with space for link headers, in the event we forward 402 * it. Otherwise, if it is aligned, make sure the entire 403 * base IP header is in the first mbuf of the chain. 404 */ 405 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) { 406 if ((m = m_copyup(m, sizeof(struct ip), 407 (max_linkhdr + 3) & ~3)) == NULL) { 408 /* XXXJRT new stat, please */ 409 IP_STATINC(IP_STAT_TOOSMALL); 410 return; 411 } 412 } else if (__predict_false(m->m_len < sizeof (struct ip))) { 413 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) { 414 IP_STATINC(IP_STAT_TOOSMALL); 415 return; 416 } 417 } 418 ip = mtod(m, struct ip *); 419 if (ip->ip_v != IPVERSION) { 420 IP_STATINC(IP_STAT_BADVERS); 421 goto bad; 422 } 423 hlen = ip->ip_hl << 2; 424 if (hlen < sizeof(struct ip)) { /* minimum header length */ 425 IP_STATINC(IP_STAT_BADHLEN); 426 goto bad; 427 } 428 if (hlen > m->m_len) { 429 if ((m = m_pullup(m, hlen)) == NULL) { 430 IP_STATINC(IP_STAT_BADHLEN); 431 return; 432 } 433 ip = mtod(m, struct ip *); 434 } 435 436 /* 437 * RFC1122: packets with a multicast source address are 438 * not allowed. 439 */ 440 if (IN_MULTICAST(ip->ip_src.s_addr)) { 441 IP_STATINC(IP_STAT_BADADDR); 442 goto bad; 443 } 444 445 /* 127/8 must not appear on wire - RFC1122 */ 446 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 447 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 448 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 449 IP_STATINC(IP_STAT_BADADDR); 450 goto bad; 451 } 452 } 453 454 switch (m->m_pkthdr.csum_flags & 455 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) | 456 M_CSUM_IPv4_BAD)) { 457 case M_CSUM_IPv4|M_CSUM_IPv4_BAD: 458 INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); 459 goto badcsum; 460 461 case M_CSUM_IPv4: 462 /* Checksum was okay. */ 463 INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); 464 break; 465 466 default: 467 /* 468 * Must compute it ourselves. Maybe skip checksum on 469 * loopback interfaces. 470 */ 471 if (__predict_true(!(m->m_pkthdr.rcvif->if_flags & 472 IFF_LOOPBACK) || ip_do_loopback_cksum)) { 473 INET_CSUM_COUNTER_INCR(&ip_swcsum); 474 if (in_cksum(m, hlen) != 0) 475 goto badcsum; 476 } 477 break; 478 } 479 480 /* Retrieve the packet length. */ 481 len = ntohs(ip->ip_len); 482 483 /* 484 * Check for additional length bogosity 485 */ 486 if (len < hlen) { 487 IP_STATINC(IP_STAT_BADLEN); 488 goto bad; 489 } 490 491 /* 492 * Check that the amount of data in the buffers 493 * is as at least much as the IP header would have us expect. 494 * Trim mbufs if longer than we expect. 495 * Drop packet if shorter than we expect. 496 */ 497 if (m->m_pkthdr.len < len) { 498 IP_STATINC(IP_STAT_TOOSHORT); 499 goto bad; 500 } 501 if (m->m_pkthdr.len > len) { 502 if (m->m_len == m->m_pkthdr.len) { 503 m->m_len = len; 504 m->m_pkthdr.len = len; 505 } else 506 m_adj(m, len - m->m_pkthdr.len); 507 } 508 509 /* 510 * Assume that we can create a fast-forward IP flow entry 511 * based on this packet. 512 */ 513 m->m_flags |= M_CANFASTFWD; 514 515 /* 516 * Run through list of hooks for input packets. If there are any 517 * filters which require that additional packets in the flow are 518 * not fast-forwarded, they must clear the M_CANFASTFWD flag. 519 * Note that filters must _never_ set this flag, as another filter 520 * in the list may have previously cleared it. 521 */ 522 /* 523 * let ipfilter look at packet on the wire, 524 * not the decapsulated packet. 525 */ 526#if defined(IPSEC) 527 if (!ipsec_indone(m)) 528#else 529 if (1) 530#endif 531 { 532 struct in_addr odst; 533 534 odst = ip->ip_dst; 535 if (pfil_run_hooks(inet_pfil_hook, &m, m->m_pkthdr.rcvif, 536 PFIL_IN) != 0) 537 return; 538 if (m == NULL) 539 return; 540 ip = mtod(m, struct ip *); 541 hlen = ip->ip_hl << 2; 542 /* 543 * XXX The setting of "srcrt" here is to prevent ip_forward() 544 * from generating ICMP redirects for packets that have 545 * been redirected by a hook back out on to the same LAN that 546 * they came from and is not an indication that the packet 547 * is being inffluenced by source routing options. This 548 * allows things like 549 * "rdr tlp0 0/0 port 80 -> 1.1.1.200 3128 tcp" 550 * where tlp0 is both on the 1.1.1.0/24 network and is the 551 * default route for hosts on 1.1.1.0/24. Of course this 552 * also requires a "map tlp0 ..." to complete the story. 553 * One might argue whether or not this kind of network config. 554 * should be supported in this manner... 555 */ 556 srcrt = (odst.s_addr != ip->ip_dst.s_addr); 557 } 558 559#ifdef ALTQ 560 /* XXX Temporary until ALTQ is changed to use a pfil hook */ 561 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) { 562 /* packet dropped by traffic conditioner */ 563 return; 564 } 565#endif 566 567 /* 568 * Process options and, if not destined for us, 569 * ship it on. ip_dooptions returns 1 when an 570 * error was detected (causing an icmp message 571 * to be sent and the original packet to be freed). 572 */ 573 ip_nhops = 0; /* for source routed packets */ 574 if (hlen > sizeof (struct ip) && ip_dooptions(m)) 575 return; 576 577 /* 578 * Enable a consistency check between the destination address 579 * and the arrival interface for a unicast packet (the RFC 1122 580 * strong ES model) if IP forwarding is disabled and the packet 581 * is not locally generated. 582 * 583 * XXX - Checking also should be disabled if the destination 584 * address is ipnat'ed to a different interface. 585 * 586 * XXX - Checking is incompatible with IP aliases added 587 * to the loopback interface instead of the interface where 588 * the packets are received. 589 * 590 * XXX - We need to add a per ifaddr flag for this so that 591 * we get finer grain control. 592 */ 593 checkif = ip_checkinterface && (ipforwarding == 0) && 594 (m->m_pkthdr.rcvif != NULL) && 595 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0); 596 597 /* 598 * Check our list of addresses, to see if the packet is for us. 599 * 600 * Traditional 4.4BSD did not consult IFF_UP at all. 601 * The behavior here is to treat addresses on !IFF_UP interface 602 * as not mine. 603 */ 604 downmatch = 0; 605 LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 606 if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) { 607 if (checkif && ia->ia_ifp != m->m_pkthdr.rcvif) 608 continue; 609 if ((ia->ia_ifp->if_flags & IFF_UP) != 0) 610 break; 611 else 612 downmatch++; 613 } 614 } 615 if (ia != NULL) 616 goto ours; 617 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 618 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) { 619 if (ifa->ifa_addr->sa_family != AF_INET) 620 continue; 621 ia = ifatoia(ifa); 622 if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) || 623 in_hosteq(ip->ip_dst, ia->ia_netbroadcast) || 624 /* 625 * Look for all-0's host part (old broadcast addr), 626 * either for subnet or net. 627 */ 628 ip->ip_dst.s_addr == ia->ia_subnet || 629 ip->ip_dst.s_addr == ia->ia_net) 630 goto ours; 631 /* 632 * An interface with IP address zero accepts 633 * all packets that arrive on that interface. 634 */ 635 if (in_nullhost(ia->ia_addr.sin_addr)) 636 goto ours; 637 } 638 } 639 if (IN_MULTICAST(ip->ip_dst.s_addr)) { 640 struct in_multi *inm; 641#ifdef MROUTING 642 extern struct socket *ip_mrouter; 643 644 if (ip_mrouter) { 645 /* 646 * If we are acting as a multicast router, all 647 * incoming multicast packets are passed to the 648 * kernel-level multicast forwarding function. 649 * The packet is returned (relatively) intact; if 650 * ip_mforward() returns a non-zero value, the packet 651 * must be discarded, else it may be accepted below. 652 * 653 * (The IP ident field is put in the same byte order 654 * as expected when ip_mforward() is called from 655 * ip_output().) 656 */ 657 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { 658 IP_STATINC(IP_STAT_CANTFORWARD); 659 m_freem(m); 660 return; 661 } 662 663 /* 664 * The process-level routing demon needs to receive 665 * all multicast IGMP packets, whether or not this 666 * host belongs to their destination groups. 667 */ 668 if (ip->ip_p == IPPROTO_IGMP) 669 goto ours; 670 IP_STATINC(IP_STAT_CANTFORWARD); 671 } 672#endif 673 /* 674 * See if we belong to the destination multicast group on the 675 * arrival interface. 676 */ 677 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 678 if (inm == NULL) { 679 IP_STATINC(IP_STAT_CANTFORWARD); 680 m_freem(m); 681 return; 682 } 683 goto ours; 684 } 685 if (ip->ip_dst.s_addr == INADDR_BROADCAST || 686 in_nullhost(ip->ip_dst)) 687 goto ours; 688 689 /* 690 * Not for us; forward if possible and desirable. 691 */ 692 if (ipforwarding == 0) { 693 IP_STATINC(IP_STAT_CANTFORWARD); 694 m_freem(m); 695 } else { 696 /* 697 * If ip_dst matched any of my address on !IFF_UP interface, 698 * and there's no IFF_UP interface that matches ip_dst, 699 * send icmp unreach. Forwarding it will result in in-kernel 700 * forwarding loop till TTL goes to 0. 701 */ 702 if (downmatch) { 703 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 704 IP_STATINC(IP_STAT_CANTFORWARD); 705 return; 706 } 707#ifdef IPSEC 708 /* Perform IPsec, if any. */ 709 if (ipsec4_input(m, IP_FORWARDING | (ip_directedbcast ? 710 IP_ALLOWBROADCAST : 0)) != 0) { 711 goto bad; 712 } 713#endif 714 ip_forward(m, srcrt); 715 } 716 return; 717 718ours: 719 /* 720 * If offset or IP_MF are set, must reassemble. 721 */ 722 if (ip->ip_off & ~htons(IP_DF|IP_RF)) { 723 /* 724 * Pass to IP reassembly mechanism. 725 */ 726 if (ip_reass_packet(&m, ip) != 0) { 727 /* Failed; invalid fragment(s) or packet. */ 728 goto bad; 729 } 730 if (m == NULL) { 731 /* More fragments should come; silently return. */ 732 return; 733 } 734 /* 735 * Reassembly is done, we have the final packet. 736 * Updated cached data in local variable(s). 737 */ 738 ip = mtod(m, struct ip *); 739 hlen = ip->ip_hl << 2; 740 } 741 742#ifdef IPSEC 743 /* 744 * Enforce IPsec policy checking if we are seeing last header. 745 * Note that we do not visit this with protocols with PCB layer 746 * code - like UDP/TCP/raw IP. 747 */ 748 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) { 749 if (ipsec4_input(m, 0) != 0) { 750 goto bad; 751 } 752 } 753#endif 754 755 /* 756 * Switch out to protocol's input routine. 757 */ 758#if IFA_STATS 759 if (ia && ip) 760 ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len); 761#endif 762 IP_STATINC(IP_STAT_DELIVERED); 763 { 764 int off = hlen, nh = ip->ip_p; 765 766 (*inetsw[ip_protox[nh]].pr_input)(m, off, nh); 767 return; 768 } 769bad: 770 m_freem(m); 771 return; 772 773badcsum: 774 IP_STATINC(IP_STAT_BADSUM); 775 m_freem(m); 776} 777 778/* 779 * IP timer processing. 780 */ 781void 782ip_slowtimo(void) 783{ 784 785 mutex_enter(softnet_lock); 786 KERNEL_LOCK(1, NULL); 787 788 ip_reass_slowtimo(); 789 790 KERNEL_UNLOCK_ONE(NULL); 791 mutex_exit(softnet_lock); 792} 793 794/* 795 * IP drain processing. 796 */ 797void 798ip_drain(void) 799{ 800 801 KERNEL_LOCK(1, NULL); 802 ip_reass_drain(); 803 KERNEL_UNLOCK_ONE(NULL); 804} 805 806/* 807 * ip_dooptions: perform option processing on a datagram, possibly discarding 808 * it if bad options are encountered, or forwarding it if source-routed. 809 * 810 * => Returns true if packet has been forwarded/freed. 811 * => Returns false if the packet should be processed further. 812 */ 813static bool 814ip_dooptions(struct mbuf *m) 815{ 816 struct ip *ip = mtod(m, struct ip *); 817 u_char *cp, *cp0; 818 struct ip_timestamp *ipt; 819 struct in_ifaddr *ia; 820 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 821 struct in_addr dst; 822 n_time ntime; 823 824 dst = ip->ip_dst; 825 cp = (u_char *)(ip + 1); 826 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 827 for (; cnt > 0; cnt -= optlen, cp += optlen) { 828 opt = cp[IPOPT_OPTVAL]; 829 if (opt == IPOPT_EOL) 830 break; 831 if (opt == IPOPT_NOP) 832 optlen = 1; 833 else { 834 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 835 code = &cp[IPOPT_OLEN] - (u_char *)ip; 836 goto bad; 837 } 838 optlen = cp[IPOPT_OLEN]; 839 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 840 code = &cp[IPOPT_OLEN] - (u_char *)ip; 841 goto bad; 842 } 843 } 844 switch (opt) { 845 846 default: 847 break; 848 849 /* 850 * Source routing with record. 851 * Find interface with current destination address. 852 * If none on this machine then drop if strictly routed, 853 * or do nothing if loosely routed. 854 * Record interface address and bring up next address 855 * component. If strictly routed make sure next 856 * address is on directly accessible net. 857 */ 858 case IPOPT_LSRR: 859 case IPOPT_SSRR: 860 if (ip_allowsrcrt == 0) { 861 type = ICMP_UNREACH; 862 code = ICMP_UNREACH_NET_PROHIB; 863 goto bad; 864 } 865 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 866 code = &cp[IPOPT_OLEN] - (u_char *)ip; 867 goto bad; 868 } 869 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 870 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 871 goto bad; 872 } 873 ipaddr.sin_addr = ip->ip_dst; 874 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))); 875 if (ia == 0) { 876 if (opt == IPOPT_SSRR) { 877 type = ICMP_UNREACH; 878 code = ICMP_UNREACH_SRCFAIL; 879 goto bad; 880 } 881 /* 882 * Loose routing, and not at next destination 883 * yet; nothing to do except forward. 884 */ 885 break; 886 } 887 off--; /* 0 origin */ 888 if ((off + sizeof(struct in_addr)) > optlen) { 889 /* 890 * End of source route. Should be for us. 891 */ 892 save_rte(cp, ip->ip_src); 893 break; 894 } 895 /* 896 * locate outgoing interface 897 */ 898 memcpy((void *)&ipaddr.sin_addr, (void *)(cp + off), 899 sizeof(ipaddr.sin_addr)); 900 if (opt == IPOPT_SSRR) 901 ia = ifatoia(ifa_ifwithladdr(sintosa(&ipaddr))); 902 else 903 ia = ip_rtaddr(ipaddr.sin_addr); 904 if (ia == 0) { 905 type = ICMP_UNREACH; 906 code = ICMP_UNREACH_SRCFAIL; 907 goto bad; 908 } 909 ip->ip_dst = ipaddr.sin_addr; 910 bcopy((void *)&ia->ia_addr.sin_addr, 911 (void *)(cp + off), sizeof(struct in_addr)); 912 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 913 /* 914 * Let ip_intr's mcast routing check handle mcast pkts 915 */ 916 forward = !IN_MULTICAST(ip->ip_dst.s_addr); 917 break; 918 919 case IPOPT_RR: 920 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 921 code = &cp[IPOPT_OLEN] - (u_char *)ip; 922 goto bad; 923 } 924 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 925 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 926 goto bad; 927 } 928 /* 929 * If no space remains, ignore. 930 */ 931 off--; /* 0 origin */ 932 if ((off + sizeof(struct in_addr)) > optlen) 933 break; 934 memcpy((void *)&ipaddr.sin_addr, (void *)(&ip->ip_dst), 935 sizeof(ipaddr.sin_addr)); 936 /* 937 * locate outgoing interface; if we're the destination, 938 * use the incoming interface (should be same). 939 */ 940 if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))) 941 == NULL && 942 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) { 943 type = ICMP_UNREACH; 944 code = ICMP_UNREACH_HOST; 945 goto bad; 946 } 947 bcopy((void *)&ia->ia_addr.sin_addr, 948 (void *)(cp + off), sizeof(struct in_addr)); 949 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 950 break; 951 952 case IPOPT_TS: 953 code = cp - (u_char *)ip; 954 ipt = (struct ip_timestamp *)cp; 955 if (ipt->ipt_len < 4 || ipt->ipt_len > 40) { 956 code = (u_char *)&ipt->ipt_len - (u_char *)ip; 957 goto bad; 958 } 959 if (ipt->ipt_ptr < 5) { 960 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip; 961 goto bad; 962 } 963 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) { 964 if (++ipt->ipt_oflw == 0) { 965 code = (u_char *)&ipt->ipt_ptr - 966 (u_char *)ip; 967 goto bad; 968 } 969 break; 970 } 971 cp0 = (cp + ipt->ipt_ptr - 1); 972 switch (ipt->ipt_flg) { 973 974 case IPOPT_TS_TSONLY: 975 break; 976 977 case IPOPT_TS_TSANDADDR: 978 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 979 sizeof(struct in_addr) > ipt->ipt_len) { 980 code = (u_char *)&ipt->ipt_ptr - 981 (u_char *)ip; 982 goto bad; 983 } 984 ipaddr.sin_addr = dst; 985 ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr), 986 m->m_pkthdr.rcvif)); 987 if (ia == 0) 988 continue; 989 bcopy(&ia->ia_addr.sin_addr, 990 cp0, sizeof(struct in_addr)); 991 ipt->ipt_ptr += sizeof(struct in_addr); 992 break; 993 994 case IPOPT_TS_PRESPEC: 995 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 996 sizeof(struct in_addr) > ipt->ipt_len) { 997 code = (u_char *)&ipt->ipt_ptr - 998 (u_char *)ip; 999 goto bad; 1000 } 1001 memcpy(&ipaddr.sin_addr, cp0, 1002 sizeof(struct in_addr)); 1003 if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))) 1004 == NULL) 1005 continue; 1006 ipt->ipt_ptr += sizeof(struct in_addr); 1007 break; 1008 1009 default: 1010 /* XXX can't take &ipt->ipt_flg */ 1011 code = (u_char *)&ipt->ipt_ptr - 1012 (u_char *)ip + 1; 1013 goto bad; 1014 } 1015 ntime = iptime(); 1016 cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */ 1017 memmove((char *)cp + ipt->ipt_ptr - 1, cp0, 1018 sizeof(n_time)); 1019 ipt->ipt_ptr += sizeof(n_time); 1020 } 1021 } 1022 if (forward) { 1023 if (ip_forwsrcrt == 0) { 1024 type = ICMP_UNREACH; 1025 code = ICMP_UNREACH_SRCFAIL; 1026 goto bad; 1027 } 1028 ip_forward(m, 1); 1029 return true; 1030 } 1031 return false; 1032bad: 1033 icmp_error(m, type, code, 0, 0); 1034 IP_STATINC(IP_STAT_BADOPTIONS); 1035 return true; 1036} 1037 1038/* 1039 * ip_rtaddr: given address of next destination (final or next hop), 1040 * return internet address info of interface to be used to get there. 1041 */ 1042static struct in_ifaddr * 1043ip_rtaddr(struct in_addr dst) 1044{ 1045 struct rtentry *rt; 1046 union { 1047 struct sockaddr dst; 1048 struct sockaddr_in dst4; 1049 } u; 1050 1051 sockaddr_in_init(&u.dst4, &dst, 0); 1052 1053 if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL) 1054 return NULL; 1055 1056 return ifatoia(rt->rt_ifa); 1057} 1058 1059/* 1060 * save_rte: save incoming source route for use in replies, to be picked 1061 * up later by ip_srcroute if the receiver is interested. 1062 */ 1063static void 1064save_rte(u_char *option, struct in_addr dst) 1065{ 1066 unsigned olen; 1067 1068 olen = option[IPOPT_OLEN]; 1069 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1070 return; 1071 memcpy((void *)ip_srcrt.srcopt, (void *)option, olen); 1072 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1073 ip_srcrt.dst = dst; 1074} 1075 1076/* 1077 * Retrieve incoming source route for use in replies, 1078 * in the same form used by setsockopt. 1079 * The first hop is placed before the options, will be removed later. 1080 */ 1081struct mbuf * 1082ip_srcroute(void) 1083{ 1084 struct in_addr *p, *q; 1085 struct mbuf *m; 1086 1087 if (ip_nhops == 0) 1088 return NULL; 1089 m = m_get(M_DONTWAIT, MT_SOOPTS); 1090 if (m == 0) 1091 return NULL; 1092 1093 MCLAIM(m, &inetdomain.dom_mowner); 1094#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1095 1096 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1097 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1098 OPTSIZ; 1099 1100 /* 1101 * First save first hop for return route 1102 */ 1103 p = &ip_srcrt.route[ip_nhops - 1]; 1104 *(mtod(m, struct in_addr *)) = *p--; 1105 1106 /* 1107 * Copy option fields and padding (nop) to mbuf. 1108 */ 1109 ip_srcrt.nop = IPOPT_NOP; 1110 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1111 memmove(mtod(m, char *) + sizeof(struct in_addr), &ip_srcrt.nop, 1112 OPTSIZ); 1113 q = (struct in_addr *)(mtod(m, char *) + 1114 sizeof(struct in_addr) + OPTSIZ); 1115#undef OPTSIZ 1116 /* 1117 * Record return path as an IP source route, 1118 * reversing the path (pointers are now aligned). 1119 */ 1120 while (p >= ip_srcrt.route) { 1121 *q++ = *p--; 1122 } 1123 /* 1124 * Last hop goes to final destination. 1125 */ 1126 *q = ip_srcrt.dst; 1127 return (m); 1128} 1129 1130const int inetctlerrmap[PRC_NCMDS] = { 1131 [PRC_MSGSIZE] = EMSGSIZE, 1132 [PRC_HOSTDEAD] = EHOSTDOWN, 1133 [PRC_HOSTUNREACH] = EHOSTUNREACH, 1134 [PRC_UNREACH_NET] = EHOSTUNREACH, 1135 [PRC_UNREACH_HOST] = EHOSTUNREACH, 1136 [PRC_UNREACH_PROTOCOL] = ECONNREFUSED, 1137 [PRC_UNREACH_PORT] = ECONNREFUSED, 1138 [PRC_UNREACH_SRCFAIL] = EHOSTUNREACH, 1139 [PRC_PARAMPROB] = ENOPROTOOPT, 1140}; 1141 1142void 1143ip_fasttimo(void) 1144{ 1145 if (ip_drainwanted) { 1146 ip_drain(); 1147 ip_drainwanted = 0; 1148 } 1149} 1150 1151void 1152ip_drainstub(void) 1153{ 1154 ip_drainwanted = 1; 1155} 1156 1157/* 1158 * Forward a packet. If some error occurs return the sender 1159 * an icmp packet. Note we can't always generate a meaningful 1160 * icmp message because icmp doesn't have a large enough repertoire 1161 * of codes and types. 1162 * 1163 * If not forwarding, just drop the packet. This could be confusing 1164 * if ipforwarding was zero but some routing protocol was advancing 1165 * us as a gateway to somewhere. However, we must let the routing 1166 * protocol deal with that. 1167 * 1168 * The srcrt parameter indicates whether the packet is being forwarded 1169 * via a source route. 1170 */ 1171static void 1172ip_forward(struct mbuf *m, int srcrt) 1173{ 1174 struct ip *ip = mtod(m, struct ip *); 1175 struct rtentry *rt; 1176 int error, type = 0, code = 0, destmtu = 0; 1177 struct mbuf *mcopy; 1178 n_long dest; 1179 union { 1180 struct sockaddr dst; 1181 struct sockaddr_in dst4; 1182 } u; 1183 1184 /* 1185 * We are now in the output path. 1186 */ 1187 MCLAIM(m, &ip_tx_mowner); 1188 1189 /* 1190 * Clear any in-bound checksum flags for this packet. 1191 */ 1192 m->m_pkthdr.csum_flags = 0; 1193 1194 dest = 0; 1195 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1196 IP_STATINC(IP_STAT_CANTFORWARD); 1197 m_freem(m); 1198 return; 1199 } 1200 if (ip->ip_ttl <= IPTTLDEC) { 1201 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1202 return; 1203 } 1204 1205 sockaddr_in_init(&u.dst4, &ip->ip_dst, 0); 1206 if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL) { 1207 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, dest, 0); 1208 return; 1209 } 1210 1211 /* 1212 * Save at most 68 bytes of the packet in case 1213 * we need to generate an ICMP message to the src. 1214 * Pullup to avoid sharing mbuf cluster between m and mcopy. 1215 */ 1216 mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT); 1217 if (mcopy) 1218 mcopy = m_pullup(mcopy, ip->ip_hl << 2); 1219 1220 ip->ip_ttl -= IPTTLDEC; 1221 1222 /* 1223 * If forwarding packet using same interface that it came in on, 1224 * perhaps should send a redirect to sender to shortcut a hop. 1225 * Only send redirect if source is sending directly to us, 1226 * and if packet was not source routed (or has any options). 1227 * Also, don't send redirect if forwarding using a default route 1228 * or a route modified by a redirect. 1229 */ 1230 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1231 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1232 !in_nullhost(satocsin(rt_getkey(rt))->sin_addr) && 1233 ipsendredirects && !srcrt) { 1234 if (rt->rt_ifa && 1235 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) == 1236 ifatoia(rt->rt_ifa)->ia_subnet) { 1237 if (rt->rt_flags & RTF_GATEWAY) 1238 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1239 else 1240 dest = ip->ip_dst.s_addr; 1241 /* 1242 * Router requirements says to only send host 1243 * redirects. 1244 */ 1245 type = ICMP_REDIRECT; 1246 code = ICMP_REDIRECT_HOST; 1247 } 1248 } 1249 1250 error = ip_output(m, NULL, &ipforward_rt, 1251 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 1252 NULL, NULL); 1253 1254 if (error) 1255 IP_STATINC(IP_STAT_CANTFORWARD); 1256 else { 1257 uint64_t *ips = IP_STAT_GETREF(); 1258 ips[IP_STAT_FORWARD]++; 1259 if (type) { 1260 ips[IP_STAT_REDIRECTSENT]++; 1261 IP_STAT_PUTREF(); 1262 } else { 1263 IP_STAT_PUTREF(); 1264 if (mcopy) { 1265#ifdef GATEWAY 1266 if (mcopy->m_flags & M_CANFASTFWD) 1267 ipflow_create(&ipforward_rt, mcopy); 1268#endif 1269 m_freem(mcopy); 1270 } 1271 return; 1272 } 1273 } 1274 if (mcopy == NULL) 1275 return; 1276 1277 switch (error) { 1278 1279 case 0: /* forwarded, but need redirect */ 1280 /* type, code set above */ 1281 break; 1282 1283 case ENETUNREACH: /* shouldn't happen, checked above */ 1284 case EHOSTUNREACH: 1285 case ENETDOWN: 1286 case EHOSTDOWN: 1287 default: 1288 type = ICMP_UNREACH; 1289 code = ICMP_UNREACH_HOST; 1290 break; 1291 1292 case EMSGSIZE: 1293 type = ICMP_UNREACH; 1294 code = ICMP_UNREACH_NEEDFRAG; 1295 1296 if ((rt = rtcache_validate(&ipforward_rt)) != NULL) 1297 destmtu = rt->rt_ifp->if_mtu; 1298#ifdef IPSEC 1299 (void)ipsec4_forward(mcopy, &destmtu); 1300#endif 1301 IP_STATINC(IP_STAT_CANTFRAG); 1302 break; 1303 1304 case ENOBUFS: 1305 /* 1306 * Do not generate ICMP_SOURCEQUENCH as required in RFC 1812, 1307 * Requirements for IP Version 4 Routers. Source quench can 1308 * big problem under DoS attacks or if the underlying 1309 * interface is rate-limited. 1310 */ 1311 if (mcopy) 1312 m_freem(mcopy); 1313 return; 1314 } 1315 icmp_error(mcopy, type, code, dest, destmtu); 1316} 1317 1318void 1319ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 1320 struct mbuf *m) 1321{ 1322 struct socket *so = inp->inp_socket; 1323 int inpflags = inp->inp_flags; 1324 1325 if (so->so_options & SO_TIMESTAMP 1326#ifdef SO_OTIMESTAMP 1327 || so->so_options & SO_OTIMESTAMP 1328#endif 1329 ) { 1330 struct timeval tv; 1331 1332 microtime(&tv); 1333#ifdef SO_OTIMESTAMP 1334 if (so->so_options & SO_OTIMESTAMP) { 1335 struct timeval50 tv50; 1336 timeval_to_timeval50(&tv, &tv50); 1337 *mp = sbcreatecontrol((void *) &tv50, sizeof(tv50), 1338 SCM_OTIMESTAMP, SOL_SOCKET); 1339 } else 1340#endif 1341 *mp = sbcreatecontrol((void *) &tv, sizeof(tv), 1342 SCM_TIMESTAMP, SOL_SOCKET); 1343 if (*mp) 1344 mp = &(*mp)->m_next; 1345 } 1346 if (inpflags & INP_RECVDSTADDR) { 1347 *mp = sbcreatecontrol((void *) &ip->ip_dst, 1348 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1349 if (*mp) 1350 mp = &(*mp)->m_next; 1351 } 1352 if (inpflags & INP_RECVPKTINFO) { 1353 struct in_pktinfo ipi; 1354 ipi.ipi_addr = ip->ip_src; 1355 ipi.ipi_ifindex = m->m_pkthdr.rcvif->if_index; 1356 *mp = sbcreatecontrol((void *) &ipi, 1357 sizeof(ipi), IP_RECVPKTINFO, IPPROTO_IP); 1358 if (*mp) 1359 mp = &(*mp)->m_next; 1360 } 1361 if (inpflags & INP_PKTINFO) { 1362 struct in_pktinfo ipi; 1363 ipi.ipi_addr = ip->ip_dst; 1364 ipi.ipi_ifindex = m->m_pkthdr.rcvif->if_index; 1365 *mp = sbcreatecontrol((void *) &ipi, 1366 sizeof(ipi), IP_PKTINFO, IPPROTO_IP); 1367 if (*mp) 1368 mp = &(*mp)->m_next; 1369 } 1370 if (inpflags & INP_RECVIF) { 1371 struct sockaddr_dl sdl; 1372 1373 sockaddr_dl_init(&sdl, sizeof(sdl), m->m_pkthdr.rcvif ? 1374 m->m_pkthdr.rcvif->if_index : 0, 0, NULL, 0, NULL, 0); 1375 *mp = sbcreatecontrol(&sdl, sdl.sdl_len, IP_RECVIF, IPPROTO_IP); 1376 if (*mp) 1377 mp = &(*mp)->m_next; 1378 } 1379 if (inpflags & INP_RECVTTL) { 1380 *mp = sbcreatecontrol((void *) &ip->ip_ttl, 1381 sizeof(uint8_t), IP_RECVTTL, IPPROTO_IP); 1382 if (*mp) 1383 mp = &(*mp)->m_next; 1384 } 1385} 1386 1387/* 1388 * sysctl helper routine for net.inet.ip.forwsrcrt. 1389 */ 1390static int 1391sysctl_net_inet_ip_forwsrcrt(SYSCTLFN_ARGS) 1392{ 1393 int error, tmp; 1394 struct sysctlnode node; 1395 1396 node = *rnode; 1397 tmp = ip_forwsrcrt; 1398 node.sysctl_data = &tmp; 1399 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1400 if (error || newp == NULL) 1401 return (error); 1402 1403 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FORWSRCRT, 1404 0, NULL, NULL, NULL); 1405 if (error) 1406 return (error); 1407 1408 ip_forwsrcrt = tmp; 1409 1410 return (0); 1411} 1412 1413/* 1414 * sysctl helper routine for net.inet.ip.mtudisctimeout. checks the 1415 * range of the new value and tweaks timers if it changes. 1416 */ 1417static int 1418sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS) 1419{ 1420 int error, tmp; 1421 struct sysctlnode node; 1422 1423 node = *rnode; 1424 tmp = ip_mtudisc_timeout; 1425 node.sysctl_data = &tmp; 1426 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1427 if (error || newp == NULL) 1428 return (error); 1429 if (tmp < 0) 1430 return (EINVAL); 1431 1432 mutex_enter(softnet_lock); 1433 1434 ip_mtudisc_timeout = tmp; 1435 rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout); 1436 1437 mutex_exit(softnet_lock); 1438 1439 return (0); 1440} 1441 1442static int 1443sysctl_net_inet_ip_stats(SYSCTLFN_ARGS) 1444{ 1445 1446 return (NETSTAT_SYSCTL(ipstat_percpu, IP_NSTATS)); 1447} 1448 1449static void 1450sysctl_net_inet_ip_setup(struct sysctllog **clog) 1451{ 1452 sysctl_createv(clog, 0, NULL, NULL, 1453 CTLFLAG_PERMANENT, 1454 CTLTYPE_NODE, "inet", 1455 SYSCTL_DESCR("PF_INET related settings"), 1456 NULL, 0, NULL, 0, 1457 CTL_NET, PF_INET, CTL_EOL); 1458 sysctl_createv(clog, 0, NULL, NULL, 1459 CTLFLAG_PERMANENT, 1460 CTLTYPE_NODE, "ip", 1461 SYSCTL_DESCR("IPv4 related settings"), 1462 NULL, 0, NULL, 0, 1463 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL); 1464 1465 sysctl_createv(clog, 0, NULL, NULL, 1466 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1467 CTLTYPE_INT, "forwarding", 1468 SYSCTL_DESCR("Enable forwarding of INET datagrams"), 1469 NULL, 0, &ipforwarding, 0, 1470 CTL_NET, PF_INET, IPPROTO_IP, 1471 IPCTL_FORWARDING, CTL_EOL); 1472 sysctl_createv(clog, 0, NULL, NULL, 1473 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1474 CTLTYPE_INT, "redirect", 1475 SYSCTL_DESCR("Enable sending of ICMP redirect messages"), 1476 NULL, 0, &ipsendredirects, 0, 1477 CTL_NET, PF_INET, IPPROTO_IP, 1478 IPCTL_SENDREDIRECTS, CTL_EOL); 1479 sysctl_createv(clog, 0, NULL, NULL, 1480 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1481 CTLTYPE_INT, "ttl", 1482 SYSCTL_DESCR("Default TTL for an INET datagram"), 1483 NULL, 0, &ip_defttl, 0, 1484 CTL_NET, PF_INET, IPPROTO_IP, 1485 IPCTL_DEFTTL, CTL_EOL); 1486#ifdef IPCTL_DEFMTU 1487 sysctl_createv(clog, 0, NULL, NULL, 1488 CTLFLAG_PERMANENT /* |CTLFLAG_READWRITE? */, 1489 CTLTYPE_INT, "mtu", 1490 SYSCTL_DESCR("Default MTA for an INET route"), 1491 NULL, 0, &ip_mtu, 0, 1492 CTL_NET, PF_INET, IPPROTO_IP, 1493 IPCTL_DEFMTU, CTL_EOL); 1494#endif /* IPCTL_DEFMTU */ 1495 sysctl_createv(clog, 0, NULL, NULL, 1496 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1497 CTLTYPE_INT, "forwsrcrt", 1498 SYSCTL_DESCR("Enable forwarding of source-routed " 1499 "datagrams"), 1500 sysctl_net_inet_ip_forwsrcrt, 0, &ip_forwsrcrt, 0, 1501 CTL_NET, PF_INET, IPPROTO_IP, 1502 IPCTL_FORWSRCRT, CTL_EOL); 1503 sysctl_createv(clog, 0, NULL, NULL, 1504 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1505 CTLTYPE_INT, "directed-broadcast", 1506 SYSCTL_DESCR("Enable forwarding of broadcast datagrams"), 1507 NULL, 0, &ip_directedbcast, 0, 1508 CTL_NET, PF_INET, IPPROTO_IP, 1509 IPCTL_DIRECTEDBCAST, CTL_EOL); 1510 sysctl_createv(clog, 0, NULL, NULL, 1511 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1512 CTLTYPE_INT, "allowsrcrt", 1513 SYSCTL_DESCR("Accept source-routed datagrams"), 1514 NULL, 0, &ip_allowsrcrt, 0, 1515 CTL_NET, PF_INET, IPPROTO_IP, 1516 IPCTL_ALLOWSRCRT, CTL_EOL); 1517 1518 sysctl_createv(clog, 0, NULL, NULL, 1519 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1520 CTLTYPE_INT, "mtudisc", 1521 SYSCTL_DESCR("Use RFC1191 Path MTU Discovery"), 1522 NULL, 0, &ip_mtudisc, 0, 1523 CTL_NET, PF_INET, IPPROTO_IP, 1524 IPCTL_MTUDISC, CTL_EOL); 1525 sysctl_createv(clog, 0, NULL, NULL, 1526 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1527 CTLTYPE_INT, "anonportmin", 1528 SYSCTL_DESCR("Lowest ephemeral port number to assign"), 1529 sysctl_net_inet_ip_ports, 0, &anonportmin, 0, 1530 CTL_NET, PF_INET, IPPROTO_IP, 1531 IPCTL_ANONPORTMIN, CTL_EOL); 1532 sysctl_createv(clog, 0, NULL, NULL, 1533 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1534 CTLTYPE_INT, "anonportmax", 1535 SYSCTL_DESCR("Highest ephemeral port number to assign"), 1536 sysctl_net_inet_ip_ports, 0, &anonportmax, 0, 1537 CTL_NET, PF_INET, IPPROTO_IP, 1538 IPCTL_ANONPORTMAX, CTL_EOL); 1539 sysctl_createv(clog, 0, NULL, NULL, 1540 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1541 CTLTYPE_INT, "mtudisctimeout", 1542 SYSCTL_DESCR("Lifetime of a Path MTU Discovered route"), 1543 sysctl_net_inet_ip_pmtudto, 0, (void *)&ip_mtudisc_timeout, 0, 1544 CTL_NET, PF_INET, IPPROTO_IP, 1545 IPCTL_MTUDISCTIMEOUT, CTL_EOL); 1546#if NGIF > 0 1547 sysctl_createv(clog, 0, NULL, NULL, 1548 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1549 CTLTYPE_INT, "gifttl", 1550 SYSCTL_DESCR("Default TTL for a gif tunnel datagram"), 1551 NULL, 0, &ip_gif_ttl, 0, 1552 CTL_NET, PF_INET, IPPROTO_IP, 1553 IPCTL_GIF_TTL, CTL_EOL); 1554#endif /* NGIF */ 1555#ifndef IPNOPRIVPORTS 1556 sysctl_createv(clog, 0, NULL, NULL, 1557 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1558 CTLTYPE_INT, "lowportmin", 1559 SYSCTL_DESCR("Lowest privileged ephemeral port number " 1560 "to assign"), 1561 sysctl_net_inet_ip_ports, 0, &lowportmin, 0, 1562 CTL_NET, PF_INET, IPPROTO_IP, 1563 IPCTL_LOWPORTMIN, CTL_EOL); 1564 sysctl_createv(clog, 0, NULL, NULL, 1565 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1566 CTLTYPE_INT, "lowportmax", 1567 SYSCTL_DESCR("Highest privileged ephemeral port number " 1568 "to assign"), 1569 sysctl_net_inet_ip_ports, 0, &lowportmax, 0, 1570 CTL_NET, PF_INET, IPPROTO_IP, 1571 IPCTL_LOWPORTMAX, CTL_EOL); 1572#endif /* IPNOPRIVPORTS */ 1573#if NGRE > 0 1574 sysctl_createv(clog, 0, NULL, NULL, 1575 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1576 CTLTYPE_INT, "grettl", 1577 SYSCTL_DESCR("Default TTL for a gre tunnel datagram"), 1578 NULL, 0, &ip_gre_ttl, 0, 1579 CTL_NET, PF_INET, IPPROTO_IP, 1580 IPCTL_GRE_TTL, CTL_EOL); 1581#endif /* NGRE */ 1582 sysctl_createv(clog, 0, NULL, NULL, 1583 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1584 CTLTYPE_INT, "checkinterface", 1585 SYSCTL_DESCR("Enable receive side of Strong ES model " 1586 "from RFC1122"), 1587 NULL, 0, &ip_checkinterface, 0, 1588 CTL_NET, PF_INET, IPPROTO_IP, 1589 IPCTL_CHECKINTERFACE, CTL_EOL); 1590 sysctl_createv(clog, 0, NULL, NULL, 1591 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1592 CTLTYPE_INT, "random_id", 1593 SYSCTL_DESCR("Assign random ip_id values"), 1594 NULL, 0, &ip_do_randomid, 0, 1595 CTL_NET, PF_INET, IPPROTO_IP, 1596 IPCTL_RANDOMID, CTL_EOL); 1597 sysctl_createv(clog, 0, NULL, NULL, 1598 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1599 CTLTYPE_INT, "do_loopback_cksum", 1600 SYSCTL_DESCR("Perform IP checksum on loopback"), 1601 NULL, 0, &ip_do_loopback_cksum, 0, 1602 CTL_NET, PF_INET, IPPROTO_IP, 1603 IPCTL_LOOPBACKCKSUM, CTL_EOL); 1604 sysctl_createv(clog, 0, NULL, NULL, 1605 CTLFLAG_PERMANENT, 1606 CTLTYPE_STRUCT, "stats", 1607 SYSCTL_DESCR("IP statistics"), 1608 sysctl_net_inet_ip_stats, 0, NULL, 0, 1609 CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS, 1610 CTL_EOL); 1611 1612 /* anonportalgo RFC6056 subtree */ 1613 const struct sysctlnode *portalgo_node; 1614 sysctl_createv(clog, 0, NULL, &portalgo_node, 1615 CTLFLAG_PERMANENT, 1616 CTLTYPE_NODE, "anonportalgo", 1617 SYSCTL_DESCR("Anonymous Port Algorithm Selection (RFC 6056)"), 1618 NULL, 0, NULL, 0, 1619 CTL_NET, PF_INET, IPPROTO_IP, CTL_CREATE, CTL_EOL); 1620 sysctl_createv(clog, 0, &portalgo_node, NULL, 1621 CTLFLAG_PERMANENT, 1622 CTLTYPE_STRING, "available", 1623 SYSCTL_DESCR("available algorithms"), 1624 sysctl_portalgo_available, 0, NULL, PORTALGO_MAXLEN, 1625 CTL_CREATE, CTL_EOL); 1626 sysctl_createv(clog, 0, &portalgo_node, NULL, 1627 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1628 CTLTYPE_STRING, "selected", 1629 SYSCTL_DESCR("selected algorithm"), 1630 sysctl_portalgo_selected4, 0, NULL, PORTALGO_MAXLEN, 1631 CTL_CREATE, CTL_EOL); 1632 sysctl_createv(clog, 0, &portalgo_node, NULL, 1633 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1634 CTLTYPE_STRUCT, "reserve", 1635 SYSCTL_DESCR("bitmap of reserved ports"), 1636 sysctl_portalgo_reserve4, 0, NULL, 0, 1637 CTL_CREATE, CTL_EOL); 1638} 1639 1640void 1641ip_statinc(u_int stat) 1642{ 1643 1644 KASSERT(stat < IP_NSTATS); 1645 IP_STATINC(stat); 1646} 1647