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

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

CVSweb <webmaster@jp.NetBSD.org>