1 /* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 30 * $FreeBSD$ 31 */ 32 33 #include "opt_bootp.h" 34 #include "opt_ipfw.h" 35 #include "opt_ipdn.h" 36 #include "opt_ipdivert.h" 37 #include "opt_ipfilter.h" 38 #include "opt_ipstealth.h" 39 #include "opt_ipsec.h" 40 #include "opt_mac.h" 41 #include "opt_pfil_hooks.h" 42 #include "opt_random_ip_id.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/mac.h> 47 #include <sys/mbuf.h> 48 #include <sys/malloc.h> 49 #include <sys/domain.h> 50 #include <sys/protosw.h> 51 #include <sys/socket.h> 52 #include <sys/time.h> 53 #include <sys/kernel.h> 54 #include <sys/syslog.h> 55 #include <sys/sysctl.h> 56 57 #include <net/pfil.h> 58 #include <net/if.h> 59 #include <net/if_types.h> 60 #include <net/if_var.h> 61 #include <net/if_dl.h> 62 #include <net/route.h> 63 #include <net/netisr.h> 64 65 #include <netinet/in.h> 66 #include <netinet/in_systm.h> 67 #include <netinet/in_var.h> 68 #include <netinet/ip.h> 69 #include <netinet/in_pcb.h> 70 #include <netinet/ip_var.h> 71 #include <netinet/ip_icmp.h> 72 #include <machine/in_cksum.h> 73 74 #include <sys/socketvar.h> 75 76 #include <netinet/ip_fw.h> 77 #include <netinet/ip_divert.h> 78 #include <netinet/ip_dummynet.h> 79 80 #ifdef IPSEC 81 #include <netinet6/ipsec.h> 82 #include <netkey/key.h> 83 #endif 84 85 #ifdef FAST_IPSEC 86 #include <netipsec/ipsec.h> 87 #include <netipsec/key.h> 88 #endif 89 90 int rsvp_on = 0; 91 92 int ipforwarding = 0; 93 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 94 &ipforwarding, 0, "Enable IP forwarding between interfaces"); 95 96 static int ipsendredirects = 1; /* XXX */ 97 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 98 &ipsendredirects, 0, "Enable sending IP redirects"); 99 100 int ip_defttl = IPDEFTTL; 101 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 102 &ip_defttl, 0, "Maximum TTL on IP packets"); 103 104 static int ip_dosourceroute = 0; 105 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 106 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); 107 108 static int ip_acceptsourceroute = 0; 109 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 110 CTLFLAG_RW, &ip_acceptsourceroute, 0, 111 "Enable accepting source routed IP packets"); 112 113 int ip_doopts = 1; /* 0 = ignore, 1 = process, 2 = reject */ 114 SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW, 115 &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)"); 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 = m_claim_next(m, PACKET_TAG_IPFORWARD); 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 /* ignore or reject packets with IP options */ 1295 if (ip_doopts == 0) 1296 return 0; 1297 else if (ip_doopts == 2) { 1298 type = ICMP_UNREACH; 1299 code = ICMP_UNREACH_FILTER_PROHIB; 1300 goto bad; 1301 } 1302 1303 dst = ip->ip_dst; 1304 cp = (u_char *)(ip + 1); 1305 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1306 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1307 opt = cp[IPOPT_OPTVAL]; 1308 if (opt == IPOPT_EOL) 1309 break; 1310 if (opt == IPOPT_NOP) 1311 optlen = 1; 1312 else { 1313 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1314 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1315 goto bad; 1316 } 1317 optlen = cp[IPOPT_OLEN]; 1318 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1319 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1320 goto bad; 1321 } 1322 } 1323 switch (opt) { 1324 1325 default: 1326 break; 1327 1328 /* 1329 * Source routing with record. 1330 * Find interface with current destination address. 1331 * If none on this machine then drop if strictly routed, 1332 * or do nothing if loosely routed. 1333 * Record interface address and bring up next address 1334 * component. If strictly routed make sure next 1335 * address is on directly accessible net. 1336 */ 1337 case IPOPT_LSRR: 1338 case IPOPT_SSRR: 1339 #ifdef IPSTEALTH 1340 if (ipstealth && pass > 0) 1341 break; 1342 #endif 1343 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1344 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1345 goto bad; 1346 } 1347 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1348 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1349 goto bad; 1350 } 1351 ipaddr.sin_addr = ip->ip_dst; 1352 ia = (struct in_ifaddr *) 1353 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1354 if (ia == 0) { 1355 if (opt == IPOPT_SSRR) { 1356 type = ICMP_UNREACH; 1357 code = ICMP_UNREACH_SRCFAIL; 1358 goto bad; 1359 } 1360 if (!ip_dosourceroute) 1361 goto nosourcerouting; 1362 /* 1363 * Loose routing, and not at next destination 1364 * yet; nothing to do except forward. 1365 */ 1366 break; 1367 } 1368 off--; /* 0 origin */ 1369 if (off > optlen - (int)sizeof(struct in_addr)) { 1370 /* 1371 * End of source route. Should be for us. 1372 */ 1373 if (!ip_acceptsourceroute) 1374 goto nosourcerouting; 1375 save_rte(cp, ip->ip_src); 1376 break; 1377 } 1378 #ifdef IPSTEALTH 1379 if (ipstealth) 1380 goto dropit; 1381 #endif 1382 if (!ip_dosourceroute) { 1383 if (ipforwarding) { 1384 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1385 /* 1386 * Acting as a router, so generate ICMP 1387 */ 1388 nosourcerouting: 1389 strcpy(buf, inet_ntoa(ip->ip_dst)); 1390 log(LOG_WARNING, 1391 "attempted source route from %s to %s\n", 1392 inet_ntoa(ip->ip_src), buf); 1393 type = ICMP_UNREACH; 1394 code = ICMP_UNREACH_SRCFAIL; 1395 goto bad; 1396 } else { 1397 /* 1398 * Not acting as a router, so silently drop. 1399 */ 1400 #ifdef IPSTEALTH 1401 dropit: 1402 #endif 1403 ipstat.ips_cantforward++; 1404 m_freem(m); 1405 return (1); 1406 } 1407 } 1408 1409 /* 1410 * locate outgoing interface 1411 */ 1412 (void)memcpy(&ipaddr.sin_addr, cp + off, 1413 sizeof(ipaddr.sin_addr)); 1414 1415 if (opt == IPOPT_SSRR) { 1416 #define INA struct in_ifaddr * 1417 #define SA struct sockaddr * 1418 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 1419 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1420 } else 1421 ia = ip_rtaddr(ipaddr.sin_addr); 1422 if (ia == 0) { 1423 type = ICMP_UNREACH; 1424 code = ICMP_UNREACH_SRCFAIL; 1425 goto bad; 1426 } 1427 ip->ip_dst = ipaddr.sin_addr; 1428 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1429 sizeof(struct in_addr)); 1430 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1431 /* 1432 * Let ip_intr's mcast routing check handle mcast pkts 1433 */ 1434 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1435 break; 1436 1437 case IPOPT_RR: 1438 #ifdef IPSTEALTH 1439 if (ipstealth && pass == 0) 1440 break; 1441 #endif 1442 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1443 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1444 goto bad; 1445 } 1446 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1447 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1448 goto bad; 1449 } 1450 /* 1451 * If no space remains, ignore. 1452 */ 1453 off--; /* 0 origin */ 1454 if (off > optlen - (int)sizeof(struct in_addr)) 1455 break; 1456 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1457 sizeof(ipaddr.sin_addr)); 1458 /* 1459 * locate outgoing interface; if we're the destination, 1460 * use the incoming interface (should be same). 1461 */ 1462 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1463 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1464 type = ICMP_UNREACH; 1465 code = ICMP_UNREACH_HOST; 1466 goto bad; 1467 } 1468 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1469 sizeof(struct in_addr)); 1470 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1471 break; 1472 1473 case IPOPT_TS: 1474 #ifdef IPSTEALTH 1475 if (ipstealth && pass == 0) 1476 break; 1477 #endif 1478 code = cp - (u_char *)ip; 1479 if (optlen < 4 || optlen > 40) { 1480 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1481 goto bad; 1482 } 1483 if ((off = cp[IPOPT_OFFSET]) < 5) { 1484 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1485 goto bad; 1486 } 1487 if (off > optlen - (int)sizeof(int32_t)) { 1488 cp[IPOPT_OFFSET + 1] += (1 << 4); 1489 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) { 1490 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1491 goto bad; 1492 } 1493 break; 1494 } 1495 off--; /* 0 origin */ 1496 sin = (struct in_addr *)(cp + off); 1497 switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 1498 1499 case IPOPT_TS_TSONLY: 1500 break; 1501 1502 case IPOPT_TS_TSANDADDR: 1503 if (off + sizeof(n_time) + 1504 sizeof(struct in_addr) > optlen) { 1505 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1506 goto bad; 1507 } 1508 ipaddr.sin_addr = dst; 1509 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1510 m->m_pkthdr.rcvif); 1511 if (ia == 0) 1512 continue; 1513 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1514 sizeof(struct in_addr)); 1515 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1516 off += sizeof(struct in_addr); 1517 break; 1518 1519 case IPOPT_TS_PRESPEC: 1520 if (off + sizeof(n_time) + 1521 sizeof(struct in_addr) > optlen) { 1522 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1523 goto bad; 1524 } 1525 (void)memcpy(&ipaddr.sin_addr, sin, 1526 sizeof(struct in_addr)); 1527 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1528 continue; 1529 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1530 off += sizeof(struct in_addr); 1531 break; 1532 1533 default: 1534 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 1535 goto bad; 1536 } 1537 ntime = iptime(); 1538 (void)memcpy(cp + off, &ntime, sizeof(n_time)); 1539 cp[IPOPT_OFFSET] += sizeof(n_time); 1540 } 1541 } 1542 if (forward && ipforwarding) { 1543 ip_forward(m, 1, next_hop); 1544 return (1); 1545 } 1546 return (0); 1547 bad: 1548 icmp_error(m, type, code, 0, 0); 1549 ipstat.ips_badoptions++; 1550 return (1); 1551 } 1552 1553 /* 1554 * Given address of next destination (final or next hop), 1555 * return internet address info of interface to be used to get there. 1556 */ 1557 struct in_ifaddr * 1558 ip_rtaddr(dst) 1559 struct in_addr dst; 1560 { 1561 struct route sro; 1562 struct sockaddr_in *sin; 1563 struct in_ifaddr *ifa; 1564 1565 bzero(&sro, sizeof(sro)); 1566 sin = (struct sockaddr_in *)&sro.ro_dst; 1567 sin->sin_family = AF_INET; 1568 sin->sin_len = sizeof(*sin); 1569 sin->sin_addr = dst; 1570 rtalloc_ign(&sro, RTF_CLONING); 1571 1572 if (sro.ro_rt == NULL) 1573 return ((struct in_ifaddr *)0); 1574 1575 ifa = ifatoia(sro.ro_rt->rt_ifa); 1576 RTFREE(sro.ro_rt); 1577 return ifa; 1578 } 1579 1580 /* 1581 * Save incoming source route for use in replies, 1582 * to be picked up later by ip_srcroute if the receiver is interested. 1583 */ 1584 static void 1585 save_rte(option, dst) 1586 u_char *option; 1587 struct in_addr dst; 1588 { 1589 unsigned olen; 1590 1591 olen = option[IPOPT_OLEN]; 1592 #ifdef DIAGNOSTIC 1593 if (ipprintfs) 1594 printf("save_rte: olen %d\n", olen); 1595 #endif 1596 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1597 return; 1598 bcopy(option, ip_srcrt.srcopt, olen); 1599 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1600 ip_srcrt.dst = dst; 1601 } 1602 1603 /* 1604 * Retrieve incoming source route for use in replies, 1605 * in the same form used by setsockopt. 1606 * The first hop is placed before the options, will be removed later. 1607 */ 1608 struct mbuf * 1609 ip_srcroute() 1610 { 1611 register struct in_addr *p, *q; 1612 register struct mbuf *m; 1613 1614 if (ip_nhops == 0) 1615 return ((struct mbuf *)0); 1616 m = m_get(M_DONTWAIT, MT_HEADER); 1617 if (m == 0) 1618 return ((struct mbuf *)0); 1619 1620 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1621 1622 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1623 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1624 OPTSIZ; 1625 #ifdef DIAGNOSTIC 1626 if (ipprintfs) 1627 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1628 #endif 1629 1630 /* 1631 * First save first hop for return route 1632 */ 1633 p = &ip_srcrt.route[ip_nhops - 1]; 1634 *(mtod(m, struct in_addr *)) = *p--; 1635 #ifdef DIAGNOSTIC 1636 if (ipprintfs) 1637 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1638 #endif 1639 1640 /* 1641 * Copy option fields and padding (nop) to mbuf. 1642 */ 1643 ip_srcrt.nop = IPOPT_NOP; 1644 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1645 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1646 &ip_srcrt.nop, OPTSIZ); 1647 q = (struct in_addr *)(mtod(m, caddr_t) + 1648 sizeof(struct in_addr) + OPTSIZ); 1649 #undef OPTSIZ 1650 /* 1651 * Record return path as an IP source route, 1652 * reversing the path (pointers are now aligned). 1653 */ 1654 while (p >= ip_srcrt.route) { 1655 #ifdef DIAGNOSTIC 1656 if (ipprintfs) 1657 printf(" %lx", (u_long)ntohl(q->s_addr)); 1658 #endif 1659 *q++ = *p--; 1660 } 1661 /* 1662 * Last hop goes to final destination. 1663 */ 1664 *q = ip_srcrt.dst; 1665 #ifdef DIAGNOSTIC 1666 if (ipprintfs) 1667 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1668 #endif 1669 return (m); 1670 } 1671 1672 /* 1673 * Strip out IP options, at higher 1674 * level protocol in the kernel. 1675 * Second argument is buffer to which options 1676 * will be moved, and return value is their length. 1677 * XXX should be deleted; last arg currently ignored. 1678 */ 1679 void 1680 ip_stripoptions(m, mopt) 1681 register struct mbuf *m; 1682 struct mbuf *mopt; 1683 { 1684 register int i; 1685 struct ip *ip = mtod(m, struct ip *); 1686 register caddr_t opts; 1687 int olen; 1688 1689 olen = (ip->ip_hl << 2) - sizeof (struct ip); 1690 opts = (caddr_t)(ip + 1); 1691 i = m->m_len - (sizeof (struct ip) + olen); 1692 bcopy(opts + olen, opts, (unsigned)i); 1693 m->m_len -= olen; 1694 if (m->m_flags & M_PKTHDR) 1695 m->m_pkthdr.len -= olen; 1696 ip->ip_v = IPVERSION; 1697 ip->ip_hl = sizeof(struct ip) >> 2; 1698 } 1699 1700 u_char inetctlerrmap[PRC_NCMDS] = { 1701 0, 0, 0, 0, 1702 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1703 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1704 EMSGSIZE, EHOSTUNREACH, 0, 0, 1705 0, 0, EHOSTUNREACH, 0, 1706 ENOPROTOOPT, ECONNREFUSED 1707 }; 1708 1709 /* 1710 * Forward a packet. If some error occurs return the sender 1711 * an icmp packet. Note we can't always generate a meaningful 1712 * icmp message because icmp doesn't have a large enough repertoire 1713 * of codes and types. 1714 * 1715 * If not forwarding, just drop the packet. This could be confusing 1716 * if ipforwarding was zero but some routing protocol was advancing 1717 * us as a gateway to somewhere. However, we must let the routing 1718 * protocol deal with that. 1719 * 1720 * The srcrt parameter indicates whether the packet is being forwarded 1721 * via a source route. 1722 */ 1723 static void 1724 ip_forward(struct mbuf *m, int srcrt, struct sockaddr_in *next_hop) 1725 { 1726 struct ip *ip = mtod(m, struct ip *); 1727 struct in_ifaddr *ia; 1728 int error, type = 0, code = 0; 1729 struct mbuf *mcopy; 1730 n_long dest; 1731 struct in_addr pkt_dst; 1732 struct ifnet *destifp; 1733 #if defined(IPSEC) || defined(FAST_IPSEC) 1734 struct ifnet dummyifp; 1735 #endif 1736 1737 /* 1738 * Cache the destination address of the packet; this may be 1739 * changed by use of 'ipfw fwd'. 1740 */ 1741 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst; 1742 1743 #ifdef DIAGNOSTIC 1744 if (ipprintfs) 1745 printf("forward: src %lx dst %lx ttl %x\n", 1746 (u_long)ip->ip_src.s_addr, (u_long)pkt_dst.s_addr, 1747 ip->ip_ttl); 1748 #endif 1749 1750 1751 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(pkt_dst) == 0) { 1752 ipstat.ips_cantforward++; 1753 m_freem(m); 1754 return; 1755 } 1756 #ifdef IPSTEALTH 1757 if (!ipstealth) { 1758 #endif 1759 if (ip->ip_ttl <= IPTTLDEC) { 1760 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1761 0, 0); 1762 return; 1763 } 1764 #ifdef IPSTEALTH 1765 } 1766 #endif 1767 1768 if ((ia = ip_rtaddr(pkt_dst)) == 0) { 1769 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1770 return; 1771 } 1772 1773 /* 1774 * Save the IP header and at most 8 bytes of the payload, 1775 * in case we need to generate an ICMP message to the src. 1776 * 1777 * XXX this can be optimized a lot by saving the data in a local 1778 * buffer on the stack (72 bytes at most), and only allocating the 1779 * mbuf if really necessary. The vast majority of the packets 1780 * are forwarded without having to send an ICMP back (either 1781 * because unnecessary, or because rate limited), so we are 1782 * really we are wasting a lot of work here. 1783 * 1784 * We don't use m_copy() because it might return a reference 1785 * to a shared cluster. Both this function and ip_output() 1786 * assume exclusive access to the IP header in `m', so any 1787 * data in a cluster may change before we reach icmp_error(). 1788 */ 1789 MGET(mcopy, M_DONTWAIT, m->m_type); 1790 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) { 1791 /* 1792 * It's probably ok if the pkthdr dup fails (because 1793 * the deep copy of the tag chain failed), but for now 1794 * be conservative and just discard the copy since 1795 * code below may some day want the tags. 1796 */ 1797 m_free(mcopy); 1798 mcopy = NULL; 1799 } 1800 if (mcopy != NULL) { 1801 mcopy->m_len = imin((ip->ip_hl << 2) + 8, 1802 (int)ip->ip_len); 1803 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1804 } 1805 1806 #ifdef IPSTEALTH 1807 if (!ipstealth) { 1808 #endif 1809 ip->ip_ttl -= IPTTLDEC; 1810 #ifdef IPSTEALTH 1811 } 1812 #endif 1813 1814 /* 1815 * If forwarding packet using same interface that it came in on, 1816 * perhaps should send a redirect to sender to shortcut a hop. 1817 * Only send redirect if source is sending directly to us, 1818 * and if packet was not source routed (or has any options). 1819 * Also, don't send redirect if forwarding using a default route 1820 * or a route modified by a redirect. 1821 */ 1822 dest = 0; 1823 if (ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) { 1824 struct sockaddr_in *sin; 1825 struct route ro; 1826 struct rtentry *rt; 1827 1828 bzero(&ro, sizeof(ro)); 1829 sin = (struct sockaddr_in *)&ro.ro_dst; 1830 sin->sin_family = AF_INET; 1831 sin->sin_len = sizeof(*sin); 1832 sin->sin_addr = pkt_dst; 1833 rtalloc_ign(&ro, RTF_CLONING); 1834 1835 rt = ro.ro_rt; 1836 1837 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1838 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1839 ipsendredirects && !srcrt && !next_hop) { 1840 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1841 u_long src = ntohl(ip->ip_src.s_addr); 1842 1843 if (RTA(rt) && 1844 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1845 if (rt->rt_flags & RTF_GATEWAY) 1846 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1847 else 1848 dest = pkt_dst.s_addr; 1849 /* Router requirements says to only send host redirects */ 1850 type = ICMP_REDIRECT; 1851 code = ICMP_REDIRECT_HOST; 1852 #ifdef DIAGNOSTIC 1853 if (ipprintfs) 1854 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1855 #endif 1856 } 1857 } 1858 if (rt) 1859 RTFREE(rt); 1860 } 1861 1862 if (next_hop) { 1863 struct m_tag *mtag = m_tag_get(PACKET_TAG_IPFORWARD, 1864 sizeof(struct sockaddr_in *), M_NOWAIT); 1865 if (mtag == NULL) { 1866 m_freem(m); 1867 return; 1868 } 1869 *(struct sockaddr_in **)(mtag+1) = next_hop; 1870 m_tag_prepend(m, mtag); 1871 } 1872 error = ip_output(m, (struct mbuf *)0, NULL, IP_FORWARDING, 0, NULL); 1873 if (error) 1874 ipstat.ips_cantforward++; 1875 else { 1876 ipstat.ips_forward++; 1877 if (type) 1878 ipstat.ips_redirectsent++; 1879 else { 1880 if (mcopy) 1881 m_freem(mcopy); 1882 return; 1883 } 1884 } 1885 if (mcopy == NULL) 1886 return; 1887 destifp = NULL; 1888 1889 switch (error) { 1890 1891 case 0: /* forwarded, but need redirect */ 1892 /* type, code set above */ 1893 break; 1894 1895 case ENETUNREACH: /* shouldn't happen, checked above */ 1896 case EHOSTUNREACH: 1897 case ENETDOWN: 1898 case EHOSTDOWN: 1899 default: 1900 type = ICMP_UNREACH; 1901 code = ICMP_UNREACH_HOST; 1902 break; 1903 1904 case EMSGSIZE: 1905 type = ICMP_UNREACH; 1906 code = ICMP_UNREACH_NEEDFRAG; 1907 #if defined(IPSEC) || defined(FAST_IPSEC) 1908 /* 1909 * If the packet is routed over IPsec tunnel, tell the 1910 * originator the tunnel MTU. 1911 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1912 * XXX quickhack!!! 1913 */ 1914 { 1915 struct secpolicy *sp = NULL; 1916 int ipsecerror; 1917 int ipsechdr; 1918 struct route *ro; 1919 1920 #ifdef IPSEC 1921 sp = ipsec4_getpolicybyaddr(mcopy, 1922 IPSEC_DIR_OUTBOUND, 1923 IP_FORWARDING, 1924 &ipsecerror); 1925 #else /* FAST_IPSEC */ 1926 sp = ipsec_getpolicybyaddr(mcopy, 1927 IPSEC_DIR_OUTBOUND, 1928 IP_FORWARDING, 1929 &ipsecerror); 1930 #endif 1931 if (sp != NULL) { 1932 /* count IPsec header size */ 1933 ipsechdr = ipsec4_hdrsiz(mcopy, 1934 IPSEC_DIR_OUTBOUND, 1935 NULL); 1936 1937 /* 1938 * find the correct route for outer IPv4 1939 * header, compute tunnel MTU. 1940 * 1941 * XXX BUG ALERT 1942 * The "dummyifp" code relies upon the fact 1943 * that icmp_error() touches only ifp->if_mtu. 1944 */ 1945 /*XXX*/ 1946 destifp = NULL; 1947 if (sp->req != NULL 1948 && sp->req->sav != NULL 1949 && sp->req->sav->sah != NULL) { 1950 ro = &sp->req->sav->sah->sa_route; 1951 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1952 dummyifp.if_mtu = 1953 ro->ro_rt->rt_ifp->if_mtu; 1954 dummyifp.if_mtu -= ipsechdr; 1955 destifp = &dummyifp; 1956 } 1957 } 1958 1959 #ifdef IPSEC 1960 key_freesp(sp); 1961 #else /* FAST_IPSEC */ 1962 KEY_FREESP(&sp); 1963 #endif 1964 ipstat.ips_cantfrag++; 1965 break; 1966 } else 1967 #endif /*IPSEC || FAST_IPSEC*/ 1968 destifp = ia->ia_ifp; 1969 #if defined(IPSEC) || defined(FAST_IPSEC) 1970 } 1971 #endif /*IPSEC || FAST_IPSEC*/ 1972 ipstat.ips_cantfrag++; 1973 break; 1974 1975 case ENOBUFS: 1976 /* 1977 * A router should not generate ICMP_SOURCEQUENCH as 1978 * required in RFC1812 Requirements for IP Version 4 Routers. 1979 * Source quench could be a big problem under DoS attacks, 1980 * or if the underlying interface is rate-limited. 1981 * Those who need source quench packets may re-enable them 1982 * via the net.inet.ip.sendsourcequench sysctl. 1983 */ 1984 if (ip_sendsourcequench == 0) { 1985 m_freem(mcopy); 1986 return; 1987 } else { 1988 type = ICMP_SOURCEQUENCH; 1989 code = 0; 1990 } 1991 break; 1992 1993 case EACCES: /* ipfw denied packet */ 1994 m_freem(mcopy); 1995 return; 1996 } 1997 icmp_error(mcopy, type, code, dest, destifp); 1998 } 1999 2000 void 2001 ip_savecontrol(inp, mp, ip, m) 2002 register struct inpcb *inp; 2003 register struct mbuf **mp; 2004 register struct ip *ip; 2005 register struct mbuf *m; 2006 { 2007 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 2008 struct bintime bt; 2009 2010 bintime(&bt); 2011 if (inp->inp_socket->so_options & SO_BINTIME) { 2012 *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt), 2013 SCM_BINTIME, SOL_SOCKET); 2014 if (*mp) 2015 mp = &(*mp)->m_next; 2016 } 2017 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 2018 struct timeval tv; 2019 2020 bintime2timeval(&bt, &tv); 2021 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 2022 SCM_TIMESTAMP, SOL_SOCKET); 2023 if (*mp) 2024 mp = &(*mp)->m_next; 2025 } 2026 } 2027 if (inp->inp_flags & INP_RECVDSTADDR) { 2028 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 2029 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 2030 if (*mp) 2031 mp = &(*mp)->m_next; 2032 } 2033 if (inp->inp_flags & INP_RECVTTL) { 2034 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, 2035 sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 2036 if (*mp) 2037 mp = &(*mp)->m_next; 2038 } 2039 #ifdef notyet 2040 /* XXX 2041 * Moving these out of udp_input() made them even more broken 2042 * than they already were. 2043 */ 2044 /* options were tossed already */ 2045 if (inp->inp_flags & INP_RECVOPTS) { 2046 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 2047 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 2048 if (*mp) 2049 mp = &(*mp)->m_next; 2050 } 2051 /* ip_srcroute doesn't do what we want here, need to fix */ 2052 if (inp->inp_flags & INP_RECVRETOPTS) { 2053 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 2054 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 2055 if (*mp) 2056 mp = &(*mp)->m_next; 2057 } 2058 #endif 2059 if (inp->inp_flags & INP_RECVIF) { 2060 struct ifnet *ifp; 2061 struct sdlbuf { 2062 struct sockaddr_dl sdl; 2063 u_char pad[32]; 2064 } sdlbuf; 2065 struct sockaddr_dl *sdp; 2066 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 2067 2068 if (((ifp = m->m_pkthdr.rcvif)) 2069 && ( ifp->if_index && (ifp->if_index <= if_index))) { 2070 sdp = (struct sockaddr_dl *) 2071 (ifaddr_byindex(ifp->if_index)->ifa_addr); 2072 /* 2073 * Change our mind and don't try copy. 2074 */ 2075 if ((sdp->sdl_family != AF_LINK) 2076 || (sdp->sdl_len > sizeof(sdlbuf))) { 2077 goto makedummy; 2078 } 2079 bcopy(sdp, sdl2, sdp->sdl_len); 2080 } else { 2081 makedummy: 2082 sdl2->sdl_len 2083 = offsetof(struct sockaddr_dl, sdl_data[0]); 2084 sdl2->sdl_family = AF_LINK; 2085 sdl2->sdl_index = 0; 2086 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 2087 } 2088 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 2089 IP_RECVIF, IPPROTO_IP); 2090 if (*mp) 2091 mp = &(*mp)->m_next; 2092 } 2093 } 2094 2095 /* 2096 * XXX these routines are called from the upper part of the kernel. 2097 * They need to be locked when we remove Giant. 2098 * 2099 * They could also be moved to ip_mroute.c, since all the RSVP 2100 * handling is done there already. 2101 */ 2102 static int ip_rsvp_on; 2103 struct socket *ip_rsvpd; 2104 int 2105 ip_rsvp_init(struct socket *so) 2106 { 2107 if (so->so_type != SOCK_RAW || 2108 so->so_proto->pr_protocol != IPPROTO_RSVP) 2109 return EOPNOTSUPP; 2110 2111 if (ip_rsvpd != NULL) 2112 return EADDRINUSE; 2113 2114 ip_rsvpd = so; 2115 /* 2116 * This may seem silly, but we need to be sure we don't over-increment 2117 * the RSVP counter, in case something slips up. 2118 */ 2119 if (!ip_rsvp_on) { 2120 ip_rsvp_on = 1; 2121 rsvp_on++; 2122 } 2123 2124 return 0; 2125 } 2126 2127 int 2128 ip_rsvp_done(void) 2129 { 2130 ip_rsvpd = NULL; 2131 /* 2132 * This may seem silly, but we need to be sure we don't over-decrement 2133 * the RSVP counter, in case something slips up. 2134 */ 2135 if (ip_rsvp_on) { 2136 ip_rsvp_on = 0; 2137 rsvp_on--; 2138 } 2139 return 0; 2140 } 2141 2142 void 2143 rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */ 2144 { 2145 if (rsvp_input_p) { /* call the real one if loaded */ 2146 rsvp_input_p(m, off); 2147 return; 2148 } 2149 2150 /* Can still get packets with rsvp_on = 0 if there is a local member 2151 * of the group to which the RSVP packet is addressed. But in this 2152 * case we want to throw the packet away. 2153 */ 2154 2155 if (!rsvp_on) { 2156 m_freem(m); 2157 return; 2158 } 2159 2160 if (ip_rsvpd != NULL) { 2161 rip_input(m, off); 2162 return; 2163 } 2164 /* Drop the packet */ 2165 m_freem(m); 2166 } 2167