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

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

CVSweb <webmaster@jp.NetBSD.org>