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 NET_LOCK_GIANT(); 940 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 941 NET_UNLOCK_GIANT(); 942 return; 943 bad: 944 m_freem(m); 945 } 946 947 /* 948 * Take incoming datagram fragment and try to reassemble it into 949 * whole datagram. If a chain for reassembly of this datagram already 950 * exists, then it is given as fp; otherwise have to make a chain. 951 * 952 * When IPDIVERT enabled, keep additional state with each packet that 953 * tells us if we need to divert or tee the packet we're building. 954 * In particular, *divinfo includes the port and TEE flag, 955 * *divert_rule is the number of the matching rule. 956 */ 957 958 static struct mbuf * 959 ip_reass(struct mbuf *m, struct ipqhead *head, struct ipq *fp) 960 { 961 struct ip *ip = mtod(m, struct ip *); 962 register struct mbuf *p, *q, *nq; 963 struct mbuf *t; 964 int hlen = ip->ip_hl << 2; 965 int i, next; 966 u_int8_t ecn, ecn0; 967 968 IPQ_LOCK_ASSERT(); 969 970 /* 971 * Presence of header sizes in mbufs 972 * would confuse code below. 973 */ 974 m->m_data += hlen; 975 m->m_len -= hlen; 976 977 /* 978 * If first fragment to arrive, create a reassembly queue. 979 */ 980 if (fp == NULL) { 981 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 982 goto dropfrag; 983 fp = mtod(t, struct ipq *); 984 #ifdef MAC 985 if (mac_init_ipq(fp, M_NOWAIT) != 0) { 986 m_free(t); 987 goto dropfrag; 988 } 989 mac_create_ipq(m, fp); 990 #endif 991 TAILQ_INSERT_HEAD(head, fp, ipq_list); 992 nipq++; 993 fp->ipq_nfrags = 1; 994 fp->ipq_ttl = IPFRAGTTL; 995 fp->ipq_p = ip->ip_p; 996 fp->ipq_id = ip->ip_id; 997 fp->ipq_src = ip->ip_src; 998 fp->ipq_dst = ip->ip_dst; 999 fp->ipq_frags = m; 1000 m->m_nextpkt = NULL; 1001 goto inserted; 1002 } else { 1003 fp->ipq_nfrags++; 1004 #ifdef MAC 1005 mac_update_ipq(m, fp); 1006 #endif 1007 } 1008 1009 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 1010 1011 /* 1012 * Handle ECN by comparing this segment with the first one; 1013 * if CE is set, do not lose CE. 1014 * drop if CE and not-ECT are mixed for the same packet. 1015 */ 1016 ecn = ip->ip_tos & IPTOS_ECN_MASK; 1017 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 1018 if (ecn == IPTOS_ECN_CE) { 1019 if (ecn0 == IPTOS_ECN_NOTECT) 1020 goto dropfrag; 1021 if (ecn0 != IPTOS_ECN_CE) 1022 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 1023 } 1024 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 1025 goto dropfrag; 1026 1027 /* 1028 * Find a segment which begins after this one does. 1029 */ 1030 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 1031 if (GETIP(q)->ip_off > ip->ip_off) 1032 break; 1033 1034 /* 1035 * If there is a preceding segment, it may provide some of 1036 * our data already. If so, drop the data from the incoming 1037 * segment. If it provides all of our data, drop us, otherwise 1038 * stick new segment in the proper place. 1039 * 1040 * If some of the data is dropped from the the preceding 1041 * segment, then it's checksum is invalidated. 1042 */ 1043 if (p) { 1044 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 1045 if (i > 0) { 1046 if (i >= ip->ip_len) 1047 goto dropfrag; 1048 m_adj(m, i); 1049 m->m_pkthdr.csum_flags = 0; 1050 ip->ip_off += i; 1051 ip->ip_len -= i; 1052 } 1053 m->m_nextpkt = p->m_nextpkt; 1054 p->m_nextpkt = m; 1055 } else { 1056 m->m_nextpkt = fp->ipq_frags; 1057 fp->ipq_frags = m; 1058 } 1059 1060 /* 1061 * While we overlap succeeding segments trim them or, 1062 * if they are completely covered, dequeue them. 1063 */ 1064 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 1065 q = nq) { 1066 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off; 1067 if (i < GETIP(q)->ip_len) { 1068 GETIP(q)->ip_len -= i; 1069 GETIP(q)->ip_off += i; 1070 m_adj(q, i); 1071 q->m_pkthdr.csum_flags = 0; 1072 break; 1073 } 1074 nq = q->m_nextpkt; 1075 m->m_nextpkt = nq; 1076 ipstat.ips_fragdropped++; 1077 fp->ipq_nfrags--; 1078 m_freem(q); 1079 } 1080 1081 inserted: 1082 1083 #ifdef IPDIVERT 1084 if (ip->ip_off != 0) { 1085 /* 1086 * Strip any divert information; only the info 1087 * on the first fragment is used/kept. 1088 */ 1089 struct m_tag *mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 1090 if (mtag) 1091 m_tag_delete(m, mtag); 1092 } 1093 #endif 1094 1095 /* 1096 * Check for complete reassembly and perform frag per packet 1097 * limiting. 1098 * 1099 * Frag limiting is performed here so that the nth frag has 1100 * a chance to complete the packet before we drop the packet. 1101 * As a result, n+1 frags are actually allowed per packet, but 1102 * only n will ever be stored. (n = maxfragsperpacket.) 1103 * 1104 */ 1105 next = 0; 1106 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1107 if (GETIP(q)->ip_off != next) { 1108 if (fp->ipq_nfrags > maxfragsperpacket) { 1109 ipstat.ips_fragdropped += fp->ipq_nfrags; 1110 ip_freef(head, fp); 1111 } 1112 return (0); 1113 } 1114 next += GETIP(q)->ip_len; 1115 } 1116 /* Make sure the last packet didn't have the IP_MF flag */ 1117 if (p->m_flags & M_FRAG) { 1118 if (fp->ipq_nfrags > maxfragsperpacket) { 1119 ipstat.ips_fragdropped += fp->ipq_nfrags; 1120 ip_freef(head, fp); 1121 } 1122 return (0); 1123 } 1124 1125 /* 1126 * Reassembly is complete. Make sure the packet is a sane size. 1127 */ 1128 q = fp->ipq_frags; 1129 ip = GETIP(q); 1130 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 1131 ipstat.ips_toolong++; 1132 ipstat.ips_fragdropped += fp->ipq_nfrags; 1133 ip_freef(head, fp); 1134 return (0); 1135 } 1136 1137 /* 1138 * Concatenate fragments. 1139 */ 1140 m = q; 1141 t = m->m_next; 1142 m->m_next = 0; 1143 m_cat(m, t); 1144 nq = q->m_nextpkt; 1145 q->m_nextpkt = 0; 1146 for (q = nq; q != NULL; q = nq) { 1147 nq = q->m_nextpkt; 1148 q->m_nextpkt = NULL; 1149 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1150 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1151 m_cat(m, q); 1152 } 1153 #ifdef MAC 1154 mac_create_datagram_from_ipq(fp, m); 1155 mac_destroy_ipq(fp); 1156 #endif 1157 1158 /* 1159 * Create header for new ip packet by 1160 * modifying header of first packet; 1161 * dequeue and discard fragment reassembly header. 1162 * Make header visible. 1163 */ 1164 ip->ip_len = next; 1165 ip->ip_src = fp->ipq_src; 1166 ip->ip_dst = fp->ipq_dst; 1167 TAILQ_REMOVE(head, fp, ipq_list); 1168 nipq--; 1169 (void) m_free(dtom(fp)); 1170 m->m_len += (ip->ip_hl << 2); 1171 m->m_data -= (ip->ip_hl << 2); 1172 /* some debugging cruft by sklower, below, will go away soon */ 1173 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 1174 m_fixhdr(m); 1175 return (m); 1176 1177 dropfrag: 1178 ipstat.ips_fragdropped++; 1179 if (fp != NULL) 1180 fp->ipq_nfrags--; 1181 m_freem(m); 1182 return (0); 1183 1184 #undef GETIP 1185 } 1186 1187 /* 1188 * Free a fragment reassembly header and all 1189 * associated datagrams. 1190 */ 1191 static void 1192 ip_freef(fhp, fp) 1193 struct ipqhead *fhp; 1194 struct ipq *fp; 1195 { 1196 register struct mbuf *q; 1197 1198 IPQ_LOCK_ASSERT(); 1199 1200 while (fp->ipq_frags) { 1201 q = fp->ipq_frags; 1202 fp->ipq_frags = q->m_nextpkt; 1203 m_freem(q); 1204 } 1205 TAILQ_REMOVE(fhp, fp, ipq_list); 1206 (void) m_free(dtom(fp)); 1207 nipq--; 1208 } 1209 1210 /* 1211 * IP timer processing; 1212 * if a timer expires on a reassembly 1213 * queue, discard it. 1214 */ 1215 void 1216 ip_slowtimo() 1217 { 1218 register struct ipq *fp; 1219 int s = splnet(); 1220 int i; 1221 1222 IPQ_LOCK(); 1223 for (i = 0; i < IPREASS_NHASH; i++) { 1224 for(fp = TAILQ_FIRST(&ipq[i]); fp;) { 1225 struct ipq *fpp; 1226 1227 fpp = fp; 1228 fp = TAILQ_NEXT(fp, ipq_list); 1229 if(--fpp->ipq_ttl == 0) { 1230 ipstat.ips_fragtimeout += fpp->ipq_nfrags; 1231 ip_freef(&ipq[i], fpp); 1232 } 1233 } 1234 } 1235 /* 1236 * If we are over the maximum number of fragments 1237 * (due to the limit being lowered), drain off 1238 * enough to get down to the new limit. 1239 */ 1240 if (maxnipq >= 0 && nipq > maxnipq) { 1241 for (i = 0; i < IPREASS_NHASH; i++) { 1242 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) { 1243 ipstat.ips_fragdropped += 1244 TAILQ_FIRST(&ipq[i])->ipq_nfrags; 1245 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i])); 1246 } 1247 } 1248 } 1249 IPQ_UNLOCK(); 1250 splx(s); 1251 } 1252 1253 /* 1254 * Drain off all datagram fragments. 1255 */ 1256 void 1257 ip_drain() 1258 { 1259 int i; 1260 1261 IPQ_LOCK(); 1262 for (i = 0; i < IPREASS_NHASH; i++) { 1263 while(!TAILQ_EMPTY(&ipq[i])) { 1264 ipstat.ips_fragdropped += 1265 TAILQ_FIRST(&ipq[i])->ipq_nfrags; 1266 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i])); 1267 } 1268 } 1269 IPQ_UNLOCK(); 1270 in_rtqdrain(); 1271 } 1272 1273 /* 1274 * Do option processing on a datagram, 1275 * possibly discarding it if bad options are encountered, 1276 * or forwarding it if source-routed. 1277 * The pass argument is used when operating in the IPSTEALTH 1278 * mode to tell what options to process: 1279 * [LS]SRR (pass 0) or the others (pass 1). 1280 * The reason for as many as two passes is that when doing IPSTEALTH, 1281 * non-routing options should be processed only if the packet is for us. 1282 * Returns 1 if packet has been forwarded/freed, 1283 * 0 if the packet should be processed further. 1284 */ 1285 static int 1286 ip_dooptions(struct mbuf *m, int pass, struct sockaddr_in *next_hop) 1287 { 1288 struct ip *ip = mtod(m, struct ip *); 1289 u_char *cp; 1290 struct in_ifaddr *ia; 1291 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1292 struct in_addr *sin, dst; 1293 n_time ntime; 1294 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 1295 1296 dst = ip->ip_dst; 1297 cp = (u_char *)(ip + 1); 1298 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1299 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1300 opt = cp[IPOPT_OPTVAL]; 1301 if (opt == IPOPT_EOL) 1302 break; 1303 if (opt == IPOPT_NOP) 1304 optlen = 1; 1305 else { 1306 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1307 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1308 goto bad; 1309 } 1310 optlen = cp[IPOPT_OLEN]; 1311 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1312 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1313 goto bad; 1314 } 1315 } 1316 switch (opt) { 1317 1318 default: 1319 break; 1320 1321 /* 1322 * Source routing with record. 1323 * Find interface with current destination address. 1324 * If none on this machine then drop if strictly routed, 1325 * or do nothing if loosely routed. 1326 * Record interface address and bring up next address 1327 * component. If strictly routed make sure next 1328 * address is on directly accessible net. 1329 */ 1330 case IPOPT_LSRR: 1331 case IPOPT_SSRR: 1332 #ifdef IPSTEALTH 1333 if (ipstealth && pass > 0) 1334 break; 1335 #endif 1336 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1337 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1338 goto bad; 1339 } 1340 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1341 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1342 goto bad; 1343 } 1344 ipaddr.sin_addr = ip->ip_dst; 1345 ia = (struct in_ifaddr *) 1346 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1347 if (ia == 0) { 1348 if (opt == IPOPT_SSRR) { 1349 type = ICMP_UNREACH; 1350 code = ICMP_UNREACH_SRCFAIL; 1351 goto bad; 1352 } 1353 if (!ip_dosourceroute) 1354 goto nosourcerouting; 1355 /* 1356 * Loose routing, and not at next destination 1357 * yet; nothing to do except forward. 1358 */ 1359 break; 1360 } 1361 off--; /* 0 origin */ 1362 if (off > optlen - (int)sizeof(struct in_addr)) { 1363 /* 1364 * End of source route. Should be for us. 1365 */ 1366 if (!ip_acceptsourceroute) 1367 goto nosourcerouting; 1368 save_rte(cp, ip->ip_src); 1369 break; 1370 } 1371 #ifdef IPSTEALTH 1372 if (ipstealth) 1373 goto dropit; 1374 #endif 1375 if (!ip_dosourceroute) { 1376 if (ipforwarding) { 1377 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1378 /* 1379 * Acting as a router, so generate ICMP 1380 */ 1381 nosourcerouting: 1382 strcpy(buf, inet_ntoa(ip->ip_dst)); 1383 log(LOG_WARNING, 1384 "attempted source route from %s to %s\n", 1385 inet_ntoa(ip->ip_src), buf); 1386 type = ICMP_UNREACH; 1387 code = ICMP_UNREACH_SRCFAIL; 1388 goto bad; 1389 } else { 1390 /* 1391 * Not acting as a router, so silently drop. 1392 */ 1393 #ifdef IPSTEALTH 1394 dropit: 1395 #endif 1396 ipstat.ips_cantforward++; 1397 m_freem(m); 1398 return (1); 1399 } 1400 } 1401 1402 /* 1403 * locate outgoing interface 1404 */ 1405 (void)memcpy(&ipaddr.sin_addr, cp + off, 1406 sizeof(ipaddr.sin_addr)); 1407 1408 if (opt == IPOPT_SSRR) { 1409 #define INA struct in_ifaddr * 1410 #define SA struct sockaddr * 1411 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 1412 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1413 } else 1414 ia = ip_rtaddr(ipaddr.sin_addr); 1415 if (ia == 0) { 1416 type = ICMP_UNREACH; 1417 code = ICMP_UNREACH_SRCFAIL; 1418 goto bad; 1419 } 1420 ip->ip_dst = ipaddr.sin_addr; 1421 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1422 sizeof(struct in_addr)); 1423 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1424 /* 1425 * Let ip_intr's mcast routing check handle mcast pkts 1426 */ 1427 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1428 break; 1429 1430 case IPOPT_RR: 1431 #ifdef IPSTEALTH 1432 if (ipstealth && pass == 0) 1433 break; 1434 #endif 1435 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1436 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1437 goto bad; 1438 } 1439 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1440 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1441 goto bad; 1442 } 1443 /* 1444 * If no space remains, ignore. 1445 */ 1446 off--; /* 0 origin */ 1447 if (off > optlen - (int)sizeof(struct in_addr)) 1448 break; 1449 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1450 sizeof(ipaddr.sin_addr)); 1451 /* 1452 * locate outgoing interface; if we're the destination, 1453 * use the incoming interface (should be same). 1454 */ 1455 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1456 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1457 type = ICMP_UNREACH; 1458 code = ICMP_UNREACH_HOST; 1459 goto bad; 1460 } 1461 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1462 sizeof(struct in_addr)); 1463 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1464 break; 1465 1466 case IPOPT_TS: 1467 #ifdef IPSTEALTH 1468 if (ipstealth && pass == 0) 1469 break; 1470 #endif 1471 code = cp - (u_char *)ip; 1472 if (optlen < 4 || optlen > 40) { 1473 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1474 goto bad; 1475 } 1476 if ((off = cp[IPOPT_OFFSET]) < 5) { 1477 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1478 goto bad; 1479 } 1480 if (off > optlen - (int)sizeof(int32_t)) { 1481 cp[IPOPT_OFFSET + 1] += (1 << 4); 1482 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) { 1483 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1484 goto bad; 1485 } 1486 break; 1487 } 1488 off--; /* 0 origin */ 1489 sin = (struct in_addr *)(cp + off); 1490 switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 1491 1492 case IPOPT_TS_TSONLY: 1493 break; 1494 1495 case IPOPT_TS_TSANDADDR: 1496 if (off + sizeof(n_time) + 1497 sizeof(struct in_addr) > optlen) { 1498 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1499 goto bad; 1500 } 1501 ipaddr.sin_addr = dst; 1502 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1503 m->m_pkthdr.rcvif); 1504 if (ia == 0) 1505 continue; 1506 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1507 sizeof(struct in_addr)); 1508 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1509 off += sizeof(struct in_addr); 1510 break; 1511 1512 case IPOPT_TS_PRESPEC: 1513 if (off + sizeof(n_time) + 1514 sizeof(struct in_addr) > optlen) { 1515 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1516 goto bad; 1517 } 1518 (void)memcpy(&ipaddr.sin_addr, sin, 1519 sizeof(struct in_addr)); 1520 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1521 continue; 1522 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1523 off += sizeof(struct in_addr); 1524 break; 1525 1526 default: 1527 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 1528 goto bad; 1529 } 1530 ntime = iptime(); 1531 (void)memcpy(cp + off, &ntime, sizeof(n_time)); 1532 cp[IPOPT_OFFSET] += sizeof(n_time); 1533 } 1534 } 1535 if (forward && ipforwarding) { 1536 ip_forward(m, 1, next_hop); 1537 return (1); 1538 } 1539 return (0); 1540 bad: 1541 icmp_error(m, type, code, 0, 0); 1542 ipstat.ips_badoptions++; 1543 return (1); 1544 } 1545 1546 /* 1547 * Given address of next destination (final or next hop), 1548 * return internet address info of interface to be used to get there. 1549 */ 1550 struct in_ifaddr * 1551 ip_rtaddr(dst) 1552 struct in_addr dst; 1553 { 1554 struct route sro; 1555 struct sockaddr_in *sin; 1556 struct in_ifaddr *ifa; 1557 1558 bzero(&sro, sizeof(sro)); 1559 sin = (struct sockaddr_in *)&sro.ro_dst; 1560 sin->sin_family = AF_INET; 1561 sin->sin_len = sizeof(*sin); 1562 sin->sin_addr = dst; 1563 rtalloc_ign(&sro, RTF_CLONING); 1564 1565 if (sro.ro_rt == NULL) 1566 return ((struct in_ifaddr *)0); 1567 1568 ifa = ifatoia(sro.ro_rt->rt_ifa); 1569 RTFREE(sro.ro_rt); 1570 return ifa; 1571 } 1572 1573 /* 1574 * Save incoming source route for use in replies, 1575 * to be picked up later by ip_srcroute if the receiver is interested. 1576 */ 1577 static void 1578 save_rte(option, dst) 1579 u_char *option; 1580 struct in_addr dst; 1581 { 1582 unsigned olen; 1583 1584 olen = option[IPOPT_OLEN]; 1585 #ifdef DIAGNOSTIC 1586 if (ipprintfs) 1587 printf("save_rte: olen %d\n", olen); 1588 #endif 1589 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1590 return; 1591 bcopy(option, ip_srcrt.srcopt, olen); 1592 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1593 ip_srcrt.dst = dst; 1594 } 1595 1596 /* 1597 * Retrieve incoming source route for use in replies, 1598 * in the same form used by setsockopt. 1599 * The first hop is placed before the options, will be removed later. 1600 */ 1601 struct mbuf * 1602 ip_srcroute() 1603 { 1604 register struct in_addr *p, *q; 1605 register struct mbuf *m; 1606 1607 if (ip_nhops == 0) 1608 return ((struct mbuf *)0); 1609 m = m_get(M_DONTWAIT, MT_HEADER); 1610 if (m == 0) 1611 return ((struct mbuf *)0); 1612 1613 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1614 1615 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1616 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1617 OPTSIZ; 1618 #ifdef DIAGNOSTIC 1619 if (ipprintfs) 1620 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1621 #endif 1622 1623 /* 1624 * First save first hop for return route 1625 */ 1626 p = &ip_srcrt.route[ip_nhops - 1]; 1627 *(mtod(m, struct in_addr *)) = *p--; 1628 #ifdef DIAGNOSTIC 1629 if (ipprintfs) 1630 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1631 #endif 1632 1633 /* 1634 * Copy option fields and padding (nop) to mbuf. 1635 */ 1636 ip_srcrt.nop = IPOPT_NOP; 1637 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1638 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1639 &ip_srcrt.nop, OPTSIZ); 1640 q = (struct in_addr *)(mtod(m, caddr_t) + 1641 sizeof(struct in_addr) + OPTSIZ); 1642 #undef OPTSIZ 1643 /* 1644 * Record return path as an IP source route, 1645 * reversing the path (pointers are now aligned). 1646 */ 1647 while (p >= ip_srcrt.route) { 1648 #ifdef DIAGNOSTIC 1649 if (ipprintfs) 1650 printf(" %lx", (u_long)ntohl(q->s_addr)); 1651 #endif 1652 *q++ = *p--; 1653 } 1654 /* 1655 * Last hop goes to final destination. 1656 */ 1657 *q = ip_srcrt.dst; 1658 #ifdef DIAGNOSTIC 1659 if (ipprintfs) 1660 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1661 #endif 1662 return (m); 1663 } 1664 1665 /* 1666 * Strip out IP options, at higher 1667 * level protocol in the kernel. 1668 * Second argument is buffer to which options 1669 * will be moved, and return value is their length. 1670 * XXX should be deleted; last arg currently ignored. 1671 */ 1672 void 1673 ip_stripoptions(m, mopt) 1674 register struct mbuf *m; 1675 struct mbuf *mopt; 1676 { 1677 register int i; 1678 struct ip *ip = mtod(m, struct ip *); 1679 register caddr_t opts; 1680 int olen; 1681 1682 olen = (ip->ip_hl << 2) - sizeof (struct ip); 1683 opts = (caddr_t)(ip + 1); 1684 i = m->m_len - (sizeof (struct ip) + olen); 1685 bcopy(opts + olen, opts, (unsigned)i); 1686 m->m_len -= olen; 1687 if (m->m_flags & M_PKTHDR) 1688 m->m_pkthdr.len -= olen; 1689 ip->ip_v = IPVERSION; 1690 ip->ip_hl = sizeof(struct ip) >> 2; 1691 } 1692 1693 u_char inetctlerrmap[PRC_NCMDS] = { 1694 0, 0, 0, 0, 1695 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1696 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1697 EMSGSIZE, EHOSTUNREACH, 0, 0, 1698 0, 0, EHOSTUNREACH, 0, 1699 ENOPROTOOPT, ECONNREFUSED 1700 }; 1701 1702 /* 1703 * Forward a packet. If some error occurs return the sender 1704 * an icmp packet. Note we can't always generate a meaningful 1705 * icmp message because icmp doesn't have a large enough repertoire 1706 * of codes and types. 1707 * 1708 * If not forwarding, just drop the packet. This could be confusing 1709 * if ipforwarding was zero but some routing protocol was advancing 1710 * us as a gateway to somewhere. However, we must let the routing 1711 * protocol deal with that. 1712 * 1713 * The srcrt parameter indicates whether the packet is being forwarded 1714 * via a source route. 1715 */ 1716 static void 1717 ip_forward(struct mbuf *m, int srcrt, struct sockaddr_in *next_hop) 1718 { 1719 struct ip *ip = mtod(m, struct ip *); 1720 struct in_ifaddr *ia; 1721 int error, type = 0, code = 0; 1722 struct mbuf *mcopy; 1723 n_long dest; 1724 struct in_addr pkt_dst; 1725 struct ifnet *destifp; 1726 #if defined(IPSEC) || defined(FAST_IPSEC) 1727 struct ifnet dummyifp; 1728 #endif 1729 1730 /* 1731 * Cache the destination address of the packet; this may be 1732 * changed by use of 'ipfw fwd'. 1733 */ 1734 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst; 1735 1736 #ifdef DIAGNOSTIC 1737 if (ipprintfs) 1738 printf("forward: src %lx dst %lx ttl %x\n", 1739 (u_long)ip->ip_src.s_addr, (u_long)pkt_dst.s_addr, 1740 ip->ip_ttl); 1741 #endif 1742 1743 1744 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(pkt_dst) == 0) { 1745 ipstat.ips_cantforward++; 1746 m_freem(m); 1747 return; 1748 } 1749 #ifdef IPSTEALTH 1750 if (!ipstealth) { 1751 #endif 1752 if (ip->ip_ttl <= IPTTLDEC) { 1753 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1754 0, 0); 1755 return; 1756 } 1757 #ifdef IPSTEALTH 1758 } 1759 #endif 1760 1761 if ((ia = ip_rtaddr(pkt_dst)) == 0) { 1762 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1763 return; 1764 } 1765 1766 /* 1767 * Save the IP header and at most 8 bytes of the payload, 1768 * in case we need to generate an ICMP message to the src. 1769 * 1770 * XXX this can be optimized a lot by saving the data in a local 1771 * buffer on the stack (72 bytes at most), and only allocating the 1772 * mbuf if really necessary. The vast majority of the packets 1773 * are forwarded without having to send an ICMP back (either 1774 * because unnecessary, or because rate limited), so we are 1775 * really we are wasting a lot of work here. 1776 * 1777 * We don't use m_copy() because it might return a reference 1778 * to a shared cluster. Both this function and ip_output() 1779 * assume exclusive access to the IP header in `m', so any 1780 * data in a cluster may change before we reach icmp_error(). 1781 */ 1782 MGET(mcopy, M_DONTWAIT, m->m_type); 1783 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) { 1784 /* 1785 * It's probably ok if the pkthdr dup fails (because 1786 * the deep copy of the tag chain failed), but for now 1787 * be conservative and just discard the copy since 1788 * code below may some day want the tags. 1789 */ 1790 m_free(mcopy); 1791 mcopy = NULL; 1792 } 1793 if (mcopy != NULL) { 1794 mcopy->m_len = imin((ip->ip_hl << 2) + 8, 1795 (int)ip->ip_len); 1796 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1797 } 1798 1799 #ifdef IPSTEALTH 1800 if (!ipstealth) { 1801 #endif 1802 ip->ip_ttl -= IPTTLDEC; 1803 #ifdef IPSTEALTH 1804 } 1805 #endif 1806 1807 /* 1808 * If forwarding packet using same interface that it came in on, 1809 * perhaps should send a redirect to sender to shortcut a hop. 1810 * Only send redirect if source is sending directly to us, 1811 * and if packet was not source routed (or has any options). 1812 * Also, don't send redirect if forwarding using a default route 1813 * or a route modified by a redirect. 1814 */ 1815 dest = 0; 1816 if (ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) { 1817 struct sockaddr_in *sin; 1818 struct route ro; 1819 struct rtentry *rt; 1820 1821 bzero(&ro, sizeof(ro)); 1822 sin = (struct sockaddr_in *)&ro.ro_dst; 1823 sin->sin_family = AF_INET; 1824 sin->sin_len = sizeof(*sin); 1825 sin->sin_addr = pkt_dst; 1826 rtalloc_ign(&ro, RTF_CLONING); 1827 1828 rt = ro.ro_rt; 1829 1830 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1831 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1832 ipsendredirects && !srcrt && !next_hop) { 1833 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1834 u_long src = ntohl(ip->ip_src.s_addr); 1835 1836 if (RTA(rt) && 1837 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1838 if (rt->rt_flags & RTF_GATEWAY) 1839 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1840 else 1841 dest = pkt_dst.s_addr; 1842 /* Router requirements says to only send host redirects */ 1843 type = ICMP_REDIRECT; 1844 code = ICMP_REDIRECT_HOST; 1845 #ifdef DIAGNOSTIC 1846 if (ipprintfs) 1847 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1848 #endif 1849 } 1850 } 1851 if (rt) 1852 RTFREE(rt); 1853 } 1854 1855 if (next_hop) { 1856 struct m_tag *mtag = m_tag_get(PACKET_TAG_IPFORWARD, 1857 sizeof(struct sockaddr_in *), M_NOWAIT); 1858 if (mtag == NULL) { 1859 m_freem(m); 1860 return; 1861 } 1862 *(struct sockaddr_in **)(mtag+1) = next_hop; 1863 m_tag_prepend(m, mtag); 1864 } 1865 error = ip_output(m, (struct mbuf *)0, NULL, IP_FORWARDING, 0, NULL); 1866 if (error) 1867 ipstat.ips_cantforward++; 1868 else { 1869 ipstat.ips_forward++; 1870 if (type) 1871 ipstat.ips_redirectsent++; 1872 else { 1873 if (mcopy) 1874 m_freem(mcopy); 1875 return; 1876 } 1877 } 1878 if (mcopy == NULL) 1879 return; 1880 destifp = NULL; 1881 1882 switch (error) { 1883 1884 case 0: /* forwarded, but need redirect */ 1885 /* type, code set above */ 1886 break; 1887 1888 case ENETUNREACH: /* shouldn't happen, checked above */ 1889 case EHOSTUNREACH: 1890 case ENETDOWN: 1891 case EHOSTDOWN: 1892 default: 1893 type = ICMP_UNREACH; 1894 code = ICMP_UNREACH_HOST; 1895 break; 1896 1897 case EMSGSIZE: 1898 type = ICMP_UNREACH; 1899 code = ICMP_UNREACH_NEEDFRAG; 1900 #if defined(IPSEC) || defined(FAST_IPSEC) 1901 /* 1902 * If the packet is routed over IPsec tunnel, tell the 1903 * originator the tunnel MTU. 1904 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1905 * XXX quickhack!!! 1906 */ 1907 { 1908 struct secpolicy *sp = NULL; 1909 int ipsecerror; 1910 int ipsechdr; 1911 struct route *ro; 1912 1913 #ifdef IPSEC 1914 sp = ipsec4_getpolicybyaddr(mcopy, 1915 IPSEC_DIR_OUTBOUND, 1916 IP_FORWARDING, 1917 &ipsecerror); 1918 #else /* FAST_IPSEC */ 1919 sp = ipsec_getpolicybyaddr(mcopy, 1920 IPSEC_DIR_OUTBOUND, 1921 IP_FORWARDING, 1922 &ipsecerror); 1923 #endif 1924 if (sp != NULL) { 1925 /* count IPsec header size */ 1926 ipsechdr = ipsec4_hdrsiz(mcopy, 1927 IPSEC_DIR_OUTBOUND, 1928 NULL); 1929 1930 /* 1931 * find the correct route for outer IPv4 1932 * header, compute tunnel MTU. 1933 * 1934 * XXX BUG ALERT 1935 * The "dummyifp" code relies upon the fact 1936 * that icmp_error() touches only ifp->if_mtu. 1937 */ 1938 /*XXX*/ 1939 destifp = NULL; 1940 if (sp->req != NULL 1941 && sp->req->sav != NULL 1942 && sp->req->sav->sah != NULL) { 1943 ro = &sp->req->sav->sah->sa_route; 1944 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1945 dummyifp.if_mtu = 1946 ro->ro_rt->rt_ifp->if_mtu; 1947 dummyifp.if_mtu -= ipsechdr; 1948 destifp = &dummyifp; 1949 } 1950 } 1951 1952 #ifdef IPSEC 1953 key_freesp(sp); 1954 #else /* FAST_IPSEC */ 1955 KEY_FREESP(&sp); 1956 #endif 1957 ipstat.ips_cantfrag++; 1958 break; 1959 } else 1960 #endif /*IPSEC || FAST_IPSEC*/ 1961 destifp = ia->ia_ifp; 1962 #if defined(IPSEC) || defined(FAST_IPSEC) 1963 } 1964 #endif /*IPSEC || FAST_IPSEC*/ 1965 ipstat.ips_cantfrag++; 1966 break; 1967 1968 case ENOBUFS: 1969 /* 1970 * A router should not generate ICMP_SOURCEQUENCH as 1971 * required in RFC1812 Requirements for IP Version 4 Routers. 1972 * Source quench could be a big problem under DoS attacks, 1973 * or if the underlying interface is rate-limited. 1974 * Those who need source quench packets may re-enable them 1975 * via the net.inet.ip.sendsourcequench sysctl. 1976 */ 1977 if (ip_sendsourcequench == 0) { 1978 m_freem(mcopy); 1979 return; 1980 } else { 1981 type = ICMP_SOURCEQUENCH; 1982 code = 0; 1983 } 1984 break; 1985 1986 case EACCES: /* ipfw denied packet */ 1987 m_freem(mcopy); 1988 return; 1989 } 1990 icmp_error(mcopy, type, code, dest, destifp); 1991 } 1992 1993 void 1994 ip_savecontrol(inp, mp, ip, m) 1995 register struct inpcb *inp; 1996 register struct mbuf **mp; 1997 register struct ip *ip; 1998 register struct mbuf *m; 1999 { 2000 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 2001 struct bintime bt; 2002 2003 bintime(&bt); 2004 if (inp->inp_socket->so_options & SO_BINTIME) { 2005 *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt), 2006 SCM_BINTIME, SOL_SOCKET); 2007 if (*mp) 2008 mp = &(*mp)->m_next; 2009 } 2010 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 2011 struct timeval tv; 2012 2013 bintime2timeval(&bt, &tv); 2014 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 2015 SCM_TIMESTAMP, SOL_SOCKET); 2016 if (*mp) 2017 mp = &(*mp)->m_next; 2018 } 2019 } 2020 if (inp->inp_flags & INP_RECVDSTADDR) { 2021 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 2022 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 2023 if (*mp) 2024 mp = &(*mp)->m_next; 2025 } 2026 if (inp->inp_flags & INP_RECVTTL) { 2027 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, 2028 sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 2029 if (*mp) 2030 mp = &(*mp)->m_next; 2031 } 2032 #ifdef notyet 2033 /* XXX 2034 * Moving these out of udp_input() made them even more broken 2035 * than they already were. 2036 */ 2037 /* options were tossed already */ 2038 if (inp->inp_flags & INP_RECVOPTS) { 2039 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 2040 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 2041 if (*mp) 2042 mp = &(*mp)->m_next; 2043 } 2044 /* ip_srcroute doesn't do what we want here, need to fix */ 2045 if (inp->inp_flags & INP_RECVRETOPTS) { 2046 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 2047 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 2048 if (*mp) 2049 mp = &(*mp)->m_next; 2050 } 2051 #endif 2052 if (inp->inp_flags & INP_RECVIF) { 2053 struct ifnet *ifp; 2054 struct sdlbuf { 2055 struct sockaddr_dl sdl; 2056 u_char pad[32]; 2057 } sdlbuf; 2058 struct sockaddr_dl *sdp; 2059 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 2060 2061 if (((ifp = m->m_pkthdr.rcvif)) 2062 && ( ifp->if_index && (ifp->if_index <= if_index))) { 2063 sdp = (struct sockaddr_dl *) 2064 (ifaddr_byindex(ifp->if_index)->ifa_addr); 2065 /* 2066 * Change our mind and don't try copy. 2067 */ 2068 if ((sdp->sdl_family != AF_LINK) 2069 || (sdp->sdl_len > sizeof(sdlbuf))) { 2070 goto makedummy; 2071 } 2072 bcopy(sdp, sdl2, sdp->sdl_len); 2073 } else { 2074 makedummy: 2075 sdl2->sdl_len 2076 = offsetof(struct sockaddr_dl, sdl_data[0]); 2077 sdl2->sdl_family = AF_LINK; 2078 sdl2->sdl_index = 0; 2079 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 2080 } 2081 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 2082 IP_RECVIF, IPPROTO_IP); 2083 if (*mp) 2084 mp = &(*mp)->m_next; 2085 } 2086 } 2087 2088 /* 2089 * XXX these routines are called from the upper part of the kernel. 2090 * They need to be locked when we remove Giant. 2091 * 2092 * They could also be moved to ip_mroute.c, since all the RSVP 2093 * handling is done there already. 2094 */ 2095 static int ip_rsvp_on; 2096 struct socket *ip_rsvpd; 2097 int 2098 ip_rsvp_init(struct socket *so) 2099 { 2100 if (so->so_type != SOCK_RAW || 2101 so->so_proto->pr_protocol != IPPROTO_RSVP) 2102 return EOPNOTSUPP; 2103 2104 if (ip_rsvpd != NULL) 2105 return EADDRINUSE; 2106 2107 ip_rsvpd = so; 2108 /* 2109 * This may seem silly, but we need to be sure we don't over-increment 2110 * the RSVP counter, in case something slips up. 2111 */ 2112 if (!ip_rsvp_on) { 2113 ip_rsvp_on = 1; 2114 rsvp_on++; 2115 } 2116 2117 return 0; 2118 } 2119 2120 int 2121 ip_rsvp_done(void) 2122 { 2123 ip_rsvpd = NULL; 2124 /* 2125 * This may seem silly, but we need to be sure we don't over-decrement 2126 * the RSVP counter, in case something slips up. 2127 */ 2128 if (ip_rsvp_on) { 2129 ip_rsvp_on = 0; 2130 rsvp_on--; 2131 } 2132 return 0; 2133 } 2134 2135 void 2136 rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */ 2137 { 2138 if (rsvp_input_p) { /* call the real one if loaded */ 2139 rsvp_input_p(m, off); 2140 return; 2141 } 2142 2143 /* Can still get packets with rsvp_on = 0 if there is a local member 2144 * of the group to which the RSVP packet is addressed. But in this 2145 * case we want to throw the packet away. 2146 */ 2147 2148 if (!rsvp_on) { 2149 m_freem(m); 2150 return; 2151 } 2152 2153 if (ip_rsvpd != NULL) { 2154 rip_input(m, off); 2155 return; 2156 } 2157 /* Drop the packet */ 2158 m_freem(m); 2159 } 2160