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

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

CVSweb <webmaster@jp.NetBSD.org>