chiark / gitweb /
7d291e5ea34244dacad7fc5d334d08f4deb9e9b5
[elogind.git] / src / network / networkd.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Tom Gundersen <teg@jklm.no>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #pragma once
23
24 #include <arpa/inet.h>
25
26 #include "sd-event.h"
27 #include "sd-rtnl.h"
28 #include "sd-bus.h"
29 #include "sd-dhcp-client.h"
30 #include "sd-dhcp-server.h"
31 #include "sd-ipv4ll.h"
32 #include "sd-icmp6-nd.h"
33 #include "sd-dhcp6-client.h"
34 #include "udev.h"
35
36 #include "rtnl-util.h"
37 #include "hashmap.h"
38 #include "list.h"
39 #include "set.h"
40 #include "condition-util.h"
41 #include "in-addr-util.h"
42
43 #define CACHE_INFO_INFINITY_LIFE_TIME 0xFFFFFFFFU
44 #define DHCP_ROUTE_METRIC 1024
45 #define IPV4LL_ROUTE_METRIC 2048
46
47 typedef struct NetDev NetDev;
48 typedef struct Network Network;
49 typedef struct Link Link;
50 typedef struct Address Address;
51 typedef struct Route Route;
52 typedef struct Manager Manager;
53 typedef struct AddressPool AddressPool;
54
55 typedef enum DHCPSupport {
56         DHCP_SUPPORT_NONE,
57         DHCP_SUPPORT_BOTH,
58         DHCP_SUPPORT_V4,
59         DHCP_SUPPORT_V6,
60         _DHCP_SUPPORT_MAX,
61         _DHCP_SUPPORT_INVALID = -1,
62 } DHCPSupport;
63
64 struct Network {
65         Manager *manager;
66
67         char *filename;
68
69         struct ether_addr *match_mac;
70         char *match_path;
71         char *match_driver;
72         char *match_type;
73         char *match_name;
74         char *dhcp_vendor_class_identifier;
75
76         Condition *match_host;
77         Condition *match_virt;
78         Condition *match_kernel;
79         Condition *match_arch;
80
81         char *description;
82         NetDev *bridge;
83         NetDev *bond;
84         NetDev *tunnel;
85         Hashmap *vlans;
86         Hashmap *macvlans;
87         Hashmap *vxlans;
88         DHCPSupport dhcp;
89         bool dhcp_dns;
90         bool dhcp_ntp;
91         bool dhcp_mtu;
92         bool dhcp_hostname;
93         bool dhcp_domainname;
94         bool dhcp_sendhost;
95         bool dhcp_broadcast;
96         bool dhcp_critical;
97         bool dhcp_routes;
98         bool ipv4ll;
99
100         bool dhcp_server;
101
102         LIST_HEAD(Address, static_addresses);
103         LIST_HEAD(Route, static_routes);
104
105         Hashmap *addresses_by_section;
106         Hashmap *routes_by_section;
107
108         LIST_HEAD(Address, dns);
109         LIST_HEAD(Address, ntp);
110
111         LIST_FIELDS(Network, networks);
112 };
113
114 struct Address {
115         Network *network;
116         uint64_t section;
117
118         int family;
119         unsigned char prefixlen;
120         unsigned char scope;
121         char *label;
122
123         struct in_addr broadcast;
124         struct ifa_cacheinfo cinfo;
125
126         union in_addr_union in_addr;
127         union in_addr_union in_addr_peer;
128
129         LIST_FIELDS(Address, addresses);
130 };
131
132 struct Route {
133         Network *network;
134         uint64_t section;
135
136         int family;
137         unsigned char dst_prefixlen;
138         unsigned char scope;
139         uint32_t metrics;
140         unsigned char protocol;  /* RTPROT_* */
141
142         union in_addr_union in_addr;
143         union in_addr_union dst_addr;
144
145         LIST_FIELDS(Route, routes);
146 };
147
148 typedef enum LinkState {
149         LINK_STATE_INITIALIZING,
150         LINK_STATE_ENSLAVING,
151         LINK_STATE_SETTING_ADDRESSES,
152         LINK_STATE_SETTING_ROUTES,
153         LINK_STATE_CONFIGURED,
154         LINK_STATE_UNMANAGED,
155         LINK_STATE_FAILED,
156         LINK_STATE_LINGER,
157         _LINK_STATE_MAX,
158         _LINK_STATE_INVALID = -1
159 } LinkState;
160
161 typedef enum LinkOperationalState {
162         LINK_OPERSTATE_UNKNOWN,
163         LINK_OPERSTATE_DORMANT,
164         LINK_OPERSTATE_CARRIER,
165         LINK_OPERSTATE_DEGRADED,
166         LINK_OPERSTATE_ROUTABLE,
167         _LINK_OPERSTATE_MAX,
168         _LINK_OPERSTATE_INVALID = -1
169 } LinkOperationalState;
170
171 struct Link {
172         Manager *manager;
173
174         int n_ref;
175
176         uint64_t ifindex;
177         char *ifname;
178         char *state_file;
179         struct ether_addr mac;
180         struct udev_device *udev_device;
181
182         unsigned flags;
183         uint8_t kernel_operstate;
184
185         Network *network;
186
187         LinkState state;
188         LinkOperationalState operstate;
189
190         unsigned addr_messages;
191         unsigned route_messages;
192         unsigned enslaving;
193
194         LIST_HEAD(Address, addresses);
195
196         sd_dhcp_client *dhcp_client;
197         sd_dhcp_lease *dhcp_lease;
198         char *lease_file;
199         uint16_t original_mtu;
200         sd_ipv4ll *ipv4ll;
201
202         LIST_HEAD(Address, pool_addresses);
203
204         sd_dhcp_server *dhcp_server;
205
206         sd_icmp6_nd *icmp6_router_discovery;
207         sd_dhcp6_client *dhcp6_client;
208 };
209
210 struct AddressPool {
211         Manager *manager;
212
213         int family;
214         unsigned prefixlen;
215
216         union in_addr_union in_addr;
217
218         LIST_FIELDS(AddressPool, address_pools);
219 };
220
221 struct Manager {
222         sd_rtnl *rtnl;
223         sd_event *event;
224         sd_bus *bus;
225         struct udev *udev;
226         struct udev_monitor *udev_monitor;
227         sd_event_source *udev_event_source;
228         sd_event_source *sigterm_event_source;
229         sd_event_source *sigint_event_source;
230
231         char *state_file;
232
233         Hashmap *links;
234         Hashmap *netdevs;
235         LIST_HEAD(Network, networks);
236         LIST_HEAD(AddressPool, address_pools);
237
238         usec_t network_dirs_ts_usec;
239 };
240
241 extern const char* const network_dirs[];
242
243 /* Manager */
244
245 int manager_new(Manager **ret);
246 void manager_free(Manager *m);
247
248 int manager_load_config(Manager *m);
249 bool manager_should_reload(Manager *m);
250
251 int manager_rtnl_enumerate_links(Manager *m);
252
253 int manager_rtnl_listen(Manager *m);
254 int manager_udev_listen(Manager *m);
255 int manager_bus_listen(Manager *m);
256
257 int manager_save(Manager *m);
258
259 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found);
260
261 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
262 #define _cleanup_manager_free_ _cleanup_(manager_freep)
263
264 /* Network */
265
266 int network_load(Manager *manager);
267
268 void network_free(Network *network);
269
270 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
271 #define _cleanup_network_free_ _cleanup_(network_freep)
272
273 int network_get(Manager *manager, struct udev_device *device,
274                 const char *ifname, const struct ether_addr *mac,
275                 Network **ret);
276 int network_apply(Manager *manager, Network *network, Link *link);
277
278 int config_parse_netdev(const char *unit, const char *filename, unsigned line,
279                         const char *section, unsigned section_line, const char *lvalue,
280                         int ltype, const char *rvalue, void *data, void *userdata);
281
282 int config_parse_tunnel(const char *unit,
283                         const char *filename,
284                         unsigned line,
285                         const char *section,
286                         unsigned section_line,
287                         const char *lvalue,
288                         int ltype,
289                         const char *rvalue,
290                         void *data,
291                         void *userdata);
292
293 int config_parse_tunnel_address(const char *unit,
294                                 const char *filename,
295                                 unsigned line,
296                                 const char *section,
297                                 unsigned section_line,
298                                 const char *lvalue,
299                                 int ltype,
300                                 const char *rvalue,
301                                 void *data,
302                                 void *userdata);
303
304 /* gperf */
305 const struct ConfigPerfItem* network_network_gperf_lookup(const char *key, unsigned length);
306
307 /* Route */
308 int route_new_static(Network *network, unsigned section, Route **ret);
309 int route_new_dynamic(Route **ret, unsigned char rtm_protocol);
310 void route_free(Route *route);
311 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
312 int route_drop(Route *route, Link *link, sd_rtnl_message_handler_t callback);
313
314
315 DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
316 #define _cleanup_route_free_ _cleanup_(route_freep)
317
318 int config_parse_gateway(const char *unit, const char *filename, unsigned line,
319                          const char *section, unsigned section_line, const char *lvalue,
320                          int ltype, const char *rvalue, void *data, void *userdata);
321
322 int config_parse_destination(const char *unit, const char *filename, unsigned line,
323                              const char *section, unsigned section_line, const char *lvalue,
324                              int ltype, const char *rvalue, void *data, void *userdata);
325
326 int config_parse_route_priority(const char *unit, const char *filename, unsigned line,
327                                 const char *section, unsigned section_line, const char *lvalue,
328                                 int ltype, const char *rvalue, void *data, void *userdata);
329 /* Address */
330 int address_new_static(Network *network, unsigned section, Address **ret);
331 int address_new_dynamic(Address **ret);
332 void address_free(Address *address);
333 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
334 int address_update(Address *address, Link *link, sd_rtnl_message_handler_t callback);
335 int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callback);
336 bool address_equal(Address *a1, Address *a2);
337
338 DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
339 #define _cleanup_address_free_ _cleanup_(address_freep)
340
341 int config_parse_dns(const char *unit, const char *filename, unsigned line,
342                      const char *section, unsigned section_line, const char *lvalue,
343                      int ltype, const char *rvalue, void *data, void *userdata);
344
345 int config_parse_address(const char *unit, const char *filename, unsigned line,
346                          const char *section, unsigned section_line, const char *lvalue,
347                          int ltype, const char *rvalue, void *data, void *userdata);
348
349 int config_parse_broadcast(const char *unit, const char *filename, unsigned line,
350                            const char *section, unsigned section_line, const char *lvalue,
351                            int ltype, const char *rvalue, void *data, void *userdata);
352
353 int config_parse_label(const char *unit, const char *filename, unsigned line,
354                        const char *section, unsigned section_line, const char *lvalue,
355                        int ltype, const char *rvalue, void *data, void *userdata);
356
357 /* Link */
358
359 Link *link_unref(Link *link);
360 Link *link_ref(Link *link);
361 int link_get(Manager *m, int ifindex, Link **ret);
362 int link_add(Manager *manager, sd_rtnl_message *message, Link **ret);
363 void link_drop(Link *link);
364
365 int link_update(Link *link, sd_rtnl_message *message);
366 int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata);
367
368 int link_initialized(Link *link, struct udev_device *device);
369
370 int link_save(Link *link);
371
372 bool link_has_carrier(unsigned flags, uint8_t operstate);
373
374 const char* link_state_to_string(LinkState s) _const_;
375 LinkState link_state_from_string(const char *s) _pure_;
376
377 const char* link_operstate_to_string(LinkOperationalState s) _const_;
378 LinkOperationalState link_operstate_from_string(const char *s) _pure_;
379
380 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_unref);
381 #define _cleanup_link_unref_ _cleanup_(link_unrefp)
382
383 /* DHCP support */
384
385 const char* dhcp_support_to_string(DHCPSupport i) _const_;
386 DHCPSupport dhcp_support_from_string(const char *s) _pure_;
387
388 int config_parse_dhcp(const char *unit, const char *filename, unsigned line,
389                       const char *section, unsigned section_line, const char *lvalue,
390                       int ltype, const char *rvalue, void *data, void *userdata);
391
392 /* Address Pool */
393
394 int address_pool_new(Manager *m, AddressPool **ret, int family, const union in_addr_union *u, unsigned prefixlen);
395 int address_pool_new_from_string(Manager *m, AddressPool **ret, int family, const char *p, unsigned prefixlen);
396 void address_pool_free(AddressPool *p);
397
398 int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found);
399
400 /* Macros which append INTERFACE= to the message */
401
402 #define log_full_link(level, link, fmt, ...) log_meta_object(level, __FILE__, __LINE__, __func__, "INTERFACE=", link->ifname, "%-*s: " fmt, IFNAMSIZ, link->ifname, ##__VA_ARGS__)
403 #define log_debug_link(link, ...)       log_full_link(LOG_DEBUG, link, ##__VA_ARGS__)
404 #define log_info_link(link, ...)        log_full_link(LOG_INFO, link, ##__VA_ARGS__)
405 #define log_notice_link(link, ...)      log_full_link(LOG_NOTICE, link, ##__VA_ARGS__)
406 #define log_warning_link(link, ...)     log_full_link(LOG_WARNING, link, ##__VA_ARGS__)
407 #define log_error_link(link, ...)       log_full_link(LOG_ERR, link, ##__VA_ARGS__)
408
409 #define log_struct_link(level, link, ...) log_struct(level, "INTERFACE=%s", link->ifname, __VA_ARGS__)
410
411 #define ADDRESS_FMT_VAL(address)            \
412         (address).s_addr & 0xFF,            \
413         ((address).s_addr >> 8) & 0xFF,     \
414         ((address).s_addr >> 16) & 0xFF,    \
415         (address).s_addr >> 24