qemu/slirp/src/ip_input.c
<<
>>
Prefs
   1/* SPDX-License-Identifier: BSD-3-Clause */
   2/*
   3 * Copyright (c) 1982, 1986, 1988, 1993
   4 *      The Regents of the University of California.  All rights reserved.
   5 *
   6 * Redistribution and use in source and binary forms, with or without
   7 * modification, are permitted provided that the following conditions
   8 * are met:
   9 * 1. Redistributions of source code must retain the above copyright
  10 *    notice, this list of conditions and the following disclaimer.
  11 * 2. Redistributions in binary form must reproduce the above copyright
  12 *    notice, this list of conditions and the following disclaimer in the
  13 *    documentation and/or other materials provided with the distribution.
  14 * 3. Neither the name of the University nor the names of its contributors
  15 *    may be used to endorse or promote products derived from this software
  16 *    without specific prior written permission.
  17 *
  18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28 * SUCH DAMAGE.
  29 *
  30 *      @(#)ip_input.c  8.2 (Berkeley) 1/4/94
  31 * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
  32 */
  33
  34/*
  35 * Changes and additions relating to SLiRP are
  36 * Copyright (c) 1995 Danny Gasparovski.
  37 */
  38
  39#include "slirp.h"
  40#include "ip_icmp.h"
  41
  42static struct ip *ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp);
  43static void ip_freef(Slirp *slirp, struct ipq *fp);
  44static void ip_enq(register struct ipasfrag *p,
  45                   register struct ipasfrag *prev);
  46static void ip_deq(register struct ipasfrag *p);
  47
  48/*
  49 * IP initialization: fill in IP protocol switch table.
  50 * All protocols not implemented in kernel go to raw IP protocol handler.
  51 */
  52void
  53ip_init(Slirp *slirp)
  54{
  55    slirp->ipq.ip_link.next = slirp->ipq.ip_link.prev = &slirp->ipq.ip_link;
  56    udp_init(slirp);
  57    tcp_init(slirp);
  58    icmp_init(slirp);
  59}
  60
  61void ip_cleanup(Slirp *slirp)
  62{
  63    udp_cleanup(slirp);
  64    tcp_cleanup(slirp);
  65    icmp_cleanup(slirp);
  66}
  67
  68/*
  69 * Ip input routine.  Checksum and byte swap header.  If fragmented
  70 * try to reassemble.  Process options.  Pass to next level.
  71 */
  72void
  73ip_input(struct mbuf *m)
  74{
  75        Slirp *slirp = m->slirp;
  76        register struct ip *ip;
  77        int hlen;
  78
  79        if (!slirp->in_enabled) {
  80                goto bad;
  81        }
  82
  83        DEBUG_CALL("ip_input");
  84        DEBUG_ARG("m = %p", m);
  85        DEBUG_ARG("m_len = %d", m->m_len);
  86
  87        if (m->m_len < sizeof (struct ip)) {
  88                goto bad;
  89        }
  90
  91        ip = mtod(m, struct ip *);
  92
  93        if (ip->ip_v != IPVERSION) {
  94                goto bad;
  95        }
  96
  97        hlen = ip->ip_hl << 2;
  98        if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
  99          goto bad;                                  /* or packet too short */
 100        }
 101
 102        /* keep ip header intact for ICMP reply
 103         * ip->ip_sum = cksum(m, hlen);
 104         * if (ip->ip_sum) {
 105         */
 106        if(cksum(m,hlen)) {
 107          goto bad;
 108        }
 109
 110        /*
 111         * Convert fields to host representation.
 112         */
 113        NTOHS(ip->ip_len);
 114        if (ip->ip_len < hlen) {
 115                goto bad;
 116        }
 117        NTOHS(ip->ip_id);
 118        NTOHS(ip->ip_off);
 119
 120        /*
 121         * Check that the amount of data in the buffers
 122         * is as at least much as the IP header would have us expect.
 123         * Trim mbufs if longer than we expect.
 124         * Drop packet if shorter than we expect.
 125         */
 126        if (m->m_len < ip->ip_len) {
 127                goto bad;
 128        }
 129
 130        /* Should drop packet if mbuf too long? hmmm... */
 131        if (m->m_len > ip->ip_len)
 132           m_adj(m, ip->ip_len - m->m_len);
 133
 134        /* check ip_ttl for a correct ICMP reply */
 135        if (ip->ip_ttl == 0) {
 136            icmp_send_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, "ttl");
 137            goto bad;
 138        }
 139
 140        /*
 141         * If offset or IP_MF are set, must reassemble.
 142         * Otherwise, nothing need be done.
 143         * (We could look in the reassembly queue to see
 144         * if the packet was previously fragmented,
 145         * but it's not worth the time; just let them time out.)
 146         *
 147         * XXX This should fail, don't fragment yet
 148         */
 149        if (ip->ip_off &~ IP_DF) {
 150          register struct ipq *fp;
 151      struct qlink *l;
 152                /*
 153                 * Look for queue of fragments
 154                 * of this datagram.
 155                 */
 156                for (l = slirp->ipq.ip_link.next; l != &slirp->ipq.ip_link;
 157                     l = l->next) {
 158            fp = container_of(l, struct ipq, ip_link);
 159            if (ip->ip_id == fp->ipq_id &&
 160                    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
 161                    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
 162                    ip->ip_p == fp->ipq_p)
 163                    goto found;
 164        }
 165        fp = NULL;
 166        found:
 167
 168                /*
 169                 * Adjust ip_len to not reflect header,
 170                 * set ip_mff if more fragments are expected,
 171                 * convert offset of this to bytes.
 172                 */
 173                ip->ip_len -= hlen;
 174                if (ip->ip_off & IP_MF)
 175                  ip->ip_tos |= 1;
 176                else
 177                  ip->ip_tos &= ~1;
 178
 179                ip->ip_off <<= 3;
 180
 181                /*
 182                 * If datagram marked as having more fragments
 183                 * or if this is not the first fragment,
 184                 * attempt reassembly; if it succeeds, proceed.
 185                 */
 186                if (ip->ip_tos & 1 || ip->ip_off) {
 187                        ip = ip_reass(slirp, ip, fp);
 188                        if (ip == NULL)
 189                                return;
 190                        m = dtom(slirp, ip);
 191                } else
 192                        if (fp)
 193                           ip_freef(slirp, fp);
 194
 195        } else
 196                ip->ip_len -= hlen;
 197
 198        /*
 199         * Switch out to protocol's input routine.
 200         */
 201        switch (ip->ip_p) {
 202         case IPPROTO_TCP:
 203                tcp_input(m, hlen, (struct socket *)NULL, AF_INET);
 204                break;
 205         case IPPROTO_UDP:
 206                udp_input(m, hlen);
 207                break;
 208         case IPPROTO_ICMP:
 209                icmp_input(m, hlen);
 210                break;
 211         default:
 212                m_free(m);
 213        }
 214        return;
 215bad:
 216        m_free(m);
 217}
 218
 219#define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
 220#define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
 221/*
 222 * Take incoming datagram fragment and try to
 223 * reassemble it into whole datagram.  If a chain for
 224 * reassembly of this datagram already exists, then it
 225 * is given as fp; otherwise have to make a chain.
 226 */
 227static struct ip *
 228ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp)
 229{
 230        register struct mbuf *m = dtom(slirp, ip);
 231        register struct ipasfrag *q;
 232        int hlen = ip->ip_hl << 2;
 233        int i, next;
 234
 235        DEBUG_CALL("ip_reass");
 236        DEBUG_ARG("ip = %p", ip);
 237        DEBUG_ARG("fp = %p", fp);
 238        DEBUG_ARG("m = %p", m);
 239
 240        /*
 241         * Presence of header sizes in mbufs
 242         * would confuse code below.
 243         * Fragment m_data is concatenated.
 244         */
 245        m->m_data += hlen;
 246        m->m_len -= hlen;
 247
 248        /*
 249         * If first fragment to arrive, create a reassembly queue.
 250         */
 251        if (fp == NULL) {
 252          struct mbuf *t = m_get(slirp);
 253
 254          if (t == NULL) {
 255              goto dropfrag;
 256          }
 257          fp = mtod(t, struct ipq *);
 258          insque(&fp->ip_link, &slirp->ipq.ip_link);
 259          fp->ipq_ttl = IPFRAGTTL;
 260          fp->ipq_p = ip->ip_p;
 261          fp->ipq_id = ip->ip_id;
 262          fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
 263          fp->ipq_src = ip->ip_src;
 264          fp->ipq_dst = ip->ip_dst;
 265          q = (struct ipasfrag *)fp;
 266          goto insert;
 267        }
 268
 269        /*
 270         * Find a segment which begins after this one does.
 271         */
 272        for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
 273            q = q->ipf_next)
 274                if (q->ipf_off > ip->ip_off)
 275                        break;
 276
 277        /*
 278         * If there is a preceding segment, it may provide some of
 279         * our data already.  If so, drop the data from the incoming
 280         * segment.  If it provides all of our data, drop us.
 281         */
 282        if (q->ipf_prev != &fp->frag_link) {
 283        struct ipasfrag *pq = q->ipf_prev;
 284                i = pq->ipf_off + pq->ipf_len - ip->ip_off;
 285                if (i > 0) {
 286                        if (i >= ip->ip_len)
 287                                goto dropfrag;
 288                        m_adj(dtom(slirp, ip), i);
 289                        ip->ip_off += i;
 290                        ip->ip_len -= i;
 291                }
 292        }
 293
 294        /*
 295         * While we overlap succeeding segments trim them or,
 296         * if they are completely covered, dequeue them.
 297         */
 298        while (q != (struct ipasfrag*)&fp->frag_link &&
 299            ip->ip_off + ip->ip_len > q->ipf_off) {
 300                i = (ip->ip_off + ip->ip_len) - q->ipf_off;
 301                if (i < q->ipf_len) {
 302                        q->ipf_len -= i;
 303                        q->ipf_off += i;
 304                        m_adj(dtom(slirp, q), i);
 305                        break;
 306                }
 307                q = q->ipf_next;
 308                m_free(dtom(slirp, q->ipf_prev));
 309                ip_deq(q->ipf_prev);
 310        }
 311
 312insert:
 313        /*
 314         * Stick new segment in its place;
 315         * check for complete reassembly.
 316         */
 317        ip_enq(iptofrag(ip), q->ipf_prev);
 318        next = 0;
 319        for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
 320            q = q->ipf_next) {
 321                if (q->ipf_off != next)
 322                        return NULL;
 323                next += q->ipf_len;
 324        }
 325        if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
 326                return NULL;
 327
 328        /*
 329         * Reassembly is complete; concatenate fragments.
 330         */
 331    q = fp->frag_link.next;
 332        m = dtom(slirp, q);
 333
 334        q = (struct ipasfrag *) q->ipf_next;
 335        while (q != (struct ipasfrag*)&fp->frag_link) {
 336          struct mbuf *t = dtom(slirp, q);
 337          q = (struct ipasfrag *) q->ipf_next;
 338          m_cat(m, t);
 339        }
 340
 341        /*
 342         * Create header for new ip packet by
 343         * modifying header of first packet;
 344         * dequeue and discard fragment reassembly header.
 345         * Make header visible.
 346         */
 347        q = fp->frag_link.next;
 348
 349        /*
 350         * If the fragments concatenated to an mbuf that's
 351         * bigger than the total size of the fragment, then and
 352         * m_ext buffer was alloced. But fp->ipq_next points to
 353         * the old buffer (in the mbuf), so we must point ip
 354         * into the new buffer.
 355         */
 356        if (m->m_flags & M_EXT) {
 357          int delta = (char *)q - m->m_dat;
 358          q = (struct ipasfrag *)(m->m_ext + delta);
 359        }
 360
 361    ip = fragtoip(q);
 362        ip->ip_len = next;
 363        ip->ip_tos &= ~1;
 364        ip->ip_src = fp->ipq_src;
 365        ip->ip_dst = fp->ipq_dst;
 366        remque(&fp->ip_link);
 367        (void) m_free(dtom(slirp, fp));
 368        m->m_len += (ip->ip_hl << 2);
 369        m->m_data -= (ip->ip_hl << 2);
 370
 371        return ip;
 372
 373dropfrag:
 374        m_free(m);
 375        return NULL;
 376}
 377
 378/*
 379 * Free a fragment reassembly header and all
 380 * associated datagrams.
 381 */
 382static void
 383ip_freef(Slirp *slirp, struct ipq *fp)
 384{
 385        register struct ipasfrag *q, *p;
 386
 387        for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
 388                p = q->ipf_next;
 389                ip_deq(q);
 390                m_free(dtom(slirp, q));
 391        }
 392        remque(&fp->ip_link);
 393        (void) m_free(dtom(slirp, fp));
 394}
 395
 396/*
 397 * Put an ip fragment on a reassembly chain.
 398 * Like insque, but pointers in middle of structure.
 399 */
 400static void
 401ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
 402{
 403        DEBUG_CALL("ip_enq");
 404        DEBUG_ARG("prev = %p", prev);
 405        p->ipf_prev =  prev;
 406        p->ipf_next = prev->ipf_next;
 407        ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
 408        prev->ipf_next = p;
 409}
 410
 411/*
 412 * To ip_enq as remque is to insque.
 413 */
 414static void
 415ip_deq(register struct ipasfrag *p)
 416{
 417        ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
 418        ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
 419}
 420
 421/*
 422 * IP timer processing;
 423 * if a timer expires on a reassembly
 424 * queue, discard it.
 425 */
 426void
 427ip_slowtimo(Slirp *slirp)
 428{
 429    struct qlink *l;
 430
 431        DEBUG_CALL("ip_slowtimo");
 432
 433    l = slirp->ipq.ip_link.next;
 434
 435        if (l == NULL)
 436           return;
 437
 438    while (l != &slirp->ipq.ip_link) {
 439        struct ipq *fp = container_of(l, struct ipq, ip_link);
 440        l = l->next;
 441                if (--fp->ipq_ttl == 0) {
 442                        ip_freef(slirp, fp);
 443                }
 444    }
 445}
 446
 447/*
 448 * Strip out IP options, at higher
 449 * level protocol in the kernel.
 450 * Second argument is buffer to which options
 451 * will be moved, and return value is their length.
 452 * (XXX) should be deleted; last arg currently ignored.
 453 */
 454void
 455ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
 456{
 457        register int i;
 458        struct ip *ip = mtod(m, struct ip *);
 459        register char *opts;
 460        int olen;
 461
 462        olen = (ip->ip_hl<<2) - sizeof (struct ip);
 463        opts = (char *)(ip + 1);
 464        i = m->m_len - (sizeof (struct ip) + olen);
 465        memcpy(opts, opts  + olen, (unsigned)i);
 466        m->m_len -= olen;
 467
 468        ip->ip_hl = sizeof(struct ip) >> 2;
 469}
 470