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