| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/if_arp.h> |
| 4 |
#include <linux/if_tunnel.h> |
| 5 |
#include <linux/ip6_tunnel.h> |
| 6 |
#include <netinet/in.h> |
| 7 |
|
| 8 |
#include "sd-netlink.h" |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "conf-parser.h" |
| 12 |
#include "hexdecoct.h" |
| 13 |
#include "missing-network.h" |
| 14 |
#include "netdev-util.h" |
| 15 |
#include "netlink-util.h" |
| 16 |
#include "networkd-manager.h" |
| 17 |
#include "parse-util.h" |
| 18 |
#include "siphash24.h" |
| 19 |
#include "string-table.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "tunnel.h" |
| 22 |
|
| 23 |
#define DEFAULT_IPV6_TTL 64 |
| 24 |
#define IP6_FLOWINFO_FLOWLABEL htobe32(0x000FFFFF) |
| 25 |
#define IP6_TNL_F_ALLOW_LOCAL_REMOTE 0x40 |
| 26 |
|
| 27 |
#define HASH_KEY SD_ID128_MAKE(74,c4,de,12,f3,d9,41,34,bb,3d,c1,a4,42,93,50,87) |
| 28 |
|
| 29 |
static const uint8_t tunnel_mode_to_proto[_TUNNEL_MODE_MAX] = { |
| 30 |
[TUNNEL_MODE_ANY] = 0, |
| 31 |
[TUNNEL_MODE_IPIP] = IPPROTO_IPIP, |
| 32 |
[TUNNEL_MODE_IP6IP] = IPPROTO_IPV6, |
| 33 |
[TUNNEL_MODE_IPIP6] = IPPROTO_IPIP, |
| 34 |
[TUNNEL_MODE_IP6IP6] = IPPROTO_IPV6, |
| 35 |
}; |
| 36 |
|
| 37 |
static const char* const tunnel_mode_table[_TUNNEL_MODE_MAX] = { |
| 38 |
[TUNNEL_MODE_ANY] = "any", |
| 39 |
[TUNNEL_MODE_IPIP] = "ipip", |
| 40 |
[TUNNEL_MODE_IP6IP] = "ip6ip", |
| 41 |
[TUNNEL_MODE_IPIP6] = "ipip6", |
| 42 |
[TUNNEL_MODE_IP6IP6] = "ip6ip6", |
| 43 |
}; |
| 44 |
|
| 45 |
DEFINE_STRING_TABLE_LOOKUP(tunnel_mode, TunnelMode); |
| 46 |
DEFINE_CONFIG_PARSE_ENUM(config_parse_tunnel_mode, tunnel_mode, TunnelMode); |
| 47 |
|
| 48 |
static int dhcp4_pd_create_6rd_tunnel_name(Link *link) { |
| 49 |
_cleanup_free_ char *ifname_alloc = NULL; |
| 50 |
uint8_t ipv4masklen, sixrd_prefixlen, *buf, *p; |
| 51 |
struct in_addr ipv4address; |
| 52 |
struct in6_addr sixrd_prefix; |
| 53 |
char ifname[IFNAMSIZ]; |
| 54 |
uint64_t result; |
| 55 |
size_t sz; |
| 56 |
int r; |
| 57 |
|
| 58 |
assert(link); |
| 59 |
assert(link->dhcp_lease); |
| 60 |
|
| 61 |
if (link->dhcp4_6rd_tunnel_name) |
| 62 |
return 0; /* Already set. Do not change even if the 6rd option is changed. */ |
| 63 |
|
| 64 |
r = sd_dhcp_lease_get_address(link->dhcp_lease, &ipv4address); |
| 65 |
if (r < 0) |
| 66 |
return r; |
| 67 |
|
| 68 |
r = sd_dhcp_lease_get_6rd(link->dhcp_lease, &ipv4masklen, &sixrd_prefixlen, &sixrd_prefix, NULL, NULL); |
| 69 |
if (r < 0) |
| 70 |
return r; |
| 71 |
|
| 72 |
sz = sizeof(uint8_t) * 2 + sizeof(struct in6_addr) + sizeof(struct in_addr); |
| 73 |
buf = newa(uint8_t, sz); |
| 74 |
p = buf; |
| 75 |
p = mempcpy(p, &ipv4masklen, sizeof(uint8_t)); |
| 76 |
p = mempcpy(p, &ipv4address, sizeof(struct in_addr)); |
| 77 |
p = mempcpy(p, &sixrd_prefixlen, sizeof(uint8_t)); |
| 78 |
p = mempcpy(p, &sixrd_prefix, sizeof(struct in6_addr)); |
| 79 |
|
| 80 |
result = siphash24(buf, sz, HASH_KEY.bytes); |
| 81 |
memcpy(ifname, "6rd-", STRLEN("6rd-")); |
| 82 |
ifname[STRLEN("6rd-") ] = urlsafe_base64char(result >> 54); |
| 83 |
ifname[STRLEN("6rd-") + 1] = urlsafe_base64char(result >> 48); |
| 84 |
ifname[STRLEN("6rd-") + 2] = urlsafe_base64char(result >> 42); |
| 85 |
ifname[STRLEN("6rd-") + 3] = urlsafe_base64char(result >> 36); |
| 86 |
ifname[STRLEN("6rd-") + 4] = urlsafe_base64char(result >> 30); |
| 87 |
ifname[STRLEN("6rd-") + 5] = urlsafe_base64char(result >> 24); |
| 88 |
ifname[STRLEN("6rd-") + 6] = urlsafe_base64char(result >> 18); |
| 89 |
ifname[STRLEN("6rd-") + 7] = urlsafe_base64char(result >> 12); |
| 90 |
ifname[STRLEN("6rd-") + 8] = urlsafe_base64char(result >> 6); |
| 91 |
ifname[STRLEN("6rd-") + 9] = urlsafe_base64char(result); |
| 92 |
ifname[STRLEN("6rd-") + 10] = '\0'; |
| 93 |
assert_cc(STRLEN("6rd-") + 10 <= IFNAMSIZ); |
| 94 |
|
| 95 |
ifname_alloc = strdup(ifname); |
| 96 |
if (!ifname_alloc) |
| 97 |
return -ENOMEM; |
| 98 |
|
| 99 |
link->dhcp4_6rd_tunnel_name = TAKE_PTR(ifname_alloc); |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
int dhcp4_pd_create_6rd_tunnel(Link *link, link_netlink_message_handler_t callback) { |
| 104 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 105 |
uint8_t ipv4masklen, sixrd_prefixlen; |
| 106 |
struct in_addr ipv4address; |
| 107 |
struct in6_addr sixrd_prefix; |
| 108 |
Link *sit = NULL; |
| 109 |
int r; |
| 110 |
|
| 111 |
assert(link); |
| 112 |
assert(link->manager); |
| 113 |
assert(link->manager->rtnl); |
| 114 |
assert(link->dhcp_lease); |
| 115 |
assert(callback); |
| 116 |
|
| 117 |
r = sd_dhcp_lease_get_address(link->dhcp_lease, &ipv4address); |
| 118 |
if (r < 0) |
| 119 |
return r; |
| 120 |
|
| 121 |
r = sd_dhcp_lease_get_6rd(link->dhcp_lease, &ipv4masklen, &sixrd_prefixlen, &sixrd_prefix, NULL, NULL); |
| 122 |
if (r < 0) |
| 123 |
return r; |
| 124 |
|
| 125 |
r = dhcp4_pd_create_6rd_tunnel_name(link); |
| 126 |
if (r < 0) |
| 127 |
return r; |
| 128 |
|
| 129 |
(void) link_get_by_name(link->manager, link->dhcp4_6rd_tunnel_name, &sit); |
| 130 |
|
| 131 |
r = sd_rtnl_message_new_link(link->manager->rtnl, &m, RTM_NEWLINK, sit ? sit->ifindex : 0); |
| 132 |
if (r < 0) |
| 133 |
return r; |
| 134 |
|
| 135 |
r = sd_netlink_message_append_string(m, IFLA_IFNAME, link->dhcp4_6rd_tunnel_name); |
| 136 |
if (r < 0) |
| 137 |
return r; |
| 138 |
|
| 139 |
r = sd_netlink_message_open_container(m, IFLA_LINKINFO); |
| 140 |
if (r < 0) |
| 141 |
return r; |
| 142 |
|
| 143 |
r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "sit"); |
| 144 |
if (r < 0) |
| 145 |
return r; |
| 146 |
|
| 147 |
r = sd_netlink_message_append_in_addr(m, IFLA_IPTUN_LOCAL, &ipv4address); |
| 148 |
if (r < 0) |
| 149 |
return r; |
| 150 |
|
| 151 |
r = sd_netlink_message_append_u8(m, IFLA_IPTUN_TTL, 64); |
| 152 |
if (r < 0) |
| 153 |
return r; |
| 154 |
|
| 155 |
r = sd_netlink_message_append_in6_addr(m, IFLA_IPTUN_6RD_PREFIX, &sixrd_prefix); |
| 156 |
if (r < 0) |
| 157 |
return r; |
| 158 |
|
| 159 |
r = sd_netlink_message_append_u16(m, IFLA_IPTUN_6RD_PREFIXLEN, sixrd_prefixlen); |
| 160 |
if (r < 0) |
| 161 |
return r; |
| 162 |
|
| 163 |
struct in_addr relay_prefix = ipv4address; |
| 164 |
(void) in4_addr_mask(&relay_prefix, ipv4masklen); |
| 165 |
r = sd_netlink_message_append_u32(m, IFLA_IPTUN_6RD_RELAY_PREFIX, relay_prefix.s_addr); |
| 166 |
if (r < 0) |
| 167 |
return r; |
| 168 |
|
| 169 |
r = sd_netlink_message_append_u16(m, IFLA_IPTUN_6RD_RELAY_PREFIXLEN, ipv4masklen); |
| 170 |
if (r < 0) |
| 171 |
return r; |
| 172 |
|
| 173 |
r = sd_netlink_message_close_container(m); |
| 174 |
if (r < 0) |
| 175 |
return r; |
| 176 |
|
| 177 |
r = sd_netlink_message_close_container(m); |
| 178 |
if (r < 0) |
| 179 |
return r; |
| 180 |
|
| 181 |
r = netlink_call_async(link->manager->rtnl, NULL, m, callback, |
| 182 |
link_netlink_destroy_callback, link); |
| 183 |
if (r < 0) |
| 184 |
return r; |
| 185 |
|
| 186 |
link_ref(link); |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
static int tunnel_get_local_address(Tunnel *t, Link *link, union in_addr_union *ret) { |
| 191 |
assert(t); |
| 192 |
|
| 193 |
if (t->local_type < 0) { |
| 194 |
if (ret) |
| 195 |
*ret = t->local; |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
return link_get_local_address(link, t->local_type, t->family, NULL, ret); |
| 200 |
} |
| 201 |
|
| 202 |
static int netdev_ipip_sit_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
| 203 |
assert(m); |
| 204 |
|
| 205 |
union in_addr_union local; |
| 206 |
Tunnel *t = ASSERT_PTR(netdev)->kind == NETDEV_KIND_IPIP ? IPIP(netdev) : SIT(netdev); |
| 207 |
int r; |
| 208 |
|
| 209 |
if (t->mode >= 0) { |
| 210 |
r = sd_netlink_message_append_u8(m, IFLA_IPTUN_PROTO, tunnel_mode_to_proto[t->mode]); |
| 211 |
if (r < 0) |
| 212 |
return r; |
| 213 |
} |
| 214 |
|
| 215 |
if (t->external) { |
| 216 |
r = sd_netlink_message_append_flag(m, IFLA_IPTUN_COLLECT_METADATA); |
| 217 |
if (r < 0) |
| 218 |
return r; |
| 219 |
|
| 220 |
/* If external mode is enabled, then the following settings should not be appended. */ |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
if (link || t->assign_to_loopback) { |
| 225 |
r = sd_netlink_message_append_u32(m, IFLA_IPTUN_LINK, link ? link->ifindex : LOOPBACK_IFINDEX); |
| 226 |
if (r < 0) |
| 227 |
return r; |
| 228 |
} |
| 229 |
|
| 230 |
r = tunnel_get_local_address(t, link, &local); |
| 231 |
if (r < 0) |
| 232 |
return log_netdev_error_errno(netdev, r, "Could not find local address: %m"); |
| 233 |
|
| 234 |
r = sd_netlink_message_append_in_addr(m, IFLA_IPTUN_LOCAL, &local.in); |
| 235 |
if (r < 0) |
| 236 |
return r; |
| 237 |
|
| 238 |
r = sd_netlink_message_append_in_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in); |
| 239 |
if (r < 0) |
| 240 |
return r; |
| 241 |
|
| 242 |
r = sd_netlink_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl); |
| 243 |
if (r < 0) |
| 244 |
return r; |
| 245 |
|
| 246 |
r = sd_netlink_message_append_u8(m, IFLA_IPTUN_PMTUDISC, t->pmtudisc); |
| 247 |
if (r < 0) |
| 248 |
return r; |
| 249 |
|
| 250 |
if (t->fou_tunnel) { |
| 251 |
r = sd_netlink_message_append_u16(m, IFLA_IPTUN_ENCAP_TYPE, t->fou_encap_type); |
| 252 |
if (r < 0) |
| 253 |
return r; |
| 254 |
|
| 255 |
r = sd_netlink_message_append_u16(m, IFLA_IPTUN_ENCAP_SPORT, htobe16(t->encap_src_port)); |
| 256 |
if (r < 0) |
| 257 |
return r; |
| 258 |
|
| 259 |
r = sd_netlink_message_append_u16(m, IFLA_IPTUN_ENCAP_DPORT, htobe16(t->fou_destination_port)); |
| 260 |
if (r < 0) |
| 261 |
return r; |
| 262 |
} |
| 263 |
|
| 264 |
if (netdev->kind == NETDEV_KIND_SIT) { |
| 265 |
if (t->sixrd_prefixlen > 0) { |
| 266 |
r = sd_netlink_message_append_in6_addr(m, IFLA_IPTUN_6RD_PREFIX, &t->sixrd_prefix); |
| 267 |
if (r < 0) |
| 268 |
return r; |
| 269 |
|
| 270 |
/* u16 is deliberate here, even though we're passing a netmask that can never be |
| 271 |
* >128. The kernel is expecting to receive the prefixlen as a u16. |
| 272 |
*/ |
| 273 |
r = sd_netlink_message_append_u16(m, IFLA_IPTUN_6RD_PREFIXLEN, t->sixrd_prefixlen); |
| 274 |
if (r < 0) |
| 275 |
return r; |
| 276 |
} |
| 277 |
|
| 278 |
if (t->isatap >= 0) { |
| 279 |
uint16_t flags = 0; |
| 280 |
|
| 281 |
SET_FLAG(flags, SIT_ISATAP, t->isatap); |
| 282 |
|
| 283 |
r = sd_netlink_message_append_u16(m, IFLA_IPTUN_FLAGS, flags); |
| 284 |
if (r < 0) |
| 285 |
return r; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
static int netdev_gre_erspan_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
| 293 |
union in_addr_union local; |
| 294 |
uint32_t ikey = 0; |
| 295 |
uint32_t okey = 0; |
| 296 |
uint16_t iflags = 0; |
| 297 |
uint16_t oflags = 0; |
| 298 |
Tunnel *t; |
| 299 |
int r; |
| 300 |
|
| 301 |
assert(netdev); |
| 302 |
assert(m); |
| 303 |
|
| 304 |
switch (netdev->kind) { |
| 305 |
case NETDEV_KIND_GRE: |
| 306 |
t = GRE(netdev); |
| 307 |
break; |
| 308 |
case NETDEV_KIND_ERSPAN: |
| 309 |
t = ERSPAN(netdev); |
| 310 |
break; |
| 311 |
case NETDEV_KIND_GRETAP: |
| 312 |
t = GRETAP(netdev); |
| 313 |
break; |
| 314 |
default: |
| 315 |
assert_not_reached(); |
| 316 |
} |
| 317 |
|
| 318 |
if (t->external) { |
| 319 |
r = sd_netlink_message_append_flag(m, IFLA_GRE_COLLECT_METADATA); |
| 320 |
if (r < 0) |
| 321 |
return r; |
| 322 |
|
| 323 |
/* If external mode is enabled, then the following settings should not be appended. */ |
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
if (link || t->assign_to_loopback) { |
| 328 |
r = sd_netlink_message_append_u32(m, IFLA_GRE_LINK, link ? link->ifindex : LOOPBACK_IFINDEX); |
| 329 |
if (r < 0) |
| 330 |
return r; |
| 331 |
} |
| 332 |
|
| 333 |
if (netdev->kind == NETDEV_KIND_ERSPAN) { |
| 334 |
r = sd_netlink_message_append_u8(m, IFLA_GRE_ERSPAN_VER, t->erspan_version); |
| 335 |
if (r < 0) |
| 336 |
return r; |
| 337 |
|
| 338 |
if (t->erspan_version == 1) { |
| 339 |
r = sd_netlink_message_append_u32(m, IFLA_GRE_ERSPAN_INDEX, t->erspan_index); |
| 340 |
if (r < 0) |
| 341 |
return r; |
| 342 |
|
| 343 |
} else if (t->erspan_version == 2) { |
| 344 |
r = sd_netlink_message_append_u8(m, IFLA_GRE_ERSPAN_DIR, t->erspan_direction); |
| 345 |
if (r < 0) |
| 346 |
return r; |
| 347 |
|
| 348 |
r = sd_netlink_message_append_u16(m, IFLA_GRE_ERSPAN_HWID, t->erspan_hwid); |
| 349 |
if (r < 0) |
| 350 |
return r; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
r = tunnel_get_local_address(t, link, &local); |
| 355 |
if (r < 0) |
| 356 |
return log_netdev_error_errno(netdev, r, "Could not find local address: %m"); |
| 357 |
|
| 358 |
r = sd_netlink_message_append_in_addr(m, IFLA_GRE_LOCAL, &local.in); |
| 359 |
if (r < 0) |
| 360 |
return r; |
| 361 |
|
| 362 |
r = sd_netlink_message_append_in_addr(m, IFLA_GRE_REMOTE, &t->remote.in); |
| 363 |
if (r < 0) |
| 364 |
return r; |
| 365 |
|
| 366 |
r = sd_netlink_message_append_u8(m, IFLA_GRE_TTL, t->ttl); |
| 367 |
if (r < 0) |
| 368 |
return r; |
| 369 |
|
| 370 |
r = sd_netlink_message_append_u8(m, IFLA_GRE_TOS, t->tos); |
| 371 |
if (r < 0) |
| 372 |
return r; |
| 373 |
|
| 374 |
r = sd_netlink_message_append_u8(m, IFLA_GRE_PMTUDISC, t->pmtudisc); |
| 375 |
if (r < 0) |
| 376 |
return r; |
| 377 |
|
| 378 |
r = sd_netlink_message_append_u8(m, IFLA_GRE_IGNORE_DF, t->ignore_df); |
| 379 |
if (r < 0) |
| 380 |
return r; |
| 381 |
|
| 382 |
if (t->key != 0) { |
| 383 |
ikey = okey = htobe32(t->key); |
| 384 |
iflags |= GRE_KEY; |
| 385 |
oflags |= GRE_KEY; |
| 386 |
} |
| 387 |
|
| 388 |
if (t->ikey != 0) { |
| 389 |
ikey = htobe32(t->ikey); |
| 390 |
iflags |= GRE_KEY; |
| 391 |
} |
| 392 |
|
| 393 |
if (t->okey != 0) { |
| 394 |
okey = htobe32(t->okey); |
| 395 |
oflags |= GRE_KEY; |
| 396 |
} |
| 397 |
|
| 398 |
if (t->gre_erspan_sequence > 0) { |
| 399 |
iflags |= GRE_SEQ; |
| 400 |
oflags |= GRE_SEQ; |
| 401 |
} else if (t->gre_erspan_sequence == 0) { |
| 402 |
iflags &= ~GRE_SEQ; |
| 403 |
oflags &= ~GRE_SEQ; |
| 404 |
} |
| 405 |
|
| 406 |
r = sd_netlink_message_append_u32(m, IFLA_GRE_IKEY, ikey); |
| 407 |
if (r < 0) |
| 408 |
return r; |
| 409 |
|
| 410 |
r = sd_netlink_message_append_u32(m, IFLA_GRE_OKEY, okey); |
| 411 |
if (r < 0) |
| 412 |
return r; |
| 413 |
|
| 414 |
r = sd_netlink_message_append_u16(m, IFLA_GRE_IFLAGS, iflags); |
| 415 |
if (r < 0) |
| 416 |
return r; |
| 417 |
|
| 418 |
r = sd_netlink_message_append_u16(m, IFLA_GRE_OFLAGS, oflags); |
| 419 |
if (r < 0) |
| 420 |
return r; |
| 421 |
|
| 422 |
if (t->fou_tunnel) { |
| 423 |
r = sd_netlink_message_append_u16(m, IFLA_GRE_ENCAP_TYPE, t->fou_encap_type); |
| 424 |
if (r < 0) |
| 425 |
return r; |
| 426 |
|
| 427 |
r = sd_netlink_message_append_u16(m, IFLA_GRE_ENCAP_SPORT, htobe16(t->encap_src_port)); |
| 428 |
if (r < 0) |
| 429 |
return r; |
| 430 |
|
| 431 |
r = sd_netlink_message_append_u16(m, IFLA_GRE_ENCAP_DPORT, htobe16(t->fou_destination_port)); |
| 432 |
if (r < 0) |
| 433 |
return r; |
| 434 |
} |
| 435 |
|
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
static int netdev_ip6gre_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
| 440 |
union in_addr_union local; |
| 441 |
uint32_t ikey = 0, okey = 0; |
| 442 |
uint16_t iflags = 0, oflags = 0; |
| 443 |
Tunnel *t; |
| 444 |
int r; |
| 445 |
|
| 446 |
assert(netdev); |
| 447 |
assert(m); |
| 448 |
|
| 449 |
if (netdev->kind == NETDEV_KIND_IP6GRE) |
| 450 |
t = IP6GRE(netdev); |
| 451 |
else |
| 452 |
t = IP6GRETAP(netdev); |
| 453 |
|
| 454 |
if (t->external) { |
| 455 |
r = sd_netlink_message_append_flag(m, IFLA_GRE_COLLECT_METADATA); |
| 456 |
if (r < 0) |
| 457 |
return r; |
| 458 |
|
| 459 |
/* If external mode is enabled, then the following settings should not be appended. */ |
| 460 |
return 0; |
| 461 |
} |
| 462 |
|
| 463 |
if (link || t->assign_to_loopback) { |
| 464 |
r = sd_netlink_message_append_u32(m, IFLA_GRE_LINK, link ? link->ifindex : LOOPBACK_IFINDEX); |
| 465 |
if (r < 0) |
| 466 |
return r; |
| 467 |
} |
| 468 |
|
| 469 |
r = tunnel_get_local_address(t, link, &local); |
| 470 |
if (r < 0) |
| 471 |
return log_netdev_error_errno(netdev, r, "Could not find local address: %m"); |
| 472 |
|
| 473 |
r = sd_netlink_message_append_in6_addr(m, IFLA_GRE_LOCAL, &local.in6); |
| 474 |
if (r < 0) |
| 475 |
return r; |
| 476 |
|
| 477 |
r = sd_netlink_message_append_in6_addr(m, IFLA_GRE_REMOTE, &t->remote.in6); |
| 478 |
if (r < 0) |
| 479 |
return r; |
| 480 |
|
| 481 |
r = sd_netlink_message_append_u8(m, IFLA_GRE_TTL, t->ttl); |
| 482 |
if (r < 0) |
| 483 |
return r; |
| 484 |
|
| 485 |
if (t->ipv6_flowlabel != _NETDEV_IPV6_FLOWLABEL_INVALID) { |
| 486 |
r = sd_netlink_message_append_u32(m, IFLA_GRE_FLOWINFO, t->ipv6_flowlabel); |
| 487 |
if (r < 0) |
| 488 |
return r; |
| 489 |
} |
| 490 |
|
| 491 |
r = sd_netlink_message_append_u32(m, IFLA_GRE_FLAGS, t->flags); |
| 492 |
if (r < 0) |
| 493 |
return r; |
| 494 |
|
| 495 |
if (t->key != 0) { |
| 496 |
ikey = okey = htobe32(t->key); |
| 497 |
iflags |= GRE_KEY; |
| 498 |
oflags |= GRE_KEY; |
| 499 |
} |
| 500 |
|
| 501 |
if (t->ikey != 0) { |
| 502 |
ikey = htobe32(t->ikey); |
| 503 |
iflags |= GRE_KEY; |
| 504 |
} |
| 505 |
|
| 506 |
if (t->okey != 0) { |
| 507 |
okey = htobe32(t->okey); |
| 508 |
oflags |= GRE_KEY; |
| 509 |
} |
| 510 |
|
| 511 |
r = sd_netlink_message_append_u32(m, IFLA_GRE_IKEY, ikey); |
| 512 |
if (r < 0) |
| 513 |
return r; |
| 514 |
|
| 515 |
r = sd_netlink_message_append_u32(m, IFLA_GRE_OKEY, okey); |
| 516 |
if (r < 0) |
| 517 |
return r; |
| 518 |
|
| 519 |
r = sd_netlink_message_append_u16(m, IFLA_GRE_IFLAGS, iflags); |
| 520 |
if (r < 0) |
| 521 |
return r; |
| 522 |
|
| 523 |
r = sd_netlink_message_append_u16(m, IFLA_GRE_OFLAGS, oflags); |
| 524 |
if (r < 0) |
| 525 |
return r; |
| 526 |
|
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
static int netdev_vti_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
| 531 |
assert(netdev); |
| 532 |
assert(m); |
| 533 |
|
| 534 |
union in_addr_union local; |
| 535 |
uint32_t ikey, okey; |
| 536 |
Tunnel *t = netdev->kind == NETDEV_KIND_VTI ? VTI(netdev) : VTI6(netdev); |
| 537 |
int r; |
| 538 |
|
| 539 |
if (link || t->assign_to_loopback) { |
| 540 |
r = sd_netlink_message_append_u32(m, IFLA_VTI_LINK, link ? link->ifindex : LOOPBACK_IFINDEX); |
| 541 |
if (r < 0) |
| 542 |
return r; |
| 543 |
} |
| 544 |
|
| 545 |
if (t->key != 0) |
| 546 |
ikey = okey = htobe32(t->key); |
| 547 |
else { |
| 548 |
ikey = htobe32(t->ikey); |
| 549 |
okey = htobe32(t->okey); |
| 550 |
} |
| 551 |
|
| 552 |
r = sd_netlink_message_append_u32(m, IFLA_VTI_IKEY, ikey); |
| 553 |
if (r < 0) |
| 554 |
return r; |
| 555 |
|
| 556 |
r = sd_netlink_message_append_u32(m, IFLA_VTI_OKEY, okey); |
| 557 |
if (r < 0) |
| 558 |
return r; |
| 559 |
|
| 560 |
r = tunnel_get_local_address(t, link, &local); |
| 561 |
if (r < 0) |
| 562 |
return log_netdev_error_errno(netdev, r, "Could not find local address: %m"); |
| 563 |
|
| 564 |
r = netlink_message_append_in_addr_union(m, IFLA_VTI_LOCAL, t->family, &local); |
| 565 |
if (r < 0) |
| 566 |
return r; |
| 567 |
|
| 568 |
r = netlink_message_append_in_addr_union(m, IFLA_VTI_REMOTE, t->family, &t->remote); |
| 569 |
if (r < 0) |
| 570 |
return r; |
| 571 |
|
| 572 |
return 0; |
| 573 |
} |
| 574 |
|
| 575 |
static int netdev_ip6tnl_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
| 576 |
assert(netdev); |
| 577 |
assert(m); |
| 578 |
|
| 579 |
union in_addr_union local; |
| 580 |
Tunnel *t = IP6TNL(netdev); |
| 581 |
int r; |
| 582 |
|
| 583 |
if (t->mode >= 0) { |
| 584 |
r = sd_netlink_message_append_u8(m, IFLA_IPTUN_PROTO, tunnel_mode_to_proto[t->mode]); |
| 585 |
if (r < 0) |
| 586 |
return r; |
| 587 |
} |
| 588 |
|
| 589 |
if (t->external) { |
| 590 |
r = sd_netlink_message_append_flag(m, IFLA_IPTUN_COLLECT_METADATA); |
| 591 |
if (r < 0) |
| 592 |
return r; |
| 593 |
|
| 594 |
/* If external mode is enabled, then the following settings should not be appended. */ |
| 595 |
return 0; |
| 596 |
} |
| 597 |
|
| 598 |
if (link || t->assign_to_loopback) { |
| 599 |
r = sd_netlink_message_append_u32(m, IFLA_IPTUN_LINK, link ? link->ifindex : LOOPBACK_IFINDEX); |
| 600 |
if (r < 0) |
| 601 |
return r; |
| 602 |
} |
| 603 |
|
| 604 |
r = tunnel_get_local_address(t, link, &local); |
| 605 |
if (r < 0) |
| 606 |
return log_netdev_error_errno(netdev, r, "Could not find local address: %m"); |
| 607 |
|
| 608 |
r = sd_netlink_message_append_in6_addr(m, IFLA_IPTUN_LOCAL, &local.in6); |
| 609 |
if (r < 0) |
| 610 |
return r; |
| 611 |
|
| 612 |
r = sd_netlink_message_append_in6_addr(m, IFLA_IPTUN_REMOTE, &t->remote.in6); |
| 613 |
if (r < 0) |
| 614 |
return r; |
| 615 |
|
| 616 |
r = sd_netlink_message_append_u8(m, IFLA_IPTUN_TTL, t->ttl); |
| 617 |
if (r < 0) |
| 618 |
return r; |
| 619 |
|
| 620 |
if (t->ipv6_flowlabel != _NETDEV_IPV6_FLOWLABEL_INVALID) { |
| 621 |
r = sd_netlink_message_append_u32(m, IFLA_IPTUN_FLOWINFO, t->ipv6_flowlabel); |
| 622 |
if (r < 0) |
| 623 |
return r; |
| 624 |
} |
| 625 |
|
| 626 |
if (t->copy_dscp) |
| 627 |
t->flags |= IP6_TNL_F_RCV_DSCP_COPY; |
| 628 |
|
| 629 |
if (t->allow_localremote >= 0) |
| 630 |
SET_FLAG(t->flags, IP6_TNL_F_ALLOW_LOCAL_REMOTE, t->allow_localremote); |
| 631 |
|
| 632 |
r = sd_netlink_message_append_u32(m, IFLA_IPTUN_FLAGS, t->flags); |
| 633 |
if (r < 0) |
| 634 |
return r; |
| 635 |
|
| 636 |
if (t->encap_limit != 0) { |
| 637 |
r = sd_netlink_message_append_u8(m, IFLA_IPTUN_ENCAP_LIMIT, t->encap_limit); |
| 638 |
if (r < 0) |
| 639 |
return r; |
| 640 |
} |
| 641 |
|
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
static int netdev_tunnel_is_ready_to_create(NetDev *netdev, Link *link) { |
| 646 |
assert(netdev); |
| 647 |
|
| 648 |
Tunnel *t = ASSERT_PTR(TUNNEL(netdev)); |
| 649 |
|
| 650 |
if (t->independent) |
| 651 |
return true; |
| 652 |
|
| 653 |
return tunnel_get_local_address(t, link, NULL) >= 0; |
| 654 |
} |
| 655 |
|
| 656 |
static int netdev_tunnel_verify(NetDev *netdev, const char *filename) { |
| 657 |
assert(netdev); |
| 658 |
assert(filename); |
| 659 |
|
| 660 |
Tunnel *t = ASSERT_PTR(TUNNEL(netdev)); |
| 661 |
|
| 662 |
if (t->external) { |
| 663 |
if (IN_SET(netdev->kind, NETDEV_KIND_VTI, NETDEV_KIND_VTI6)) |
| 664 |
log_netdev_debug(netdev, "vti/vti6 tunnel do not support external mode, ignoring."); |
| 665 |
else { |
| 666 |
/* tunnel with external mode does not require underlying interface. */ |
| 667 |
t->independent = true; |
| 668 |
|
| 669 |
/* tunnel with external mode does not require any settings checked below. */ |
| 670 |
return 0; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
if (IN_SET(netdev->kind, NETDEV_KIND_VTI, NETDEV_KIND_IPIP, NETDEV_KIND_SIT, NETDEV_KIND_GRE, NETDEV_KIND_GRETAP, NETDEV_KIND_ERSPAN)) { |
| 675 |
if (!IN_SET(t->family, AF_UNSPEC, AF_INET)) |
| 676 |
return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 677 |
"%s tunnel without a local/remote IPv4 address configured in %s, ignoring.", |
| 678 |
netdev_kind_to_string(netdev->kind), filename); |
| 679 |
|
| 680 |
t->family = AF_INET; /* For netlink_message_append_in_addr_union(). */ |
| 681 |
} |
| 682 |
|
| 683 |
if (IN_SET(netdev->kind, NETDEV_KIND_VTI6, NETDEV_KIND_IP6TNL, NETDEV_KIND_IP6GRE, NETDEV_KIND_IP6GRETAP)) { |
| 684 |
if (!IN_SET(t->family, AF_UNSPEC, AF_INET6)) |
| 685 |
return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 686 |
"%s tunnel without a local/remote IPv6 address configured in %s, ignoring,", |
| 687 |
netdev_kind_to_string(netdev->kind), filename); |
| 688 |
t->family = AF_INET6; /* For netlink_message_append_in_addr_union(). */ |
| 689 |
} |
| 690 |
|
| 691 |
if (t->fou_tunnel && t->fou_destination_port <= 0) |
| 692 |
return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 693 |
"FooOverUDP missing port configured in %s. Ignoring", filename); |
| 694 |
|
| 695 |
if (t->assign_to_loopback) |
| 696 |
t->independent = true; |
| 697 |
|
| 698 |
if (t->independent && t->local_type >= 0) |
| 699 |
return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 700 |
"The local address cannot be '%s' when Independent= or AssignToLoopback= is enabled, ignoring.", |
| 701 |
strna(netdev_local_address_type_to_string(t->local_type))); |
| 702 |
|
| 703 |
if (t->pmtudisc > 0 && t->ignore_df) |
| 704 |
return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 705 |
"IgnoreDontFragment= cannot be enabled when DiscoverPathMTU= is enabled"); |
| 706 |
if (t->pmtudisc < 0) |
| 707 |
t->pmtudisc = !t->ignore_df; |
| 708 |
|
| 709 |
if (t->mode >= 0) |
| 710 |
switch (netdev->kind) { |
| 711 |
case NETDEV_KIND_IPIP: |
| 712 |
if (!IN_SET(t->mode, TUNNEL_MODE_ANY, TUNNEL_MODE_IPIP)) |
| 713 |
return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 714 |
"Specified unsupported tunnel mode %s, ignoring.", |
| 715 |
tunnel_mode_to_string(t->mode)); |
| 716 |
break; |
| 717 |
case NETDEV_KIND_SIT: |
| 718 |
if (!IN_SET(t->mode, TUNNEL_MODE_ANY, TUNNEL_MODE_IPIP, TUNNEL_MODE_IP6IP)) |
| 719 |
return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 720 |
"Specified unsupported tunnel mode %s, ignoring.", |
| 721 |
tunnel_mode_to_string(t->mode)); |
| 722 |
break; |
| 723 |
case NETDEV_KIND_IP6TNL: |
| 724 |
if (!IN_SET(t->mode, TUNNEL_MODE_ANY, TUNNEL_MODE_IPIP6, TUNNEL_MODE_IP6IP6)) |
| 725 |
return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 726 |
"Specified unsupported tunnel mode %s, ignoring.", |
| 727 |
tunnel_mode_to_string(t->mode)); |
| 728 |
break; |
| 729 |
default: |
| 730 |
return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
| 731 |
"%s tunnel does not support Mode= setting, ignoring: %s", |
| 732 |
netdev_kind_to_string(netdev->kind), |
| 733 |
tunnel_mode_to_string(t->mode)); |
| 734 |
} |
| 735 |
|
| 736 |
return 0; |
| 737 |
} |
| 738 |
|
| 739 |
static bool tunnel_needs_reconfigure(NetDev *netdev, NetDevLocalAddressType type) { |
| 740 |
assert(type >= 0 && type < _NETDEV_LOCAL_ADDRESS_TYPE_MAX); |
| 741 |
|
| 742 |
Tunnel *t = ASSERT_PTR(TUNNEL(netdev)); |
| 743 |
|
| 744 |
return t->local_type == type; |
| 745 |
} |
| 746 |
|
| 747 |
static int unset_local(Tunnel *t) { |
| 748 |
assert(t); |
| 749 |
|
| 750 |
/* Unset the previous assignment. */ |
| 751 |
t->local = IN_ADDR_NULL; |
| 752 |
t->local_type = _NETDEV_LOCAL_ADDRESS_TYPE_INVALID; |
| 753 |
|
| 754 |
/* If the remote address is not specified, also clear the address family. */ |
| 755 |
if (!in_addr_is_set(t->family, &t->remote)) |
| 756 |
t->family = AF_UNSPEC; |
| 757 |
|
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
int config_parse_tunnel_local_address( |
| 762 |
const char *unit, |
| 763 |
const char *filename, |
| 764 |
unsigned line, |
| 765 |
const char *section, |
| 766 |
unsigned section_line, |
| 767 |
const char *lvalue, |
| 768 |
int ltype, |
| 769 |
const char *rvalue, |
| 770 |
void *data, |
| 771 |
void *userdata) { |
| 772 |
|
| 773 |
union in_addr_union buffer = IN_ADDR_NULL; |
| 774 |
NetDevLocalAddressType type; |
| 775 |
Tunnel *t = ASSERT_PTR(userdata); |
| 776 |
int r, f; |
| 777 |
|
| 778 |
assert(filename); |
| 779 |
assert(lvalue); |
| 780 |
assert(rvalue); |
| 781 |
|
| 782 |
if (isempty(rvalue) || streq(rvalue, "any")) |
| 783 |
return unset_local(t); |
| 784 |
|
| 785 |
type = netdev_local_address_type_from_string(rvalue); |
| 786 |
if (IN_SET(type, NETDEV_LOCAL_ADDRESS_IPV4LL, NETDEV_LOCAL_ADDRESS_DHCP4)) |
| 787 |
f = AF_INET; |
| 788 |
else if (IN_SET(type, NETDEV_LOCAL_ADDRESS_IPV6LL, NETDEV_LOCAL_ADDRESS_DHCP6, NETDEV_LOCAL_ADDRESS_SLAAC, NETDEV_LOCAL_ADDRESS_DHCP_PD)) |
| 789 |
f = AF_INET6; |
| 790 |
else { |
| 791 |
type = _NETDEV_LOCAL_ADDRESS_TYPE_INVALID; |
| 792 |
r = in_addr_from_string_auto(rvalue, &f, &buffer); |
| 793 |
if (r < 0) { |
| 794 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 795 |
"Tunnel address \"%s\" invalid, ignoring assignment: %m", rvalue); |
| 796 |
return 0; |
| 797 |
} |
| 798 |
|
| 799 |
if (in_addr_is_null(f, &buffer)) |
| 800 |
return unset_local(t); |
| 801 |
} |
| 802 |
|
| 803 |
if (t->family != AF_UNSPEC && t->family != f) { |
| 804 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 805 |
"Address family does not match the previous assignment, ignoring assignment: %s", rvalue); |
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
t->family = f; |
| 810 |
t->local = buffer; |
| 811 |
t->local_type = type; |
| 812 |
return 0; |
| 813 |
} |
| 814 |
|
| 815 |
static int unset_remote(Tunnel *t) { |
| 816 |
assert(t); |
| 817 |
|
| 818 |
/* Unset the previous assignment. */ |
| 819 |
t->remote = IN_ADDR_NULL; |
| 820 |
|
| 821 |
/* If the local address is not specified, also clear the address family. */ |
| 822 |
if (t->local_type == _NETDEV_LOCAL_ADDRESS_TYPE_INVALID && |
| 823 |
!in_addr_is_set(t->family, &t->local)) |
| 824 |
t->family = AF_UNSPEC; |
| 825 |
|
| 826 |
return 0; |
| 827 |
} |
| 828 |
|
| 829 |
int config_parse_tunnel_remote_address( |
| 830 |
const char *unit, |
| 831 |
const char *filename, |
| 832 |
unsigned line, |
| 833 |
const char *section, |
| 834 |
unsigned section_line, |
| 835 |
const char *lvalue, |
| 836 |
int ltype, |
| 837 |
const char *rvalue, |
| 838 |
void *data, |
| 839 |
void *userdata) { |
| 840 |
|
| 841 |
union in_addr_union buffer; |
| 842 |
Tunnel *t = ASSERT_PTR(userdata); |
| 843 |
int r, f; |
| 844 |
|
| 845 |
assert(filename); |
| 846 |
assert(lvalue); |
| 847 |
assert(rvalue); |
| 848 |
|
| 849 |
if (isempty(rvalue) || streq(rvalue, "any")) |
| 850 |
return unset_remote(t); |
| 851 |
|
| 852 |
r = in_addr_from_string_auto(rvalue, &f, &buffer); |
| 853 |
if (r < 0) { |
| 854 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 855 |
"Tunnel address \"%s\" invalid, ignoring assignment: %m", rvalue); |
| 856 |
return 0; |
| 857 |
} |
| 858 |
|
| 859 |
if (in_addr_is_null(f, &buffer)) |
| 860 |
return unset_remote(t); |
| 861 |
|
| 862 |
if (t->family != AF_UNSPEC && t->family != f) { |
| 863 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 864 |
"Address family does not match the previous assignment, ignoring assignment: %s", rvalue); |
| 865 |
return 0; |
| 866 |
} |
| 867 |
|
| 868 |
t->family = f; |
| 869 |
t->remote = buffer; |
| 870 |
return 0; |
| 871 |
} |
| 872 |
|
| 873 |
int config_parse_tunnel_key( |
| 874 |
const char *unit, |
| 875 |
const char *filename, |
| 876 |
unsigned line, |
| 877 |
const char *section, |
| 878 |
unsigned section_line, |
| 879 |
const char *lvalue, |
| 880 |
int ltype, |
| 881 |
const char *rvalue, |
| 882 |
void *data, |
| 883 |
void *userdata) { |
| 884 |
|
| 885 |
uint32_t *dest = ASSERT_PTR(data), k; |
| 886 |
union in_addr_union buffer; |
| 887 |
int r; |
| 888 |
|
| 889 |
assert(filename); |
| 890 |
assert(rvalue); |
| 891 |
|
| 892 |
r = in_addr_from_string(AF_INET, rvalue, &buffer); |
| 893 |
if (r < 0) { |
| 894 |
r = safe_atou32(rvalue, &k); |
| 895 |
if (r < 0) { |
| 896 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 897 |
"Failed to parse tunnel key ignoring assignment: %s", rvalue); |
| 898 |
return 0; |
| 899 |
} |
| 900 |
} else |
| 901 |
k = be32toh(buffer.in.s_addr); |
| 902 |
|
| 903 |
*dest = k; |
| 904 |
return 0; |
| 905 |
} |
| 906 |
|
| 907 |
int config_parse_ipv6_flowlabel( |
| 908 |
const char *unit, |
| 909 |
const char *filename, |
| 910 |
unsigned line, |
| 911 |
const char *section, |
| 912 |
unsigned section_line, |
| 913 |
const char *lvalue, |
| 914 |
int ltype, |
| 915 |
const char *rvalue, |
| 916 |
void *data, |
| 917 |
void *userdata) { |
| 918 |
|
| 919 |
Tunnel *t = ASSERT_PTR(userdata); |
| 920 |
uint32_t k; |
| 921 |
int r; |
| 922 |
|
| 923 |
assert(filename); |
| 924 |
assert(rvalue); |
| 925 |
|
| 926 |
if (streq(rvalue, "inherit")) { |
| 927 |
t->ipv6_flowlabel = IP6_FLOWINFO_FLOWLABEL; |
| 928 |
t->flags |= IP6_TNL_F_USE_ORIG_FLOWLABEL; |
| 929 |
return 0; |
| 930 |
} |
| 931 |
|
| 932 |
r = config_parse_uint32_bounded( |
| 933 |
unit, filename, line, section, section_line, lvalue, rvalue, |
| 934 |
0, 0xFFFFF, true, |
| 935 |
&k); |
| 936 |
if (r <= 0) |
| 937 |
return r; |
| 938 |
t->ipv6_flowlabel = htobe32(k) & IP6_FLOWINFO_FLOWLABEL; |
| 939 |
t->flags &= ~IP6_TNL_F_USE_ORIG_FLOWLABEL; |
| 940 |
|
| 941 |
return 0; |
| 942 |
} |
| 943 |
|
| 944 |
int config_parse_encap_limit( |
| 945 |
const char *unit, |
| 946 |
const char *filename, |
| 947 |
unsigned line, |
| 948 |
const char *section, |
| 949 |
unsigned section_line, |
| 950 |
const char *lvalue, |
| 951 |
int ltype, |
| 952 |
const char *rvalue, |
| 953 |
void *data, |
| 954 |
void *userdata) { |
| 955 |
|
| 956 |
assert(filename); |
| 957 |
assert(rvalue); |
| 958 |
|
| 959 |
Tunnel *t = ASSERT_PTR(userdata); |
| 960 |
int r; |
| 961 |
|
| 962 |
if (streq(rvalue, "none")) { |
| 963 |
t->encap_limit = 0; |
| 964 |
t->flags |= IP6_TNL_F_IGN_ENCAP_LIMIT; |
| 965 |
return 0; |
| 966 |
} |
| 967 |
|
| 968 |
r = config_parse_uint8_bounded( |
| 969 |
unit, filename, line, section, section_line, lvalue, rvalue, |
| 970 |
0, UINT8_MAX, true, |
| 971 |
&t->encap_limit); |
| 972 |
if (r <= 0) |
| 973 |
return r; |
| 974 |
t->flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT; |
| 975 |
|
| 976 |
return 0; |
| 977 |
} |
| 978 |
|
| 979 |
int config_parse_6rd_prefix( |
| 980 |
const char *unit, |
| 981 |
const char *filename, |
| 982 |
unsigned line, |
| 983 |
const char *section, |
| 984 |
unsigned section_line, |
| 985 |
const char *lvalue, |
| 986 |
int ltype, |
| 987 |
const char *rvalue, |
| 988 |
void *data, |
| 989 |
void *userdata) { |
| 990 |
|
| 991 |
Tunnel *t = userdata; |
| 992 |
union in_addr_union p; |
| 993 |
uint8_t l; |
| 994 |
int r; |
| 995 |
|
| 996 |
assert(filename); |
| 997 |
assert(lvalue); |
| 998 |
assert(rvalue); |
| 999 |
|
| 1000 |
r = in_addr_prefix_from_string(rvalue, AF_INET6, &p, &l); |
| 1001 |
if (r < 0) { |
| 1002 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse 6rd prefix \"%s\", ignoring: %m", rvalue); |
| 1003 |
return 0; |
| 1004 |
} |
| 1005 |
if (l == 0) { |
| 1006 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "6rd prefix length of \"%s\" must be greater than zero, ignoring", rvalue); |
| 1007 |
return 0; |
| 1008 |
} |
| 1009 |
|
| 1010 |
t->sixrd_prefix = p.in6; |
| 1011 |
t->sixrd_prefixlen = l; |
| 1012 |
|
| 1013 |
return 0; |
| 1014 |
} |
| 1015 |
|
| 1016 |
int config_parse_erspan_version( |
| 1017 |
const char *unit, |
| 1018 |
const char *filename, |
| 1019 |
unsigned line, |
| 1020 |
const char *section, |
| 1021 |
unsigned section_line, |
| 1022 |
const char *lvalue, |
| 1023 |
int ltype, |
| 1024 |
const char *rvalue, |
| 1025 |
void *data, |
| 1026 |
void *userdata) { |
| 1027 |
|
| 1028 |
assert(filename); |
| 1029 |
assert(lvalue); |
| 1030 |
assert(rvalue); |
| 1031 |
|
| 1032 |
uint8_t *v = ASSERT_PTR(data); |
| 1033 |
|
| 1034 |
if (isempty(rvalue)) { |
| 1035 |
*v = 1; /* defaults to 1 */ |
| 1036 |
return 0; |
| 1037 |
} |
| 1038 |
|
| 1039 |
return config_parse_uint8_bounded( |
| 1040 |
unit, filename, line, section, section_line, lvalue, rvalue, |
| 1041 |
0, 2, true, |
| 1042 |
v); |
| 1043 |
} |
| 1044 |
|
| 1045 |
int config_parse_erspan_index( |
| 1046 |
const char *unit, |
| 1047 |
const char *filename, |
| 1048 |
unsigned line, |
| 1049 |
const char *section, |
| 1050 |
unsigned section_line, |
| 1051 |
const char *lvalue, |
| 1052 |
int ltype, |
| 1053 |
const char *rvalue, |
| 1054 |
void *data, |
| 1055 |
void *userdata) { |
| 1056 |
|
| 1057 |
assert(filename); |
| 1058 |
assert(lvalue); |
| 1059 |
assert(rvalue); |
| 1060 |
|
| 1061 |
uint32_t *v = ASSERT_PTR(data); |
| 1062 |
|
| 1063 |
if (isempty(rvalue)) { |
| 1064 |
*v = 0; /* defaults to 0 */ |
| 1065 |
return 0; |
| 1066 |
} |
| 1067 |
|
| 1068 |
return config_parse_uint32_bounded( |
| 1069 |
unit, filename, line, section, section_line, lvalue, rvalue, |
| 1070 |
0, 0x100000 - 1, true, |
| 1071 |
v); |
| 1072 |
} |
| 1073 |
|
| 1074 |
int config_parse_erspan_direction( |
| 1075 |
const char *unit, |
| 1076 |
const char *filename, |
| 1077 |
unsigned line, |
| 1078 |
const char *section, |
| 1079 |
unsigned section_line, |
| 1080 |
const char *lvalue, |
| 1081 |
int ltype, |
| 1082 |
const char *rvalue, |
| 1083 |
void *data, |
| 1084 |
void *userdata) { |
| 1085 |
|
| 1086 |
assert(filename); |
| 1087 |
assert(lvalue); |
| 1088 |
assert(rvalue); |
| 1089 |
|
| 1090 |
uint8_t *v = ASSERT_PTR(data); |
| 1091 |
|
| 1092 |
if (isempty(rvalue) || streq(rvalue, "ingress")) |
| 1093 |
*v = 0; /* defaults to ingress */ |
| 1094 |
else if (streq(rvalue, "egress")) |
| 1095 |
*v = 1; |
| 1096 |
else |
| 1097 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1098 |
"Invalid erspan direction \"%s\", which must be \"ingress\" or \"egress\", ignoring.", rvalue); |
| 1099 |
|
| 1100 |
return 0; |
| 1101 |
} |
| 1102 |
|
| 1103 |
int config_parse_erspan_hwid( |
| 1104 |
const char *unit, |
| 1105 |
const char *filename, |
| 1106 |
unsigned line, |
| 1107 |
const char *section, |
| 1108 |
unsigned section_line, |
| 1109 |
const char *lvalue, |
| 1110 |
int ltype, |
| 1111 |
const char *rvalue, |
| 1112 |
void *data, |
| 1113 |
void *userdata) { |
| 1114 |
|
| 1115 |
assert(filename); |
| 1116 |
assert(lvalue); |
| 1117 |
assert(rvalue); |
| 1118 |
|
| 1119 |
uint16_t *v = ASSERT_PTR(data); |
| 1120 |
|
| 1121 |
if (isempty(rvalue)) { |
| 1122 |
*v = 0; /* defaults to 0 */ |
| 1123 |
return 0; |
| 1124 |
} |
| 1125 |
|
| 1126 |
return config_parse_uint16_bounded( |
| 1127 |
unit, filename, line, section, section_line, lvalue, rvalue, |
| 1128 |
0, 63, true, |
| 1129 |
v); |
| 1130 |
} |
| 1131 |
|
| 1132 |
static void netdev_tunnel_init(NetDev *netdev) { |
| 1133 |
Tunnel *t = ASSERT_PTR(TUNNEL(netdev)); |
| 1134 |
|
| 1135 |
t->local_type = _NETDEV_LOCAL_ADDRESS_TYPE_INVALID; |
| 1136 |
t->pmtudisc = -1; |
| 1137 |
t->fou_encap_type = NETDEV_FOO_OVER_UDP_ENCAP_DIRECT; |
| 1138 |
t->isatap = -1; |
| 1139 |
t->gre_erspan_sequence = -1; |
| 1140 |
t->encap_limit = IPV6_DEFAULT_TNL_ENCAP_LIMIT; |
| 1141 |
t->mode = _TUNNEL_MODE_INVALID; |
| 1142 |
t->ipv6_flowlabel = _NETDEV_IPV6_FLOWLABEL_INVALID; |
| 1143 |
t->allow_localremote = -1; |
| 1144 |
t->erspan_version = 1; |
| 1145 |
|
| 1146 |
if (IN_SET(netdev->kind, NETDEV_KIND_IP6GRE, NETDEV_KIND_IP6GRETAP, NETDEV_KIND_IP6TNL)) |
| 1147 |
t->ttl = DEFAULT_IPV6_TTL; |
| 1148 |
} |
| 1149 |
|
| 1150 |
static bool tunnel_can_set_mac(NetDev *netdev, const struct hw_addr_data *hw_addr) { |
| 1151 |
assert(IN_SET(netdev->kind, NETDEV_KIND_GRETAP, NETDEV_KIND_IP6GRETAP, NETDEV_KIND_ERSPAN)); |
| 1152 |
return true; |
| 1153 |
} |
| 1154 |
|
| 1155 |
const NetDevVTable ipip_vtable = { |
| 1156 |
.object_size = sizeof(Tunnel), |
| 1157 |
.init = netdev_tunnel_init, |
| 1158 |
.sections = NETDEV_COMMON_SECTIONS "Tunnel\0", |
| 1159 |
.fill_message_create = netdev_ipip_sit_fill_message_create, |
| 1160 |
.create_type = NETDEV_CREATE_STACKED, |
| 1161 |
.is_ready_to_create = netdev_tunnel_is_ready_to_create, |
| 1162 |
.config_verify = netdev_tunnel_verify, |
| 1163 |
.needs_reconfigure = tunnel_needs_reconfigure, |
| 1164 |
.iftype = ARPHRD_TUNNEL, |
| 1165 |
}; |
| 1166 |
|
| 1167 |
const NetDevVTable sit_vtable = { |
| 1168 |
.object_size = sizeof(Tunnel), |
| 1169 |
.init = netdev_tunnel_init, |
| 1170 |
.sections = NETDEV_COMMON_SECTIONS "Tunnel\0", |
| 1171 |
.fill_message_create = netdev_ipip_sit_fill_message_create, |
| 1172 |
.create_type = NETDEV_CREATE_STACKED, |
| 1173 |
.is_ready_to_create = netdev_tunnel_is_ready_to_create, |
| 1174 |
.config_verify = netdev_tunnel_verify, |
| 1175 |
.needs_reconfigure = tunnel_needs_reconfigure, |
| 1176 |
.iftype = ARPHRD_SIT, |
| 1177 |
}; |
| 1178 |
|
| 1179 |
const NetDevVTable vti_vtable = { |
| 1180 |
.object_size = sizeof(Tunnel), |
| 1181 |
.init = netdev_tunnel_init, |
| 1182 |
.sections = NETDEV_COMMON_SECTIONS "Tunnel\0", |
| 1183 |
.fill_message_create = netdev_vti_fill_message_create, |
| 1184 |
.create_type = NETDEV_CREATE_STACKED, |
| 1185 |
.is_ready_to_create = netdev_tunnel_is_ready_to_create, |
| 1186 |
.config_verify = netdev_tunnel_verify, |
| 1187 |
.needs_reconfigure = tunnel_needs_reconfigure, |
| 1188 |
.iftype = ARPHRD_TUNNEL, |
| 1189 |
}; |
| 1190 |
|
| 1191 |
const NetDevVTable vti6_vtable = { |
| 1192 |
.object_size = sizeof(Tunnel), |
| 1193 |
.init = netdev_tunnel_init, |
| 1194 |
.sections = NETDEV_COMMON_SECTIONS "Tunnel\0", |
| 1195 |
.fill_message_create = netdev_vti_fill_message_create, |
| 1196 |
.create_type = NETDEV_CREATE_STACKED, |
| 1197 |
.is_ready_to_create = netdev_tunnel_is_ready_to_create, |
| 1198 |
.config_verify = netdev_tunnel_verify, |
| 1199 |
.needs_reconfigure = tunnel_needs_reconfigure, |
| 1200 |
.iftype = ARPHRD_TUNNEL6, |
| 1201 |
}; |
| 1202 |
|
| 1203 |
const NetDevVTable gre_vtable = { |
| 1204 |
.object_size = sizeof(Tunnel), |
| 1205 |
.init = netdev_tunnel_init, |
| 1206 |
.sections = NETDEV_COMMON_SECTIONS "Tunnel\0", |
| 1207 |
.fill_message_create = netdev_gre_erspan_fill_message_create, |
| 1208 |
.create_type = NETDEV_CREATE_STACKED, |
| 1209 |
.is_ready_to_create = netdev_tunnel_is_ready_to_create, |
| 1210 |
.config_verify = netdev_tunnel_verify, |
| 1211 |
.needs_reconfigure = tunnel_needs_reconfigure, |
| 1212 |
.iftype = ARPHRD_IPGRE, |
| 1213 |
}; |
| 1214 |
|
| 1215 |
const NetDevVTable gretap_vtable = { |
| 1216 |
.object_size = sizeof(Tunnel), |
| 1217 |
.init = netdev_tunnel_init, |
| 1218 |
.sections = NETDEV_COMMON_SECTIONS "Tunnel\0", |
| 1219 |
.fill_message_create = netdev_gre_erspan_fill_message_create, |
| 1220 |
.create_type = NETDEV_CREATE_STACKED, |
| 1221 |
.is_ready_to_create = netdev_tunnel_is_ready_to_create, |
| 1222 |
.config_verify = netdev_tunnel_verify, |
| 1223 |
.needs_reconfigure = tunnel_needs_reconfigure, |
| 1224 |
.can_set_mac = tunnel_can_set_mac, |
| 1225 |
.iftype = ARPHRD_ETHER, |
| 1226 |
.generate_mac = true, |
| 1227 |
}; |
| 1228 |
|
| 1229 |
const NetDevVTable ip6gre_vtable = { |
| 1230 |
.object_size = sizeof(Tunnel), |
| 1231 |
.init = netdev_tunnel_init, |
| 1232 |
.sections = NETDEV_COMMON_SECTIONS "Tunnel\0", |
| 1233 |
.fill_message_create = netdev_ip6gre_fill_message_create, |
| 1234 |
.create_type = NETDEV_CREATE_STACKED, |
| 1235 |
.is_ready_to_create = netdev_tunnel_is_ready_to_create, |
| 1236 |
.config_verify = netdev_tunnel_verify, |
| 1237 |
.needs_reconfigure = tunnel_needs_reconfigure, |
| 1238 |
.iftype = ARPHRD_IP6GRE, |
| 1239 |
}; |
| 1240 |
|
| 1241 |
const NetDevVTable ip6gretap_vtable = { |
| 1242 |
.object_size = sizeof(Tunnel), |
| 1243 |
.init = netdev_tunnel_init, |
| 1244 |
.sections = NETDEV_COMMON_SECTIONS "Tunnel\0", |
| 1245 |
.fill_message_create = netdev_ip6gre_fill_message_create, |
| 1246 |
.create_type = NETDEV_CREATE_STACKED, |
| 1247 |
.is_ready_to_create = netdev_tunnel_is_ready_to_create, |
| 1248 |
.config_verify = netdev_tunnel_verify, |
| 1249 |
.needs_reconfigure = tunnel_needs_reconfigure, |
| 1250 |
.can_set_mac = tunnel_can_set_mac, |
| 1251 |
.iftype = ARPHRD_ETHER, |
| 1252 |
.generate_mac = true, |
| 1253 |
}; |
| 1254 |
|
| 1255 |
const NetDevVTable ip6tnl_vtable = { |
| 1256 |
.object_size = sizeof(Tunnel), |
| 1257 |
.init = netdev_tunnel_init, |
| 1258 |
.sections = NETDEV_COMMON_SECTIONS "Tunnel\0", |
| 1259 |
.fill_message_create = netdev_ip6tnl_fill_message_create, |
| 1260 |
.create_type = NETDEV_CREATE_STACKED, |
| 1261 |
.is_ready_to_create = netdev_tunnel_is_ready_to_create, |
| 1262 |
.config_verify = netdev_tunnel_verify, |
| 1263 |
.needs_reconfigure = tunnel_needs_reconfigure, |
| 1264 |
.iftype = ARPHRD_TUNNEL6, |
| 1265 |
}; |
| 1266 |
|
| 1267 |
const NetDevVTable erspan_vtable = { |
| 1268 |
.object_size = sizeof(Tunnel), |
| 1269 |
.init = netdev_tunnel_init, |
| 1270 |
.sections = NETDEV_COMMON_SECTIONS "Tunnel\0", |
| 1271 |
.fill_message_create = netdev_gre_erspan_fill_message_create, |
| 1272 |
.create_type = NETDEV_CREATE_STACKED, |
| 1273 |
.is_ready_to_create = netdev_tunnel_is_ready_to_create, |
| 1274 |
.config_verify = netdev_tunnel_verify, |
| 1275 |
.needs_reconfigure = tunnel_needs_reconfigure, |
| 1276 |
.can_set_mac = tunnel_can_set_mac, |
| 1277 |
.iftype = ARPHRD_ETHER, |
| 1278 |
.generate_mac = true, |
| 1279 |
}; |