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