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

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

CVSweb <webmaster@jp.NetBSD.org>