ip_input.c 11.6 KB
Newer Older
1
/* SPDX-License-Identifier: BSD-3-Clause */
B
bellard 已提交
2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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.
14
 * 3. Neither the name of the University nor the names of its contributors
B
bellard 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *    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.
 */

39
#include "slirp.h"
B
bellard 已提交
40 41
#include "ip_icmp.h"

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

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

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

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

79 80 81 82
	if (!slirp->in_enabled) {
		goto bad;
	}

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

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

B
bellard 已提交
91
	ip = mtod(m, struct ip *);
92

B
bellard 已提交
93 94 95 96 97 98
	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 已提交
99
	  goto bad;                                  /* or packet too short */
B
bellard 已提交
100 101 102
	}

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

B
bellard 已提交
130 131 132 133 134
	/* 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 已提交
135 136 137
	if (ip->ip_ttl == 0) {
	    icmp_send_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, "ttl");
	    goto bad;
B
bellard 已提交
138 139 140 141 142 143 144 145
	}

	/*
	 * 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.)
146
	 *
B
bellard 已提交
147 148 149 150
	 * XXX This should fail, don't fragment yet
	 */
	if (ip->ip_off &~ IP_DF) {
	  register struct ipq *fp;
B
blueswir1 已提交
151
      struct qlink *l;
B
bellard 已提交
152 153 154 155
		/*
		 * Look for queue of fragments
		 * of this datagram.
		 */
156 157
		for (l = slirp->ipq.ip_link.next; l != &slirp->ipq.ip_link;
		     l = l->next) {
B
blueswir1 已提交
158 159 160 161 162
            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 已提交
163
		    goto found;
B
blueswir1 已提交
164 165
        }
        fp = NULL;
B
bellard 已提交
166 167 168 169 170 171 172 173 174
	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 已提交
175
		  ip->ip_tos |= 1;
176
		else
B
blueswir1 已提交
177
		  ip->ip_tos &= ~1;
B
bellard 已提交
178 179 180 181 182 183 184 185

		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 已提交
186
		if (ip->ip_tos & 1 || ip->ip_off) {
187
			ip = ip_reass(slirp, ip, fp);
188
                        if (ip == NULL)
B
bellard 已提交
189
				return;
190
			m = dtom(slirp, ip);
B
bellard 已提交
191 192
		} else
			if (fp)
P
Paolo Bonzini 已提交
193
			   ip_freef(slirp, fp);
B
bellard 已提交
194 195 196 197 198 199 200 201 202

	} else
		ip->ip_len -= hlen;

	/*
	 * Switch out to protocol's input routine.
	 */
	switch (ip->ip_p) {
	 case IPPROTO_TCP:
203
		tcp_input(m, hlen, (struct socket *)NULL, AF_INET);
B
bellard 已提交
204 205 206 207 208 209 210 211 212 213 214 215
		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 已提交
216
	m_free(m);
B
bellard 已提交
217 218
}

B
blueswir1 已提交
219 220
#define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
#define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
B
bellard 已提交
221 222 223 224 225 226
/*
 * 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.
 */
227
static struct ip *
228
ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp)
B
bellard 已提交
229
{
230
	register struct mbuf *m = dtom(slirp, ip);
B
bellard 已提交
231 232 233
	register struct ipasfrag *q;
	int hlen = ip->ip_hl << 2;
	int i, next;
234

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

	/*
	 * 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.
	 */
251
        if (fp == NULL) {
252 253 254 255 256
	  struct mbuf *t = m_get(slirp);

	  if (t == NULL) {
	      goto dropfrag;
	  }
B
bellard 已提交
257
	  fp = mtod(t, struct ipq *);
258
	  insque(&fp->ip_link, &slirp->ipq.ip_link);
B
bellard 已提交
259 260 261
	  fp->ipq_ttl = IPFRAGTTL;
	  fp->ipq_p = ip->ip_p;
	  fp->ipq_id = ip->ip_id;
B
blueswir1 已提交
262 263 264
	  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 已提交
265 266 267
	  q = (struct ipasfrag *)fp;
	  goto insert;
	}
268

B
bellard 已提交
269 270 271
	/*
	 * Find a segment which begins after this one does.
	 */
B
blueswir1 已提交
272 273 274
	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 已提交
275 276 277 278 279 280 281
			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 已提交
282 283 284
	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 已提交
285 286 287
		if (i > 0) {
			if (i >= ip->ip_len)
				goto dropfrag;
288
			m_adj(dtom(slirp, ip), i);
B
bellard 已提交
289 290 291 292 293 294 295 296 297
			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 已提交
298 299 300 301 302 303
	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;
304
			m_adj(dtom(slirp, q), i);
B
bellard 已提交
305 306
			break;
		}
B
blueswir1 已提交
307
		q = q->ipf_next;
J
Jan Kiszka 已提交
308
		m_free(dtom(slirp, q->ipf_prev));
B
blueswir1 已提交
309
		ip_deq(q->ipf_prev);
B
bellard 已提交
310 311 312 313 314 315 316
	}

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

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

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

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

	/*
	 * 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) {
357
	  int delta = (char *)q - m->m_dat;
B
blueswir1 已提交
358
	  q = (struct ipasfrag *)(m->m_ext + delta);
B
bellard 已提交
359 360
	}

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

B
blueswir1 已提交
371
	return ip;
B
bellard 已提交
372 373

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

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

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

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

/*
 * To ip_enq as remque is to insque.
 */
414 415
static void
ip_deq(register struct ipasfrag *p)
B
bellard 已提交
416 417 418 419 420 421 422 423 424 425 426
{
	((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
427
ip_slowtimo(Slirp *slirp)
B
bellard 已提交
428
{
B
blueswir1 已提交
429
    struct qlink *l;
430

B
bellard 已提交
431
	DEBUG_CALL("ip_slowtimo");
432

433
    l = slirp->ipq.ip_link.next;
B
blueswir1 已提交
434

435
        if (l == NULL)
B
bellard 已提交
436 437
	   return;

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

/*
 * 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
455
ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
B
bellard 已提交
456 457 458
{
	register int i;
	struct ip *ip = mtod(m, struct ip *);
459
	register char *opts;
B
bellard 已提交
460 461 462
	int olen;

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

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