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