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 in_ifaddr *ia; 1165 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1166 struct in_addr *sin, dst; 1167 n_time ntime; 1168 1169 dst = ip->ip_dst; 1170 cp = (u_char *)(ip + 1); 1171 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1172 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1173 opt = cp[IPOPT_OPTVAL]; 1174 if (opt == IPOPT_EOL) 1175 break; 1176 if (opt == IPOPT_NOP) 1177 optlen = 1; 1178 else { 1179 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1180 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1181 goto bad; 1182 } 1183 optlen = cp[IPOPT_OLEN]; 1184 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1185 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1186 goto bad; 1187 } 1188 } 1189 switch (opt) { 1190 1191 default: 1192 break; 1193 1194 /* 1195 * Source routing with record. 1196 * Find interface with current destination address. 1197 * If none on this machine then drop if strictly routed, 1198 * or do nothing if loosely routed. 1199 * Record interface address and bring up next address 1200 * component. If strictly routed make sure next 1201 * address is on directly accessible net. 1202 */ 1203 case IPOPT_LSRR: 1204 case IPOPT_SSRR: 1205 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1206 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1207 goto bad; 1208 } 1209 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1210 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1211 goto bad; 1212 } 1213 ipaddr.sin_addr = ip->ip_dst; 1214 ia = (struct in_ifaddr *) 1215 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1216 if (ia == 0) { 1217 if (opt == IPOPT_SSRR) { 1218 type = ICMP_UNREACH; 1219 code = ICMP_UNREACH_SRCFAIL; 1220 goto bad; 1221 } 1222 if (!ip_dosourceroute) 1223 goto nosourcerouting; 1224 /* 1225 * Loose routing, and not at next destination 1226 * yet; nothing to do except forward. 1227 */ 1228 break; 1229 } 1230 off--; /* 0 origin */ 1231 if (off > optlen - (int)sizeof(struct in_addr)) { 1232 /* 1233 * End of source route. Should be for us. 1234 */ 1235 if (!ip_acceptsourceroute) 1236 goto nosourcerouting; 1237 save_rte(cp, ip->ip_src); 1238 break; 1239 } 1240 1241 if (!ip_dosourceroute) { 1242 if (ipforwarding) { 1243 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1244 /* 1245 * Acting as a router, so generate ICMP 1246 */ 1247 nosourcerouting: 1248 strcpy(buf, inet_ntoa(ip->ip_dst)); 1249 log(LOG_WARNING, 1250 "attempted source route from %s to %s\n", 1251 inet_ntoa(ip->ip_src), buf); 1252 type = ICMP_UNREACH; 1253 code = ICMP_UNREACH_SRCFAIL; 1254 goto bad; 1255 } else { 1256 /* 1257 * Not acting as a router, so silently drop. 1258 */ 1259 ipstat.ips_cantforward++; 1260 m_freem(m); 1261 return (1); 1262 } 1263 } 1264 1265 /* 1266 * locate outgoing interface 1267 */ 1268 (void)memcpy(&ipaddr.sin_addr, cp + off, 1269 sizeof(ipaddr.sin_addr)); 1270 1271 if (opt == IPOPT_SSRR) { 1272 #define INA struct in_ifaddr * 1273 #define SA struct sockaddr * 1274 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 1275 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1276 } else 1277 ia = ip_rtaddr(ipaddr.sin_addr); 1278 if (ia == 0) { 1279 type = ICMP_UNREACH; 1280 code = ICMP_UNREACH_SRCFAIL; 1281 goto bad; 1282 } 1283 ip->ip_dst = ipaddr.sin_addr; 1284 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1285 sizeof(struct in_addr)); 1286 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1287 /* 1288 * Let ip_intr's mcast routing check handle mcast pkts 1289 */ 1290 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1291 break; 1292 1293 case IPOPT_RR: 1294 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1295 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1296 goto bad; 1297 } 1298 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1299 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1300 goto bad; 1301 } 1302 /* 1303 * If no space remains, ignore. 1304 */ 1305 off--; /* 0 origin */ 1306 if (off > optlen - (int)sizeof(struct in_addr)) 1307 break; 1308 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1309 sizeof(ipaddr.sin_addr)); 1310 /* 1311 * locate outgoing interface; if we're the destination, 1312 * use the incoming interface (should be same). 1313 */ 1314 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1315 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1316 type = ICMP_UNREACH; 1317 code = ICMP_UNREACH_HOST; 1318 goto bad; 1319 } 1320 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1321 sizeof(struct in_addr)); 1322 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1323 break; 1324 1325 case IPOPT_TS: 1326 code = cp - (u_char *)ip; 1327 if (optlen < 4 || optlen > 40) { 1328 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1329 goto bad; 1330 } 1331 if ((off = cp[IPOPT_OFFSET]) < 5) { 1332 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1333 goto bad; 1334 } 1335 if (off > optlen - (int)sizeof(int32_t)) { 1336 cp[IPOPT_OFFSET + 1] += (1 << 4); 1337 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) { 1338 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1339 goto bad; 1340 } 1341 break; 1342 } 1343 off--; /* 0 origin */ 1344 sin = (struct in_addr *)(cp + off); 1345 switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 1346 1347 case IPOPT_TS_TSONLY: 1348 break; 1349 1350 case IPOPT_TS_TSANDADDR: 1351 if (off + sizeof(n_time) + 1352 sizeof(struct in_addr) > optlen) { 1353 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1354 goto bad; 1355 } 1356 ipaddr.sin_addr = dst; 1357 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1358 m->m_pkthdr.rcvif); 1359 if (ia == 0) 1360 continue; 1361 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1362 sizeof(struct in_addr)); 1363 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1364 break; 1365 1366 case IPOPT_TS_PRESPEC: 1367 if (off + sizeof(n_time) + 1368 sizeof(struct in_addr) > optlen) { 1369 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1370 goto bad; 1371 } 1372 (void)memcpy(&ipaddr.sin_addr, sin, 1373 sizeof(struct in_addr)); 1374 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1375 continue; 1376 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1377 break; 1378 1379 default: 1380 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 1381 goto bad; 1382 } 1383 ntime = iptime(); 1384 (void)memcpy(cp + off, &ntime, sizeof(n_time)); 1385 cp[IPOPT_OFFSET] += sizeof(n_time); 1386 } 1387 } 1388 if (forward && ipforwarding) { 1389 ip_forward(m, 1); 1390 return (1); 1391 } 1392 return (0); 1393 bad: 1394 icmp_error(m, type, code, 0, 0); 1395 ipstat.ips_badoptions++; 1396 return (1); 1397 } 1398 1399 /* 1400 * Given address of next destination (final or next hop), 1401 * return internet address info of interface to be used to get there. 1402 */ 1403 static struct in_ifaddr * 1404 ip_rtaddr(dst) 1405 struct in_addr dst; 1406 { 1407 register struct sockaddr_in *sin; 1408 1409 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 1410 1411 if (ipforward_rt.ro_rt == 0 || 1412 !(ipforward_rt.ro_rt->rt_flags & RTF_UP) || 1413 dst.s_addr != sin->sin_addr.s_addr) { 1414 if (ipforward_rt.ro_rt) { 1415 RTFREE(ipforward_rt.ro_rt); 1416 ipforward_rt.ro_rt = 0; 1417 } 1418 sin->sin_family = AF_INET; 1419 sin->sin_len = sizeof(*sin); 1420 sin->sin_addr = dst; 1421 1422 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1423 } 1424 if (ipforward_rt.ro_rt == 0) 1425 return ((struct in_ifaddr *)0); 1426 return (ifatoia(ipforward_rt.ro_rt->rt_ifa)); 1427 } 1428 1429 /* 1430 * Save incoming source route for use in replies, 1431 * to be picked up later by ip_srcroute if the receiver is interested. 1432 */ 1433 void 1434 save_rte(option, dst) 1435 u_char *option; 1436 struct in_addr dst; 1437 { 1438 unsigned olen; 1439 1440 olen = option[IPOPT_OLEN]; 1441 #ifdef DIAGNOSTIC 1442 if (ipprintfs) 1443 printf("save_rte: olen %d\n", olen); 1444 #endif 1445 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1446 return; 1447 bcopy(option, ip_srcrt.srcopt, olen); 1448 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1449 ip_srcrt.dst = dst; 1450 } 1451 1452 /* 1453 * Retrieve incoming source route for use in replies, 1454 * in the same form used by setsockopt. 1455 * The first hop is placed before the options, will be removed later. 1456 */ 1457 struct mbuf * 1458 ip_srcroute() 1459 { 1460 register struct in_addr *p, *q; 1461 register struct mbuf *m; 1462 1463 if (ip_nhops == 0) 1464 return ((struct mbuf *)0); 1465 m = m_get(M_DONTWAIT, MT_HEADER); 1466 if (m == 0) 1467 return ((struct mbuf *)0); 1468 1469 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1470 1471 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1472 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1473 OPTSIZ; 1474 #ifdef DIAGNOSTIC 1475 if (ipprintfs) 1476 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1477 #endif 1478 1479 /* 1480 * First save first hop for return route 1481 */ 1482 p = &ip_srcrt.route[ip_nhops - 1]; 1483 *(mtod(m, struct in_addr *)) = *p--; 1484 #ifdef DIAGNOSTIC 1485 if (ipprintfs) 1486 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1487 #endif 1488 1489 /* 1490 * Copy option fields and padding (nop) to mbuf. 1491 */ 1492 ip_srcrt.nop = IPOPT_NOP; 1493 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1494 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1495 &ip_srcrt.nop, OPTSIZ); 1496 q = (struct in_addr *)(mtod(m, caddr_t) + 1497 sizeof(struct in_addr) + OPTSIZ); 1498 #undef OPTSIZ 1499 /* 1500 * Record return path as an IP source route, 1501 * reversing the path (pointers are now aligned). 1502 */ 1503 while (p >= ip_srcrt.route) { 1504 #ifdef DIAGNOSTIC 1505 if (ipprintfs) 1506 printf(" %lx", (u_long)ntohl(q->s_addr)); 1507 #endif 1508 *q++ = *p--; 1509 } 1510 /* 1511 * Last hop goes to final destination. 1512 */ 1513 *q = ip_srcrt.dst; 1514 #ifdef DIAGNOSTIC 1515 if (ipprintfs) 1516 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1517 #endif 1518 return (m); 1519 } 1520 1521 /* 1522 * Strip out IP options, at higher 1523 * level protocol in the kernel. 1524 * Second argument is buffer to which options 1525 * will be moved, and return value is their length. 1526 * XXX should be deleted; last arg currently ignored. 1527 */ 1528 void 1529 ip_stripoptions(m, mopt) 1530 register struct mbuf *m; 1531 struct mbuf *mopt; 1532 { 1533 register int i; 1534 struct ip *ip = mtod(m, struct ip *); 1535 register caddr_t opts; 1536 int olen; 1537 1538 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1539 opts = (caddr_t)(ip + 1); 1540 i = m->m_len - (sizeof (struct ip) + olen); 1541 bcopy(opts + olen, opts, (unsigned)i); 1542 m->m_len -= olen; 1543 if (m->m_flags & M_PKTHDR) 1544 m->m_pkthdr.len -= olen; 1545 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); 1546 } 1547 1548 u_char inetctlerrmap[PRC_NCMDS] = { 1549 0, 0, 0, 0, 1550 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1551 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1552 EMSGSIZE, EHOSTUNREACH, 0, 0, 1553 0, 0, 0, 0, 1554 ENOPROTOOPT, ECONNREFUSED 1555 }; 1556 1557 /* 1558 * Forward a packet. If some error occurs return the sender 1559 * an icmp packet. Note we can't always generate a meaningful 1560 * icmp message because icmp doesn't have a large enough repertoire 1561 * of codes and types. 1562 * 1563 * If not forwarding, just drop the packet. This could be confusing 1564 * if ipforwarding was zero but some routing protocol was advancing 1565 * us as a gateway to somewhere. However, we must let the routing 1566 * protocol deal with that. 1567 * 1568 * The srcrt parameter indicates whether the packet is being forwarded 1569 * via a source route. 1570 */ 1571 static void 1572 ip_forward(m, srcrt) 1573 struct mbuf *m; 1574 int srcrt; 1575 { 1576 register struct ip *ip = mtod(m, struct ip *); 1577 register struct rtentry *rt; 1578 int error, type = 0, code = 0; 1579 struct mbuf *mcopy; 1580 n_long dest; 1581 struct ifnet *destifp; 1582 #ifdef IPSEC 1583 struct ifnet dummyifp; 1584 #endif 1585 1586 dest = 0; 1587 #ifdef DIAGNOSTIC 1588 if (ipprintfs) 1589 printf("forward: src %lx dst %lx ttl %x\n", 1590 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr, 1591 ip->ip_ttl); 1592 #endif 1593 1594 1595 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1596 ipstat.ips_cantforward++; 1597 m_freem(m); 1598 return; 1599 } 1600 #ifdef IPSTEALTH 1601 if (!ipstealth) { 1602 #endif 1603 if (ip->ip_ttl <= IPTTLDEC) { 1604 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1605 dest, 0); 1606 return; 1607 } 1608 #ifdef IPSTEALTH 1609 } 1610 #endif 1611 1612 if (ip_rtaddr(ip->ip_dst) == 0) { 1613 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1614 return; 1615 } else 1616 rt = ipforward_rt.ro_rt; 1617 1618 /* 1619 * Save the IP header and at most 8 bytes of the payload, 1620 * in case we need to generate an ICMP message to the src. 1621 * 1622 * We don't use m_copy() because it might return a reference 1623 * to a shared cluster. Both this function and ip_output() 1624 * assume exclusive access to the IP header in `m', so any 1625 * data in a cluster may change before we reach icmp_error(). 1626 */ 1627 MGET(mcopy, M_DONTWAIT, m->m_type); 1628 if (mcopy != NULL) { 1629 M_COPY_PKTHDR(mcopy, m); 1630 mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8, 1631 (int)ip->ip_len); 1632 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1633 } 1634 1635 #ifdef IPSTEALTH 1636 if (!ipstealth) { 1637 #endif 1638 ip->ip_ttl -= IPTTLDEC; 1639 #ifdef IPSTEALTH 1640 } 1641 #endif 1642 1643 /* 1644 * If forwarding packet using same interface that it came in on, 1645 * perhaps should send a redirect to sender to shortcut a hop. 1646 * Only send redirect if source is sending directly to us, 1647 * and if packet was not source routed (or has any options). 1648 * Also, don't send redirect if forwarding using a default route 1649 * or a route modified by a redirect. 1650 */ 1651 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1652 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1653 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1654 ipsendredirects && !srcrt) { 1655 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1656 u_long src = ntohl(ip->ip_src.s_addr); 1657 1658 if (RTA(rt) && 1659 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1660 if (rt->rt_flags & RTF_GATEWAY) 1661 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1662 else 1663 dest = ip->ip_dst.s_addr; 1664 /* Router requirements says to only send host redirects */ 1665 type = ICMP_REDIRECT; 1666 code = ICMP_REDIRECT_HOST; 1667 #ifdef DIAGNOSTIC 1668 if (ipprintfs) 1669 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1670 #endif 1671 } 1672 } 1673 1674 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1675 IP_FORWARDING, 0); 1676 if (error) 1677 ipstat.ips_cantforward++; 1678 else { 1679 ipstat.ips_forward++; 1680 if (type) 1681 ipstat.ips_redirectsent++; 1682 else { 1683 if (mcopy) { 1684 ipflow_create(&ipforward_rt, mcopy); 1685 m_freem(mcopy); 1686 } 1687 return; 1688 } 1689 } 1690 if (mcopy == NULL) 1691 return; 1692 destifp = NULL; 1693 1694 switch (error) { 1695 1696 case 0: /* forwarded, but need redirect */ 1697 /* type, code set above */ 1698 break; 1699 1700 case ENETUNREACH: /* shouldn't happen, checked above */ 1701 case EHOSTUNREACH: 1702 case ENETDOWN: 1703 case EHOSTDOWN: 1704 default: 1705 type = ICMP_UNREACH; 1706 code = ICMP_UNREACH_HOST; 1707 break; 1708 1709 case EMSGSIZE: 1710 type = ICMP_UNREACH; 1711 code = ICMP_UNREACH_NEEDFRAG; 1712 #ifndef IPSEC 1713 if (ipforward_rt.ro_rt) 1714 destifp = ipforward_rt.ro_rt->rt_ifp; 1715 #else 1716 /* 1717 * If the packet is routed over IPsec tunnel, tell the 1718 * originator the tunnel MTU. 1719 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1720 * XXX quickhack!!! 1721 */ 1722 if (ipforward_rt.ro_rt) { 1723 struct secpolicy *sp = NULL; 1724 int ipsecerror; 1725 int ipsechdr; 1726 struct route *ro; 1727 1728 sp = ipsec4_getpolicybyaddr(mcopy, 1729 IPSEC_DIR_OUTBOUND, 1730 IP_FORWARDING, 1731 &ipsecerror); 1732 1733 if (sp == NULL) 1734 destifp = ipforward_rt.ro_rt->rt_ifp; 1735 else { 1736 /* count IPsec header size */ 1737 ipsechdr = ipsec4_hdrsiz(mcopy, 1738 IPSEC_DIR_OUTBOUND, 1739 NULL); 1740 1741 /* 1742 * find the correct route for outer IPv4 1743 * header, compute tunnel MTU. 1744 * 1745 * XXX BUG ALERT 1746 * The "dummyifp" code relies upon the fact 1747 * that icmp_error() touches only ifp->if_mtu. 1748 */ 1749 /*XXX*/ 1750 destifp = NULL; 1751 if (sp->req != NULL 1752 && sp->req->sav != NULL 1753 && sp->req->sav->sah != NULL) { 1754 ro = &sp->req->sav->sah->sa_route; 1755 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1756 dummyifp.if_mtu = 1757 ro->ro_rt->rt_ifp->if_mtu; 1758 dummyifp.if_mtu -= ipsechdr; 1759 destifp = &dummyifp; 1760 } 1761 } 1762 1763 key_freesp(sp); 1764 } 1765 } 1766 #endif /*IPSEC*/ 1767 ipstat.ips_cantfrag++; 1768 break; 1769 1770 case ENOBUFS: 1771 type = ICMP_SOURCEQUENCH; 1772 code = 0; 1773 break; 1774 1775 case EACCES: /* ipfw denied packet */ 1776 m_freem(mcopy); 1777 return; 1778 } 1779 icmp_error(mcopy, type, code, dest, destifp); 1780 } 1781 1782 void 1783 ip_savecontrol(inp, mp, ip, m) 1784 register struct inpcb *inp; 1785 register struct mbuf **mp; 1786 register struct ip *ip; 1787 register struct mbuf *m; 1788 { 1789 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1790 struct timeval tv; 1791 1792 microtime(&tv); 1793 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1794 SCM_TIMESTAMP, SOL_SOCKET); 1795 if (*mp) 1796 mp = &(*mp)->m_next; 1797 } 1798 if (inp->inp_flags & INP_RECVDSTADDR) { 1799 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1800 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1801 if (*mp) 1802 mp = &(*mp)->m_next; 1803 } 1804 #ifdef notyet 1805 /* XXX 1806 * Moving these out of udp_input() made them even more broken 1807 * than they already were. 1808 */ 1809 /* options were tossed already */ 1810 if (inp->inp_flags & INP_RECVOPTS) { 1811 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1812 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1813 if (*mp) 1814 mp = &(*mp)->m_next; 1815 } 1816 /* ip_srcroute doesn't do what we want here, need to fix */ 1817 if (inp->inp_flags & INP_RECVRETOPTS) { 1818 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1819 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1820 if (*mp) 1821 mp = &(*mp)->m_next; 1822 } 1823 #endif 1824 if (inp->inp_flags & INP_RECVIF) { 1825 struct ifnet *ifp; 1826 struct sdlbuf { 1827 struct sockaddr_dl sdl; 1828 u_char pad[32]; 1829 } sdlbuf; 1830 struct sockaddr_dl *sdp; 1831 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 1832 1833 if (((ifp = m->m_pkthdr.rcvif)) 1834 && ( ifp->if_index && (ifp->if_index <= if_index))) { 1835 sdp = (struct sockaddr_dl *) 1836 (ifaddr_byindex(ifp->if_index)->ifa_addr); 1837 /* 1838 * Change our mind and don't try copy. 1839 */ 1840 if ((sdp->sdl_family != AF_LINK) 1841 || (sdp->sdl_len > sizeof(sdlbuf))) { 1842 goto makedummy; 1843 } 1844 bcopy(sdp, sdl2, sdp->sdl_len); 1845 } else { 1846 makedummy: 1847 sdl2->sdl_len 1848 = offsetof(struct sockaddr_dl, sdl_data[0]); 1849 sdl2->sdl_family = AF_LINK; 1850 sdl2->sdl_index = 0; 1851 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1852 } 1853 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 1854 IP_RECVIF, IPPROTO_IP); 1855 if (*mp) 1856 mp = &(*mp)->m_next; 1857 } 1858 } 1859 1860 int 1861 ip_rsvp_init(struct socket *so) 1862 { 1863 if (so->so_type != SOCK_RAW || 1864 so->so_proto->pr_protocol != IPPROTO_RSVP) 1865 return EOPNOTSUPP; 1866 1867 if (ip_rsvpd != NULL) 1868 return EADDRINUSE; 1869 1870 ip_rsvpd = so; 1871 /* 1872 * This may seem silly, but we need to be sure we don't over-increment 1873 * the RSVP counter, in case something slips up. 1874 */ 1875 if (!ip_rsvp_on) { 1876 ip_rsvp_on = 1; 1877 rsvp_on++; 1878 } 1879 1880 return 0; 1881 } 1882 1883 int 1884 ip_rsvp_done(void) 1885 { 1886 ip_rsvpd = NULL; 1887 /* 1888 * This may seem silly, but we need to be sure we don't over-decrement 1889 * the RSVP counter, in case something slips up. 1890 */ 1891 if (ip_rsvp_on) { 1892 ip_rsvp_on = 0; 1893 rsvp_on--; 1894 } 1895 return 0; 1896 } 1897