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

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

CVSweb <webmaster@jp.NetBSD.org>