1 /* $OpenBSD: ip_input.c,v 1.171 2009/08/23 20:06:25 david Exp $ */ 2 /* $NetBSD: ip_input.c,v 1.30 1996/03/16 23:53:58 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 33 */ 34 35 #include "pf.h" 36 #include "carp.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/mbuf.h> 41 #include <sys/domain.h> 42 #include <sys/protosw.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 #include <sys/syslog.h> 46 #include <sys/sysctl.h> 47 #include <sys/pool.h> 48 49 #include <net/if.h> 50 #include <net/if_dl.h> 51 #include <net/route.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/if_ether.h> 56 #include <netinet/ip.h> 57 #include <netinet/in_pcb.h> 58 #include <netinet/in_var.h> 59 #include <netinet/ip_var.h> 60 #include <netinet/ip_icmp.h> 61 62 #if NPF > 0 63 #include <net/pfvar.h> 64 #endif 65 66 #ifdef MROUTING 67 #include <netinet/ip_mroute.h> 68 #endif 69 70 #ifdef IPSEC 71 #include <netinet/ip_ipsp.h> 72 #endif /* IPSEC */ 73 74 #if NCARP > 0 75 #include <net/if_types.h> 76 #include <netinet/ip_carp.h> 77 #endif 78 79 #define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */ 80 81 struct ipqhead ipq; 82 83 int encdebug = 0; 84 int ipsec_keep_invalid = IPSEC_DEFAULT_EMBRYONIC_SA_TIMEOUT; 85 int ipsec_require_pfs = IPSEC_DEFAULT_PFS; 86 int ipsec_soft_allocations = IPSEC_DEFAULT_SOFT_ALLOCATIONS; 87 int ipsec_exp_allocations = IPSEC_DEFAULT_EXP_ALLOCATIONS; 88 int ipsec_soft_bytes = IPSEC_DEFAULT_SOFT_BYTES; 89 int ipsec_exp_bytes = IPSEC_DEFAULT_EXP_BYTES; 90 int ipsec_soft_timeout = IPSEC_DEFAULT_SOFT_TIMEOUT; 91 int ipsec_exp_timeout = IPSEC_DEFAULT_EXP_TIMEOUT; 92 int ipsec_soft_first_use = IPSEC_DEFAULT_SOFT_FIRST_USE; 93 int ipsec_exp_first_use = IPSEC_DEFAULT_EXP_FIRST_USE; 94 int ipsec_expire_acquire = IPSEC_DEFAULT_EXPIRE_ACQUIRE; 95 char ipsec_def_enc[20]; 96 char ipsec_def_auth[20]; 97 char ipsec_def_comp[20]; 98 99 /* values controllable via sysctl */ 100 int ipforwarding = 0; 101 int ipmforwarding = 0; 102 int ipmultipath = 0; 103 int ipsendredirects = 1; 104 int ip_dosourceroute = 0; 105 int ip_defttl = IPDEFTTL; 106 int ip_mtudisc = 1; 107 u_int ip_mtudisc_timeout = IPMTUDISCTIMEOUT; 108 int ip_directedbcast = 0; 109 #ifdef DIAGNOSTIC 110 int ipprintfs = 0; 111 #endif 112 113 struct rttimer_queue *ip_mtudisc_timeout_q = NULL; 114 115 int ipsec_auth_default_level = IPSEC_AUTH_LEVEL_DEFAULT; 116 int ipsec_esp_trans_default_level = IPSEC_ESP_TRANS_LEVEL_DEFAULT; 117 int ipsec_esp_network_default_level = IPSEC_ESP_NETWORK_LEVEL_DEFAULT; 118 int ipsec_ipcomp_default_level = IPSEC_IPCOMP_LEVEL_DEFAULT; 119 120 /* Keep track of memory used for reassembly */ 121 int ip_maxqueue = 300; 122 int ip_frags = 0; 123 124 /* from in_pcb.c */ 125 extern int ipport_firstauto; 126 extern int ipport_lastauto; 127 extern int ipport_hifirstauto; 128 extern int ipport_hilastauto; 129 extern struct baddynamicports baddynamicports; 130 extern int la_hold_total; 131 132 int *ipctl_vars[IPCTL_MAXID] = IPCTL_VARS; 133 134 extern struct domain inetdomain; 135 extern struct protosw inetsw[]; 136 u_char ip_protox[IPPROTO_MAX]; 137 int ipqmaxlen = IFQ_MAXLEN; 138 struct in_ifaddrhead in_ifaddr; 139 struct ifqueue ipintrq; 140 141 struct pool ipqent_pool; 142 struct pool ipq_pool; 143 144 struct ipstat ipstat; 145 146 char * 147 inet_ntoa(ina) 148 struct in_addr ina; 149 { 150 static char buf[4*sizeof "123"]; 151 unsigned char *ucp = (unsigned char *)&ina; 152 153 snprintf(buf, sizeof buf, "%d.%d.%d.%d", 154 ucp[0] & 0xff, ucp[1] & 0xff, 155 ucp[2] & 0xff, ucp[3] & 0xff); 156 return (buf); 157 } 158 159 /* 160 * We need to save the IP options in case a protocol wants to respond 161 * to an incoming packet over the same route if the packet got here 162 * using IP source routing. This allows connection establishment and 163 * maintenance when the remote end is on a network that is not known 164 * to us. 165 */ 166 int ip_nhops = 0; 167 static struct ip_srcrt { 168 struct in_addr dst; /* final destination */ 169 char nop; /* one NOP to align */ 170 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 171 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 172 } ip_srcrt; 173 174 void save_rte(u_char *, struct in_addr); 175 int ip_weadvertise(u_int32_t, u_int); 176 177 /* 178 * IP initialization: fill in IP protocol switch table. 179 * All protocols not implemented in kernel go to raw IP protocol handler. 180 */ 181 void 182 ip_init() 183 { 184 struct protosw *pr; 185 int i; 186 const u_int16_t defbaddynamicports_tcp[] = DEFBADDYNAMICPORTS_TCP; 187 const u_int16_t defbaddynamicports_udp[] = DEFBADDYNAMICPORTS_UDP; 188 189 pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl", 190 NULL); 191 pool_init(&ipq_pool, sizeof(struct ipq), 0, 0, 0, "ipqpl", 192 NULL); 193 194 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 195 if (pr == 0) 196 panic("ip_init"); 197 for (i = 0; i < IPPROTO_MAX; i++) 198 ip_protox[i] = pr - inetsw; 199 for (pr = inetdomain.dom_protosw; 200 pr < inetdomain.dom_protoswNPROTOSW; pr++) 201 if (pr->pr_domain->dom_family == PF_INET && 202 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 203 ip_protox[pr->pr_protocol] = pr - inetsw; 204 LIST_INIT(&ipq); 205 ipintrq.ifq_maxlen = ipqmaxlen; 206 TAILQ_INIT(&in_ifaddr); 207 if (ip_mtudisc != 0) 208 ip_mtudisc_timeout_q = 209 rt_timer_queue_create(ip_mtudisc_timeout); 210 211 /* Fill in list of ports not to allocate dynamically. */ 212 bzero((void *)&baddynamicports, sizeof(baddynamicports)); 213 for (i = 0; defbaddynamicports_tcp[i] != 0; i++) 214 DP_SET(baddynamicports.tcp, defbaddynamicports_tcp[i]); 215 for (i = 0; defbaddynamicports_udp[i] != 0; i++) 216 DP_SET(baddynamicports.udp, defbaddynamicports_udp[i]); 217 218 strlcpy(ipsec_def_enc, IPSEC_DEFAULT_DEF_ENC, sizeof(ipsec_def_enc)); 219 strlcpy(ipsec_def_auth, IPSEC_DEFAULT_DEF_AUTH, sizeof(ipsec_def_auth)); 220 strlcpy(ipsec_def_comp, IPSEC_DEFAULT_DEF_COMP, sizeof(ipsec_def_comp)); 221 } 222 223 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 224 struct route ipforward_rt; 225 u_int ipforward_rtableid; 226 227 void 228 ipintr() 229 { 230 struct mbuf *m; 231 int s; 232 233 for (;;) { 234 /* 235 * Get next datagram off input queue and get IP header 236 * in first mbuf. 237 */ 238 s = splnet(); 239 IF_DEQUEUE(&ipintrq, m); 240 splx(s); 241 if (m == NULL) 242 return; 243 #ifdef DIAGNOSTIC 244 if ((m->m_flags & M_PKTHDR) == 0) 245 panic("ipintr no HDR"); 246 #endif 247 ipv4_input(m); 248 } 249 } 250 251 /* 252 * Ip input routine. Checksum and byte swap header. If fragmented 253 * try to reassemble. Process options. Pass to next level. 254 */ 255 void 256 ipv4_input(m) 257 struct mbuf *m; 258 { 259 struct ip *ip; 260 struct ipq *fp; 261 struct in_ifaddr *ia; 262 struct ipqent *ipqe; 263 int hlen, mff, len; 264 in_addr_t pfrdr = 0; 265 #ifdef IPSEC 266 int error, s; 267 struct tdb *tdb; 268 struct tdb_ident *tdbi; 269 struct m_tag *mtag; 270 #endif /* IPSEC */ 271 272 /* 273 * If no IP addresses have been set yet but the interfaces 274 * are receiving, can't do anything with incoming packets yet. 275 */ 276 if (TAILQ_EMPTY(&in_ifaddr)) 277 goto bad; 278 ipstat.ips_total++; 279 if (m->m_len < sizeof (struct ip) && 280 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 281 ipstat.ips_toosmall++; 282 return; 283 } 284 ip = mtod(m, struct ip *); 285 if (ip->ip_v != IPVERSION) { 286 ipstat.ips_badvers++; 287 goto bad; 288 } 289 hlen = ip->ip_hl << 2; 290 if (hlen < sizeof(struct ip)) { /* minimum header length */ 291 ipstat.ips_badhlen++; 292 goto bad; 293 } 294 if (hlen > m->m_len) { 295 if ((m = m_pullup(m, hlen)) == NULL) { 296 ipstat.ips_badhlen++; 297 return; 298 } 299 ip = mtod(m, struct ip *); 300 } 301 302 /* 127/8 must not appear on wire - RFC1122 */ 303 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 304 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 305 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 306 ipstat.ips_badaddr++; 307 goto bad; 308 } 309 } 310 311 if ((m->m_pkthdr.csum_flags & M_IPV4_CSUM_IN_OK) == 0) { 312 if (m->m_pkthdr.csum_flags & M_IPV4_CSUM_IN_BAD) { 313 ipstat.ips_inhwcsum++; 314 ipstat.ips_badsum++; 315 goto bad; 316 } 317 318 if (in_cksum(m, hlen) != 0) { 319 ipstat.ips_badsum++; 320 goto bad; 321 } 322 } else { 323 m->m_pkthdr.csum_flags &= ~M_IPV4_CSUM_IN_OK; 324 ipstat.ips_inhwcsum++; 325 } 326 327 /* Retrieve the packet length. */ 328 len = ntohs(ip->ip_len); 329 330 /* 331 * Convert fields to host representation. 332 */ 333 if (len < hlen) { 334 ipstat.ips_badlen++; 335 goto bad; 336 } 337 338 /* 339 * Check that the amount of data in the buffers 340 * is at least as much as the IP header would have us expect. 341 * Trim mbufs if longer than we expect. 342 * Drop packet if shorter than we expect. 343 */ 344 if (m->m_pkthdr.len < len) { 345 ipstat.ips_tooshort++; 346 goto bad; 347 } 348 if (m->m_pkthdr.len > len) { 349 if (m->m_len == m->m_pkthdr.len) { 350 m->m_len = len; 351 m->m_pkthdr.len = len; 352 } else 353 m_adj(m, len - m->m_pkthdr.len); 354 } 355 356 #if NCARP > 0 357 if (m->m_pkthdr.rcvif->if_type == IFT_CARP && 358 ip->ip_p != IPPROTO_ICMP && carp_lsdrop(m, AF_INET, 359 &ip->ip_src.s_addr, &ip->ip_dst.s_addr)) 360 goto bad; 361 #endif 362 363 #if NPF > 0 364 /* 365 * Packet filter 366 */ 367 pfrdr = ip->ip_dst.s_addr; 368 if (pf_test(PF_IN, m->m_pkthdr.rcvif, &m, NULL) != PF_PASS) 369 goto bad; 370 if (m == NULL) 371 return; 372 373 ip = mtod(m, struct ip *); 374 hlen = ip->ip_hl << 2; 375 pfrdr = (pfrdr != ip->ip_dst.s_addr); 376 #endif 377 378 /* 379 * Process options and, if not destined for us, 380 * ship it on. ip_dooptions returns 1 when an 381 * error was detected (causing an icmp message 382 * to be sent and the original packet to be freed). 383 */ 384 ip_nhops = 0; /* for source routed packets */ 385 if (hlen > sizeof (struct ip) && ip_dooptions(m)) { 386 return; 387 } 388 389 if (m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) 390 goto ours; 391 392 #if NPF > 0 393 if (m->m_pkthdr.pf.statekey && 394 ((struct pf_state_key *)m->m_pkthdr.pf.statekey)->inp) 395 goto ours; 396 397 /* 398 * Check our list of addresses, to see if the packet is for us. 399 * if we have linked state keys it is certainly to be forwarded. 400 */ 401 if (!m->m_pkthdr.pf.statekey || 402 !((struct pf_state_key *)m->m_pkthdr.pf.statekey)->reverse) 403 #endif 404 if ((ia = in_iawithaddr(ip->ip_dst, m, m->m_pkthdr.rdomain)) != 405 NULL && (ia->ia_ifp->if_flags & IFF_UP)) 406 goto ours; 407 408 if (IN_MULTICAST(ip->ip_dst.s_addr)) { 409 struct in_multi *inm; 410 #ifdef MROUTING 411 extern struct socket *ip_mrouter; 412 413 if (m->m_flags & M_EXT) { 414 if ((m = m_pullup(m, hlen)) == NULL) { 415 ipstat.ips_toosmall++; 416 return; 417 } 418 ip = mtod(m, struct ip *); 419 } 420 if (ipmforwarding && ip_mrouter) { 421 /* 422 * If we are acting as a multicast router, all 423 * incoming multicast packets are passed to the 424 * kernel-level multicast forwarding function. 425 * The packet is returned (relatively) intact; if 426 * ip_mforward() returns a non-zero value, the packet 427 * must be discarded, else it may be accepted below. 428 * 429 * (The IP ident field is put in the same byte order 430 * as expected when ip_mforward() is called from 431 * ip_output().) 432 */ 433 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { 434 ipstat.ips_cantforward++; 435 m_freem(m); 436 return; 437 } 438 439 /* 440 * The process-level routing daemon needs to receive 441 * all multicast IGMP packets, whether or not this 442 * host belongs to their destination groups. 443 */ 444 if (ip->ip_p == IPPROTO_IGMP) 445 goto ours; 446 ipstat.ips_forward++; 447 } 448 #endif 449 /* 450 * See if we belong to the destination multicast group on the 451 * arrival interface. 452 */ 453 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 454 if (inm == NULL) { 455 ipstat.ips_notmember++; 456 if (!IN_LOCAL_GROUP(ip->ip_dst.s_addr)) 457 ipstat.ips_cantforward++; 458 m_freem(m); 459 return; 460 } 461 goto ours; 462 } 463 464 if (ip->ip_dst.s_addr == INADDR_BROADCAST || 465 ip->ip_dst.s_addr == INADDR_ANY) 466 goto ours; 467 468 #if NCARP > 0 469 if (m->m_pkthdr.rcvif->if_type == IFT_CARP && 470 ip->ip_p == IPPROTO_ICMP && carp_lsdrop(m, AF_INET, 471 &ip->ip_src.s_addr, &ip->ip_dst.s_addr)) 472 goto bad; 473 #endif 474 /* 475 * Not for us; forward if possible and desirable. 476 */ 477 if (ipforwarding == 0) { 478 ipstat.ips_cantforward++; 479 m_freem(m); 480 return; 481 } 482 #ifdef IPSEC 483 if (ipsec_in_use) { 484 /* 485 * IPsec policy check for forwarded packets. Look at 486 * inner-most IPsec SA used. 487 */ 488 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 489 s = splnet(); 490 if (mtag != NULL) { 491 tdbi = (struct tdb_ident *)(mtag + 1); 492 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto); 493 } else 494 tdb = NULL; 495 ipsp_spd_lookup(m, AF_INET, hlen, &error, 496 IPSP_DIRECTION_IN, tdb, NULL); 497 splx(s); 498 499 /* Error or otherwise drop-packet indication */ 500 if (error) { 501 ipstat.ips_cantforward++; 502 m_freem(m); 503 return; 504 } 505 506 /* 507 * Fall through, forward packet. Outbound IPsec policy 508 * checking will occur in ip_output(). 509 */ 510 } 511 #endif /* IPSEC */ 512 513 ip_forward(m, pfrdr); 514 return; 515 516 ours: 517 /* 518 * If offset or IP_MF are set, must reassemble. 519 * Otherwise, nothing need be done. 520 * (We could look in the reassembly queue to see 521 * if the packet was previously fragmented, 522 * but it's not worth the time; just let them time out.) 523 */ 524 if (ip->ip_off &~ htons(IP_DF | IP_RF)) { 525 if (m->m_flags & M_EXT) { /* XXX */ 526 if ((m = m_pullup(m, hlen)) == NULL) { 527 ipstat.ips_toosmall++; 528 return; 529 } 530 ip = mtod(m, struct ip *); 531 } 532 533 /* 534 * Look for queue of fragments 535 * of this datagram. 536 */ 537 LIST_FOREACH(fp, &ipq, ipq_q) 538 if (ip->ip_id == fp->ipq_id && 539 ip->ip_src.s_addr == fp->ipq_src.s_addr && 540 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 541 ip->ip_p == fp->ipq_p) 542 goto found; 543 fp = 0; 544 found: 545 546 /* 547 * Adjust ip_len to not reflect header, 548 * set ipqe_mff if more fragments are expected, 549 * convert offset of this to bytes. 550 */ 551 ip->ip_len = htons(ntohs(ip->ip_len) - hlen); 552 mff = (ip->ip_off & htons(IP_MF)) != 0; 553 if (mff) { 554 /* 555 * Make sure that fragments have a data length 556 * that's a non-zero multiple of 8 bytes. 557 */ 558 if (ntohs(ip->ip_len) == 0 || 559 (ntohs(ip->ip_len) & 0x7) != 0) { 560 ipstat.ips_badfrags++; 561 goto bad; 562 } 563 } 564 ip->ip_off = htons(ntohs(ip->ip_off) << 3); 565 566 /* 567 * If datagram marked as having more fragments 568 * or if this is not the first fragment, 569 * attempt reassembly; if it succeeds, proceed. 570 */ 571 if (mff || ip->ip_off) { 572 ipstat.ips_fragments++; 573 if (ip_frags + 1 > ip_maxqueue) { 574 ip_flush(); 575 ipstat.ips_rcvmemdrop++; 576 goto bad; 577 } 578 579 ipqe = pool_get(&ipqent_pool, PR_NOWAIT); 580 if (ipqe == NULL) { 581 ipstat.ips_rcvmemdrop++; 582 goto bad; 583 } 584 ip_frags++; 585 ipqe->ipqe_mff = mff; 586 ipqe->ipqe_m = m; 587 ipqe->ipqe_ip = ip; 588 m = ip_reass(ipqe, fp); 589 if (m == 0) { 590 return; 591 } 592 ipstat.ips_reassembled++; 593 ip = mtod(m, struct ip *); 594 hlen = ip->ip_hl << 2; 595 ip->ip_len = htons(ntohs(ip->ip_len) + hlen); 596 } else 597 if (fp) 598 ip_freef(fp); 599 } 600 601 #ifdef IPSEC 602 if (!ipsec_in_use) 603 goto skipipsec; 604 605 /* 606 * If it's a protected packet for us, skip the policy check. 607 * That's because we really only care about the properties of 608 * the protected packet, and not the intermediate versions. 609 * While this is not the most paranoid setting, it allows 610 * some flexibility in handling nested tunnels (in setting up 611 * the policies). 612 */ 613 if ((ip->ip_p == IPPROTO_ESP) || (ip->ip_p == IPPROTO_AH) || 614 (ip->ip_p == IPPROTO_IPCOMP)) 615 goto skipipsec; 616 617 /* 618 * If the protected packet was tunneled, then we need to 619 * verify the protected packet's information, not the 620 * external headers. Thus, skip the policy lookup for the 621 * external packet, and keep the IPsec information linked on 622 * the packet header (the encapsulation routines know how 623 * to deal with that). 624 */ 625 if ((ip->ip_p == IPPROTO_IPIP) || (ip->ip_p == IPPROTO_IPV6)) 626 goto skipipsec; 627 628 /* 629 * If the protected packet is TCP or UDP, we'll do the 630 * policy check in the respective input routine, so we can 631 * check for bypass sockets. 632 */ 633 if ((ip->ip_p == IPPROTO_TCP) || (ip->ip_p == IPPROTO_UDP)) 634 goto skipipsec; 635 636 /* 637 * IPsec policy check for local-delivery packets. Look at the 638 * inner-most SA that protected the packet. This is in fact 639 * a bit too restrictive (it could end up causing packets to 640 * be dropped that semantically follow the policy, e.g., in 641 * certain SA-bundle configurations); but the alternative is 642 * very complicated (and requires keeping track of what 643 * kinds of tunneling headers have been seen in-between the 644 * IPsec headers), and I don't think we lose much functionality 645 * that's needed in the real world (who uses bundles anyway ?). 646 */ 647 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 648 s = splnet(); 649 if (mtag) { 650 tdbi = (struct tdb_ident *)(mtag + 1); 651 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto); 652 } else 653 tdb = NULL; 654 ipsp_spd_lookup(m, AF_INET, hlen, &error, IPSP_DIRECTION_IN, 655 tdb, NULL); 656 splx(s); 657 658 /* Error or otherwise drop-packet indication. */ 659 if (error) { 660 ipstat.ips_cantforward++; 661 m_freem(m); 662 return; 663 } 664 665 skipipsec: 666 /* Otherwise, just fall through and deliver the packet */ 667 #endif /* IPSEC */ 668 669 /* 670 * Switch out to protocol's input routine. 671 */ 672 ipstat.ips_delivered++; 673 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen, NULL, 0); 674 return; 675 bad: 676 m_freem(m); 677 } 678 679 struct in_ifaddr * 680 in_iawithaddr(struct in_addr ina, struct mbuf *m, u_int rdomain) 681 { 682 struct in_ifaddr *ia; 683 684 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) { 685 if (ia->ia_ifp->if_rdomain != rdomain) 686 continue; 687 if ((ina.s_addr == ia->ia_addr.sin_addr.s_addr) || 688 ((ia->ia_ifp->if_flags & (IFF_LOOPBACK|IFF_LINK1)) == 689 (IFF_LOOPBACK|IFF_LINK1) && 690 ia->ia_subnet == (ina.s_addr & ia->ia_subnetmask))) 691 return ia; 692 if (((ip_directedbcast == 0) || (m && ip_directedbcast && 693 ia->ia_ifp == m->m_pkthdr.rcvif)) && 694 (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 695 if (ina.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 696 ina.s_addr == ia->ia_netbroadcast.s_addr || 697 /* 698 * Look for all-0's host part (old broadcast addr), 699 * either for subnet or net. 700 */ 701 ina.s_addr == ia->ia_subnet || 702 ina.s_addr == ia->ia_net) { 703 /* Make sure M_BCAST is set */ 704 if (m) 705 m->m_flags |= M_BCAST; 706 return ia; 707 } 708 } 709 } 710 711 return NULL; 712 } 713 714 /* 715 * Take incoming datagram fragment and try to 716 * reassemble it into whole datagram. If a chain for 717 * reassembly of this datagram already exists, then it 718 * is given as fp; otherwise have to make a chain. 719 */ 720 struct mbuf * 721 ip_reass(ipqe, fp) 722 struct ipqent *ipqe; 723 struct ipq *fp; 724 { 725 struct mbuf *m = ipqe->ipqe_m; 726 struct ipqent *nq, *p, *q; 727 struct ip *ip; 728 struct mbuf *t; 729 int hlen = ipqe->ipqe_ip->ip_hl << 2; 730 int i, next; 731 u_int8_t ecn, ecn0; 732 733 /* 734 * Presence of header sizes in mbufs 735 * would confuse code below. 736 */ 737 m->m_data += hlen; 738 m->m_len -= hlen; 739 740 /* 741 * If first fragment to arrive, create a reassembly queue. 742 */ 743 if (fp == NULL) { 744 fp = pool_get(&ipq_pool, PR_NOWAIT); 745 if (fp == NULL) 746 goto dropfrag; 747 LIST_INSERT_HEAD(&ipq, fp, ipq_q); 748 fp->ipq_ttl = IPFRAGTTL; 749 fp->ipq_p = ipqe->ipqe_ip->ip_p; 750 fp->ipq_id = ipqe->ipqe_ip->ip_id; 751 LIST_INIT(&fp->ipq_fragq); 752 fp->ipq_src = ipqe->ipqe_ip->ip_src; 753 fp->ipq_dst = ipqe->ipqe_ip->ip_dst; 754 p = NULL; 755 goto insert; 756 } 757 758 /* 759 * Handle ECN by comparing this segment with the first one; 760 * if CE is set, do not lose CE. 761 * drop if CE and not-ECT are mixed for the same packet. 762 */ 763 ecn = ipqe->ipqe_ip->ip_tos & IPTOS_ECN_MASK; 764 ecn0 = LIST_FIRST(&fp->ipq_fragq)->ipqe_ip->ip_tos & IPTOS_ECN_MASK; 765 if (ecn == IPTOS_ECN_CE) { 766 if (ecn0 == IPTOS_ECN_NOTECT) 767 goto dropfrag; 768 if (ecn0 != IPTOS_ECN_CE) 769 LIST_FIRST(&fp->ipq_fragq)->ipqe_ip->ip_tos |= IPTOS_ECN_CE; 770 } 771 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 772 goto dropfrag; 773 774 /* 775 * Find a segment which begins after this one does. 776 */ 777 for (p = NULL, q = LIST_FIRST(&fp->ipq_fragq); 778 q != LIST_END(&fp->ipq_fragq); p = q, q = LIST_NEXT(q, ipqe_q)) 779 if (ntohs(q->ipqe_ip->ip_off) > ntohs(ipqe->ipqe_ip->ip_off)) 780 break; 781 782 /* 783 * If there is a preceding segment, it may provide some of 784 * our data already. If so, drop the data from the incoming 785 * segment. If it provides all of our data, drop us. 786 */ 787 if (p != NULL) { 788 i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) - 789 ntohs(ipqe->ipqe_ip->ip_off); 790 if (i > 0) { 791 if (i >= ntohs(ipqe->ipqe_ip->ip_len)) 792 goto dropfrag; 793 m_adj(ipqe->ipqe_m, i); 794 ipqe->ipqe_ip->ip_off = 795 htons(ntohs(ipqe->ipqe_ip->ip_off) + i); 796 ipqe->ipqe_ip->ip_len = 797 htons(ntohs(ipqe->ipqe_ip->ip_len) - i); 798 } 799 } 800 801 /* 802 * While we overlap succeeding segments trim them or, 803 * if they are completely covered, dequeue them. 804 */ 805 for (; q != NULL && 806 ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len) > 807 ntohs(q->ipqe_ip->ip_off); q = nq) { 808 i = (ntohs(ipqe->ipqe_ip->ip_off) + 809 ntohs(ipqe->ipqe_ip->ip_len)) - ntohs(q->ipqe_ip->ip_off); 810 if (i < ntohs(q->ipqe_ip->ip_len)) { 811 q->ipqe_ip->ip_len = 812 htons(ntohs(q->ipqe_ip->ip_len) - i); 813 q->ipqe_ip->ip_off = 814 htons(ntohs(q->ipqe_ip->ip_off) + i); 815 m_adj(q->ipqe_m, i); 816 break; 817 } 818 nq = LIST_NEXT(q, ipqe_q); 819 m_freem(q->ipqe_m); 820 LIST_REMOVE(q, ipqe_q); 821 pool_put(&ipqent_pool, q); 822 ip_frags--; 823 } 824 825 insert: 826 /* 827 * Stick new segment in its place; 828 * check for complete reassembly. 829 */ 830 if (p == NULL) { 831 LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q); 832 } else { 833 LIST_INSERT_AFTER(p, ipqe, ipqe_q); 834 } 835 next = 0; 836 for (p = NULL, q = LIST_FIRST(&fp->ipq_fragq); 837 q != LIST_END(&fp->ipq_fragq); p = q, q = LIST_NEXT(q, ipqe_q)) { 838 if (ntohs(q->ipqe_ip->ip_off) != next) 839 return (0); 840 next += ntohs(q->ipqe_ip->ip_len); 841 } 842 if (p->ipqe_mff) 843 return (0); 844 845 /* 846 * Reassembly is complete. Check for a bogus message size and 847 * concatenate fragments. 848 */ 849 q = LIST_FIRST(&fp->ipq_fragq); 850 ip = q->ipqe_ip; 851 if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) { 852 ipstat.ips_toolong++; 853 ip_freef(fp); 854 return (0); 855 } 856 m = q->ipqe_m; 857 t = m->m_next; 858 m->m_next = 0; 859 m_cat(m, t); 860 nq = LIST_NEXT(q, ipqe_q); 861 pool_put(&ipqent_pool, q); 862 ip_frags--; 863 for (q = nq; q != NULL; q = nq) { 864 t = q->ipqe_m; 865 nq = LIST_NEXT(q, ipqe_q); 866 pool_put(&ipqent_pool, q); 867 ip_frags--; 868 m_cat(m, t); 869 } 870 871 /* 872 * Create header for new ip packet by 873 * modifying header of first packet; 874 * dequeue and discard fragment reassembly header. 875 * Make header visible. 876 */ 877 ip->ip_len = htons(next); 878 ip->ip_src = fp->ipq_src; 879 ip->ip_dst = fp->ipq_dst; 880 LIST_REMOVE(fp, ipq_q); 881 pool_put(&ipq_pool, fp); 882 m->m_len += (ip->ip_hl << 2); 883 m->m_data -= (ip->ip_hl << 2); 884 /* some debugging cruft by sklower, below, will go away soon */ 885 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 886 int plen = 0; 887 for (t = m; t; t = t->m_next) 888 plen += t->m_len; 889 m->m_pkthdr.len = plen; 890 } 891 return (m); 892 893 dropfrag: 894 ipstat.ips_fragdropped++; 895 m_freem(m); 896 pool_put(&ipqent_pool, ipqe); 897 ip_frags--; 898 return (0); 899 } 900 901 /* 902 * Free a fragment reassembly header and all 903 * associated datagrams. 904 */ 905 void 906 ip_freef(fp) 907 struct ipq *fp; 908 { 909 struct ipqent *q, *p; 910 911 for (q = LIST_FIRST(&fp->ipq_fragq); q != LIST_END(&fp->ipq_fragq); 912 q = p) { 913 p = LIST_NEXT(q, ipqe_q); 914 m_freem(q->ipqe_m); 915 LIST_REMOVE(q, ipqe_q); 916 pool_put(&ipqent_pool, q); 917 ip_frags--; 918 } 919 LIST_REMOVE(fp, ipq_q); 920 pool_put(&ipq_pool, fp); 921 } 922 923 /* 924 * IP timer processing; 925 * if a timer expires on a reassembly queue, discard it. 926 * clear the forwarding cache, there might be a better route. 927 */ 928 void 929 ip_slowtimo() 930 { 931 struct ipq *fp, *nfp; 932 int s = splsoftnet(); 933 934 for (fp = LIST_FIRST(&ipq); fp != LIST_END(&ipq); fp = nfp) { 935 nfp = LIST_NEXT(fp, ipq_q); 936 if (--fp->ipq_ttl == 0) { 937 ipstat.ips_fragtimeout++; 938 ip_freef(fp); 939 } 940 } 941 if (ipforward_rt.ro_rt) { 942 RTFREE(ipforward_rt.ro_rt); 943 ipforward_rt.ro_rt = 0; 944 } 945 splx(s); 946 } 947 948 /* 949 * Drain off all datagram fragments. 950 */ 951 void 952 ip_drain() 953 { 954 955 while (!LIST_EMPTY(&ipq)) { 956 ipstat.ips_fragdropped++; 957 ip_freef(LIST_FIRST(&ipq)); 958 } 959 } 960 961 /* 962 * Flush a bunch of datagram fragments, till we are down to 75%. 963 */ 964 void 965 ip_flush() 966 { 967 int max = 50; 968 969 /* ipq already locked */ 970 while (!LIST_EMPTY(&ipq) && ip_frags > ip_maxqueue * 3 / 4 && --max) { 971 ipstat.ips_fragdropped++; 972 ip_freef(LIST_FIRST(&ipq)); 973 } 974 } 975 976 /* 977 * Do option processing on a datagram, 978 * possibly discarding it if bad options are encountered, 979 * or forwarding it if source-routed. 980 * Returns 1 if packet has been forwarded/freed, 981 * 0 if the packet should be processed further. 982 */ 983 int 984 ip_dooptions(m) 985 struct mbuf *m; 986 { 987 struct ip *ip = mtod(m, struct ip *); 988 u_char *cp; 989 struct ip_timestamp ipt; 990 struct in_ifaddr *ia; 991 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 992 struct in_addr sin, dst; 993 n_time ntime; 994 995 dst = ip->ip_dst; 996 cp = (u_char *)(ip + 1); 997 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 998 999 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1000 opt = cp[IPOPT_OPTVAL]; 1001 if (opt == IPOPT_EOL) 1002 break; 1003 if (opt == IPOPT_NOP) 1004 optlen = 1; 1005 else { 1006 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1007 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1008 goto bad; 1009 } 1010 optlen = cp[IPOPT_OLEN]; 1011 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1012 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1013 goto bad; 1014 } 1015 } 1016 1017 switch (opt) { 1018 1019 default: 1020 break; 1021 1022 /* 1023 * Source routing with record. 1024 * Find interface with current destination address. 1025 * If none on this machine then drop if strictly routed, 1026 * or do nothing if loosely routed. 1027 * Record interface address and bring up next address 1028 * component. If strictly routed make sure next 1029 * address is on directly accessible net. 1030 */ 1031 case IPOPT_LSRR: 1032 case IPOPT_SSRR: 1033 if (!ip_dosourceroute) { 1034 type = ICMP_UNREACH; 1035 code = ICMP_UNREACH_SRCFAIL; 1036 goto bad; 1037 } 1038 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1039 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1040 goto bad; 1041 } 1042 ipaddr.sin_addr = ip->ip_dst; 1043 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr), 1044 m->m_pkthdr.rdomain)); 1045 if (ia == 0) { 1046 if (opt == IPOPT_SSRR) { 1047 type = ICMP_UNREACH; 1048 code = ICMP_UNREACH_SRCFAIL; 1049 goto bad; 1050 } 1051 /* 1052 * Loose routing, and not at next destination 1053 * yet; nothing to do except forward. 1054 */ 1055 break; 1056 } 1057 off--; /* 0 origin */ 1058 if ((off + sizeof(struct in_addr)) > optlen) { 1059 /* 1060 * End of source route. Should be for us. 1061 */ 1062 save_rte(cp, ip->ip_src); 1063 break; 1064 } 1065 1066 /* 1067 * locate outgoing interface 1068 */ 1069 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 1070 sizeof(ipaddr.sin_addr)); 1071 if (opt == IPOPT_SSRR) { 1072 #define INA struct in_ifaddr * 1073 #define SA struct sockaddr * 1074 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr, 1075 m->m_pkthdr.rdomain)) == 0) 1076 ia = (INA)ifa_ifwithnet((SA)&ipaddr, 1077 m->m_pkthdr.rdomain); 1078 } else 1079 /* keep packet in the original VRF instance */ 1080 /* XXX rdomain or rtableid ??? */ 1081 ia = ip_rtaddr(ipaddr.sin_addr, 1082 m->m_pkthdr.rdomain); 1083 if (ia == 0) { 1084 type = ICMP_UNREACH; 1085 code = ICMP_UNREACH_SRCFAIL; 1086 goto bad; 1087 } 1088 ip->ip_dst = ipaddr.sin_addr; 1089 bcopy((caddr_t)&ia->ia_addr.sin_addr, 1090 (caddr_t)(cp + off), sizeof(struct in_addr)); 1091 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1092 /* 1093 * Let ip_intr's mcast routing check handle mcast pkts 1094 */ 1095 forward = !IN_MULTICAST(ip->ip_dst.s_addr); 1096 break; 1097 1098 case IPOPT_RR: 1099 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1100 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1101 goto bad; 1102 } 1103 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1104 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1105 goto bad; 1106 } 1107 1108 /* 1109 * If no space remains, ignore. 1110 */ 1111 off--; /* 0 origin */ 1112 if ((off + sizeof(struct in_addr)) > optlen) 1113 break; 1114 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 1115 sizeof(ipaddr.sin_addr)); 1116 /* 1117 * locate outgoing interface; if we're the destination, 1118 * use the incoming interface (should be same). 1119 * Again keep the packet inside the VRF instance. 1120 * XXX rdomain vs. rtableid ??? 1121 */ 1122 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr, 1123 m->m_pkthdr.rdomain)) == 0 && 1124 (ia = ip_rtaddr(ipaddr.sin_addr, 1125 m->m_pkthdr.rdomain)) == 0) { 1126 type = ICMP_UNREACH; 1127 code = ICMP_UNREACH_HOST; 1128 goto bad; 1129 } 1130 bcopy((caddr_t)&ia->ia_addr.sin_addr, 1131 (caddr_t)(cp + off), sizeof(struct in_addr)); 1132 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1133 break; 1134 1135 case IPOPT_TS: 1136 code = cp - (u_char *)ip; 1137 if (optlen < sizeof(struct ip_timestamp)) 1138 goto bad; 1139 bcopy(cp, &ipt, sizeof(struct ip_timestamp)); 1140 if (ipt.ipt_ptr < 5 || ipt.ipt_len < 5) 1141 goto bad; 1142 if (ipt.ipt_ptr - 1 + sizeof(n_time) > ipt.ipt_len) { 1143 if (++ipt.ipt_oflw == 0) 1144 goto bad; 1145 break; 1146 } 1147 bcopy(cp + ipt.ipt_ptr - 1, &sin, sizeof sin); 1148 switch (ipt.ipt_flg) { 1149 1150 case IPOPT_TS_TSONLY: 1151 break; 1152 1153 case IPOPT_TS_TSANDADDR: 1154 if (ipt.ipt_ptr - 1 + sizeof(n_time) + 1155 sizeof(struct in_addr) > ipt.ipt_len) 1156 goto bad; 1157 ipaddr.sin_addr = dst; 1158 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1159 m->m_pkthdr.rcvif); 1160 if (ia == 0) 1161 continue; 1162 bcopy((caddr_t)&ia->ia_addr.sin_addr, 1163 (caddr_t)&sin, sizeof(struct in_addr)); 1164 ipt.ipt_ptr += sizeof(struct in_addr); 1165 break; 1166 1167 case IPOPT_TS_PRESPEC: 1168 if (ipt.ipt_ptr - 1 + sizeof(n_time) + 1169 sizeof(struct in_addr) > ipt.ipt_len) 1170 goto bad; 1171 bcopy((caddr_t)&sin, (caddr_t)&ipaddr.sin_addr, 1172 sizeof(struct in_addr)); 1173 if (ifa_ifwithaddr((SA)&ipaddr, 1174 m->m_pkthdr.rdomain) == 0) 1175 continue; 1176 ipt.ipt_ptr += sizeof(struct in_addr); 1177 break; 1178 1179 default: 1180 /* XXX can't take &ipt->ipt_flg */ 1181 code = (u_char *)&ipt.ipt_ptr - 1182 (u_char *)ip + 1; 1183 goto bad; 1184 } 1185 ntime = iptime(); 1186 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt.ipt_ptr - 1, 1187 sizeof(n_time)); 1188 ipt.ipt_ptr += sizeof(n_time); 1189 } 1190 } 1191 if (forward && ipforwarding) { 1192 ip_forward(m, 1); 1193 return (1); 1194 } 1195 return (0); 1196 bad: 1197 icmp_error(m, type, code, 0, 0); 1198 ipstat.ips_badoptions++; 1199 return (1); 1200 } 1201 1202 /* 1203 * Given address of next destination (final or next hop), 1204 * return internet address info of interface to be used to get there. 1205 */ 1206 struct in_ifaddr * 1207 ip_rtaddr(struct in_addr dst, u_int rtableid) 1208 { 1209 struct sockaddr_in *sin; 1210 1211 sin = satosin(&ipforward_rt.ro_dst); 1212 1213 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 1214 if (ipforward_rt.ro_rt) { 1215 RTFREE(ipforward_rt.ro_rt); 1216 ipforward_rt.ro_rt = 0; 1217 } 1218 sin->sin_family = AF_INET; 1219 sin->sin_len = sizeof(*sin); 1220 sin->sin_addr = dst; 1221 1222 ipforward_rt.ro_rt = rtalloc1(&ipforward_rt.ro_dst, 1, 1223 rtableid); 1224 } 1225 if (ipforward_rt.ro_rt == 0) 1226 return ((struct in_ifaddr *)0); 1227 return (ifatoia(ipforward_rt.ro_rt->rt_ifa)); 1228 } 1229 1230 /* 1231 * Save incoming source route for use in replies, 1232 * to be picked up later by ip_srcroute if the receiver is interested. 1233 */ 1234 void 1235 save_rte(option, dst) 1236 u_char *option; 1237 struct in_addr dst; 1238 { 1239 unsigned olen; 1240 1241 olen = option[IPOPT_OLEN]; 1242 #ifdef DIAGNOSTIC 1243 if (ipprintfs) 1244 printf("save_rte: olen %d\n", olen); 1245 #endif /* 0 */ 1246 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1247 return; 1248 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 1249 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1250 ip_srcrt.dst = dst; 1251 } 1252 1253 /* 1254 * Check whether we do proxy ARP for this address and we point to ourselves. 1255 * Code shamelessly copied from arplookup(). 1256 */ 1257 int 1258 ip_weadvertise(u_int32_t addr, u_int rtableid) 1259 { 1260 struct rtentry *rt; 1261 struct ifnet *ifp; 1262 struct ifaddr *ifa; 1263 struct sockaddr_inarp sin; 1264 1265 sin.sin_len = sizeof(sin); 1266 sin.sin_family = AF_INET; 1267 sin.sin_addr.s_addr = addr; 1268 sin.sin_other = SIN_PROXY; 1269 rt = rtalloc1(sintosa(&sin), 0, rtableid); 1270 if (rt == 0) 1271 return 0; 1272 1273 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || 1274 rt->rt_gateway->sa_family != AF_LINK) { 1275 RTFREE(rt); 1276 return 0; 1277 } 1278 1279 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1280 if (ifp->if_rdomain != rtableid) 1281 continue; 1282 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1283 if (ifa->ifa_addr->sa_family != rt->rt_gateway->sa_family) 1284 continue; 1285 1286 if (!bcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr), 1287 LLADDR((struct sockaddr_dl *)rt->rt_gateway), 1288 ETHER_ADDR_LEN)) { 1289 RTFREE(rt); 1290 return 1; 1291 } 1292 } 1293 } 1294 1295 RTFREE(rt); 1296 return 0; 1297 } 1298 1299 /* 1300 * Retrieve incoming source route for use in replies, 1301 * in the same form used by setsockopt. 1302 * The first hop is placed before the options, will be removed later. 1303 */ 1304 struct mbuf * 1305 ip_srcroute() 1306 { 1307 struct in_addr *p, *q; 1308 struct mbuf *m; 1309 1310 if (ip_nhops == 0) 1311 return ((struct mbuf *)0); 1312 m = m_get(M_DONTWAIT, MT_SOOPTS); 1313 if (m == 0) 1314 return ((struct mbuf *)0); 1315 1316 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1317 1318 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1319 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1320 OPTSIZ; 1321 #ifdef DIAGNOSTIC 1322 if (ipprintfs) 1323 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1324 #endif 1325 1326 /* 1327 * First save first hop for return route 1328 */ 1329 p = &ip_srcrt.route[ip_nhops - 1]; 1330 *(mtod(m, struct in_addr *)) = *p--; 1331 #ifdef DIAGNOSTIC 1332 if (ipprintfs) 1333 printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr)); 1334 #endif 1335 1336 /* 1337 * Copy option fields and padding (nop) to mbuf. 1338 */ 1339 ip_srcrt.nop = IPOPT_NOP; 1340 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1341 bcopy((caddr_t)&ip_srcrt.nop, 1342 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 1343 q = (struct in_addr *)(mtod(m, caddr_t) + 1344 sizeof(struct in_addr) + OPTSIZ); 1345 #undef OPTSIZ 1346 /* 1347 * Record return path as an IP source route, 1348 * reversing the path (pointers are now aligned). 1349 */ 1350 while (p >= ip_srcrt.route) { 1351 #ifdef DIAGNOSTIC 1352 if (ipprintfs) 1353 printf(" %x", ntohl(q->s_addr)); 1354 #endif 1355 *q++ = *p--; 1356 } 1357 /* 1358 * Last hop goes to final destination. 1359 */ 1360 *q = ip_srcrt.dst; 1361 #ifdef DIAGNOSTIC 1362 if (ipprintfs) 1363 printf(" %x\n", ntohl(q->s_addr)); 1364 #endif 1365 return (m); 1366 } 1367 1368 /* 1369 * Strip out IP options, at higher 1370 * level protocol in the kernel. 1371 * Second argument is buffer to which options 1372 * will be moved, and return value is their length. 1373 * XXX should be deleted; last arg currently ignored. 1374 */ 1375 void 1376 ip_stripoptions(m, mopt) 1377 struct mbuf *m; 1378 struct mbuf *mopt; 1379 { 1380 int i; 1381 struct ip *ip = mtod(m, struct ip *); 1382 caddr_t opts; 1383 int olen; 1384 1385 olen = (ip->ip_hl<<2) - sizeof (struct ip); 1386 opts = (caddr_t)(ip + 1); 1387 i = m->m_len - (sizeof (struct ip) + olen); 1388 bcopy(opts + olen, opts, (unsigned)i); 1389 m->m_len -= olen; 1390 if (m->m_flags & M_PKTHDR) 1391 m->m_pkthdr.len -= olen; 1392 ip->ip_hl = sizeof(struct ip) >> 2; 1393 } 1394 1395 int inetctlerrmap[PRC_NCMDS] = { 1396 0, 0, 0, 0, 1397 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1398 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1399 EMSGSIZE, EHOSTUNREACH, 0, 0, 1400 0, 0, 0, 0, 1401 ENOPROTOOPT 1402 }; 1403 1404 /* 1405 * Forward a packet. If some error occurs return the sender 1406 * an icmp packet. Note we can't always generate a meaningful 1407 * icmp message because icmp doesn't have a large enough repertoire 1408 * of codes and types. 1409 * 1410 * If not forwarding, just drop the packet. This could be confusing 1411 * if ipforwarding was zero but some routing protocol was advancing 1412 * us as a gateway to somewhere. However, we must let the routing 1413 * protocol deal with that. 1414 * 1415 * The srcrt parameter indicates whether the packet is being forwarded 1416 * via a source route. 1417 */ 1418 void 1419 ip_forward(m, srcrt) 1420 struct mbuf *m; 1421 int srcrt; 1422 { 1423 struct ip *ip = mtod(m, struct ip *); 1424 struct sockaddr_in *sin; 1425 struct rtentry *rt; 1426 int error, type = 0, code = 0, destmtu = 0; 1427 u_int rtableid = 0; 1428 struct mbuf *mcopy; 1429 n_long dest; 1430 1431 dest = 0; 1432 #ifdef DIAGNOSTIC 1433 if (ipprintfs) 1434 printf("forward: src %x dst %x ttl %x\n", ip->ip_src.s_addr, 1435 ip->ip_dst.s_addr, ip->ip_ttl); 1436 #endif 1437 if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 1438 ipstat.ips_cantforward++; 1439 m_freem(m); 1440 return; 1441 } 1442 if (ip->ip_ttl <= IPTTLDEC) { 1443 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1444 return; 1445 } 1446 1447 rtableid = m->m_pkthdr.rdomain; 1448 if (m->m_pkthdr.pf.rtableid) 1449 rtableid = m->m_pkthdr.pf.rtableid; 1450 1451 sin = satosin(&ipforward_rt.ro_dst); 1452 if ((rt = ipforward_rt.ro_rt) == 0 || 1453 ip->ip_dst.s_addr != sin->sin_addr.s_addr || 1454 rtableid != ipforward_rtableid) { 1455 if (ipforward_rt.ro_rt) { 1456 RTFREE(ipforward_rt.ro_rt); 1457 ipforward_rt.ro_rt = 0; 1458 } 1459 sin->sin_family = AF_INET; 1460 sin->sin_len = sizeof(*sin); 1461 sin->sin_addr = ip->ip_dst; 1462 1463 rtalloc_mpath(&ipforward_rt, &ip->ip_src.s_addr, rtableid); 1464 if (ipforward_rt.ro_rt == 0) { 1465 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1466 return; 1467 } 1468 ipforward_rtableid = rtableid; 1469 rt = ipforward_rt.ro_rt; 1470 } 1471 1472 /* 1473 * Save at most 68 bytes of the packet in case 1474 * we need to generate an ICMP message to the src. 1475 * Pullup to avoid sharing mbuf cluster between m and mcopy. 1476 */ 1477 mcopy = m_copym(m, 0, min(ntohs(ip->ip_len), 68), M_DONTWAIT); 1478 if (mcopy) 1479 mcopy = m_pullup(mcopy, min(ntohs(ip->ip_len), 68)); 1480 1481 ip->ip_ttl -= IPTTLDEC; 1482 1483 /* 1484 * If forwarding packet using same interface that it came in on, 1485 * perhaps should send a redirect to sender to shortcut a hop. 1486 * Only send redirect if source is sending directly to us, 1487 * and if packet was not source routed (or has any options). 1488 * Also, don't send redirect if forwarding using a default route 1489 * or a route modified by a redirect. 1490 * Don't send redirect if we advertise destination's arp address 1491 * as ours (proxy arp). 1492 */ 1493 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1494 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1495 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1496 ipsendredirects && !srcrt && 1497 !ip_weadvertise(satosin(rt_key(rt))->sin_addr.s_addr, 1498 m->m_pkthdr.rdomain)) { 1499 if (rt->rt_ifa && 1500 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) == 1501 ifatoia(rt->rt_ifa)->ia_subnet) { 1502 if (rt->rt_flags & RTF_GATEWAY) 1503 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1504 else 1505 dest = ip->ip_dst.s_addr; 1506 /* Router requirements says to only send host redirects */ 1507 type = ICMP_REDIRECT; 1508 code = ICMP_REDIRECT_HOST; 1509 #ifdef DIAGNOSTIC 1510 if (ipprintfs) 1511 printf("redirect (%d) to %x\n", code, (u_int32_t)dest); 1512 #endif 1513 } 1514 } 1515 1516 error = ip_output(m, (struct mbuf *)NULL, &ipforward_rt, 1517 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 1518 (void *)NULL, (void *)NULL); 1519 if (error) 1520 ipstat.ips_cantforward++; 1521 else { 1522 ipstat.ips_forward++; 1523 if (type) 1524 ipstat.ips_redirectsent++; 1525 else 1526 goto freecopy; 1527 } 1528 if (mcopy == NULL) 1529 goto freert; 1530 1531 switch (error) { 1532 1533 case 0: /* forwarded, but need redirect */ 1534 /* type, code set above */ 1535 break; 1536 1537 case ENETUNREACH: /* shouldn't happen, checked above */ 1538 case EHOSTUNREACH: 1539 case ENETDOWN: 1540 case EHOSTDOWN: 1541 default: 1542 type = ICMP_UNREACH; 1543 code = ICMP_UNREACH_HOST; 1544 break; 1545 1546 case EMSGSIZE: 1547 type = ICMP_UNREACH; 1548 code = ICMP_UNREACH_NEEDFRAG; 1549 1550 #ifdef IPSEC 1551 if (ipforward_rt.ro_rt) { 1552 struct rtentry *rt = ipforward_rt.ro_rt; 1553 1554 if (rt->rt_rmx.rmx_mtu) 1555 destmtu = rt->rt_rmx.rmx_mtu; 1556 else 1557 destmtu = ipforward_rt.ro_rt->rt_ifp->if_mtu; 1558 } 1559 #endif /*IPSEC*/ 1560 ipstat.ips_cantfrag++; 1561 break; 1562 1563 case ENOBUFS: 1564 /* 1565 * a router should not generate ICMP_SOURCEQUENCH as 1566 * required in RFC1812 Requirements for IP Version 4 Routers. 1567 * source quench could be a big problem under DoS attacks, 1568 * or the underlying interface is rate-limited. 1569 */ 1570 goto freecopy; 1571 } 1572 1573 icmp_error(mcopy, type, code, dest, destmtu); 1574 goto freert; 1575 1576 freecopy: 1577 if (mcopy) 1578 m_free(mcopy); 1579 freert: 1580 #ifndef SMALL_KERNEL 1581 if (ipmultipath && ipforward_rt.ro_rt && 1582 (ipforward_rt.ro_rt->rt_flags & RTF_MPATH)) { 1583 RTFREE(ipforward_rt.ro_rt); 1584 ipforward_rt.ro_rt = 0; 1585 } 1586 #endif 1587 return; 1588 } 1589 1590 int 1591 ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 1592 int *name; 1593 u_int namelen; 1594 void *oldp; 1595 size_t *oldlenp; 1596 void *newp; 1597 size_t newlen; 1598 { 1599 int error; 1600 #ifdef MROUTING 1601 extern int ip_mrtproto; 1602 extern struct mrtstat mrtstat; 1603 #endif 1604 1605 /* Almost all sysctl names at this level are terminal. */ 1606 if (namelen != 1 && name[0] != IPCTL_IFQUEUE) 1607 return (ENOTDIR); 1608 1609 switch (name[0]) { 1610 #ifdef notyet 1611 case IPCTL_DEFMTU: 1612 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu)); 1613 #endif 1614 case IPCTL_SOURCEROUTE: 1615 /* 1616 * Don't allow this to change in a secure environment. 1617 */ 1618 if (newp && securelevel > 0) 1619 return (EPERM); 1620 return (sysctl_int(oldp, oldlenp, newp, newlen, 1621 &ip_dosourceroute)); 1622 case IPCTL_MTUDISC: 1623 error = sysctl_int(oldp, oldlenp, newp, newlen, 1624 &ip_mtudisc); 1625 if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) { 1626 ip_mtudisc_timeout_q = 1627 rt_timer_queue_create(ip_mtudisc_timeout); 1628 } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) { 1629 rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE); 1630 Free(ip_mtudisc_timeout_q); 1631 ip_mtudisc_timeout_q = NULL; 1632 } 1633 return error; 1634 case IPCTL_MTUDISCTIMEOUT: 1635 error = sysctl_int(oldp, oldlenp, newp, newlen, 1636 &ip_mtudisc_timeout); 1637 if (ip_mtudisc_timeout_q != NULL) 1638 rt_timer_queue_change(ip_mtudisc_timeout_q, 1639 ip_mtudisc_timeout); 1640 return (error); 1641 case IPCTL_IPSEC_ENC_ALGORITHM: 1642 return (sysctl_tstring(oldp, oldlenp, newp, newlen, 1643 ipsec_def_enc, sizeof(ipsec_def_enc))); 1644 case IPCTL_IPSEC_AUTH_ALGORITHM: 1645 return (sysctl_tstring(oldp, oldlenp, newp, newlen, 1646 ipsec_def_auth, 1647 sizeof(ipsec_def_auth))); 1648 case IPCTL_IPSEC_IPCOMP_ALGORITHM: 1649 return (sysctl_tstring(oldp, oldlenp, newp, newlen, 1650 ipsec_def_comp, 1651 sizeof(ipsec_def_comp))); 1652 case IPCTL_IFQUEUE: 1653 return (sysctl_ifq(name + 1, namelen - 1, 1654 oldp, oldlenp, newp, newlen, &ipintrq)); 1655 case IPCTL_STATS: 1656 if (newp != NULL) 1657 return (EPERM); 1658 return (sysctl_struct(oldp, oldlenp, newp, newlen, 1659 &ipstat, sizeof(ipstat))); 1660 case IPCTL_MRTSTATS: 1661 #ifdef MROUTING 1662 if (newp != NULL) 1663 return (EPERM); 1664 return (sysctl_struct(oldp, oldlenp, newp, newlen, 1665 &mrtstat, sizeof(mrtstat))); 1666 #else 1667 return (EOPNOTSUPP); 1668 #endif 1669 case IPCTL_MRTPROTO: 1670 #ifdef MROUTING 1671 return (sysctl_rdint(oldp, oldlenp, newp, ip_mrtproto)); 1672 #else 1673 return (EOPNOTSUPP); 1674 #endif 1675 default: 1676 if (name[0] < IPCTL_MAXID) 1677 return (sysctl_int_arr(ipctl_vars, name, namelen, 1678 oldp, oldlenp, newp, newlen)); 1679 return (EOPNOTSUPP); 1680 } 1681 /* NOTREACHED */ 1682 } 1683 1684 void 1685 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 1686 struct mbuf *m) 1687 { 1688 #ifdef SO_TIMESTAMP 1689 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1690 struct timeval tv; 1691 1692 microtime(&tv); 1693 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1694 SCM_TIMESTAMP, SOL_SOCKET); 1695 if (*mp) 1696 mp = &(*mp)->m_next; 1697 } 1698 #endif 1699 if (inp->inp_flags & INP_RECVDSTADDR) { 1700 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1701 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1702 if (*mp) 1703 mp = &(*mp)->m_next; 1704 } 1705 #ifdef notyet 1706 /* this code is broken and will probably never be fixed. */ 1707 /* options were tossed already */ 1708 if (inp->inp_flags & INP_RECVOPTS) { 1709 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1710 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1711 if (*mp) 1712 mp = &(*mp)->m_next; 1713 } 1714 /* ip_srcroute doesn't do what we want here, need to fix */ 1715 if (inp->inp_flags & INP_RECVRETOPTS) { 1716 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1717 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1718 if (*mp) 1719 mp = &(*mp)->m_next; 1720 } 1721 #endif 1722 if (inp->inp_flags & INP_RECVIF) { 1723 struct sockaddr_dl sdl; 1724 struct ifnet *ifp; 1725 1726 if ((ifp = m->m_pkthdr.rcvif) == NULL || 1727 ifp->if_sadl == NULL) { 1728 bzero(&sdl, sizeof(sdl)); 1729 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]); 1730 sdl.sdl_family = AF_LINK; 1731 sdl.sdl_index = ifp != NULL ? ifp->if_index : 0; 1732 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0; 1733 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len, 1734 IP_RECVIF, IPPROTO_IP); 1735 } else { 1736 *mp = sbcreatecontrol((caddr_t) ifp->if_sadl, 1737 ifp->if_sadl->sdl_len, IP_RECVIF, IPPROTO_IP); 1738 } 1739 if (*mp) 1740 mp = &(*mp)->m_next; 1741 } 1742 if (inp->inp_flags & INP_RECVTTL) { 1743 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl, 1744 sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 1745 if (*mp) 1746 mp = &(*mp)->m_next; 1747 } 1748 } 1749 1750