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

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

CVSweb <webmaster@jp.NetBSD.org>