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