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