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

1.131   ! itojun      1: /*     $NetBSD: ip_input.c,v 1.130 2001/03/02 04:26:10 itojun Exp $    */
1.89      itojun      2:
                      3: /*
                      4:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      5:  * All rights reserved.
                      6:  *
                      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.
                     18:  *
                     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:  * 3. All advertising materials mentioning features or use of this software
                     49:  *    must display the following acknowledgement:
                     50:  *     This product includes software developed by the NetBSD
                     51:  *     Foundation, Inc. and its contributors.
                     52:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     53:  *    contributors may be used to endorse or promote products derived
                     54:  *    from this software without specific prior written permission.
                     55:  *
                     56:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     57:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     58:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     59:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     60:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     61:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     62:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     63:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     64:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     65:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     66:  * POSSIBILITY OF SUCH DAMAGE.
                     67:  */
1.14      cgd        68:
1.1       cgd        69: /*
1.13      mycroft    70:  * Copyright (c) 1982, 1986, 1988, 1993
                     71:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        72:  *
                     73:  * Redistribution and use in source and binary forms, with or without
                     74:  * modification, are permitted provided that the following conditions
                     75:  * are met:
                     76:  * 1. Redistributions of source code must retain the above copyright
                     77:  *    notice, this list of conditions and the following disclaimer.
                     78:  * 2. Redistributions in binary form must reproduce the above copyright
                     79:  *    notice, this list of conditions and the following disclaimer in the
                     80:  *    documentation and/or other materials provided with the distribution.
                     81:  * 3. All advertising materials mentioning features or use of this software
                     82:  *    must display the following acknowledgement:
                     83:  *     This product includes software developed by the University of
                     84:  *     California, Berkeley and its contributors.
                     85:  * 4. Neither the name of the University nor the names of its contributors
                     86:  *    may be used to endorse or promote products derived from this software
                     87:  *    without specific prior written permission.
                     88:  *
                     89:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     90:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     91:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     92:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     93:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     94:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     95:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     96:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     97:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     98:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     99:  * SUCH DAMAGE.
                    100:  *
1.14      cgd       101:  *     @(#)ip_input.c  8.2 (Berkeley) 1/4/94
1.1       cgd       102:  */
1.55      scottr    103:
1.62      matt      104: #include "opt_gateway.h"
1.69      mrg       105: #include "opt_pfil_hooks.h"
1.91      thorpej   106: #include "opt_ipsec.h"
1.55      scottr    107: #include "opt_mrouting.h"
1.1       cgd       108:
1.5       mycroft   109: #include <sys/param.h>
                    110: #include <sys/systm.h>
                    111: #include <sys/malloc.h>
                    112: #include <sys/mbuf.h>
                    113: #include <sys/domain.h>
                    114: #include <sys/protosw.h>
                    115: #include <sys/socket.h>
1.44      thorpej   116: #include <sys/socketvar.h>
1.5       mycroft   117: #include <sys/errno.h>
                    118: #include <sys/time.h>
                    119: #include <sys/kernel.h>
1.28      christos  120: #include <sys/proc.h>
1.72      thorpej   121: #include <sys/pool.h>
1.28      christos  122:
1.115     mrg       123: #include <uvm/uvm_extern.h>
                    124:
1.28      christos  125: #include <sys/sysctl.h>
1.1       cgd       126:
1.5       mycroft   127: #include <net/if.h>
1.44      thorpej   128: #include <net/if_dl.h>
1.5       mycroft   129: #include <net/route.h>
1.45      mrg       130: #include <net/pfil.h>
1.1       cgd       131:
1.5       mycroft   132: #include <netinet/in.h>
                    133: #include <netinet/in_systm.h>
                    134: #include <netinet/ip.h>
                    135: #include <netinet/in_pcb.h>
                    136: #include <netinet/in_var.h>
                    137: #include <netinet/ip_var.h>
                    138: #include <netinet/ip_icmp.h>
1.89      itojun    139: /* just for gif_ttl */
                    140: #include <netinet/in_gif.h>
                    141: #include "gif.h"
1.111     jdolecek  142:
                    143: #ifdef MROUTING
                    144: #include <netinet/ip_mroute.h>
                    145: #endif
1.89      itojun    146:
                    147: #ifdef IPSEC
                    148: #include <netinet6/ipsec.h>
                    149: #include <netkey/key.h>
                    150: #endif
1.44      thorpej   151:
1.1       cgd       152: #ifndef        IPFORWARDING
                    153: #ifdef GATEWAY
                    154: #define        IPFORWARDING    1       /* forward IP packets not for us */
                    155: #else /* GATEWAY */
                    156: #define        IPFORWARDING    0       /* don't forward IP packets not for us */
                    157: #endif /* GATEWAY */
                    158: #endif /* IPFORWARDING */
                    159: #ifndef        IPSENDREDIRECTS
                    160: #define        IPSENDREDIRECTS 1
                    161: #endif
1.26      thorpej   162: #ifndef IPFORWSRCRT
1.47      cjs       163: #define        IPFORWSRCRT     1       /* forward source-routed packets */
                    164: #endif
                    165: #ifndef IPALLOWSRCRT
1.48      mrg       166: #define        IPALLOWSRCRT    1       /* allow source-routed packets */
1.26      thorpej   167: #endif
1.53      kml       168: #ifndef IPMTUDISC
                    169: #define IPMTUDISC      0
                    170: #endif
1.60      kml       171: #ifndef IPMTUDISCTIMEOUT
1.61      kml       172: #define IPMTUDISCTIMEOUT (10 * 60)     /* as per RFC 1191 */
1.60      kml       173: #endif
1.53      kml       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.60      kml       193: u_int  ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
1.1       cgd       194: #ifdef DIAGNOSTIC
                    195: int    ipprintfs = 0;
                    196: #endif
                    197:
1.60      kml       198: struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
                    199:
1.1       cgd       200: extern struct domain inetdomain;
                    201: int    ipqmaxlen = IFQ_MAXLEN;
1.22      mycroft   202: struct in_ifaddrhead in_ifaddr;
1.57      tls       203: struct in_ifaddrhashhead *in_ifaddrhashtbl;
1.13      mycroft   204: struct ifqueue ipintrq;
1.63      matt      205: struct ipstat  ipstat;
                    206: u_int16_t      ip_id;
1.75      thorpej   207:
1.121     thorpej   208: #ifdef PFIL_HOOKS
                    209: struct pfil_head inet_pfil_hook;
                    210: #endif
                    211:
1.63      matt      212: struct ipqhead ipq;
1.75      thorpej   213: int    ipq_locked;
1.131   ! itojun    214: int    ip_nfragpackets = 0;
        !           215: int    ip_maxfragpackets = -1;
1.75      thorpej   216:
                    217: static __inline int ipq_lock_try __P((void));
                    218: static __inline void ipq_unlock __P((void));
                    219:
                    220: static __inline int
                    221: ipq_lock_try()
                    222: {
                    223:        int s;
                    224:
                    225:        s = splimp();
                    226:        if (ipq_locked) {
                    227:                splx(s);
                    228:                return (0);
                    229:        }
                    230:        ipq_locked = 1;
                    231:        splx(s);
                    232:        return (1);
                    233: }
                    234:
                    235: static __inline void
                    236: ipq_unlock()
                    237: {
                    238:        int s;
                    239:
                    240:        s = splimp();
                    241:        ipq_locked = 0;
                    242:        splx(s);
                    243: }
                    244:
                    245: #ifdef DIAGNOSTIC
                    246: #define        IPQ_LOCK()                                                      \
                    247: do {                                                                   \
                    248:        if (ipq_lock_try() == 0) {                                      \
                    249:                printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
                    250:                panic("ipq_lock");                                      \
                    251:        }                                                               \
                    252: } while (0)
                    253: #define        IPQ_LOCK_CHECK()                                                \
                    254: do {                                                                   \
                    255:        if (ipq_locked == 0) {                                          \
                    256:                printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
                    257:                panic("ipq lock check");                                \
                    258:        }                                                               \
                    259: } while (0)
                    260: #else
                    261: #define        IPQ_LOCK()              (void) ipq_lock_try()
                    262: #define        IPQ_LOCK_CHECK()        /* nothing */
                    263: #endif
                    264:
                    265: #define        IPQ_UNLOCK()            ipq_unlock()
1.1       cgd       266:
1.72      thorpej   267: struct pool ipqent_pool;
                    268:
1.1       cgd       269: /*
                    270:  * We need to save the IP options in case a protocol wants to respond
                    271:  * to an incoming packet over the same route if the packet got here
                    272:  * using IP source routing.  This allows connection establishment and
                    273:  * maintenance when the remote end is on a network that is not known
                    274:  * to us.
                    275:  */
                    276: int    ip_nhops = 0;
                    277: static struct ip_srcrt {
                    278:        struct  in_addr dst;                    /* final destination */
                    279:        char    nop;                            /* one NOP to align */
                    280:        char    srcopt[IPOPT_OFFSET + 1];       /* OPTVAL, OLEN and OFFSET */
                    281:        struct  in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
                    282: } ip_srcrt;
                    283:
1.13      mycroft   284: static void save_rte __P((u_char *, struct in_addr));
1.35      mycroft   285:
1.1       cgd       286: /*
                    287:  * IP initialization: fill in IP protocol switch table.
                    288:  * All protocols not implemented in kernel go to raw IP protocol handler.
                    289:  */
1.8       mycroft   290: void
1.1       cgd       291: ip_init()
                    292: {
1.109     augustss  293:        struct protosw *pr;
                    294:        int i;
1.1       cgd       295:
1.72      thorpej   296:        pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl",
                    297:            0, NULL, NULL, M_IPQ);
                    298:
1.1       cgd       299:        pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
                    300:        if (pr == 0)
                    301:                panic("ip_init");
                    302:        for (i = 0; i < IPPROTO_MAX; i++)
                    303:                ip_protox[i] = pr - inetsw;
                    304:        for (pr = inetdomain.dom_protosw;
                    305:            pr < inetdomain.dom_protoswNPROTOSW; pr++)
                    306:                if (pr->pr_domain->dom_family == PF_INET &&
                    307:                    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
                    308:                        ip_protox[pr->pr_protocol] = pr - inetsw;
1.25      cgd       309:        LIST_INIT(&ipq);
1.1       cgd       310:        ip_id = time.tv_sec & 0xffff;
                    311:        ipintrq.ifq_maxlen = ipqmaxlen;
1.22      mycroft   312:        TAILQ_INIT(&in_ifaddr);
1.120     ad        313:        in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, M_IFADDR,
                    314:            M_WAITOK, &in_ifaddrhash);
1.60      kml       315:        if (ip_mtudisc != 0)
                    316:                ip_mtudisc_timeout_q =
                    317:                    rt_timer_queue_create(ip_mtudisc_timeout);
1.73      thorpej   318: #ifdef GATEWAY
                    319:        ipflow_init();
                    320: #endif
1.121     thorpej   321:
                    322: #ifdef PFIL_HOOKS
                    323:        /* Register our Packet Filter hook. */
1.126     thorpej   324:        inet_pfil_hook.ph_type = PFIL_TYPE_AF;
                    325:        inet_pfil_hook.ph_af   = AF_INET;
1.121     thorpej   326:        i = pfil_head_register(&inet_pfil_hook);
                    327:        if (i != 0)
                    328:                printf("ip_init: WARNING: unable to register pfil hook, "
                    329:                    "error %d\n", i);
                    330: #endif /* PFIL_HOOKS */
1.1       cgd       331: }
                    332:
                    333: struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
                    334: struct route ipforward_rt;
                    335:
                    336: /*
1.89      itojun    337:  * IP software interrupt routine
                    338:  */
                    339: void
                    340: ipintr()
                    341: {
                    342:        int s;
                    343:        struct mbuf *m;
                    344:
                    345:        while (1) {
                    346:                s = splimp();
                    347:                IF_DEQUEUE(&ipintrq, m);
                    348:                splx(s);
                    349:                if (m == 0)
                    350:                        return;
                    351:                ip_input(m);
                    352:        }
                    353: }
                    354:
                    355: /*
1.1       cgd       356:  * Ip input routine.  Checksum and byte swap header.  If fragmented
                    357:  * try to reassemble.  Process options.  Pass to next level.
                    358:  */
1.8       mycroft   359: void
1.89      itojun    360: ip_input(struct mbuf *m)
1.1       cgd       361: {
1.109     augustss  362:        struct ip *ip = NULL;
                    363:        struct ipq *fp;
                    364:        struct in_ifaddr *ia;
                    365:        struct ifaddr *ifa;
1.25      cgd       366:        struct ipqent *ipqe;
1.89      itojun    367:        int hlen = 0, mff, len;
1.100     itojun    368:        int downmatch;
1.1       cgd       369:
                    370: #ifdef DIAGNOSTIC
                    371:        if ((m->m_flags & M_PKTHDR) == 0)
                    372:                panic("ipintr no HDR");
                    373: #endif
1.89      itojun    374: #ifdef IPSEC
                    375:        /*
                    376:         * should the inner packet be considered authentic?
                    377:         * see comment in ah4_input().
                    378:         */
                    379:        if (m) {
                    380:                m->m_flags &= ~M_AUTHIPHDR;
                    381:                m->m_flags &= ~M_AUTHIPDGM;
                    382:        }
                    383: #endif
1.1       cgd       384:        /*
                    385:         * If no IP addresses have been set yet but the interfaces
                    386:         * are receiving, can't do anything with incoming packets yet.
                    387:         */
1.22      mycroft   388:        if (in_ifaddr.tqh_first == 0)
1.1       cgd       389:                goto bad;
                    390:        ipstat.ips_total++;
                    391:        if (m->m_len < sizeof (struct ip) &&
                    392:            (m = m_pullup(m, sizeof (struct ip))) == 0) {
                    393:                ipstat.ips_toosmall++;
1.89      itojun    394:                return;
1.1       cgd       395:        }
                    396:        ip = mtod(m, struct ip *);
1.13      mycroft   397:        if (ip->ip_v != IPVERSION) {
                    398:                ipstat.ips_badvers++;
                    399:                goto bad;
                    400:        }
1.1       cgd       401:        hlen = ip->ip_hl << 2;
                    402:        if (hlen < sizeof(struct ip)) { /* minimum header length */
                    403:                ipstat.ips_badhlen++;
                    404:                goto bad;
                    405:        }
                    406:        if (hlen > m->m_len) {
                    407:                if ((m = m_pullup(m, hlen)) == 0) {
                    408:                        ipstat.ips_badhlen++;
1.89      itojun    409:                        return;
1.1       cgd       410:                }
                    411:                ip = mtod(m, struct ip *);
                    412:        }
1.98      thorpej   413:
1.85      hwr       414:        /*
1.99      thorpej   415:         * RFC1122: packets with a multicast source address are
1.98      thorpej   416:         * not allowed.
1.85      hwr       417:         */
                    418:        if (IN_MULTICAST(ip->ip_src.s_addr)) {
1.130     itojun    419:                ipstat.ips_badaddr++;
1.85      hwr       420:                goto bad;
1.129     itojun    421:        }
                    422:
                    423:        /* 127/8 must not appear on wire - RFC1122 */
                    424:        if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
                    425:            (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
1.130     itojun    426:                if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
                    427:                        ipstat.ips_badaddr++;
1.129     itojun    428:                        goto bad;
1.130     itojun    429:                }
1.85      hwr       430:        }
                    431:
1.78      mycroft   432:        if (in_cksum(m, hlen) != 0) {
1.1       cgd       433:                ipstat.ips_badsum++;
                    434:                goto bad;
                    435:        }
                    436:
1.121     thorpej   437:        /* Retrieve the packet length. */
                    438:        len = ntohs(ip->ip_len);
1.81      proff     439:
                    440:        /*
                    441:         * Check for additional length bogosity
                    442:         */
1.84      proff     443:        if (len < hlen) {
1.81      proff     444:                ipstat.ips_badlen++;
                    445:                goto bad;
                    446:        }
1.1       cgd       447:
                    448:        /*
                    449:         * Check that the amount of data in the buffers
                    450:         * is as at least much as the IP header would have us expect.
                    451:         * Trim mbufs if longer than we expect.
                    452:         * Drop packet if shorter than we expect.
                    453:         */
1.35      mycroft   454:        if (m->m_pkthdr.len < len) {
1.1       cgd       455:                ipstat.ips_tooshort++;
                    456:                goto bad;
                    457:        }
1.35      mycroft   458:        if (m->m_pkthdr.len > len) {
1.1       cgd       459:                if (m->m_len == m->m_pkthdr.len) {
1.35      mycroft   460:                        m->m_len = len;
                    461:                        m->m_pkthdr.len = len;
1.1       cgd       462:                } else
1.35      mycroft   463:                        m_adj(m, len - m->m_pkthdr.len);
1.1       cgd       464:        }
                    465:
1.94      itojun    466: #ifdef IPSEC
                    467:        /* ipflow (IP fast fowarding) is not compatible with IPsec. */
                    468:        m->m_flags &= ~M_CANFASTFWD;
                    469: #else
1.64      thorpej   470:        /*
                    471:         * Assume that we can create a fast-forward IP flow entry
                    472:         * based on this packet.
                    473:         */
                    474:        m->m_flags |= M_CANFASTFWD;
1.94      itojun    475: #endif
1.64      thorpej   476:
1.36      mrg       477: #ifdef PFIL_HOOKS
1.33      mrg       478:        /*
1.64      thorpej   479:         * Run through list of hooks for input packets.  If there are any
                    480:         * filters which require that additional packets in the flow are
                    481:         * not fast-forwarded, they must clear the M_CANFASTFWD flag.
                    482:         * Note that filters must _never_ set this flag, as another filter
                    483:         * in the list may have previously cleared it.
1.33      mrg       484:         */
1.127     itojun    485:        /*
                    486:         * let ipfilter look at packet on the wire,
                    487:         * not the decapsulated packet.
                    488:         */
                    489: #ifdef IPSEC
                    490:        if (!ipsec_gethist(m, NULL))
                    491: #else
                    492:        if (1)
                    493: #endif
                    494:        {
                    495:                if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
                    496:                                   PFIL_IN) != 0)
1.121     thorpej   497:                return;
1.127     itojun    498:                if (m == NULL)
                    499:                        return;
                    500:                ip = mtod(m, struct ip *);
                    501:        }
1.36      mrg       502: #endif /* PFIL_HOOKS */
1.123     thorpej   503:
                    504: #ifdef ALTQ
                    505:        /* XXX Temporary until ALTQ is changed to use a pfil hook */
                    506:        if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) {
                    507:                /* packet dropped by traffic conditioner */
                    508:                return;
                    509:        }
                    510: #endif
1.121     thorpej   511:
                    512:        /*
                    513:         * Convert fields to host representation.
                    514:         */
                    515:        NTOHS(ip->ip_len);
                    516:        NTOHS(ip->ip_off);
1.33      mrg       517:
1.1       cgd       518:        /*
                    519:         * Process options and, if not destined for us,
                    520:         * ship it on.  ip_dooptions returns 1 when an
                    521:         * error was detected (causing an icmp message
                    522:         * to be sent and the original packet to be freed).
                    523:         */
                    524:        ip_nhops = 0;           /* for source routed packets */
                    525:        if (hlen > sizeof (struct ip) && ip_dooptions(m))
1.89      itojun    526:                return;
1.1       cgd       527:
                    528:        /*
                    529:         * Check our list of addresses, to see if the packet is for us.
1.100     itojun    530:         *
                    531:         * Traditional 4.4BSD did not consult IFF_UP at all.
                    532:         * The behavior here is to treat addresses on !IFF_UP interface
                    533:         * as not mine.
1.1       cgd       534:         */
1.100     itojun    535:        downmatch = 0;
1.97      itojun    536:        for (ia = IN_IFADDR_HASH(ip->ip_dst.s_addr).lh_first;
                    537:             ia != NULL;
                    538:             ia = ia->ia_hash.le_next) {
                    539:                if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
                    540:                        if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
                    541:                                break;
1.100     itojun    542:                        else
                    543:                                downmatch++;
1.97      itojun    544:                }
                    545:        }
1.86      thorpej   546:        if (ia != NULL)
                    547:                goto ours;
1.57      tls       548:        if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
                    549:                for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
                    550:                    ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
                    551:                        if (ifa->ifa_addr->sa_family != AF_INET) continue;
                    552:                        ia = ifatoia(ifa);
1.35      mycroft   553:                        if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
                    554:                            in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
1.20      mycroft   555:                            /*
                    556:                             * Look for all-0's host part (old broadcast addr),
                    557:                             * either for subnet or net.
                    558:                             */
                    559:                            ip->ip_dst.s_addr == ia->ia_subnet ||
1.18      mycroft   560:                            ip->ip_dst.s_addr == ia->ia_net)
1.1       cgd       561:                                goto ours;
1.57      tls       562:                        /*
                    563:                         * An interface with IP address zero accepts
                    564:                         * all packets that arrive on that interface.
                    565:                         */
                    566:                        if (in_nullhost(ia->ia_addr.sin_addr))
                    567:                                goto ours;
1.1       cgd       568:                }
                    569:        }
1.18      mycroft   570:        if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4       hpeyerl   571:                struct in_multi *inm;
                    572: #ifdef MROUTING
                    573:                extern struct socket *ip_mrouter;
1.10      brezak    574:
                    575:                if (m->m_flags & M_EXT) {
                    576:                        if ((m = m_pullup(m, hlen)) == 0) {
                    577:                                ipstat.ips_toosmall++;
1.89      itojun    578:                                return;
1.10      brezak    579:                        }
                    580:                        ip = mtod(m, struct ip *);
                    581:                }
1.4       hpeyerl   582:
                    583:                if (ip_mrouter) {
                    584:                        /*
                    585:                         * If we are acting as a multicast router, all
                    586:                         * incoming multicast packets are passed to the
                    587:                         * kernel-level multicast forwarding function.
                    588:                         * The packet is returned (relatively) intact; if
                    589:                         * ip_mforward() returns a non-zero value, the packet
                    590:                         * must be discarded, else it may be accepted below.
                    591:                         *
                    592:                         * (The IP ident field is put in the same byte order
                    593:                         * as expected when ip_mforward() is called from
                    594:                         * ip_output().)
                    595:                         */
1.13      mycroft   596:                        if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
                    597:                                ipstat.ips_cantforward++;
1.4       hpeyerl   598:                                m_freem(m);
1.89      itojun    599:                                return;
1.4       hpeyerl   600:                        }
                    601:
                    602:                        /*
                    603:                         * The process-level routing demon needs to receive
                    604:                         * all multicast IGMP packets, whether or not this
                    605:                         * host belongs to their destination groups.
                    606:                         */
                    607:                        if (ip->ip_p == IPPROTO_IGMP)
                    608:                                goto ours;
1.13      mycroft   609:                        ipstat.ips_forward++;
1.4       hpeyerl   610:                }
                    611: #endif
                    612:                /*
                    613:                 * See if we belong to the destination multicast group on the
                    614:                 * arrival interface.
                    615:                 */
                    616:                IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
                    617:                if (inm == NULL) {
1.13      mycroft   618:                        ipstat.ips_cantforward++;
1.4       hpeyerl   619:                        m_freem(m);
1.89      itojun    620:                        return;
1.4       hpeyerl   621:                }
                    622:                goto ours;
                    623:        }
1.19      mycroft   624:        if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
1.35      mycroft   625:            in_nullhost(ip->ip_dst))
1.1       cgd       626:                goto ours;
                    627:
                    628:        /*
                    629:         * Not for us; forward if possible and desirable.
                    630:         */
                    631:        if (ipforwarding == 0) {
                    632:                ipstat.ips_cantforward++;
                    633:                m_freem(m);
1.100     itojun    634:        } else {
                    635:                /*
                    636:                 * If ip_dst matched any of my address on !IFF_UP interface,
                    637:                 * and there's no IFF_UP interface that matches ip_dst,
                    638:                 * send icmp unreach.  Forwarding it will result in in-kernel
                    639:                 * forwarding loop till TTL goes to 0.
                    640:                 */
                    641:                if (downmatch) {
                    642:                        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
                    643:                        ipstat.ips_cantforward++;
                    644:                        return;
                    645:                }
1.1       cgd       646:                ip_forward(m, 0);
1.100     itojun    647:        }
1.89      itojun    648:        return;
1.1       cgd       649:
                    650: ours:
                    651:        /*
                    652:         * If offset or IP_MF are set, must reassemble.
                    653:         * Otherwise, nothing need be done.
                    654:         * (We could look in the reassembly queue to see
                    655:         * if the packet was previously fragmented,
                    656:         * but it's not worth the time; just let them time out.)
                    657:         */
1.37      perry     658:        if (ip->ip_off & ~(IP_DF|IP_RF)) {
1.1       cgd       659:                /*
                    660:                 * Look for queue of fragments
                    661:                 * of this datagram.
                    662:                 */
1.75      thorpej   663:                IPQ_LOCK();
1.25      cgd       664:                for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
1.1       cgd       665:                        if (ip->ip_id == fp->ipq_id &&
1.35      mycroft   666:                            in_hosteq(ip->ip_src, fp->ipq_src) &&
                    667:                            in_hosteq(ip->ip_dst, fp->ipq_dst) &&
1.1       cgd       668:                            ip->ip_p == fp->ipq_p)
                    669:                                goto found;
                    670:                fp = 0;
                    671: found:
                    672:
                    673:                /*
                    674:                 * Adjust ip_len to not reflect header,
1.25      cgd       675:                 * set ipqe_mff if more fragments are expected,
1.1       cgd       676:                 * convert offset of this to bytes.
                    677:                 */
                    678:                ip->ip_len -= hlen;
1.25      cgd       679:                mff = (ip->ip_off & IP_MF) != 0;
                    680:                if (mff) {
1.16      cgd       681:                        /*
                    682:                         * Make sure that fragments have a data length
                    683:                         * that's a non-zero multiple of 8 bytes.
                    684:                         */
1.17      cgd       685:                        if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
1.16      cgd       686:                                ipstat.ips_badfrags++;
1.75      thorpej   687:                                IPQ_UNLOCK();
1.16      cgd       688:                                goto bad;
                    689:                        }
                    690:                }
1.1       cgd       691:                ip->ip_off <<= 3;
                    692:
                    693:                /*
                    694:                 * If datagram marked as having more fragments
                    695:                 * or if this is not the first fragment,
                    696:                 * attempt reassembly; if it succeeds, proceed.
                    697:                 */
1.25      cgd       698:                if (mff || ip->ip_off) {
1.1       cgd       699:                        ipstat.ips_fragments++;
1.72      thorpej   700:                        ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
1.25      cgd       701:                        if (ipqe == NULL) {
                    702:                                ipstat.ips_rcvmemdrop++;
1.75      thorpej   703:                                IPQ_UNLOCK();
1.25      cgd       704:                                goto bad;
                    705:                        }
                    706:                        ipqe->ipqe_mff = mff;
1.50      thorpej   707:                        ipqe->ipqe_m = m;
1.25      cgd       708:                        ipqe->ipqe_ip = ip;
1.50      thorpej   709:                        m = ip_reass(ipqe, fp);
1.75      thorpej   710:                        if (m == 0) {
                    711:                                IPQ_UNLOCK();
1.89      itojun    712:                                return;
1.75      thorpej   713:                        }
1.13      mycroft   714:                        ipstat.ips_reassembled++;
1.50      thorpej   715:                        ip = mtod(m, struct ip *);
1.74      thorpej   716:                        hlen = ip->ip_hl << 2;
1.79      mycroft   717:                        ip->ip_len += hlen;
1.1       cgd       718:                } else
                    719:                        if (fp)
                    720:                                ip_freef(fp);
1.75      thorpej   721:                IPQ_UNLOCK();
1.79      mycroft   722:        }
1.128     itojun    723:
                    724: #ifdef IPSEC
                    725:        /*
                    726:         * enforce IPsec policy checking if we are seeing last header.
                    727:         * note that we do not visit this with protocols with pcb layer
                    728:         * code - like udp/tcp/raw ip.
                    729:         */
                    730:        if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
                    731:            ipsec4_in_reject(m, NULL)) {
                    732:                ipsecstat.in_polvio++;
                    733:                goto bad;
                    734:        }
                    735: #endif
1.1       cgd       736:
                    737:        /*
                    738:         * Switch out to protocol's input routine.
                    739:         */
1.82      aidan     740: #if IFA_STATS
1.122     itojun    741:        if (ia && ip)
                    742:                ia->ia_ifa.ifa_data.ifad_inbytes += ip->ip_len;
1.82      aidan     743: #endif
1.1       cgd       744:        ipstat.ips_delivered++;
1.89      itojun    745:     {
                    746:        int off = hlen, nh = ip->ip_p;
                    747:
                    748:        (*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
                    749:        return;
                    750:     }
1.1       cgd       751: bad:
                    752:        m_freem(m);
                    753: }
                    754:
                    755: /*
                    756:  * Take incoming datagram fragment and try to
                    757:  * reassemble it into whole datagram.  If a chain for
                    758:  * reassembly of this datagram already exists, then it
                    759:  * is given as fp; otherwise have to make a chain.
                    760:  */
1.50      thorpej   761: struct mbuf *
1.25      cgd       762: ip_reass(ipqe, fp)
1.109     augustss  763:        struct ipqent *ipqe;
                    764:        struct ipq *fp;
1.1       cgd       765: {
1.109     augustss  766:        struct mbuf *m = ipqe->ipqe_m;
                    767:        struct ipqent *nq, *p, *q;
1.25      cgd       768:        struct ip *ip;
1.1       cgd       769:        struct mbuf *t;
1.25      cgd       770:        int hlen = ipqe->ipqe_ip->ip_hl << 2;
1.1       cgd       771:        int i, next;
                    772:
1.75      thorpej   773:        IPQ_LOCK_CHECK();
                    774:
1.1       cgd       775:        /*
                    776:         * Presence of header sizes in mbufs
                    777:         * would confuse code below.
                    778:         */
                    779:        m->m_data += hlen;
                    780:        m->m_len -= hlen;
                    781:
                    782:        /*
                    783:         * If first fragment to arrive, create a reassembly queue.
                    784:         */
                    785:        if (fp == 0) {
1.131   ! itojun    786:                /*
        !           787:                 * Enforce upper bound on number of fragmented packets
        !           788:                 * for which we attempt reassembly;
        !           789:                 * If maxfrag is 0, never accept fragments.
        !           790:                 * If maxfrag is -1, accept all fragments without limitation.
        !           791:                 */
        !           792:                if (ip_maxfragpackets < 0)
        !           793:                        ;
        !           794:                else if (ip_nfragpackets >= ip_maxfragpackets)
        !           795:                        goto dropfrag;
        !           796:                ip_nfragpackets++;
1.50      thorpej   797:                MALLOC(fp, struct ipq *, sizeof (struct ipq),
                    798:                    M_FTABLE, M_NOWAIT);
                    799:                if (fp == NULL)
1.1       cgd       800:                        goto dropfrag;
1.25      cgd       801:                LIST_INSERT_HEAD(&ipq, fp, ipq_q);
1.1       cgd       802:                fp->ipq_ttl = IPFRAGTTL;
1.25      cgd       803:                fp->ipq_p = ipqe->ipqe_ip->ip_p;
                    804:                fp->ipq_id = ipqe->ipqe_ip->ip_id;
                    805:                LIST_INIT(&fp->ipq_fragq);
                    806:                fp->ipq_src = ipqe->ipqe_ip->ip_src;
                    807:                fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
                    808:                p = NULL;
1.1       cgd       809:                goto insert;
                    810:        }
                    811:
                    812:        /*
                    813:         * Find a segment which begins after this one does.
                    814:         */
1.25      cgd       815:        for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
                    816:            p = q, q = q->ipqe_q.le_next)
                    817:                if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
1.1       cgd       818:                        break;
                    819:
                    820:        /*
                    821:         * If there is a preceding segment, it may provide some of
                    822:         * our data already.  If so, drop the data from the incoming
                    823:         * segment.  If it provides all of our data, drop us.
                    824:         */
1.25      cgd       825:        if (p != NULL) {
                    826:                i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
                    827:                    ipqe->ipqe_ip->ip_off;
1.1       cgd       828:                if (i > 0) {
1.25      cgd       829:                        if (i >= ipqe->ipqe_ip->ip_len)
1.1       cgd       830:                                goto dropfrag;
1.50      thorpej   831:                        m_adj(ipqe->ipqe_m, i);
1.25      cgd       832:                        ipqe->ipqe_ip->ip_off += i;
                    833:                        ipqe->ipqe_ip->ip_len -= i;
1.1       cgd       834:                }
                    835:        }
                    836:
                    837:        /*
                    838:         * While we overlap succeeding segments trim them or,
                    839:         * if they are completely covered, dequeue them.
                    840:         */
1.25      cgd       841:        for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
                    842:            q->ipqe_ip->ip_off; q = nq) {
                    843:                i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
                    844:                    q->ipqe_ip->ip_off;
                    845:                if (i < q->ipqe_ip->ip_len) {
                    846:                        q->ipqe_ip->ip_len -= i;
                    847:                        q->ipqe_ip->ip_off += i;
1.50      thorpej   848:                        m_adj(q->ipqe_m, i);
1.1       cgd       849:                        break;
                    850:                }
1.25      cgd       851:                nq = q->ipqe_q.le_next;
1.50      thorpej   852:                m_freem(q->ipqe_m);
1.25      cgd       853:                LIST_REMOVE(q, ipqe_q);
1.72      thorpej   854:                pool_put(&ipqent_pool, q);
1.1       cgd       855:        }
                    856:
                    857: insert:
                    858:        /*
                    859:         * Stick new segment in its place;
                    860:         * check for complete reassembly.
                    861:         */
1.25      cgd       862:        if (p == NULL) {
                    863:                LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
                    864:        } else {
                    865:                LIST_INSERT_AFTER(p, ipqe, ipqe_q);
                    866:        }
1.1       cgd       867:        next = 0;
1.25      cgd       868:        for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
                    869:            p = q, q = q->ipqe_q.le_next) {
                    870:                if (q->ipqe_ip->ip_off != next)
1.1       cgd       871:                        return (0);
1.25      cgd       872:                next += q->ipqe_ip->ip_len;
1.1       cgd       873:        }
1.25      cgd       874:        if (p->ipqe_mff)
1.1       cgd       875:                return (0);
                    876:
                    877:        /*
1.41      thorpej   878:         * Reassembly is complete.  Check for a bogus message size and
                    879:         * concatenate fragments.
1.1       cgd       880:         */
1.25      cgd       881:        q = fp->ipq_fragq.lh_first;
                    882:        ip = q->ipqe_ip;
1.41      thorpej   883:        if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
                    884:                ipstat.ips_toolong++;
                    885:                ip_freef(fp);
                    886:                return (0);
                    887:        }
1.50      thorpej   888:        m = q->ipqe_m;
1.1       cgd       889:        t = m->m_next;
                    890:        m->m_next = 0;
                    891:        m_cat(m, t);
1.25      cgd       892:        nq = q->ipqe_q.le_next;
1.72      thorpej   893:        pool_put(&ipqent_pool, q);
1.25      cgd       894:        for (q = nq; q != NULL; q = nq) {
1.50      thorpej   895:                t = q->ipqe_m;
1.25      cgd       896:                nq = q->ipqe_q.le_next;
1.72      thorpej   897:                pool_put(&ipqent_pool, q);
1.1       cgd       898:                m_cat(m, t);
                    899:        }
                    900:
                    901:        /*
                    902:         * Create header for new ip packet by
                    903:         * modifying header of first packet;
                    904:         * dequeue and discard fragment reassembly header.
                    905:         * Make header visible.
                    906:         */
                    907:        ip->ip_len = next;
1.25      cgd       908:        ip->ip_src = fp->ipq_src;
                    909:        ip->ip_dst = fp->ipq_dst;
                    910:        LIST_REMOVE(fp, ipq_q);
1.50      thorpej   911:        FREE(fp, M_FTABLE);
1.131   ! itojun    912:        ip_nfragpackets--;
1.1       cgd       913:        m->m_len += (ip->ip_hl << 2);
                    914:        m->m_data -= (ip->ip_hl << 2);
                    915:        /* some debugging cruft by sklower, below, will go away soon */
                    916:        if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1.109     augustss  917:                int plen = 0;
1.50      thorpej   918:                for (t = m; t; t = t->m_next)
                    919:                        plen += t->m_len;
                    920:                m->m_pkthdr.len = plen;
1.1       cgd       921:        }
1.50      thorpej   922:        return (m);
1.1       cgd       923:
                    924: dropfrag:
                    925:        ipstat.ips_fragdropped++;
                    926:        m_freem(m);
1.72      thorpej   927:        pool_put(&ipqent_pool, ipqe);
1.1       cgd       928:        return (0);
                    929: }
                    930:
                    931: /*
                    932:  * Free a fragment reassembly header and all
                    933:  * associated datagrams.
                    934:  */
1.8       mycroft   935: void
1.1       cgd       936: ip_freef(fp)
                    937:        struct ipq *fp;
                    938: {
1.109     augustss  939:        struct ipqent *q, *p;
1.1       cgd       940:
1.75      thorpej   941:        IPQ_LOCK_CHECK();
                    942:
1.25      cgd       943:        for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
                    944:                p = q->ipqe_q.le_next;
1.50      thorpej   945:                m_freem(q->ipqe_m);
1.25      cgd       946:                LIST_REMOVE(q, ipqe_q);
1.72      thorpej   947:                pool_put(&ipqent_pool, q);
1.1       cgd       948:        }
1.25      cgd       949:        LIST_REMOVE(fp, ipq_q);
1.50      thorpej   950:        FREE(fp, M_FTABLE);
1.131   ! itojun    951:        ip_nfragpackets--;
1.1       cgd       952: }
                    953:
                    954: /*
                    955:  * IP timer processing;
                    956:  * if a timer expires on a reassembly
                    957:  * queue, discard it.
                    958:  */
1.8       mycroft   959: void
1.1       cgd       960: ip_slowtimo()
                    961: {
1.109     augustss  962:        struct ipq *fp, *nfp;
1.24      mycroft   963:        int s = splsoftnet();
1.1       cgd       964:
1.75      thorpej   965:        IPQ_LOCK();
1.25      cgd       966:        for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
                    967:                nfp = fp->ipq_q.le_next;
                    968:                if (--fp->ipq_ttl == 0) {
1.1       cgd       969:                        ipstat.ips_fragtimeout++;
1.25      cgd       970:                        ip_freef(fp);
1.1       cgd       971:                }
                    972:        }
1.131   ! itojun    973:        /*
        !           974:         * If we are over the maximum number of fragments
        !           975:         * (due to the limit being lowered), drain off
        !           976:         * enough to get down to the new limit.
        !           977:         */
        !           978:        if (ip_maxfragpackets < 0)
        !           979:                ;
        !           980:        else {
        !           981:                while (ip_nfragpackets > ip_maxfragpackets && ipq.lh_first)
        !           982:                        ip_freef(ipq.lh_first);
        !           983:        }
1.75      thorpej   984:        IPQ_UNLOCK();
1.63      matt      985: #ifdef GATEWAY
                    986:        ipflow_slowtimo();
                    987: #endif
1.1       cgd       988:        splx(s);
                    989: }
                    990:
                    991: /*
                    992:  * Drain off all datagram fragments.
                    993:  */
1.8       mycroft   994: void
1.1       cgd       995: ip_drain()
                    996: {
                    997:
1.75      thorpej   998:        /*
                    999:         * We may be called from a device's interrupt context.  If
                   1000:         * the ipq is already busy, just bail out now.
                   1001:         */
                   1002:        if (ipq_lock_try() == 0)
                   1003:                return;
                   1004:
1.25      cgd      1005:        while (ipq.lh_first != NULL) {
1.1       cgd      1006:                ipstat.ips_fragdropped++;
1.25      cgd      1007:                ip_freef(ipq.lh_first);
1.1       cgd      1008:        }
1.75      thorpej  1009:
                   1010:        IPQ_UNLOCK();
1.1       cgd      1011: }
                   1012:
                   1013: /*
                   1014:  * Do option processing on a datagram,
                   1015:  * possibly discarding it if bad options are encountered,
                   1016:  * or forwarding it if source-routed.
                   1017:  * Returns 1 if packet has been forwarded/freed,
                   1018:  * 0 if the packet should be processed further.
                   1019:  */
1.8       mycroft  1020: int
1.1       cgd      1021: ip_dooptions(m)
                   1022:        struct mbuf *m;
                   1023: {
1.109     augustss 1024:        struct ip *ip = mtod(m, struct ip *);
                   1025:        u_char *cp, *cp0;
                   1026:        struct ip_timestamp *ipt;
                   1027:        struct in_ifaddr *ia;
1.1       cgd      1028:        int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1.104     thorpej  1029:        struct in_addr dst;
1.1       cgd      1030:        n_time ntime;
                   1031:
1.13      mycroft  1032:        dst = ip->ip_dst;
1.1       cgd      1033:        cp = (u_char *)(ip + 1);
                   1034:        cnt = (ip->ip_hl << 2) - sizeof (struct ip);
                   1035:        for (; cnt > 0; cnt -= optlen, cp += optlen) {
                   1036:                opt = cp[IPOPT_OPTVAL];
                   1037:                if (opt == IPOPT_EOL)
                   1038:                        break;
                   1039:                if (opt == IPOPT_NOP)
                   1040:                        optlen = 1;
                   1041:                else {
1.113     itojun   1042:                        if (cnt < IPOPT_OLEN + sizeof(*cp)) {
                   1043:                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
                   1044:                                goto bad;
                   1045:                        }
1.1       cgd      1046:                        optlen = cp[IPOPT_OLEN];
1.114     itojun   1047:                        if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1.1       cgd      1048:                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
                   1049:                                goto bad;
                   1050:                        }
                   1051:                }
                   1052:                switch (opt) {
                   1053:
                   1054:                default:
                   1055:                        break;
                   1056:
                   1057:                /*
                   1058:                 * Source routing with record.
                   1059:                 * Find interface with current destination address.
                   1060:                 * If none on this machine then drop if strictly routed,
                   1061:                 * or do nothing if loosely routed.
                   1062:                 * Record interface address and bring up next address
                   1063:                 * component.  If strictly routed make sure next
                   1064:                 * address is on directly accessible net.
                   1065:                 */
                   1066:                case IPOPT_LSRR:
                   1067:                case IPOPT_SSRR:
1.47      cjs      1068:                        if (ip_allowsrcrt == 0) {
                   1069:                                type = ICMP_UNREACH;
                   1070:                                code = ICMP_UNREACH_NET_PROHIB;
                   1071:                                goto bad;
                   1072:                        }
1.114     itojun   1073:                        if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
                   1074:                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
                   1075:                                goto bad;
                   1076:                        }
1.1       cgd      1077:                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
                   1078:                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
                   1079:                                goto bad;
                   1080:                        }
                   1081:                        ipaddr.sin_addr = ip->ip_dst;
1.19      mycroft  1082:                        ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1.1       cgd      1083:                        if (ia == 0) {
                   1084:                                if (opt == IPOPT_SSRR) {
                   1085:                                        type = ICMP_UNREACH;
                   1086:                                        code = ICMP_UNREACH_SRCFAIL;
                   1087:                                        goto bad;
                   1088:                                }
                   1089:                                /*
                   1090:                                 * Loose routing, and not at next destination
                   1091:                                 * yet; nothing to do except forward.
                   1092:                                 */
                   1093:                                break;
                   1094:                        }
                   1095:                        off--;                  /* 0 origin */
1.112     sommerfe 1096:                        if ((off + sizeof(struct in_addr)) > optlen) {
1.1       cgd      1097:                                /*
                   1098:                                 * End of source route.  Should be for us.
                   1099:                                 */
                   1100:                                save_rte(cp, ip->ip_src);
                   1101:                                break;
                   1102:                        }
                   1103:                        /*
                   1104:                         * locate outgoing interface
                   1105:                         */
                   1106:                        bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
                   1107:                            sizeof(ipaddr.sin_addr));
1.96      thorpej  1108:                        if (opt == IPOPT_SSRR)
                   1109:                                ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
                   1110:                        else
1.1       cgd      1111:                                ia = ip_rtaddr(ipaddr.sin_addr);
                   1112:                        if (ia == 0) {
                   1113:                                type = ICMP_UNREACH;
                   1114:                                code = ICMP_UNREACH_SRCFAIL;
                   1115:                                goto bad;
                   1116:                        }
                   1117:                        ip->ip_dst = ipaddr.sin_addr;
1.20      mycroft  1118:                        bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1       cgd      1119:                            (caddr_t)(cp + off), sizeof(struct in_addr));
                   1120:                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13      mycroft  1121:                        /*
                   1122:                         * Let ip_intr's mcast routing check handle mcast pkts
                   1123:                         */
1.18      mycroft  1124:                        forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1       cgd      1125:                        break;
                   1126:
                   1127:                case IPOPT_RR:
1.114     itojun   1128:                        if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
                   1129:                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
                   1130:                                goto bad;
                   1131:                        }
1.1       cgd      1132:                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
                   1133:                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
                   1134:                                goto bad;
                   1135:                        }
                   1136:                        /*
                   1137:                         * If no space remains, ignore.
                   1138:                         */
                   1139:                        off--;                  /* 0 origin */
1.112     sommerfe 1140:                        if ((off + sizeof(struct in_addr)) > optlen)
1.1       cgd      1141:                                break;
                   1142:                        bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
                   1143:                            sizeof(ipaddr.sin_addr));
                   1144:                        /*
                   1145:                         * locate outgoing interface; if we're the destination,
                   1146:                         * use the incoming interface (should be same).
                   1147:                         */
1.96      thorpej  1148:                        if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
                   1149:                            == NULL &&
                   1150:                            (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1.1       cgd      1151:                                type = ICMP_UNREACH;
                   1152:                                code = ICMP_UNREACH_HOST;
                   1153:                                goto bad;
                   1154:                        }
1.20      mycroft  1155:                        bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1       cgd      1156:                            (caddr_t)(cp + off), sizeof(struct in_addr));
                   1157:                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
                   1158:                        break;
                   1159:
                   1160:                case IPOPT_TS:
                   1161:                        code = cp - (u_char *)ip;
                   1162:                        ipt = (struct ip_timestamp *)cp;
1.114     itojun   1163:                        if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
                   1164:                                code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1.1       cgd      1165:                                goto bad;
1.114     itojun   1166:                        }
                   1167:                        if (ipt->ipt_ptr < 5) {
                   1168:                                code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
                   1169:                                goto bad;
                   1170:                        }
1.15      cgd      1171:                        if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.114     itojun   1172:                                if (++ipt->ipt_oflw == 0) {
                   1173:                                        code = (u_char *)&ipt->ipt_ptr -
                   1174:                                            (u_char *)ip;
1.1       cgd      1175:                                        goto bad;
1.114     itojun   1176:                                }
1.1       cgd      1177:                                break;
                   1178:                        }
1.104     thorpej  1179:                        cp0 = (cp + ipt->ipt_ptr - 1);
1.1       cgd      1180:                        switch (ipt->ipt_flg) {
                   1181:
                   1182:                        case IPOPT_TS_TSONLY:
                   1183:                                break;
                   1184:
                   1185:                        case IPOPT_TS_TSANDADDR:
1.66      thorpej  1186:                                if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.114     itojun   1187:                                    sizeof(struct in_addr) > ipt->ipt_len) {
                   1188:                                        code = (u_char *)&ipt->ipt_ptr -
                   1189:                                            (u_char *)ip;
1.1       cgd      1190:                                        goto bad;
1.114     itojun   1191:                                }
1.13      mycroft  1192:                                ipaddr.sin_addr = dst;
1.96      thorpej  1193:                                ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
                   1194:                                    m->m_pkthdr.rcvif));
1.13      mycroft  1195:                                if (ia == 0)
                   1196:                                        continue;
1.104     thorpej  1197:                                bcopy(&ia->ia_addr.sin_addr,
                   1198:                                    cp0, sizeof(struct in_addr));
1.1       cgd      1199:                                ipt->ipt_ptr += sizeof(struct in_addr);
                   1200:                                break;
                   1201:
                   1202:                        case IPOPT_TS_PRESPEC:
1.66      thorpej  1203:                                if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.114     itojun   1204:                                    sizeof(struct in_addr) > ipt->ipt_len) {
                   1205:                                        code = (u_char *)&ipt->ipt_ptr -
                   1206:                                            (u_char *)ip;
1.1       cgd      1207:                                        goto bad;
1.114     itojun   1208:                                }
1.104     thorpej  1209:                                bcopy(cp0, &ipaddr.sin_addr,
1.1       cgd      1210:                                    sizeof(struct in_addr));
1.96      thorpej  1211:                                if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
                   1212:                                    == NULL)
1.1       cgd      1213:                                        continue;
                   1214:                                ipt->ipt_ptr += sizeof(struct in_addr);
                   1215:                                break;
                   1216:
                   1217:                        default:
1.114     itojun   1218:                                /* XXX can't take &ipt->ipt_flg */
                   1219:                                code = (u_char *)&ipt->ipt_ptr -
                   1220:                                    (u_char *)ip + 1;
1.1       cgd      1221:                                goto bad;
                   1222:                        }
                   1223:                        ntime = iptime();
1.107     thorpej  1224:                        cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
                   1225:                        bcopy(cp0, (caddr_t)cp + ipt->ipt_ptr - 1,
1.1       cgd      1226:                            sizeof(n_time));
                   1227:                        ipt->ipt_ptr += sizeof(n_time);
                   1228:                }
                   1229:        }
                   1230:        if (forward) {
1.26      thorpej  1231:                if (ip_forwsrcrt == 0) {
                   1232:                        type = ICMP_UNREACH;
                   1233:                        code = ICMP_UNREACH_SRCFAIL;
                   1234:                        goto bad;
                   1235:                }
1.1       cgd      1236:                ip_forward(m, 1);
                   1237:                return (1);
1.13      mycroft  1238:        }
                   1239:        return (0);
1.1       cgd      1240: bad:
1.13      mycroft  1241:        icmp_error(m, type, code, 0, 0);
                   1242:        ipstat.ips_badoptions++;
1.1       cgd      1243:        return (1);
                   1244: }
                   1245:
                   1246: /*
                   1247:  * Given address of next destination (final or next hop),
                   1248:  * return internet address info of interface to be used to get there.
                   1249:  */
                   1250: struct in_ifaddr *
                   1251: ip_rtaddr(dst)
                   1252:         struct in_addr dst;
                   1253: {
1.109     augustss 1254:        struct sockaddr_in *sin;
1.1       cgd      1255:
1.19      mycroft  1256:        sin = satosin(&ipforward_rt.ro_dst);
1.1       cgd      1257:
1.35      mycroft  1258:        if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
1.1       cgd      1259:                if (ipforward_rt.ro_rt) {
                   1260:                        RTFREE(ipforward_rt.ro_rt);
                   1261:                        ipforward_rt.ro_rt = 0;
                   1262:                }
                   1263:                sin->sin_family = AF_INET;
                   1264:                sin->sin_len = sizeof(*sin);
                   1265:                sin->sin_addr = dst;
                   1266:
                   1267:                rtalloc(&ipforward_rt);
                   1268:        }
                   1269:        if (ipforward_rt.ro_rt == 0)
                   1270:                return ((struct in_ifaddr *)0);
1.19      mycroft  1271:        return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1.1       cgd      1272: }
                   1273:
                   1274: /*
                   1275:  * Save incoming source route for use in replies,
                   1276:  * to be picked up later by ip_srcroute if the receiver is interested.
                   1277:  */
1.13      mycroft  1278: void
1.1       cgd      1279: save_rte(option, dst)
                   1280:        u_char *option;
                   1281:        struct in_addr dst;
                   1282: {
                   1283:        unsigned olen;
                   1284:
                   1285:        olen = option[IPOPT_OLEN];
                   1286: #ifdef DIAGNOSTIC
                   1287:        if (ipprintfs)
1.39      christos 1288:                printf("save_rte: olen %d\n", olen);
1.89      itojun   1289: #endif /* 0 */
1.1       cgd      1290:        if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
                   1291:                return;
                   1292:        bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
                   1293:        ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
                   1294:        ip_srcrt.dst = dst;
                   1295: }
                   1296:
                   1297: /*
                   1298:  * Retrieve incoming source route for use in replies,
                   1299:  * in the same form used by setsockopt.
                   1300:  * The first hop is placed before the options, will be removed later.
                   1301:  */
                   1302: struct mbuf *
                   1303: ip_srcroute()
                   1304: {
1.109     augustss 1305:        struct in_addr *p, *q;
                   1306:        struct mbuf *m;
1.1       cgd      1307:
                   1308:        if (ip_nhops == 0)
                   1309:                return ((struct mbuf *)0);
                   1310:        m = m_get(M_DONTWAIT, MT_SOOPTS);
                   1311:        if (m == 0)
                   1312:                return ((struct mbuf *)0);
                   1313:
1.13      mycroft  1314: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1       cgd      1315:
                   1316:        /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
                   1317:        m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
                   1318:            OPTSIZ;
                   1319: #ifdef DIAGNOSTIC
                   1320:        if (ipprintfs)
1.39      christos 1321:                printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1.1       cgd      1322: #endif
                   1323:
                   1324:        /*
                   1325:         * First save first hop for return route
                   1326:         */
                   1327:        p = &ip_srcrt.route[ip_nhops - 1];
                   1328:        *(mtod(m, struct in_addr *)) = *p--;
                   1329: #ifdef DIAGNOSTIC
                   1330:        if (ipprintfs)
1.39      christos 1331:                printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1.1       cgd      1332: #endif
                   1333:
                   1334:        /*
                   1335:         * Copy option fields and padding (nop) to mbuf.
                   1336:         */
                   1337:        ip_srcrt.nop = IPOPT_NOP;
                   1338:        ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
                   1339:        bcopy((caddr_t)&ip_srcrt.nop,
                   1340:            mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
                   1341:        q = (struct in_addr *)(mtod(m, caddr_t) +
                   1342:            sizeof(struct in_addr) + OPTSIZ);
                   1343: #undef OPTSIZ
                   1344:        /*
                   1345:         * Record return path as an IP source route,
                   1346:         * reversing the path (pointers are now aligned).
                   1347:         */
                   1348:        while (p >= ip_srcrt.route) {
                   1349: #ifdef DIAGNOSTIC
                   1350:                if (ipprintfs)
1.39      christos 1351:                        printf(" %x", ntohl(q->s_addr));
1.1       cgd      1352: #endif
                   1353:                *q++ = *p--;
                   1354:        }
                   1355:        /*
                   1356:         * Last hop goes to final destination.
                   1357:         */
                   1358:        *q = ip_srcrt.dst;
                   1359: #ifdef DIAGNOSTIC
                   1360:        if (ipprintfs)
1.39      christos 1361:                printf(" %x\n", ntohl(q->s_addr));
1.1       cgd      1362: #endif
                   1363:        return (m);
                   1364: }
                   1365:
                   1366: /*
                   1367:  * Strip out IP options, at higher
                   1368:  * level protocol in the kernel.
                   1369:  * Second argument is buffer to which options
                   1370:  * will be moved, and return value is their length.
                   1371:  * XXX should be deleted; last arg currently ignored.
                   1372:  */
1.8       mycroft  1373: void
1.1       cgd      1374: ip_stripoptions(m, mopt)
1.109     augustss 1375:        struct mbuf *m;
1.1       cgd      1376:        struct mbuf *mopt;
                   1377: {
1.109     augustss 1378:        int i;
1.1       cgd      1379:        struct ip *ip = mtod(m, struct ip *);
1.109     augustss 1380:        caddr_t opts;
1.1       cgd      1381:        int olen;
                   1382:
1.79      mycroft  1383:        olen = (ip->ip_hl << 2) - sizeof (struct ip);
1.1       cgd      1384:        opts = (caddr_t)(ip + 1);
                   1385:        i = m->m_len - (sizeof (struct ip) + olen);
                   1386:        bcopy(opts  + olen, opts, (unsigned)i);
                   1387:        m->m_len -= olen;
                   1388:        if (m->m_flags & M_PKTHDR)
                   1389:                m->m_pkthdr.len -= olen;
1.79      mycroft  1390:        ip->ip_len -= olen;
                   1391:        ip->ip_hl = sizeof (struct ip) >> 2;
1.1       cgd      1392: }
                   1393:
1.23      mycroft  1394: int inetctlerrmap[PRC_NCMDS] = {
1.1       cgd      1395:        0,              0,              0,              0,
                   1396:        0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
                   1397:        EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
                   1398:        EMSGSIZE,       EHOSTUNREACH,   0,              0,
                   1399:        0,              0,              0,              0,
                   1400:        ENOPROTOOPT
                   1401: };
                   1402:
                   1403: /*
                   1404:  * Forward a packet.  If some error occurs return the sender
                   1405:  * an icmp packet.  Note we can't always generate a meaningful
                   1406:  * icmp message because icmp doesn't have a large enough repertoire
                   1407:  * of codes and types.
                   1408:  *
                   1409:  * If not forwarding, just drop the packet.  This could be confusing
                   1410:  * if ipforwarding was zero but some routing protocol was advancing
                   1411:  * us as a gateway to somewhere.  However, we must let the routing
                   1412:  * protocol deal with that.
                   1413:  *
                   1414:  * The srcrt parameter indicates whether the packet is being forwarded
                   1415:  * via a source route.
                   1416:  */
1.13      mycroft  1417: void
1.1       cgd      1418: ip_forward(m, srcrt)
                   1419:        struct mbuf *m;
                   1420:        int srcrt;
                   1421: {
1.109     augustss 1422:        struct ip *ip = mtod(m, struct ip *);
                   1423:        struct sockaddr_in *sin;
                   1424:        struct rtentry *rt;
1.28      christos 1425:        int error, type = 0, code = 0;
1.1       cgd      1426:        struct mbuf *mcopy;
1.13      mycroft  1427:        n_long dest;
                   1428:        struct ifnet *destifp;
1.89      itojun   1429: #ifdef IPSEC
                   1430:        struct ifnet dummyifp;
                   1431: #endif
1.1       cgd      1432:
1.13      mycroft  1433:        dest = 0;
1.1       cgd      1434: #ifdef DIAGNOSTIC
                   1435:        if (ipprintfs)
1.70      thorpej  1436:                printf("forward: src %2.2x dst %2.2x ttl %x\n",
                   1437:                    ntohl(ip->ip_src.s_addr),
                   1438:                    ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
1.1       cgd      1439: #endif
1.93      sommerfe 1440:        if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1.1       cgd      1441:                ipstat.ips_cantforward++;
                   1442:                m_freem(m);
                   1443:                return;
                   1444:        }
                   1445:        if (ip->ip_ttl <= IPTTLDEC) {
1.13      mycroft  1446:                icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1       cgd      1447:                return;
                   1448:        }
                   1449:        ip->ip_ttl -= IPTTLDEC;
                   1450:
1.19      mycroft  1451:        sin = satosin(&ipforward_rt.ro_dst);
1.1       cgd      1452:        if ((rt = ipforward_rt.ro_rt) == 0 ||
1.35      mycroft  1453:            !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1.1       cgd      1454:                if (ipforward_rt.ro_rt) {
                   1455:                        RTFREE(ipforward_rt.ro_rt);
                   1456:                        ipforward_rt.ro_rt = 0;
                   1457:                }
                   1458:                sin->sin_family = AF_INET;
1.35      mycroft  1459:                sin->sin_len = sizeof(struct sockaddr_in);
1.1       cgd      1460:                sin->sin_addr = ip->ip_dst;
                   1461:
                   1462:                rtalloc(&ipforward_rt);
                   1463:                if (ipforward_rt.ro_rt == 0) {
1.13      mycroft  1464:                        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1.1       cgd      1465:                        return;
                   1466:                }
                   1467:                rt = ipforward_rt.ro_rt;
                   1468:        }
                   1469:
                   1470:        /*
1.34      mycroft  1471:         * Save at most 68 bytes of the packet in case
1.1       cgd      1472:         * we need to generate an ICMP message to the src.
1.119     itojun   1473:         * Pullup to avoid sharing mbuf cluster between m and mcopy.
1.1       cgd      1474:         */
1.119     itojun   1475:        mcopy = m_copym(m, 0, imin((int)ip->ip_len, 68), M_DONTWAIT);
                   1476:        if (mcopy)
                   1477:                mcopy = m_pullup(mcopy, ip->ip_hl << 2);
1.1       cgd      1478:
                   1479:        /*
                   1480:         * If forwarding packet using same interface that it came in on,
                   1481:         * perhaps should send a redirect to sender to shortcut a hop.
                   1482:         * Only send redirect if source is sending directly to us,
                   1483:         * and if packet was not source routed (or has any options).
                   1484:         * Also, don't send redirect if forwarding using a default route
                   1485:         * or a route modified by a redirect.
                   1486:         */
                   1487:        if (rt->rt_ifp == m->m_pkthdr.rcvif &&
                   1488:            (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1.35      mycroft  1489:            !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1.1       cgd      1490:            ipsendredirects && !srcrt) {
1.19      mycroft  1491:                if (rt->rt_ifa &&
                   1492:                    (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
                   1493:                    ifatoia(rt->rt_ifa)->ia_subnet) {
1.77      thorpej  1494:                        if (rt->rt_flags & RTF_GATEWAY)
                   1495:                                dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
                   1496:                        else
                   1497:                                dest = ip->ip_dst.s_addr;
                   1498:                        /*
                   1499:                         * Router requirements says to only send host
                   1500:                         * redirects.
                   1501:                         */
                   1502:                        type = ICMP_REDIRECT;
                   1503:                        code = ICMP_REDIRECT_HOST;
1.1       cgd      1504: #ifdef DIAGNOSTIC
1.77      thorpej  1505:                        if (ipprintfs)
                   1506:                                printf("redirect (%d) to %x\n", code,
                   1507:                                    (u_int32_t)dest);
1.1       cgd      1508: #endif
                   1509:                }
                   1510:        }
                   1511:
1.89      itojun   1512: #ifdef IPSEC
1.103     itojun   1513:        /* Don't lookup socket in forwading case */
1.127     itojun   1514:        (void)ipsec_setsocket(m, NULL);
1.103     itojun   1515: #endif
1.27      thorpej  1516:        error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
                   1517:            (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1.1       cgd      1518:        if (error)
                   1519:                ipstat.ips_cantforward++;
                   1520:        else {
                   1521:                ipstat.ips_forward++;
                   1522:                if (type)
                   1523:                        ipstat.ips_redirectsent++;
                   1524:                else {
1.63      matt     1525:                        if (mcopy) {
                   1526: #ifdef GATEWAY
1.64      thorpej  1527:                                if (mcopy->m_flags & M_CANFASTFWD)
                   1528:                                        ipflow_create(&ipforward_rt, mcopy);
1.63      matt     1529: #endif
1.1       cgd      1530:                                m_freem(mcopy);
1.63      matt     1531:                        }
1.1       cgd      1532:                        return;
                   1533:                }
                   1534:        }
                   1535:        if (mcopy == NULL)
                   1536:                return;
1.13      mycroft  1537:        destifp = NULL;
                   1538:
1.1       cgd      1539:        switch (error) {
                   1540:
                   1541:        case 0:                         /* forwarded, but need redirect */
                   1542:                /* type, code set above */
                   1543:                break;
                   1544:
                   1545:        case ENETUNREACH:               /* shouldn't happen, checked above */
                   1546:        case EHOSTUNREACH:
                   1547:        case ENETDOWN:
                   1548:        case EHOSTDOWN:
                   1549:        default:
                   1550:                type = ICMP_UNREACH;
                   1551:                code = ICMP_UNREACH_HOST;
                   1552:                break;
                   1553:
                   1554:        case EMSGSIZE:
                   1555:                type = ICMP_UNREACH;
                   1556:                code = ICMP_UNREACH_NEEDFRAG;
1.89      itojun   1557: #ifndef IPSEC
1.13      mycroft  1558:                if (ipforward_rt.ro_rt)
                   1559:                        destifp = ipforward_rt.ro_rt->rt_ifp;
1.89      itojun   1560: #else
                   1561:                /*
                   1562:                 * If the packet is routed over IPsec tunnel, tell the
                   1563:                 * originator the tunnel MTU.
                   1564:                 *      tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
                   1565:                 * XXX quickhack!!!
                   1566:                 */
                   1567:                if (ipforward_rt.ro_rt) {
                   1568:                        struct secpolicy *sp;
                   1569:                        int ipsecerror;
1.95      itojun   1570:                        size_t ipsechdr;
1.89      itojun   1571:                        struct route *ro;
                   1572:
                   1573:                        sp = ipsec4_getpolicybyaddr(mcopy,
1.95      itojun   1574:                                                    IPSEC_DIR_OUTBOUND,
                   1575:                                                    IP_FORWARDING,
                   1576:                                                    &ipsecerror);
1.89      itojun   1577:
                   1578:                        if (sp == NULL)
                   1579:                                destifp = ipforward_rt.ro_rt->rt_ifp;
                   1580:                        else {
                   1581:                                /* count IPsec header size */
1.95      itojun   1582:                                ipsechdr = ipsec4_hdrsiz(mcopy,
                   1583:                                                         IPSEC_DIR_OUTBOUND,
                   1584:                                                         NULL);
1.89      itojun   1585:
                   1586:                                /*
                   1587:                                 * find the correct route for outer IPv4
                   1588:                                 * header, compute tunnel MTU.
                   1589:                                 *
                   1590:                                 * XXX BUG ALERT
                   1591:                                 * The "dummyifp" code relies upon the fact
                   1592:                                 * that icmp_error() touches only ifp->if_mtu.
                   1593:                                 */
                   1594:                                /*XXX*/
                   1595:                                destifp = NULL;
                   1596:                                if (sp->req != NULL
1.95      itojun   1597:                                 && sp->req->sav != NULL
                   1598:                                 && sp->req->sav->sah != NULL) {
                   1599:                                        ro = &sp->req->sav->sah->sa_route;
1.89      itojun   1600:                                        if (ro->ro_rt && ro->ro_rt->rt_ifp) {
                   1601:                                                dummyifp.if_mtu =
                   1602:                                                    ro->ro_rt->rt_ifp->if_mtu;
                   1603:                                                dummyifp.if_mtu -= ipsechdr;
                   1604:                                                destifp = &dummyifp;
                   1605:                                        }
                   1606:                                }
                   1607:
                   1608:                                key_freesp(sp);
                   1609:                        }
                   1610:                }
                   1611: #endif /*IPSEC*/
1.1       cgd      1612:                ipstat.ips_cantfrag++;
                   1613:                break;
                   1614:
                   1615:        case ENOBUFS:
                   1616:                type = ICMP_SOURCEQUENCH;
                   1617:                code = 0;
                   1618:                break;
                   1619:        }
1.13      mycroft  1620:        icmp_error(mcopy, type, code, dest, destifp);
1.44      thorpej  1621: }
                   1622:
                   1623: void
                   1624: ip_savecontrol(inp, mp, ip, m)
1.109     augustss 1625:        struct inpcb *inp;
                   1626:        struct mbuf **mp;
                   1627:        struct ip *ip;
                   1628:        struct mbuf *m;
1.44      thorpej  1629: {
                   1630:
                   1631:        if (inp->inp_socket->so_options & SO_TIMESTAMP) {
                   1632:                struct timeval tv;
                   1633:
                   1634:                microtime(&tv);
                   1635:                *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
                   1636:                    SCM_TIMESTAMP, SOL_SOCKET);
                   1637:                if (*mp)
                   1638:                        mp = &(*mp)->m_next;
                   1639:        }
                   1640:        if (inp->inp_flags & INP_RECVDSTADDR) {
                   1641:                *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
                   1642:                    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
                   1643:                if (*mp)
                   1644:                        mp = &(*mp)->m_next;
                   1645:        }
                   1646: #ifdef notyet
                   1647:        /*
                   1648:         * XXX
                   1649:         * Moving these out of udp_input() made them even more broken
                   1650:         * than they already were.
                   1651:         *      - fenner@parc.xerox.com
                   1652:         */
                   1653:        /* options were tossed already */
                   1654:        if (inp->inp_flags & INP_RECVOPTS) {
                   1655:                *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
                   1656:                    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
                   1657:                if (*mp)
                   1658:                        mp = &(*mp)->m_next;
                   1659:        }
                   1660:        /* ip_srcroute doesn't do what we want here, need to fix */
                   1661:        if (inp->inp_flags & INP_RECVRETOPTS) {
                   1662:                *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
                   1663:                    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
                   1664:                if (*mp)
                   1665:                        mp = &(*mp)->m_next;
                   1666:        }
                   1667: #endif
                   1668:        if (inp->inp_flags & INP_RECVIF) {
                   1669:                struct sockaddr_dl sdl;
                   1670:
                   1671:                sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
                   1672:                sdl.sdl_family = AF_LINK;
                   1673:                sdl.sdl_index = m->m_pkthdr.rcvif ?
                   1674:                    m->m_pkthdr.rcvif->if_index : 0;
                   1675:                sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
                   1676:                *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
                   1677:                    IP_RECVIF, IPPROTO_IP);
                   1678:                if (*mp)
                   1679:                        mp = &(*mp)->m_next;
                   1680:        }
1.13      mycroft  1681: }
                   1682:
                   1683: int
                   1684: ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                   1685:        int *name;
                   1686:        u_int namelen;
                   1687:        void *oldp;
                   1688:        size_t *oldlenp;
                   1689:        void *newp;
                   1690:        size_t newlen;
                   1691: {
1.88      sommerfe 1692:        extern int subnetsarelocal, hostzeroisbroadcast;
1.52      thorpej  1693:
1.54      lukem    1694:        int error, old;
                   1695:
1.13      mycroft  1696:        /* All sysctl names at this level are terminal. */
                   1697:        if (namelen != 1)
                   1698:                return (ENOTDIR);
                   1699:
                   1700:        switch (name[0]) {
                   1701:        case IPCTL_FORWARDING:
                   1702:                return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
                   1703:        case IPCTL_SENDREDIRECTS:
                   1704:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1705:                        &ipsendredirects));
                   1706:        case IPCTL_DEFTTL:
                   1707:                return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
                   1708: #ifdef notyet
                   1709:        case IPCTL_DEFMTU:
                   1710:                return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
                   1711: #endif
1.26      thorpej  1712:        case IPCTL_FORWSRCRT:
1.47      cjs      1713:                /* Don't allow this to change in a secure environment.  */
1.26      thorpej  1714:                if (securelevel > 0)
1.46      cjs      1715:                        return (sysctl_rdint(oldp, oldlenp, newp,
                   1716:                            ip_forwsrcrt));
                   1717:                else
                   1718:                        return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1719:                            &ip_forwsrcrt));
1.27      thorpej  1720:        case IPCTL_DIRECTEDBCAST:
                   1721:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1722:                    &ip_directedbcast));
1.47      cjs      1723:        case IPCTL_ALLOWSRCRT:
                   1724:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1725:                    &ip_allowsrcrt));
1.52      thorpej  1726:        case IPCTL_SUBNETSARELOCAL:
                   1727:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1728:                    &subnetsarelocal));
1.53      kml      1729:        case IPCTL_MTUDISC:
1.60      kml      1730:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1731:                    &ip_mtudisc);
                   1732:                if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
                   1733:                        ip_mtudisc_timeout_q =
                   1734:                            rt_timer_queue_create(ip_mtudisc_timeout);
                   1735:                } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
                   1736:                        rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE);
                   1737:                        ip_mtudisc_timeout_q = NULL;
                   1738:                }
                   1739:                return error;
1.54      lukem    1740:        case IPCTL_ANONPORTMIN:
                   1741:                old = anonportmin;
                   1742:                error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
1.118     itojun   1743:                if (anonportmin >= anonportmax || anonportmin < 0
                   1744:                    || anonportmin > 65535
1.54      lukem    1745: #ifndef IPNOPRIVPORTS
                   1746:                    || anonportmin < IPPORT_RESERVED
                   1747: #endif
                   1748:                    ) {
                   1749:                        anonportmin = old;
                   1750:                        return (EINVAL);
                   1751:                }
                   1752:                return (error);
                   1753:        case IPCTL_ANONPORTMAX:
                   1754:                old = anonportmax;
                   1755:                error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
1.118     itojun   1756:                if (anonportmin >= anonportmax || anonportmax < 0
                   1757:                    || anonportmax > 65535
1.54      lukem    1758: #ifndef IPNOPRIVPORTS
                   1759:                    || anonportmax < IPPORT_RESERVED
                   1760: #endif
                   1761:                    ) {
                   1762:                        anonportmax = old;
                   1763:                        return (EINVAL);
                   1764:                }
1.60      kml      1765:                return (error);
                   1766:        case IPCTL_MTUDISCTIMEOUT:
                   1767:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1768:                   &ip_mtudisc_timeout);
                   1769:                if (ip_mtudisc_timeout_q != NULL)
                   1770:                        rt_timer_queue_change(ip_mtudisc_timeout_q,
                   1771:                                              ip_mtudisc_timeout);
1.54      lukem    1772:                return (error);
1.65      matt     1773: #ifdef GATEWAY
                   1774:        case IPCTL_MAXFLOWS:
1.67      thorpej  1775:            {
                   1776:                int s;
                   1777:
1.65      matt     1778:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1779:                   &ip_maxflows);
1.67      thorpej  1780:                s = splsoftnet();
1.65      matt     1781:                ipflow_reap(0);
1.67      thorpej  1782:                splx(s);
1.65      matt     1783:                return (error);
1.67      thorpej  1784:            }
1.89      itojun   1785: #endif
1.90      itojun   1786:        case IPCTL_HOSTZEROBROADCAST:
                   1787:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1788:                    &hostzeroisbroadcast));
1.89      itojun   1789: #if NGIF > 0
                   1790:        case IPCTL_GIF_TTL:
                   1791:                return(sysctl_int(oldp, oldlenp, newp, newlen,
1.90      itojun   1792:                                  &ip_gif_ttl));
1.117     tron     1793: #endif
                   1794:
                   1795: #ifndef IPNOPRIVPORTS
                   1796:        case IPCTL_LOWPORTMIN:
                   1797:                old = lowportmin;
                   1798:                error = sysctl_int(oldp, oldlenp, newp, newlen, &lowportmin);
                   1799:                if (lowportmin >= lowportmax
                   1800:                    || lowportmin > IPPORT_RESERVEDMAX
                   1801:                    || lowportmin < IPPORT_RESERVEDMIN
                   1802:                    ) {
                   1803:                        lowportmin = old;
                   1804:                        return (EINVAL);
                   1805:                }
                   1806:                return (error);
                   1807:        case IPCTL_LOWPORTMAX:
                   1808:                old = lowportmax;
                   1809:                error = sysctl_int(oldp, oldlenp, newp, newlen, &lowportmax);
                   1810:                if (lowportmin >= lowportmax
                   1811:                    || lowportmax > IPPORT_RESERVEDMAX
                   1812:                    || lowportmax < IPPORT_RESERVEDMIN
                   1813:                    ) {
                   1814:                        lowportmax = old;
                   1815:                        return (EINVAL);
                   1816:                }
                   1817:                return (error);
1.65      matt     1818: #endif
1.131   ! itojun   1819:
        !          1820:        case IPCTL_MAXFRAGPACKETS:
        !          1821:                return (sysctl_int(oldp, oldlenp, newp, newlen,
        !          1822:                    &ip_maxfragpackets));
1.88      sommerfe 1823:
1.13      mycroft  1824:        default:
                   1825:                return (EOPNOTSUPP);
                   1826:        }
                   1827:        /* NOTREACHED */
1.1       cgd      1828: }

CVSweb <webmaster@jp.NetBSD.org>