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

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

CVSweb <webmaster@jp.NetBSD.org>