Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/include/ip6.h
2 views
1
/* SPDX-License-Identifier: BSD-3-Clause */
2
/*
3
* Copyright (c) 2013
4
* Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
5
*/
6
7
#ifndef SLIRP_IP6_H
8
#define SLIRP_IP6_H
9
10
#include <glib.h>
11
#include <string.h>
12
13
#include "util.h"
14
15
#define ALLNODES_MULTICAST \
16
{ \
17
.s6_addr = { \
18
0xff, \
19
0x02, \
20
0x00, \
21
0x00, \
22
0x00, \
23
0x00, \
24
0x00, \
25
0x00, \
26
0x00, \
27
0x00, \
28
0x00, \
29
0x00, \
30
0x00, \
31
0x00, \
32
0x00, \
33
0x01 \
34
} \
35
}
36
37
#define SOLICITED_NODE_PREFIX \
38
{ \
39
.s6_addr = { \
40
0xff, \
41
0x02, \
42
0x00, \
43
0x00, \
44
0x00, \
45
0x00, \
46
0x00, \
47
0x00, \
48
0x00, \
49
0x00, \
50
0x00, \
51
0x01, \
52
0xff, \
53
0x00, \
54
0x00, \
55
0x00 \
56
} \
57
}
58
59
#define LINKLOCAL_ADDR \
60
{ \
61
.s6_addr = { \
62
0xfe, \
63
0x80, \
64
0x00, \
65
0x00, \
66
0x00, \
67
0x00, \
68
0x00, \
69
0x00, \
70
0x00, \
71
0x00, \
72
0x00, \
73
0x00, \
74
0x00, \
75
0x00, \
76
0x00, \
77
0x02 \
78
} \
79
}
80
81
#define ZERO_ADDR \
82
{ \
83
.s6_addr = { \
84
0x00, \
85
0x00, \
86
0x00, \
87
0x00, \
88
0x00, \
89
0x00, \
90
0x00, \
91
0x00, \
92
0x00, \
93
0x00, \
94
0x00, \
95
0x00, \
96
0x00, \
97
0x00, \
98
0x00, \
99
0x00 \
100
} \
101
}
102
103
/* Check that two IPv6 addresses are equal */
104
static inline bool in6_equal(const struct in6_addr *a, const struct in6_addr *b)
105
{
106
return memcmp(a, b, sizeof(*a)) == 0;
107
}
108
109
/* Check that two IPv6 addresses are equal in their network part */
110
static inline bool in6_equal_net(const struct in6_addr *a,
111
const struct in6_addr *b, int prefix_len)
112
{
113
if (memcmp(a, b, prefix_len / 8) != 0) {
114
return 0;
115
}
116
117
if (prefix_len % 8 == 0) {
118
return 1;
119
}
120
121
return a->s6_addr[prefix_len / 8] >> (8 - (prefix_len % 8)) ==
122
b->s6_addr[prefix_len / 8] >> (8 - (prefix_len % 8));
123
}
124
125
/* Check that two IPv6 addresses are equal in their machine part */
126
static inline bool in6_equal_mach(const struct in6_addr *a,
127
const struct in6_addr *b, int prefix_len)
128
{
129
if (memcmp(&(a->s6_addr[DIV_ROUND_UP(prefix_len, 8)]),
130
&(b->s6_addr[DIV_ROUND_UP(prefix_len, 8)]),
131
16 - DIV_ROUND_UP(prefix_len, 8)) != 0) {
132
return 0;
133
}
134
135
if (prefix_len % 8 == 0) {
136
return 1;
137
}
138
139
return (a->s6_addr[prefix_len / 8] &
140
((1U << (8 - (prefix_len % 8))) - 1)) ==
141
(b->s6_addr[prefix_len / 8] & ((1U << (8 - (prefix_len % 8))) - 1));
142
}
143
144
/* Check that the IPv6 is equal to the virtual router */
145
#define in6_equal_router(a) \
146
((in6_equal_net(a, &slirp->vprefix_addr6, slirp->vprefix_len) && \
147
in6_equal_mach(a, &slirp->vhost_addr6, slirp->vprefix_len)) || \
148
(in6_equal_net(a, &(struct in6_addr)LINKLOCAL_ADDR, 64) && \
149
in6_equal_mach(a, &slirp->vhost_addr6, 64)))
150
151
/* Check that the IPv6 is equal to the virtual DNS server */
152
#define in6_equal_dns(a) \
153
((in6_equal_net(a, &slirp->vprefix_addr6, slirp->vprefix_len) && \
154
in6_equal_mach(a, &slirp->vnameserver_addr6, slirp->vprefix_len)) || \
155
(in6_equal_net(a, &(struct in6_addr)LINKLOCAL_ADDR, 64) && \
156
in6_equal_mach(a, &slirp->vnameserver_addr6, 64)))
157
158
/* Check that the IPv6 is equal to the host */
159
#define in6_equal_host(a) (in6_equal_router(a) || in6_equal_dns(a))
160
161
/* Check that the IPv6 is within the sollicited node multicast network */
162
#define in6_solicitednode_multicast(a) \
163
(in6_equal_net(a, &(struct in6_addr)SOLICITED_NODE_PREFIX, 104))
164
165
/* Check that the IPv6 is zero */
166
#define in6_zero(a) (in6_equal(a, &(struct in6_addr)ZERO_ADDR))
167
168
/* Compute emulated host MAC address from its ipv6 address */
169
static inline void in6_compute_ethaddr(struct in6_addr ip,
170
uint8_t eth[ETH_ALEN])
171
{
172
eth[0] = 0x52;
173
eth[1] = 0x56;
174
memcpy(&eth[2], &ip.s6_addr[16 - (ETH_ALEN - 2)], ETH_ALEN - 2);
175
}
176
177
/*
178
* Definitions for internet protocol version 6.
179
* Per RFC 2460, December 1998.
180
*/
181
#define IP6VERSION 6
182
#define IP6_HOP_LIMIT 255
183
184
/*
185
* Structure of an internet header, naked of options.
186
*/
187
struct ip6 {
188
#if (G_BYTE_ORDER == G_BIG_ENDIAN) && !defined(_MSC_VER)
189
uint8_t ip_v : 4, /* version */
190
ip_tc_hi : 4; /* traffic class */
191
uint8_t ip_tc_lo : 4, ip_fl_hi : 4; /* flow label */
192
#else
193
uint8_t ip_tc_hi : 4, ip_v : 4;
194
uint8_t ip_fl_hi : 4, ip_tc_lo : 4;
195
#endif
196
uint16_t ip_fl_lo;
197
uint16_t ip_pl; /* payload length */
198
uint8_t ip_nh; /* next header */
199
uint8_t ip_hl; /* hop limit */
200
struct in6_addr ip_src, ip_dst; /* source and dest address */
201
};
202
203
/*
204
* IPv6 pseudo-header used by upper-layer protocols
205
*/
206
struct ip6_pseudohdr {
207
struct in6_addr ih_src; /* source internet address */
208
struct in6_addr ih_dst; /* destination internet address */
209
uint32_t ih_pl; /* upper-layer packet length */
210
uint16_t ih_zero_hi; /* zero */
211
uint8_t ih_zero_lo; /* zero */
212
uint8_t ih_nh; /* next header */
213
};
214
215
/*
216
* We don't want to mark these ip6 structs as packed as they are naturally
217
* correctly aligned; instead assert that there is no stray padding.
218
* If we marked the struct as packed then we would be unable to take
219
* the address of any of the fields in it.
220
*/
221
G_STATIC_ASSERT(sizeof(struct ip6) == 40);
222
G_STATIC_ASSERT(sizeof(struct ip6_pseudohdr) == 40);
223
224
#endif
225
226