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