chiark / gitweb /
75309a9b85f67970097954962b6b8c40ac7b8712
[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 #include <linux/rtnetlink.h>
26
27 #include "sd-event.h"
28 #include "sd-rtnl.h"
29 #include "sd-dhcp-client.h"
30 #include "udev.h"
31
32 #include "rtnl-util.h"
33 #include "hashmap.h"
34 #include "list.h"
35
36 typedef struct Bridge Bridge;
37 typedef struct Network Network;
38 typedef struct Link Link;
39 typedef struct Address Address;
40 typedef struct Route Route;
41 typedef struct Manager Manager;
42
43 typedef struct bridge_join_callback bridge_join_callback;
44
45 struct bridge_join_callback {
46         sd_rtnl_message_handler_t callback;
47         Link *link;
48
49         LIST_FIELDS(bridge_join_callback, callbacks);
50 };
51
52 typedef enum BridgeState {
53         BRIDGE_STATE_FAILED,
54         BRIDGE_STATE_CREATING,
55         BRIDGE_STATE_CREATED,
56         BRIDGE_STATE_READY,
57         _BRIDGE_STATE_MAX,
58         _BRIDGE_STATE_INVALID = -1,
59 } BridgeState;
60
61 struct Bridge {
62         Manager *manager;
63
64         char *filename;
65
66         char *description;
67         char *name;
68
69         Link *link;
70         BridgeState state;
71
72         LIST_HEAD(bridge_join_callback, callbacks);
73 };
74
75 struct Network {
76         Manager *manager;
77
78         char *filename;
79
80         struct ether_addr *match_mac;
81         char *match_path;
82         char *match_driver;
83         char *match_type;
84         char *match_name;
85
86         char *description;
87         Bridge *bridge;
88         bool dhcp;
89
90         LIST_HEAD(Address, static_addresses);
91         LIST_HEAD(Route, static_routes);
92
93         Hashmap *addresses_by_section;
94         Hashmap *routes_by_section;
95
96         LIST_FIELDS(Network, networks);
97 };
98
99 struct Address {
100         Network *network;
101         uint64_t section;
102
103         unsigned char family;
104         unsigned char prefixlen;
105         char *label;
106
107         struct in_addr netmask;
108
109         union {
110                 struct in_addr in;
111                 struct in6_addr in6;
112         } in_addr;
113
114         LIST_FIELDS(Address, static_addresses);
115 };
116
117 struct Route {
118         Network *network;
119         uint64_t section;
120
121         unsigned char family;
122         unsigned char dst_family;
123         unsigned char dst_prefixlen;
124
125         union {
126                 struct in_addr in;
127                 struct in6_addr in6;
128         } in_addr;
129
130         union {
131                 struct in_addr in;
132                 struct in6_addr in6;
133         } dst_addr;
134
135         LIST_FIELDS(Route, static_routes);
136 };
137
138 typedef enum LinkState {
139         LINK_STATE_JOINING_BRIDGE,
140         LINK_STATE_SETTING_ADDRESSES,
141         LINK_STATE_SETTING_ROUTES,
142         LINK_STATE_CONFIGURED,
143         LINK_STATE_FAILED,
144         _LINK_STATE_MAX,
145         _LINK_STATE_INVALID = -1
146 } LinkState;
147
148 struct Link {
149         Manager *manager;
150
151         uint64_t ifindex;
152         char *ifname;
153         struct ether_addr mac;
154
155         unsigned flags;
156
157         Network *network;
158
159         Route *dhcp_route;
160         Address *dhcp_address;
161
162         LinkState state;
163
164         unsigned addr_messages;
165         unsigned route_messages;
166
167         sd_dhcp_client *dhcp;
168 };
169
170 struct Manager {
171         sd_rtnl *rtnl;
172         sd_event *event;
173         struct udev *udev;
174         struct udev_monitor *udev_monitor;
175         sd_event_source *udev_event_source;
176
177         Hashmap *links;
178         Hashmap *bridges;
179         LIST_HEAD(Network, networks);
180
181         char **network_dirs;
182         usec_t network_dirs_ts_usec;
183 };
184
185 /* Manager */
186
187 int manager_new(Manager **ret);
188 void manager_free(Manager *m);
189
190 int manager_load_config(Manager *m);
191 bool manager_should_reload(Manager *m);
192
193 int manager_udev_enumerate_links(Manager *m);
194 int manager_udev_listen(Manager *m);
195
196 int manager_rtnl_listen(Manager *m);
197
198 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
199 #define _cleanup_manager_free_ _cleanup_(manager_freep)
200
201 /* Bridge */
202
203 int bridge_load(Manager *manager);
204
205 void bridge_free(Bridge *bridge);
206
207 DEFINE_TRIVIAL_CLEANUP_FUNC(Bridge*, bridge_free);
208 #define _cleanup_bridge_free_ _cleanup_(bridge_freep)
209
210 int bridge_get(Manager *manager, const char *name, Bridge **ret);
211 int bridge_set_link(Manager *m, Link *link);
212 int bridge_join(Bridge *bridge, Link *link, sd_rtnl_message_handler_t cb);
213
214 /* Network */
215
216 int network_load(Manager *manager);
217
218 void network_free(Network *network);
219
220 DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
221 #define _cleanup_network_free_ _cleanup_(network_freep)
222
223 int network_get(Manager *manager, struct udev_device *device, Network **ret);
224 int network_apply(Manager *manager, Network *network, Link *link);
225
226 int config_parse_bridge(const char *unit, const char *filename, unsigned line,
227                         const char *section, unsigned section_line, const char *lvalue,
228                         int ltype, const char *rvalue, void *data, void *userdata);
229
230 /* gperf */
231
232 const struct ConfigPerfItem* network_gperf_lookup(const char *key, unsigned length);
233
234 /* Route */
235 int route_new_static(Network *network, unsigned section, Route **ret);
236 int route_new_dynamic(Route **ret);
237 void route_free(Route *route);
238 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
239
240 DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
241 #define _cleanup_route_free_ _cleanup_(route_freep)
242
243 int config_parse_gateway(const char *unit, const char *filename, unsigned line,
244                          const char *section, unsigned section_line, const char *lvalue,
245                          int ltype, const char *rvalue, void *data, void *userdata);
246
247 int config_parse_destination(const char *unit, const char *filename, unsigned line,
248                              const char *section, unsigned section_line, const char *lvalue,
249                              int ltype, const char *rvalue, void *data, void *userdata);
250
251 /* Address */
252 int address_new_static(Network *network, unsigned section, Address **ret);
253 int address_new_dynamic(Address **ret);
254 void address_free(Address *address);
255 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
256 int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callback);
257
258 DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
259 #define _cleanup_address_free_ _cleanup_(address_freep)
260
261 int config_parse_address(const char *unit, const char *filename, unsigned line,
262                          const char *section, unsigned section_line, const char *lvalue,
263                          int ltype, const char *rvalue, void *data, void *userdata);
264
265 int config_parse_label(const char *unit, const char *filename, unsigned line,
266                        const char *section, unsigned section_line, const char *lvalue,
267                        int ltype, const char *rvalue, void *data, void *userdata);
268
269 /* Link */
270
271 int link_new(Manager *manager, struct udev_device *device, Link **ret);
272 void link_free(Link *link);
273 int link_add(Manager *manager, struct udev_device *device, Link **ret);
274 int link_configure(Link *link);
275
276 int link_update(Link *link, sd_rtnl_message *message);
277
278 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free);
279 #define _cleanup_link_free_ _cleanup_(link_freep)