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

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

CVSweb <webmaster@jp.NetBSD.org>