mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-24 02:03:54 +03:00
sd-ndisc: introduce separate callbacks
As the data passed is very different, we introduce four different callbacks: - Generic - router discovery timed out or state machine stopped - Router - router and link configuration received - Prefix onlink - configuration for an onlink prefix received - Prefix autonomous - configuration for to configure a SLAAC address for a prefix received
This commit is contained in:
parent
cb53894d3b
commit
9d96e6c3ef
@ -29,6 +29,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "async.h"
|
||||
#include "icmp6-util.h"
|
||||
#include "in-addr-util.h"
|
||||
#include "list.h"
|
||||
#include "socket-util.h"
|
||||
|
||||
@ -47,6 +48,12 @@ enum NDiscState {
|
||||
#define ICMP6_RECV_SIZE (IP6_MIN_MTU - sizeof(struct ip6_hdr))
|
||||
#define NDISC_OPT_LEN_UNITS 8
|
||||
|
||||
#define ND_RA_FLAG_PREF 0x18
|
||||
#define ND_RA_FLAG_PREF_LOW 0x03
|
||||
#define ND_RA_FLAG_PREF_MEDIUM 0x0
|
||||
#define ND_RA_FLAG_PREF_HIGH 0x1
|
||||
#define ND_RA_FLAG_PREF_INVALID 0x2
|
||||
|
||||
typedef struct NDiscPrefix NDiscPrefix;
|
||||
|
||||
struct NDiscPrefix {
|
||||
@ -75,6 +82,9 @@ struct sd_ndisc {
|
||||
sd_event_source *recv;
|
||||
sd_event_source *timeout;
|
||||
int nd_sent;
|
||||
sd_ndisc_router_callback_t router_callback;
|
||||
sd_ndisc_prefix_autonomous_callback_t prefix_autonomous_callback;
|
||||
sd_ndisc_prefix_onlink_callback_t prefix_onlink_callback;
|
||||
sd_ndisc_callback_t callback;
|
||||
void *userdata;
|
||||
};
|
||||
@ -119,15 +129,17 @@ static int ndisc_prefix_new(sd_ndisc *nd, NDiscPrefix **ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void ndisc_notify(sd_ndisc *nd, int event) {
|
||||
if (nd->callback)
|
||||
nd->callback(nd, event, nd->userdata);
|
||||
}
|
||||
|
||||
int sd_ndisc_set_callback(sd_ndisc *nd, sd_ndisc_callback_t callback,
|
||||
void *userdata) {
|
||||
int sd_ndisc_set_callback(sd_ndisc *nd,
|
||||
sd_ndisc_router_callback_t router_callback,
|
||||
sd_ndisc_prefix_onlink_callback_t prefix_onlink_callback,
|
||||
sd_ndisc_prefix_autonomous_callback_t prefix_autonomous_callback,
|
||||
sd_ndisc_callback_t callback,
|
||||
void *userdata) {
|
||||
assert(nd);
|
||||
|
||||
nd->router_callback = router_callback;
|
||||
nd->prefix_onlink_callback = prefix_onlink_callback;
|
||||
nd->prefix_autonomous_callback = prefix_autonomous_callback;
|
||||
nd->callback = callback;
|
||||
nd->userdata = userdata;
|
||||
|
||||
@ -320,7 +332,7 @@ static int ndisc_prefix_match(sd_ndisc *nd, const struct in6_addr *addr,
|
||||
static int ndisc_prefix_update(sd_ndisc *nd, ssize_t len,
|
||||
const struct nd_opt_prefix_info *prefix_opt) {
|
||||
NDiscPrefix *prefix;
|
||||
uint32_t lifetime;
|
||||
uint32_t lifetime_valid, lifetime_preferred;
|
||||
usec_t time_now;
|
||||
char time_string[FORMAT_TIMESPAN_MAX];
|
||||
int r;
|
||||
@ -331,10 +343,17 @@ static int ndisc_prefix_update(sd_ndisc *nd, ssize_t len,
|
||||
if (len < prefix_opt->nd_opt_pi_len)
|
||||
return -ENOMSG;
|
||||
|
||||
if (!(prefix_opt->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK))
|
||||
if (!(prefix_opt->nd_opt_pi_flags_reserved & (ND_OPT_PI_FLAG_ONLINK | ND_OPT_PI_FLAG_AUTO)))
|
||||
return 0;
|
||||
|
||||
lifetime = be32toh(prefix_opt->nd_opt_pi_valid_time);
|
||||
if (in_addr_is_link_local(AF_INET6, (const union in_addr_union *) &prefix_opt->nd_opt_pi_prefix) > 0)
|
||||
return 0;
|
||||
|
||||
lifetime_valid = be32toh(prefix_opt->nd_opt_pi_valid_time);
|
||||
lifetime_preferred = be32toh(prefix_opt->nd_opt_pi_preferred_time);
|
||||
|
||||
if (lifetime_valid < lifetime_preferred)
|
||||
return 0;
|
||||
|
||||
r = ndisc_prefix_match(nd, &prefix_opt->nd_opt_pi_prefix,
|
||||
prefix_opt->nd_opt_pi_prefix_len, &prefix);
|
||||
@ -357,8 +376,8 @@ static int ndisc_prefix_update(sd_ndisc *nd, ssize_t len,
|
||||
|
||||
log_ndisc(nd, "New prefix "SD_NDISC_ADDRESS_FORMAT_STR"/%d lifetime %d expires in %s",
|
||||
SD_NDISC_ADDRESS_FORMAT_VAL(prefix->addr),
|
||||
prefix->len, lifetime,
|
||||
format_timespan(time_string, FORMAT_TIMESPAN_MAX, lifetime * USEC_PER_SEC, USEC_PER_SEC));
|
||||
prefix->len, lifetime_valid,
|
||||
format_timespan(time_string, FORMAT_TIMESPAN_MAX, lifetime_valid * USEC_PER_SEC, USEC_PER_SEC));
|
||||
|
||||
LIST_PREPEND(prefixes, nd->prefixes, prefix);
|
||||
|
||||
@ -378,17 +397,24 @@ static int ndisc_prefix_update(sd_ndisc *nd, ssize_t len,
|
||||
|
||||
log_ndisc(nd, "Update prefix "SD_NDISC_ADDRESS_FORMAT_STR"/%d lifetime %d expires in %s",
|
||||
SD_NDISC_ADDRESS_FORMAT_VAL(prefix->addr),
|
||||
prefix->len, lifetime,
|
||||
format_timespan(time_string, FORMAT_TIMESPAN_MAX, lifetime * USEC_PER_SEC, USEC_PER_SEC));
|
||||
prefix->len, lifetime_valid,
|
||||
format_timespan(time_string, FORMAT_TIMESPAN_MAX, lifetime_valid * USEC_PER_SEC, 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;
|
||||
prefix->valid_until = time_now + lifetime_valid * USEC_PER_SEC;
|
||||
|
||||
return r;
|
||||
if ((prefix_opt->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK) && nd->prefix_onlink_callback)
|
||||
nd->prefix_onlink_callback(nd, &prefix->addr, prefix->len, prefix->valid_until, nd->userdata);
|
||||
|
||||
if ((prefix_opt->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO) && nd->prefix_autonomous_callback)
|
||||
nd->prefix_autonomous_callback(nd, &prefix->addr, prefix->len, lifetime_preferred, lifetime_valid,
|
||||
nd->userdata);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ndisc_ra_parse(sd_ndisc *nd, struct nd_router_advert *ra,
|
||||
@ -453,11 +479,13 @@ static int ndisc_ra_parse(sd_ndisc *nd, struct nd_router_advert *ra,
|
||||
}
|
||||
|
||||
static int ndisc_router_advertisment_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
||||
sd_ndisc *nd = userdata;
|
||||
int r, buflen = 0;
|
||||
ssize_t len;
|
||||
_cleanup_free_ struct nd_router_advert *ra = NULL;
|
||||
int event = SD_NDISC_EVENT_ROUTER_ADVERTISMENT_NONE;
|
||||
sd_ndisc *nd = userdata;
|
||||
int r, buflen = 0, pref, stateful;
|
||||
union sockaddr_union router = {};
|
||||
socklen_t router_len = sizeof(router);
|
||||
unsigned lifetime;
|
||||
ssize_t len;
|
||||
|
||||
assert(s);
|
||||
assert(nd);
|
||||
@ -471,9 +499,12 @@ static int ndisc_router_advertisment_recv(sd_event_source *s, int fd, uint32_t r
|
||||
if (!ra)
|
||||
return -ENOMEM;
|
||||
|
||||
len = read(fd, ra, buflen);
|
||||
len = recvfrom(fd, ra, buflen, 0, &router.sa, &router_len);
|
||||
if (len < 0) {
|
||||
log_ndisc(nd, "Could not receive message from UDP socket: %m");
|
||||
log_ndisc(nd, "Could not receive message from ICMPv6 socket: %m");
|
||||
return 0;
|
||||
} else if (router_len != sizeof(router.in6) && router_len != 0) {
|
||||
log_ndisc(nd, "Received invalid source address size from ICMPv6 socket: %zu bytes", (size_t)router_len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -487,26 +518,34 @@ static int ndisc_router_advertisment_recv(sd_event_source *s, int fd, uint32_t r
|
||||
|
||||
nd->state = NDISC_STATE_ADVERTISMENT_LISTEN;
|
||||
|
||||
if (ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER )
|
||||
event = SD_NDISC_EVENT_ROUTER_ADVERTISMENT_OTHER;
|
||||
|
||||
if (ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED)
|
||||
event = SD_NDISC_EVENT_ROUTER_ADVERTISMENT_MANAGED;
|
||||
|
||||
log_ndisc(nd, "Received Router Advertisement flags %s/%s",
|
||||
ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED ? "MANAGED" : "none",
|
||||
ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER ? "OTHER" : "none");
|
||||
|
||||
if (event != SD_NDISC_EVENT_ROUTER_ADVERTISMENT_NONE) {
|
||||
r = ndisc_ra_parse(nd, ra, len);
|
||||
if (r < 0) {
|
||||
log_ndisc(nd, "Could not parse Router Advertisement: %s",
|
||||
strerror(-r));
|
||||
return 0;
|
||||
}
|
||||
stateful = ra->nd_ra_flags_reserved & (ND_RA_FLAG_MANAGED | ND_RA_FLAG_OTHER);
|
||||
switch (ra->nd_ra_flags_reserved & ND_RA_FLAG_PREF >> 3) {
|
||||
case ND_RA_FLAG_PREF_LOW:
|
||||
pref = -1;
|
||||
break;
|
||||
case ND_RA_FLAG_PREF_HIGH:
|
||||
pref = 1;
|
||||
break;
|
||||
default:
|
||||
pref = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
ndisc_notify(nd, event);
|
||||
lifetime = be16toh(ra->nd_ra_router_lifetime);
|
||||
|
||||
log_ndisc(nd, "Received Router Advertisement: flags %s preference %s lifetime %u sec",
|
||||
stateful & ND_RA_FLAG_MANAGED ? "MANAGED" : stateful & ND_RA_FLAG_OTHER ? "OTHER" : "none",
|
||||
pref == 1 ? "high" : pref == -1 ? "low" : "medium",
|
||||
lifetime);
|
||||
|
||||
r = ndisc_ra_parse(nd, ra, len);
|
||||
if (r < 0) {
|
||||
log_ndisc(nd, "Could not parse Router Advertisement: %s", strerror(-r));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (nd->router_callback)
|
||||
nd->router_callback(nd, stateful, router_len != 0 ? &router.in6.sin6_addr : NULL, lifetime, pref, nd->userdata);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -525,7 +564,8 @@ static int ndisc_router_solicitation_timeout(sd_event_source *s, uint64_t usec,
|
||||
nd->timeout = sd_event_source_unref(nd->timeout);
|
||||
|
||||
if (nd->nd_sent >= NDISC_MAX_ROUTER_SOLICITATIONS) {
|
||||
ndisc_notify(nd, SD_NDISC_EVENT_ROUTER_ADVERTISMENT_TIMEOUT);
|
||||
if (nd->callback)
|
||||
nd->callback(nd, SD_NDISC_EVENT_TIMEOUT, nd->userdata);
|
||||
nd->state = NDISC_STATE_ADVERTISMENT_LISTEN;
|
||||
} else {
|
||||
if (memcmp(&nd->mac_addr, &unset, sizeof(struct ether_addr)))
|
||||
@ -576,7 +616,8 @@ int sd_ndisc_stop(sd_ndisc *nd) {
|
||||
|
||||
nd->state = NDISC_STATE_IDLE;
|
||||
|
||||
ndisc_notify(nd, SD_NDISC_EVENT_STOP);
|
||||
if (nd->callback)
|
||||
nd->callback(nd, SD_NDISC_EVENT_STOP, nd->userdata);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -85,31 +85,28 @@ int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) {
|
||||
return send_ra_function(0);
|
||||
}
|
||||
|
||||
static void test_rs_done(sd_ndisc *nd, int event, void *userdata) {
|
||||
static void test_rs_done(sd_ndisc *nd, uint8_t flags, const struct in6_addr *gateway, unsigned lifetime, int pref, void *userdata) {
|
||||
sd_event *e = userdata;
|
||||
static unsigned idx = 0;
|
||||
struct {
|
||||
uint8_t flag;
|
||||
int event;
|
||||
} flag_event[] = {
|
||||
{ 0, SD_NDISC_EVENT_STOP },
|
||||
{ 0, SD_NDISC_EVENT_STOP },
|
||||
{ 0, SD_NDISC_EVENT_ROUTER_ADVERTISMENT_NONE },
|
||||
{ ND_RA_FLAG_OTHER, SD_NDISC_EVENT_ROUTER_ADVERTISMENT_OTHER },
|
||||
{ ND_RA_FLAG_MANAGED, SD_NDISC_EVENT_ROUTER_ADVERTISMENT_MANAGED }
|
||||
uint8_t flags_array[] = {
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
ND_RA_FLAG_OTHER,
|
||||
ND_RA_FLAG_MANAGED
|
||||
};
|
||||
uint32_t mtu;
|
||||
|
||||
assert_se(nd);
|
||||
|
||||
assert_se(event == flag_event[idx].event);
|
||||
assert_se(flags == flags_array[idx]);
|
||||
idx++;
|
||||
|
||||
if (verbose)
|
||||
printf(" got event %d\n", event);
|
||||
printf(" got event 0x%02x\n", flags);
|
||||
|
||||
if (idx < ELEMENTSOF(flag_event)) {
|
||||
send_ra(flag_event[idx].flag);
|
||||
if (idx < ELEMENTSOF(flags_array)) {
|
||||
send_ra(flags_array[idx]);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -137,7 +134,7 @@ static void test_rs(void) {
|
||||
|
||||
assert_se(sd_ndisc_set_index(nd, 42) >= 0);
|
||||
assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
|
||||
assert_se(sd_ndisc_set_callback(nd, test_rs_done, e) >= 0);
|
||||
assert_se(sd_ndisc_set_callback(nd, test_rs_done, NULL, NULL, NULL, e) >= 0);
|
||||
|
||||
assert_se(sd_event_add_time(e, &test_hangcheck, clock_boottime_or_monotonic(),
|
||||
time_now + 2 *USEC_PER_SEC, 0,
|
||||
|
@ -20,41 +20,44 @@
|
||||
***/
|
||||
|
||||
#include <netinet/ether.h>
|
||||
#include <netinet/icmp6.h>
|
||||
#include <linux/if.h>
|
||||
|
||||
#include "sd-ndisc.h"
|
||||
|
||||
#include "networkd-link.h"
|
||||
|
||||
static void ndisc_router_handler(sd_ndisc *nd, int event, void *userdata) {
|
||||
static void ndisc_router_handler(sd_ndisc *nd, uint8_t flags, const struct in6_addr *gateway, unsigned lifetime, int pref, void *userdata) {
|
||||
Link *link = userdata;
|
||||
|
||||
assert(link);
|
||||
assert(link->network);
|
||||
assert(link->manager);
|
||||
|
||||
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
||||
return;
|
||||
|
||||
switch(event) {
|
||||
case SD_NDISC_EVENT_STOP:
|
||||
case SD_NDISC_EVENT_ROUTER_ADVERTISMENT_NONE:
|
||||
if (flags & ND_RA_FLAG_MANAGED)
|
||||
dhcp6_configure(link, false);
|
||||
else if (flags & ND_RA_FLAG_OTHER)
|
||||
dhcp6_configure(link, true);
|
||||
}
|
||||
|
||||
static void ndisc_handler(sd_ndisc *nd, int event, void *userdata) {
|
||||
Link *link = userdata;
|
||||
|
||||
assert(link);
|
||||
|
||||
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
||||
return;
|
||||
|
||||
case SD_NDISC_EVENT_ROUTER_ADVERTISMENT_OTHER:
|
||||
dhcp6_configure(link, true);
|
||||
|
||||
break;
|
||||
case SD_NDISC_EVENT_ROUTER_ADVERTISMENT_TIMEOUT:
|
||||
case SD_NDISC_EVENT_ROUTER_ADVERTISMENT_MANAGED:
|
||||
switch (event) {
|
||||
case SD_NDISC_EVENT_TIMEOUT:
|
||||
dhcp6_configure(link, false);
|
||||
|
||||
break;
|
||||
|
||||
case SD_NDISC_EVENT_STOP:
|
||||
break;
|
||||
default:
|
||||
log_link_warning(link, "IPv6 Neighbor Discovery unknown event: %d", event);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,7 +83,11 @@ int ndisc_configure(Link *link) {
|
||||
return r;
|
||||
|
||||
r = sd_ndisc_set_callback(link->ndisc_router_discovery,
|
||||
ndisc_router_handler, link);
|
||||
ndisc_router_handler,
|
||||
NULL,
|
||||
NULL,
|
||||
ndisc_handler,
|
||||
link);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
@ -31,20 +31,25 @@
|
||||
_SD_BEGIN_DECLARATIONS;
|
||||
|
||||
enum {
|
||||
SD_NDISC_EVENT_ROUTER_ADVERTISMENT_NONE = 0,
|
||||
SD_NDISC_EVENT_ROUTER_ADVERTISMENT_TIMEOUT = 1,
|
||||
SD_NDISC_EVENT_ROUTER_ADVERTISMENT_OTHER = 2,
|
||||
SD_NDISC_EVENT_ROUTER_ADVERTISMENT_MANAGED = 3,
|
||||
SD_NDISC_EVENT_STOP = 4,
|
||||
SD_NDISC_EVENT_STOP = 0,
|
||||
SD_NDISC_EVENT_TIMEOUT = 1,
|
||||
};
|
||||
|
||||
typedef struct sd_ndisc sd_ndisc;
|
||||
|
||||
typedef void(*sd_ndisc_callback_t)(sd_ndisc *nd, int event,
|
||||
void *userdata);
|
||||
typedef void(*sd_ndisc_router_callback_t)(sd_ndisc *nd, uint8_t flags, const struct in6_addr *gateway, unsigned lifetime, int pref, void *userdata);
|
||||
typedef void(*sd_ndisc_prefix_onlink_callback_t)(sd_ndisc *nd, const struct in6_addr *prefix, unsigned prefixlen,
|
||||
unsigned lifetime, void *userdata);
|
||||
typedef void(*sd_ndisc_prefix_autonomous_callback_t)(sd_ndisc *nd, const struct in6_addr *prefix, unsigned prefixlen,
|
||||
unsigned lifetime_prefered, unsigned lifetime_valid, void *userdata);
|
||||
typedef void(*sd_ndisc_callback_t)(sd_ndisc *nd, int event, void *userdata);
|
||||
|
||||
int sd_ndisc_set_callback(sd_ndisc *nd, sd_ndisc_callback_t cb,
|
||||
void *userdata);
|
||||
int sd_ndisc_set_callback(sd_ndisc *nd,
|
||||
sd_ndisc_router_callback_t rcb,
|
||||
sd_ndisc_prefix_onlink_callback_t plcb,
|
||||
sd_ndisc_prefix_autonomous_callback_t pacb,
|
||||
sd_ndisc_callback_t cb,
|
||||
void *userdata);
|
||||
int sd_ndisc_set_index(sd_ndisc *nd, int interface_index);
|
||||
int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user