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