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