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

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

CVSweb <webmaster@jp.NetBSD.org>