Annotation of kernel/bsd/netinet/ip_input.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.