Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/ip_input.c
2 views
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 ipas *p, register struct ipas *prev);
45
static void ip_deq(register struct ipas *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 *q;
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
q = container_of(l, struct ipq, ip_link);
158
if (ip->ip_id == q->ipq_id &&
159
ip->ip_src.s_addr == q->ipq_src.s_addr &&
160
ip->ip_dst.s_addr == q->ipq_dst.s_addr &&
161
ip->ip_p == q->ipq_p)
162
goto found;
163
}
164
q = 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, q);
187
if (ip == NULL)
188
return;
189
m = dtom(slirp, ip);
190
} else if (q)
191
ip_freef(slirp, q);
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 iptoas(P) container_of((P), struct ipas, ipf_ip)
218
#define astoip(P) (&(P)->ipf_ip)
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 q; otherwise have to make a chain.
224
*/
225
static struct ip *ip_reass(Slirp *slirp, struct ip *ip, struct ipq *q)
226
{
227
register struct mbuf *m = dtom(slirp, ip);
228
struct ipas *first = container_of(q, struct ipas, ipq);
229
register struct ipas *cursor;
230
int hlen = ip->ip_hl << 2;
231
int i, next;
232
233
DEBUG_CALL("ip_reass");
234
DEBUG_ARG("ip = %p", ip);
235
DEBUG_ARG("q = %p", q);
236
DEBUG_ARG("m = %p", m);
237
238
/*
239
* Presence of header sizes in mbufs
240
* would confuse code below.
241
* Fragment m_data is concatenated.
242
*/
243
m->m_data += hlen;
244
m->m_len -= hlen;
245
246
/*
247
* If first fragment to arrive, create a reassembly queue.
248
*/
249
if (q == NULL) {
250
struct mbuf *t = m_get(slirp);
251
252
if (t == NULL) {
253
goto dropfrag;
254
}
255
first = mtod(t, struct ipas *);
256
q = &first->ipq;
257
slirp_insque(&q->ip_link, &slirp->ipq.ip_link);
258
q->ipq_ttl = IPFRAGTTL;
259
q->ipq_p = ip->ip_p;
260
q->ipq_id = ip->ip_id;
261
first->link.next = first->link.prev = first;
262
q->ipq_src = ip->ip_src;
263
q->ipq_dst = ip->ip_dst;
264
cursor = first;
265
goto insert;
266
}
267
268
/*
269
* Find a segment which begins after this one does.
270
*/
271
for (cursor = first->link.next; cursor != first; cursor = cursor->link.next)
272
if (cursor->ipf_off > ip->ip_off)
273
break;
274
275
/*
276
* If there is a preceding segment, it may provide some of
277
* our data already. If so, drop the data from the incoming
278
* segment. If it provides all of our data, drop us.
279
*/
280
if (cursor->link.prev != first) {
281
struct ipas *pq = cursor->link.prev;
282
i = pq->ipf_off + pq->ipf_len - ip->ip_off;
283
if (i > 0) {
284
if (i >= ip->ip_len)
285
goto dropfrag;
286
m_adj(dtom(slirp, ip), i);
287
ip->ip_off += i;
288
ip->ip_len -= i;
289
}
290
}
291
292
/*
293
* While we overlap succeeding segments trim them or,
294
* if they are completely covered, dequeue them.
295
*/
296
while (cursor != first && ip->ip_off + ip->ip_len > cursor->ipf_off) {
297
struct ipas *prev;
298
i = (ip->ip_off + ip->ip_len) - cursor->ipf_off;
299
if (i < cursor->ipf_len) {
300
cursor->ipf_len -= i;
301
cursor->ipf_off += i;
302
m_adj(dtom(slirp, cursor), i);
303
break;
304
}
305
prev = cursor;
306
cursor = cursor->link.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(iptoas(ip), cursor->link.prev);
317
next = 0;
318
for (cursor = first->link.next; cursor != first; cursor = cursor->link.next) {
319
if (cursor->ipf_off != next)
320
return NULL;
321
next += cursor->ipf_len;
322
}
323
if (((struct ipas *)(cursor->link.prev))->ipf_tos & 1)
324
return NULL;
325
326
/*
327
* Reassembly is complete; concatenate fragments.
328
*/
329
cursor = first->link.next;
330
m = dtom(slirp, cursor);
331
int delta = (char *)cursor - (m->m_flags & M_EXT ? m->m_ext : m->m_dat);
332
333
cursor = cursor->link.next;
334
while (cursor != first) {
335
struct mbuf *t = dtom(slirp, cursor);
336
cursor = cursor->link.next;
337
m_cat(m, t);
338
}
339
340
/*
341
* Create header for new ip packet by
342
* modifying header of first packet;
343
* dequeue and discard fragment reassembly header.
344
* Make header visible.
345
*/
346
cursor = first->link.next;
347
348
/*
349
* If the fragments concatenated to an mbuf that's bigger than the total
350
* size of the fragment and the mbuf was not already using an m_ext buffer,
351
* then an m_ext buffer was allocated. But q->ipq_next points to the old
352
* buffer (in the mbuf), so we must point ip into the new buffer.
353
*/
354
if (m->m_flags & M_EXT) {
355
cursor = (struct ipas *)(m->m_ext + delta);
356
}
357
358
ip = astoip(cursor);
359
ip->ip_len = next;
360
ip->ip_tos &= ~1;
361
ip->ip_src = q->ipq_src;
362
ip->ip_dst = q->ipq_dst;
363
slirp_remque(&q->ip_link);
364
m_free(dtom(slirp, q));
365
m->m_len += (ip->ip_hl << 2);
366
m->m_data -= (ip->ip_hl << 2);
367
368
return ip;
369
370
dropfrag:
371
m_free(m);
372
return NULL;
373
}
374
375
/*
376
* Free a fragment reassembly header and all
377
* associated datagrams.
378
*/
379
static void ip_freef(Slirp *slirp, struct ipq *q)
380
{
381
struct ipas *first = container_of(q, struct ipas, ipq);
382
register struct ipas *cursor, *next;
383
384
for (cursor = first->link.next; cursor != first; cursor = next) {
385
next = cursor->link.next;
386
ip_deq(cursor);
387
m_free(dtom(slirp, cursor));
388
}
389
slirp_remque(&q->ip_link);
390
m_free(dtom(slirp, q));
391
}
392
393
/*
394
* Put an ip fragment on a reassembly chain.
395
* Like slirp_insque, but pointers in middle of structure.
396
*/
397
static void ip_enq(register struct ipas *p, register struct ipas *prev)
398
{
399
DEBUG_CALL("ip_enq");
400
DEBUG_ARG("prev = %p", prev);
401
p->link.prev = prev;
402
p->link.next = prev->link.next;
403
((struct ipas *)(prev->link.next))->link.prev = p;
404
prev->link.next = p;
405
}
406
407
/*
408
* To ip_enq as slirp_remque is to slirp_insque.
409
*/
410
static void ip_deq(register struct ipas *p)
411
{
412
((struct ipas *)(p->link.prev))->link.next = p->link.next;
413
((struct ipas *)(p->link.next))->link.prev = p->link.prev;
414
}
415
416
void ip_slowtimo(Slirp *slirp)
417
{
418
struct qlink *l;
419
420
DEBUG_CALL("ip_slowtimo");
421
422
l = slirp->ipq.ip_link.next;
423
424
if (l == NULL)
425
return;
426
427
while (l != &slirp->ipq.ip_link) {
428
struct ipq *q = container_of(l, struct ipq, ip_link);
429
l = l->next;
430
if (--q->ipq_ttl == 0) {
431
ip_freef(slirp, q);
432
}
433
}
434
}
435
436
void ip_stripoptions(register struct mbuf *m)
437
{
438
register int i;
439
struct ip *ip = mtod(m, struct ip *);
440
register char *opts;
441
int olen;
442
443
olen = (ip->ip_hl << 2) - sizeof(struct ip);
444
opts = (char *)(ip + 1);
445
i = m->m_len - (sizeof(struct ip) + olen);
446
memmove(opts, opts + olen, (unsigned)i);
447
m->m_len -= olen;
448
449
ip->ip_hl = sizeof(struct ip) >> 2;
450
}
451
452