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

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

CVSweb <webmaster@jp.NetBSD.org>