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

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

CVSweb <webmaster@jp.NetBSD.org>