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