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