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