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