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