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