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

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

CVSweb <webmaster@jp.NetBSD.org>