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

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

CVSweb <webmaster@jp.NetBSD.org>