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

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

CVSweb <webmaster@jp.NetBSD.org>