]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-ipv4ll.c
sd-network: IPv4 link-local support [v2]
[thirdparty/systemd.git] / src / libsystemd-network / sd-ipv4ll.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2014 Axis Communications AB. All rights reserved.
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <arpa/inet.h>
25
26 #include "util.h"
27 #include "list.h"
28
29 #include "ipv4ll-internal.h"
30 #include "sd-ipv4ll.h"
31
32 /* Constants from the RFC */
33 #define PROBE_WAIT 1
34 #define PROBE_NUM 3
35 #define PROBE_MIN 1
36 #define PROBE_MAX 2
37 #define ANNOUNCE_WAIT 2
38 #define ANNOUNCE_NUM 2
39 #define ANNOUNCE_INTERVAL 2
40 #define MAX_CONFLICTS 10
41 #define RATE_LIMIT_INTERVAL 60
42 #define DEFEND_INTERVAL 10
43
44 #define IPV4LL_NETWORK 0xA9FE0000L
45 #define IPV4LL_NETMASK 0xFFFF0000L
46
47 typedef enum IPv4LLTrigger{
48 IPV4LL_TRIGGER_NULL,
49 IPV4LL_TRIGGER_PACKET,
50 IPV4LL_TRIGGER_TIMEOUT,
51 _IPV4LL_TRIGGER_MAX,
52 _IPV4LL_TRIGGER_INVALID = -1
53 } IPv4LLTrigger;
54
55 typedef enum IPv4LLState {
56 IPV4LL_STATE_INIT,
57 IPV4LL_STATE_WAITING_PROBE,
58 IPV4LL_STATE_PROBING,
59 IPV4LL_STATE_WAITING_ANNOUNCE,
60 IPV4LL_STATE_ANNOUNCING,
61 IPV4LL_STATE_RUNNING,
62 _IPV4LL_STATE_MAX,
63 _IPV4LL_STATE_INVALID = -1
64 } IPv4LLState;
65
66 struct sd_ipv4ll {
67 IPv4LLState state;
68 int index;
69 int fd;
70 union sockaddr_union link;
71 int iteration;
72 int conflict;
73 sd_event_source *receive_message;
74 sd_event_source *timer;
75 usec_t next_wakeup;
76 usec_t defend_window;
77 int next_wakeup_valid;
78 be32_t address;
79 /* External */
80 be32_t claimed_address;
81 struct ether_addr mac_addr;
82 sd_event *event;
83 int event_priority;
84 sd_ipv4ll_cb_t cb;
85 void* userdata;
86 };
87
88 static void ipv4ll_run_state_machine(sd_ipv4ll *ll, IPv4LLTrigger trigger, void *trigger_data);
89
90 static void ipv4ll_set_state(sd_ipv4ll *ll, IPv4LLState st, int reset_counter) {
91
92 assert(ll);
93 assert(st < _IPV4LL_STATE_MAX);
94
95 if (st == ll->state && !reset_counter) {
96 ll->iteration++;
97 } else {
98 ll->state = st;
99 ll->iteration = 0;
100 }
101 }
102
103 static int ipv4ll_client_notify(sd_ipv4ll *ll, int event) {
104 assert(ll);
105
106 if (ll->cb)
107 ll->cb(ll, event, ll->userdata);
108
109 return 0;
110 }
111
112 static int ipv4ll_stop(sd_ipv4ll *ll, int event) {
113 assert(ll);
114
115 ll->receive_message = sd_event_source_unref(ll->receive_message);
116 if (ll->fd >= 0)
117 close_nointr_nofail(ll->fd);
118 ll->fd = -1;
119
120 ll->timer = sd_event_source_unref(ll->timer);
121
122 ipv4ll_client_notify(ll, event);
123
124 ll->claimed_address = 0;
125
126 ipv4ll_set_state (ll, IPV4LL_STATE_INIT, 1);
127
128 log_ipv4ll(ll, "STOPPED");
129
130 return 0;
131 }
132
133 static be32_t ipv4ll_pick_address(sd_ipv4ll *ll) {
134 be32_t addr;
135
136 assert(ll);
137
138 if (ll->address) {
139 do {
140 uint32_t r = random_u32() & 0x0000FFFF;
141 addr = htonl(IPV4LL_NETWORK | r);
142 } while (addr == ll->address ||
143 (ntohl(addr) & IPV4LL_NETMASK) != IPV4LL_NETWORK ||
144 (ntohl(addr) & 0x0000FF00) == 0x0000 ||
145 (ntohl(addr) & 0x0000FF00) == 0xFF00);
146 } else {
147 uint32_t a = 1;
148 int i;
149
150 for (i = 0; i < ETH_ALEN; i++)
151 a += ll->mac_addr.ether_addr_octet[i]*i;
152 a = (a % 0xFE00) + 0x0100;
153 addr = htonl(IPV4LL_NETWORK | (uint32_t) a);
154 }
155
156 return addr;
157 }
158
159 static int ipv4ll_timer(sd_event_source *s, uint64_t usec, void *userdata) {
160 sd_ipv4ll *ll = (sd_ipv4ll*)userdata;
161
162 assert(ll);
163
164 ll->next_wakeup_valid = 0;
165 ipv4ll_run_state_machine(ll, IPV4LL_TRIGGER_TIMEOUT, NULL);
166
167 return 0;
168 }
169
170 static void ipv4ll_set_next_wakeup (sd_ipv4ll *ll, int sec, int random_sec) {
171 usec_t next_timeout = 0;
172 usec_t time_now = 0;
173
174 assert(sec >= 0);
175 assert(random_sec >= 0);
176 assert(ll);
177
178 next_timeout = sec * USEC_PER_SEC;
179
180 if (random_sec)
181 next_timeout += random_u32() % (random_sec * USEC_PER_SEC);
182
183 if (sd_event_get_now_monotonic(ll->event, &time_now) < 0)
184 time_now = now(CLOCK_MONOTONIC);
185
186 ll->next_wakeup = time_now + next_timeout;
187 ll->next_wakeup_valid = 1;
188 }
189
190 static bool ipv4ll_arp_conflict (sd_ipv4ll *ll, struct ether_arp *arp) {
191 assert(ll);
192 assert(arp);
193
194 if (memcmp(arp->arp_spa, &ll->address, sizeof(ll->address)) == 0 &&
195 memcmp(arp->arp_sha, &ll->mac_addr, ETH_ALEN) != 0)
196 return true;
197
198 return false;
199 }
200
201 static bool ipv4ll_arp_probe_conflict (sd_ipv4ll *ll, struct ether_arp *arp) {
202 assert(ll);
203 assert(arp);
204
205 if (ipv4ll_arp_conflict(ll, arp))
206 return true;
207
208 if (memcmp(arp->arp_tpa, &ll->address, sizeof(ll->address)) == 0 &&
209 memcmp(arp->arp_sha, &ll->mac_addr, ETH_ALEN))
210 return true;
211
212 return false;
213 }
214
215 static void ipv4ll_run_state_machine(sd_ipv4ll *ll, IPv4LLTrigger trigger, void *trigger_data) {
216 struct ether_arp out_packet;
217 int out_packet_ready = 0;
218 int r = 0;
219
220 assert(ll);
221 assert(trigger < _IPV4LL_TRIGGER_MAX);
222
223 if (ll->state == IPV4LL_STATE_INIT) {
224
225 log_ipv4ll(ll, "PROBE");
226 ipv4ll_set_state(ll, IPV4LL_STATE_WAITING_PROBE, 1);
227 ipv4ll_set_next_wakeup(ll, 0, PROBE_WAIT);
228
229 } else if ((ll->state == IPV4LL_STATE_WAITING_PROBE && trigger == IPV4LL_TRIGGER_TIMEOUT) ||
230 (ll->state == IPV4LL_STATE_PROBING && trigger == IPV4LL_TRIGGER_TIMEOUT && ll->iteration < PROBE_NUM-2)) {
231
232 /* Send a probe */
233 arp_packet_probe(&out_packet, ll->address, &ll->mac_addr);
234 out_packet_ready = 1;
235 ipv4ll_set_state(ll, IPV4LL_STATE_PROBING, 0);
236
237 ipv4ll_set_next_wakeup(ll, PROBE_MIN, (PROBE_MAX-PROBE_MIN));
238
239 } else if (ll->state == IPV4LL_STATE_PROBING && trigger == IPV4LL_TRIGGER_TIMEOUT && ll->iteration >= PROBE_NUM-2) {
240
241 /* Send the last probe */
242 arp_packet_probe(&out_packet, ll->address, &ll->mac_addr);
243 out_packet_ready = 1;
244 ipv4ll_set_state(ll, IPV4LL_STATE_WAITING_ANNOUNCE, 1);
245
246 ipv4ll_set_next_wakeup(ll, ANNOUNCE_WAIT, 0);
247
248 } else if ((ll->state == IPV4LL_STATE_WAITING_ANNOUNCE && trigger == IPV4LL_TRIGGER_TIMEOUT) ||
249 (ll->state == IPV4LL_STATE_ANNOUNCING && trigger == IPV4LL_TRIGGER_TIMEOUT && ll->iteration < ANNOUNCE_NUM-1)) {
250
251 /* Send announcement packet */
252 arp_packet_announcement(&out_packet, ll->address, &ll->mac_addr);
253 out_packet_ready = 1;
254 ipv4ll_set_state(ll, IPV4LL_STATE_ANNOUNCING, 0);
255
256 ipv4ll_set_next_wakeup(ll, ANNOUNCE_INTERVAL, 0);
257
258 if (ll->iteration == 0) {
259 log_ipv4ll(ll, "ANNOUNCE");
260 ll->claimed_address = ll->address;
261 r = ipv4ll_client_notify(ll, IPV4LL_EVENT_BIND);
262 ll->conflict = 0;
263 }
264
265 } else if ((ll->state == IPV4LL_STATE_ANNOUNCING && trigger == IPV4LL_TRIGGER_TIMEOUT &&
266 ll->iteration >= ANNOUNCE_NUM-1)) {
267
268 ipv4ll_set_state(ll, IPV4LL_STATE_RUNNING, 0);
269 ll->next_wakeup_valid = 0;
270
271 } else if (trigger == IPV4LL_TRIGGER_PACKET) {
272
273 int conflicted = 0;
274 usec_t time_now;
275 struct ether_arp* in_packet = (struct ether_arp*)trigger_data;
276
277 assert(in_packet);
278
279 if (IN_SET(ll->state, IPV4LL_STATE_ANNOUNCING, IPV4LL_STATE_RUNNING)) {
280
281 if (ipv4ll_arp_conflict(ll, in_packet)) {
282
283 r = sd_event_get_now_monotonic(ll->event, &time_now);
284 if (r < 0)
285 goto out;
286
287 /* Defend address */
288 if (time_now > ll->defend_window) {
289 ll->defend_window = time_now + DEFEND_INTERVAL * USEC_PER_SEC;
290 arp_packet_announcement(&out_packet, ll->address, &ll->mac_addr);
291 out_packet_ready = 1;
292 } else
293 conflicted = 1;
294 }
295
296 } else if (IN_SET(ll->state, IPV4LL_STATE_WAITING_PROBE,
297 IPV4LL_STATE_PROBING,
298 IPV4LL_STATE_WAITING_ANNOUNCE)) {
299
300 conflicted = ipv4ll_arp_probe_conflict(ll, in_packet);
301 }
302
303 if (conflicted) {
304 log_ipv4ll(ll, "CONFLICT");
305 r = ipv4ll_client_notify(ll, IPV4LL_EVENT_CONFLICT);
306 ll->claimed_address = 0;
307
308 /* Pick a new address */
309 ll->address = ipv4ll_pick_address(ll);
310 ll->conflict++;
311 ll->defend_window = 0;
312 ipv4ll_set_state(ll, IPV4LL_STATE_WAITING_PROBE, 1);
313
314 if (ll->conflict >= MAX_CONFLICTS) {
315 log_ipv4ll(ll, "MAX_CONFLICTS");
316 ipv4ll_set_next_wakeup(ll, RATE_LIMIT_INTERVAL, PROBE_WAIT);
317 } else
318 ipv4ll_set_next_wakeup(ll, 0, PROBE_WAIT);
319
320 }
321 }
322
323 if (out_packet_ready) {
324 r = arp_network_send_raw_socket(ll->fd, &ll->link, &out_packet);
325 if (r < 0) {
326 log_ipv4ll(ll, "failed to send arp packet out");
327 goto out;
328 }
329 }
330
331 if (ll->next_wakeup_valid) {
332 ll->timer = sd_event_source_unref(ll->timer);
333 r = sd_event_add_monotonic(ll->event, &ll->timer,
334 ll->next_wakeup, 0, ipv4ll_timer, ll);
335 if (r < 0)
336 goto out;
337
338 r = sd_event_source_set_priority(ll->timer, ll->event_priority);
339 if (r < 0)
340 goto out;
341 }
342
343 out:
344 if (r < 0)
345 ipv4ll_stop(ll, r);
346 }
347
348 static int ipv4ll_receive_message(sd_event_source *s, int fd,
349 uint32_t revents, void *userdata) {
350 int r;
351 struct ether_arp arp;
352 sd_ipv4ll *ll = (sd_ipv4ll*)userdata;
353
354 assert(ll);
355
356 r = read(fd, &arp, sizeof(struct ether_arp));
357 if (r < (int) sizeof(struct ether_arp))
358 return 0;
359
360 r = arp_packet_verify_headers(&arp);
361 if (r < 0)
362 return 0;
363
364 ipv4ll_run_state_machine(ll, IPV4LL_TRIGGER_PACKET, &arp);
365
366 return 0;
367 }
368
369 int sd_ipv4ll_set_index(sd_ipv4ll *ll, int interface_index) {
370 assert_return(ll, -EINVAL);
371 assert_return(interface_index >= -1, -EINVAL);
372 assert_return(ll->state == IPV4LL_STATE_INIT, -EBUSY);
373
374 ll->index = interface_index;
375
376 return 0;
377 }
378
379 int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) {
380 assert_return(ll, -EINVAL);
381 assert_return(ll->state == IPV4LL_STATE_INIT, -EBUSY);
382
383 memcpy(&ll->mac_addr.ether_addr_octet, addr, ETH_ALEN);
384
385 return 0;
386 }
387
388 int sd_ipv4ll_detach_event(sd_ipv4ll *ll) {
389 assert_return(ll, -EINVAL);
390
391 ll->event = sd_event_unref(ll->event);
392
393 return 0;
394 }
395
396 int sd_ipv4ll_attach_event(sd_ipv4ll *ll, sd_event *event, int priority) {
397 int r;
398
399 assert_return(ll, -EINVAL);
400 assert_return(!ll->event, -EBUSY);
401
402 if (event)
403 ll->event = sd_event_ref(event);
404 else {
405 r = sd_event_default(&ll->event);
406 if (r < 0) {
407 ipv4ll_stop(ll, IPV4LL_EVENT_STOP);
408 return r;
409 }
410 }
411
412 ll->event_priority = priority;
413
414 return 0;
415 }
416
417 int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_cb_t cb, void *userdata) {
418 assert_return(ll, -EINVAL);
419
420 ll->cb = cb;
421 ll->userdata = userdata;
422
423 return 0;
424 }
425
426 int sd_ipv4ll_get_address(sd_ipv4ll *ll, struct in_addr *address){
427 assert_return(ll, -EINVAL);
428 assert_return(address, -EINVAL);
429
430 if (ll->claimed_address == 0) {
431 return -ENOENT;
432 }
433
434 address->s_addr = ll->claimed_address;
435 return 0;
436 }
437
438 int sd_ipv4ll_start (sd_ipv4ll *ll) {
439 int r;
440
441 assert_return(ll, -EINVAL);
442 assert_return(ll->event, -EINVAL);
443 assert_return(ll->index > 0, -EINVAL);
444 assert_return(ll->state == IPV4LL_STATE_INIT, -EBUSY);
445
446 r = arp_network_bind_raw_socket(ll->index, &ll->link);
447
448 if (r < 0)
449 goto out;
450
451 ll->fd = r;
452 ll->conflict = 0;
453 ll->defend_window = 0;
454 ll->claimed_address = 0;
455
456 if (ll->address == 0)
457 ll->address = ipv4ll_pick_address(ll);
458
459 ipv4ll_set_state (ll, IPV4LL_STATE_INIT, 1);
460
461 r = sd_event_add_io(ll->event, &ll->receive_message, ll->fd,
462 EPOLLIN, ipv4ll_receive_message, ll);
463 if (r < 0)
464 goto out;
465
466 r = sd_event_source_set_priority(ll->receive_message, ll->event_priority);
467 if (r < 0)
468 goto out;
469
470 r = sd_event_add_monotonic(ll->event, &ll->timer, now(CLOCK_MONOTONIC), 0,
471 ipv4ll_timer, ll);
472
473 if (r < 0)
474 goto out;
475
476 r = sd_event_source_set_priority(ll->timer, ll->event_priority);
477
478 out:
479 if (r < 0)
480 ipv4ll_stop(ll, IPV4LL_EVENT_STOP);
481
482 return 0;
483 }
484
485 int sd_ipv4ll_stop(sd_ipv4ll *ll) {
486 return ipv4ll_stop(ll, IPV4LL_EVENT_STOP);
487 }
488
489 void sd_ipv4ll_free (sd_ipv4ll *ll) {
490 if (!ll)
491 return;
492
493 sd_ipv4ll_stop(ll);
494 sd_ipv4ll_detach_event(ll);
495
496 free(ll);
497 }
498
499 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_ipv4ll*, sd_ipv4ll_free);
500 #define _cleanup_ipv4ll_free_ _cleanup_(sd_ipv4ll_freep)
501
502 int sd_ipv4ll_new(sd_ipv4ll **ret) {
503 _cleanup_ipv4ll_free_ sd_ipv4ll *ll = NULL;
504
505 assert_return(ret, -EINVAL);
506
507 ll = new0(sd_ipv4ll, 1);
508 if (!ll)
509 return -ENOMEM;
510
511 ll->state = IPV4LL_STATE_INIT;
512 ll->index = -1;
513 ll->fd = -1;
514
515 *ret = ll;
516 ll = NULL;
517
518 return 0;
519 }