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

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

CVSweb <webmaster@jp.NetBSD.org>