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

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

CVSweb <webmaster@jp.NetBSD.org>