[BACK]Return to ip_input.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet

Annotation of src/sys/netinet/ip_input.c, Revision 1.114.4.9

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

CVSweb <webmaster@jp.NetBSD.org>