ip_input.c revision 133069
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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 30 * $FreeBSD: head/sys/netinet/ip_input.c 133069 2004-08-03 12:31:38Z andre $ 31 */ 32 33#include "opt_bootp.h" 34#include "opt_ipfw.h" 35#include "opt_ipdn.h" 36#include "opt_ipdivert.h" 37#include "opt_ipfilter.h" 38#include "opt_ipstealth.h" 39#include "opt_ipsec.h" 40#include "opt_mac.h" 41#include "opt_pfil_hooks.h" 42#include "opt_random_ip_id.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/mac.h> 47#include <sys/mbuf.h> 48#include <sys/malloc.h> 49#include <sys/domain.h> 50#include <sys/protosw.h> 51#include <sys/socket.h> 52#include <sys/time.h> 53#include <sys/kernel.h> 54#include <sys/syslog.h> 55#include <sys/sysctl.h> 56 57#include <net/pfil.h> 58#include <net/if.h> 59#include <net/if_types.h> 60#include <net/if_var.h> 61#include <net/if_dl.h> 62#include <net/route.h> 63#include <net/netisr.h> 64 65#include <netinet/in.h> 66#include <netinet/in_systm.h> 67#include <netinet/in_var.h> 68#include <netinet/ip.h> 69#include <netinet/in_pcb.h> 70#include <netinet/ip_var.h> 71#include <netinet/ip_icmp.h> 72#include <machine/in_cksum.h> 73 74#include <sys/socketvar.h> 75 76#include <netinet/ip_fw.h> 77#include <netinet/ip_divert.h> 78#include <netinet/ip_dummynet.h> 79 80#ifdef IPSEC 81#include <netinet6/ipsec.h> 82#include <netkey/key.h> 83#endif 84 85#ifdef FAST_IPSEC 86#include <netipsec/ipsec.h> 87#include <netipsec/key.h> 88#endif 89 90int rsvp_on = 0; 91 92int ipforwarding = 0; 93SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 94 &ipforwarding, 0, "Enable IP forwarding between interfaces"); 95 96static int ipsendredirects = 1; /* XXX */ 97SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 98 &ipsendredirects, 0, "Enable sending IP redirects"); 99 100int ip_defttl = IPDEFTTL; 101SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 102 &ip_defttl, 0, "Maximum TTL on IP packets"); 103 104static int ip_dosourceroute = 0; 105SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 106 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); 107 108static int ip_acceptsourceroute = 0; 109SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 110 CTLFLAG_RW, &ip_acceptsourceroute, 0, 111 "Enable accepting source routed IP packets"); 112 113int ip_doopts = 1; /* 0 = ignore, 1 = process, 2 = reject */ 114SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW, 115 &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)"); 116 117static int ip_keepfaith = 0; 118SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, 119 &ip_keepfaith, 0, 120 "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 121 122static int nipq = 0; /* total # of reass queues */ 123static int maxnipq; 124SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW, 125 &maxnipq, 0, 126 "Maximum number of IPv4 fragment reassembly queue entries"); 127 128static int maxfragsperpacket; 129SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW, 130 &maxfragsperpacket, 0, 131 "Maximum number of IPv4 fragments allowed per packet"); 132 133static int ip_sendsourcequench = 0; 134SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW, 135 &ip_sendsourcequench, 0, 136 "Enable the transmission of source quench packets"); 137 138/* 139 * XXX - Setting ip_checkinterface mostly implements the receive side of 140 * the Strong ES model described in RFC 1122, but since the routing table 141 * and transmit implementation do not implement the Strong ES model, 142 * setting this to 1 results in an odd hybrid. 143 * 144 * XXX - ip_checkinterface currently must be disabled if you use ipnat 145 * to translate the destination address to another local interface. 146 * 147 * XXX - ip_checkinterface must be disabled if you add IP aliases 148 * to the loopback interface instead of the interface where the 149 * packets for those addresses are received. 150 */ 151static int ip_checkinterface = 1; 152SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW, 153 &ip_checkinterface, 0, "Verify packet arrives on correct interface"); 154 155#ifdef DIAGNOSTIC 156static int ipprintfs = 0; 157#endif 158#ifdef PFIL_HOOKS 159struct pfil_head inet_pfil_hook; 160#endif 161 162static struct ifqueue ipintrq; 163static int ipqmaxlen = IFQ_MAXLEN; 164 165extern struct domain inetdomain; 166extern struct protosw inetsw[]; 167u_char ip_protox[IPPROTO_MAX]; 168struct in_ifaddrhead in_ifaddrhead; /* first inet address */ 169struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */ 170u_long in_ifaddrhmask; /* mask for hash table */ 171 172SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW, 173 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue"); 174SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, 175 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue"); 176 177struct ipstat ipstat; 178SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW, 179 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)"); 180 181/* Packet reassembly stuff */ 182#define IPREASS_NHASH_LOG2 6 183#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) 184#define IPREASS_HMASK (IPREASS_NHASH - 1) 185#define IPREASS_HASH(x,y) \ 186 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) 187 188static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH]; 189struct mtx ipqlock; 190 191#define IPQ_LOCK() mtx_lock(&ipqlock) 192#define IPQ_UNLOCK() mtx_unlock(&ipqlock) 193#define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF) 194#define IPQ_LOCK_ASSERT() mtx_assert(&ipqlock, MA_OWNED) 195 196#ifdef IPCTL_DEFMTU 197SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 198 &ip_mtu, 0, "Default MTU"); 199#endif 200 201#ifdef IPSTEALTH 202int ipstealth = 0; 203SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 204 &ipstealth, 0, ""); 205#endif 206 207 208/* Firewall hooks */ 209ip_fw_chk_t *ip_fw_chk_ptr; 210int fw_enable = 1 ; 211int fw_one_pass = 1; 212 213/* Dummynet hooks */ 214ip_dn_io_t *ip_dn_io_ptr; 215 216/* 217 * XXX this is ugly -- the following two global variables are 218 * used to store packet state while it travels through the stack. 219 * Note that the code even makes assumptions on the size and 220 * alignment of fields inside struct ip_srcrt so e.g. adding some 221 * fields will break the code. This needs to be fixed. 222 * 223 * We need to save the IP options in case a protocol wants to respond 224 * to an incoming packet over the same route if the packet got here 225 * using IP source routing. This allows connection establishment and 226 * maintenance when the remote end is on a network that is not known 227 * to us. 228 */ 229static int ip_nhops = 0; 230static struct ip_srcrt { 231 struct in_addr dst; /* final destination */ 232 char nop; /* one NOP to align */ 233 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 234 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 235} ip_srcrt; 236 237static void save_rte(u_char *, struct in_addr); 238static int ip_dooptions(struct mbuf *m, int, 239 struct sockaddr_in *next_hop); 240static void ip_forward(struct mbuf *m, int srcrt, 241 struct sockaddr_in *next_hop); 242static void ip_freef(struct ipqhead *, struct ipq *); 243static struct mbuf *ip_reass(struct mbuf *); 244 245/* 246 * IP initialization: fill in IP protocol switch table. 247 * All protocols not implemented in kernel go to raw IP protocol handler. 248 */ 249void 250ip_init() 251{ 252 register struct protosw *pr; 253 register int i; 254 255 TAILQ_INIT(&in_ifaddrhead); 256 in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask); 257 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 258 if (pr == 0) 259 panic("ip_init"); 260 for (i = 0; i < IPPROTO_MAX; i++) 261 ip_protox[i] = pr - inetsw; 262 for (pr = inetdomain.dom_protosw; 263 pr < inetdomain.dom_protoswNPROTOSW; pr++) 264 if (pr->pr_domain->dom_family == PF_INET && 265 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 266 ip_protox[pr->pr_protocol] = pr - inetsw; 267 268#ifdef PFIL_HOOKS 269 inet_pfil_hook.ph_type = PFIL_TYPE_AF; 270 inet_pfil_hook.ph_af = AF_INET; 271 if ((i = pfil_head_register(&inet_pfil_hook)) != 0) 272 printf("%s: WARNING: unable to register pfil hook, " 273 "error %d\n", __func__, i); 274#endif /* PFIL_HOOKS */ 275 276 IPQ_LOCK_INIT(); 277 for (i = 0; i < IPREASS_NHASH; i++) 278 TAILQ_INIT(&ipq[i]); 279 280 maxnipq = nmbclusters / 32; 281 maxfragsperpacket = 16; 282 283#ifndef RANDOM_IP_ID 284 ip_id = time_second & 0xffff; 285#endif 286 ipintrq.ifq_maxlen = ipqmaxlen; 287 mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF); 288 netisr_register(NETISR_IP, ip_input, &ipintrq, NETISR_MPSAFE); 289} 290 291/* 292 * Ip input routine. Checksum and byte swap header. If fragmented 293 * try to reassemble. Process options. Pass to next level. 294 */ 295void 296ip_input(struct mbuf *m) 297{ 298 struct ip *ip = NULL; 299 struct in_ifaddr *ia = NULL; 300 struct ifaddr *ifa; 301 int i, checkif, hlen = 0; 302 u_short sum; 303 struct in_addr pkt_dst; 304#ifdef IPDIVERT 305 u_int32_t divert_info; /* packet divert/tee info */ 306#endif 307 struct ip_fw_args args; 308 int dchg = 0; /* dest changed after fw */ 309#ifdef PFIL_HOOKS 310 struct in_addr odst; /* original dst address */ 311#endif 312#ifdef FAST_IPSEC 313 struct m_tag *mtag; 314 struct tdb_ident *tdbi; 315 struct secpolicy *sp; 316 int s, error; 317#endif /* FAST_IPSEC */ 318 319 args.eh = NULL; 320 args.oif = NULL; 321 322 M_ASSERTPKTHDR(m); 323 324 args.next_hop = m_claim_next(m, PACKET_TAG_IPFORWARD); 325 args.rule = ip_dn_claim_rule(m); 326 327 if (m->m_flags & M_FASTFWD_OURS) { 328 /* ip_fastforward firewall changed dest to local */ 329 m->m_flags &= ~M_FASTFWD_OURS; /* for reflected mbufs */ 330 goto ours; 331 } 332 333 if (args.rule) { /* dummynet already filtered us */ 334 ip = mtod(m, struct ip *); 335 hlen = ip->ip_hl << 2; 336 goto iphack ; 337 } 338 339 ipstat.ips_total++; 340 341 if (m->m_pkthdr.len < sizeof(struct ip)) 342 goto tooshort; 343 344 if (m->m_len < sizeof (struct ip) && 345 (m = m_pullup(m, sizeof (struct ip))) == 0) { 346 ipstat.ips_toosmall++; 347 return; 348 } 349 ip = mtod(m, struct ip *); 350 351 if (ip->ip_v != IPVERSION) { 352 ipstat.ips_badvers++; 353 goto bad; 354 } 355 356 hlen = ip->ip_hl << 2; 357 if (hlen < sizeof(struct ip)) { /* minimum header length */ 358 ipstat.ips_badhlen++; 359 goto bad; 360 } 361 if (hlen > m->m_len) { 362 if ((m = m_pullup(m, hlen)) == 0) { 363 ipstat.ips_badhlen++; 364 return; 365 } 366 ip = mtod(m, struct ip *); 367 } 368 369 /* 127/8 must not appear on wire - RFC1122 */ 370 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 371 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 372 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 373 ipstat.ips_badaddr++; 374 goto bad; 375 } 376 } 377 378 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 379 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 380 } else { 381 if (hlen == sizeof(struct ip)) { 382 sum = in_cksum_hdr(ip); 383 } else { 384 sum = in_cksum(m, hlen); 385 } 386 } 387 if (sum) { 388 ipstat.ips_badsum++; 389 goto bad; 390 } 391 392#ifdef ALTQ 393 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 394 /* packet is dropped by traffic conditioner */ 395 return; 396#endif 397 398 /* 399 * Convert fields to host representation. 400 */ 401 ip->ip_len = ntohs(ip->ip_len); 402 if (ip->ip_len < hlen) { 403 ipstat.ips_badlen++; 404 goto bad; 405 } 406 ip->ip_off = ntohs(ip->ip_off); 407 408 /* 409 * Check that the amount of data in the buffers 410 * is as at least much as the IP header would have us expect. 411 * Trim mbufs if longer than we expect. 412 * Drop packet if shorter than we expect. 413 */ 414 if (m->m_pkthdr.len < ip->ip_len) { 415tooshort: 416 ipstat.ips_tooshort++; 417 goto bad; 418 } 419 if (m->m_pkthdr.len > ip->ip_len) { 420 if (m->m_len == m->m_pkthdr.len) { 421 m->m_len = ip->ip_len; 422 m->m_pkthdr.len = ip->ip_len; 423 } else 424 m_adj(m, ip->ip_len - m->m_pkthdr.len); 425 } 426#if defined(IPSEC) && !defined(IPSEC_FILTERGIF) 427 /* 428 * Bypass packet filtering for packets from a tunnel (gif). 429 */ 430 if (ipsec_getnhist(m)) 431 goto pass; 432#endif 433#if defined(FAST_IPSEC) && !defined(IPSEC_FILTERGIF) 434 /* 435 * Bypass packet filtering for packets from a tunnel (gif). 436 */ 437 if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL) 438 goto pass; 439#endif 440 441 /* 442 * IpHack's section. 443 * Right now when no processing on packet has done 444 * and it is still fresh out of network we do our black 445 * deals with it. 446 * - Firewall: deny/allow/divert 447 * - Xlate: translate packet's addr/port (NAT). 448 * - Pipe: pass pkt through dummynet. 449 * - Wrap: fake packet's addr/port <unimpl.> 450 * - Encapsulate: put it in another IP and send out. <unimp.> 451 */ 452 453iphack: 454 455#ifdef PFIL_HOOKS 456 /* 457 * Run through list of hooks for input packets. 458 * 459 * NB: Beware of the destination address changing (e.g. 460 * by NAT rewriting). When this happens, tell 461 * ip_forward to do the right thing. 462 */ 463 odst = ip->ip_dst; 464 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, 465 PFIL_IN) != 0) 466 return; 467 if (m == NULL) /* consumed by filter */ 468 return; 469 ip = mtod(m, struct ip *); 470 dchg = (odst.s_addr != ip->ip_dst.s_addr); 471#endif /* PFIL_HOOKS */ 472 473 if (fw_enable && IPFW_LOADED) { 474 /* 475 * If we've been forwarded from the output side, then 476 * skip the firewall a second time 477 */ 478 if (args.next_hop) 479 goto ours; 480 481 args.m = m; 482 i = ip_fw_chk_ptr(&args); 483 m = args.m; 484 485 if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) { /* drop */ 486 if (m) 487 m_freem(m); 488 return; 489 } 490 ip = mtod(m, struct ip *); /* just in case m changed */ 491 if (i == 0 && args.next_hop == NULL) /* common case */ 492 goto pass; 493 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG) != 0) { 494 /* Send packet to the appropriate pipe */ 495 ip_dn_io_ptr(m, i&0xffff, DN_TO_IP_IN, &args); 496 return; 497 } 498#ifdef IPDIVERT 499 if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) { 500 /* Divert or tee packet */ 501 goto ours; 502 } 503#endif 504 if (i == 0 && args.next_hop != NULL) 505 goto pass; 506 /* 507 * if we get here, the packet must be dropped 508 */ 509 m_freem(m); 510 return; 511 } 512pass: 513 514 /* 515 * Process options and, if not destined for us, 516 * ship it on. ip_dooptions returns 1 when an 517 * error was detected (causing an icmp message 518 * to be sent and the original packet to be freed). 519 */ 520 ip_nhops = 0; /* for source routed packets */ 521 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0, args.next_hop)) 522 return; 523 524 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 525 * matter if it is destined to another node, or whether it is 526 * a multicast one, RSVP wants it! and prevents it from being forwarded 527 * anywhere else. Also checks if the rsvp daemon is running before 528 * grabbing the packet. 529 */ 530 if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 531 goto ours; 532 533 /* 534 * Check our list of addresses, to see if the packet is for us. 535 * If we don't have any addresses, assume any unicast packet 536 * we receive might be for us (and let the upper layers deal 537 * with it). 538 */ 539 if (TAILQ_EMPTY(&in_ifaddrhead) && 540 (m->m_flags & (M_MCAST|M_BCAST)) == 0) 541 goto ours; 542 543 /* 544 * Cache the destination address of the packet; this may be 545 * changed by use of 'ipfw fwd'. 546 */ 547 pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst; 548 549 /* 550 * Enable a consistency check between the destination address 551 * and the arrival interface for a unicast packet (the RFC 1122 552 * strong ES model) if IP forwarding is disabled and the packet 553 * is not locally generated and the packet is not subject to 554 * 'ipfw fwd'. 555 * 556 * XXX - Checking also should be disabled if the destination 557 * address is ipnat'ed to a different interface. 558 * 559 * XXX - Checking is incompatible with IP aliases added 560 * to the loopback interface instead of the interface where 561 * the packets are received. 562 */ 563 checkif = ip_checkinterface && (ipforwarding == 0) && 564 m->m_pkthdr.rcvif != NULL && 565 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) && 566 (args.next_hop == NULL) && (dchg == 0); 567 568 /* 569 * Check for exact addresses in the hash bucket. 570 */ 571 LIST_FOREACH(ia, INADDR_HASH(pkt_dst.s_addr), ia_hash) { 572 /* 573 * If the address matches, verify that the packet 574 * arrived via the correct interface if checking is 575 * enabled. 576 */ 577 if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr && 578 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) 579 goto ours; 580 } 581 /* 582 * Check for broadcast addresses. 583 * 584 * Only accept broadcast packets that arrive via the matching 585 * interface. Reception of forwarded directed broadcasts would 586 * be handled via ip_forward() and ether_output() with the loopback 587 * into the stack for SIMPLEX interfaces handled by ether_output(). 588 */ 589 if (m->m_pkthdr.rcvif != NULL && 590 m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 591 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 592 if (ifa->ifa_addr->sa_family != AF_INET) 593 continue; 594 ia = ifatoia(ifa); 595 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 596 pkt_dst.s_addr) 597 goto ours; 598 if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr) 599 goto ours; 600#ifdef BOOTP_COMPAT 601 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 602 goto ours; 603#endif 604 } 605 } 606 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 607 struct in_multi *inm; 608 if (ip_mrouter) { 609 /* 610 * If we are acting as a multicast router, all 611 * incoming multicast packets are passed to the 612 * kernel-level multicast forwarding function. 613 * The packet is returned (relatively) intact; if 614 * ip_mforward() returns a non-zero value, the packet 615 * must be discarded, else it may be accepted below. 616 */ 617 if (ip_mforward && 618 ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 619 ipstat.ips_cantforward++; 620 m_freem(m); 621 return; 622 } 623 624 /* 625 * The process-level routing daemon needs to receive 626 * all multicast IGMP packets, whether or not this 627 * host belongs to their destination groups. 628 */ 629 if (ip->ip_p == IPPROTO_IGMP) 630 goto ours; 631 ipstat.ips_forward++; 632 } 633 /* 634 * See if we belong to the destination multicast group on the 635 * arrival interface. 636 */ 637 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 638 if (inm == NULL) { 639 ipstat.ips_notmember++; 640 m_freem(m); 641 return; 642 } 643 goto ours; 644 } 645 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 646 goto ours; 647 if (ip->ip_dst.s_addr == INADDR_ANY) 648 goto ours; 649 650 /* 651 * FAITH(Firewall Aided Internet Translator) 652 */ 653 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 654 if (ip_keepfaith) { 655 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 656 goto ours; 657 } 658 m_freem(m); 659 return; 660 } 661 662 /* 663 * Not for us; forward if possible and desirable. 664 */ 665 if (ipforwarding == 0) { 666 ipstat.ips_cantforward++; 667 m_freem(m); 668 } else { 669#ifdef IPSEC 670 /* 671 * Enforce inbound IPsec SPD. 672 */ 673 if (ipsec4_in_reject(m, NULL)) { 674 ipsecstat.in_polvio++; 675 goto bad; 676 } 677#endif /* IPSEC */ 678#ifdef FAST_IPSEC 679 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 680 s = splnet(); 681 if (mtag != NULL) { 682 tdbi = (struct tdb_ident *)(mtag + 1); 683 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 684 } else { 685 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 686 IP_FORWARDING, &error); 687 } 688 if (sp == NULL) { /* NB: can happen if error */ 689 splx(s); 690 /*XXX error stat???*/ 691 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/ 692 goto bad; 693 } 694 695 /* 696 * Check security policy against packet attributes. 697 */ 698 error = ipsec_in_reject(sp, m); 699 KEY_FREESP(&sp); 700 splx(s); 701 if (error) { 702 ipstat.ips_cantforward++; 703 goto bad; 704 } 705#endif /* FAST_IPSEC */ 706 ip_forward(m, dchg, args.next_hop); 707 } 708 return; 709 710ours: 711#ifdef IPSTEALTH 712 /* 713 * IPSTEALTH: Process non-routing options only 714 * if the packet is destined for us. 715 */ 716 if (ipstealth && hlen > sizeof (struct ip) && 717 ip_dooptions(m, 1, args.next_hop)) 718 return; 719#endif /* IPSTEALTH */ 720 721 /* Count the packet in the ip address stats */ 722 if (ia != NULL) { 723 ia->ia_ifa.if_ipackets++; 724 ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 725 } 726 727 /* 728 * Attempt reassembly; if it succeeds, proceed. 729 * ip_reass() will return a different mbuf. 730 */ 731 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 732 m = ip_reass(m); 733 if (m == NULL) 734 return; 735 ip = mtod(m, struct ip *); 736 /* Get the header length of the reassembled packet */ 737 hlen = ip->ip_hl << 2; 738#ifdef IPDIVERT 739 /* Restore original checksum before diverting packet */ 740 if (divert_find_info(m) != 0) { 741 ip->ip_len = htons(ip->ip_len); 742 ip->ip_off = htons(ip->ip_off); 743 ip->ip_sum = 0; 744 if (hlen == sizeof(struct ip)) 745 ip->ip_sum = in_cksum_hdr(ip); 746 else 747 ip->ip_sum = in_cksum(m, hlen); 748 ip->ip_off = ntohs(ip->ip_off); 749 ip->ip_len = ntohs(ip->ip_len); 750 } 751#endif 752 } 753 754 /* 755 * Further protocols expect the packet length to be w/o the 756 * IP header. 757 */ 758 ip->ip_len -= hlen; 759 760#ifdef IPDIVERT 761 /* 762 * Divert or tee packet to the divert protocol if required. 763 */ 764 divert_info = divert_find_info(m); 765 if (divert_info != 0) { 766 struct mbuf *clone; 767 768 /* Clone packet if we're doing a 'tee' */ 769 if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0) 770 clone = divert_clone(m); 771 else 772 clone = NULL; 773 774 /* Restore packet header fields to original values */ 775 ip->ip_len += hlen; 776 ip->ip_len = htons(ip->ip_len); 777 ip->ip_off = htons(ip->ip_off); 778 779 /* Deliver packet to divert input routine */ 780 divert_packet(m, 1); 781 ipstat.ips_delivered++; 782 783 /* If 'tee', continue with original packet */ 784 if (clone == NULL) 785 return; 786 m = clone; 787 ip = mtod(m, struct ip *); 788 ip->ip_len += hlen; 789 /* 790 * Jump backwards to complete processing of the 791 * packet. We do not need to clear args.next_hop 792 * as that will not be used again and the cloned packet 793 * doesn't contain a divert packet tag so we won't 794 * re-entry this block. 795 */ 796 goto pass; 797 } 798#endif 799 800#ifdef IPSEC 801 /* 802 * enforce IPsec policy checking if we are seeing last header. 803 * note that we do not visit this with protocols with pcb layer 804 * code - like udp/tcp/raw ip. 805 */ 806 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 && 807 ipsec4_in_reject(m, NULL)) { 808 ipsecstat.in_polvio++; 809 goto bad; 810 } 811#endif 812#if FAST_IPSEC 813 /* 814 * enforce IPsec policy checking if we are seeing last header. 815 * note that we do not visit this with protocols with pcb layer 816 * code - like udp/tcp/raw ip. 817 */ 818 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) { 819 /* 820 * Check if the packet has already had IPsec processing 821 * done. If so, then just pass it along. This tag gets 822 * set during AH, ESP, etc. input handling, before the 823 * packet is returned to the ip input queue for delivery. 824 */ 825 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 826 s = splnet(); 827 if (mtag != NULL) { 828 tdbi = (struct tdb_ident *)(mtag + 1); 829 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 830 } else { 831 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 832 IP_FORWARDING, &error); 833 } 834 if (sp != NULL) { 835 /* 836 * Check security policy against packet attributes. 837 */ 838 error = ipsec_in_reject(sp, m); 839 KEY_FREESP(&sp); 840 } else { 841 /* XXX error stat??? */ 842 error = EINVAL; 843DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/ 844 goto bad; 845 } 846 splx(s); 847 if (error) 848 goto bad; 849 } 850#endif /* FAST_IPSEC */ 851 852 /* 853 * Switch out to protocol's input routine. 854 */ 855 ipstat.ips_delivered++; 856 if (args.next_hop && ip->ip_p == IPPROTO_TCP) { 857 /* attach next hop info for TCP */ 858 struct m_tag *mtag = m_tag_get(PACKET_TAG_IPFORWARD, 859 sizeof(struct sockaddr_in *), M_NOWAIT); 860 if (mtag == NULL) 861 goto bad; 862 *(struct sockaddr_in **)(mtag+1) = args.next_hop; 863 m_tag_prepend(m, mtag); 864 } 865 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 866 return; 867bad: 868 m_freem(m); 869} 870 871/* 872 * Take incoming datagram fragment and try to reassemble it into 873 * whole datagram. If the argument is the first fragment or one 874 * in between the function will return NULL and store the mbuf 875 * in the fragment chain. If the argument is the last fragment 876 * the packet will be reassembled and the pointer to the new 877 * mbuf returned for further processing. Only m_tags attached 878 * to the first packet/fragment are preserved. 879 * The IP header is *NOT* adjusted out of iplen. 880 */ 881 882struct mbuf * 883ip_reass(struct mbuf *m) 884{ 885 struct ip *ip; 886 struct mbuf *p, *q, *nq, *t; 887 struct ipq *fp = NULL; 888 struct ipqhead *head; 889 int i, hlen, next; 890 u_int8_t ecn, ecn0; 891 u_short hash; 892 893 /* If maxnipq is 0, never accept fragments. */ 894 if (maxnipq == 0) { 895 ipstat.ips_fragments++; 896 ipstat.ips_fragdropped++; 897 goto dropfrag; 898 } 899 900 ip = mtod(m, struct ip *); 901 hlen = ip->ip_hl << 2; 902 903 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 904 head = &ipq[hash]; 905 IPQ_LOCK(); 906 907 /* 908 * Look for queue of fragments 909 * of this datagram. 910 */ 911 TAILQ_FOREACH(fp, head, ipq_list) 912 if (ip->ip_id == fp->ipq_id && 913 ip->ip_src.s_addr == fp->ipq_src.s_addr && 914 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 915#ifdef MAC 916 mac_fragment_match(m, fp) && 917#endif 918 ip->ip_p == fp->ipq_p) 919 goto found; 920 921 fp = NULL; 922 923 /* 924 * Enforce upper bound on number of fragmented packets 925 * for which we attempt reassembly; 926 * If maxnipq is -1, accept all fragments without limitation. 927 */ 928 if ((nipq > maxnipq) && (maxnipq > 0)) { 929 /* 930 * drop something from the tail of the current queue 931 * before proceeding further 932 */ 933 struct ipq *q = TAILQ_LAST(head, ipqhead); 934 if (q == NULL) { /* gak */ 935 for (i = 0; i < IPREASS_NHASH; i++) { 936 struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead); 937 if (r) { 938 ipstat.ips_fragtimeout += r->ipq_nfrags; 939 ip_freef(&ipq[i], r); 940 break; 941 } 942 } 943 } else { 944 ipstat.ips_fragtimeout += q->ipq_nfrags; 945 ip_freef(head, q); 946 } 947 } 948 949found: 950 /* 951 * Adjust ip_len to not reflect header, 952 * convert offset of this to bytes. 953 */ 954 ip->ip_len -= hlen; 955 if (ip->ip_off & IP_MF) { 956 /* 957 * Make sure that fragments have a data length 958 * that's a non-zero multiple of 8 bytes. 959 */ 960 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 961 IPQ_UNLOCK(); 962 ipstat.ips_toosmall++; /* XXX */ 963 goto dropfrag; 964 } 965 m->m_flags |= M_FRAG; 966 } else 967 m->m_flags &= ~M_FRAG; 968 ip->ip_off <<= 3; 969 970 971 /* 972 * Attempt reassembly; if it succeeds, proceed. 973 * ip_reass() will return a different mbuf. 974 */ 975 ipstat.ips_fragments++; 976 m->m_pkthdr.header = ip; 977 978 /* Previous ip_reass() started here. */ 979 /* 980 * Presence of header sizes in mbufs 981 * would confuse code below. 982 */ 983 m->m_data += hlen; 984 m->m_len -= hlen; 985 986 /* 987 * If first fragment to arrive, create a reassembly queue. 988 */ 989 if (fp == NULL) { 990 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 991 goto dropfrag; 992 fp = mtod(t, struct ipq *); 993#ifdef MAC 994 if (mac_init_ipq(fp, M_NOWAIT) != 0) { 995 m_free(t); 996 goto dropfrag; 997 } 998 mac_create_ipq(m, fp); 999#endif 1000 TAILQ_INSERT_HEAD(head, fp, ipq_list); 1001 nipq++; 1002 fp->ipq_nfrags = 1; 1003 fp->ipq_ttl = IPFRAGTTL; 1004 fp->ipq_p = ip->ip_p; 1005 fp->ipq_id = ip->ip_id; 1006 fp->ipq_src = ip->ip_src; 1007 fp->ipq_dst = ip->ip_dst; 1008 fp->ipq_frags = m; 1009 m->m_nextpkt = NULL; 1010 goto inserted; 1011 } else { 1012 fp->ipq_nfrags++; 1013#ifdef MAC 1014 mac_update_ipq(m, fp); 1015#endif 1016 } 1017 1018#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 1019 1020 /* 1021 * Handle ECN by comparing this segment with the first one; 1022 * if CE is set, do not lose CE. 1023 * drop if CE and not-ECT are mixed for the same packet. 1024 */ 1025 ecn = ip->ip_tos & IPTOS_ECN_MASK; 1026 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 1027 if (ecn == IPTOS_ECN_CE) { 1028 if (ecn0 == IPTOS_ECN_NOTECT) 1029 goto dropfrag; 1030 if (ecn0 != IPTOS_ECN_CE) 1031 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 1032 } 1033 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 1034 goto dropfrag; 1035 1036 /* 1037 * Find a segment which begins after this one does. 1038 */ 1039 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 1040 if (GETIP(q)->ip_off > ip->ip_off) 1041 break; 1042 1043 /* 1044 * If there is a preceding segment, it may provide some of 1045 * our data already. If so, drop the data from the incoming 1046 * segment. If it provides all of our data, drop us, otherwise 1047 * stick new segment in the proper place. 1048 * 1049 * If some of the data is dropped from the the preceding 1050 * segment, then it's checksum is invalidated. 1051 */ 1052 if (p) { 1053 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 1054 if (i > 0) { 1055 if (i >= ip->ip_len) 1056 goto dropfrag; 1057 m_adj(m, i); 1058 m->m_pkthdr.csum_flags = 0; 1059 ip->ip_off += i; 1060 ip->ip_len -= i; 1061 } 1062 m->m_nextpkt = p->m_nextpkt; 1063 p->m_nextpkt = m; 1064 } else { 1065 m->m_nextpkt = fp->ipq_frags; 1066 fp->ipq_frags = m; 1067 } 1068 1069 /* 1070 * While we overlap succeeding segments trim them or, 1071 * if they are completely covered, dequeue them. 1072 */ 1073 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 1074 q = nq) { 1075 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off; 1076 if (i < GETIP(q)->ip_len) { 1077 GETIP(q)->ip_len -= i; 1078 GETIP(q)->ip_off += i; 1079 m_adj(q, i); 1080 q->m_pkthdr.csum_flags = 0; 1081 break; 1082 } 1083 nq = q->m_nextpkt; 1084 m->m_nextpkt = nq; 1085 ipstat.ips_fragdropped++; 1086 fp->ipq_nfrags--; 1087 m_freem(q); 1088 } 1089 1090inserted: 1091 1092#ifdef IPDIVERT 1093 if (ip->ip_off != 0) { 1094 /* 1095 * Strip any divert information; only the info 1096 * on the first fragment is used/kept. 1097 */ 1098 struct m_tag *mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 1099 if (mtag) 1100 m_tag_delete(m, mtag); 1101 } 1102#endif 1103 1104 /* 1105 * Check for complete reassembly and perform frag per packet 1106 * limiting. 1107 * 1108 * Frag limiting is performed here so that the nth frag has 1109 * a chance to complete the packet before we drop the packet. 1110 * As a result, n+1 frags are actually allowed per packet, but 1111 * only n will ever be stored. (n = maxfragsperpacket.) 1112 * 1113 */ 1114 next = 0; 1115 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1116 if (GETIP(q)->ip_off != next) { 1117 if (fp->ipq_nfrags > maxfragsperpacket) { 1118 ipstat.ips_fragdropped += fp->ipq_nfrags; 1119 ip_freef(head, fp); 1120 } 1121 goto done; 1122 } 1123 next += GETIP(q)->ip_len; 1124 } 1125 /* Make sure the last packet didn't have the IP_MF flag */ 1126 if (p->m_flags & M_FRAG) { 1127 if (fp->ipq_nfrags > maxfragsperpacket) { 1128 ipstat.ips_fragdropped += fp->ipq_nfrags; 1129 ip_freef(head, fp); 1130 } 1131 goto done; 1132 } 1133 1134 /* 1135 * Reassembly is complete. Make sure the packet is a sane size. 1136 */ 1137 q = fp->ipq_frags; 1138 ip = GETIP(q); 1139 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 1140 ipstat.ips_toolong++; 1141 ipstat.ips_fragdropped += fp->ipq_nfrags; 1142 ip_freef(head, fp); 1143 goto done; 1144 } 1145 1146 /* 1147 * Concatenate fragments. 1148 */ 1149 m = q; 1150 t = m->m_next; 1151 m->m_next = 0; 1152 m_cat(m, t); 1153 nq = q->m_nextpkt; 1154 q->m_nextpkt = 0; 1155 for (q = nq; q != NULL; q = nq) { 1156 nq = q->m_nextpkt; 1157 q->m_nextpkt = NULL; 1158 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1159 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1160 m_cat(m, q); 1161 } 1162#ifdef MAC 1163 mac_create_datagram_from_ipq(fp, m); 1164 mac_destroy_ipq(fp); 1165#endif 1166 1167 /* 1168 * Create header for new ip packet by modifying header of first 1169 * packet; dequeue and discard fragment reassembly header. 1170 * Make header visible. 1171 */ 1172 ip->ip_len = (ip->ip_hl << 2) + next; 1173 ip->ip_src = fp->ipq_src; 1174 ip->ip_dst = fp->ipq_dst; 1175 TAILQ_REMOVE(head, fp, ipq_list); 1176 nipq--; 1177 (void) m_free(dtom(fp)); 1178 m->m_len += (ip->ip_hl << 2); 1179 m->m_data -= (ip->ip_hl << 2); 1180 /* some debugging cruft by sklower, below, will go away soon */ 1181 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 1182 m_fixhdr(m); 1183 ipstat.ips_reassembled++; 1184 IPQ_UNLOCK(); 1185 return (m); 1186 1187dropfrag: 1188 ipstat.ips_fragdropped++; 1189 if (fp != NULL) 1190 fp->ipq_nfrags--; 1191 m_freem(m); 1192done: 1193 IPQ_UNLOCK(); 1194 return (NULL); 1195 1196#undef GETIP 1197} 1198 1199/* 1200 * Free a fragment reassembly header and all 1201 * associated datagrams. 1202 */ 1203static void 1204ip_freef(fhp, fp) 1205 struct ipqhead *fhp; 1206 struct ipq *fp; 1207{ 1208 register struct mbuf *q; 1209 1210 IPQ_LOCK_ASSERT(); 1211 1212 while (fp->ipq_frags) { 1213 q = fp->ipq_frags; 1214 fp->ipq_frags = q->m_nextpkt; 1215 m_freem(q); 1216 } 1217 TAILQ_REMOVE(fhp, fp, ipq_list); 1218 (void) m_free(dtom(fp)); 1219 nipq--; 1220} 1221 1222/* 1223 * IP timer processing; 1224 * if a timer expires on a reassembly 1225 * queue, discard it. 1226 */ 1227void 1228ip_slowtimo() 1229{ 1230 register struct ipq *fp; 1231 int s = splnet(); 1232 int i; 1233 1234 IPQ_LOCK(); 1235 for (i = 0; i < IPREASS_NHASH; i++) { 1236 for(fp = TAILQ_FIRST(&ipq[i]); fp;) { 1237 struct ipq *fpp; 1238 1239 fpp = fp; 1240 fp = TAILQ_NEXT(fp, ipq_list); 1241 if(--fpp->ipq_ttl == 0) { 1242 ipstat.ips_fragtimeout += fpp->ipq_nfrags; 1243 ip_freef(&ipq[i], fpp); 1244 } 1245 } 1246 } 1247 /* 1248 * If we are over the maximum number of fragments 1249 * (due to the limit being lowered), drain off 1250 * enough to get down to the new limit. 1251 */ 1252 if (maxnipq >= 0 && nipq > maxnipq) { 1253 for (i = 0; i < IPREASS_NHASH; i++) { 1254 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) { 1255 ipstat.ips_fragdropped += 1256 TAILQ_FIRST(&ipq[i])->ipq_nfrags; 1257 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i])); 1258 } 1259 } 1260 } 1261 IPQ_UNLOCK(); 1262 splx(s); 1263} 1264 1265/* 1266 * Drain off all datagram fragments. 1267 */ 1268void 1269ip_drain() 1270{ 1271 int i; 1272 1273 IPQ_LOCK(); 1274 for (i = 0; i < IPREASS_NHASH; i++) { 1275 while(!TAILQ_EMPTY(&ipq[i])) { 1276 ipstat.ips_fragdropped += 1277 TAILQ_FIRST(&ipq[i])->ipq_nfrags; 1278 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i])); 1279 } 1280 } 1281 IPQ_UNLOCK(); 1282 in_rtqdrain(); 1283} 1284 1285/* 1286 * Do option processing on a datagram, 1287 * possibly discarding it if bad options are encountered, 1288 * or forwarding it if source-routed. 1289 * The pass argument is used when operating in the IPSTEALTH 1290 * mode to tell what options to process: 1291 * [LS]SRR (pass 0) or the others (pass 1). 1292 * The reason for as many as two passes is that when doing IPSTEALTH, 1293 * non-routing options should be processed only if the packet is for us. 1294 * Returns 1 if packet has been forwarded/freed, 1295 * 0 if the packet should be processed further. 1296 */ 1297static int 1298ip_dooptions(struct mbuf *m, int pass, struct sockaddr_in *next_hop) 1299{ 1300 struct ip *ip = mtod(m, struct ip *); 1301 u_char *cp; 1302 struct in_ifaddr *ia; 1303 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1304 struct in_addr *sin, dst; 1305 n_time ntime; 1306 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 1307 1308 /* ignore or reject packets with IP options */ 1309 if (ip_doopts == 0) 1310 return 0; 1311 else if (ip_doopts == 2) { 1312 type = ICMP_UNREACH; 1313 code = ICMP_UNREACH_FILTER_PROHIB; 1314 goto bad; 1315 } 1316 1317 dst = ip->ip_dst; 1318 cp = (u_char *)(ip + 1); 1319 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1320 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1321 opt = cp[IPOPT_OPTVAL]; 1322 if (opt == IPOPT_EOL) 1323 break; 1324 if (opt == IPOPT_NOP) 1325 optlen = 1; 1326 else { 1327 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1328 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1329 goto bad; 1330 } 1331 optlen = cp[IPOPT_OLEN]; 1332 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1333 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1334 goto bad; 1335 } 1336 } 1337 switch (opt) { 1338 1339 default: 1340 break; 1341 1342 /* 1343 * Source routing with record. 1344 * Find interface with current destination address. 1345 * If none on this machine then drop if strictly routed, 1346 * or do nothing if loosely routed. 1347 * Record interface address and bring up next address 1348 * component. If strictly routed make sure next 1349 * address is on directly accessible net. 1350 */ 1351 case IPOPT_LSRR: 1352 case IPOPT_SSRR: 1353#ifdef IPSTEALTH 1354 if (ipstealth && pass > 0) 1355 break; 1356#endif 1357 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1358 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1359 goto bad; 1360 } 1361 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1362 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1363 goto bad; 1364 } 1365 ipaddr.sin_addr = ip->ip_dst; 1366 ia = (struct in_ifaddr *) 1367 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1368 if (ia == 0) { 1369 if (opt == IPOPT_SSRR) { 1370 type = ICMP_UNREACH; 1371 code = ICMP_UNREACH_SRCFAIL; 1372 goto bad; 1373 } 1374 if (!ip_dosourceroute) 1375 goto nosourcerouting; 1376 /* 1377 * Loose routing, and not at next destination 1378 * yet; nothing to do except forward. 1379 */ 1380 break; 1381 } 1382 off--; /* 0 origin */ 1383 if (off > optlen - (int)sizeof(struct in_addr)) { 1384 /* 1385 * End of source route. Should be for us. 1386 */ 1387 if (!ip_acceptsourceroute) 1388 goto nosourcerouting; 1389 save_rte(cp, ip->ip_src); 1390 break; 1391 } 1392#ifdef IPSTEALTH 1393 if (ipstealth) 1394 goto dropit; 1395#endif 1396 if (!ip_dosourceroute) { 1397 if (ipforwarding) { 1398 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1399 /* 1400 * Acting as a router, so generate ICMP 1401 */ 1402nosourcerouting: 1403 strcpy(buf, inet_ntoa(ip->ip_dst)); 1404 log(LOG_WARNING, 1405 "attempted source route from %s to %s\n", 1406 inet_ntoa(ip->ip_src), buf); 1407 type = ICMP_UNREACH; 1408 code = ICMP_UNREACH_SRCFAIL; 1409 goto bad; 1410 } else { 1411 /* 1412 * Not acting as a router, so silently drop. 1413 */ 1414#ifdef IPSTEALTH 1415dropit: 1416#endif 1417 ipstat.ips_cantforward++; 1418 m_freem(m); 1419 return (1); 1420 } 1421 } 1422 1423 /* 1424 * locate outgoing interface 1425 */ 1426 (void)memcpy(&ipaddr.sin_addr, cp + off, 1427 sizeof(ipaddr.sin_addr)); 1428 1429 if (opt == IPOPT_SSRR) { 1430#define INA struct in_ifaddr * 1431#define SA struct sockaddr * 1432 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 1433 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1434 } else 1435 ia = ip_rtaddr(ipaddr.sin_addr); 1436 if (ia == 0) { 1437 type = ICMP_UNREACH; 1438 code = ICMP_UNREACH_SRCFAIL; 1439 goto bad; 1440 } 1441 ip->ip_dst = ipaddr.sin_addr; 1442 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1443 sizeof(struct in_addr)); 1444 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1445 /* 1446 * Let ip_intr's mcast routing check handle mcast pkts 1447 */ 1448 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1449 break; 1450 1451 case IPOPT_RR: 1452#ifdef IPSTEALTH 1453 if (ipstealth && pass == 0) 1454 break; 1455#endif 1456 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1457 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1458 goto bad; 1459 } 1460 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1461 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1462 goto bad; 1463 } 1464 /* 1465 * If no space remains, ignore. 1466 */ 1467 off--; /* 0 origin */ 1468 if (off > optlen - (int)sizeof(struct in_addr)) 1469 break; 1470 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1471 sizeof(ipaddr.sin_addr)); 1472 /* 1473 * locate outgoing interface; if we're the destination, 1474 * use the incoming interface (should be same). 1475 */ 1476 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1477 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1478 type = ICMP_UNREACH; 1479 code = ICMP_UNREACH_HOST; 1480 goto bad; 1481 } 1482 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1483 sizeof(struct in_addr)); 1484 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1485 break; 1486 1487 case IPOPT_TS: 1488#ifdef IPSTEALTH 1489 if (ipstealth && pass == 0) 1490 break; 1491#endif 1492 code = cp - (u_char *)ip; 1493 if (optlen < 4 || optlen > 40) { 1494 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1495 goto bad; 1496 } 1497 if ((off = cp[IPOPT_OFFSET]) < 5) { 1498 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1499 goto bad; 1500 } 1501 if (off > optlen - (int)sizeof(int32_t)) { 1502 cp[IPOPT_OFFSET + 1] += (1 << 4); 1503 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) { 1504 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1505 goto bad; 1506 } 1507 break; 1508 } 1509 off--; /* 0 origin */ 1510 sin = (struct in_addr *)(cp + off); 1511 switch (cp[IPOPT_OFFSET + 1] & 0x0f) { 1512 1513 case IPOPT_TS_TSONLY: 1514 break; 1515 1516 case IPOPT_TS_TSANDADDR: 1517 if (off + sizeof(n_time) + 1518 sizeof(struct in_addr) > optlen) { 1519 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1520 goto bad; 1521 } 1522 ipaddr.sin_addr = dst; 1523 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1524 m->m_pkthdr.rcvif); 1525 if (ia == 0) 1526 continue; 1527 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1528 sizeof(struct in_addr)); 1529 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1530 off += sizeof(struct in_addr); 1531 break; 1532 1533 case IPOPT_TS_PRESPEC: 1534 if (off + sizeof(n_time) + 1535 sizeof(struct in_addr) > optlen) { 1536 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1537 goto bad; 1538 } 1539 (void)memcpy(&ipaddr.sin_addr, sin, 1540 sizeof(struct in_addr)); 1541 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1542 continue; 1543 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1544 off += sizeof(struct in_addr); 1545 break; 1546 1547 default: 1548 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip; 1549 goto bad; 1550 } 1551 ntime = iptime(); 1552 (void)memcpy(cp + off, &ntime, sizeof(n_time)); 1553 cp[IPOPT_OFFSET] += sizeof(n_time); 1554 } 1555 } 1556 if (forward && ipforwarding) { 1557 ip_forward(m, 1, next_hop); 1558 return (1); 1559 } 1560 return (0); 1561bad: 1562 icmp_error(m, type, code, 0, 0); 1563 ipstat.ips_badoptions++; 1564 return (1); 1565} 1566 1567/* 1568 * Given address of next destination (final or next hop), 1569 * return internet address info of interface to be used to get there. 1570 */ 1571struct in_ifaddr * 1572ip_rtaddr(dst) 1573 struct in_addr dst; 1574{ 1575 struct route sro; 1576 struct sockaddr_in *sin; 1577 struct in_ifaddr *ifa; 1578 1579 bzero(&sro, sizeof(sro)); 1580 sin = (struct sockaddr_in *)&sro.ro_dst; 1581 sin->sin_family = AF_INET; 1582 sin->sin_len = sizeof(*sin); 1583 sin->sin_addr = dst; 1584 rtalloc_ign(&sro, RTF_CLONING); 1585 1586 if (sro.ro_rt == NULL) 1587 return ((struct in_ifaddr *)0); 1588 1589 ifa = ifatoia(sro.ro_rt->rt_ifa); 1590 RTFREE(sro.ro_rt); 1591 return ifa; 1592} 1593 1594/* 1595 * Save incoming source route for use in replies, 1596 * to be picked up later by ip_srcroute if the receiver is interested. 1597 */ 1598static void 1599save_rte(option, dst) 1600 u_char *option; 1601 struct in_addr dst; 1602{ 1603 unsigned olen; 1604 1605 olen = option[IPOPT_OLEN]; 1606#ifdef DIAGNOSTIC 1607 if (ipprintfs) 1608 printf("save_rte: olen %d\n", olen); 1609#endif 1610 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1611 return; 1612 bcopy(option, ip_srcrt.srcopt, olen); 1613 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1614 ip_srcrt.dst = dst; 1615} 1616 1617/* 1618 * Retrieve incoming source route for use in replies, 1619 * in the same form used by setsockopt. 1620 * The first hop is placed before the options, will be removed later. 1621 */ 1622struct mbuf * 1623ip_srcroute() 1624{ 1625 register struct in_addr *p, *q; 1626 register struct mbuf *m; 1627 1628 if (ip_nhops == 0) 1629 return ((struct mbuf *)0); 1630 m = m_get(M_DONTWAIT, MT_HEADER); 1631 if (m == 0) 1632 return ((struct mbuf *)0); 1633 1634#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1635 1636 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1637 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1638 OPTSIZ; 1639#ifdef DIAGNOSTIC 1640 if (ipprintfs) 1641 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1642#endif 1643 1644 /* 1645 * First save first hop for return route 1646 */ 1647 p = &ip_srcrt.route[ip_nhops - 1]; 1648 *(mtod(m, struct in_addr *)) = *p--; 1649#ifdef DIAGNOSTIC 1650 if (ipprintfs) 1651 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1652#endif 1653 1654 /* 1655 * Copy option fields and padding (nop) to mbuf. 1656 */ 1657 ip_srcrt.nop = IPOPT_NOP; 1658 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1659 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1660 &ip_srcrt.nop, OPTSIZ); 1661 q = (struct in_addr *)(mtod(m, caddr_t) + 1662 sizeof(struct in_addr) + OPTSIZ); 1663#undef OPTSIZ 1664 /* 1665 * Record return path as an IP source route, 1666 * reversing the path (pointers are now aligned). 1667 */ 1668 while (p >= ip_srcrt.route) { 1669#ifdef DIAGNOSTIC 1670 if (ipprintfs) 1671 printf(" %lx", (u_long)ntohl(q->s_addr)); 1672#endif 1673 *q++ = *p--; 1674 } 1675 /* 1676 * Last hop goes to final destination. 1677 */ 1678 *q = ip_srcrt.dst; 1679#ifdef DIAGNOSTIC 1680 if (ipprintfs) 1681 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1682#endif 1683 return (m); 1684} 1685 1686/* 1687 * Strip out IP options, at higher 1688 * level protocol in the kernel. 1689 * Second argument is buffer to which options 1690 * will be moved, and return value is their length. 1691 * XXX should be deleted; last arg currently ignored. 1692 */ 1693void 1694ip_stripoptions(m, mopt) 1695 register struct mbuf *m; 1696 struct mbuf *mopt; 1697{ 1698 register int i; 1699 struct ip *ip = mtod(m, struct ip *); 1700 register caddr_t opts; 1701 int olen; 1702 1703 olen = (ip->ip_hl << 2) - sizeof (struct ip); 1704 opts = (caddr_t)(ip + 1); 1705 i = m->m_len - (sizeof (struct ip) + olen); 1706 bcopy(opts + olen, opts, (unsigned)i); 1707 m->m_len -= olen; 1708 if (m->m_flags & M_PKTHDR) 1709 m->m_pkthdr.len -= olen; 1710 ip->ip_v = IPVERSION; 1711 ip->ip_hl = sizeof(struct ip) >> 2; 1712} 1713 1714u_char inetctlerrmap[PRC_NCMDS] = { 1715 0, 0, 0, 0, 1716 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1717 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1718 EMSGSIZE, EHOSTUNREACH, 0, 0, 1719 0, 0, EHOSTUNREACH, 0, 1720 ENOPROTOOPT, ECONNREFUSED 1721}; 1722 1723/* 1724 * Forward a packet. If some error occurs return the sender 1725 * an icmp packet. Note we can't always generate a meaningful 1726 * icmp message because icmp doesn't have a large enough repertoire 1727 * of codes and types. 1728 * 1729 * If not forwarding, just drop the packet. This could be confusing 1730 * if ipforwarding was zero but some routing protocol was advancing 1731 * us as a gateway to somewhere. However, we must let the routing 1732 * protocol deal with that. 1733 * 1734 * The srcrt parameter indicates whether the packet is being forwarded 1735 * via a source route. 1736 */ 1737static void 1738ip_forward(struct mbuf *m, int srcrt, struct sockaddr_in *next_hop) 1739{ 1740 struct ip *ip = mtod(m, struct ip *); 1741 struct in_ifaddr *ia; 1742 int error, type = 0, code = 0; 1743 struct mbuf *mcopy; 1744 n_long dest; 1745 struct in_addr pkt_dst; 1746 struct ifnet *destifp; 1747#if defined(IPSEC) || defined(FAST_IPSEC) 1748 struct ifnet dummyifp; 1749#endif 1750 1751 /* 1752 * Cache the destination address of the packet; this may be 1753 * changed by use of 'ipfw fwd'. 1754 */ 1755 pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst; 1756 1757#ifdef DIAGNOSTIC 1758 if (ipprintfs) 1759 printf("forward: src %lx dst %lx ttl %x\n", 1760 (u_long)ip->ip_src.s_addr, (u_long)pkt_dst.s_addr, 1761 ip->ip_ttl); 1762#endif 1763 1764 1765 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(pkt_dst) == 0) { 1766 ipstat.ips_cantforward++; 1767 m_freem(m); 1768 return; 1769 } 1770#ifdef IPSTEALTH 1771 if (!ipstealth) { 1772#endif 1773 if (ip->ip_ttl <= IPTTLDEC) { 1774 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1775 0, 0); 1776 return; 1777 } 1778#ifdef IPSTEALTH 1779 } 1780#endif 1781 1782 if ((ia = ip_rtaddr(pkt_dst)) == 0) { 1783 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1784 return; 1785 } 1786 1787 /* 1788 * Save the IP header and at most 8 bytes of the payload, 1789 * in case we need to generate an ICMP message to the src. 1790 * 1791 * XXX this can be optimized a lot by saving the data in a local 1792 * buffer on the stack (72 bytes at most), and only allocating the 1793 * mbuf if really necessary. The vast majority of the packets 1794 * are forwarded without having to send an ICMP back (either 1795 * because unnecessary, or because rate limited), so we are 1796 * really we are wasting a lot of work here. 1797 * 1798 * We don't use m_copy() because it might return a reference 1799 * to a shared cluster. Both this function and ip_output() 1800 * assume exclusive access to the IP header in `m', so any 1801 * data in a cluster may change before we reach icmp_error(). 1802 */ 1803 MGET(mcopy, M_DONTWAIT, m->m_type); 1804 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) { 1805 /* 1806 * It's probably ok if the pkthdr dup fails (because 1807 * the deep copy of the tag chain failed), but for now 1808 * be conservative and just discard the copy since 1809 * code below may some day want the tags. 1810 */ 1811 m_free(mcopy); 1812 mcopy = NULL; 1813 } 1814 if (mcopy != NULL) { 1815 mcopy->m_len = imin((ip->ip_hl << 2) + 8, 1816 (int)ip->ip_len); 1817 mcopy->m_pkthdr.len = mcopy->m_len; 1818 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1819 } 1820 1821#ifdef IPSTEALTH 1822 if (!ipstealth) { 1823#endif 1824 ip->ip_ttl -= IPTTLDEC; 1825#ifdef IPSTEALTH 1826 } 1827#endif 1828 1829 /* 1830 * If forwarding packet using same interface that it came in on, 1831 * perhaps should send a redirect to sender to shortcut a hop. 1832 * Only send redirect if source is sending directly to us, 1833 * and if packet was not source routed (or has any options). 1834 * Also, don't send redirect if forwarding using a default route 1835 * or a route modified by a redirect. 1836 */ 1837 dest = 0; 1838 if (ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) { 1839 struct sockaddr_in *sin; 1840 struct route ro; 1841 struct rtentry *rt; 1842 1843 bzero(&ro, sizeof(ro)); 1844 sin = (struct sockaddr_in *)&ro.ro_dst; 1845 sin->sin_family = AF_INET; 1846 sin->sin_len = sizeof(*sin); 1847 sin->sin_addr = pkt_dst; 1848 rtalloc_ign(&ro, RTF_CLONING); 1849 1850 rt = ro.ro_rt; 1851 1852 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1853 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1854 ipsendredirects && !srcrt && !next_hop) { 1855#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1856 u_long src = ntohl(ip->ip_src.s_addr); 1857 1858 if (RTA(rt) && 1859 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1860 if (rt->rt_flags & RTF_GATEWAY) 1861 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1862 else 1863 dest = pkt_dst.s_addr; 1864 /* Router requirements says to only send host redirects */ 1865 type = ICMP_REDIRECT; 1866 code = ICMP_REDIRECT_HOST; 1867#ifdef DIAGNOSTIC 1868 if (ipprintfs) 1869 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1870#endif 1871 } 1872 } 1873 if (rt) 1874 RTFREE(rt); 1875 } 1876 1877 if (next_hop) { 1878 struct m_tag *mtag = m_tag_get(PACKET_TAG_IPFORWARD, 1879 sizeof(struct sockaddr_in *), M_NOWAIT); 1880 if (mtag == NULL) { 1881 m_freem(m); 1882 return; 1883 } 1884 *(struct sockaddr_in **)(mtag+1) = next_hop; 1885 m_tag_prepend(m, mtag); 1886 } 1887 error = ip_output(m, (struct mbuf *)0, NULL, IP_FORWARDING, 0, NULL); 1888 if (error) 1889 ipstat.ips_cantforward++; 1890 else { 1891 ipstat.ips_forward++; 1892 if (type) 1893 ipstat.ips_redirectsent++; 1894 else { 1895 if (mcopy) 1896 m_freem(mcopy); 1897 return; 1898 } 1899 } 1900 if (mcopy == NULL) 1901 return; 1902 destifp = NULL; 1903 1904 switch (error) { 1905 1906 case 0: /* forwarded, but need redirect */ 1907 /* type, code set above */ 1908 break; 1909 1910 case ENETUNREACH: /* shouldn't happen, checked above */ 1911 case EHOSTUNREACH: 1912 case ENETDOWN: 1913 case EHOSTDOWN: 1914 default: 1915 type = ICMP_UNREACH; 1916 code = ICMP_UNREACH_HOST; 1917 break; 1918 1919 case EMSGSIZE: 1920 type = ICMP_UNREACH; 1921 code = ICMP_UNREACH_NEEDFRAG; 1922#if defined(IPSEC) || defined(FAST_IPSEC) 1923 /* 1924 * If the packet is routed over IPsec tunnel, tell the 1925 * originator the tunnel MTU. 1926 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1927 * XXX quickhack!!! 1928 */ 1929 { 1930 struct secpolicy *sp = NULL; 1931 int ipsecerror; 1932 int ipsechdr; 1933 struct route *ro; 1934 1935#ifdef IPSEC 1936 sp = ipsec4_getpolicybyaddr(mcopy, 1937 IPSEC_DIR_OUTBOUND, 1938 IP_FORWARDING, 1939 &ipsecerror); 1940#else /* FAST_IPSEC */ 1941 sp = ipsec_getpolicybyaddr(mcopy, 1942 IPSEC_DIR_OUTBOUND, 1943 IP_FORWARDING, 1944 &ipsecerror); 1945#endif 1946 if (sp != NULL) { 1947 /* count IPsec header size */ 1948 ipsechdr = ipsec4_hdrsiz(mcopy, 1949 IPSEC_DIR_OUTBOUND, 1950 NULL); 1951 1952 /* 1953 * find the correct route for outer IPv4 1954 * header, compute tunnel MTU. 1955 * 1956 * XXX BUG ALERT 1957 * The "dummyifp" code relies upon the fact 1958 * that icmp_error() touches only ifp->if_mtu. 1959 */ 1960 /*XXX*/ 1961 destifp = NULL; 1962 if (sp->req != NULL 1963 && sp->req->sav != NULL 1964 && sp->req->sav->sah != NULL) { 1965 ro = &sp->req->sav->sah->sa_route; 1966 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1967 dummyifp.if_mtu = 1968 ro->ro_rt->rt_rmx.rmx_mtu ? 1969 ro->ro_rt->rt_rmx.rmx_mtu : 1970 ro->ro_rt->rt_ifp->if_mtu; 1971 dummyifp.if_mtu -= ipsechdr; 1972 destifp = &dummyifp; 1973 } 1974 } 1975 1976#ifdef IPSEC 1977 key_freesp(sp); 1978#else /* FAST_IPSEC */ 1979 KEY_FREESP(&sp); 1980#endif 1981 ipstat.ips_cantfrag++; 1982 break; 1983 } else 1984#endif /*IPSEC || FAST_IPSEC*/ 1985 destifp = ia->ia_ifp; 1986#if defined(IPSEC) || defined(FAST_IPSEC) 1987 } 1988#endif /*IPSEC || FAST_IPSEC*/ 1989 ipstat.ips_cantfrag++; 1990 break; 1991 1992 case ENOBUFS: 1993 /* 1994 * A router should not generate ICMP_SOURCEQUENCH as 1995 * required in RFC1812 Requirements for IP Version 4 Routers. 1996 * Source quench could be a big problem under DoS attacks, 1997 * or if the underlying interface is rate-limited. 1998 * Those who need source quench packets may re-enable them 1999 * via the net.inet.ip.sendsourcequench sysctl. 2000 */ 2001 if (ip_sendsourcequench == 0) { 2002 m_freem(mcopy); 2003 return; 2004 } else { 2005 type = ICMP_SOURCEQUENCH; 2006 code = 0; 2007 } 2008 break; 2009 2010 case EACCES: /* ipfw denied packet */ 2011 m_freem(mcopy); 2012 return; 2013 } 2014 icmp_error(mcopy, type, code, dest, destifp); 2015} 2016 2017void 2018ip_savecontrol(inp, mp, ip, m) 2019 register struct inpcb *inp; 2020 register struct mbuf **mp; 2021 register struct ip *ip; 2022 register struct mbuf *m; 2023{ 2024 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 2025 struct bintime bt; 2026 2027 bintime(&bt); 2028 if (inp->inp_socket->so_options & SO_BINTIME) { 2029 *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt), 2030 SCM_BINTIME, SOL_SOCKET); 2031 if (*mp) 2032 mp = &(*mp)->m_next; 2033 } 2034 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 2035 struct timeval tv; 2036 2037 bintime2timeval(&bt, &tv); 2038 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 2039 SCM_TIMESTAMP, SOL_SOCKET); 2040 if (*mp) 2041 mp = &(*mp)->m_next; 2042 } 2043 } 2044 if (inp->inp_flags & INP_RECVDSTADDR) { 2045 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 2046 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 2047 if (*mp) 2048 mp = &(*mp)->m_next; 2049 } 2050 if (inp->inp_flags & INP_RECVTTL) { 2051 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, 2052 sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 2053 if (*mp) 2054 mp = &(*mp)->m_next; 2055 } 2056#ifdef notyet 2057 /* XXX 2058 * Moving these out of udp_input() made them even more broken 2059 * than they already were. 2060 */ 2061 /* options were tossed already */ 2062 if (inp->inp_flags & INP_RECVOPTS) { 2063 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 2064 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 2065 if (*mp) 2066 mp = &(*mp)->m_next; 2067 } 2068 /* ip_srcroute doesn't do what we want here, need to fix */ 2069 if (inp->inp_flags & INP_RECVRETOPTS) { 2070 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 2071 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 2072 if (*mp) 2073 mp = &(*mp)->m_next; 2074 } 2075#endif 2076 if (inp->inp_flags & INP_RECVIF) { 2077 struct ifnet *ifp; 2078 struct sdlbuf { 2079 struct sockaddr_dl sdl; 2080 u_char pad[32]; 2081 } sdlbuf; 2082 struct sockaddr_dl *sdp; 2083 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 2084 2085 if (((ifp = m->m_pkthdr.rcvif)) 2086 && ( ifp->if_index && (ifp->if_index <= if_index))) { 2087 sdp = (struct sockaddr_dl *) 2088 (ifaddr_byindex(ifp->if_index)->ifa_addr); 2089 /* 2090 * Change our mind and don't try copy. 2091 */ 2092 if ((sdp->sdl_family != AF_LINK) 2093 || (sdp->sdl_len > sizeof(sdlbuf))) { 2094 goto makedummy; 2095 } 2096 bcopy(sdp, sdl2, sdp->sdl_len); 2097 } else { 2098makedummy: 2099 sdl2->sdl_len 2100 = offsetof(struct sockaddr_dl, sdl_data[0]); 2101 sdl2->sdl_family = AF_LINK; 2102 sdl2->sdl_index = 0; 2103 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 2104 } 2105 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 2106 IP_RECVIF, IPPROTO_IP); 2107 if (*mp) 2108 mp = &(*mp)->m_next; 2109 } 2110} 2111 2112/* 2113 * XXX these routines are called from the upper part of the kernel. 2114 * They need to be locked when we remove Giant. 2115 * 2116 * They could also be moved to ip_mroute.c, since all the RSVP 2117 * handling is done there already. 2118 */ 2119static int ip_rsvp_on; 2120struct socket *ip_rsvpd; 2121int 2122ip_rsvp_init(struct socket *so) 2123{ 2124 if (so->so_type != SOCK_RAW || 2125 so->so_proto->pr_protocol != IPPROTO_RSVP) 2126 return EOPNOTSUPP; 2127 2128 if (ip_rsvpd != NULL) 2129 return EADDRINUSE; 2130 2131 ip_rsvpd = so; 2132 /* 2133 * This may seem silly, but we need to be sure we don't over-increment 2134 * the RSVP counter, in case something slips up. 2135 */ 2136 if (!ip_rsvp_on) { 2137 ip_rsvp_on = 1; 2138 rsvp_on++; 2139 } 2140 2141 return 0; 2142} 2143 2144int 2145ip_rsvp_done(void) 2146{ 2147 ip_rsvpd = NULL; 2148 /* 2149 * This may seem silly, but we need to be sure we don't over-decrement 2150 * the RSVP counter, in case something slips up. 2151 */ 2152 if (ip_rsvp_on) { 2153 ip_rsvp_on = 0; 2154 rsvp_on--; 2155 } 2156 return 0; 2157} 2158 2159void 2160rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */ 2161{ 2162 if (rsvp_input_p) { /* call the real one if loaded */ 2163 rsvp_input_p(m, off); 2164 return; 2165 } 2166 2167 /* Can still get packets with rsvp_on = 0 if there is a local member 2168 * of the group to which the RSVP packet is addressed. But in this 2169 * case we want to throw the packet away. 2170 */ 2171 2172 if (!rsvp_on) { 2173 m_freem(m); 2174 return; 2175 } 2176 2177 if (ip_rsvpd != NULL) { 2178 rip_input(m, off); 2179 return; 2180 } 2181 /* Drop the packet */ 2182 m_freem(m); 2183} 2184