]>
Commit | Line | Data |
---|---|---|
5c1d3fc9 UTL |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright (C) 2014 Axis Communications AB. All rights reserved. | |
5707940e | 5 | Copyright (C) 2015 Tom Gundersen |
5c1d3fc9 UTL |
6 | |
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
07630cea | 21 | #include <arpa/inet.h> |
5c1d3fc9 | 22 | #include <errno.h> |
5c1d3fc9 | 23 | #include <stdio.h> |
07630cea LP |
24 | #include <stdlib.h> |
25 | #include <string.h> | |
26 | ||
27 | #include "sd-ipv4acd.h" | |
28 | #include "sd-ipv4ll.h" | |
5c1d3fc9 | 29 | |
b5efdb8a | 30 | #include "alloc-util.h" |
e3dca008 | 31 | #include "event-util.h" |
129dc1b4 | 32 | #include "in-addr-util.h" |
5c1d3fc9 | 33 | #include "list.h" |
3df3e884 | 34 | #include "random-util.h" |
e3dca008 TG |
35 | #include "refcnt.h" |
36 | #include "siphash24.h" | |
37 | #include "sparse-endian.h" | |
38 | #include "util.h" | |
5c1d3fc9 | 39 | |
5c1d3fc9 UTL |
40 | #define IPV4LL_NETWORK 0xA9FE0000L |
41 | #define IPV4LL_NETMASK 0xFFFF0000L | |
42 | ||
b45e4eb6 TG |
43 | #define IPV4LL_DONT_DESTROY(ll) \ |
44 | _cleanup_ipv4ll_unref_ _unused_ sd_ipv4ll *_dont_destroy_##ll = sd_ipv4ll_ref(ll) | |
45 | ||
5c1d3fc9 | 46 | struct sd_ipv4ll { |
9c8e3101 | 47 | unsigned n_ref; |
56cd007a | 48 | |
e3dca008 TG |
49 | sd_ipv4acd *acd; |
50 | be32_t address; /* the address pushed to ACD */ | |
b5db00e5 UTL |
51 | struct random_data *random_data; |
52 | char *random_data_state; | |
e3dca008 | 53 | |
5c1d3fc9 UTL |
54 | /* External */ |
55 | be32_t claimed_address; | |
5c1d3fc9 UTL |
56 | sd_ipv4ll_cb_t cb; |
57 | void* userdata; | |
58 | }; | |
59 | ||
b45e4eb6 TG |
60 | sd_ipv4ll *sd_ipv4ll_ref(sd_ipv4ll *ll) { |
61 | if (!ll) | |
62 | return NULL; | |
63 | ||
64 | assert(ll->n_ref >= 1); | |
65 | ll->n_ref++; | |
66 | ||
67 | return ll; | |
68 | } | |
69 | ||
70 | sd_ipv4ll *sd_ipv4ll_unref(sd_ipv4ll *ll) { | |
71 | if (!ll) | |
72 | return NULL; | |
73 | ||
74 | assert(ll->n_ref >= 1); | |
75 | ll->n_ref--; | |
76 | ||
77 | if (ll->n_ref > 0) | |
78 | return NULL; | |
79 | ||
e3dca008 | 80 | sd_ipv4acd_unref(ll->acd); |
b45e4eb6 TG |
81 | |
82 | free(ll->random_data); | |
83 | free(ll->random_data_state); | |
84 | free(ll); | |
85 | ||
86 | return NULL; | |
87 | } | |
88 | ||
89 | DEFINE_TRIVIAL_CLEANUP_FUNC(sd_ipv4ll*, sd_ipv4ll_unref); | |
90 | #define _cleanup_ipv4ll_unref_ _cleanup_(sd_ipv4ll_unrefp) | |
91 | ||
e3dca008 TG |
92 | static void ipv4ll_on_acd(sd_ipv4acd *ll, int event, void *userdata); |
93 | ||
b45e4eb6 TG |
94 | int sd_ipv4ll_new(sd_ipv4ll **ret) { |
95 | _cleanup_ipv4ll_unref_ sd_ipv4ll *ll = NULL; | |
e3dca008 | 96 | int r; |
b45e4eb6 TG |
97 | |
98 | assert_return(ret, -EINVAL); | |
99 | ||
100 | ll = new0(sd_ipv4ll, 1); | |
101 | if (!ll) | |
102 | return -ENOMEM; | |
103 | ||
0c28d288 LP |
104 | ll->n_ref = 1; |
105 | ||
e3dca008 TG |
106 | r = sd_ipv4acd_new(&ll->acd); |
107 | if (r < 0) | |
108 | return r; | |
109 | ||
110 | r = sd_ipv4acd_set_callback(ll->acd, ipv4ll_on_acd, ll); | |
111 | if (r < 0) | |
112 | return r; | |
113 | ||
b45e4eb6 TG |
114 | *ret = ll; |
115 | ll = NULL; | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
b45e4eb6 | 120 | int sd_ipv4ll_stop(sd_ipv4ll *ll) { |
94a355a1 | 121 | int r; |
5c1d3fc9 | 122 | |
e3dca008 | 123 | assert_return(ll, -EINVAL); |
94a355a1 | 124 | |
e3dca008 | 125 | r = sd_ipv4acd_stop(ll->acd); |
94a355a1 TG |
126 | if (r < 0) |
127 | return r; | |
128 | ||
94a355a1 | 129 | return 0; |
5c1d3fc9 UTL |
130 | } |
131 | ||
e3dca008 TG |
132 | int sd_ipv4ll_set_index(sd_ipv4ll *ll, int interface_index) { |
133 | assert_return(ll, -EINVAL); | |
5c1d3fc9 | 134 | |
e3dca008 | 135 | return sd_ipv4acd_set_index(ll->acd, interface_index); |
5c1d3fc9 UTL |
136 | } |
137 | ||
e3dca008 | 138 | #define HASH_KEY SD_ID128_MAKE(df,04,22,98,3f,ad,14,52,f9,87,2e,d1,9c,70,e2,f2) |
5c1d3fc9 | 139 | |
e3dca008 | 140 | int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) { |
b26f7e8e TG |
141 | int r; |
142 | ||
e3dca008 | 143 | assert_return(ll, -EINVAL); |
b26f7e8e | 144 | |
e3dca008 | 145 | if (!ll->random_data) { |
dbe81cbd | 146 | uint64_t seed; |
b26f7e8e | 147 | |
e3dca008 | 148 | /* If no random data is set, generate some from the MAC */ |
933f9cae | 149 | seed = siphash24(&addr->ether_addr_octet, ETH_ALEN, HASH_KEY.bytes); |
5c1d3fc9 | 150 | |
e3dca008 | 151 | assert_cc(sizeof(unsigned) <= 8); |
5c1d3fc9 | 152 | |
933f9cae | 153 | r = sd_ipv4ll_set_address_seed(ll, (unsigned) htole64(seed)); |
028e0b20 | 154 | if (r < 0) |
e3dca008 | 155 | return r; |
5c1d3fc9 UTL |
156 | } |
157 | ||
e3dca008 | 158 | return sd_ipv4acd_set_mac(ll->acd, addr); |
5c1d3fc9 UTL |
159 | } |
160 | ||
161 | int sd_ipv4ll_detach_event(sd_ipv4ll *ll) { | |
162 | assert_return(ll, -EINVAL); | |
163 | ||
e3dca008 | 164 | return sd_ipv4acd_detach_event(ll->acd); |
5c1d3fc9 UTL |
165 | } |
166 | ||
167 | int sd_ipv4ll_attach_event(sd_ipv4ll *ll, sd_event *event, int priority) { | |
168 | int r; | |
169 | ||
170 | assert_return(ll, -EINVAL); | |
5c1d3fc9 | 171 | |
e3dca008 TG |
172 | r = sd_ipv4acd_attach_event(ll->acd, event, priority); |
173 | if (r < 0) | |
174 | return r; | |
5c1d3fc9 UTL |
175 | |
176 | return 0; | |
177 | } | |
178 | ||
179 | int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_cb_t cb, void *userdata) { | |
180 | assert_return(ll, -EINVAL); | |
181 | ||
182 | ll->cb = cb; | |
183 | ll->userdata = userdata; | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | int sd_ipv4ll_get_address(sd_ipv4ll *ll, struct in_addr *address){ | |
189 | assert_return(ll, -EINVAL); | |
190 | assert_return(address, -EINVAL); | |
191 | ||
ece174c5 | 192 | if (ll->claimed_address == 0) |
5c1d3fc9 | 193 | return -ENOENT; |
5c1d3fc9 UTL |
194 | |
195 | address->s_addr = ll->claimed_address; | |
e3dca008 | 196 | |
5c1d3fc9 UTL |
197 | return 0; |
198 | } | |
199 | ||
e3dca008 TG |
200 | int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, unsigned seed) { |
201 | _cleanup_free_ struct random_data *random_data = NULL; | |
202 | _cleanup_free_ char *random_data_state = NULL; | |
b5db00e5 UTL |
203 | int r; |
204 | ||
205 | assert_return(ll, -EINVAL); | |
d9bf4f8c | 206 | |
e3dca008 TG |
207 | random_data = new0(struct random_data, 1); |
208 | if (!random_data) | |
209 | return -ENOMEM; | |
b5db00e5 | 210 | |
e3dca008 TG |
211 | random_data_state = new0(char, 128); |
212 | if (!random_data_state) | |
213 | return -ENOMEM; | |
b5db00e5 | 214 | |
e3dca008 TG |
215 | r = initstate_r(seed, random_data_state, 128, random_data); |
216 | if (r < 0) | |
217 | return r; | |
b5db00e5 | 218 | |
e3dca008 TG |
219 | free(ll->random_data); |
220 | ll->random_data = random_data; | |
221 | random_data = NULL; | |
b5db00e5 | 222 | |
e3dca008 TG |
223 | free(ll->random_data_state); |
224 | ll->random_data_state = random_data_state; | |
225 | random_data_state = NULL; | |
b5db00e5 | 226 | |
e3dca008 | 227 | return 0; |
b5db00e5 UTL |
228 | } |
229 | ||
04c01369 | 230 | int sd_ipv4ll_is_running(sd_ipv4ll *ll) { |
75677581 | 231 | assert_return(ll, false); |
aba496a5 | 232 | |
e3dca008 | 233 | return sd_ipv4acd_is_running(ll->acd); |
aba496a5 UTL |
234 | } |
235 | ||
129dc1b4 TG |
236 | static bool ipv4ll_address_is_valid(const struct in_addr *address) { |
237 | uint32_t addr; | |
238 | ||
239 | assert(address); | |
240 | ||
241 | if (!in_addr_is_link_local(AF_INET, (const union in_addr_union *) address)) | |
242 | return false; | |
243 | ||
244 | addr = be32toh(address->s_addr); | |
245 | ||
246 | if ((addr & 0x0000FF00) == 0x0000 || | |
247 | (addr & 0x0000FF00) == 0xFF00) | |
248 | return false; | |
249 | ||
250 | return true; | |
251 | } | |
252 | ||
253 | int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address) { | |
254 | int r; | |
255 | ||
256 | assert_return(ll, -EINVAL); | |
257 | assert_return(address, -EINVAL); | |
258 | assert_return(ipv4ll_address_is_valid(address), -EINVAL); | |
259 | ||
260 | r = sd_ipv4acd_set_address(ll->acd, address); | |
261 | if (r < 0) | |
262 | return r; | |
263 | ||
264 | ll->address = address->s_addr; | |
265 | ||
266 | return 0; | |
267 | } | |
268 | ||
e3dca008 TG |
269 | static int ipv4ll_pick_address(sd_ipv4ll *ll) { |
270 | struct in_addr in_addr; | |
271 | be32_t addr; | |
5c1d3fc9 | 272 | int r; |
e3dca008 | 273 | int32_t random; |
5c1d3fc9 | 274 | |
e3dca008 TG |
275 | assert(ll); |
276 | assert(ll->random_data); | |
4d978a46 | 277 | |
e3dca008 TG |
278 | do { |
279 | r = random_r(ll->random_data, &random); | |
280 | if (r < 0) | |
281 | return r; | |
282 | addr = htonl((random & 0x0000FFFF) | IPV4LL_NETWORK); | |
283 | } while (addr == ll->address || | |
e3dca008 TG |
284 | (ntohl(addr) & 0x0000FF00) == 0x0000 || |
285 | (ntohl(addr) & 0x0000FF00) == 0xFF00); | |
5c1d3fc9 | 286 | |
e3dca008 | 287 | in_addr.s_addr = addr; |
5c1d3fc9 | 288 | |
129dc1b4 | 289 | r = sd_ipv4ll_set_address(ll, &in_addr); |
e3dca008 TG |
290 | if (r < 0) |
291 | return r; | |
b5db00e5 | 292 | |
e3dca008 TG |
293 | return 0; |
294 | } | |
295 | ||
296 | int sd_ipv4ll_start(sd_ipv4ll *ll) { | |
297 | int r; | |
298 | ||
299 | assert_return(ll, -EINVAL); | |
300 | assert_return(ll->random_data, -EINVAL); | |
b5db00e5 UTL |
301 | |
302 | if (ll->address == 0) { | |
e3dca008 | 303 | r = ipv4ll_pick_address(ll); |
b5db00e5 | 304 | if (r < 0) |
e3dca008 | 305 | return r; |
b5db00e5 | 306 | } |
5c1d3fc9 | 307 | |
e3dca008 | 308 | r = sd_ipv4acd_start(ll->acd); |
996d1697 | 309 | if (r < 0) |
e3dca008 | 310 | return r; |
996d1697 | 311 | |
e3dca008 TG |
312 | return 0; |
313 | } | |
996d1697 | 314 | |
e3dca008 TG |
315 | static void ipv4ll_client_notify(sd_ipv4ll *ll, int event) { |
316 | assert(ll); | |
5c1d3fc9 | 317 | |
e3dca008 TG |
318 | if (ll->cb) |
319 | ll->cb(ll, event, ll->userdata); | |
320 | } | |
5c1d3fc9 | 321 | |
e3dca008 TG |
322 | void ipv4ll_on_acd(sd_ipv4acd *acd, int event, void *userdata) { |
323 | sd_ipv4ll *ll = userdata; | |
324 | IPV4LL_DONT_DESTROY(ll); | |
325 | int r; | |
5c1d3fc9 | 326 | |
e3dca008 TG |
327 | assert(acd); |
328 | assert(ll); | |
9021bb9f | 329 | |
e3dca008 | 330 | switch (event) { |
2237aa02 | 331 | case SD_IPV4ACD_EVENT_STOP: |
be19c5b5 | 332 | ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_STOP); |
5c1d3fc9 | 333 | |
e3dca008 TG |
334 | ll->claimed_address = 0; |
335 | ||
336 | break; | |
2237aa02 | 337 | case SD_IPV4ACD_EVENT_BIND: |
e3dca008 | 338 | ll->claimed_address = ll->address; |
be19c5b5 | 339 | ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_BIND); |
e3dca008 TG |
340 | |
341 | break; | |
2237aa02 | 342 | case SD_IPV4ACD_EVENT_CONFLICT: |
e3dca008 TG |
343 | /* if an address was already bound we must call up to the |
344 | user to handle this, otherwise we just try again */ | |
345 | if (ll->claimed_address != 0) { | |
be19c5b5 | 346 | ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_CONFLICT); |
e3dca008 TG |
347 | |
348 | ll->claimed_address = 0; | |
349 | } else { | |
350 | r = ipv4ll_pick_address(ll); | |
351 | if (r < 0) | |
352 | goto error; | |
353 | ||
354 | r = sd_ipv4acd_start(ll->acd); | |
355 | if (r < 0) | |
356 | goto error; | |
357 | } | |
358 | ||
359 | break; | |
360 | default: | |
361 | assert_not_reached("Invalid IPv4ACD event."); | |
362 | } | |
363 | ||
364 | return; | |
365 | ||
366 | error: | |
be19c5b5 | 367 | ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_STOP); |
5c1d3fc9 | 368 | } |