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