static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link) { int r; sd_dhcp6_lease *lease; struct in6_addr ip6_addr; uint32_t lifetime_preferred, lifetime_valid; uint8_t prefixlen; r = sd_dhcp6_client_get_lease(client, &lease); if (r < 0) return r; sd_dhcp6_lease_reset_address_iter(lease); while (sd_dhcp6_lease_get_address(lease, &ip6_addr, &lifetime_preferred, &lifetime_valid) >= 0) { r = sd_icmp6_ra_get_prefixlen(link->icmp6_router_discovery, &ip6_addr, &prefixlen); if (r < 0 && r != -EADDRNOTAVAIL) { log_link_warning_errno(link, r, "Could not get prefix information: %m"); return r; } if (r == -EADDRNOTAVAIL) prefixlen = 128; r = dhcp6_address_change(link, &ip6_addr, prefixlen, lifetime_preferred, lifetime_valid); if (r < 0) return r; } return 0; }
static int dhcp6_prefix_expired(Link *link) { int r; sd_dhcp6_lease *lease; struct in6_addr *expired_prefix, ip6_addr; uint8_t expired_prefixlen; uint32_t lifetime_preferred, lifetime_valid; r = sd_icmp6_ra_get_expired_prefix(link->icmp6_router_discovery, &expired_prefix, &expired_prefixlen); if (r < 0) return r; r = sd_dhcp6_client_get_lease(link->dhcp6_client, &lease); if (r < 0) return r; sd_dhcp6_lease_reset_address_iter(lease); while (sd_dhcp6_lease_get_address(lease, &ip6_addr, &lifetime_preferred, &lifetime_valid) >= 0) { r = sd_icmp6_prefix_match(expired_prefix, expired_prefixlen, &ip6_addr); if (r >= 0) { r = dhcp6_address_update(link, &ip6_addr, 128, lifetime_preferred, lifetime_valid); return r; } } return 0; }
static int test_client_verify_information_request(DHCP6Message *information_request, uint8_t *option, size_t len) { _cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL; uint8_t *optval; uint16_t optcode; size_t optlen; bool found_clientid = false, found_elapsed_time = false; int r; struct in6_addr addr; uint32_t lt_pref, lt_valid; assert_se(information_request->type == DHCP6_INFORMATION_REQUEST); assert_se(dhcp6_lease_new(&lease) >= 0); while ((r = dhcp6_option_parse(&option, &len, &optcode, &optlen, &optval)) >= 0) { switch(optcode) { case SD_DHCP6_OPTION_CLIENTID: assert_se(!found_clientid); found_clientid = true; assert_se(optlen == sizeof(test_duid)); memcpy(&test_duid, optval, sizeof(test_duid)); break; case SD_DHCP6_OPTION_IA_NA: assert_not_reached("IA TA option must not be present"); break; case SD_DHCP6_OPTION_SERVERID: assert_not_reached("Server ID option must not be present"); break; case SD_DHCP6_OPTION_ELAPSED_TIME: assert_se(!found_elapsed_time); found_elapsed_time = true; assert_se(optlen == 2); break; } } assert_se(r == -ENOMSG); assert_se(found_clientid && found_elapsed_time); sd_dhcp6_lease_reset_address_iter(lease); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) == -ENOMSG); return 0; }
static int dhcp6_prefix_expired(Link *link) { int r; sd_dhcp6_lease *lease; struct in6_addr *expired_prefix, ip6_addr; uint8_t expired_prefixlen; uint32_t lifetime_preferred, lifetime_valid; r = sd_icmp6_ra_get_expired_prefix(link->icmp6_router_discovery, &expired_prefix, &expired_prefixlen); if (r < 0) return r; r = sd_dhcp6_client_get_lease(link->dhcp6_client, &lease); if (r < 0) return r; log_link_struct(link, LOG_INFO, "MESSAGE=%-*s: IPv6 prefix "SD_ICMP6_ADDRESS_FORMAT_STR"/%d expired", IFNAMSIZ, link->ifname, SD_ICMP6_ADDRESS_FORMAT_VAL(*expired_prefix), expired_prefixlen, NULL); sd_dhcp6_lease_reset_address_iter(lease); while (sd_dhcp6_lease_get_address(lease, &ip6_addr, &lifetime_preferred, &lifetime_valid) >= 0) { r = sd_icmp6_prefix_match(expired_prefix, expired_prefixlen, &ip6_addr); if (r < 0) continue; log_link_struct(link, LOG_INFO, "MESSAGE=%-*s: IPv6 prefix length updated "SD_ICMP6_ADDRESS_FORMAT_STR"/%d", IFNAMSIZ, link->ifname, SD_ICMP6_ADDRESS_FORMAT_VAL(ip6_addr), 128, NULL); dhcp6_address_update(link, &ip6_addr, 128, lifetime_preferred, lifetime_valid); } return 0; }
static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link) { int r; sd_dhcp6_lease *lease; struct in6_addr ip6_addr; uint32_t lifetime_preferred, lifetime_valid; r = sd_dhcp6_client_get_lease(client, &lease); if (r < 0) return r; sd_dhcp6_lease_reset_address_iter(lease); while (sd_dhcp6_lease_get_address(lease, &ip6_addr, &lifetime_preferred, &lifetime_valid) >= 0) { r = dhcp6_address_change(link, &ip6_addr, lifetime_preferred, lifetime_valid); if (r < 0) return r; } return 0; }
static NMIP6Config * lease_to_ip6_config (const char *iface, int ifindex, sd_dhcp6_lease *lease, GHashTable *options, gboolean log_lease, gboolean info_only, GError **error) { struct in6_addr tmp_addr, *dns; uint32_t lft_pref, lft_valid; NMIP6Config *ip6_config; const char *addr_str; char **domains; GString *str; int num, i; gint32 ts; g_return_val_if_fail (lease, NULL); ip6_config = nm_ip6_config_new (ifindex); ts = nm_utils_get_monotonic_timestamp_s (); str = g_string_sized_new (30); /* Addresses */ sd_dhcp6_lease_reset_address_iter (lease); while (sd_dhcp6_lease_get_address (lease, &tmp_addr, &lft_pref, &lft_valid) >= 0) { NMPlatformIP6Address address = { .plen = 128, .address = tmp_addr, .timestamp = ts, .lifetime = lft_valid, .preferred = lft_pref, .addr_source = NM_IP_CONFIG_SOURCE_DHCP, }; nm_ip6_config_add_address (ip6_config, &address); addr_str = nm_utils_inet6_ntop (&tmp_addr, NULL); g_string_append_printf (str, "%s%s", str->len ? " " : "", addr_str); LOG_LEASE (LOGD_DHCP6, " address %s", nm_platform_ip6_address_to_string (&address, NULL, 0)); }; if (str->len) { add_option (options, dhcp6_requests, DHCP6_OPTION_IP_ADDRESS, str->str); g_string_set_size (str , 0); } if (!info_only && nm_ip6_config_get_num_addresses (ip6_config) == 0) { g_string_free (str, TRUE); g_object_unref (ip6_config); g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, "no address received in managed mode"); return NULL; } /* DNS servers */ num = sd_dhcp6_lease_get_dns (lease, &dns); if (num > 0) { for (i = 0; i < num; i++) { nm_ip6_config_add_nameserver (ip6_config, &dns[i]); addr_str = nm_utils_inet6_ntop (&dns[i], NULL); g_string_append_printf (str, "%s%s", str->len ? " " : "", addr_str); LOG_LEASE (LOGD_DHCP6, " nameserver %s", addr_str); } add_option (options, dhcp6_requests, SD_DHCP6_OPTION_DNS_SERVERS, str->str); g_string_set_size (str, 0); } /* Search domains */ num = sd_dhcp6_lease_get_domains (lease, &domains); if (num > 0) { for (i = 0; i < num; i++) { nm_ip6_config_add_search (ip6_config, domains[i]); g_string_append_printf (str, "%s%s", str->len ? " " : "", domains[i]); LOG_LEASE (LOGD_DHCP6, " domain name '%s'", domains[i]); } add_option (options, dhcp6_requests, SD_DHCP6_OPTION_DOMAIN_LIST, str->str); g_string_set_size (str, 0); } g_string_free (str, TRUE); return ip6_config; } static void bound6_handle (NMDhcpSystemd *self) { NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self); const char *iface = nm_dhcp_client_get_iface (NM_DHCP_CLIENT (self)); gs_unref_object NMIP6Config *ip6_config = NULL; gs_unref_hashtable GHashTable *options = NULL; gs_free_error GError *error = NULL; sd_dhcp6_lease *lease; int r; r = sd_dhcp6_client_get_lease (priv->client6, &lease); if (r < 0 || !lease) { _LOGW (" no lease!"); nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_FAIL, NULL, NULL); return; } _LOGD ("lease available"); options = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free); ip6_config = lease_to_ip6_config (iface, nm_dhcp_client_get_ifindex (NM_DHCP_CLIENT (self)), lease, options, TRUE, priv->info_only, &error); if (ip6_config) { nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_BOUND, G_OBJECT (ip6_config), options); } else { _LOGW ("%s", error->message); nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_FAIL, NULL, NULL); } }
static int test_client_verify_request(DHCP6Message *request, uint8_t *option, size_t len) { _cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL; uint8_t *optval; uint16_t optcode; size_t optlen; bool found_clientid = false, found_iana = false, found_serverid = false, found_elapsed_time = false; int r; struct in6_addr addr; be32_t val; uint32_t lt_pref, lt_valid; assert_se(request->type == DHCP6_REQUEST); assert_se(dhcp6_lease_new(&lease) >= 0); while ((r = dhcp6_option_parse(&option, &len, &optcode, &optlen, &optval)) >= 0) { switch(optcode) { case SD_DHCP6_OPTION_CLIENTID: assert_se(!found_clientid); found_clientid = true; assert_se(!memcmp(optval, &test_duid, sizeof(test_duid))); break; case SD_DHCP6_OPTION_IA_NA: assert_se(!found_iana); found_iana = true; assert_se(optlen == 40); assert_se(!memcmp(optval, &test_iaid, sizeof(test_iaid))); val = htobe32(80); assert_se(!memcmp(optval + 4, &val, sizeof(val))); val = htobe32(120); assert_se(!memcmp(optval + 8, &val, sizeof(val))); assert_se(!dhcp6_option_parse_ia(&optval, &optlen, optcode, &lease->ia)); break; case SD_DHCP6_OPTION_SERVERID: assert_se(!found_serverid); found_serverid = true; assert_se(optlen == 14); assert_se(!memcmp(&msg_advertise[179], optval, optlen)); break; case SD_DHCP6_OPTION_ELAPSED_TIME: assert_se(!found_elapsed_time); found_elapsed_time = true; assert_se(optlen == 2); break; } } assert_se(r == -ENOMSG); assert_se(found_clientid && found_iana && found_serverid && found_elapsed_time); sd_dhcp6_lease_reset_address_iter(lease); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) >= 0); assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr))); assert_se(lt_pref == 150); assert_se(lt_valid == 180); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) == -ENOMSG); return 0; }
static int test_advertise_option(sd_event *e) { _cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL; DHCP6Message *advertise = (DHCP6Message *)msg_advertise; uint8_t *optval, *opt = msg_advertise + sizeof(DHCP6Message); uint16_t optcode; size_t optlen, len = sizeof(msg_advertise) - sizeof(DHCP6Message); be32_t val; uint8_t preference = 255; struct in6_addr addr; uint32_t lt_pref, lt_valid; int r; bool opt_clientid = false; struct in6_addr *addrs; char **domains; if (verbose) printf("* %s\n", __FUNCTION__); assert_se(dhcp6_lease_new(&lease) >= 0); assert_se(advertise->type == DHCP6_ADVERTISE); assert_se((be32toh(advertise->transaction_id) & 0x00ffffff) == 0x0fb4e5); while ((r = dhcp6_option_parse(&opt, &len, &optcode, &optlen, &optval)) >= 0) { switch(optcode) { case SD_DHCP6_OPTION_CLIENTID: assert_se(optlen == 14); opt_clientid = true; break; case SD_DHCP6_OPTION_IA_NA: assert_se(optlen == 94); assert_se(!memcmp(optval, &msg_advertise[26], optlen)); val = htobe32(0x0ecfa37d); assert_se(!memcmp(optval, &val, sizeof(val))); val = htobe32(80); assert_se(!memcmp(optval + 4, &val, sizeof(val))); val = htobe32(120); assert_se(!memcmp(optval + 8, &val, sizeof(val))); assert_se(dhcp6_option_parse_ia(&optval, &optlen, optcode, &lease->ia) >= 0); break; case SD_DHCP6_OPTION_SERVERID: assert_se(optlen == 14); assert_se(!memcmp(optval, &msg_advertise[179], optlen)); assert_se(dhcp6_lease_set_serverid(lease, optval, optlen) >= 0); break; case SD_DHCP6_OPTION_PREFERENCE: assert_se(optlen == 1); assert_se(!*optval); assert_se(dhcp6_lease_set_preference(lease, *optval) >= 0); break; case SD_DHCP6_OPTION_ELAPSED_TIME: assert_se(optlen == 2); break; case SD_DHCP6_OPTION_DNS_SERVERS: assert_se(optlen == 16); assert_se(dhcp6_lease_set_dns(lease, optval, optlen) >= 0); break; case SD_DHCP6_OPTION_DOMAIN_LIST: assert_se(optlen == 11); assert_se(dhcp6_lease_set_domains(lease, optval, optlen) >= 0); break; case SD_DHCP6_OPTION_SNTP_SERVERS: assert_se(optlen == 16); assert_se(dhcp6_lease_set_sntp(lease, optval, optlen) >= 0); break; default: break; } } assert_se(r == -ENOMSG); assert_se(opt_clientid); sd_dhcp6_lease_reset_address_iter(lease); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) >= 0); assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr))); assert_se(lt_pref == 150); assert_se(lt_valid == 180); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) == -ENOMSG); sd_dhcp6_lease_reset_address_iter(lease); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) >= 0); assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr))); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) == -ENOMSG); sd_dhcp6_lease_reset_address_iter(lease); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) >= 0); assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr))); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) == -ENOMSG); assert_se(dhcp6_lease_get_serverid(lease, &opt, &len) >= 0); assert_se(len == 14); assert_se(!memcmp(opt, &msg_advertise[179], len)); assert_se(dhcp6_lease_get_preference(lease, &preference) >= 0); assert_se(preference == 0); r = sd_dhcp6_lease_get_dns(lease, &addrs); assert_se(r == 1); assert_se(!memcmp(addrs, &msg_advertise[124], r * 16)); r = sd_dhcp6_lease_get_domains(lease, &domains); assert_se(r == 1); assert_se(!strcmp("lab.intra", domains[0])); assert_se(domains[1] == NULL); r = sd_dhcp6_lease_get_ntp_addrs(lease, &addrs); assert_se(r == 1); assert_se(!memcmp(addrs, &msg_advertise[159], r * 16)); return 0; }
static int test_client_verify_information_request(DHCP6Message *information_request, size_t len) { _cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL; size_t pos = 0; bool found_clientid = false, found_elapsed_time = false; struct in6_addr addr; uint32_t lt_pref, lt_valid; assert_se(information_request->type == DHCP6_INFORMATION_REQUEST); assert_se(dhcp6_lease_new(&lease) >= 0); len -= sizeof(DHCP6Message); while (pos < len) { DHCP6Option *option = (DHCP6Option *)&information_request->options[pos]; uint16_t optcode = be16toh(option->code); uint16_t optlen = be16toh(option->len); uint8_t *optval = option->data; switch(optcode) { case SD_DHCP6_OPTION_CLIENTID: assert_se(!found_clientid); found_clientid = true; assert_se(optlen == sizeof(test_duid)); memcpy(&test_duid, optval, sizeof(test_duid)); break; case SD_DHCP6_OPTION_IA_NA: assert_not_reached("IA TA option must not be present"); break; case SD_DHCP6_OPTION_SERVERID: assert_not_reached("Server ID option must not be present"); break; case SD_DHCP6_OPTION_ELAPSED_TIME: assert_se(!found_elapsed_time); found_elapsed_time = true; assert_se(optlen == 2); break; } pos += sizeof(*option) + optlen; } assert_se(pos == len); assert_se(found_clientid && found_elapsed_time); sd_dhcp6_lease_reset_address_iter(lease); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) == -ENOMSG); return 0; }
static int test_client_verify_request(DHCP6Message *request, size_t len) { _cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL; size_t pos = 0; bool found_clientid = false, found_iana = false, found_serverid = false, found_elapsed_time = false, found_fqdn = false; struct in6_addr addr; be32_t val; uint32_t lt_pref, lt_valid; assert_se(request->type == DHCP6_REQUEST); assert_se(dhcp6_lease_new(&lease) >= 0); len -= sizeof(DHCP6Message); while (pos < len) { DHCP6Option *option = (DHCP6Option *)&request->options[pos]; uint16_t optcode = be16toh(option->code); uint16_t optlen = be16toh(option->len); uint8_t *optval = option->data; switch(optcode) { case SD_DHCP6_OPTION_CLIENTID: assert_se(!found_clientid); found_clientid = true; assert_se(!memcmp(optval, &test_duid, sizeof(test_duid))); break; case SD_DHCP6_OPTION_IA_NA: assert_se(!found_iana); found_iana = true; assert_se(optlen == 40); assert_se(!memcmp(optval, &test_iaid, sizeof(test_iaid))); val = htobe32(80); assert_se(!memcmp(optval + 4, &val, sizeof(val))); val = htobe32(120); assert_se(!memcmp(optval + 8, &val, sizeof(val))); assert_se(!dhcp6_option_parse_ia(option, &lease->ia)); break; case SD_DHCP6_OPTION_SERVERID: assert_se(!found_serverid); found_serverid = true; assert_se(optlen == 14); assert_se(!memcmp(&msg_advertise[179], optval, optlen)); break; case SD_DHCP6_OPTION_ELAPSED_TIME: assert_se(!found_elapsed_time); found_elapsed_time = true; assert_se(optlen == 2); break; case SD_DHCP6_OPTION_FQDN: assert_se(!found_fqdn); found_fqdn = true; assert_se(optlen == 17); assert_se(optval[0] == 0x01); assert_se(!memcmp(optval + 1, fqdn_wire, sizeof(fqdn_wire))); break; } pos += sizeof(*option) + optlen; } assert_se(found_clientid && found_iana && found_serverid && found_elapsed_time); sd_dhcp6_lease_reset_address_iter(lease); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) >= 0); assert_se(!memcmp(&addr, &msg_advertise[42], sizeof(addr))); assert_se(lt_pref == 150); assert_se(lt_valid == 180); assert_se(sd_dhcp6_lease_get_address(lease, &addr, <_pref, <_valid) == -ENOMSG); return 0; }