summaryrefslogtreecommitdiffstats
path: root/src/libsystemd-network/ndisc-option.h
blob: da52858caa0eb728165131552508e0591a1a4653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once

#include <inttypes.h>
#include <net/ethernet.h>
#include <netinet/icmp6.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <sys/uio.h>

#include "sd-ndisc-protocol.h"

#include "icmp6-packet.h"
#include "macro.h"
#include "set.h"
#include "time-util.h"

typedef struct sd_ndisc_raw {
        uint8_t *bytes;
        size_t length;
} sd_ndisc_raw;

/* Mostly equivalent to struct nd_opt_prefix_info, but using usec_t. */
typedef struct sd_ndisc_prefix {
        uint8_t flags;
        uint8_t prefixlen;
        struct in6_addr address;
        usec_t valid_lifetime;
        usec_t preferred_lifetime;
        /* timestamp in CLOCK_BOOTTIME, used when sending option for adjusting lifetime. */
        usec_t valid_until;
        usec_t preferred_until;
} sd_ndisc_prefix;

typedef struct sd_ndisc_home_agent {
        uint16_t preference;
        usec_t lifetime;
        usec_t valid_until;
} sd_ndisc_home_agent;

typedef struct sd_ndisc_route {
        uint8_t preference;
        uint8_t prefixlen;
        struct in6_addr address;
        usec_t lifetime;
        usec_t valid_until;
} sd_ndisc_route;

typedef struct sd_ndisc_rdnss {
        size_t n_addresses;
        struct in6_addr *addresses;
        usec_t lifetime;
        usec_t valid_until;
} sd_ndisc_rdnss;

typedef struct sd_ndisc_dnssl {
        char **domains;
        usec_t lifetime;
        usec_t valid_until;
} sd_ndisc_dnssl;

typedef struct sd_ndisc_prefix64 {
        uint8_t prefixlen;
        struct in6_addr prefix;
        usec_t lifetime;
        usec_t valid_until;
} sd_ndisc_prefix64;

typedef struct sd_ndisc_option {
        uint8_t type;
        size_t offset;

        union {
                sd_ndisc_raw raw;               /* for testing or unsupported options */
                struct ether_addr mac;          /* SD_NDISC_OPTION_SOURCE_LL_ADDRESS or SD_NDISC_OPTION_TARGET_LL_ADDRESS */
                sd_ndisc_prefix prefix;         /* SD_NDISC_OPTION_PREFIX_INFORMATION */
                struct ip6_hdr hdr;             /* SD_NDISC_OPTION_REDIRECTED_HEADER */
                uint32_t mtu;                   /* SD_NDISC_OPTION_MTU */
                sd_ndisc_home_agent home_agent; /* SD_NDISC_OPTION_HOME_AGENT */
                sd_ndisc_route route;           /* SD_NDISC_OPTION_ROUTE_INFORMATION */
                sd_ndisc_rdnss rdnss;           /* SD_NDISC_OPTION_RDNSS */
                uint64_t extended_flags;        /* SD_NDISC_OPTION_FLAGS_EXTENSION */
                sd_ndisc_dnssl dnssl;           /* SD_NDISC_OPTION_DNSSL */
                char *captive_portal;           /* SD_NDISC_OPTION_CAPTIVE_PORTAL */
                sd_ndisc_prefix64 prefix64;     /* SD_NDISC_OPTION_PREF64 */
        };
} sd_ndisc_option;

/* RFC 8781: PREF64 or (NAT64 prefix) */
#define PREF64_SCALED_LIFETIME_MASK      0xfff8
#define PREF64_PLC_MASK                  0x0007
#define PREF64_MAX_LIFETIME_USEC         (65528 * USEC_PER_SEC)

typedef enum PrefixLengthCode {
        PREFIX_LENGTH_CODE_96,
        PREFIX_LENGTH_CODE_64,
        PREFIX_LENGTH_CODE_56,
        PREFIX_LENGTH_CODE_48,
        PREFIX_LENGTH_CODE_40,
        PREFIX_LENGTH_CODE_32,
        _PREFIX_LENGTH_CODE_MAX,
        _PREFIX_LENGTH_CODE_INVALID = -EINVAL,
} PrefixLengthCode;

/* rfc8781: section 4 - Scaled Lifetime: 13-bit unsigned integer. PREFIX_LEN (Prefix Length Code): 3-bit unsigned integer */
struct nd_opt_prefix64_info {
        uint8_t type;
        uint8_t length;
        uint16_t lifetime_and_plc;
        uint8_t prefix[12];
} _packed_;

int pref64_prefix_length_to_plc(uint8_t prefixlen, uint8_t *ret);

sd_ndisc_option* ndisc_option_free(sd_ndisc_option *option);

int ndisc_option_parse(
                ICMP6Packet *p,
                size_t offset,
                uint8_t *ret_type,
                size_t *ret_len,
                const uint8_t **ret_opt);

int ndisc_parse_options(ICMP6Packet *p, Set **ret_options);

static inline sd_ndisc_option* ndisc_option_get(Set *options, const sd_ndisc_option *p) {
        return set_get(options, ASSERT_PTR(p));
}
static inline sd_ndisc_option* ndisc_option_get_by_type(Set *options, uint8_t type) {
        return ndisc_option_get(options, &(const sd_ndisc_option) { .type = type });
}
int ndisc_option_get_mac(Set *options, uint8_t type, struct ether_addr *ret);

static inline void ndisc_option_remove(Set *options, const sd_ndisc_option *p) {
        ndisc_option_free(set_remove(options, ASSERT_PTR(p)));
}
static inline void ndisc_option_remove_by_type(Set *options, uint8_t type) {
        ndisc_option_remove(options, &(const sd_ndisc_option) { .type = type });
}

int ndisc_option_set_raw(
                Set **options,
                size_t length,
                const uint8_t *bytes);
int ndisc_option_add_link_layer_address(
                Set **options,
                uint8_t type,
                size_t offset,
                const struct ether_addr *mac);
static inline int ndisc_option_set_link_layer_address(
                Set **options,
                uint8_t type,
                const struct ether_addr *mac) {
        return ndisc_option_add_link_layer_address(options, type, 0, mac);
}
int ndisc_option_add_prefix_internal(
                Set **options,
                size_t offset,
                uint8_t flags,
                uint8_t prefixlen,
                const struct in6_addr *address,
                usec_t valid_lifetime,
                usec_t preferred_lifetime,
                usec_t valid_until,
                usec_t preferred_until);
static inline int ndisc_option_add_prefix(
                Set **options,
                size_t offset,
                uint8_t flags,
                uint8_t prefixlen,
                const struct in6_addr *address,
                usec_t valid_lifetime,
                usec_t preferred_lifetime) {
        return ndisc_option_add_prefix_internal(options, offset, flags, prefixlen, address,
                                                valid_lifetime, preferred_lifetime,
                                                USEC_INFINITY, USEC_INFINITY);
}
static inline int ndisc_option_set_prefix(
                Set **options,
                uint8_t flags,
                uint8_t prefixlen,
                const struct in6_addr *address,
                usec_t valid_lifetime,
                usec_t preferred_lifetime,
                usec_t valid_until,
                usec_t preferred_until) {
        return ndisc_option_add_prefix_internal(options, 0, flags, prefixlen, address,
                                                valid_lifetime, preferred_lifetime,
                                                valid_until, preferred_until);
}
int ndisc_option_add_redirected_header(
                Set **options,
                size_t offset,
                const struct ip6_hdr *hdr);
int ndisc_option_add_mtu(
                Set **options,
                size_t offset,
                uint32_t mtu);
static inline int ndisc_option_set_mtu(
                Set **options,
                uint32_t mtu) {
        return ndisc_option_add_mtu(options, 0, mtu);
}
int ndisc_option_add_home_agent_internal(
                Set **options,
                size_t offset,
                uint16_t preference,
                usec_t lifetime,
                usec_t valid_until);
static inline int ndisc_option_add_home_agent(
                Set **options,
                size_t offset,
                uint16_t preference,
                usec_t lifetime) {
        return ndisc_option_add_home_agent_internal(options, offset, preference, lifetime, USEC_INFINITY);
}
static inline int ndisc_option_set_home_agent(
                Set **options,
                uint16_t preference,
                usec_t lifetime,
                usec_t valid_until) {
        return ndisc_option_add_home_agent_internal(options, 0, preference, lifetime, valid_until);
}
int ndisc_option_add_route_internal(
                Set **options,
                size_t offset,
                uint8_t preference,
                uint8_t prefixlen,
                const struct in6_addr *prefix,
                usec_t lifetime,
                usec_t valid_until);
static inline int ndisc_option_add_route(
                Set **options,
                size_t offset,
                uint8_t preference,
                uint8_t prefixlen,
                const struct in6_addr *prefix,
                usec_t lifetime) {
        return ndisc_option_add_route_internal(options, offset, preference, prefixlen, prefix, lifetime, USEC_INFINITY);
}
static inline int ndisc_option_set_route(
                Set **options,
                uint8_t preference,
                uint8_t prefixlen,
                const struct in6_addr *prefix,
                usec_t lifetime,
                usec_t valid_until) {
        return ndisc_option_add_route_internal(options, 0, preference, prefixlen, prefix, lifetime, valid_until);
}
int ndisc_option_add_rdnss_internal(
                Set **options,
                size_t offset,
                size_t n_addresses,
                const struct in6_addr *addresses,
                usec_t lifetime,
                usec_t valid_until);
static inline int ndisc_option_add_rdnss(
                Set **options,
                size_t offset,
                size_t n_addresses,
                const struct in6_addr *addresses,
                usec_t lifetime) {
        return ndisc_option_add_rdnss_internal(options, offset, n_addresses, addresses, lifetime, USEC_INFINITY);
}
static inline int ndisc_option_set_rdnss(
                Set **options,
                size_t n_addresses,
                const struct in6_addr *addresses,
                usec_t lifetime,
                usec_t valid_until) {
        return ndisc_option_add_rdnss_internal(options, 0, n_addresses, addresses, lifetime, valid_until);
}
int ndisc_option_add_flags_extension(
                Set **options,
                size_t offset,
                uint64_t flags);
int ndisc_option_add_dnssl_internal(
                Set **options,
                size_t offset,
                char * const *domains,
                usec_t lifetime,
                usec_t valid_until);
static inline int ndisc_option_add_dnssl(
                Set **options,
                size_t offset,
                char * const *domains,
                usec_t lifetime) {
        return ndisc_option_add_dnssl_internal(options, offset, domains, lifetime, USEC_INFINITY);
}
static inline int ndisc_option_set_dnssl(
                Set **options,
                char * const *domains,
                usec_t lifetime,
                usec_t valid_until) {
        return ndisc_option_add_dnssl_internal(options, 0, domains, lifetime, valid_until);
}
int ndisc_option_add_captive_portal(
                Set **options,
                size_t offset,
                const char *portal);
static inline int ndisc_option_set_captive_portal(
                Set **options,
                const char *portal) {
        return ndisc_option_add_captive_portal(options, 0, portal);
}
int ndisc_option_add_prefix64_internal(
                Set **options,
                size_t offset,
                uint8_t prefixlen,
                const struct in6_addr *prefix,
                usec_t lifetime,
                usec_t valid_until);
static inline int ndisc_option_add_prefix64(
                Set **options,
                size_t offset,
                uint8_t prefixlen,
                const struct in6_addr *prefix,
                usec_t lifetime) {
        return ndisc_option_add_prefix64_internal(options, offset, prefixlen, prefix, lifetime, USEC_INFINITY);
}
static inline int ndisc_option_set_prefix64(
                Set **options,
                uint8_t prefixlen,
                const struct in6_addr *prefix,
                usec_t lifetime,
                usec_t valid_until) {
        return ndisc_option_add_prefix64_internal(options, 0, prefixlen, prefix, lifetime, valid_until);
}

int ndisc_send(int fd, const struct sockaddr_in6 *dst, const struct icmp6_hdr *hdr, Set *options, usec_t timestamp);