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

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

CVSweb <webmaster@jp.NetBSD.org>