1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <linux/nl80211.h>
5 
6 #include "sd-bus.h"
7 #include "sd-device.h"
8 #include "sd-lldp-tx.h"
9 
10 #include "bridge.h"
11 #include "condition.h"
12 #include "conf-parser.h"
13 #include "hashmap.h"
14 #include "ipoib.h"
15 #include "net-condition.h"
16 #include "netdev.h"
17 #include "networkd-bridge-vlan.h"
18 #include "networkd-dhcp-common.h"
19 #include "networkd-dhcp4.h"
20 #include "networkd-dhcp6.h"
21 #include "networkd-ipv6ll.h"
22 #include "networkd-lldp-rx.h"
23 #include "networkd-ndisc.h"
24 #include "networkd-radv.h"
25 #include "networkd-sysctl.h"
26 #include "networkd-util.h"
27 #include "ordered-set.h"
28 #include "resolve-util.h"
29 #include "socket-netlink.h"
30 
31 typedef enum KeepConfiguration {
32         KEEP_CONFIGURATION_NO            = 0,
33         KEEP_CONFIGURATION_DHCP_ON_START = 1 << 0,
34         KEEP_CONFIGURATION_DHCP_ON_STOP  = 1 << 1,
35         KEEP_CONFIGURATION_DHCP          = KEEP_CONFIGURATION_DHCP_ON_START | KEEP_CONFIGURATION_DHCP_ON_STOP,
36         KEEP_CONFIGURATION_STATIC        = 1 << 2,
37         KEEP_CONFIGURATION_YES           = KEEP_CONFIGURATION_DHCP | KEEP_CONFIGURATION_STATIC,
38         _KEEP_CONFIGURATION_MAX,
39         _KEEP_CONFIGURATION_INVALID = -EINVAL,
40 } KeepConfiguration;
41 
42 typedef enum ActivationPolicy {
43         ACTIVATION_POLICY_UP,
44         ACTIVATION_POLICY_ALWAYS_UP,
45         ACTIVATION_POLICY_MANUAL,
46         ACTIVATION_POLICY_ALWAYS_DOWN,
47         ACTIVATION_POLICY_DOWN,
48         ACTIVATION_POLICY_BOUND,
49         _ACTIVATION_POLICY_MAX,
50         _ACTIVATION_POLICY_INVALID = -EINVAL,
51 } ActivationPolicy;
52 
53 typedef struct Manager Manager;
54 
55 typedef struct NetworkDHCPServerEmitAddress {
56         bool emit;
57         struct in_addr *addresses;
58         size_t n_addresses;
59 } NetworkDHCPServerEmitAddress;
60 
61 struct Network {
62         Manager *manager;
63 
64         unsigned n_ref;
65 
66         char *name;
67         char *filename;
68         Hashmap *stats_by_path;
69         char *description;
70 
71         /* [Match] section */
72         NetMatch match;
73         LIST_HEAD(Condition, conditions);
74 
75         /* Master or stacked netdevs */
76         bool keep_master;
77         NetDev *batadv;
78         NetDev *bridge;
79         NetDev *bond;
80         NetDev *vrf;
81         NetDev *xfrm;
82         Hashmap *stacked_netdevs;
83         char *batadv_name;
84         char *bridge_name;
85         char *bond_name;
86         char *vrf_name;
87         Hashmap *stacked_netdev_names;
88 
89         /* [Link] section */
90         struct hw_addr_data hw_addr;
91         uint32_t mtu;
92         int32_t group;
93         int arp;
94         int multicast;
95         int allmulticast;
96         int promiscuous;
97         bool unmanaged;
98         int required_for_online; /* Is this network required to be considered online? */
99         LinkOperationalStateRange required_operstate_for_online;
100         AddressFamily required_family_for_online;
101         ActivationPolicy activation_policy;
102 
103         /* misc settings */
104         bool configure_without_carrier;
105         bool ignore_carrier_loss_set;
106         usec_t ignore_carrier_loss_usec; /* timespan */
107         KeepConfiguration keep_configuration;
108         char **bind_carrier;
109         bool default_route_on_device;
110         AddressFamily ip_masquerade;
111 
112         /* DHCP Client Support */
113         AddressFamily dhcp;
114         DHCPClientIdentifier dhcp_client_identifier;
115         DUID dhcp_duid;
116         uint32_t dhcp_iaid;
117         bool dhcp_iaid_set;
118         char *dhcp_vendor_class_identifier;
119         char *dhcp_mudurl;
120         char **dhcp_user_class;
121         char *dhcp_hostname;
122         char *dhcp_label;
123         uint64_t dhcp_max_attempts;
124         uint32_t dhcp_route_metric;
125         bool dhcp_route_metric_set;
126         uint32_t dhcp_route_table;
127         bool dhcp_route_table_set;
128         uint32_t dhcp_fallback_lease_lifetime;
129         uint32_t dhcp_route_mtu;
130         uint16_t dhcp_client_port;
131         int dhcp_critical;
132         int dhcp_ip_service_type;
133         bool dhcp_anonymize;
134         bool dhcp_send_hostname;
135         int dhcp_broadcast;
136         bool dhcp_use_dns;
137         bool dhcp_use_dns_set;
138         bool dhcp_routes_to_dns;
139         bool dhcp_use_ntp;
140         bool dhcp_use_ntp_set;
141         bool dhcp_routes_to_ntp;
142         bool dhcp_use_sip;
143         bool dhcp_use_mtu;
144         bool dhcp_use_routes;
145         int dhcp_use_gateway;
146         bool dhcp_use_timezone;
147         bool dhcp_use_hostname;
148         bool dhcp_use_6rd;
149         bool dhcp_send_release;
150         bool dhcp_send_decline;
151         DHCPUseDomains dhcp_use_domains;
152         bool dhcp_use_domains_set;
153         Set *dhcp_deny_listed_ip;
154         Set *dhcp_allow_listed_ip;
155         Set *dhcp_request_options;
156         OrderedHashmap *dhcp_client_send_options;
157         OrderedHashmap *dhcp_client_send_vendor_options;
158 
159         /* DHCPv6 Client support */
160         bool dhcp6_use_address;
161         bool dhcp6_use_pd_prefix;
162         bool dhcp6_use_dns;
163         bool dhcp6_use_dns_set;
164         bool dhcp6_use_hostname;
165         bool dhcp6_use_ntp;
166         bool dhcp6_use_ntp_set;
167         DHCPUseDomains dhcp6_use_domains;
168         bool dhcp6_use_domains_set;
169         uint32_t dhcp6_iaid;
170         bool dhcp6_iaid_set;
171         bool dhcp6_iaid_set_explicitly;
172         DUID dhcp6_duid;
173         uint8_t dhcp6_pd_prefix_length;
174         struct in6_addr dhcp6_pd_prefix_hint;
175         char *dhcp6_mudurl;
176         char **dhcp6_user_class;
177         char **dhcp6_vendor_class;
178         DHCP6ClientStartMode dhcp6_client_start_mode;
179         OrderedHashmap *dhcp6_client_send_options;
180         OrderedHashmap *dhcp6_client_send_vendor_options;
181         Set *dhcp6_request_options;
182 
183         /* DHCP Server Support */
184         bool dhcp_server;
185         bool dhcp_server_bind_to_interface;
186         unsigned char dhcp_server_address_prefixlen;
187         struct in_addr dhcp_server_address;
188         int dhcp_server_uplink_index;
189         char *dhcp_server_uplink_name;
190         struct in_addr dhcp_server_relay_target;
191         char *dhcp_server_relay_agent_circuit_id;
192         char *dhcp_server_relay_agent_remote_id;
193         NetworkDHCPServerEmitAddress dhcp_server_emit[_SD_DHCP_LEASE_SERVER_TYPE_MAX];
194         bool dhcp_server_emit_router;
195         struct in_addr dhcp_server_router;
196         bool dhcp_server_emit_timezone;
197         char *dhcp_server_timezone;
198         usec_t dhcp_server_default_lease_time_usec, dhcp_server_max_lease_time_usec;
199         uint32_t dhcp_server_pool_offset;
200         uint32_t dhcp_server_pool_size;
201         OrderedHashmap *dhcp_server_send_options;
202         OrderedHashmap *dhcp_server_send_vendor_options;
203         struct in_addr dhcp_server_boot_server_address;
204         char *dhcp_server_boot_server_name;
205         char *dhcp_server_boot_filename;
206 
207         /* link-local addressing support */
208         AddressFamily link_local;
209         IPv6LinkLocalAddressGenMode ipv6ll_address_gen_mode;
210         struct in6_addr ipv6ll_stable_secret;
211         bool ipv4ll_route;
212 
213         /* IPv6 RA support */
214         RADVPrefixDelegation router_prefix_delegation;
215         usec_t router_lifetime_usec;
216         uint8_t router_preference;
217         bool router_managed;
218         bool router_other_information;
219         bool router_emit_dns;
220         bool router_emit_domains;
221         usec_t router_dns_lifetime_usec;
222         struct in6_addr *router_dns;
223         unsigned n_router_dns;
224         OrderedSet *router_search_domains;
225         int router_uplink_index;
226         char *router_uplink_name;
227 
228         /* DHCP Prefix Delegation support */
229         int dhcp_pd;
230         bool dhcp_pd_announce;
231         bool dhcp_pd_assign;
232         bool dhcp_pd_manage_temporary_address;
233         int64_t dhcp_pd_subnet_id;
234         uint32_t dhcp_pd_route_metric;
235         Set *dhcp_pd_tokens;
236         int dhcp_pd_uplink_index;
237         char *dhcp_pd_uplink_name;
238 
239         /* Bridge Support */
240         int use_bpdu;
241         int hairpin;
242         int isolated;
243         int fast_leave;
244         int allow_port_to_be_root;
245         int unicast_flood;
246         int multicast_flood;
247         int multicast_to_unicast;
248         int neighbor_suppression;
249         int learning;
250         int bridge_proxy_arp;
251         int bridge_proxy_arp_wifi;
252         uint32_t cost;
253         uint16_t priority;
254         MulticastRouter multicast_router;
255 
256         /* Bridge VLAN */
257         bool use_br_vlan;
258         uint16_t pvid;
259         uint32_t br_vid_bitmap[BRIDGE_VLAN_BITMAP_LEN];
260         uint32_t br_untagged_bitmap[BRIDGE_VLAN_BITMAP_LEN];
261 
262         /* CAN support */
263         uint32_t can_bitrate;
264         unsigned can_sample_point;
265         nsec_t can_time_quanta_ns;
266         uint32_t can_propagation_segment;
267         uint32_t can_phase_buffer_segment_1;
268         uint32_t can_phase_buffer_segment_2;
269         uint32_t can_sync_jump_width;
270         uint32_t can_data_bitrate;
271         unsigned can_data_sample_point;
272         nsec_t can_data_time_quanta_ns;
273         uint32_t can_data_propagation_segment;
274         uint32_t can_data_phase_buffer_segment_1;
275         uint32_t can_data_phase_buffer_segment_2;
276         uint32_t can_data_sync_jump_width;
277         usec_t can_restart_us;
278         uint32_t can_control_mode_mask;
279         uint32_t can_control_mode_flags;
280         uint16_t can_termination;
281         bool can_termination_set;
282 
283         /* IPoIB support */
284         IPoIBMode ipoib_mode;
285         int ipoib_umcast;
286 
287         /* sysctl settings */
288         AddressFamily ip_forward;
289         int ipv4_accept_local;
290         int ipv4_route_localnet;
291         int ipv6_dad_transmits;
292         int ipv6_hop_limit;
293         int proxy_arp;
294         uint32_t ipv6_mtu;
295         IPv6PrivacyExtensions ipv6_privacy_extensions;
296         int ipv6_proxy_ndp;
297         Set *ipv6_proxy_ndp_addresses;
298 
299         /* IPv6 accept RA */
300         int ipv6_accept_ra;
301         bool ipv6_accept_ra_use_dns;
302         bool ipv6_accept_ra_use_gateway;
303         bool ipv6_accept_ra_use_route_prefix;
304         bool ipv6_accept_ra_use_autonomous_prefix;
305         bool ipv6_accept_ra_use_onlink_prefix;
306         bool ipv6_accept_ra_use_mtu;
307         bool active_slave;
308         bool primary_slave;
309         DHCPUseDomains ipv6_accept_ra_use_domains;
310         IPv6AcceptRAStartDHCP6Client ipv6_accept_ra_start_dhcp6_client;
311         uint32_t ipv6_accept_ra_route_table;
312         bool ipv6_accept_ra_route_table_set;
313         uint32_t ipv6_accept_ra_route_metric;
314         bool ipv6_accept_ra_route_metric_set;
315         Set *ndisc_deny_listed_router;
316         Set *ndisc_allow_listed_router;
317         Set *ndisc_deny_listed_prefix;
318         Set *ndisc_allow_listed_prefix;
319         Set *ndisc_deny_listed_route_prefix;
320         Set *ndisc_allow_listed_route_prefix;
321         Set *ndisc_tokens;
322 
323         /* LLDP support */
324         LLDPMode lldp_mode; /* LLDP reception */
325         sd_lldp_multicast_mode_t lldp_multicast_mode; /* LLDP transmission */
326         char *lldp_mudurl;  /* LLDP MUD URL */
327 
328         OrderedHashmap *addresses_by_section;
329         Hashmap *routes_by_section;
330         Hashmap *nexthops_by_section;
331         Hashmap *bridge_fdb_entries_by_section;
332         Hashmap *bridge_mdb_entries_by_section;
333         Hashmap *neighbors_by_section;
334         Hashmap *address_labels_by_section;
335         Hashmap *prefixes_by_section;
336         Hashmap *route_prefixes_by_section;
337         Hashmap *rules_by_section;
338         Hashmap *dhcp_static_leases_by_section;
339         Hashmap *qdiscs_by_section;
340         Hashmap *tclasses_by_section;
341         OrderedHashmap *sr_iov_by_section;
342 
343         /* All kinds of DNS configuration */
344         struct in_addr_full **dns;
345         unsigned n_dns;
346         OrderedSet *search_domains, *route_domains;
347         int dns_default_route;
348         ResolveSupport llmnr;
349         ResolveSupport mdns;
350         DnssecMode dnssec_mode;
351         DnsOverTlsMode dns_over_tls_mode;
352         Set *dnssec_negative_trust_anchors;
353 
354         /* NTP */
355         char **ntp;
356 };
357 
358 Network *network_ref(Network *network);
359 Network *network_unref(Network *network);
360 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_unref);
361 
362 int network_load(Manager *manager, OrderedHashmap **networks);
363 int network_reload(Manager *manager);
364 int network_load_one(Manager *manager, OrderedHashmap **networks, const char *filename);
365 int network_verify(Network *network);
366 
367 int manager_build_dhcp_pd_subnet_ids(Manager *manager);
368 
369 int network_get_by_name(Manager *manager, const char *name, Network **ret);
370 void network_apply_anonymize_if_set(Network *network);
371 
372 bool network_has_static_ipv6_configurations(Network *network);
373 
374 CONFIG_PARSER_PROTOTYPE(config_parse_stacked_netdev);
375 CONFIG_PARSER_PROTOTYPE(config_parse_tunnel);
376 CONFIG_PARSER_PROTOTYPE(config_parse_domains);
377 CONFIG_PARSER_PROTOTYPE(config_parse_dns);
378 CONFIG_PARSER_PROTOTYPE(config_parse_timezone);
379 CONFIG_PARSER_PROTOTYPE(config_parse_dnssec_negative_trust_anchors);
380 CONFIG_PARSER_PROTOTYPE(config_parse_ntp);
381 CONFIG_PARSER_PROTOTYPE(config_parse_required_for_online);
382 CONFIG_PARSER_PROTOTYPE(config_parse_required_family_for_online);
383 CONFIG_PARSER_PROTOTYPE(config_parse_keep_configuration);
384 CONFIG_PARSER_PROTOTYPE(config_parse_activation_policy);
385 CONFIG_PARSER_PROTOTYPE(config_parse_link_group);
386 CONFIG_PARSER_PROTOTYPE(config_parse_ignore_carrier_loss);
387 
388 const struct ConfigPerfItem* network_network_gperf_lookup(const char *key, GPERF_LEN_TYPE length);
389 
390 const char* keep_configuration_to_string(KeepConfiguration i) _const_;
391 KeepConfiguration keep_configuration_from_string(const char *s) _pure_;
392 
393 const char* activation_policy_to_string(ActivationPolicy i) _const_;
394 ActivationPolicy activation_policy_from_string(const char *s) _pure_;
395