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

1.294   ! dyoung      1: /*     $NetBSD: ip_input.c,v 1.293 2010/12/13 14:18:50 matt Exp $      */
1.89      itojun      2:
                      3: /*
                      4:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      5:  * All rights reserved.
1.152     itojun      6:  *
1.89      itojun      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. Neither the name of the project nor the names of its contributors
                     16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
1.152     itojun     18:  *
1.89      itojun     19:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
1.76      thorpej    31:
                     32: /*-
                     33:  * Copyright (c) 1998 The NetBSD Foundation, Inc.
                     34:  * All rights reserved.
                     35:  *
                     36:  * This code is derived from software contributed to The NetBSD Foundation
                     37:  * by Public Access Networks Corporation ("Panix").  It was developed under
                     38:  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
                     39:  *
                     40:  * Redistribution and use in source and binary forms, with or without
                     41:  * modification, are permitted provided that the following conditions
                     42:  * are met:
                     43:  * 1. Redistributions of source code must retain the above copyright
                     44:  *    notice, this list of conditions and the following disclaimer.
                     45:  * 2. Redistributions in binary form must reproduce the above copyright
                     46:  *    notice, this list of conditions and the following disclaimer in the
                     47:  *    documentation and/or other materials provided with the distribution.
                     48:  *
                     49:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     50:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     51:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     52:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     53:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     54:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     55:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     56:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     57:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     58:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     59:  * POSSIBILITY OF SUCH DAMAGE.
                     60:  */
1.14      cgd        61:
1.1       cgd        62: /*
1.13      mycroft    63:  * Copyright (c) 1982, 1986, 1988, 1993
                     64:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        65:  *
                     66:  * Redistribution and use in source and binary forms, with or without
                     67:  * modification, are permitted provided that the following conditions
                     68:  * are met:
                     69:  * 1. Redistributions of source code must retain the above copyright
                     70:  *    notice, this list of conditions and the following disclaimer.
                     71:  * 2. Redistributions in binary form must reproduce the above copyright
                     72:  *    notice, this list of conditions and the following disclaimer in the
                     73:  *    documentation and/or other materials provided with the distribution.
1.172     agc        74:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        75:  *    may be used to endorse or promote products derived from this software
                     76:  *    without specific prior written permission.
                     77:  *
                     78:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     79:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     80:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     81:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     82:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     83:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     84:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     85:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     86:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     87:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     88:  * SUCH DAMAGE.
                     89:  *
1.14      cgd        90:  *     @(#)ip_input.c  8.2 (Berkeley) 1/4/94
1.1       cgd        91:  */
1.141     lukem      92:
                     93: #include <sys/cdefs.h>
1.294   ! dyoung     94: __KERNEL_RCSID(0, "$NetBSD: ip_input.c,v 1.293 2010/12/13 14:18:50 matt Exp $");
1.55      scottr     95:
1.184     jonathan   96: #include "opt_inet.h"
1.278     christos   97: #include "opt_compat_netbsd.h"
1.62      matt       98: #include "opt_gateway.h"
1.69      mrg        99: #include "opt_pfil_hooks.h"
1.91      thorpej   100: #include "opt_ipsec.h"
1.55      scottr    101: #include "opt_mrouting.h"
1.167     martin    102: #include "opt_mbuftrace.h"
1.135     thorpej   103: #include "opt_inet_csum.h"
1.1       cgd       104:
1.5       mycroft   105: #include <sys/param.h>
                    106: #include <sys/systm.h>
                    107: #include <sys/mbuf.h>
                    108: #include <sys/domain.h>
                    109: #include <sys/protosw.h>
                    110: #include <sys/socket.h>
1.44      thorpej   111: #include <sys/socketvar.h>
1.5       mycroft   112: #include <sys/errno.h>
                    113: #include <sys/time.h>
                    114: #include <sys/kernel.h>
1.72      thorpej   115: #include <sys/pool.h>
1.28      christos  116: #include <sys/sysctl.h>
1.230     elad      117: #include <sys/kauth.h>
1.1       cgd       118:
1.5       mycroft   119: #include <net/if.h>
1.44      thorpej   120: #include <net/if_dl.h>
1.5       mycroft   121: #include <net/route.h>
1.45      mrg       122: #include <net/pfil.h>
1.1       cgd       123:
1.5       mycroft   124: #include <netinet/in.h>
                    125: #include <netinet/in_systm.h>
                    126: #include <netinet/ip.h>
                    127: #include <netinet/in_pcb.h>
1.215     yamt      128: #include <netinet/in_proto.h>
1.5       mycroft   129: #include <netinet/in_var.h>
                    130: #include <netinet/ip_var.h>
1.266     thorpej   131: #include <netinet/ip_private.h>
1.5       mycroft   132: #include <netinet/ip_icmp.h>
1.89      itojun    133: /* just for gif_ttl */
                    134: #include <netinet/in_gif.h>
                    135: #include "gif.h"
1.144     martin    136: #include <net/if_gre.h>
                    137: #include "gre.h"
1.111     jdolecek  138:
                    139: #ifdef MROUTING
                    140: #include <netinet/ip_mroute.h>
                    141: #endif
1.89      itojun    142:
                    143: #ifdef IPSEC
                    144: #include <netinet6/ipsec.h>
1.267     thorpej   145: #include <netinet6/ipsec_private.h>
1.89      itojun    146: #include <netkey/key.h>
                    147: #endif
1.173     jonathan  148: #ifdef FAST_IPSEC
                    149: #include <netipsec/ipsec.h>
                    150: #include <netipsec/key.h>
                    151: #endif /* FAST_IPSEC*/
1.44      thorpej   152:
1.1       cgd       153: #ifndef        IPFORWARDING
                    154: #ifdef GATEWAY
                    155: #define        IPFORWARDING    1       /* forward IP packets not for us */
                    156: #else /* GATEWAY */
                    157: #define        IPFORWARDING    0       /* don't forward IP packets not for us */
                    158: #endif /* GATEWAY */
                    159: #endif /* IPFORWARDING */
                    160: #ifndef        IPSENDREDIRECTS
                    161: #define        IPSENDREDIRECTS 1
                    162: #endif
1.26      thorpej   163: #ifndef IPFORWSRCRT
1.47      cjs       164: #define        IPFORWSRCRT     1       /* forward source-routed packets */
                    165: #endif
                    166: #ifndef IPALLOWSRCRT
1.48      mrg       167: #define        IPALLOWSRCRT    1       /* allow source-routed packets */
1.26      thorpej   168: #endif
1.53      kml       169: #ifndef IPMTUDISC
1.153     itojun    170: #define IPMTUDISC      1
1.53      kml       171: #endif
1.60      kml       172: #ifndef IPMTUDISCTIMEOUT
1.61      kml       173: #define IPMTUDISCTIMEOUT (10 * 60)     /* as per RFC 1191 */
1.60      kml       174: #endif
1.53      kml       175:
1.278     christos  176: #ifdef COMPAT_50
                    177: #include <compat/sys/time.h>
                    178: #include <compat/sys/socket.h>
                    179: #endif
                    180:
1.27      thorpej   181: /*
                    182:  * Note: DIRECTED_BROADCAST is handled this way so that previous
                    183:  * configuration using this option will Just Work.
                    184:  */
                    185: #ifndef IPDIRECTEDBCAST
                    186: #ifdef DIRECTED_BROADCAST
                    187: #define IPDIRECTEDBCAST        1
                    188: #else
                    189: #define        IPDIRECTEDBCAST 0
                    190: #endif /* DIRECTED_BROADCAST */
                    191: #endif /* IPDIRECTEDBCAST */
1.1       cgd       192: int    ipforwarding = IPFORWARDING;
                    193: int    ipsendredirects = IPSENDREDIRECTS;
1.13      mycroft   194: int    ip_defttl = IPDEFTTL;
1.26      thorpej   195: int    ip_forwsrcrt = IPFORWSRCRT;
1.27      thorpej   196: int    ip_directedbcast = IPDIRECTEDBCAST;
1.47      cjs       197: int    ip_allowsrcrt = IPALLOWSRCRT;
1.53      kml       198: int    ip_mtudisc = IPMTUDISC;
1.156     itojun    199: int    ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
1.1       cgd       200: #ifdef DIAGNOSTIC
                    201: int    ipprintfs = 0;
                    202: #endif
1.184     jonathan  203:
                    204: int    ip_do_randomid = 0;
                    205:
1.165     christos  206: /*
                    207:  * XXX - Setting ip_checkinterface mostly implements the receive side of
                    208:  * the Strong ES model described in RFC 1122, but since the routing table
                    209:  * and transmit implementation do not implement the Strong ES model,
                    210:  * setting this to 1 results in an odd hybrid.
                    211:  *
                    212:  * XXX - ip_checkinterface currently must be disabled if you use ipnat
                    213:  * to translate the destination address to another local interface.
                    214:  *
                    215:  * XXX - ip_checkinterface must be disabled if you add IP aliases
                    216:  * to the loopback interface instead of the interface where the
                    217:  * packets for those addresses are received.
                    218:  */
                    219: int    ip_checkinterface = 0;
                    220:
1.1       cgd       221:
1.60      kml       222: struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
                    223:
1.150     matt      224: u_long in_ifaddrhash;                          /* size of hash table - 1 */
                    225: int    in_ifaddrentries;                       /* total number of addrs */
1.212     perry     226: struct in_ifaddrhead in_ifaddrhead;
1.57      tls       227: struct in_ifaddrhashhead *in_ifaddrhashtbl;
1.166     matt      228: u_long in_multihash;                           /* size of hash table - 1 */
                    229: int    in_multientries;                        /* total number of addrs */
                    230: struct in_multihashhead *in_multihashtbl;
1.13      mycroft   231: struct ifqueue ipintrq;
1.286     tls       232:
1.291     rmind     233: ipid_state_t *         ip_ids;
1.183     jonathan  234: uint16_t ip_id;
1.75      thorpej   235:
1.266     thorpej   236: percpu_t *ipstat_percpu;
                    237:
1.121     thorpej   238: #ifdef PFIL_HOOKS
                    239: struct pfil_head inet_pfil_hook;
                    240: #endif
                    241:
1.275     pooka     242: struct pool inmulti_pool;
1.72      thorpej   243:
1.135     thorpej   244: #ifdef INET_CSUM_COUNTERS
                    245: #include <sys/device.h>
                    246:
                    247: struct evcnt ip_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    248:     NULL, "inet", "hwcsum bad");
                    249: struct evcnt ip_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    250:     NULL, "inet", "hwcsum ok");
                    251: struct evcnt ip_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    252:     NULL, "inet", "swcsum");
                    253:
                    254: #define        INET_CSUM_COUNTER_INCR(ev)      (ev)->ev_count++
                    255:
1.201     matt      256: EVCNT_ATTACH_STATIC(ip_hwcsum_bad);
                    257: EVCNT_ATTACH_STATIC(ip_hwcsum_ok);
                    258: EVCNT_ATTACH_STATIC(ip_swcsum);
                    259:
1.135     thorpej   260: #else
                    261:
                    262: #define        INET_CSUM_COUNTER_INCR(ev)      /* nothing */
                    263:
                    264: #endif /* INET_CSUM_COUNTERS */
                    265:
1.1       cgd       266: /*
                    267:  * We need to save the IP options in case a protocol wants to respond
                    268:  * to an incoming packet over the same route if the packet got here
                    269:  * using IP source routing.  This allows connection establishment and
                    270:  * maintenance when the remote end is on a network that is not known
                    271:  * to us.
                    272:  */
                    273: int    ip_nhops = 0;
                    274: static struct ip_srcrt {
                    275:        struct  in_addr dst;                    /* final destination */
                    276:        char    nop;                            /* one NOP to align */
                    277:        char    srcopt[IPOPT_OFFSET + 1];       /* OPTVAL, OLEN and OFFSET */
                    278:        struct  in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
                    279: } ip_srcrt;
                    280:
1.210     perry     281: static void save_rte(u_char *, struct in_addr);
1.35      mycroft   282:
1.164     matt      283: #ifdef MBUFTRACE
1.234     dogcow    284: struct mowner ip_rx_mowner = MOWNER_INIT("internet", "rx");
                    285: struct mowner ip_tx_mowner = MOWNER_INIT("internet", "tx");
1.164     matt      286: #endif
                    287:
1.284     pooka     288: static void sysctl_net_inet_ip_setup(struct sysctllog **);
                    289:
1.1       cgd       290: /*
                    291:  * IP initialization: fill in IP protocol switch table.
                    292:  * All protocols not implemented in kernel go to raw IP protocol handler.
                    293:  */
1.8       mycroft   294: void
1.211     perry     295: ip_init(void)
1.1       cgd       296: {
1.199     matt      297:        const struct protosw *pr;
1.109     augustss  298:        int i;
1.1       cgd       299:
1.284     pooka     300:        sysctl_net_inet_ip_setup(NULL);
                    301:
1.275     pooka     302:        pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
                    303:            NULL, IPL_SOFTNET);
                    304:
1.1       cgd       305:        pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
                    306:        if (pr == 0)
                    307:                panic("ip_init");
                    308:        for (i = 0; i < IPPROTO_MAX; i++)
                    309:                ip_protox[i] = pr - inetsw;
                    310:        for (pr = inetdomain.dom_protosw;
                    311:            pr < inetdomain.dom_protoswNPROTOSW; pr++)
                    312:                if (pr->pr_domain->dom_family == PF_INET &&
                    313:                    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
                    314:                        ip_protox[pr->pr_protocol] = pr - inetsw;
1.192     jonathan  315:
1.288     rmind     316:        ip_reass_init();
1.190     jonathan  317:
1.291     rmind     318:        ip_ids = ip_id_init();
1.227     kardel    319:        ip_id = time_second & 0xfffff;
1.194     jonathan  320:
1.294   ! dyoung    321:        ipintrq.ifq_maxlen = IFQ_MAXLEN;
1.194     jonathan  322:
1.181     jonathan  323:        TAILQ_INIT(&in_ifaddrhead);
1.272     ad        324:        in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
                    325:            &in_ifaddrhash);
                    326:        in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
                    327:            &in_multihash);
1.160     itojun    328:        ip_mtudisc_timeout_q = rt_timer_queue_create(ip_mtudisc_timeout);
1.73      thorpej   329: #ifdef GATEWAY
1.248     liamjfoy  330:        ipflow_init(ip_hashsize);
1.73      thorpej   331: #endif
1.121     thorpej   332:
                    333: #ifdef PFIL_HOOKS
                    334:        /* Register our Packet Filter hook. */
1.126     thorpej   335:        inet_pfil_hook.ph_type = PFIL_TYPE_AF;
                    336:        inet_pfil_hook.ph_af   = AF_INET;
1.121     thorpej   337:        i = pfil_head_register(&inet_pfil_hook);
                    338:        if (i != 0)
                    339:                printf("ip_init: WARNING: unable to register pfil hook, "
                    340:                    "error %d\n", i);
                    341: #endif /* PFIL_HOOKS */
1.135     thorpej   342:
1.164     matt      343: #ifdef MBUFTRACE
                    344:        MOWNER_ATTACH(&ip_tx_mowner);
                    345:        MOWNER_ATTACH(&ip_rx_mowner);
                    346: #endif /* MBUFTRACE */
1.266     thorpej   347:
                    348:        ipstat_percpu = percpu_alloc(sizeof(uint64_t) * IP_NSTATS);
1.1       cgd       349: }
                    350:
1.229     christos  351: struct sockaddr_in ipaddr = {
                    352:        .sin_len = sizeof(ipaddr),
                    353:        .sin_family = AF_INET,
                    354: };
1.1       cgd       355: struct route ipforward_rt;
                    356:
                    357: /*
1.89      itojun    358:  * IP software interrupt routine
                    359:  */
                    360: void
1.211     perry     361: ipintr(void)
1.89      itojun    362: {
                    363:        int s;
                    364:        struct mbuf *m;
1.286     tls       365:        struct ifqueue lcl_intrq;
                    366:
                    367:        memset(&lcl_intrq, 0, sizeof(lcl_intrq));
1.89      itojun    368:
1.268     ad        369:        mutex_enter(softnet_lock);
                    370:        KERNEL_LOCK(1, NULL);
1.286     tls       371:        if (!IF_IS_EMPTY(&ipintrq)) {
1.132     thorpej   372:                s = splnet();
1.286     tls       373:
                    374:                /* Take existing queue onto stack */
                    375:                lcl_intrq = ipintrq;
                    376:
                    377:                /* Zero out global queue, preserving maxlen and drops */
                    378:                ipintrq.ifq_head = NULL;
                    379:                ipintrq.ifq_tail = NULL;
                    380:                ipintrq.ifq_len = 0;
                    381:                ipintrq.ifq_maxlen = lcl_intrq.ifq_maxlen;
                    382:                ipintrq.ifq_drops = lcl_intrq.ifq_drops;
                    383:
1.89      itojun    384:                splx(s);
1.286     tls       385:        }
                    386:        KERNEL_UNLOCK_ONE(NULL);
                    387:        while (!IF_IS_EMPTY(&lcl_intrq)) {
                    388:                IF_DEQUEUE(&lcl_intrq, m);
1.268     ad        389:                if (m == NULL)
                    390:                        break;
1.89      itojun    391:                ip_input(m);
                    392:        }
1.268     ad        393:        mutex_exit(softnet_lock);
1.89      itojun    394: }
                    395:
                    396: /*
1.1       cgd       397:  * Ip input routine.  Checksum and byte swap header.  If fragmented
                    398:  * try to reassemble.  Process options.  Pass to next level.
                    399:  */
1.8       mycroft   400: void
1.89      itojun    401: ip_input(struct mbuf *m)
1.1       cgd       402: {
1.109     augustss  403:        struct ip *ip = NULL;
                    404:        struct in_ifaddr *ia;
                    405:        struct ifaddr *ifa;
1.288     rmind     406:        int hlen = 0, len;
1.100     itojun    407:        int downmatch;
1.165     christos  408:        int checkif;
1.169     itojun    409:        int srcrt = 0;
1.173     jonathan  410: #ifdef FAST_IPSEC
                    411:        struct m_tag *mtag;
                    412:        struct tdb_ident *tdbi;
                    413:        struct secpolicy *sp;
1.289     rmind     414:        int error, s;
1.173     jonathan  415: #endif /* FAST_IPSEC */
1.1       cgd       416:
1.164     matt      417:        MCLAIM(m, &ip_rx_mowner);
1.289     rmind     418:        KASSERT((m->m_flags & M_PKTHDR) != 0);
1.164     matt      419:
1.1       cgd       420:        /*
                    421:         * If no IP addresses have been set yet but the interfaces
                    422:         * are receiving, can't do anything with incoming packets yet.
                    423:         */
1.181     jonathan  424:        if (TAILQ_FIRST(&in_ifaddrhead) == 0)
1.1       cgd       425:                goto bad;
1.266     thorpej   426:        IP_STATINC(IP_STAT_TOTAL);
1.154     thorpej   427:        /*
                    428:         * If the IP header is not aligned, slurp it up into a new
                    429:         * mbuf with space for link headers, in the event we forward
                    430:         * it.  Otherwise, if it is aligned, make sure the entire
                    431:         * base IP header is in the first mbuf of the chain.
                    432:         */
1.244     christos  433:        if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
1.154     thorpej   434:                if ((m = m_copyup(m, sizeof(struct ip),
                    435:                                  (max_linkhdr + 3) & ~3)) == NULL) {
                    436:                        /* XXXJRT new stat, please */
1.266     thorpej   437:                        IP_STATINC(IP_STAT_TOOSMALL);
1.154     thorpej   438:                        return;
                    439:                }
                    440:        } else if (__predict_false(m->m_len < sizeof (struct ip))) {
                    441:                if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
1.266     thorpej   442:                        IP_STATINC(IP_STAT_TOOSMALL);
1.154     thorpej   443:                        return;
                    444:                }
1.1       cgd       445:        }
                    446:        ip = mtod(m, struct ip *);
1.13      mycroft   447:        if (ip->ip_v != IPVERSION) {
1.266     thorpej   448:                IP_STATINC(IP_STAT_BADVERS);
1.13      mycroft   449:                goto bad;
                    450:        }
1.1       cgd       451:        hlen = ip->ip_hl << 2;
                    452:        if (hlen < sizeof(struct ip)) { /* minimum header length */
1.266     thorpej   453:                IP_STATINC(IP_STAT_BADHLEN);
1.1       cgd       454:                goto bad;
                    455:        }
                    456:        if (hlen > m->m_len) {
                    457:                if ((m = m_pullup(m, hlen)) == 0) {
1.266     thorpej   458:                        IP_STATINC(IP_STAT_BADHLEN);
1.89      itojun    459:                        return;
1.1       cgd       460:                }
                    461:                ip = mtod(m, struct ip *);
                    462:        }
1.98      thorpej   463:
1.85      hwr       464:        /*
1.99      thorpej   465:         * RFC1122: packets with a multicast source address are
1.98      thorpej   466:         * not allowed.
1.85      hwr       467:         */
                    468:        if (IN_MULTICAST(ip->ip_src.s_addr)) {
1.266     thorpej   469:                IP_STATINC(IP_STAT_BADADDR);
1.85      hwr       470:                goto bad;
1.129     itojun    471:        }
                    472:
                    473:        /* 127/8 must not appear on wire - RFC1122 */
                    474:        if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
                    475:            (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
1.130     itojun    476:                if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
1.266     thorpej   477:                        IP_STATINC(IP_STAT_BADADDR);
1.129     itojun    478:                        goto bad;
1.130     itojun    479:                }
1.85      hwr       480:        }
                    481:
1.135     thorpej   482:        switch (m->m_pkthdr.csum_flags &
1.137     thorpej   483:                ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
1.135     thorpej   484:                 M_CSUM_IPv4_BAD)) {
                    485:        case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
                    486:                INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad);
                    487:                goto badcsum;
                    488:
                    489:        case M_CSUM_IPv4:
                    490:                /* Checksum was okay. */
                    491:                INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok);
                    492:                break;
                    493:
                    494:        default:
1.206     thorpej   495:                /*
                    496:                 * Must compute it ourselves.  Maybe skip checksum on
                    497:                 * loopback interfaces.
                    498:                 */
                    499:                if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
                    500:                                     IFF_LOOPBACK) || ip_do_loopback_cksum)) {
                    501:                        INET_CSUM_COUNTER_INCR(&ip_swcsum);
                    502:                        if (in_cksum(m, hlen) != 0)
                    503:                                goto badcsum;
                    504:                }
1.135     thorpej   505:                break;
1.1       cgd       506:        }
                    507:
1.121     thorpej   508:        /* Retrieve the packet length. */
                    509:        len = ntohs(ip->ip_len);
1.81      proff     510:
                    511:        /*
                    512:         * Check for additional length bogosity
                    513:         */
1.84      proff     514:        if (len < hlen) {
1.266     thorpej   515:                IP_STATINC(IP_STAT_BADLEN);
1.81      proff     516:                goto bad;
                    517:        }
1.1       cgd       518:
                    519:        /*
                    520:         * Check that the amount of data in the buffers
                    521:         * is as at least much as the IP header would have us expect.
                    522:         * Trim mbufs if longer than we expect.
                    523:         * Drop packet if shorter than we expect.
                    524:         */
1.35      mycroft   525:        if (m->m_pkthdr.len < len) {
1.266     thorpej   526:                IP_STATINC(IP_STAT_TOOSHORT);
1.1       cgd       527:                goto bad;
                    528:        }
1.35      mycroft   529:        if (m->m_pkthdr.len > len) {
1.1       cgd       530:                if (m->m_len == m->m_pkthdr.len) {
1.35      mycroft   531:                        m->m_len = len;
                    532:                        m->m_pkthdr.len = len;
1.1       cgd       533:                } else
1.35      mycroft   534:                        m_adj(m, len - m->m_pkthdr.len);
1.1       cgd       535:        }
                    536:
1.193     scw       537: #if defined(IPSEC)
1.149     wiz       538:        /* ipflow (IP fast forwarding) is not compatible with IPsec. */
1.94      itojun    539:        m->m_flags &= ~M_CANFASTFWD;
                    540: #else
1.64      thorpej   541:        /*
                    542:         * Assume that we can create a fast-forward IP flow entry
                    543:         * based on this packet.
                    544:         */
                    545:        m->m_flags |= M_CANFASTFWD;
1.94      itojun    546: #endif
1.64      thorpej   547:
1.36      mrg       548: #ifdef PFIL_HOOKS
1.33      mrg       549:        /*
1.64      thorpej   550:         * Run through list of hooks for input packets.  If there are any
                    551:         * filters which require that additional packets in the flow are
                    552:         * not fast-forwarded, they must clear the M_CANFASTFWD flag.
                    553:         * Note that filters must _never_ set this flag, as another filter
                    554:         * in the list may have previously cleared it.
1.33      mrg       555:         */
1.127     itojun    556:        /*
                    557:         * let ipfilter look at packet on the wire,
                    558:         * not the decapsulated packet.
                    559:         */
                    560: #ifdef IPSEC
1.136     itojun    561:        if (!ipsec_getnhist(m))
1.186     scw       562: #elif defined(FAST_IPSEC)
                    563:        if (!ipsec_indone(m))
1.127     itojun    564: #else
                    565:        if (1)
                    566: #endif
                    567:        {
1.169     itojun    568:                struct in_addr odst;
                    569:
                    570:                odst = ip->ip_dst;
1.127     itojun    571:                if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
1.168     itojun    572:                    PFIL_IN) != 0)
                    573:                        return;
1.127     itojun    574:                if (m == NULL)
                    575:                        return;
                    576:                ip = mtod(m, struct ip *);
1.142     darrenr   577:                hlen = ip->ip_hl << 2;
1.205     darrenr   578:                /*
                    579:                 * XXX The setting of "srcrt" here is to prevent ip_forward()
                    580:                 * from generating ICMP redirects for packets that have
                    581:                 * been redirected by a hook back out on to the same LAN that
                    582:                 * they came from and is not an indication that the packet
                    583:                 * is being inffluenced by source routing options.  This
                    584:                 * allows things like
                    585:                 * "rdr tlp0 0/0 port 80 -> 1.1.1.200 3128 tcp"
                    586:                 * where tlp0 is both on the 1.1.1.0/24 network and is the
                    587:                 * default route for hosts on 1.1.1.0/24.  Of course this
                    588:                 * also requires a "map tlp0 ..." to complete the story.
                    589:                 * One might argue whether or not this kind of network config.
1.212     perry     590:                 * should be supported in this manner...
1.205     darrenr   591:                 */
1.169     itojun    592:                srcrt = (odst.s_addr != ip->ip_dst.s_addr);
1.127     itojun    593:        }
1.36      mrg       594: #endif /* PFIL_HOOKS */
1.123     thorpej   595:
                    596: #ifdef ALTQ
                    597:        /* XXX Temporary until ALTQ is changed to use a pfil hook */
                    598:        if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
                    599:                /* packet dropped by traffic conditioner */
                    600:                return;
                    601:        }
                    602: #endif
1.121     thorpej   603:
                    604:        /*
1.1       cgd       605:         * Process options and, if not destined for us,
                    606:         * ship it on.  ip_dooptions returns 1 when an
                    607:         * error was detected (causing an icmp message
                    608:         * to be sent and the original packet to be freed).
                    609:         */
                    610:        ip_nhops = 0;           /* for source routed packets */
                    611:        if (hlen > sizeof (struct ip) && ip_dooptions(m))
1.89      itojun    612:                return;
1.1       cgd       613:
                    614:        /*
1.165     christos  615:         * Enable a consistency check between the destination address
                    616:         * and the arrival interface for a unicast packet (the RFC 1122
                    617:         * strong ES model) if IP forwarding is disabled and the packet
                    618:         * is not locally generated.
                    619:         *
                    620:         * XXX - Checking also should be disabled if the destination
                    621:         * address is ipnat'ed to a different interface.
                    622:         *
                    623:         * XXX - Checking is incompatible with IP aliases added
                    624:         * to the loopback interface instead of the interface where
                    625:         * the packets are received.
                    626:         *
                    627:         * XXX - We need to add a per ifaddr flag for this so that
                    628:         * we get finer grain control.
                    629:         */
                    630:        checkif = ip_checkinterface && (ipforwarding == 0) &&
                    631:            (m->m_pkthdr.rcvif != NULL) &&
                    632:            ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0);
                    633:
                    634:        /*
1.1       cgd       635:         * Check our list of addresses, to see if the packet is for us.
1.100     itojun    636:         *
                    637:         * Traditional 4.4BSD did not consult IFF_UP at all.
                    638:         * The behavior here is to treat addresses on !IFF_UP interface
                    639:         * as not mine.
1.1       cgd       640:         */
1.100     itojun    641:        downmatch = 0;
1.140     matt      642:        LIST_FOREACH(ia, &IN_IFADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
1.97      itojun    643:                if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
1.165     christos  644:                        if (checkif && ia->ia_ifp != m->m_pkthdr.rcvif)
                    645:                                continue;
1.97      itojun    646:                        if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
                    647:                                break;
1.100     itojun    648:                        else
                    649:                                downmatch++;
1.97      itojun    650:                }
                    651:        }
1.86      thorpej   652:        if (ia != NULL)
                    653:                goto ours;
1.225     christos  654:        if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
1.209     matt      655:                IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
1.140     matt      656:                        if (ifa->ifa_addr->sa_family != AF_INET)
                    657:                                continue;
1.57      tls       658:                        ia = ifatoia(ifa);
1.35      mycroft   659:                        if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
                    660:                            in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
1.20      mycroft   661:                            /*
                    662:                             * Look for all-0's host part (old broadcast addr),
                    663:                             * either for subnet or net.
                    664:                             */
                    665:                            ip->ip_dst.s_addr == ia->ia_subnet ||
1.18      mycroft   666:                            ip->ip_dst.s_addr == ia->ia_net)
1.1       cgd       667:                                goto ours;
1.57      tls       668:                        /*
                    669:                         * An interface with IP address zero accepts
                    670:                         * all packets that arrive on that interface.
                    671:                         */
                    672:                        if (in_nullhost(ia->ia_addr.sin_addr))
                    673:                                goto ours;
1.1       cgd       674:                }
                    675:        }
1.18      mycroft   676:        if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4       hpeyerl   677:                struct in_multi *inm;
                    678: #ifdef MROUTING
                    679:                extern struct socket *ip_mrouter;
1.10      brezak    680:
1.4       hpeyerl   681:                if (ip_mrouter) {
                    682:                        /*
                    683:                         * If we are acting as a multicast router, all
                    684:                         * incoming multicast packets are passed to the
                    685:                         * kernel-level multicast forwarding function.
                    686:                         * The packet is returned (relatively) intact; if
                    687:                         * ip_mforward() returns a non-zero value, the packet
                    688:                         * must be discarded, else it may be accepted below.
                    689:                         *
                    690:                         * (The IP ident field is put in the same byte order
                    691:                         * as expected when ip_mforward() is called from
                    692:                         * ip_output().)
                    693:                         */
1.13      mycroft   694:                        if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
1.266     thorpej   695:                                IP_STATINC(IP_STAT_CANTFORWARD);
1.4       hpeyerl   696:                                m_freem(m);
1.89      itojun    697:                                return;
1.4       hpeyerl   698:                        }
                    699:
                    700:                        /*
                    701:                         * The process-level routing demon needs to receive
                    702:                         * all multicast IGMP packets, whether or not this
                    703:                         * host belongs to their destination groups.
                    704:                         */
                    705:                        if (ip->ip_p == IPPROTO_IGMP)
                    706:                                goto ours;
1.266     thorpej   707:                        IP_STATINC(IP_STAT_CANTFORWARD);
1.4       hpeyerl   708:                }
                    709: #endif
                    710:                /*
                    711:                 * See if we belong to the destination multicast group on the
                    712:                 * arrival interface.
                    713:                 */
                    714:                IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
                    715:                if (inm == NULL) {
1.266     thorpej   716:                        IP_STATINC(IP_STAT_CANTFORWARD);
1.4       hpeyerl   717:                        m_freem(m);
1.89      itojun    718:                        return;
1.4       hpeyerl   719:                }
                    720:                goto ours;
                    721:        }
1.19      mycroft   722:        if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
1.35      mycroft   723:            in_nullhost(ip->ip_dst))
1.1       cgd       724:                goto ours;
                    725:
                    726:        /*
                    727:         * Not for us; forward if possible and desirable.
                    728:         */
                    729:        if (ipforwarding == 0) {
1.266     thorpej   730:                IP_STATINC(IP_STAT_CANTFORWARD);
1.1       cgd       731:                m_freem(m);
1.100     itojun    732:        } else {
                    733:                /*
                    734:                 * If ip_dst matched any of my address on !IFF_UP interface,
                    735:                 * and there's no IFF_UP interface that matches ip_dst,
                    736:                 * send icmp unreach.  Forwarding it will result in in-kernel
                    737:                 * forwarding loop till TTL goes to 0.
                    738:                 */
                    739:                if (downmatch) {
                    740:                        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1.266     thorpej   741:                        IP_STATINC(IP_STAT_CANTFORWARD);
1.100     itojun    742:                        return;
                    743:                }
1.145     itojun    744: #ifdef IPSEC
                    745:                if (ipsec4_in_reject(m, NULL)) {
1.267     thorpej   746:                        IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.145     itojun    747:                        goto bad;
                    748:                }
                    749: #endif
1.173     jonathan  750: #ifdef FAST_IPSEC
                    751:                mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
                    752:                s = splsoftnet();
                    753:                if (mtag != NULL) {
                    754:                        tdbi = (struct tdb_ident *)(mtag + 1);
                    755:                        sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
                    756:                } else {
                    757:                        sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
1.212     perry     758:                                                   IP_FORWARDING, &error);
1.173     jonathan  759:                }
                    760:                if (sp == NULL) {       /* NB: can happen if error */
                    761:                        splx(s);
                    762:                        /*XXX error stat???*/
                    763:                        DPRINTF(("ip_input: no SP for forwarding\n"));  /*XXX*/
                    764:                        goto bad;
                    765:                }
                    766:
                    767:                /*
                    768:                 * Check security policy against packet attributes.
                    769:                 */
                    770:                error = ipsec_in_reject(sp, m);
                    771:                KEY_FREESP(&sp);
                    772:                splx(s);
                    773:                if (error) {
1.266     thorpej   774:                        IP_STATINC(IP_STAT_CANTFORWARD);
1.173     jonathan  775:                        goto bad;
1.193     scw       776:                }
                    777:
                    778:                /*
                    779:                 * Peek at the outbound SP for this packet to determine if
                    780:                 * it's a Fast Forward candidate.
                    781:                 */
                    782:                mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
                    783:                if (mtag != NULL)
                    784:                        m->m_flags &= ~M_CANFASTFWD;
                    785:                else {
                    786:                        s = splsoftnet();
                    787:                        sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND,
                    788:                            (IP_FORWARDING |
                    789:                             (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
                    790:                            &error, NULL);
                    791:                        if (sp != NULL) {
                    792:                                m->m_flags &= ~M_CANFASTFWD;
                    793:                                KEY_FREESP(&sp);
                    794:                        }
                    795:                        splx(s);
1.173     jonathan  796:                }
                    797: #endif /* FAST_IPSEC */
1.145     itojun    798:
1.169     itojun    799:                ip_forward(m, srcrt);
1.100     itojun    800:        }
1.89      itojun    801:        return;
1.1       cgd       802:
                    803: ours:
                    804:        /*
                    805:         * If offset or IP_MF are set, must reassemble.
                    806:         */
1.155     itojun    807:        if (ip->ip_off & ~htons(IP_DF|IP_RF)) {
1.289     rmind     808:                /*
                    809:                 * Pass to IP reassembly mechanism.
                    810:                 */
1.290     rmind     811:                if (ip_reass_packet(&m, ip) != 0) {
1.289     rmind     812:                        /* Failed; invalid fragment(s) or packet. */
1.288     rmind     813:                        goto bad;
1.16      cgd       814:                }
1.290     rmind     815:                if (m == NULL) {
1.289     rmind     816:                        /* More fragments should come; silently return. */
                    817:                        return;
                    818:                }
1.290     rmind     819:                /*
                    820:                 * Reassembly is done, we have the final packet.
                    821:                 * Updated cached data in local variable(s).
                    822:                 */
1.289     rmind     823:                ip = mtod(m, struct ip *);
                    824:                hlen = ip->ip_hl << 2;
1.79      mycroft   825:        }
1.128     itojun    826:
1.173     jonathan  827: #if defined(IPSEC)
1.128     itojun    828:        /*
                    829:         * enforce IPsec policy checking if we are seeing last header.
                    830:         * note that we do not visit this with protocols with pcb layer
                    831:         * code - like udp/tcp/raw ip.
                    832:         */
                    833:        if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
                    834:            ipsec4_in_reject(m, NULL)) {
1.267     thorpej   835:                IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.128     itojun    836:                goto bad;
                    837:        }
                    838: #endif
1.226     liamjfoy  839: #ifdef FAST_IPSEC
1.173     jonathan  840:        /*
                    841:         * enforce IPsec policy checking if we are seeing last header.
                    842:         * note that we do not visit this with protocols with pcb layer
                    843:         * code - like udp/tcp/raw ip.
                    844:         */
                    845:        if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
                    846:                /*
                    847:                 * Check if the packet has already had IPsec processing
                    848:                 * done.  If so, then just pass it along.  This tag gets
                    849:                 * set during AH, ESP, etc. input handling, before the
                    850:                 * packet is returned to the ip input queue for delivery.
1.212     perry     851:                 */
1.173     jonathan  852:                mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
                    853:                s = splsoftnet();
                    854:                if (mtag != NULL) {
                    855:                        tdbi = (struct tdb_ident *)(mtag + 1);
                    856:                        sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
                    857:                } else {
                    858:                        sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
1.212     perry     859:                                                   IP_FORWARDING, &error);
1.173     jonathan  860:                }
                    861:                if (sp != NULL) {
                    862:                        /*
                    863:                         * Check security policy against packet attributes.
                    864:                         */
                    865:                        error = ipsec_in_reject(sp, m);
                    866:                        KEY_FREESP(&sp);
                    867:                } else {
                    868:                        /* XXX error stat??? */
                    869:                        error = EINVAL;
                    870: DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
                    871:                }
                    872:                splx(s);
                    873:                if (error)
                    874:                        goto bad;
                    875:        }
                    876: #endif /* FAST_IPSEC */
1.1       cgd       877:
                    878:        /*
                    879:         * Switch out to protocol's input routine.
                    880:         */
1.82      aidan     881: #if IFA_STATS
1.122     itojun    882:        if (ia && ip)
1.155     itojun    883:                ia->ia_ifa.ifa_data.ifad_inbytes += ntohs(ip->ip_len);
1.82      aidan     884: #endif
1.266     thorpej   885:        IP_STATINC(IP_STAT_DELIVERED);
1.89      itojun    886:     {
                    887:        int off = hlen, nh = ip->ip_p;
                    888:
                    889:        (*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
                    890:        return;
                    891:     }
1.1       cgd       892: bad:
                    893:        m_freem(m);
1.135     thorpej   894:        return;
                    895:
                    896: badcsum:
1.266     thorpej   897:        IP_STATINC(IP_STAT_BADSUM);
1.135     thorpej   898:        m_freem(m);
1.1       cgd       899: }
                    900:
                    901: /*
1.288     rmind     902:  * IP timer processing.
1.1       cgd       903:  */
1.8       mycroft   904: void
1.211     perry     905: ip_slowtimo(void)
1.1       cgd       906: {
1.268     ad        907:
                    908:        mutex_enter(softnet_lock);
                    909:        KERNEL_LOCK(1, NULL);
1.1       cgd       910:
1.288     rmind     911:        ip_reass_slowtimo();
1.268     ad        912:
                    913:        KERNEL_UNLOCK_ONE(NULL);
                    914:        mutex_exit(softnet_lock);
1.1       cgd       915: }
                    916:
                    917: /*
1.288     rmind     918:  * IP drain processing.
1.1       cgd       919:  */
1.8       mycroft   920: void
1.211     perry     921: ip_drain(void)
1.1       cgd       922: {
                    923:
1.268     ad        924:        KERNEL_LOCK(1, NULL);
1.288     rmind     925:        ip_reass_drain();
1.268     ad        926:        KERNEL_UNLOCK_ONE(NULL);
1.1       cgd       927: }
                    928:
                    929: /*
                    930:  * Do option processing on a datagram,
                    931:  * possibly discarding it if bad options are encountered,
                    932:  * or forwarding it if source-routed.
                    933:  * Returns 1 if packet has been forwarded/freed,
                    934:  * 0 if the packet should be processed further.
                    935:  */
1.8       mycroft   936: int
1.211     perry     937: ip_dooptions(struct mbuf *m)
1.1       cgd       938: {
1.109     augustss  939:        struct ip *ip = mtod(m, struct ip *);
                    940:        u_char *cp, *cp0;
                    941:        struct ip_timestamp *ipt;
                    942:        struct in_ifaddr *ia;
1.1       cgd       943:        int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1.104     thorpej   944:        struct in_addr dst;
1.1       cgd       945:        n_time ntime;
                    946:
1.13      mycroft   947:        dst = ip->ip_dst;
1.1       cgd       948:        cp = (u_char *)(ip + 1);
                    949:        cnt = (ip->ip_hl << 2) - sizeof (struct ip);
                    950:        for (; cnt > 0; cnt -= optlen, cp += optlen) {
                    951:                opt = cp[IPOPT_OPTVAL];
                    952:                if (opt == IPOPT_EOL)
                    953:                        break;
                    954:                if (opt == IPOPT_NOP)
                    955:                        optlen = 1;
                    956:                else {
1.113     itojun    957:                        if (cnt < IPOPT_OLEN + sizeof(*cp)) {
                    958:                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
                    959:                                goto bad;
                    960:                        }
1.1       cgd       961:                        optlen = cp[IPOPT_OLEN];
1.114     itojun    962:                        if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1.1       cgd       963:                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
                    964:                                goto bad;
                    965:                        }
                    966:                }
                    967:                switch (opt) {
                    968:
                    969:                default:
                    970:                        break;
                    971:
                    972:                /*
                    973:                 * Source routing with record.
                    974:                 * Find interface with current destination address.
                    975:                 * If none on this machine then drop if strictly routed,
                    976:                 * or do nothing if loosely routed.
                    977:                 * Record interface address and bring up next address
                    978:                 * component.  If strictly routed make sure next
                    979:                 * address is on directly accessible net.
                    980:                 */
                    981:                case IPOPT_LSRR:
                    982:                case IPOPT_SSRR:
1.47      cjs       983:                        if (ip_allowsrcrt == 0) {
                    984:                                type = ICMP_UNREACH;
                    985:                                code = ICMP_UNREACH_NET_PROHIB;
                    986:                                goto bad;
                    987:                        }
1.114     itojun    988:                        if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
                    989:                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
                    990:                                goto bad;
                    991:                        }
1.1       cgd       992:                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
                    993:                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
                    994:                                goto bad;
                    995:                        }
                    996:                        ipaddr.sin_addr = ip->ip_dst;
1.19      mycroft   997:                        ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1.1       cgd       998:                        if (ia == 0) {
                    999:                                if (opt == IPOPT_SSRR) {
                   1000:                                        type = ICMP_UNREACH;
                   1001:                                        code = ICMP_UNREACH_SRCFAIL;
                   1002:                                        goto bad;
                   1003:                                }
                   1004:                                /*
                   1005:                                 * Loose routing, and not at next destination
                   1006:                                 * yet; nothing to do except forward.
                   1007:                                 */
                   1008:                                break;
                   1009:                        }
                   1010:                        off--;                  /* 0 origin */
1.112     sommerfe 1011:                        if ((off + sizeof(struct in_addr)) > optlen) {
1.1       cgd      1012:                                /*
                   1013:                                 * End of source route.  Should be for us.
                   1014:                                 */
                   1015:                                save_rte(cp, ip->ip_src);
                   1016:                                break;
                   1017:                        }
                   1018:                        /*
                   1019:                         * locate outgoing interface
                   1020:                         */
1.281     tsutsui  1021:                        memcpy((void *)&ipaddr.sin_addr, (void *)(cp + off),
1.1       cgd      1022:                            sizeof(ipaddr.sin_addr));
1.96      thorpej  1023:                        if (opt == IPOPT_SSRR)
1.196     itojun   1024:                                ia = ifatoia(ifa_ifwithladdr(sintosa(&ipaddr)));
1.96      thorpej  1025:                        else
1.1       cgd      1026:                                ia = ip_rtaddr(ipaddr.sin_addr);
                   1027:                        if (ia == 0) {
                   1028:                                type = ICMP_UNREACH;
                   1029:                                code = ICMP_UNREACH_SRCFAIL;
                   1030:                                goto bad;
                   1031:                        }
                   1032:                        ip->ip_dst = ipaddr.sin_addr;
1.244     christos 1033:                        bcopy((void *)&ia->ia_addr.sin_addr,
                   1034:                            (void *)(cp + off), sizeof(struct in_addr));
1.1       cgd      1035:                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13      mycroft  1036:                        /*
                   1037:                         * Let ip_intr's mcast routing check handle mcast pkts
                   1038:                         */
1.18      mycroft  1039:                        forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1       cgd      1040:                        break;
                   1041:
                   1042:                case IPOPT_RR:
1.114     itojun   1043:                        if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
                   1044:                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
                   1045:                                goto bad;
                   1046:                        }
1.1       cgd      1047:                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
                   1048:                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
                   1049:                                goto bad;
                   1050:                        }
                   1051:                        /*
                   1052:                         * If no space remains, ignore.
                   1053:                         */
                   1054:                        off--;                  /* 0 origin */
1.112     sommerfe 1055:                        if ((off + sizeof(struct in_addr)) > optlen)
1.1       cgd      1056:                                break;
1.281     tsutsui  1057:                        memcpy((void *)&ipaddr.sin_addr, (void *)(&ip->ip_dst),
1.1       cgd      1058:                            sizeof(ipaddr.sin_addr));
                   1059:                        /*
                   1060:                         * locate outgoing interface; if we're the destination,
                   1061:                         * use the incoming interface (should be same).
                   1062:                         */
1.96      thorpej  1063:                        if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
                   1064:                            == NULL &&
                   1065:                            (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1.1       cgd      1066:                                type = ICMP_UNREACH;
                   1067:                                code = ICMP_UNREACH_HOST;
                   1068:                                goto bad;
                   1069:                        }
1.244     christos 1070:                        bcopy((void *)&ia->ia_addr.sin_addr,
                   1071:                            (void *)(cp + off), sizeof(struct in_addr));
1.1       cgd      1072:                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
                   1073:                        break;
                   1074:
                   1075:                case IPOPT_TS:
                   1076:                        code = cp - (u_char *)ip;
                   1077:                        ipt = (struct ip_timestamp *)cp;
1.114     itojun   1078:                        if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
                   1079:                                code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1.1       cgd      1080:                                goto bad;
1.114     itojun   1081:                        }
                   1082:                        if (ipt->ipt_ptr < 5) {
                   1083:                                code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
                   1084:                                goto bad;
                   1085:                        }
1.15      cgd      1086:                        if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.114     itojun   1087:                                if (++ipt->ipt_oflw == 0) {
                   1088:                                        code = (u_char *)&ipt->ipt_ptr -
                   1089:                                            (u_char *)ip;
1.1       cgd      1090:                                        goto bad;
1.114     itojun   1091:                                }
1.1       cgd      1092:                                break;
                   1093:                        }
1.104     thorpej  1094:                        cp0 = (cp + ipt->ipt_ptr - 1);
1.1       cgd      1095:                        switch (ipt->ipt_flg) {
                   1096:
                   1097:                        case IPOPT_TS_TSONLY:
                   1098:                                break;
                   1099:
                   1100:                        case IPOPT_TS_TSANDADDR:
1.66      thorpej  1101:                                if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.114     itojun   1102:                                    sizeof(struct in_addr) > ipt->ipt_len) {
                   1103:                                        code = (u_char *)&ipt->ipt_ptr -
                   1104:                                            (u_char *)ip;
1.1       cgd      1105:                                        goto bad;
1.114     itojun   1106:                                }
1.13      mycroft  1107:                                ipaddr.sin_addr = dst;
1.96      thorpej  1108:                                ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
                   1109:                                    m->m_pkthdr.rcvif));
1.13      mycroft  1110:                                if (ia == 0)
                   1111:                                        continue;
1.104     thorpej  1112:                                bcopy(&ia->ia_addr.sin_addr,
                   1113:                                    cp0, sizeof(struct in_addr));
1.1       cgd      1114:                                ipt->ipt_ptr += sizeof(struct in_addr);
                   1115:                                break;
                   1116:
                   1117:                        case IPOPT_TS_PRESPEC:
1.66      thorpej  1118:                                if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.114     itojun   1119:                                    sizeof(struct in_addr) > ipt->ipt_len) {
                   1120:                                        code = (u_char *)&ipt->ipt_ptr -
                   1121:                                            (u_char *)ip;
1.1       cgd      1122:                                        goto bad;
1.114     itojun   1123:                                }
1.281     tsutsui  1124:                                memcpy(&ipaddr.sin_addr, cp0,
1.1       cgd      1125:                                    sizeof(struct in_addr));
1.96      thorpej  1126:                                if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
                   1127:                                    == NULL)
1.1       cgd      1128:                                        continue;
                   1129:                                ipt->ipt_ptr += sizeof(struct in_addr);
                   1130:                                break;
                   1131:
                   1132:                        default:
1.114     itojun   1133:                                /* XXX can't take &ipt->ipt_flg */
                   1134:                                code = (u_char *)&ipt->ipt_ptr -
                   1135:                                    (u_char *)ip + 1;
1.1       cgd      1136:                                goto bad;
                   1137:                        }
                   1138:                        ntime = iptime();
1.107     thorpej  1139:                        cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
1.244     christos 1140:                        memmove((char *)cp + ipt->ipt_ptr - 1, cp0,
1.1       cgd      1141:                            sizeof(n_time));
                   1142:                        ipt->ipt_ptr += sizeof(n_time);
                   1143:                }
                   1144:        }
                   1145:        if (forward) {
1.26      thorpej  1146:                if (ip_forwsrcrt == 0) {
                   1147:                        type = ICMP_UNREACH;
                   1148:                        code = ICMP_UNREACH_SRCFAIL;
                   1149:                        goto bad;
                   1150:                }
1.1       cgd      1151:                ip_forward(m, 1);
                   1152:                return (1);
1.13      mycroft  1153:        }
                   1154:        return (0);
1.1       cgd      1155: bad:
1.13      mycroft  1156:        icmp_error(m, type, code, 0, 0);
1.266     thorpej  1157:        IP_STATINC(IP_STAT_BADOPTIONS);
1.1       cgd      1158:        return (1);
                   1159: }
                   1160:
                   1161: /*
                   1162:  * Given address of next destination (final or next hop),
                   1163:  * return internet address info of interface to be used to get there.
                   1164:  */
                   1165: struct in_ifaddr *
1.211     perry    1166: ip_rtaddr(struct in_addr dst)
1.1       cgd      1167: {
1.249     dyoung   1168:        struct rtentry *rt;
                   1169:        union {
                   1170:                struct sockaddr         dst;
                   1171:                struct sockaddr_in      dst4;
                   1172:        } u;
                   1173:
                   1174:        sockaddr_in_init(&u.dst4, &dst, 0);
                   1175:
                   1176:        if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL)
                   1177:                return NULL;
                   1178:
                   1179:        return ifatoia(rt->rt_ifa);
1.1       cgd      1180: }
                   1181:
                   1182: /*
                   1183:  * Save incoming source route for use in replies,
                   1184:  * to be picked up later by ip_srcroute if the receiver is interested.
                   1185:  */
1.13      mycroft  1186: void
1.211     perry    1187: save_rte(u_char *option, struct in_addr dst)
1.1       cgd      1188: {
                   1189:        unsigned olen;
                   1190:
                   1191:        olen = option[IPOPT_OLEN];
                   1192: #ifdef DIAGNOSTIC
                   1193:        if (ipprintfs)
1.39      christos 1194:                printf("save_rte: olen %d\n", olen);
1.89      itojun   1195: #endif /* 0 */
1.1       cgd      1196:        if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
                   1197:                return;
1.281     tsutsui  1198:        memcpy((void *)ip_srcrt.srcopt, (void *)option, olen);
1.1       cgd      1199:        ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
                   1200:        ip_srcrt.dst = dst;
                   1201: }
                   1202:
                   1203: /*
                   1204:  * Retrieve incoming source route for use in replies,
                   1205:  * in the same form used by setsockopt.
                   1206:  * The first hop is placed before the options, will be removed later.
                   1207:  */
                   1208: struct mbuf *
1.211     perry    1209: ip_srcroute(void)
1.1       cgd      1210: {
1.109     augustss 1211:        struct in_addr *p, *q;
                   1212:        struct mbuf *m;
1.1       cgd      1213:
                   1214:        if (ip_nhops == 0)
1.237     dyoung   1215:                return NULL;
1.1       cgd      1216:        m = m_get(M_DONTWAIT, MT_SOOPTS);
                   1217:        if (m == 0)
1.237     dyoung   1218:                return NULL;
1.1       cgd      1219:
1.164     matt     1220:        MCLAIM(m, &inetdomain.dom_mowner);
1.13      mycroft  1221: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1       cgd      1222:
                   1223:        /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
                   1224:        m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
                   1225:            OPTSIZ;
                   1226: #ifdef DIAGNOSTIC
                   1227:        if (ipprintfs)
1.39      christos 1228:                printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1.1       cgd      1229: #endif
                   1230:
                   1231:        /*
                   1232:         * First save first hop for return route
                   1233:         */
                   1234:        p = &ip_srcrt.route[ip_nhops - 1];
                   1235:        *(mtod(m, struct in_addr *)) = *p--;
                   1236: #ifdef DIAGNOSTIC
                   1237:        if (ipprintfs)
1.39      christos 1238:                printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1.1       cgd      1239: #endif
                   1240:
                   1241:        /*
                   1242:         * Copy option fields and padding (nop) to mbuf.
                   1243:         */
                   1244:        ip_srcrt.nop = IPOPT_NOP;
                   1245:        ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1.244     christos 1246:        memmove(mtod(m, char *) + sizeof(struct in_addr), &ip_srcrt.nop,
                   1247:            OPTSIZ);
                   1248:        q = (struct in_addr *)(mtod(m, char *) +
1.1       cgd      1249:            sizeof(struct in_addr) + OPTSIZ);
                   1250: #undef OPTSIZ
                   1251:        /*
                   1252:         * Record return path as an IP source route,
                   1253:         * reversing the path (pointers are now aligned).
                   1254:         */
                   1255:        while (p >= ip_srcrt.route) {
                   1256: #ifdef DIAGNOSTIC
                   1257:                if (ipprintfs)
1.39      christos 1258:                        printf(" %x", ntohl(q->s_addr));
1.1       cgd      1259: #endif
                   1260:                *q++ = *p--;
                   1261:        }
                   1262:        /*
                   1263:         * Last hop goes to final destination.
                   1264:         */
                   1265:        *q = ip_srcrt.dst;
                   1266: #ifdef DIAGNOSTIC
                   1267:        if (ipprintfs)
1.39      christos 1268:                printf(" %x\n", ntohl(q->s_addr));
1.1       cgd      1269: #endif
                   1270:        return (m);
                   1271: }
                   1272:
1.139     matt     1273: const int inetctlerrmap[PRC_NCMDS] = {
1.256     yamt     1274:        [PRC_MSGSIZE] = EMSGSIZE,
                   1275:        [PRC_HOSTDEAD] = EHOSTDOWN,
                   1276:        [PRC_HOSTUNREACH] = EHOSTUNREACH,
                   1277:        [PRC_UNREACH_NET] = EHOSTUNREACH,
                   1278:        [PRC_UNREACH_HOST] = EHOSTUNREACH,
                   1279:        [PRC_UNREACH_PROTOCOL] = ECONNREFUSED,
                   1280:        [PRC_UNREACH_PORT] = ECONNREFUSED,
                   1281:        [PRC_UNREACH_SRCFAIL] = EHOSTUNREACH,
                   1282:        [PRC_PARAMPROB] = ENOPROTOOPT,
1.1       cgd      1283: };
                   1284:
                   1285: /*
                   1286:  * Forward a packet.  If some error occurs return the sender
                   1287:  * an icmp packet.  Note we can't always generate a meaningful
                   1288:  * icmp message because icmp doesn't have a large enough repertoire
                   1289:  * of codes and types.
                   1290:  *
                   1291:  * If not forwarding, just drop the packet.  This could be confusing
                   1292:  * if ipforwarding was zero but some routing protocol was advancing
                   1293:  * us as a gateway to somewhere.  However, we must let the routing
                   1294:  * protocol deal with that.
                   1295:  *
                   1296:  * The srcrt parameter indicates whether the packet is being forwarded
                   1297:  * via a source route.
                   1298:  */
1.13      mycroft  1299: void
1.211     perry    1300: ip_forward(struct mbuf *m, int srcrt)
1.1       cgd      1301: {
1.109     augustss 1302:        struct ip *ip = mtod(m, struct ip *);
                   1303:        struct rtentry *rt;
1.220     christos 1304:        int error, type = 0, code = 0, destmtu = 0;
1.1       cgd      1305:        struct mbuf *mcopy;
1.13      mycroft  1306:        n_long dest;
1.249     dyoung   1307:        union {
                   1308:                struct sockaddr         dst;
                   1309:                struct sockaddr_in      dst4;
                   1310:        } u;
1.164     matt     1311:
                   1312:        /*
                   1313:         * We are now in the output path.
                   1314:         */
                   1315:        MCLAIM(m, &ip_tx_mowner);
1.135     thorpej  1316:
                   1317:        /*
                   1318:         * Clear any in-bound checksum flags for this packet.
                   1319:         */
                   1320:        m->m_pkthdr.csum_flags = 0;
1.1       cgd      1321:
1.13      mycroft  1322:        dest = 0;
1.1       cgd      1323: #ifdef DIAGNOSTIC
1.224     joerg    1324:        if (ipprintfs) {
                   1325:                printf("forward: src %s ", inet_ntoa(ip->ip_src));
                   1326:                printf("dst %s ttl %x\n", inet_ntoa(ip->ip_dst), ip->ip_ttl);
                   1327:        }
1.1       cgd      1328: #endif
1.93      sommerfe 1329:        if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1.266     thorpej  1330:                IP_STATINC(IP_STAT_CANTFORWARD);
1.1       cgd      1331:                m_freem(m);
                   1332:                return;
                   1333:        }
                   1334:        if (ip->ip_ttl <= IPTTLDEC) {
1.13      mycroft  1335:                icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1       cgd      1336:                return;
                   1337:        }
                   1338:
1.249     dyoung   1339:        sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
                   1340:        if ((rt = rtcache_lookup(&ipforward_rt, &u.dst)) == NULL) {
                   1341:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, dest, 0);
                   1342:                return;
1.1       cgd      1343:        }
                   1344:
                   1345:        /*
1.34      mycroft  1346:         * Save at most 68 bytes of the packet in case
1.1       cgd      1347:         * we need to generate an ICMP message to the src.
1.119     itojun   1348:         * Pullup to avoid sharing mbuf cluster between m and mcopy.
1.1       cgd      1349:         */
1.155     itojun   1350:        mcopy = m_copym(m, 0, imin(ntohs(ip->ip_len), 68), M_DONTWAIT);
1.119     itojun   1351:        if (mcopy)
                   1352:                mcopy = m_pullup(mcopy, ip->ip_hl << 2);
1.1       cgd      1353:
1.221     christos 1354:        ip->ip_ttl -= IPTTLDEC;
                   1355:
1.1       cgd      1356:        /*
                   1357:         * If forwarding packet using same interface that it came in on,
                   1358:         * perhaps should send a redirect to sender to shortcut a hop.
                   1359:         * Only send redirect if source is sending directly to us,
                   1360:         * and if packet was not source routed (or has any options).
                   1361:         * Also, don't send redirect if forwarding using a default route
                   1362:         * or a route modified by a redirect.
                   1363:         */
                   1364:        if (rt->rt_ifp == m->m_pkthdr.rcvif &&
                   1365:            (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1.250     dyoung   1366:            !in_nullhost(satocsin(rt_getkey(rt))->sin_addr) &&
1.1       cgd      1367:            ipsendredirects && !srcrt) {
1.19      mycroft  1368:                if (rt->rt_ifa &&
                   1369:                    (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
                   1370:                    ifatoia(rt->rt_ifa)->ia_subnet) {
1.77      thorpej  1371:                        if (rt->rt_flags & RTF_GATEWAY)
                   1372:                                dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
                   1373:                        else
                   1374:                                dest = ip->ip_dst.s_addr;
                   1375:                        /*
                   1376:                         * Router requirements says to only send host
                   1377:                         * redirects.
                   1378:                         */
                   1379:                        type = ICMP_REDIRECT;
                   1380:                        code = ICMP_REDIRECT_HOST;
1.1       cgd      1381: #ifdef DIAGNOSTIC
1.77      thorpej  1382:                        if (ipprintfs)
                   1383:                                printf("redirect (%d) to %x\n", code,
                   1384:                                    (u_int32_t)dest);
1.1       cgd      1385: #endif
                   1386:                }
                   1387:        }
                   1388:
1.238     dyoung   1389:        error = ip_output(m, NULL, &ipforward_rt,
1.173     jonathan 1390:            (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
1.174     itojun   1391:            (struct ip_moptions *)NULL, (struct socket *)NULL);
1.173     jonathan 1392:
1.1       cgd      1393:        if (error)
1.266     thorpej  1394:                IP_STATINC(IP_STAT_CANTFORWARD);
1.1       cgd      1395:        else {
1.266     thorpej  1396:                uint64_t *ips = IP_STAT_GETREF();
                   1397:                ips[IP_STAT_FORWARD]++;
                   1398:                if (type) {
                   1399:                        ips[IP_STAT_REDIRECTSENT]++;
                   1400:                        IP_STAT_PUTREF();
                   1401:                } else {
                   1402:                        IP_STAT_PUTREF();
1.63      matt     1403:                        if (mcopy) {
                   1404: #ifdef GATEWAY
1.64      thorpej  1405:                                if (mcopy->m_flags & M_CANFASTFWD)
                   1406:                                        ipflow_create(&ipforward_rt, mcopy);
1.63      matt     1407: #endif
1.1       cgd      1408:                                m_freem(mcopy);
1.63      matt     1409:                        }
1.1       cgd      1410:                        return;
                   1411:                }
                   1412:        }
                   1413:        if (mcopy == NULL)
                   1414:                return;
1.13      mycroft  1415:
1.1       cgd      1416:        switch (error) {
                   1417:
                   1418:        case 0:                         /* forwarded, but need redirect */
                   1419:                /* type, code set above */
                   1420:                break;
                   1421:
                   1422:        case ENETUNREACH:               /* shouldn't happen, checked above */
                   1423:        case EHOSTUNREACH:
                   1424:        case ENETDOWN:
                   1425:        case EHOSTDOWN:
                   1426:        default:
                   1427:                type = ICMP_UNREACH;
                   1428:                code = ICMP_UNREACH_HOST;
                   1429:                break;
                   1430:
                   1431:        case EMSGSIZE:
                   1432:                type = ICMP_UNREACH;
                   1433:                code = ICMP_UNREACH_NEEDFRAG;
1.263     cube     1434:
1.274     seanb    1435:                if ((rt = rtcache_validate(&ipforward_rt)) != NULL)
                   1436:                        destmtu = rt->rt_ifp->if_mtu;
1.263     cube     1437:
                   1438: #if defined(IPSEC) || defined(FAST_IPSEC)
1.274     seanb    1439:                {
1.263     cube     1440:                        /*
                   1441:                         * If the packet is routed over IPsec tunnel, tell the
                   1442:                         * originator the tunnel MTU.
                   1443:                         *      tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
                   1444:                         * XXX quickhack!!!
                   1445:                         */
                   1446:
1.89      itojun   1447:                        struct secpolicy *sp;
                   1448:                        int ipsecerror;
1.95      itojun   1449:                        size_t ipsechdr;
1.89      itojun   1450:                        struct route *ro;
                   1451:
                   1452:                        sp = ipsec4_getpolicybyaddr(mcopy,
1.170     itojun   1453:                            IPSEC_DIR_OUTBOUND, IP_FORWARDING,
                   1454:                            &ipsecerror);
1.89      itojun   1455:
1.263     cube     1456:                        if (sp != NULL) {
1.89      itojun   1457:                                /* count IPsec header size */
1.95      itojun   1458:                                ipsechdr = ipsec4_hdrsiz(mcopy,
1.170     itojun   1459:                                    IPSEC_DIR_OUTBOUND, NULL);
1.89      itojun   1460:
                   1461:                                /*
                   1462:                                 * find the correct route for outer IPv4
                   1463:                                 * header, compute tunnel MTU.
                   1464:                                 */
1.220     christos 1465:
1.89      itojun   1466:                                if (sp->req != NULL
1.95      itojun   1467:                                 && sp->req->sav != NULL
                   1468:                                 && sp->req->sav->sah != NULL) {
                   1469:                                        ro = &sp->req->sav->sah->sa_route;
1.274     seanb    1470:                                        rt = rtcache_validate(ro);
1.257     dyoung   1471:                                        if (rt && rt->rt_ifp) {
1.220     christos 1472:                                                destmtu =
1.257     dyoung   1473:                                                    rt->rt_rmx.rmx_mtu ?
                   1474:                                                    rt->rt_rmx.rmx_mtu :
                   1475:                                                    rt->rt_ifp->if_mtu;
1.220     christos 1476:                                                destmtu -= ipsechdr;
1.89      itojun   1477:                                        }
                   1478:                                }
                   1479:
1.173     jonathan 1480: #ifdef IPSEC
1.89      itojun   1481:                                key_freesp(sp);
1.173     jonathan 1482: #else
                   1483:                                KEY_FREESP(&sp);
                   1484: #endif
1.89      itojun   1485:                        }
1.274     seanb    1486:                }
1.263     cube     1487: #endif /*defined(IPSEC) || defined(FAST_IPSEC)*/
1.266     thorpej  1488:                IP_STATINC(IP_STAT_CANTFRAG);
1.1       cgd      1489:                break;
                   1490:
                   1491:        case ENOBUFS:
1.143     itojun   1492: #if 1
                   1493:                /*
                   1494:                 * a router should not generate ICMP_SOURCEQUENCH as
                   1495:                 * required in RFC1812 Requirements for IP Version 4 Routers.
                   1496:                 * source quench could be a big problem under DoS attacks,
1.149     wiz      1497:                 * or if the underlying interface is rate-limited.
1.143     itojun   1498:                 */
                   1499:                if (mcopy)
                   1500:                        m_freem(mcopy);
                   1501:                return;
                   1502: #else
1.1       cgd      1503:                type = ICMP_SOURCEQUENCH;
                   1504:                code = 0;
                   1505:                break;
1.143     itojun   1506: #endif
1.1       cgd      1507:        }
1.220     christos 1508:        icmp_error(mcopy, type, code, dest, destmtu);
1.44      thorpej  1509: }
                   1510:
                   1511: void
1.211     perry    1512: ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
                   1513:     struct mbuf *m)
1.44      thorpej  1514: {
                   1515:
1.283     minskim  1516:        if (inp->inp_socket->so_options & SO_TIMESTAMP
1.278     christos 1517: #ifdef SO_OTIMESTAMP
1.283     minskim  1518:            || inp->inp_socket->so_options & SO_OTIMESTAMP
1.278     christos 1519: #endif
                   1520:            ) {
1.44      thorpej  1521:                struct timeval tv;
                   1522:
                   1523:                microtime(&tv);
1.278     christos 1524: #ifdef SO_OTIMESTAMP
                   1525:                if (inp->inp_socket->so_options & SO_OTIMESTAMP) {
                   1526:                        struct timeval50 tv50;
                   1527:                        timeval_to_timeval50(&tv, &tv50);
                   1528:                        *mp = sbcreatecontrol((void *) &tv50, sizeof(tv50),
                   1529:                            SCM_OTIMESTAMP, SOL_SOCKET);
                   1530:                } else
                   1531: #endif
1.244     christos 1532:                *mp = sbcreatecontrol((void *) &tv, sizeof(tv),
1.44      thorpej  1533:                    SCM_TIMESTAMP, SOL_SOCKET);
                   1534:                if (*mp)
                   1535:                        mp = &(*mp)->m_next;
                   1536:        }
                   1537:        if (inp->inp_flags & INP_RECVDSTADDR) {
1.244     christos 1538:                *mp = sbcreatecontrol((void *) &ip->ip_dst,
1.44      thorpej  1539:                    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
                   1540:                if (*mp)
                   1541:                        mp = &(*mp)->m_next;
                   1542:        }
                   1543: #ifdef notyet
                   1544:        /*
                   1545:         * XXX
                   1546:         * Moving these out of udp_input() made them even more broken
                   1547:         * than they already were.
                   1548:         *      - fenner@parc.xerox.com
                   1549:         */
                   1550:        /* options were tossed already */
                   1551:        if (inp->inp_flags & INP_RECVOPTS) {
1.244     christos 1552:                *mp = sbcreatecontrol((void *) opts_deleted_above,
1.44      thorpej  1553:                    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
                   1554:                if (*mp)
                   1555:                        mp = &(*mp)->m_next;
                   1556:        }
                   1557:        /* ip_srcroute doesn't do what we want here, need to fix */
                   1558:        if (inp->inp_flags & INP_RECVRETOPTS) {
1.244     christos 1559:                *mp = sbcreatecontrol((void *) ip_srcroute(),
1.44      thorpej  1560:                    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
                   1561:                if (*mp)
                   1562:                        mp = &(*mp)->m_next;
                   1563:        }
                   1564: #endif
                   1565:        if (inp->inp_flags & INP_RECVIF) {
                   1566:                struct sockaddr_dl sdl;
                   1567:
1.252     dyoung   1568:                sockaddr_dl_init(&sdl, sizeof(sdl),
                   1569:                    (m->m_pkthdr.rcvif != NULL)
                   1570:                        ?  m->m_pkthdr.rcvif->if_index
                   1571:                        : 0,
                   1572:                        0, NULL, 0, NULL, 0);
1.251     dyoung   1573:                *mp = sbcreatecontrol(&sdl, sdl.sdl_len, IP_RECVIF, IPPROTO_IP);
1.44      thorpej  1574:                if (*mp)
                   1575:                        mp = &(*mp)->m_next;
                   1576:        }
1.282     minskim  1577:        if (inp->inp_flags & INP_RECVTTL) {
                   1578:                *mp = sbcreatecontrol((void *) &ip->ip_ttl,
                   1579:                    sizeof(uint8_t), IP_RECVTTL, IPPROTO_IP);
                   1580:                if (*mp)
                   1581:                        mp = &(*mp)->m_next;
                   1582:        }
1.13      mycroft  1583: }
                   1584:
1.189     atatat   1585: /*
1.228     elad     1586:  * sysctl helper routine for net.inet.ip.forwsrcrt.
                   1587:  */
                   1588: static int
                   1589: sysctl_net_inet_ip_forwsrcrt(SYSCTLFN_ARGS)
                   1590: {
                   1591:        int error, tmp;
                   1592:        struct sysctlnode node;
                   1593:
                   1594:        node = *rnode;
                   1595:        tmp = ip_forwsrcrt;
                   1596:        node.sysctl_data = &tmp;
                   1597:        error = sysctl_lookup(SYSCTLFN_CALL(&node));
                   1598:        if (error || newp == NULL)
                   1599:                return (error);
                   1600:
1.280     elad     1601:        error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_FORWSRCRT,
                   1602:            0, NULL, NULL, NULL);
                   1603:        if (error)
                   1604:                return (error);
1.228     elad     1605:
                   1606:        ip_forwsrcrt = tmp;
                   1607:
                   1608:        return (0);
                   1609: }
                   1610:
                   1611: /*
1.189     atatat   1612:  * sysctl helper routine for net.inet.ip.mtudisctimeout.  checks the
                   1613:  * range of the new value and tweaks timers if it changes.
                   1614:  */
                   1615: static int
                   1616: sysctl_net_inet_ip_pmtudto(SYSCTLFN_ARGS)
1.13      mycroft  1617: {
1.189     atatat   1618:        int error, tmp;
                   1619:        struct sysctlnode node;
                   1620:
                   1621:        node = *rnode;
                   1622:        tmp = ip_mtudisc_timeout;
                   1623:        node.sysctl_data = &tmp;
                   1624:        error = sysctl_lookup(SYSCTLFN_CALL(&node));
                   1625:        if (error || newp == NULL)
                   1626:                return (error);
                   1627:        if (tmp < 0)
                   1628:                return (EINVAL);
1.52      thorpej  1629:
1.273     matt     1630:        mutex_enter(softnet_lock);
                   1631:
1.189     atatat   1632:        ip_mtudisc_timeout = tmp;
                   1633:        rt_timer_queue_change(ip_mtudisc_timeout_q, ip_mtudisc_timeout);
                   1634:
1.273     matt     1635:        mutex_exit(softnet_lock);
                   1636:
1.189     atatat   1637:        return (0);
                   1638: }
1.54      lukem    1639:
1.65      matt     1640: #ifdef GATEWAY
1.189     atatat   1641: /*
1.247     liamjfoy 1642:  * sysctl helper routine for net.inet.ip.maxflows.
1.189     atatat   1643:  */
                   1644: static int
                   1645: sysctl_net_inet_ip_maxflows(SYSCTLFN_ARGS)
                   1646: {
1.273     matt     1647:        int error;
1.67      thorpej  1648:
1.273     matt     1649:        error = sysctl_lookup(SYSCTLFN_CALL(rnode));
                   1650:        if (error || newp == NULL)
                   1651:                return (error);
                   1652:
                   1653:        mutex_enter(softnet_lock);
                   1654:        KERNEL_LOCK(1, NULL);
1.212     perry    1655:
1.265     thorpej  1656:        ipflow_prune();
1.273     matt     1657:
                   1658:        KERNEL_UNLOCK_ONE(NULL);
                   1659:        mutex_exit(softnet_lock);
1.144     martin   1660:
1.189     atatat   1661:        return (0);
                   1662: }
1.248     liamjfoy 1663:
                   1664: static int
                   1665: sysctl_net_inet_ip_hashsize(SYSCTLFN_ARGS)
1.283     minskim  1666: {
1.248     liamjfoy 1667:        int error, tmp;
                   1668:        struct sysctlnode node;
                   1669:
                   1670:        node = *rnode;
                   1671:        tmp = ip_hashsize;
                   1672:        node.sysctl_data = &tmp;
                   1673:        error = sysctl_lookup(SYSCTLFN_CALL(&node));
                   1674:        if (error || newp == NULL)
                   1675:                return (error);
                   1676:
                   1677:        if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
                   1678:                /*
                   1679:                 * Can only fail due to malloc()
                   1680:                 */
1.273     matt     1681:                mutex_enter(softnet_lock);
                   1682:                KERNEL_LOCK(1, NULL);
                   1683:
                   1684:                error = ipflow_invalidate_all(tmp);
                   1685:
                   1686:                KERNEL_UNLOCK_ONE(NULL);
                   1687:                mutex_exit(softnet_lock);
                   1688:
1.248     liamjfoy 1689:        } else {
                   1690:                /*
                   1691:                 * EINVAL if not a power of 2
                   1692:                 */
1.273     matt     1693:                error = EINVAL;
1.283     minskim  1694:        }
1.248     liamjfoy 1695:
1.273     matt     1696:        return error;
1.248     liamjfoy 1697: }
1.189     atatat   1698: #endif /* GATEWAY */
1.117     tron     1699:
1.266     thorpej  1700: static int
                   1701: sysctl_net_inet_ip_stats(SYSCTLFN_ARGS)
                   1702: {
                   1703:
1.271     thorpej  1704:        return (NETSTAT_SYSCTL(ipstat_percpu, IP_NSTATS));
1.266     thorpej  1705: }
1.131     itojun   1706:
1.284     pooka    1707: static void
                   1708: sysctl_net_inet_ip_setup(struct sysctllog **clog)
1.189     atatat   1709: {
                   1710:        extern int subnetsarelocal, hostzeroisbroadcast;
1.180     jonathan 1711:
1.197     atatat   1712:        sysctl_createv(clog, 0, NULL, NULL,
                   1713:                       CTLFLAG_PERMANENT,
1.189     atatat   1714:                       CTLTYPE_NODE, "net", NULL,
                   1715:                       NULL, 0, NULL, 0,
                   1716:                       CTL_NET, CTL_EOL);
1.197     atatat   1717:        sysctl_createv(clog, 0, NULL, NULL,
                   1718:                       CTLFLAG_PERMANENT,
1.203     atatat   1719:                       CTLTYPE_NODE, "inet",
                   1720:                       SYSCTL_DESCR("PF_INET related settings"),
1.189     atatat   1721:                       NULL, 0, NULL, 0,
                   1722:                       CTL_NET, PF_INET, CTL_EOL);
1.197     atatat   1723:        sysctl_createv(clog, 0, NULL, NULL,
                   1724:                       CTLFLAG_PERMANENT,
1.203     atatat   1725:                       CTLTYPE_NODE, "ip",
                   1726:                       SYSCTL_DESCR("IPv4 related settings"),
1.189     atatat   1727:                       NULL, 0, NULL, 0,
                   1728:                       CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
1.212     perry    1729:
1.197     atatat   1730:        sysctl_createv(clog, 0, NULL, NULL,
                   1731:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1732:                       CTLTYPE_INT, "forwarding",
                   1733:                       SYSCTL_DESCR("Enable forwarding of INET datagrams"),
1.189     atatat   1734:                       NULL, 0, &ipforwarding, 0,
                   1735:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1736:                       IPCTL_FORWARDING, CTL_EOL);
1.197     atatat   1737:        sysctl_createv(clog, 0, NULL, NULL,
                   1738:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1739:                       CTLTYPE_INT, "redirect",
                   1740:                       SYSCTL_DESCR("Enable sending of ICMP redirect messages"),
1.189     atatat   1741:                       NULL, 0, &ipsendredirects, 0,
                   1742:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1743:                       IPCTL_SENDREDIRECTS, CTL_EOL);
1.197     atatat   1744:        sysctl_createv(clog, 0, NULL, NULL,
                   1745:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1746:                       CTLTYPE_INT, "ttl",
                   1747:                       SYSCTL_DESCR("Default TTL for an INET datagram"),
1.189     atatat   1748:                       NULL, 0, &ip_defttl, 0,
                   1749:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1750:                       IPCTL_DEFTTL, CTL_EOL);
                   1751: #ifdef IPCTL_DEFMTU
1.197     atatat   1752:        sysctl_createv(clog, 0, NULL, NULL,
                   1753:                       CTLFLAG_PERMANENT /* |CTLFLAG_READWRITE? */,
1.203     atatat   1754:                       CTLTYPE_INT, "mtu",
                   1755:                       SYSCTL_DESCR("Default MTA for an INET route"),
1.189     atatat   1756:                       NULL, 0, &ip_mtu, 0,
                   1757:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1758:                       IPCTL_DEFMTU, CTL_EOL);
                   1759: #endif /* IPCTL_DEFMTU */
1.197     atatat   1760:        sysctl_createv(clog, 0, NULL, NULL,
1.228     elad     1761:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1762:                       CTLTYPE_INT, "forwsrcrt",
                   1763:                       SYSCTL_DESCR("Enable forwarding of source-routed "
                   1764:                                    "datagrams"),
1.228     elad     1765:                       sysctl_net_inet_ip_forwsrcrt, 0, &ip_forwsrcrt, 0,
1.189     atatat   1766:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1767:                       IPCTL_FORWSRCRT, CTL_EOL);
1.197     atatat   1768:        sysctl_createv(clog, 0, NULL, NULL,
                   1769:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1770:                       CTLTYPE_INT, "directed-broadcast",
                   1771:                       SYSCTL_DESCR("Enable forwarding of broadcast datagrams"),
1.189     atatat   1772:                       NULL, 0, &ip_directedbcast, 0,
                   1773:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1774:                       IPCTL_DIRECTEDBCAST, CTL_EOL);
1.197     atatat   1775:        sysctl_createv(clog, 0, NULL, NULL,
                   1776:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1777:                       CTLTYPE_INT, "allowsrcrt",
                   1778:                       SYSCTL_DESCR("Accept source-routed datagrams"),
1.189     atatat   1779:                       NULL, 0, &ip_allowsrcrt, 0,
                   1780:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1781:                       IPCTL_ALLOWSRCRT, CTL_EOL);
1.197     atatat   1782:        sysctl_createv(clog, 0, NULL, NULL,
                   1783:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1784:                       CTLTYPE_INT, "subnetsarelocal",
                   1785:                       SYSCTL_DESCR("Whether logical subnets are considered "
                   1786:                                    "local"),
1.189     atatat   1787:                       NULL, 0, &subnetsarelocal, 0,
                   1788:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1789:                       IPCTL_SUBNETSARELOCAL, CTL_EOL);
1.197     atatat   1790:        sysctl_createv(clog, 0, NULL, NULL,
                   1791:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1792:                       CTLTYPE_INT, "mtudisc",
                   1793:                       SYSCTL_DESCR("Use RFC1191 Path MTU Discovery"),
1.189     atatat   1794:                       NULL, 0, &ip_mtudisc, 0,
                   1795:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1796:                       IPCTL_MTUDISC, CTL_EOL);
1.197     atatat   1797:        sysctl_createv(clog, 0, NULL, NULL,
                   1798:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1799:                       CTLTYPE_INT, "anonportmin",
                   1800:                       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
1.189     atatat   1801:                       sysctl_net_inet_ip_ports, 0, &anonportmin, 0,
                   1802:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1803:                       IPCTL_ANONPORTMIN, CTL_EOL);
1.197     atatat   1804:        sysctl_createv(clog, 0, NULL, NULL,
                   1805:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1806:                       CTLTYPE_INT, "anonportmax",
                   1807:                       SYSCTL_DESCR("Highest ephemeral port number to assign"),
1.189     atatat   1808:                       sysctl_net_inet_ip_ports, 0, &anonportmax, 0,
                   1809:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1810:                       IPCTL_ANONPORTMAX, CTL_EOL);
1.197     atatat   1811:        sysctl_createv(clog, 0, NULL, NULL,
                   1812:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1813:                       CTLTYPE_INT, "mtudisctimeout",
                   1814:                       SYSCTL_DESCR("Lifetime of a Path MTU Discovered route"),
1.189     atatat   1815:                       sysctl_net_inet_ip_pmtudto, 0, &ip_mtudisc_timeout, 0,
                   1816:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1817:                       IPCTL_MTUDISCTIMEOUT, CTL_EOL);
                   1818: #ifdef GATEWAY
1.197     atatat   1819:        sysctl_createv(clog, 0, NULL, NULL,
                   1820:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1821:                       CTLTYPE_INT, "maxflows",
                   1822:                       SYSCTL_DESCR("Number of flows for fast forwarding"),
1.189     atatat   1823:                       sysctl_net_inet_ip_maxflows, 0, &ip_maxflows, 0,
                   1824:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1825:                       IPCTL_MAXFLOWS, CTL_EOL);
1.248     liamjfoy 1826:        sysctl_createv(clog, 0, NULL, NULL,
                   1827:                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1828:                        CTLTYPE_INT, "hashsize",
                   1829:                        SYSCTL_DESCR("Size of hash table for fast forwarding (IPv4)"),
                   1830:                        sysctl_net_inet_ip_hashsize, 0, &ip_hashsize, 0,
                   1831:                        CTL_NET, PF_INET, IPPROTO_IP,
                   1832:                        CTL_CREATE, CTL_EOL);
1.189     atatat   1833: #endif /* GATEWAY */
1.197     atatat   1834:        sysctl_createv(clog, 0, NULL, NULL,
                   1835:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1836:                       CTLTYPE_INT, "hostzerobroadcast",
                   1837:                       SYSCTL_DESCR("All zeroes address is broadcast address"),
1.189     atatat   1838:                       NULL, 0, &hostzeroisbroadcast, 0,
                   1839:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1840:                       IPCTL_HOSTZEROBROADCAST, CTL_EOL);
                   1841: #if NGIF > 0
1.197     atatat   1842:        sysctl_createv(clog, 0, NULL, NULL,
                   1843:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1844:                       CTLTYPE_INT, "gifttl",
                   1845:                       SYSCTL_DESCR("Default TTL for a gif tunnel datagram"),
1.189     atatat   1846:                       NULL, 0, &ip_gif_ttl, 0,
                   1847:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1848:                       IPCTL_GIF_TTL, CTL_EOL);
                   1849: #endif /* NGIF */
                   1850: #ifndef IPNOPRIVPORTS
1.197     atatat   1851:        sysctl_createv(clog, 0, NULL, NULL,
                   1852:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1853:                       CTLTYPE_INT, "lowportmin",
                   1854:                       SYSCTL_DESCR("Lowest privileged ephemeral port number "
                   1855:                                    "to assign"),
1.189     atatat   1856:                       sysctl_net_inet_ip_ports, 0, &lowportmin, 0,
                   1857:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1858:                       IPCTL_LOWPORTMIN, CTL_EOL);
1.197     atatat   1859:        sysctl_createv(clog, 0, NULL, NULL,
                   1860:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1861:                       CTLTYPE_INT, "lowportmax",
                   1862:                       SYSCTL_DESCR("Highest privileged ephemeral port number "
                   1863:                                    "to assign"),
1.189     atatat   1864:                       sysctl_net_inet_ip_ports, 0, &lowportmax, 0,
                   1865:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1866:                       IPCTL_LOWPORTMAX, CTL_EOL);
                   1867: #endif /* IPNOPRIVPORTS */
                   1868: #if NGRE > 0
1.197     atatat   1869:        sysctl_createv(clog, 0, NULL, NULL,
                   1870:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1871:                       CTLTYPE_INT, "grettl",
                   1872:                       SYSCTL_DESCR("Default TTL for a gre tunnel datagram"),
1.189     atatat   1873:                       NULL, 0, &ip_gre_ttl, 0,
                   1874:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1875:                       IPCTL_GRE_TTL, CTL_EOL);
                   1876: #endif /* NGRE */
1.197     atatat   1877:        sysctl_createv(clog, 0, NULL, NULL,
                   1878:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1879:                       CTLTYPE_INT, "checkinterface",
                   1880:                       SYSCTL_DESCR("Enable receive side of Strong ES model "
                   1881:                                    "from RFC1122"),
1.189     atatat   1882:                       NULL, 0, &ip_checkinterface, 0,
                   1883:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1884:                       IPCTL_CHECKINTERFACE, CTL_EOL);
1.197     atatat   1885:        sysctl_createv(clog, 0, NULL, NULL,
                   1886:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.203     atatat   1887:                       CTLTYPE_INT, "random_id",
                   1888:                       SYSCTL_DESCR("Assign random ip_id values"),
1.189     atatat   1889:                       NULL, 0, &ip_do_randomid, 0,
                   1890:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1891:                       IPCTL_RANDOMID, CTL_EOL);
1.206     thorpej  1892:        sysctl_createv(clog, 0, NULL, NULL,
                   1893:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   1894:                       CTLTYPE_INT, "do_loopback_cksum",
                   1895:                       SYSCTL_DESCR("Perform IP checksum on loopback"),
                   1896:                       NULL, 0, &ip_do_loopback_cksum, 0,
                   1897:                       CTL_NET, PF_INET, IPPROTO_IP,
                   1898:                       IPCTL_LOOPBACKCKSUM, CTL_EOL);
1.219     elad     1899:        sysctl_createv(clog, 0, NULL, NULL,
                   1900:                       CTLFLAG_PERMANENT,
                   1901:                       CTLTYPE_STRUCT, "stats",
                   1902:                       SYSCTL_DESCR("IP statistics"),
1.266     thorpej  1903:                       sysctl_net_inet_ip_stats, 0, NULL, 0,
1.219     elad     1904:                       CTL_NET, PF_INET, IPPROTO_IP, IPCTL_STATS,
                   1905:                       CTL_EOL);
1.1       cgd      1906: }
1.266     thorpej  1907:
                   1908: void
                   1909: ip_statinc(u_int stat)
                   1910: {
                   1911:
                   1912:        KASSERT(stat < IP_NSTATS);
                   1913:        IP_STATINC(stat);
                   1914: }

CVSweb <webmaster@jp.NetBSD.org>