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