]>
Commit | Line | Data |
---|---|---|
f579559b TG |
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" | |
f5be5601 | 29 | #include "sd-dhcp-client.h" |
f579559b TG |
30 | #include "udev.h" |
31 | ||
32 | #include "rtnl-util.h" | |
33 | #include "hashmap.h" | |
34 | #include "list.h" | |
35 | ||
02b59d57 | 36 | typedef struct Bridge Bridge; |
f579559b TG |
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 | ||
02b59d57 TG |
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, | |
02b59d57 TG |
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 | ||
f579559b TG |
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; | |
02b59d57 | 86 | Bridge *bridge; |
f5be5601 | 87 | bool dhcp; |
5be4d38e | 88 | bool dhcp_dns; |
f579559b | 89 | |
f048a16b TG |
90 | LIST_HEAD(Address, static_addresses); |
91 | LIST_HEAD(Route, static_routes); | |
3bef724f | 92 | Address *dns; |
f579559b | 93 | |
6ae115c1 TG |
94 | Hashmap *addresses_by_section; |
95 | Hashmap *routes_by_section; | |
96 | ||
f579559b TG |
97 | LIST_FIELDS(Network, networks); |
98 | }; | |
99 | ||
100 | struct Address { | |
101 | Network *network; | |
6ae115c1 | 102 | uint64_t section; |
f579559b TG |
103 | |
104 | unsigned char family; | |
105 | unsigned char prefixlen; | |
106 | char *label; | |
107 | ||
8cd11a0f TG |
108 | struct in_addr netmask; |
109 | ||
f579559b TG |
110 | union { |
111 | struct in_addr in; | |
112 | struct in6_addr in6; | |
113 | } in_addr; | |
114 | ||
f048a16b | 115 | LIST_FIELDS(Address, static_addresses); |
f579559b TG |
116 | }; |
117 | ||
118 | struct Route { | |
119 | Network *network; | |
6ae115c1 | 120 | uint64_t section; |
f579559b TG |
121 | |
122 | unsigned char family; | |
b3070dc0 | 123 | unsigned char dst_family; |
6ae115c1 | 124 | unsigned char dst_prefixlen; |
f579559b TG |
125 | |
126 | union { | |
127 | struct in_addr in; | |
128 | struct in6_addr in6; | |
129 | } in_addr; | |
130 | ||
6ae115c1 TG |
131 | union { |
132 | struct in_addr in; | |
133 | struct in6_addr in6; | |
134 | } dst_addr; | |
135 | ||
f048a16b | 136 | LIST_FIELDS(Route, static_routes); |
f579559b TG |
137 | }; |
138 | ||
f882c247 | 139 | typedef enum LinkState { |
ef1ba606 TG |
140 | LINK_STATE_JOINING_BRIDGE, |
141 | LINK_STATE_SETTING_ADDRESSES, | |
142 | LINK_STATE_SETTING_ROUTES, | |
f882c247 TG |
143 | LINK_STATE_CONFIGURED, |
144 | LINK_STATE_FAILED, | |
145 | _LINK_STATE_MAX, | |
146 | _LINK_STATE_INVALID = -1 | |
147 | } LinkState; | |
148 | ||
f579559b TG |
149 | struct Link { |
150 | Manager *manager; | |
151 | ||
0617ffab | 152 | uint64_t ifindex; |
c166a070 | 153 | char *ifname; |
8cd11a0f | 154 | struct ether_addr mac; |
f579559b TG |
155 | |
156 | unsigned flags; | |
157 | ||
158 | Network *network; | |
f882c247 | 159 | |
f5be5601 TG |
160 | Route *dhcp_route; |
161 | Address *dhcp_address; | |
3bef724f | 162 | Address *dns; |
f5be5601 | 163 | |
f882c247 TG |
164 | LinkState state; |
165 | ||
f5be5601 TG |
166 | unsigned addr_messages; |
167 | unsigned route_messages; | |
168 | ||
169 | sd_dhcp_client *dhcp; | |
f579559b TG |
170 | }; |
171 | ||
172 | struct Manager { | |
173 | sd_rtnl *rtnl; | |
174 | sd_event *event; | |
175 | struct udev *udev; | |
176 | struct udev_monitor *udev_monitor; | |
177 | sd_event_source *udev_event_source; | |
178 | ||
179 | Hashmap *links; | |
02b59d57 | 180 | Hashmap *bridges; |
f579559b TG |
181 | LIST_HEAD(Network, networks); |
182 | ||
f579559b TG |
183 | usec_t network_dirs_ts_usec; |
184 | }; | |
185 | ||
2ad8416d ZJS |
186 | extern const char* const network_dirs[]; |
187 | ||
f579559b TG |
188 | /* Manager */ |
189 | ||
190 | int manager_new(Manager **ret); | |
191 | void manager_free(Manager *m); | |
192 | ||
02b59d57 TG |
193 | int manager_load_config(Manager *m); |
194 | bool manager_should_reload(Manager *m); | |
195 | ||
f579559b TG |
196 | int manager_udev_enumerate_links(Manager *m); |
197 | int manager_udev_listen(Manager *m); | |
198 | ||
f882c247 TG |
199 | int manager_rtnl_listen(Manager *m); |
200 | ||
3bef724f TG |
201 | int manager_update_resolv_conf(Manager *m); |
202 | ||
f579559b TG |
203 | DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free); |
204 | #define _cleanup_manager_free_ _cleanup_(manager_freep) | |
205 | ||
02b59d57 TG |
206 | /* Bridge */ |
207 | ||
208 | int bridge_load(Manager *manager); | |
209 | ||
210 | void bridge_free(Bridge *bridge); | |
211 | ||
212 | DEFINE_TRIVIAL_CLEANUP_FUNC(Bridge*, bridge_free); | |
213 | #define _cleanup_bridge_free_ _cleanup_(bridge_freep) | |
214 | ||
215 | int bridge_get(Manager *manager, const char *name, Bridge **ret); | |
216 | int bridge_set_link(Manager *m, Link *link); | |
217 | int bridge_join(Bridge *bridge, Link *link, sd_rtnl_message_handler_t cb); | |
218 | ||
f579559b TG |
219 | /* Network */ |
220 | ||
221 | int network_load(Manager *manager); | |
f579559b TG |
222 | |
223 | void network_free(Network *network); | |
224 | ||
225 | DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free); | |
226 | #define _cleanup_network_free_ _cleanup_(network_freep) | |
227 | ||
228 | int network_get(Manager *manager, struct udev_device *device, Network **ret); | |
229 | int network_apply(Manager *manager, Network *network, Link *link); | |
230 | ||
02b59d57 TG |
231 | int config_parse_bridge(const char *unit, const char *filename, unsigned line, |
232 | const char *section, unsigned section_line, const char *lvalue, | |
233 | int ltype, const char *rvalue, void *data, void *userdata); | |
234 | ||
235 | /* gperf */ | |
236 | ||
f579559b TG |
237 | const struct ConfigPerfItem* network_gperf_lookup(const char *key, unsigned length); |
238 | ||
239 | /* Route */ | |
f048a16b TG |
240 | int route_new_static(Network *network, unsigned section, Route **ret); |
241 | int route_new_dynamic(Route **ret); | |
f579559b | 242 | void route_free(Route *route); |
f882c247 | 243 | int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback); |
f579559b TG |
244 | |
245 | DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free); | |
246 | #define _cleanup_route_free_ _cleanup_(route_freep) | |
247 | ||
248 | int config_parse_gateway(const char *unit, const char *filename, unsigned line, | |
71a61510 TG |
249 | const char *section, unsigned section_line, const char *lvalue, |
250 | int ltype, const char *rvalue, void *data, void *userdata); | |
f579559b | 251 | |
6ae115c1 TG |
252 | int config_parse_destination(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 | ||
f579559b | 256 | /* Address */ |
f048a16b TG |
257 | int address_new_static(Network *network, unsigned section, Address **ret); |
258 | int address_new_dynamic(Address **ret); | |
f579559b | 259 | void address_free(Address *address); |
f882c247 | 260 | int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback); |
407fe036 | 261 | int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callback); |
f579559b TG |
262 | |
263 | DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free); | |
264 | #define _cleanup_address_free_ _cleanup_(address_freep) | |
265 | ||
3bef724f TG |
266 | int config_parse_dns(const char *unit, const char *filename, unsigned line, |
267 | const char *section, unsigned section_line, const char *lvalue, | |
268 | int ltype, const char *rvalue, void *data, void *userdata); | |
269 | ||
f579559b | 270 | int config_parse_address(const char *unit, const char *filename, unsigned line, |
71a61510 TG |
271 | const char *section, unsigned section_line, const char *lvalue, |
272 | int ltype, const char *rvalue, void *data, void *userdata); | |
f579559b | 273 | |
6ae115c1 TG |
274 | int config_parse_label(const char *unit, const char *filename, unsigned line, |
275 | const char *section, unsigned section_line, const char *lvalue, | |
276 | int ltype, const char *rvalue, void *data, void *userdata); | |
277 | ||
f579559b TG |
278 | /* Link */ |
279 | ||
280 | int link_new(Manager *manager, struct udev_device *device, Link **ret); | |
281 | void link_free(Link *link); | |
aa3437a5 | 282 | int link_add(Manager *manager, struct udev_device *device, Link **ret); |
f882c247 | 283 | int link_configure(Link *link); |
f579559b | 284 | |
22936833 | 285 | int link_update(Link *link, sd_rtnl_message *message); |
dd3efc09 | 286 | |
f579559b TG |
287 | DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free); |
288 | #define _cleanup_link_free_ _cleanup_(link_freep) | |
3333d748 ZJS |
289 | |
290 | /* Macros which append INTERFACE= to the message */ | |
291 | ||
39032b87 ZJS |
292 | #define log_full_link(level, link, fmt, ...) log_meta_object(level, __FILE__, __LINE__, __func__, "INTERFACE=", link->ifname, "%s: " fmt, link->ifname, ##__VA_ARGS__) |
293 | #define log_debug_link(link, ...) log_full_link(LOG_DEBUG, link, ##__VA_ARGS__) | |
294 | #define log_info_link(link, ...) log_full_link(LOG_INFO, link, ##__VA_ARGS__) | |
295 | #define log_notice_link(link, ...) log_full_link(LOG_NOTICE, link, ##__VA_ARGS__) | |
296 | #define log_warning_link(link, ...) log_full_link(LOG_WARNING, link, ##__VA_ARGS__) | |
297 | #define log_error_link(link, ...) log_full_link(LOG_ERR, link, ##__VA_ARGS__) | |
3333d748 ZJS |
298 | |
299 | #define log_struct_link(level, link, ...) log_struct(level, "INTERFACE=%s", link->ifname, __VA_ARGS__) | |
300 | ||
301 | /* More macros which append INTERFACE= to the message */ | |
302 | ||
39032b87 ZJS |
303 | #define log_full_bridge(level, bridge, fmt, ...) log_meta_object(level, __FILE__, __LINE__, __func__, "INTERFACE=", bridge->name, "%s: " fmt, bridge->name, ##__VA_ARGS__) |
304 | #define log_debug_bridge(bridge, ...) log_full_bridge(LOG_DEBUG, bridge, ##__VA_ARGS__) | |
305 | #define log_info_bridge(bridge, ...) log_full_bridge(LOG_INFO, bridge, ##__VA_ARGS__) | |
306 | #define log_notice_bridge(bridge, ...) log_full_bridge(LOG_NOTICE, bridge, ##__VA_ARGS__) | |
307 | #define log_warning_bridge(bridge, ...) log_full_bridge(LOG_WARNING, bridge,## __VA_ARGS__) | |
308 | #define log_error_bridge(bridge, ...) log_full_bridge(LOG_ERR, bridge, ##__VA_ARGS__) | |
3333d748 ZJS |
309 | |
310 | #define log_struct_bridge(level, bridge, ...) log_struct(level, "INTERFACE=%s", bridge->name, __VA_ARGS__) | |
311 | ||
312 | #define BRIDGE(bridge) "INTERFACE=%s", bridge->name | |
62870613 ZJS |
313 | #define ADDRESS_FMT_VAL(address) \ |
314 | (address).s_addr & 0xFF, \ | |
315 | ((address).s_addr >> 8) & 0xFF, \ | |
316 | ((address).s_addr >> 16) & 0xFF, \ | |
317 | (address).s_addr >> 24 |