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

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

CVSweb <webmaster@jp.NetBSD.org>