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

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

CVSweb <webmaster@jp.NetBSD.org>