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