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

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

CVSweb <webmaster@jp.NetBSD.org>