[BACK]Return to ip_input.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet

Annotation of src/sys/netinet/ip_input.c, Revision 1.266

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

CVSweb <webmaster@jp.NetBSD.org>