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