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