[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.275.4.1.8.1

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