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

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

CVSweb <webmaster@jp.NetBSD.org>