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

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

CVSweb <webmaster@jp.NetBSD.org>