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

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

CVSweb <webmaster@jp.NetBSD.org>