| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <net/ethernet.h> |
| 5 |
#include <netinet/ip6.h> |
| 6 |
|
| 7 |
#include "sd-dns-resolver.h" |
| 8 |
#include "sd-forward.h" |
| 9 |
|
| 10 |
#include "time-util.h" |
| 11 |
|
| 12 |
typedef struct sd_ndisc_raw { |
| 13 |
uint8_t *bytes; |
| 14 |
size_t length; |
| 15 |
} sd_ndisc_raw; |
| 16 |
|
| 17 |
/* Mostly equivalent to struct nd_opt_prefix_info, but using usec_t. */ |
| 18 |
typedef struct sd_ndisc_prefix { |
| 19 |
uint8_t flags; |
| 20 |
uint8_t prefixlen; |
| 21 |
struct in6_addr address; |
| 22 |
usec_t valid_lifetime; |
| 23 |
usec_t preferred_lifetime; |
| 24 |
/* timestamp in CLOCK_BOOTTIME, used when sending option for adjusting lifetime. */ |
| 25 |
usec_t valid_until; |
| 26 |
usec_t preferred_until; |
| 27 |
} sd_ndisc_prefix; |
| 28 |
|
| 29 |
typedef struct sd_ndisc_home_agent { |
| 30 |
uint16_t preference; |
| 31 |
usec_t lifetime; |
| 32 |
usec_t valid_until; |
| 33 |
} sd_ndisc_home_agent; |
| 34 |
|
| 35 |
typedef struct sd_ndisc_route { |
| 36 |
uint8_t preference; |
| 37 |
uint8_t prefixlen; |
| 38 |
struct in6_addr address; |
| 39 |
usec_t lifetime; |
| 40 |
usec_t valid_until; |
| 41 |
} sd_ndisc_route; |
| 42 |
|
| 43 |
typedef struct sd_ndisc_rdnss { |
| 44 |
size_t n_addresses; |
| 45 |
struct in6_addr *addresses; |
| 46 |
usec_t lifetime; |
| 47 |
usec_t valid_until; |
| 48 |
} sd_ndisc_rdnss; |
| 49 |
|
| 50 |
typedef struct sd_ndisc_dnssl { |
| 51 |
char **domains; |
| 52 |
usec_t lifetime; |
| 53 |
usec_t valid_until; |
| 54 |
} sd_ndisc_dnssl; |
| 55 |
|
| 56 |
typedef struct sd_ndisc_prefix64 { |
| 57 |
uint8_t prefixlen; |
| 58 |
struct in6_addr prefix; |
| 59 |
usec_t lifetime; |
| 60 |
usec_t valid_until; |
| 61 |
} sd_ndisc_prefix64; |
| 62 |
|
| 63 |
typedef struct sd_ndisc_dnr { |
| 64 |
sd_dns_resolver *resolver; |
| 65 |
usec_t lifetime; |
| 66 |
usec_t valid_until; |
| 67 |
} sd_ndisc_dnr; |
| 68 |
|
| 69 |
typedef struct sd_ndisc_option { |
| 70 |
uint8_t type; |
| 71 |
size_t offset; |
| 72 |
|
| 73 |
union { |
| 74 |
sd_ndisc_raw raw; /* for testing or unsupported options */ |
| 75 |
struct ether_addr mac; /* SD_NDISC_OPTION_SOURCE_LL_ADDRESS or SD_NDISC_OPTION_TARGET_LL_ADDRESS */ |
| 76 |
sd_ndisc_prefix prefix; /* SD_NDISC_OPTION_PREFIX_INFORMATION */ |
| 77 |
struct ip6_hdr hdr; /* SD_NDISC_OPTION_REDIRECTED_HEADER */ |
| 78 |
uint32_t mtu; /* SD_NDISC_OPTION_MTU */ |
| 79 |
sd_ndisc_home_agent home_agent; /* SD_NDISC_OPTION_HOME_AGENT */ |
| 80 |
sd_ndisc_route route; /* SD_NDISC_OPTION_ROUTE_INFORMATION */ |
| 81 |
sd_ndisc_rdnss rdnss; /* SD_NDISC_OPTION_RDNSS */ |
| 82 |
uint64_t extended_flags; /* SD_NDISC_OPTION_FLAGS_EXTENSION */ |
| 83 |
sd_ndisc_dnssl dnssl; /* SD_NDISC_OPTION_DNSSL */ |
| 84 |
char *captive_portal; /* SD_NDISC_OPTION_CAPTIVE_PORTAL */ |
| 85 |
sd_ndisc_prefix64 prefix64; /* SD_NDISC_OPTION_PREF64 */ |
| 86 |
sd_ndisc_dnr encrypted_dns; /* SD_NDISC_OPTION_ENCRYPTED_DNS */ |
| 87 |
}; |
| 88 |
} sd_ndisc_option; |
| 89 |
|
| 90 |
/* RFC 8781: PREF64 or (NAT64 prefix) */ |
| 91 |
#define PREF64_SCALED_LIFETIME_MASK 0xfff8 |
| 92 |
#define PREF64_PLC_MASK 0x0007 |
| 93 |
#define PREF64_MAX_LIFETIME_USEC (65528 * USEC_PER_SEC) |
| 94 |
|
| 95 |
typedef enum PrefixLengthCode { |
| 96 |
PREFIX_LENGTH_CODE_96, |
| 97 |
PREFIX_LENGTH_CODE_64, |
| 98 |
PREFIX_LENGTH_CODE_56, |
| 99 |
PREFIX_LENGTH_CODE_48, |
| 100 |
PREFIX_LENGTH_CODE_40, |
| 101 |
PREFIX_LENGTH_CODE_32, |
| 102 |
_PREFIX_LENGTH_CODE_MAX, |
| 103 |
_PREFIX_LENGTH_CODE_INVALID = -EINVAL, |
| 104 |
} PrefixLengthCode; |
| 105 |
|
| 106 |
/* rfc8781: section 4 - Scaled Lifetime: 13-bit unsigned integer. PREFIX_LEN (Prefix Length Code): 3-bit unsigned integer */ |
| 107 |
struct nd_opt_prefix64_info { |
| 108 |
uint8_t type; |
| 109 |
uint8_t length; |
| 110 |
uint16_t lifetime_and_plc; |
| 111 |
uint8_t prefix[12]; |
| 112 |
} _packed_; |
| 113 |
|
| 114 |
int pref64_prefix_length_to_plc(uint8_t prefixlen, uint8_t *ret); |
| 115 |
|
| 116 |
sd_ndisc_option* ndisc_option_free(sd_ndisc_option *option); |
| 117 |
|
| 118 |
int ndisc_option_parse( |
| 119 |
ICMP6Packet *p, |
| 120 |
size_t offset, |
| 121 |
uint8_t *ret_type, |
| 122 |
size_t *ret_len, |
| 123 |
const uint8_t **ret_opt); |
| 124 |
|
| 125 |
int ndisc_parse_options(ICMP6Packet *p, Set **ret_options); |
| 126 |
|
| 127 |
sd_ndisc_option* ndisc_option_get(Set *options, const sd_ndisc_option *p); |
| 128 |
sd_ndisc_option* ndisc_option_get_by_type(Set *options, uint8_t type); |
| 129 |
int ndisc_option_get_mac(Set *options, uint8_t type, struct ether_addr *ret); |
| 130 |
|
| 131 |
void ndisc_option_remove(Set *options, const sd_ndisc_option *p); |
| 132 |
static inline void ndisc_option_remove_by_type(Set *options, uint8_t type) { |
| 133 |
ndisc_option_remove(options, &(const sd_ndisc_option) { .type = type }); |
| 134 |
} |
| 135 |
|
| 136 |
int ndisc_option_add_link_layer_address( |
| 137 |
Set **options, |
| 138 |
uint8_t type, |
| 139 |
size_t offset, |
| 140 |
const struct ether_addr *mac); |
| 141 |
static inline int ndisc_option_set_link_layer_address( |
| 142 |
Set **options, |
| 143 |
uint8_t type, |
| 144 |
const struct ether_addr *mac) { |
| 145 |
return ndisc_option_add_link_layer_address(options, type, 0, mac); |
| 146 |
} |
| 147 |
int ndisc_option_add_prefix_internal( |
| 148 |
Set **options, |
| 149 |
size_t offset, |
| 150 |
uint8_t flags, |
| 151 |
uint8_t prefixlen, |
| 152 |
const struct in6_addr *address, |
| 153 |
usec_t valid_lifetime, |
| 154 |
usec_t preferred_lifetime, |
| 155 |
usec_t valid_until, |
| 156 |
usec_t preferred_until); |
| 157 |
static inline int ndisc_option_add_prefix( |
| 158 |
Set **options, |
| 159 |
size_t offset, |
| 160 |
uint8_t flags, |
| 161 |
uint8_t prefixlen, |
| 162 |
const struct in6_addr *address, |
| 163 |
usec_t valid_lifetime, |
| 164 |
usec_t preferred_lifetime) { |
| 165 |
return ndisc_option_add_prefix_internal(options, offset, flags, prefixlen, address, |
| 166 |
valid_lifetime, preferred_lifetime, |
| 167 |
USEC_INFINITY, USEC_INFINITY); |
| 168 |
} |
| 169 |
static inline int ndisc_option_set_prefix( |
| 170 |
Set **options, |
| 171 |
uint8_t flags, |
| 172 |
uint8_t prefixlen, |
| 173 |
const struct in6_addr *address, |
| 174 |
usec_t valid_lifetime, |
| 175 |
usec_t preferred_lifetime, |
| 176 |
usec_t valid_until, |
| 177 |
usec_t preferred_until) { |
| 178 |
return ndisc_option_add_prefix_internal(options, 0, flags, prefixlen, address, |
| 179 |
valid_lifetime, preferred_lifetime, |
| 180 |
valid_until, preferred_until); |
| 181 |
} |
| 182 |
int ndisc_option_add_redirected_header( |
| 183 |
Set **options, |
| 184 |
size_t offset, |
| 185 |
const struct ip6_hdr *hdr); |
| 186 |
int ndisc_option_add_mtu( |
| 187 |
Set **options, |
| 188 |
size_t offset, |
| 189 |
uint32_t mtu); |
| 190 |
static inline int ndisc_option_set_mtu( |
| 191 |
Set **options, |
| 192 |
uint32_t mtu) { |
| 193 |
return ndisc_option_add_mtu(options, 0, mtu); |
| 194 |
} |
| 195 |
int ndisc_option_add_home_agent_internal( |
| 196 |
Set **options, |
| 197 |
size_t offset, |
| 198 |
uint16_t preference, |
| 199 |
usec_t lifetime, |
| 200 |
usec_t valid_until); |
| 201 |
static inline int ndisc_option_add_home_agent( |
| 202 |
Set **options, |
| 203 |
size_t offset, |
| 204 |
uint16_t preference, |
| 205 |
usec_t lifetime) { |
| 206 |
return ndisc_option_add_home_agent_internal(options, offset, preference, lifetime, USEC_INFINITY); |
| 207 |
} |
| 208 |
static inline int ndisc_option_set_home_agent( |
| 209 |
Set **options, |
| 210 |
uint16_t preference, |
| 211 |
usec_t lifetime, |
| 212 |
usec_t valid_until) { |
| 213 |
return ndisc_option_add_home_agent_internal(options, 0, preference, lifetime, valid_until); |
| 214 |
} |
| 215 |
int ndisc_option_add_route_internal( |
| 216 |
Set **options, |
| 217 |
size_t offset, |
| 218 |
uint8_t preference, |
| 219 |
uint8_t prefixlen, |
| 220 |
const struct in6_addr *prefix, |
| 221 |
usec_t lifetime, |
| 222 |
usec_t valid_until); |
| 223 |
static inline int ndisc_option_add_route( |
| 224 |
Set **options, |
| 225 |
size_t offset, |
| 226 |
uint8_t preference, |
| 227 |
uint8_t prefixlen, |
| 228 |
const struct in6_addr *prefix, |
| 229 |
usec_t lifetime) { |
| 230 |
return ndisc_option_add_route_internal(options, offset, preference, prefixlen, prefix, lifetime, USEC_INFINITY); |
| 231 |
} |
| 232 |
static inline int ndisc_option_set_route( |
| 233 |
Set **options, |
| 234 |
uint8_t preference, |
| 235 |
uint8_t prefixlen, |
| 236 |
const struct in6_addr *prefix, |
| 237 |
usec_t lifetime, |
| 238 |
usec_t valid_until) { |
| 239 |
return ndisc_option_add_route_internal(options, 0, preference, prefixlen, prefix, lifetime, valid_until); |
| 240 |
} |
| 241 |
int ndisc_option_add_rdnss_internal( |
| 242 |
Set **options, |
| 243 |
size_t offset, |
| 244 |
size_t n_addresses, |
| 245 |
const struct in6_addr *addresses, |
| 246 |
usec_t lifetime, |
| 247 |
usec_t valid_until); |
| 248 |
static inline int ndisc_option_add_rdnss( |
| 249 |
Set **options, |
| 250 |
size_t offset, |
| 251 |
size_t n_addresses, |
| 252 |
const struct in6_addr *addresses, |
| 253 |
usec_t lifetime) { |
| 254 |
return ndisc_option_add_rdnss_internal(options, offset, n_addresses, addresses, lifetime, USEC_INFINITY); |
| 255 |
} |
| 256 |
static inline int ndisc_option_set_rdnss( |
| 257 |
Set **options, |
| 258 |
size_t n_addresses, |
| 259 |
const struct in6_addr *addresses, |
| 260 |
usec_t lifetime, |
| 261 |
usec_t valid_until) { |
| 262 |
return ndisc_option_add_rdnss_internal(options, 0, n_addresses, addresses, lifetime, valid_until); |
| 263 |
} |
| 264 |
int ndisc_option_add_flags_extension( |
| 265 |
Set **options, |
| 266 |
size_t offset, |
| 267 |
uint64_t flags); |
| 268 |
int ndisc_option_add_dnssl_internal( |
| 269 |
Set **options, |
| 270 |
size_t offset, |
| 271 |
char * const *domains, |
| 272 |
usec_t lifetime, |
| 273 |
usec_t valid_until); |
| 274 |
static inline int ndisc_option_add_dnssl( |
| 275 |
Set **options, |
| 276 |
size_t offset, |
| 277 |
char * const *domains, |
| 278 |
usec_t lifetime) { |
| 279 |
return ndisc_option_add_dnssl_internal(options, offset, domains, lifetime, USEC_INFINITY); |
| 280 |
} |
| 281 |
static inline int ndisc_option_set_dnssl( |
| 282 |
Set **options, |
| 283 |
char * const *domains, |
| 284 |
usec_t lifetime, |
| 285 |
usec_t valid_until) { |
| 286 |
return ndisc_option_add_dnssl_internal(options, 0, domains, lifetime, valid_until); |
| 287 |
} |
| 288 |
int ndisc_option_add_captive_portal( |
| 289 |
Set **options, |
| 290 |
size_t offset, |
| 291 |
const char *portal); |
| 292 |
static inline int ndisc_option_set_captive_portal( |
| 293 |
Set **options, |
| 294 |
const char *portal) { |
| 295 |
return ndisc_option_add_captive_portal(options, 0, portal); |
| 296 |
} |
| 297 |
int ndisc_option_add_prefix64_internal( |
| 298 |
Set **options, |
| 299 |
size_t offset, |
| 300 |
uint8_t prefixlen, |
| 301 |
const struct in6_addr *prefix, |
| 302 |
usec_t lifetime, |
| 303 |
usec_t valid_until); |
| 304 |
static inline int ndisc_option_add_prefix64( |
| 305 |
Set **options, |
| 306 |
size_t offset, |
| 307 |
uint8_t prefixlen, |
| 308 |
const struct in6_addr *prefix, |
| 309 |
usec_t lifetime) { |
| 310 |
return ndisc_option_add_prefix64_internal(options, offset, prefixlen, prefix, lifetime, USEC_INFINITY); |
| 311 |
} |
| 312 |
static inline int ndisc_option_set_prefix64( |
| 313 |
Set **options, |
| 314 |
uint8_t prefixlen, |
| 315 |
const struct in6_addr *prefix, |
| 316 |
usec_t lifetime, |
| 317 |
usec_t valid_until) { |
| 318 |
return ndisc_option_add_prefix64_internal(options, 0, prefixlen, prefix, lifetime, valid_until); |
| 319 |
} |
| 320 |
|
| 321 |
int ndisc_option_add_encrypted_dns_internal( |
| 322 |
Set **options, |
| 323 |
size_t offset, |
| 324 |
sd_dns_resolver *res, |
| 325 |
usec_t lifetime, |
| 326 |
usec_t valid_until); |
| 327 |
static inline int ndisc_option_add_encrypted_dns( |
| 328 |
Set **options, |
| 329 |
size_t offset, |
| 330 |
sd_dns_resolver *res, |
| 331 |
usec_t lifetime) { |
| 332 |
return ndisc_option_add_encrypted_dns_internal(options, offset, res, lifetime, USEC_INFINITY); |
| 333 |
} |
| 334 |
static inline int ndisc_option_set_encrypted_dns( |
| 335 |
Set **options, |
| 336 |
size_t offset, |
| 337 |
sd_dns_resolver *res, |
| 338 |
usec_t lifetime, |
| 339 |
usec_t valid_until) { |
| 340 |
return ndisc_option_add_encrypted_dns_internal(options, 0, res, lifetime, valid_until); |
| 341 |
} |
| 342 |
|
| 343 |
int ndisc_send(int fd, const struct in6_addr *dst, const struct icmp6_hdr *hdr, Set *options, usec_t timestamp); |