]>
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, | |
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 | ||
f579559b TG |
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; | |
02b59d57 | 87 | Bridge *bridge; |
f5be5601 | 88 | bool dhcp; |
f579559b | 89 | |
f048a16b TG |
90 | LIST_HEAD(Address, static_addresses); |
91 | LIST_HEAD(Route, static_routes); | |
f579559b | 92 | |
6ae115c1 TG |
93 | Hashmap *addresses_by_section; |
94 | Hashmap *routes_by_section; | |
95 | ||
f579559b TG |
96 | LIST_FIELDS(Network, networks); |
97 | }; | |
98 | ||
99 | struct Address { | |
100 | Network *network; | |
6ae115c1 | 101 | uint64_t section; |
f579559b TG |
102 | |
103 | unsigned char family; | |
104 | unsigned char prefixlen; | |
105 | char *label; | |
106 | ||
8cd11a0f TG |
107 | struct in_addr netmask; |
108 | ||
f579559b TG |
109 | union { |
110 | struct in_addr in; | |
111 | struct in6_addr in6; | |
112 | } in_addr; | |
113 | ||
f048a16b | 114 | LIST_FIELDS(Address, static_addresses); |
f579559b TG |
115 | }; |
116 | ||
117 | struct Route { | |
118 | Network *network; | |
6ae115c1 | 119 | uint64_t section; |
f579559b TG |
120 | |
121 | unsigned char family; | |
6ae115c1 | 122 | unsigned char dst_prefixlen; |
f579559b TG |
123 | |
124 | union { | |
125 | struct in_addr in; | |
126 | struct in6_addr in6; | |
127 | } in_addr; | |
128 | ||
6ae115c1 TG |
129 | union { |
130 | struct in_addr in; | |
131 | struct in6_addr in6; | |
132 | } dst_addr; | |
133 | ||
f048a16b | 134 | LIST_FIELDS(Route, static_routes); |
f579559b TG |
135 | }; |
136 | ||
f882c247 | 137 | typedef enum LinkState { |
ef1ba606 TG |
138 | LINK_STATE_JOINING_BRIDGE, |
139 | LINK_STATE_SETTING_ADDRESSES, | |
140 | LINK_STATE_SETTING_ROUTES, | |
f882c247 TG |
141 | LINK_STATE_CONFIGURED, |
142 | LINK_STATE_FAILED, | |
143 | _LINK_STATE_MAX, | |
144 | _LINK_STATE_INVALID = -1 | |
145 | } LinkState; | |
146 | ||
f579559b TG |
147 | struct Link { |
148 | Manager *manager; | |
149 | ||
0617ffab | 150 | uint64_t ifindex; |
c166a070 | 151 | char *ifname; |
8cd11a0f | 152 | struct ether_addr mac; |
f579559b TG |
153 | |
154 | unsigned flags; | |
155 | ||
156 | Network *network; | |
f882c247 | 157 | |
f5be5601 TG |
158 | Route *dhcp_route; |
159 | Address *dhcp_address; | |
160 | ||
f882c247 TG |
161 | LinkState state; |
162 | ||
f5be5601 TG |
163 | unsigned addr_messages; |
164 | unsigned route_messages; | |
165 | ||
166 | sd_dhcp_client *dhcp; | |
f579559b TG |
167 | }; |
168 | ||
169 | struct Manager { | |
170 | sd_rtnl *rtnl; | |
171 | sd_event *event; | |
172 | struct udev *udev; | |
173 | struct udev_monitor *udev_monitor; | |
174 | sd_event_source *udev_event_source; | |
175 | ||
176 | Hashmap *links; | |
02b59d57 | 177 | Hashmap *bridges; |
f579559b TG |
178 | LIST_HEAD(Network, networks); |
179 | ||
180 | char **network_dirs; | |
181 | usec_t network_dirs_ts_usec; | |
182 | }; | |
183 | ||
184 | /* Manager */ | |
185 | ||
186 | int manager_new(Manager **ret); | |
187 | void manager_free(Manager *m); | |
188 | ||
02b59d57 TG |
189 | int manager_load_config(Manager *m); |
190 | bool manager_should_reload(Manager *m); | |
191 | ||
f579559b TG |
192 | int manager_udev_enumerate_links(Manager *m); |
193 | int manager_udev_listen(Manager *m); | |
194 | ||
f882c247 TG |
195 | int manager_rtnl_listen(Manager *m); |
196 | ||
f579559b TG |
197 | DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free); |
198 | #define _cleanup_manager_free_ _cleanup_(manager_freep) | |
199 | ||
02b59d57 TG |
200 | /* Bridge */ |
201 | ||
202 | int bridge_load(Manager *manager); | |
203 | ||
204 | void bridge_free(Bridge *bridge); | |
205 | ||
206 | DEFINE_TRIVIAL_CLEANUP_FUNC(Bridge*, bridge_free); | |
207 | #define _cleanup_bridge_free_ _cleanup_(bridge_freep) | |
208 | ||
209 | int bridge_get(Manager *manager, const char *name, Bridge **ret); | |
210 | int bridge_set_link(Manager *m, Link *link); | |
211 | int bridge_join(Bridge *bridge, Link *link, sd_rtnl_message_handler_t cb); | |
212 | ||
f579559b TG |
213 | /* Network */ |
214 | ||
215 | int network_load(Manager *manager); | |
f579559b TG |
216 | |
217 | void network_free(Network *network); | |
218 | ||
219 | DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free); | |
220 | #define _cleanup_network_free_ _cleanup_(network_freep) | |
221 | ||
222 | int network_get(Manager *manager, struct udev_device *device, Network **ret); | |
223 | int network_apply(Manager *manager, Network *network, Link *link); | |
224 | ||
02b59d57 TG |
225 | int config_parse_bridge(const char *unit, const char *filename, unsigned line, |
226 | const char *section, unsigned section_line, const char *lvalue, | |
227 | int ltype, const char *rvalue, void *data, void *userdata); | |
228 | ||
229 | /* gperf */ | |
230 | ||
f579559b TG |
231 | const struct ConfigPerfItem* network_gperf_lookup(const char *key, unsigned length); |
232 | ||
233 | /* Route */ | |
f048a16b TG |
234 | int route_new_static(Network *network, unsigned section, Route **ret); |
235 | int route_new_dynamic(Route **ret); | |
f579559b | 236 | void route_free(Route *route); |
f882c247 | 237 | int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback); |
f579559b TG |
238 | |
239 | DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free); | |
240 | #define _cleanup_route_free_ _cleanup_(route_freep) | |
241 | ||
242 | int config_parse_gateway(const char *unit, const char *filename, unsigned line, | |
71a61510 TG |
243 | const char *section, unsigned section_line, const char *lvalue, |
244 | int ltype, const char *rvalue, void *data, void *userdata); | |
f579559b | 245 | |
6ae115c1 TG |
246 | int config_parse_destination(const char *unit, const char *filename, unsigned line, |
247 | const char *section, unsigned section_line, const char *lvalue, | |
248 | int ltype, const char *rvalue, void *data, void *userdata); | |
249 | ||
f579559b | 250 | /* Address */ |
f048a16b TG |
251 | int address_new_static(Network *network, unsigned section, Address **ret); |
252 | int address_new_dynamic(Address **ret); | |
f579559b | 253 | void address_free(Address *address); |
f882c247 | 254 | int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback); |
407fe036 | 255 | int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callback); |
f579559b TG |
256 | |
257 | DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free); | |
258 | #define _cleanup_address_free_ _cleanup_(address_freep) | |
259 | ||
260 | int config_parse_address(const char *unit, const char *filename, unsigned line, | |
71a61510 TG |
261 | const char *section, unsigned section_line, const char *lvalue, |
262 | int ltype, const char *rvalue, void *data, void *userdata); | |
f579559b | 263 | |
6ae115c1 TG |
264 | int config_parse_label(const char *unit, const char *filename, unsigned line, |
265 | const char *section, unsigned section_line, const char *lvalue, | |
266 | int ltype, const char *rvalue, void *data, void *userdata); | |
267 | ||
f579559b TG |
268 | /* Link */ |
269 | ||
270 | int link_new(Manager *manager, struct udev_device *device, Link **ret); | |
271 | void link_free(Link *link); | |
272 | int link_add(Manager *manager, struct udev_device *device); | |
f882c247 | 273 | int link_configure(Link *link); |
f579559b | 274 | |
22936833 | 275 | int link_update(Link *link, sd_rtnl_message *message); |
dd3efc09 | 276 | |
f579559b TG |
277 | DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free); |
278 | #define _cleanup_link_free_ _cleanup_(link_freep) |