***/
#include <netinet/icmp6.h>
+#include <netinet/in.h>
#include <netinet/ip6.h>
-#include <string.h>
#include <stdbool.h>
-#include <netinet/in.h>
+#include <string.h>
#include <sys/ioctl.h>
+#include "sd-ndisc.h"
+
#include "async.h"
+#include "icmp6-util.h"
#include "list.h"
#include "socket-util.h"
-#include "icmp6-util.h"
-#include "sd-ndisc.h"
-
#define NDISC_ROUTER_SOLICITATION_INTERVAL 4 * USEC_PER_SEC
#define NDISC_MAX_ROUTER_SOLICITATIONS 3
struct NDiscPrefix {
unsigned n_ref;
+ sd_ndisc *nd;
+
LIST_FIELDS(NDiscPrefix, prefixes);
uint8_t len;
- sd_event_source *timeout_valid;
+ usec_t valid_until;
struct in6_addr addr;
};
if (prefix->n_ref > 0)
return NULL;
- prefix->timeout_valid = sd_event_source_unref(prefix->timeout_valid);
+ if (prefix->nd)
+ LIST_REMOVE(prefixes, prefix->nd->prefixes, prefix);
+
free(prefix);
+
return NULL;
}
-static int ndisc_prefix_new(NDiscPrefix **ret) {
+static int ndisc_prefix_new(sd_ndisc *nd, NDiscPrefix **ret) {
_cleanup_free_ NDiscPrefix *prefix = NULL;
assert(ret);
prefix->n_ref = 1;
LIST_INIT(prefixes, prefix);
+ prefix->nd = nd;
*ret = prefix;
prefix = NULL;
ndisc_init(nd);
sd_ndisc_detach_event(nd);
- LIST_FOREACH_SAFE(prefixes, prefix, p, nd->prefixes) {
- LIST_REMOVE(prefixes, nd->prefixes, prefix);
-
+ LIST_FOREACH_SAFE(prefixes, prefix, p, nd->prefixes)
prefix = ndisc_prefix_unref(prefix);
- }
free(nd);
return 0;
}
-static int ndisc_prefix_timeout(sd_event_source *s, uint64_t usec,
- void *userdata) {
- sd_ndisc *nd = userdata;
- NDiscPrefix *prefix, *p;
-
- assert(nd);
-
- LIST_FOREACH_SAFE(prefixes, prefix, p, nd->prefixes) {
- if (prefix->timeout_valid != s)
- continue;
-
- log_ndisc(nd, "Prefix expired "SD_NDISC_ADDRESS_FORMAT_STR"/%d",
- SD_NDISC_ADDRESS_FORMAT_VAL(prefix->addr),
- prefix->len);
-
- LIST_REMOVE(prefixes, nd->prefixes, prefix);
-
- prefix = ndisc_prefix_unref(prefix);
-
- break;
- }
-
- return 0;
-}
-
-static int ndisc_prefix_set_timeout(sd_ndisc *nd,
- NDiscPrefix *prefix,
- usec_t valid) {
- usec_t time_now;
- int r;
-
- assert_return(prefix, -EINVAL);
-
- r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
- if (r < 0)
- return r;
-
- prefix->timeout_valid = sd_event_source_unref(prefix->timeout_valid);
-
- r = sd_event_add_time(nd->event, &prefix->timeout_valid,
- clock_boottime_or_monotonic(), time_now + valid,
- USEC_PER_SEC, ndisc_prefix_timeout, nd);
- if (r < 0)
- goto error;
-
- r = sd_event_source_set_priority(prefix->timeout_valid,
- nd->event_priority);
- if (r < 0)
- goto error;
-
- r = sd_event_source_set_description(prefix->timeout_valid,
- "ndisc-prefix-timeout");
-
-error:
- if (r < 0)
- prefix->timeout_valid =
- sd_event_source_unref(prefix->timeout_valid);
-
- return r;
-}
-
static int prefix_match(const struct in6_addr *prefix, uint8_t prefixlen,
const struct in6_addr *addr,
uint8_t addr_prefixlen) {
return 0;
}
-static int ndisc_prefix_match(NDiscPrefix *head, const struct in6_addr *addr,
- uint8_t addr_len, NDiscPrefix **result) {
- NDiscPrefix *prefix;
+static int ndisc_prefix_match(sd_ndisc *nd, const struct in6_addr *addr,
+ uint8_t addr_len, NDiscPrefix **result) {
+ NDiscPrefix *prefix, *p;
+ usec_t time_now;
+ int r;
+
+ assert(nd);
+
+ r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
+ if (r < 0)
+ return r;
+
+ LIST_FOREACH_SAFE(prefixes, prefix, p, nd->prefixes) {
+ if (prefix->valid_until < time_now) {
+ prefix = ndisc_prefix_unref(prefix);
+
+ continue;
+ }
- LIST_FOREACH(prefixes, prefix, head) {
if (prefix_match(&prefix->addr, prefix->len, addr, addr_len) >= 0) {
*result = prefix;
return 0;
return -EADDRNOTAVAIL;
}
-int sd_ndisc_prefix_match(struct in6_addr *prefix, uint8_t prefixlen,
- struct in6_addr *addr) {
- return prefix_match(prefix, prefixlen, addr, sizeof(addr->s6_addr) * 8);
-}
-
-int sd_ndisc_get_prefixlen(sd_ndisc *nd, const struct in6_addr *addr,
- uint8_t *prefixlen) {
- int r;
- NDiscPrefix *prefix;
-
- assert_return(nd, -EINVAL);
- assert_return(addr, -EINVAL);
- assert_return(prefixlen, -EINVAL);
-
- r = ndisc_prefix_match(nd->prefixes, addr,
- sizeof(addr->s6_addr) * 8, &prefix);
- if (r < 0)
- return r;
-
- *prefixlen = prefix->len;
-
- return 0;
-}
-
static int ndisc_prefix_update(sd_ndisc *nd, ssize_t len,
- const struct nd_opt_prefix_info *prefix_opt) {
- int r;
+ const struct nd_opt_prefix_info *prefix_opt) {
NDiscPrefix *prefix;
uint32_t lifetime;
+ usec_t time_now;
char time_string[FORMAT_TIMESPAN_MAX];
+ int r;
- assert_return(nd, -EINVAL);
- assert_return(prefix_opt, -EINVAL);
+ assert(nd);
+ assert(prefix_opt);
if (len < prefix_opt->nd_opt_pi_len)
return -ENOMSG;
lifetime = be32toh(prefix_opt->nd_opt_pi_valid_time);
- r = ndisc_prefix_match(nd->prefixes,
- &prefix_opt->nd_opt_pi_prefix,
- prefix_opt->nd_opt_pi_prefix_len, &prefix);
+ r = ndisc_prefix_match(nd, &prefix_opt->nd_opt_pi_prefix,
+ prefix_opt->nd_opt_pi_prefix_len, &prefix);
if (r < 0 && r != -EADDRNOTAVAIL)
return r;
callback will be called immediately to clean up the prefix */
if (r == -EADDRNOTAVAIL) {
- r = ndisc_prefix_new(&prefix);
+ r = ndisc_prefix_new(nd, &prefix);
if (r < 0)
return r;
format_timespan(time_string, FORMAT_TIMESPAN_MAX, lifetime * USEC_PER_SEC, USEC_PER_SEC));
}
- r = ndisc_prefix_set_timeout(nd, prefix, lifetime * USEC_PER_SEC);
+ r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
+ if (r < 0)
+ return r;
+
+ prefix->valid_until = time_now + lifetime * USEC_PER_SEC;
return r;
}
nd->nd_sent++;
- r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
- if (r < 0) {
- ndisc_notify(nd, r);
- return 0;
- }
+ assert_se(sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now) >= 0);
next_timeout = time_now + NDISC_ROUTER_SOLICITATION_INTERVAL;
return 0;
}
- r = sd_event_source_set_priority(nd->timeout,
- nd->event_priority);
- if (r < 0) {
- ndisc_notify(nd, r);
+ r = sd_event_source_set_priority(nd->timeout, nd->event_priority);
+ if (r < 0)
return 0;
- }
r = sd_event_source_set_description(nd->timeout, "ndisc-timeout");
- if (r < 0) {
- ndisc_notify(nd, r);
+ if (r < 0)
return 0;
- }
}
return 0;