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

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

CVSweb <webmaster@jp.NetBSD.org>