ip_input.c revision 1.214
1/* $NetBSD: ip_input.c,v 1.214 2005/04/18 21:50:25 yamt Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32/*- 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the NetBSD 51 * Foundation, Inc. and its contributors. 52 * 4. Neither the name of The NetBSD Foundation nor the names of its 53 * contributors may be used to endorse or promote products derived 54 * from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69/* 70 * Copyright (c) 1982, 1986, 1988, 1993 71 * The Regents of the University of California. All rights reserved. 72 * 73 * Redistribution and use in source and binary forms, with or without 74 * modification, are permitted provided that the following conditions 75 * are met: 76 * 1. Redistributions of source code must retain the above copyright 77 * notice, this list of conditions and the following disclaimer. 78 * 2. Redistributions in binary form must reproduce the above copyright 79 * notice, this list of conditions and the following disclaimer in the 80 * documentation and/or other materials provided with the distribution. 81 * 3. Neither the name of the University nor the names of its contributors 82 * may be used to endorse or promote products derived from this software 83 * without specific prior written permission. 84 * 85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 95 * SUCH DAMAGE. 96 * 97 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 98 */ 99 100#include <sys/cdefs.h> 101__KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.214 2005/04/18 21:50:25 yamt Exp $"); 102 103#include "opt_inet.h" 104#include "opt_gateway.h" 105#include "opt_pfil_hooks.h" 106#include "opt_ipsec.h" 107#include "opt_mrouting.h" 108#include "opt_mbuftrace.h" 109#include "opt_inet_csum.h" 110 111#include <sys/param.h> 112#include <sys/systm.h> 113#include <sys/malloc.h> 114#include <sys/mbuf.h> 115#include <sys/domain.h> 116#include <sys/protosw.h> 117#include <sys/socket.h> 118#include <sys/socketvar.h> 119#include <sys/errno.h> 120#include <sys/time.h> 121#include <sys/kernel.h> 122#include <sys/pool.h> 123#include <sys/sysctl.h> 124 125#include <net/if.h> 126#include <net/if_dl.h> 127#include <net/route.h> 128#include <net/pfil.h> 129 130#include <netinet/in.h> 131#include <netinet/in_systm.h> 132#include <netinet/ip.h> 133#include <netinet/in_pcb.h> 134#include <netinet/in_var.h> 135#include <netinet/ip_var.h> 136#include <netinet/ip_icmp.h> 137/* just for gif_ttl */ 138#include <netinet/in_gif.h> 139#include "gif.h" 140#include <net/if_gre.h> 141#include "gre.h" 142 143#ifdef MROUTING 144#include <netinet/ip_mroute.h> 145#endif 146 147#ifdef IPSEC 148#include <netinet6/ipsec.h> 149#include <netkey/key.h> 150#endif 151#ifdef FAST_IPSEC 152#include <netipsec/ipsec.h> 153#include <netipsec/key.h> 154#endif /* FAST_IPSEC*/ 155 156#ifndef IPFORWARDING 157#ifdef GATEWAY 158#define IPFORWARDING 1 /* forward IP packets not for us */ 159#else /* GATEWAY */ 160#define IPFORWARDING 0 /* don't forward IP packets not for us */ 161#endif /* GATEWAY */ 162#endif /* IPFORWARDING */ 163#ifndef IPSENDREDIRECTS 164#define IPSENDREDIRECTS 1 165#endif 166#ifndef IPFORWSRCRT 167#define IPFORWSRCRT 1 /* forward source-routed packets */ 168#endif 169#ifndef IPALLOWSRCRT 170#define IPALLOWSRCRT 1 /* allow source-routed packets */ 171#endif 172#ifndef IPMTUDISC 173#define IPMTUDISC 1 174#endif 175#ifndef IPMTUDISCTIMEOUT 176#define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */ 177#endif 178 179/* 180 * Note: DIRECTED_BROADCAST is handled this way so that previous 181 * configuration using this option will Just Work. 182 */ 183#ifndef IPDIRECTEDBCAST 184#ifdef DIRECTED_BROADCAST 185#define IPDIRECTEDBCAST 1 186#else 187#define IPDIRECTEDBCAST 0 188#endif /* DIRECTED_BROADCAST */ 189#endif /* IPDIRECTEDBCAST */ 190int ipforwarding = IPFORWARDING; 191int ipsendredirects = IPSENDREDIRECTS; 192int ip_defttl = IPDEFTTL; 193int ip_forwsrcrt = IPFORWSRCRT; 194int ip_directedbcast = IPDIRECTEDBCAST; 195int ip_allowsrcrt = IPALLOWSRCRT; 196int ip_mtudisc = IPMTUDISC; 197int ip_mtudisc_timeout = IPMTUDISCTIMEOUT; 198#ifdef DIAGNOSTIC 199int ipprintfs = 0; 200#endif 201 202int ip_do_randomid = 0; 203 204/* 205 * XXX - Setting ip_checkinterface mostly implements the receive side of 206 * the Strong ES model described in RFC 1122, but since the routing table 207 * and transmit implementation do not implement the Strong ES model, 208 * setting this to 1 results in an odd hybrid. 209 * 210 * XXX - ip_checkinterface currently must be disabled if you use ipnat 211 * to translate the destination address to another local interface. 212 * 213 * XXX - ip_checkinterface must be disabled if you add IP aliases 214 * to the loopback interface instead of the interface where the 215 * packets for those addresses are received. 216 */ 217int ip_checkinterface = 0; 218 219 220struct rttimer_queue *ip_mtudisc_timeout_q = NULL; 221 222int ipqmaxlen = IFQ_MAXLEN; 223u_long in_ifaddrhash; /* size of hash table - 1 */ 224int in_ifaddrentries; /* total number of addrs */ 225struct in_ifaddrhead in_ifaddrhead; 226struct in_ifaddrhashhead *in_ifaddrhashtbl; 227u_long in_multihash; /* size of hash table - 1 */ 228int in_multientries; /* total number of addrs */ 229struct in_multihashhead *in_multihashtbl; 230struct ifqueue ipintrq; 231struct ipstat ipstat; 232uint16_t ip_id; 233 234#ifdef PFIL_HOOKS 235struct pfil_head inet_pfil_hook; 236#endif 237 238/* 239 * Cached copy of nmbclusters. If nbclusters is different, 240 * recalculate IP parameters derived from nmbclusters. 241 */ 242static int ip_nmbclusters; /* copy of nmbclusters */ 243static void ip_nmbclusters_changed(void); /* recalc limits */ 244 245#define CHECK_NMBCLUSTER_PARAMS() \ 246do { \ 247 if (__predict_false(ip_nmbclusters != nmbclusters)) \ 248 ip_nmbclusters_changed(); \ 249} while (/*CONSTCOND*/0) 250 251/* IP datagram reassembly queues (hashed) */ 252#define IPREASS_NHASH_LOG2 6 253#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) 254#define IPREASS_HMASK (IPREASS_NHASH - 1) 255#define IPREASS_HASH(x,y) \ 256 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) 257struct ipqhead ipq[IPREASS_NHASH]; 258int ipq_locked; 259static int ip_nfragpackets; /* packets in reass queue */ 260static int ip_nfrags; /* total fragments in reass queues */ 261 262int ip_maxfragpackets = 200; /* limit on packets. XXX sysctl */ 263int ip_maxfrags; /* limit on fragments. XXX sysctl */ 264 265 266/* 267 * Additive-Increase/Multiplicative-Decrease (AIMD) strategy for 268 * IP reassembly queue buffer managment. 269 * 270 * We keep a count of total IP fragments (NB: not fragmented packets!) 271 * awaiting reassembly (ip_nfrags) and a limit (ip_maxfrags) on fragments. 272 * If ip_nfrags exceeds ip_maxfrags the limit, we drop half the 273 * total fragments in reassembly queues.This AIMD policy avoids 274 * repeatedly deleting single packets under heavy fragmentation load 275 * (e.g., from lossy NFS peers). 276 */ 277static u_int ip_reass_ttl_decr(u_int ticks); 278static void ip_reass_drophalf(void); 279 280 281static __inline int ipq_lock_try(void); 282static __inline void ipq_unlock(void); 283 284static __inline int 285ipq_lock_try(void) 286{ 287 int s; 288 289 /* 290 * Use splvm() -- we're blocking things that would cause 291 * mbuf allocation. 292 */ 293 s = splvm(); 294 if (ipq_locked) { 295 splx(s); 296 return (0); 297 } 298 ipq_locked = 1; 299 splx(s); 300 return (1); 301} 302 303static __inline void 304ipq_unlock(void) 305{ 306 int s; 307 308 s = splvm(); 309 ipq_locked = 0; 310 splx(s); 311} 312 313#ifdef DIAGNOSTIC 314#define IPQ_LOCK() \ 315do { \ 316 if (ipq_lock_try() == 0) { \ 317 printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \ 318 panic("ipq_lock"); \ 319 } \ 320} while (/*CONSTCOND*/ 0) 321#define IPQ_LOCK_CHECK() \ 322do { \ 323 if (ipq_locked == 0) { \ 324 printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \ 325 panic("ipq lock check"); \ 326 } \ 327} while (/*CONSTCOND*/ 0) 328#else 329#define IPQ_LOCK() (void) ipq_lock_try() 330#define IPQ_LOCK_CHECK() /* nothing */ 331#endif 332 333#define IPQ_UNLOCK() ipq_unlock() 334 335POOL_INIT(inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl", NULL); 336POOL_INIT(ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl", NULL); 337 338#ifdef INET_CSUM_COUNTERS 339#include <sys/device.h> 340 341struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 342 NULL, "inet", "hwcsum bad"); 343struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 344 NULL, "inet", "hwcsum ok"); 345struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 346 NULL, "inet", "swcsum"); 347 348#define INET_CSUM_COUNTER_INCR(ev) (ev)->ev_count++ 349 350EVCNT_ATTACH_STATIC(ip_hwcsum_bad); 351EVCNT_ATTACH_STATIC(ip_hwcsum_ok); 352EVCNT_ATTACH_STATIC(ip_swcsum); 353 354#else 355 356#define INET_CSUM_COUNTER_INCR(ev) /* nothing */ 357 358#endif /* INET_CSUM_COUNTERS */ 359 360/* 361 * We need to save the IP options in case a protocol wants to respond 362 * to an incoming packet over the same route if the packet got here 363 * using IP source routing. This allows connection establishment and 364 * maintenance when the remote end is on a network that is not known 365 * to us. 366 */ 367int ip_nhops = 0; 368static struct ip_srcrt { 369 struct in_addr dst; /* final destination */ 370 char nop; /* one NOP to align */ 371 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 372 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 373} ip_srcrt; 374 375static void save_rte(u_char *, struct in_addr); 376 377#ifdef MBUFTRACE 378struct mowner ip_rx_mowner = { "internet", "rx" }; 379struct mowner ip_tx_mowner = { "internet", "tx" }; 380#endif 381 382/* 383 * Compute IP limits derived from the value of nmbclusters. 384 */ 385static void 386ip_nmbclusters_changed(void) 387{ 388 ip_maxfrags = nmbclusters / 4; 389 ip_nmbclusters = nmbclusters; 390} 391 392/* 393 * IP initialization: fill in IP protocol switch table. 394 * All protocols not implemented in kernel go to raw IP protocol handler. 395 */ 396void 397ip_init(void) 398{ 399 const struct protosw *pr; 400 int i; 401 402 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 403 if (pr == 0) 404 panic("ip_init"); 405 for (i = 0; i < IPPROTO_MAX; i++) 406 ip_protox[i] = pr - inetsw; 407 for (pr = inetdomain.dom_protosw; 408 pr < inetdomain.dom_protoswNPROTOSW; pr++) 409 if (pr->pr_domain->dom_family == PF_INET && 410 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 411 ip_protox[pr->pr_protocol] = pr - inetsw; 412 413 for (i = 0; i < IPREASS_NHASH; i++) 414 LIST_INIT(&ipq[i]); 415 416 ip_id = time.tv_sec & 0xfffff; 417 418 ipintrq.ifq_maxlen = ipqmaxlen; 419 ip_nmbclusters_changed(); 420 421 TAILQ_INIT(&in_ifaddrhead); 422 in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IFADDR, 423 M_WAITOK, &in_ifaddrhash); 424 in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IPMADDR, 425 M_WAITOK, &in_multihash); 426 ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout); 427#ifdef GATEWAY 428 ipflow_init(); 429#endif 430 431#ifdef PFIL_HOOKS 432 /* Register our Packet Filter hook. */ 433 inet_pfil_hook.ph_type = PFIL_TYPE_AF; 434 inet_pfil_hook.ph_af = AF_INET; 435 i = pfil_head_register(&inet_pfil_hook); 436 if (i != 0) 437 printf("ip_init: WARNING: unable to register pfil hook, " 438 "error %d\n", i); 439#endif /* PFIL_HOOKS */ 440 441#ifdef MBUFTRACE 442 MOWNER_ATTACH(&ip_tx_mowner); 443 MOWNER_ATTACH(&ip_rx_mowner); 444#endif /* MBUFTRACE */ 445} 446 447struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 448struct route ipforward_rt; 449 450/* 451 * IP software interrupt routine 452 */ 453void 454ipintr(void) 455{ 456 int s; 457 struct mbuf *m; 458 459 while (1) { 460 s = splnet(); 461 IF_DEQUEUE(&ipintrq, m); 462 splx(s); 463 if (m == 0) 464 return; 465 MCLAIM(m, &ip_rx_mowner); 466 ip_input(m); 467 } 468} 469 470/* 471 * Ip input routine. Checksum and byte swap header. If fragmented 472 * try to reassemble. Process options. Pass to next level. 473 */ 474void 475ip_input(struct mbuf *m) 476{ 477 struct ip *ip = NULL; 478 struct ipq *fp; 479 struct in_ifaddr *ia; 480 struct ifaddr *ifa; 481 struct ipqent *ipqe; 482 int hlen = 0, mff, len; 483 int downmatch; 484 int checkif; 485 int srcrt = 0; 486 u_int hash; 487#ifdef FAST_IPSEC 488 struct m_tag *mtag; 489 struct tdb_ident *tdbi; 490 struct secpolicy *sp; 491 int s, error; 492#endif /* FAST_IPSEC */ 493 494 MCLAIM(m, &ip_rx_mowner); 495#ifdef DIAGNOSTIC 496 if ((m->m_flags & M_PKTHDR) == 0) 497 panic("ipintr no HDR"); 498#endif 499 500 /* 501 * If no IP addresses have been set yet but the interfaces 502 * are receiving, can't do anything with incoming packets yet. 503 */ 504 if (TAILQ_FIRST(&in_ifaddrhead) == 0) 505 goto bad; 506 ipstat.ips_total++; 507 /* 508 * If the IP header is not aligned, slurp it up into a new 509 * mbuf with space for link headers, in the event we forward 510 * it. Otherwise, if it is aligned, make sure the entire 511 * base IP header is in the first mbuf of the chain. 512 */ 513 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { 514 if ((m = m_copyup(m, sizeof(struct ip), 515 (max_linkhdr + 3) & ~3)) == NULL) { 516 /* XXXJRT new stat, please */ 517 ipstat.ips_toosmall++; 518 return; 519 } 520 } else if (__predict_false(m->m_len < sizeof (struct ip))) { 521 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) { 522 ipstat.ips_toosmall++; 523 return; 524 } 525 } 526 ip = mtod(m, struct ip *); 527 if (ip->ip_v != IPVERSION) { 528 ipstat.ips_badvers++; 529 goto bad; 530 } 531 hlen = ip->ip_hl << 2; 532 if (hlen < sizeof(struct ip)) { /* minimum header length */ 533 ipstat.ips_badhlen++; 534 goto bad; 535 } 536 if (hlen > m->m_len) { 537 if ((m = m_pullup(m, hlen)) == 0) { 538 ipstat.ips_badhlen++; 539 return; 540 } 541 ip = mtod(m, struct ip *); 542 } 543 544 /* 545 * RFC1122: packets with a multicast source address are 546 * not allowed. 547 */ 548 if (IN_MULTICAST(ip->ip_src.s_addr)) { 549 ipstat.ips_badaddr++; 550 goto bad; 551 } 552 553 /* 127/8 must not appear on wire - RFC1122 */ 554 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 555 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 556 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 557 ipstat.ips_badaddr++; 558 goto bad; 559 } 560 } 561 562 switch (m->m_pkthdr.csum_flags & 563 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) | 564 M_CSUM_IPv4_BAD)) { 565 case M_CSUM_IPv4|M_CSUM_IPv4_BAD: 566 INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); 567 goto badcsum; 568 569 case M_CSUM_IPv4: 570 /* Checksum was okay. */ 571 INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); 572 break; 573 574 default: 575 /* 576 * Must compute it ourselves. Maybe skip checksum on 577 * loopback interfaces. 578 */ 579 if (__predict_true(!(m->m_pkthdr.rcvif->if_flags & 580 IFF_LOOPBACK) || ip_do_loopback_cksum)) { 581 INET_CSUM_COUNTER_INCR(&ip_swcsum); 582 if (in_cksum(m, hlen) != 0) 583 goto badcsum; 584 } 585 break; 586 } 587 588 /* Retrieve the packet length. */ 589 len = ntohs(ip->ip_len); 590 591 /* 592 * Check for additional length bogosity 593 */ 594 if (len < hlen) { 595 ipstat.ips_badlen++; 596 goto bad; 597 } 598 599 /* 600 * Check that the amount of data in the buffers 601 * is as at least much as the IP header would have us expect. 602 * Trim mbufs if longer than we expect. 603 * Drop packet if shorter than we expect. 604 */ 605 if (m->m_pkthdr.len < len) { 606 ipstat.ips_tooshort++; 607 goto bad; 608 } 609 if (m->m_pkthdr.len > len) { 610 if (m->m_len == m->m_pkthdr.len) { 611 m->m_len = len; 612 m->m_pkthdr.len = len; 613 } else 614 m_adj(m, len - m->m_pkthdr.len); 615 } 616 617#if defined(IPSEC) 618 /* ipflow (IP fast forwarding) is not compatible with IPsec. */ 619 m->m_flags &= ~M_CANFASTFWD; 620#else 621 /* 622 * Assume that we can create a fast-forward IP flow entry 623 * based on this packet. 624 */ 625 m->m_flags |= M_CANFASTFWD; 626#endif 627 628#ifdef PFIL_HOOKS 629 /* 630 * Run through list of hooks for input packets. If there are any 631 * filters which require that additional packets in the flow are 632 * not fast-forwarded, they must clear the M_CANFASTFWD flag. 633 * Note that filters must _never_ set this flag, as another filter 634 * in the list may have previously cleared it. 635 */ 636 /* 637 * let ipfilter look at packet on the wire, 638 * not the decapsulated packet. 639 */ 640#ifdef IPSEC 641 if (!ipsec_getnhist(m)) 642#elif defined(FAST_IPSEC) 643 if (!ipsec_indone(m)) 644#else 645 if (1) 646#endif 647 { 648 struct in_addr odst; 649 650 odst = ip->ip_dst; 651 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif, 652 PFIL_IN) != 0) 653 return; 654 if (m == NULL) 655 return; 656 ip = mtod(m, struct ip *); 657 hlen = ip->ip_hl << 2; 658 /* 659 * XXX The setting of "srcrt" here is to prevent ip_forward() 660 * from generating ICMP redirects for packets that have 661 * been redirected by a hook back out on to the same LAN that 662 * they came from and is not an indication that the packet 663 * is being inffluenced by source routing options. This 664 * allows things like 665 * "rdr tlp0 0/0 port 80 -> 1.1.1.200 3128 tcp" 666 * where tlp0 is both on the 1.1.1.0/24 network and is the 667 * default route for hosts on 1.1.1.0/24. Of course this 668 * also requires a "map tlp0 ..." to complete the story. 669 * One might argue whether or not this kind of network config. 670 * should be supported in this manner... 671 */ 672 srcrt = (odst.s_addr != ip->ip_dst.s_addr); 673 } 674#endif /* PFIL_HOOKS */ 675 676#ifdef ALTQ 677 /* XXX Temporary until ALTQ is changed to use a pfil hook */ 678 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) { 679 /* packet dropped by traffic conditioner */ 680 return; 681 } 682#endif 683 684 /* 685 * Process options and, if not destined for us, 686 * ship it on. ip_dooptions returns 1 when an 687 * error was detected (causing an icmp message 688 * to be sent and the original packet to be freed). 689 */ 690 ip_nhops = 0; /* for source routed packets */ 691 if (hlen > sizeof (struct ip) && ip_dooptions(m)) 692 return; 693 694 /* 695 * Enable a consistency check between the destination address 696 * and the arrival interface for a unicast packet (the RFC 1122 697 * strong ES model) if IP forwarding is disabled and the packet 698 * is not locally generated. 699 * 700 * XXX - Checking also should be disabled if the destination 701 * address is ipnat'ed to a different interface. 702 * 703 * XXX - Checking is incompatible with IP aliases added 704 * to the loopback interface instead of the interface where 705 * the packets are received. 706 * 707 * XXX - We need to add a per ifaddr flag for this so that 708 * we get finer grain control. 709 */ 710 checkif = ip_checkinterface && (ipforwarding == 0) && 711 (m->m_pkthdr.rcvif != NULL) && 712 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0); 713 714 /* 715 * Check our list of addresses, to see if the packet is for us. 716 * 717 * Traditional 4.4BSD did not consult IFF_UP at all. 718 * The behavior here is to treat addresses on !IFF_UP interface 719 * as not mine. 720 */ 721 downmatch = 0; 722 LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 723 if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) { 724 if (checkif && ia->ia_ifp != m->m_pkthdr.rcvif) 725 continue; 726 if ((ia->ia_ifp->if_flags & IFF_UP) != 0) 727 break; 728 else 729 downmatch++; 730 } 731 } 732 if (ia != NULL) 733 goto ours; 734 if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 735 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) { 736 if (ifa->ifa_addr->sa_family != AF_INET) 737 continue; 738 ia = ifatoia(ifa); 739 if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) || 740 in_hosteq(ip->ip_dst, ia->ia_netbroadcast) || 741 /* 742 * Look for all-0's host part (old broadcast addr), 743 * either for subnet or net. 744 */ 745 ip->ip_dst.s_addr == ia->ia_subnet || 746 ip->ip_dst.s_addr == ia->ia_net) 747 goto ours; 748 /* 749 * An interface with IP address zero accepts 750 * all packets that arrive on that interface. 751 */ 752 if (in_nullhost(ia->ia_addr.sin_addr)) 753 goto ours; 754 } 755 } 756 if (IN_MULTICAST(ip->ip_dst.s_addr)) { 757 struct in_multi *inm; 758#ifdef MROUTING 759 extern struct socket *ip_mrouter; 760 761 if (ip_mrouter) { 762 /* 763 * If we are acting as a multicast router, all 764 * incoming multicast packets are passed to the 765 * kernel-level multicast forwarding function. 766 * The packet is returned (relatively) intact; if 767 * ip_mforward() returns a non-zero value, the packet 768 * must be discarded, else it may be accepted below. 769 * 770 * (The IP ident field is put in the same byte order 771 * as expected when ip_mforward() is called from 772 * ip_output().) 773 */ 774 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { 775 ipstat.ips_cantforward++; 776 m_freem(m); 777 return; 778 } 779 780 /* 781 * The process-level routing demon needs to receive 782 * all multicast IGMP packets, whether or not this 783 * host belongs to their destination groups. 784 */ 785 if (ip->ip_p == IPPROTO_IGMP) 786 goto ours; 787 ipstat.ips_forward++; 788 } 789#endif 790 /* 791 * See if we belong to the destination multicast group on the 792 * arrival interface. 793 */ 794 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 795 if (inm == NULL) { 796 ipstat.ips_cantforward++; 797 m_freem(m); 798 return; 799 } 800 goto ours; 801 } 802 if (ip->ip_dst.s_addr == INADDR_BROADCAST || 803 in_nullhost(ip->ip_dst)) 804 goto ours; 805 806 /* 807 * Not for us; forward if possible and desirable. 808 */ 809 if (ipforwarding == 0) { 810 ipstat.ips_cantforward++; 811 m_freem(m); 812 } else { 813 /* 814 * If ip_dst matched any of my address on !IFF_UP interface, 815 * and there's no IFF_UP interface that matches ip_dst, 816 * send icmp unreach. Forwarding it will result in in-kernel 817 * forwarding loop till TTL goes to 0. 818 */ 819 if (downmatch) { 820 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 821 ipstat.ips_cantforward++; 822 return; 823 } 824#ifdef IPSEC 825 if (ipsec4_in_reject(m, NULL)) { 826 ipsecstat.in_polvio++; 827 goto bad; 828 } 829#endif 830#ifdef FAST_IPSEC 831 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 832 s = splsoftnet(); 833 if (mtag != NULL) { 834 tdbi = (struct tdb_ident *)(mtag + 1); 835 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 836 } else { 837 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 838 IP_FORWARDING, &error); 839 } 840 if (sp == NULL) { /* NB: can happen if error */ 841 splx(s); 842 /*XXX error stat???*/ 843 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/ 844 goto bad; 845 } 846 847 /* 848 * Check security policy against packet attributes. 849 */ 850 error = ipsec_in_reject(sp, m); 851 KEY_FREESP(&sp); 852 splx(s); 853 if (error) { 854 ipstat.ips_cantforward++; 855 goto bad; 856 } 857 858 /* 859 * Peek at the outbound SP for this packet to determine if 860 * it's a Fast Forward candidate. 861 */ 862 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 863 if (mtag != NULL) 864 m->m_flags &= ~M_CANFASTFWD; 865 else { 866 s = splsoftnet(); 867 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, 868 (IP_FORWARDING | 869 (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 870 &error, NULL); 871 if (sp != NULL) { 872 m->m_flags &= ~M_CANFASTFWD; 873 KEY_FREESP(&sp); 874 } 875 splx(s); 876 } 877#endif /* FAST_IPSEC */ 878 879 ip_forward(m, srcrt); 880 } 881 return; 882 883ours: 884 /* 885 * If offset or IP_MF are set, must reassemble. 886 * Otherwise, nothing need be done. 887 * (We could look in the reassembly queue to see 888 * if the packet was previously fragmented, 889 * but it's not worth the time; just let them time out.) 890 */ 891 if (ip->ip_off & ~htons(IP_DF|IP_RF)) { 892 893 /* 894 * Look for queue of fragments 895 * of this datagram. 896 */ 897 IPQ_LOCK(); 898 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 899 /* XXX LIST_FOREACH(fp, &ipq[hash], ipq_q) */ 900 for (fp = LIST_FIRST(&ipq[hash]); fp != NULL; 901 fp = LIST_NEXT(fp, ipq_q)) { 902 if (ip->ip_id == fp->ipq_id && 903 in_hosteq(ip->ip_src, fp->ipq_src) && 904 in_hosteq(ip->ip_dst, fp->ipq_dst) && 905 ip->ip_p == fp->ipq_p) 906 goto found; 907 908 } 909 fp = 0; 910found: 911 912 /* 913 * Adjust ip_len to not reflect header, 914 * set ipqe_mff if more fragments are expected, 915 * convert offset of this to bytes. 916 */ 917 ip->ip_len = htons(ntohs(ip->ip_len) - hlen); 918 mff = (ip->ip_off & htons(IP_MF)) != 0; 919 if (mff) { 920 /* 921 * Make sure that fragments have a data length 922 * that's a non-zero multiple of 8 bytes. 923 */ 924 if (ntohs(ip->ip_len) == 0 || 925 (ntohs(ip->ip_len) & 0x7) != 0) { 926 ipstat.ips_badfrags++; 927 IPQ_UNLOCK(); 928 goto bad; 929 } 930 } 931 ip->ip_off = htons((ntohs(ip->ip_off) & IP_OFFMASK) << 3); 932 933 /* 934 * If datagram marked as having more fragments 935 * or if this is not the first fragment, 936 * attempt reassembly; if it succeeds, proceed. 937 */ 938 if (mff || ip->ip_off != htons(0)) { 939 ipstat.ips_fragments++; 940 ipqe = pool_get(&ipqent_pool, PR_NOWAIT); 941 if (ipqe == NULL) { 942 ipstat.ips_rcvmemdrop++; 943 IPQ_UNLOCK(); 944 goto bad; 945 } 946 ipqe->ipqe_mff = mff; 947 ipqe->ipqe_m = m; 948 ipqe->ipqe_ip = ip; 949 m = ip_reass(ipqe, fp, &ipq[hash]); 950 if (m == 0) { 951 IPQ_UNLOCK(); 952 return; 953 } 954 ipstat.ips_reassembled++; 955 ip = mtod(m, struct ip *); 956 hlen = ip->ip_hl << 2; 957 ip->ip_len = htons(ntohs(ip->ip_len) + hlen); 958 } else 959 if (fp) 960 ip_freef(fp); 961 IPQ_UNLOCK(); 962 } 963 964#if defined(IPSEC) 965 /* 966 * enforce IPsec policy checking if we are seeing last header. 967 * note that we do not visit this with protocols with pcb layer 968 * code - like udp/tcp/raw ip. 969 */ 970 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 && 971 ipsec4_in_reject(m, NULL)) { 972 ipsecstat.in_polvio++; 973 goto bad; 974 } 975#endif 976#if FAST_IPSEC 977 /* 978 * enforce IPsec policy checking if we are seeing last header. 979 * note that we do not visit this with protocols with pcb layer 980 * code - like udp/tcp/raw ip. 981 */ 982 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) { 983 /* 984 * Check if the packet has already had IPsec processing 985 * done. If so, then just pass it along. This tag gets 986 * set during AH, ESP, etc. input handling, before the 987 * packet is returned to the ip input queue for delivery. 988 */ 989 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 990 s = splsoftnet(); 991 if (mtag != NULL) { 992 tdbi = (struct tdb_ident *)(mtag + 1); 993 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 994 } else { 995 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 996 IP_FORWARDING, &error); 997 } 998 if (sp != NULL) { 999 /* 1000 * Check security policy against packet attributes. 1001 */ 1002 error = ipsec_in_reject(sp, m); 1003 KEY_FREESP(&sp); 1004 } else { 1005 /* XXX error stat??? */ 1006 error = EINVAL; 1007DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/ 1008 goto bad; 1009 } 1010 splx(s); 1011 if (error) 1012 goto bad; 1013 } 1014#endif /* FAST_IPSEC */ 1015 1016 /* 1017 * Switch out to protocol's input routine. 1018 */ 1019#if IFA_STATS 1020 if (ia && ip) 1021 ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len); 1022#endif 1023 ipstat.ips_delivered++; 1024 { 1025 int off = hlen, nh = ip->ip_p; 1026 1027 (*inetsw[ip_protox[nh]].pr_input)(m, off, nh); 1028 return; 1029 } 1030bad: 1031 m_freem(m); 1032 return; 1033 1034badcsum: 1035 ipstat.ips_badsum++; 1036 m_freem(m); 1037} 1038 1039/* 1040 * Take incoming datagram fragment and try to 1041 * reassemble it into whole datagram. If a chain for 1042 * reassembly of this datagram already exists, then it 1043 * is given as fp; otherwise have to make a chain. 1044 */ 1045struct mbuf * 1046ip_reass(struct ipqent *ipqe, struct ipq *fp, struct ipqhead *ipqhead) 1047{ 1048 struct mbuf *m = ipqe->ipqe_m; 1049 struct ipqent *nq, *p, *q; 1050 struct ip *ip; 1051 struct mbuf *t; 1052 int hlen = ipqe->ipqe_ip->ip_hl << 2; 1053 int i, next; 1054 1055 IPQ_LOCK_CHECK(); 1056 1057 /* 1058 * Presence of header sizes in mbufs 1059 * would confuse code below. 1060 */ 1061 m->m_data += hlen; 1062 m->m_len -= hlen; 1063 1064#ifdef notyet 1065 /* make sure fragment limit is up-to-date */ 1066 CHECK_NMBCLUSTER_PARAMS(); 1067 1068 /* If we have too many fragments, drop the older half. */ 1069 if (ip_nfrags >= ip_maxfrags) 1070 ip_reass_drophalf(void); 1071#endif 1072 1073 /* 1074 * We are about to add a fragment; increment frag count. 1075 */ 1076 ip_nfrags++; 1077 1078 /* 1079 * If first fragment to arrive, create a reassembly queue. 1080 */ 1081 if (fp == 0) { 1082 /* 1083 * Enforce upper bound on number of fragmented packets 1084 * for which we attempt reassembly; 1085 * If maxfrag is 0, never accept fragments. 1086 * If maxfrag is -1, accept all fragments without limitation. 1087 */ 1088 if (ip_maxfragpackets < 0) 1089 ; 1090 else if (ip_nfragpackets >= ip_maxfragpackets) 1091 goto dropfrag; 1092 ip_nfragpackets++; 1093 MALLOC(fp, struct ipq *, sizeof (struct ipq), 1094 M_FTABLE, M_NOWAIT); 1095 if (fp == NULL) 1096 goto dropfrag; 1097 LIST_INSERT_HEAD(ipqhead, fp, ipq_q); 1098 fp->ipq_nfrags = 1; 1099 fp->ipq_ttl = IPFRAGTTL; 1100 fp->ipq_p = ipqe->ipqe_ip->ip_p; 1101 fp->ipq_id = ipqe->ipqe_ip->ip_id; 1102 TAILQ_INIT(&fp->ipq_fragq); 1103 fp->ipq_src = ipqe->ipqe_ip->ip_src; 1104 fp->ipq_dst = ipqe->ipqe_ip->ip_dst; 1105 p = NULL; 1106 goto insert; 1107 } else { 1108 fp->ipq_nfrags++; 1109 } 1110 1111 /* 1112 * Find a segment which begins after this one does. 1113 */ 1114 for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; 1115 p = q, q = TAILQ_NEXT(q, ipqe_q)) 1116 if (ntohs(q->ipqe_ip->ip_off) > ntohs(ipqe->ipqe_ip->ip_off)) 1117 break; 1118 1119 /* 1120 * If there is a preceding segment, it may provide some of 1121 * our data already. If so, drop the data from the incoming 1122 * segment. If it provides all of our data, drop us. 1123 */ 1124 if (p != NULL) { 1125 i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) - 1126 ntohs(ipqe->ipqe_ip->ip_off); 1127 if (i > 0) { 1128 if (i >= ntohs(ipqe->ipqe_ip->ip_len)) 1129 goto dropfrag; 1130 m_adj(ipqe->ipqe_m, i); 1131 ipqe->ipqe_ip->ip_off = 1132 htons(ntohs(ipqe->ipqe_ip->ip_off) + i); 1133 ipqe->ipqe_ip->ip_len = 1134 htons(ntohs(ipqe->ipqe_ip->ip_len) - i); 1135 } 1136 } 1137 1138 /* 1139 * While we overlap succeeding segments trim them or, 1140 * if they are completely covered, dequeue them. 1141 */ 1142 for (; q != NULL && 1143 ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len) > 1144 ntohs(q->ipqe_ip->ip_off); q = nq) { 1145 i = (ntohs(ipqe->ipqe_ip->ip_off) + 1146 ntohs(ipqe->ipqe_ip->ip_len)) - ntohs(q->ipqe_ip->ip_off); 1147 if (i < ntohs(q->ipqe_ip->ip_len)) { 1148 q->ipqe_ip->ip_len = 1149 htons(ntohs(q->ipqe_ip->ip_len) - i); 1150 q->ipqe_ip->ip_off = 1151 htons(ntohs(q->ipqe_ip->ip_off) + i); 1152 m_adj(q->ipqe_m, i); 1153 break; 1154 } 1155 nq = TAILQ_NEXT(q, ipqe_q); 1156 m_freem(q->ipqe_m); 1157 TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q); 1158 pool_put(&ipqent_pool, q); 1159 fp->ipq_nfrags--; 1160 ip_nfrags--; 1161 } 1162 1163insert: 1164 /* 1165 * Stick new segment in its place; 1166 * check for complete reassembly. 1167 */ 1168 if (p == NULL) { 1169 TAILQ_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q); 1170 } else { 1171 TAILQ_INSERT_AFTER(&fp->ipq_fragq, p, ipqe, ipqe_q); 1172 } 1173 next = 0; 1174 for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; 1175 p = q, q = TAILQ_NEXT(q, ipqe_q)) { 1176 if (ntohs(q->ipqe_ip->ip_off) != next) 1177 return (0); 1178 next += ntohs(q->ipqe_ip->ip_len); 1179 } 1180 if (p->ipqe_mff) 1181 return (0); 1182 1183 /* 1184 * Reassembly is complete. Check for a bogus message size and 1185 * concatenate fragments. 1186 */ 1187 q = TAILQ_FIRST(&fp->ipq_fragq); 1188 ip = q->ipqe_ip; 1189 if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) { 1190 ipstat.ips_toolong++; 1191 ip_freef(fp); 1192 return (0); 1193 } 1194 m = q->ipqe_m; 1195 t = m->m_next; 1196 m->m_next = 0; 1197 m_cat(m, t); 1198 nq = TAILQ_NEXT(q, ipqe_q); 1199 pool_put(&ipqent_pool, q); 1200 for (q = nq; q != NULL; q = nq) { 1201 t = q->ipqe_m; 1202 nq = TAILQ_NEXT(q, ipqe_q); 1203 pool_put(&ipqent_pool, q); 1204 m_cat(m, t); 1205 } 1206 ip_nfrags -= fp->ipq_nfrags; 1207 1208 /* 1209 * Create header for new ip packet by 1210 * modifying header of first packet; 1211 * dequeue and discard fragment reassembly header. 1212 * Make header visible. 1213 */ 1214 ip->ip_len = htons(next); 1215 ip->ip_src = fp->ipq_src; 1216 ip->ip_dst = fp->ipq_dst; 1217 LIST_REMOVE(fp, ipq_q); 1218 FREE(fp, M_FTABLE); 1219 ip_nfragpackets--; 1220 m->m_len += (ip->ip_hl << 2); 1221 m->m_data -= (ip->ip_hl << 2); 1222 /* some debugging cruft by sklower, below, will go away soon */ 1223 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 1224 int plen = 0; 1225 for (t = m; t; t = t->m_next) 1226 plen += t->m_len; 1227 m->m_pkthdr.len = plen; 1228 m->m_pkthdr.csum_flags = 0; 1229 } 1230 return (m); 1231 1232dropfrag: 1233 if (fp != 0) 1234 fp->ipq_nfrags--; 1235 ip_nfrags--; 1236 ipstat.ips_fragdropped++; 1237 m_freem(m); 1238 pool_put(&ipqent_pool, ipqe); 1239 return (0); 1240} 1241 1242/* 1243 * Free a fragment reassembly header and all 1244 * associated datagrams. 1245 */ 1246void 1247ip_freef(struct ipq *fp) 1248{ 1249 struct ipqent *q, *p; 1250 u_int nfrags = 0; 1251 1252 IPQ_LOCK_CHECK(); 1253 1254 for (q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; q = p) { 1255 p = TAILQ_NEXT(q, ipqe_q); 1256 m_freem(q->ipqe_m); 1257 nfrags++; 1258 TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q); 1259 pool_put(&ipqent_pool, q); 1260 } 1261 1262 if (nfrags != fp->ipq_nfrags) 1263 printf("ip_freef: nfrags %d != %d\n", fp->ipq_nfrags, nfrags); 1264 ip_nfrags -= nfrags; 1265 LIST_REMOVE(fp, ipq_q); 1266 FREE(fp, M_FTABLE); 1267 ip_nfragpackets--; 1268} 1269 1270/* 1271 * IP reassembly TTL machinery for multiplicative drop. 1272 */ 1273static u_int fragttl_histo[(IPFRAGTTL+1)]; 1274 1275 1276/* 1277 * Decrement TTL of all reasembly queue entries by `ticks'. 1278 * Count number of distinct fragments (as opposed to partial, fragmented 1279 * datagrams) in the reassembly queue. While we traverse the entire 1280 * reassembly queue, compute and return the median TTL over all fragments. 1281 */ 1282static u_int 1283ip_reass_ttl_decr(u_int ticks) 1284{ 1285 u_int nfrags, median, dropfraction, keepfraction; 1286 struct ipq *fp, *nfp; 1287 int i; 1288 1289 nfrags = 0; 1290 memset(fragttl_histo, 0, sizeof fragttl_histo); 1291 1292 for (i = 0; i < IPREASS_NHASH; i++) { 1293 for (fp = LIST_FIRST(&ipq[i]); fp != NULL; fp = nfp) { 1294 fp->ipq_ttl = ((fp->ipq_ttl <= ticks) ? 1295 0 : fp->ipq_ttl - ticks); 1296 nfp = LIST_NEXT(fp, ipq_q); 1297 if (fp->ipq_ttl == 0) { 1298 ipstat.ips_fragtimeout++; 1299 ip_freef(fp); 1300 } else { 1301 nfrags += fp->ipq_nfrags; 1302 fragttl_histo[fp->ipq_ttl] += fp->ipq_nfrags; 1303 } 1304 } 1305 } 1306 1307 KASSERT(ip_nfrags == nfrags); 1308 1309 /* Find median (or other drop fraction) in histogram. */ 1310 dropfraction = (ip_nfrags / 2); 1311 keepfraction = ip_nfrags - dropfraction; 1312 for (i = IPFRAGTTL, median = 0; i >= 0; i--) { 1313 median += fragttl_histo[i]; 1314 if (median >= keepfraction) 1315 break; 1316 } 1317 1318 /* Return TTL of median (or other fraction). */ 1319 return (u_int)i; 1320} 1321 1322void 1323ip_reass_drophalf(void) 1324{ 1325 1326 u_int median_ticks; 1327 /* 1328 * Compute median TTL of all fragments, and count frags 1329 * with that TTL or lower (roughly half of all fragments). 1330 */ 1331 median_ticks = ip_reass_ttl_decr(0); 1332 1333 /* Drop half. */ 1334 median_ticks = ip_reass_ttl_decr(median_ticks); 1335 1336} 1337 1338/* 1339 * IP timer processing; 1340 * if a timer expires on a reassembly 1341 * queue, discard it. 1342 */ 1343void 1344ip_slowtimo(void) 1345{ 1346 static u_int dropscanidx = 0; 1347 u_int i; 1348 u_int median_ttl; 1349 int s = splsoftnet(); 1350 1351 IPQ_LOCK(); 1352 1353 /* Age TTL of all fragments by 1 tick .*/ 1354 median_ttl = ip_reass_ttl_decr(1); 1355 1356 /* make sure fragment limit is up-to-date */ 1357 CHECK_NMBCLUSTER_PARAMS(); 1358 1359 /* If we have too many fragments, drop the older half. */ 1360 if (ip_nfrags > ip_maxfrags) 1361 ip_reass_ttl_decr(median_ttl); 1362 1363 /* 1364 * If we are over the maximum number of fragmented packets 1365 * (due to the limit being lowered), drain off 1366 * enough to get down to the new limit. Start draining 1367 * from the reassembly hashqueue most recently drained. 1368 */ 1369 if (ip_maxfragpackets < 0) 1370 ; 1371 else { 1372 int wrapped = 0; 1373 1374 i = dropscanidx; 1375 while (ip_nfragpackets > ip_maxfragpackets && wrapped == 0) { 1376 while (LIST_FIRST(&ipq[i]) != NULL) 1377 ip_freef(LIST_FIRST(&ipq[i])); 1378 if (++i >= IPREASS_NHASH) { 1379 i = 0; 1380 } 1381 /* 1382 * Dont scan forever even if fragment counters are 1383 * wrong: stop after scanning entire reassembly queue. 1384 */ 1385 if (i == dropscanidx) 1386 wrapped = 1; 1387 } 1388 dropscanidx = i; 1389 } 1390 IPQ_UNLOCK(); 1391#ifdef GATEWAY 1392 ipflow_slowtimo(); 1393#endif 1394 splx(s); 1395} 1396 1397/* 1398 * Drain off all datagram fragments. 1399 */ 1400void 1401ip_drain(void) 1402{ 1403 1404 /* 1405 * We may be called from a device's interrupt context. If 1406 * the ipq is already busy, just bail out now. 1407 */ 1408 if (ipq_lock_try() == 0) 1409 return; 1410 1411 /* 1412 * Drop half the total fragments now. If more mbufs are needed, 1413 * we will be called again soon. 1414 */ 1415 ip_reass_drophalf(); 1416 1417 IPQ_UNLOCK(); 1418} 1419 1420/* 1421 * Do option processing on a datagram, 1422 * possibly discarding it if bad options are encountered, 1423 * or forwarding it if source-routed. 1424 * Returns 1 if packet has been forwarded/freed, 1425 * 0 if the packet should be processed further. 1426 */ 1427int 1428ip_dooptions(struct mbuf *m) 1429{ 1430 struct ip *ip = mtod(m, struct ip *); 1431 u_char *cp, *cp0; 1432 struct ip_timestamp *ipt; 1433 struct in_ifaddr *ia; 1434 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1435 struct in_addr dst; 1436 n_time ntime; 1437 1438 dst = ip->ip_dst; 1439 cp = (u_char *)(ip + 1); 1440 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1441 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1442 opt = cp[IPOPT_OPTVAL]; 1443 if (opt == IPOPT_EOL) 1444 break; 1445 if (opt == IPOPT_NOP) 1446 optlen = 1; 1447 else { 1448 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1449 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1450 goto bad; 1451 } 1452 optlen = cp[IPOPT_OLEN]; 1453 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1454 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1455 goto bad; 1456 } 1457 } 1458 switch (opt) { 1459 1460 default: 1461 break; 1462 1463 /* 1464 * Source routing with record. 1465 * Find interface with current destination address. 1466 * If none on this machine then drop if strictly routed, 1467 * or do nothing if loosely routed. 1468 * Record interface address and bring up next address 1469 * component. If strictly routed make sure next 1470 * address is on directly accessible net. 1471 */ 1472 case IPOPT_LSRR: 1473 case IPOPT_SSRR: 1474 if (ip_allowsrcrt == 0) { 1475 type = ICMP_UNREACH; 1476 code = ICMP_UNREACH_NET_PROHIB; 1477 goto bad; 1478 } 1479 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1480 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1481 goto bad; 1482 } 1483 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1484 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1485 goto bad; 1486 } 1487 ipaddr.sin_addr = ip->ip_dst; 1488 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))); 1489 if (ia == 0) { 1490 if (opt == IPOPT_SSRR) { 1491 type = ICMP_UNREACH; 1492 code = ICMP_UNREACH_SRCFAIL; 1493 goto bad; 1494 } 1495 /* 1496 * Loose routing, and not at next destination 1497 * yet; nothing to do except forward. 1498 */ 1499 break; 1500 } 1501 off--; /* 0 origin */ 1502 if ((off + sizeof(struct in_addr)) > optlen) { 1503 /* 1504 * End of source route. Should be for us. 1505 */ 1506 save_rte(cp, ip->ip_src); 1507 break; 1508 } 1509 /* 1510 * locate outgoing interface 1511 */ 1512 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 1513 sizeof(ipaddr.sin_addr)); 1514 if (opt == IPOPT_SSRR) 1515 ia = ifatoia(ifa_ifwithladdr(sintosa(&ipaddr))); 1516 else 1517 ia = ip_rtaddr(ipaddr.sin_addr); 1518 if (ia == 0) { 1519 type = ICMP_UNREACH; 1520 code = ICMP_UNREACH_SRCFAIL; 1521 goto bad; 1522 } 1523 ip->ip_dst = ipaddr.sin_addr; 1524 bcopy((caddr_t)&ia->ia_addr.sin_addr, 1525 (caddr_t)(cp + off), sizeof(struct in_addr)); 1526 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1527 /* 1528 * Let ip_intr's mcast routing check handle mcast pkts 1529 */ 1530 forward = !IN_MULTICAST(ip->ip_dst.s_addr); 1531 break; 1532 1533 case IPOPT_RR: 1534 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1535 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1536 goto bad; 1537 } 1538 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1539 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1540 goto bad; 1541 } 1542 /* 1543 * If no space remains, ignore. 1544 */ 1545 off--; /* 0 origin */ 1546 if ((off + sizeof(struct in_addr)) > optlen) 1547 break; 1548 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 1549 sizeof(ipaddr.sin_addr)); 1550 /* 1551 * locate outgoing interface; if we're the destination, 1552 * use the incoming interface (should be same). 1553 */ 1554 if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))) 1555 == NULL && 1556 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) { 1557 type = ICMP_UNREACH; 1558 code = ICMP_UNREACH_HOST; 1559 goto bad; 1560 } 1561 bcopy((caddr_t)&ia->ia_addr.sin_addr, 1562 (caddr_t)(cp + off), sizeof(struct in_addr)); 1563 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1564 break; 1565 1566 case IPOPT_TS: 1567 code = cp - (u_char *)ip; 1568 ipt = (struct ip_timestamp *)cp; 1569 if (ipt->ipt_len < 4 || ipt->ipt_len > 40) { 1570 code = (u_char *)&ipt->ipt_len - (u_char *)ip; 1571 goto bad; 1572 } 1573 if (ipt->ipt_ptr < 5) { 1574 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip; 1575 goto bad; 1576 } 1577 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) { 1578 if (++ipt->ipt_oflw == 0) { 1579 code = (u_char *)&ipt->ipt_ptr - 1580 (u_char *)ip; 1581 goto bad; 1582 } 1583 break; 1584 } 1585 cp0 = (cp + ipt->ipt_ptr - 1); 1586 switch (ipt->ipt_flg) { 1587 1588 case IPOPT_TS_TSONLY: 1589 break; 1590 1591 case IPOPT_TS_TSANDADDR: 1592 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1593 sizeof(struct in_addr) > ipt->ipt_len) { 1594 code = (u_char *)&ipt->ipt_ptr - 1595 (u_char *)ip; 1596 goto bad; 1597 } 1598 ipaddr.sin_addr = dst; 1599 ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr), 1600 m->m_pkthdr.rcvif)); 1601 if (ia == 0) 1602 continue; 1603 bcopy(&ia->ia_addr.sin_addr, 1604 cp0, sizeof(struct in_addr)); 1605 ipt->ipt_ptr += sizeof(struct in_addr); 1606 break; 1607 1608 case IPOPT_TS_PRESPEC: 1609 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1610 sizeof(struct in_addr) > ipt->ipt_len) { 1611 code = (u_char *)&ipt->ipt_ptr - 1612 (u_char *)ip; 1613 goto bad; 1614 } 1615 bcopy(cp0, &ipaddr.sin_addr, 1616 sizeof(struct in_addr)); 1617 if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))) 1618 == NULL) 1619 continue; 1620 ipt->ipt_ptr += sizeof(struct in_addr); 1621 break; 1622 1623 default: 1624 /* XXX can't take &ipt->ipt_flg */ 1625 code = (u_char *)&ipt->ipt_ptr - 1626 (u_char *)ip + 1; 1627 goto bad; 1628 } 1629 ntime = iptime(); 1630 cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */ 1631 bcopy(cp0, (caddr_t)cp + ipt->ipt_ptr - 1, 1632 sizeof(n_time)); 1633 ipt->ipt_ptr += sizeof(n_time); 1634 } 1635 } 1636 if (forward) { 1637 if (ip_forwsrcrt == 0) { 1638 type = ICMP_UNREACH; 1639 code = ICMP_UNREACH_SRCFAIL; 1640 goto bad; 1641 } 1642 ip_forward(m, 1); 1643 return (1); 1644 } 1645 return (0); 1646bad: 1647 icmp_error(m, type, code, 0, 0); 1648 ipstat.ips_badoptions++; 1649 return (1); 1650} 1651 1652/* 1653 * Given address of next destination (final or next hop), 1654 * return internet address info of interface to be used to get there. 1655 */ 1656struct in_ifaddr * 1657ip_rtaddr(struct in_addr dst) 1658{ 1659 struct sockaddr_in *sin; 1660 1661 sin = satosin(&ipforward_rt.ro_dst); 1662 1663 if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) { 1664 if (ipforward_rt.ro_rt) { 1665 RTFREE(ipforward_rt.ro_rt); 1666 ipforward_rt.ro_rt = 0; 1667 } 1668 sin->sin_family = AF_INET; 1669 sin->sin_len = sizeof(*sin); 1670 sin->sin_addr = dst; 1671 1672 rtalloc(&ipforward_rt); 1673 } 1674 if (ipforward_rt.ro_rt == 0) 1675 return ((struct in_ifaddr *)0); 1676 return (ifatoia(ipforward_rt.ro_rt->rt_ifa)); 1677} 1678 1679/* 1680 * Save incoming source route for use in replies, 1681 * to be picked up later by ip_srcroute if the receiver is interested. 1682 */ 1683void 1684save_rte(u_char *option, struct in_addr dst) 1685{ 1686 unsigned olen; 1687 1688 olen = option[IPOPT_OLEN]; 1689#ifdef DIAGNOSTIC 1690 if (ipprintfs) 1691 printf("save_rte: olen %d\n", olen); 1692#endif /* 0 */ 1693 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1694 return; 1695 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 1696 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1697 ip_srcrt.dst = dst; 1698} 1699 1700/* 1701 * Retrieve incoming source route for use in replies, 1702 * in the same form used by setsockopt. 1703 * The first hop is placed before the options, will be removed later. 1704 */ 1705struct mbuf * 1706ip_srcroute(void) 1707{ 1708 struct in_addr *p, *q; 1709 struct mbuf *m; 1710 1711 if (ip_nhops == 0) 1712 return ((struct mbuf *)0); 1713 m = m_get(M_DONTWAIT, MT_SOOPTS); 1714 if (m == 0) 1715 return ((struct mbuf *)0); 1716 1717 MCLAIM(m, &inetdomain.dom_mowner); 1718#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1719 1720 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1721 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1722 OPTSIZ; 1723#ifdef DIAGNOSTIC 1724 if (ipprintfs) 1725 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1726#endif 1727 1728 /* 1729 * First save first hop for return route 1730 */ 1731 p = &ip_srcrt.route[ip_nhops - 1]; 1732 *(mtod(m, struct in_addr *)) = *p--; 1733#ifdef DIAGNOSTIC 1734 if (ipprintfs) 1735 printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr)); 1736#endif 1737 1738 /* 1739 * Copy option fields and padding (nop) to mbuf. 1740 */ 1741 ip_srcrt.nop = IPOPT_NOP; 1742 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1743 bcopy((caddr_t)&ip_srcrt.nop, 1744 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 1745 q = (struct in_addr *)(mtod(m, caddr_t) + 1746 sizeof(struct in_addr) + OPTSIZ); 1747#undef OPTSIZ 1748 /* 1749 * Record return path as an IP source route, 1750 * reversing the path (pointers are now aligned). 1751 */ 1752 while (p >= ip_srcrt.route) { 1753#ifdef DIAGNOSTIC 1754 if (ipprintfs) 1755 printf(" %x", ntohl(q->s_addr)); 1756#endif 1757 *q++ = *p--; 1758 } 1759 /* 1760 * Last hop goes to final destination. 1761 */ 1762 *q = ip_srcrt.dst; 1763#ifdef DIAGNOSTIC 1764 if (ipprintfs) 1765 printf(" %x\n", ntohl(q->s_addr)); 1766#endif 1767 return (m); 1768} 1769 1770/* 1771 * Strip out IP options, at higher 1772 * level protocol in the kernel. 1773 * Second argument is buffer to which options 1774 * will be moved, and return value is their length. 1775 * XXX should be deleted; last arg currently ignored. 1776 */ 1777void 1778ip_stripoptions(struct mbuf *m, struct mbuf *mopt) 1779{ 1780 int i; 1781 struct ip *ip = mtod(m, struct ip *); 1782 caddr_t opts; 1783 int olen; 1784 1785 olen = (ip->ip_hl << 2) - sizeof (struct ip); 1786 opts = (caddr_t)(ip + 1); 1787 i = m->m_len - (sizeof (struct ip) + olen); 1788 bcopy(opts + olen, opts, (unsigned)i); 1789 m->m_len -= olen; 1790 if (m->m_flags & M_PKTHDR) 1791 m->m_pkthdr.len -= olen; 1792 ip->ip_len = htons(ntohs(ip->ip_len) - olen); 1793 ip->ip_hl = sizeof (struct ip) >> 2; 1794} 1795 1796const int inetctlerrmap[PRC_NCMDS] = { 1797 0, 0, 0, 0, 1798 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1799 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1800 EMSGSIZE, EHOSTUNREACH, 0, 0, 1801 0, 0, 0, 0, 1802 ENOPROTOOPT 1803}; 1804 1805/* 1806 * Forward a packet. If some error occurs return the sender 1807 * an icmp packet. Note we can't always generate a meaningful 1808 * icmp message because icmp doesn't have a large enough repertoire 1809 * of codes and types. 1810 * 1811 * If not forwarding, just drop the packet. This could be confusing 1812 * if ipforwarding was zero but some routing protocol was advancing 1813 * us as a gateway to somewhere. However, we must let the routing 1814 * protocol deal with that. 1815 * 1816 * The srcrt parameter indicates whether the packet is being forwarded 1817 * via a source route. 1818 */ 1819void 1820ip_forward(struct mbuf *m, int srcrt) 1821{ 1822 struct ip *ip = mtod(m, struct ip *); 1823 struct sockaddr_in *sin; 1824 struct rtentry *rt; 1825 int error, type = 0, code = 0; 1826 struct mbuf *mcopy; 1827 n_long dest; 1828 struct ifnet *destifp; 1829#if defined(IPSEC) || defined(FAST_IPSEC) 1830 struct ifnet dummyifp; 1831#endif 1832 1833 /* 1834 * We are now in the output path. 1835 */ 1836 MCLAIM(m, &ip_tx_mowner); 1837 1838 /* 1839 * Clear any in-bound checksum flags for this packet. 1840 */ 1841 m->m_pkthdr.csum_flags = 0; 1842 1843 dest = 0; 1844#ifdef DIAGNOSTIC 1845 if (ipprintfs) 1846 printf("forward: src %2.2x dst %2.2x ttl %x\n", 1847 ntohl(ip->ip_src.s_addr), 1848 ntohl(ip->ip_dst.s_addr), ip->ip_ttl); 1849#endif 1850 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1851 ipstat.ips_cantforward++; 1852 m_freem(m); 1853 return; 1854 } 1855 if (ip->ip_ttl <= IPTTLDEC) { 1856 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 1857 return; 1858 } 1859 ip->ip_ttl -= IPTTLDEC; 1860 1861 sin = satosin(&ipforward_rt.ro_dst); 1862 if ((rt = ipforward_rt.ro_rt) == 0 || 1863 !in_hosteq(ip->ip_dst, sin->sin_addr)) { 1864 if (ipforward_rt.ro_rt) { 1865 RTFREE(ipforward_rt.ro_rt); 1866 ipforward_rt.ro_rt = 0; 1867 } 1868 sin->sin_family = AF_INET; 1869 sin->sin_len = sizeof(struct sockaddr_in); 1870 sin->sin_addr = ip->ip_dst; 1871 1872 rtalloc(&ipforward_rt); 1873 if (ipforward_rt.ro_rt == 0) { 1874 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1875 return; 1876 } 1877 rt = ipforward_rt.ro_rt; 1878 } 1879 1880 /* 1881 * Save at most 68 bytes of the packet in case 1882 * we need to generate an ICMP message to the src. 1883 * Pullup to avoid sharing mbuf cluster between m and mcopy. 1884 */ 1885 mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT); 1886 if (mcopy) 1887 mcopy = m_pullup(mcopy, ip->ip_hl << 2); 1888 1889 /* 1890 * If forwarding packet using same interface that it came in on, 1891 * perhaps should send a redirect to sender to shortcut a hop. 1892 * Only send redirect if source is sending directly to us, 1893 * and if packet was not source routed (or has any options). 1894 * Also, don't send redirect if forwarding using a default route 1895 * or a route modified by a redirect. 1896 */ 1897 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1898 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1899 !in_nullhost(satosin(rt_key(rt))->sin_addr) && 1900 ipsendredirects && !srcrt) { 1901 if (rt->rt_ifa && 1902 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) == 1903 ifatoia(rt->rt_ifa)->ia_subnet) { 1904 if (rt->rt_flags & RTF_GATEWAY) 1905 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1906 else 1907 dest = ip->ip_dst.s_addr; 1908 /* 1909 * Router requirements says to only send host 1910 * redirects. 1911 */ 1912 type = ICMP_REDIRECT; 1913 code = ICMP_REDIRECT_HOST; 1914#ifdef DIAGNOSTIC 1915 if (ipprintfs) 1916 printf("redirect (%d) to %x\n", code, 1917 (u_int32_t)dest); 1918#endif 1919 } 1920 } 1921 1922 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1923 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 1924 (struct ip_moptions *)NULL, (struct socket *)NULL); 1925 1926 if (error) 1927 ipstat.ips_cantforward++; 1928 else { 1929 ipstat.ips_forward++; 1930 if (type) 1931 ipstat.ips_redirectsent++; 1932 else { 1933 if (mcopy) { 1934#ifdef GATEWAY 1935 if (mcopy->m_flags & M_CANFASTFWD) 1936 ipflow_create(&ipforward_rt, mcopy); 1937#endif 1938 m_freem(mcopy); 1939 } 1940 return; 1941 } 1942 } 1943 if (mcopy == NULL) 1944 return; 1945 destifp = NULL; 1946 1947 switch (error) { 1948 1949 case 0: /* forwarded, but need redirect */ 1950 /* type, code set above */ 1951 break; 1952 1953 case ENETUNREACH: /* shouldn't happen, checked above */ 1954 case EHOSTUNREACH: 1955 case ENETDOWN: 1956 case EHOSTDOWN: 1957 default: 1958 type = ICMP_UNREACH; 1959 code = ICMP_UNREACH_HOST; 1960 break; 1961 1962 case EMSGSIZE: 1963 type = ICMP_UNREACH; 1964 code = ICMP_UNREACH_NEEDFRAG; 1965#if !defined(IPSEC) && !defined(FAST_IPSEC) 1966 if (ipforward_rt.ro_rt) 1967 destifp = ipforward_rt.ro_rt->rt_ifp; 1968#else 1969 /* 1970 * If the packet is routed over IPsec tunnel, tell the 1971 * originator the tunnel MTU. 1972 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1973 * XXX quickhack!!! 1974 */ 1975 if (ipforward_rt.ro_rt) { 1976 struct secpolicy *sp; 1977 int ipsecerror; 1978 size_t ipsechdr; 1979 struct route *ro; 1980 1981 sp = ipsec4_getpolicybyaddr(mcopy, 1982 IPSEC_DIR_OUTBOUND, IP_FORWARDING, 1983 &ipsecerror); 1984 1985 if (sp == NULL) 1986 destifp = ipforward_rt.ro_rt->rt_ifp; 1987 else { 1988 /* count IPsec header size */ 1989 ipsechdr = ipsec4_hdrsiz(mcopy, 1990 IPSEC_DIR_OUTBOUND, NULL); 1991 1992 /* 1993 * find the correct route for outer IPv4 1994 * header, compute tunnel MTU. 1995 * 1996 * XXX BUG ALERT 1997 * The "dummyifp" code relies upon the fact 1998 * that icmp_error() touches only ifp->if_mtu. 1999 */ 2000 /*XXX*/ 2001 destifp = NULL; 2002 if (sp->req != NULL 2003 && sp->req->sav != NULL 2004 && sp->req->sav->sah != NULL) { 2005 ro = &sp->req->sav->sah->sa_route; 2006 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 2007 dummyifp.if_mtu = 2008 ro->ro_rt->rt_rmx.rmx_mtu ? 2009 ro->ro_rt->rt_rmx.rmx_mtu : 2010 ro->ro_rt->rt_ifp->if_mtu; 2011 dummyifp.if_mtu -= ipsechdr; 2012 destifp = &dummyifp; 2013 } 2014 } 2015 2016#ifdef IPSEC 2017 key_freesp(sp); 2018#else 2019 KEY_FREESP(&sp); 2020#endif 2021 } 2022 } 2023#endif /*IPSEC*/ 2024 ipstat.ips_cantfrag++; 2025 break; 2026 2027 case ENOBUFS: 2028#if 1 2029 /* 2030 * a router should not generate ICMP_SOURCEQUENCH as 2031 * required in RFC1812 Requirements for IP Version 4 Routers. 2032 * source quench could be a big problem under DoS attacks, 2033 * or if the underlying interface is rate-limited. 2034 */ 2035 if (mcopy) 2036 m_freem(mcopy); 2037 return; 2038#else 2039 type = ICMP_SOURCEQUENCH; 2040 code = 0; 2041 break; 2042#endif 2043 } 2044 icmp_error(mcopy, type, code, dest, destifp); 2045} 2046 2047void 2048ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 2049 struct mbuf *m) 2050{ 2051 2052 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 2053 struct timeval tv; 2054 2055 microtime(&tv); 2056 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 2057 SCM_TIMESTAMP, SOL_SOCKET); 2058 if (*mp) 2059 mp = &(*mp)->m_next; 2060 } 2061 if (inp->inp_flags & INP_RECVDSTADDR) { 2062 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 2063 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 2064 if (*mp) 2065 mp = &(*mp)->m_next; 2066 } 2067#ifdef notyet 2068 /* 2069 * XXX 2070 * Moving these out of udp_input() made them even more broken 2071 * than they already were. 2072 * - fenner@parc.xerox.com 2073 */ 2074 /* options were tossed already */ 2075 if (inp->inp_flags & INP_RECVOPTS) { 2076 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 2077 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 2078 if (*mp) 2079 mp = &(*mp)->m_next; 2080 } 2081 /* ip_srcroute doesn't do what we want here, need to fix */ 2082 if (inp->inp_flags & INP_RECVRETOPTS) { 2083 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 2084 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 2085 if (*mp) 2086 mp = &(*mp)->m_next; 2087 } 2088#endif 2089 if (inp->inp_flags & INP_RECVIF) { 2090 struct sockaddr_dl sdl; 2091 2092 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]); 2093 sdl.sdl_family = AF_LINK; 2094 sdl.sdl_index = m->m_pkthdr.rcvif ? 2095 m->m_pkthdr.rcvif->if_index : 0; 2096 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0; 2097 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len, 2098 IP_RECVIF, IPPROTO_IP); 2099 if (*mp) 2100 mp = &(*mp)->m_next; 2101 } 2102} 2103 2104/* 2105 * sysctl helper routine for net.inet.ip.mtudisctimeout. checks the 2106 * range of the new value and tweaks timers if it changes. 2107 */ 2108static int 2109sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS) 2110{ 2111 int error, tmp; 2112 struct sysctlnode node; 2113 2114 node = *rnode; 2115 tmp = ip_mtudisc_timeout; 2116 node.sysctl_data = &tmp; 2117 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2118 if (error || newp == NULL) 2119 return (error); 2120 if (tmp < 0) 2121 return (EINVAL); 2122 2123 ip_mtudisc_timeout = tmp; 2124 rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout); 2125 2126 return (0); 2127} 2128 2129#ifdef GATEWAY 2130/* 2131 * sysctl helper routine for net.inet.ip.maxflows. apparently if 2132 * maxflows is even looked up, we "reap flows". 2133 */ 2134static int 2135sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS) 2136{ 2137 int s; 2138 2139 s = sysctl_lookup(SYSCTLFN_CALL(rnode)); 2140 if (s) 2141 return (s); 2142 2143 s = splsoftnet(); 2144 ipflow_reap(0); 2145 splx(s); 2146 2147 return (0); 2148} 2149#endif /* GATEWAY */ 2150 2151 2152SYSCTL_SETUP(sysctl_net_inet_ip_setup, "sysctl net.inet.ip subtree setup") 2153{ 2154 extern int subnetsarelocal, hostzeroisbroadcast; 2155 2156 sysctl_createv(clog, 0, NULL, NULL, 2157 CTLFLAG_PERMANENT, 2158 CTLTYPE_NODE, "net", NULL, 2159 NULL, 0, NULL, 0, 2160 CTL_NET, CTL_EOL); 2161 sysctl_createv(clog, 0, NULL, NULL, 2162 CTLFLAG_PERMANENT, 2163 CTLTYPE_NODE, "inet", 2164 SYSCTL_DESCR("PF_INET related settings"), 2165 NULL, 0, NULL, 0, 2166 CTL_NET, PF_INET, CTL_EOL); 2167 sysctl_createv(clog, 0, NULL, NULL, 2168 CTLFLAG_PERMANENT, 2169 CTLTYPE_NODE, "ip", 2170 SYSCTL_DESCR("IPv4 related settings"), 2171 NULL, 0, NULL, 0, 2172 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL); 2173 2174 sysctl_createv(clog, 0, NULL, NULL, 2175 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2176 CTLTYPE_INT, "forwarding", 2177 SYSCTL_DESCR("Enable forwarding of INET datagrams"), 2178 NULL, 0, &ipforwarding, 0, 2179 CTL_NET, PF_INET, IPPROTO_IP, 2180 IPCTL_FORWARDING, CTL_EOL); 2181 sysctl_createv(clog, 0, NULL, NULL, 2182 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2183 CTLTYPE_INT, "redirect", 2184 SYSCTL_DESCR("Enable sending of ICMP redirect messages"), 2185 NULL, 0, &ipsendredirects, 0, 2186 CTL_NET, PF_INET, IPPROTO_IP, 2187 IPCTL_SENDREDIRECTS, CTL_EOL); 2188 sysctl_createv(clog, 0, NULL, NULL, 2189 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2190 CTLTYPE_INT, "ttl", 2191 SYSCTL_DESCR("Default TTL for an INET datagram"), 2192 NULL, 0, &ip_defttl, 0, 2193 CTL_NET, PF_INET, IPPROTO_IP, 2194 IPCTL_DEFTTL, CTL_EOL); 2195#ifdef IPCTL_DEFMTU 2196 sysctl_createv(clog, 0, NULL, NULL, 2197 CTLFLAG_PERMANENT /* |CTLFLAG_READWRITE? */, 2198 CTLTYPE_INT, "mtu", 2199 SYSCTL_DESCR("Default MTA for an INET route"), 2200 NULL, 0, &ip_mtu, 0, 2201 CTL_NET, PF_INET, IPPROTO_IP, 2202 IPCTL_DEFMTU, CTL_EOL); 2203#endif /* IPCTL_DEFMTU */ 2204 sysctl_createv(clog, 0, NULL, NULL, 2205 CTLFLAG_PERMANENT|CTLFLAG_READONLY1, 2206 CTLTYPE_INT, "forwsrcrt", 2207 SYSCTL_DESCR("Enable forwarding of source-routed " 2208 "datagrams"), 2209 NULL, 0, &ip_forwsrcrt, 0, 2210 CTL_NET, PF_INET, IPPROTO_IP, 2211 IPCTL_FORWSRCRT, CTL_EOL); 2212 sysctl_createv(clog, 0, NULL, NULL, 2213 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2214 CTLTYPE_INT, "directed-broadcast", 2215 SYSCTL_DESCR("Enable forwarding of broadcast datagrams"), 2216 NULL, 0, &ip_directedbcast, 0, 2217 CTL_NET, PF_INET, IPPROTO_IP, 2218 IPCTL_DIRECTEDBCAST, CTL_EOL); 2219 sysctl_createv(clog, 0, NULL, NULL, 2220 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2221 CTLTYPE_INT, "allowsrcrt", 2222 SYSCTL_DESCR("Accept source-routed datagrams"), 2223 NULL, 0, &ip_allowsrcrt, 0, 2224 CTL_NET, PF_INET, IPPROTO_IP, 2225 IPCTL_ALLOWSRCRT, CTL_EOL); 2226 sysctl_createv(clog, 0, NULL, NULL, 2227 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2228 CTLTYPE_INT, "subnetsarelocal", 2229 SYSCTL_DESCR("Whether logical subnets are considered " 2230 "local"), 2231 NULL, 0, &subnetsarelocal, 0, 2232 CTL_NET, PF_INET, IPPROTO_IP, 2233 IPCTL_SUBNETSARELOCAL, CTL_EOL); 2234 sysctl_createv(clog, 0, NULL, NULL, 2235 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2236 CTLTYPE_INT, "mtudisc", 2237 SYSCTL_DESCR("Use RFC1191 Path MTU Discovery"), 2238 NULL, 0, &ip_mtudisc, 0, 2239 CTL_NET, PF_INET, IPPROTO_IP, 2240 IPCTL_MTUDISC, CTL_EOL); 2241 sysctl_createv(clog, 0, NULL, NULL, 2242 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2243 CTLTYPE_INT, "anonportmin", 2244 SYSCTL_DESCR("Lowest ephemeral port number to assign"), 2245 sysctl_net_inet_ip_ports, 0, &anonportmin, 0, 2246 CTL_NET, PF_INET, IPPROTO_IP, 2247 IPCTL_ANONPORTMIN, CTL_EOL); 2248 sysctl_createv(clog, 0, NULL, NULL, 2249 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2250 CTLTYPE_INT, "anonportmax", 2251 SYSCTL_DESCR("Highest ephemeral port number to assign"), 2252 sysctl_net_inet_ip_ports, 0, &anonportmax, 0, 2253 CTL_NET, PF_INET, IPPROTO_IP, 2254 IPCTL_ANONPORTMAX, CTL_EOL); 2255 sysctl_createv(clog, 0, NULL, NULL, 2256 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2257 CTLTYPE_INT, "mtudisctimeout", 2258 SYSCTL_DESCR("Lifetime of a Path MTU Discovered route"), 2259 sysctl_net_inet_ip_pmtudto, 0, &ip_mtudisc_timeout, 0, 2260 CTL_NET, PF_INET, IPPROTO_IP, 2261 IPCTL_MTUDISCTIMEOUT, CTL_EOL); 2262#ifdef GATEWAY 2263 sysctl_createv(clog, 0, NULL, NULL, 2264 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2265 CTLTYPE_INT, "maxflows", 2266 SYSCTL_DESCR("Number of flows for fast forwarding"), 2267 sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0, 2268 CTL_NET, PF_INET, IPPROTO_IP, 2269 IPCTL_MAXFLOWS, CTL_EOL); 2270#endif /* GATEWAY */ 2271 sysctl_createv(clog, 0, NULL, NULL, 2272 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2273 CTLTYPE_INT, "hostzerobroadcast", 2274 SYSCTL_DESCR("All zeroes address is broadcast address"), 2275 NULL, 0, &hostzeroisbroadcast, 0, 2276 CTL_NET, PF_INET, IPPROTO_IP, 2277 IPCTL_HOSTZEROBROADCAST, CTL_EOL); 2278#if NGIF > 0 2279 sysctl_createv(clog, 0, NULL, NULL, 2280 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2281 CTLTYPE_INT, "gifttl", 2282 SYSCTL_DESCR("Default TTL for a gif tunnel datagram"), 2283 NULL, 0, &ip_gif_ttl, 0, 2284 CTL_NET, PF_INET, IPPROTO_IP, 2285 IPCTL_GIF_TTL, CTL_EOL); 2286#endif /* NGIF */ 2287#ifndef IPNOPRIVPORTS 2288 sysctl_createv(clog, 0, NULL, NULL, 2289 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2290 CTLTYPE_INT, "lowportmin", 2291 SYSCTL_DESCR("Lowest privileged ephemeral port number " 2292 "to assign"), 2293 sysctl_net_inet_ip_ports, 0, &lowportmin, 0, 2294 CTL_NET, PF_INET, IPPROTO_IP, 2295 IPCTL_LOWPORTMIN, CTL_EOL); 2296 sysctl_createv(clog, 0, NULL, NULL, 2297 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2298 CTLTYPE_INT, "lowportmax", 2299 SYSCTL_DESCR("Highest privileged ephemeral port number " 2300 "to assign"), 2301 sysctl_net_inet_ip_ports, 0, &lowportmax, 0, 2302 CTL_NET, PF_INET, IPPROTO_IP, 2303 IPCTL_LOWPORTMAX, CTL_EOL); 2304#endif /* IPNOPRIVPORTS */ 2305 sysctl_createv(clog, 0, NULL, NULL, 2306 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2307 CTLTYPE_INT, "maxfragpackets", 2308 SYSCTL_DESCR("Maximum number of fragments to retain for " 2309 "possible reassembly"), 2310 NULL, 0, &ip_maxfragpackets, 0, 2311 CTL_NET, PF_INET, IPPROTO_IP, 2312 IPCTL_MAXFRAGPACKETS, CTL_EOL); 2313#if NGRE > 0 2314 sysctl_createv(clog, 0, NULL, NULL, 2315 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2316 CTLTYPE_INT, "grettl", 2317 SYSCTL_DESCR("Default TTL for a gre tunnel datagram"), 2318 NULL, 0, &ip_gre_ttl, 0, 2319 CTL_NET, PF_INET, IPPROTO_IP, 2320 IPCTL_GRE_TTL, CTL_EOL); 2321#endif /* NGRE */ 2322 sysctl_createv(clog, 0, NULL, NULL, 2323 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2324 CTLTYPE_INT, "checkinterface", 2325 SYSCTL_DESCR("Enable receive side of Strong ES model " 2326 "from RFC1122"), 2327 NULL, 0, &ip_checkinterface, 0, 2328 CTL_NET, PF_INET, IPPROTO_IP, 2329 IPCTL_CHECKINTERFACE, CTL_EOL); 2330 sysctl_createv(clog, 0, NULL, NULL, 2331 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2332 CTLTYPE_INT, "random_id", 2333 SYSCTL_DESCR("Assign random ip_id values"), 2334 NULL, 0, &ip_do_randomid, 0, 2335 CTL_NET, PF_INET, IPPROTO_IP, 2336 IPCTL_RANDOMID, CTL_EOL); 2337 sysctl_createv(clog, 0, NULL, NULL, 2338 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2339 CTLTYPE_INT, "do_loopback_cksum", 2340 SYSCTL_DESCR("Perform IP checksum on loopback"), 2341 NULL, 0, &ip_do_loopback_cksum, 0, 2342 CTL_NET, PF_INET, IPPROTO_IP, 2343 IPCTL_LOOPBACKCKSUM, CTL_EOL); 2344} 2345