Lines Matching refs:ll
54 #define log_ipv4acd_full(ll, level, error, fmt, ...) log_internal(level, error, __FILE__, __LINE__, __func__, "ACD: " fmt, ##__VA_ARGS__)
56 #define log_ipv4acd_debug(ll, ...) log_ipv4acd_full(ll, LOG_DEBUG, 0, ##__VA_ARGS__)
57 #define log_ipv4acd_info(ll, ...) log_ipv4acd_full(ll, LOG_INFO, 0, ##__VA_ARGS__)
58 #define log_ipv4acd_notice(ll, ...) log_ipv4acd_full(ll, LOG_NOTICE, 0, ##__VA_ARGS__)
59 #define log_ipv4acd_warning(ll, ...) log_ipv4acd_full(ll, LOG_WARNING, 0, ##__VA_ARGS__)
60 #define log_ipv4acd_error(ll, ...) log_ipv4acd_full(ll, LOG_ERR, 0, ##__VA_ARGS__)
62 #define log_ipv4acd_debug_errno(ll, error, ...) log_ipv4acd_full(ll, LOG_DEBUG, error, ##__VA_ARGS__)
63 #define log_ipv4acd_info_errno(ll, error, ...) log_ipv4acd_full(ll, LOG_INFO, error, ##__VA_ARGS__)
64 #define log_ipv4acd_notice_errno(ll, error, ...) log_ipv4acd_full(ll, LOG_NOTICE, error, ##__VA_ARGS__)
65 #define log_ipv4acd_warning_errno(ll, error, ...) log_ipv4acd_full(ll, LOG_WARNING, error, ##__VA_ARGS__)
66 #define log_ipv4acd_error_errno(ll, error, ...) log_ipv4acd_full(ll, LOG_ERR, error, ##__VA_ARGS__)
99 sd_ipv4acd *sd_ipv4acd_ref(sd_ipv4acd *ll) {
100 if (ll)
101 assert_se(REFCNT_INC(ll->n_ref) >= 2);
103 return ll;
106 sd_ipv4acd *sd_ipv4acd_unref(sd_ipv4acd *ll) {
107 if (!ll || REFCNT_DEC(ll->n_ref) > 0)
110 ll->receive_message = sd_event_source_unref(ll->receive_message);
111 ll->fd = safe_close(ll->fd);
113 ll->timer = sd_event_source_unref(ll->timer);
115 sd_ipv4acd_detach_event(ll);
117 free(ll);
123 _cleanup_(sd_ipv4acd_unrefp) sd_ipv4acd *ll = NULL;
127 ll = new0(sd_ipv4acd, 1);
128 if (!ll)
131 ll->n_ref = REFCNT_INIT;
132 ll->state = IPV4ACD_STATE_INIT;
133 ll->index = -1;
134 ll->fd = -1;
136 *ret = ll;
137 ll = NULL;
142 static void ipv4acd_set_state(sd_ipv4acd *ll, IPv4ACDState st, bool reset_counter) {
144 assert(ll);
147 if (st == ll->state && !reset_counter)
148 ll->iteration++;
150 ll->state = st;
151 ll->iteration = 0;
155 static void ipv4acd_client_notify(sd_ipv4acd *ll, int event) {
156 assert(ll);
158 if (ll->cb)
159 ll->cb(ll, event, ll->userdata);
162 static void ipv4acd_stop(sd_ipv4acd *ll) {
163 assert(ll);
165 ll->receive_message = sd_event_source_unref(ll->receive_message);
166 ll->fd = safe_close(ll->fd);
168 ll->timer = sd_event_source_unref(ll->timer);
170 log_ipv4acd_debug(ll, "STOPPED");
172 ipv4acd_set_state (ll, IPV4ACD_STATE_INIT, true);
175 int sd_ipv4acd_stop(sd_ipv4acd *ll) {
176 assert_return(ll, -EINVAL);
178 ipv4acd_stop(ll);
180 ipv4acd_client_notify(ll, SD_IPV4ACD_EVENT_STOP);
187 static int ipv4acd_set_next_wakeup(sd_ipv4acd *ll, int sec, int random_sec) {
195 assert(ll);
202 assert_se(sd_event_now(ll->event, clock_boottime_or_monotonic(), &time_now) >= 0);
204 r = sd_event_add_time(ll->event, &timer, clock_boottime_or_monotonic(),
205 time_now + next_timeout, 0, ipv4acd_on_timeout, ll);
209 r = sd_event_source_set_priority(timer, ll->event_priority);
217 ll->timer = sd_event_source_unref(ll->timer);
218 ll->timer = timer;
224 static bool ipv4acd_arp_conflict(sd_ipv4acd *ll, struct ether_arp *arp) {
225 assert(ll);
229 if (memcmp(arp->arp_spa, &ll->address, sizeof(ll->address)) == 0)
237 sd_ipv4acd *ll = userdata;
240 assert(ll);
242 switch (ll->state) {
245 ipv4acd_set_state(ll, IPV4ACD_STATE_WAITING_PROBE, true);
247 if (ll->conflict >= MAX_CONFLICTS) {
248 log_ipv4acd_notice(ll, "Max conflicts reached, delaying by %us", RATE_LIMIT_INTERVAL);
249 r = ipv4acd_set_next_wakeup(ll, RATE_LIMIT_INTERVAL, PROBE_WAIT);
253 ll->conflict = 0;
255 r = ipv4acd_set_next_wakeup(ll, 0, PROBE_WAIT);
264 r = arp_send_probe(ll->fd, ll->index, ll->address, &ll->mac_addr);
266 log_ipv4acd_error_errno(ll, r, "Failed to send ARP probe: %m");
270 union in_addr_union addr = { .in.s_addr = ll->address };
274 log_ipv4acd_debug(ll, "Probing %s", address);
277 if (ll->iteration < PROBE_NUM - 2) {
278 ipv4acd_set_state(ll, IPV4ACD_STATE_PROBING, false);
280 r = ipv4acd_set_next_wakeup(ll, PROBE_MIN, (PROBE_MAX-PROBE_MIN));
284 ipv4acd_set_state(ll, IPV4ACD_STATE_WAITING_ANNOUNCE, true);
286 r = ipv4acd_set_next_wakeup(ll, ANNOUNCE_WAIT, 0);
294 if (ll->iteration >= ANNOUNCE_NUM - 1) {
295 ipv4acd_set_state(ll, IPV4ACD_STATE_RUNNING, false);
301 r = arp_send_announcement(ll->fd, ll->index, ll->address, &ll->mac_addr);
303 log_ipv4acd_error_errno(ll, r, "Failed to send ARP announcement: %m");
306 log_ipv4acd_debug(ll, "ANNOUNCE");
308 ipv4acd_set_state(ll, IPV4ACD_STATE_ANNOUNCING, false);
310 r = ipv4acd_set_next_wakeup(ll, ANNOUNCE_INTERVAL, 0);
314 if (ll->iteration == 0) {
315 ll->conflict = 0;
316 ipv4acd_client_notify(ll, SD_IPV4ACD_EVENT_BIND);
326 sd_ipv4acd_stop(ll);
331 static void ipv4acd_on_conflict(sd_ipv4acd *ll) {
333 union in_addr_union addr = { .in.s_addr = ll->address };
336 assert(ll);
338 ll->conflict++;
342 log_ipv4acd_debug(ll, "Conflict on %s (%u)", address, ll->conflict);
344 ipv4acd_stop(ll);
346 ipv4acd_client_notify(ll, SD_IPV4ACD_EVENT_CONFLICT);
351 sd_ipv4acd *ll = userdata;
355 assert(ll);
362 switch (ll->state) {
365 if (ipv4acd_arp_conflict(ll, &packet)) {
368 assert_se(sd_event_now(ll->event, clock_boottime_or_monotonic(), &ts) >= 0);
371 if (ts > ll->defend_window) {
372 ll->defend_window = ts + DEFEND_INTERVAL * USEC_PER_SEC;
373 r = arp_send_announcement(ll->fd, ll->index, ll->address, &ll->mac_addr);
375 log_ipv4acd_error_errno(ll, r, "Failed to send ARP announcement: %m");
378 log_ipv4acd_debug(ll, "DEFEND");
381 ipv4acd_on_conflict(ll);
389 ipv4acd_on_conflict(ll);
398 sd_ipv4acd_stop(ll);
403 int sd_ipv4acd_set_index(sd_ipv4acd *ll, int interface_index) {
404 assert_return(ll, -EINVAL);
406 assert_return(ll->state == IPV4ACD_STATE_INIT, -EBUSY);
408 ll->index = interface_index;
413 int sd_ipv4acd_set_mac(sd_ipv4acd *ll, const struct ether_addr *addr) {
414 assert_return(ll, -EINVAL);
416 assert_return(ll->state == IPV4ACD_STATE_INIT, -EBUSY);
418 memcpy(&ll->mac_addr, addr, ETH_ALEN);
423 int sd_ipv4acd_detach_event(sd_ipv4acd *ll) {
424 assert_return(ll, -EINVAL);
426 ll->event = sd_event_unref(ll->event);
431 int sd_ipv4acd_attach_event(sd_ipv4acd *ll, sd_event *event, int priority) {
434 assert_return(ll, -EINVAL);
435 assert_return(!ll->event, -EBUSY);
438 ll->event = sd_event_ref(event);
440 r = sd_event_default(&ll->event);
445 ll->event_priority = priority;
450 int sd_ipv4acd_set_callback(sd_ipv4acd *ll, sd_ipv4acd_cb_t cb, void *userdata) {
451 assert_return(ll, -EINVAL);
453 ll->cb = cb;
454 ll->userdata = userdata;
459 int sd_ipv4acd_set_address(sd_ipv4acd *ll, const struct in_addr *address){
460 assert_return(ll, -EINVAL);
462 assert_return(ll->state == IPV4ACD_STATE_INIT, -EBUSY);
464 ll->address = address->s_addr;
469 int sd_ipv4acd_is_running(sd_ipv4acd *ll) {
470 assert_return(ll, false);
472 return ll->state != IPV4ACD_STATE_INIT;
485 int sd_ipv4acd_start(sd_ipv4acd *ll) {
488 assert_return(ll, -EINVAL);
489 assert_return(ll->event, -EINVAL);
490 assert_return(ll->index > 0, -EINVAL);
491 assert_return(ll->address != 0, -EINVAL);
492 assert_return(!ether_addr_is_nul(&ll->mac_addr), -EINVAL);
493 assert_return(ll->state == IPV4ACD_STATE_INIT, -EBUSY);
495 ll->defend_window = 0;
497 r = arp_network_bind_raw_socket(ll->index, ll->address, &ll->mac_addr);
501 ll->fd = safe_close(ll->fd);
502 ll->fd = r;
504 r = sd_event_add_io(ll->event, &ll->receive_message, ll->fd,
505 EPOLLIN, ipv4acd_on_packet, ll);
509 r = sd_event_source_set_priority(ll->receive_message, ll->event_priority);
513 r = sd_event_source_set_description(ll->receive_message, "ipv4acd-receive-message");
517 r = ipv4acd_set_next_wakeup(ll, 0, 0);
522 ipv4acd_stop(ll);