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