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

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

CVSweb <webmaster@jp.NetBSD.org>