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

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

CVSweb <webmaster@jp.NetBSD.org>