]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-address.h
docs/RANDOM_SEEDS: update NetBSD link
[thirdparty/systemd.git] / src / network / networkd-address.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <inttypes.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7
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"
16
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)(
23 sd_netlink *rtnl,
24 sd_netlink_message *m,
25 Request *req,
26 Link *link,
27 Address *address);
28
29 struct Address {
30 Link *link;
31 Network *network;
32 ConfigSection *section;
33 NetworkConfigSource source;
34 NetworkConfigState state;
35 union in_addr_union provider; /* DHCP server or router address */
36
37 unsigned n_ref;
38
39 int family;
40 unsigned char prefixlen;
41 unsigned char scope;
42 uint32_t flags;
43 uint32_t route_metric; /* route metric for prefix route */
44 char *label, *netlabel;
45
46 int set_broadcast;
47 struct in_addr broadcast;
48
49 union in_addr_union in_addr;
50 union in_addr_union in_addr_peer;
51
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;
56
57 bool scope_set:1;
58 bool ip_masquerade_done:1;
59 bool requested_as_null:1;
60 bool used_by_dhcp_server:1;
61
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;
65
66 /* Called when address become ready */
67 address_ready_callback_t callback;
68
69 NFTSetContext nft_set_context;
70 };
71
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)
78
79 int address_flags_to_string_alloc(uint32_t flags, int family, char **ret);
80
81 void link_get_address_states(
82 Link *link,
83 LinkAddressState *ret_ipv4,
84 LinkAddressState *ret_ipv6,
85 LinkAddressState *ret_all);
86
87 extern const struct hash_ops address_hash_ops;
88
89 bool address_can_update(const Address *existing, const Address *requesting);
90
91 Address* address_ref(Address *address);
92 Address* address_unref(Address *address);
93
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);
104
105 DEFINE_SECTION_CLEANUP_FUNCTIONS(Address, address_unref);
106
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) {
114 assert(address);
115 return link_get_address(link, AF_INET6, &(union in_addr_union) { .in6 = *address }, prefixlen, ret);
116 }
117 static inline int link_get_ipv4_address(Link *link, const struct in_addr *address, unsigned char prefixlen, Address **ret) {
118 assert(address);
119 return link_get_address(link, AF_INET, &(union in_addr_union) { .in = *address }, prefixlen, ret);
120 }
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);
123
124 int link_request_address(
125 Link *link,
126 const Address *address,
127 unsigned *message_counter,
128 address_netlink_handler_t netlink_handler,
129 Request **ret);
130 int link_request_static_address(Link *link, const Address *address);
131 int link_request_static_addresses(Link *link);
132
133 int manager_rtnl_process_address(sd_netlink *nl, sd_netlink_message *message, Manager *m);
134
135 int address_section_verify(Address *address);
136 int network_drop_invalid_addresses(Network *network);
137
138 DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(Address, address);
139
140 void link_mark_addresses(Link *link, NetworkConfigSource source);
141
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);