chiark / gitweb /
cfe24f5702f0d0328290c27c3d2ee66a9b0f7cb8
[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-ipv4ll.h"
31 #include "udev.h"
32
33 #include "rtnl-util.h"
34 #include "hashmap.h"
35 #include "list.h"
36 #include "set.h"
37 #include "condition-util.h"
38
39 #define CACHE_INFO_INFINITY_LIFE_TIME 0xFFFFFFFFU
40
41 typedef struct NetDev NetDev;
42 typedef struct Network Network;
43 typedef struct Link Link;
44 typedef struct Address Address;
45 typedef struct Route Route;
46 typedef struct Manager Manager;
47
48 typedef struct netdev_enslave_callback netdev_enslave_callback;
49
50 struct netdev_enslave_callback {
51         sd_rtnl_message_handler_t callback;
52         Link *link;
53
54         LIST_FIELDS(netdev_enslave_callback, callbacks);
55 };
56
57 typedef enum MacVlanMode {
58         NETDEV_MACVLAN_MODE_PRIVATE = MACVLAN_MODE_PRIVATE,
59         NETDEV_MACVLAN_MODE_VEPA = MACVLAN_MODE_VEPA,
60         NETDEV_MACVLAN_MODE_BRIDGE = MACVLAN_MODE_BRIDGE,
61         NETDEV_MACVLAN_MODE_PASSTHRU = MACVLAN_MODE_PASSTHRU,
62         _NETDEV_MACVLAN_MODE_MAX,
63         _NETDEV_MACVLAN_MODE_INVALID = -1
64 } MacVlanMode;
65
66 typedef enum NetDevKind {
67         NETDEV_KIND_BRIDGE,
68         NETDEV_KIND_BOND,
69         NETDEV_KIND_VLAN,
70         NETDEV_KIND_MACVLAN,
71         NETDEV_KIND_IPIP,
72         NETDEV_KIND_GRE,
73         NETDEV_KIND_SIT,
74         _NETDEV_KIND_MAX,
75         _NETDEV_KIND_INVALID = -1
76 } NetDevKind;
77
78 typedef enum NetDevState {
79         NETDEV_STATE_FAILED,
80         NETDEV_STATE_CREATING,
81         NETDEV_STATE_READY,
82         NETDEV_STATE_LINGER,
83         _NETDEV_STATE_MAX,
84         _NETDEV_STATE_INVALID = -1,
85 } NetDevState;
86
87 struct NetDev {
88         Manager *manager;
89
90         int n_ref;
91
92         char *filename;
93
94         Condition *match_host;
95         Condition *match_virt;
96         Condition *match_kernel;
97         Condition *match_arch;
98
99         char *description;
100         char *ifname;
101         size_t mtu;
102         NetDevKind kind;
103
104         uint64_t vlanid;
105         int32_t macvlan_mode;
106
107         int ifindex;
108         NetDevState state;
109
110         unsigned tunnel_ttl;
111         unsigned tunnel_tos;
112         struct in_addr tunnel_local;
113         struct in_addr tunnel_remote;
114
115         LIST_HEAD(netdev_enslave_callback, callbacks);
116 };
117
118 struct Network {
119         Manager *manager;
120
121         char *filename;
122
123         struct ether_addr *match_mac;
124         char *match_path;
125         char *match_driver;
126         char *match_type;
127         char *match_name;
128         Condition *match_host;
129         Condition *match_virt;
130         Condition *match_kernel;
131         Condition *match_arch;
132
133         char *description;
134         NetDev *bridge;
135         NetDev *bond;
136         NetDev *tunnel;
137         Hashmap *vlans;
138         Hashmap *macvlans;
139         bool dhcp;
140         bool dhcp_dns;
141         bool dhcp_ntp;
142         bool dhcp_mtu;
143         bool dhcp_hostname;
144         bool dhcp_domainname;
145         bool dhcp_critical;
146         bool ipv4ll;
147
148         LIST_HEAD(Address, static_addresses);
149         LIST_HEAD(Route, static_routes);
150
151         Hashmap *addresses_by_section;
152         Hashmap *routes_by_section;
153
154         LIST_HEAD(Address, dns);
155         LIST_HEAD(Address, ntp);
156
157         LIST_FIELDS(Network, networks);
158 };
159
160 struct Address {
161         Network *network;
162         uint64_t section;
163
164         unsigned char family;
165         unsigned char prefixlen;
166         unsigned char scope;
167         char *label;
168
169         struct in_addr broadcast;
170         struct ifa_cacheinfo cinfo;
171
172         union {
173                 struct in_addr in;
174                 struct in6_addr in6;
175         } in_addr;
176
177         LIST_FIELDS(Address, addresses);
178 };
179
180 struct Route {
181         Network *network;
182         uint64_t section;
183
184         unsigned char family;
185         unsigned char dst_prefixlen;
186         unsigned char scope;
187         uint32_t metrics;
188
189         union {
190                 struct in_addr in;
191                 struct in6_addr in6;
192         } in_addr;
193
194         union {
195                 struct in_addr in;
196                 struct in6_addr in6;
197         } dst_addr;
198
199         LIST_FIELDS(Route, routes);
200 };
201
202 typedef enum LinkState {
203         LINK_STATE_INITIALIZING,
204         LINK_STATE_ENSLAVING,
205         LINK_STATE_SETTING_ADDRESSES,
206         LINK_STATE_SETTING_ROUTES,
207         LINK_STATE_CONFIGURED,
208         LINK_STATE_UNMANAGED,
209         LINK_STATE_FAILED,
210         LINK_STATE_LINGER,
211         _LINK_STATE_MAX,
212         _LINK_STATE_INVALID = -1
213 } LinkState;
214
215 typedef enum LinkOperationalState {
216         LINK_OPERSTATE_UNKNOWN,
217         LINK_OPERSTATE_DORMANT,
218         LINK_OPERSTATE_CARRIER,
219         LINK_OPERSTATE_DEGRADED,
220         LINK_OPERSTATE_ROUTABLE,
221         _LINK_OPERSTATE_MAX,
222         _LINK_OPERSTATE_INVALID = -1
223 } LinkOperationalState;
224
225 struct Link {
226         Manager *manager;
227
228         int n_ref;
229
230         uint64_t ifindex;
231         char *ifname;
232         char *state_file;
233         struct ether_addr mac;
234         struct udev_device *udev_device;
235
236         unsigned flags;
237         uint8_t kernel_operstate;
238
239         Network *network;
240
241         LinkState state;
242         LinkOperationalState operstate;
243
244         unsigned addr_messages;
245         unsigned route_messages;
246         unsigned enslaving;
247
248         LIST_HEAD(Address, addresses);
249
250         sd_dhcp_client *dhcp_client;
251         sd_dhcp_lease *dhcp_lease;
252         char *lease_file;
253         uint16_t original_mtu;
254         sd_ipv4ll *ipv4ll;
255 };
256
257 struct Manager {
258         sd_rtnl *rtnl;
259         sd_event *event;
260         sd_bus *bus;
261         struct udev *udev;
262         struct udev_monitor *udev_monitor;
263         sd_event_source *udev_event_source;
264         sd_event_source *sigterm_event_source;
265         sd_event_source *sigint_event_source;
266
267         char *state_file;
268
269         Hashmap *links;
270         Hashmap *netdevs;
271         LIST_HEAD(Network, networks);
272
273         usec_t network_dirs_ts_usec;
274         struct kmod_ctx *kmod_ctx;
275 };
276
277 extern const char* const network_dirs[];
278
279 /* Manager */
280
281 int manager_new(Manager **ret);
282 void manager_free(Manager *m);
283
284 int manager_load_config(Manager *m);
285 bool manager_should_reload(Manager *m);
286
287 int manager_rtnl_enumerate_links(Manager *m);
288
289 int manager_rtnl_listen(Manager *m);
290 int manager_udev_listen(Manager *m);
291 int manager_bus_listen(Manager *m);
292
293 int manager_save(Manager *m);
294
295 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
296 #define _cleanup_manager_free_ _cleanup_(manager_freep)
297
298 /* NetDev */
299
300 int netdev_load(Manager *manager);
301 void netdev_drop(NetDev *netdev);
302
303 NetDev *netdev_unref(NetDev *netdev);
304 NetDev *netdev_ref(NetDev *netdev);
305
306 DEFINE_TRIVIAL_CLEANUP_FUNC(NetDev*, netdev_unref);
307 #define _cleanup_netdev_unref_ _cleanup_(netdev_unrefp)
308
309 int netdev_get(Manager *manager, const char *name, NetDev **ret);
310 int netdev_set_ifindex(NetDev *netdev, sd_rtnl_message *newlink);
311 int netdev_enslave(NetDev *netdev, Link *link, sd_rtnl_message_handler_t cb);
312 int netdev_create_tunnel(Link *link, sd_rtnl_message_handler_t callback);
313
314 const char *netdev_kind_to_string(NetDevKind d) _const_;
315 NetDevKind netdev_kind_from_string(const char *d) _pure_;
316
317 const char *macvlan_mode_to_string(MacVlanMode d) _const_;
318 MacVlanMode macvlan_mode_from_string(const char *d) _pure_;
319
320 int config_parse_netdev_kind(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
321
322 int config_parse_macvlan_mode(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
323
324 /* gperf */
325 const struct ConfigPerfItem* network_netdev_gperf_lookup(const char *key, unsigned length);
326
327 /* Network */
328
329 int network_load(Manager *manager);
330
331 void network_free(Network *network);
332
333 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
334 #define _cleanup_network_free_ _cleanup_(network_freep)
335
336 int network_get(Manager *manager, struct udev_device *device,
337                 const char *ifname, const struct ether_addr *mac,
338                 Network **ret);
339 int network_apply(Manager *manager, Network *network, Link *link);
340
341 int config_parse_netdev(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_tunnel(const char *unit,
346                         const char *filename,
347                         unsigned line,
348                         const char *section,
349                         unsigned section_line,
350                         const char *lvalue,
351                         int ltype,
352                         const char *rvalue,
353                         void *data,
354                         void *userdata);
355
356 int config_parse_tunnel_address(const char *unit,
357                                 const char *filename,
358                                 unsigned line,
359                                 const char *section,
360                                 unsigned section_line,
361                                 const char *lvalue,
362                                 int ltype,
363                                 const char *rvalue,
364                                 void *data,
365                                 void *userdata);
366
367 /* gperf */
368 const struct ConfigPerfItem* network_network_gperf_lookup(const char *key, unsigned length);
369
370 /* Route */
371 int route_new_static(Network *network, unsigned section, Route **ret);
372 int route_new_dynamic(Route **ret);
373 void route_free(Route *route);
374 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
375 int route_drop(Route *route, Link *link, sd_rtnl_message_handler_t callback);
376
377
378 DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
379 #define _cleanup_route_free_ _cleanup_(route_freep)
380
381 int config_parse_gateway(const char *unit, const char *filename, unsigned line,
382                          const char *section, unsigned section_line, const char *lvalue,
383                          int ltype, const char *rvalue, void *data, void *userdata);
384
385 int config_parse_destination(const char *unit, const char *filename, unsigned line,
386                              const char *section, unsigned section_line, const char *lvalue,
387                              int ltype, const char *rvalue, void *data, void *userdata);
388
389 /* Address */
390 int address_new_static(Network *network, unsigned section, Address **ret);
391 int address_new_dynamic(Address **ret);
392 void address_free(Address *address);
393 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
394 int address_update(Address *address, Link *link, sd_rtnl_message_handler_t callback);
395 int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callback);
396 bool address_equal(Address *a1, Address *a2);
397
398 DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
399 #define _cleanup_address_free_ _cleanup_(address_freep)
400
401 int config_parse_dns(const char *unit, const char *filename, unsigned line,
402                      const char *section, unsigned section_line, const char *lvalue,
403                      int ltype, const char *rvalue, void *data, void *userdata);
404
405 int config_parse_address(const char *unit, const char *filename, unsigned line,
406                          const char *section, unsigned section_line, const char *lvalue,
407                          int ltype, const char *rvalue, void *data, void *userdata);
408
409 int config_parse_broadcast(const char *unit, const char *filename, unsigned line,
410                            const char *section, unsigned section_line, const char *lvalue,
411                            int ltype, const char *rvalue, void *data, void *userdata);
412
413 int config_parse_label(const char *unit, const char *filename, unsigned line,
414                        const char *section, unsigned section_line, const char *lvalue,
415                        int ltype, const char *rvalue, void *data, void *userdata);
416
417 /* Link */
418
419 Link *link_unref(Link *link);
420 Link *link_ref(Link *link);
421 int link_get(Manager *m, int ifindex, Link **ret);
422 int link_add(Manager *manager, sd_rtnl_message *message, Link **ret);
423 void link_drop(Link *link);
424
425 int link_update(Link *link, sd_rtnl_message *message);
426 int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata);
427
428 int link_initialized(Link *link, struct udev_device *device);
429
430 int link_save(Link *link);
431
432 bool link_has_carrier(unsigned flags, uint8_t operstate);
433
434 const char* link_state_to_string(LinkState s) _const_;
435 LinkState link_state_from_string(const char *s) _pure_;
436
437 const char* link_operstate_to_string(LinkOperationalState s) _const_;
438 LinkOperationalState link_operstate_from_string(const char *s) _pure_;
439
440 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_unref);
441 #define _cleanup_link_unref_ _cleanup_(link_unrefp)
442
443 /* Macros which append INTERFACE= to the message */
444
445 #define log_full_link(level, link, fmt, ...) log_meta_object(level, __FILE__, __LINE__, __func__, "INTERFACE=", link->ifname, "%*s: " fmt, IFNAMSIZ, link->ifname, ##__VA_ARGS__)
446 #define log_debug_link(link, ...)       log_full_link(LOG_DEBUG, link, ##__VA_ARGS__)
447 #define log_info_link(link, ...)        log_full_link(LOG_INFO, link, ##__VA_ARGS__)
448 #define log_notice_link(link, ...)      log_full_link(LOG_NOTICE, link, ##__VA_ARGS__)
449 #define log_warning_link(link, ...)     log_full_link(LOG_WARNING, link, ##__VA_ARGS__)
450 #define log_error_link(link, ...)       log_full_link(LOG_ERR, link, ##__VA_ARGS__)
451
452 #define log_struct_link(level, link, ...) log_struct(level, "INTERFACE=%s", link->ifname, __VA_ARGS__)
453
454 /* More macros which append INTERFACE= to the message */
455
456 #define log_full_netdev(level, netdev, fmt, ...) log_meta_object(level, __FILE__, __LINE__, __func__, "INTERFACE=", netdev->ifname, "%*s: " fmt, IFNAMSIZ, netdev->ifname, ##__VA_ARGS__)
457 #define log_debug_netdev(netdev, ...)       log_full_netdev(LOG_DEBUG, netdev, ##__VA_ARGS__)
458 #define log_info_netdev(netdev, ...)        log_full_netdev(LOG_INFO, netdev, ##__VA_ARGS__)
459 #define log_notice_netdev(netdev, ...)      log_full_netdev(LOG_NOTICE, netdev, ##__VA_ARGS__)
460 #define log_warning_netdev(netdev, ...)     log_full_netdev(LOG_WARNING, netdev,## __VA_ARGS__)
461 #define log_error_netdev(netdev, ...)       log_full_netdev(LOG_ERR, netdev, ##__VA_ARGS__)
462
463 #define log_struct_netdev(level, netdev, ...) log_struct(level, "INTERFACE=%s", netdev->ifname, __VA_ARGS__)
464
465 #define NETDEV(netdev) "INTERFACE=%s", netdev->ifname
466 #define ADDRESS_FMT_VAL(address)            \
467         (address).s_addr & 0xFF,            \
468         ((address).s_addr >> 8) & 0xFF,     \
469         ((address).s_addr >> 16) & 0xFF,    \
470         (address).s_addr >> 24