diff options
Diffstat (limited to 'src/ndisc')
| -rw-r--r-- | src/ndisc/nm-fake-ndisc.c | 587 | ||||
| -rw-r--r-- | src/ndisc/nm-fake-ndisc.h | 86 | ||||
| -rw-r--r-- | src/ndisc/nm-lndp-ndisc.c | 1194 | ||||
| -rw-r--r-- | src/ndisc/nm-lndp-ndisc.h | 50 | ||||
| -rw-r--r-- | src/ndisc/nm-ndisc-private.h | 80 | ||||
| -rw-r--r-- | src/ndisc/nm-ndisc.c | 2508 | ||||
| -rw-r--r-- | src/ndisc/nm-ndisc.h | 279 | ||||
| -rw-r--r-- | src/ndisc/tests/test-ndisc-fake.c | 858 | ||||
| -rw-r--r-- | src/ndisc/tests/test-ndisc-linux.c | 118 |
9 files changed, 3062 insertions, 2698 deletions
diff --git a/src/ndisc/nm-fake-ndisc.c b/src/ndisc/nm-fake-ndisc.c index 5f4594b9..bd9f5f7e 100644 --- a/src/ndisc/nm-fake-ndisc.c +++ b/src/ndisc/nm-fake-ndisc.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2013 Red Hat, Inc. */ @@ -11,386 +11,411 @@ #include "nm-ndisc-private.h" -#define _NMLOG_PREFIX_NAME "ndisc-fake" +#define _NMLOG_PREFIX_NAME "ndisc-fake" /*****************************************************************************/ typedef struct { - guint id; - guint when; - - NMNDiscDHCPLevel dhcp_level; - GArray *gateways; - GArray *prefixes; - GArray *dns_servers; - GArray *dns_domains; - int hop_limit; - guint32 mtu; + guint id; + guint when; + + NMNDiscDHCPLevel dhcp_level; + GArray * gateways; + GArray * prefixes; + GArray * dns_servers; + GArray * dns_domains; + int hop_limit; + guint32 mtu; } FakeRa; typedef struct { - struct in6_addr network; - int plen; - struct in6_addr gateway; - guint32 timestamp; - guint32 lifetime; - guint32 preferred; - NMIcmpv6RouterPref preference; + struct in6_addr network; + int plen; + struct in6_addr gateway; + guint32 timestamp; + guint32 lifetime; + guint32 preferred; + NMIcmpv6RouterPref preference; } FakePrefix; /*****************************************************************************/ enum { - RS_SENT, - LAST_SIGNAL, + RS_SENT, + LAST_SIGNAL, }; -static guint signals[LAST_SIGNAL] = { 0 }; +static guint signals[LAST_SIGNAL] = {0}; typedef struct { - guint receive_ra_id; - GSList *ras; + guint receive_ra_id; + GSList *ras; } NMFakeNDiscPrivate; struct _NMFakeRNDisc { - NMNDisc parent; - NMFakeNDiscPrivate _priv; + NMNDisc parent; + NMFakeNDiscPrivate _priv; }; struct _NMFakeRNDiscClass { - NMNDiscClass parent; + NMNDiscClass parent; }; -G_DEFINE_TYPE (NMFakeNDisc, nm_fake_ndisc, NM_TYPE_NDISC) +G_DEFINE_TYPE(NMFakeNDisc, nm_fake_ndisc, NM_TYPE_NDISC) -#define NM_FAKE_NDISC_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMFakeNDisc, NM_IS_FAKE_NDISC, NMNDisc) +#define NM_FAKE_NDISC_GET_PRIVATE(self) \ + _NM_GET_PRIVATE(self, NMFakeNDisc, NM_IS_FAKE_NDISC, NMNDisc) /*****************************************************************************/ static void -fake_ra_free (gpointer data) +fake_ra_free(gpointer data) { - FakeRa *ra = data; + FakeRa *ra = data; - g_array_free (ra->gateways, TRUE); - g_array_free (ra->prefixes, TRUE); - g_array_free (ra->dns_servers, TRUE); - g_array_free (ra->dns_domains, TRUE); - g_free (ra); + g_array_free(ra->gateways, TRUE); + g_array_free(ra->prefixes, TRUE); + g_array_free(ra->dns_servers, TRUE); + g_array_free(ra->dns_domains, TRUE); + g_free(ra); } static void -ra_dns_domain_free (gpointer data) +ra_dns_domain_free(gpointer data) { - g_free (((NMNDiscDNSDomain *)(data))->domain); + g_free(((NMNDiscDNSDomain *) (data))->domain); } static FakeRa * -find_ra (GSList *ras, guint id) +find_ra(GSList *ras, guint id) { - GSList *iter; + GSList *iter; - for (iter = ras; iter; iter = iter->next) { - if (((FakeRa *) iter->data)->id == id) - return iter->data; - } - return NULL; + for (iter = ras; iter; iter = iter->next) { + if (((FakeRa *) iter->data)->id == id) + return iter->data; + } + return NULL; } guint -nm_fake_ndisc_add_ra (NMFakeNDisc *self, - guint seconds_after_previous, - NMNDiscDHCPLevel dhcp_level, - int hop_limit, - guint32 mtu) +nm_fake_ndisc_add_ra(NMFakeNDisc * self, + guint seconds_after_previous, + NMNDiscDHCPLevel dhcp_level, + int hop_limit, + guint32 mtu) { - NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE (self); - static guint counter = 1; - FakeRa *ra; - - ra = g_malloc0 (sizeof (*ra)); - ra->id = counter++; - ra->when = seconds_after_previous; - ra->dhcp_level = dhcp_level; - ra->hop_limit = hop_limit; - ra->mtu = mtu; - ra->gateways = g_array_new (FALSE, FALSE, sizeof (NMNDiscGateway)); - ra->prefixes = g_array_new (FALSE, FALSE, sizeof (FakePrefix)); - ra->dns_servers = g_array_new (FALSE, FALSE, sizeof (NMNDiscDNSServer)); - ra->dns_domains = g_array_new (FALSE, FALSE, sizeof (NMNDiscDNSDomain)); - g_array_set_clear_func (ra->dns_domains, ra_dns_domain_free); - - priv->ras = g_slist_append (priv->ras, ra); - return ra->id; + NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(self); + static guint counter = 1; + FakeRa * ra; + + ra = g_malloc0(sizeof(*ra)); + ra->id = counter++; + ra->when = seconds_after_previous; + ra->dhcp_level = dhcp_level; + ra->hop_limit = hop_limit; + ra->mtu = mtu; + ra->gateways = g_array_new(FALSE, FALSE, sizeof(NMNDiscGateway)); + ra->prefixes = g_array_new(FALSE, FALSE, sizeof(FakePrefix)); + ra->dns_servers = g_array_new(FALSE, FALSE, sizeof(NMNDiscDNSServer)); + ra->dns_domains = g_array_new(FALSE, FALSE, sizeof(NMNDiscDNSDomain)); + g_array_set_clear_func(ra->dns_domains, ra_dns_domain_free); + + priv->ras = g_slist_append(priv->ras, ra); + return ra->id; } void -nm_fake_ndisc_add_gateway (NMFakeNDisc *self, - guint ra_id, - const char *addr, - guint32 timestamp, - guint32 lifetime, - NMIcmpv6RouterPref preference) +nm_fake_ndisc_add_gateway(NMFakeNDisc * self, + guint ra_id, + const char * addr, + guint32 timestamp, + guint32 lifetime, + NMIcmpv6RouterPref preference) { - NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE (self); - FakeRa *ra = find_ra (priv->ras, ra_id); - NMNDiscGateway *gw; - - g_assert (ra); - g_array_set_size (ra->gateways, ra->gateways->len + 1); - gw = &g_array_index (ra->gateways, NMNDiscGateway, ra->gateways->len - 1); - g_assert (inet_pton (AF_INET6, addr, &gw->address) == 1); - gw->timestamp = timestamp; - gw->lifetime = lifetime; - gw->preference = preference; + NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(self); + FakeRa * ra = find_ra(priv->ras, ra_id); + NMNDiscGateway * gw; + + g_assert(ra); + g_array_set_size(ra->gateways, ra->gateways->len + 1); + gw = &g_array_index(ra->gateways, NMNDiscGateway, ra->gateways->len - 1); + g_assert(inet_pton(AF_INET6, addr, &gw->address) == 1); + gw->timestamp = timestamp; + gw->lifetime = lifetime; + gw->preference = preference; } void -nm_fake_ndisc_add_prefix (NMFakeNDisc *self, - guint ra_id, - const char *network, - guint plen, - const char *gateway, - guint32 timestamp, - guint32 lifetime, - guint32 preferred, - NMIcmpv6RouterPref preference) +nm_fake_ndisc_add_prefix(NMFakeNDisc * self, + guint ra_id, + const char * network, + guint plen, + const char * gateway, + guint32 timestamp, + guint32 lifetime, + guint32 preferred, + NMIcmpv6RouterPref preference) { - NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE (self); - FakeRa *ra = find_ra (priv->ras, ra_id); - FakePrefix *prefix; - - g_assert (ra); - g_array_set_size (ra->prefixes, ra->prefixes->len + 1); - prefix = &g_array_index (ra->prefixes, FakePrefix, ra->prefixes->len - 1); - memset (prefix, 0, sizeof (*prefix)); - g_assert (inet_pton (AF_INET6, network, &prefix->network) == 1); - g_assert (inet_pton (AF_INET6, gateway, &prefix->gateway) == 1); - prefix->plen = plen; - prefix->timestamp = timestamp; - prefix->lifetime = lifetime; - prefix->preferred = preferred; - prefix->preference = preference; + NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(self); + FakeRa * ra = find_ra(priv->ras, ra_id); + FakePrefix * prefix; + + g_assert(ra); + g_array_set_size(ra->prefixes, ra->prefixes->len + 1); + prefix = &g_array_index(ra->prefixes, FakePrefix, ra->prefixes->len - 1); + memset(prefix, 0, sizeof(*prefix)); + g_assert(inet_pton(AF_INET6, network, &prefix->network) == 1); + g_assert(inet_pton(AF_INET6, gateway, &prefix->gateway) == 1); + prefix->plen = plen; + prefix->timestamp = timestamp; + prefix->lifetime = lifetime; + prefix->preferred = preferred; + prefix->preference = preference; } void -nm_fake_ndisc_add_dns_server (NMFakeNDisc *self, - guint ra_id, - const char *address, - guint32 timestamp, - guint32 lifetime) +nm_fake_ndisc_add_dns_server(NMFakeNDisc *self, + guint ra_id, + const char * address, + guint32 timestamp, + guint32 lifetime) { - NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE (self); - FakeRa *ra = find_ra (priv->ras, ra_id); - NMNDiscDNSServer *dns; - - g_assert (ra); - g_array_set_size (ra->dns_servers, ra->dns_servers->len + 1); - dns = &g_array_index (ra->dns_servers, NMNDiscDNSServer, ra->dns_servers->len - 1); - g_assert (inet_pton (AF_INET6, address, &dns->address) == 1); - dns->timestamp = timestamp; - dns->lifetime = lifetime; + NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(self); + FakeRa * ra = find_ra(priv->ras, ra_id); + NMNDiscDNSServer * dns; + + g_assert(ra); + g_array_set_size(ra->dns_servers, ra->dns_servers->len + 1); + dns = &g_array_index(ra->dns_servers, NMNDiscDNSServer, ra->dns_servers->len - 1); + g_assert(inet_pton(AF_INET6, address, &dns->address) == 1); + dns->timestamp = timestamp; + dns->lifetime = lifetime; } void -nm_fake_ndisc_add_dns_domain (NMFakeNDisc *self, - guint ra_id, - const char *domain, - guint32 timestamp, - guint32 lifetime) +nm_fake_ndisc_add_dns_domain(NMFakeNDisc *self, + guint ra_id, + const char * domain, + guint32 timestamp, + guint32 lifetime) { - NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE (self); - FakeRa *ra = find_ra (priv->ras, ra_id); - NMNDiscDNSDomain *dns; - - g_assert (ra); - g_array_set_size (ra->dns_domains, ra->dns_domains->len + 1); - dns = &g_array_index (ra->dns_domains, NMNDiscDNSDomain, ra->dns_domains->len - 1); - dns->domain = g_strdup (domain); - dns->timestamp = timestamp; - dns->lifetime = lifetime; + NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(self); + FakeRa * ra = find_ra(priv->ras, ra_id); + NMNDiscDNSDomain * dns; + + g_assert(ra); + g_array_set_size(ra->dns_domains, ra->dns_domains->len + 1); + dns = &g_array_index(ra->dns_domains, NMNDiscDNSDomain, ra->dns_domains->len - 1); + dns->domain = g_strdup(domain); + dns->timestamp = timestamp; + dns->lifetime = lifetime; } gboolean -nm_fake_ndisc_done (NMFakeNDisc *self) +nm_fake_ndisc_done(NMFakeNDisc *self) { - return !NM_FAKE_NDISC_GET_PRIVATE (self)->ras; + return !NM_FAKE_NDISC_GET_PRIVATE(self)->ras; } /*****************************************************************************/ static gboolean -send_rs (NMNDisc *ndisc, GError **error) +send_rs(NMNDisc *ndisc, GError **error) { - g_signal_emit (ndisc, signals[RS_SENT], 0); - return TRUE; + g_signal_emit(ndisc, signals[RS_SENT], 0); + return TRUE; } static gboolean -receive_ra (gpointer user_data) +receive_ra(gpointer user_data) { - NMFakeNDisc *self = user_data; - NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE (self); - NMNDisc *ndisc = NM_NDISC (self); - NMNDiscDataInternal *rdata = ndisc->rdata; - FakeRa *ra = priv->ras->data; - NMNDiscConfigMap changed = 0; - gint32 now = nm_utils_get_monotonic_timestamp_sec (); - guint i; - NMNDiscDHCPLevel dhcp_level; - - priv->receive_ra_id = 0; - - /* preserve the "most managed" level on updates. */ - dhcp_level = MAX (rdata->public.dhcp_level, ra->dhcp_level); - - if (rdata->public.dhcp_level != dhcp_level) { - rdata->public.dhcp_level = dhcp_level; - changed |= NM_NDISC_CONFIG_DHCP_LEVEL; - } - - for (i = 0; i < ra->gateways->len; i++) { - NMNDiscGateway *item = &g_array_index (ra->gateways, NMNDiscGateway, i); - - if (nm_ndisc_add_gateway (ndisc, item)) - changed |= NM_NDISC_CONFIG_GATEWAYS; - } - - for (i = 0; i < ra->prefixes->len; i++) { - FakePrefix *item = &g_array_index (ra->prefixes, FakePrefix, i); - NMNDiscRoute route = { - .network = item->network, - .plen = item->plen, - .gateway = item->gateway, - .timestamp = item->timestamp, - .lifetime = item->lifetime, - .preference = item->preference, - }; - - g_assert (route.plen > 0 && route.plen <= 128); - - if (nm_ndisc_add_route (ndisc, &route)) - changed |= NM_NDISC_CONFIG_ROUTES; - - if (item->plen == 64) { - NMNDiscAddress address = { - .address = item->network, - .timestamp = item->timestamp, - .lifetime = item->lifetime, - .preferred = item->preferred, - .dad_counter = 0, - }; - - if (nm_ndisc_complete_and_add_address (ndisc, &address, now)) - changed |= NM_NDISC_CONFIG_ADDRESSES; - } - } - - for (i = 0; i < ra->dns_servers->len; i++) { - NMNDiscDNSServer *item = &g_array_index (ra->dns_servers, NMNDiscDNSServer, i); - - if (nm_ndisc_add_dns_server (ndisc, item)) - changed |= NM_NDISC_CONFIG_DNS_SERVERS; - } - - for (i = 0; i < ra->dns_domains->len; i++) { - NMNDiscDNSDomain *item = &g_array_index (ra->dns_domains, NMNDiscDNSDomain, i); - - if (nm_ndisc_add_dns_domain (ndisc, item)) - changed |= NM_NDISC_CONFIG_DNS_DOMAINS; - } - - if (rdata->public.mtu != ra->mtu) { - rdata->public.mtu = ra->mtu; - changed |= NM_NDISC_CONFIG_MTU; - } - - if (rdata->public.hop_limit != ra->hop_limit) { - rdata->public.hop_limit = ra->hop_limit; - changed |= NM_NDISC_CONFIG_HOP_LIMIT; - } - - priv->ras = g_slist_remove (priv->ras, priv->ras->data); - fake_ra_free (ra); - - nm_ndisc_ra_received (NM_NDISC (self), now, changed); - - /* Schedule next RA */ - if (priv->ras) { - ra = priv->ras->data; - priv->receive_ra_id = g_timeout_add_seconds (ra->when, receive_ra, self); - } - - return G_SOURCE_REMOVE; + NMFakeNDisc * self = user_data; + NMFakeNDiscPrivate * priv = NM_FAKE_NDISC_GET_PRIVATE(self); + NMNDisc * ndisc = NM_NDISC(self); + NMNDiscDataInternal *rdata = ndisc->rdata; + FakeRa * ra = priv->ras->data; + NMNDiscConfigMap changed = 0; + gint32 now = nm_utils_get_monotonic_timestamp_sec(); + guint i; + NMNDiscDHCPLevel dhcp_level; + + priv->receive_ra_id = 0; + + /* preserve the "most managed" level on updates. */ + dhcp_level = MAX(rdata->public.dhcp_level, ra->dhcp_level); + + if (rdata->public.dhcp_level != dhcp_level) { + rdata->public.dhcp_level = dhcp_level; + changed |= NM_NDISC_CONFIG_DHCP_LEVEL; + } + + for (i = 0; i < ra->gateways->len; i++) { + NMNDiscGateway *item = &g_array_index(ra->gateways, NMNDiscGateway, i); + + if (nm_ndisc_add_gateway(ndisc, item)) + changed |= NM_NDISC_CONFIG_GATEWAYS; + } + + for (i = 0; i < ra->prefixes->len; i++) { + FakePrefix * item = &g_array_index(ra->prefixes, FakePrefix, i); + NMNDiscRoute route = { + .network = item->network, + .plen = item->plen, + .gateway = item->gateway, + .timestamp = item->timestamp, + .lifetime = item->lifetime, + .preference = item->preference, + }; + + g_assert(route.plen > 0 && route.plen <= 128); + + if (nm_ndisc_add_route(ndisc, &route)) + changed |= NM_NDISC_CONFIG_ROUTES; + + if (item->plen == 64) { + NMNDiscAddress address = { + .address = item->network, + .timestamp = item->timestamp, + .lifetime = item->lifetime, + .preferred = item->preferred, + .dad_counter = 0, + }; + + if (nm_ndisc_complete_and_add_address(ndisc, &address, now)) + changed |= NM_NDISC_CONFIG_ADDRESSES; + } + } + + for (i = 0; i < ra->dns_servers->len; i++) { + NMNDiscDNSServer *item = &g_array_index(ra->dns_servers, NMNDiscDNSServer, i); + + if (nm_ndisc_add_dns_server(ndisc, item)) + changed |= NM_NDISC_CONFIG_DNS_SERVERS; + } + + for (i = 0; i < ra->dns_domains->len; i++) { + NMNDiscDNSDomain *item = &g_array_index(ra->dns_domains, NMNDiscDNSDomain, i); + + if (nm_ndisc_add_dns_domain(ndisc, item)) + changed |= NM_NDISC_CONFIG_DNS_DOMAINS; + } + + if (rdata->public.mtu != ra->mtu) { + rdata->public.mtu = ra->mtu; + changed |= NM_NDISC_CONFIG_MTU; + } + + if (rdata->public.hop_limit != ra->hop_limit) { + rdata->public.hop_limit = ra->hop_limit; + changed |= NM_NDISC_CONFIG_HOP_LIMIT; + } + + priv->ras = g_slist_remove(priv->ras, priv->ras->data); + fake_ra_free(ra); + + nm_ndisc_ra_received(NM_NDISC(self), now, changed); + + /* Schedule next RA */ + if (priv->ras) { + ra = priv->ras->data; + priv->receive_ra_id = g_timeout_add_seconds(ra->when, receive_ra, self); + } + + return G_SOURCE_REMOVE; } static void -start (NMNDisc *ndisc) +start(NMNDisc *ndisc) { - NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE (ndisc); - FakeRa *ra; + NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(ndisc); + FakeRa * ra; + + /* Queue up the first fake RA */ + g_assert(priv->ras); + ra = priv->ras->data; - /* Queue up the first fake RA */ - g_assert (priv->ras); - ra = priv->ras->data; + g_assert(!priv->receive_ra_id); + priv->receive_ra_id = g_timeout_add_seconds(ra->when, receive_ra, ndisc); +} + +static void +stop(NMNDisc *ndisc) +{ + NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(ndisc); - g_assert (!priv->receive_ra_id); - priv->receive_ra_id = g_timeout_add_seconds (ra->when, receive_ra, ndisc); + nm_clear_g_source(&priv->receive_ra_id); } void -nm_fake_ndisc_emit_new_ras (NMFakeNDisc *self) +nm_fake_ndisc_emit_new_ras(NMFakeNDisc *self) { - if (!NM_FAKE_NDISC_GET_PRIVATE (self)->receive_ra_id) - start (NM_NDISC (self)); + if (!NM_FAKE_NDISC_GET_PRIVATE(self)->receive_ra_id) + start(NM_NDISC(self)); } /*****************************************************************************/ static void -nm_fake_ndisc_init (NMFakeNDisc *fake_ndisc) -{ -} +nm_fake_ndisc_init(NMFakeNDisc *fake_ndisc) +{} NMNDisc * -nm_fake_ndisc_new (int ifindex, const char *ifname) +nm_fake_ndisc_new(int ifindex, const char *ifname) { - return g_object_new (NM_TYPE_FAKE_NDISC, - NM_NDISC_IFINDEX, ifindex, - NM_NDISC_IFNAME, ifname, - NM_NDISC_NODE_TYPE, (int) NM_NDISC_NODE_TYPE_HOST, - NM_NDISC_STABLE_TYPE, (int) NM_UTILS_STABLE_TYPE_UUID, - NM_NDISC_NETWORK_ID, "fake", - NULL); + return g_object_new(NM_TYPE_FAKE_NDISC, + NM_NDISC_IFINDEX, + ifindex, + NM_NDISC_IFNAME, + ifname, + NM_NDISC_NODE_TYPE, + (int) NM_NDISC_NODE_TYPE_HOST, + NM_NDISC_STABLE_TYPE, + (int) NM_UTILS_STABLE_TYPE_UUID, + NM_NDISC_NETWORK_ID, + "fake", + NM_NDISC_MAX_ADDRESSES, + NM_NDISC_MAX_ADDRESSES_DEFAULT, + NM_NDISC_ROUTER_SOLICITATIONS, + NM_NDISC_ROUTER_SOLICITATIONS_DEFAULT, + NM_NDISC_ROUTER_SOLICITATION_INTERVAL, + NM_NDISC_ROUTER_SOLICITATION_INTERVAL_DEFAULT, + NM_NDISC_RA_TIMEOUT, + 30u, + NULL); } static void -dispose (GObject *object) +dispose(GObject *object) { - NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE (object); + NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(object); - nm_clear_g_source (&priv->receive_ra_id); + nm_clear_g_source(&priv->receive_ra_id); - g_slist_free_full (priv->ras, fake_ra_free); - priv->ras = NULL; + g_slist_free_full(priv->ras, fake_ra_free); + priv->ras = NULL; - G_OBJECT_CLASS (nm_fake_ndisc_parent_class)->dispose (object); + G_OBJECT_CLASS(nm_fake_ndisc_parent_class)->dispose(object); } static void -nm_fake_ndisc_class_init (NMFakeNDiscClass *klass) +nm_fake_ndisc_class_init(NMFakeNDiscClass *klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); - NMNDiscClass *ndisc_class = NM_NDISC_CLASS (klass); - - object_class->dispose = dispose; - - ndisc_class->start = start; - ndisc_class->send_rs = send_rs; - - signals[RS_SENT] = - g_signal_new (NM_FAKE_NDISC_RS_SENT, - G_OBJECT_CLASS_TYPE (klass), - G_SIGNAL_RUN_FIRST, - 0, NULL, NULL, NULL, - G_TYPE_NONE, 0); + GObjectClass *object_class = G_OBJECT_CLASS(klass); + NMNDiscClass *ndisc_class = NM_NDISC_CLASS(klass); + + object_class->dispose = dispose; + + ndisc_class->start = start; + ndisc_class->stop = stop; + ndisc_class->send_rs = send_rs; + + signals[RS_SENT] = g_signal_new(NM_FAKE_NDISC_RS_SENT, + G_OBJECT_CLASS_TYPE(klass), + G_SIGNAL_RUN_FIRST, + 0, + NULL, + NULL, + NULL, + G_TYPE_NONE, + 0); } diff --git a/src/ndisc/nm-fake-ndisc.h b/src/ndisc/nm-fake-ndisc.h index 39b88184..47f40560 100644 --- a/src/ndisc/nm-fake-ndisc.h +++ b/src/ndisc/nm-fake-ndisc.h @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2013 Red Hat, Inc. */ @@ -8,59 +8,61 @@ #include "nm-ndisc.h" -#define NM_TYPE_FAKE_NDISC (nm_fake_ndisc_get_type ()) -#define NM_FAKE_NDISC(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_FAKE_NDISC, NMFakeNDisc)) -#define NM_FAKE_NDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_FAKE_NDISC, NMFakeNDiscClass)) -#define NM_IS_FAKE_NDISC(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_FAKE_NDISC)) -#define NM_IS_FAKE_NDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_FAKE_NDISC)) -#define NM_FAKE_NDISC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_FAKE_NDISC, NMFakeNDiscClass)) +#define NM_TYPE_FAKE_NDISC (nm_fake_ndisc_get_type()) +#define NM_FAKE_NDISC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_FAKE_NDISC, NMFakeNDisc)) +#define NM_FAKE_NDISC_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), NM_TYPE_FAKE_NDISC, NMFakeNDiscClass)) +#define NM_IS_FAKE_NDISC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), NM_TYPE_FAKE_NDISC)) +#define NM_IS_FAKE_NDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), NM_TYPE_FAKE_NDISC)) +#define NM_FAKE_NDISC_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), NM_TYPE_FAKE_NDISC, NMFakeNDiscClass)) #define NM_FAKE_NDISC_RS_SENT "rs-sent" -typedef struct _NMFakeRNDisc NMFakeNDisc; +typedef struct _NMFakeRNDisc NMFakeNDisc; typedef struct _NMFakeRNDiscClass NMFakeNDiscClass; -GType nm_fake_ndisc_get_type (void); +GType nm_fake_ndisc_get_type(void); -NMNDisc *nm_fake_ndisc_new (int ifindex, const char *ifname); +NMNDisc *nm_fake_ndisc_new(int ifindex, const char *ifname); -guint nm_fake_ndisc_add_ra (NMFakeNDisc *self, - guint seconds, - NMNDiscDHCPLevel dhcp_level, - int hop_limit, - guint32 mtu); +guint nm_fake_ndisc_add_ra(NMFakeNDisc * self, + guint seconds, + NMNDiscDHCPLevel dhcp_level, + int hop_limit, + guint32 mtu); -void nm_fake_ndisc_add_gateway (NMFakeNDisc *self, - guint ra_id, - const char *addr, - guint32 timestamp, - guint32 lifetime, - NMIcmpv6RouterPref preference); +void nm_fake_ndisc_add_gateway(NMFakeNDisc * self, + guint ra_id, + const char * addr, + guint32 timestamp, + guint32 lifetime, + NMIcmpv6RouterPref preference); -void nm_fake_ndisc_add_prefix (NMFakeNDisc *self, - guint ra_id, - const char *network, - guint plen, - const char *gateway, - guint32 timestamp, - guint32 lifetime, - guint32 preferred, - NMIcmpv6RouterPref preference); +void nm_fake_ndisc_add_prefix(NMFakeNDisc * self, + guint ra_id, + const char * network, + guint plen, + const char * gateway, + guint32 timestamp, + guint32 lifetime, + guint32 preferred, + NMIcmpv6RouterPref preference); -void nm_fake_ndisc_add_dns_server (NMFakeNDisc *self, - guint ra_id, - const char *address, - guint32 timestamp, - guint32 lifetime); +void nm_fake_ndisc_add_dns_server(NMFakeNDisc *self, + guint ra_id, + const char * address, + guint32 timestamp, + guint32 lifetime); -void nm_fake_ndisc_add_dns_domain (NMFakeNDisc *self, - guint ra_id, - const char *domain, - guint32 timestamp, - guint32 lifetime); +void nm_fake_ndisc_add_dns_domain(NMFakeNDisc *self, + guint ra_id, + const char * domain, + guint32 timestamp, + guint32 lifetime); -void nm_fake_ndisc_emit_new_ras (NMFakeNDisc *self); +void nm_fake_ndisc_emit_new_ras(NMFakeNDisc *self); -gboolean nm_fake_ndisc_done (NMFakeNDisc *self); +gboolean nm_fake_ndisc_done(NMFakeNDisc *self); #endif /* __NETWORKMANAGER_FAKE_NDISC_H__ */ diff --git a/src/ndisc/nm-lndp-ndisc.c b/src/ndisc/nm-lndp-ndisc.c index b10adc91..22695c32 100644 --- a/src/ndisc/nm-lndp-ndisc.c +++ b/src/ndisc/nm-lndp-ndisc.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2013 Red Hat, Inc. */ @@ -18,635 +18,735 @@ #include "platform/nm-platform.h" #include "platform/nmp-netns.h" -#define _NMLOG_PREFIX_NAME "ndisc-lndp" +#define _NMLOG_PREFIX_NAME "ndisc-lndp" /*****************************************************************************/ typedef struct { - struct ndp *ndp; - GSource *event_source; + struct ndp *ndp; + GSource * event_source; } NMLndpNDiscPrivate; /*****************************************************************************/ struct _NMLndpNDisc { - NMNDisc parent; - NMLndpNDiscPrivate _priv; + NMNDisc parent; + NMLndpNDiscPrivate _priv; }; struct _NMLndpNDiscClass { - NMNDiscClass parent; + NMNDiscClass parent; }; /*****************************************************************************/ -G_DEFINE_TYPE (NMLndpNDisc, nm_lndp_ndisc, NM_TYPE_NDISC) +G_DEFINE_TYPE(NMLndpNDisc, nm_lndp_ndisc, NM_TYPE_NDISC) -#define NM_LNDP_NDISC_GET_PRIVATE(self) _NM_GET_PRIVATE(self, NMLndpNDisc, NM_IS_LNDP_NDISC, NMNDisc) +#define NM_LNDP_NDISC_GET_PRIVATE(self) \ + _NM_GET_PRIVATE(self, NMLndpNDisc, NM_IS_LNDP_NDISC, NMNDisc) /*****************************************************************************/ static gboolean -send_rs (NMNDisc *ndisc, GError **error) +send_rs(NMNDisc *ndisc, GError **error) { - NMLndpNDiscPrivate *priv = NM_LNDP_NDISC_GET_PRIVATE (ndisc); - struct ndp_msg *msg; - int errsv; - - errsv = ndp_msg_new (&msg, NDP_MSG_RS); - if (errsv) { - g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, - "cannot create router solicitation"); - return FALSE; - } - ndp_msg_ifindex_set (msg, nm_ndisc_get_ifindex (ndisc)); - - errsv = ndp_msg_send (priv->ndp, msg); - ndp_msg_destroy (msg); - if (errsv) { - errsv = nm_errno_native (errsv); - g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, - "%s (%d)", - nm_strerror_native (errsv), errsv); - return FALSE; - } - - return TRUE; + NMLndpNDiscPrivate *priv = NM_LNDP_NDISC_GET_PRIVATE(ndisc); + struct ndp_msg * msg; + int errsv; + + errsv = ndp_msg_new(&msg, NDP_MSG_RS); + if (errsv) { + g_set_error_literal(error, + NM_UTILS_ERROR, + NM_UTILS_ERROR_UNKNOWN, + "cannot create router solicitation"); + return FALSE; + } + ndp_msg_ifindex_set(msg, nm_ndisc_get_ifindex(ndisc)); + + errsv = ndp_msg_send(priv->ndp, msg); + ndp_msg_destroy(msg); + if (errsv) { + errsv = nm_errno_native(errsv); + g_set_error(error, + NM_UTILS_ERROR, + NM_UTILS_ERROR_UNKNOWN, + "%s (%d)", + nm_strerror_native(errsv), + errsv); + return FALSE; + } + + return TRUE; } static NMIcmpv6RouterPref -_route_preference_coerce (enum ndp_route_preference pref) +_route_preference_coerce(enum ndp_route_preference pref) { - switch (pref) { - case NDP_ROUTE_PREF_LOW: - return NM_ICMPV6_ROUTER_PREF_LOW; - case NDP_ROUTE_PREF_MEDIUM: - return NM_ICMPV6_ROUTER_PREF_MEDIUM; - case NDP_ROUTE_PREF_HIGH: - return NM_ICMPV6_ROUTER_PREF_HIGH; - } - /* unexpected value must be treated as MEDIUM (RFC 4191). */ - return NM_ICMPV6_ROUTER_PREF_MEDIUM; + switch (pref) { + case NDP_ROUTE_PREF_LOW: + return NM_ICMPV6_ROUTER_PREF_LOW; + case NDP_ROUTE_PREF_MEDIUM: + return NM_ICMPV6_ROUTER_PREF_MEDIUM; + case NDP_ROUTE_PREF_HIGH: + return NM_ICMPV6_ROUTER_PREF_HIGH; + } + /* unexpected value must be treated as MEDIUM (RFC 4191). */ + return NM_ICMPV6_ROUTER_PREF_MEDIUM; } static int -receive_ra (struct ndp *ndp, struct ndp_msg *msg, gpointer user_data) +receive_ra(struct ndp *ndp, struct ndp_msg *msg, gpointer user_data) { - NMNDisc *ndisc = (NMNDisc *) user_data; - NMNDiscDataInternal *rdata = ndisc->rdata; - NMNDiscConfigMap changed = 0; - struct ndp_msgra *msgra = ndp_msgra (msg); - struct in6_addr gateway_addr; - gint32 now = nm_utils_get_monotonic_timestamp_sec (); - int offset; - int hop_limit; - guint32 val; - - /* Router discovery is subject to the following RFC documents: - * - * http://tools.ietf.org/html/rfc4861 - * http://tools.ietf.org/html/rfc4862 - * - * The biggest difference from good old DHCP is that all configuration - * items have their own lifetimes and they are merged from various - * sources. Router discovery is *not* contract-based, so there is *no* - * single time when the configuration is finished and updates can - * come at any time. - */ - _LOGD ("received router advertisement at %d", (int) now); - - gateway_addr = *ndp_msg_addrto (msg); - if (IN6_IS_ADDR_UNSPECIFIED (&gateway_addr)) - g_return_val_if_reached (0); - - /* DHCP level: - * - * The problem with DHCP level is what to do if subsequent - * router advertisements carry different flags. Currently we just - * rewrite the flag with every inbound RA. - */ - { - NMNDiscDHCPLevel dhcp_level; - - if (ndp_msgra_flag_managed (msgra)) - dhcp_level = NM_NDISC_DHCP_LEVEL_MANAGED; - else if (ndp_msgra_flag_other (msgra)) - dhcp_level = NM_NDISC_DHCP_LEVEL_OTHERCONF; - else - dhcp_level = NM_NDISC_DHCP_LEVEL_NONE; - - /* when receiving multiple RA (possibly from different routers), - * let's keep the "most managed" level. */ - G_STATIC_ASSERT_EXPR (NM_NDISC_DHCP_LEVEL_MANAGED > NM_NDISC_DHCP_LEVEL_OTHERCONF); - G_STATIC_ASSERT_EXPR (NM_NDISC_DHCP_LEVEL_OTHERCONF > NM_NDISC_DHCP_LEVEL_NONE); - dhcp_level = MAX (dhcp_level, rdata->public.dhcp_level); - - if (dhcp_level != rdata->public.dhcp_level) { - rdata->public.dhcp_level = dhcp_level; - changed |= NM_NDISC_CONFIG_DHCP_LEVEL; - } - } - - /* Default gateway: - * - * Subsequent router advertisements can represent new default gateways - * on the network. We should present all of them in router preference - * order. - */ - { - const NMNDiscGateway gateway = { - .address = gateway_addr, - .timestamp = now, - .lifetime = ndp_msgra_router_lifetime (msgra), - .preference = _route_preference_coerce (ndp_msgra_route_preference (msgra)), - }; - - if (nm_ndisc_add_gateway (ndisc, &gateway)) - changed |= NM_NDISC_CONFIG_GATEWAYS; - } - - /* Addresses & Routes */ - ndp_msg_opt_for_each_offset (offset, msg, NDP_MSG_OPT_PREFIX) { - guint8 r_plen; - struct in6_addr r_network; - - /* Device route */ - - r_plen = ndp_msg_opt_prefix_len (msg, offset); - if (r_plen == 0 || r_plen > 128) - continue; - nm_utils_ip6_address_clear_host_address (&r_network, ndp_msg_opt_prefix (msg, offset), r_plen); - - if ( IN6_IS_ADDR_UNSPECIFIED (&r_network) - || IN6_IS_ADDR_LINKLOCAL (&r_network)) - continue; - - if (ndp_msg_opt_prefix_flag_on_link (msg, offset)) { - const NMNDiscRoute route = { - .network = r_network, - .plen = r_plen, - .timestamp = now, - .lifetime = ndp_msg_opt_prefix_valid_time (msg, offset), - }; - - if (nm_ndisc_add_route (ndisc, &route)) - changed |= NM_NDISC_CONFIG_ROUTES; - } - - /* Address */ - if ( r_plen == 64 - && ndp_msg_opt_prefix_flag_auto_addr_conf (msg, offset)) { - NMNDiscAddress address = { - .address = r_network, - .timestamp = now, - .lifetime = ndp_msg_opt_prefix_valid_time (msg, offset), - .preferred = ndp_msg_opt_prefix_preferred_time (msg, offset), - }; - - if (address.preferred <= address.lifetime) { - if (nm_ndisc_complete_and_add_address (ndisc, &address, now)) - changed |= NM_NDISC_CONFIG_ADDRESSES; - } - } - } - ndp_msg_opt_for_each_offset(offset, msg, NDP_MSG_OPT_ROUTE) { - NMNDiscRoute route = { - .gateway = gateway_addr, - .plen = ndp_msg_opt_route_prefix_len (msg, offset), - .timestamp = now, - .lifetime = ndp_msg_opt_route_lifetime (msg, offset), - .preference = _route_preference_coerce (ndp_msg_opt_route_preference (msg, offset)), - }; - - if (route.plen == 0 || route.plen > 128) - continue; - - /* Routers through this particular gateway */ - nm_utils_ip6_address_clear_host_address (&route.network, ndp_msg_opt_route_prefix (msg, offset), route.plen); - if (nm_ndisc_add_route (ndisc, &route)) - changed |= NM_NDISC_CONFIG_ROUTES; - } - - /* DNS information */ - ndp_msg_opt_for_each_offset(offset, msg, NDP_MSG_OPT_RDNSS) { - struct in6_addr *addr; - int addr_index; - - ndp_msg_opt_rdnss_for_each_addr (addr, addr_index, msg, offset) { - NMNDiscDNSServer dns_server = { - .address = *addr, - .timestamp = now, - .lifetime = ndp_msg_opt_rdnss_lifetime (msg, offset), - }; - - /* Pad the lifetime somewhat to give a bit of slack in cases - * where one RA gets lost or something (which can happen on unreliable - * links like Wi-Fi where certain types of frames are not retransmitted). - * Note that 0 has special meaning and is therefore not adjusted. - */ - if (dns_server.lifetime && dns_server.lifetime < 7200) - dns_server.lifetime = 7200; - if (nm_ndisc_add_dns_server (ndisc, &dns_server)) - changed |= NM_NDISC_CONFIG_DNS_SERVERS; - } - } - ndp_msg_opt_for_each_offset(offset, msg, NDP_MSG_OPT_DNSSL) { - char *domain; - int domain_index; - - ndp_msg_opt_dnssl_for_each_domain (domain, domain_index, msg, offset) { - NMNDiscDNSDomain dns_domain = { - .domain = domain, - .timestamp = now, - .lifetime = ndp_msg_opt_dnssl_lifetime (msg, offset), - }; - - /* Pad the lifetime somewhat to give a bit of slack in cases - * where one RA gets lost or something (which can happen on unreliable - * links like Wi-Fi where certain types of frames are not retransmitted). - * Note that 0 has special meaning and is therefore not adjusted. - */ - if (dns_domain.lifetime && dns_domain.lifetime < 7200) - dns_domain.lifetime = 7200; - if (nm_ndisc_add_dns_domain (ndisc, &dns_domain)) - changed |= NM_NDISC_CONFIG_DNS_DOMAINS; - } - } - - hop_limit = ndp_msgra_curhoplimit (msgra); - if (rdata->public.hop_limit != hop_limit) { - rdata->public.hop_limit = hop_limit; - changed |= NM_NDISC_CONFIG_HOP_LIMIT; - } - - val = ndp_msgra_reachable_time (msgra); - if (val && rdata->public.reachable_time_ms != val) { - rdata->public.reachable_time_ms = val; - changed |= NM_NDISC_CONFIG_REACHABLE_TIME; - } - - val = ndp_msgra_retransmit_time (msgra); - if (val && rdata->public.retrans_timer_ms != val) { - rdata->public.retrans_timer_ms = val; - changed |= NM_NDISC_CONFIG_RETRANS_TIMER; - } - - /* MTU */ - ndp_msg_opt_for_each_offset(offset, msg, NDP_MSG_OPT_MTU) { - guint32 mtu = ndp_msg_opt_mtu(msg, offset); - if (mtu >= 1280) { - if (rdata->public.mtu != mtu) { - rdata->public.mtu = mtu; - changed |= NM_NDISC_CONFIG_MTU; - } - } else { - /* All sorts of bad things would happen if we accepted this. - * Kernel would set it, but would flush out all IPv6 addresses away - * from the link, even the link-local, and we wouldn't be able to - * listen for further RAs that could fix the MTU. */ - _LOGW ("MTU too small for IPv6 ignored: %d", mtu); - } - } - - nm_ndisc_ra_received (ndisc, now, changed); - return 0; + NMNDisc * ndisc = (NMNDisc *) user_data; + NMNDiscDataInternal *rdata = ndisc->rdata; + NMNDiscConfigMap changed = 0; + struct ndp_msgra * msgra = ndp_msgra(msg); + struct in6_addr gateway_addr; + gint32 now = nm_utils_get_monotonic_timestamp_sec(); + int offset; + int hop_limit; + guint32 val; + + /* Router discovery is subject to the following RFC documents: + * + * http://tools.ietf.org/html/rfc4861 + * http://tools.ietf.org/html/rfc4862 + * + * The biggest difference from good old DHCP is that all configuration + * items have their own lifetimes and they are merged from various + * sources. Router discovery is *not* contract-based, so there is *no* + * single time when the configuration is finished and updates can + * come at any time. + */ + _LOGD("received router advertisement at %d", (int) now); + + gateway_addr = *ndp_msg_addrto(msg); + if (IN6_IS_ADDR_UNSPECIFIED(&gateway_addr)) + g_return_val_if_reached(0); + + /* DHCP level: + * + * The problem with DHCP level is what to do if subsequent + * router advertisements carry different flags. Currently, we just + * rewrite the flag with every inbound RA. + */ + { + NMNDiscDHCPLevel dhcp_level; + + if (ndp_msgra_flag_managed(msgra)) + dhcp_level = NM_NDISC_DHCP_LEVEL_MANAGED; + else if (ndp_msgra_flag_other(msgra)) + dhcp_level = NM_NDISC_DHCP_LEVEL_OTHERCONF; + else + dhcp_level = NM_NDISC_DHCP_LEVEL_NONE; + + /* when receiving multiple RA (possibly from different routers), + * let's keep the "most managed" level. */ + G_STATIC_ASSERT_EXPR(NM_NDISC_DHCP_LEVEL_MANAGED > NM_NDISC_DHCP_LEVEL_OTHERCONF); + G_STATIC_ASSERT_EXPR(NM_NDISC_DHCP_LEVEL_OTHERCONF > NM_NDISC_DHCP_LEVEL_NONE); + dhcp_level = MAX(dhcp_level, rdata->public.dhcp_level); + + if (dhcp_level != rdata->public.dhcp_level) { + rdata->public.dhcp_level = dhcp_level; + changed |= NM_NDISC_CONFIG_DHCP_LEVEL; + } + } + + /* Default gateway: + * + * Subsequent router advertisements can represent new default gateways + * on the network. We should present all of them in router preference + * order. + */ + { + const NMNDiscGateway gateway = { + .address = gateway_addr, + .timestamp = now, + .lifetime = ndp_msgra_router_lifetime(msgra), + .preference = _route_preference_coerce(ndp_msgra_route_preference(msgra)), + }; + + if (nm_ndisc_add_gateway(ndisc, &gateway)) + changed |= NM_NDISC_CONFIG_GATEWAYS; + } + + /* Addresses & Routes */ + ndp_msg_opt_for_each_offset (offset, msg, NDP_MSG_OPT_PREFIX) { + guint8 r_plen; + struct in6_addr r_network; + + /* Device route */ + + r_plen = ndp_msg_opt_prefix_len(msg, offset); + if (r_plen == 0 || r_plen > 128) + continue; + nm_utils_ip6_address_clear_host_address(&r_network, + ndp_msg_opt_prefix(msg, offset), + r_plen); + + if (IN6_IS_ADDR_UNSPECIFIED(&r_network) || IN6_IS_ADDR_LINKLOCAL(&r_network)) + continue; + + if (ndp_msg_opt_prefix_flag_on_link(msg, offset)) { + const NMNDiscRoute route = { + .network = r_network, + .plen = r_plen, + .timestamp = now, + .lifetime = ndp_msg_opt_prefix_valid_time(msg, offset), + }; + + if (nm_ndisc_add_route(ndisc, &route)) + changed |= NM_NDISC_CONFIG_ROUTES; + } + + /* Address */ + if (r_plen == 64 && ndp_msg_opt_prefix_flag_auto_addr_conf(msg, offset)) { + NMNDiscAddress address = { + .address = r_network, + .timestamp = now, + .lifetime = ndp_msg_opt_prefix_valid_time(msg, offset), + .preferred = ndp_msg_opt_prefix_preferred_time(msg, offset), + }; + + if (address.preferred <= address.lifetime) { + if (nm_ndisc_complete_and_add_address(ndisc, &address, now)) + changed |= NM_NDISC_CONFIG_ADDRESSES; + } + } + } + ndp_msg_opt_for_each_offset (offset, msg, NDP_MSG_OPT_ROUTE) { + NMNDiscRoute route = { + .gateway = gateway_addr, + .plen = ndp_msg_opt_route_prefix_len(msg, offset), + .timestamp = now, + .lifetime = ndp_msg_opt_route_lifetime(msg, offset), + .preference = _route_preference_coerce(ndp_msg_opt_route_preference(msg, offset)), + }; + + if (route.plen == 0 || route.plen > 128) + continue; + + /* Routers through this particular gateway */ + nm_utils_ip6_address_clear_host_address(&route.network, + ndp_msg_opt_route_prefix(msg, offset), + route.plen); + if (nm_ndisc_add_route(ndisc, &route)) + changed |= NM_NDISC_CONFIG_ROUTES; + } + + /* DNS information */ + ndp_msg_opt_for_each_offset (offset, msg, NDP_MSG_OPT_RDNSS) { + struct in6_addr *addr; + int addr_index; + + ndp_msg_opt_rdnss_for_each_addr (addr, addr_index, msg, offset) { + NMNDiscDNSServer dns_server = { + .address = *addr, + .timestamp = now, + .lifetime = ndp_msg_opt_rdnss_lifetime(msg, offset), + }; + + /* Pad the lifetime somewhat to give a bit of slack in cases + * where one RA gets lost or something (which can happen on unreliable + * links like Wi-Fi where certain types of frames are not retransmitted). + * Note that 0 has special meaning and is therefore not adjusted. + */ + if (dns_server.lifetime && dns_server.lifetime < 7200) + dns_server.lifetime = 7200; + if (nm_ndisc_add_dns_server(ndisc, &dns_server)) + changed |= NM_NDISC_CONFIG_DNS_SERVERS; + } + } + ndp_msg_opt_for_each_offset (offset, msg, NDP_MSG_OPT_DNSSL) { + char *domain; + int domain_index; + + ndp_msg_opt_dnssl_for_each_domain (domain, domain_index, msg, offset) { + NMNDiscDNSDomain dns_domain = { + .domain = domain, + .timestamp = now, + .lifetime = ndp_msg_opt_dnssl_lifetime(msg, offset), + }; + + /* Pad the lifetime somewhat to give a bit of slack in cases + * where one RA gets lost or something (which can happen on unreliable + * links like Wi-Fi where certain types of frames are not retransmitted). + * Note that 0 has special meaning and is therefore not adjusted. + */ + if (dns_domain.lifetime && dns_domain.lifetime < 7200) + dns_domain.lifetime = 7200; + if (nm_ndisc_add_dns_domain(ndisc, &dns_domain)) + changed |= NM_NDISC_CONFIG_DNS_DOMAINS; + } + } + + hop_limit = ndp_msgra_curhoplimit(msgra); + if (rdata->public.hop_limit != hop_limit) { + rdata->public.hop_limit = hop_limit; + changed |= NM_NDISC_CONFIG_HOP_LIMIT; + } + + val = ndp_msgra_reachable_time(msgra); + if (val && rdata->public.reachable_time_ms != val) { + rdata->public.reachable_time_ms = val; + changed |= NM_NDISC_CONFIG_REACHABLE_TIME; + } + + val = ndp_msgra_retransmit_time(msgra); + if (val && rdata->public.retrans_timer_ms != val) { + rdata->public.retrans_timer_ms = val; + changed |= NM_NDISC_CONFIG_RETRANS_TIMER; + } + + /* MTU */ + ndp_msg_opt_for_each_offset (offset, msg, NDP_MSG_OPT_MTU) { + guint32 mtu = ndp_msg_opt_mtu(msg, offset); + if (mtu >= 1280) { + if (rdata->public.mtu != mtu) { + rdata->public.mtu = mtu; + changed |= NM_NDISC_CONFIG_MTU; + } + } else { + /* All sorts of bad things would happen if we accepted this. + * Kernel would set it, but would flush out all IPv6 addresses away + * from the link, even the link-local, and we wouldn't be able to + * listen for further RAs that could fix the MTU. */ + _LOGW("MTU too small for IPv6 ignored: %d", mtu); + } + } + + nm_ndisc_ra_received(ndisc, now, changed); + return 0; } static void * -_ndp_msg_add_option (struct ndp_msg *msg, int len) +_ndp_msg_add_option(struct ndp_msg *msg, int len) { - void *ret = (uint8_t *)msg + ndp_msg_payload_len (msg); + void *ret = (uint8_t *) msg + ndp_msg_payload_len(msg); - len += ndp_msg_payload_len (msg); - if (len > ndp_msg_payload_maxlen (msg)) - return NULL; + len += ndp_msg_payload_len(msg); + if (len > ndp_msg_payload_maxlen(msg)) + return NULL; - ndp_msg_payload_len_set (msg, len); - return ret; + ndp_msg_payload_len_set(msg, len); + return ret; } #define NM_ND_OPT_RDNSS 25 typedef struct { - struct nd_opt_hdr header; - uint16_t reserved; - uint32_t lifetime;; - struct in6_addr addrs[0]; + struct nd_opt_hdr header; + uint16_t reserved; + uint32_t lifetime; + struct in6_addr addrs[0]; } NMLndpRdnssOption; #define NM_ND_OPT_DNSSL 31 typedef struct { - struct nd_opt_hdr header; - uint16_t reserved; - uint32_t lifetime; - char search_list[0]; + struct nd_opt_hdr header; + uint16_t reserved; + uint32_t lifetime; + char search_list[0]; } NMLndpDnsslOption; static gboolean -send_ra (NMNDisc *ndisc, GError **error) +send_ra(NMNDisc *ndisc, GError **error) { - NMLndpNDiscPrivate *priv = NM_LNDP_NDISC_GET_PRIVATE (ndisc); - NMNDiscDataInternal *rdata = ndisc->rdata; - gint32 now = nm_utils_get_monotonic_timestamp_sec (); - int errsv; - struct in6_addr *addr; - struct ndp_msg *msg; - struct nd_opt_prefix_info *prefix; - int i; - - errsv = ndp_msg_new (&msg, NDP_MSG_RA); - if (errsv) { - g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, - "cannot create a router advertisement"); - return FALSE; - } - - ndp_msg_ifindex_set (msg, nm_ndisc_get_ifindex (ndisc)); - - /* Multicast to all nodes. */ - addr = ndp_msg_addrto (msg); - addr->s6_addr32[0] = htonl(0xff020000); - addr->s6_addr32[1] = 0; - addr->s6_addr32[2] = 0; - addr->s6_addr32[3] = htonl(0x1); - - ndp_msgra_router_lifetime_set (ndp_msgra (msg), NM_NDISC_ROUTER_LIFETIME); - - /* The device let us know about all addresses that the device got - * whose prefixes are suitable for delegating. Let's announce them. */ - for (i = 0; i < rdata->addresses->len; i++) { - NMNDiscAddress *address = &g_array_index (rdata->addresses, NMNDiscAddress, i); - guint32 age = NM_CLAMP ((gint64) now - (gint64) address->timestamp, 0, G_MAXUINT32 - 1); - guint32 lifetime = address->lifetime; - guint32 preferred = address->preferred; - - /* Clamp the life times if they're not forever. */ - if (lifetime != NM_NDISC_INFINITY) - lifetime = lifetime > age ? lifetime - age : 0; - if (preferred != NM_NDISC_INFINITY) - preferred = preferred > age ? preferred - age : 0; - - prefix = _ndp_msg_add_option (msg, sizeof(*prefix)); - if (!prefix) { - /* Maybe we could sent separate RAs, but why bother... */ - _LOGW ("The RA is too big, had to omit some some prefixes."); - break; - } - - prefix->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; - prefix->nd_opt_pi_len = 4; - prefix->nd_opt_pi_prefix_len = 64; - prefix->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK; - prefix->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO; - prefix->nd_opt_pi_valid_time = htonl(lifetime); - prefix->nd_opt_pi_preferred_time = htonl(preferred); - prefix->nd_opt_pi_prefix.s6_addr32[0] = address->address.s6_addr32[0]; - prefix->nd_opt_pi_prefix.s6_addr32[1] = address->address.s6_addr32[1]; - prefix->nd_opt_pi_prefix.s6_addr32[2] = 0; - prefix->nd_opt_pi_prefix.s6_addr32[3] = 0; - } - - if (rdata->dns_servers->len) { - NMLndpRdnssOption *option; - int len = sizeof(*option) + sizeof(option->addrs[0]) * rdata->dns_servers->len; - - option = _ndp_msg_add_option (msg, len); - if (option) { - option->header.nd_opt_type = NM_ND_OPT_RDNSS; - option->header.nd_opt_len = len / 8; - option->lifetime = htonl (900); - - for (i = 0; i < rdata->dns_servers->len; i++) { - NMNDiscDNSServer *dns_server = &g_array_index (rdata->dns_servers, NMNDiscDNSServer, i); - option->addrs[i] = dns_server->address; - } - } else { - _LOGW ("The RA is too big, had to omit DNS information."); - } - - } - - if (rdata->dns_domains->len) { - NMLndpDnsslOption *option; - NMNDiscDNSDomain *dns_server; - int len = sizeof(*option); - char *search_list; - - for (i = 0; i < rdata->dns_domains->len; i++) { - dns_server = &g_array_index (rdata->dns_domains, NMNDiscDNSDomain, i); - len += strlen (dns_server->domain) + 2; - } - len = (len + 8) & ~0x7; - - option = _ndp_msg_add_option (msg, len); - if (option) { - option->header.nd_opt_type = NM_ND_OPT_DNSSL; - option->header.nd_opt_len = len / 8; - option->lifetime = htonl (900); - - search_list = option->search_list; - for (i = 0; i < rdata->dns_domains->len; i++) { - NMNDiscDNSDomain *dns_domain = &g_array_index (rdata->dns_domains, NMNDiscDNSDomain, i); - uint8_t domain_len = strlen (dns_domain->domain); - - *search_list++ = domain_len; - memcpy (search_list, dns_domain->domain, domain_len); - search_list += domain_len; - *search_list++ = '\0'; - } - } else { - _LOGW ("The RA is too big, had to omit DNS search list."); - } - } - - errsv = ndp_msg_send (priv->ndp, msg); - - ndp_msg_destroy (msg); - if (errsv) { - errsv = nm_errno_native (errsv); - g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, - "%s (%d)", - nm_strerror_native (errsv), errsv); - return FALSE; - } - - return TRUE; + NMLndpNDiscPrivate * priv = NM_LNDP_NDISC_GET_PRIVATE(ndisc); + NMNDiscDataInternal * rdata = ndisc->rdata; + gint32 now = nm_utils_get_monotonic_timestamp_sec(); + int errsv; + struct in6_addr * addr; + struct ndp_msg * msg; + struct nd_opt_prefix_info *prefix; + int i; + + errsv = ndp_msg_new(&msg, NDP_MSG_RA); + if (errsv) { + g_set_error_literal(error, + NM_UTILS_ERROR, + NM_UTILS_ERROR_UNKNOWN, + "cannot create a router advertisement"); + return FALSE; + } + + ndp_msg_ifindex_set(msg, nm_ndisc_get_ifindex(ndisc)); + + /* Multicast to all nodes. */ + addr = ndp_msg_addrto(msg); + addr->s6_addr32[0] = htonl(0xff020000); + addr->s6_addr32[1] = 0; + addr->s6_addr32[2] = 0; + addr->s6_addr32[3] = htonl(0x1); + + ndp_msgra_router_lifetime_set(ndp_msgra(msg), NM_NDISC_ROUTER_LIFETIME); + + /* The device let us know about all addresses that the device got + * whose prefixes are suitable for delegating. Let's announce them. */ + for (i = 0; i < rdata->addresses->len; i++) { + NMNDiscAddress *address = &g_array_index(rdata->addresses, NMNDiscAddress, i); + guint32 age = NM_CLAMP((gint64) now - (gint64) address->timestamp, 0, G_MAXUINT32 - 1); + guint32 lifetime = address->lifetime; + guint32 preferred = address->preferred; + + /* Clamp the life times if they're not forever. */ + if (lifetime != NM_NDISC_INFINITY) + lifetime = lifetime > age ? lifetime - age : 0; + if (preferred != NM_NDISC_INFINITY) + preferred = preferred > age ? preferred - age : 0; + + prefix = _ndp_msg_add_option(msg, sizeof(*prefix)); + if (!prefix) { + /* Maybe we could sent separate RAs, but why bother... */ + _LOGW("The RA is too big, had to omit some some prefixes."); + break; + } + + prefix->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; + prefix->nd_opt_pi_len = 4; + prefix->nd_opt_pi_prefix_len = 64; + prefix->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK; + prefix->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO; + prefix->nd_opt_pi_valid_time = htonl(lifetime); + prefix->nd_opt_pi_preferred_time = htonl(preferred); + prefix->nd_opt_pi_prefix.s6_addr32[0] = address->address.s6_addr32[0]; + prefix->nd_opt_pi_prefix.s6_addr32[1] = address->address.s6_addr32[1]; + prefix->nd_opt_pi_prefix.s6_addr32[2] = 0; + prefix->nd_opt_pi_prefix.s6_addr32[3] = 0; + } + + if (rdata->dns_servers->len) { + NMLndpRdnssOption *option; + int len = sizeof(*option) + sizeof(option->addrs[0]) * rdata->dns_servers->len; + + option = _ndp_msg_add_option(msg, len); + if (option) { + option->header.nd_opt_type = NM_ND_OPT_RDNSS; + option->header.nd_opt_len = len / 8; + option->lifetime = htonl(900); + + for (i = 0; i < rdata->dns_servers->len; i++) { + NMNDiscDNSServer *dns_server = + &g_array_index(rdata->dns_servers, NMNDiscDNSServer, i); + option->addrs[i] = dns_server->address; + } + } else { + _LOGW("The RA is too big, had to omit DNS information."); + } + } + + if (rdata->dns_domains->len) { + NMLndpDnsslOption *option; + NMNDiscDNSDomain * dns_server; + int len = sizeof(*option); + char * search_list; + + for (i = 0; i < rdata->dns_domains->len; i++) { + dns_server = &g_array_index(rdata->dns_domains, NMNDiscDNSDomain, i); + len += strlen(dns_server->domain) + 2; + } + len = (len + 8) & ~0x7; + + option = _ndp_msg_add_option(msg, len); + if (option) { + option->header.nd_opt_type = NM_ND_OPT_DNSSL; + option->header.nd_opt_len = len / 8; + option->lifetime = htonl(900); + + search_list = option->search_list; + for (i = 0; i < rdata->dns_domains->len; i++) { + NMNDiscDNSDomain *dns_domain = + &g_array_index(rdata->dns_domains, NMNDiscDNSDomain, i); + uint8_t domain_len = strlen(dns_domain->domain); + + *search_list++ = domain_len; + memcpy(search_list, dns_domain->domain, domain_len); + search_list += domain_len; + *search_list++ = '\0'; + } + } else { + _LOGW("The RA is too big, had to omit DNS search list."); + } + } + + errsv = ndp_msg_send(priv->ndp, msg); + + ndp_msg_destroy(msg); + if (errsv) { + errsv = nm_errno_native(errsv); + g_set_error(error, + NM_UTILS_ERROR, + NM_UTILS_ERROR_UNKNOWN, + "%s (%d)", + nm_strerror_native(errsv), + errsv); + return FALSE; + } + + return TRUE; } static int -receive_rs (struct ndp *ndp, struct ndp_msg *msg, gpointer user_data) +receive_rs(struct ndp *ndp, struct ndp_msg *msg, gpointer user_data) { - NMNDisc *ndisc = user_data; + NMNDisc *ndisc = user_data; - nm_ndisc_rs_received (ndisc); - return 0; + nm_ndisc_rs_received(ndisc); + return 0; } static gboolean -event_ready (int fd, - GIOCondition condition, - gpointer user_data) +event_ready(int fd, GIOCondition condition, gpointer user_data) { - gs_unref_object NMNDisc *ndisc = g_object_ref (NM_NDISC (user_data)); - nm_auto_pop_netns NMPNetns *netns = NULL; - NMLndpNDiscPrivate *priv = NM_LNDP_NDISC_GET_PRIVATE (ndisc); + gs_unref_object NMNDisc *ndisc = g_object_ref(NM_NDISC(user_data)); + nm_auto_pop_netns NMPNetns *netns = NULL; + NMLndpNDiscPrivate * priv = NM_LNDP_NDISC_GET_PRIVATE(ndisc); - _LOGD ("processing libndp events"); + _LOGD("processing libndp events"); - if (!nm_ndisc_netns_push (ndisc, &netns)) { - /* something is very wrong. Stop handling events. */ - nm_clear_g_source_inst (&priv->event_source); - return G_SOURCE_REMOVE; - } + if (!nm_ndisc_netns_push(ndisc, &netns)) { + /* something is very wrong. Stop handling events. */ + nm_clear_g_source_inst(&priv->event_source); + return G_SOURCE_REMOVE; + } - ndp_callall_eventfd_handler (priv->ndp); - return G_SOURCE_CONTINUE; + ndp_callall_eventfd_handler(priv->ndp); + return G_SOURCE_CONTINUE; } static void -start (NMNDisc *ndisc) +start(NMNDisc *ndisc) { - NMLndpNDiscPrivate *priv = NM_LNDP_NDISC_GET_PRIVATE (ndisc); - int fd; - - g_return_if_fail (!priv->event_source); - - fd = ndp_get_eventfd (priv->ndp); - - priv->event_source = nm_g_unix_fd_source_new (fd, - G_IO_IN, - G_PRIORITY_DEFAULT, - event_ready, - ndisc, - NULL); - g_source_attach (priv->event_source, NULL); - - /* Flush any pending messages to avoid using obsolete information */ - event_ready (fd, 0, ndisc); - - switch (nm_ndisc_get_node_type (ndisc)) { - case NM_NDISC_NODE_TYPE_HOST: - ndp_msgrcv_handler_register (priv->ndp, receive_ra, NDP_MSG_RA, nm_ndisc_get_ifindex (ndisc), ndisc); - break; - case NM_NDISC_NODE_TYPE_ROUTER: - ndp_msgrcv_handler_register (priv->ndp, receive_rs, NDP_MSG_RS, nm_ndisc_get_ifindex (ndisc), ndisc); - break; - default: - g_assert_not_reached (); - } + NMLndpNDiscPrivate *priv = NM_LNDP_NDISC_GET_PRIVATE(ndisc); + int fd; + + g_return_if_fail(!priv->event_source); + + fd = ndp_get_eventfd(priv->ndp); + + priv->event_source = + nm_g_unix_fd_source_new(fd, G_IO_IN, G_PRIORITY_DEFAULT, event_ready, ndisc, NULL); + g_source_attach(priv->event_source, NULL); + + /* Flush any pending messages to avoid using obsolete information */ + event_ready(fd, 0, ndisc); + + switch (nm_ndisc_get_node_type(ndisc)) { + case NM_NDISC_NODE_TYPE_HOST: + ndp_msgrcv_handler_register(priv->ndp, + receive_ra, + NDP_MSG_RA, + nm_ndisc_get_ifindex(ndisc), + ndisc); + break; + case NM_NDISC_NODE_TYPE_ROUTER: + ndp_msgrcv_handler_register(priv->ndp, + receive_rs, + NDP_MSG_RS, + nm_ndisc_get_ifindex(ndisc), + ndisc); + break; + default: + g_assert_not_reached(); + } +} + +static void +_cleanup(NMNDisc *ndisc) +{ + NMLndpNDiscPrivate *priv = NM_LNDP_NDISC_GET_PRIVATE(ndisc); + + nm_clear_g_source_inst(&priv->event_source); + + if (priv->ndp) { + switch (nm_ndisc_get_node_type(ndisc)) { + case NM_NDISC_NODE_TYPE_HOST: + ndp_msgrcv_handler_unregister(priv->ndp, + receive_ra, + NDP_MSG_RA, + nm_ndisc_get_ifindex(ndisc), + ndisc); + break; + case NM_NDISC_NODE_TYPE_ROUTER: + ndp_msgrcv_handler_unregister(priv->ndp, + receive_rs, + NDP_MSG_RS, + nm_ndisc_get_ifindex(ndisc), + ndisc); + break; + default: + nm_assert_not_reached(); + break; + } + ndp_close(priv->ndp); + priv->ndp = NULL; + } +} + +static void +stop(NMNDisc *ndisc) +{ + _cleanup(ndisc); } /*****************************************************************************/ static int -ipv6_sysctl_get (NMPlatform *platform, const char *ifname, const char *property, int min, int max, int defval) +ipv6_sysctl_get(NMPlatform *platform, + const char *ifname, + const char *property, + int min, + int max, + int defval) { - return nm_platform_sysctl_ip_conf_get_int_checked (platform, - AF_INET6, - ifname, - property, - 10, - min, - max, - defval); + return nm_platform_sysctl_ip_conf_get_int_checked(platform, + AF_INET6, + ifname, + property, + 10, + min, + max, + defval); } -static void -nm_lndp_ndisc_init (NMLndpNDisc *lndp_ndisc) +void +nm_lndp_ndisc_get_sysctl(NMPlatform *platform, + const char *ifname, + int * out_max_addresses, + int * out_router_solicitations, + int * out_router_solicitation_interval, + guint32 * out_default_ra_timeout) { + int router_solicitation_interval = 0; + int router_solicitations = 0; + + if (out_max_addresses) { + *out_max_addresses = ipv6_sysctl_get(platform, + ifname, + "max_addresses", + 0, + G_MAXINT32, + NM_NDISC_MAX_ADDRESSES_DEFAULT); + } + if (out_router_solicitations || out_default_ra_timeout) { + router_solicitations = ipv6_sysctl_get(platform, + ifname, + "router_solicitations", + 1, + G_MAXINT32, + NM_NDISC_ROUTER_SOLICITATIONS_DEFAULT); + NM_SET_OUT(out_router_solicitations, router_solicitations); + } + if (out_router_solicitation_interval || out_default_ra_timeout) { + router_solicitation_interval = + ipv6_sysctl_get(platform, + ifname, + "router_solicitation_interval", + 1, + G_MAXINT32, + NM_NDISC_ROUTER_SOLICITATION_INTERVAL_DEFAULT); + NM_SET_OUT(out_router_solicitation_interval, router_solicitation_interval); + } + if (out_default_ra_timeout) { + *out_default_ra_timeout = + NM_MAX((((gint64) router_solicitations) * router_solicitation_interval) + 1, 30); + } } +/*****************************************************************************/ + +static void +nm_lndp_ndisc_init(NMLndpNDisc *lndp_ndisc) +{} + NMNDisc * -nm_lndp_ndisc_new (NMPlatform *platform, - int ifindex, - const char *ifname, - NMUtilsStableType stable_type, - const char *network_id, - NMSettingIP6ConfigAddrGenMode addr_gen_mode, - NMNDiscNodeType node_type, - gint32 ra_timeout, - GError **error) +nm_lndp_ndisc_new(NMPlatform * platform, + int ifindex, + const char * ifname, + NMUtilsStableType stable_type, + const char * network_id, + NMSettingIP6ConfigAddrGenMode addr_gen_mode, + NMNDiscNodeType node_type, + int max_addresses, + int router_solicitations, + int router_solicitation_interval, + guint32 ra_timeout, + GError ** error) { - nm_auto_pop_netns NMPNetns *netns = NULL; - NMNDisc *ndisc; - NMLndpNDiscPrivate *priv; - int errsv; - - g_return_val_if_fail (NM_IS_PLATFORM (platform), NULL); - g_return_val_if_fail (!error || !*error, NULL); - g_return_val_if_fail (network_id, NULL); - - if (!nm_platform_netns_push (platform, &netns)) - return NULL; - - ndisc = g_object_new (NM_TYPE_LNDP_NDISC, - NM_NDISC_PLATFORM, platform, - NM_NDISC_STABLE_TYPE, (int) stable_type, - NM_NDISC_IFINDEX, ifindex, - NM_NDISC_IFNAME, ifname, - NM_NDISC_NETWORK_ID, network_id, - NM_NDISC_ADDR_GEN_MODE, (int) addr_gen_mode, - NM_NDISC_NODE_TYPE, (int) node_type, - NM_NDISC_MAX_ADDRESSES, ipv6_sysctl_get (platform, ifname, - "max_addresses", - 0, G_MAXINT32, NM_NDISC_MAX_ADDRESSES_DEFAULT), - NM_NDISC_RA_TIMEOUT, (int) ra_timeout, - NM_NDISC_ROUTER_SOLICITATIONS, ipv6_sysctl_get (platform, ifname, - "router_solicitations", - 1, G_MAXINT32, NM_NDISC_ROUTER_SOLICITATIONS_DEFAULT), - NM_NDISC_ROUTER_SOLICITATION_INTERVAL, ipv6_sysctl_get (platform, ifname, - "router_solicitation_interval", - 1, G_MAXINT32, NM_NDISC_ROUTER_SOLICITATION_INTERVAL_DEFAULT), - NULL); - - priv = NM_LNDP_NDISC_GET_PRIVATE (ndisc); - - errsv = ndp_open (&priv->ndp); - - if (errsv != 0) { - errsv = nm_errno_native (errsv); - g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN, - "failure creating libndp socket: %s (%d)", - nm_strerror_native (errsv), errsv); - g_object_unref (ndisc); - return NULL; - } - return ndisc; + nm_auto_pop_netns NMPNetns *netns = NULL; + NMNDisc * ndisc; + NMLndpNDiscPrivate * priv; + int errsv; + + g_return_val_if_fail(NM_IS_PLATFORM(platform), NULL); + g_return_val_if_fail(!error || !*error, NULL); + g_return_val_if_fail(network_id, NULL); + + if (!nm_platform_netns_push(platform, &netns)) + return NULL; + + ndisc = g_object_new(NM_TYPE_LNDP_NDISC, + NM_NDISC_PLATFORM, + platform, + NM_NDISC_STABLE_TYPE, + (int) stable_type, + NM_NDISC_IFINDEX, + ifindex, + NM_NDISC_IFNAME, + ifname, + NM_NDISC_NETWORK_ID, + network_id, + NM_NDISC_ADDR_GEN_MODE, + (int) addr_gen_mode, + NM_NDISC_NODE_TYPE, + (int) node_type, + NM_NDISC_MAX_ADDRESSES, + max_addresses, + NM_NDISC_ROUTER_SOLICITATIONS, + router_solicitations, + NM_NDISC_ROUTER_SOLICITATION_INTERVAL, + router_solicitation_interval, + NM_NDISC_RA_TIMEOUT, + (guint) ra_timeout, + NULL); + + priv = NM_LNDP_NDISC_GET_PRIVATE(ndisc); + + errsv = ndp_open(&priv->ndp); + + if (errsv != 0) { + errsv = nm_errno_native(errsv); + g_set_error(error, + NM_UTILS_ERROR, + NM_UTILS_ERROR_UNKNOWN, + "failure creating libndp socket: %s (%d)", + nm_strerror_native(errsv), + errsv); + g_object_unref(ndisc); + return NULL; + } + return ndisc; } static void -dispose (GObject *object) +dispose(GObject *object) { - NMNDisc *ndisc = NM_NDISC (object); - NMLndpNDiscPrivate *priv = NM_LNDP_NDISC_GET_PRIVATE (ndisc); - - nm_clear_g_source_inst (&priv->event_source); - - if (priv->ndp) { - switch (nm_ndisc_get_node_type (ndisc)) { - case NM_NDISC_NODE_TYPE_HOST: - ndp_msgrcv_handler_unregister (priv->ndp, receive_ra, NDP_MSG_RA, nm_ndisc_get_ifindex (ndisc), ndisc); - break; - case NM_NDISC_NODE_TYPE_ROUTER: - ndp_msgrcv_handler_unregister (priv->ndp, receive_rs, NDP_MSG_RS, nm_ndisc_get_ifindex (ndisc), ndisc); - break; - default: - g_assert_not_reached (); - } - ndp_close (priv->ndp); - priv->ndp = NULL; - } - - G_OBJECT_CLASS (nm_lndp_ndisc_parent_class)->dispose (object); + NMNDisc *ndisc = NM_NDISC(object); + + _cleanup(ndisc); + + G_OBJECT_CLASS(nm_lndp_ndisc_parent_class)->dispose(object); } static void -nm_lndp_ndisc_class_init (NMLndpNDiscClass *klass) +nm_lndp_ndisc_class_init(NMLndpNDiscClass *klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); - NMNDiscClass *ndisc_class = NM_NDISC_CLASS (klass); - - object_class->dispose = dispose; - ndisc_class->start = start; - ndisc_class->send_rs = send_rs; - ndisc_class->send_ra = send_ra; + GObjectClass *object_class = G_OBJECT_CLASS(klass); + NMNDiscClass *ndisc_class = NM_NDISC_CLASS(klass); + + object_class->dispose = dispose; + ndisc_class->start = start; + ndisc_class->stop = stop; + ndisc_class->send_rs = send_rs; + ndisc_class->send_ra = send_ra; } diff --git a/src/ndisc/nm-lndp-ndisc.h b/src/ndisc/nm-lndp-ndisc.h index 82e7b2de..a275be3e 100644 --- a/src/ndisc/nm-lndp-ndisc.h +++ b/src/ndisc/nm-lndp-ndisc.h @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2013 Red Hat, Inc. */ @@ -9,26 +9,38 @@ #include "nm-ndisc.h" #include "nm-core-utils.h" -#define NM_TYPE_LNDP_NDISC (nm_lndp_ndisc_get_type ()) -#define NM_LNDP_NDISC(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_LNDP_NDISC, NMLndpNDisc)) -#define NM_LNDP_NDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_LNDP_NDISC, NMLndpNDiscClass)) -#define NM_IS_LNDP_NDISC(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_LNDP_NDISC)) -#define NM_IS_LNDP_NDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_LNDP_NDISC)) -#define NM_LNDP_NDISC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_LNDP_NDISC, NMLndpNDiscClass)) +#define NM_TYPE_LNDP_NDISC (nm_lndp_ndisc_get_type()) +#define NM_LNDP_NDISC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_LNDP_NDISC, NMLndpNDisc)) +#define NM_LNDP_NDISC_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), NM_TYPE_LNDP_NDISC, NMLndpNDiscClass)) +#define NM_IS_LNDP_NDISC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), NM_TYPE_LNDP_NDISC)) +#define NM_IS_LNDP_NDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), NM_TYPE_LNDP_NDISC)) +#define NM_LNDP_NDISC_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), NM_TYPE_LNDP_NDISC, NMLndpNDiscClass)) -typedef struct _NMLndpNDisc NMLndpNDisc; +typedef struct _NMLndpNDisc NMLndpNDisc; typedef struct _NMLndpNDiscClass NMLndpNDiscClass; -GType nm_lndp_ndisc_get_type (void); - -NMNDisc *nm_lndp_ndisc_new (NMPlatform *platform, - int ifindex, - const char *ifname, - NMUtilsStableType stable_type, - const char *network_id, - NMSettingIP6ConfigAddrGenMode addr_gen_mode, - NMNDiscNodeType node_type, - gint32 ra_timeout, - GError **error); +GType nm_lndp_ndisc_get_type(void); + +NMNDisc *nm_lndp_ndisc_new(NMPlatform * platform, + int ifindex, + const char * ifname, + NMUtilsStableType stable_type, + const char * network_id, + NMSettingIP6ConfigAddrGenMode addr_gen_mode, + NMNDiscNodeType node_type, + int max_addresses, + int router_solicitations, + int router_solicitation_interval, + guint32 ra_timeout, + GError ** error); + +void nm_lndp_ndisc_get_sysctl(NMPlatform *platform, + const char *ifname, + int * out_max_addresses, + int * out_router_solicitations, + int * out_router_solicitation_interval, + guint32 * out_default_ra_timeout); #endif /* __NETWORKMANAGER_LNDP_NDISC_H__ */ diff --git a/src/ndisc/nm-ndisc-private.h b/src/ndisc/nm-ndisc-private.h index d31ab31f..a79a920b 100644 --- a/src/ndisc/nm-ndisc-private.h +++ b/src/ndisc/nm-ndisc-private.h @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2015 Red Hat, Inc. */ @@ -11,50 +11,56 @@ /* Functions only used by ndisc implementations */ struct _NMNDiscDataInternal { - NMNDiscData public; - GArray *gateways; - GArray *addresses; - GArray *routes; - GArray *dns_servers; - GArray *dns_domains; + NMNDiscData public; + GArray *gateways; + GArray *addresses; + GArray *routes; + GArray *dns_servers; + GArray *dns_domains; }; typedef struct _NMNDiscDataInternal NMNDiscDataInternal; -void nm_ndisc_ra_received (NMNDisc *ndisc, gint32 now, NMNDiscConfigMap changed); -void nm_ndisc_rs_received (NMNDisc *ndisc); +void nm_ndisc_ra_received(NMNDisc *ndisc, gint32 now, NMNDiscConfigMap changed); +void nm_ndisc_rs_received(NMNDisc *ndisc); -gboolean nm_ndisc_add_gateway (NMNDisc *ndisc, const NMNDiscGateway *new); -gboolean nm_ndisc_complete_and_add_address (NMNDisc *ndisc, const NMNDiscAddress *new, gint32 now_s); -gboolean nm_ndisc_add_route (NMNDisc *ndisc, const NMNDiscRoute *new); -gboolean nm_ndisc_add_dns_server (NMNDisc *ndisc, const NMNDiscDNSServer *new); -gboolean nm_ndisc_add_dns_domain (NMNDisc *ndisc, const NMNDiscDNSDomain *new); +gboolean nm_ndisc_add_gateway(NMNDisc *ndisc, const NMNDiscGateway *new); +gboolean nm_ndisc_complete_and_add_address(NMNDisc *ndisc, const NMNDiscAddress *new, gint32 now_s); +gboolean nm_ndisc_add_route(NMNDisc *ndisc, const NMNDiscRoute *new); +gboolean nm_ndisc_add_dns_server(NMNDisc *ndisc, const NMNDiscDNSServer *new); +gboolean nm_ndisc_add_dns_domain(NMNDisc *ndisc, const NMNDiscDNSDomain *new); /*****************************************************************************/ -#define _NMLOG_DOMAIN LOGD_IP6 -#define _NMLOG(level, ...) _LOG(level, _NMLOG_DOMAIN, ndisc, __VA_ARGS__) - -#define _LOG(level, domain, self, ...) \ - G_STMT_START { \ - const NMLogLevel __level = (level); \ - const NMLogDomain __domain = (domain); \ - \ - if (nm_logging_enabled (__level, __domain)) { \ - NMNDisc *const __self = (self); \ - char __prefix[64]; \ - const char *__ifname = __self ? nm_ndisc_get_ifname (__self) : NULL; \ - \ - _nm_log (__level, __domain, 0, __ifname, NULL, \ - "%s: " _NM_UTILS_MACRO_FIRST (__VA_ARGS__), \ - (__self \ - ? nm_sprintf_buf (__prefix, "%s[%p,%s%s%s]", \ - _NMLOG_PREFIX_NAME, __self, \ - NM_PRINT_FMT_QUOTE_STRING (__ifname)) \ - : _NMLOG_PREFIX_NAME) \ - _NM_UTILS_MACRO_REST (__VA_ARGS__)); \ - } \ - } G_STMT_END +#define _NMLOG_DOMAIN LOGD_IP6 +#define _NMLOG(level, ...) _LOG(level, _NMLOG_DOMAIN, ndisc, __VA_ARGS__) + +#define _LOG(level, domain, self, ...) \ + G_STMT_START \ + { \ + const NMLogLevel __level = (level); \ + const NMLogDomain __domain = (domain); \ + \ + if (nm_logging_enabled(__level, __domain)) { \ + NMNDisc *const __self = (self); \ + char __prefix[64]; \ + const char * __ifname = __self ? nm_ndisc_get_ifname(__self) : NULL; \ + \ + _nm_log(__level, \ + __domain, \ + 0, \ + __ifname, \ + NULL, \ + "%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \ + (__self ? nm_sprintf_buf(__prefix, \ + "%s[%p,%s%s%s]", \ + _NMLOG_PREFIX_NAME, \ + __self, \ + NM_PRINT_FMT_QUOTE_STRING(__ifname)) \ + : _NMLOG_PREFIX_NAME) _NM_UTILS_MACRO_REST(__VA_ARGS__)); \ + } \ + } \ + G_STMT_END /*****************************************************************************/ diff --git a/src/ndisc/nm-ndisc.c b/src/ndisc/nm-ndisc.c index 5e556084..f8805c02 100644 --- a/src/ndisc/nm-ndisc.c +++ b/src/ndisc/nm-ndisc.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2013 Red Hat, Inc. */ @@ -16,96 +16,206 @@ #include "nm-utils.h" #include "platform/nm-platform.h" #include "platform/nmp-netns.h" +#include "nm-l3-config-data.h" -#define _NMLOG_PREFIX_NAME "ndisc" +#define _NMLOG_PREFIX_NAME "ndisc" /*****************************************************************************/ struct _NMNDiscPrivate { - /* this *must* be the first field. */ - NMNDiscDataInternal rdata; - - union { - gint32 solicitations_left; - gint32 announcements_left; - }; - union { - guint send_rs_id; - guint send_ra_id; - }; - union { - gint32 last_rs; - gint32 last_ra; - }; - guint ra_timeout_id; /* first RA timeout */ - guint timeout_id; /* prefix/dns/etc lifetime timeout */ - char *last_error; - NMUtilsIPv6IfaceId iid; - - /* immutable values: */ - int ifindex; - char *ifname; - char *network_id; - NMSettingIP6ConfigAddrGenMode addr_gen_mode; - NMUtilsStableType stable_type; - gint32 ra_timeout; - gint32 max_addresses; - gint32 router_solicitations; - gint32 router_solicitation_interval; - NMNDiscNodeType node_type; - - NMPlatform *platform; - NMPNetns *netns; + /* this *must* be the first field. */ + NMNDiscDataInternal rdata; + + char * last_error; + GSource *ra_timeout_source; + + union { + gint32 solicitations_left; + gint32 announcements_left; + }; + union { + guint send_rs_id; + guint send_ra_id; + }; + union { + gint32 last_rs; + gint32 last_ra; + }; + guint timeout_id; /* prefix/dns/etc lifetime timeout */ + NMUtilsIPv6IfaceId iid; + + /* immutable values: */ + int ifindex; + char * ifname; + char * network_id; + NMSettingIP6ConfigAddrGenMode addr_gen_mode; + NMUtilsStableType stable_type; + guint32 ra_timeout; + gint32 max_addresses; + gint32 router_solicitations; + gint32 router_solicitation_interval; + NMNDiscNodeType node_type; + + NMPlatform *platform; + NMPNetns * netns; }; typedef struct _NMNDiscPrivate NMNDiscPrivate; -NM_GOBJECT_PROPERTIES_DEFINE_BASE ( - PROP_PLATFORM, - PROP_IFINDEX, - PROP_IFNAME, - PROP_STABLE_TYPE, - PROP_NETWORK_ID, - PROP_ADDR_GEN_MODE, - PROP_MAX_ADDRESSES, - PROP_RA_TIMEOUT, - PROP_ROUTER_SOLICITATIONS, - PROP_ROUTER_SOLICITATION_INTERVAL, - PROP_NODE_TYPE, -); - -enum { - CONFIG_RECEIVED, - RA_TIMEOUT_SIGNAL, - LAST_SIGNAL -}; +NM_GOBJECT_PROPERTIES_DEFINE_BASE(PROP_PLATFORM, + PROP_IFINDEX, + PROP_IFNAME, + PROP_STABLE_TYPE, + PROP_NETWORK_ID, + PROP_ADDR_GEN_MODE, + PROP_MAX_ADDRESSES, + PROP_RA_TIMEOUT, + PROP_ROUTER_SOLICITATIONS, + PROP_ROUTER_SOLICITATION_INTERVAL, + PROP_NODE_TYPE, ); + +enum { CONFIG_RECEIVED, RA_TIMEOUT_SIGNAL, LAST_SIGNAL }; -static guint signals[LAST_SIGNAL] = { 0 }; +static guint signals[LAST_SIGNAL] = {0}; -G_DEFINE_TYPE (NMNDisc, nm_ndisc, G_TYPE_OBJECT) +G_DEFINE_TYPE(NMNDisc, nm_ndisc, G_TYPE_OBJECT) #define NM_NDISC_GET_PRIVATE(self) _NM_GET_PRIVATE_PTR(self, NMNDisc, NM_IS_NDISC) /*****************************************************************************/ -static void _config_changed_log (NMNDisc *ndisc, NMNDiscConfigMap changed); +static void _config_changed_log(NMNDisc *ndisc, NMNDiscConfigMap changed); + +/*****************************************************************************/ + +NML3ConfigData * +nm_ndisc_data_to_l3cd(NMDedupMultiIndex * multi_idx, + int ifindex, + const NMNDiscData * rdata, + NMSettingIP6ConfigPrivacy ip6_privacy, + guint32 route_table, + guint32 route_metric, + gboolean kernel_support_rta_pref, + gboolean kernel_support_extended_ifa_flags) +{ + nm_auto_unref_l3cd_init NML3ConfigData *l3cd = NULL; + guint32 ifa_flags; + guint8 plen; + guint i; + + l3cd = nm_l3_config_data_new(multi_idx, ifindex); + + nm_l3_config_data_set_source(l3cd, NM_IP_CONFIG_SOURCE_NDISC); + + nm_l3_config_data_set_ip6_privacy(l3cd, ip6_privacy); + + /* Check, whether kernel is recent enough to help user space handling RA. + * If it's not supported, we have no ipv6-privacy and must add autoconf + * addresses as /128. The reason for the /128 is to prevent the kernel + * from adding a prefix route for this address. */ + ifa_flags = 0; + if (kernel_support_extended_ifa_flags) { + ifa_flags |= IFA_F_NOPREFIXROUTE; + if (NM_IN_SET(ip6_privacy, + NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR, + NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR)) + ifa_flags |= IFA_F_MANAGETEMPADDR; + plen = 64; + } else + plen = 128; + + for (i = 0; i < rdata->addresses_n; i++) { + const NMNDiscAddress *ndisc_addr = &rdata->addresses[i]; + NMPlatformIP6Address a; + + a = (NMPlatformIP6Address){ + .ifindex = ifindex, + .address = ndisc_addr->address, + .plen = plen, + .timestamp = ndisc_addr->timestamp, + .lifetime = ndisc_addr->lifetime, + .preferred = MIN(ndisc_addr->lifetime, ndisc_addr->preferred), + .addr_source = NM_IP_CONFIG_SOURCE_NDISC, + .n_ifa_flags = ifa_flags, + }; + + nm_l3_config_data_add_address_6(l3cd, &a); + } + + for (i = 0; i < rdata->routes_n; i++) { + const NMNDiscRoute *ndisc_route = &rdata->routes[i]; + NMPlatformIP6Route r; + + r = (NMPlatformIP6Route){ + .ifindex = ifindex, + .network = ndisc_route->network, + .plen = ndisc_route->plen, + .gateway = ndisc_route->gateway, + .rt_source = NM_IP_CONFIG_SOURCE_NDISC, + .table_coerced = nm_platform_route_table_coerce(route_table), + .metric = route_metric, + .rt_pref = ndisc_route->preference, + }; + nm_assert((NMIcmpv6RouterPref) r.rt_pref == ndisc_route->preference); + + nm_l3_config_data_add_route_6(l3cd, &r); + } + + if (rdata->gateways_n > 0) { + const NMIcmpv6RouterPref first_pref = rdata->gateways[0].preference; + NMPlatformIP6Route r = { + .rt_source = NM_IP_CONFIG_SOURCE_NDISC, + .ifindex = ifindex, + .table_coerced = nm_platform_route_table_coerce(route_table), + .metric = route_metric, + }; + + for (i = 0; i < rdata->gateways_n; i++) { + r.gateway = rdata->gateways[i].address; + r.rt_pref = rdata->gateways[i].preference; + nm_assert((NMIcmpv6RouterPref) r.rt_pref == rdata->gateways[i].preference); + nm_l3_config_data_add_route_6(l3cd, &r); + + if (first_pref != rdata->gateways[i].preference && !kernel_support_rta_pref) { + /* We are unable to configure a router preference. Hence, we skip all gateways + * with a different preference from the first gateway. Note, that the gateways + * are sorted in order of highest to lowest preference. */ + break; + } + } + } + + for (i = 0; i < rdata->dns_servers_n; i++) + nm_l3_config_data_add_nameserver(l3cd, AF_INET6, &rdata->dns_servers[i].address); + + for (i = 0; i < rdata->dns_domains_n; i++) + nm_l3_config_data_add_search(l3cd, AF_INET6, rdata->dns_domains[i].domain); + + nm_l3_config_data_set_ndisc_hop_limit(l3cd, rdata->hop_limit); + nm_l3_config_data_set_ndisc_reachable_time_msec(l3cd, rdata->reachable_time_ms); + nm_l3_config_data_set_ndisc_retrans_timer_msec(l3cd, rdata->retrans_timer_ms); + + nm_l3_config_data_set_ip6_mtu(l3cd, rdata->mtu); + + return g_steal_pointer(&l3cd); +} /*****************************************************************************/ static guint8 -_preference_to_priority (NMIcmpv6RouterPref pref) +_preference_to_priority(NMIcmpv6RouterPref pref) { - switch (pref) { - case NM_ICMPV6_ROUTER_PREF_LOW: - return 1; - case NM_ICMPV6_ROUTER_PREF_MEDIUM: - return 2; - case NM_ICMPV6_ROUTER_PREF_HIGH: - return 3; - case NM_ICMPV6_ROUTER_PREF_INVALID: - break; - } - return 0; + switch (pref) { + case NM_ICMPV6_ROUTER_PREF_LOW: + return 1; + case NM_ICMPV6_ROUTER_PREF_MEDIUM: + return 2; + case NM_ICMPV6_ROUTER_PREF_HIGH: + return 3; + case NM_ICMPV6_ROUTER_PREF_INVALID: + break; + } + return 0; } /*****************************************************************************/ @@ -114,240 +224,243 @@ _preference_to_priority (NMIcmpv6RouterPref pref) #define _EXPIRY_INFINITY G_MAXINT64 static gint64 -get_expiry_time (guint32 timestamp, guint32 lifetime) +get_expiry_time(guint32 timestamp, guint32 lifetime) { - nm_assert (timestamp > 0); - nm_assert (timestamp <= G_MAXINT32); + nm_assert(timestamp > 0); + nm_assert(timestamp <= G_MAXINT32); - if (lifetime == NM_NDISC_INFINITY) - return _EXPIRY_INFINITY; - return ((gint64) timestamp) + ((gint64) lifetime); + if (lifetime == NM_NDISC_INFINITY) + return _EXPIRY_INFINITY; + return ((gint64) timestamp) + ((gint64) lifetime); } -#define get_expiry(item) \ - ({ \ - typeof (item) _item = (item); \ - nm_assert (_item); \ - get_expiry_time (_item->timestamp, _item->lifetime); \ - }) - -#define get_expiry_half(item) \ - ({ \ - typeof (item) _item = (item); \ - nm_assert (_item); \ - (_item->lifetime == NM_NDISC_INFINITY) \ - ? _EXPIRY_INFINITY \ - : get_expiry_time (_item->timestamp, _item->lifetime / 2); \ - }) - -#define get_expiry_preferred(item) \ - ({ \ - typeof (item) _item = (item); \ - nm_assert (_item); \ - get_expiry_time (_item->timestamp, _item->preferred); \ - }) +#define get_expiry(item) \ + ({ \ + typeof(item) _item = (item); \ + nm_assert(_item); \ + get_expiry_time(_item->timestamp, _item->lifetime); \ + }) + +#define get_expiry_half(item) \ + ({ \ + typeof(item) _item = (item); \ + nm_assert(_item); \ + (_item->lifetime == NM_NDISC_INFINITY) \ + ? _EXPIRY_INFINITY \ + : get_expiry_time(_item->timestamp, _item->lifetime / 2); \ + }) + +#define get_expiry_preferred(item) \ + ({ \ + typeof(item) _item = (item); \ + nm_assert(_item); \ + get_expiry_time(_item->timestamp, _item->preferred); \ + }) static gboolean -expiry_next (gint32 now_s, gint64 expiry_timestamp, gint32 *nextevent) +expiry_next(gint32 now_s, gint64 expiry_timestamp, gint32 *nextevent) { - gint32 e; - - if (expiry_timestamp == _EXPIRY_INFINITY) - return TRUE; - e = MIN (expiry_timestamp, ((gint64) (G_MAXINT32 - 1))); - if (now_s >= e) - return FALSE; - if (nextevent) { - if (*nextevent > e) - *nextevent = e; - } - return TRUE; + gint32 e; + + if (expiry_timestamp == _EXPIRY_INFINITY) + return TRUE; + e = MIN(expiry_timestamp, ((gint64)(G_MAXINT32 - 1))); + if (now_s >= e) + return FALSE; + if (nextevent) { + if (*nextevent > e) + *nextevent = e; + } + return TRUE; } static const char * -_get_exp (char *buf, gsize buf_size, gint64 now_ns, gint64 expiry_time) +_get_exp(char *buf, gsize buf_size, gint64 now_ns, gint64 expiry_time) { - int l; - - if (expiry_time == _EXPIRY_INFINITY) - return "permanent"; - l = g_snprintf (buf, buf_size, - "%.4f", - ((double) ((expiry_time * NM_UTILS_NSEC_PER_SEC) - now_ns)) / ((double) NM_UTILS_NSEC_PER_SEC)); - nm_assert (l < buf_size); - return buf; + int l; + + if (expiry_time == _EXPIRY_INFINITY) + return "permanent"; + l = g_snprintf(buf, + buf_size, + "%.4f", + ((double) ((expiry_time * NM_UTILS_NSEC_PER_SEC) - now_ns)) + / ((double) NM_UTILS_NSEC_PER_SEC)); + nm_assert(l < buf_size); + return buf; } -#define get_exp(buf, now_ns, item) \ - _get_exp ((buf), G_N_ELEMENTS (buf), (now_ns), (get_expiry (item))) +#define get_exp(buf, now_ns, item) _get_exp((buf), G_N_ELEMENTS(buf), (now_ns), (get_expiry(item))) /*****************************************************************************/ NMPNetns * -nm_ndisc_netns_get (NMNDisc *self) +nm_ndisc_netns_get(NMNDisc *self) { - g_return_val_if_fail (NM_IS_NDISC (self), NULL); + g_return_val_if_fail(NM_IS_NDISC(self), NULL); - return NM_NDISC_GET_PRIVATE (self)->netns; + return NM_NDISC_GET_PRIVATE(self)->netns; } gboolean -nm_ndisc_netns_push (NMNDisc *self, NMPNetns **netns) +nm_ndisc_netns_push(NMNDisc *self, NMPNetns **netns) { - NMNDiscPrivate *priv; + NMNDiscPrivate *priv; - g_return_val_if_fail (NM_IS_NDISC (self), FALSE); + g_return_val_if_fail(NM_IS_NDISC(self), FALSE); - priv = NM_NDISC_GET_PRIVATE (self); - if ( priv->netns - && !nmp_netns_push (priv->netns)) { - NM_SET_OUT (netns, NULL); - return FALSE; - } + priv = NM_NDISC_GET_PRIVATE(self); + if (priv->netns && !nmp_netns_push(priv->netns)) { + NM_SET_OUT(netns, NULL); + return FALSE; + } - NM_SET_OUT (netns, priv->netns); - return TRUE; + NM_SET_OUT(netns, priv->netns); + return TRUE; } /*****************************************************************************/ int -nm_ndisc_get_ifindex (NMNDisc *self) +nm_ndisc_get_ifindex(NMNDisc *self) { - g_return_val_if_fail (NM_IS_NDISC (self), 0); + g_return_val_if_fail(NM_IS_NDISC(self), 0); - return NM_NDISC_GET_PRIVATE (self)->ifindex; + return NM_NDISC_GET_PRIVATE(self)->ifindex; } const char * -nm_ndisc_get_ifname (NMNDisc *self) +nm_ndisc_get_ifname(NMNDisc *self) { - g_return_val_if_fail (NM_IS_NDISC (self), NULL); + g_return_val_if_fail(NM_IS_NDISC(self), NULL); - return NM_NDISC_GET_PRIVATE (self)->ifname; + return NM_NDISC_GET_PRIVATE(self)->ifname; } NMNDiscNodeType -nm_ndisc_get_node_type (NMNDisc *self) +nm_ndisc_get_node_type(NMNDisc *self) { - g_return_val_if_fail (NM_IS_NDISC (self), NM_NDISC_NODE_TYPE_INVALID); + g_return_val_if_fail(NM_IS_NDISC(self), NM_NDISC_NODE_TYPE_INVALID); - return NM_NDISC_GET_PRIVATE (self)->node_type; + return NM_NDISC_GET_PRIVATE(self)->node_type; } /*****************************************************************************/ static void -_ASSERT_data_gateways (const NMNDiscDataInternal *data) +_ASSERT_data_gateways(const NMNDiscDataInternal *data) { #if NM_MORE_ASSERTS > 10 - guint i, j; - const NMNDiscGateway *item_prev = NULL; + guint i, j; + const NMNDiscGateway *item_prev = NULL; - if (!data->gateways->len) - return; + if (!data->gateways->len) + return; - for (i = 0; i < data->gateways->len; i++) { - const NMNDiscGateway *item = &g_array_index (data->gateways, NMNDiscGateway, i); + for (i = 0; i < data->gateways->len; i++) { + const NMNDiscGateway *item = &g_array_index(data->gateways, NMNDiscGateway, i); - nm_assert (!IN6_IS_ADDR_UNSPECIFIED (&item->address)); - nm_assert (item->timestamp > 0 && item->timestamp <= G_MAXINT32); - for (j = 0; j < i; j++) { - const NMNDiscGateway *item2 = &g_array_index (data->gateways, NMNDiscGateway, j); + nm_assert(!IN6_IS_ADDR_UNSPECIFIED(&item->address)); + nm_assert(item->timestamp > 0 && item->timestamp <= G_MAXINT32); + for (j = 0; j < i; j++) { + const NMNDiscGateway *item2 = &g_array_index(data->gateways, NMNDiscGateway, j); - nm_assert (!IN6_ARE_ADDR_EQUAL (&item->address, &item2->address)); - } + nm_assert(!IN6_ARE_ADDR_EQUAL(&item->address, &item2->address)); + } - nm_assert (item->lifetime > 0); - if (i > 0) - nm_assert (_preference_to_priority (item_prev->preference) >= _preference_to_priority (item->preference)); + nm_assert(item->lifetime > 0); + if (i > 0) + nm_assert(_preference_to_priority(item_prev->preference) + >= _preference_to_priority(item->preference)); - item_prev = item; - } + item_prev = item; + } #endif } /*****************************************************************************/ static const NMNDiscData * -_data_complete (NMNDiscDataInternal *data) +_data_complete(NMNDiscDataInternal *data) { - _ASSERT_data_gateways (data); - -#define _SET(data, field) \ - G_STMT_START { \ - if ((data->public.field##_n = data->field->len) > 0) \ - data->public.field = (gpointer) data->field->data; \ - else \ - data->public.field = NULL; \ - } G_STMT_END - _SET (data, gateways); - _SET (data, addresses); - _SET (data, routes); - _SET (data, dns_servers); - _SET (data, dns_domains); + _ASSERT_data_gateways(data); + +#define _SET(data, field) \ + G_STMT_START \ + { \ + if ((data->public.field##_n = data->field->len) > 0) \ + data->public.field = (gpointer) data->field->data; \ + else \ + data->public.field = NULL; \ + } \ + G_STMT_END + _SET(data, gateways); + _SET(data, addresses); + _SET(data, routes); + _SET(data, dns_servers); + _SET(data, dns_domains); #undef _SET - return &data->public; + return &data->public; } void -nm_ndisc_emit_config_change (NMNDisc *self, NMNDiscConfigMap changed) +nm_ndisc_emit_config_change(NMNDisc *self, NMNDiscConfigMap changed) { - _config_changed_log (self, changed); - g_signal_emit (self, signals[CONFIG_RECEIVED], 0, - _data_complete (&NM_NDISC_GET_PRIVATE (self)->rdata), - (guint) changed); + _config_changed_log(self, changed); + g_signal_emit(self, + signals[CONFIG_RECEIVED], + 0, + _data_complete(&NM_NDISC_GET_PRIVATE(self)->rdata), + (guint) changed); } /*****************************************************************************/ gboolean -nm_ndisc_add_gateway (NMNDisc *ndisc, const NMNDiscGateway *new) +nm_ndisc_add_gateway(NMNDisc *ndisc, const NMNDiscGateway *new) { - NMNDiscDataInternal *rdata = &NM_NDISC_GET_PRIVATE(ndisc)->rdata; - guint i; - guint insert_idx = G_MAXUINT; - - for (i = 0; i < rdata->gateways->len; ) { - NMNDiscGateway *item = &g_array_index (rdata->gateways, NMNDiscGateway, i); - - if (IN6_ARE_ADDR_EQUAL (&item->address, &new->address)) { - if (new->lifetime == 0) { - g_array_remove_index (rdata->gateways, i); - _ASSERT_data_gateways (rdata); - return TRUE; - } - - if (item->preference != new->preference) { - g_array_remove_index (rdata->gateways, i); - continue; - } - - if (get_expiry (item) == get_expiry (new)) - return FALSE; - - *item = *new; - _ASSERT_data_gateways (rdata); - return TRUE; - } - - /* Put before less preferable gateways. */ - if ( _preference_to_priority (item->preference) < _preference_to_priority (new->preference) - && insert_idx == G_MAXUINT) - insert_idx = i; - - i++; - } - - if (new->lifetime) { - g_array_insert_val (rdata->gateways, - insert_idx == G_MAXUINT - ? rdata->gateways->len - : insert_idx, - *new); - } - _ASSERT_data_gateways (rdata); - return !!new->lifetime; + NMNDiscDataInternal *rdata = &NM_NDISC_GET_PRIVATE(ndisc)->rdata; + guint i; + guint insert_idx = G_MAXUINT; + + for (i = 0; i < rdata->gateways->len;) { + NMNDiscGateway *item = &g_array_index(rdata->gateways, NMNDiscGateway, i); + + if (IN6_ARE_ADDR_EQUAL(&item->address, &new->address)) { + if (new->lifetime == 0) { + g_array_remove_index(rdata->gateways, i); + _ASSERT_data_gateways(rdata); + return TRUE; + } + + if (item->preference != new->preference) { + g_array_remove_index(rdata->gateways, i); + continue; + } + + if (get_expiry(item) == get_expiry(new)) + return FALSE; + + *item = *new; + _ASSERT_data_gateways(rdata); + return TRUE; + } + + /* Put before less preferable gateways. */ + if (_preference_to_priority(item->preference) < _preference_to_priority(new->preference) + && insert_idx == G_MAXUINT) + insert_idx = i; + + i++; + } + + if (new->lifetime) { + g_array_insert_val(rdata->gateways, + insert_idx == G_MAXUINT ? rdata->gateways->len : insert_idx, + *new); + } + _ASSERT_data_gateways(rdata); + return !!new->lifetime; } /** @@ -365,485 +478,485 @@ nm_ndisc_add_gateway (NMNDisc *ndisc, const NMNDiscGateway *new) * Returns: %TRUE if the address could be completed, %FALSE otherwise. **/ static gboolean -complete_address (NMNDisc *ndisc, NMNDiscAddress *addr) +complete_address(NMNDisc *ndisc, NMNDiscAddress *addr) { - NMNDiscPrivate *priv; - GError *error = NULL; - - g_return_val_if_fail (NM_IS_NDISC (ndisc), FALSE); - - priv = NM_NDISC_GET_PRIVATE (ndisc); - if (priv->addr_gen_mode == NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_STABLE_PRIVACY) { - if (!nm_utils_ipv6_addr_set_stable_privacy (priv->stable_type, - &addr->address, - priv->ifname, - priv->network_id, - addr->dad_counter++, - &error)) { - _LOGW ("complete-address: failed to generate an stable-privacy address: %s", - error->message); - g_clear_error (&error); - return FALSE; - } - _LOGD ("complete-address: using an stable-privacy address"); - return TRUE; - } - - if (!priv->iid.id) { - _LOGW ("complete-address: can't generate an EUI-64 address: no interface identifier"); - return FALSE; - } - - if (addr->address.s6_addr32[2] == 0x0 && addr->address.s6_addr32[3] == 0x0) { - _LOGD ("complete-address: adding an EUI-64 address"); - nm_utils_ipv6_addr_set_interface_identifier (&addr->address, priv->iid); - return TRUE; - } - - _LOGW ("complete-address: can't generate a new EUI-64 address"); - return FALSE; + NMNDiscPrivate *priv; + GError * error = NULL; + + g_return_val_if_fail(NM_IS_NDISC(ndisc), FALSE); + + priv = NM_NDISC_GET_PRIVATE(ndisc); + if (priv->addr_gen_mode == NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_STABLE_PRIVACY) { + if (!nm_utils_ipv6_addr_set_stable_privacy(priv->stable_type, + &addr->address, + priv->ifname, + priv->network_id, + addr->dad_counter++, + &error)) { + _LOGW("complete-address: failed to generate an stable-privacy address: %s", + error->message); + g_clear_error(&error); + return FALSE; + } + _LOGD("complete-address: using an stable-privacy address"); + return TRUE; + } + + if (!priv->iid.id) { + _LOGW("complete-address: can't generate an EUI-64 address: no interface identifier"); + return FALSE; + } + + if (addr->address.s6_addr32[2] == 0x0 && addr->address.s6_addr32[3] == 0x0) { + _LOGD("complete-address: adding an EUI-64 address"); + nm_utils_ipv6_addr_set_interface_identifier(&addr->address, priv->iid); + return TRUE; + } + + _LOGW("complete-address: can't generate a new EUI-64 address"); + return FALSE; } static gboolean -nm_ndisc_add_address (NMNDisc *ndisc, - const NMNDiscAddress *new, - gint32 now_s, - gboolean from_ra) +nm_ndisc_add_address(NMNDisc *ndisc, const NMNDiscAddress *new, gint32 now_s, gboolean from_ra) { - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); - NMNDiscDataInternal *rdata = &priv->rdata; - NMNDiscAddress new2; - NMNDiscAddress *existing = NULL; - guint i; - - nm_assert (new); - nm_assert (new->timestamp > 0 && new->timestamp < G_MAXINT32); - nm_assert (!IN6_IS_ADDR_UNSPECIFIED (&new->address)); - nm_assert (!IN6_IS_ADDR_LINKLOCAL (&new->address)); - nm_assert (new->preferred <= new->lifetime); - nm_assert (!from_ra || now_s > 0); - - for (i = 0; i < rdata->addresses->len; i++) { - NMNDiscAddress *item = &g_array_index (rdata->addresses, NMNDiscAddress, i); - - if (from_ra) { - /* RFC4862 5.5.3.d, we find an existing address with the same prefix. - * (note that all prefixes at this point have implicitly length /64). */ - if (memcmp (&item->address, &new->address, 8) == 0) { - existing = item; - break; - } - } else { - if (IN6_ARE_ADDR_EQUAL (&item->address, &new->address)) { - existing = item; - break; - } - } - } - - if (existing) { - if (from_ra) { - const gint32 NM_NDISC_PREFIX_LFT_MIN = 7200; /* seconds, RFC4862 5.5.3.e */ - gint64 old_expiry_lifetime, old_expiry_preferred; - - old_expiry_lifetime = get_expiry (existing); - old_expiry_preferred = get_expiry_preferred (existing); - - if (new->lifetime == NM_NDISC_INFINITY) - existing->lifetime = NM_NDISC_INFINITY; - else { - gint64 new_lifetime, remaining_lifetime; - - /* see RFC4862 5.5.3.e */ - if (existing->lifetime == NM_NDISC_INFINITY) - remaining_lifetime = G_MAXINT64; - else - remaining_lifetime = ((gint64) existing->timestamp) + ((gint64) existing->lifetime) - ((gint64) now_s); - new_lifetime = ((gint64) new->timestamp) + ((gint64) new->lifetime) - ((gint64) now_s); - - if ( new_lifetime > (gint64) NM_NDISC_PREFIX_LFT_MIN - || new_lifetime > remaining_lifetime) { - existing->timestamp = now_s; - existing->lifetime = CLAMP (new_lifetime, (gint64) 0, (gint64) (G_MAXUINT32 - 1)); - } else if (remaining_lifetime <= (gint64) NM_NDISC_PREFIX_LFT_MIN) { - /* keep the current lifetime. */ - } else { - existing->timestamp = now_s; - existing->lifetime = NM_NDISC_PREFIX_LFT_MIN; - } - } - - if (new->preferred == NM_NDISC_INFINITY) { - nm_assert (existing->lifetime == NM_NDISC_INFINITY); - existing->preferred = new->preferred; - } else { - existing->preferred = NM_CLAMP (((gint64) new->timestamp) + ((gint64) new->preferred) - ((gint64) existing->timestamp), - 0, G_MAXUINT32 - 1); - if (existing->lifetime != NM_NDISC_INFINITY) - existing->preferred = MIN (existing->preferred, existing->lifetime); - } - - return old_expiry_lifetime != get_expiry (existing) - || old_expiry_preferred != get_expiry_preferred (existing); - } - - if (new->lifetime == 0) { - g_array_remove_index (rdata->addresses, i); - return TRUE; - } - - if ( get_expiry (existing) == get_expiry (new) - && get_expiry_preferred (existing) == get_expiry_preferred (new)) - return FALSE; - - existing->timestamp = new->timestamp; - existing->lifetime = new->lifetime; - existing->preferred = new->preferred; - return TRUE; - } - - /* we create at most max_addresses autoconf addresses. This is different from - * what the kernel does, because it considers *all* addresses (including - * static and other temporary addresses). - **/ - if ( priv->max_addresses - && rdata->addresses->len >= priv->max_addresses) - return FALSE; - - if (new->lifetime == 0) - return FALSE; - - if (from_ra) { - new2 = *new; - new2.dad_counter = 0; - if (!complete_address (ndisc, &new2)) - return FALSE; - new = &new2; - } - - g_array_append_val (rdata->addresses, *new); - return TRUE; + NMNDiscPrivate * priv = NM_NDISC_GET_PRIVATE(ndisc); + NMNDiscDataInternal *rdata = &priv->rdata; + NMNDiscAddress new2; + NMNDiscAddress * existing = NULL; + guint i; + + nm_assert(new); + nm_assert(new->timestamp > 0 && new->timestamp < G_MAXINT32); + nm_assert(!IN6_IS_ADDR_UNSPECIFIED(&new->address)); + nm_assert(!IN6_IS_ADDR_LINKLOCAL(&new->address)); + nm_assert(new->preferred <= new->lifetime); + nm_assert(!from_ra || now_s > 0); + + for (i = 0; i < rdata->addresses->len; i++) { + NMNDiscAddress *item = &g_array_index(rdata->addresses, NMNDiscAddress, i); + + if (from_ra) { + /* RFC4862 5.5.3.d, we find an existing address with the same prefix. + * (note that all prefixes at this point have implicitly length /64). */ + if (memcmp(&item->address, &new->address, 8) == 0) { + existing = item; + break; + } + } else { + if (IN6_ARE_ADDR_EQUAL(&item->address, &new->address)) { + existing = item; + break; + } + } + } + + if (existing) { + if (from_ra) { + const gint32 NM_NDISC_PREFIX_LFT_MIN = 7200; /* seconds, RFC4862 5.5.3.e */ + gint64 old_expiry_lifetime, old_expiry_preferred; + + old_expiry_lifetime = get_expiry(existing); + old_expiry_preferred = get_expiry_preferred(existing); + + if (new->lifetime == NM_NDISC_INFINITY) + existing->lifetime = NM_NDISC_INFINITY; + else { + gint64 new_lifetime, remaining_lifetime; + + /* see RFC4862 5.5.3.e */ + if (existing->lifetime == NM_NDISC_INFINITY) + remaining_lifetime = G_MAXINT64; + else + remaining_lifetime = ((gint64) existing->timestamp) + + ((gint64) existing->lifetime) - ((gint64) now_s); + new_lifetime = + ((gint64) new->timestamp) + ((gint64) new->lifetime) - ((gint64) now_s); + + if (new_lifetime > (gint64) NM_NDISC_PREFIX_LFT_MIN + || new_lifetime > remaining_lifetime) { + existing->timestamp = now_s; + existing->lifetime = CLAMP(new_lifetime, (gint64) 0, (gint64)(G_MAXUINT32 - 1)); + } else if (remaining_lifetime <= (gint64) NM_NDISC_PREFIX_LFT_MIN) { + /* keep the current lifetime. */ + } else { + existing->timestamp = now_s; + existing->lifetime = NM_NDISC_PREFIX_LFT_MIN; + } + } + + if (new->preferred == NM_NDISC_INFINITY) { + nm_assert(existing->lifetime == NM_NDISC_INFINITY); + existing->preferred = new->preferred; + } else { + existing->preferred = NM_CLAMP(((gint64) new->timestamp) + ((gint64) new->preferred) + - ((gint64) existing->timestamp), + 0, + G_MAXUINT32 - 1); + if (existing->lifetime != NM_NDISC_INFINITY) + existing->preferred = MIN(existing->preferred, existing->lifetime); + } + + return old_expiry_lifetime != get_expiry(existing) + || old_expiry_preferred != get_expiry_preferred(existing); + } + + if (new->lifetime == 0) { + g_array_remove_index(rdata->addresses, i); + return TRUE; + } + + if (get_expiry(existing) == get_expiry(new) + && get_expiry_preferred(existing) == get_expiry_preferred(new)) + return FALSE; + + existing->timestamp = new->timestamp; + existing->lifetime = new->lifetime; + existing->preferred = new->preferred; + return TRUE; + } + + /* we create at most max_addresses autoconf addresses. This is different from + * what the kernel does, because it considers *all* addresses (including + * static and other temporary addresses). + **/ + if (priv->max_addresses && rdata->addresses->len >= priv->max_addresses) + return FALSE; + + if (new->lifetime == 0) + return FALSE; + + if (from_ra) { + new2 = *new; + new2.dad_counter = 0; + if (!complete_address(ndisc, &new2)) + return FALSE; + new = &new2; + } + + g_array_append_val(rdata->addresses, *new); + return TRUE; } gboolean -nm_ndisc_complete_and_add_address (NMNDisc *ndisc, - const NMNDiscAddress *new, - gint32 now_s) +nm_ndisc_complete_and_add_address(NMNDisc *ndisc, const NMNDiscAddress *new, gint32 now_s) { - return nm_ndisc_add_address (ndisc, new, now_s, TRUE); + return nm_ndisc_add_address(ndisc, new, now_s, TRUE); } gboolean -nm_ndisc_add_route (NMNDisc *ndisc, const NMNDiscRoute *new) +nm_ndisc_add_route(NMNDisc *ndisc, const NMNDiscRoute *new) { - NMNDiscPrivate *priv; - NMNDiscDataInternal *rdata; - guint i; - guint insert_idx = G_MAXUINT; - - if (new->plen == 0 || new->plen > 128) { - /* Only expect non-default routes. The router has no idea what the - * local configuration or user preferences are, so sending routes - * with a prefix length of 0 must be ignored by NMNDisc. - * - * Also, upper layers also don't expect that NMNDisc exposes routes - * with a plen or zero or larger then 128. - */ - g_return_val_if_reached (FALSE); - } - - priv = NM_NDISC_GET_PRIVATE (ndisc); - rdata = &priv->rdata; - - for (i = 0; i < rdata->routes->len; ) { - NMNDiscRoute *item = &g_array_index (rdata->routes, NMNDiscRoute, i); - - if ( IN6_ARE_ADDR_EQUAL (&item->network, &new->network) - && item->plen == new->plen) { - if (new->lifetime == 0) { - g_array_remove_index (rdata->routes, i); - return TRUE; - } - - if (item->preference != new->preference) { - g_array_remove_index (rdata->routes, i); - continue; - } - - if ( get_expiry (item) == get_expiry (new) - && IN6_ARE_ADDR_EQUAL (&item->gateway, &new->gateway)) - return FALSE; - - *item = *new; - return TRUE; - } - - /* Put before less preferable routes. */ - if ( _preference_to_priority (item->preference) < _preference_to_priority (new->preference) - && insert_idx == G_MAXUINT) - insert_idx = i; - - i++; - } - - if (new->lifetime) { - g_array_insert_val (rdata->routes, - insert_idx == G_MAXUINT - ? 0u - : insert_idx, - *new); - } - return !!new->lifetime; + NMNDiscPrivate * priv; + NMNDiscDataInternal *rdata; + guint i; + guint insert_idx = G_MAXUINT; + + if (new->plen == 0 || new->plen > 128) { + /* Only expect non-default routes. The router has no idea what the + * local configuration or user preferences are, so sending routes + * with a prefix length of 0 must be ignored by NMNDisc. + * + * Also, upper layers also don't expect that NMNDisc exposes routes + * with a plen or zero or larger then 128. + */ + g_return_val_if_reached(FALSE); + } + + priv = NM_NDISC_GET_PRIVATE(ndisc); + rdata = &priv->rdata; + + for (i = 0; i < rdata->routes->len;) { + NMNDiscRoute *item = &g_array_index(rdata->routes, NMNDiscRoute, i); + + if (IN6_ARE_ADDR_EQUAL(&item->network, &new->network) && item->plen == new->plen) { + if (new->lifetime == 0) { + g_array_remove_index(rdata->routes, i); + return TRUE; + } + + if (item->preference != new->preference) { + g_array_remove_index(rdata->routes, i); + continue; + } + + if (get_expiry(item) == get_expiry(new) + && IN6_ARE_ADDR_EQUAL(&item->gateway, &new->gateway)) + return FALSE; + + *item = *new; + return TRUE; + } + + /* Put before less preferable routes. */ + if (_preference_to_priority(item->preference) < _preference_to_priority(new->preference) + && insert_idx == G_MAXUINT) + insert_idx = i; + + i++; + } + + if (new->lifetime) { + g_array_insert_val(rdata->routes, insert_idx == G_MAXUINT ? 0u : insert_idx, *new); + } + return !!new->lifetime; } gboolean -nm_ndisc_add_dns_server (NMNDisc *ndisc, const NMNDiscDNSServer *new) +nm_ndisc_add_dns_server(NMNDisc *ndisc, const NMNDiscDNSServer *new) { - NMNDiscPrivate *priv; - NMNDiscDataInternal *rdata; - guint i; + NMNDiscPrivate * priv; + NMNDiscDataInternal *rdata; + guint i; - priv = NM_NDISC_GET_PRIVATE (ndisc); - rdata = &priv->rdata; + priv = NM_NDISC_GET_PRIVATE(ndisc); + rdata = &priv->rdata; - for (i = 0; i < rdata->dns_servers->len; i++) { - NMNDiscDNSServer *item = &g_array_index (rdata->dns_servers, NMNDiscDNSServer, i); + for (i = 0; i < rdata->dns_servers->len; i++) { + NMNDiscDNSServer *item = &g_array_index(rdata->dns_servers, NMNDiscDNSServer, i); - if (IN6_ARE_ADDR_EQUAL (&item->address, &new->address)) { - if (new->lifetime == 0) { - g_array_remove_index (rdata->dns_servers, i); - return TRUE; - } + if (IN6_ARE_ADDR_EQUAL(&item->address, &new->address)) { + if (new->lifetime == 0) { + g_array_remove_index(rdata->dns_servers, i); + return TRUE; + } - if (get_expiry (item) == get_expiry (new)) - return FALSE; + if (get_expiry(item) == get_expiry(new)) + return FALSE; - *item = *new; - return TRUE; - } - } + *item = *new; + return TRUE; + } + } - if (new->lifetime) - g_array_append_val (rdata->dns_servers, *new); - return !!new->lifetime; + if (new->lifetime) + g_array_append_val(rdata->dns_servers, *new); + return !!new->lifetime; } /* Copies new->domain if 'new' is added to the dns_domains list */ gboolean -nm_ndisc_add_dns_domain (NMNDisc *ndisc, const NMNDiscDNSDomain *new) +nm_ndisc_add_dns_domain(NMNDisc *ndisc, const NMNDiscDNSDomain *new) { - NMNDiscPrivate *priv; - NMNDiscDataInternal *rdata; - NMNDiscDNSDomain *item; - guint i; - - priv = NM_NDISC_GET_PRIVATE (ndisc); - rdata = &priv->rdata; - - for (i = 0; i < rdata->dns_domains->len; i++) { - item = &g_array_index (rdata->dns_domains, NMNDiscDNSDomain, i); - - if (!g_strcmp0 (item->domain, new->domain)) { - if (new->lifetime == 0) { - g_array_remove_index (rdata->dns_domains, i); - return TRUE; - } - - if (get_expiry (item) == get_expiry (new)) - return FALSE; - - item->timestamp = new->timestamp; - item->lifetime = new->lifetime; - return TRUE; - } - } - - if (new->lifetime) { - g_array_append_val (rdata->dns_domains, *new); - item = &g_array_index (rdata->dns_domains, - NMNDiscDNSDomain, - rdata->dns_domains->len - 1); - item->domain = g_strdup (new->domain); - } - return !!new->lifetime; + NMNDiscPrivate * priv; + NMNDiscDataInternal *rdata; + NMNDiscDNSDomain * item; + guint i; + + priv = NM_NDISC_GET_PRIVATE(ndisc); + rdata = &priv->rdata; + + for (i = 0; i < rdata->dns_domains->len; i++) { + item = &g_array_index(rdata->dns_domains, NMNDiscDNSDomain, i); + + if (!g_strcmp0(item->domain, new->domain)) { + if (new->lifetime == 0) { + g_array_remove_index(rdata->dns_domains, i); + return TRUE; + } + + if (get_expiry(item) == get_expiry(new)) + return FALSE; + + item->timestamp = new->timestamp; + item->lifetime = new->lifetime; + return TRUE; + } + } + + if (new->lifetime) { + g_array_append_val(rdata->dns_domains, *new); + item = &g_array_index(rdata->dns_domains, NMNDiscDNSDomain, rdata->dns_domains->len - 1); + item->domain = g_strdup(new->domain); + } + return !!new->lifetime; } /*****************************************************************************/ -#define _MAYBE_WARN(...) G_STMT_START { \ - gboolean _different_message; \ - \ - _different_message = g_strcmp0 (priv->last_error, error->message) != 0; \ - _NMLOG (_different_message ? LOGL_WARN : LOGL_DEBUG, __VA_ARGS__); \ - if (_different_message) { \ - nm_clear_g_free (&priv->last_error); \ - priv->last_error = g_strdup (error->message); \ - } \ - } G_STMT_END +#define _MAYBE_WARN(...) \ + G_STMT_START \ + { \ + gboolean _different_message; \ + \ + _different_message = g_strcmp0(priv->last_error, error->message) != 0; \ + _NMLOG(_different_message ? LOGL_WARN : LOGL_DEBUG, __VA_ARGS__); \ + if (_different_message) { \ + nm_clear_g_free(&priv->last_error); \ + priv->last_error = g_strdup(error->message); \ + } \ + } \ + G_STMT_END static gboolean -send_rs_timeout (NMNDisc *ndisc) +send_rs_timeout(NMNDisc *ndisc) { - nm_auto_pop_netns NMPNetns *netns = NULL; - NMNDiscClass *klass = NM_NDISC_GET_CLASS (ndisc); - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); - GError *error = NULL; - - priv->send_rs_id = 0; - - if (!nm_ndisc_netns_push (ndisc, &netns)) - return G_SOURCE_REMOVE; - - if (klass->send_rs (ndisc, &error)) { - _LOGD ("router solicitation sent"); - priv->solicitations_left--; - nm_clear_g_free (&priv->last_error); - } else { - _MAYBE_WARN ("failure sending router solicitation: %s", error->message); - g_clear_error (&error); - } - - priv->last_rs = nm_utils_get_monotonic_timestamp_sec (); - if (priv->solicitations_left > 0) { - _LOGD ("scheduling router solicitation retry in %d seconds.", - (int) priv->router_solicitation_interval); - priv->send_rs_id = g_timeout_add_seconds (priv->router_solicitation_interval, - (GSourceFunc) send_rs_timeout, ndisc); - } else { - _LOGD ("did not receive a router advertisement after %d solicitations.", - (int) priv->router_solicitations); - } - - return G_SOURCE_REMOVE; + nm_auto_pop_netns NMPNetns *netns = NULL; + NMNDiscClass * klass = NM_NDISC_GET_CLASS(ndisc); + NMNDiscPrivate * priv = NM_NDISC_GET_PRIVATE(ndisc); + GError * error = NULL; + + priv->send_rs_id = 0; + + if (!nm_ndisc_netns_push(ndisc, &netns)) + return G_SOURCE_REMOVE; + + if (klass->send_rs(ndisc, &error)) { + _LOGD("router solicitation sent"); + priv->solicitations_left--; + nm_clear_g_free(&priv->last_error); + } else { + _MAYBE_WARN("failure sending router solicitation: %s", error->message); + g_clear_error(&error); + } + + priv->last_rs = nm_utils_get_monotonic_timestamp_sec(); + if (priv->solicitations_left > 0) { + _LOGD("scheduling router solicitation retry in %d seconds.", + (int) priv->router_solicitation_interval); + priv->send_rs_id = g_timeout_add_seconds(priv->router_solicitation_interval, + (GSourceFunc) send_rs_timeout, + ndisc); + } else { + _LOGD("did not receive a router advertisement after %d solicitations.", + (int) priv->router_solicitations); + } + + return G_SOURCE_REMOVE; } static void -solicit_routers (NMNDisc *ndisc) +solicit_routers(NMNDisc *ndisc) { - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); - gint32 now, next; - gint64 t; + NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE(ndisc); + gint32 now, next; + gint64 t; - if (priv->send_rs_id) - return; + if (priv->send_rs_id) + return; - now = nm_utils_get_monotonic_timestamp_sec (); - priv->solicitations_left = priv->router_solicitations; + now = nm_utils_get_monotonic_timestamp_sec(); + priv->solicitations_left = priv->router_solicitations; - t = (((gint64) priv->last_rs) + priv->router_solicitation_interval) - now; - next = CLAMP (t, 0, G_MAXINT32); - _LOGD ("scheduling explicit router solicitation request in %" G_GINT32_FORMAT " seconds.", - next); - priv->send_rs_id = g_timeout_add_seconds ((guint32) next, (GSourceFunc) send_rs_timeout, ndisc); + t = (((gint64) priv->last_rs) + priv->router_solicitation_interval) - now; + next = CLAMP(t, 0, G_MAXINT32); + _LOGD("scheduling explicit router solicitation request in %" G_GINT32_FORMAT " seconds.", next); + priv->send_rs_id = g_timeout_add_seconds((guint32) next, (GSourceFunc) send_rs_timeout, ndisc); } static gboolean -announce_router (NMNDisc *ndisc) +announce_router(NMNDisc *ndisc) { - nm_auto_pop_netns NMPNetns *netns = NULL; - NMNDiscClass *klass = NM_NDISC_GET_CLASS (ndisc); - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); - GError *error = NULL; - - if (!nm_ndisc_netns_push (ndisc, &netns)) - return G_SOURCE_REMOVE; - - priv->last_ra = nm_utils_get_monotonic_timestamp_sec (); - if (klass->send_ra (ndisc, &error)) { - _LOGD ("router advertisement sent"); - nm_clear_g_free (&priv->last_error); - } else { - _MAYBE_WARN ("failure sending router advertisement: %s", error->message); - g_clear_error (&error); - } - - if (--priv->announcements_left) { - _LOGD ("will resend an initial router advertisement"); - - /* Schedule next initial announcement retransmit. */ - priv->send_ra_id = g_timeout_add_seconds (g_random_int_range (NM_NDISC_ROUTER_ADVERT_DELAY, - NM_NDISC_ROUTER_ADVERT_INITIAL_INTERVAL), - (GSourceFunc) announce_router, ndisc); - } else { - _LOGD ("will send an unsolicited router advertisement"); - - /* Schedule next unsolicited announcement. */ - priv->announcements_left = 1; - priv->send_ra_id = g_timeout_add_seconds (NM_NDISC_ROUTER_ADVERT_MAX_INTERVAL, - (GSourceFunc) announce_router, - ndisc); - } - - return G_SOURCE_REMOVE; + nm_auto_pop_netns NMPNetns *netns = NULL; + NMNDiscClass * klass = NM_NDISC_GET_CLASS(ndisc); + NMNDiscPrivate * priv = NM_NDISC_GET_PRIVATE(ndisc); + GError * error = NULL; + + if (!nm_ndisc_netns_push(ndisc, &netns)) + return G_SOURCE_REMOVE; + + priv->last_ra = nm_utils_get_monotonic_timestamp_sec(); + if (klass->send_ra(ndisc, &error)) { + _LOGD("router advertisement sent"); + nm_clear_g_free(&priv->last_error); + } else { + _MAYBE_WARN("failure sending router advertisement: %s", error->message); + g_clear_error(&error); + } + + if (--priv->announcements_left) { + _LOGD("will resend an initial router advertisement"); + + /* Schedule next initial announcement retransmit. */ + priv->send_ra_id = + g_timeout_add_seconds(g_random_int_range(NM_NDISC_ROUTER_ADVERT_DELAY, + NM_NDISC_ROUTER_ADVERT_INITIAL_INTERVAL), + (GSourceFunc) announce_router, + ndisc); + } else { + _LOGD("will send an unsolicited router advertisement"); + + /* Schedule next unsolicited announcement. */ + priv->announcements_left = 1; + priv->send_ra_id = g_timeout_add_seconds(NM_NDISC_ROUTER_ADVERT_MAX_INTERVAL, + (GSourceFunc) announce_router, + ndisc); + } + + return G_SOURCE_REMOVE; } static void -announce_router_initial (NMNDisc *ndisc) +announce_router_initial(NMNDisc *ndisc) { - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); - - _LOGD ("will send an initial router advertisement"); - - /* Retry three more times. */ - priv->announcements_left = NM_NDISC_ROUTER_ADVERTISEMENTS_DEFAULT; - - /* Unschedule an unsolicited resend if we are allowed to send now. */ - if (G_LIKELY (nm_utils_get_monotonic_timestamp_sec () - priv->last_ra > NM_NDISC_ROUTER_ADVERT_DELAY)) - nm_clear_g_source (&priv->send_ra_id); - - /* Schedule the initial send rather early. Clamp the delay by minimal - * delay and not the initial advert internal so that we start fast. */ - if (G_LIKELY (!priv->send_ra_id)) { - priv->send_ra_id = g_timeout_add_seconds (g_random_int_range (0, NM_NDISC_ROUTER_ADVERT_DELAY), - (GSourceFunc) announce_router, ndisc); - } + NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE(ndisc); + + _LOGD("will send an initial router advertisement"); + + /* Retry three more times. */ + priv->announcements_left = NM_NDISC_ROUTER_ADVERTISEMENTS_DEFAULT; + + /* Unschedule an unsolicited resend if we are allowed to send now. */ + if (G_LIKELY(nm_utils_get_monotonic_timestamp_sec() - priv->last_ra + > NM_NDISC_ROUTER_ADVERT_DELAY)) + nm_clear_g_source(&priv->send_ra_id); + + /* Schedule the initial send rather early. Clamp the delay by minimal + * delay and not the initial advert internal so that we start fast. */ + if (G_LIKELY(!priv->send_ra_id)) { + priv->send_ra_id = + g_timeout_add_seconds(g_random_int_range(0, NM_NDISC_ROUTER_ADVERT_DELAY), + (GSourceFunc) announce_router, + ndisc); + } } static void -announce_router_solicited (NMNDisc *ndisc) +announce_router_solicited(NMNDisc *ndisc) { - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); + NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE(ndisc); - _LOGD ("will send an solicited router advertisement"); + _LOGD("will send an solicited router advertisement"); - /* Unschedule an unsolicited resend if we are allowed to send now. */ - if (nm_utils_get_monotonic_timestamp_sec () - priv->last_ra > NM_NDISC_ROUTER_ADVERT_DELAY) - nm_clear_g_source (&priv->send_ra_id); + /* Unschedule an unsolicited resend if we are allowed to send now. */ + if (nm_utils_get_monotonic_timestamp_sec() - priv->last_ra > NM_NDISC_ROUTER_ADVERT_DELAY) + nm_clear_g_source(&priv->send_ra_id); - if (!priv->send_ra_id) { - priv->send_ra_id = g_timeout_add (g_random_int_range (0, NM_NDISC_ROUTER_ADVERT_DELAY_MS), - (GSourceFunc) announce_router, ndisc); - } + if (!priv->send_ra_id) { + priv->send_ra_id = g_timeout_add(g_random_int_range(0, NM_NDISC_ROUTER_ADVERT_DELAY_MS), + (GSourceFunc) announce_router, + ndisc); + } } /*****************************************************************************/ void -nm_ndisc_set_config (NMNDisc *ndisc, - const GArray *addresses, - const GArray *dns_servers, - const GArray *dns_domains) +nm_ndisc_set_config(NMNDisc * ndisc, + const GArray *addresses, + const GArray *dns_servers, + const GArray *dns_domains) { - gboolean changed = FALSE; - guint i; - - for (i = 0; i < addresses->len; i++) { - if (nm_ndisc_add_address (ndisc, &g_array_index (addresses, NMNDiscAddress, i), 0, FALSE)) - changed = TRUE; - } - - for (i = 0; i < dns_servers->len; i++) { - if (nm_ndisc_add_dns_server (ndisc, &g_array_index (dns_servers, NMNDiscDNSServer, i))) - changed = TRUE; - } - - for (i = 0; i < dns_domains->len; i++) { - if (nm_ndisc_add_dns_domain (ndisc, &g_array_index (dns_domains, NMNDiscDNSDomain, i))) - changed = TRUE; - } - - if (changed) - announce_router_initial (ndisc); + gboolean changed = FALSE; + guint i; + + for (i = 0; i < addresses->len; i++) { + if (nm_ndisc_add_address(ndisc, &g_array_index(addresses, NMNDiscAddress, i), 0, FALSE)) + changed = TRUE; + } + + for (i = 0; i < dns_servers->len; i++) { + if (nm_ndisc_add_dns_server(ndisc, &g_array_index(dns_servers, NMNDiscDNSServer, i))) + changed = TRUE; + } + + for (i = 0; i < dns_domains->len; i++) { + if (nm_ndisc_add_dns_domain(ndisc, &g_array_index(dns_domains, NMNDiscDNSDomain, i))) + changed = TRUE; + } + + if (changed) + announce_router_initial(ndisc); } /** @@ -866,645 +979,718 @@ nm_ndisc_set_config (NMNDisc *ndisc, * Returns: %TRUE if addresses need to be regenerated, %FALSE otherwise. **/ gboolean -nm_ndisc_set_iid (NMNDisc *ndisc, const NMUtilsIPv6IfaceId iid) +nm_ndisc_set_iid(NMNDisc *ndisc, const NMUtilsIPv6IfaceId iid) { - NMNDiscPrivate *priv; - NMNDiscDataInternal *rdata; + NMNDiscPrivate * priv; + NMNDiscDataInternal *rdata; - g_return_val_if_fail (NM_IS_NDISC (ndisc), FALSE); + g_return_val_if_fail(NM_IS_NDISC(ndisc), FALSE); - priv = NM_NDISC_GET_PRIVATE (ndisc); - rdata = &priv->rdata; + priv = NM_NDISC_GET_PRIVATE(ndisc); + rdata = &priv->rdata; - if (priv->iid.id != iid.id) { - priv->iid = iid; + if (priv->iid.id != iid.id) { + priv->iid = iid; - if (priv->addr_gen_mode == NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_STABLE_PRIVACY) - return FALSE; + if (priv->addr_gen_mode == NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_STABLE_PRIVACY) + return FALSE; - if (rdata->addresses->len) { - _LOGD ("IPv6 interface identifier changed, flushing addresses"); - g_array_remove_range (rdata->addresses, 0, rdata->addresses->len); - nm_ndisc_emit_config_change (ndisc, NM_NDISC_CONFIG_ADDRESSES); - solicit_routers (ndisc); - } - return TRUE; - } + if (rdata->addresses->len) { + _LOGD("IPv6 interface identifier changed, flushing addresses"); + g_array_remove_range(rdata->addresses, 0, rdata->addresses->len); + nm_ndisc_emit_config_change(ndisc, NM_NDISC_CONFIG_ADDRESSES); + solicit_routers(ndisc); + } + return TRUE; + } - return FALSE; + return FALSE; } static gboolean -ndisc_ra_timeout_cb (gpointer user_data) +ndisc_ra_timeout_cb(gpointer user_data) { - NMNDisc *ndisc = NM_NDISC (user_data); + NMNDisc *ndisc = NM_NDISC(user_data); + + nm_clear_g_source_inst(&NM_NDISC_GET_PRIVATE(ndisc)->ra_timeout_source); + g_signal_emit(ndisc, signals[RA_TIMEOUT_SIGNAL], 0); + return G_SOURCE_REMOVE; +} - NM_NDISC_GET_PRIVATE (ndisc)->ra_timeout_id = 0; - g_signal_emit (ndisc, signals[RA_TIMEOUT_SIGNAL], 0); - return G_SOURCE_REMOVE; +void +nm_ndisc_start(NMNDisc *ndisc) +{ + nm_auto_pop_netns NMPNetns *netns = NULL; + NMNDiscPrivate * priv; + + g_return_if_fail(NM_IS_NDISC(ndisc)); + + priv = NM_NDISC_GET_PRIVATE(ndisc); + + nm_assert(NM_NDISC_GET_CLASS(ndisc)->start); + nm_assert(!priv->ra_timeout_source); + + _LOGD("starting neighbor discovery for ifindex %d%s", + priv->ifindex, + priv->node_type == NM_NDISC_NODE_TYPE_HOST ? " (solicit)" : " (announce)"); + + if (!nm_ndisc_netns_push(ndisc, &netns)) + return; + + NM_NDISC_GET_CLASS(ndisc)->start(ndisc); + + if (priv->node_type == NM_NDISC_NODE_TYPE_HOST) { + G_STATIC_ASSERT_EXPR(NM_RA_TIMEOUT_DEFAULT == 0); + G_STATIC_ASSERT_EXPR(NM_RA_TIMEOUT_INFINITY == G_MAXINT32); + nm_assert(priv->ra_timeout > 0u); + nm_assert(priv->ra_timeout <= NM_RA_TIMEOUT_INFINITY); + + if (priv->ra_timeout < NM_RA_TIMEOUT_INFINITY) { + guint timeout_msec; + + _LOGD("scheduling RA timeout in %u seconds", priv->ra_timeout); + if (priv->ra_timeout < G_MAXUINT / 1000u) + timeout_msec = priv->ra_timeout * 1000u; + else + timeout_msec = G_MAXUINT; + priv->ra_timeout_source = nm_g_timeout_source_new(timeout_msec, + G_PRIORITY_DEFAULT, + ndisc_ra_timeout_cb, + ndisc, + NULL); + g_source_attach(priv->ra_timeout_source, NULL); + } + + solicit_routers(ndisc); + return; + } + + nm_assert(priv->ra_timeout == 0u); + nm_assert(priv->node_type == NM_NDISC_NODE_TYPE_ROUTER); + announce_router_initial(ndisc); } void -nm_ndisc_start (NMNDisc *ndisc) +nm_ndisc_stop(NMNDisc *ndisc) { - nm_auto_pop_netns NMPNetns *netns = NULL; - NMNDiscPrivate *priv; - - g_return_if_fail (NM_IS_NDISC (ndisc)); - - priv = NM_NDISC_GET_PRIVATE (ndisc); - - nm_assert (NM_NDISC_GET_CLASS (ndisc)->start); - nm_assert (!priv->ra_timeout_id); - - _LOGD ("starting neighbor discovery for ifindex %d%s", - priv->ifindex, - priv->node_type == NM_NDISC_NODE_TYPE_HOST - ? " (solicit)" - : " (announce)"); - - if (!nm_ndisc_netns_push (ndisc, &netns)) - return; - - NM_NDISC_GET_CLASS (ndisc)->start (ndisc); - - if (priv->node_type == NM_NDISC_NODE_TYPE_HOST) { - gint32 ra_timeout = priv->ra_timeout; - - G_STATIC_ASSERT_EXPR (NM_RA_TIMEOUT_DEFAULT == 0); - G_STATIC_ASSERT_EXPR (NM_RA_TIMEOUT_INFINITY == G_MAXINT32); - if (ra_timeout != NM_RA_TIMEOUT_INFINITY) { - if (ra_timeout == NM_RA_TIMEOUT_DEFAULT) { - ra_timeout = NM_MAX ((((gint64) priv->router_solicitations) * priv->router_solicitation_interval) + 1, - 30); - } - nm_assert (ra_timeout > 0 && ra_timeout < NM_RA_TIMEOUT_INFINITY); - _LOGD ("scheduling RA timeout in %d seconds", ra_timeout); - priv->ra_timeout_id = g_timeout_add_seconds (ra_timeout, ndisc_ra_timeout_cb, ndisc); - } - solicit_routers (ndisc); - return; - } - - nm_assert (priv->node_type == NM_NDISC_NODE_TYPE_ROUTER); - announce_router_initial (ndisc); + nm_auto_pop_netns NMPNetns *netns = NULL; + NMNDiscDataInternal * rdata; + NMNDiscPrivate * priv; + + g_return_if_fail(NM_IS_NDISC(ndisc)); + + priv = NM_NDISC_GET_PRIVATE(ndisc); + + nm_assert(NM_NDISC_GET_CLASS(ndisc)->stop); + + _LOGD("stopping neighbor discovery for ifindex %d", priv->ifindex); + + if (!nm_ndisc_netns_push(ndisc, &netns)) + return; + + NM_NDISC_GET_CLASS(ndisc)->stop(ndisc); + + rdata = &priv->rdata; + + g_array_set_size(rdata->gateways, 0); + g_array_set_size(rdata->addresses, 0); + g_array_set_size(rdata->routes, 0); + g_array_set_size(rdata->dns_servers, 0); + g_array_set_size(rdata->dns_domains, 0); + priv->rdata.public.hop_limit = 64; + + /* Start at very low number so that last_rs - router_solicitation_interval + * is much lower than nm_utils_get_monotonic_timestamp_sec() at startup. + */ + priv->last_rs = G_MININT32; + nm_clear_g_source_inst(&priv->ra_timeout_source); + nm_clear_g_source(&priv->send_rs_id); + nm_clear_g_source(&priv->send_ra_id); + nm_clear_g_free(&priv->last_error); + nm_clear_g_source(&priv->timeout_id); + + priv->solicitations_left = 0; + priv->announcements_left = 0; + + priv->last_rs = G_MININT32; + priv->last_ra = G_MININT32; } NMNDiscConfigMap -nm_ndisc_dad_failed (NMNDisc *ndisc, const struct in6_addr *address, gboolean emit_changed_signal) +nm_ndisc_dad_failed(NMNDisc *ndisc, const struct in6_addr *address, gboolean emit_changed_signal) { - NMNDiscDataInternal *rdata; - guint i; - gboolean changed = FALSE; + NMNDiscDataInternal *rdata; + guint i; + gboolean changed = FALSE; - rdata = &NM_NDISC_GET_PRIVATE (ndisc)->rdata; + rdata = &NM_NDISC_GET_PRIVATE(ndisc)->rdata; - for (i = 0; i < rdata->addresses->len; ) { - NMNDiscAddress *item = &g_array_index (rdata->addresses, NMNDiscAddress, i); + for (i = 0; i < rdata->addresses->len;) { + NMNDiscAddress *item = &g_array_index(rdata->addresses, NMNDiscAddress, i); - if (IN6_ARE_ADDR_EQUAL (&item->address, address)) { - char sbuf[NM_UTILS_INET_ADDRSTRLEN]; + if (IN6_ARE_ADDR_EQUAL(&item->address, address)) { + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; - _LOGD ("DAD failed for discovered address %s", _nm_utils_inet6_ntop (address, sbuf)); - changed = TRUE; - if (!complete_address (ndisc, item)) { - g_array_remove_index (rdata->addresses, i); - continue; - } - } - i++; - } + _LOGD("DAD failed for discovered address %s", _nm_utils_inet6_ntop(address, sbuf)); + changed = TRUE; + if (!complete_address(ndisc, item)) { + g_array_remove_index(rdata->addresses, i); + continue; + } + } + i++; + } - if (emit_changed_signal && changed) - nm_ndisc_emit_config_change (ndisc, NM_NDISC_CONFIG_ADDRESSES); + if (emit_changed_signal && changed) + nm_ndisc_emit_config_change(ndisc, NM_NDISC_CONFIG_ADDRESSES); - return changed ? NM_NDISC_CONFIG_ADDRESSES : NM_NDISC_CONFIG_NONE; + return changed ? NM_NDISC_CONFIG_ADDRESSES : NM_NDISC_CONFIG_NONE; } #define CONFIG_MAP_MAX_STR 7 static void -config_map_to_string (NMNDiscConfigMap map, char *p) +config_map_to_string(NMNDiscConfigMap map, char *p) { - if (map & NM_NDISC_CONFIG_DHCP_LEVEL) - *p++ = 'd'; - if (map & NM_NDISC_CONFIG_GATEWAYS) - *p++ = 'G'; - if (map & NM_NDISC_CONFIG_ADDRESSES) - *p++ = 'A'; - if (map & NM_NDISC_CONFIG_ROUTES) - *p++ = 'R'; - if (map & NM_NDISC_CONFIG_DNS_SERVERS) - *p++ = 'S'; - if (map & NM_NDISC_CONFIG_DNS_DOMAINS) - *p++ = 'D'; - *p = '\0'; + if (map & NM_NDISC_CONFIG_DHCP_LEVEL) + *p++ = 'd'; + if (map & NM_NDISC_CONFIG_GATEWAYS) + *p++ = 'G'; + if (map & NM_NDISC_CONFIG_ADDRESSES) + *p++ = 'A'; + if (map & NM_NDISC_CONFIG_ROUTES) + *p++ = 'R'; + if (map & NM_NDISC_CONFIG_DNS_SERVERS) + *p++ = 'S'; + if (map & NM_NDISC_CONFIG_DNS_DOMAINS) + *p++ = 'D'; + *p = '\0'; } static const char * -dhcp_level_to_string (NMNDiscDHCPLevel dhcp_level) +dhcp_level_to_string(NMNDiscDHCPLevel dhcp_level) { - switch (dhcp_level) { - case NM_NDISC_DHCP_LEVEL_NONE: - return "none"; - case NM_NDISC_DHCP_LEVEL_OTHERCONF: - return "otherconf"; - case NM_NDISC_DHCP_LEVEL_MANAGED: - return "managed"; - default: - return "INVALID"; - } + switch (dhcp_level) { + case NM_NDISC_DHCP_LEVEL_NONE: + return "none"; + case NM_NDISC_DHCP_LEVEL_OTHERCONF: + return "otherconf"; + case NM_NDISC_DHCP_LEVEL_MANAGED: + return "managed"; + default: + return "INVALID"; + } } static void -_config_changed_log (NMNDisc *ndisc, NMNDiscConfigMap changed) +_config_changed_log(NMNDisc *ndisc, NMNDiscConfigMap changed) { - NMNDiscPrivate *priv; - NMNDiscDataInternal *rdata; - guint i; - char changedstr[CONFIG_MAP_MAX_STR]; - char addrstr[INET6_ADDRSTRLEN]; - char str_pref[35]; - char str_exp[100]; - gint64 now_ns; - - if (!_LOGD_ENABLED ()) - return; - - now_ns = nm_utils_get_monotonic_timestamp_nsec (); - - priv = NM_NDISC_GET_PRIVATE (ndisc); - rdata = &priv->rdata; - - config_map_to_string (changed, changedstr); - _LOGD ("neighbor discovery configuration changed [%s]:", changedstr); - _LOGD (" dhcp-level %s", dhcp_level_to_string (priv->rdata.public.dhcp_level)); - - if (rdata->public.hop_limit) - _LOGD (" hop limit : %d", rdata->public.hop_limit); - if (rdata->public.reachable_time_ms) - _LOGD (" reachable time : %u", (guint) rdata->public.reachable_time_ms); - if (rdata->public.retrans_timer_ms) - _LOGD (" retrans timer : %u", (guint) rdata->public.retrans_timer_ms); - - for (i = 0; i < rdata->gateways->len; i++) { - NMNDiscGateway *gateway = &g_array_index (rdata->gateways, NMNDiscGateway, i); - - inet_ntop (AF_INET6, &gateway->address, addrstr, sizeof (addrstr)); - _LOGD (" gateway %s pref %s exp %s", addrstr, - nm_icmpv6_router_pref_to_string (gateway->preference, str_pref, sizeof (str_pref)), - get_exp (str_exp, now_ns, gateway)); - } - for (i = 0; i < rdata->addresses->len; i++) { - const NMNDiscAddress *address = &g_array_index (rdata->addresses, NMNDiscAddress, i); - - inet_ntop (AF_INET6, &address->address, addrstr, sizeof (addrstr)); - _LOGD (" address %s exp %s", addrstr, - get_exp (str_exp, now_ns, address)); - } - for (i = 0; i < rdata->routes->len; i++) { - NMNDiscRoute *route = &g_array_index (rdata->routes, NMNDiscRoute, i); - char sbuf[NM_UTILS_INET_ADDRSTRLEN]; - - inet_ntop (AF_INET6, &route->network, addrstr, sizeof (addrstr)); - _LOGD (" route %s/%u via %s pref %s exp %s", addrstr, (guint) route->plen, - _nm_utils_inet6_ntop (&route->gateway, sbuf), - nm_icmpv6_router_pref_to_string (route->preference, str_pref, sizeof (str_pref)), - get_exp (str_exp, now_ns, route)); - } - for (i = 0; i < rdata->dns_servers->len; i++) { - NMNDiscDNSServer *dns_server = &g_array_index (rdata->dns_servers, NMNDiscDNSServer, i); - - inet_ntop (AF_INET6, &dns_server->address, addrstr, sizeof (addrstr)); - _LOGD (" dns_server %s exp %s", addrstr, - get_exp (str_exp, now_ns, dns_server)); - } - for (i = 0; i < rdata->dns_domains->len; i++) { - NMNDiscDNSDomain *dns_domain = &g_array_index (rdata->dns_domains, NMNDiscDNSDomain, i); - - _LOGD (" dns_domain %s exp %s", dns_domain->domain, - get_exp (str_exp, now_ns, dns_domain)); - } + NMNDiscPrivate * priv; + NMNDiscDataInternal *rdata; + guint i; + char changedstr[CONFIG_MAP_MAX_STR]; + char addrstr[INET6_ADDRSTRLEN]; + char str_pref[35]; + char str_exp[100]; + gint64 now_ns; + + if (!_LOGD_ENABLED()) + return; + + now_ns = nm_utils_get_monotonic_timestamp_nsec(); + + priv = NM_NDISC_GET_PRIVATE(ndisc); + rdata = &priv->rdata; + + config_map_to_string(changed, changedstr); + _LOGD("neighbor discovery configuration changed [%s]:", changedstr); + _LOGD(" dhcp-level %s", dhcp_level_to_string(priv->rdata.public.dhcp_level)); + + if (rdata->public.hop_limit) + _LOGD(" hop limit : %d", rdata->public.hop_limit); + if (rdata->public.reachable_time_ms) + _LOGD(" reachable time : %u", (guint) rdata->public.reachable_time_ms); + if (rdata->public.retrans_timer_ms) + _LOGD(" retrans timer : %u", (guint) rdata->public.retrans_timer_ms); + + for (i = 0; i < rdata->gateways->len; i++) { + NMNDiscGateway *gateway = &g_array_index(rdata->gateways, NMNDiscGateway, i); + + inet_ntop(AF_INET6, &gateway->address, addrstr, sizeof(addrstr)); + _LOGD(" gateway %s pref %s exp %s", + addrstr, + nm_icmpv6_router_pref_to_string(gateway->preference, str_pref, sizeof(str_pref)), + get_exp(str_exp, now_ns, gateway)); + } + for (i = 0; i < rdata->addresses->len; i++) { + const NMNDiscAddress *address = &g_array_index(rdata->addresses, NMNDiscAddress, i); + + inet_ntop(AF_INET6, &address->address, addrstr, sizeof(addrstr)); + _LOGD(" address %s exp %s", addrstr, get_exp(str_exp, now_ns, address)); + } + for (i = 0; i < rdata->routes->len; i++) { + NMNDiscRoute *route = &g_array_index(rdata->routes, NMNDiscRoute, i); + char sbuf[NM_UTILS_INET_ADDRSTRLEN]; + + inet_ntop(AF_INET6, &route->network, addrstr, sizeof(addrstr)); + _LOGD(" route %s/%u via %s pref %s exp %s", + addrstr, + (guint) route->plen, + _nm_utils_inet6_ntop(&route->gateway, sbuf), + nm_icmpv6_router_pref_to_string(route->preference, str_pref, sizeof(str_pref)), + get_exp(str_exp, now_ns, route)); + } + for (i = 0; i < rdata->dns_servers->len; i++) { + NMNDiscDNSServer *dns_server = &g_array_index(rdata->dns_servers, NMNDiscDNSServer, i); + + inet_ntop(AF_INET6, &dns_server->address, addrstr, sizeof(addrstr)); + _LOGD(" dns_server %s exp %s", addrstr, get_exp(str_exp, now_ns, dns_server)); + } + for (i = 0; i < rdata->dns_domains->len; i++) { + NMNDiscDNSDomain *dns_domain = &g_array_index(rdata->dns_domains, NMNDiscDNSDomain, i); + + _LOGD(" dns_domain %s exp %s", dns_domain->domain, get_exp(str_exp, now_ns, dns_domain)); + } } static void -clean_gateways (NMNDisc *ndisc, gint32 now, NMNDiscConfigMap *changed, gint32 *nextevent) +clean_gateways(NMNDisc *ndisc, gint32 now, NMNDiscConfigMap *changed, gint32 *nextevent) { - NMNDiscDataInternal *rdata; - guint i; + NMNDiscDataInternal *rdata; + guint i; - rdata = &NM_NDISC_GET_PRIVATE (ndisc)->rdata; + rdata = &NM_NDISC_GET_PRIVATE(ndisc)->rdata; - for (i = 0; i < rdata->gateways->len; ) { - NMNDiscGateway *item = &g_array_index (rdata->gateways, NMNDiscGateway, i); + for (i = 0; i < rdata->gateways->len;) { + NMNDiscGateway *item = &g_array_index(rdata->gateways, NMNDiscGateway, i); - if (!expiry_next (now, get_expiry (item), nextevent)) { - g_array_remove_index (rdata->gateways, i); - *changed |= NM_NDISC_CONFIG_GATEWAYS; - continue; - } + if (!expiry_next(now, get_expiry(item), nextevent)) { + g_array_remove_index(rdata->gateways, i); + *changed |= NM_NDISC_CONFIG_GATEWAYS; + continue; + } - i++; - } + i++; + } - _ASSERT_data_gateways (rdata); + _ASSERT_data_gateways(rdata); } static void -clean_addresses (NMNDisc *ndisc, gint32 now, NMNDiscConfigMap *changed, gint32 *nextevent) +clean_addresses(NMNDisc *ndisc, gint32 now, NMNDiscConfigMap *changed, gint32 *nextevent) { - NMNDiscDataInternal *rdata; - guint i; + NMNDiscDataInternal *rdata; + guint i; - rdata = &NM_NDISC_GET_PRIVATE (ndisc)->rdata; + rdata = &NM_NDISC_GET_PRIVATE(ndisc)->rdata; - for (i = 0; i < rdata->addresses->len; ) { - const NMNDiscAddress *item = &g_array_index (rdata->addresses, NMNDiscAddress, i); + for (i = 0; i < rdata->addresses->len;) { + const NMNDiscAddress *item = &g_array_index(rdata->addresses, NMNDiscAddress, i); - if (!expiry_next (now, get_expiry (item), nextevent)) { - g_array_remove_index (rdata->addresses, i); - *changed |= NM_NDISC_CONFIG_ADDRESSES; - continue; - } + if (!expiry_next(now, get_expiry(item), nextevent)) { + g_array_remove_index(rdata->addresses, i); + *changed |= NM_NDISC_CONFIG_ADDRESSES; + continue; + } - i++; - } + i++; + } } static void -clean_routes (NMNDisc *ndisc, gint32 now, NMNDiscConfigMap *changed, gint32 *nextevent) +clean_routes(NMNDisc *ndisc, gint32 now, NMNDiscConfigMap *changed, gint32 *nextevent) { - NMNDiscDataInternal *rdata; - guint i; + NMNDiscDataInternal *rdata; + guint i; - rdata = &NM_NDISC_GET_PRIVATE (ndisc)->rdata; + rdata = &NM_NDISC_GET_PRIVATE(ndisc)->rdata; - for (i = 0; i < rdata->routes->len; ) { - NMNDiscRoute *item = &g_array_index (rdata->routes, NMNDiscRoute, i); + for (i = 0; i < rdata->routes->len;) { + NMNDiscRoute *item = &g_array_index(rdata->routes, NMNDiscRoute, i); - if (!expiry_next (now, get_expiry (item), nextevent)) { - g_array_remove_index (rdata->routes, i); - *changed |= NM_NDISC_CONFIG_ROUTES; - continue; - } + if (!expiry_next(now, get_expiry(item), nextevent)) { + g_array_remove_index(rdata->routes, i); + *changed |= NM_NDISC_CONFIG_ROUTES; + continue; + } - i++; - } + i++; + } } static void -clean_dns_servers (NMNDisc *ndisc, gint32 now, NMNDiscConfigMap *changed, gint32 *nextevent) +clean_dns_servers(NMNDisc *ndisc, gint32 now, NMNDiscConfigMap *changed, gint32 *nextevent) { - NMNDiscDataInternal *rdata; - guint i; - - rdata = &NM_NDISC_GET_PRIVATE (ndisc)->rdata; - - for (i = 0; i < rdata->dns_servers->len; ) { - NMNDiscDNSServer *item = &g_array_index (rdata->dns_servers, NMNDiscDNSServer, i); - gint64 refresh; - - refresh = get_expiry_half (item); - if (refresh != _EXPIRY_INFINITY) { - if (!expiry_next (now, get_expiry (item), NULL)) { - g_array_remove_index (rdata->dns_servers, i); - *changed |= NM_NDISC_CONFIG_DNS_SERVERS; - continue; - } - - if (now >= refresh) - solicit_routers (ndisc); - else if (*nextevent > refresh) - *nextevent = refresh; - } - i++; - } + NMNDiscDataInternal *rdata; + guint i; + + rdata = &NM_NDISC_GET_PRIVATE(ndisc)->rdata; + + for (i = 0; i < rdata->dns_servers->len;) { + NMNDiscDNSServer *item = &g_array_index(rdata->dns_servers, NMNDiscDNSServer, i); + gint64 refresh; + + refresh = get_expiry_half(item); + if (refresh != _EXPIRY_INFINITY) { + if (!expiry_next(now, get_expiry(item), NULL)) { + g_array_remove_index(rdata->dns_servers, i); + *changed |= NM_NDISC_CONFIG_DNS_SERVERS; + continue; + } + + if (now >= refresh) + solicit_routers(ndisc); + else if (*nextevent > refresh) + *nextevent = refresh; + } + i++; + } } static void -clean_dns_domains (NMNDisc *ndisc, gint32 now, NMNDiscConfigMap *changed, gint32 *nextevent) +clean_dns_domains(NMNDisc *ndisc, gint32 now, NMNDiscConfigMap *changed, gint32 *nextevent) { - NMNDiscDataInternal *rdata; - guint i; - - rdata = &NM_NDISC_GET_PRIVATE (ndisc)->rdata; - - for (i = 0; i < rdata->dns_domains->len; ) { - NMNDiscDNSDomain *item = &g_array_index (rdata->dns_domains, NMNDiscDNSDomain, i); - gint64 refresh; - - refresh = get_expiry_half (item); - if (refresh != _EXPIRY_INFINITY) { - if (!expiry_next (now, get_expiry (item), NULL)) { - g_array_remove_index (rdata->dns_domains, i); - *changed |= NM_NDISC_CONFIG_DNS_DOMAINS; - continue; - } - - if (now >= refresh) - solicit_routers (ndisc); - else if (*nextevent > refresh) - *nextevent = refresh; - } - i++; - } + NMNDiscDataInternal *rdata; + guint i; + + rdata = &NM_NDISC_GET_PRIVATE(ndisc)->rdata; + + for (i = 0; i < rdata->dns_domains->len;) { + NMNDiscDNSDomain *item = &g_array_index(rdata->dns_domains, NMNDiscDNSDomain, i); + gint64 refresh; + + refresh = get_expiry_half(item); + if (refresh != _EXPIRY_INFINITY) { + if (!expiry_next(now, get_expiry(item), NULL)) { + g_array_remove_index(rdata->dns_domains, i); + *changed |= NM_NDISC_CONFIG_DNS_DOMAINS; + continue; + } + + if (now >= refresh) + solicit_routers(ndisc); + else if (*nextevent > refresh) + *nextevent = refresh; + } + i++; + } } -static gboolean timeout_cb (gpointer user_data); +static gboolean timeout_cb(gpointer user_data); static void -check_timestamps (NMNDisc *ndisc, gint32 now, NMNDiscConfigMap changed) +check_timestamps(NMNDisc *ndisc, gint32 now, NMNDiscConfigMap changed) { - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); - /* Use a magic date in the distant future (~68 years) */ - gint32 nextevent = G_MAXINT32; - - nm_clear_g_source (&priv->timeout_id); - - clean_gateways (ndisc, now, &changed, &nextevent); - clean_addresses (ndisc, now, &changed, &nextevent); - clean_routes (ndisc, now, &changed, &nextevent); - clean_dns_servers (ndisc, now, &changed, &nextevent); - clean_dns_domains (ndisc, now, &changed, &nextevent); - - if (nextevent != G_MAXINT32) { - if (nextevent <= now) - g_return_if_reached (); - _LOGD ("scheduling next now/lifetime check: %d seconds", - (int) (nextevent - now)); - priv->timeout_id = g_timeout_add_seconds (nextevent - now, timeout_cb, ndisc); - } - - if (changed) - nm_ndisc_emit_config_change (ndisc, changed); + NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE(ndisc); + /* Use a magic date in the distant future (~68 years) */ + gint32 nextevent = G_MAXINT32; + + nm_clear_g_source(&priv->timeout_id); + + clean_gateways(ndisc, now, &changed, &nextevent); + clean_addresses(ndisc, now, &changed, &nextevent); + clean_routes(ndisc, now, &changed, &nextevent); + clean_dns_servers(ndisc, now, &changed, &nextevent); + clean_dns_domains(ndisc, now, &changed, &nextevent); + + if (nextevent != G_MAXINT32) { + if (nextevent <= now) + g_return_if_reached(); + _LOGD("scheduling next now/lifetime check: %d seconds", (int) (nextevent - now)); + priv->timeout_id = g_timeout_add_seconds(nextevent - now, timeout_cb, ndisc); + } + + if (changed) + nm_ndisc_emit_config_change(ndisc, changed); } static gboolean -timeout_cb (gpointer user_data) +timeout_cb(gpointer user_data) { - NMNDisc *self = user_data; + NMNDisc *self = user_data; - NM_NDISC_GET_PRIVATE (self)->timeout_id = 0; - check_timestamps (self, nm_utils_get_monotonic_timestamp_sec (), 0); - return G_SOURCE_REMOVE; + NM_NDISC_GET_PRIVATE(self)->timeout_id = 0; + check_timestamps(self, nm_utils_get_monotonic_timestamp_sec(), 0); + return G_SOURCE_REMOVE; } void -nm_ndisc_ra_received (NMNDisc *ndisc, gint32 now, NMNDiscConfigMap changed) +nm_ndisc_ra_received(NMNDisc *ndisc, gint32 now, NMNDiscConfigMap changed) { - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); + NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE(ndisc); - nm_clear_g_source (&priv->ra_timeout_id); - nm_clear_g_source (&priv->send_rs_id); - nm_clear_g_free (&priv->last_error); - check_timestamps (ndisc, now, changed); + nm_clear_g_source_inst(&priv->ra_timeout_source); + nm_clear_g_source(&priv->send_rs_id); + nm_clear_g_free(&priv->last_error); + check_timestamps(ndisc, now, changed); } void -nm_ndisc_rs_received (NMNDisc *ndisc) +nm_ndisc_rs_received(NMNDisc *ndisc) { - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); + NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE(ndisc); - nm_clear_g_free (&priv->last_error); - announce_router_solicited (ndisc); + nm_clear_g_free(&priv->last_error); + announce_router_solicited(ndisc); } /*****************************************************************************/ static void -dns_domain_free (gpointer data) +dns_domain_free(gpointer data) { - g_free (((NMNDiscDNSDomain *)(data))->domain); + g_free(((NMNDiscDNSDomain *) (data))->domain); } static void -set_property (GObject *object, guint prop_id, - const GValue *value, GParamSpec *pspec) +set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { - NMNDisc *self = NM_NDISC (object); - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (self); - - switch (prop_id) { - case PROP_PLATFORM: - /* construct-only */ - priv->platform = g_value_get_object (value) ?: NM_PLATFORM_GET; - if (!priv->platform) - g_return_if_reached (); - - g_object_ref (priv->platform); - - priv->netns = nm_platform_netns_get (priv->platform); - if (priv->netns) - g_object_ref (priv->netns); - - g_return_if_fail (!priv->netns || priv->netns == nmp_netns_get_current ()); - break; - case PROP_IFINDEX: - /* construct-only */ - priv->ifindex = g_value_get_int (value); - g_return_if_fail (priv->ifindex > 0); - break; - case PROP_IFNAME: - /* construct-only */ - priv->ifname = g_value_dup_string (value); - g_return_if_fail (priv->ifname && priv->ifname[0]); - break; - case PROP_STABLE_TYPE: - /* construct-only */ - priv->stable_type = g_value_get_int (value); - break; - case PROP_NETWORK_ID: - /* construct-only */ - priv->network_id = g_value_dup_string (value); - g_return_if_fail (priv->network_id); - break; - case PROP_ADDR_GEN_MODE: - /* construct-only */ - priv->addr_gen_mode = g_value_get_int (value); - break; - case PROP_MAX_ADDRESSES: - /* construct-only */ - priv->max_addresses = g_value_get_int (value); - break; - case PROP_RA_TIMEOUT: - /* construct-only */ - priv->ra_timeout = g_value_get_int (value); - break; - case PROP_ROUTER_SOLICITATIONS: - /* construct-only */ - priv->router_solicitations = g_value_get_int (value); - break; - case PROP_ROUTER_SOLICITATION_INTERVAL: - /* construct-only */ - priv->router_solicitation_interval = g_value_get_int (value); - break; - case PROP_NODE_TYPE: - /* construct-only */ - priv->node_type = g_value_get_int (value); - nm_assert (NM_IN_SET (priv->node_type, NM_NDISC_NODE_TYPE_HOST, - NM_NDISC_NODE_TYPE_ROUTER)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } + NMNDisc * self = NM_NDISC(object); + NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE(self); + + switch (prop_id) { + case PROP_PLATFORM: + /* construct-only */ + priv->platform = g_value_get_object(value) ?: NM_PLATFORM_GET; + if (!priv->platform) + g_return_if_reached(); + + g_object_ref(priv->platform); + + priv->netns = nm_platform_netns_get(priv->platform); + if (priv->netns) + g_object_ref(priv->netns); + + g_return_if_fail(!priv->netns || priv->netns == nmp_netns_get_current()); + break; + case PROP_IFINDEX: + /* construct-only */ + priv->ifindex = g_value_get_int(value); + g_return_if_fail(priv->ifindex > 0); + break; + case PROP_IFNAME: + /* construct-only */ + priv->ifname = g_value_dup_string(value); + g_return_if_fail(priv->ifname && priv->ifname[0]); + break; + case PROP_STABLE_TYPE: + /* construct-only */ + priv->stable_type = g_value_get_int(value); + break; + case PROP_NETWORK_ID: + /* construct-only */ + priv->network_id = g_value_dup_string(value); + g_return_if_fail(priv->network_id); + break; + case PROP_ADDR_GEN_MODE: + /* construct-only */ + priv->addr_gen_mode = g_value_get_int(value); + break; + case PROP_MAX_ADDRESSES: + /* construct-only */ + priv->max_addresses = g_value_get_int(value); + break; + case PROP_RA_TIMEOUT: + /* construct-only */ + priv->ra_timeout = g_value_get_uint(value); + nm_assert(priv->ra_timeout <= NM_RA_TIMEOUT_INFINITY); + break; + case PROP_ROUTER_SOLICITATIONS: + /* construct-only */ + priv->router_solicitations = g_value_get_int(value); + break; + case PROP_ROUTER_SOLICITATION_INTERVAL: + /* construct-only */ + priv->router_solicitation_interval = g_value_get_int(value); + break; + case PROP_NODE_TYPE: + /* construct-only */ + priv->node_type = g_value_get_int(value); + nm_assert(NM_IN_SET(priv->node_type, NM_NDISC_NODE_TYPE_HOST, NM_NDISC_NODE_TYPE_ROUTER)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } } static void -nm_ndisc_init (NMNDisc *ndisc) +nm_ndisc_init(NMNDisc *ndisc) { - NMNDiscPrivate *priv; - NMNDiscDataInternal *rdata; - - priv = G_TYPE_INSTANCE_GET_PRIVATE (ndisc, NM_TYPE_NDISC, NMNDiscPrivate); - ndisc->_priv = priv; - - rdata = &priv->rdata; - - rdata->gateways = g_array_new (FALSE, FALSE, sizeof (NMNDiscGateway)); - rdata->addresses = g_array_new (FALSE, FALSE, sizeof (NMNDiscAddress)); - rdata->routes = g_array_new (FALSE, FALSE, sizeof (NMNDiscRoute)); - rdata->dns_servers = g_array_new (FALSE, FALSE, sizeof (NMNDiscDNSServer)); - rdata->dns_domains = g_array_new (FALSE, FALSE, sizeof (NMNDiscDNSDomain)); - g_array_set_clear_func (rdata->dns_domains, dns_domain_free); - priv->rdata.public.hop_limit = 64; - - /* Start at very low number so that last_rs - router_solicitation_interval - * is much lower than nm_utils_get_monotonic_timestamp_sec() at startup. - */ - priv->last_rs = G_MININT32; + NMNDiscPrivate * priv; + NMNDiscDataInternal *rdata; + + priv = G_TYPE_INSTANCE_GET_PRIVATE(ndisc, NM_TYPE_NDISC, NMNDiscPrivate); + ndisc->_priv = priv; + + rdata = &priv->rdata; + + rdata->gateways = g_array_new(FALSE, FALSE, sizeof(NMNDiscGateway)); + rdata->addresses = g_array_new(FALSE, FALSE, sizeof(NMNDiscAddress)); + rdata->routes = g_array_new(FALSE, FALSE, sizeof(NMNDiscRoute)); + rdata->dns_servers = g_array_new(FALSE, FALSE, sizeof(NMNDiscDNSServer)); + rdata->dns_domains = g_array_new(FALSE, FALSE, sizeof(NMNDiscDNSDomain)); + g_array_set_clear_func(rdata->dns_domains, dns_domain_free); + priv->rdata.public.hop_limit = 64; + + /* Start at very low number so that last_rs - router_solicitation_interval + * is much lower than nm_utils_get_monotonic_timestamp_sec() at startup. + */ + priv->last_rs = G_MININT32; } static void -dispose (GObject *object) +dispose(GObject *object) { - NMNDisc *ndisc = NM_NDISC (object); - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); + NMNDisc * ndisc = NM_NDISC(object); + NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE(ndisc); - nm_clear_g_source (&priv->ra_timeout_id); - nm_clear_g_source (&priv->send_rs_id); - nm_clear_g_source (&priv->send_ra_id); - nm_clear_g_free (&priv->last_error); + nm_clear_g_source_inst(&priv->ra_timeout_source); + nm_clear_g_source(&priv->send_rs_id); + nm_clear_g_source(&priv->send_ra_id); + nm_clear_g_free(&priv->last_error); - nm_clear_g_source (&priv->timeout_id); + nm_clear_g_source(&priv->timeout_id); - G_OBJECT_CLASS (nm_ndisc_parent_class)->dispose (object); + G_OBJECT_CLASS(nm_ndisc_parent_class)->dispose(object); } static void -finalize (GObject *object) +finalize(GObject *object) { - NMNDisc *ndisc = NM_NDISC (object); - NMNDiscPrivate *priv = NM_NDISC_GET_PRIVATE (ndisc); - NMNDiscDataInternal *rdata = &priv->rdata; + NMNDisc * ndisc = NM_NDISC(object); + NMNDiscPrivate * priv = NM_NDISC_GET_PRIVATE(ndisc); + NMNDiscDataInternal *rdata = &priv->rdata; - g_free (priv->ifname); - g_free (priv->network_id); + g_free(priv->ifname); + g_free(priv->network_id); - g_array_unref (rdata->gateways); - g_array_unref (rdata->addresses); - g_array_unref (rdata->routes); - g_array_unref (rdata->dns_servers); - g_array_unref (rdata->dns_domains); + g_array_unref(rdata->gateways); + g_array_unref(rdata->addresses); + g_array_unref(rdata->routes); + g_array_unref(rdata->dns_servers); + g_array_unref(rdata->dns_domains); - g_clear_object (&priv->netns); - g_clear_object (&priv->platform); + g_clear_object(&priv->netns); + g_clear_object(&priv->platform); - G_OBJECT_CLASS (nm_ndisc_parent_class)->finalize (object); + G_OBJECT_CLASS(nm_ndisc_parent_class)->finalize(object); } static void -nm_ndisc_class_init (NMNDiscClass *klass) +nm_ndisc_class_init(NMNDiscClass *klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - g_type_class_add_private (klass, sizeof (NMNDiscPrivate)); - - object_class->set_property = set_property; - object_class->dispose = dispose; - object_class->finalize = finalize; - - obj_properties[PROP_PLATFORM] = - g_param_spec_object (NM_NDISC_PLATFORM, "", "", - NM_TYPE_PLATFORM, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - obj_properties[PROP_IFINDEX] = - g_param_spec_int (NM_NDISC_IFINDEX, "", "", - 0, G_MAXINT, 0, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - obj_properties[PROP_IFNAME] = - g_param_spec_string (NM_NDISC_IFNAME, "", "", - NULL, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - obj_properties[PROP_STABLE_TYPE] = - g_param_spec_int (NM_NDISC_STABLE_TYPE, "", "", - NM_UTILS_STABLE_TYPE_UUID, NM_UTILS_STABLE_TYPE_RANDOM, NM_UTILS_STABLE_TYPE_UUID, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - obj_properties[PROP_NETWORK_ID] = - g_param_spec_string (NM_NDISC_NETWORK_ID, "", "", - NULL, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - obj_properties[PROP_ADDR_GEN_MODE] = - g_param_spec_int (NM_NDISC_ADDR_GEN_MODE, "", "", - NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_EUI64, NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_STABLE_PRIVACY, NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_EUI64, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - obj_properties[PROP_MAX_ADDRESSES] = - g_param_spec_int (NM_NDISC_MAX_ADDRESSES, "", "", - 0, G_MAXINT32, NM_NDISC_MAX_ADDRESSES_DEFAULT, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - G_STATIC_ASSERT_EXPR (G_MAXINT32 == NM_RA_TIMEOUT_INFINITY); - obj_properties[PROP_RA_TIMEOUT] = - g_param_spec_int (NM_NDISC_RA_TIMEOUT, "", "", - 0, G_MAXINT32, 0, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - obj_properties[PROP_ROUTER_SOLICITATIONS] = - g_param_spec_int (NM_NDISC_ROUTER_SOLICITATIONS, "", "", - 1, G_MAXINT32, NM_NDISC_ROUTER_SOLICITATIONS_DEFAULT, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - obj_properties[PROP_ROUTER_SOLICITATION_INTERVAL] = - g_param_spec_int (NM_NDISC_ROUTER_SOLICITATION_INTERVAL, "", "", - 1, G_MAXINT32, NM_NDISC_ROUTER_SOLICITATION_INTERVAL_DEFAULT, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - obj_properties[PROP_NODE_TYPE] = - g_param_spec_int (NM_NDISC_NODE_TYPE, "", "", - NM_NDISC_NODE_TYPE_INVALID, NM_NDISC_NODE_TYPE_ROUTER, NM_NDISC_NODE_TYPE_INVALID, - G_PARAM_WRITABLE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_STRINGS); - g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties); - - signals[CONFIG_RECEIVED] = - g_signal_new (NM_NDISC_CONFIG_RECEIVED, - G_OBJECT_CLASS_TYPE (klass), - G_SIGNAL_RUN_FIRST, - 0, - NULL, NULL, NULL, - G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_UINT); - signals[RA_TIMEOUT_SIGNAL] = - g_signal_new (NM_NDISC_RA_TIMEOUT_SIGNAL, - G_OBJECT_CLASS_TYPE (klass), - G_SIGNAL_RUN_FIRST, - 0, - NULL, NULL, NULL, - G_TYPE_NONE, 0); + GObjectClass *object_class = G_OBJECT_CLASS(klass); + + g_type_class_add_private(klass, sizeof(NMNDiscPrivate)); + + object_class->set_property = set_property; + object_class->dispose = dispose; + object_class->finalize = finalize; + + obj_properties[PROP_PLATFORM] = + g_param_spec_object(NM_NDISC_PLATFORM, + "", + "", + NM_TYPE_PLATFORM, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_IFINDEX] = + g_param_spec_int(NM_NDISC_IFINDEX, + "", + "", + 0, + G_MAXINT, + 0, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_IFNAME] = + g_param_spec_string(NM_NDISC_IFNAME, + "", + "", + NULL, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_STABLE_TYPE] = + g_param_spec_int(NM_NDISC_STABLE_TYPE, + "", + "", + NM_UTILS_STABLE_TYPE_UUID, + NM_UTILS_STABLE_TYPE_RANDOM, + NM_UTILS_STABLE_TYPE_UUID, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_NETWORK_ID] = + g_param_spec_string(NM_NDISC_NETWORK_ID, + "", + "", + NULL, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_ADDR_GEN_MODE] = + g_param_spec_int(NM_NDISC_ADDR_GEN_MODE, + "", + "", + NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_EUI64, + NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_STABLE_PRIVACY, + NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_EUI64, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_MAX_ADDRESSES] = + g_param_spec_int(NM_NDISC_MAX_ADDRESSES, + "", + "", + 0, + G_MAXINT32, + NM_NDISC_MAX_ADDRESSES_DEFAULT, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + G_STATIC_ASSERT_EXPR(G_MAXINT32 == NM_RA_TIMEOUT_INFINITY); + obj_properties[PROP_RA_TIMEOUT] = + g_param_spec_uint(NM_NDISC_RA_TIMEOUT, + "", + "", + 0, + G_MAXINT32, + 0, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_ROUTER_SOLICITATIONS] = + g_param_spec_int(NM_NDISC_ROUTER_SOLICITATIONS, + "", + "", + 1, + G_MAXINT32, + NM_NDISC_ROUTER_SOLICITATIONS_DEFAULT, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_ROUTER_SOLICITATION_INTERVAL] = + g_param_spec_int(NM_NDISC_ROUTER_SOLICITATION_INTERVAL, + "", + "", + 1, + G_MAXINT32, + NM_NDISC_ROUTER_SOLICITATION_INTERVAL_DEFAULT, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_NODE_TYPE] = + g_param_spec_int(NM_NDISC_NODE_TYPE, + "", + "", + NM_NDISC_NODE_TYPE_INVALID, + NM_NDISC_NODE_TYPE_ROUTER, + NM_NDISC_NODE_TYPE_INVALID, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); + g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties); + + signals[CONFIG_RECEIVED] = g_signal_new(NM_NDISC_CONFIG_RECEIVED, + G_OBJECT_CLASS_TYPE(klass), + G_SIGNAL_RUN_FIRST, + 0, + NULL, + NULL, + NULL, + G_TYPE_NONE, + 2, + G_TYPE_POINTER, + G_TYPE_UINT); + signals[RA_TIMEOUT_SIGNAL] = g_signal_new(NM_NDISC_RA_TIMEOUT_SIGNAL, + G_OBJECT_CLASS_TYPE(klass), + G_SIGNAL_RUN_FIRST, + 0, + NULL, + NULL, + NULL, + G_TYPE_NONE, + 0); } diff --git a/src/ndisc/nm-ndisc.h b/src/ndisc/nm-ndisc.h index eeda38e2..0578c45b 100644 --- a/src/ndisc/nm-ndisc.h +++ b/src/ndisc/nm-ndisc.h @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2013 Red Hat, Inc. */ @@ -16,133 +16,133 @@ #include "platform/nm-platform.h" #include "platform/nmp-object.h" -#define NM_RA_TIMEOUT_DEFAULT ((gint32) 0) -#define NM_RA_TIMEOUT_INFINITY G_MAXINT32 - -#define NM_TYPE_NDISC (nm_ndisc_get_type ()) -#define NM_NDISC(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_NDISC, NMNDisc)) -#define NM_NDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_NDISC, NMNDiscClass)) -#define NM_IS_NDISC(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_NDISC)) -#define NM_IS_NDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_NDISC)) -#define NM_NDISC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_NDISC, NMNDiscClass)) - -#define NM_NDISC_PLATFORM "platform" -#define NM_NDISC_IFINDEX "ifindex" -#define NM_NDISC_IFNAME "ifname" -#define NM_NDISC_NETWORK_ID "network-id" -#define NM_NDISC_ADDR_GEN_MODE "addr-gen-mode" -#define NM_NDISC_STABLE_TYPE "stable-type" -#define NM_NDISC_NODE_TYPE "node-type" -#define NM_NDISC_MAX_ADDRESSES "max-addresses" -#define NM_NDISC_RA_TIMEOUT "ra-timeout" -#define NM_NDISC_ROUTER_SOLICITATIONS "router-solicitations" +#define NM_RA_TIMEOUT_DEFAULT ((guint32) 0) +#define NM_RA_TIMEOUT_INFINITY ((guint32) G_MAXINT32) + +#define NM_TYPE_NDISC (nm_ndisc_get_type()) +#define NM_NDISC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_NDISC, NMNDisc)) +#define NM_NDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), NM_TYPE_NDISC, NMNDiscClass)) +#define NM_IS_NDISC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), NM_TYPE_NDISC)) +#define NM_IS_NDISC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), NM_TYPE_NDISC)) +#define NM_NDISC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), NM_TYPE_NDISC, NMNDiscClass)) + +#define NM_NDISC_PLATFORM "platform" +#define NM_NDISC_IFINDEX "ifindex" +#define NM_NDISC_IFNAME "ifname" +#define NM_NDISC_NETWORK_ID "network-id" +#define NM_NDISC_ADDR_GEN_MODE "addr-gen-mode" +#define NM_NDISC_STABLE_TYPE "stable-type" +#define NM_NDISC_NODE_TYPE "node-type" +#define NM_NDISC_MAX_ADDRESSES "max-addresses" +#define NM_NDISC_RA_TIMEOUT "ra-timeout" +#define NM_NDISC_ROUTER_SOLICITATIONS "router-solicitations" #define NM_NDISC_ROUTER_SOLICITATION_INTERVAL "router-solicitation-interval" #define NM_NDISC_CONFIG_RECEIVED "config-received" #define NM_NDISC_RA_TIMEOUT_SIGNAL "ra-timeout-signal" typedef enum { - NM_NDISC_DHCP_LEVEL_UNKNOWN, - NM_NDISC_DHCP_LEVEL_NONE, - NM_NDISC_DHCP_LEVEL_OTHERCONF, - NM_NDISC_DHCP_LEVEL_MANAGED + NM_NDISC_DHCP_LEVEL_UNKNOWN, + NM_NDISC_DHCP_LEVEL_NONE, + NM_NDISC_DHCP_LEVEL_OTHERCONF, + NM_NDISC_DHCP_LEVEL_MANAGED } NMNDiscDHCPLevel; /* we rely on the fact that NM_NDISC_INFINITY is the largest possible * time duration (G_MAXUINT32) and that the range of finite values * goes from 0 to G_MAXUINT32-1. */ -#define NM_NDISC_INFINITY G_MAXUINT32 +#define NM_NDISC_INFINITY G_MAXUINT32 struct _NMNDiscGateway { - struct in6_addr address; - guint32 timestamp; - guint32 lifetime; - NMIcmpv6RouterPref preference; + struct in6_addr address; + guint32 timestamp; + guint32 lifetime; + NMIcmpv6RouterPref preference; }; typedef struct _NMNDiscGateway NMNDiscGateway; struct _NMNDiscAddress { - struct in6_addr address; - guint8 dad_counter; - guint32 timestamp; - guint32 lifetime; - guint32 preferred; + struct in6_addr address; + guint8 dad_counter; + guint32 timestamp; + guint32 lifetime; + guint32 preferred; }; typedef struct _NMNDiscAddress NMNDiscAddress; struct _NMNDiscRoute { - struct in6_addr network; - guint8 plen; - struct in6_addr gateway; - guint32 timestamp; - guint32 lifetime; - NMIcmpv6RouterPref preference; + struct in6_addr network; + guint8 plen; + struct in6_addr gateway; + guint32 timestamp; + guint32 lifetime; + NMIcmpv6RouterPref preference; }; typedef struct _NMNDiscRoute NMNDiscRoute; typedef struct { - struct in6_addr address; - guint32 timestamp; - guint32 lifetime; + struct in6_addr address; + guint32 timestamp; + guint32 lifetime; } NMNDiscDNSServer; typedef struct { - char *domain; - guint32 timestamp; - guint32 lifetime; + char * domain; + guint32 timestamp; + guint32 lifetime; } NMNDiscDNSDomain; typedef enum { - NM_NDISC_CONFIG_NONE = 0, - NM_NDISC_CONFIG_DHCP_LEVEL = 1 << 0, - NM_NDISC_CONFIG_GATEWAYS = 1 << 1, - NM_NDISC_CONFIG_ADDRESSES = 1 << 2, - NM_NDISC_CONFIG_ROUTES = 1 << 3, - NM_NDISC_CONFIG_DNS_SERVERS = 1 << 4, - NM_NDISC_CONFIG_DNS_DOMAINS = 1 << 5, - NM_NDISC_CONFIG_HOP_LIMIT = 1 << 6, - NM_NDISC_CONFIG_MTU = 1 << 7, - NM_NDISC_CONFIG_REACHABLE_TIME = 1 << 8, - NM_NDISC_CONFIG_RETRANS_TIMER = 1 << 9, + NM_NDISC_CONFIG_NONE = 0, + NM_NDISC_CONFIG_DHCP_LEVEL = 1 << 0, + NM_NDISC_CONFIG_GATEWAYS = 1 << 1, + NM_NDISC_CONFIG_ADDRESSES = 1 << 2, + NM_NDISC_CONFIG_ROUTES = 1 << 3, + NM_NDISC_CONFIG_DNS_SERVERS = 1 << 4, + NM_NDISC_CONFIG_DNS_DOMAINS = 1 << 5, + NM_NDISC_CONFIG_HOP_LIMIT = 1 << 6, + NM_NDISC_CONFIG_MTU = 1 << 7, + NM_NDISC_CONFIG_REACHABLE_TIME = 1 << 8, + NM_NDISC_CONFIG_RETRANS_TIMER = 1 << 9, } NMNDiscConfigMap; typedef enum { - NM_NDISC_NODE_TYPE_INVALID, - NM_NDISC_NODE_TYPE_HOST, - NM_NDISC_NODE_TYPE_ROUTER, + NM_NDISC_NODE_TYPE_INVALID, + NM_NDISC_NODE_TYPE_HOST, + NM_NDISC_NODE_TYPE_ROUTER, } NMNDiscNodeType; -#define NM_NDISC_MAX_ADDRESSES_DEFAULT 16 -#define NM_NDISC_ROUTER_SOLICITATIONS_DEFAULT 3 /* RFC4861 MAX_RTR_SOLICITATIONS */ -#define NM_NDISC_ROUTER_SOLICITATION_INTERVAL_DEFAULT 4 /* RFC4861 RTR_SOLICITATION_INTERVAL */ -#define NM_NDISC_ROUTER_ADVERTISEMENTS_DEFAULT 3 /* RFC4861 MAX_INITIAL_RTR_ADVERTISEMENTS */ -#define NM_NDISC_ROUTER_ADVERT_DELAY 3 /* RFC4861 MIN_DELAY_BETWEEN_RAS */ -#define NM_NDISC_ROUTER_ADVERT_INITIAL_INTERVAL 16 /* RFC4861 MAX_INITIAL_RTR_ADVERT_INTERVAL */ -#define NM_NDISC_ROUTER_ADVERT_DELAY_MS 500 /* RFC4861 MAX_RA_DELAY_TIME */ -#define NM_NDISC_ROUTER_ADVERT_MAX_INTERVAL 600 /* RFC4861 MaxRtrAdvInterval default */ -#define NM_NDISC_ROUTER_LIFETIME 900 /* 1.5 * NM_NDISC_ROUTER_ADVERT_MAX_INTERVAL */ +#define NM_NDISC_MAX_ADDRESSES_DEFAULT 16 +#define NM_NDISC_ROUTER_SOLICITATIONS_DEFAULT 3 /* RFC4861 MAX_RTR_SOLICITATIONS */ +#define NM_NDISC_ROUTER_SOLICITATION_INTERVAL_DEFAULT 4 /* RFC4861 RTR_SOLICITATION_INTERVAL */ +#define NM_NDISC_ROUTER_ADVERTISEMENTS_DEFAULT 3 /* RFC4861 MAX_INITIAL_RTR_ADVERTISEMENTS */ +#define NM_NDISC_ROUTER_ADVERT_DELAY 3 /* RFC4861 MIN_DELAY_BETWEEN_RAS */ +#define NM_NDISC_ROUTER_ADVERT_INITIAL_INTERVAL 16 /* RFC4861 MAX_INITIAL_RTR_ADVERT_INTERVAL */ +#define NM_NDISC_ROUTER_ADVERT_DELAY_MS 500 /* RFC4861 MAX_RA_DELAY_TIME */ +#define NM_NDISC_ROUTER_ADVERT_MAX_INTERVAL 600 /* RFC4861 MaxRtrAdvInterval default */ +#define NM_NDISC_ROUTER_LIFETIME 900 /* 1.5 * NM_NDISC_ROUTER_ADVERT_MAX_INTERVAL */ struct _NMNDiscPrivate; struct _NMNDiscDataInternal; typedef struct { - NMNDiscDHCPLevel dhcp_level; - guint32 mtu; - int hop_limit; - guint32 reachable_time_ms; - guint32 retrans_timer_ms; - - guint gateways_n; - guint addresses_n; - guint routes_n; - guint dns_servers_n; - guint dns_domains_n; - - const NMNDiscGateway *gateways; - const NMNDiscAddress *addresses; - const NMNDiscRoute *routes; - const NMNDiscDNSServer *dns_servers; - const NMNDiscDNSDomain *dns_domains; + NMNDiscDHCPLevel dhcp_level; + guint32 mtu; + int hop_limit; + guint32 reachable_time_ms; + guint32 retrans_timer_ms; + + guint gateways_n; + guint addresses_n; + guint routes_n; + guint dns_servers_n; + guint dns_domains_n; + + const NMNDiscGateway * gateways; + const NMNDiscAddress * addresses; + const NMNDiscRoute * routes; + const NMNDiscDNSServer *dns_servers; + const NMNDiscDNSDomain *dns_domains; } NMNDiscData; /** @@ -152,69 +152,82 @@ typedef struct { * caches advertised items and removes them when they are obsolete. */ typedef struct { - GObject parent; - union { - struct _NMNDiscPrivate *_priv; - struct _NMNDiscDataInternal *rdata; - }; + GObject parent; + union { + struct _NMNDiscPrivate * _priv; + struct _NMNDiscDataInternal *rdata; + }; } NMNDisc; typedef struct { - GObjectClass parent; + GObjectClass parent; - void (*start) (NMNDisc *ndisc); - gboolean (*send_rs) (NMNDisc *ndisc, GError **error); - gboolean (*send_ra) (NMNDisc *ndisc, GError **error); + void (*start)(NMNDisc *ndisc); + void (*stop)(NMNDisc *ndisc); + gboolean (*send_rs)(NMNDisc *ndisc, GError **error); + gboolean (*send_ra)(NMNDisc *ndisc, GError **error); } NMNDiscClass; -GType nm_ndisc_get_type (void); +GType nm_ndisc_get_type(void); -void nm_ndisc_emit_config_change (NMNDisc *self, NMNDiscConfigMap changed); +void nm_ndisc_emit_config_change(NMNDisc *self, NMNDiscConfigMap changed); -int nm_ndisc_get_ifindex (NMNDisc *self); -const char *nm_ndisc_get_ifname (NMNDisc *self); -NMNDiscNodeType nm_ndisc_get_node_type (NMNDisc *self); +int nm_ndisc_get_ifindex(NMNDisc *self); +const char * nm_ndisc_get_ifname(NMNDisc *self); +NMNDiscNodeType nm_ndisc_get_node_type(NMNDisc *self); -gboolean nm_ndisc_set_iid (NMNDisc *ndisc, const NMUtilsIPv6IfaceId iid); -void nm_ndisc_start (NMNDisc *ndisc); -NMNDiscConfigMap nm_ndisc_dad_failed (NMNDisc *ndisc, - const struct in6_addr *address, - gboolean emit_changed_signal); -void nm_ndisc_set_config (NMNDisc *ndisc, - const GArray *addresses, - const GArray *dns_servers, - const GArray *dns_domains); +gboolean nm_ndisc_set_iid(NMNDisc *ndisc, const NMUtilsIPv6IfaceId iid); +void nm_ndisc_start(NMNDisc *ndisc); +void nm_ndisc_stop(NMNDisc *ndisc); +NMNDiscConfigMap +nm_ndisc_dad_failed(NMNDisc *ndisc, const struct in6_addr *address, gboolean emit_changed_signal); +void nm_ndisc_set_config(NMNDisc * ndisc, + const GArray *addresses, + const GArray *dns_servers, + const GArray *dns_domains); -NMPlatform *nm_ndisc_get_platform (NMNDisc *self); -NMPNetns *nm_ndisc_netns_get (NMNDisc *self); -gboolean nm_ndisc_netns_push (NMNDisc *self, NMPNetns **netns); +NMPlatform *nm_ndisc_get_platform(NMNDisc *self); +NMPNetns * nm_ndisc_netns_get(NMNDisc *self); +gboolean nm_ndisc_netns_push(NMNDisc *self, NMPNetns **netns); static inline gboolean -nm_ndisc_dad_addr_is_fail_candidate_event (NMPlatformSignalChangeType change_type, - const NMPlatformIP6Address *addr) +nm_ndisc_dad_addr_is_fail_candidate_event(NMPlatformSignalChangeType change_type, + const NMPlatformIP6Address *addr) { - return !NM_FLAGS_HAS (addr->n_ifa_flags, IFA_F_TEMPORARY) - && ( (change_type == NM_PLATFORM_SIGNAL_CHANGED && addr->n_ifa_flags & IFA_F_DADFAILED) - || (change_type == NM_PLATFORM_SIGNAL_REMOVED && addr->n_ifa_flags & IFA_F_TENTATIVE)); + return !NM_FLAGS_HAS(addr->n_ifa_flags, IFA_F_TEMPORARY) + && ((change_type == NM_PLATFORM_SIGNAL_CHANGED && addr->n_ifa_flags & IFA_F_DADFAILED) + || (change_type == NM_PLATFORM_SIGNAL_REMOVED + && addr->n_ifa_flags & IFA_F_TENTATIVE)); } static inline gboolean -nm_ndisc_dad_addr_is_fail_candidate (NMPlatform *platform, - const NMPObject *obj) +nm_ndisc_dad_addr_is_fail_candidate(NMPlatform *platform, const NMPObject *obj) { - const NMPlatformIP6Address *addr; - - addr = NMP_OBJECT_CAST_IP6_ADDRESS (nm_platform_lookup_obj (platform, - NMP_CACHE_ID_TYPE_OBJECT_TYPE, - obj)); - if ( addr - && ( NM_FLAGS_HAS (addr->n_ifa_flags, IFA_F_TEMPORARY) - || !NM_FLAGS_HAS (addr->n_ifa_flags, IFA_F_DADFAILED))) { - /* the address still/again exists and is not in DADFAILED state. Skip it. */ - return FALSE; - } - - return TRUE; + const NMPlatformIP6Address *addr; + + addr = NMP_OBJECT_CAST_IP6_ADDRESS( + nm_platform_lookup_obj(platform, NMP_CACHE_ID_TYPE_OBJECT_TYPE, obj)); + if (addr + && (NM_FLAGS_HAS(addr->n_ifa_flags, IFA_F_TEMPORARY) + || !NM_FLAGS_HAS(addr->n_ifa_flags, IFA_F_DADFAILED))) { + /* the address still/again exists and is not in DADFAILED state. Skip it. */ + return FALSE; + } + + return TRUE; } +/*****************************************************************************/ + +struct _NML3ConfigData; + +struct _NML3ConfigData *nm_ndisc_data_to_l3cd(NMDedupMultiIndex * multi_idx, + int ifindex, + const NMNDiscData * rdata, + NMSettingIP6ConfigPrivacy ip6_privacy, + guint32 route_table, + guint32 route_metric, + gboolean kernel_support_rta_pref, + gboolean kernel_support_extended_ifa_flags); + #endif /* __NETWORKMANAGER_NDISC_H__ */ diff --git a/src/ndisc/tests/test-ndisc-fake.c b/src/ndisc/tests/test-ndisc-fake.c index 254110e4..dfb0b37b 100644 --- a/src/ndisc/tests/test-ndisc-fake.c +++ b/src/ndisc/tests/test-ndisc-fake.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2015 Red Hat, Inc. */ @@ -15,518 +15,524 @@ #include "nm-test-utils-core.h" static NMFakeNDisc * -ndisc_new (void) +ndisc_new(void) { - NMNDisc *ndisc; - const int ifindex = 1; - const char *ifname = nm_platform_link_get_name (NM_PLATFORM_GET, ifindex); - NMUtilsIPv6IfaceId iid = { }; - - ndisc = nm_fake_ndisc_new (ifindex, ifname); - iid.id_u8[7] = 1; - nm_ndisc_set_iid (ndisc, iid); - g_assert (ndisc); - return NM_FAKE_NDISC (ndisc); + NMNDisc * ndisc; + const int ifindex = 1; + const char * ifname = nm_platform_link_get_name(NM_PLATFORM_GET, ifindex); + NMUtilsIPv6IfaceId iid = {}; + + ndisc = nm_fake_ndisc_new(ifindex, ifname); + iid.id_u8[7] = 1; + nm_ndisc_set_iid(ndisc, iid); + g_assert(ndisc); + return NM_FAKE_NDISC(ndisc); } static void -match_gateway (const NMNDiscData *rdata, guint idx, const char *addr, guint32 ts, guint32 lt, NMIcmpv6RouterPref pref) +match_gateway(const NMNDiscData *rdata, + guint idx, + const char * addr, + guint32 ts, + guint32 lt, + NMIcmpv6RouterPref pref) { - const NMNDiscGateway *gw; - char buf[INET6_ADDRSTRLEN]; + const NMNDiscGateway *gw; + char buf[INET6_ADDRSTRLEN]; - g_assert (rdata); - g_assert_cmpint (idx, <, rdata->gateways_n); - g_assert (rdata->gateways); + g_assert(rdata); + g_assert_cmpint(idx, <, rdata->gateways_n); + g_assert(rdata->gateways); - gw = &rdata->gateways[idx]; + gw = &rdata->gateways[idx]; - g_assert_cmpstr (inet_ntop (AF_INET6, &gw->address, buf, sizeof (buf)), ==, addr); - g_assert_cmpint (gw->timestamp, ==, ts); - g_assert_cmpint (gw->lifetime, ==, lt); - g_assert_cmpint (gw->preference, ==, pref); + g_assert_cmpstr(inet_ntop(AF_INET6, &gw->address, buf, sizeof(buf)), ==, addr); + g_assert_cmpint(gw->timestamp, ==, ts); + g_assert_cmpint(gw->lifetime, ==, lt); + g_assert_cmpint(gw->preference, ==, pref); } -#define match_address(rdata, idx, addr, ts, lt, pref) \ - G_STMT_START { \ - const NMNDiscData *_rdata = (rdata); \ - guint _idx = (idx); \ - const NMNDiscAddress *_a; \ - guint _ts = (ts); \ - \ - g_assert (_rdata); \ - g_assert_cmpint (_idx, <, _rdata->addresses_n); \ - g_assert (_rdata->addresses); \ - \ - _a = &_rdata->addresses[_idx]; \ - \ - nmtst_assert_ip6_address (&_a->address, (addr)); \ - g_assert_cmpint (_a->timestamp, <=, _ts + 1); \ - g_assert_cmpint ((int) _a->timestamp, >=, (int) _ts - 1); \ - g_assert_cmpint (_a->timestamp + _a->lifetime, ==, _ts + (lt)); \ - g_assert_cmpint (_a->timestamp + _a->preferred, ==, _ts + (pref)); \ - } G_STMT_END +#define match_address(rdata, idx, addr, ts, lt, pref) \ + G_STMT_START \ + { \ + const NMNDiscData * _rdata = (rdata); \ + guint _idx = (idx); \ + const NMNDiscAddress *_a; \ + guint _ts = (ts); \ + \ + g_assert(_rdata); \ + g_assert_cmpint(_idx, <, _rdata->addresses_n); \ + g_assert(_rdata->addresses); \ + \ + _a = &_rdata->addresses[_idx]; \ + \ + nmtst_assert_ip6_address(&_a->address, (addr)); \ + g_assert_cmpint(_a->timestamp, <=, _ts + 1); \ + g_assert_cmpint((int) _a->timestamp, >=, (int) _ts - 1); \ + g_assert_cmpint(_a->timestamp + _a->lifetime, ==, _ts + (lt)); \ + g_assert_cmpint(_a->timestamp + _a->preferred, ==, _ts + (pref)); \ + } \ + G_STMT_END #define match_route(rdata, idx, nw, pl, gw, ts, lt, pref) \ - G_STMT_START { \ - const NMNDiscData *_rdata = (rdata); \ - guint _idx = (idx); \ - const NMNDiscRoute *_r; \ - int _plen = (pl); \ - \ - g_assert (_rdata); \ - g_assert_cmpint (_idx, <, _rdata->routes_n); \ - g_assert (_rdata->routes); \ - g_assert (_plen > 0 && _plen <= 128); \ - \ - _r = &_rdata->routes[idx]; \ - \ - nmtst_assert_ip6_address (&_r->network, (nw)); \ - g_assert_cmpint ((int) _r->plen, ==, _plen); \ - nmtst_assert_ip6_address (&_r->gateway, (gw)); \ - g_assert_cmpint (_r->timestamp, ==, (ts)); \ - g_assert_cmpint (_r->lifetime, ==, (lt)); \ - g_assert_cmpint (_r->preference, ==, (pref)); \ - } G_STMT_END + G_STMT_START \ + { \ + const NMNDiscData * _rdata = (rdata); \ + guint _idx = (idx); \ + const NMNDiscRoute *_r; \ + int _plen = (pl); \ + \ + g_assert(_rdata); \ + g_assert_cmpint(_idx, <, _rdata->routes_n); \ + g_assert(_rdata->routes); \ + g_assert(_plen > 0 && _plen <= 128); \ + \ + _r = &_rdata->routes[idx]; \ + \ + nmtst_assert_ip6_address(&_r->network, (nw)); \ + g_assert_cmpint((int) _r->plen, ==, _plen); \ + nmtst_assert_ip6_address(&_r->gateway, (gw)); \ + g_assert_cmpint(_r->timestamp, ==, (ts)); \ + g_assert_cmpint(_r->lifetime, ==, (lt)); \ + g_assert_cmpint(_r->preference, ==, (pref)); \ + } \ + G_STMT_END static void -match_dns_server (const NMNDiscData *rdata, guint idx, const char *addr, guint32 ts, guint32 lt) +match_dns_server(const NMNDiscData *rdata, guint idx, const char *addr, guint32 ts, guint32 lt) { - const NMNDiscDNSServer *dns; - char buf[INET6_ADDRSTRLEN]; + const NMNDiscDNSServer *dns; + char buf[INET6_ADDRSTRLEN]; - g_assert (rdata); - g_assert_cmpint (idx, <, rdata->dns_servers_n); - g_assert (rdata->dns_servers); + g_assert(rdata); + g_assert_cmpint(idx, <, rdata->dns_servers_n); + g_assert(rdata->dns_servers); - dns = &rdata->dns_servers[idx]; + dns = &rdata->dns_servers[idx]; - g_assert_cmpstr (inet_ntop (AF_INET6, &dns->address, buf, sizeof (buf)), ==, addr); - g_assert_cmpint (dns->timestamp, ==, ts); - g_assert_cmpint (dns->lifetime, ==, lt); + g_assert_cmpstr(inet_ntop(AF_INET6, &dns->address, buf, sizeof(buf)), ==, addr); + g_assert_cmpint(dns->timestamp, ==, ts); + g_assert_cmpint(dns->lifetime, ==, lt); } static void -match_dns_domain (const NMNDiscData *rdata, guint idx, const char *domain, guint32 ts, guint32 lt) +match_dns_domain(const NMNDiscData *rdata, guint idx, const char *domain, guint32 ts, guint32 lt) { - const NMNDiscDNSDomain *dns; + const NMNDiscDNSDomain *dns; - g_assert (rdata); - g_assert_cmpint (idx, <, rdata->dns_domains_n); - g_assert (rdata->dns_domains); + g_assert(rdata); + g_assert_cmpint(idx, <, rdata->dns_domains_n); + g_assert(rdata->dns_domains); - dns = &rdata->dns_domains[idx]; + dns = &rdata->dns_domains[idx]; - g_assert_cmpstr (dns->domain, ==, domain); - g_assert_cmpint (dns->timestamp, ==, ts); - g_assert_cmpint (dns->lifetime, ==, lt); + g_assert_cmpstr(dns->domain, ==, domain); + g_assert_cmpint(dns->timestamp, ==, ts); + g_assert_cmpint(dns->lifetime, ==, lt); } typedef struct { - GMainLoop *loop; - guint counter; - guint rs_counter; - guint32 timestamp1; - guint32 first_solicit; - guint32 timeout_id; + GMainLoop *loop; + guint counter; + guint rs_counter; + guint32 timestamp1; + guint32 first_solicit; + guint32 timeout_id; } TestData; static void -test_simple_changed (NMNDisc *ndisc, const NMNDiscData *rdata, guint changed_int, TestData *data) +test_simple_changed(NMNDisc *ndisc, const NMNDiscData *rdata, guint changed_int, TestData *data) { - NMNDiscConfigMap changed = changed_int; - - g_assert_cmpint (changed, ==, NM_NDISC_CONFIG_DHCP_LEVEL | - NM_NDISC_CONFIG_GATEWAYS | - NM_NDISC_CONFIG_ADDRESSES | - NM_NDISC_CONFIG_ROUTES | - NM_NDISC_CONFIG_DNS_SERVERS | - NM_NDISC_CONFIG_DNS_DOMAINS | - NM_NDISC_CONFIG_HOP_LIMIT | - NM_NDISC_CONFIG_MTU); - g_assert_cmpint (rdata->dhcp_level, ==, NM_NDISC_DHCP_LEVEL_OTHERCONF); - match_gateway (rdata, 0, "fe80::1", data->timestamp1, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); - match_address (rdata, 0, "2001:db8:a:a::1", data->timestamp1, 10, 10); - match_route (rdata, 0, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1, 10, 10); - match_dns_server (rdata, 0, "2001:db8:c:c::1", data->timestamp1, 10); - match_dns_domain (rdata, 0, "foobar.com", data->timestamp1, 10); - - g_assert (nm_fake_ndisc_done (NM_FAKE_NDISC (ndisc))); - data->counter++; - g_main_loop_quit (data->loop); + NMNDiscConfigMap changed = changed_int; + + g_assert_cmpint(changed, + ==, + NM_NDISC_CONFIG_DHCP_LEVEL | NM_NDISC_CONFIG_GATEWAYS + | NM_NDISC_CONFIG_ADDRESSES | NM_NDISC_CONFIG_ROUTES + | NM_NDISC_CONFIG_DNS_SERVERS | NM_NDISC_CONFIG_DNS_DOMAINS + | NM_NDISC_CONFIG_HOP_LIMIT | NM_NDISC_CONFIG_MTU); + g_assert_cmpint(rdata->dhcp_level, ==, NM_NDISC_DHCP_LEVEL_OTHERCONF); + match_gateway(rdata, 0, "fe80::1", data->timestamp1, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); + match_address(rdata, 0, "2001:db8:a:a::1", data->timestamp1, 10, 10); + match_route(rdata, 0, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1, 10, 10); + match_dns_server(rdata, 0, "2001:db8:c:c::1", data->timestamp1, 10); + match_dns_domain(rdata, 0, "foobar.com", data->timestamp1, 10); + + g_assert(nm_fake_ndisc_done(NM_FAKE_NDISC(ndisc))); + data->counter++; + g_main_loop_quit(data->loop); } static void -test_simple (void) +test_simple(void) { - NMFakeNDisc *ndisc = ndisc_new (); - guint32 now = nm_utils_get_monotonic_timestamp_sec (); - TestData data = { g_main_loop_new (NULL, FALSE), 0, 0, now }; - guint id; - - id = nm_fake_ndisc_add_ra (ndisc, 1, NM_NDISC_DHCP_LEVEL_OTHERCONF, 4, 1500); - g_assert (id); - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); - nm_fake_ndisc_add_prefix (ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10, 10); - nm_fake_ndisc_add_dns_server (ndisc, id, "2001:db8:c:c::1", now, 10); - nm_fake_ndisc_add_dns_domain (ndisc, id, "foobar.com", now, 10); - - g_signal_connect (ndisc, - NM_NDISC_CONFIG_RECEIVED, - G_CALLBACK (test_simple_changed), - &data); - - nm_ndisc_start (NM_NDISC (ndisc)); - g_main_loop_run (data.loop); - g_assert_cmpint (data.counter, ==, 1); - - g_object_unref (ndisc); - g_main_loop_unref (data.loop); + NMFakeNDisc *ndisc = ndisc_new(); + guint32 now = nm_utils_get_monotonic_timestamp_sec(); + TestData data = {g_main_loop_new(NULL, FALSE), 0, 0, now}; + guint id; + + id = nm_fake_ndisc_add_ra(ndisc, 1, NM_NDISC_DHCP_LEVEL_OTHERCONF, 4, 1500); + g_assert(id); + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); + nm_fake_ndisc_add_prefix(ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10, 10); + nm_fake_ndisc_add_dns_server(ndisc, id, "2001:db8:c:c::1", now, 10); + nm_fake_ndisc_add_dns_domain(ndisc, id, "foobar.com", now, 10); + + g_signal_connect(ndisc, NM_NDISC_CONFIG_RECEIVED, G_CALLBACK(test_simple_changed), &data); + + nm_ndisc_start(NM_NDISC(ndisc)); + g_main_loop_run(data.loop); + g_assert_cmpint(data.counter, ==, 1); + + g_object_unref(ndisc); + g_main_loop_unref(data.loop); } static void -test_everything_rs_sent (NMNDisc *ndisc, TestData *data) +test_everything_rs_sent(NMNDisc *ndisc, TestData *data) { - g_assert_cmpint (data->rs_counter, ==, 0); - data->rs_counter++; + g_assert_cmpint(data->rs_counter, ==, 0); + data->rs_counter++; } static void -test_everything_changed (NMNDisc *ndisc, const NMNDiscData *rdata, guint changed_int, TestData *data) +test_everything_changed(NMNDisc *ndisc, const NMNDiscData *rdata, guint changed_int, TestData *data) { - NMNDiscConfigMap changed = changed_int; - - if (data->counter == 0) { - g_assert_cmpint (data->rs_counter, ==, 1); - g_assert_cmpint (changed, ==, NM_NDISC_CONFIG_DHCP_LEVEL | - NM_NDISC_CONFIG_GATEWAYS | - NM_NDISC_CONFIG_ADDRESSES | - NM_NDISC_CONFIG_ROUTES | - NM_NDISC_CONFIG_DNS_SERVERS | - NM_NDISC_CONFIG_DNS_DOMAINS | - NM_NDISC_CONFIG_HOP_LIMIT | - NM_NDISC_CONFIG_MTU); - match_gateway (rdata, 0, "fe80::1", data->timestamp1, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); - match_address (rdata, 0, "2001:db8:a:a::1", data->timestamp1, 10, 10); - match_route (rdata, 0, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1, 10, 10); - match_dns_server (rdata, 0, "2001:db8:c:c::1", data->timestamp1, 10); - match_dns_domain (rdata, 0, "foobar.com", data->timestamp1, 10); - } else if (data->counter == 1) { - g_assert_cmpint (changed, ==, NM_NDISC_CONFIG_GATEWAYS | - NM_NDISC_CONFIG_ADDRESSES | - NM_NDISC_CONFIG_ROUTES | - NM_NDISC_CONFIG_DNS_SERVERS | - NM_NDISC_CONFIG_DNS_DOMAINS); - - g_assert_cmpint (rdata->gateways_n, ==, 1); - match_gateway (rdata, 0, "fe80::2", data->timestamp1, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); - g_assert_cmpint (rdata->addresses_n, ==, 2); - match_address (rdata, 0, "2001:db8:a:a::1", data->timestamp1, 10, 0); - match_address (rdata, 1, "2001:db8:a:b::1", data->timestamp1, 10, 10); - g_assert_cmpint (rdata->routes_n, ==, 1); - match_route (rdata, 0, "2001:db8:a:b::", 64, "fe80::2", data->timestamp1, 10, 10); - g_assert_cmpint (rdata->dns_servers_n, ==, 1); - match_dns_server (rdata, 0, "2001:db8:c:c::2", data->timestamp1, 10); - g_assert_cmpint (rdata->dns_domains_n, ==, 1); - match_dns_domain (rdata, 0, "foobar2.com", data->timestamp1, 10); - - g_assert (nm_fake_ndisc_done (NM_FAKE_NDISC (ndisc))); - g_main_loop_quit (data->loop); - } else - g_assert_not_reached (); - - data->counter++; + NMNDiscConfigMap changed = changed_int; + + if (data->counter == 0) { + g_assert_cmpint(data->rs_counter, ==, 1); + g_assert_cmpint(changed, + ==, + NM_NDISC_CONFIG_DHCP_LEVEL | NM_NDISC_CONFIG_GATEWAYS + | NM_NDISC_CONFIG_ADDRESSES | NM_NDISC_CONFIG_ROUTES + | NM_NDISC_CONFIG_DNS_SERVERS | NM_NDISC_CONFIG_DNS_DOMAINS + | NM_NDISC_CONFIG_HOP_LIMIT | NM_NDISC_CONFIG_MTU); + match_gateway(rdata, 0, "fe80::1", data->timestamp1, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); + match_address(rdata, 0, "2001:db8:a:a::1", data->timestamp1, 10, 10); + match_route(rdata, 0, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1, 10, 10); + match_dns_server(rdata, 0, "2001:db8:c:c::1", data->timestamp1, 10); + match_dns_domain(rdata, 0, "foobar.com", data->timestamp1, 10); + } else if (data->counter == 1) { + g_assert_cmpint(changed, + ==, + NM_NDISC_CONFIG_GATEWAYS | NM_NDISC_CONFIG_ADDRESSES + | NM_NDISC_CONFIG_ROUTES | NM_NDISC_CONFIG_DNS_SERVERS + | NM_NDISC_CONFIG_DNS_DOMAINS); + + g_assert_cmpint(rdata->gateways_n, ==, 1); + match_gateway(rdata, 0, "fe80::2", data->timestamp1, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); + g_assert_cmpint(rdata->addresses_n, ==, 2); + match_address(rdata, 0, "2001:db8:a:a::1", data->timestamp1, 10, 0); + match_address(rdata, 1, "2001:db8:a:b::1", data->timestamp1, 10, 10); + g_assert_cmpint(rdata->routes_n, ==, 1); + match_route(rdata, 0, "2001:db8:a:b::", 64, "fe80::2", data->timestamp1, 10, 10); + g_assert_cmpint(rdata->dns_servers_n, ==, 1); + match_dns_server(rdata, 0, "2001:db8:c:c::2", data->timestamp1, 10); + g_assert_cmpint(rdata->dns_domains_n, ==, 1); + match_dns_domain(rdata, 0, "foobar2.com", data->timestamp1, 10); + + g_assert(nm_fake_ndisc_done(NM_FAKE_NDISC(ndisc))); + g_main_loop_quit(data->loop); + } else + g_assert_not_reached(); + + data->counter++; } static void -test_everything (void) +test_everything(void) { - NMFakeNDisc *ndisc = ndisc_new (); - guint32 now = nm_utils_get_monotonic_timestamp_sec (); - TestData data = { g_main_loop_new (NULL, FALSE), 0, 0, now }; - guint id; - - id = nm_fake_ndisc_add_ra (ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); - g_assert (id); - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); - nm_fake_ndisc_add_prefix (ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10, 10); - nm_fake_ndisc_add_dns_server (ndisc, id, "2001:db8:c:c::1", now, 10); - nm_fake_ndisc_add_dns_domain (ndisc, id, "foobar.com", now, 10); - - /* expire everything from the first RA in the second */ - id = nm_fake_ndisc_add_ra (ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); - g_assert (id); - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::1", now, 0, NM_ICMPV6_ROUTER_PREF_MEDIUM); - nm_fake_ndisc_add_prefix (ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 0, 0, 0); - nm_fake_ndisc_add_dns_server (ndisc, id, "2001:db8:c:c::1", now, 0); - nm_fake_ndisc_add_dns_domain (ndisc, id, "foobar.com", now, 0); - - /* and add some new stuff */ - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::2", now, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); - nm_fake_ndisc_add_prefix (ndisc, id, "2001:db8:a:b::", 64, "fe80::2", now, 10, 10, 10); - nm_fake_ndisc_add_dns_server (ndisc, id, "2001:db8:c:c::2", now, 10); - nm_fake_ndisc_add_dns_domain (ndisc, id, "foobar2.com", now, 10); - - g_signal_connect (ndisc, - NM_NDISC_CONFIG_RECEIVED, - G_CALLBACK (test_everything_changed), - &data); - g_signal_connect (ndisc, - NM_FAKE_NDISC_RS_SENT, - G_CALLBACK (test_everything_rs_sent), - &data); - - nm_ndisc_start (NM_NDISC (ndisc)); - g_main_loop_run (data.loop); - g_assert_cmpint (data.counter, ==, 2); - g_assert_cmpint (data.rs_counter, ==, 1); - - g_object_unref (ndisc); - g_main_loop_unref (data.loop); + NMFakeNDisc *ndisc = ndisc_new(); + guint32 now = nm_utils_get_monotonic_timestamp_sec(); + TestData data = {g_main_loop_new(NULL, FALSE), 0, 0, now}; + guint id; + + id = nm_fake_ndisc_add_ra(ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert(id); + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); + nm_fake_ndisc_add_prefix(ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10, 10); + nm_fake_ndisc_add_dns_server(ndisc, id, "2001:db8:c:c::1", now, 10); + nm_fake_ndisc_add_dns_domain(ndisc, id, "foobar.com", now, 10); + + /* expire everything from the first RA in the second */ + id = nm_fake_ndisc_add_ra(ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert(id); + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::1", now, 0, NM_ICMPV6_ROUTER_PREF_MEDIUM); + nm_fake_ndisc_add_prefix(ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 0, 0, 0); + nm_fake_ndisc_add_dns_server(ndisc, id, "2001:db8:c:c::1", now, 0); + nm_fake_ndisc_add_dns_domain(ndisc, id, "foobar.com", now, 0); + + /* and add some new stuff */ + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::2", now, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); + nm_fake_ndisc_add_prefix(ndisc, id, "2001:db8:a:b::", 64, "fe80::2", now, 10, 10, 10); + nm_fake_ndisc_add_dns_server(ndisc, id, "2001:db8:c:c::2", now, 10); + nm_fake_ndisc_add_dns_domain(ndisc, id, "foobar2.com", now, 10); + + g_signal_connect(ndisc, NM_NDISC_CONFIG_RECEIVED, G_CALLBACK(test_everything_changed), &data); + g_signal_connect(ndisc, NM_FAKE_NDISC_RS_SENT, G_CALLBACK(test_everything_rs_sent), &data); + + nm_ndisc_start(NM_NDISC(ndisc)); + g_main_loop_run(data.loop); + g_assert_cmpint(data.counter, ==, 2); + g_assert_cmpint(data.rs_counter, ==, 1); + + g_object_unref(ndisc); + g_main_loop_unref(data.loop); } static void -test_preference_order_cb (NMNDisc *ndisc, const NMNDiscData *rdata, guint changed_int, TestData *data) +test_preference_order_cb(NMNDisc * ndisc, + const NMNDiscData *rdata, + guint changed_int, + TestData * data) { - NMNDiscConfigMap changed = changed_int; - - if (data->counter == 1) { - g_assert_cmpint (changed, ==, NM_NDISC_CONFIG_GATEWAYS | - NM_NDISC_CONFIG_ADDRESSES | - NM_NDISC_CONFIG_ROUTES); - - g_assert_cmpint (rdata->gateways_n, ==, 2); - match_gateway (rdata, 0, "fe80::1", data->timestamp1, 10, NM_ICMPV6_ROUTER_PREF_HIGH); - match_gateway (rdata, 1, "fe80::2", data->timestamp1 + 1, 10, NM_ICMPV6_ROUTER_PREF_LOW); - g_assert_cmpint (rdata->addresses_n, ==, 2); - match_address (rdata, 0, "2001:db8:a:a::1", data->timestamp1, 10, 10); - match_address (rdata, 1, "2001:db8:a:b::1", data->timestamp1 + 1, 10, 10); - g_assert_cmpint (rdata->routes_n, ==, 2); - match_route (rdata, 0, "2001:db8:a:b::", 64, "fe80::2", data->timestamp1 + 1, 10, 10); - match_route (rdata, 1, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1, 10, 5); - - g_assert (nm_fake_ndisc_done (NM_FAKE_NDISC (ndisc))); - g_main_loop_quit (data->loop); - } - - data->counter++; + NMNDiscConfigMap changed = changed_int; + + if (data->counter == 1) { + g_assert_cmpint(changed, + ==, + NM_NDISC_CONFIG_GATEWAYS | NM_NDISC_CONFIG_ADDRESSES + | NM_NDISC_CONFIG_ROUTES); + + g_assert_cmpint(rdata->gateways_n, ==, 2); + match_gateway(rdata, 0, "fe80::1", data->timestamp1, 10, NM_ICMPV6_ROUTER_PREF_HIGH); + match_gateway(rdata, 1, "fe80::2", data->timestamp1 + 1, 10, NM_ICMPV6_ROUTER_PREF_LOW); + g_assert_cmpint(rdata->addresses_n, ==, 2); + match_address(rdata, 0, "2001:db8:a:a::1", data->timestamp1, 10, 10); + match_address(rdata, 1, "2001:db8:a:b::1", data->timestamp1 + 1, 10, 10); + g_assert_cmpint(rdata->routes_n, ==, 2); + match_route(rdata, 0, "2001:db8:a:b::", 64, "fe80::2", data->timestamp1 + 1, 10, 10); + match_route(rdata, 1, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1, 10, 5); + + g_assert(nm_fake_ndisc_done(NM_FAKE_NDISC(ndisc))); + g_main_loop_quit(data->loop); + } + + data->counter++; } static void -test_preference_order (void) +test_preference_order(void) { - NMFakeNDisc *ndisc = ndisc_new (); - guint32 now = nm_utils_get_monotonic_timestamp_sec (); - TestData data = { g_main_loop_new (NULL, FALSE), 0, 0, now }; - guint id; - - /* Test insertion order of gateways */ - - id = nm_fake_ndisc_add_ra (ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); - g_assert (id); - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_HIGH); - nm_fake_ndisc_add_prefix (ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10, 5); - - id = nm_fake_ndisc_add_ra (ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); - g_assert (id); - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::2", ++now, 10, NM_ICMPV6_ROUTER_PREF_LOW); - nm_fake_ndisc_add_prefix (ndisc, id, "2001:db8:a:b::", 64, "fe80::2", now, 10, 10, 10); - - g_signal_connect (ndisc, - NM_NDISC_CONFIG_RECEIVED, - G_CALLBACK (test_preference_order_cb), - &data); - - nm_ndisc_start (NM_NDISC (ndisc)); - g_main_loop_run (data.loop); - g_assert_cmpint (data.counter, ==, 2); - - g_object_unref (ndisc); - g_main_loop_unref (data.loop); + NMFakeNDisc *ndisc = ndisc_new(); + guint32 now = nm_utils_get_monotonic_timestamp_sec(); + TestData data = {g_main_loop_new(NULL, FALSE), 0, 0, now}; + guint id; + + /* Test insertion order of gateways */ + + id = nm_fake_ndisc_add_ra(ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert(id); + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_HIGH); + nm_fake_ndisc_add_prefix(ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10, 5); + + id = nm_fake_ndisc_add_ra(ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert(id); + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::2", ++now, 10, NM_ICMPV6_ROUTER_PREF_LOW); + nm_fake_ndisc_add_prefix(ndisc, id, "2001:db8:a:b::", 64, "fe80::2", now, 10, 10, 10); + + g_signal_connect(ndisc, NM_NDISC_CONFIG_RECEIVED, G_CALLBACK(test_preference_order_cb), &data); + + nm_ndisc_start(NM_NDISC(ndisc)); + g_main_loop_run(data.loop); + g_assert_cmpint(data.counter, ==, 2); + + g_object_unref(ndisc); + g_main_loop_unref(data.loop); } static void -test_preference_changed_cb (NMNDisc *ndisc, const NMNDiscData *rdata, guint changed_int, TestData *data) +test_preference_changed_cb(NMNDisc * ndisc, + const NMNDiscData *rdata, + guint changed_int, + TestData * data) { - NMNDiscConfigMap changed = changed_int; - - if (data->counter == 1) { - g_assert_cmpint (changed, ==, NM_NDISC_CONFIG_GATEWAYS | - NM_NDISC_CONFIG_ADDRESSES | - NM_NDISC_CONFIG_ROUTES); - g_assert_cmpint (rdata->gateways_n, ==, 2); - match_gateway (rdata, 0, "fe80::2", data->timestamp1 + 1, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); - match_gateway (rdata, 1, "fe80::1", data->timestamp1, 10, NM_ICMPV6_ROUTER_PREF_LOW); - g_assert_cmpint (rdata->addresses_n, ==, 2); - match_address (rdata, 0, "2001:db8:a:a::1", data->timestamp1, 10, 10); - match_address (rdata, 1, "2001:db8:a:b::1", data->timestamp1 + 1, 10, 10); - g_assert_cmpint (rdata->routes_n, ==, 2); - match_route (rdata, 0, "2001:db8:a:b::", 64, "fe80::2", data->timestamp1 + 1, 10, 10); - match_route (rdata, 1, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1, 10, 5); - } else if (data->counter == 2) { - g_assert_cmpint (changed, ==, NM_NDISC_CONFIG_GATEWAYS | - NM_NDISC_CONFIG_ADDRESSES | - NM_NDISC_CONFIG_ROUTES); - - g_assert_cmpint (rdata->gateways_n, ==, 2); - match_gateway (rdata, 0, "fe80::1", data->timestamp1 + 2, 10, NM_ICMPV6_ROUTER_PREF_HIGH); - match_gateway (rdata, 1, "fe80::2", data->timestamp1 + 1, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); - g_assert_cmpint (rdata->addresses_n, ==, 2); - match_address (rdata, 0, "2001:db8:a:a::1", data->timestamp1 + 3, 9, 9); - match_address (rdata, 1, "2001:db8:a:b::1", data->timestamp1 + 1, 10, 10); - g_assert_cmpint (rdata->routes_n, ==, 2); - match_route (rdata, 0, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1 + 2, 10, 15); - match_route (rdata, 1, "2001:db8:a:b::", 64, "fe80::2", data->timestamp1 + 1, 10, 10); - - g_assert (nm_fake_ndisc_done (NM_FAKE_NDISC (ndisc))); - g_main_loop_quit (data->loop); - } - - data->counter++; + NMNDiscConfigMap changed = changed_int; + + if (data->counter == 1) { + g_assert_cmpint(changed, + ==, + NM_NDISC_CONFIG_GATEWAYS | NM_NDISC_CONFIG_ADDRESSES + | NM_NDISC_CONFIG_ROUTES); + g_assert_cmpint(rdata->gateways_n, ==, 2); + match_gateway(rdata, 0, "fe80::2", data->timestamp1 + 1, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); + match_gateway(rdata, 1, "fe80::1", data->timestamp1, 10, NM_ICMPV6_ROUTER_PREF_LOW); + g_assert_cmpint(rdata->addresses_n, ==, 2); + match_address(rdata, 0, "2001:db8:a:a::1", data->timestamp1, 10, 10); + match_address(rdata, 1, "2001:db8:a:b::1", data->timestamp1 + 1, 10, 10); + g_assert_cmpint(rdata->routes_n, ==, 2); + match_route(rdata, 0, "2001:db8:a:b::", 64, "fe80::2", data->timestamp1 + 1, 10, 10); + match_route(rdata, 1, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1, 10, 5); + } else if (data->counter == 2) { + g_assert_cmpint(changed, + ==, + NM_NDISC_CONFIG_GATEWAYS | NM_NDISC_CONFIG_ADDRESSES + | NM_NDISC_CONFIG_ROUTES); + + g_assert_cmpint(rdata->gateways_n, ==, 2); + match_gateway(rdata, 0, "fe80::1", data->timestamp1 + 2, 10, NM_ICMPV6_ROUTER_PREF_HIGH); + match_gateway(rdata, 1, "fe80::2", data->timestamp1 + 1, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); + g_assert_cmpint(rdata->addresses_n, ==, 2); + match_address(rdata, 0, "2001:db8:a:a::1", data->timestamp1 + 3, 9, 9); + match_address(rdata, 1, "2001:db8:a:b::1", data->timestamp1 + 1, 10, 10); + g_assert_cmpint(rdata->routes_n, ==, 2); + match_route(rdata, 0, "2001:db8:a:a::", 64, "fe80::1", data->timestamp1 + 2, 10, 15); + match_route(rdata, 1, "2001:db8:a:b::", 64, "fe80::2", data->timestamp1 + 1, 10, 10); + + g_assert(nm_fake_ndisc_done(NM_FAKE_NDISC(ndisc))); + g_main_loop_quit(data->loop); + } + + data->counter++; } static void -test_preference_changed (void) +test_preference_changed(void) { - NMFakeNDisc *ndisc = ndisc_new (); - guint32 now = nm_utils_get_monotonic_timestamp_sec (); - TestData data = { g_main_loop_new (NULL, FALSE), 0, 0, now }; - guint id; - - /* Test that when a low-preference and medium gateway send advertisements, - * that if the low-preference gateway switches to high-preference, we do - * not get duplicates in the gateway list. - */ - - id = nm_fake_ndisc_add_ra (ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); - g_assert (id); - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_LOW); - nm_fake_ndisc_add_prefix (ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10, 5); - - id = nm_fake_ndisc_add_ra (ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); - g_assert (id); - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::2", ++now, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); - nm_fake_ndisc_add_prefix (ndisc, id, "2001:db8:a:b::", 64, "fe80::2", now, 10, 10, 10); - - id = nm_fake_ndisc_add_ra (ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); - g_assert (id); - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::1", ++now, 10, NM_ICMPV6_ROUTER_PREF_HIGH); - nm_fake_ndisc_add_prefix (ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10, 15); - - g_signal_connect (ndisc, - NM_NDISC_CONFIG_RECEIVED, - G_CALLBACK (test_preference_changed_cb), - &data); - - nm_ndisc_start (NM_NDISC (ndisc)); - g_main_loop_run (data.loop); - g_assert_cmpint (data.counter, ==, 3); - - g_object_unref (ndisc); - g_main_loop_unref (data.loop); + NMFakeNDisc *ndisc = ndisc_new(); + guint32 now = nm_utils_get_monotonic_timestamp_sec(); + TestData data = {g_main_loop_new(NULL, FALSE), 0, 0, now}; + guint id; + + /* Test that when a low-preference and medium gateway send advertisements, + * that if the low-preference gateway switches to high-preference, we do + * not get duplicates in the gateway list. + */ + + id = nm_fake_ndisc_add_ra(ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert(id); + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_LOW); + nm_fake_ndisc_add_prefix(ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10, 5); + + id = nm_fake_ndisc_add_ra(ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert(id); + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::2", ++now, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); + nm_fake_ndisc_add_prefix(ndisc, id, "2001:db8:a:b::", 64, "fe80::2", now, 10, 10, 10); + + id = nm_fake_ndisc_add_ra(ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert(id); + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::1", ++now, 10, NM_ICMPV6_ROUTER_PREF_HIGH); + nm_fake_ndisc_add_prefix(ndisc, id, "2001:db8:a:a::", 64, "fe80::1", now, 10, 10, 15); + + g_signal_connect(ndisc, + NM_NDISC_CONFIG_RECEIVED, + G_CALLBACK(test_preference_changed_cb), + &data); + + nm_ndisc_start(NM_NDISC(ndisc)); + g_main_loop_run(data.loop); + g_assert_cmpint(data.counter, ==, 3); + + g_object_unref(ndisc); + g_main_loop_unref(data.loop); } static void -test_dns_solicit_loop_changed (NMNDisc *ndisc, const NMNDiscData *rdata, guint changed_int, TestData *data) +test_dns_solicit_loop_changed(NMNDisc * ndisc, + const NMNDiscData *rdata, + guint changed_int, + TestData * data) { - data->counter++; + data->counter++; } static gboolean -success_timeout (TestData *data) +success_timeout(TestData *data) { - data->timeout_id = 0; - g_main_loop_quit (data->loop); - return G_SOURCE_REMOVE; + data->timeout_id = 0; + g_main_loop_quit(data->loop); + return G_SOURCE_REMOVE; } static void -test_dns_solicit_loop_rs_sent (NMFakeNDisc *ndisc, TestData *data) +test_dns_solicit_loop_rs_sent(NMFakeNDisc *ndisc, TestData *data) { - guint32 now = nm_utils_get_monotonic_timestamp_sec (); - guint id; - - if (data->rs_counter > 0 && data->rs_counter < 6) { - if (data->rs_counter == 1) { - data->first_solicit = now; - /* Kill the test after 10 seconds if it hasn't failed yet */ - data->timeout_id = g_timeout_add_seconds (10, (GSourceFunc) success_timeout, data); - } - - /* On all but the first solicitation, which should be triggered by the - * DNS servers reaching 1/2 lifetime, emit a new RA without the DNS - * servers again. - */ - id = nm_fake_ndisc_add_ra (ndisc, 0, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); - g_assert (id); - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); - - nm_fake_ndisc_emit_new_ras (ndisc); - } else if (data->rs_counter >= 6) { - /* Fail if we've sent too many solicitations in the past 4 seconds */ - g_assert_cmpint (now - data->first_solicit, >, 4); - g_source_remove (data->timeout_id); - g_main_loop_quit (data->loop); - } - data->rs_counter++; + guint32 now = nm_utils_get_monotonic_timestamp_sec(); + guint id; + + if (data->rs_counter > 0 && data->rs_counter < 6) { + if (data->rs_counter == 1) { + data->first_solicit = now; + /* Kill the test after 10 seconds if it hasn't failed yet */ + data->timeout_id = g_timeout_add_seconds(10, (GSourceFunc) success_timeout, data); + } + + /* On all but the first solicitation, which should be triggered by the + * DNS servers reaching 1/2 lifetime, emit a new RA without the DNS + * servers again. + */ + id = nm_fake_ndisc_add_ra(ndisc, 0, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert(id); + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_MEDIUM); + + nm_fake_ndisc_emit_new_ras(ndisc); + } else if (data->rs_counter >= 6) { + /* Fail if we've sent too many solicitations in the past 4 seconds */ + g_assert_cmpint(now - data->first_solicit, >, 4); + g_source_remove(data->timeout_id); + g_main_loop_quit(data->loop); + } + data->rs_counter++; } static void -test_dns_solicit_loop (void) +test_dns_solicit_loop(void) { - NMFakeNDisc *ndisc = ndisc_new (); - guint32 now = nm_utils_get_monotonic_timestamp_sec (); - TestData data = { g_main_loop_new (NULL, FALSE), 0, 0, now, 0 }; - guint id; - - /* Ensure that no solicitation loop happens when DNS servers or domains - * stop being sent in advertisements. This can happen if two routers - * send RAs, but the one sending DNS info stops responding, or if one - * router removes the DNS info from the RA without zero-lifetiming them - * first. - */ - - id = nm_fake_ndisc_add_ra (ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); - g_assert (id); - nm_fake_ndisc_add_gateway (ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_LOW); - nm_fake_ndisc_add_dns_server (ndisc, id, "2001:db8:c:c::1", now, 6); - - g_signal_connect (ndisc, - NM_NDISC_CONFIG_RECEIVED, - G_CALLBACK (test_dns_solicit_loop_changed), - &data); - g_signal_connect (ndisc, - NM_FAKE_NDISC_RS_SENT, - G_CALLBACK (test_dns_solicit_loop_rs_sent), - &data); - - nm_ndisc_start (NM_NDISC (ndisc)); - g_main_loop_run (data.loop); - g_assert_cmpint (data.counter, ==, 3); - - g_object_unref (ndisc); - g_main_loop_unref (data.loop); + NMFakeNDisc *ndisc = ndisc_new(); + guint32 now = nm_utils_get_monotonic_timestamp_sec(); + TestData data = {g_main_loop_new(NULL, FALSE), 0, 0, now, 0}; + guint id; + + /* Ensure that no solicitation loop happens when DNS servers or domains + * stop being sent in advertisements. This can happen if two routers + * send RAs, but the one sending DNS info stops responding, or if one + * router removes the DNS info from the RA without zero-lifetiming them + * first. + */ + + id = nm_fake_ndisc_add_ra(ndisc, 1, NM_NDISC_DHCP_LEVEL_NONE, 4, 1500); + g_assert(id); + nm_fake_ndisc_add_gateway(ndisc, id, "fe80::1", now, 10, NM_ICMPV6_ROUTER_PREF_LOW); + nm_fake_ndisc_add_dns_server(ndisc, id, "2001:db8:c:c::1", now, 6); + + g_signal_connect(ndisc, + NM_NDISC_CONFIG_RECEIVED, + G_CALLBACK(test_dns_solicit_loop_changed), + &data); + g_signal_connect(ndisc, + NM_FAKE_NDISC_RS_SENT, + G_CALLBACK(test_dns_solicit_loop_rs_sent), + &data); + + nm_ndisc_start(NM_NDISC(ndisc)); + g_main_loop_run(data.loop); + g_assert_cmpint(data.counter, ==, 3); + + g_object_unref(ndisc); + g_main_loop_unref(data.loop); } -NMTST_DEFINE (); +NMTST_DEFINE(); int -main (int argc, char **argv) +main(int argc, char **argv) { - nmtst_init_with_logging (&argc, &argv, NULL, "DEFAULT"); + nmtst_init_with_logging(&argc, &argv, NULL, "DEFAULT"); - if (nmtst_test_quick ()) { - g_print ("Skipping test: don't run long running test %s (NMTST_DEBUG=slow)\n", g_get_prgname () ?: "test-ndisc-fake"); - return g_test_run (); - } + if (nmtst_test_quick()) { + g_print("Skipping test: don't run long running test %s (NMTST_DEBUG=slow)\n", + g_get_prgname() ?: "test-ndisc-fake"); + return g_test_run(); + } - nm_fake_platform_setup (); + nm_fake_platform_setup(); - g_test_add_func ("/ndisc/simple", test_simple); - g_test_add_func ("/ndisc/everything-changed", test_everything); - g_test_add_func ("/ndisc/preference-order", test_preference_order); - g_test_add_func ("/ndisc/preference-changed", test_preference_changed); - g_test_add_func ("/ndisc/dns-solicit-loop", test_dns_solicit_loop); + g_test_add_func("/ndisc/simple", test_simple); + g_test_add_func("/ndisc/everything-changed", test_everything); + g_test_add_func("/ndisc/preference-order", test_preference_order); + g_test_add_func("/ndisc/preference-changed", test_preference_changed); + g_test_add_func("/ndisc/dns-solicit-loop", test_dns_solicit_loop); - return g_test_run (); + return g_test_run(); } diff --git a/src/ndisc/tests/test-ndisc-linux.c b/src/ndisc/tests/test-ndisc-linux.c index a25478e8..7e5e258b 100644 --- a/src/ndisc/tests/test-ndisc-linux.c +++ b/src/ndisc/tests/test-ndisc-linux.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2013 Red Hat, Inc. */ @@ -14,58 +14,72 @@ #include "nm-test-utils-core.h" -NMTST_DEFINE (); +NMTST_DEFINE(); int -main (int argc, char **argv) +main(int argc, char **argv) { - GMainLoop *loop; - NMNDisc *ndisc; - int ifindex = 1; - const char *ifname; - NMUtilsIPv6IfaceId iid = { }; - GError *error = NULL; - - nmtst_init_with_logging (&argc, &argv, NULL, "DEFAULT"); - - if (getuid () != 0) { - g_print ("Missing permission: must run as root\n"); - return EXIT_FAILURE; - } - - loop = g_main_loop_new (NULL, FALSE); - - nm_linux_platform_setup (); - - if (argv[1]) { - ifname = argv[1]; - ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, ifname); - } else { - g_print ("Missing command line argument \"interface-name\"\n"); - return EXIT_FAILURE; - } - - ndisc = nm_lndp_ndisc_new (NM_PLATFORM_GET, - ifindex, - ifname, - NM_UTILS_STABLE_TYPE_UUID, - "8ce666e8-d34d-4fb1-b858-f15a7al28086", - NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_EUI64, - NM_NDISC_NODE_TYPE_HOST, - 0, - &error); - if (!ndisc) { - g_print ("Failed to create NMNDisc instance: %s\n", error->message); - g_error_free (error); - return EXIT_FAILURE; - } - - iid.id_u8[7] = 1; - nm_ndisc_set_iid (ndisc, iid); - nm_ndisc_start (ndisc); - g_main_loop_run (loop); - - g_clear_object (&ndisc); - - return EXIT_SUCCESS; + GMainLoop * loop; + NMNDisc * ndisc; + int ifindex = 1; + const char * ifname; + NMUtilsIPv6IfaceId iid = {}; + GError * error = NULL; + int max_addresses; + int router_solicitations; + int router_solicitation_interval; + guint32 ra_timeout; + + nmtst_init_with_logging(&argc, &argv, NULL, "DEFAULT"); + + if (getuid() != 0) { + g_print("Missing permission: must run as root\n"); + return EXIT_FAILURE; + } + + loop = g_main_loop_new(NULL, FALSE); + + nm_linux_platform_setup(); + + if (argv[1]) { + ifname = argv[1]; + ifindex = nm_platform_link_get_ifindex(NM_PLATFORM_GET, ifname); + } else { + g_print("Missing command line argument \"interface-name\"\n"); + return EXIT_FAILURE; + } + + nm_lndp_ndisc_get_sysctl(NM_PLATFORM_GET, + ifname, + &max_addresses, + &router_solicitations, + &router_solicitation_interval, + &ra_timeout); + + ndisc = nm_lndp_ndisc_new(NM_PLATFORM_GET, + ifindex, + ifname, + NM_UTILS_STABLE_TYPE_UUID, + "8ce666e8-d34d-4fb1-b858-f15a7al28086", + NM_SETTING_IP6_CONFIG_ADDR_GEN_MODE_EUI64, + NM_NDISC_NODE_TYPE_HOST, + max_addresses, + router_solicitations, + router_solicitation_interval, + ra_timeout, + &error); + if (!ndisc) { + g_print("Failed to create NMNDisc instance: %s\n", error->message); + g_error_free(error); + return EXIT_FAILURE; + } + + iid.id_u8[7] = 1; + nm_ndisc_set_iid(ndisc, iid); + nm_ndisc_start(ndisc); + g_main_loop_run(loop); + + g_clear_object(&ndisc); + + return EXIT_SUCCESS; } |