chiark / gitweb /
networkd: add minimal IP forwarding and masquerading support to .network files
[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 #include "sd-lldp.h"
36
37 #include "rtnl-util.h"
38 #include "hashmap.h"
39 #include "list.h"
40 #include "set.h"
41 #include "condition.h"
42 #include "in-addr-util.h"
43
44 #define CACHE_INFO_INFINITY_LIFE_TIME 0xFFFFFFFFU
45 #define DHCP_ROUTE_METRIC 1024
46 #define IPV4LL_ROUTE_METRIC 2048
47
48 typedef struct NetDev NetDev;
49 typedef struct Network Network;
50 typedef struct Link Link;
51 typedef struct Address Address;
52 typedef struct Route Route;
53 typedef struct Manager Manager;
54 typedef struct AddressPool AddressPool;
55 typedef struct FdbEntry FdbEntry;
56
57 typedef enum DHCPSupport {
58         DHCP_SUPPORT_NONE,
59         DHCP_SUPPORT_BOTH,
60         DHCP_SUPPORT_V4,
61         DHCP_SUPPORT_V6,
62         _DHCP_SUPPORT_MAX,
63         _DHCP_SUPPORT_INVALID = -1,
64 } DHCPSupport;
65
66 typedef enum LLMNRSupport {
67         LLMNR_SUPPORT_NO,
68         LLMNR_SUPPORT_YES,
69         LLMNR_SUPPORT_RESOLVE,
70         _LLMNR_SUPPORT_MAX,
71         _LLMNR_SUPPORT_INVALID = -1,
72 } LLMNRSupport;
73
74 struct FdbEntry {
75         Network *network;
76         unsigned section;
77
78         struct ether_addr *mac_addr;
79         uint16_t vlan_id;
80
81         LIST_FIELDS(FdbEntry, static_fdb_entries);
82 };
83
84 struct Network {
85         Manager *manager;
86
87         char *filename;
88
89         struct ether_addr *match_mac;
90         char *match_path;
91         char *match_driver;
92         char *match_type;
93         char *match_name;
94         char *dhcp_vendor_class_identifier;
95
96         Condition *match_host;
97         Condition *match_virt;
98         Condition *match_kernel;
99         Condition *match_arch;
100
101         char *description;
102         NetDev *bridge;
103         NetDev *bond;
104         Hashmap *stacked_netdevs;
105         DHCPSupport dhcp;
106         bool dhcp_dns;
107         bool dhcp_ntp;
108         bool dhcp_mtu;
109         bool dhcp_hostname;
110         bool dhcp_domains;
111         bool dhcp_sendhost;
112         bool dhcp_broadcast;
113         bool dhcp_critical;
114         bool dhcp_routes;
115         unsigned dhcp_route_metric;
116         bool ipv4ll;
117         bool ipv4ll_route;
118
119         bool dhcp_server;
120
121         unsigned cost;
122
123         bool ip_masquerade;
124         bool ip_forward;
125
126         struct ether_addr *mac;
127         unsigned mtu;
128
129         bool lldp;
130
131         LIST_HEAD(Address, static_addresses);
132         LIST_HEAD(Route, static_routes);
133         LIST_HEAD(FdbEntry, static_fdb_entries);
134
135         Hashmap *addresses_by_section;
136         Hashmap *routes_by_section;
137         Hashmap *fdb_entries_by_section;
138
139         bool wildcard_domain;
140         char **domains, **dns, **ntp;
141
142         LLMNRSupport llmnr;
143
144         LIST_FIELDS(Network, networks);
145 };
146
147 struct Address {
148         Network *network;
149         unsigned section;
150
151         int family;
152         unsigned char prefixlen;
153         unsigned char scope;
154         unsigned char flags;
155         char *label;
156
157         struct in_addr broadcast;
158         struct ifa_cacheinfo cinfo;
159
160         union in_addr_union in_addr;
161         union in_addr_union in_addr_peer;
162
163         bool ip_forward_done;
164
165         LIST_FIELDS(Address, addresses);
166 };
167
168 struct Route {
169         Network *network;
170         unsigned section;
171
172         int family;
173         unsigned char dst_prefixlen;
174         unsigned char src_prefixlen;
175         unsigned char scope;
176         uint32_t metrics;
177         unsigned char protocol;  /* RTPROT_* */
178
179         union in_addr_union in_addr;
180         union in_addr_union dst_addr;
181         union in_addr_union src_addr;
182         union in_addr_union prefsrc_addr;
183
184         LIST_FIELDS(Route, routes);
185 };
186
187 struct AddressPool {
188         Manager *manager;
189
190         int family;
191         unsigned prefixlen;
192
193         union in_addr_union in_addr;
194
195         LIST_FIELDS(AddressPool, address_pools);
196 };
197
198 struct Manager {
199         sd_rtnl *rtnl;
200         sd_event *event;
201         sd_bus *bus;
202         struct udev *udev;
203         struct udev_monitor *udev_monitor;
204         sd_event_source *udev_event_source;
205
206         char *state_file;
207
208         Hashmap *links;
209         Hashmap *netdevs;
210         LIST_HEAD(Network, networks);
211         LIST_HEAD(AddressPool, address_pools);
212
213         usec_t network_dirs_ts_usec;
214 };
215
216 extern const char* const network_dirs[];
217
218 /* Manager */
219
220 int manager_new(Manager **ret);
221 void manager_free(Manager *m);
222
223 int manager_load_config(Manager *m);
224 bool manager_should_reload(Manager *m);
225
226 int manager_rtnl_enumerate_links(Manager *m);
227 int manager_rtnl_enumerate_addresses(Manager *m);
228
229 int manager_rtnl_listen(Manager *m);
230 int manager_udev_listen(Manager *m);
231 int manager_bus_listen(Manager *m);
232
233 int manager_save(Manager *m);
234
235 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found);
236
237 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
238 #define _cleanup_manager_free_ _cleanup_(manager_freep)
239
240 /* Network */
241
242 int network_load(Manager *manager);
243
244 void network_free(Network *network);
245
246 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
247 #define _cleanup_network_free_ _cleanup_(network_freep)
248
249 int network_get(Manager *manager, struct udev_device *device,
250                 const char *ifname, const struct ether_addr *mac,
251                 Network **ret);
252 int network_apply(Manager *manager, Network *network, Link *link);
253
254 int config_parse_netdev(const char *unit, const char *filename, unsigned line,
255                         const char *section, unsigned section_line, const char *lvalue,
256                         int ltype, const char *rvalue, void *data, void *userdata);
257
258 int config_parse_domains(const char *unit,
259                          const char *filename,
260                          unsigned line,
261                          const char *section,
262                          unsigned section_line,
263                          const char *lvalue,
264                          int ltype,
265                          const char *rvalue,
266                          void *data,
267                          void *userdata);
268
269 int config_parse_tunnel(const char *unit,
270                         const char *filename,
271                         unsigned line,
272                         const char *section,
273                         unsigned section_line,
274                         const char *lvalue,
275                         int ltype,
276                         const char *rvalue,
277                         void *data,
278                         void *userdata);
279
280 int config_parse_tunnel_address(const char *unit,
281                                 const char *filename,
282                                 unsigned line,
283                                 const char *section,
284                                 unsigned section_line,
285                                 const char *lvalue,
286                                 int ltype,
287                                 const char *rvalue,
288                                 void *data,
289                                 void *userdata);
290
291 int config_parse_vxlan_group_address(const char *unit,
292                                      const char *filename,
293                                      unsigned line,
294                                      const char *section,
295                                      unsigned section_line,
296                                      const char *lvalue,
297                                      int ltype,
298                                      const char *rvalue,
299                                      void *data,
300                                      void *userdata);
301
302 /* gperf */
303 const struct ConfigPerfItem* network_network_gperf_lookup(const char *key, unsigned length);
304
305 /* Route */
306 int route_new_static(Network *network, unsigned section, Route **ret);
307 int route_new_dynamic(Route **ret, unsigned char rtm_protocol);
308 void route_free(Route *route);
309 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
310 int route_drop(Route *route, Link *link, sd_rtnl_message_handler_t callback);
311
312
313 DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
314 #define _cleanup_route_free_ _cleanup_(route_freep)
315
316 int config_parse_gateway(const char *unit, const char *filename, unsigned line,
317                          const char *section, unsigned section_line, const char *lvalue,
318                          int ltype, const char *rvalue, void *data, void *userdata);
319
320 int config_parse_destination(const char *unit, const char *filename, unsigned line,
321                              const char *section, unsigned section_line, const char *lvalue,
322                              int ltype, const char *rvalue, void *data, void *userdata);
323
324 int config_parse_route_priority(const char *unit, const char *filename, unsigned line,
325                                 const char *section, unsigned section_line, const char *lvalue,
326                                 int ltype, const char *rvalue, void *data, void *userdata);
327 /* Address */
328 int address_new_static(Network *network, unsigned section, Address **ret);
329 int address_new_dynamic(Address **ret);
330 void address_free(Address *address);
331 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
332 int address_update(Address *address, Link *link, sd_rtnl_message_handler_t callback);
333 int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callback);
334 int address_establish(Address *address, Link *link);
335 int address_release(Address *address, Link *link);
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_address(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_broadcast(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_label(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 /* Forwarding database table. */
354 int fdb_entry_configure(sd_rtnl *const rtnl, FdbEntry *const fdb_entry, const int ifindex);
355 void fdb_entry_free(FdbEntry *fdb_entry);
356 int fdb_entry_new_static(Network *const network, const unsigned section, FdbEntry **ret);
357
358 DEFINE_TRIVIAL_CLEANUP_FUNC(FdbEntry*, fdb_entry_free);
359 #define _cleanup_fdbentry_free_ _cleanup_(fdb_entry_freep)
360
361 int config_parse_fdb_hwaddr(const char *unit, const char *filename, unsigned line,
362                             const char *section, unsigned section_line, const char *lvalue,
363                             int ltype, const char *rvalue, void *data, void *userdata);
364
365 int config_parse_fdb_vlan_id(const char *unit, const char *filename, unsigned line,
366                              const char *section, unsigned section_line, const char *lvalue,
367                              int ltype, const char *rvalue, void *data, void *userdata);
368
369 /* DHCP support */
370
371 const char* dhcp_support_to_string(DHCPSupport i) _const_;
372 DHCPSupport dhcp_support_from_string(const char *s) _pure_;
373
374 int config_parse_dhcp(const char *unit, const char *filename, unsigned line,
375                       const char *section, unsigned section_line, const char *lvalue,
376                       int ltype, const char *rvalue, void *data, void *userdata);
377
378 /* LLMNR support */
379
380 const char* llmnr_support_to_string(LLMNRSupport i) _const_;
381 LLMNRSupport llmnr_support_from_string(const char *s) _pure_;
382
383 int config_parse_llmnr(const char *unit, const char *filename, unsigned line,
384                       const char *section, unsigned section_line, const char *lvalue,
385                       int ltype, const char *rvalue, void *data, void *userdata);
386
387 /* Address Pool */
388
389 int address_pool_new(Manager *m, AddressPool **ret, int family, const union in_addr_union *u, unsigned prefixlen);
390 int address_pool_new_from_string(Manager *m, AddressPool **ret, int family, const char *p, unsigned prefixlen);
391 void address_pool_free(AddressPool *p);
392
393 int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found);