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_icmp.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_icmp.c 8.2 (Berkeley) 1/4/94
31
* ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
32
*/
33
34
#include "slirp.h"
35
#include "ip_icmp.h"
36
37
#ifndef _WIN32
38
#include <sys/param.h>
39
#endif
40
41
#ifndef WITH_ICMP_ERROR_MSG
42
#define WITH_ICMP_ERROR_MSG 0
43
#endif
44
45
/* The message sent when emulating PING */
46
/* Be nice and tell them it's just a pseudo-ping packet */
47
static const char icmp_ping_msg[] =
48
"This is a pseudo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST "
49
"packets.\n";
50
51
/* list of actions for icmp_send_error() on RX of an icmp message */
52
static const int icmp_flush[19] = {
53
/* ECHO REPLY (0) */ 0,
54
1,
55
1,
56
/* DEST UNREACH (3) */ 1,
57
/* SOURCE QUENCH (4)*/ 1,
58
/* REDIRECT (5) */ 1,
59
1,
60
1,
61
/* ECHO (8) */ 0,
62
/* ROUTERADVERT (9) */ 1,
63
/* ROUTERSOLICIT (10) */ 1,
64
/* TIME EXCEEDED (11) */ 1,
65
/* PARAMETER PROBLEM (12) */ 1,
66
/* TIMESTAMP (13) */ 0,
67
/* TIMESTAMP REPLY (14) */ 0,
68
/* INFO (15) */ 0,
69
/* INFO REPLY (16) */ 0,
70
/* ADDR MASK (17) */ 0,
71
/* ADDR MASK REPLY (18) */ 0
72
};
73
74
void icmp_init(Slirp *slirp)
75
{
76
slirp->icmp.so_next = slirp->icmp.so_prev = &slirp->icmp;
77
slirp->icmp_last_so = &slirp->icmp;
78
}
79
80
void icmp_cleanup(Slirp *slirp)
81
{
82
struct socket *so, *so_next;
83
84
for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so_next) {
85
so_next = so->so_next;
86
icmp_detach(so);
87
}
88
}
89
90
/* Send ICMP packet to the Internet, and save it to so_m */
91
static int icmp_send(struct socket *so, struct mbuf *m, int hlen)
92
{
93
Slirp *slirp = m->slirp;
94
95
struct sockaddr_in addr;
96
97
/*
98
* The behavior of reading SOCK_DGRAM+IPPROTO_ICMP sockets is inconsistent
99
* between host OSes. On Linux, only the ICMP header and payload is
100
* included. On macOS/Darwin, the socket acts like a raw socket and
101
* includes the IP header as well. On other BSDs, SOCK_DGRAM+IPPROTO_ICMP
102
* sockets aren't supported at all, so we treat them like raw sockets. It
103
* isn't possible to detect this difference at runtime, so we must use an
104
* #ifdef to determine if we need to remove the IP header.
105
*/
106
#if defined(BSD) && !defined(__GNU__)
107
so->so_type = IPPROTO_IP;
108
#else
109
so->so_type = IPPROTO_ICMP;
110
#endif
111
112
so->s = slirp_socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
113
if (so->s == -1) {
114
if (errno == EAFNOSUPPORT
115
|| errno == EPROTONOSUPPORT
116
|| errno == EACCES) {
117
/* Kernel doesn't support or allow ping sockets. */
118
so->so_type = IPPROTO_IP;
119
so->s = slirp_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
120
}
121
}
122
if (so->s == -1) {
123
return -1;
124
}
125
so->slirp->cb->register_poll_fd(so->s, so->slirp->opaque);
126
127
if (slirp_bind_outbound(so, AF_INET) != 0) {
128
// bind failed - close socket
129
closesocket(so->s);
130
so->s = -1;
131
return -1;
132
}
133
134
M_DUP_DEBUG(slirp, m, 0, 0);
135
struct ip *ip = mtod(m, struct ip *);
136
137
so->so_m = m;
138
so->so_faddr = ip->ip_dst;
139
so->so_laddr = ip->ip_src;
140
so->so_iptos = ip->ip_tos;
141
so->so_state = SS_ISFCONNECTED;
142
so->so_expire = curtime + SO_EXPIRE;
143
144
addr.sin_family = AF_INET;
145
addr.sin_addr = so->so_faddr;
146
147
slirp_insque(so, &so->slirp->icmp);
148
149
if (sendto(so->s, m->m_data + hlen, m->m_len - hlen, 0,
150
(struct sockaddr *)&addr, sizeof(addr)) == -1) {
151
DEBUG_MISC("icmp_input icmp sendto tx errno = %d-%s", errno,
152
strerror(errno));
153
icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
154
icmp_detach(so);
155
}
156
157
return 0;
158
}
159
160
void icmp_detach(struct socket *so)
161
{
162
so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
163
closesocket(so->s);
164
sofree(so);
165
}
166
167
/*
168
* Process a received ICMP message.
169
*/
170
void icmp_input(struct mbuf *m, int hlen)
171
{
172
Slirp *slirp = m->slirp;
173
M_DUP_DEBUG(slirp, m, 0, 0);
174
175
register struct icmp *icp;
176
register struct ip *ip = mtod(m, struct ip *);
177
int icmplen = ip->ip_len;
178
179
DEBUG_CALL("icmp_input");
180
DEBUG_ARG("m = %p", m);
181
DEBUG_ARG("m_len = %d", m->m_len);
182
183
/*
184
* Locate icmp structure in mbuf, and check
185
* that its not corrupted and of at least minimum length.
186
*/
187
if (icmplen < ICMP_MINLEN) { /* min 8 bytes payload */
188
freeit:
189
m_free(m);
190
goto end_error;
191
}
192
193
m->m_len -= hlen;
194
m->m_data += hlen;
195
icp = mtod(m, struct icmp *);
196
if (cksum(m, icmplen)) {
197
goto freeit;
198
}
199
m->m_len += hlen;
200
m->m_data -= hlen;
201
202
DEBUG_ARG("icmp_type = %d", icp->icmp_type);
203
switch (icp->icmp_type) {
204
case ICMP_ECHO:
205
ip->ip_len += hlen; /* since ip_input subtracts this */
206
if (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr ||
207
ip->ip_dst.s_addr == slirp->vnameserver_addr.s_addr) {
208
icmp_reflect(m);
209
} else if (slirp->restricted) {
210
goto freeit;
211
} else {
212
struct socket *so;
213
struct sockaddr_storage addr;
214
int ttl;
215
216
so = socreate(slirp, IPPROTO_ICMP);
217
if (icmp_send(so, m, hlen) == 0) {
218
/* We could send this as ICMP, good! */
219
return;
220
}
221
222
/* We could not send this as ICMP, try to send it on UDP echo
223
* service (7), wishfully hoping that it is open there. */
224
225
if (udp_attach(so, AF_INET) == -1) {
226
DEBUG_MISC("icmp_input udp_attach errno = %d-%s", errno,
227
strerror(errno));
228
sofree(so);
229
m_free(m);
230
goto end_error;
231
}
232
so->so_m = m;
233
so->so_ffamily = AF_INET;
234
so->so_faddr = ip->ip_dst;
235
so->so_fport = htons(7);
236
so->so_lfamily = AF_INET;
237
so->so_laddr = ip->ip_src;
238
so->so_lport = htons(9);
239
so->so_iptos = ip->ip_tos;
240
so->so_state = SS_ISFCONNECTED;
241
242
/* Send the packet */
243
addr = so->fhost.ss;
244
if (sotranslate_out(so, &addr) < 0) {
245
icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
246
strerror(errno));
247
udp_detach(so);
248
return;
249
}
250
251
/*
252
* Check for TTL
253
*/
254
ttl = ip->ip_ttl-1;
255
if (ttl <= 0) {
256
DEBUG_MISC("udp ttl exceeded");
257
icmp_send_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0,
258
NULL);
259
udp_detach(so);
260
break;
261
}
262
setsockopt(so->s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
263
264
if (sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0,
265
(struct sockaddr *)&addr, sockaddr_size(&addr)) == -1) {
266
DEBUG_MISC("icmp_input udp sendto tx errno = %d-%s", errno,
267
strerror(errno));
268
icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
269
strerror(errno));
270
udp_detach(so);
271
}
272
} /* if ip->ip_dst.s_addr == alias_addr.s_addr */
273
break;
274
case ICMP_UNREACH:
275
/* XXX? report error? close socket? */
276
case ICMP_TIMXCEED:
277
case ICMP_PARAMPROB:
278
case ICMP_SOURCEQUENCH:
279
case ICMP_TSTAMP:
280
case ICMP_MASKREQ:
281
case ICMP_REDIRECT:
282
m_free(m);
283
break;
284
285
default:
286
m_free(m);
287
} /* switch */
288
289
end_error:
290
/* m is m_free()'d xor put in a socket xor or given to ip_send */
291
return;
292
}
293
294
295
/*
296
* Send an ICMP message in response to a situation
297
*
298
* RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
299
*MAY send more (we do). MUST NOT change this header information. MUST NOT reply
300
*to a multicast/broadcast IP address. MUST NOT reply to a multicast/broadcast
301
*MAC address. MUST reply to only the first fragment.
302
*/
303
/*
304
* Send ICMP_UNREACH back to the source regarding msrc.
305
* mbuf *msrc is used as a template, but is NOT m_free()'d.
306
* It is reported as the bad ip packet. The header should
307
* be fully correct and in host byte order.
308
* ICMP fragmentation is illegal. All machines must accept 576 bytes in one
309
* packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
310
*/
311
312
#define ICMP_MAXDATALEN (IP_MSS - 28)
313
void icmp_forward_error(struct mbuf *msrc, uint8_t type, uint8_t code, int minsize,
314
const char *message, struct in_addr *src)
315
{
316
unsigned hlen, shlen, s_ip_len;
317
register struct ip *ip;
318
register struct icmp *icp;
319
register struct mbuf *m;
320
321
DEBUG_CALL("icmp_send_error");
322
DEBUG_ARG("msrc = %p", msrc);
323
DEBUG_ARG("msrc_len = %d", msrc->m_len);
324
325
if (type != ICMP_UNREACH && type != ICMP_TIMXCEED)
326
goto end_error;
327
328
/* check msrc */
329
if (!msrc)
330
goto end_error;
331
ip = mtod(msrc, struct ip *);
332
if (slirp_debug & DBG_MISC) {
333
char addr_src[INET_ADDRSTRLEN];
334
char addr_dst[INET_ADDRSTRLEN];
335
336
inet_ntop(AF_INET, &ip->ip_src, addr_src, sizeof(addr_src));
337
inet_ntop(AF_INET, &ip->ip_dst, addr_dst, sizeof(addr_dst));
338
DEBUG_MISC(" %.16s to %.16s", addr_src, addr_dst);
339
}
340
if (ip->ip_off & IP_OFFMASK)
341
goto end_error; /* Only reply to fragment 0 */
342
343
/* Do not reply to source-only IPs */
344
if ((ip->ip_src.s_addr & htonl(~(0xf << 28))) == 0) {
345
goto end_error;
346
}
347
348
shlen = ip->ip_hl << 2;
349
s_ip_len = ip->ip_len;
350
if (ip->ip_p == IPPROTO_ICMP) {
351
icp = (struct icmp *)((char *)ip + shlen);
352
/*
353
* Assume any unknown ICMP type is an error. This isn't
354
* specified by the RFC, but think about it..
355
*/
356
if (icp->icmp_type > 18 || icmp_flush[icp->icmp_type])
357
goto end_error;
358
}
359
360
/* make a copy */
361
m = m_get(msrc->slirp);
362
if (!m) {
363
goto end_error;
364
}
365
366
{
367
int new_m_size;
368
new_m_size =
369
sizeof(struct ip) + ICMP_MINLEN + msrc->m_len + ICMP_MAXDATALEN;
370
if (new_m_size > m->m_size)
371
m_inc(m, new_m_size);
372
}
373
memcpy(m->m_data, msrc->m_data, msrc->m_len);
374
m->m_len = msrc->m_len; /* copy msrc to m */
375
376
/* make the header of the reply packet */
377
ip = mtod(m, struct ip *);
378
hlen = sizeof(struct ip); /* no options in reply */
379
380
/* fill in icmp */
381
m->m_data += hlen;
382
m->m_len -= hlen;
383
384
icp = mtod(m, struct icmp *);
385
386
if (minsize)
387
s_ip_len = shlen + ICMP_MINLEN; /* return header+8b only */
388
else if (s_ip_len > ICMP_MAXDATALEN) /* maximum size */
389
s_ip_len = ICMP_MAXDATALEN;
390
391
m->m_len = ICMP_MINLEN + s_ip_len; /* 8 bytes ICMP header */
392
393
/* min. size = 8+sizeof(struct ip)+8 */
394
395
icp->icmp_type = type;
396
icp->icmp_code = code;
397
icp->icmp_id = 0;
398
icp->icmp_seq = 0;
399
400
memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
401
HTONS(icp->icmp_ip.ip_len);
402
HTONS(icp->icmp_ip.ip_id);
403
HTONS(icp->icmp_ip.ip_off);
404
405
if (message && WITH_ICMP_ERROR_MSG) { /* append message to ICMP packet */
406
int message_len;
407
char *cpnt;
408
message_len = strlen(message);
409
if (message_len > ICMP_MAXDATALEN)
410
message_len = ICMP_MAXDATALEN;
411
cpnt = (char *)m->m_data + m->m_len;
412
memcpy(cpnt, message, message_len);
413
m->m_len += message_len;
414
}
415
416
icp->icmp_cksum = 0;
417
icp->icmp_cksum = cksum(m, m->m_len);
418
419
m->m_data -= hlen;
420
m->m_len += hlen;
421
422
/* fill in ip */
423
ip->ip_hl = hlen >> 2;
424
ip->ip_len = m->m_len;
425
426
ip->ip_tos = ((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */
427
428
ip->ip_ttl = MAXTTL;
429
ip->ip_p = IPPROTO_ICMP;
430
ip->ip_dst = ip->ip_src; /* ip addresses */
431
ip->ip_src = *src;
432
433
ip_output((struct socket *)NULL, m);
434
435
end_error:
436
return;
437
}
438
#undef ICMP_MAXDATALEN
439
440
void icmp_send_error(struct mbuf *msrc, uint8_t type, uint8_t code, int minsize,
441
const char *message)
442
{
443
icmp_forward_error(msrc, type, code, minsize, message, &msrc->slirp->vhost_addr);
444
}
445
446
/*
447
* Reflect the ip packet back to the source
448
*/
449
void icmp_reflect(struct mbuf *m)
450
{
451
register struct ip *ip = mtod(m, struct ip *);
452
int hlen = ip->ip_hl << 2;
453
int optlen = hlen - sizeof(struct ip);
454
register struct icmp *icp;
455
456
/*
457
* Send an icmp packet back to the ip level,
458
* after supplying a checksum.
459
*/
460
m->m_data += hlen;
461
m->m_len -= hlen;
462
icp = mtod(m, struct icmp *);
463
464
icp->icmp_type = ICMP_ECHOREPLY;
465
icp->icmp_cksum = 0;
466
icp->icmp_cksum = cksum(m, ip->ip_len - hlen);
467
468
m->m_data -= hlen;
469
m->m_len += hlen;
470
471
/* fill in ip */
472
if (optlen > 0) {
473
/*
474
* Strip out original options by copying rest of first
475
* mbuf's data back, and adjust the IP length.
476
*/
477
memmove((char *)(ip + 1), (char *)ip + hlen,
478
(unsigned)(m->m_len - hlen));
479
hlen -= optlen;
480
ip->ip_hl = hlen >> 2;
481
ip->ip_len -= optlen;
482
m->m_len -= optlen;
483
}
484
485
ip->ip_ttl = MAXTTL;
486
{ /* swap */
487
struct in_addr icmp_dst;
488
icmp_dst = ip->ip_dst;
489
ip->ip_dst = ip->ip_src;
490
ip->ip_src = icmp_dst;
491
}
492
493
ip_output((struct socket *)NULL, m);
494
}
495
496
void icmp_receive(struct socket *so)
497
{
498
struct mbuf *m = so->so_m;
499
struct ip *ip = mtod(m, struct ip *);
500
int hlen = ip->ip_hl << 2;
501
uint8_t error_code;
502
struct icmp *icp;
503
int id, len;
504
505
m->m_data += hlen;
506
m->m_len -= hlen;
507
icp = mtod(m, struct icmp *);
508
509
id = icp->icmp_id;
510
len = recv(so->s, icp, M_ROOM(m), 0);
511
512
if (so->so_type == IPPROTO_IP) {
513
if (len >= sizeof(struct ip)) {
514
struct ip *inner_ip = mtod(m, struct ip *);
515
int inner_hlen = inner_ip->ip_hl << 2;
516
if (inner_hlen > len) {
517
len = -1;
518
errno = -EINVAL;
519
} else {
520
len -= inner_hlen;
521
memmove(icp, (unsigned char *)icp + inner_hlen, len);
522
}
523
} else {
524
len = -1;
525
errno = -EINVAL;
526
}
527
}
528
529
icp->icmp_id = id;
530
531
m->m_data -= hlen;
532
m->m_len += hlen;
533
534
if (len == -1 || len == 0) {
535
if (errno == ENETUNREACH) {
536
error_code = ICMP_UNREACH_NET;
537
} else {
538
error_code = ICMP_UNREACH_HOST;
539
}
540
DEBUG_MISC(" udp icmp rx errno = %d-%s", errno, strerror(errno));
541
icmp_send_error(so->so_m, ICMP_UNREACH, error_code, 0, strerror(errno));
542
} else {
543
icmp_reflect(so->so_m);
544
so->so_m = NULL; /* Don't m_free() it again! */
545
}
546
icmp_detach(so);
547
}
548
549