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

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

CVSweb <webmaster@jp.NetBSD.org>