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