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