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