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