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 |
|
---|
42 | static struct ip *ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp);
|
---|
43 | static void ip_freef(Slirp *slirp, struct ipq *fp);
|
---|
44 | static void ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev);
|
---|
45 | static void ip_deq(register struct ipasfrag *p);
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * IP initialization: fill in IP protocol switch table.
|
---|
49 | * All protocols not implemented in kernel go to raw IP protocol handler.
|
---|
50 | */
|
---|
51 | void ip_init(Slirp *slirp)
|
---|
52 | {
|
---|
53 | slirp->ipq.ip_link.next = slirp->ipq.ip_link.prev = &slirp->ipq.ip_link;
|
---|
54 | udp_init(slirp);
|
---|
55 | tcp_init(slirp);
|
---|
56 | icmp_init(slirp);
|
---|
57 | }
|
---|
58 |
|
---|
59 | void ip_cleanup(Slirp *slirp)
|
---|
60 | {
|
---|
61 | udp_cleanup(slirp);
|
---|
62 | tcp_cleanup(slirp);
|
---|
63 | icmp_cleanup(slirp);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Ip input routine. Checksum and byte swap header. If fragmented
|
---|
68 | * try to reassemble. Process options. Pass to next level.
|
---|
69 | */
|
---|
70 | void ip_input(struct mbuf *m)
|
---|
71 | {
|
---|
72 | Slirp *slirp = m->slirp;
|
---|
73 | M_DUP_DEBUG(slirp, m, 0, TCPIPHDR_DELTA);
|
---|
74 |
|
---|
75 | register struct ip *ip;
|
---|
76 | int hlen;
|
---|
77 |
|
---|
78 | if (!slirp->in_enabled) {
|
---|
79 | goto bad;
|
---|
80 | }
|
---|
81 |
|
---|
82 | DEBUG_CALL("ip_input");
|
---|
83 | DEBUG_ARG("m = %p", m);
|
---|
84 | DEBUG_ARG("m_len = %d", m->m_len);
|
---|
85 |
|
---|
86 | if (m->m_len < sizeof(struct ip)) {
|
---|
87 | goto bad;
|
---|
88 | }
|
---|
89 |
|
---|
90 | ip = mtod(m, struct ip *);
|
---|
91 |
|
---|
92 | if (ip->ip_v != IPVERSION) {
|
---|
93 | goto bad;
|
---|
94 | }
|
---|
95 |
|
---|
96 | hlen = ip->ip_hl << 2;
|
---|
97 | if (hlen < sizeof(struct ip) || hlen > m->m_len) { /* min header length */
|
---|
98 | goto bad; /* or packet too short */
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* keep ip header intact for ICMP reply
|
---|
102 | * ip->ip_sum = cksum(m, hlen);
|
---|
103 | * if (ip->ip_sum) {
|
---|
104 | */
|
---|
105 | if (cksum(m, hlen)) {
|
---|
106 | goto bad;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Convert fields to host representation.
|
---|
111 | */
|
---|
112 | NTOHS(ip->ip_len);
|
---|
113 | if (ip->ip_len < hlen) {
|
---|
114 | goto bad;
|
---|
115 | }
|
---|
116 | NTOHS(ip->ip_id);
|
---|
117 | NTOHS(ip->ip_off);
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Check that the amount of data in the buffers
|
---|
121 | * is as at least much as the IP header would have us expect.
|
---|
122 | * Trim mbufs if longer than we expect.
|
---|
123 | * Drop packet if shorter than we expect.
|
---|
124 | */
|
---|
125 | if (m->m_len < ip->ip_len) {
|
---|
126 | goto bad;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* Should drop packet if mbuf too long? hmmm... */
|
---|
130 | if (m->m_len > ip->ip_len)
|
---|
131 | m_adj(m, ip->ip_len - m->m_len);
|
---|
132 |
|
---|
133 | /* check ip_ttl for a correct ICMP reply */
|
---|
134 | if (ip->ip_ttl == 0) {
|
---|
135 | icmp_send_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, "ttl");
|
---|
136 | goto bad;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * If offset or IP_MF are set, must reassemble.
|
---|
141 | * Otherwise, nothing need be done.
|
---|
142 | * (We could look in the reassembly queue to see
|
---|
143 | * if the packet was previously fragmented,
|
---|
144 | * but it's not worth the time; just let them time out.)
|
---|
145 | *
|
---|
146 | * XXX This should fail, don't fragment yet
|
---|
147 | */
|
---|
148 | if (ip->ip_off & ~IP_DF) {
|
---|
149 | register struct ipq *fp;
|
---|
150 | struct qlink *l;
|
---|
151 | /*
|
---|
152 | * Look for queue of fragments
|
---|
153 | * of this datagram.
|
---|
154 | */
|
---|
155 | for (l = slirp->ipq.ip_link.next; l != &slirp->ipq.ip_link;
|
---|
156 | l = l->next) {
|
---|
157 | fp = container_of(l, struct ipq, ip_link);
|
---|
158 | if (ip->ip_id == fp->ipq_id &&
|
---|
159 | ip->ip_src.s_addr == fp->ipq_src.s_addr &&
|
---|
160 | ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
|
---|
161 | ip->ip_p == fp->ipq_p)
|
---|
162 | goto found;
|
---|
163 | }
|
---|
164 | fp = NULL;
|
---|
165 | found:
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Adjust ip_len to not reflect header,
|
---|
169 | * set ip_mff if more fragments are expected,
|
---|
170 | * convert offset of this to bytes.
|
---|
171 | */
|
---|
172 | ip->ip_len -= hlen;
|
---|
173 | if (ip->ip_off & IP_MF)
|
---|
174 | ip->ip_tos |= 1;
|
---|
175 | else
|
---|
176 | ip->ip_tos &= ~1;
|
---|
177 |
|
---|
178 | ip->ip_off <<= 3;
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * If datagram marked as having more fragments
|
---|
182 | * or if this is not the first fragment,
|
---|
183 | * attempt reassembly; if it succeeds, proceed.
|
---|
184 | */
|
---|
185 | if (ip->ip_tos & 1 || ip->ip_off) {
|
---|
186 | ip = ip_reass(slirp, ip, fp);
|
---|
187 | if (ip == NULL)
|
---|
188 | return;
|
---|
189 | m = dtom(slirp, ip);
|
---|
190 | } else if (fp)
|
---|
191 | ip_freef(slirp, fp);
|
---|
192 |
|
---|
193 | } else
|
---|
194 | ip->ip_len -= hlen;
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * Switch out to protocol's input routine.
|
---|
198 | */
|
---|
199 | switch (ip->ip_p) {
|
---|
200 | case IPPROTO_TCP:
|
---|
201 | tcp_input(m, hlen, (struct socket *)NULL, AF_INET);
|
---|
202 | break;
|
---|
203 | case IPPROTO_UDP:
|
---|
204 | udp_input(m, hlen);
|
---|
205 | break;
|
---|
206 | case IPPROTO_ICMP:
|
---|
207 | icmp_input(m, hlen);
|
---|
208 | break;
|
---|
209 | default:
|
---|
210 | m_free(m);
|
---|
211 | }
|
---|
212 | return;
|
---|
213 | bad:
|
---|
214 | m_free(m);
|
---|
215 | }
|
---|
216 |
|
---|
217 | #define iptofrag(P) ((struct ipasfrag *)(((char *)(P)) - sizeof(struct qlink)))
|
---|
218 | #define fragtoip(P) ((struct ip *)(((char *)(P)) + sizeof(struct qlink)))
|
---|
219 | /*
|
---|
220 | * Take incoming datagram fragment and try to
|
---|
221 | * reassemble it into whole datagram. If a chain for
|
---|
222 | * reassembly of this datagram already exists, then it
|
---|
223 | * is given as fp; otherwise have to make a chain.
|
---|
224 | */
|
---|
225 | static struct ip *ip_reass(Slirp *slirp, struct ip *ip, struct ipq *fp)
|
---|
226 | {
|
---|
227 | register struct mbuf *m = dtom(slirp, ip);
|
---|
228 | register struct ipasfrag *q;
|
---|
229 | int hlen = ip->ip_hl << 2;
|
---|
230 | int i, next;
|
---|
231 |
|
---|
232 | DEBUG_CALL("ip_reass");
|
---|
233 | DEBUG_ARG("ip = %p", ip);
|
---|
234 | DEBUG_ARG("fp = %p", fp);
|
---|
235 | DEBUG_ARG("m = %p", m);
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * Presence of header sizes in mbufs
|
---|
239 | * would confuse code below.
|
---|
240 | * Fragment m_data is concatenated.
|
---|
241 | */
|
---|
242 | m->m_data += hlen;
|
---|
243 | m->m_len -= hlen;
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * If first fragment to arrive, create a reassembly queue.
|
---|
247 | */
|
---|
248 | if (fp == NULL) {
|
---|
249 | struct mbuf *t = m_get(slirp);
|
---|
250 |
|
---|
251 | if (t == NULL) {
|
---|
252 | goto dropfrag;
|
---|
253 | }
|
---|
254 | fp = mtod(t, struct ipq *);
|
---|
255 | slirp_insque(&fp->ip_link, &slirp->ipq.ip_link);
|
---|
256 | fp->ipq_ttl = IPFRAGTTL;
|
---|
257 | fp->ipq_p = ip->ip_p;
|
---|
258 | fp->ipq_id = ip->ip_id;
|
---|
259 | fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
|
---|
260 | fp->ipq_src = ip->ip_src;
|
---|
261 | fp->ipq_dst = ip->ip_dst;
|
---|
262 | q = (struct ipasfrag *)fp;
|
---|
263 | goto insert;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * Find a segment which begins after this one does.
|
---|
268 | */
|
---|
269 | for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
|
---|
270 | q = q->ipf_next)
|
---|
271 | if (q->ipf_off > ip->ip_off)
|
---|
272 | break;
|
---|
273 |
|
---|
274 | /*
|
---|
275 | * If there is a preceding segment, it may provide some of
|
---|
276 | * our data already. If so, drop the data from the incoming
|
---|
277 | * segment. If it provides all of our data, drop us.
|
---|
278 | */
|
---|
279 | if (q->ipf_prev != &fp->frag_link) {
|
---|
280 | struct ipasfrag *pq = q->ipf_prev;
|
---|
281 | i = pq->ipf_off + pq->ipf_len - ip->ip_off;
|
---|
282 | if (i > 0) {
|
---|
283 | if (i >= ip->ip_len)
|
---|
284 | goto dropfrag;
|
---|
285 | m_adj(dtom(slirp, ip), i);
|
---|
286 | ip->ip_off += i;
|
---|
287 | ip->ip_len -= i;
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | /*
|
---|
292 | * While we overlap succeeding segments trim them or,
|
---|
293 | * if they are completely covered, dequeue them.
|
---|
294 | */
|
---|
295 | while (q != (struct ipasfrag *)&fp->frag_link &&
|
---|
296 | ip->ip_off + ip->ip_len > q->ipf_off) {
|
---|
297 | struct ipasfrag *prev;
|
---|
298 | i = (ip->ip_off + ip->ip_len) - q->ipf_off;
|
---|
299 | if (i < q->ipf_len) {
|
---|
300 | q->ipf_len -= i;
|
---|
301 | q->ipf_off += i;
|
---|
302 | m_adj(dtom(slirp, q), i);
|
---|
303 | break;
|
---|
304 | }
|
---|
305 | prev = q;
|
---|
306 | q = q->ipf_next;
|
---|
307 | ip_deq(prev);
|
---|
308 | m_free(dtom(slirp, prev));
|
---|
309 | }
|
---|
310 |
|
---|
311 | insert:
|
---|
312 | /*
|
---|
313 | * Stick new segment in its place;
|
---|
314 | * check for complete reassembly.
|
---|
315 | */
|
---|
316 | ip_enq(iptofrag(ip), q->ipf_prev);
|
---|
317 | next = 0;
|
---|
318 | for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
|
---|
319 | q = q->ipf_next) {
|
---|
320 | if (q->ipf_off != next)
|
---|
321 | return NULL;
|
---|
322 | next += q->ipf_len;
|
---|
323 | }
|
---|
324 | if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
|
---|
325 | return NULL;
|
---|
326 |
|
---|
327 | /*
|
---|
328 | * Reassembly is complete; concatenate fragments.
|
---|
329 | */
|
---|
330 | q = fp->frag_link.next;
|
---|
331 | m = dtom(slirp, q);
|
---|
332 | int delta = (char *)q - (m->m_flags & M_EXT ? m->m_ext : m->m_dat);
|
---|
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 bigger than the total
|
---|
351 | * size of the fragment and the mbuf was not already using an m_ext buffer,
|
---|
352 | * then an m_ext buffer was allocated. But fp->ipq_next points to the old
|
---|
353 | * buffer (in the mbuf), so we must point ip into the new buffer.
|
---|
354 | */
|
---|
355 | if (m->m_flags & M_EXT) {
|
---|
356 | q = (struct ipasfrag *)(m->m_ext + delta);
|
---|
357 | }
|
---|
358 |
|
---|
359 | ip = fragtoip(q);
|
---|
360 | ip->ip_len = next;
|
---|
361 | ip->ip_tos &= ~1;
|
---|
362 | ip->ip_src = fp->ipq_src;
|
---|
363 | ip->ip_dst = fp->ipq_dst;
|
---|
364 | slirp_remque(&fp->ip_link);
|
---|
365 | m_free(dtom(slirp, fp));
|
---|
366 | m->m_len += (ip->ip_hl << 2);
|
---|
367 | m->m_data -= (ip->ip_hl << 2);
|
---|
368 |
|
---|
369 | return ip;
|
---|
370 |
|
---|
371 | dropfrag:
|
---|
372 | m_free(m);
|
---|
373 | return NULL;
|
---|
374 | }
|
---|
375 |
|
---|
376 | /*
|
---|
377 | * Free a fragment reassembly header and all
|
---|
378 | * associated datagrams.
|
---|
379 | */
|
---|
380 | static void ip_freef(Slirp *slirp, struct ipq *fp)
|
---|
381 | {
|
---|
382 | register struct ipasfrag *q, *p;
|
---|
383 |
|
---|
384 | for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
|
---|
385 | q = p) {
|
---|
386 | p = q->ipf_next;
|
---|
387 | ip_deq(q);
|
---|
388 | m_free(dtom(slirp, q));
|
---|
389 | }
|
---|
390 | slirp_remque(&fp->ip_link);
|
---|
391 | m_free(dtom(slirp, fp));
|
---|
392 | }
|
---|
393 |
|
---|
394 | /*
|
---|
395 | * Put an ip fragment on a reassembly chain.
|
---|
396 | * Like slirp_insque, but pointers in middle of structure.
|
---|
397 | */
|
---|
398 | static void ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
|
---|
399 | {
|
---|
400 | DEBUG_CALL("ip_enq");
|
---|
401 | DEBUG_ARG("prev = %p", prev);
|
---|
402 | p->ipf_prev = prev;
|
---|
403 | p->ipf_next = prev->ipf_next;
|
---|
404 | ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
|
---|
405 | prev->ipf_next = p;
|
---|
406 | }
|
---|
407 |
|
---|
408 | /*
|
---|
409 | * To ip_enq as slirp_remque is to slirp_insque.
|
---|
410 | */
|
---|
411 | static void ip_deq(register struct ipasfrag *p)
|
---|
412 | {
|
---|
413 | ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
|
---|
414 | ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /*
|
---|
418 | * IP timer processing;
|
---|
419 | * if a timer expires on a reassembly
|
---|
420 | * queue, discard it.
|
---|
421 | */
|
---|
422 | void ip_slowtimo(Slirp *slirp)
|
---|
423 | {
|
---|
424 | struct qlink *l;
|
---|
425 |
|
---|
426 | DEBUG_CALL("ip_slowtimo");
|
---|
427 |
|
---|
428 | l = slirp->ipq.ip_link.next;
|
---|
429 |
|
---|
430 | if (l == NULL)
|
---|
431 | return;
|
---|
432 |
|
---|
433 | while (l != &slirp->ipq.ip_link) {
|
---|
434 | struct ipq *fp = container_of(l, struct ipq, ip_link);
|
---|
435 | l = l->next;
|
---|
436 | if (--fp->ipq_ttl == 0) {
|
---|
437 | ip_freef(slirp, fp);
|
---|
438 | }
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | /*
|
---|
443 | * Strip out IP options, at higher
|
---|
444 | * level protocol in the kernel.
|
---|
445 | * Second argument is buffer to which options
|
---|
446 | * will be moved, and return value is their length.
|
---|
447 | * (XXX) should be deleted; last arg currently ignored.
|
---|
448 | */
|
---|
449 | void ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
|
---|
450 | {
|
---|
451 | register int i;
|
---|
452 | struct ip *ip = mtod(m, struct ip *);
|
---|
453 | register char *opts;
|
---|
454 | int olen;
|
---|
455 |
|
---|
456 | olen = (ip->ip_hl << 2) - sizeof(struct ip);
|
---|
457 | opts = (char *)(ip + 1);
|
---|
458 | i = m->m_len - (sizeof(struct ip) + olen);
|
---|
459 | memmove(opts, opts + olen, (unsigned)i);
|
---|
460 | m->m_len -= olen;
|
---|
461 |
|
---|
462 | ip->ip_hl = sizeof(struct ip) >> 2;
|
---|
463 | }
|
---|