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