| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "firewall-util.h" |
| 5 |
#include "in-addr-util.h" |
| 6 |
#include "networkd-forward.h" |
| 7 |
#include "networkd-util.h" |
| 8 |
|
| 9 |
typedef int (*address_ready_callback_t)(Address *address); |
| 10 |
typedef int (*address_netlink_handler_t)( |
| 11 |
sd_netlink *rtnl, |
| 12 |
sd_netlink_message *m, |
| 13 |
Request *req, |
| 14 |
Link *link, |
| 15 |
Address *address); |
| 16 |
|
| 17 |
typedef struct Address { |
| 18 |
Link *link; |
| 19 |
Network *network; |
| 20 |
ConfigSection *section; |
| 21 |
NetworkConfigSource source; |
| 22 |
NetworkConfigState state; |
| 23 |
union in_addr_union provider; /* DHCP server or router address */ |
| 24 |
|
| 25 |
unsigned n_ref; |
| 26 |
|
| 27 |
int family; |
| 28 |
unsigned char prefixlen; |
| 29 |
unsigned char scope; |
| 30 |
uint32_t flags; |
| 31 |
uint32_t route_metric; /* route metric for prefix route */ |
| 32 |
char *label, *netlabel; |
| 33 |
|
| 34 |
int set_broadcast; |
| 35 |
struct in_addr broadcast; |
| 36 |
|
| 37 |
union in_addr_union in_addr; |
| 38 |
union in_addr_union in_addr_peer; |
| 39 |
|
| 40 |
/* These are absolute points in time, and NOT timespans/durations. |
| 41 |
* Must be specified with clock_boottime_or_monotonic(). */ |
| 42 |
usec_t lifetime_valid_usec; |
| 43 |
usec_t lifetime_preferred_usec; |
| 44 |
|
| 45 |
bool scope_set:1; |
| 46 |
bool ip_masquerade_done:1; |
| 47 |
bool requested_as_null:1; |
| 48 |
bool used_by_dhcp_server:1; |
| 49 |
/* If this address is configured by non-DHCPv6 protocol (i.e. statically, NDISC, or any foreign |
| 50 |
* methods), and the DHCPv6 client requests the same address, set this flag. |
| 51 |
* See link_check_addresses_ready() and verify_dhcp6_address(). */ |
| 52 |
bool also_requested_by_dhcp6:1; |
| 53 |
|
| 54 |
/* duplicate_address_detection is only used by static or IPv4 dynamic addresses. |
| 55 |
* To control DAD for IPv6 dynamic addresses, set IFA_F_NODAD to flags. */ |
| 56 |
AddressFamily duplicate_address_detection; |
| 57 |
|
| 58 |
/* Used by address generator. */ |
| 59 |
IPv6Token *token; |
| 60 |
|
| 61 |
/* Called when address become ready */ |
| 62 |
address_ready_callback_t callback; |
| 63 |
|
| 64 |
NFTSetContext nft_set_context; |
| 65 |
} Address; |
| 66 |
|
| 67 |
void log_address_debug(const Address *address, const char *str, const Link *link); |
| 68 |
|
| 69 |
const char* format_lifetime(char *buf, size_t l, usec_t lifetime_usec) _warn_unused_result_; |
| 70 |
/* Note: the lifetime of the compound literal is the immediately surrounding block, |
| 71 |
* see C11 §6.5.2.5, and |
| 72 |
* https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */ |
| 73 |
#define FORMAT_LIFETIME(lifetime) \ |
| 74 |
format_lifetime((char[FORMAT_TIMESPAN_MAX+STRLEN("for ")]){}, FORMAT_TIMESPAN_MAX+STRLEN("for "), lifetime) |
| 75 |
|
| 76 |
int address_flags_to_string_alloc(uint32_t flags, int family, char **ret); |
| 77 |
|
| 78 |
void link_get_address_states( |
| 79 |
Link *link, |
| 80 |
LinkAddressState *ret_ipv4, |
| 81 |
LinkAddressState *ret_ipv6, |
| 82 |
LinkAddressState *ret_all); |
| 83 |
|
| 84 |
void address_hash_func(const Address *a, struct siphash *state); |
| 85 |
int address_compare_func(const Address *a1, const Address *a2); |
| 86 |
extern const struct hash_ops address_hash_ops; |
| 87 |
|
| 88 |
bool address_can_update(const Address *existing, const Address *requesting); |
| 89 |
|
| 90 |
DECLARE_TRIVIAL_REF_UNREF_FUNC(Address, address); |
| 91 |
|
| 92 |
int address_new(Address **ret); |
| 93 |
int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret); |
| 94 |
int address_get(Link *link, const Address *in, Address **ret); |
| 95 |
int address_get_harder(Link *link, const Address *in, Address **ret); |
| 96 |
int address_configure_handler_internal(sd_netlink_message *m, Link *link, Address *address); |
| 97 |
int address_remove(Address *address, Link *link); |
| 98 |
int address_remove_and_cancel(Address *address, Link *link); |
| 99 |
int address_dup(const Address *src, Address **ret); |
| 100 |
bool address_is_ready(const Address *a); |
| 101 |
bool link_check_addresses_ready(Link *link, NetworkConfigSource source); |
| 102 |
|
| 103 |
DEFINE_SECTION_CLEANUP_FUNCTIONS(Address, address_unref); |
| 104 |
|
| 105 |
int link_drop_static_addresses(Link *link); |
| 106 |
int link_drop_unmanaged_addresses(Link *link); |
| 107 |
int link_drop_ipv6ll_addresses(Link *link); |
| 108 |
bool link_address_is_dynamic(const Link *link, const Address *address); |
| 109 |
|
| 110 |
int link_get_address_full( |
| 111 |
Link *link, |
| 112 |
int family, |
| 113 |
const union in_addr_union *address, |
| 114 |
const union in_addr_union *peer, /* optional, can be NULL */ |
| 115 |
unsigned char prefixlen, /* optional, can be 0 */ |
| 116 |
Address **ret); |
| 117 |
static inline int link_get_address(Link *link, int family, const union in_addr_union *address, Address **ret) { |
| 118 |
return link_get_address_full(link, family, address, NULL, 0, ret); |
| 119 |
} |
| 120 |
static inline int link_get_ipv6_address(Link *link, const struct in6_addr *address, Address **ret) { |
| 121 |
assert(address); |
| 122 |
return link_get_address(link, AF_INET6, &(union in_addr_union) { .in6 = *address }, ret); |
| 123 |
} |
| 124 |
int manager_get_address_full( |
| 125 |
Manager *manager, |
| 126 |
int family, |
| 127 |
const union in_addr_union *address, |
| 128 |
const union in_addr_union *peer, |
| 129 |
unsigned char prefixlen, |
| 130 |
Address **ret); |
| 131 |
static inline int manager_get_address(Manager *manager, int family, const union in_addr_union *address, Address **ret) { |
| 132 |
return manager_get_address_full(manager, family, address, NULL, 0, ret); |
| 133 |
} |
| 134 |
|
| 135 |
int link_request_address( |
| 136 |
Link *link, |
| 137 |
const Address *address, |
| 138 |
unsigned *message_counter, |
| 139 |
address_netlink_handler_t netlink_handler, |
| 140 |
Request **ret); |
| 141 |
int link_request_static_address(Link *link, const Address *address); |
| 142 |
int link_request_static_addresses(Link *link); |
| 143 |
|
| 144 |
int manager_rtnl_process_address(sd_netlink *nl, sd_netlink_message *message, Manager *m); |
| 145 |
|
| 146 |
int address_section_verify(Address *address); |
| 147 |
int network_drop_invalid_addresses(Network *network); |
| 148 |
|
| 149 |
DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(Address, address); |
| 150 |
|
| 151 |
void link_mark_addresses(Link *link, NetworkConfigSource source); |
| 152 |
|
| 153 |
typedef enum AddressConfParserType { |
| 154 |
ADDRESS_ADDRESS, |
| 155 |
ADDRESS_PEER, |
| 156 |
ADDRESS_BROADCAST, |
| 157 |
ADDRESS_LABEL, |
| 158 |
ADDRESS_PREFERRED_LIFETIME, |
| 159 |
ADDRESS_HOME_ADDRESS, |
| 160 |
ADDRESS_MANAGE_TEMPORARY_ADDRESS, |
| 161 |
ADDRESS_PREFIX_ROUTE, |
| 162 |
ADDRESS_ADD_PREFIX_ROUTE, |
| 163 |
ADDRESS_AUTO_JOIN, |
| 164 |
ADDRESS_DAD, |
| 165 |
ADDRESS_SCOPE, |
| 166 |
ADDRESS_ROUTE_METRIC, |
| 167 |
ADDRESS_NET_LABEL, |
| 168 |
ADDRESS_NFT_SET, |
| 169 |
_ADDRESS_CONF_PARSER_MAX, |
| 170 |
_ADDRESS_CONF_PARSER_INVALID = -EINVAL, |
| 171 |
} AddressConfParserType; |
| 172 |
|
| 173 |
CONFIG_PARSER_PROTOTYPE(config_parse_address_section); |