[BACK]Return to ip_input.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet

Annotation of src/sys/netinet/ip_input.c, Revision 1.319

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

CVSweb <webmaster@jp.NetBSD.org>