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

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

CVSweb <webmaster@jp.NetBSD.org>