ip_input.c revision 190909
1/*- 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/sys/netinet/ip_input.c 190909 2009-04-11 05:58:58Z zec $"); 34 35#include "opt_bootp.h" 36#include "opt_ipfw.h" 37#include "opt_ipstealth.h" 38#include "opt_ipsec.h" 39#include "opt_route.h" 40#include "opt_mac.h" 41#include "opt_carp.h" 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/callout.h> 46#include <sys/mbuf.h> 47#include <sys/malloc.h> 48#include <sys/domain.h> 49#include <sys/protosw.h> 50#include <sys/socket.h> 51#include <sys/time.h> 52#include <sys/kernel.h> 53#include <sys/lock.h> 54#include <sys/rwlock.h> 55#include <sys/syslog.h> 56#include <sys/sysctl.h> 57#include <sys/vimage.h> 58 59#include <net/pfil.h> 60#include <net/if.h> 61#include <net/if_types.h> 62#include <net/if_var.h> 63#include <net/if_dl.h> 64#include <net/route.h> 65#include <net/netisr.h> 66#include <net/vnet.h> 67 68#include <netinet/in.h> 69#include <netinet/in_systm.h> 70#include <netinet/in_var.h> 71#include <netinet/ip.h> 72#include <netinet/in_pcb.h> 73#include <netinet/ip_var.h> 74#include <netinet/ip_icmp.h> 75#include <netinet/ip_options.h> 76#include <machine/in_cksum.h> 77#include <netinet/vinet.h> 78#ifdef DEV_CARP 79#include <netinet/ip_carp.h> 80#endif 81#ifdef IPSEC 82#include <netinet/ip_ipsec.h> 83#endif /* IPSEC */ 84 85#include <sys/socketvar.h> 86 87/* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */ 88#include <netinet/ip_fw.h> 89#include <netinet/ip_dummynet.h> 90 91#include <security/mac/mac_framework.h> 92 93#ifdef CTASSERT 94CTASSERT(sizeof(struct ip) == 20); 95#endif 96 97#ifndef VIMAGE 98#ifndef VIMAGE_GLOBALS 99struct vnet_inet vnet_inet_0; 100#endif 101#endif 102 103#ifdef VIMAGE_GLOBALS 104static int ipsendredirects; 105static int ip_checkinterface; 106static int ip_keepfaith; 107static int ip_sendsourcequench; 108int ip_defttl; 109int ip_do_randomid; 110int ipforwarding; 111struct in_ifaddrhead in_ifaddrhead; /* first inet address */ 112struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */ 113u_long in_ifaddrhmask; /* mask for hash table */ 114struct ipstat ipstat; 115static int ip_rsvp_on; 116struct socket *ip_rsvpd; 117int rsvp_on; 118static struct ipqhead ipq[IPREASS_NHASH]; 119static int maxnipq; /* Administrative limit on # reass queues. */ 120static int maxfragsperpacket; 121int ipstealth; 122static int nipq; /* Total # of reass queues */ 123#endif 124 125SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_FORWARDING, 126 forwarding, CTLFLAG_RW, ipforwarding, 0, 127 "Enable IP forwarding between interfaces"); 128 129SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_SENDREDIRECTS, 130 redirect, CTLFLAG_RW, ipsendredirects, 0, 131 "Enable sending IP redirects"); 132 133SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_DEFTTL, 134 ttl, CTLFLAG_RW, ip_defttl, 0, "Maximum TTL on IP packets"); 135 136SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, IPCTL_KEEPFAITH, 137 keepfaith, CTLFLAG_RW, ip_keepfaith, 0, 138 "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 139 140SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, 141 sendsourcequench, CTLFLAG_RW, ip_sendsourcequench, 0, 142 "Enable the transmission of source quench packets"); 143 144SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, random_id, 145 CTLFLAG_RW, ip_do_randomid, 0, "Assign random ip_id values"); 146 147/* 148 * XXX - Setting ip_checkinterface mostly implements the receive side of 149 * the Strong ES model described in RFC 1122, but since the routing table 150 * and transmit implementation do not implement the Strong ES model, 151 * setting this to 1 results in an odd hybrid. 152 * 153 * XXX - ip_checkinterface currently must be disabled if you use ipnat 154 * to translate the destination address to another local interface. 155 * 156 * XXX - ip_checkinterface must be disabled if you add IP aliases 157 * to the loopback interface instead of the interface where the 158 * packets for those addresses are received. 159 */ 160SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, 161 check_interface, CTLFLAG_RW, ip_checkinterface, 0, 162 "Verify packet arrives on correct interface"); 163 164struct pfil_head inet_pfil_hook; /* Packet filter hooks */ 165 166static struct ifqueue ipintrq; 167static int ipqmaxlen = IFQ_MAXLEN; 168 169extern struct domain inetdomain; 170extern struct protosw inetsw[]; 171u_char ip_protox[IPPROTO_MAX]; 172 173SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW, 174 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue"); 175SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, 176 &ipintrq.ifq_drops, 0, 177 "Number of packets dropped from the IP input queue"); 178 179SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW, 180 ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)"); 181 182#ifdef VIMAGE_GLOBALS 183static uma_zone_t ipq_zone; 184#endif 185static struct mtx ipqlock; 186 187#define IPQ_LOCK() mtx_lock(&ipqlock) 188#define IPQ_UNLOCK() mtx_unlock(&ipqlock) 189#define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF) 190#define IPQ_LOCK_ASSERT() mtx_assert(&ipqlock, MA_OWNED) 191 192static void maxnipq_update(void); 193static void ipq_zone_change(void *); 194 195SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, fragpackets, 196 CTLFLAG_RD, nipq, 0, 197 "Current number of IPv4 fragment reassembly queue entries"); 198 199SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, maxfragsperpacket, 200 CTLFLAG_RW, maxfragsperpacket, 0, 201 "Maximum number of IPv4 fragments allowed per packet"); 202 203struct callout ipport_tick_callout; 204 205#ifdef IPCTL_DEFMTU 206SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 207 &ip_mtu, 0, "Default MTU"); 208#endif 209 210#ifdef IPSTEALTH 211SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 212 ipstealth, 0, "IP stealth mode, no TTL decrementation on forwarding"); 213#endif 214 215/* 216 * ipfw_ether and ipfw_bridge hooks. 217 * XXX: Temporary until those are converted to pfil_hooks as well. 218 */ 219ip_fw_chk_t *ip_fw_chk_ptr = NULL; 220ip_dn_io_t *ip_dn_io_ptr = NULL; 221#ifdef VIMAGE_GLOBALS 222int fw_one_pass; 223#endif 224 225static void ip_freef(struct ipqhead *, struct ipq *); 226 227#ifndef VIMAGE_GLOBALS 228static void vnet_inet_register(void); 229 230static const vnet_modinfo_t vnet_inet_modinfo = { 231 .vmi_id = VNET_MOD_INET, 232 .vmi_name = "inet", 233}; 234 235static void vnet_inet_register() 236{ 237 238 vnet_mod_register(&vnet_inet_modinfo); 239} 240 241SYSINIT(inet, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, vnet_inet_register, 0); 242#endif 243 244/* 245 * IP initialization: fill in IP protocol switch table. 246 * All protocols not implemented in kernel go to raw IP protocol handler. 247 */ 248void 249ip_init(void) 250{ 251 INIT_VNET_INET(curvnet); 252 struct protosw *pr; 253 int i; 254 255 V_ipsendredirects = 1; /* XXX */ 256 V_ip_checkinterface = 0; 257 V_ip_keepfaith = 0; 258 V_ip_sendsourcequench = 0; 259 V_rsvp_on = 0; 260 V_ip_defttl = IPDEFTTL; 261 V_ip_do_randomid = 0; 262 V_ip_id = time_second & 0xffff; 263 V_ipforwarding = 0; 264 V_ipstealth = 0; 265 V_nipq = 0; /* Total # of reass queues */ 266 267 V_ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */ 268 V_ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */ 269 V_ipport_firstauto = IPPORT_EPHEMERALFIRST; /* 10000 */ 270 V_ipport_lastauto = IPPORT_EPHEMERALLAST; /* 65535 */ 271 V_ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */ 272 V_ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */ 273 V_ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */ 274 V_ipport_reservedlow = 0; 275 V_ipport_randomized = 1; /* user controlled via sysctl */ 276 V_ipport_randomcps = 10; /* user controlled via sysctl */ 277 V_ipport_randomtime = 45; /* user controlled via sysctl */ 278 V_ipport_stoprandom = 0; /* toggled by ipport_tick */ 279 280 V_fw_one_pass = 1; 281 282#ifdef NOTYET 283 /* XXX global static but not instantiated in this file */ 284 V_ipfastforward_active = 0; 285 V_subnetsarelocal = 0; 286 V_sameprefixcarponly = 0; 287#endif 288 289 TAILQ_INIT(&V_in_ifaddrhead); 290 V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask); 291 292 /* Initialize IP reassembly queue. */ 293 for (i = 0; i < IPREASS_NHASH; i++) 294 TAILQ_INIT(&V_ipq[i]); 295 V_maxnipq = nmbclusters / 32; 296 V_maxfragsperpacket = 16; 297 V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL, 298 NULL, UMA_ALIGN_PTR, 0); 299 maxnipq_update(); 300 301 /* Skip initialization of globals for non-default instances. */ 302 if (!IS_DEFAULT_VNET(curvnet)) 303 return; 304 305 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 306 if (pr == NULL) 307 panic("ip_init: PF_INET not found"); 308 309 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */ 310 for (i = 0; i < IPPROTO_MAX; i++) 311 ip_protox[i] = pr - inetsw; 312 /* 313 * Cycle through IP protocols and put them into the appropriate place 314 * in ip_protox[]. 315 */ 316 for (pr = inetdomain.dom_protosw; 317 pr < inetdomain.dom_protoswNPROTOSW; pr++) 318 if (pr->pr_domain->dom_family == PF_INET && 319 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 320 /* Be careful to only index valid IP protocols. */ 321 if (pr->pr_protocol < IPPROTO_MAX) 322 ip_protox[pr->pr_protocol] = pr - inetsw; 323 } 324 325 /* Initialize packet filter hooks. */ 326 inet_pfil_hook.ph_type = PFIL_TYPE_AF; 327 inet_pfil_hook.ph_af = AF_INET; 328 if ((i = pfil_head_register(&inet_pfil_hook)) != 0) 329 printf("%s: WARNING: unable to register pfil hook, " 330 "error %d\n", __func__, i); 331 332 /* Start ipport_tick. */ 333 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE); 334 ipport_tick(NULL); 335 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 336 SHUTDOWN_PRI_DEFAULT); 337 EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change, 338 NULL, EVENTHANDLER_PRI_ANY); 339 340 /* Initialize various other remaining things. */ 341 IPQ_LOCK_INIT(); 342 ipintrq.ifq_maxlen = ipqmaxlen; 343 mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF); 344 netisr_register(NETISR_IP, ip_input, &ipintrq, 0); 345} 346 347void 348ip_fini(void *xtp) 349{ 350 351 callout_stop(&ipport_tick_callout); 352} 353 354/* 355 * Ip input routine. Checksum and byte swap header. If fragmented 356 * try to reassemble. Process options. Pass to next level. 357 */ 358void 359ip_input(struct mbuf *m) 360{ 361 INIT_VNET_INET(curvnet); 362 struct ip *ip = NULL; 363 struct in_ifaddr *ia = NULL; 364 struct ifaddr *ifa; 365 int checkif, hlen = 0; 366 u_short sum; 367 int dchg = 0; /* dest changed after fw */ 368 struct in_addr odst; /* original dst address */ 369 370 M_ASSERTPKTHDR(m); 371 372 if (m->m_flags & M_FASTFWD_OURS) { 373 /* 374 * Firewall or NAT changed destination to local. 375 * We expect ip_len and ip_off to be in host byte order. 376 */ 377 m->m_flags &= ~M_FASTFWD_OURS; 378 /* Set up some basics that will be used later. */ 379 ip = mtod(m, struct ip *); 380 hlen = ip->ip_hl << 2; 381 goto ours; 382 } 383 384 V_ipstat.ips_total++; 385 386 if (m->m_pkthdr.len < sizeof(struct ip)) 387 goto tooshort; 388 389 if (m->m_len < sizeof (struct ip) && 390 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 391 V_ipstat.ips_toosmall++; 392 return; 393 } 394 ip = mtod(m, struct ip *); 395 396 if (ip->ip_v != IPVERSION) { 397 V_ipstat.ips_badvers++; 398 goto bad; 399 } 400 401 hlen = ip->ip_hl << 2; 402 if (hlen < sizeof(struct ip)) { /* minimum header length */ 403 V_ipstat.ips_badhlen++; 404 goto bad; 405 } 406 if (hlen > m->m_len) { 407 if ((m = m_pullup(m, hlen)) == NULL) { 408 V_ipstat.ips_badhlen++; 409 return; 410 } 411 ip = mtod(m, struct ip *); 412 } 413 414 /* 127/8 must not appear on wire - RFC1122 */ 415 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 416 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 417 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 418 V_ipstat.ips_badaddr++; 419 goto bad; 420 } 421 } 422 423 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 424 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 425 } else { 426 if (hlen == sizeof(struct ip)) { 427 sum = in_cksum_hdr(ip); 428 } else { 429 sum = in_cksum(m, hlen); 430 } 431 } 432 if (sum) { 433 V_ipstat.ips_badsum++; 434 goto bad; 435 } 436 437#ifdef ALTQ 438 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 439 /* packet is dropped by traffic conditioner */ 440 return; 441#endif 442 443 /* 444 * Convert fields to host representation. 445 */ 446 ip->ip_len = ntohs(ip->ip_len); 447 if (ip->ip_len < hlen) { 448 V_ipstat.ips_badlen++; 449 goto bad; 450 } 451 ip->ip_off = ntohs(ip->ip_off); 452 453 /* 454 * Check that the amount of data in the buffers 455 * is as at least much as the IP header would have us expect. 456 * Trim mbufs if longer than we expect. 457 * Drop packet if shorter than we expect. 458 */ 459 if (m->m_pkthdr.len < ip->ip_len) { 460tooshort: 461 V_ipstat.ips_tooshort++; 462 goto bad; 463 } 464 if (m->m_pkthdr.len > ip->ip_len) { 465 if (m->m_len == m->m_pkthdr.len) { 466 m->m_len = ip->ip_len; 467 m->m_pkthdr.len = ip->ip_len; 468 } else 469 m_adj(m, ip->ip_len - m->m_pkthdr.len); 470 } 471#ifdef IPSEC 472 /* 473 * Bypass packet filtering for packets from a tunnel (gif). 474 */ 475 if (ip_ipsec_filtertunnel(m)) 476 goto passin; 477#endif /* IPSEC */ 478 479 /* 480 * Run through list of hooks for input packets. 481 * 482 * NB: Beware of the destination address changing (e.g. 483 * by NAT rewriting). When this happens, tell 484 * ip_forward to do the right thing. 485 */ 486 487 /* Jump over all PFIL processing if hooks are not active. */ 488 if (!PFIL_HOOKED(&inet_pfil_hook)) 489 goto passin; 490 491 odst = ip->ip_dst; 492 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, 493 PFIL_IN, NULL) != 0) 494 return; 495 if (m == NULL) /* consumed by filter */ 496 return; 497 498 ip = mtod(m, struct ip *); 499 dchg = (odst.s_addr != ip->ip_dst.s_addr); 500 501#ifdef IPFIREWALL_FORWARD 502 if (m->m_flags & M_FASTFWD_OURS) { 503 m->m_flags &= ~M_FASTFWD_OURS; 504 goto ours; 505 } 506 if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) { 507 /* 508 * Directly ship on the packet. This allows to forward packets 509 * that were destined for us to some other directly connected 510 * host. 511 */ 512 ip_forward(m, dchg); 513 return; 514 } 515#endif /* IPFIREWALL_FORWARD */ 516 517passin: 518 /* 519 * Process options and, if not destined for us, 520 * ship it on. ip_dooptions returns 1 when an 521 * error was detected (causing an icmp message 522 * to be sent and the original packet to be freed). 523 */ 524 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0)) 525 return; 526 527 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 528 * matter if it is destined to another node, or whether it is 529 * a multicast one, RSVP wants it! and prevents it from being forwarded 530 * anywhere else. Also checks if the rsvp daemon is running before 531 * grabbing the packet. 532 */ 533 if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP) 534 goto ours; 535 536 /* 537 * Check our list of addresses, to see if the packet is for us. 538 * If we don't have any addresses, assume any unicast packet 539 * we receive might be for us (and let the upper layers deal 540 * with it). 541 */ 542 if (TAILQ_EMPTY(&V_in_ifaddrhead) && 543 (m->m_flags & (M_MCAST|M_BCAST)) == 0) 544 goto ours; 545 546 /* 547 * Enable a consistency check between the destination address 548 * and the arrival interface for a unicast packet (the RFC 1122 549 * strong ES model) if IP forwarding is disabled and the packet 550 * is not locally generated and the packet is not subject to 551 * 'ipfw fwd'. 552 * 553 * XXX - Checking also should be disabled if the destination 554 * address is ipnat'ed to a different interface. 555 * 556 * XXX - Checking is incompatible with IP aliases added 557 * to the loopback interface instead of the interface where 558 * the packets are received. 559 * 560 * XXX - This is the case for carp vhost IPs as well so we 561 * insert a workaround. If the packet got here, we already 562 * checked with carp_iamatch() and carp_forus(). 563 */ 564 checkif = V_ip_checkinterface && (V_ipforwarding == 0) && 565 m->m_pkthdr.rcvif != NULL && 566 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) && 567#ifdef DEV_CARP 568 !m->m_pkthdr.rcvif->if_carp && 569#endif 570 (dchg == 0); 571 572 /* 573 * Check for exact addresses in the hash bucket. 574 */ 575 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 576 /* 577 * If the address matches, verify that the packet 578 * arrived via the correct interface if checking is 579 * enabled. 580 */ 581 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr && 582 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) 583 goto ours; 584 } 585 /* 586 * Check for broadcast addresses. 587 * 588 * Only accept broadcast packets that arrive via the matching 589 * interface. Reception of forwarded directed broadcasts would 590 * be handled via ip_forward() and ether_output() with the loopback 591 * into the stack for SIMPLEX interfaces handled by ether_output(). 592 */ 593 if (m->m_pkthdr.rcvif != NULL && 594 m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 595 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 596 if (ifa->ifa_addr->sa_family != AF_INET) 597 continue; 598 ia = ifatoia(ifa); 599 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 600 ip->ip_dst.s_addr) 601 goto ours; 602 if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) 603 goto ours; 604#ifdef BOOTP_COMPAT 605 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 606 goto ours; 607#endif 608 } 609 } 610 /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */ 611 if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) { 612 V_ipstat.ips_cantforward++; 613 m_freem(m); 614 return; 615 } 616 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 617 if (V_ip_mrouter) { 618 /* 619 * If we are acting as a multicast router, all 620 * incoming multicast packets are passed to the 621 * kernel-level multicast forwarding function. 622 * The packet is returned (relatively) intact; if 623 * ip_mforward() returns a non-zero value, the packet 624 * must be discarded, else it may be accepted below. 625 */ 626 if (ip_mforward && 627 ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 628 V_ipstat.ips_cantforward++; 629 m_freem(m); 630 return; 631 } 632 633 /* 634 * The process-level routing daemon needs to receive 635 * all multicast IGMP packets, whether or not this 636 * host belongs to their destination groups. 637 */ 638 if (ip->ip_p == IPPROTO_IGMP) 639 goto ours; 640 V_ipstat.ips_forward++; 641 } 642 /* 643 * Assume the packet is for us, to avoid prematurely taking 644 * a lock on the in_multi hash. Protocols must perform 645 * their own filtering and update statistics accordingly. 646 */ 647 goto ours; 648 } 649 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 650 goto ours; 651 if (ip->ip_dst.s_addr == INADDR_ANY) 652 goto ours; 653 654 /* 655 * FAITH(Firewall Aided Internet Translator) 656 */ 657 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 658 if (V_ip_keepfaith) { 659 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 660 goto ours; 661 } 662 m_freem(m); 663 return; 664 } 665 666 /* 667 * Not for us; forward if possible and desirable. 668 */ 669 if (V_ipforwarding == 0) { 670 V_ipstat.ips_cantforward++; 671 m_freem(m); 672 } else { 673#ifdef IPSEC 674 if (ip_ipsec_fwd(m)) 675 goto bad; 676#endif /* IPSEC */ 677 ip_forward(m, dchg); 678 } 679 return; 680 681ours: 682#ifdef IPSTEALTH 683 /* 684 * IPSTEALTH: Process non-routing options only 685 * if the packet is destined for us. 686 */ 687 if (V_ipstealth && hlen > sizeof (struct ip) && 688 ip_dooptions(m, 1)) 689 return; 690#endif /* IPSTEALTH */ 691 692 /* Count the packet in the ip address stats */ 693 if (ia != NULL) { 694 ia->ia_ifa.if_ipackets++; 695 ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 696 } 697 698 /* 699 * Attempt reassembly; if it succeeds, proceed. 700 * ip_reass() will return a different mbuf. 701 */ 702 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 703 m = ip_reass(m); 704 if (m == NULL) 705 return; 706 ip = mtod(m, struct ip *); 707 /* Get the header length of the reassembled packet */ 708 hlen = ip->ip_hl << 2; 709 } 710 711 /* 712 * Further protocols expect the packet length to be w/o the 713 * IP header. 714 */ 715 ip->ip_len -= hlen; 716 717#ifdef IPSEC 718 /* 719 * enforce IPsec policy checking if we are seeing last header. 720 * note that we do not visit this with protocols with pcb layer 721 * code - like udp/tcp/raw ip. 722 */ 723 if (ip_ipsec_input(m)) 724 goto bad; 725#endif /* IPSEC */ 726 727 /* 728 * Switch out to protocol's input routine. 729 */ 730 V_ipstat.ips_delivered++; 731 732 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 733 return; 734bad: 735 m_freem(m); 736} 737 738/* 739 * After maxnipq has been updated, propagate the change to UMA. The UMA zone 740 * max has slightly different semantics than the sysctl, for historical 741 * reasons. 742 */ 743static void 744maxnipq_update(void) 745{ 746 INIT_VNET_INET(curvnet); 747 748 /* 749 * -1 for unlimited allocation. 750 */ 751 if (V_maxnipq < 0) 752 uma_zone_set_max(V_ipq_zone, 0); 753 /* 754 * Positive number for specific bound. 755 */ 756 if (V_maxnipq > 0) 757 uma_zone_set_max(V_ipq_zone, V_maxnipq); 758 /* 759 * Zero specifies no further fragment queue allocation -- set the 760 * bound very low, but rely on implementation elsewhere to actually 761 * prevent allocation and reclaim current queues. 762 */ 763 if (V_maxnipq == 0) 764 uma_zone_set_max(V_ipq_zone, 1); 765} 766 767static void 768ipq_zone_change(void *tag) 769{ 770 INIT_VNET_INET(curvnet); 771 772 if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) { 773 V_maxnipq = nmbclusters / 32; 774 maxnipq_update(); 775 } 776} 777 778static int 779sysctl_maxnipq(SYSCTL_HANDLER_ARGS) 780{ 781 INIT_VNET_INET(curvnet); 782 int error, i; 783 784 i = V_maxnipq; 785 error = sysctl_handle_int(oidp, &i, 0, req); 786 if (error || !req->newptr) 787 return (error); 788 789 /* 790 * XXXRW: Might be a good idea to sanity check the argument and place 791 * an extreme upper bound. 792 */ 793 if (i < -1) 794 return (EINVAL); 795 V_maxnipq = i; 796 maxnipq_update(); 797 return (0); 798} 799 800SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW, 801 NULL, 0, sysctl_maxnipq, "I", 802 "Maximum number of IPv4 fragment reassembly queue entries"); 803 804/* 805 * Take incoming datagram fragment and try to reassemble it into 806 * whole datagram. If the argument is the first fragment or one 807 * in between the function will return NULL and store the mbuf 808 * in the fragment chain. If the argument is the last fragment 809 * the packet will be reassembled and the pointer to the new 810 * mbuf returned for further processing. Only m_tags attached 811 * to the first packet/fragment are preserved. 812 * The IP header is *NOT* adjusted out of iplen. 813 */ 814struct mbuf * 815ip_reass(struct mbuf *m) 816{ 817 INIT_VNET_INET(curvnet); 818 struct ip *ip; 819 struct mbuf *p, *q, *nq, *t; 820 struct ipq *fp = NULL; 821 struct ipqhead *head; 822 int i, hlen, next; 823 u_int8_t ecn, ecn0; 824 u_short hash; 825 826 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */ 827 if (V_maxnipq == 0 || V_maxfragsperpacket == 0) { 828 V_ipstat.ips_fragments++; 829 V_ipstat.ips_fragdropped++; 830 m_freem(m); 831 return (NULL); 832 } 833 834 ip = mtod(m, struct ip *); 835 hlen = ip->ip_hl << 2; 836 837 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 838 head = &V_ipq[hash]; 839 IPQ_LOCK(); 840 841 /* 842 * Look for queue of fragments 843 * of this datagram. 844 */ 845 TAILQ_FOREACH(fp, head, ipq_list) 846 if (ip->ip_id == fp->ipq_id && 847 ip->ip_src.s_addr == fp->ipq_src.s_addr && 848 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 849#ifdef MAC 850 mac_ipq_match(m, fp) && 851#endif 852 ip->ip_p == fp->ipq_p) 853 goto found; 854 855 fp = NULL; 856 857 /* 858 * Attempt to trim the number of allocated fragment queues if it 859 * exceeds the administrative limit. 860 */ 861 if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) { 862 /* 863 * drop something from the tail of the current queue 864 * before proceeding further 865 */ 866 struct ipq *q = TAILQ_LAST(head, ipqhead); 867 if (q == NULL) { /* gak */ 868 for (i = 0; i < IPREASS_NHASH; i++) { 869 struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead); 870 if (r) { 871 V_ipstat.ips_fragtimeout += 872 r->ipq_nfrags; 873 ip_freef(&V_ipq[i], r); 874 break; 875 } 876 } 877 } else { 878 V_ipstat.ips_fragtimeout += q->ipq_nfrags; 879 ip_freef(head, q); 880 } 881 } 882 883found: 884 /* 885 * Adjust ip_len to not reflect header, 886 * convert offset of this to bytes. 887 */ 888 ip->ip_len -= hlen; 889 if (ip->ip_off & IP_MF) { 890 /* 891 * Make sure that fragments have a data length 892 * that's a non-zero multiple of 8 bytes. 893 */ 894 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 895 V_ipstat.ips_toosmall++; /* XXX */ 896 goto dropfrag; 897 } 898 m->m_flags |= M_FRAG; 899 } else 900 m->m_flags &= ~M_FRAG; 901 ip->ip_off <<= 3; 902 903 904 /* 905 * Attempt reassembly; if it succeeds, proceed. 906 * ip_reass() will return a different mbuf. 907 */ 908 V_ipstat.ips_fragments++; 909 m->m_pkthdr.header = ip; 910 911 /* Previous ip_reass() started here. */ 912 /* 913 * Presence of header sizes in mbufs 914 * would confuse code below. 915 */ 916 m->m_data += hlen; 917 m->m_len -= hlen; 918 919 /* 920 * If first fragment to arrive, create a reassembly queue. 921 */ 922 if (fp == NULL) { 923 fp = uma_zalloc(V_ipq_zone, M_NOWAIT); 924 if (fp == NULL) 925 goto dropfrag; 926#ifdef MAC 927 if (mac_ipq_init(fp, M_NOWAIT) != 0) { 928 uma_zfree(V_ipq_zone, fp); 929 fp = NULL; 930 goto dropfrag; 931 } 932 mac_ipq_create(m, fp); 933#endif 934 TAILQ_INSERT_HEAD(head, fp, ipq_list); 935 V_nipq++; 936 fp->ipq_nfrags = 1; 937 fp->ipq_ttl = IPFRAGTTL; 938 fp->ipq_p = ip->ip_p; 939 fp->ipq_id = ip->ip_id; 940 fp->ipq_src = ip->ip_src; 941 fp->ipq_dst = ip->ip_dst; 942 fp->ipq_frags = m; 943 m->m_nextpkt = NULL; 944 goto done; 945 } else { 946 fp->ipq_nfrags++; 947#ifdef MAC 948 mac_ipq_update(m, fp); 949#endif 950 } 951 952#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 953 954 /* 955 * Handle ECN by comparing this segment with the first one; 956 * if CE is set, do not lose CE. 957 * drop if CE and not-ECT are mixed for the same packet. 958 */ 959 ecn = ip->ip_tos & IPTOS_ECN_MASK; 960 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 961 if (ecn == IPTOS_ECN_CE) { 962 if (ecn0 == IPTOS_ECN_NOTECT) 963 goto dropfrag; 964 if (ecn0 != IPTOS_ECN_CE) 965 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 966 } 967 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 968 goto dropfrag; 969 970 /* 971 * Find a segment which begins after this one does. 972 */ 973 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 974 if (GETIP(q)->ip_off > ip->ip_off) 975 break; 976 977 /* 978 * If there is a preceding segment, it may provide some of 979 * our data already. If so, drop the data from the incoming 980 * segment. If it provides all of our data, drop us, otherwise 981 * stick new segment in the proper place. 982 * 983 * If some of the data is dropped from the the preceding 984 * segment, then it's checksum is invalidated. 985 */ 986 if (p) { 987 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 988 if (i > 0) { 989 if (i >= ip->ip_len) 990 goto dropfrag; 991 m_adj(m, i); 992 m->m_pkthdr.csum_flags = 0; 993 ip->ip_off += i; 994 ip->ip_len -= i; 995 } 996 m->m_nextpkt = p->m_nextpkt; 997 p->m_nextpkt = m; 998 } else { 999 m->m_nextpkt = fp->ipq_frags; 1000 fp->ipq_frags = m; 1001 } 1002 1003 /* 1004 * While we overlap succeeding segments trim them or, 1005 * if they are completely covered, dequeue them. 1006 */ 1007 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 1008 q = nq) { 1009 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off; 1010 if (i < GETIP(q)->ip_len) { 1011 GETIP(q)->ip_len -= i; 1012 GETIP(q)->ip_off += i; 1013 m_adj(q, i); 1014 q->m_pkthdr.csum_flags = 0; 1015 break; 1016 } 1017 nq = q->m_nextpkt; 1018 m->m_nextpkt = nq; 1019 V_ipstat.ips_fragdropped++; 1020 fp->ipq_nfrags--; 1021 m_freem(q); 1022 } 1023 1024 /* 1025 * Check for complete reassembly and perform frag per packet 1026 * limiting. 1027 * 1028 * Frag limiting is performed here so that the nth frag has 1029 * a chance to complete the packet before we drop the packet. 1030 * As a result, n+1 frags are actually allowed per packet, but 1031 * only n will ever be stored. (n = maxfragsperpacket.) 1032 * 1033 */ 1034 next = 0; 1035 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1036 if (GETIP(q)->ip_off != next) { 1037 if (fp->ipq_nfrags > V_maxfragsperpacket) { 1038 V_ipstat.ips_fragdropped += fp->ipq_nfrags; 1039 ip_freef(head, fp); 1040 } 1041 goto done; 1042 } 1043 next += GETIP(q)->ip_len; 1044 } 1045 /* Make sure the last packet didn't have the IP_MF flag */ 1046 if (p->m_flags & M_FRAG) { 1047 if (fp->ipq_nfrags > V_maxfragsperpacket) { 1048 V_ipstat.ips_fragdropped += fp->ipq_nfrags; 1049 ip_freef(head, fp); 1050 } 1051 goto done; 1052 } 1053 1054 /* 1055 * Reassembly is complete. Make sure the packet is a sane size. 1056 */ 1057 q = fp->ipq_frags; 1058 ip = GETIP(q); 1059 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 1060 V_ipstat.ips_toolong++; 1061 V_ipstat.ips_fragdropped += fp->ipq_nfrags; 1062 ip_freef(head, fp); 1063 goto done; 1064 } 1065 1066 /* 1067 * Concatenate fragments. 1068 */ 1069 m = q; 1070 t = m->m_next; 1071 m->m_next = NULL; 1072 m_cat(m, t); 1073 nq = q->m_nextpkt; 1074 q->m_nextpkt = NULL; 1075 for (q = nq; q != NULL; q = nq) { 1076 nq = q->m_nextpkt; 1077 q->m_nextpkt = NULL; 1078 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1079 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1080 m_cat(m, q); 1081 } 1082 /* 1083 * In order to do checksumming faster we do 'end-around carry' here 1084 * (and not in for{} loop), though it implies we are not going to 1085 * reassemble more than 64k fragments. 1086 */ 1087 m->m_pkthdr.csum_data = 1088 (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16); 1089#ifdef MAC 1090 mac_ipq_reassemble(fp, m); 1091 mac_ipq_destroy(fp); 1092#endif 1093 1094 /* 1095 * Create header for new ip packet by modifying header of first 1096 * packet; dequeue and discard fragment reassembly header. 1097 * Make header visible. 1098 */ 1099 ip->ip_len = (ip->ip_hl << 2) + next; 1100 ip->ip_src = fp->ipq_src; 1101 ip->ip_dst = fp->ipq_dst; 1102 TAILQ_REMOVE(head, fp, ipq_list); 1103 V_nipq--; 1104 uma_zfree(V_ipq_zone, fp); 1105 m->m_len += (ip->ip_hl << 2); 1106 m->m_data -= (ip->ip_hl << 2); 1107 /* some debugging cruft by sklower, below, will go away soon */ 1108 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 1109 m_fixhdr(m); 1110 V_ipstat.ips_reassembled++; 1111 IPQ_UNLOCK(); 1112 return (m); 1113 1114dropfrag: 1115 V_ipstat.ips_fragdropped++; 1116 if (fp != NULL) 1117 fp->ipq_nfrags--; 1118 m_freem(m); 1119done: 1120 IPQ_UNLOCK(); 1121 return (NULL); 1122 1123#undef GETIP 1124} 1125 1126/* 1127 * Free a fragment reassembly header and all 1128 * associated datagrams. 1129 */ 1130static void 1131ip_freef(struct ipqhead *fhp, struct ipq *fp) 1132{ 1133 INIT_VNET_INET(curvnet); 1134 struct mbuf *q; 1135 1136 IPQ_LOCK_ASSERT(); 1137 1138 while (fp->ipq_frags) { 1139 q = fp->ipq_frags; 1140 fp->ipq_frags = q->m_nextpkt; 1141 m_freem(q); 1142 } 1143 TAILQ_REMOVE(fhp, fp, ipq_list); 1144 uma_zfree(V_ipq_zone, fp); 1145 V_nipq--; 1146} 1147 1148/* 1149 * IP timer processing; 1150 * if a timer expires on a reassembly 1151 * queue, discard it. 1152 */ 1153void 1154ip_slowtimo(void) 1155{ 1156 VNET_ITERATOR_DECL(vnet_iter); 1157 struct ipq *fp; 1158 int i; 1159 1160 IPQ_LOCK(); 1161 VNET_LIST_RLOCK(); 1162 VNET_FOREACH(vnet_iter) { 1163 CURVNET_SET(vnet_iter); 1164 INIT_VNET_INET(vnet_iter); 1165 for (i = 0; i < IPREASS_NHASH; i++) { 1166 for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) { 1167 struct ipq *fpp; 1168 1169 fpp = fp; 1170 fp = TAILQ_NEXT(fp, ipq_list); 1171 if(--fpp->ipq_ttl == 0) { 1172 V_ipstat.ips_fragtimeout += 1173 fpp->ipq_nfrags; 1174 ip_freef(&V_ipq[i], fpp); 1175 } 1176 } 1177 } 1178 /* 1179 * If we are over the maximum number of fragments 1180 * (due to the limit being lowered), drain off 1181 * enough to get down to the new limit. 1182 */ 1183 if (V_maxnipq >= 0 && V_nipq > V_maxnipq) { 1184 for (i = 0; i < IPREASS_NHASH; i++) { 1185 while (V_nipq > V_maxnipq && 1186 !TAILQ_EMPTY(&V_ipq[i])) { 1187 V_ipstat.ips_fragdropped += 1188 TAILQ_FIRST(&V_ipq[i])->ipq_nfrags; 1189 ip_freef(&V_ipq[i], 1190 TAILQ_FIRST(&V_ipq[i])); 1191 } 1192 } 1193 } 1194 CURVNET_RESTORE(); 1195 } 1196 VNET_LIST_RUNLOCK(); 1197 IPQ_UNLOCK(); 1198} 1199 1200/* 1201 * Drain off all datagram fragments. 1202 */ 1203void 1204ip_drain(void) 1205{ 1206 VNET_ITERATOR_DECL(vnet_iter); 1207 int i; 1208 1209 IPQ_LOCK(); 1210 VNET_LIST_RLOCK(); 1211 VNET_FOREACH(vnet_iter) { 1212 CURVNET_SET(vnet_iter); 1213 INIT_VNET_INET(vnet_iter); 1214 for (i = 0; i < IPREASS_NHASH; i++) { 1215 while(!TAILQ_EMPTY(&V_ipq[i])) { 1216 V_ipstat.ips_fragdropped += 1217 TAILQ_FIRST(&V_ipq[i])->ipq_nfrags; 1218 ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i])); 1219 } 1220 } 1221 CURVNET_RESTORE(); 1222 } 1223 VNET_LIST_RUNLOCK(); 1224 IPQ_UNLOCK(); 1225 in_rtqdrain(); 1226} 1227 1228/* 1229 * The protocol to be inserted into ip_protox[] must be already registered 1230 * in inetsw[], either statically or through pf_proto_register(). 1231 */ 1232int 1233ipproto_register(u_char ipproto) 1234{ 1235 struct protosw *pr; 1236 1237 /* Sanity checks. */ 1238 if (ipproto == 0) 1239 return (EPROTONOSUPPORT); 1240 1241 /* 1242 * The protocol slot must not be occupied by another protocol 1243 * already. An index pointing to IPPROTO_RAW is unused. 1244 */ 1245 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1246 if (pr == NULL) 1247 return (EPFNOSUPPORT); 1248 if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */ 1249 return (EEXIST); 1250 1251 /* Find the protocol position in inetsw[] and set the index. */ 1252 for (pr = inetdomain.dom_protosw; 1253 pr < inetdomain.dom_protoswNPROTOSW; pr++) { 1254 if (pr->pr_domain->dom_family == PF_INET && 1255 pr->pr_protocol && pr->pr_protocol == ipproto) { 1256 /* Be careful to only index valid IP protocols. */ 1257 if (pr->pr_protocol < IPPROTO_MAX) { 1258 ip_protox[pr->pr_protocol] = pr - inetsw; 1259 return (0); 1260 } else 1261 return (EINVAL); 1262 } 1263 } 1264 return (EPROTONOSUPPORT); 1265} 1266 1267int 1268ipproto_unregister(u_char ipproto) 1269{ 1270 struct protosw *pr; 1271 1272 /* Sanity checks. */ 1273 if (ipproto == 0) 1274 return (EPROTONOSUPPORT); 1275 1276 /* Check if the protocol was indeed registered. */ 1277 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1278 if (pr == NULL) 1279 return (EPFNOSUPPORT); 1280 if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */ 1281 return (ENOENT); 1282 1283 /* Reset the protocol slot to IPPROTO_RAW. */ 1284 ip_protox[ipproto] = pr - inetsw; 1285 return (0); 1286} 1287 1288/* 1289 * Given address of next destination (final or next hop), 1290 * return internet address info of interface to be used to get there. 1291 */ 1292struct in_ifaddr * 1293ip_rtaddr(struct in_addr dst, u_int fibnum) 1294{ 1295 struct route sro; 1296 struct sockaddr_in *sin; 1297 struct in_ifaddr *ifa; 1298 1299 bzero(&sro, sizeof(sro)); 1300 sin = (struct sockaddr_in *)&sro.ro_dst; 1301 sin->sin_family = AF_INET; 1302 sin->sin_len = sizeof(*sin); 1303 sin->sin_addr = dst; 1304 in_rtalloc_ign(&sro, 0, fibnum); 1305 1306 if (sro.ro_rt == NULL) 1307 return (NULL); 1308 1309 ifa = ifatoia(sro.ro_rt->rt_ifa); 1310 RTFREE(sro.ro_rt); 1311 return (ifa); 1312} 1313 1314u_char inetctlerrmap[PRC_NCMDS] = { 1315 0, 0, 0, 0, 1316 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1317 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1318 EMSGSIZE, EHOSTUNREACH, 0, 0, 1319 0, 0, EHOSTUNREACH, 0, 1320 ENOPROTOOPT, ECONNREFUSED 1321}; 1322 1323/* 1324 * Forward a packet. If some error occurs return the sender 1325 * an icmp packet. Note we can't always generate a meaningful 1326 * icmp message because icmp doesn't have a large enough repertoire 1327 * of codes and types. 1328 * 1329 * If not forwarding, just drop the packet. This could be confusing 1330 * if ipforwarding was zero but some routing protocol was advancing 1331 * us as a gateway to somewhere. However, we must let the routing 1332 * protocol deal with that. 1333 * 1334 * The srcrt parameter indicates whether the packet is being forwarded 1335 * via a source route. 1336 */ 1337void 1338ip_forward(struct mbuf *m, int srcrt) 1339{ 1340 INIT_VNET_INET(curvnet); 1341 struct ip *ip = mtod(m, struct ip *); 1342 struct in_ifaddr *ia = NULL; 1343 struct mbuf *mcopy; 1344 struct in_addr dest; 1345 struct route ro; 1346 int error, type = 0, code = 0, mtu = 0; 1347 1348 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1349 V_ipstat.ips_cantforward++; 1350 m_freem(m); 1351 return; 1352 } 1353#ifdef IPSTEALTH 1354 if (!V_ipstealth) { 1355#endif 1356 if (ip->ip_ttl <= IPTTLDEC) { 1357 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1358 0, 0); 1359 return; 1360 } 1361#ifdef IPSTEALTH 1362 } 1363#endif 1364 1365 ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m)); 1366 if (!srcrt && ia == NULL) { 1367 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1368 return; 1369 } 1370 1371 /* 1372 * Save the IP header and at most 8 bytes of the payload, 1373 * in case we need to generate an ICMP message to the src. 1374 * 1375 * XXX this can be optimized a lot by saving the data in a local 1376 * buffer on the stack (72 bytes at most), and only allocating the 1377 * mbuf if really necessary. The vast majority of the packets 1378 * are forwarded without having to send an ICMP back (either 1379 * because unnecessary, or because rate limited), so we are 1380 * really we are wasting a lot of work here. 1381 * 1382 * We don't use m_copy() because it might return a reference 1383 * to a shared cluster. Both this function and ip_output() 1384 * assume exclusive access to the IP header in `m', so any 1385 * data in a cluster may change before we reach icmp_error(). 1386 */ 1387 MGETHDR(mcopy, M_DONTWAIT, m->m_type); 1388 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) { 1389 /* 1390 * It's probably ok if the pkthdr dup fails (because 1391 * the deep copy of the tag chain failed), but for now 1392 * be conservative and just discard the copy since 1393 * code below may some day want the tags. 1394 */ 1395 m_free(mcopy); 1396 mcopy = NULL; 1397 } 1398 if (mcopy != NULL) { 1399 mcopy->m_len = min(ip->ip_len, M_TRAILINGSPACE(mcopy)); 1400 mcopy->m_pkthdr.len = mcopy->m_len; 1401 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1402 } 1403 1404#ifdef IPSTEALTH 1405 if (!V_ipstealth) { 1406#endif 1407 ip->ip_ttl -= IPTTLDEC; 1408#ifdef IPSTEALTH 1409 } 1410#endif 1411 1412 /* 1413 * If forwarding packet using same interface that it came in on, 1414 * perhaps should send a redirect to sender to shortcut a hop. 1415 * Only send redirect if source is sending directly to us, 1416 * and if packet was not source routed (or has any options). 1417 * Also, don't send redirect if forwarding using a default route 1418 * or a route modified by a redirect. 1419 */ 1420 dest.s_addr = 0; 1421 if (!srcrt && V_ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) { 1422 struct sockaddr_in *sin; 1423 struct rtentry *rt; 1424 1425 bzero(&ro, sizeof(ro)); 1426 sin = (struct sockaddr_in *)&ro.ro_dst; 1427 sin->sin_family = AF_INET; 1428 sin->sin_len = sizeof(*sin); 1429 sin->sin_addr = ip->ip_dst; 1430 in_rtalloc_ign(&ro, 0, M_GETFIB(m)); 1431 1432 rt = ro.ro_rt; 1433 1434 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1435 satosin(rt_key(rt))->sin_addr.s_addr != 0) { 1436#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1437 u_long src = ntohl(ip->ip_src.s_addr); 1438 1439 if (RTA(rt) && 1440 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1441 if (rt->rt_flags & RTF_GATEWAY) 1442 dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr; 1443 else 1444 dest.s_addr = ip->ip_dst.s_addr; 1445 /* Router requirements says to only send host redirects */ 1446 type = ICMP_REDIRECT; 1447 code = ICMP_REDIRECT_HOST; 1448 } 1449 } 1450 if (rt) 1451 RTFREE(rt); 1452 } 1453 1454 /* 1455 * Try to cache the route MTU from ip_output so we can consider it for 1456 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191. 1457 */ 1458 bzero(&ro, sizeof(ro)); 1459 1460 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL); 1461 1462 if (error == EMSGSIZE && ro.ro_rt) 1463 mtu = ro.ro_rt->rt_rmx.rmx_mtu; 1464 if (ro.ro_rt) 1465 RTFREE(ro.ro_rt); 1466 1467 if (error) 1468 V_ipstat.ips_cantforward++; 1469 else { 1470 V_ipstat.ips_forward++; 1471 if (type) 1472 V_ipstat.ips_redirectsent++; 1473 else { 1474 if (mcopy) 1475 m_freem(mcopy); 1476 return; 1477 } 1478 } 1479 if (mcopy == NULL) 1480 return; 1481 1482 switch (error) { 1483 1484 case 0: /* forwarded, but need redirect */ 1485 /* type, code set above */ 1486 break; 1487 1488 case ENETUNREACH: /* shouldn't happen, checked above */ 1489 case EHOSTUNREACH: 1490 case ENETDOWN: 1491 case EHOSTDOWN: 1492 default: 1493 type = ICMP_UNREACH; 1494 code = ICMP_UNREACH_HOST; 1495 break; 1496 1497 case EMSGSIZE: 1498 type = ICMP_UNREACH; 1499 code = ICMP_UNREACH_NEEDFRAG; 1500 1501#ifdef IPSEC 1502 /* 1503 * If IPsec is configured for this path, 1504 * override any possibly mtu value set by ip_output. 1505 */ 1506 mtu = ip_ipsec_mtu(m, mtu); 1507#endif /* IPSEC */ 1508 /* 1509 * If the MTU was set before make sure we are below the 1510 * interface MTU. 1511 * If the MTU wasn't set before use the interface mtu or 1512 * fall back to the next smaller mtu step compared to the 1513 * current packet size. 1514 */ 1515 if (mtu != 0) { 1516 if (ia != NULL) 1517 mtu = min(mtu, ia->ia_ifp->if_mtu); 1518 } else { 1519 if (ia != NULL) 1520 mtu = ia->ia_ifp->if_mtu; 1521 else 1522 mtu = ip_next_mtu(ip->ip_len, 0); 1523 } 1524 V_ipstat.ips_cantfrag++; 1525 break; 1526 1527 case ENOBUFS: 1528 /* 1529 * A router should not generate ICMP_SOURCEQUENCH as 1530 * required in RFC1812 Requirements for IP Version 4 Routers. 1531 * Source quench could be a big problem under DoS attacks, 1532 * or if the underlying interface is rate-limited. 1533 * Those who need source quench packets may re-enable them 1534 * via the net.inet.ip.sendsourcequench sysctl. 1535 */ 1536 if (V_ip_sendsourcequench == 0) { 1537 m_freem(mcopy); 1538 return; 1539 } else { 1540 type = ICMP_SOURCEQUENCH; 1541 code = 0; 1542 } 1543 break; 1544 1545 case EACCES: /* ipfw denied packet */ 1546 m_freem(mcopy); 1547 return; 1548 } 1549 icmp_error(mcopy, type, code, dest.s_addr, mtu); 1550} 1551 1552void 1553ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 1554 struct mbuf *m) 1555{ 1556 INIT_VNET_NET(inp->inp_vnet); 1557 1558 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 1559 struct bintime bt; 1560 1561 bintime(&bt); 1562 if (inp->inp_socket->so_options & SO_BINTIME) { 1563 *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt), 1564 SCM_BINTIME, SOL_SOCKET); 1565 if (*mp) 1566 mp = &(*mp)->m_next; 1567 } 1568 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1569 struct timeval tv; 1570 1571 bintime2timeval(&bt, &tv); 1572 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1573 SCM_TIMESTAMP, SOL_SOCKET); 1574 if (*mp) 1575 mp = &(*mp)->m_next; 1576 } 1577 } 1578 if (inp->inp_flags & INP_RECVDSTADDR) { 1579 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1580 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1581 if (*mp) 1582 mp = &(*mp)->m_next; 1583 } 1584 if (inp->inp_flags & INP_RECVTTL) { 1585 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, 1586 sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 1587 if (*mp) 1588 mp = &(*mp)->m_next; 1589 } 1590#ifdef notyet 1591 /* XXX 1592 * Moving these out of udp_input() made them even more broken 1593 * than they already were. 1594 */ 1595 /* options were tossed already */ 1596 if (inp->inp_flags & INP_RECVOPTS) { 1597 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1598 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1599 if (*mp) 1600 mp = &(*mp)->m_next; 1601 } 1602 /* ip_srcroute doesn't do what we want here, need to fix */ 1603 if (inp->inp_flags & INP_RECVRETOPTS) { 1604 *mp = sbcreatecontrol((caddr_t) ip_srcroute(m), 1605 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1606 if (*mp) 1607 mp = &(*mp)->m_next; 1608 } 1609#endif 1610 if (inp->inp_flags & INP_RECVIF) { 1611 struct ifnet *ifp; 1612 struct sdlbuf { 1613 struct sockaddr_dl sdl; 1614 u_char pad[32]; 1615 } sdlbuf; 1616 struct sockaddr_dl *sdp; 1617 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 1618 1619 if (((ifp = m->m_pkthdr.rcvif)) 1620 && ( ifp->if_index && (ifp->if_index <= V_if_index))) { 1621 sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr; 1622 /* 1623 * Change our mind and don't try copy. 1624 */ 1625 if ((sdp->sdl_family != AF_LINK) 1626 || (sdp->sdl_len > sizeof(sdlbuf))) { 1627 goto makedummy; 1628 } 1629 bcopy(sdp, sdl2, sdp->sdl_len); 1630 } else { 1631makedummy: 1632 sdl2->sdl_len 1633 = offsetof(struct sockaddr_dl, sdl_data[0]); 1634 sdl2->sdl_family = AF_LINK; 1635 sdl2->sdl_index = 0; 1636 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1637 } 1638 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 1639 IP_RECVIF, IPPROTO_IP); 1640 if (*mp) 1641 mp = &(*mp)->m_next; 1642 } 1643} 1644 1645/* 1646 * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the 1647 * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on 1648 * locking. This code remains in ip_input.c as ip_mroute.c is optionally 1649 * compiled. 1650 */ 1651int 1652ip_rsvp_init(struct socket *so) 1653{ 1654 INIT_VNET_INET(so->so_vnet); 1655 1656 if (so->so_type != SOCK_RAW || 1657 so->so_proto->pr_protocol != IPPROTO_RSVP) 1658 return EOPNOTSUPP; 1659 1660 if (V_ip_rsvpd != NULL) 1661 return EADDRINUSE; 1662 1663 V_ip_rsvpd = so; 1664 /* 1665 * This may seem silly, but we need to be sure we don't over-increment 1666 * the RSVP counter, in case something slips up. 1667 */ 1668 if (!V_ip_rsvp_on) { 1669 V_ip_rsvp_on = 1; 1670 V_rsvp_on++; 1671 } 1672 1673 return 0; 1674} 1675 1676int 1677ip_rsvp_done(void) 1678{ 1679 INIT_VNET_INET(curvnet); 1680 1681 V_ip_rsvpd = NULL; 1682 /* 1683 * This may seem silly, but we need to be sure we don't over-decrement 1684 * the RSVP counter, in case something slips up. 1685 */ 1686 if (V_ip_rsvp_on) { 1687 V_ip_rsvp_on = 0; 1688 V_rsvp_on--; 1689 } 1690 return 0; 1691} 1692 1693void 1694rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */ 1695{ 1696 INIT_VNET_INET(curvnet); 1697 1698 if (rsvp_input_p) { /* call the real one if loaded */ 1699 rsvp_input_p(m, off); 1700 return; 1701 } 1702 1703 /* Can still get packets with rsvp_on = 0 if there is a local member 1704 * of the group to which the RSVP packet is addressed. But in this 1705 * case we want to throw the packet away. 1706 */ 1707 1708 if (!V_rsvp_on) { 1709 m_freem(m); 1710 return; 1711 } 1712 1713 if (V_ip_rsvpd != NULL) { 1714 rip_input(m, off); 1715 return; 1716 } 1717 /* Drop the packet */ 1718 m_freem(m); 1719} 1720