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

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

CVSweb <webmaster@jp.NetBSD.org>