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

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

CVSweb <webmaster@jp.NetBSD.org>