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