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

1.30.4.1! thorpej     1: /*     $NetBSD: ip_input.c,v 1.30 1996/03/16 23:53:58 christos Exp $   */
1.14      cgd         2:
1.1       cgd         3: /*
1.13      mycroft     4:  * Copyright (c) 1982, 1986, 1988, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  *
1.14      cgd        35:  *     @(#)ip_input.c  8.2 (Berkeley) 1/4/94
1.1       cgd        36:  */
                     37:
1.5       mycroft    38: #include <sys/param.h>
                     39: #include <sys/systm.h>
                     40: #include <sys/malloc.h>
                     41: #include <sys/mbuf.h>
                     42: #include <sys/domain.h>
                     43: #include <sys/protosw.h>
                     44: #include <sys/socket.h>
                     45: #include <sys/errno.h>
                     46: #include <sys/time.h>
                     47: #include <sys/kernel.h>
1.28      christos   48: #include <sys/proc.h>
                     49:
                     50: #include <vm/vm.h>
                     51: #include <sys/sysctl.h>
1.1       cgd        52:
1.5       mycroft    53: #include <net/if.h>
                     54: #include <net/route.h>
1.1       cgd        55:
1.5       mycroft    56: #include <netinet/in.h>
                     57: #include <netinet/in_systm.h>
                     58: #include <netinet/ip.h>
                     59: #include <netinet/in_pcb.h>
                     60: #include <netinet/in_var.h>
                     61: #include <netinet/ip_var.h>
                     62: #include <netinet/ip_icmp.h>
1.1       cgd        63:
                     64: #ifndef        IPFORWARDING
                     65: #ifdef GATEWAY
                     66: #define        IPFORWARDING    1       /* forward IP packets not for us */
                     67: #else /* GATEWAY */
                     68: #define        IPFORWARDING    0       /* don't forward IP packets not for us */
                     69: #endif /* GATEWAY */
                     70: #endif /* IPFORWARDING */
                     71: #ifndef        IPSENDREDIRECTS
                     72: #define        IPSENDREDIRECTS 1
                     73: #endif
1.26      thorpej    74: #ifndef IPFORWSRCRT
                     75: #define        IPFORWSRCRT     1       /* allow source-routed packets */
                     76: #endif
1.27      thorpej    77: /*
                     78:  * Note: DIRECTED_BROADCAST is handled this way so that previous
                     79:  * configuration using this option will Just Work.
                     80:  */
                     81: #ifndef IPDIRECTEDBCAST
                     82: #ifdef DIRECTED_BROADCAST
                     83: #define IPDIRECTEDBCAST        1
                     84: #else
                     85: #define        IPDIRECTEDBCAST 0
                     86: #endif /* DIRECTED_BROADCAST */
                     87: #endif /* IPDIRECTEDBCAST */
1.1       cgd        88: int    ipforwarding = IPFORWARDING;
                     89: int    ipsendredirects = IPSENDREDIRECTS;
1.13      mycroft    90: int    ip_defttl = IPDEFTTL;
1.26      thorpej    91: int    ip_forwsrcrt = IPFORWSRCRT;
1.27      thorpej    92: int    ip_directedbcast = IPDIRECTEDBCAST;
1.1       cgd        93: #ifdef DIAGNOSTIC
                     94: int    ipprintfs = 0;
                     95: #endif
                     96:
                     97: extern struct domain inetdomain;
                     98: extern struct protosw inetsw[];
                     99: u_char ip_protox[IPPROTO_MAX];
                    100: int    ipqmaxlen = IFQ_MAXLEN;
1.22      mycroft   101: struct in_ifaddrhead in_ifaddr;
1.13      mycroft   102: struct ifqueue ipintrq;
1.1       cgd       103:
                    104: /*
                    105:  * We need to save the IP options in case a protocol wants to respond
                    106:  * to an incoming packet over the same route if the packet got here
                    107:  * using IP source routing.  This allows connection establishment and
                    108:  * maintenance when the remote end is on a network that is not known
                    109:  * to us.
                    110:  */
                    111: int    ip_nhops = 0;
                    112: static struct ip_srcrt {
                    113:        struct  in_addr dst;                    /* final destination */
                    114:        char    nop;                            /* one NOP to align */
                    115:        char    srcopt[IPOPT_OFFSET + 1];       /* OPTVAL, OLEN and OFFSET */
                    116:        struct  in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
                    117: } ip_srcrt;
                    118:
1.13      mycroft   119: static void save_rte __P((u_char *, struct in_addr));
1.1       cgd       120: /*
                    121:  * IP initialization: fill in IP protocol switch table.
                    122:  * All protocols not implemented in kernel go to raw IP protocol handler.
                    123:  */
1.8       mycroft   124: void
1.1       cgd       125: ip_init()
                    126: {
                    127:        register struct protosw *pr;
                    128:        register int i;
                    129:
                    130:        pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
                    131:        if (pr == 0)
                    132:                panic("ip_init");
                    133:        for (i = 0; i < IPPROTO_MAX; i++)
                    134:                ip_protox[i] = pr - inetsw;
                    135:        for (pr = inetdomain.dom_protosw;
                    136:            pr < inetdomain.dom_protoswNPROTOSW; pr++)
                    137:                if (pr->pr_domain->dom_family == PF_INET &&
                    138:                    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
                    139:                        ip_protox[pr->pr_protocol] = pr - inetsw;
1.25      cgd       140:        LIST_INIT(&ipq);
1.1       cgd       141:        ip_id = time.tv_sec & 0xffff;
                    142:        ipintrq.ifq_maxlen = ipqmaxlen;
1.22      mycroft   143:        TAILQ_INIT(&in_ifaddr);
1.1       cgd       144: }
                    145:
                    146: struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
                    147: struct route ipforward_rt;
                    148:
                    149: /*
                    150:  * Ip input routine.  Checksum and byte swap header.  If fragmented
                    151:  * try to reassemble.  Process options.  Pass to next level.
                    152:  */
1.8       mycroft   153: void
1.1       cgd       154: ipintr()
                    155: {
                    156:        register struct ip *ip;
                    157:        register struct mbuf *m;
                    158:        register struct ipq *fp;
                    159:        register struct in_ifaddr *ia;
1.25      cgd       160:        struct ipqent *ipqe;
                    161:        int hlen, mff, s;
1.1       cgd       162:
                    163: next:
                    164:        /*
                    165:         * Get next datagram off input queue and get IP header
                    166:         * in first mbuf.
                    167:         */
                    168:        s = splimp();
                    169:        IF_DEQUEUE(&ipintrq, m);
                    170:        splx(s);
1.13      mycroft   171:        if (m == 0)
1.1       cgd       172:                return;
                    173: #ifdef DIAGNOSTIC
                    174:        if ((m->m_flags & M_PKTHDR) == 0)
                    175:                panic("ipintr no HDR");
                    176: #endif
                    177:        /*
                    178:         * If no IP addresses have been set yet but the interfaces
                    179:         * are receiving, can't do anything with incoming packets yet.
                    180:         */
1.22      mycroft   181:        if (in_ifaddr.tqh_first == 0)
1.1       cgd       182:                goto bad;
                    183:        ipstat.ips_total++;
                    184:        if (m->m_len < sizeof (struct ip) &&
                    185:            (m = m_pullup(m, sizeof (struct ip))) == 0) {
                    186:                ipstat.ips_toosmall++;
                    187:                goto next;
                    188:        }
                    189:        ip = mtod(m, struct ip *);
1.13      mycroft   190:        if (ip->ip_v != IPVERSION) {
                    191:                ipstat.ips_badvers++;
                    192:                goto bad;
                    193:        }
1.1       cgd       194:        hlen = ip->ip_hl << 2;
                    195:        if (hlen < sizeof(struct ip)) { /* minimum header length */
                    196:                ipstat.ips_badhlen++;
                    197:                goto bad;
                    198:        }
                    199:        if (hlen > m->m_len) {
                    200:                if ((m = m_pullup(m, hlen)) == 0) {
                    201:                        ipstat.ips_badhlen++;
                    202:                        goto next;
                    203:                }
                    204:                ip = mtod(m, struct ip *);
                    205:        }
1.28      christos  206:        if ((ip->ip_sum = in_cksum(m, hlen)) != 0) {
1.1       cgd       207:                ipstat.ips_badsum++;
                    208:                goto bad;
                    209:        }
                    210:
                    211:        /*
                    212:         * Convert fields to host representation.
                    213:         */
                    214:        NTOHS(ip->ip_len);
                    215:        if (ip->ip_len < hlen) {
                    216:                ipstat.ips_badlen++;
                    217:                goto bad;
                    218:        }
                    219:        NTOHS(ip->ip_id);
                    220:        NTOHS(ip->ip_off);
                    221:
                    222:        /*
                    223:         * Check that the amount of data in the buffers
                    224:         * is as at least much as the IP header would have us expect.
                    225:         * Trim mbufs if longer than we expect.
                    226:         * Drop packet if shorter than we expect.
                    227:         */
                    228:        if (m->m_pkthdr.len < ip->ip_len) {
                    229:                ipstat.ips_tooshort++;
                    230:                goto bad;
                    231:        }
                    232:        if (m->m_pkthdr.len > ip->ip_len) {
                    233:                if (m->m_len == m->m_pkthdr.len) {
                    234:                        m->m_len = ip->ip_len;
                    235:                        m->m_pkthdr.len = ip->ip_len;
                    236:                } else
                    237:                        m_adj(m, ip->ip_len - m->m_pkthdr.len);
                    238:        }
                    239:
                    240:        /*
                    241:         * Process options and, if not destined for us,
                    242:         * ship it on.  ip_dooptions returns 1 when an
                    243:         * error was detected (causing an icmp message
                    244:         * to be sent and the original packet to be freed).
                    245:         */
                    246:        ip_nhops = 0;           /* for source routed packets */
                    247:        if (hlen > sizeof (struct ip) && ip_dooptions(m))
                    248:                goto next;
                    249:
                    250:        /*
                    251:         * Check our list of addresses, to see if the packet is for us.
                    252:         */
1.22      mycroft   253:        for (ia = in_ifaddr.tqh_first; ia; ia = ia->ia_list.tqe_next) {
1.20      mycroft   254:                if (ip->ip_dst.s_addr == ia->ia_addr.sin_addr.s_addr)
1.1       cgd       255:                        goto ours;
1.27      thorpej   256:                if (((ip_directedbcast == 0) || (ip_directedbcast &&
                    257:                    ia->ia_ifp == m->m_pkthdr.rcvif)) &&
1.1       cgd       258:                    (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
1.20      mycroft   259:                        if (ip->ip_dst.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
                    260:                            ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr ||
                    261:                            /*
                    262:                             * Look for all-0's host part (old broadcast addr),
                    263:                             * either for subnet or net.
                    264:                             */
                    265:                            ip->ip_dst.s_addr == ia->ia_subnet ||
1.18      mycroft   266:                            ip->ip_dst.s_addr == ia->ia_net)
1.1       cgd       267:                                goto ours;
                    268:                }
                    269:        }
1.18      mycroft   270:        if (IN_MULTICAST(ip->ip_dst.s_addr)) {
1.4       hpeyerl   271:                struct in_multi *inm;
                    272: #ifdef MROUTING
                    273:                extern struct socket *ip_mrouter;
1.10      brezak    274:
                    275:                if (m->m_flags & M_EXT) {
                    276:                        if ((m = m_pullup(m, hlen)) == 0) {
                    277:                                ipstat.ips_toosmall++;
                    278:                                goto next;
                    279:                        }
                    280:                        ip = mtod(m, struct ip *);
                    281:                }
1.4       hpeyerl   282:
                    283:                if (ip_mrouter) {
                    284:                        /*
                    285:                         * If we are acting as a multicast router, all
                    286:                         * incoming multicast packets are passed to the
                    287:                         * kernel-level multicast forwarding function.
                    288:                         * The packet is returned (relatively) intact; if
                    289:                         * ip_mforward() returns a non-zero value, the packet
                    290:                         * must be discarded, else it may be accepted below.
                    291:                         *
                    292:                         * (The IP ident field is put in the same byte order
                    293:                         * as expected when ip_mforward() is called from
                    294:                         * ip_output().)
                    295:                         */
                    296:                        ip->ip_id = htons(ip->ip_id);
1.13      mycroft   297:                        if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
                    298:                                ipstat.ips_cantforward++;
1.4       hpeyerl   299:                                m_freem(m);
                    300:                                goto next;
                    301:                        }
                    302:                        ip->ip_id = ntohs(ip->ip_id);
                    303:
                    304:                        /*
                    305:                         * The process-level routing demon needs to receive
                    306:                         * all multicast IGMP packets, whether or not this
                    307:                         * host belongs to their destination groups.
                    308:                         */
                    309:                        if (ip->ip_p == IPPROTO_IGMP)
                    310:                                goto ours;
1.13      mycroft   311:                        ipstat.ips_forward++;
1.4       hpeyerl   312:                }
                    313: #endif
                    314:                /*
                    315:                 * See if we belong to the destination multicast group on the
                    316:                 * arrival interface.
                    317:                 */
                    318:                IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
                    319:                if (inm == NULL) {
1.13      mycroft   320:                        ipstat.ips_cantforward++;
1.4       hpeyerl   321:                        m_freem(m);
                    322:                        goto next;
                    323:                }
                    324:                goto ours;
                    325:        }
1.19      mycroft   326:        if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
                    327:            ip->ip_dst.s_addr == INADDR_ANY)
1.1       cgd       328:                goto ours;
                    329:
                    330:        /*
                    331:         * Not for us; forward if possible and desirable.
                    332:         */
                    333:        if (ipforwarding == 0) {
                    334:                ipstat.ips_cantforward++;
                    335:                m_freem(m);
                    336:        } else
                    337:                ip_forward(m, 0);
                    338:        goto next;
                    339:
                    340: ours:
                    341:        /*
                    342:         * If offset or IP_MF are set, must reassemble.
                    343:         * Otherwise, nothing need be done.
                    344:         * (We could look in the reassembly queue to see
                    345:         * if the packet was previously fragmented,
                    346:         * but it's not worth the time; just let them time out.)
                    347:         */
                    348:        if (ip->ip_off &~ IP_DF) {
                    349:                if (m->m_flags & M_EXT) {               /* XXX */
                    350:                        if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
                    351:                                ipstat.ips_toosmall++;
                    352:                                goto next;
                    353:                        }
                    354:                        ip = mtod(m, struct ip *);
                    355:                }
                    356:                /*
                    357:                 * Look for queue of fragments
                    358:                 * of this datagram.
                    359:                 */
1.25      cgd       360:                for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
1.1       cgd       361:                        if (ip->ip_id == fp->ipq_id &&
                    362:                            ip->ip_src.s_addr == fp->ipq_src.s_addr &&
                    363:                            ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
                    364:                            ip->ip_p == fp->ipq_p)
                    365:                                goto found;
                    366:                fp = 0;
                    367: found:
                    368:
                    369:                /*
                    370:                 * Adjust ip_len to not reflect header,
1.25      cgd       371:                 * set ipqe_mff if more fragments are expected,
1.1       cgd       372:                 * convert offset of this to bytes.
                    373:                 */
                    374:                ip->ip_len -= hlen;
1.25      cgd       375:                mff = (ip->ip_off & IP_MF) != 0;
                    376:                if (mff) {
1.16      cgd       377:                        /*
                    378:                         * Make sure that fragments have a data length
                    379:                         * that's a non-zero multiple of 8 bytes.
                    380:                         */
1.17      cgd       381:                        if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
1.16      cgd       382:                                ipstat.ips_badfrags++;
                    383:                                goto bad;
                    384:                        }
                    385:                }
1.1       cgd       386:                ip->ip_off <<= 3;
                    387:
                    388:                /*
                    389:                 * If datagram marked as having more fragments
                    390:                 * or if this is not the first fragment,
                    391:                 * attempt reassembly; if it succeeds, proceed.
                    392:                 */
1.25      cgd       393:                if (mff || ip->ip_off) {
1.1       cgd       394:                        ipstat.ips_fragments++;
1.25      cgd       395:                        MALLOC(ipqe, struct ipqent *, sizeof (struct ipqent),
                    396:                            M_IPQ, M_NOWAIT);
                    397:                        if (ipqe == NULL) {
                    398:                                ipstat.ips_rcvmemdrop++;
                    399:                                goto bad;
                    400:                        }
                    401:                        ipqe->ipqe_mff = mff;
                    402:                        ipqe->ipqe_ip = ip;
                    403:                        ip = ip_reass(ipqe, fp);
1.1       cgd       404:                        if (ip == 0)
                    405:                                goto next;
1.13      mycroft   406:                        ipstat.ips_reassembled++;
1.1       cgd       407:                        m = dtom(ip);
                    408:                } else
                    409:                        if (fp)
                    410:                                ip_freef(fp);
                    411:        } else
                    412:                ip->ip_len -= hlen;
                    413:
                    414:        /*
                    415:         * Switch out to protocol's input routine.
                    416:         */
                    417:        ipstat.ips_delivered++;
                    418:        (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
                    419:        goto next;
                    420: bad:
                    421:        m_freem(m);
                    422:        goto next;
                    423: }
                    424:
                    425: /*
                    426:  * Take incoming datagram fragment and try to
                    427:  * reassemble it into whole datagram.  If a chain for
                    428:  * reassembly of this datagram already exists, then it
                    429:  * is given as fp; otherwise have to make a chain.
                    430:  */
                    431: struct ip *
1.25      cgd       432: ip_reass(ipqe, fp)
                    433:        register struct ipqent *ipqe;
1.1       cgd       434:        register struct ipq *fp;
                    435: {
1.25      cgd       436:        register struct mbuf *m = dtom(ipqe->ipqe_ip);
                    437:        register struct ipqent *nq, *p, *q;
                    438:        struct ip *ip;
1.1       cgd       439:        struct mbuf *t;
1.25      cgd       440:        int hlen = ipqe->ipqe_ip->ip_hl << 2;
1.1       cgd       441:        int i, next;
                    442:
                    443:        /*
                    444:         * Presence of header sizes in mbufs
                    445:         * would confuse code below.
                    446:         */
                    447:        m->m_data += hlen;
                    448:        m->m_len -= hlen;
                    449:
                    450:        /*
                    451:         * If first fragment to arrive, create a reassembly queue.
                    452:         */
                    453:        if (fp == 0) {
                    454:                if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
                    455:                        goto dropfrag;
                    456:                fp = mtod(t, struct ipq *);
1.25      cgd       457:                LIST_INSERT_HEAD(&ipq, fp, ipq_q);
1.1       cgd       458:                fp->ipq_ttl = IPFRAGTTL;
1.25      cgd       459:                fp->ipq_p = ipqe->ipqe_ip->ip_p;
                    460:                fp->ipq_id = ipqe->ipqe_ip->ip_id;
                    461:                LIST_INIT(&fp->ipq_fragq);
                    462:                fp->ipq_src = ipqe->ipqe_ip->ip_src;
                    463:                fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
                    464:                p = NULL;
1.1       cgd       465:                goto insert;
                    466:        }
                    467:
                    468:        /*
                    469:         * Find a segment which begins after this one does.
                    470:         */
1.25      cgd       471:        for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
                    472:            p = q, q = q->ipqe_q.le_next)
                    473:                if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
1.1       cgd       474:                        break;
                    475:
                    476:        /*
                    477:         * If there is a preceding segment, it may provide some of
                    478:         * our data already.  If so, drop the data from the incoming
                    479:         * segment.  If it provides all of our data, drop us.
                    480:         */
1.25      cgd       481:        if (p != NULL) {
                    482:                i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
                    483:                    ipqe->ipqe_ip->ip_off;
1.1       cgd       484:                if (i > 0) {
1.25      cgd       485:                        if (i >= ipqe->ipqe_ip->ip_len)
1.1       cgd       486:                                goto dropfrag;
1.25      cgd       487:                        m_adj(dtom(ipqe->ipqe_ip), i);
                    488:                        ipqe->ipqe_ip->ip_off += i;
                    489:                        ipqe->ipqe_ip->ip_len -= i;
1.1       cgd       490:                }
                    491:        }
                    492:
                    493:        /*
                    494:         * While we overlap succeeding segments trim them or,
                    495:         * if they are completely covered, dequeue them.
                    496:         */
1.25      cgd       497:        for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
                    498:            q->ipqe_ip->ip_off; q = nq) {
                    499:                i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
                    500:                    q->ipqe_ip->ip_off;
                    501:                if (i < q->ipqe_ip->ip_len) {
                    502:                        q->ipqe_ip->ip_len -= i;
                    503:                        q->ipqe_ip->ip_off += i;
                    504:                        m_adj(dtom(q->ipqe_ip), i);
1.1       cgd       505:                        break;
                    506:                }
1.25      cgd       507:                nq = q->ipqe_q.le_next;
                    508:                m_freem(dtom(q->ipqe_ip));
                    509:                LIST_REMOVE(q, ipqe_q);
                    510:                FREE(q, M_IPQ);
1.1       cgd       511:        }
                    512:
                    513: insert:
                    514:        /*
                    515:         * Stick new segment in its place;
                    516:         * check for complete reassembly.
                    517:         */
1.25      cgd       518:        if (p == NULL) {
                    519:                LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
                    520:        } else {
                    521:                LIST_INSERT_AFTER(p, ipqe, ipqe_q);
                    522:        }
1.1       cgd       523:        next = 0;
1.25      cgd       524:        for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
                    525:            p = q, q = q->ipqe_q.le_next) {
                    526:                if (q->ipqe_ip->ip_off != next)
1.1       cgd       527:                        return (0);
1.25      cgd       528:                next += q->ipqe_ip->ip_len;
1.1       cgd       529:        }
1.25      cgd       530:        if (p->ipqe_mff)
1.1       cgd       531:                return (0);
                    532:
                    533:        /*
1.30.4.1! thorpej   534:         * Reassembly is complete.  Check for a bogus message size and
        !           535:         * concatenate fragments.
1.1       cgd       536:         */
1.25      cgd       537:        q = fp->ipq_fragq.lh_first;
                    538:        ip = q->ipqe_ip;
1.30.4.1! thorpej   539:        if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
        !           540:                ipstat.ips_toolong++;
        !           541:                ip_freef(fp);
        !           542:                return (0);
        !           543:        }
1.25      cgd       544:        m = dtom(q->ipqe_ip);
1.1       cgd       545:        t = m->m_next;
                    546:        m->m_next = 0;
                    547:        m_cat(m, t);
1.25      cgd       548:        nq = q->ipqe_q.le_next;
                    549:        FREE(q, M_IPQ);
                    550:        for (q = nq; q != NULL; q = nq) {
                    551:                t = dtom(q->ipqe_ip);
                    552:                nq = q->ipqe_q.le_next;
                    553:                FREE(q, M_IPQ);
1.1       cgd       554:                m_cat(m, t);
                    555:        }
                    556:
                    557:        /*
                    558:         * Create header for new ip packet by
                    559:         * modifying header of first packet;
                    560:         * dequeue and discard fragment reassembly header.
                    561:         * Make header visible.
                    562:         */
                    563:        ip->ip_len = next;
1.25      cgd       564:        ip->ip_src = fp->ipq_src;
                    565:        ip->ip_dst = fp->ipq_dst;
                    566:        LIST_REMOVE(fp, ipq_q);
1.1       cgd       567:        (void) m_free(dtom(fp));
                    568:        m->m_len += (ip->ip_hl << 2);
                    569:        m->m_data -= (ip->ip_hl << 2);
                    570:        /* some debugging cruft by sklower, below, will go away soon */
                    571:        if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
                    572:                register int plen = 0;
                    573:                for (t = m; m; m = m->m_next)
                    574:                        plen += m->m_len;
                    575:                t->m_pkthdr.len = plen;
                    576:        }
1.25      cgd       577:        return (ip);
1.1       cgd       578:
                    579: dropfrag:
                    580:        ipstat.ips_fragdropped++;
                    581:        m_freem(m);
1.25      cgd       582:        FREE(ipqe, M_IPQ);
1.1       cgd       583:        return (0);
                    584: }
                    585:
                    586: /*
                    587:  * Free a fragment reassembly header and all
                    588:  * associated datagrams.
                    589:  */
1.8       mycroft   590: void
1.1       cgd       591: ip_freef(fp)
                    592:        struct ipq *fp;
                    593: {
1.25      cgd       594:        register struct ipqent *q, *p;
1.1       cgd       595:
1.25      cgd       596:        for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
                    597:                p = q->ipqe_q.le_next;
                    598:                m_freem(dtom(q->ipqe_ip));
                    599:                LIST_REMOVE(q, ipqe_q);
                    600:                FREE(q, M_IPQ);
1.1       cgd       601:        }
1.25      cgd       602:        LIST_REMOVE(fp, ipq_q);
1.1       cgd       603:        (void) m_free(dtom(fp));
                    604: }
                    605:
                    606: /*
                    607:  * IP timer processing;
                    608:  * if a timer expires on a reassembly
                    609:  * queue, discard it.
                    610:  */
1.8       mycroft   611: void
1.1       cgd       612: ip_slowtimo()
                    613: {
1.25      cgd       614:        register struct ipq *fp, *nfp;
1.24      mycroft   615:        int s = splsoftnet();
1.1       cgd       616:
1.25      cgd       617:        for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
                    618:                nfp = fp->ipq_q.le_next;
                    619:                if (--fp->ipq_ttl == 0) {
1.1       cgd       620:                        ipstat.ips_fragtimeout++;
1.25      cgd       621:                        ip_freef(fp);
1.1       cgd       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:
1.25      cgd       634:        while (ipq.lh_first != NULL) {
1.1       cgd       635:                ipstat.ips_fragdropped++;
1.25      cgd       636:                ip_freef(ipq.lh_first);
1.1       cgd       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 *
1.29      mrg       725:                            ia = (INA)ifa_ifwithladdr((SA)&ipaddr);
1.1       cgd       726:                        } else
                    727:                                ia = ip_rtaddr(ipaddr.sin_addr);
                    728:                        if (ia == 0) {
                    729:                                type = ICMP_UNREACH;
                    730:                                code = ICMP_UNREACH_SRCFAIL;
                    731:                                goto bad;
                    732:                        }
                    733:                        ip->ip_dst = ipaddr.sin_addr;
1.20      mycroft   734:                        bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1       cgd       735:                            (caddr_t)(cp + off), sizeof(struct in_addr));
                    736:                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1.13      mycroft   737:                        /*
                    738:                         * Let ip_intr's mcast routing check handle mcast pkts
                    739:                         */
1.18      mycroft   740:                        forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1.1       cgd       741:                        break;
                    742:
                    743:                case IPOPT_RR:
                    744:                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
                    745:                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
                    746:                                goto bad;
                    747:                        }
                    748:                        /*
                    749:                         * If no space remains, ignore.
                    750:                         */
                    751:                        off--;                  /* 0 origin */
                    752:                        if (off > optlen - sizeof(struct in_addr))
                    753:                                break;
                    754:                        bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
                    755:                            sizeof(ipaddr.sin_addr));
                    756:                        /*
                    757:                         * locate outgoing interface; if we're the destination,
                    758:                         * use the incoming interface (should be same).
                    759:                         */
                    760:                        if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
                    761:                            (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
                    762:                                type = ICMP_UNREACH;
                    763:                                code = ICMP_UNREACH_HOST;
                    764:                                goto bad;
                    765:                        }
1.20      mycroft   766:                        bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1       cgd       767:                            (caddr_t)(cp + off), sizeof(struct in_addr));
                    768:                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
                    769:                        break;
                    770:
                    771:                case IPOPT_TS:
                    772:                        code = cp - (u_char *)ip;
                    773:                        ipt = (struct ip_timestamp *)cp;
                    774:                        if (ipt->ipt_len < 5)
                    775:                                goto bad;
1.15      cgd       776:                        if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1.1       cgd       777:                                if (++ipt->ipt_oflw == 0)
                    778:                                        goto bad;
                    779:                                break;
                    780:                        }
                    781:                        sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
                    782:                        switch (ipt->ipt_flg) {
                    783:
                    784:                        case IPOPT_TS_TSONLY:
                    785:                                break;
                    786:
                    787:                        case IPOPT_TS_TSANDADDR:
                    788:                                if (ipt->ipt_ptr + sizeof(n_time) +
                    789:                                    sizeof(struct in_addr) > ipt->ipt_len)
                    790:                                        goto bad;
1.13      mycroft   791:                                ipaddr.sin_addr = dst;
                    792:                                ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
                    793:                                                            m->m_pkthdr.rcvif);
                    794:                                if (ia == 0)
                    795:                                        continue;
1.20      mycroft   796:                                bcopy((caddr_t)&ia->ia_addr.sin_addr,
1.1       cgd       797:                                    (caddr_t)sin, sizeof(struct in_addr));
                    798:                                ipt->ipt_ptr += sizeof(struct in_addr);
                    799:                                break;
                    800:
                    801:                        case IPOPT_TS_PRESPEC:
                    802:                                if (ipt->ipt_ptr + sizeof(n_time) +
                    803:                                    sizeof(struct in_addr) > ipt->ipt_len)
                    804:                                        goto bad;
                    805:                                bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
                    806:                                    sizeof(struct in_addr));
                    807:                                if (ifa_ifwithaddr((SA)&ipaddr) == 0)
                    808:                                        continue;
                    809:                                ipt->ipt_ptr += sizeof(struct in_addr);
                    810:                                break;
                    811:
                    812:                        default:
                    813:                                goto bad;
                    814:                        }
                    815:                        ntime = iptime();
                    816:                        bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
                    817:                            sizeof(n_time));
                    818:                        ipt->ipt_ptr += sizeof(n_time);
                    819:                }
                    820:        }
                    821:        if (forward) {
1.26      thorpej   822:                if (ip_forwsrcrt == 0) {
                    823:                        type = ICMP_UNREACH;
                    824:                        code = ICMP_UNREACH_SRCFAIL;
                    825:                        goto bad;
                    826:                }
1.1       cgd       827:                ip_forward(m, 1);
                    828:                return (1);
1.13      mycroft   829:        }
                    830:        return (0);
1.1       cgd       831: bad:
1.13      mycroft   832:        ip->ip_len -= ip->ip_hl << 2;   /* XXX icmp_error adds in hdr length */
                    833:        icmp_error(m, type, code, 0, 0);
                    834:        ipstat.ips_badoptions++;
1.1       cgd       835:        return (1);
                    836: }
                    837:
                    838: /*
                    839:  * Given address of next destination (final or next hop),
                    840:  * return internet address info of interface to be used to get there.
                    841:  */
                    842: struct in_ifaddr *
                    843: ip_rtaddr(dst)
                    844:         struct in_addr dst;
                    845: {
                    846:        register struct sockaddr_in *sin;
                    847:
1.19      mycroft   848:        sin = satosin(&ipforward_rt.ro_dst);
1.1       cgd       849:
                    850:        if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
                    851:                if (ipforward_rt.ro_rt) {
                    852:                        RTFREE(ipforward_rt.ro_rt);
                    853:                        ipforward_rt.ro_rt = 0;
                    854:                }
                    855:                sin->sin_family = AF_INET;
                    856:                sin->sin_len = sizeof(*sin);
                    857:                sin->sin_addr = dst;
                    858:
                    859:                rtalloc(&ipforward_rt);
                    860:        }
                    861:        if (ipforward_rt.ro_rt == 0)
                    862:                return ((struct in_ifaddr *)0);
1.19      mycroft   863:        return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1.1       cgd       864: }
                    865:
                    866: /*
                    867:  * Save incoming source route for use in replies,
                    868:  * to be picked up later by ip_srcroute if the receiver is interested.
                    869:  */
1.13      mycroft   870: void
1.1       cgd       871: save_rte(option, dst)
                    872:        u_char *option;
                    873:        struct in_addr dst;
                    874: {
                    875:        unsigned olen;
                    876:
                    877:        olen = option[IPOPT_OLEN];
                    878: #ifdef DIAGNOSTIC
                    879:        if (ipprintfs)
                    880:                printf("save_rte: olen %d\n", olen);
                    881: #endif
                    882:        if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
                    883:                return;
                    884:        bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
                    885:        ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
                    886:        ip_srcrt.dst = dst;
                    887: }
                    888:
                    889: /*
                    890:  * Retrieve incoming source route for use in replies,
                    891:  * in the same form used by setsockopt.
                    892:  * The first hop is placed before the options, will be removed later.
                    893:  */
                    894: struct mbuf *
                    895: ip_srcroute()
                    896: {
                    897:        register struct in_addr *p, *q;
                    898:        register struct mbuf *m;
                    899:
                    900:        if (ip_nhops == 0)
                    901:                return ((struct mbuf *)0);
                    902:        m = m_get(M_DONTWAIT, MT_SOOPTS);
                    903:        if (m == 0)
                    904:                return ((struct mbuf *)0);
                    905:
1.13      mycroft   906: #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1.1       cgd       907:
                    908:        /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
                    909:        m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
                    910:            OPTSIZ;
                    911: #ifdef DIAGNOSTIC
                    912:        if (ipprintfs)
                    913:                printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
                    914: #endif
                    915:
                    916:        /*
                    917:         * First save first hop for return route
                    918:         */
                    919:        p = &ip_srcrt.route[ip_nhops - 1];
                    920:        *(mtod(m, struct in_addr *)) = *p--;
                    921: #ifdef DIAGNOSTIC
                    922:        if (ipprintfs)
1.30      christos  923:                printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1.1       cgd       924: #endif
                    925:
                    926:        /*
                    927:         * Copy option fields and padding (nop) to mbuf.
                    928:         */
                    929:        ip_srcrt.nop = IPOPT_NOP;
                    930:        ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
                    931:        bcopy((caddr_t)&ip_srcrt.nop,
                    932:            mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
                    933:        q = (struct in_addr *)(mtod(m, caddr_t) +
                    934:            sizeof(struct in_addr) + OPTSIZ);
                    935: #undef OPTSIZ
                    936:        /*
                    937:         * Record return path as an IP source route,
                    938:         * reversing the path (pointers are now aligned).
                    939:         */
                    940:        while (p >= ip_srcrt.route) {
                    941: #ifdef DIAGNOSTIC
                    942:                if (ipprintfs)
1.30      christos  943:                        printf(" %x", ntohl(q->s_addr));
1.1       cgd       944: #endif
                    945:                *q++ = *p--;
                    946:        }
                    947:        /*
                    948:         * Last hop goes to final destination.
                    949:         */
                    950:        *q = ip_srcrt.dst;
                    951: #ifdef DIAGNOSTIC
                    952:        if (ipprintfs)
1.30      christos  953:                printf(" %x\n", ntohl(q->s_addr));
1.1       cgd       954: #endif
                    955:        return (m);
                    956: }
                    957:
                    958: /*
                    959:  * Strip out IP options, at higher
                    960:  * level protocol in the kernel.
                    961:  * Second argument is buffer to which options
                    962:  * will be moved, and return value is their length.
                    963:  * XXX should be deleted; last arg currently ignored.
                    964:  */
1.8       mycroft   965: void
1.1       cgd       966: ip_stripoptions(m, mopt)
                    967:        register struct mbuf *m;
                    968:        struct mbuf *mopt;
                    969: {
                    970:        register int i;
                    971:        struct ip *ip = mtod(m, struct ip *);
                    972:        register caddr_t opts;
                    973:        int olen;
                    974:
                    975:        olen = (ip->ip_hl<<2) - sizeof (struct ip);
                    976:        opts = (caddr_t)(ip + 1);
                    977:        i = m->m_len - (sizeof (struct ip) + olen);
                    978:        bcopy(opts  + olen, opts, (unsigned)i);
                    979:        m->m_len -= olen;
                    980:        if (m->m_flags & M_PKTHDR)
                    981:                m->m_pkthdr.len -= olen;
                    982:        ip->ip_hl = sizeof(struct ip) >> 2;
                    983: }
                    984:
1.23      mycroft   985: int inetctlerrmap[PRC_NCMDS] = {
1.1       cgd       986:        0,              0,              0,              0,
                    987:        0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
                    988:        EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
                    989:        EMSGSIZE,       EHOSTUNREACH,   0,              0,
                    990:        0,              0,              0,              0,
                    991:        ENOPROTOOPT
                    992: };
                    993:
                    994: /*
                    995:  * Forward a packet.  If some error occurs return the sender
                    996:  * an icmp packet.  Note we can't always generate a meaningful
                    997:  * icmp message because icmp doesn't have a large enough repertoire
                    998:  * of codes and types.
                    999:  *
                   1000:  * If not forwarding, just drop the packet.  This could be confusing
                   1001:  * if ipforwarding was zero but some routing protocol was advancing
                   1002:  * us as a gateway to somewhere.  However, we must let the routing
                   1003:  * protocol deal with that.
                   1004:  *
                   1005:  * The srcrt parameter indicates whether the packet is being forwarded
                   1006:  * via a source route.
                   1007:  */
1.13      mycroft  1008: void
1.1       cgd      1009: ip_forward(m, srcrt)
                   1010:        struct mbuf *m;
                   1011:        int srcrt;
                   1012: {
                   1013:        register struct ip *ip = mtod(m, struct ip *);
                   1014:        register struct sockaddr_in *sin;
                   1015:        register struct rtentry *rt;
1.28      christos 1016:        int error, type = 0, code = 0;
1.1       cgd      1017:        struct mbuf *mcopy;
1.13      mycroft  1018:        n_long dest;
                   1019:        struct ifnet *destifp;
1.1       cgd      1020:
1.13      mycroft  1021:        dest = 0;
1.1       cgd      1022: #ifdef DIAGNOSTIC
                   1023:        if (ipprintfs)
1.28      christos 1024:                printf("forward: src %x dst %x ttl %x\n",
                   1025:                       ip->ip_src.s_addr, ip->ip_dst.s_addr, ip->ip_ttl);
1.1       cgd      1026: #endif
                   1027:        if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
                   1028:                ipstat.ips_cantforward++;
                   1029:                m_freem(m);
                   1030:                return;
                   1031:        }
                   1032:        HTONS(ip->ip_id);
                   1033:        if (ip->ip_ttl <= IPTTLDEC) {
1.13      mycroft  1034:                icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1.1       cgd      1035:                return;
                   1036:        }
                   1037:        ip->ip_ttl -= IPTTLDEC;
                   1038:
1.19      mycroft  1039:        sin = satosin(&ipforward_rt.ro_dst);
1.1       cgd      1040:        if ((rt = ipforward_rt.ro_rt) == 0 ||
                   1041:            ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
                   1042:                if (ipforward_rt.ro_rt) {
                   1043:                        RTFREE(ipforward_rt.ro_rt);
                   1044:                        ipforward_rt.ro_rt = 0;
                   1045:                }
                   1046:                sin->sin_family = AF_INET;
                   1047:                sin->sin_len = sizeof(*sin);
                   1048:                sin->sin_addr = ip->ip_dst;
                   1049:
                   1050:                rtalloc(&ipforward_rt);
                   1051:                if (ipforward_rt.ro_rt == 0) {
1.13      mycroft  1052:                        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1.1       cgd      1053:                        return;
                   1054:                }
                   1055:                rt = ipforward_rt.ro_rt;
                   1056:        }
                   1057:
                   1058:        /*
                   1059:         * Save at most 64 bytes of the packet in case
                   1060:         * we need to generate an ICMP message to the src.
                   1061:         */
                   1062:        mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
                   1063:
                   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.30      christos 1088:                        printf("redirect (%d) to %x\n", code, (u_int32_t)dest);
1.1       cgd      1089: #endif
                   1090:                }
                   1091:        }
                   1092:
1.27      thorpej  1093:        error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
                   1094:            (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 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
1.26      thorpej  1167:        case IPCTL_FORWSRCRT:
                   1168:                /*
                   1169:                 * Don't allow this to change in a secure environment.
                   1170:                 */
                   1171:                if (securelevel > 0)
                   1172:                        return (EPERM);
                   1173:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1174:                    &ip_forwsrcrt));
1.27      thorpej  1175:        case IPCTL_DIRECTEDBCAST:
                   1176:                return (sysctl_int(oldp, oldlenp, newp, newlen,
                   1177:                    &ip_directedbcast));
1.13      mycroft  1178:        default:
                   1179:                return (EOPNOTSUPP);
                   1180:        }
                   1181:        /* NOTREACHED */
1.1       cgd      1182: }

CVSweb <webmaster@jp.NetBSD.org>