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