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