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

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

CVSweb <webmaster@jp.NetBSD.org>