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

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

CVSweb <webmaster@jp.NetBSD.org>