ip_input.c 11.6 KB
Newer Older
B
bellard 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (c) 1982, 1986, 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the University nor the names of its contributors
B
bellard 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
 * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
 */

/*
 * Changes and additions relating to SLiRP are
 * Copyright (c) 1995 Danny Gasparovski.
36
 *
B
bellard 已提交
37 38 39 40
 * Please read the file COPYRIGHT for the
 * terms and conditions of the copyright.
 */

41
#include "slirp.h"
B
bellard 已提交
42 43
#include "ip_icmp.h"

44 45
static struct ip *ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp);
static void ip_freef(Slirp *slirp, struct ipq *fp);
46 47 48 49
static void ip_enq(register struct ipasfrag *p,
                   register struct ipasfrag *prev);
static void ip_deq(register struct ipasfrag *p);

B
bellard 已提交
50 51 52 53 54
/*
 * IP initialization: fill in IP protocol switch table.
 * All protocols not implemented in kernel go to raw IP protocol handler.
 */
void
55
ip_init(Slirp *slirp)
B
bellard 已提交
56
{
57 58 59
    slirp->ipq.ip_link.next = slirp->ipq.ip_link.prev = &slirp->ipq.ip_link;
    udp_init(slirp);
    tcp_init(slirp);
60
    icmp_init(slirp);
B
bellard 已提交
61 62
}

63 64 65 66 67 68 69
void ip_cleanup(Slirp *slirp)
{
    udp_cleanup(slirp);
    tcp_cleanup(slirp);
    icmp_cleanup(slirp);
}

B
bellard 已提交
70 71 72 73 74
/*
 * Ip input routine.  Checksum and byte swap header.  If fragmented
 * try to reassemble.  Process options.  Pass to next level.
 */
void
75
ip_input(struct mbuf *m)
B
bellard 已提交
76
{
77
	Slirp *slirp = m->slirp;
B
bellard 已提交
78 79
	register struct ip *ip;
	int hlen;
80

81 82 83 84
	if (!slirp->in_enabled) {
		goto bad;
	}

B
bellard 已提交
85
	DEBUG_CALL("ip_input");
86
	DEBUG_ARG("m = %p", m);
B
bellard 已提交
87 88 89
	DEBUG_ARG("m_len = %d", m->m_len);

	if (m->m_len < sizeof (struct ip)) {
90
		goto bad;
B
bellard 已提交
91
	}
92

B
bellard 已提交
93
	ip = mtod(m, struct ip *);
94

B
bellard 已提交
95 96 97 98 99 100
	if (ip->ip_v != IPVERSION) {
		goto bad;
	}

	hlen = ip->ip_hl << 2;
	if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
J
Jan Kiszka 已提交
101
	  goto bad;                                  /* or packet too short */
B
bellard 已提交
102 103 104
	}

        /* keep ip header intact for ICMP reply
105 106
	 * ip->ip_sum = cksum(m, hlen);
	 * if (ip->ip_sum) {
B
bellard 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	 */
	if(cksum(m,hlen)) {
	  goto bad;
	}

	/*
	 * Convert fields to host representation.
	 */
	NTOHS(ip->ip_len);
	if (ip->ip_len < hlen) {
		goto bad;
	}
	NTOHS(ip->ip_id);
	NTOHS(ip->ip_off);

	/*
	 * Check that the amount of data in the buffers
	 * is as at least much as the IP header would have us expect.
	 * Trim mbufs if longer than we expect.
	 * Drop packet if shorter than we expect.
	 */
	if (m->m_len < ip->ip_len) {
		goto bad;
	}
131

B
bellard 已提交
132 133 134 135 136
	/* Should drop packet if mbuf too long? hmmm... */
	if (m->m_len > ip->ip_len)
	   m_adj(m, ip->ip_len - m->m_len);

	/* check ip_ttl for a correct ICMP reply */
Y
Yann Bordenave 已提交
137 138 139
	if (ip->ip_ttl == 0) {
	    icmp_send_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, "ttl");
	    goto bad;
B
bellard 已提交
140 141 142 143 144 145 146 147
	}

	/*
	 * If offset or IP_MF are set, must reassemble.
	 * Otherwise, nothing need be done.
	 * (We could look in the reassembly queue to see
	 * if the packet was previously fragmented,
	 * but it's not worth the time; just let them time out.)
148
	 *
B
bellard 已提交
149 150 151 152
	 * XXX This should fail, don't fragment yet
	 */
	if (ip->ip_off &~ IP_DF) {
	  register struct ipq *fp;
B
blueswir1 已提交
153
      struct qlink *l;
B
bellard 已提交
154 155 156 157
		/*
		 * Look for queue of fragments
		 * of this datagram.
		 */
158 159
		for (l = slirp->ipq.ip_link.next; l != &slirp->ipq.ip_link;
		     l = l->next) {
B
blueswir1 已提交
160 161 162 163 164
            fp = container_of(l, struct ipq, ip_link);
            if (ip->ip_id == fp->ipq_id &&
                    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
                    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
                    ip->ip_p == fp->ipq_p)
B
bellard 已提交
165
		    goto found;
B
blueswir1 已提交
166 167
        }
        fp = NULL;
B
bellard 已提交
168 169 170 171 172 173 174 175 176
	found:

		/*
		 * Adjust ip_len to not reflect header,
		 * set ip_mff if more fragments are expected,
		 * convert offset of this to bytes.
		 */
		ip->ip_len -= hlen;
		if (ip->ip_off & IP_MF)
B
blueswir1 已提交
177
		  ip->ip_tos |= 1;
178
		else
B
blueswir1 已提交
179
		  ip->ip_tos &= ~1;
B
bellard 已提交
180 181 182 183 184 185 186 187

		ip->ip_off <<= 3;

		/*
		 * If datagram marked as having more fragments
		 * or if this is not the first fragment,
		 * attempt reassembly; if it succeeds, proceed.
		 */
B
blueswir1 已提交
188
		if (ip->ip_tos & 1 || ip->ip_off) {
189
			ip = ip_reass(slirp, ip, fp);
190
                        if (ip == NULL)
B
bellard 已提交
191
				return;
192
			m = dtom(slirp, ip);
B
bellard 已提交
193 194
		} else
			if (fp)
P
Paolo Bonzini 已提交
195
			   ip_freef(slirp, fp);
B
bellard 已提交
196 197 198 199 200 201 202 203 204

	} else
		ip->ip_len -= hlen;

	/*
	 * Switch out to protocol's input routine.
	 */
	switch (ip->ip_p) {
	 case IPPROTO_TCP:
205
		tcp_input(m, hlen, (struct socket *)NULL, AF_INET);
B
bellard 已提交
206 207 208 209 210 211 212 213 214 215 216 217
		break;
	 case IPPROTO_UDP:
		udp_input(m, hlen);
		break;
	 case IPPROTO_ICMP:
		icmp_input(m, hlen);
		break;
	 default:
		m_free(m);
	}
	return;
bad:
J
Jan Kiszka 已提交
218
	m_free(m);
B
bellard 已提交
219 220
}

B
blueswir1 已提交
221 222
#define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
#define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
B
bellard 已提交
223 224 225 226 227 228
/*
 * Take incoming datagram fragment and try to
 * reassemble it into whole datagram.  If a chain for
 * reassembly of this datagram already exists, then it
 * is given as fp; otherwise have to make a chain.
 */
229
static struct ip *
230
ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp)
B
bellard 已提交
231
{
232
	register struct mbuf *m = dtom(slirp, ip);
B
bellard 已提交
233 234 235
	register struct ipasfrag *q;
	int hlen = ip->ip_hl << 2;
	int i, next;
236

B
bellard 已提交
237
	DEBUG_CALL("ip_reass");
238 239 240
	DEBUG_ARG("ip = %p", ip);
	DEBUG_ARG("fp = %p", fp);
	DEBUG_ARG("m = %p", m);
B
bellard 已提交
241 242 243 244 245 246 247 248 249 250 251 252

	/*
	 * Presence of header sizes in mbufs
	 * would confuse code below.
         * Fragment m_data is concatenated.
	 */
	m->m_data += hlen;
	m->m_len -= hlen;

	/*
	 * If first fragment to arrive, create a reassembly queue.
	 */
253
        if (fp == NULL) {
254 255 256 257 258
	  struct mbuf *t = m_get(slirp);

	  if (t == NULL) {
	      goto dropfrag;
	  }
B
bellard 已提交
259
	  fp = mtod(t, struct ipq *);
260
	  insque(&fp->ip_link, &slirp->ipq.ip_link);
B
bellard 已提交
261 262 263
	  fp->ipq_ttl = IPFRAGTTL;
	  fp->ipq_p = ip->ip_p;
	  fp->ipq_id = ip->ip_id;
B
blueswir1 已提交
264 265 266
	  fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
	  fp->ipq_src = ip->ip_src;
	  fp->ipq_dst = ip->ip_dst;
B
bellard 已提交
267 268 269
	  q = (struct ipasfrag *)fp;
	  goto insert;
	}
270

B
bellard 已提交
271 272 273
	/*
	 * Find a segment which begins after this one does.
	 */
B
blueswir1 已提交
274 275 276
	for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
            q = q->ipf_next)
		if (q->ipf_off > ip->ip_off)
B
bellard 已提交
277 278 279 280 281 282 283
			break;

	/*
	 * If there is a preceding segment, it may provide some of
	 * our data already.  If so, drop the data from the incoming
	 * segment.  If it provides all of our data, drop us.
	 */
B
blueswir1 已提交
284 285 286
	if (q->ipf_prev != &fp->frag_link) {
        struct ipasfrag *pq = q->ipf_prev;
		i = pq->ipf_off + pq->ipf_len - ip->ip_off;
B
bellard 已提交
287 288 289
		if (i > 0) {
			if (i >= ip->ip_len)
				goto dropfrag;
290
			m_adj(dtom(slirp, ip), i);
B
bellard 已提交
291 292 293 294 295 296 297 298 299
			ip->ip_off += i;
			ip->ip_len -= i;
		}
	}

	/*
	 * While we overlap succeeding segments trim them or,
	 * if they are completely covered, dequeue them.
	 */
B
blueswir1 已提交
300 301 302 303 304 305
	while (q != (struct ipasfrag*)&fp->frag_link &&
            ip->ip_off + ip->ip_len > q->ipf_off) {
		i = (ip->ip_off + ip->ip_len) - q->ipf_off;
		if (i < q->ipf_len) {
			q->ipf_len -= i;
			q->ipf_off += i;
306
			m_adj(dtom(slirp, q), i);
B
bellard 已提交
307 308
			break;
		}
B
blueswir1 已提交
309
		q = q->ipf_next;
J
Jan Kiszka 已提交
310
		m_free(dtom(slirp, q->ipf_prev));
B
blueswir1 已提交
311
		ip_deq(q->ipf_prev);
B
bellard 已提交
312 313 314 315 316 317 318
	}

insert:
	/*
	 * Stick new segment in its place;
	 * check for complete reassembly.
	 */
B
blueswir1 已提交
319
	ip_enq(iptofrag(ip), q->ipf_prev);
B
bellard 已提交
320
	next = 0;
B
blueswir1 已提交
321 322 323
	for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
            q = q->ipf_next) {
		if (q->ipf_off != next)
324
                        return NULL;
B
blueswir1 已提交
325
		next += q->ipf_len;
B
bellard 已提交
326
	}
B
blueswir1 已提交
327
	if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
328
                return NULL;
B
bellard 已提交
329 330 331 332

	/*
	 * Reassembly is complete; concatenate fragments.
	 */
B
blueswir1 已提交
333
    q = fp->frag_link.next;
334
	m = dtom(slirp, q);
B
bellard 已提交
335 336

	q = (struct ipasfrag *) q->ipf_next;
B
blueswir1 已提交
337
	while (q != (struct ipasfrag*)&fp->frag_link) {
338
	  struct mbuf *t = dtom(slirp, q);
B
bellard 已提交
339
	  q = (struct ipasfrag *) q->ipf_next;
340
	  m_cat(m, t);
B
bellard 已提交
341 342 343 344 345 346 347 348
	}

	/*
	 * Create header for new ip packet by
	 * modifying header of first packet;
	 * dequeue and discard fragment reassembly header.
	 * Make header visible.
	 */
B
blueswir1 已提交
349
	q = fp->frag_link.next;
B
bellard 已提交
350 351 352 353 354 355 356 357 358

	/*
	 * If the fragments concatenated to an mbuf that's
	 * bigger than the total size of the fragment, then and
	 * m_ext buffer was alloced. But fp->ipq_next points to
	 * the old buffer (in the mbuf), so we must point ip
	 * into the new buffer.
	 */
	if (m->m_flags & M_EXT) {
359
	  int delta = (char *)q - m->m_dat;
B
blueswir1 已提交
360
	  q = (struct ipasfrag *)(m->m_ext + delta);
B
bellard 已提交
361 362
	}

B
blueswir1 已提交
363
    ip = fragtoip(q);
B
bellard 已提交
364
	ip->ip_len = next;
B
blueswir1 已提交
365 366 367 368
	ip->ip_tos &= ~1;
	ip->ip_src = fp->ipq_src;
	ip->ip_dst = fp->ipq_dst;
	remque(&fp->ip_link);
369
	(void) m_free(dtom(slirp, fp));
B
bellard 已提交
370 371 372
	m->m_len += (ip->ip_hl << 2);
	m->m_data -= (ip->ip_hl << 2);

B
blueswir1 已提交
373
	return ip;
B
bellard 已提交
374 375

dropfrag:
J
Jan Kiszka 已提交
376
	m_free(m);
377
        return NULL;
B
bellard 已提交
378 379 380 381 382 383
}

/*
 * Free a fragment reassembly header and all
 * associated datagrams.
 */
384
static void
385
ip_freef(Slirp *slirp, struct ipq *fp)
B
bellard 已提交
386 387 388
{
	register struct ipasfrag *q, *p;

B
blueswir1 已提交
389 390
	for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
		p = q->ipf_next;
B
bellard 已提交
391
		ip_deq(q);
J
Jan Kiszka 已提交
392
		m_free(dtom(slirp, q));
B
bellard 已提交
393
	}
B
blueswir1 已提交
394
	remque(&fp->ip_link);
395
	(void) m_free(dtom(slirp, fp));
B
bellard 已提交
396 397 398 399 400 401
}

/*
 * Put an ip fragment on a reassembly chain.
 * Like insque, but pointers in middle of structure.
 */
402 403
static void
ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
B
bellard 已提交
404 405
{
	DEBUG_CALL("ip_enq");
406
	DEBUG_ARG("prev = %p", prev);
B
blueswir1 已提交
407
	p->ipf_prev =  prev;
B
bellard 已提交
408
	p->ipf_next = prev->ipf_next;
B
blueswir1 已提交
409 410
	((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
	prev->ipf_next = p;
B
bellard 已提交
411 412 413 414 415
}

/*
 * To ip_enq as remque is to insque.
 */
416 417
static void
ip_deq(register struct ipasfrag *p)
B
bellard 已提交
418 419 420 421 422 423 424 425 426 427 428
{
	((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
	((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
}

/*
 * IP timer processing;
 * if a timer expires on a reassembly
 * queue, discard it.
 */
void
429
ip_slowtimo(Slirp *slirp)
B
bellard 已提交
430
{
B
blueswir1 已提交
431
    struct qlink *l;
432

B
bellard 已提交
433
	DEBUG_CALL("ip_slowtimo");
434

435
    l = slirp->ipq.ip_link.next;
B
blueswir1 已提交
436

437
        if (l == NULL)
B
bellard 已提交
438 439
	   return;

440
    while (l != &slirp->ipq.ip_link) {
B
blueswir1 已提交
441 442 443
        struct ipq *fp = container_of(l, struct ipq, ip_link);
        l = l->next;
		if (--fp->ipq_ttl == 0) {
444
			ip_freef(slirp, fp);
B
bellard 已提交
445
		}
446
    }
B
bellard 已提交
447 448 449 450 451 452 453 454 455 456
}

/*
 * Strip out IP options, at higher
 * level protocol in the kernel.
 * Second argument is buffer to which options
 * will be moved, and return value is their length.
 * (XXX) should be deleted; last arg currently ignored.
 */
void
457
ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
B
bellard 已提交
458 459 460
{
	register int i;
	struct ip *ip = mtod(m, struct ip *);
461
	register char *opts;
B
bellard 已提交
462 463 464
	int olen;

	olen = (ip->ip_hl<<2) - sizeof (struct ip);
465
	opts = (char *)(ip + 1);
B
bellard 已提交
466 467 468
	i = m->m_len - (sizeof (struct ip) + olen);
	memcpy(opts, opts  + olen, (unsigned)i);
	m->m_len -= olen;
469

B
bellard 已提交
470 471
	ip->ip_hl = sizeof(struct ip) >> 2;
}