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

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

CVSweb <webmaster@jp.NetBSD.org>