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

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

CVSweb <webmaster@jp.NetBSD.org>