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

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

CVSweb <webmaster@jp.NetBSD.org>