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

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

CVSweb <webmaster@jp.NetBSD.org>