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