]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-link.h
networkd: Fix race condition in [RoutingPolicyRule] handling (#7615)
[thirdparty/systemd.git] / src / network / networkd-link.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2013 Tom Gundersen <teg@jklm.no>
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <endian.h>
24
25 #include "sd-bus.h"
26 #include "sd-dhcp-client.h"
27 #include "sd-dhcp-server.h"
28 #include "sd-dhcp6-client.h"
29 #include "sd-ipv4ll.h"
30 #include "sd-lldp.h"
31 #include "sd-ndisc.h"
32 #include "sd-radv.h"
33 #include "sd-netlink.h"
34
35 #include "list.h"
36 #include "set.h"
37
38 typedef enum LinkState {
39 LINK_STATE_PENDING,
40 LINK_STATE_ENSLAVING,
41 LINK_STATE_SETTING_ADDRESSES,
42 LINK_STATE_SETTING_ROUTES,
43 LINK_STATE_CONFIGURED,
44 LINK_STATE_UNMANAGED,
45 LINK_STATE_FAILED,
46 LINK_STATE_LINGER,
47 _LINK_STATE_MAX,
48 _LINK_STATE_INVALID = -1
49 } LinkState;
50
51 typedef enum LinkOperationalState {
52 LINK_OPERSTATE_OFF,
53 LINK_OPERSTATE_NO_CARRIER,
54 LINK_OPERSTATE_DORMANT,
55 LINK_OPERSTATE_CARRIER,
56 LINK_OPERSTATE_DEGRADED,
57 LINK_OPERSTATE_ROUTABLE,
58 _LINK_OPERSTATE_MAX,
59 _LINK_OPERSTATE_INVALID = -1
60 } LinkOperationalState;
61
62 typedef struct Manager Manager;
63 typedef struct Network Network;
64 typedef struct Address Address;
65
66 typedef struct Link {
67 Manager *manager;
68
69 int n_ref;
70
71 int ifindex;
72 char *ifname;
73 char *kind;
74 unsigned short iftype;
75 char *state_file;
76 struct ether_addr mac;
77 struct in6_addr ipv6ll_address;
78 uint32_t mtu;
79 struct udev_device *udev_device;
80
81 unsigned flags;
82 uint8_t kernel_operstate;
83
84 Network *network;
85
86 LinkState state;
87 LinkOperationalState operstate;
88
89 unsigned address_messages;
90 unsigned address_label_messages;
91 unsigned route_messages;
92 unsigned routing_policy_rule_messages;
93 unsigned routing_policy_rule_remove_messages;
94 unsigned enslaving;
95
96 Set *addresses;
97 Set *addresses_foreign;
98 Set *routes;
99 Set *routes_foreign;
100
101 sd_dhcp_client *dhcp_client;
102 sd_dhcp_lease *dhcp_lease;
103 char *lease_file;
104 uint16_t original_mtu;
105 unsigned dhcp4_messages;
106 bool dhcp4_configured;
107 bool dhcp6_configured;
108
109 unsigned ndisc_messages;
110 bool ndisc_configured;
111
112 sd_ipv4ll *ipv4ll;
113 bool ipv4ll_address:1;
114 bool ipv4ll_route:1;
115
116 bool static_routes_configured;
117 bool routing_policy_rules_configured;
118 bool setting_mtu;
119
120 LIST_HEAD(Address, pool_addresses);
121
122 sd_dhcp_server *dhcp_server;
123
124 sd_ndisc *ndisc;
125 Set *ndisc_rdnss;
126 Set *ndisc_dnssl;
127
128 sd_radv *radv;
129
130 sd_dhcp6_client *dhcp6_client;
131 bool rtnl_extended_attrs;
132
133 /* This is about LLDP reception */
134 sd_lldp *lldp;
135 char *lldp_file;
136
137 /* This is about LLDP transmission */
138 unsigned lldp_tx_fast; /* The LLDP txFast counter (See 802.1ab-2009, section 9.2.5.18) */
139 sd_event_source *lldp_emit_event_source;
140
141 Hashmap *bound_by_links;
142 Hashmap *bound_to_links;
143 } Link;
144
145 Link *link_unref(Link *link);
146 Link *link_ref(Link *link);
147 int link_get(Manager *m, int ifindex, Link **ret);
148 int link_add(Manager *manager, sd_netlink_message *message, Link **ret);
149 void link_drop(Link *link);
150
151 int link_up(Link *link);
152 int link_down(Link *link);
153
154 int link_address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata);
155 int link_route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata);
156
157 void link_enter_failed(Link *link);
158 int link_initialized(Link *link, struct udev_device *device);
159
160 void link_check_ready(Link *link);
161
162 void link_update_operstate(Link *link);
163 int link_update(Link *link, sd_netlink_message *message);
164
165 void link_dirty(Link *link);
166 void link_clean(Link *link);
167 int link_save(Link *link);
168
169 int link_carrier_reset(Link *link);
170 bool link_has_carrier(Link *link);
171
172 int link_ipv6ll_gained(Link *link, const struct in6_addr *address);
173
174 int link_set_mtu(Link *link, uint32_t mtu);
175
176 int ipv4ll_configure(Link *link);
177 int dhcp4_configure(Link *link);
178 int dhcp4_set_promote_secondaries(Link *link);
179 int dhcp6_configure(Link *link);
180 int dhcp6_request_address(Link *link, int ir);
181
182 const char* link_state_to_string(LinkState s) _const_;
183 LinkState link_state_from_string(const char *s) _pure_;
184
185 const char* link_operstate_to_string(LinkOperationalState s) _const_;
186 LinkOperationalState link_operstate_from_string(const char *s) _pure_;
187
188 extern const sd_bus_vtable link_vtable[];
189
190 int link_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error);
191 int link_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error);
192 int link_send_changed(Link *link, const char *property, ...) _sentinel_;
193
194 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_unref);
195 #define _cleanup_link_unref_ _cleanup_(link_unrefp)
196
197 /* Macros which append INTERFACE= to the message */
198
199 #define log_link_full(link, level, error, ...) \
200 ({ \
201 const Link *_l = (link); \
202 _l ? log_object_internal(level, error, __FILE__, __LINE__, __func__, "INTERFACE=", _l->ifname, NULL, NULL, ##__VA_ARGS__) : \
203 log_internal(level, error, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
204 }) \
205
206 #define log_link_debug(link, ...) log_link_full(link, LOG_DEBUG, 0, ##__VA_ARGS__)
207 #define log_link_info(link, ...) log_link_full(link, LOG_INFO, 0, ##__VA_ARGS__)
208 #define log_link_notice(link, ...) log_link_full(link, LOG_NOTICE, 0, ##__VA_ARGS__)
209 #define log_link_warning(link, ...) log_link_full(link, LOG_WARNING, 0, ##__VA_ARGS__)
210 #define log_link_error(link, ...) log_link_full(link, LOG_ERR, 0, ##__VA_ARGS__)
211
212 #define log_link_debug_errno(link, error, ...) log_link_full(link, LOG_DEBUG, error, ##__VA_ARGS__)
213 #define log_link_info_errno(link, error, ...) log_link_full(link, LOG_INFO, error, ##__VA_ARGS__)
214 #define log_link_notice_errno(link, error, ...) log_link_full(link, LOG_NOTICE, error, ##__VA_ARGS__)
215 #define log_link_warning_errno(link, error, ...) log_link_full(link, LOG_WARNING, error, ##__VA_ARGS__)
216 #define log_link_error_errno(link, error, ...) log_link_full(link, LOG_ERR, error, ##__VA_ARGS__)
217
218 #define LOG_LINK_MESSAGE(link, fmt, ...) "MESSAGE=%s: " fmt, (link)->ifname, ##__VA_ARGS__
219 #define LOG_LINK_INTERFACE(link) "INTERFACE=%s", (link)->ifname
220
221 #define ADDRESS_FMT_VAL(address) \
222 be32toh((address).s_addr) >> 24, \
223 (be32toh((address).s_addr) >> 16) & 0xFFu, \
224 (be32toh((address).s_addr) >> 8) & 0xFFu, \
225 be32toh((address).s_addr) & 0xFFu