Lines Matching refs:ra
30 _cleanup_(sd_radv_unrefp) sd_radv *ra = NULL; in sd_radv_new()
34 ra = new(sd_radv, 1); in sd_radv_new()
35 if (!ra) in sd_radv_new()
38 *ra = (sd_radv) { in sd_radv_new()
44 *ret = TAKE_PTR(ra); in sd_radv_new()
49 int sd_radv_attach_event(sd_radv *ra, sd_event *event, int64_t priority) { in sd_radv_attach_event() argument
52 assert_return(ra, -EINVAL); in sd_radv_attach_event()
53 assert_return(!ra->event, -EBUSY); in sd_radv_attach_event()
56 ra->event = sd_event_ref(event); in sd_radv_attach_event()
58 r = sd_event_default(&ra->event); in sd_radv_attach_event()
63 ra->event_priority = priority; in sd_radv_attach_event()
68 int sd_radv_detach_event(sd_radv *ra) { in sd_radv_detach_event() argument
70 assert_return(ra, -EINVAL); in sd_radv_detach_event()
72 ra->event = sd_event_unref(ra->event); in sd_radv_detach_event()
76 sd_event *sd_radv_get_event(sd_radv *ra) { in sd_radv_get_event() argument
77 assert_return(ra, NULL); in sd_radv_get_event()
79 return ra->event; in sd_radv_get_event()
82 int sd_radv_is_running(sd_radv *ra) { in sd_radv_is_running() argument
83 assert_return(ra, false); in sd_radv_is_running()
85 return ra->state != RADV_STATE_IDLE; in sd_radv_is_running()
88 static void radv_reset(sd_radv *ra) { in radv_reset() argument
89 assert(ra); in radv_reset()
91 (void) event_source_disable(ra->timeout_event_source); in radv_reset()
93 ra->recv_event_source = sd_event_source_disable_unref(ra->recv_event_source); in radv_reset()
95 ra->ra_sent = 0; in radv_reset()
98 static sd_radv *radv_free(sd_radv *ra) { in radv_free() argument
99 if (!ra) in radv_free()
102 while (ra->prefixes) { in radv_free()
103 sd_radv_prefix *p = ra->prefixes; in radv_free()
105 LIST_REMOVE(prefix, ra->prefixes, p); in radv_free()
109 while (ra->route_prefixes) { in radv_free()
110 sd_radv_route_prefix *p = ra->route_prefixes; in radv_free()
112 LIST_REMOVE(prefix, ra->route_prefixes, p); in radv_free()
116 free(ra->rdnss); in radv_free()
117 free(ra->dnssl); in radv_free()
119 radv_reset(ra); in radv_free()
121 sd_event_source_unref(ra->timeout_event_source); in radv_free()
122 sd_radv_detach_event(ra); in radv_free()
124 ra->fd = safe_close(ra->fd); in radv_free()
125 free(ra->ifname); in radv_free()
127 return mfree(ra); in radv_free()
150 static int radv_send(sd_radv *ra, const struct in6_addr *dst, usec_t lifetime_usec) { in radv_send() argument
172 struct iovec iov[5 + ra->n_prefixes + ra->n_route_prefixes]; in radv_send()
181 assert(ra); in radv_send()
184 r = sd_event_now(ra->event, CLOCK_BOOTTIME, &time_now); in radv_send()
192 adv.nd_ra_curhoplimit = ra->hop_limit; in radv_send()
193 adv.nd_ra_flags_reserved = ra->flags; in radv_send()
200 if (!ether_addr_is_null(&ra->mac_addr)) { in radv_send()
201 opt_mac.slladdr = ra->mac_addr; in radv_send()
205 if (ra->mtu > 0) { in radv_send()
206 opt_mtu.nd_opt_mtu_mtu = htobe32(ra->mtu); in radv_send()
210 LIST_FOREACH(prefix, p, ra->prefixes) { in radv_send()
226 LIST_FOREACH(prefix, rt, ra->route_prefixes) { in radv_send()
233 if (ra->rdnss) in radv_send()
234 iov[msg.msg_iovlen++] = IOVEC_MAKE(ra->rdnss, ra->rdnss->length * 8); in radv_send()
236 if (ra->dnssl) in radv_send()
237 iov[msg.msg_iovlen++] = IOVEC_MAKE(ra->dnssl, ra->dnssl->length * 8); in radv_send()
239 if (sendmsg(ra->fd, &msg, 0) < 0) in radv_send()
246 sd_radv *ra = userdata; in radv_recv() local
255 assert(ra); in radv_recv()
256 assert(ra->event); in radv_recv()
263 … log_radv_errno(ra, buflen, "Failed to determine datagram size to read, ignoring: %m"); in radv_recv()
279 log_radv(ra, "Received RS from non-link-local address %s. Ignoring", addr); in radv_recv()
283 log_radv(ra, "Received RS with invalid hop limit. Ignoring."); in radv_recv()
287 … log_radv(ra, "Received invalid source address from ICMPv6 socket. Ignoring."); in radv_recv()
291 … log_radv_errno(ra, r, "Unexpected error receiving from ICMPv6 socket, ignoring: %m"); in radv_recv()
299 log_radv(ra, "Too short packet received, ignoring"); in radv_recv()
305 r = radv_send(ra, &src, ra->lifetime_usec); in radv_recv()
307 …log_radv_errno(ra, r, "Unable to send solicited Router Advertisement to %s, ignoring: %m", strnull… in radv_recv()
309 log_radv(ra, "Sent solicited Router Advertisement to %s", strnull(addr)); in radv_recv()
316 sd_radv *ra = userdata; in radv_timeout() local
320 assert(ra); in radv_timeout()
321 assert(ra->event); in radv_timeout()
322 assert(router_lifetime_is_valid(ra->lifetime_usec)); in radv_timeout()
324 r = sd_event_now(ra->event, CLOCK_BOOTTIME, &time_now); in radv_timeout()
328 r = radv_send(ra, NULL, ra->lifetime_usec); in radv_timeout()
330 log_radv_errno(ra, r, "Unable to send Router Advertisement: %m"); in radv_timeout()
333 if (ra->ra_sent < RADV_MAX_INITIAL_RTR_ADVERTISEMENTS) in radv_timeout()
340 if (ra->lifetime_usec > 0) in radv_timeout()
341 max_timeout = MIN(max_timeout, ra->lifetime_usec); in radv_timeout()
357 log_radv(ra, "Next Router Advertisement in %s", FORMAT_TIMESPAN(timeout, USEC_PER_SEC)); in radv_timeout()
359 r = event_reset_time(ra->event, &ra->timeout_event_source, in radv_timeout()
362 radv_timeout, ra, in radv_timeout()
363 ra->event_priority, "radv-timeout", true); in radv_timeout()
367 ra->ra_sent++; in radv_timeout()
372 sd_radv_stop(ra); in radv_timeout()
377 int sd_radv_stop(sd_radv *ra) { in sd_radv_stop() argument
380 if (!ra) in sd_radv_stop()
383 if (ra->state == RADV_STATE_IDLE) in sd_radv_stop()
386 log_radv(ra, "Stopping IPv6 Router Advertisement daemon"); in sd_radv_stop()
390 r = radv_send(ra, NULL, 0); in sd_radv_stop()
392 …log_radv_errno(ra, r, "Unable to send last Router Advertisement with router lifetime set to zero: … in sd_radv_stop()
394 radv_reset(ra); in sd_radv_stop()
395 ra->fd = safe_close(ra->fd); in sd_radv_stop()
396 ra->state = RADV_STATE_IDLE; in sd_radv_stop()
401 int sd_radv_start(sd_radv *ra) { in sd_radv_start() argument
404 assert_return(ra, -EINVAL); in sd_radv_start()
405 assert_return(ra->event, -EINVAL); in sd_radv_start()
406 assert_return(ra->ifindex > 0, -EINVAL); in sd_radv_start()
408 if (ra->state != RADV_STATE_IDLE) in sd_radv_start()
411 r = event_reset_time(ra->event, &ra->timeout_event_source, in sd_radv_start()
414 radv_timeout, ra, in sd_radv_start()
415 ra->event_priority, "radv-timeout", true); in sd_radv_start()
419 r = icmp6_bind_router_advertisement(ra->ifindex); in sd_radv_start()
423 ra->fd = r; in sd_radv_start()
425 r = sd_event_add_io(ra->event, &ra->recv_event_source, ra->fd, EPOLLIN, radv_recv, ra); in sd_radv_start()
429 r = sd_event_source_set_priority(ra->recv_event_source, ra->event_priority); in sd_radv_start()
433 (void) sd_event_source_set_description(ra->recv_event_source, "radv-receive-message"); in sd_radv_start()
435 ra->state = RADV_STATE_ADVERTISING; in sd_radv_start()
437 log_radv(ra, "Started IPv6 Router Advertisement daemon"); in sd_radv_start()
442 radv_reset(ra); in sd_radv_start()
447 int sd_radv_set_ifindex(sd_radv *ra, int ifindex) { in sd_radv_set_ifindex() argument
448 assert_return(ra, -EINVAL); in sd_radv_set_ifindex()
451 if (ra->state != RADV_STATE_IDLE) in sd_radv_set_ifindex()
454 ra->ifindex = ifindex; in sd_radv_set_ifindex()
459 int sd_radv_set_ifname(sd_radv *ra, const char *ifname) { in sd_radv_set_ifname() argument
460 assert_return(ra, -EINVAL); in sd_radv_set_ifname()
466 return free_and_strdup(&ra->ifname, ifname); in sd_radv_set_ifname()
469 int sd_radv_get_ifname(sd_radv *ra, const char **ret) { in sd_radv_get_ifname() argument
472 assert_return(ra, -EINVAL); in sd_radv_get_ifname()
474 r = get_ifname(ra->ifindex, &ra->ifname); in sd_radv_get_ifname()
479 *ret = ra->ifname; in sd_radv_get_ifname()
484 int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr) { in sd_radv_set_mac() argument
485 assert_return(ra, -EINVAL); in sd_radv_set_mac()
487 if (ra->state != RADV_STATE_IDLE) in sd_radv_set_mac()
491 ra->mac_addr = *mac_addr; in sd_radv_set_mac()
493 zero(ra->mac_addr); in sd_radv_set_mac()
498 int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu) { in sd_radv_set_mtu() argument
499 assert_return(ra, -EINVAL); in sd_radv_set_mtu()
502 ra->mtu = mtu; in sd_radv_set_mtu()
507 int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit) { in sd_radv_set_hop_limit() argument
508 assert_return(ra, -EINVAL); in sd_radv_set_hop_limit()
510 if (ra->state != RADV_STATE_IDLE) in sd_radv_set_hop_limit()
513 ra->hop_limit = hop_limit; in sd_radv_set_hop_limit()
518 int sd_radv_set_router_lifetime(sd_radv *ra, uint64_t lifetime_usec) { in sd_radv_set_router_lifetime() argument
519 assert_return(ra, -EINVAL); in sd_radv_set_router_lifetime()
521 if (ra->state != RADV_STATE_IDLE) in sd_radv_set_router_lifetime()
530 (ra->flags & (0x3 << 3)) != (SD_NDISC_PREFERENCE_MEDIUM << 3)) in sd_radv_set_router_lifetime()
533 ra->lifetime_usec = lifetime_usec; in sd_radv_set_router_lifetime()
538 int sd_radv_set_managed_information(sd_radv *ra, int managed) { in sd_radv_set_managed_information() argument
539 assert_return(ra, -EINVAL); in sd_radv_set_managed_information()
541 if (ra->state != RADV_STATE_IDLE) in sd_radv_set_managed_information()
544 SET_FLAG(ra->flags, ND_RA_FLAG_MANAGED, managed); in sd_radv_set_managed_information()
549 int sd_radv_set_other_information(sd_radv *ra, int other) { in sd_radv_set_other_information() argument
550 assert_return(ra, -EINVAL); in sd_radv_set_other_information()
552 if (ra->state != RADV_STATE_IDLE) in sd_radv_set_other_information()
555 SET_FLAG(ra->flags, ND_RA_FLAG_OTHER, other); in sd_radv_set_other_information()
560 int sd_radv_set_preference(sd_radv *ra, unsigned preference) { in sd_radv_set_preference() argument
561 assert_return(ra, -EINVAL); in sd_radv_set_preference()
569 if (ra->lifetime_usec == 0 && preference != SD_NDISC_PREFERENCE_MEDIUM) in sd_radv_set_preference()
572 ra->flags = (ra->flags & ~(0x3 << 3)) | (preference << 3); in sd_radv_set_preference()
577 int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) { in sd_radv_add_prefix() argument
582 assert_return(ra, -EINVAL); in sd_radv_add_prefix()
591 LIST_FOREACH(prefix, cur, ra->prefixes) { in sd_radv_add_prefix()
610 return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST), in sd_radv_add_prefix()
620 LIST_REMOVE(prefix, ra->prefixes, found); in sd_radv_add_prefix()
624 LIST_APPEND(prefix, ra->prefixes, p); in sd_radv_add_prefix()
626 log_radv(ra, "Updated/replaced IPv6 prefix %s (preferred: %s, valid: %s)", in sd_radv_add_prefix()
634 LIST_APPEND(prefix, ra->prefixes, p); in sd_radv_add_prefix()
635 ra->n_prefixes++; in sd_radv_add_prefix()
637 log_radv(ra, "Added prefix %s", strna(addr_p)); in sd_radv_add_prefix()
640 if (ra->state == RADV_STATE_IDLE) in sd_radv_add_prefix()
643 if (ra->ra_sent == 0) in sd_radv_add_prefix()
647 r = radv_send(ra, NULL, ra->lifetime_usec); in sd_radv_add_prefix()
649 log_radv_errno(ra, r, "Unable to send Router Advertisement for added prefix %s: %m", in sd_radv_add_prefix()
652 … log_radv(ra, "Sent Router Advertisement for added/updated prefix %s.", strna(addr_p)); in sd_radv_add_prefix()
658 sd_radv *ra, in sd_radv_remove_prefix() argument
662 if (!ra) in sd_radv_remove_prefix()
668 LIST_FOREACH(prefix, cur, ra->prefixes) { in sd_radv_remove_prefix()
675 LIST_REMOVE(prefix, ra->prefixes, cur); in sd_radv_remove_prefix()
676 ra->n_prefixes--; in sd_radv_remove_prefix()
682 int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) { in sd_radv_add_route_prefix() argument
687 assert_return(ra, -EINVAL); in sd_radv_add_route_prefix()
692 LIST_FOREACH(prefix, cur, ra->route_prefixes) { in sd_radv_add_route_prefix()
711 return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST), in sd_radv_add_route_prefix()
721 LIST_REMOVE(prefix, ra->route_prefixes, found); in sd_radv_add_route_prefix()
725 LIST_APPEND(prefix, ra->route_prefixes, p); in sd_radv_add_route_prefix()
727 log_radv(ra, "Updated/replaced IPv6 route prefix %s (lifetime: %s)", in sd_radv_add_route_prefix()
734 LIST_APPEND(prefix, ra->route_prefixes, p); in sd_radv_add_route_prefix()
735 ra->n_route_prefixes++; in sd_radv_add_route_prefix()
737 log_radv(ra, "Added route prefix %s", strna(addr_p)); in sd_radv_add_route_prefix()
740 if (ra->state == RADV_STATE_IDLE) in sd_radv_add_route_prefix()
743 if (ra->ra_sent == 0) in sd_radv_add_route_prefix()
747 r = radv_send(ra, NULL, ra->lifetime_usec); in sd_radv_add_route_prefix()
749 … log_radv_errno(ra, r, "Unable to send Router Advertisement for added route prefix %s: %m", in sd_radv_add_route_prefix()
752 log_radv(ra, "Sent Router Advertisement for added route prefix %s.", strna(addr_p)); in sd_radv_add_route_prefix()
758 sd_radv *ra, in sd_radv_set_rdnss() argument
766 assert_return(ra, -EINVAL); in sd_radv_set_rdnss()
770 ra->rdnss = mfree(ra->rdnss); in sd_radv_set_rdnss()
771 ra->n_rdnss = 0; in sd_radv_set_rdnss()
788 free_and_replace(ra->rdnss, opt_rdnss); in sd_radv_set_rdnss()
790 ra->n_rdnss = n_dns; in sd_radv_set_rdnss()
796 sd_radv *ra, in sd_radv_set_dnssl() argument
804 assert_return(ra, -EINVAL); in sd_radv_set_dnssl()
807 ra->dnssl = mfree(ra->dnssl); in sd_radv_set_dnssl()
841 free_and_replace(ra->dnssl, opt_dnssl); in sd_radv_set_dnssl()