1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "conf-parser.h"
9 #include "firewall-util.h"
10 #include "hash-funcs.h"
11 #include "in-addr-util.h"
12 #include "network-util.h"
13 #include "networkd-link.h"
14 #include "networkd-util.h"
15 #include "time-util.h"
17 typedef struct Address Address
;
18 typedef struct Manager Manager
;
19 typedef struct Network Network
;
20 typedef struct Request Request
;
21 typedef int (*address_ready_callback_t
)(Address
*address
);
22 typedef int (*address_netlink_handler_t
)(
24 sd_netlink_message
*m
,
32 ConfigSection
*section
;
33 NetworkConfigSource source
;
34 NetworkConfigState state
;
35 union in_addr_union provider
; /* DHCP server or router address */
40 unsigned char prefixlen
;
43 uint32_t route_metric
; /* route metric for prefix route */
44 char *label
, *netlabel
;
47 struct in_addr broadcast
;
49 union in_addr_union in_addr
;
50 union in_addr_union in_addr_peer
;
52 /* These are absolute points in time, and NOT timespans/durations.
53 * Must be specified with clock_boottime_or_monotonic(). */
54 usec_t lifetime_valid_usec
;
55 usec_t lifetime_preferred_usec
;
58 bool ip_masquerade_done
:1;
59 bool requested_as_null
:1;
60 bool used_by_dhcp_server
:1;
62 /* duplicate_address_detection is only used by static or IPv4 dynamic addresses.
63 * To control DAD for IPv6 dynamic addresses, set IFA_F_NODAD to flags. */
64 AddressFamily duplicate_address_detection
;
66 /* Called when address become ready */
67 address_ready_callback_t callback
;
69 NFTSetContext nft_set_context
;
72 const char* format_lifetime(char *buf
, size_t l
, usec_t lifetime_usec
) _warn_unused_result_
;
73 /* Note: the lifetime of the compound literal is the immediately surrounding block,
74 * see C11 ยง6.5.2.5, and
75 * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
76 #define FORMAT_LIFETIME(lifetime) \
77 format_lifetime((char[FORMAT_TIMESPAN_MAX+STRLEN("for ")]){}, FORMAT_TIMESPAN_MAX+STRLEN("for "), lifetime)
79 int address_flags_to_string_alloc(uint32_t flags
, int family
, char **ret
);
81 void link_get_address_states(
83 LinkAddressState
*ret_ipv4
,
84 LinkAddressState
*ret_ipv6
,
85 LinkAddressState
*ret_all
);
87 extern const struct hash_ops address_hash_ops
;
89 bool address_can_update(const Address
*existing
, const Address
*requesting
);
91 Address
* address_ref(Address
*address
);
92 Address
* address_unref(Address
*address
);
94 int address_new(Address
**ret
);
95 int address_new_static(Network
*network
, const char *filename
, unsigned section_line
, Address
**ret
);
96 int address_get(Link
*link
, const Address
*in
, Address
**ret
);
97 int address_get_harder(Link
*link
, const Address
*in
, Address
**ret
);
98 int address_configure_handler_internal(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
, const char *error_msg
);
99 int address_remove(Address
*address
, Link
*link
);
100 int address_remove_and_cancel(Address
*address
, Link
*link
);
101 int address_dup(const Address
*src
, Address
**ret
);
102 bool address_is_ready(const Address
*a
);
103 bool link_check_addresses_ready(Link
*link
, NetworkConfigSource source
);
105 DEFINE_SECTION_CLEANUP_FUNCTIONS(Address
, address_unref
);
107 int link_drop_static_addresses(Link
*link
);
108 int link_drop_foreign_addresses(Link
*link
);
109 int link_drop_ipv6ll_addresses(Link
*link
);
110 void link_foreignize_addresses(Link
*link
);
111 bool link_address_is_dynamic(const Link
*link
, const Address
*address
);
112 int link_get_address(Link
*link
, int family
, const union in_addr_union
*address
, unsigned char prefixlen
, Address
**ret
);
113 static inline int link_get_ipv6_address(Link
*link
, const struct in6_addr
*address
, unsigned char prefixlen
, Address
**ret
) {
115 return link_get_address(link
, AF_INET6
, &(union in_addr_union
) { .in6
= *address
}, prefixlen
, ret
);
117 static inline int link_get_ipv4_address(Link
*link
, const struct in_addr
*address
, unsigned char prefixlen
, Address
**ret
) {
119 return link_get_address(link
, AF_INET
, &(union in_addr_union
) { .in
= *address
}, prefixlen
, ret
);
121 int manager_get_address(Manager
*manager
, int family
, const union in_addr_union
*address
, unsigned char prefixlen
, Address
**ret
);
122 bool manager_has_address(Manager
*manager
, int family
, const union in_addr_union
*address
);
124 int link_request_address(
126 const Address
*address
,
127 unsigned *message_counter
,
128 address_netlink_handler_t netlink_handler
,
130 int link_request_static_address(Link
*link
, const Address
*address
);
131 int link_request_static_addresses(Link
*link
);
133 int manager_rtnl_process_address(sd_netlink
*nl
, sd_netlink_message
*message
, Manager
*m
);
135 int address_section_verify(Address
*address
);
136 int network_drop_invalid_addresses(Network
*network
);
138 DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(Address
, address
);
140 void link_mark_addresses(Link
*link
, NetworkConfigSource source
);
142 CONFIG_PARSER_PROTOTYPE(config_parse_address
);
143 CONFIG_PARSER_PROTOTYPE(config_parse_broadcast
);
144 CONFIG_PARSER_PROTOTYPE(config_parse_label
);
145 CONFIG_PARSER_PROTOTYPE(config_parse_lifetime
);
146 CONFIG_PARSER_PROTOTYPE(config_parse_address_flags
);
147 CONFIG_PARSER_PROTOTYPE(config_parse_address_scope
);
148 CONFIG_PARSER_PROTOTYPE(config_parse_address_route_metric
);
149 CONFIG_PARSER_PROTOTYPE(config_parse_duplicate_address_detection
);
150 CONFIG_PARSER_PROTOTYPE(config_parse_address_netlabel
);
151 CONFIG_PARSER_PROTOTYPE(config_parse_address_ip_nft_set
);