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