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

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

CVSweb <webmaster@jp.NetBSD.org>