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

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

CVSweb <webmaster@jp.NetBSD.org>