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