ip_reass.c revision 142215
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 * $FreeBSD: head/sys/netinet/ip_input.c 142215 2005-02-22 13:04:05Z glebius $ 31 */ 32 33#include "opt_bootp.h" 34#include "opt_ipfw.h" 35#include "opt_ipstealth.h" 36#include "opt_ipsec.h" 37#include "opt_mac.h" 38#include "opt_carp.h" 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/callout.h> 43#include <sys/mac.h> 44#include <sys/mbuf.h> 45#include <sys/malloc.h> 46#include <sys/domain.h> 47#include <sys/protosw.h> 48#include <sys/socket.h> 49#include <sys/time.h> 50#include <sys/kernel.h> 51#include <sys/syslog.h> 52#include <sys/sysctl.h> 53 54#include <net/pfil.h> 55#include <net/if.h> 56#include <net/if_types.h> 57#include <net/if_var.h> 58#include <net/if_dl.h> 59#include <net/route.h> 60#include <net/netisr.h> 61 62#include <netinet/in.h> 63#include <netinet/in_systm.h> 64#include <netinet/in_var.h> 65#include <netinet/ip.h> 66#include <netinet/in_pcb.h> 67#include <netinet/ip_var.h> 68#include <netinet/ip_icmp.h> 69#include <machine/in_cksum.h> 70#ifdef DEV_CARP 71#include <netinet/ip_carp.h> 72#endif 73 74#include <sys/socketvar.h> 75 76/* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */ 77#include <netinet/ip_fw.h> 78#include <netinet/ip_dummynet.h> 79 80#ifdef IPSEC 81#include <netinet6/ipsec.h> 82#include <netkey/key.h> 83#endif 84 85#ifdef FAST_IPSEC 86#include <netipsec/ipsec.h> 87#include <netipsec/key.h> 88#endif 89 90int rsvp_on = 0; 91 92int ipforwarding = 0; 93SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 94 &ipforwarding, 0, "Enable IP forwarding between interfaces"); 95 96static int ipsendredirects = 1; /* XXX */ 97SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 98 &ipsendredirects, 0, "Enable sending IP redirects"); 99 100int ip_defttl = IPDEFTTL; 101SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 102 &ip_defttl, 0, "Maximum TTL on IP packets"); 103 104static int ip_dosourceroute = 0; 105SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 106 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); 107 108static int ip_acceptsourceroute = 0; 109SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 110 CTLFLAG_RW, &ip_acceptsourceroute, 0, 111 "Enable accepting source routed IP packets"); 112 113int ip_doopts = 1; /* 0 = ignore, 1 = process, 2 = reject */ 114SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW, 115 &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)"); 116 117static int ip_keepfaith = 0; 118SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, 119 &ip_keepfaith, 0, 120 "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 121 122static int nipq = 0; /* total # of reass queues */ 123static int maxnipq; 124SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW, 125 &maxnipq, 0, 126 "Maximum number of IPv4 fragment reassembly queue entries"); 127 128static int maxfragsperpacket; 129SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW, 130 &maxfragsperpacket, 0, 131 "Maximum number of IPv4 fragments allowed per packet"); 132 133static int ip_sendsourcequench = 0; 134SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW, 135 &ip_sendsourcequench, 0, 136 "Enable the transmission of source quench packets"); 137 138int ip_do_randomid = 0; 139SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW, 140 &ip_do_randomid, 0, 141 "Assign random ip_id values"); 142 143/* 144 * XXX - Setting ip_checkinterface mostly implements the receive side of 145 * the Strong ES model described in RFC 1122, but since the routing table 146 * and transmit implementation do not implement the Strong ES model, 147 * setting this to 1 results in an odd hybrid. 148 * 149 * XXX - ip_checkinterface currently must be disabled if you use ipnat 150 * to translate the destination address to another local interface. 151 * 152 * XXX - ip_checkinterface must be disabled if you add IP aliases 153 * to the loopback interface instead of the interface where the 154 * packets for those addresses are received. 155 */ 156static int ip_checkinterface = 0; 157SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW, 158 &ip_checkinterface, 0, "Verify packet arrives on correct interface"); 159 160#ifdef DIAGNOSTIC 161static int ipprintfs = 0; 162#endif 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]; 172struct in_ifaddrhead in_ifaddrhead; /* first inet address */ 173struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */ 174u_long in_ifaddrhmask; /* mask for hash table */ 175 176SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW, 177 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue"); 178SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, 179 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue"); 180 181struct ipstat ipstat; 182SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW, 183 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)"); 184 185/* Packet reassembly stuff */ 186#define IPREASS_NHASH_LOG2 6 187#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) 188#define IPREASS_HMASK (IPREASS_NHASH - 1) 189#define IPREASS_HASH(x,y) \ 190 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) 191 192static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH]; 193struct mtx ipqlock; 194struct callout ipport_tick_callout; 195 196#define IPQ_LOCK() mtx_lock(&ipqlock) 197#define IPQ_UNLOCK() mtx_unlock(&ipqlock) 198#define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF) 199#define IPQ_LOCK_ASSERT() mtx_assert(&ipqlock, MA_OWNED) 200 201#ifdef IPCTL_DEFMTU 202SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 203 &ip_mtu, 0, "Default MTU"); 204#endif 205 206#ifdef IPSTEALTH 207int ipstealth = 0; 208SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 209 &ipstealth, 0, ""); 210#endif 211 212/* 213 * ipfw_ether and ipfw_bridge hooks. 214 * XXX: Temporary until those are converted to pfil_hooks as well. 215 */ 216ip_fw_chk_t *ip_fw_chk_ptr = NULL; 217ip_dn_io_t *ip_dn_io_ptr = NULL; 218int fw_enable = 1; 219int fw_one_pass = 1; 220 221/* 222 * XXX this is ugly. IP options source routing magic. 223 */ 224struct ipoptrt { 225 struct in_addr dst; /* final destination */ 226 char nop; /* one NOP to align */ 227 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 228 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 229}; 230 231struct ipopt_tag { 232 struct m_tag tag; 233 int ip_nhops; 234 struct ipoptrt ip_srcrt; 235}; 236 237static void save_rte(struct mbuf *, u_char *, struct in_addr); 238static int ip_dooptions(struct mbuf *m, int); 239static void ip_forward(struct mbuf *m, int srcrt); 240static void ip_freef(struct ipqhead *, struct ipq *); 241 242/* 243 * IP initialization: fill in IP protocol switch table. 244 * All protocols not implemented in kernel go to raw IP protocol handler. 245 */ 246void 247ip_init() 248{ 249 register struct protosw *pr; 250 register int i; 251 252 TAILQ_INIT(&in_ifaddrhead); 253 in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask); 254 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 255 if (pr == NULL) 256 panic("ip_init: PF_INET not found"); 257 258 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */ 259 for (i = 0; i < IPPROTO_MAX; i++) 260 ip_protox[i] = pr - inetsw; 261 /* 262 * Cycle through IP protocols and put them into the appropriate place 263 * in ip_protox[]. 264 */ 265 for (pr = inetdomain.dom_protosw; 266 pr < inetdomain.dom_protoswNPROTOSW; pr++) 267 if (pr->pr_domain->dom_family == PF_INET && 268 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 269 /* Be careful to only index valid IP protocols. */ 270 if (pr->pr_protocol <= IPPROTO_MAX) 271 ip_protox[pr->pr_protocol] = pr - inetsw; 272 } 273 274 /* Initialize packet filter hooks. */ 275 inet_pfil_hook.ph_type = PFIL_TYPE_AF; 276 inet_pfil_hook.ph_af = AF_INET; 277 if ((i = pfil_head_register(&inet_pfil_hook)) != 0) 278 printf("%s: WARNING: unable to register pfil hook, " 279 "error %d\n", __func__, i); 280 281 /* Initialize IP reassembly queue. */ 282 IPQ_LOCK_INIT(); 283 for (i = 0; i < IPREASS_NHASH; i++) 284 TAILQ_INIT(&ipq[i]); 285 maxnipq = nmbclusters / 32; 286 maxfragsperpacket = 16; 287 288 /* Start ipport_tick. */ 289 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE); 290 ipport_tick(NULL); 291 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL, 292 SHUTDOWN_PRI_DEFAULT); 293 294 /* Initialize various other remaining things. */ 295 ip_id = time_second & 0xffff; 296 ipintrq.ifq_maxlen = ipqmaxlen; 297 mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF); 298 netisr_register(NETISR_IP, ip_input, &ipintrq, NETISR_MPSAFE); 299} 300 301void ip_fini(xtp) 302 void *xtp; 303{ 304 callout_stop(&ipport_tick_callout); 305} 306 307/* 308 * Ip input routine. Checksum and byte swap header. If fragmented 309 * try to reassemble. Process options. Pass to next level. 310 */ 311void 312ip_input(struct mbuf *m) 313{ 314 struct ip *ip = NULL; 315 struct in_ifaddr *ia = NULL; 316 struct ifaddr *ifa; 317 int checkif, hlen = 0; 318 u_short sum; 319 int dchg = 0; /* dest changed after fw */ 320 struct in_addr odst; /* original dst address */ 321#ifdef FAST_IPSEC 322 struct m_tag *mtag; 323 struct tdb_ident *tdbi; 324 struct secpolicy *sp; 325 int s, error; 326#endif /* FAST_IPSEC */ 327 328 M_ASSERTPKTHDR(m); 329 330 if (m->m_flags & M_FASTFWD_OURS) { 331 /* 332 * Firewall or NAT changed destination to local. 333 * We expect ip_len and ip_off to be in host byte order. 334 */ 335 m->m_flags &= ~M_FASTFWD_OURS; 336 /* Set up some basics that will be used later. */ 337 ip = mtod(m, struct ip *); 338 hlen = ip->ip_hl << 2; 339 goto ours; 340 } 341 342 ipstat.ips_total++; 343 344 if (m->m_pkthdr.len < sizeof(struct ip)) 345 goto tooshort; 346 347 if (m->m_len < sizeof (struct ip) && 348 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 349 ipstat.ips_toosmall++; 350 return; 351 } 352 ip = mtod(m, struct ip *); 353 354 if (ip->ip_v != IPVERSION) { 355 ipstat.ips_badvers++; 356 goto bad; 357 } 358 359 hlen = ip->ip_hl << 2; 360 if (hlen < sizeof(struct ip)) { /* minimum header length */ 361 ipstat.ips_badhlen++; 362 goto bad; 363 } 364 if (hlen > m->m_len) { 365 if ((m = m_pullup(m, hlen)) == NULL) { 366 ipstat.ips_badhlen++; 367 return; 368 } 369 ip = mtod(m, struct ip *); 370 } 371 372 /* 127/8 must not appear on wire - RFC1122 */ 373 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 374 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 375 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 376 ipstat.ips_badaddr++; 377 goto bad; 378 } 379 } 380 381 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 382 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 383 } else { 384 if (hlen == sizeof(struct ip)) { 385 sum = in_cksum_hdr(ip); 386 } else { 387 sum = in_cksum(m, hlen); 388 } 389 } 390 if (sum) { 391 ipstat.ips_badsum++; 392 goto bad; 393 } 394 395#ifdef ALTQ 396 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 397 /* packet is dropped by traffic conditioner */ 398 return; 399#endif 400 401 /* 402 * Convert fields to host representation. 403 */ 404 ip->ip_len = ntohs(ip->ip_len); 405 if (ip->ip_len < hlen) { 406 ipstat.ips_badlen++; 407 goto bad; 408 } 409 ip->ip_off = ntohs(ip->ip_off); 410 411 /* 412 * Check that the amount of data in the buffers 413 * is as at least much as the IP header would have us expect. 414 * Trim mbufs if longer than we expect. 415 * Drop packet if shorter than we expect. 416 */ 417 if (m->m_pkthdr.len < ip->ip_len) { 418tooshort: 419 ipstat.ips_tooshort++; 420 goto bad; 421 } 422 if (m->m_pkthdr.len > ip->ip_len) { 423 if (m->m_len == m->m_pkthdr.len) { 424 m->m_len = ip->ip_len; 425 m->m_pkthdr.len = ip->ip_len; 426 } else 427 m_adj(m, ip->ip_len - m->m_pkthdr.len); 428 } 429#if defined(IPSEC) && !defined(IPSEC_FILTERGIF) 430 /* 431 * Bypass packet filtering for packets from a tunnel (gif). 432 */ 433 if (ipsec_getnhist(m)) 434 goto passin; 435#endif 436#if defined(FAST_IPSEC) && !defined(IPSEC_FILTERGIF) 437 /* 438 * Bypass packet filtering for packets from a tunnel (gif). 439 */ 440 if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL) 441 goto passin; 442#endif 443 444 /* 445 * Run through list of hooks for input packets. 446 * 447 * NB: Beware of the destination address changing (e.g. 448 * by NAT rewriting). When this happens, tell 449 * ip_forward to do the right thing. 450 */ 451 452 /* Jump over all PFIL processing if hooks are not active. */ 453 if (inet_pfil_hook.ph_busy_count == -1) 454 goto passin; 455 456 odst = ip->ip_dst; 457 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, 458 PFIL_IN, NULL) != 0) 459 return; 460 if (m == NULL) /* consumed by filter */ 461 return; 462 463 ip = mtod(m, struct ip *); 464 dchg = (odst.s_addr != ip->ip_dst.s_addr); 465 466#ifdef IPFIREWALL_FORWARD 467 if (m->m_flags & M_FASTFWD_OURS) { 468 m->m_flags &= ~M_FASTFWD_OURS; 469 goto ours; 470 } 471 dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL); 472#endif /* IPFIREWALL_FORWARD */ 473 474passin: 475 /* 476 * Process options and, if not destined for us, 477 * ship it on. ip_dooptions returns 1 when an 478 * error was detected (causing an icmp message 479 * to be sent and the original packet to be freed). 480 */ 481 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0)) 482 return; 483 484 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 485 * matter if it is destined to another node, or whether it is 486 * a multicast one, RSVP wants it! and prevents it from being forwarded 487 * anywhere else. Also checks if the rsvp daemon is running before 488 * grabbing the packet. 489 */ 490 if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 491 goto ours; 492 493 /* 494 * Check our list of addresses, to see if the packet is for us. 495 * If we don't have any addresses, assume any unicast packet 496 * we receive might be for us (and let the upper layers deal 497 * with it). 498 */ 499 if (TAILQ_EMPTY(&in_ifaddrhead) && 500 (m->m_flags & (M_MCAST|M_BCAST)) == 0) 501 goto ours; 502 503 /* 504 * Enable a consistency check between the destination address 505 * and the arrival interface for a unicast packet (the RFC 1122 506 * strong ES model) if IP forwarding is disabled and the packet 507 * is not locally generated and the packet is not subject to 508 * 'ipfw fwd'. 509 * 510 * XXX - Checking also should be disabled if the destination 511 * address is ipnat'ed to a different interface. 512 * 513 * XXX - Checking is incompatible with IP aliases added 514 * to the loopback interface instead of the interface where 515 * the packets are received. 516 * 517 * XXX - This is the case for carp vhost IPs as well so we 518 * insert a workaround. If the packet got here, we already 519 * checked with carp_iamatch() and carp_forus(). 520 */ 521 checkif = ip_checkinterface && (ipforwarding == 0) && 522 m->m_pkthdr.rcvif != NULL && 523 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) && 524#ifdef DEV_CARP 525 !m->m_pkthdr.rcvif->if_carp && 526#endif 527 (dchg == 0); 528 529 /* 530 * Check for exact addresses in the hash bucket. 531 */ 532 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 533 /* 534 * If the address matches, verify that the packet 535 * arrived via the correct interface if checking is 536 * enabled. 537 */ 538 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr && 539 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) 540 goto ours; 541 } 542 /* 543 * Check for broadcast addresses. 544 * 545 * Only accept broadcast packets that arrive via the matching 546 * interface. Reception of forwarded directed broadcasts would 547 * be handled via ip_forward() and ether_output() with the loopback 548 * into the stack for SIMPLEX interfaces handled by ether_output(). 549 */ 550 if (m->m_pkthdr.rcvif != NULL && 551 m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 552 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 553 if (ifa->ifa_addr->sa_family != AF_INET) 554 continue; 555 ia = ifatoia(ifa); 556 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 557 ip->ip_dst.s_addr) 558 goto ours; 559 if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr) 560 goto ours; 561#ifdef BOOTP_COMPAT 562 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 563 goto ours; 564#endif 565 } 566 } 567 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 568 struct in_multi *inm; 569 if (ip_mrouter) { 570 /* 571 * If we are acting as a multicast router, all 572 * incoming multicast packets are passed to the 573 * kernel-level multicast forwarding function. 574 * The packet is returned (relatively) intact; if 575 * ip_mforward() returns a non-zero value, the packet 576 * must be discarded, else it may be accepted below. 577 */ 578 if (ip_mforward && 579 ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 580 ipstat.ips_cantforward++; 581 m_freem(m); 582 return; 583 } 584 585 /* 586 * The process-level routing daemon needs to receive 587 * all multicast IGMP packets, whether or not this 588 * host belongs to their destination groups. 589 */ 590 if (ip->ip_p == IPPROTO_IGMP) 591 goto ours; 592 ipstat.ips_forward++; 593 } 594 /* 595 * See if we belong to the destination multicast group on the 596 * arrival interface. 597 */ 598 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 599 if (inm == NULL) { 600 ipstat.ips_notmember++; 601 m_freem(m); 602 return; 603 } 604 goto ours; 605 } 606 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 607 goto ours; 608 if (ip->ip_dst.s_addr == INADDR_ANY) 609 goto ours; 610 611 /* 612 * FAITH(Firewall Aided Internet Translator) 613 */ 614 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 615 if (ip_keepfaith) { 616 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 617 goto ours; 618 } 619 m_freem(m); 620 return; 621 } 622 623 /* 624 * Not for us; forward if possible and desirable. 625 */ 626 if (ipforwarding == 0) { 627 ipstat.ips_cantforward++; 628 m_freem(m); 629 } else { 630#ifdef IPSEC 631 /* 632 * Enforce inbound IPsec SPD. 633 */ 634 if (ipsec4_in_reject(m, NULL)) { 635 ipsecstat.in_polvio++; 636 goto bad; 637 } 638#endif /* IPSEC */ 639#ifdef FAST_IPSEC 640 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 641 s = splnet(); 642 if (mtag != NULL) { 643 tdbi = (struct tdb_ident *)(mtag + 1); 644 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 645 } else { 646 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 647 IP_FORWARDING, &error); 648 } 649 if (sp == NULL) { /* NB: can happen if error */ 650 splx(s); 651 /*XXX error stat???*/ 652 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/ 653 goto bad; 654 } 655 656 /* 657 * Check security policy against packet attributes. 658 */ 659 error = ipsec_in_reject(sp, m); 660 KEY_FREESP(&sp); 661 splx(s); 662 if (error) { 663 ipstat.ips_cantforward++; 664 goto bad; 665 } 666#endif /* FAST_IPSEC */ 667 ip_forward(m, dchg); 668 } 669 return; 670 671ours: 672#ifdef IPSTEALTH 673 /* 674 * IPSTEALTH: Process non-routing options only 675 * if the packet is destined for us. 676 */ 677 if (ipstealth && hlen > sizeof (struct ip) && 678 ip_dooptions(m, 1)) 679 return; 680#endif /* IPSTEALTH */ 681 682 /* Count the packet in the ip address stats */ 683 if (ia != NULL) { 684 ia->ia_ifa.if_ipackets++; 685 ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 686 } 687 688 /* 689 * Attempt reassembly; if it succeeds, proceed. 690 * ip_reass() will return a different mbuf. 691 */ 692 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 693 m = ip_reass(m); 694 if (m == NULL) 695 return; 696 ip = mtod(m, struct ip *); 697 /* Get the header length of the reassembled packet */ 698 hlen = ip->ip_hl << 2; 699 } 700 701 /* 702 * Further protocols expect the packet length to be w/o the 703 * IP header. 704 */ 705 ip->ip_len -= hlen; 706 707#ifdef IPSEC 708 /* 709 * enforce IPsec policy checking if we are seeing last header. 710 * note that we do not visit this with protocols with pcb layer 711 * code - like udp/tcp/raw ip. 712 */ 713 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 && 714 ipsec4_in_reject(m, NULL)) { 715 ipsecstat.in_polvio++; 716 goto bad; 717 } 718#endif 719#if FAST_IPSEC 720 /* 721 * enforce IPsec policy checking if we are seeing last header. 722 * note that we do not visit this with protocols with pcb layer 723 * code - like udp/tcp/raw ip. 724 */ 725 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) { 726 /* 727 * Check if the packet has already had IPsec processing 728 * done. If so, then just pass it along. This tag gets 729 * set during AH, ESP, etc. input handling, before the 730 * packet is returned to the ip input queue for delivery. 731 */ 732 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 733 s = splnet(); 734 if (mtag != NULL) { 735 tdbi = (struct tdb_ident *)(mtag + 1); 736 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 737 } else { 738 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 739 IP_FORWARDING, &error); 740 } 741 if (sp != NULL) { 742 /* 743 * Check security policy against packet attributes. 744 */ 745 error = ipsec_in_reject(sp, m); 746 KEY_FREESP(&sp); 747 } else { 748 /* XXX error stat??? */ 749 error = EINVAL; 750DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/ 751 goto bad; 752 } 753 splx(s); 754 if (error) 755 goto bad; 756 } 757#endif /* FAST_IPSEC */ 758 759 /* 760 * Switch out to protocol's input routine. 761 */ 762 ipstat.ips_delivered++; 763 764 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 765 return; 766bad: 767 m_freem(m); 768} 769 770/* 771 * Take incoming datagram fragment and try to reassemble it into 772 * whole datagram. If the argument is the first fragment or one 773 * in between the function will return NULL and store the mbuf 774 * in the fragment chain. If the argument is the last fragment 775 * the packet will be reassembled and the pointer to the new 776 * mbuf returned for further processing. Only m_tags attached 777 * to the first packet/fragment are preserved. 778 * The IP header is *NOT* adjusted out of iplen. 779 */ 780 781struct mbuf * 782ip_reass(struct mbuf *m) 783{ 784 struct ip *ip; 785 struct mbuf *p, *q, *nq, *t; 786 struct ipq *fp = NULL; 787 struct ipqhead *head; 788 int i, hlen, next; 789 u_int8_t ecn, ecn0; 790 u_short hash; 791 792 /* If maxnipq is 0, never accept fragments. */ 793 if (maxnipq == 0) { 794 ipstat.ips_fragments++; 795 ipstat.ips_fragdropped++; 796 m_freem(m); 797 return (NULL); 798 } 799 800 ip = mtod(m, struct ip *); 801 hlen = ip->ip_hl << 2; 802 803 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 804 head = &ipq[hash]; 805 IPQ_LOCK(); 806 807 /* 808 * Look for queue of fragments 809 * of this datagram. 810 */ 811 TAILQ_FOREACH(fp, head, ipq_list) 812 if (ip->ip_id == fp->ipq_id && 813 ip->ip_src.s_addr == fp->ipq_src.s_addr && 814 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 815#ifdef MAC 816 mac_fragment_match(m, fp) && 817#endif 818 ip->ip_p == fp->ipq_p) 819 goto found; 820 821 fp = NULL; 822 823 /* 824 * Enforce upper bound on number of fragmented packets 825 * for which we attempt reassembly; 826 * If maxnipq is -1, accept all fragments without limitation. 827 */ 828 if ((nipq > maxnipq) && (maxnipq > 0)) { 829 /* 830 * drop something from the tail of the current queue 831 * before proceeding further 832 */ 833 struct ipq *q = TAILQ_LAST(head, ipqhead); 834 if (q == NULL) { /* gak */ 835 for (i = 0; i < IPREASS_NHASH; i++) { 836 struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead); 837 if (r) { 838 ipstat.ips_fragtimeout += r->ipq_nfrags; 839 ip_freef(&ipq[i], r); 840 break; 841 } 842 } 843 } else { 844 ipstat.ips_fragtimeout += q->ipq_nfrags; 845 ip_freef(head, q); 846 } 847 } 848 849found: 850 /* 851 * Adjust ip_len to not reflect header, 852 * convert offset of this to bytes. 853 */ 854 ip->ip_len -= hlen; 855 if (ip->ip_off & IP_MF) { 856 /* 857 * Make sure that fragments have a data length 858 * that's a non-zero multiple of 8 bytes. 859 */ 860 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 861 ipstat.ips_toosmall++; /* XXX */ 862 goto dropfrag; 863 } 864 m->m_flags |= M_FRAG; 865 } else 866 m->m_flags &= ~M_FRAG; 867 ip->ip_off <<= 3; 868 869 870 /* 871 * Attempt reassembly; if it succeeds, proceed. 872 * ip_reass() will return a different mbuf. 873 */ 874 ipstat.ips_fragments++; 875 m->m_pkthdr.header = ip; 876 877 /* Previous ip_reass() started here. */ 878 /* 879 * Presence of header sizes in mbufs 880 * would confuse code below. 881 */ 882 m->m_data += hlen; 883 m->m_len -= hlen; 884 885 /* 886 * If first fragment to arrive, create a reassembly queue. 887 */ 888 if (fp == NULL) { 889 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 890 goto dropfrag; 891 fp = mtod(t, struct ipq *); 892#ifdef MAC 893 if (mac_init_ipq(fp, M_NOWAIT) != 0) { 894 m_free(t); 895 goto dropfrag; 896 } 897 mac_create_ipq(m, fp); 898#endif 899 TAILQ_INSERT_HEAD(head, fp, ipq_list); 900 nipq++; 901 fp->ipq_nfrags = 1; 902 fp->ipq_ttl = IPFRAGTTL; 903 fp->ipq_p = ip->ip_p; 904 fp->ipq_id = ip->ip_id; 905 fp->ipq_src = ip->ip_src; 906 fp->ipq_dst = ip->ip_dst; 907 fp->ipq_frags = m; 908 m->m_nextpkt = NULL; 909 goto inserted; 910 } else { 911 fp->ipq_nfrags++; 912#ifdef MAC 913 mac_update_ipq(m, fp); 914#endif 915 } 916 917#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 918 919 /* 920 * Handle ECN by comparing this segment with the first one; 921 * if CE is set, do not lose CE. 922 * drop if CE and not-ECT are mixed for the same packet. 923 */ 924 ecn = ip->ip_tos & IPTOS_ECN_MASK; 925 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 926 if (ecn == IPTOS_ECN_CE) { 927 if (ecn0 == IPTOS_ECN_NOTECT) 928 goto dropfrag; 929 if (ecn0 != IPTOS_ECN_CE) 930 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 931 } 932 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 933 goto dropfrag; 934 935 /* 936 * Find a segment which begins after this one does. 937 */ 938 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 939 if (GETIP(q)->ip_off > ip->ip_off) 940 break; 941 942 /* 943 * If there is a preceding segment, it may provide some of 944 * our data already. If so, drop the data from the incoming 945 * segment. If it provides all of our data, drop us, otherwise 946 * stick new segment in the proper place. 947 * 948 * If some of the data is dropped from the the preceding 949 * segment, then it's checksum is invalidated. 950 */ 951 if (p) { 952 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 953 if (i > 0) { 954 if (i >= ip->ip_len) 955 goto dropfrag; 956 m_adj(m, i); 957 m->m_pkthdr.csum_flags = 0; 958 ip->ip_off += i; 959 ip->ip_len -= i; 960 } 961 m->m_nextpkt = p->m_nextpkt; 962 p->m_nextpkt = m; 963 } else { 964 m->m_nextpkt = fp->ipq_frags; 965 fp->ipq_frags = m; 966 } 967 968 /* 969 * While we overlap succeeding segments trim them or, 970 * if they are completely covered, dequeue them. 971 */ 972 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 973 q = nq) { 974 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off; 975 if (i < GETIP(q)->ip_len) { 976 GETIP(q)->ip_len -= i; 977 GETIP(q)->ip_off += i; 978 m_adj(q, i); 979 q->m_pkthdr.csum_flags = 0; 980 break; 981 } 982 nq = q->m_nextpkt; 983 m->m_nextpkt = nq; 984 ipstat.ips_fragdropped++; 985 fp->ipq_nfrags--; 986 m_freem(q); 987 } 988 989inserted: 990 991 /* 992 * Check for complete reassembly and perform frag per packet 993 * limiting. 994 * 995 * Frag limiting is performed here so that the nth frag has 996 * a chance to complete the packet before we drop the packet. 997 * As a result, n+1 frags are actually allowed per packet, but 998 * only n will ever be stored. (n = maxfragsperpacket.) 999 * 1000 */ 1001 next = 0; 1002 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1003 if (GETIP(q)->ip_off != next) { 1004 if (fp->ipq_nfrags > maxfragsperpacket) { 1005 ipstat.ips_fragdropped += fp->ipq_nfrags; 1006 ip_freef(head, fp); 1007 } 1008 goto done; 1009 } 1010 next += GETIP(q)->ip_len; 1011 } 1012 /* Make sure the last packet didn't have the IP_MF flag */ 1013 if (p->m_flags & M_FRAG) { 1014 if (fp->ipq_nfrags > maxfragsperpacket) { 1015 ipstat.ips_fragdropped += fp->ipq_nfrags; 1016 ip_freef(head, fp); 1017 } 1018 goto done; 1019 } 1020 1021 /* 1022 * Reassembly is complete. Make sure the packet is a sane size. 1023 */ 1024 q = fp->ipq_frags; 1025 ip = GETIP(q); 1026 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 1027 ipstat.ips_toolong++; 1028 ipstat.ips_fragdropped += fp->ipq_nfrags; 1029 ip_freef(head, fp); 1030 goto done; 1031 } 1032 1033 /* 1034 * Concatenate fragments. 1035 */ 1036 m = q; 1037 t = m->m_next; 1038 m->m_next = NULL; 1039 m_cat(m, t); 1040 nq = q->m_nextpkt; 1041 q->m_nextpkt = NULL; 1042 for (q = nq; q != NULL; q = nq) { 1043 nq = q->m_nextpkt; 1044 q->m_nextpkt = NULL; 1045 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1046 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1047 m_cat(m, q); 1048 } 1049#ifdef MAC 1050 mac_create_datagram_from_ipq(fp, m); 1051 mac_destroy_ipq(fp); 1052#endif 1053 1054 /* 1055 * Create header for new ip packet by modifying header of first 1056 * packet; dequeue and discard fragment reassembly header. 1057 * Make header visible. 1058 */ 1059 ip->ip_len = (ip->ip_hl << 2) + next; 1060 ip->ip_src = fp->ipq_src; 1061 ip->ip_dst = fp->ipq_dst; 1062 TAILQ_REMOVE(head, fp, ipq_list); 1063 nipq--; 1064 (void) m_free(dtom(fp)); 1065 m->m_len += (ip->ip_hl << 2); 1066 m->m_data -= (ip->ip_hl << 2); 1067 /* some debugging cruft by sklower, below, will go away soon */ 1068 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 1069 m_fixhdr(m); 1070 ipstat.ips_reassembled++; 1071 IPQ_UNLOCK(); 1072 return (m); 1073 1074dropfrag: 1075 ipstat.ips_fragdropped++; 1076 if (fp != NULL) 1077 fp->ipq_nfrags--; 1078 m_freem(m); 1079done: 1080 IPQ_UNLOCK(); 1081 return (NULL); 1082 1083#undef GETIP 1084} 1085 1086/* 1087 * Free a fragment reassembly header and all 1088 * associated datagrams. 1089 */ 1090static void 1091ip_freef(fhp, fp) 1092 struct ipqhead *fhp; 1093 struct ipq *fp; 1094{ 1095 register struct mbuf *q; 1096 1097 IPQ_LOCK_ASSERT(); 1098 1099 while (fp->ipq_frags) { 1100 q = fp->ipq_frags; 1101 fp->ipq_frags = q->m_nextpkt; 1102 m_freem(q); 1103 } 1104 TAILQ_REMOVE(fhp, fp, ipq_list); 1105 (void) m_free(dtom(fp)); 1106 nipq--; 1107} 1108 1109/* 1110 * IP timer processing; 1111 * if a timer expires on a reassembly 1112 * queue, discard it. 1113 */ 1114void 1115ip_slowtimo() 1116{ 1117 register struct ipq *fp; 1118 int s = splnet(); 1119 int i; 1120 1121 IPQ_LOCK(); 1122 for (i = 0; i < IPREASS_NHASH; i++) { 1123 for(fp = TAILQ_FIRST(&ipq[i]); fp;) { 1124 struct ipq *fpp; 1125 1126 fpp = fp; 1127 fp = TAILQ_NEXT(fp, ipq_list); 1128 if(--fpp->ipq_ttl == 0) { 1129 ipstat.ips_fragtimeout += fpp->ipq_nfrags; 1130 ip_freef(&ipq[i], fpp); 1131 } 1132 } 1133 } 1134 /* 1135 * If we are over the maximum number of fragments 1136 * (due to the limit being lowered), drain off 1137 * enough to get down to the new limit. 1138 */ 1139 if (maxnipq >= 0 && nipq > maxnipq) { 1140 for (i = 0; i < IPREASS_NHASH; i++) { 1141 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) { 1142 ipstat.ips_fragdropped += 1143 TAILQ_FIRST(&ipq[i])->ipq_nfrags; 1144 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i])); 1145 } 1146 } 1147 } 1148 IPQ_UNLOCK(); 1149 splx(s); 1150} 1151 1152/* 1153 * Drain off all datagram fragments. 1154 */ 1155void 1156ip_drain() 1157{ 1158 int i; 1159 1160 IPQ_LOCK(); 1161 for (i = 0; i < IPREASS_NHASH; i++) { 1162 while(!TAILQ_EMPTY(&ipq[i])) { 1163 ipstat.ips_fragdropped += 1164 TAILQ_FIRST(&ipq[i])->ipq_nfrags; 1165 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i])); 1166 } 1167 } 1168 IPQ_UNLOCK(); 1169 in_rtqdrain(); 1170} 1171 1172/* 1173 * The protocol to be inserted into ip_protox[] must be already registered 1174 * in inetsw[], either statically or through pf_proto_register(). 1175 */ 1176int 1177ipproto_register(u_char ipproto) 1178{ 1179 struct protosw *pr; 1180 1181 /* Sanity checks. */ 1182 if (ipproto == 0) 1183 return (EPROTONOSUPPORT); 1184 1185 /* 1186 * The protocol slot must not be occupied by another protocol 1187 * already. An index pointing to IPPROTO_RAW is unused. 1188 */ 1189 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1190 if (pr == NULL) 1191 return (EPFNOSUPPORT); 1192 if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */ 1193 return (EEXIST); 1194 1195 /* Find the protocol position in inetsw[] and set the index. */ 1196 for (pr = inetdomain.dom_protosw; 1197 pr < inetdomain.dom_protoswNPROTOSW; pr++) { 1198 if (pr->pr_domain->dom_family == PF_INET && 1199 pr->pr_protocol && pr->pr_protocol == ipproto) { 1200 /* Be careful to only index valid IP protocols. */ 1201 if (pr->pr_protocol <= IPPROTO_MAX) { 1202 ip_protox[pr->pr_protocol] = pr - inetsw; 1203 return (0); 1204 } else 1205 return (EINVAL); 1206 } 1207 } 1208 return (EPROTONOSUPPORT); 1209} 1210 1211int 1212ipproto_unregister(u_char ipproto) 1213{ 1214 struct protosw *pr; 1215 1216 /* Sanity checks. */ 1217 if (ipproto == 0) 1218 return (EPROTONOSUPPORT); 1219 1220 /* Check if the protocol was indeed registered. */ 1221 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1222 if (pr == NULL) 1223 return (EPFNOSUPPORT); 1224 if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */ 1225 return (ENOENT); 1226 1227 /* Reset the protocol slot to IPPROTO_RAW. */ 1228 ip_protox[ipproto] = pr - inetsw; 1229 return (0); 1230} 1231 1232 1233/* 1234 * Do option processing on a datagram, 1235 * possibly discarding it if bad options are encountered, 1236 * or forwarding it if source-routed. 1237 * The pass argument is used when operating in the IPSTEALTH 1238 * mode to tell what options to process: 1239 * [LS]SRR (pass 0) or the others (pass 1). 1240 * The reason for as many as two passes is that when doing IPSTEALTH, 1241 * non-routing options should be processed only if the packet is for us. 1242 * Returns 1 if packet has been forwarded/freed, 1243 * 0 if the packet should be processed further. 1244 */ 1245static int 1246ip_dooptions(struct mbuf *m, int pass) 1247{ 1248 struct ip *ip = mtod(m, struct ip *); 1249 u_char *cp; 1250 struct in_ifaddr *ia; 1251 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1252 struct in_addr *sin, dst; 1253 n_time ntime; 1254 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 1255 1256 /* ignore or reject packets with IP options */ 1257 if (ip_doopts == 0) 1258 return 0; 1259 else if (ip_doopts == 2) { 1260 type = ICMP_UNREACH; 1261 code = ICMP_UNREACH_FILTER_PROHIB; 1262 goto bad; 1263 } 1264 1265 dst = ip->ip_dst; 1266 cp = (u_char *)(ip + 1); 1267 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1268 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1269 opt = cp[IPOPT_OPTVAL]; 1270 if (opt == IPOPT_EOL) 1271 break; 1272 if (opt == IPOPT_NOP) 1273 optlen = 1; 1274 else { 1275 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1276 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1277 goto bad; 1278 } 1279 optlen = cp[IPOPT_OLEN]; 1280 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1281 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1282 goto bad; 1283 } 1284 } 1285 switch (opt) { 1286 1287 default: 1288 break; 1289 1290 /* 1291 * Source routing with record. 1292 * Find interface with current destination address. 1293 * If none on this machine then drop if strictly routed, 1294 * or do nothing if loosely routed. 1295 * Record interface address and bring up next address 1296 * component. If strictly routed make sure next 1297 * address is on directly accessible net. 1298 */ 1299 case IPOPT_LSRR: 1300 case IPOPT_SSRR: 1301#ifdef IPSTEALTH 1302 if (ipstealth && pass > 0) 1303 break; 1304#endif 1305 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1306 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1307 goto bad; 1308 } 1309 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1310 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1311 goto bad; 1312 } 1313 ipaddr.sin_addr = ip->ip_dst; 1314 ia = (struct in_ifaddr *) 1315 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1316 if (ia == NULL) { 1317 if (opt == IPOPT_SSRR) { 1318 type = ICMP_UNREACH; 1319 code = ICMP_UNREACH_SRCFAIL; 1320 goto bad; 1321 } 1322 if (!ip_dosourceroute) 1323 goto nosourcerouting; 1324 /* 1325 * Loose routing, and not at next destination 1326 * yet; nothing to do except forward. 1327 */ 1328 break; 1329 } 1330 off--; /* 0 origin */ 1331 if (off > optlen - (int)sizeof(struct in_addr)) { 1332 /* 1333 * End of source route. Should be for us. 1334 */ 1335 if (!ip_acceptsourceroute) 1336 goto nosourcerouting; 1337 save_rte(m, cp, ip->ip_src); 1338 break; 1339 } 1340#ifdef IPSTEALTH 1341 if (ipstealth) 1342 goto dropit; 1343#endif 1344 if (!ip_dosourceroute) { 1345 if (ipforwarding) { 1346 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1347 /* 1348 * Acting as a router, so generate ICMP 1349 */ 1350nosourcerouting: 1351 strcpy(buf, inet_ntoa(ip->ip_dst)); 1352 log(LOG_WARNING, 1353 "attempted source route from %s to %s\n", 1354 inet_ntoa(ip->ip_src), buf); 1355 type = ICMP_UNREACH; 1356 code = ICMP_UNREACH_SRCFAIL; 1357 goto bad; 1358 } else { 1359 /* 1360 * Not acting as a router, so silently drop. 1361 */ 1362#ifdef IPSTEALTH 1363dropit: 1364#endif 1365 ipstat.ips_cantforward++; 1366 m_freem(m); 1367 return (1); 1368 } 1369 } 1370 1371 /* 1372 * locate outgoing interface 1373 */ 1374 (void)memcpy(&ipaddr.sin_addr, cp + off, 1375 sizeof(ipaddr.sin_addr)); 1376 1377 if (opt == IPOPT_SSRR) { 1378#define INA struct in_ifaddr * 1379#define SA struct sockaddr * 1380 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL) 1381 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1382 } else 1383 ia = ip_rtaddr(ipaddr.sin_addr); 1384 if (ia == NULL) { 1385 type = ICMP_UNREACH; 1386 code = ICMP_UNREACH_SRCFAIL; 1387 goto bad; 1388 } 1389 ip->ip_dst = ipaddr.sin_addr; 1390 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1391 sizeof(struct in_addr)); 1392 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1393 /* 1394 * Let ip_intr's mcast routing check handle mcast pkts 1395 */ 1396 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1397 break; 1398 1399 case IPOPT_RR: 1400#ifdef IPSTEALTH 1401 if (ipstealth && pass == 0) 1402 break; 1403#endif 1404 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1405 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1406 goto bad; 1407 } 1408 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1409 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1410 goto bad; 1411 } 1412 /* 1413 * If no space remains, ignore. 1414 */ 1415 off--; /* 0 origin */ 1416 if (off > optlen - (int)sizeof(struct in_addr)) 1417 break; 1418 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1419 sizeof(ipaddr.sin_addr)); 1420 /* 1421 * locate outgoing interface; if we're the destination, 1422 * use the incoming interface (should be same). 1423 */ 1424 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL && 1425 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) { 1426 type = ICMP_UNREACH; 1427 code = ICMP_UNREACH_HOST; 1428 goto bad; 1429 } 1430 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1431 sizeof(struct in_addr)); 1432 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1433 break; 1434 1435 case IPOPT_TS: 1436#ifdef IPSTEALTH 1437 if (ipstealth && pass == 0) 1438 break; 1439#endif 1440 code = cp - (u_char *)ip; 1441 if (optlen < 4 || optlen > 40) { 1442 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1443 goto bad; 1444 } 1445 if ((off = cp[IPOPT_OFFSET]) < 5) { 1446 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1447 goto bad; 1448 } 1449 if (off > optlen - (int)sizeof(int32_t)) { 1450 cp[IPOPT_OFFSET + 1] += (1 << 4); 1451 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) { 1452 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1453 goto bad; 1454 } 1455 break; 1456 } 1457 off--; /* 0 origin */ 1458 sin = (struct in_addr *)(cp + off); 1459 switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 1460 1461 case IPOPT_TS_TSONLY: 1462 break; 1463 1464 case IPOPT_TS_TSANDADDR: 1465 if (off + sizeof(n_time) + 1466 sizeof(struct in_addr) > optlen) { 1467 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1468 goto bad; 1469 } 1470 ipaddr.sin_addr = dst; 1471 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1472 m->m_pkthdr.rcvif); 1473 if (ia == NULL) 1474 continue; 1475 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1476 sizeof(struct in_addr)); 1477 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1478 off += sizeof(struct in_addr); 1479 break; 1480 1481 case IPOPT_TS_PRESPEC: 1482 if (off + sizeof(n_time) + 1483 sizeof(struct in_addr) > optlen) { 1484 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1485 goto bad; 1486 } 1487 (void)memcpy(&ipaddr.sin_addr, sin, 1488 sizeof(struct in_addr)); 1489 if (ifa_ifwithaddr((SA)&ipaddr) == NULL) 1490 continue; 1491 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1492 off += sizeof(struct in_addr); 1493 break; 1494 1495 default: 1496 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 1497 goto bad; 1498 } 1499 ntime = iptime(); 1500 (void)memcpy(cp + off, &ntime, sizeof(n_time)); 1501 cp[IPOPT_OFFSET] += sizeof(n_time); 1502 } 1503 } 1504 if (forward && ipforwarding) { 1505 ip_forward(m, 1); 1506 return (1); 1507 } 1508 return (0); 1509bad: 1510 icmp_error(m, type, code, 0, 0); 1511 ipstat.ips_badoptions++; 1512 return (1); 1513} 1514 1515/* 1516 * Given address of next destination (final or next hop), 1517 * return internet address info of interface to be used to get there. 1518 */ 1519struct in_ifaddr * 1520ip_rtaddr(dst) 1521 struct in_addr dst; 1522{ 1523 struct route sro; 1524 struct sockaddr_in *sin; 1525 struct in_ifaddr *ifa; 1526 1527 bzero(&sro, sizeof(sro)); 1528 sin = (struct sockaddr_in *)&sro.ro_dst; 1529 sin->sin_family = AF_INET; 1530 sin->sin_len = sizeof(*sin); 1531 sin->sin_addr = dst; 1532 rtalloc_ign(&sro, RTF_CLONING); 1533 1534 if (sro.ro_rt == NULL) 1535 return (NULL); 1536 1537 ifa = ifatoia(sro.ro_rt->rt_ifa); 1538 RTFREE(sro.ro_rt); 1539 return (ifa); 1540} 1541 1542/* 1543 * Save incoming source route for use in replies, 1544 * to be picked up later by ip_srcroute if the receiver is interested. 1545 */ 1546static void 1547save_rte(m, option, dst) 1548 struct mbuf *m; 1549 u_char *option; 1550 struct in_addr dst; 1551{ 1552 unsigned olen; 1553 struct ipopt_tag *opts; 1554 1555 opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS, 1556 sizeof(struct ipopt_tag), M_NOWAIT); 1557 if (opts == NULL) 1558 return; 1559 1560 olen = option[IPOPT_OLEN]; 1561#ifdef DIAGNOSTIC 1562 if (ipprintfs) 1563 printf("save_rte: olen %d\n", olen); 1564#endif 1565 if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) 1566 return; 1567 bcopy(option, opts->ip_srcrt.srcopt, olen); 1568 opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1569 opts->ip_srcrt.dst = dst; 1570 m_tag_prepend(m, (struct m_tag *)opts); 1571} 1572 1573/* 1574 * Retrieve incoming source route for use in replies, 1575 * in the same form used by setsockopt. 1576 * The first hop is placed before the options, will be removed later. 1577 */ 1578struct mbuf * 1579ip_srcroute(m0) 1580 struct mbuf *m0; 1581{ 1582 register struct in_addr *p, *q; 1583 register struct mbuf *m; 1584 struct ipopt_tag *opts; 1585 1586 opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL); 1587 if (opts == NULL) 1588 return (NULL); 1589 1590 if (opts->ip_nhops == 0) 1591 return (NULL); 1592 m = m_get(M_DONTWAIT, MT_HEADER); 1593 if (m == NULL) 1594 return (NULL); 1595 1596#define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt)) 1597 1598 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1599 m->m_len = opts->ip_nhops * sizeof(struct in_addr) + 1600 sizeof(struct in_addr) + OPTSIZ; 1601#ifdef DIAGNOSTIC 1602 if (ipprintfs) 1603 printf("ip_srcroute: nhops %d mlen %d", opts->ip_nhops, m->m_len); 1604#endif 1605 1606 /* 1607 * First save first hop for return route 1608 */ 1609 p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]); 1610 *(mtod(m, struct in_addr *)) = *p--; 1611#ifdef DIAGNOSTIC 1612 if (ipprintfs) 1613 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1614#endif 1615 1616 /* 1617 * Copy option fields and padding (nop) to mbuf. 1618 */ 1619 opts->ip_srcrt.nop = IPOPT_NOP; 1620 opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1621 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1622 &(opts->ip_srcrt.nop), OPTSIZ); 1623 q = (struct in_addr *)(mtod(m, caddr_t) + 1624 sizeof(struct in_addr) + OPTSIZ); 1625#undef OPTSIZ 1626 /* 1627 * Record return path as an IP source route, 1628 * reversing the path (pointers are now aligned). 1629 */ 1630 while (p >= opts->ip_srcrt.route) { 1631#ifdef DIAGNOSTIC 1632 if (ipprintfs) 1633 printf(" %lx", (u_long)ntohl(q->s_addr)); 1634#endif 1635 *q++ = *p--; 1636 } 1637 /* 1638 * Last hop goes to final destination. 1639 */ 1640 *q = opts->ip_srcrt.dst; 1641#ifdef DIAGNOSTIC 1642 if (ipprintfs) 1643 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1644#endif 1645 m_tag_delete(m0, (struct m_tag *)opts); 1646 return (m); 1647} 1648 1649/* 1650 * Strip out IP options, at higher 1651 * level protocol in the kernel. 1652 * Second argument is buffer to which options 1653 * will be moved, and return value is their length. 1654 * XXX should be deleted; last arg currently ignored. 1655 */ 1656void 1657ip_stripoptions(m, mopt) 1658 register struct mbuf *m; 1659 struct mbuf *mopt; 1660{ 1661 register int i; 1662 struct ip *ip = mtod(m, struct ip *); 1663 register caddr_t opts; 1664 int olen; 1665 1666 olen = (ip->ip_hl << 2) - sizeof (struct ip); 1667 opts = (caddr_t)(ip + 1); 1668 i = m->m_len - (sizeof (struct ip) + olen); 1669 bcopy(opts + olen, opts, (unsigned)i); 1670 m->m_len -= olen; 1671 if (m->m_flags & M_PKTHDR) 1672 m->m_pkthdr.len -= olen; 1673 ip->ip_v = IPVERSION; 1674 ip->ip_hl = sizeof(struct ip) >> 2; 1675} 1676 1677u_char inetctlerrmap[PRC_NCMDS] = { 1678 0, 0, 0, 0, 1679 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1680 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1681 EMSGSIZE, EHOSTUNREACH, 0, 0, 1682 0, 0, EHOSTUNREACH, 0, 1683 ENOPROTOOPT, ECONNREFUSED 1684}; 1685 1686/* 1687 * Forward a packet. If some error occurs return the sender 1688 * an icmp packet. Note we can't always generate a meaningful 1689 * icmp message because icmp doesn't have a large enough repertoire 1690 * of codes and types. 1691 * 1692 * If not forwarding, just drop the packet. This could be confusing 1693 * if ipforwarding was zero but some routing protocol was advancing 1694 * us as a gateway to somewhere. However, we must let the routing 1695 * protocol deal with that. 1696 * 1697 * The srcrt parameter indicates whether the packet is being forwarded 1698 * via a source route. 1699 */ 1700void 1701ip_forward(struct mbuf *m, int srcrt) 1702{ 1703 struct ip *ip = mtod(m, struct ip *); 1704 struct in_ifaddr *ia = NULL; 1705 int error, type = 0, code = 0; 1706 struct mbuf *mcopy; 1707 struct in_addr dest; 1708 struct ifnet *destifp, dummyifp; 1709 1710#ifdef DIAGNOSTIC 1711 if (ipprintfs) 1712 printf("forward: src %lx dst %lx ttl %x\n", 1713 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr, 1714 ip->ip_ttl); 1715#endif 1716 1717 1718 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1719 ipstat.ips_cantforward++; 1720 m_freem(m); 1721 return; 1722 } 1723#ifdef IPSTEALTH 1724 if (!ipstealth) { 1725#endif 1726 if (ip->ip_ttl <= IPTTLDEC) { 1727 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1728 0, 0); 1729 return; 1730 } 1731#ifdef IPSTEALTH 1732 } 1733#endif 1734 1735 if (!srcrt && (ia = ip_rtaddr(ip->ip_dst)) == NULL) { 1736 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1737 return; 1738 } 1739 1740 /* 1741 * Save the IP header and at most 8 bytes of the payload, 1742 * in case we need to generate an ICMP message to the src. 1743 * 1744 * XXX this can be optimized a lot by saving the data in a local 1745 * buffer on the stack (72 bytes at most), and only allocating the 1746 * mbuf if really necessary. The vast majority of the packets 1747 * are forwarded without having to send an ICMP back (either 1748 * because unnecessary, or because rate limited), so we are 1749 * really we are wasting a lot of work here. 1750 * 1751 * We don't use m_copy() because it might return a reference 1752 * to a shared cluster. Both this function and ip_output() 1753 * assume exclusive access to the IP header in `m', so any 1754 * data in a cluster may change before we reach icmp_error(). 1755 */ 1756 MGET(mcopy, M_DONTWAIT, m->m_type); 1757 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) { 1758 /* 1759 * It's probably ok if the pkthdr dup fails (because 1760 * the deep copy of the tag chain failed), but for now 1761 * be conservative and just discard the copy since 1762 * code below may some day want the tags. 1763 */ 1764 m_free(mcopy); 1765 mcopy = NULL; 1766 } 1767 if (mcopy != NULL) { 1768 mcopy->m_len = imin((ip->ip_hl << 2) + 8, 1769 (int)ip->ip_len); 1770 mcopy->m_pkthdr.len = mcopy->m_len; 1771 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1772 } 1773 1774#ifdef IPSTEALTH 1775 if (!ipstealth) { 1776#endif 1777 ip->ip_ttl -= IPTTLDEC; 1778#ifdef IPSTEALTH 1779 } 1780#endif 1781 1782 /* 1783 * If forwarding packet using same interface that it came in on, 1784 * perhaps should send a redirect to sender to shortcut a hop. 1785 * Only send redirect if source is sending directly to us, 1786 * and if packet was not source routed (or has any options). 1787 * Also, don't send redirect if forwarding using a default route 1788 * or a route modified by a redirect. 1789 */ 1790 dest.s_addr = 0; 1791 if (!srcrt && ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) { 1792 struct sockaddr_in *sin; 1793 struct route ro; 1794 struct rtentry *rt; 1795 1796 bzero(&ro, sizeof(ro)); 1797 sin = (struct sockaddr_in *)&ro.ro_dst; 1798 sin->sin_family = AF_INET; 1799 sin->sin_len = sizeof(*sin); 1800 sin->sin_addr = ip->ip_dst; 1801 rtalloc_ign(&ro, RTF_CLONING); 1802 1803 rt = ro.ro_rt; 1804 1805 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1806 satosin(rt_key(rt))->sin_addr.s_addr != 0) { 1807#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1808 u_long src = ntohl(ip->ip_src.s_addr); 1809 1810 if (RTA(rt) && 1811 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1812 if (rt->rt_flags & RTF_GATEWAY) 1813 dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr; 1814 else 1815 dest.s_addr = ip->ip_dst.s_addr; 1816 /* Router requirements says to only send host redirects */ 1817 type = ICMP_REDIRECT; 1818 code = ICMP_REDIRECT_HOST; 1819#ifdef DIAGNOSTIC 1820 if (ipprintfs) 1821 printf("redirect (%d) to %lx\n", code, (u_long)dest.s_addr); 1822#endif 1823 } 1824 } 1825 if (rt) 1826 RTFREE(rt); 1827 } 1828 1829 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL); 1830 if (error) 1831 ipstat.ips_cantforward++; 1832 else { 1833 ipstat.ips_forward++; 1834 if (type) 1835 ipstat.ips_redirectsent++; 1836 else { 1837 if (mcopy) 1838 m_freem(mcopy); 1839 return; 1840 } 1841 } 1842 if (mcopy == NULL) 1843 return; 1844 destifp = NULL; 1845 1846 switch (error) { 1847 1848 case 0: /* forwarded, but need redirect */ 1849 /* type, code set above */ 1850 break; 1851 1852 case ENETUNREACH: /* shouldn't happen, checked above */ 1853 case EHOSTUNREACH: 1854 case ENETDOWN: 1855 case EHOSTDOWN: 1856 default: 1857 type = ICMP_UNREACH; 1858 code = ICMP_UNREACH_HOST; 1859 break; 1860 1861 case EMSGSIZE: 1862 type = ICMP_UNREACH; 1863 code = ICMP_UNREACH_NEEDFRAG; 1864#if defined(IPSEC) || defined(FAST_IPSEC) 1865 /* 1866 * If the packet is routed over IPsec tunnel, tell the 1867 * originator the tunnel MTU. 1868 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1869 * XXX quickhack!!! 1870 */ 1871 { 1872 struct secpolicy *sp = NULL; 1873 int ipsecerror; 1874 int ipsechdr; 1875 struct route *ro; 1876 1877#ifdef IPSEC 1878 sp = ipsec4_getpolicybyaddr(mcopy, 1879 IPSEC_DIR_OUTBOUND, 1880 IP_FORWARDING, 1881 &ipsecerror); 1882#else /* FAST_IPSEC */ 1883 sp = ipsec_getpolicybyaddr(mcopy, 1884 IPSEC_DIR_OUTBOUND, 1885 IP_FORWARDING, 1886 &ipsecerror); 1887#endif 1888 if (sp != NULL) { 1889 /* count IPsec header size */ 1890 ipsechdr = ipsec4_hdrsiz(mcopy, 1891 IPSEC_DIR_OUTBOUND, 1892 NULL); 1893 1894 /* 1895 * find the correct route for outer IPv4 1896 * header, compute tunnel MTU. 1897 * 1898 * XXX BUG ALERT 1899 * The "dummyifp" code relies upon the fact 1900 * that icmp_error() touches only ifp->if_mtu. 1901 */ 1902 /*XXX*/ 1903 destifp = NULL; 1904 if (sp->req != NULL 1905 && sp->req->sav != NULL 1906 && sp->req->sav->sah != NULL) { 1907 ro = &sp->req->sav->sah->sa_route; 1908 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1909 dummyifp.if_mtu = 1910 ro->ro_rt->rt_rmx.rmx_mtu ? 1911 ro->ro_rt->rt_rmx.rmx_mtu : 1912 ro->ro_rt->rt_ifp->if_mtu; 1913 dummyifp.if_mtu -= ipsechdr; 1914 destifp = &dummyifp; 1915 } 1916 } 1917 1918#ifdef IPSEC 1919 key_freesp(sp); 1920#else /* FAST_IPSEC */ 1921 KEY_FREESP(&sp); 1922#endif 1923 ipstat.ips_cantfrag++; 1924 break; 1925 } else 1926#endif /*IPSEC || FAST_IPSEC*/ 1927 /* 1928 * When doing source routing 'ia' can be NULL. Fall back 1929 * to the minimum guaranteed routeable packet size and use 1930 * the same hack as IPSEC to setup a dummyifp for icmp. 1931 */ 1932 if (ia == NULL) { 1933 dummyifp.if_mtu = IP_MSS; 1934 destifp = &dummyifp; 1935 } else 1936 destifp = ia->ia_ifp; 1937#if defined(IPSEC) || defined(FAST_IPSEC) 1938 } 1939#endif /*IPSEC || FAST_IPSEC*/ 1940 ipstat.ips_cantfrag++; 1941 break; 1942 1943 case ENOBUFS: 1944 /* 1945 * A router should not generate ICMP_SOURCEQUENCH as 1946 * required in RFC1812 Requirements for IP Version 4 Routers. 1947 * Source quench could be a big problem under DoS attacks, 1948 * or if the underlying interface is rate-limited. 1949 * Those who need source quench packets may re-enable them 1950 * via the net.inet.ip.sendsourcequench sysctl. 1951 */ 1952 if (ip_sendsourcequench == 0) { 1953 m_freem(mcopy); 1954 return; 1955 } else { 1956 type = ICMP_SOURCEQUENCH; 1957 code = 0; 1958 } 1959 break; 1960 1961 case EACCES: /* ipfw denied packet */ 1962 m_freem(mcopy); 1963 return; 1964 } 1965 icmp_error(mcopy, type, code, dest.s_addr, destifp); 1966} 1967 1968void 1969ip_savecontrol(inp, mp, ip, m) 1970 register struct inpcb *inp; 1971 register struct mbuf **mp; 1972 register struct ip *ip; 1973 register struct mbuf *m; 1974{ 1975 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 1976 struct bintime bt; 1977 1978 bintime(&bt); 1979 if (inp->inp_socket->so_options & SO_BINTIME) { 1980 *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt), 1981 SCM_BINTIME, SOL_SOCKET); 1982 if (*mp) 1983 mp = &(*mp)->m_next; 1984 } 1985 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1986 struct timeval tv; 1987 1988 bintime2timeval(&bt, &tv); 1989 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1990 SCM_TIMESTAMP, SOL_SOCKET); 1991 if (*mp) 1992 mp = &(*mp)->m_next; 1993 } 1994 } 1995 if (inp->inp_flags & INP_RECVDSTADDR) { 1996 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1997 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1998 if (*mp) 1999 mp = &(*mp)->m_next; 2000 } 2001 if (inp->inp_flags & INP_RECVTTL) { 2002 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, 2003 sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 2004 if (*mp) 2005 mp = &(*mp)->m_next; 2006 } 2007#ifdef notyet 2008 /* XXX 2009 * Moving these out of udp_input() made them even more broken 2010 * than they already were. 2011 */ 2012 /* options were tossed already */ 2013 if (inp->inp_flags & INP_RECVOPTS) { 2014 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 2015 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 2016 if (*mp) 2017 mp = &(*mp)->m_next; 2018 } 2019 /* ip_srcroute doesn't do what we want here, need to fix */ 2020 if (inp->inp_flags & INP_RECVRETOPTS) { 2021 *mp = sbcreatecontrol((caddr_t) ip_srcroute(m), 2022 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 2023 if (*mp) 2024 mp = &(*mp)->m_next; 2025 } 2026#endif 2027 if (inp->inp_flags & INP_RECVIF) { 2028 struct ifnet *ifp; 2029 struct sdlbuf { 2030 struct sockaddr_dl sdl; 2031 u_char pad[32]; 2032 } sdlbuf; 2033 struct sockaddr_dl *sdp; 2034 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 2035 2036 if (((ifp = m->m_pkthdr.rcvif)) 2037 && ( ifp->if_index && (ifp->if_index <= if_index))) { 2038 sdp = (struct sockaddr_dl *) 2039 (ifaddr_byindex(ifp->if_index)->ifa_addr); 2040 /* 2041 * Change our mind and don't try copy. 2042 */ 2043 if ((sdp->sdl_family != AF_LINK) 2044 || (sdp->sdl_len > sizeof(sdlbuf))) { 2045 goto makedummy; 2046 } 2047 bcopy(sdp, sdl2, sdp->sdl_len); 2048 } else { 2049makedummy: 2050 sdl2->sdl_len 2051 = offsetof(struct sockaddr_dl, sdl_data[0]); 2052 sdl2->sdl_family = AF_LINK; 2053 sdl2->sdl_index = 0; 2054 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 2055 } 2056 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 2057 IP_RECVIF, IPPROTO_IP); 2058 if (*mp) 2059 mp = &(*mp)->m_next; 2060 } 2061} 2062 2063/* 2064 * XXX these routines are called from the upper part of the kernel. 2065 * They need to be locked when we remove Giant. 2066 * 2067 * They could also be moved to ip_mroute.c, since all the RSVP 2068 * handling is done there already. 2069 */ 2070static int ip_rsvp_on; 2071struct socket *ip_rsvpd; 2072int 2073ip_rsvp_init(struct socket *so) 2074{ 2075 if (so->so_type != SOCK_RAW || 2076 so->so_proto->pr_protocol != IPPROTO_RSVP) 2077 return EOPNOTSUPP; 2078 2079 if (ip_rsvpd != NULL) 2080 return EADDRINUSE; 2081 2082 ip_rsvpd = so; 2083 /* 2084 * This may seem silly, but we need to be sure we don't over-increment 2085 * the RSVP counter, in case something slips up. 2086 */ 2087 if (!ip_rsvp_on) { 2088 ip_rsvp_on = 1; 2089 rsvp_on++; 2090 } 2091 2092 return 0; 2093} 2094 2095int 2096ip_rsvp_done(void) 2097{ 2098 ip_rsvpd = NULL; 2099 /* 2100 * This may seem silly, but we need to be sure we don't over-decrement 2101 * the RSVP counter, in case something slips up. 2102 */ 2103 if (ip_rsvp_on) { 2104 ip_rsvp_on = 0; 2105 rsvp_on--; 2106 } 2107 return 0; 2108} 2109 2110void 2111rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */ 2112{ 2113 if (rsvp_input_p) { /* call the real one if loaded */ 2114 rsvp_input_p(m, off); 2115 return; 2116 } 2117 2118 /* Can still get packets with rsvp_on = 0 if there is a local member 2119 * of the group to which the RSVP packet is addressed. But in this 2120 * case we want to throw the packet away. 2121 */ 2122 2123 if (!rsvp_on) { 2124 m_freem(m); 2125 return; 2126 } 2127 2128 if (ip_rsvpd != NULL) { 2129 rip_input(m, off); 2130 return; 2131 } 2132 /* Drop the packet */ 2133 m_freem(m); 2134} 2135