Annotation of qemu/slirp/ip_input.c, revision 1.1.1.4

1.1       root        1: /*
                      2:  * Copyright (c) 1982, 1986, 1988, 1993
                      3:  *     The Regents of the University of California.  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.
1.1.1.4 ! root       13:  * 3. Neither the name of the University nor the names of its contributors
1.1       root       14:  *    may be used to endorse or promote products derived from this software
                     15:  *    without specific prior written permission.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  *
                     29:  *     @(#)ip_input.c  8.2 (Berkeley) 1/4/94
                     30:  * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
                     31:  */
                     32: 
                     33: /*
                     34:  * Changes and additions relating to SLiRP are
                     35:  * Copyright (c) 1995 Danny Gasparovski.
1.1.1.3   root       36:  *
1.1       root       37:  * Please read the file COPYRIGHT for the
                     38:  * terms and conditions of the copyright.
                     39:  */
                     40: 
                     41: #include <slirp.h>
1.1.1.4 ! root       42: #include <osdep.h>
1.1       root       43: #include "ip_icmp.h"
                     44: 
1.1.1.3   root       45: #ifdef LOG_ENABLED
1.1       root       46: struct ipstat ipstat;
1.1.1.3   root       47: #endif
                     48: 
1.1       root       49: struct ipq ipq;
                     50: 
1.1.1.4 ! root       51: static struct ip *ip_reass(register struct ip *ip,
1.1.1.3   root       52:                            register struct ipq *fp);
                     53: static void ip_freef(struct ipq *fp);
                     54: static void ip_enq(register struct ipasfrag *p,
                     55:                    register struct ipasfrag *prev);
                     56: static void ip_deq(register struct ipasfrag *p);
                     57: 
1.1       root       58: /*
                     59:  * IP initialization: fill in IP protocol switch table.
                     60:  * All protocols not implemented in kernel go to raw IP protocol handler.
                     61:  */
                     62: void
                     63: ip_init()
                     64: {
1.1.1.4 ! root       65:        ipq.ip_link.next = ipq.ip_link.prev = &ipq.ip_link;
1.1       root       66:        ip_id = tt.tv_sec & 0xffff;
                     67:        udp_init();
                     68:        tcp_init();
                     69: }
                     70: 
                     71: /*
                     72:  * Ip input routine.  Checksum and byte swap header.  If fragmented
                     73:  * try to reassemble.  Process options.  Pass to next level.
                     74:  */
                     75: void
                     76: ip_input(m)
                     77:        struct mbuf *m;
                     78: {
                     79:        register struct ip *ip;
                     80:        int hlen;
1.1.1.3   root       81: 
1.1       root       82:        DEBUG_CALL("ip_input");
                     83:        DEBUG_ARG("m = %lx", (long)m);
                     84:        DEBUG_ARG("m_len = %d", m->m_len);
                     85: 
1.1.1.3   root       86:        STAT(ipstat.ips_total++);
                     87: 
1.1       root       88:        if (m->m_len < sizeof (struct ip)) {
1.1.1.3   root       89:                STAT(ipstat.ips_toosmall++);
1.1       root       90:                return;
                     91:        }
1.1.1.3   root       92: 
1.1       root       93:        ip = mtod(m, struct ip *);
1.1.1.3   root       94: 
1.1       root       95:        if (ip->ip_v != IPVERSION) {
1.1.1.3   root       96:                STAT(ipstat.ips_badvers++);
1.1       root       97:                goto bad;
                     98:        }
                     99: 
                    100:        hlen = ip->ip_hl << 2;
                    101:        if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
1.1.1.3   root      102:          STAT(ipstat.ips_badhlen++);                     /* or packet too short */
1.1       root      103:          goto bad;
                    104:        }
                    105: 
                    106:         /* keep ip header intact for ICMP reply
1.1.1.3   root      107:         * ip->ip_sum = cksum(m, hlen);
                    108:         * if (ip->ip_sum) {
1.1       root      109:         */
                    110:        if(cksum(m,hlen)) {
1.1.1.3   root      111:          STAT(ipstat.ips_badsum++);
1.1       root      112:          goto bad;
                    113:        }
                    114: 
                    115:        /*
                    116:         * Convert fields to host representation.
                    117:         */
                    118:        NTOHS(ip->ip_len);
                    119:        if (ip->ip_len < hlen) {
1.1.1.3   root      120:                STAT(ipstat.ips_badlen++);
1.1       root      121:                goto bad;
                    122:        }
                    123:        NTOHS(ip->ip_id);
                    124:        NTOHS(ip->ip_off);
                    125: 
                    126:        /*
                    127:         * Check that the amount of data in the buffers
                    128:         * is as at least much as the IP header would have us expect.
                    129:         * Trim mbufs if longer than we expect.
                    130:         * Drop packet if shorter than we expect.
                    131:         */
                    132:        if (m->m_len < ip->ip_len) {
1.1.1.3   root      133:                STAT(ipstat.ips_tooshort++);
1.1       root      134:                goto bad;
                    135:        }
1.1.1.4 ! root      136: 
        !           137:     if (slirp_restrict) {
        !           138:         if (memcmp(&ip->ip_dst.s_addr, &special_addr, 3)) {
        !           139:             if (ip->ip_dst.s_addr == 0xffffffff && ip->ip_p != IPPROTO_UDP)
        !           140:                 goto bad;
        !           141:         } else {
        !           142:             int host = ntohl(ip->ip_dst.s_addr) & 0xff;
        !           143:             struct ex_list *ex_ptr;
        !           144: 
        !           145:             if (host == 0xff)
        !           146:                 goto bad;
        !           147: 
        !           148:             for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
        !           149:                 if (ex_ptr->ex_addr == host)
        !           150:                     break;
        !           151: 
        !           152:             if (!ex_ptr)
        !           153:                 goto bad;
        !           154:         }
        !           155:     }
        !           156: 
1.1       root      157:        /* Should drop packet if mbuf too long? hmmm... */
                    158:        if (m->m_len > ip->ip_len)
                    159:           m_adj(m, ip->ip_len - m->m_len);
                    160: 
                    161:        /* check ip_ttl for a correct ICMP reply */
                    162:        if(ip->ip_ttl==0 || ip->ip_ttl==1) {
                    163:          icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
                    164:          goto bad;
                    165:        }
                    166: 
                    167:        /*
                    168:         * Process options and, if not destined for us,
                    169:         * ship it on.  ip_dooptions returns 1 when an
                    170:         * error was detected (causing an icmp message
                    171:         * to be sent and the original packet to be freed).
                    172:         */
                    173: /* We do no IP options */
                    174: /*     if (hlen > sizeof (struct ip) && ip_dooptions(m))
                    175:  *             goto next;
                    176:  */
                    177:        /*
                    178:         * If offset or IP_MF are set, must reassemble.
                    179:         * Otherwise, nothing need be done.
                    180:         * (We could look in the reassembly queue to see
                    181:         * if the packet was previously fragmented,
                    182:         * but it's not worth the time; just let them time out.)
1.1.1.3   root      183:         *
1.1       root      184:         * XXX This should fail, don't fragment yet
                    185:         */
                    186:        if (ip->ip_off &~ IP_DF) {
                    187:          register struct ipq *fp;
1.1.1.4 ! root      188:       struct qlink *l;
1.1       root      189:                /*
                    190:                 * Look for queue of fragments
                    191:                 * of this datagram.
                    192:                 */
1.1.1.4 ! root      193:                for (l = ipq.ip_link.next; l != &ipq.ip_link; l = l->next) {
        !           194:             fp = container_of(l, struct ipq, ip_link);
        !           195:             if (ip->ip_id == fp->ipq_id &&
        !           196:                     ip->ip_src.s_addr == fp->ipq_src.s_addr &&
        !           197:                     ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
        !           198:                     ip->ip_p == fp->ipq_p)
1.1       root      199:                    goto found;
1.1.1.4 ! root      200:         }
        !           201:         fp = NULL;
1.1       root      202:        found:
                    203: 
                    204:                /*
                    205:                 * Adjust ip_len to not reflect header,
                    206:                 * set ip_mff if more fragments are expected,
                    207:                 * convert offset of this to bytes.
                    208:                 */
                    209:                ip->ip_len -= hlen;
                    210:                if (ip->ip_off & IP_MF)
1.1.1.4 ! root      211:                  ip->ip_tos |= 1;
1.1.1.3   root      212:                else
1.1.1.4 ! root      213:                  ip->ip_tos &= ~1;
1.1       root      214: 
                    215:                ip->ip_off <<= 3;
                    216: 
                    217:                /*
                    218:                 * If datagram marked as having more fragments
                    219:                 * or if this is not the first fragment,
                    220:                 * attempt reassembly; if it succeeds, proceed.
                    221:                 */
1.1.1.4 ! root      222:                if (ip->ip_tos & 1 || ip->ip_off) {
1.1.1.3   root      223:                        STAT(ipstat.ips_fragments++);
1.1.1.4 ! root      224:                        ip = ip_reass(ip, fp);
1.1       root      225:                        if (ip == 0)
                    226:                                return;
1.1.1.3   root      227:                        STAT(ipstat.ips_reassembled++);
1.1       root      228:                        m = dtom(ip);
                    229:                } else
                    230:                        if (fp)
                    231:                           ip_freef(fp);
                    232: 
                    233:        } else
                    234:                ip->ip_len -= hlen;
                    235: 
                    236:        /*
                    237:         * Switch out to protocol's input routine.
                    238:         */
1.1.1.3   root      239:        STAT(ipstat.ips_delivered++);
1.1       root      240:        switch (ip->ip_p) {
                    241:         case IPPROTO_TCP:
                    242:                tcp_input(m, hlen, (struct socket *)NULL);
                    243:                break;
                    244:         case IPPROTO_UDP:
                    245:                udp_input(m, hlen);
                    246:                break;
                    247:         case IPPROTO_ICMP:
                    248:                icmp_input(m, hlen);
                    249:                break;
                    250:         default:
1.1.1.3   root      251:                STAT(ipstat.ips_noproto++);
1.1       root      252:                m_free(m);
                    253:        }
                    254:        return;
                    255: bad:
                    256:        m_freem(m);
                    257:        return;
                    258: }
                    259: 
1.1.1.4 ! root      260: #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
        !           261: #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
1.1       root      262: /*
                    263:  * Take incoming datagram fragment and try to
                    264:  * reassemble it into whole datagram.  If a chain for
                    265:  * reassembly of this datagram already exists, then it
                    266:  * is given as fp; otherwise have to make a chain.
                    267:  */
1.1.1.3   root      268: static struct ip *
1.1.1.4 ! root      269: ip_reass(register struct ip *ip, register struct ipq *fp)
1.1       root      270: {
                    271:        register struct mbuf *m = dtom(ip);
                    272:        register struct ipasfrag *q;
                    273:        int hlen = ip->ip_hl << 2;
                    274:        int i, next;
1.1.1.3   root      275: 
1.1       root      276:        DEBUG_CALL("ip_reass");
                    277:        DEBUG_ARG("ip = %lx", (long)ip);
                    278:        DEBUG_ARG("fp = %lx", (long)fp);
                    279:        DEBUG_ARG("m = %lx", (long)m);
                    280: 
                    281:        /*
                    282:         * Presence of header sizes in mbufs
                    283:         * would confuse code below.
                    284:          * Fragment m_data is concatenated.
                    285:         */
                    286:        m->m_data += hlen;
                    287:        m->m_len -= hlen;
                    288: 
                    289:        /*
                    290:         * If first fragment to arrive, create a reassembly queue.
                    291:         */
                    292:        if (fp == 0) {
                    293:          struct mbuf *t;
                    294:          if ((t = m_get()) == NULL) goto dropfrag;
                    295:          fp = mtod(t, struct ipq *);
1.1.1.4 ! root      296:          insque(&fp->ip_link, &ipq.ip_link);
1.1       root      297:          fp->ipq_ttl = IPFRAGTTL;
                    298:          fp->ipq_p = ip->ip_p;
                    299:          fp->ipq_id = ip->ip_id;
1.1.1.4 ! root      300:          fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
        !           301:          fp->ipq_src = ip->ip_src;
        !           302:          fp->ipq_dst = ip->ip_dst;
1.1       root      303:          q = (struct ipasfrag *)fp;
                    304:          goto insert;
                    305:        }
1.1.1.3   root      306: 
1.1       root      307:        /*
                    308:         * Find a segment which begins after this one does.
                    309:         */
1.1.1.4 ! root      310:        for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
        !           311:             q = q->ipf_next)
        !           312:                if (q->ipf_off > ip->ip_off)
1.1       root      313:                        break;
                    314: 
                    315:        /*
                    316:         * If there is a preceding segment, it may provide some of
                    317:         * our data already.  If so, drop the data from the incoming
                    318:         * segment.  If it provides all of our data, drop us.
                    319:         */
1.1.1.4 ! root      320:        if (q->ipf_prev != &fp->frag_link) {
        !           321:         struct ipasfrag *pq = q->ipf_prev;
        !           322:                i = pq->ipf_off + pq->ipf_len - ip->ip_off;
1.1       root      323:                if (i > 0) {
                    324:                        if (i >= ip->ip_len)
                    325:                                goto dropfrag;
                    326:                        m_adj(dtom(ip), i);
                    327:                        ip->ip_off += i;
                    328:                        ip->ip_len -= i;
                    329:                }
                    330:        }
                    331: 
                    332:        /*
                    333:         * While we overlap succeeding segments trim them or,
                    334:         * if they are completely covered, dequeue them.
                    335:         */
1.1.1.4 ! root      336:        while (q != (struct ipasfrag*)&fp->frag_link &&
        !           337:             ip->ip_off + ip->ip_len > q->ipf_off) {
        !           338:                i = (ip->ip_off + ip->ip_len) - q->ipf_off;
        !           339:                if (i < q->ipf_len) {
        !           340:                        q->ipf_len -= i;
        !           341:                        q->ipf_off += i;
1.1       root      342:                        m_adj(dtom(q), i);
                    343:                        break;
                    344:                }
1.1.1.4 ! root      345:                q = q->ipf_next;
        !           346:                m_freem(dtom(q->ipf_prev));
        !           347:                ip_deq(q->ipf_prev);
1.1       root      348:        }
                    349: 
                    350: insert:
                    351:        /*
                    352:         * Stick new segment in its place;
                    353:         * check for complete reassembly.
                    354:         */
1.1.1.4 ! root      355:        ip_enq(iptofrag(ip), q->ipf_prev);
1.1       root      356:        next = 0;
1.1.1.4 ! root      357:        for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
        !           358:             q = q->ipf_next) {
        !           359:                if (q->ipf_off != next)
1.1       root      360:                        return (0);
1.1.1.4 ! root      361:                next += q->ipf_len;
1.1       root      362:        }
1.1.1.4 ! root      363:        if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
1.1       root      364:                return (0);
                    365: 
                    366:        /*
                    367:         * Reassembly is complete; concatenate fragments.
                    368:         */
1.1.1.4 ! root      369:     q = fp->frag_link.next;
1.1       root      370:        m = dtom(q);
                    371: 
                    372:        q = (struct ipasfrag *) q->ipf_next;
1.1.1.4 ! root      373:        while (q != (struct ipasfrag*)&fp->frag_link) {
        !           374:          struct mbuf *t = dtom(q);
1.1       root      375:          q = (struct ipasfrag *) q->ipf_next;
1.1.1.2   root      376:          m_cat(m, t);
1.1       root      377:        }
                    378: 
                    379:        /*
                    380:         * Create header for new ip packet by
                    381:         * modifying header of first packet;
                    382:         * dequeue and discard fragment reassembly header.
                    383:         * Make header visible.
                    384:         */
1.1.1.4 ! root      385:        q = fp->frag_link.next;
1.1       root      386: 
                    387:        /*
                    388:         * If the fragments concatenated to an mbuf that's
                    389:         * bigger than the total size of the fragment, then and
                    390:         * m_ext buffer was alloced. But fp->ipq_next points to
                    391:         * the old buffer (in the mbuf), so we must point ip
                    392:         * into the new buffer.
                    393:         */
                    394:        if (m->m_flags & M_EXT) {
1.1.1.4 ! root      395:          int delta = (char *)q - m->m_dat;
        !           396:          q = (struct ipasfrag *)(m->m_ext + delta);
1.1       root      397:        }
                    398: 
1.1.1.3   root      399:        /* DEBUG_ARG("ip = %lx", (long)ip);
1.1       root      400:         * ip=(struct ipasfrag *)m->m_data; */
                    401: 
1.1.1.4 ! root      402:     ip = fragtoip(q);
1.1       root      403:        ip->ip_len = next;
1.1.1.4 ! root      404:        ip->ip_tos &= ~1;
        !           405:        ip->ip_src = fp->ipq_src;
        !           406:        ip->ip_dst = fp->ipq_dst;
        !           407:        remque(&fp->ip_link);
1.1       root      408:        (void) m_free(dtom(fp));
                    409:        m->m_len += (ip->ip_hl << 2);
                    410:        m->m_data -= (ip->ip_hl << 2);
                    411: 
1.1.1.4 ! root      412:        return ip;
1.1       root      413: 
                    414: dropfrag:
1.1.1.3   root      415:        STAT(ipstat.ips_fragdropped++);
1.1       root      416:        m_freem(m);
                    417:        return (0);
                    418: }
                    419: 
                    420: /*
                    421:  * Free a fragment reassembly header and all
                    422:  * associated datagrams.
                    423:  */
1.1.1.3   root      424: static void
                    425: ip_freef(struct ipq *fp)
1.1       root      426: {
                    427:        register struct ipasfrag *q, *p;
                    428: 
1.1.1.4 ! root      429:        for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
        !           430:                p = q->ipf_next;
1.1       root      431:                ip_deq(q);
                    432:                m_freem(dtom(q));
                    433:        }
1.1.1.4 ! root      434:        remque(&fp->ip_link);
1.1       root      435:        (void) m_free(dtom(fp));
                    436: }
                    437: 
                    438: /*
                    439:  * Put an ip fragment on a reassembly chain.
                    440:  * Like insque, but pointers in middle of structure.
                    441:  */
1.1.1.3   root      442: static void
                    443: ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
1.1       root      444: {
                    445:        DEBUG_CALL("ip_enq");
                    446:        DEBUG_ARG("prev = %lx", (long)prev);
1.1.1.4 ! root      447:        p->ipf_prev =  prev;
1.1       root      448:        p->ipf_next = prev->ipf_next;
1.1.1.4 ! root      449:        ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
        !           450:        prev->ipf_next = p;
1.1       root      451: }
                    452: 
                    453: /*
                    454:  * To ip_enq as remque is to insque.
                    455:  */
1.1.1.3   root      456: static void
                    457: ip_deq(register struct ipasfrag *p)
1.1       root      458: {
                    459:        ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
                    460:        ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
                    461: }
                    462: 
                    463: /*
                    464:  * IP timer processing;
                    465:  * if a timer expires on a reassembly
                    466:  * queue, discard it.
                    467:  */
                    468: void
                    469: ip_slowtimo()
                    470: {
1.1.1.4 ! root      471:     struct qlink *l;
1.1.1.3   root      472: 
1.1       root      473:        DEBUG_CALL("ip_slowtimo");
1.1.1.3   root      474: 
1.1.1.4 ! root      475:     l = ipq.ip_link.next;
        !           476: 
        !           477:        if (l == 0)
1.1       root      478:           return;
                    479: 
1.1.1.4 ! root      480:        while (l != &ipq.ip_link) {
        !           481:         struct ipq *fp = container_of(l, struct ipq, ip_link);
        !           482:         l = l->next;
        !           483:                if (--fp->ipq_ttl == 0) {
1.1.1.3   root      484:                        STAT(ipstat.ips_fragtimeout++);
1.1.1.4 ! root      485:                        ip_freef(fp);
1.1       root      486:                }
                    487:        }
                    488: }
                    489: 
                    490: /*
                    491:  * Do option processing on a datagram,
                    492:  * possibly discarding it if bad options are encountered,
                    493:  * or forwarding it if source-routed.
                    494:  * Returns 1 if packet has been forwarded/freed,
                    495:  * 0 if the packet should be processed further.
                    496:  */
                    497: 
                    498: #ifdef notdef
                    499: 
                    500: int
                    501: ip_dooptions(m)
                    502:        struct mbuf *m;
                    503: {
                    504:        register struct ip *ip = mtod(m, struct ip *);
                    505:        register u_char *cp;
                    506:        register struct ip_timestamp *ipt;
                    507:        register struct in_ifaddr *ia;
                    508: /*     int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
                    509:        int opt, optlen, cnt, off, code, type, forward = 0;
                    510:        struct in_addr *sin, dst;
                    511: typedef u_int32_t n_time;
                    512:        n_time ntime;
                    513: 
                    514:        dst = ip->ip_dst;
                    515:        cp = (u_char *)(ip + 1);
                    516:        cnt = (ip->ip_hl << 2) - sizeof (struct ip);
                    517:        for (; cnt > 0; cnt -= optlen, cp += optlen) {
                    518:                opt = cp[IPOPT_OPTVAL];
                    519:                if (opt == IPOPT_EOL)
                    520:                        break;
                    521:                if (opt == IPOPT_NOP)
                    522:                        optlen = 1;
                    523:                else {
                    524:                        optlen = cp[IPOPT_OLEN];
                    525:                        if (optlen <= 0 || optlen > cnt) {
                    526:                                code = &cp[IPOPT_OLEN] - (u_char *)ip;
                    527:                                goto bad;
                    528:                        }
                    529:                }
                    530:                switch (opt) {
                    531: 
                    532:                default:
                    533:                        break;
                    534: 
                    535:                /*
                    536:                 * Source routing with record.
                    537:                 * Find interface with current destination address.
                    538:                 * If none on this machine then drop if strictly routed,
                    539:                 * or do nothing if loosely routed.
                    540:                 * Record interface address and bring up next address
                    541:                 * component.  If strictly routed make sure next
                    542:                 * address is on directly accessible net.
                    543:                 */
                    544:                case IPOPT_LSRR:
                    545:                case IPOPT_SSRR:
                    546:                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
                    547:                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
                    548:                                goto bad;
                    549:                        }
                    550:                        ipaddr.sin_addr = ip->ip_dst;
                    551:                        ia = (struct in_ifaddr *)
                    552:                                ifa_ifwithaddr((struct sockaddr *)&ipaddr);
                    553:                        if (ia == 0) {
                    554:                                if (opt == IPOPT_SSRR) {
                    555:                                        type = ICMP_UNREACH;
                    556:                                        code = ICMP_UNREACH_SRCFAIL;
                    557:                                        goto bad;
                    558:                                }
                    559:                                /*
                    560:                                 * Loose routing, and not at next destination
                    561:                                 * yet; nothing to do except forward.
                    562:                                 */
                    563:                                break;
                    564:                        }
                    565:                        off--;                  / * 0 origin *  /
                    566:                        if (off > optlen - sizeof(struct in_addr)) {
                    567:                                /*
                    568:                                 * End of source route.  Should be for us.
                    569:                                 */
                    570:                                save_rte(cp, ip->ip_src);
                    571:                                break;
                    572:                        }
                    573:                        /*
                    574:                         * locate outgoing interface
                    575:                         */
                    576:                        bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
                    577:                            sizeof(ipaddr.sin_addr));
                    578:                        if (opt == IPOPT_SSRR) {
                    579: #define        INA     struct in_ifaddr *
                    580: #define        SA      struct sockaddr *
                    581:                            if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
                    582:                                ia = (INA)ifa_ifwithnet((SA)&ipaddr);
                    583:                        } else
                    584:                                ia = ip_rtaddr(ipaddr.sin_addr);
                    585:                        if (ia == 0) {
                    586:                                type = ICMP_UNREACH;
                    587:                                code = ICMP_UNREACH_SRCFAIL;
                    588:                                goto bad;
                    589:                        }
                    590:                        ip->ip_dst = ipaddr.sin_addr;
                    591:                        bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
                    592:                            (caddr_t)(cp + off), sizeof(struct in_addr));
                    593:                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
                    594:                        /*
                    595:                         * Let ip_intr's mcast routing check handle mcast pkts
                    596:                         */
                    597:                        forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
                    598:                        break;
                    599: 
                    600:                case IPOPT_RR:
                    601:                        if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
                    602:                                code = &cp[IPOPT_OFFSET] - (u_char *)ip;
                    603:                                goto bad;
                    604:                        }
                    605:                        /*
                    606:                         * If no space remains, ignore.
                    607:                         */
                    608:                        off--;                   * 0 origin *
                    609:                        if (off > optlen - sizeof(struct in_addr))
                    610:                                break;
                    611:                        bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
                    612:                            sizeof(ipaddr.sin_addr));
                    613:                        /*
                    614:                         * locate outgoing interface; if we're the destination,
                    615:                         * use the incoming interface (should be same).
                    616:                         */
                    617:                        if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
                    618:                            (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
                    619:                                type = ICMP_UNREACH;
                    620:                                code = ICMP_UNREACH_HOST;
                    621:                                goto bad;
                    622:                        }
                    623:                        bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
                    624:                            (caddr_t)(cp + off), sizeof(struct in_addr));
                    625:                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
                    626:                        break;
                    627: 
                    628:                case IPOPT_TS:
                    629:                        code = cp - (u_char *)ip;
                    630:                        ipt = (struct ip_timestamp *)cp;
                    631:                        if (ipt->ipt_len < 5)
                    632:                                goto bad;
                    633:                        if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
                    634:                                if (++ipt->ipt_oflw == 0)
                    635:                                        goto bad;
                    636:                                break;
                    637:                        }
                    638:                        sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
                    639:                        switch (ipt->ipt_flg) {
                    640: 
                    641:                        case IPOPT_TS_TSONLY:
                    642:                                break;
                    643: 
                    644:                        case IPOPT_TS_TSANDADDR:
                    645:                                if (ipt->ipt_ptr + sizeof(n_time) +
                    646:                                    sizeof(struct in_addr) > ipt->ipt_len)
                    647:                                        goto bad;
                    648:                                ipaddr.sin_addr = dst;
                    649:                                ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
                    650:                                                            m->m_pkthdr.rcvif);
                    651:                                if (ia == 0)
                    652:                                        continue;
                    653:                                bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
                    654:                                    (caddr_t)sin, sizeof(struct in_addr));
                    655:                                ipt->ipt_ptr += sizeof(struct in_addr);
                    656:                                break;
                    657: 
                    658:                        case IPOPT_TS_PRESPEC:
                    659:                                if (ipt->ipt_ptr + sizeof(n_time) +
                    660:                                    sizeof(struct in_addr) > ipt->ipt_len)
                    661:                                        goto bad;
                    662:                                bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
                    663:                                    sizeof(struct in_addr));
                    664:                                if (ifa_ifwithaddr((SA)&ipaddr) == 0)
                    665:                                        continue;
                    666:                                ipt->ipt_ptr += sizeof(struct in_addr);
                    667:                                break;
                    668: 
                    669:                        default:
                    670:                                goto bad;
                    671:                        }
                    672:                        ntime = iptime();
                    673:                        bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
                    674:                            sizeof(n_time));
                    675:                        ipt->ipt_ptr += sizeof(n_time);
                    676:                }
                    677:        }
                    678:        if (forward) {
                    679:                ip_forward(m, 1);
                    680:                return (1);
                    681:        }
                    682:                }
                    683:        }
                    684:        return (0);
                    685: bad:
                    686:        /* ip->ip_len -= ip->ip_hl << 2;   XXX icmp_error adds in hdr length */
                    687: 
                    688: /* Not yet */
                    689:        icmp_error(m, type, code, 0, 0);
                    690: 
1.1.1.3   root      691:        STAT(ipstat.ips_badoptions++);
1.1       root      692:        return (1);
                    693: }
                    694: 
                    695: #endif /* notdef */
                    696: 
                    697: /*
                    698:  * Strip out IP options, at higher
                    699:  * level protocol in the kernel.
                    700:  * Second argument is buffer to which options
                    701:  * will be moved, and return value is their length.
                    702:  * (XXX) should be deleted; last arg currently ignored.
                    703:  */
                    704: void
                    705: ip_stripoptions(m, mopt)
                    706:        register struct mbuf *m;
                    707:        struct mbuf *mopt;
                    708: {
                    709:        register int i;
                    710:        struct ip *ip = mtod(m, struct ip *);
                    711:        register caddr_t opts;
                    712:        int olen;
                    713: 
                    714:        olen = (ip->ip_hl<<2) - sizeof (struct ip);
                    715:        opts = (caddr_t)(ip + 1);
                    716:        i = m->m_len - (sizeof (struct ip) + olen);
                    717:        memcpy(opts, opts  + olen, (unsigned)i);
                    718:        m->m_len -= olen;
1.1.1.3   root      719: 
1.1       root      720:        ip->ip_hl = sizeof(struct ip) >> 2;
                    721: }

unix.superglobalmegacorp.com

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