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

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

CVSweb <webmaster@jp.NetBSD.org>