diff options
Diffstat (limited to 'src/dhcp/nm-dhcp-utils.c')
-rw-r--r-- | src/dhcp/nm-dhcp-utils.c | 171 |
1 files changed, 89 insertions, 82 deletions
diff --git a/src/dhcp/nm-dhcp-utils.c b/src/dhcp/nm-dhcp-utils.c index 88ced0548e..554b7f4e73 100644 --- a/src/dhcp/nm-dhcp-utils.c +++ b/src/dhcp/nm-dhcp-utils.c @@ -25,18 +25,18 @@ ip4_process_dhcpcd_rfc3442_routes (const char *iface, const char *str, guint32 route_table, guint32 route_metric, - NMIP4Config *ip4_config, + NML3ConfigData *l3cd, guint32 *gwaddr) { - gs_free const char **routes = NULL; - const char **r; + gs_strfreev char **routes = NULL; gboolean have_routes = FALSE; + char **r; - routes = nm_utils_strsplit_set (str, " "); - if (!routes) + routes = g_strsplit (str, " ", 0); + if (g_strv_length (routes) == 0) return FALSE; - if ((NM_PTRARRAY_LEN (routes) % 2) != 0) { + if ((g_strv_length (routes) % 2) != 0) { _LOG2W (LOGD_DHCP4, iface, " classless static routes provided, but invalid"); return FALSE; } @@ -72,14 +72,17 @@ ip4_process_dhcpcd_rfc3442_routes (const char *iface, *gwaddr = rt_route; } else { _LOG2I (LOGD_DHCP4, iface, " classless static route %s/%d gw %s", *r, rt_cidr, *(r + 1)); - memset (&route, 0, sizeof (route)); - route.network = nm_utils_ip4_address_clear_host_address (rt_addr, rt_cidr); - route.plen = rt_cidr; - route.gateway = rt_route; - route.rt_source = NM_IP_CONFIG_SOURCE_DHCP; - route.metric = route_metric; - route.table_coerced = nm_platform_route_table_coerce (route_table); - nm_ip4_config_add_route (ip4_config, &route, NULL); + + route = (NMPlatformIP4Route) { + .network = nm_utils_ip4_address_clear_host_address (rt_addr, rt_cidr), + .plen = rt_cidr, + .gateway = rt_route, + .rt_source = NM_IP_CONFIG_SOURCE_DHCP, + .metric = route_metric, + .table_coerced = nm_platform_route_table_coerce (route_table), + }; + + nm_l3_config_data_add_route_4 (l3cd, &route); } } @@ -143,7 +146,7 @@ ip4_process_dhclient_rfc3442_routes (const char *iface, const char *str, guint32 route_table, guint32 route_metric, - NMIP4Config *ip4_config, + NML3ConfigData *l3cd, guint32 *gwaddr) { gs_free const char **octets = NULL; @@ -177,7 +180,8 @@ ip4_process_dhclient_rfc3442_routes (const char *iface, route.rt_source = NM_IP_CONFIG_SOURCE_DHCP; route.metric = route_metric; route.table_coerced = nm_platform_route_table_coerce (route_table); - nm_ip4_config_add_route (ip4_config, &route, NULL); + + nm_l3_config_data_add_route_4 (l3cd, &route); _LOG2I (LOGD_DHCP4, iface, " classless static route %s/%d gw %s", _nm_utils_inet4_ntop (route.network, b1), @@ -194,13 +198,13 @@ ip4_process_classless_routes (const char *iface, GHashTable *options, guint32 route_table, guint32 route_metric, - NMIP4Config *ip4_config, + NML3ConfigData *l3cd, guint32 *gwaddr) { const char *str, *p; g_return_val_if_fail (options != NULL, FALSE); - g_return_val_if_fail (ip4_config != NULL, FALSE); + g_return_val_if_fail (l3cd != NULL, FALSE); *gwaddr = 0; @@ -249,10 +253,10 @@ ip4_process_classless_routes (const char *iface, if (strchr (str, '/')) { /* dhcpcd format */ - return ip4_process_dhcpcd_rfc3442_routes (iface, str, route_table, route_metric, ip4_config, gwaddr); + return ip4_process_dhcpcd_rfc3442_routes (iface, str, route_table, route_metric, l3cd, gwaddr); } - return ip4_process_dhclient_rfc3442_routes (iface, str, route_table, route_metric, ip4_config, gwaddr); + return ip4_process_dhclient_rfc3442_routes (iface, str, route_table, route_metric, l3cd, gwaddr); } static void @@ -260,21 +264,18 @@ process_classful_routes (const char *iface, GHashTable *options, guint32 route_table, guint32 route_metric, - NMIP4Config *ip4_config) + NML3ConfigData *l3cd) { - gs_free const char **searches = NULL; - const char **s; + gs_strfreev char **searches = NULL; const char *str; + char **s; str = g_hash_table_lookup (options, "static_routes"); if (!str) return; - searches = nm_utils_strsplit_set (str, " "); - if (!searches) - return; - - if ((NM_PTRARRAY_LEN (searches) % 2) != 0) { + searches = g_strsplit (str, " ", 0); + if ((g_strv_length (searches) % 2)) { _LOG2I (LOGD_DHCP, iface, " static routes provided, but invalid"); return; } @@ -294,8 +295,10 @@ process_classful_routes (const char *iface, // FIXME: ensure the IP address and route are sane - memset (&route, 0, sizeof (route)); - route.network = rt_addr; + route = (NMPlatformIP4Route) { + .network = rt_addr, + }; + /* RFC 2132, updated by RFC 3442: The Static Routes option (option 33) does not provide a subnet mask for each route - it is assumed that the subnet mask is implicit in @@ -312,17 +315,18 @@ process_classful_routes (const char *iface, route.network = nm_utils_ip4_address_clear_host_address (route.network, route.plen); - nm_ip4_config_add_route (ip4_config, &route, NULL); + nm_l3_config_data_add_route_4 (l3cd, &route); + _LOG2I (LOGD_DHCP, iface, " static route %s", nm_platform_ip4_route_to_string (&route, NULL, 0)); } } static void -process_domain_search (const char *iface, +process_domain_search (int addr_family, + const char *iface, const char *str, - GFunc add_func, - gpointer user_data) + NML3ConfigData *l3cd) { gs_free const char **searches = NULL; gs_free char *unescaped = NULL; @@ -331,7 +335,7 @@ process_domain_search (const char *iface, int i; g_return_if_fail (str != NULL); - g_return_if_fail (add_func != NULL); + nm_assert (l3cd); unescaped = g_strdup (str); @@ -354,17 +358,11 @@ process_domain_search (const char *iface, searches = nm_utils_strsplit_set (unescaped, " "); for (s = searches; searches && *s; s++) { _LOG2I (LOGD_DHCP, iface, " domain search '%s'", *s); - add_func ((gpointer) *s, user_data); + nm_l3_config_data_add_search (l3cd, addr_family, *s); } } -static void -ip4_add_domain_search (gpointer data, gpointer user_data) -{ - nm_ip4_config_add_search (NM_IP4_CONFIG (user_data), (const char *) data); -} - -NMIP4Config * +NML3ConfigData * nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, int ifindex, const char *iface, @@ -372,7 +370,7 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, guint32 route_table, guint32 route_metric) { - gs_unref_object NMIP4Config *ip4_config = NULL; + nm_auto_unref_l3cd_init NML3ConfigData *l3cd = NULL; guint32 tmp_addr; in_addr_t addr; NMPlatformIP4Address address; @@ -381,19 +379,26 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, guint32 gateway = 0; guint8 plen = 0; char sbuf[NM_UTILS_INET_ADDRSTRLEN]; + guint32 now; g_return_val_if_fail (options != NULL, NULL); - ip4_config = nm_ip4_config_new (multi_idx, ifindex); - memset (&address, 0, sizeof (address)); - address.timestamp = nm_utils_get_monotonic_timestamp_sec (); + l3cd = nm_l3_config_data_new (multi_idx, ifindex); + nm_l3_config_data_set_source (l3cd, NM_IP_CONFIG_SOURCE_DHCP); + + now = nm_utils_get_monotonic_timestamp_sec (); + + address = (NMPlatformIP4Address) { + .timestamp = now, + }; str = g_hash_table_lookup (options, "ip_address"); - if (str && (inet_pton (AF_INET, str, &addr) > 0)) - _LOG2I (LOGD_DHCP4, iface, " address %s", str); - else + if ( !str + || !nm_utils_parse_inaddr_bin (AF_INET, str, NULL, &addr)) return NULL; + _LOG2I (LOGD_DHCP4, iface, " address %s", str); + str = g_hash_table_lookup (options, "subnet_mask"); if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) { plen = nm_utils_ip4_netmask_to_prefix (tmp_addr); @@ -408,8 +413,8 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, /* Routes: if the server returns classless static routes, we MUST ignore * the 'static_routes' option. */ - if (!ip4_process_classless_routes (iface, options, route_table, route_metric, ip4_config, &gateway)) - process_classful_routes (iface, options, route_table, route_metric, ip4_config); + if (!ip4_process_classless_routes (iface, options, route_table, route_metric, l3cd, &gateway)) + process_classful_routes (iface, options, route_table, route_metric, l3cd); if (gateway) { _LOG2I (LOGD_DHCP4, iface, " gateway %s", _nm_utils_inet4_ntop (gateway, sbuf)); @@ -420,10 +425,10 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, */ str = g_hash_table_lookup (options, "routers"); if (str) { - gs_free const char **routers = nm_utils_strsplit_set (str, " "); - const char **s; + char **routers = g_strsplit (str, " ", 0); + char **s; - for (s = routers; routers && *s; s++) { + for (s = routers; *s; s++) { /* FIXME: how to handle multiple routers? */ if (inet_pton (AF_INET, *s, &gateway) > 0) { _LOG2I (LOGD_DHCP4, iface, " gateway %s", *s); @@ -432,6 +437,7 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, } else _LOG2W (LOGD_DHCP4, iface, "ignoring invalid gateway '%s'", *s); } + g_strfreev (routers); } } @@ -443,7 +449,7 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, .metric = route_metric, }; - nm_ip4_config_add_route (ip4_config, &r, NULL); + nm_l3_config_data_add_route_4 (l3cd, &r); } str = g_hash_table_lookup (options, "dhcp_lease_time"); @@ -453,7 +459,8 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, } address.addr_source = NM_IP_CONFIG_SOURCE_DHCP; - nm_ip4_config_add_address (ip4_config, &address); + + nm_l3_config_data_add_address_4 (l3cd, &address); str = g_hash_table_lookup (options, "host_name"); if (str) @@ -467,7 +474,7 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, for (s = dns; dns && *s; s++) { if (inet_pton (AF_INET, *s, &tmp_addr) > 0) { if (tmp_addr) { - nm_ip4_config_add_nameserver (ip4_config, tmp_addr); + nm_l3_config_data_add_nameserver (l3cd, AF_INET, &tmp_addr); _LOG2I (LOGD_DHCP4, iface, " nameserver '%s'", *s); } } else @@ -482,13 +489,13 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, for (s = domains; domains && *s; s++) { _LOG2I (LOGD_DHCP4, iface, " domain name '%s'", *s); - nm_ip4_config_add_domain (ip4_config, *s); + nm_l3_config_data_add_domain (l3cd, AF_INET, *s); } } str = g_hash_table_lookup (options, "domain_search"); if (str) - process_domain_search (iface, str, ip4_add_domain_search, ip4_config); + process_domain_search (AF_INET, iface, str, l3cd); str = g_hash_table_lookup (options, "netbios_name_servers"); if (str) { @@ -498,7 +505,7 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, for (s = nbns; nbns && *s; s++) { if (inet_pton (AF_INET, *s, &tmp_addr) > 0) { if (tmp_addr) { - nm_ip4_config_add_wins (ip4_config, tmp_addr); + nm_l3_config_data_add_wins (l3cd, tmp_addr); _LOG2I (LOGD_DHCP4, iface, " wins '%s'", *s); } } else @@ -516,13 +523,13 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, return NULL; if (int_mtu > 576) - nm_ip4_config_set_mtu (ip4_config, int_mtu, NM_IP_CONFIG_SOURCE_DHCP); + nm_l3_config_data_set_mtu (l3cd, int_mtu); } str = g_hash_table_lookup (options, "nis_domain"); if (str) { _LOG2I (LOGD_DHCP4, iface, " NIS domain '%s'", str); - nm_ip4_config_set_nis_domain (ip4_config, str); + nm_l3_config_data_add_domain (l3cd, AF_INET, str); } str = g_hash_table_lookup (options, "nis_servers"); @@ -533,7 +540,7 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, for (s = nis; nis && *s; s++) { if (inet_pton (AF_INET, *s, &tmp_addr) > 0) { if (tmp_addr) { - nm_ip4_config_add_nis_server (ip4_config, tmp_addr); + nm_l3_config_data_add_nis_server (l3cd, tmp_addr); _LOG2I (LOGD_DHCP4, iface, " nis '%s'", *s); } } else @@ -542,19 +549,14 @@ nm_dhcp_utils_ip4_config_from_options (NMDedupMultiIndex *multi_idx, } str = g_hash_table_lookup (options, "vendor_encapsulated_options"); - nm_ip4_config_set_metered (ip4_config, str && strstr (str, "ANDROID_METERED")); + if (str && strstr (str, "ANDROID_METERED")) + nm_l3_config_data_set_metered (l3cd, TRUE); - return g_steal_pointer (&ip4_config); + return g_steal_pointer (&l3cd); } /*****************************************************************************/ -static void -ip6_add_domain_search (gpointer data, gpointer user_data) -{ - nm_ip6_config_add_search (NM_IP6_CONFIG (user_data), (const char *) data); -} - NMPlatformIP6Address nm_dhcp_utils_ip6_prefix_from_options (GHashTable *options) { @@ -603,25 +605,30 @@ nm_dhcp_utils_ip6_prefix_from_options (GHashTable *options) return address; } -NMIP6Config * +NML3ConfigData * nm_dhcp_utils_ip6_config_from_options (NMDedupMultiIndex *multi_idx, int ifindex, const char *iface, GHashTable *options, gboolean info_only) { - gs_unref_object NMIP6Config *ip6_config = NULL; + nm_auto_unref_l3cd_init NML3ConfigData *l3cd = NULL; struct in6_addr tmp_addr; NMPlatformIP6Address address; char *str = NULL; + guint32 now; g_return_val_if_fail (options != NULL, NULL); - memset (&address, 0, sizeof (address)); - address.plen = 128; - address.timestamp = nm_utils_get_monotonic_timestamp_sec (); + now = nm_utils_get_monotonic_timestamp_sec (); + + address = (NMPlatformIP6Address) { + .plen = 128, + .timestamp = now, + }; - ip6_config = nm_ip6_config_new (multi_idx, ifindex); + l3cd = nm_l3_config_data_new (multi_idx, ifindex); + nm_l3_config_data_set_source (l3cd, NM_IP_CONFIG_SOURCE_DHCP); str = g_hash_table_lookup (options, "max_life"); if (str) { @@ -645,7 +652,7 @@ nm_dhcp_utils_ip6_config_from_options (NMDedupMultiIndex *multi_idx, address.address = tmp_addr; address.addr_source = NM_IP_CONFIG_SOURCE_DHCP; - nm_ip6_config_add_address (ip6_config, &address); + nm_l3_config_data_add_address_6 (l3cd, &address); _LOG2I (LOGD_DHCP6, iface, " address %s", str); } else if (info_only == FALSE) { /* No address in Managed mode is a hard error */ @@ -664,7 +671,7 @@ nm_dhcp_utils_ip6_config_from_options (NMDedupMultiIndex *multi_idx, for (s = dns; dns && *s; s++) { if (inet_pton (AF_INET6, *s, &tmp_addr) > 0) { if (!IN6_IS_ADDR_UNSPECIFIED (&tmp_addr)) { - nm_ip6_config_add_nameserver (ip6_config, &tmp_addr); + nm_l3_config_data_add_nameserver (l3cd, AF_INET6, &tmp_addr); _LOG2I (LOGD_DHCP6, iface, " nameserver '%s'", *s); } } else @@ -674,9 +681,9 @@ nm_dhcp_utils_ip6_config_from_options (NMDedupMultiIndex *multi_idx, str = g_hash_table_lookup (options, "dhcp6_domain_search"); if (str) - process_domain_search (iface, str, ip6_add_domain_search, ip6_config); + process_domain_search (AF_INET6, iface, str, l3cd); - return g_steal_pointer (&ip6_config); + return g_steal_pointer (&l3cd); } char * |