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

1.73    ! thorpej     1: /*     $NetBSD: ip_input.c,v 1.72 1998/10/08 01:19:25 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.57      tls       362:        INADDR_TO_IA(ip->ip_dst, ia);
                    363:        if (ia != NULL) goto ours;
                    364:        if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
                    365:                for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
                    366:                    ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
                    367:                        if (ifa->ifa_addr->sa_family != AF_INET) continue;
                    368:                        ia = ifatoia(ifa);
1.35      mycroft   369:                        if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
                    370:                            in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
1.20      mycroft   371:                            /*
                    372:                             * Look for all-0's host part (old broadcast addr),
                    373:                             * either for subnet or net.
                    374:                             */
                    375:                            ip->ip_dst.s_addr == ia->ia_subnet ||
1.18      mycroft   376:                            ip->ip_dst.s_addr == ia->ia_net)
1.1       cgd       377:                                goto ours;
1.57      tls       378:                        /*
                    379:                         * An interface with IP address zero accepts
                    380:                         * all packets that arrive on that interface.
                    381:                         */
                    382:                        if (in_nullhost(ia->ia_addr.sin_addr))
                    383:                                goto ours;
1.1       cgd       384:                }
                    385:        }
1.18      mycroft   386:        if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4       hpeyerl   387:                struct in_multi *inm;
                    388: #ifdef MROUTING
                    389:                extern struct socket *ip_mrouter;
1.10      brezak    390:
                    391:                if (m->m_flags & M_EXT) {
                    392:                        if ((m = m_pullup(m, hlen)) == 0) {
                    393:                                ipstat.ips_toosmall++;
                    394:                                goto next;
                    395:                        }
                    396:                        ip = mtod(m, struct ip *);
                    397:                }
1.4       hpeyerl   398:
                    399:                if (ip_mrouter) {
                    400:                        /*
                    401:                         * If we are acting as a multicast router, all
                    402:                         * incoming multicast packets are passed to the
                    403:                         * kernel-level multicast forwarding function.
                    404:                         * The packet is returned (relatively) intact; if
                    405:                         * ip_mforward() returns a non-zero value, the packet
                    406:                         * must be discarded, else it may be accepted below.
                    407:                         *
                    408:                         * (The IP ident field is put in the same byte order
                    409:                         * as expected when ip_mforward() is called from
                    410:                         * ip_output().)
                    411:                         */
                    412:                        ip->ip_id = htons(ip->ip_id);
1.13      mycroft   413:                        if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
                    414:                                ipstat.ips_cantforward++;
1.4       hpeyerl   415:                                m_freem(m);
                    416:                                goto next;
                    417:                        }
                    418:                        ip->ip_id = ntohs(ip->ip_id);
                    419:
                    420:                        /*
                    421:                         * The process-level routing demon needs to receive
                    422:                         * all multicast IGMP packets, whether or not this
                    423:                         * host belongs to their destination groups.
                    424:                         */
                    425:                        if (ip->ip_p == IPPROTO_IGMP)
                    426:                                goto ours;
1.13      mycroft   427:                        ipstat.ips_forward++;
1.4       hpeyerl   428:                }
                    429: #endif
                    430:                /*
                    431:                 * See if we belong to the destination multicast group on the
                    432:                 * arrival interface.
                    433:                 */
                    434:                IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
                    435:                if (inm == NULL) {
1.13      mycroft   436:                        ipstat.ips_cantforward++;
1.4       hpeyerl   437:                        m_freem(m);
                    438:                        goto next;
                    439:                }
                    440:                goto ours;
                    441:        }
1.19      mycroft   442:        if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
1.35      mycroft   443:            in_nullhost(ip->ip_dst))
1.1       cgd       444:                goto ours;
                    445:
                    446:        /*
                    447:         * Not for us; forward if possible and desirable.
                    448:         */
                    449:        if (ipforwarding == 0) {
                    450:                ipstat.ips_cantforward++;
                    451:                m_freem(m);
                    452:        } else
                    453:                ip_forward(m, 0);
                    454:        goto next;
                    455:
                    456: ours:
                    457:        /*
                    458:         * If offset or IP_MF are set, must reassemble.
                    459:         * Otherwise, nothing need be done.
                    460:         * (We could look in the reassembly queue to see
                    461:         * if the packet was previously fragmented,
                    462:         * but it's not worth the time; just let them time out.)
                    463:         */
1.37      perry     464:        if (ip->ip_off & ~(IP_DF|IP_RF)) {
1.1       cgd       465:                /*
                    466:                 * Look for queue of fragments
                    467:                 * of this datagram.
                    468:                 */
1.25      cgd       469:                for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
1.1       cgd       470:                        if (ip->ip_id == fp->ipq_id &&
1.35      mycroft   471:                            in_hosteq(ip->ip_src, fp->ipq_src) &&
                    472:                            in_hosteq(ip->ip_dst, fp->ipq_dst) &&
1.1       cgd       473:                            ip->ip_p == fp->ipq_p)
                    474:                                goto found;
                    475:                fp = 0;
                    476: found:
                    477:
                    478:                /*
                    479:                 * Adjust ip_len to not reflect header,
1.25      cgd       480:                 * set ipqe_mff if more fragments are expected,
1.1       cgd       481:                 * convert offset of this to bytes.
                    482:                 */
                    483:                ip->ip_len -= hlen;
1.25      cgd       484:                mff = (ip->ip_off & IP_MF) != 0;
                    485:                if (mff) {
1.16      cgd       486:                        /*
                    487:                         * Make sure that fragments have a data length
                    488:                         * that's a non-zero multiple of 8 bytes.
                    489:                         */
1.17      cgd       490:                        if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
1.16      cgd       491:                                ipstat.ips_badfrags++;
                    492:                                goto bad;
                    493:                        }
                    494:                }
1.1       cgd       495:                ip->ip_off <<= 3;
                    496:
                    497:                /*
                    498:                 * If datagram marked as having more fragments
                    499:                 * or if this is not the first fragment,
                    500:                 * attempt reassembly; if it succeeds, proceed.
                    501:                 */
1.25      cgd       502:                if (mff || ip->ip_off) {
1.1       cgd       503:                        ipstat.ips_fragments++;
1.72      thorpej   504:                        ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
1.25      cgd       505:                        if (ipqe == NULL) {
                    506:                                ipstat.ips_rcvmemdrop++;
                    507:                                goto bad;
                    508:                        }
                    509:                        ipqe->ipqe_mff = mff;
1.50      thorpej   510:                        ipqe->ipqe_m = m;
1.25      cgd       511:                        ipqe->ipqe_ip = ip;
1.50      thorpej   512:                        m = ip_reass(ipqe, fp);
                    513:                        if (m == 0)
1.1       cgd       514:                                goto next;
1.13      mycroft   515:                        ipstat.ips_reassembled++;
1.50      thorpej   516:                        ip = mtod(m, struct ip *);
1.1       cgd       517:                } else
                    518:                        if (fp)
                    519:                                ip_freef(fp);
                    520:        } else
                    521:                ip->ip_len -= hlen;
                    522:
                    523:        /*
                    524:         * Switch out to protocol's input routine.
                    525:         */
                    526:        ipstat.ips_delivered++;
                    527:        (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
                    528:        goto next;
                    529: bad:
                    530:        m_freem(m);
                    531:        goto next;
                    532: }
                    533:
                    534: /*
                    535:  * Take incoming datagram fragment and try to
                    536:  * reassemble it into whole datagram.  If a chain for
                    537:  * reassembly of this datagram already exists, then it
                    538:  * is given as fp; otherwise have to make a chain.
                    539:  */
1.50      thorpej   540: struct mbuf *
1.25      cgd       541: ip_reass(ipqe, fp)
                    542:        register struct ipqent *ipqe;
1.1       cgd       543:        register struct ipq *fp;
                    544: {
1.50      thorpej   545:        register struct mbuf *m = ipqe->ipqe_m;
1.25      cgd       546:        register struct ipqent *nq, *p, *q;
                    547:        struct ip *ip;
1.1       cgd       548:        struct mbuf *t;
1.25      cgd       549:        int hlen = ipqe->ipqe_ip->ip_hl << 2;
1.1       cgd       550:        int i, next;
                    551:
                    552:        /*
                    553:         * Presence of header sizes in mbufs
                    554:         * would confuse code below.
                    555:         */
                    556:        m->m_data += hlen;
                    557:        m->m_len -= hlen;
                    558:
                    559:        /*
                    560:         * If first fragment to arrive, create a reassembly queue.
                    561:         */
                    562:        if (fp == 0) {
1.50      thorpej   563:                MALLOC(fp, struct ipq *, sizeof (struct ipq),
                    564:                    M_FTABLE, M_NOWAIT);
                    565:                if (fp == NULL)
1.1       cgd       566:                        goto dropfrag;
1.25      cgd       567:                LIST_INSERT_HEAD(&ipq, fp, ipq_q);
1.1       cgd       568:                fp->ipq_ttl = IPFRAGTTL;
1.25      cgd       569:                fp->ipq_p = ipqe->ipqe_ip->ip_p;
                    570:                fp->ipq_id = ipqe->ipqe_ip->ip_id;
                    571:                LIST_INIT(&fp->ipq_fragq);
                    572:                fp->ipq_src = ipqe->ipqe_ip->ip_src;
                    573:                fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
                    574:                p = NULL;
1.1       cgd       575:                goto insert;
                    576:        }
                    577:
                    578:        /*
                    579:         * Find a segment which begins after this one does.
                    580:         */
1.25      cgd       581:        for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
                    582:            p = q, q = q->ipqe_q.le_next)
                    583:                if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
1.1       cgd       584:                        break;
                    585:
                    586:        /*
                    587:         * If there is a preceding segment, it may provide some of
                    588:         * our data already.  If so, drop the data from the incoming
                    589:         * segment.  If it provides all of our data, drop us.
                    590:         */
1.25      cgd       591:        if (p != NULL) {
                    592:                i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
                    593:                    ipqe->ipqe_ip->ip_off;
1.1       cgd       594:                if (i > 0) {
1.25      cgd       595:                        if (i >= ipqe->ipqe_ip->ip_len)
1.1       cgd       596:                                goto dropfrag;
1.50      thorpej   597:                        m_adj(ipqe->ipqe_m, i);
1.25      cgd       598:                        ipqe->ipqe_ip->ip_off += i;
                    599:                        ipqe->ipqe_ip->ip_len -= i;
1.1       cgd       600:                }
                    601:        }
                    602:
                    603:        /*
                    604:         * While we overlap succeeding segments trim them or,
                    605:         * if they are completely covered, dequeue them.
                    606:         */
1.25      cgd       607:        for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
                    608:            q->ipqe_ip->ip_off; q = nq) {
                    609:                i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
                    610:                    q->ipqe_ip->ip_off;
                    611:                if (i < q->ipqe_ip->ip_len) {
                    612:                        q->ipqe_ip->ip_len -= i;
                    613:                        q->ipqe_ip->ip_off += i;
1.50      thorpej   614:                        m_adj(q->ipqe_m, i);
1.1       cgd       615:                        break;
                    616:                }
1.25      cgd       617:                nq = q->ipqe_q.le_next;
1.50      thorpej   618:                m_freem(q->ipqe_m);
1.25      cgd       619:                LIST_REMOVE(q, ipqe_q);
1.72      thorpej   620:                pool_put(&ipqent_pool, q);
1.1       cgd       621:        }
                    622:
                    623: insert:
                    624:        /*
                    625:         * Stick new segment in its place;
                    626:         * check for complete reassembly.
                    627:         */
1.25      cgd       628:        if (p == NULL) {
                    629:                LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
                    630:        } else {
                    631:                LIST_INSERT_AFTER(p, ipqe, ipqe_q);
                    632:        }
1.1       cgd       633:        next = 0;
1.25      cgd       634:        for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
                    635:            p = q, q = q->ipqe_q.le_next) {
                    636:                if (q->ipqe_ip->ip_off != next)
1.1       cgd       637:                        return (0);
1.25      cgd       638:                next += q->ipqe_ip->ip_len;
1.1       cgd       639:        }
1.25      cgd       640:        if (p->ipqe_mff)
1.1       cgd       641:                return (0);
                    642:
                    643:        /*
1.41      thorpej   644:         * Reassembly is complete.  Check for a bogus message size and
                    645:         * concatenate fragments.
1.1       cgd       646:         */
1.25      cgd       647:        q = fp->ipq_fragq.lh_first;
                    648:        ip = q->ipqe_ip;
1.41      thorpej   649:        if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
                    650:                ipstat.ips_toolong++;
                    651:                ip_freef(fp);
                    652:                return (0);
                    653:        }
1.50      thorpej   654:        m = q->ipqe_m;
1.1       cgd       655:        t = m->m_next;
                    656:        m->m_next = 0;
                    657:        m_cat(m, t);
1.25      cgd       658:        nq = q->ipqe_q.le_next;
1.72      thorpej   659:        pool_put(&ipqent_pool, q);
1.25      cgd       660:        for (q = nq; q != NULL; q = nq) {
1.50      thorpej   661:                t = q->ipqe_m;
1.25      cgd       662:                nq = q->ipqe_q.le_next;
1.72      thorpej   663:                pool_put(&ipqent_pool, q);
1.1       cgd       664:                m_cat(m, t);
                    665:        }
                    666:
                    667:        /*
                    668:         * Create header for new ip packet by
                    669:         * modifying header of first packet;
                    670:         * dequeue and discard fragment reassembly header.
                    671:         * Make header visible.
                    672:         */
                    673:        ip->ip_len = next;
1.25      cgd       674:        ip->ip_src = fp->ipq_src;
                    675:        ip->ip_dst = fp->ipq_dst;
                    676:        LIST_REMOVE(fp, ipq_q);
1.50      thorpej   677:        FREE(fp, M_FTABLE);
1.1       cgd       678:        m->m_len += (ip->ip_hl << 2);
                    679:        m->m_data -= (ip->ip_hl << 2);
                    680:        /* some debugging cruft by sklower, below, will go away soon */
                    681:        if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
                    682:                register int plen = 0;
1.50      thorpej   683:                for (t = m; t; t = t->m_next)
                    684:                        plen += t->m_len;
                    685:                m->m_pkthdr.len = plen;
1.1       cgd       686:        }
1.50      thorpej   687:        return (m);
1.1       cgd       688:
                    689: dropfrag:
                    690:        ipstat.ips_fragdropped++;
                    691:        m_freem(m);
1.72      thorpej   692:        pool_put(&ipqent_pool, ipqe);
1.1       cgd       693:        return (0);
                    694: }
                    695:
                    696: /*
                    697:  * Free a fragment reassembly header and all
                    698:  * associated datagrams.
                    699:  */
1.8       mycroft   700: void
1.1       cgd       701: ip_freef(fp)
                    702:        struct ipq *fp;
                    703: {
1.25      cgd       704:        register struct ipqent *q, *p;
1.1       cgd       705:
1.25      cgd       706:        for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
                    707:                p = q->ipqe_q.le_next;
1.50      thorpej   708:                m_freem(q->ipqe_m);
1.25      cgd       709:                LIST_REMOVE(q, ipqe_q);
1.72      thorpej   710:                pool_put(&ipqent_pool, q);
1.1       cgd       711:        }
1.25      cgd       712:        LIST_REMOVE(fp, ipq_q);
1.50      thorpej   713:        FREE(fp, M_FTABLE);
1.1       cgd       714: }
                    715:
                    716: /*
                    717:  * IP timer processing;
                    718:  * if a timer expires on a reassembly
                    719:  * queue, discard it.
                    720:  */
1.8       mycroft   721: void
1.1       cgd       722: ip_slowtimo()
                    723: {
1.25      cgd       724:        register struct ipq *fp, *nfp;
1.24      mycroft   725:        int s = splsoftnet();
1.1       cgd       726:
1.25      cgd       727:        for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
                    728:                nfp = fp->ipq_q.le_next;
                    729:                if (--fp->ipq_ttl == 0) {
1.1       cgd       730:                        ipstat.ips_fragtimeout++;
1.25      cgd       731:                        ip_freef(fp);
1.1       cgd       732:                }
                    733:        }
1.63      matt      734: #ifdef GATEWAY
                    735:        ipflow_slowtimo();
                    736: #endif
1.1       cgd       737:        splx(s);
                    738: }
                    739:
                    740: /*
                    741:  * Drain off all datagram fragments.
                    742:  */
1.8       mycroft   743: void
1.1       cgd       744: ip_drain()
                    745: {
                    746:
1.25      cgd       747:        while (ipq.lh_first != NULL) {
1.1       cgd       748:                ipstat.ips_fragdropped++;
1.25      cgd       749:                ip_freef(ipq.lh_first);
1.1       cgd       750:        }
                    751: }
                    752:
                    753: /*
                    754:  * Do option processing on a datagram,
                    755:  * possibly discarding it if bad options are encountered,
                    756:  * or forwarding it if source-routed.
                    757:  * Returns 1 if packet has been forwarded/freed,
                    758:  * 0 if the packet should be processed further.
                    759:  */
1.8       mycroft   760: int
1.1       cgd       761: ip_dooptions(m)
                    762:        struct mbuf *m;
                    763: {
                    764:        register struct ip *ip = mtod(m, struct ip *);
                    765:        register u_char *cp;
                    766:        register struct ip_timestamp *ipt;
                    767:        register struct in_ifaddr *ia;
                    768:        int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1.13      mycroft   769:        struct in_addr *sin, dst;
1.1       cgd       770:        n_time ntime;
                    771:
1.13      mycroft   772:        dst = ip->ip_dst;
1.1       cgd       773:        cp = (u_char *)(ip + 1);
                    774:        cnt = (ip->ip_hl << 2) - sizeof (struct ip);
                    775:        for (; cnt > 0; cnt -= optlen, cp += optlen) {
                    776:                opt = cp[IPOPT_OPTVAL];
                    777:                if (opt == IPOPT_EOL)
                    778:                        break;
                    779:                if (opt == IPOPT_NOP)
                    780:                        optlen = 1;
                    781:                else {
                    782:                        optlen = cp[IPOPT_OLEN];
                    783:                        if (optlen <= 0 || optlen > cnt) {
                    784:                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
                    785:                                goto bad;
                    786:                        }
                    787:                }
                    788:                switch (opt) {
                    789:
                    790:                default:
                    791:                        break;
                    792:
                    793:                /*
                    794:                 * Source routing with record.
                    795:                 * Find interface with current destination address.
                    796:                 * If none on this machine then drop if strictly routed,
                    797:                 * or do nothing if loosely routed.
                    798:                 * Record interface address and bring up next address
                    799:                 * component.  If strictly routed make sure next
                    800:                 * address is on directly accessible net.
                    801:                 */
                    802:                case IPOPT_LSRR:
                    803:                case IPOPT_SSRR:
1.47      cjs       804:                        if (ip_allowsrcrt == 0) {
                    805:                                type = ICMP_UNREACH;
                    806:                                code = ICMP_UNREACH_NET_PROHIB;
                    807:                                goto bad;
                    808:                        }
1.1       cgd       809:                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
                    810:                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
                    811:                                goto bad;
                    812:                        }
                    813:                        ipaddr.sin_addr = ip->ip_dst;
1.19      mycroft   814:                        ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1.1       cgd       815:                        if (ia == 0) {
                    816:                                if (opt == IPOPT_SSRR) {
                    817:                                        type = ICMP_UNREACH;
                    818:                                        code = ICMP_UNREACH_SRCFAIL;
                    819:                                        goto bad;
                    820:                                }
                    821:                                /*
                    822:                                 * Loose routing, and not at next destination
                    823:                                 * yet; nothing to do except forward.
                    824:                                 */
                    825:                                break;
                    826:                        }
                    827:                        off--;                  /* 0 origin */
                    828:                        if (off > optlen - sizeof(struct in_addr)) {
                    829:                                /*
                    830:                                 * End of source route.  Should be for us.
                    831:                                 */
                    832:                                save_rte(cp, ip->ip_src);
                    833:                                break;
                    834:                        }
                    835:                        /*
                    836:                         * locate outgoing interface
                    837:                         */
                    838:                        bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
                    839:                            sizeof(ipaddr.sin_addr));
                    840:                        if (opt == IPOPT_SSRR) {
                    841: #define        INA     struct in_ifaddr *
                    842: #define        SA      struct sockaddr *
1.29      mrg       843:                            ia = (INA)ifa_ifwithladdr((SA)&ipaddr);
1.1       cgd       844:                        } else
                    845:                                ia = ip_rtaddr(ipaddr.sin_addr);
                    846:                        if (ia == 0) {
                    847:                                type = ICMP_UNREACH;
                    848:                                code = ICMP_UNREACH_SRCFAIL;
                    849:                                goto bad;
                    850:                        }
                    851:                        ip->ip_dst = ipaddr.sin_addr;
1.20      mycroft   852:                        bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1       cgd       853:                            (caddr_t)(cp + off), sizeof(struct in_addr));
                    854:                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13      mycroft   855:                        /*
                    856:                         * Let ip_intr's mcast routing check handle mcast pkts
                    857:                         */
1.18      mycroft   858:                        forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1       cgd       859:                        break;
                    860:
                    861:                case IPOPT_RR:
                    862:                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
                    863:                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
                    864:                                goto bad;
                    865:                        }
                    866:                        /*
                    867:                         * If no space remains, ignore.
                    868:                         */
                    869:                        off--;                  /* 0 origin */
                    870:                        if (off > optlen - sizeof(struct in_addr))
                    871:                                break;
                    872:                        bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
                    873:                            sizeof(ipaddr.sin_addr));
                    874:                        /*
                    875:                         * locate outgoing interface; if we're the destination,
                    876:                         * use the incoming interface (should be same).
                    877:                         */
                    878:                        if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
                    879:                            (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
                    880:                                type = ICMP_UNREACH;
                    881:                                code = ICMP_UNREACH_HOST;
                    882:                                goto bad;
                    883:                        }
1.20      mycroft   884:                        bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1       cgd       885:                            (caddr_t)(cp + off), sizeof(struct in_addr));
                    886:                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
                    887:                        break;
                    888:
                    889:                case IPOPT_TS:
                    890:                        code = cp - (u_char *)ip;
                    891:                        ipt = (struct ip_timestamp *)cp;
                    892:                        if (ipt->ipt_len < 5)
                    893:                                goto bad;
1.15      cgd       894:                        if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.1       cgd       895:                                if (++ipt->ipt_oflw == 0)
                    896:                                        goto bad;
                    897:                                break;
                    898:                        }
                    899:                        sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
                    900:                        switch (ipt->ipt_flg) {
                    901:
                    902:                        case IPOPT_TS_TSONLY:
                    903:                                break;
                    904:
                    905:                        case IPOPT_TS_TSANDADDR:
1.66      thorpej   906:                                if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.1       cgd       907:                                    sizeof(struct in_addr) > ipt->ipt_len)
                    908:                                        goto bad;
1.13      mycroft   909:                                ipaddr.sin_addr = dst;
                    910:                                ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
                    911:                                                            m->m_pkthdr.rcvif);
                    912:                                if (ia == 0)
                    913:                                        continue;
1.20      mycroft   914:                                bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1       cgd       915:                                    (caddr_t)sin, sizeof(struct in_addr));
                    916:                                ipt->ipt_ptr += sizeof(struct in_addr);
                    917:                                break;
                    918:
                    919:                        case IPOPT_TS_PRESPEC:
1.66      thorpej   920:                                if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1.1       cgd       921:                                    sizeof(struct in_addr) > ipt->ipt_len)
                    922:                                        goto bad;
                    923:                                bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
                    924:                                    sizeof(struct in_addr));
                    925:                                if (ifa_ifwithaddr((SA)&ipaddr) == 0)
                    926:                                        continue;
                    927:                                ipt->ipt_ptr += sizeof(struct in_addr);
                    928:                                break;
                    929:
                    930:                        default:
                    931:                                goto bad;
                    932:                        }
                    933:                        ntime = iptime();
                    934:                        bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
                    935:                            sizeof(n_time));
                    936:                        ipt->ipt_ptr += sizeof(n_time);
                    937:                }
                    938:        }
                    939:        if (forward) {
1.26      thorpej   940:                if (ip_forwsrcrt == 0) {
                    941:                        type = ICMP_UNREACH;
                    942:                        code = ICMP_UNREACH_SRCFAIL;
                    943:                        goto bad;
                    944:                }
1.1       cgd       945:                ip_forward(m, 1);
                    946:                return (1);
1.13      mycroft   947:        }
                    948:        return (0);
1.1       cgd       949: bad:
1.13      mycroft   950:        ip->ip_len -= ip->ip_hl << 2;   /* XXX icmp_error adds in hdr length */
                    951:        icmp_error(m, type, code, 0, 0);
                    952:        ipstat.ips_badoptions++;
1.1       cgd       953:        return (1);
                    954: }
                    955:
                    956: /*
                    957:  * Given address of next destination (final or next hop),
                    958:  * return internet address info of interface to be used to get there.
                    959:  */
                    960: struct in_ifaddr *
                    961: ip_rtaddr(dst)
                    962:         struct in_addr dst;
                    963: {
                    964:        register struct sockaddr_in *sin;
                    965:
1.19      mycroft   966:        sin = satosin(&ipforward_rt.ro_dst);
1.1       cgd       967:
1.35      mycroft   968:        if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
1.1       cgd       969:                if (ipforward_rt.ro_rt) {
                    970:                        RTFREE(ipforward_rt.ro_rt);
                    971:                        ipforward_rt.ro_rt = 0;
                    972:                }
                    973:                sin->sin_family = AF_INET;
                    974:                sin->sin_len = sizeof(*sin);
                    975:                sin->sin_addr = dst;
                    976:
                    977:                rtalloc(&ipforward_rt);
                    978:        }
                    979:        if (ipforward_rt.ro_rt == 0)
                    980:                return ((struct in_ifaddr *)0);
1.19      mycroft   981:        return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1.1       cgd       982: }
                    983:
                    984: /*
                    985:  * Save incoming source route for use in replies,
                    986:  * to be picked up later by ip_srcroute if the receiver is interested.
                    987:  */
1.13      mycroft   988: void
1.1       cgd       989: save_rte(option, dst)
                    990:        u_char *option;
                    991:        struct in_addr dst;
                    992: {
                    993:        unsigned olen;
                    994:
                    995:        olen = option[IPOPT_OLEN];
                    996: #ifdef DIAGNOSTIC
                    997:        if (ipprintfs)
1.39      christos  998:                printf("save_rte: olen %d\n", olen);
1.1       cgd       999: #endif
                   1000:        if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
                   1001:                return;
                   1002:        bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
                   1003:        ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
                   1004:        ip_srcrt.dst = dst;
                   1005: }
                   1006:
                   1007: /*
                   1008:  * Retrieve incoming source route for use in replies,
                   1009:  * in the same form used by setsockopt.
                   1010:  * The first hop is placed before the options, will be removed later.
                   1011:  */
                   1012: struct mbuf *
                   1013: ip_srcroute()
                   1014: {
                   1015:        register struct in_addr *p, *q;
                   1016:        register struct mbuf *m;
                   1017:
                   1018:        if (ip_nhops == 0)
                   1019:                return ((struct mbuf *)0);
                   1020:        m = m_get(M_DONTWAIT, MT_SOOPTS);
                   1021:        if (m == 0)
                   1022:                return ((struct mbuf *)0);
                   1023:
1.13      mycroft  1024: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1       cgd      1025:
                   1026:        /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
                   1027:        m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
                   1028:            OPTSIZ;
                   1029: #ifdef DIAGNOSTIC
                   1030:        if (ipprintfs)
1.39      christos 1031:                printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1.1       cgd      1032: #endif
                   1033:
                   1034:        /*
                   1035:         * First save first hop for return route
                   1036:         */
                   1037:        p = &ip_srcrt.route[ip_nhops - 1];
                   1038:        *(mtod(m, struct in_addr *)) = *p--;
                   1039: #ifdef DIAGNOSTIC
                   1040:        if (ipprintfs)
1.39      christos 1041:                printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1.1       cgd      1042: #endif
                   1043:
                   1044:        /*
                   1045:         * Copy option fields and padding (nop) to mbuf.
                   1046:         */
                   1047:        ip_srcrt.nop = IPOPT_NOP;
                   1048:        ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
                   1049:        bcopy((caddr_t)&ip_srcrt.nop,
                   1050:            mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
                   1051:        q = (struct in_addr *)(mtod(m, caddr_t) +
                   1052:            sizeof(struct in_addr) + OPTSIZ);
                   1053: #undef OPTSIZ
                   1054:        /*
                   1055:         * Record return path as an IP source route,
                   1056:         * reversing the path (pointers are now aligned).
                   1057:         */
                   1058:        while (p >= ip_srcrt.route) {
                   1059: #ifdef DIAGNOSTIC
                   1060:                if (ipprintfs)
1.39      christos 1061:                        printf(" %x", ntohl(q->s_addr));
1.1       cgd      1062: #endif
                   1063:                *q++ = *p--;
                   1064:        }
                   1065:        /*
                   1066:         * Last hop goes to final destination.
                   1067:         */
                   1068:        *q = ip_srcrt.dst;
                   1069: #ifdef DIAGNOSTIC
                   1070:        if (ipprintfs)
1.39      christos 1071:                printf(" %x\n", ntohl(q->s_addr));
1.1       cgd      1072: #endif
                   1073:        return (m);
                   1074: }
                   1075:
                   1076: /*
                   1077:  * Strip out IP options, at higher
                   1078:  * level protocol in the kernel.
                   1079:  * Second argument is buffer to which options
                   1080:  * will be moved, and return value is their length.
                   1081:  * XXX should be deleted; last arg currently ignored.
                   1082:  */
1.8       mycroft  1083: void
1.1       cgd      1084: ip_stripoptions(m, mopt)
                   1085:        register struct mbuf *m;
                   1086:        struct mbuf *mopt;
                   1087: {
                   1088:        register int i;
                   1089:        struct ip *ip = mtod(m, struct ip *);
                   1090:        register caddr_t opts;
                   1091:        int olen;
                   1092:
                   1093:        olen = (ip->ip_hl<<2) - sizeof (struct ip);
                   1094:        opts = (caddr_t)(ip + 1);
                   1095:        i = m->m_len - (sizeof (struct ip) + olen);
                   1096:        bcopy(opts  + olen, opts, (unsigned)i);
                   1097:        m->m_len -= olen;
                   1098:        if (m->m_flags & M_PKTHDR)
                   1099:                m->m_pkthdr.len -= olen;
                   1100:        ip->ip_hl = sizeof(struct ip) >> 2;
                   1101: }
                   1102:
1.23      mycroft  1103: int inetctlerrmap[PRC_NCMDS] = {
1.1       cgd      1104:        0,              0,              0,              0,
                   1105:        0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
                   1106:        EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
                   1107:        EMSGSIZE,       EHOSTUNREACH,   0,              0,
                   1108:        0,              0,              0,              0,
                   1109:        ENOPROTOOPT
                   1110: };
                   1111:
                   1112: /*
                   1113:  * Forward a packet.  If some error occurs return the sender
                   1114:  * an icmp packet.  Note we can't always generate a meaningful
                   1115:  * icmp message because icmp doesn't have a large enough repertoire
                   1116:  * of codes and types.
                   1117:  *
                   1118:  * If not forwarding, just drop the packet.  This could be confusing
                   1119:  * if ipforwarding was zero but some routing protocol was advancing
                   1120:  * us as a gateway to somewhere.  However, we must let the routing
                   1121:  * protocol deal with that.
                   1122:  *
                   1123:  * The srcrt parameter indicates whether the packet is being forwarded
                   1124:  * via a source route.
                   1125:  */
1.13      mycroft  1126: void
1.1       cgd      1127: ip_forward(m, srcrt)
                   1128:        struct mbuf *m;
                   1129:        int srcrt;
                   1130: {
                   1131:        register struct ip *ip = mtod(m, struct ip *);
                   1132:        register struct sockaddr_in *sin;
                   1133:        register struct rtentry *rt;
1.28      christos 1134:        int error, type = 0, code = 0;
1.1       cgd      1135:        struct mbuf *mcopy;
1.13      mycroft  1136:        n_long dest;
                   1137:        struct ifnet *destifp;
1.1       cgd      1138:
1.13      mycroft  1139:        dest = 0;
1.1       cgd      1140: #ifdef DIAGNOSTIC
                   1141:        if (ipprintfs)
1.70      thorpej  1142:                printf("forward: src %2.2x dst %2.2x ttl %x\n",
                   1143:                    ntohl(ip->ip_src.s_addr),
                   1144:                    ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
1.1       cgd      1145: #endif
                   1146:        if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
                   1147:                ipstat.ips_cantforward++;
                   1148:                m_freem(m);
                   1149:                return;
                   1150:        }
                   1151:        HTONS(ip->ip_id);
                   1152:        if (ip->ip_ttl <= IPTTLDEC) {
1.13      mycroft  1153:                icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1       cgd      1154:                return;
                   1155:        }
                   1156:        ip->ip_ttl -= IPTTLDEC;
                   1157:
1.19      mycroft  1158:        sin = satosin(&ipforward_rt.ro_dst);
1.1       cgd      1159:        if ((rt = ipforward_rt.ro_rt) == 0 ||
1.35      mycroft  1160:            !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1.1       cgd      1161:                if (ipforward_rt.ro_rt) {
                   1162:                        RTFREE(ipforward_rt.ro_rt);
                   1163:                        ipforward_rt.ro_rt = 0;
                   1164:                }
                   1165:                sin->sin_family = AF_INET;
1.35      mycroft  1166:                sin->sin_len = sizeof(struct sockaddr_in);
1.1       cgd      1167:                sin->sin_addr = ip->ip_dst;
                   1168:
                   1169:                rtalloc(&ipforward_rt);
                   1170:                if (ipforward_rt.ro_rt == 0) {
1.13      mycroft  1171:                        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1.1       cgd      1172:                        return;
                   1173:                }
                   1174:                rt = ipforward_rt.ro_rt;
                   1175:        }
                   1176:
                   1177:        /*
1.34      mycroft  1178:         * Save at most 68 bytes of the packet in case
1.1       cgd      1179:         * we need to generate an ICMP message to the src.
                   1180:         */
1.34      mycroft  1181:        mcopy = m_copy(m, 0, imin((int)ip->ip_len, 68));
1.1       cgd      1182:
                   1183:        /*
                   1184:         * If forwarding packet using same interface that it came in on,
                   1185:         * perhaps should send a redirect to sender to shortcut a hop.
                   1186:         * Only send redirect if source is sending directly to us,
                   1187:         * and if packet was not source routed (or has any options).
                   1188:         * Also, don't send redirect if forwarding using a default route
                   1189:         * or a route modified by a redirect.
                   1190:         */
                   1191:        if (rt->rt_ifp == m->m_pkthdr.rcvif &&
                   1192:            (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1.35      mycroft  1193:            !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1.1       cgd      1194:            ipsendredirects && !srcrt) {
1.19      mycroft  1195:                if (rt->rt_ifa &&
                   1196:                    (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
                   1197:                    ifatoia(rt->rt_ifa)->ia_subnet) {
1.1       cgd      1198:                    if (rt->rt_flags & RTF_GATEWAY)
1.13      mycroft  1199:                        dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1.1       cgd      1200:                    else
1.13      mycroft  1201:                        dest = ip->ip_dst.s_addr;
                   1202:                    /* Router requirements says to only send host redirects */
1.1       cgd      1203:                    type = ICMP_REDIRECT;
1.13      mycroft  1204:                    code = ICMP_REDIRECT_HOST;
1.1       cgd      1205: #ifdef DIAGNOSTIC
                   1206:                    if (ipprintfs)
1.39      christos 1207:                        printf("redirect (%d) to %x\n", code, (u_int32_t)dest);
1.1       cgd      1208: #endif
                   1209:                }
                   1210:        }
                   1211:
1.27      thorpej  1212:        error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
                   1213:            (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1.1       cgd      1214:        if (error)
                   1215:                ipstat.ips_cantforward++;
                   1216:        else {
                   1217:                ipstat.ips_forward++;
                   1218:                if (type)
                   1219:                        ipstat.ips_redirectsent++;
                   1220:                else {
1.63      matt     1221:                        if (mcopy) {
                   1222: #ifdef GATEWAY
1.64      thorpej  1223:                                if (mcopy->m_flags & M_CANFASTFWD)
                   1224:                                        ipflow_create(&ipforward_rt, mcopy);
1.63      matt     1225: #endif
1.1       cgd      1226:                                m_freem(mcopy);
1.63      matt     1227:                        }
1.1       cgd      1228:                        return;
                   1229:                }
                   1230:        }
                   1231:        if (mcopy == NULL)
                   1232:                return;
1.13      mycroft  1233:        destifp = NULL;
                   1234:
1.1       cgd      1235:        switch (error) {
                   1236:
                   1237:        case 0:                         /* forwarded, but need redirect */
                   1238:                /* type, code set above */
                   1239:                break;
                   1240:
                   1241:        case ENETUNREACH:               /* shouldn't happen, checked above */
                   1242:        case EHOSTUNREACH:
                   1243:        case ENETDOWN:
                   1244:        case EHOSTDOWN:
                   1245:        default:
                   1246:                type = ICMP_UNREACH;
                   1247:                code = ICMP_UNREACH_HOST;
                   1248:                break;
                   1249:
                   1250:        case EMSGSIZE:
                   1251:                type = ICMP_UNREACH;
                   1252:                code = ICMP_UNREACH_NEEDFRAG;
1.13      mycroft  1253:                if (ipforward_rt.ro_rt)
                   1254:                        destifp = ipforward_rt.ro_rt->rt_ifp;
1.1       cgd      1255:                ipstat.ips_cantfrag++;
                   1256:                break;
                   1257:
                   1258:        case ENOBUFS:
                   1259:                type = ICMP_SOURCEQUENCH;
                   1260:                code = 0;
                   1261:                break;
                   1262:        }
1.13      mycroft  1263:        icmp_error(mcopy, type, code, dest, destifp);
1.44      thorpej  1264: }
                   1265:
                   1266: void
                   1267: ip_savecontrol(inp, mp, ip, m)
                   1268:        register struct inpcb *inp;
                   1269:        register struct mbuf **mp;
                   1270:        register struct ip *ip;
                   1271:        register struct mbuf *m;
                   1272: {
                   1273:
                   1274:        if (inp->inp_socket->so_options & SO_TIMESTAMP) {
                   1275:                struct timeval tv;
                   1276:
                   1277:                microtime(&tv);
                   1278:                *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
                   1279:                    SCM_TIMESTAMP, SOL_SOCKET);
                   1280:                if (*mp)
                   1281:                        mp = &(*mp)->m_next;
                   1282:        }
                   1283:        if (inp->inp_flags & INP_RECVDSTADDR) {
                   1284:                *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
                   1285:                    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
                   1286:                if (*mp)
                   1287:                        mp = &(*mp)->m_next;
                   1288:        }
                   1289: #ifdef notyet
                   1290:        /*
                   1291:         * XXX
                   1292:         * Moving these out of udp_input() made them even more broken
                   1293:         * than they already were.
                   1294:         *      - fenner@parc.xerox.com
                   1295:         */
                   1296:        /* options were tossed already */
                   1297:        if (inp->inp_flags & INP_RECVOPTS) {
                   1298:                *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
                   1299:                    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
                   1300:                if (*mp)
                   1301:                        mp = &(*mp)->m_next;
                   1302:        }
                   1303:        /* ip_srcroute doesn't do what we want here, need to fix */
                   1304:        if (inp->inp_flags & INP_RECVRETOPTS) {
                   1305:                *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
                   1306:                    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
                   1307:                if (*mp)
                   1308:                        mp = &(*mp)->m_next;
                   1309:        }
                   1310: #endif
                   1311:        if (inp->inp_flags & INP_RECVIF) {
                   1312:                struct sockaddr_dl sdl;
                   1313:
                   1314:                sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
                   1315:                sdl.sdl_family = AF_LINK;
                   1316:                sdl.sdl_index = m->m_pkthdr.rcvif ?
                   1317:                    m->m_pkthdr.rcvif->if_index : 0;
                   1318:                sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
                   1319:                *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
                   1320:                    IP_RECVIF, IPPROTO_IP);
                   1321:                if (*mp)
                   1322:                        mp = &(*mp)->m_next;
                   1323:        }
1.13      mycroft  1324: }
                   1325:
                   1326: int
                   1327: ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
                   1328:        int *name;
                   1329:        u_int namelen;
                   1330:        void *oldp;
                   1331:        size_t *oldlenp;
                   1332:        void *newp;
                   1333:        size_t newlen;
                   1334: {
1.52      thorpej  1335:        extern int subnetsarelocal;
                   1336:
1.54      lukem    1337:        int error, old;
                   1338:
1.13      mycroft  1339:        /* All sysctl names at this level are terminal. */
                   1340:        if (namelen != 1)
                   1341:                return (ENOTDIR);
                   1342:
                   1343:        switch (name[0]) {
                   1344:        case IPCTL_FORWARDING:
                   1345:                return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
                   1346:        case IPCTL_SENDREDIRECTS:
                   1347:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1348:                        &ipsendredirects));
                   1349:        case IPCTL_DEFTTL:
                   1350:                return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
                   1351: #ifdef notyet
                   1352:        case IPCTL_DEFMTU:
                   1353:                return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
                   1354: #endif
1.26      thorpej  1355:        case IPCTL_FORWSRCRT:
1.47      cjs      1356:                /* Don't allow this to change in a secure environment.  */
1.26      thorpej  1357:                if (securelevel > 0)
1.46      cjs      1358:                        return (sysctl_rdint(oldp, oldlenp, newp,
                   1359:                            ip_forwsrcrt));
                   1360:                else
                   1361:                        return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1362:                            &ip_forwsrcrt));
1.27      thorpej  1363:        case IPCTL_DIRECTEDBCAST:
                   1364:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1365:                    &ip_directedbcast));
1.47      cjs      1366:        case IPCTL_ALLOWSRCRT:
                   1367:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1368:                    &ip_allowsrcrt));
1.52      thorpej  1369:        case IPCTL_SUBNETSARELOCAL:
                   1370:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1371:                    &subnetsarelocal));
1.53      kml      1372:        case IPCTL_MTUDISC:
1.60      kml      1373:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1374:                    &ip_mtudisc);
                   1375:                if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
                   1376:                        ip_mtudisc_timeout_q =
                   1377:                            rt_timer_queue_create(ip_mtudisc_timeout);
                   1378:                } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
                   1379:                        rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE);
                   1380:                        ip_mtudisc_timeout_q = NULL;
                   1381:                }
                   1382:                return error;
1.54      lukem    1383:        case IPCTL_ANONPORTMIN:
                   1384:                old = anonportmin;
                   1385:                error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
                   1386:                if (anonportmin >= anonportmax || anonportmin > 65535
                   1387: #ifndef IPNOPRIVPORTS
                   1388:                    || anonportmin < IPPORT_RESERVED
                   1389: #endif
                   1390:                    ) {
                   1391:                        anonportmin = old;
                   1392:                        return (EINVAL);
                   1393:                }
                   1394:                return (error);
                   1395:        case IPCTL_ANONPORTMAX:
                   1396:                old = anonportmax;
                   1397:                error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
                   1398:                if (anonportmin >= anonportmax || anonportmax > 65535
                   1399: #ifndef IPNOPRIVPORTS
                   1400:                    || anonportmax < IPPORT_RESERVED
                   1401: #endif
                   1402:                    ) {
                   1403:                        anonportmax = old;
                   1404:                        return (EINVAL);
                   1405:                }
1.60      kml      1406:                return (error);
                   1407:        case IPCTL_MTUDISCTIMEOUT:
                   1408:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1409:                   &ip_mtudisc_timeout);
                   1410:                if (ip_mtudisc_timeout_q != NULL)
                   1411:                        rt_timer_queue_change(ip_mtudisc_timeout_q,
                   1412:                                              ip_mtudisc_timeout);
1.54      lukem    1413:                return (error);
1.65      matt     1414: #ifdef GATEWAY
                   1415:        case IPCTL_MAXFLOWS:
1.67      thorpej  1416:            {
                   1417:                int s;
                   1418:
1.65      matt     1419:                error = sysctl_int(oldp, oldlenp, newp, newlen,
                   1420:                   &ip_maxflows);
1.67      thorpej  1421:                s = splsoftnet();
1.65      matt     1422:                ipflow_reap(0);
1.67      thorpej  1423:                splx(s);
1.65      matt     1424:                return (error);
1.67      thorpej  1425:            }
1.65      matt     1426: #endif
1.13      mycroft  1427:        default:
                   1428:                return (EOPNOTSUPP);
                   1429:        }
                   1430:        /* NOTREACHED */
1.1       cgd      1431: }

CVSweb <webmaster@jp.NetBSD.org>