dhcp: handle classless static routes (bgo #567246)

Based on patches by Johan Bilien <jobi@via.ecp.fr>,
nick loeve <trickie@gmail.com>, and Roy Marples <roy@marples.name>
with significant changes for dhclient formatting and test cases.

Note that dhclient needs help before it can actually parse
classless static routes by adding the following to the
dhclient.conf file:

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;
This commit is contained in:
Dan Williams
2009-01-18 23:19:09 -05:00
parent f06a136a99
commit f2e8870338
9 changed files with 1065 additions and 199 deletions

View File

@@ -26,6 +26,8 @@
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <config.h>
@@ -288,3 +290,133 @@ out:
g_ptr_array_free (dhclient_argv, TRUE);
return success;
}
static const char **
process_rfc3442_route (const char **octets, NMIP4Route **out_route)
{
const char **o = octets;
int addr_len = 0, i = 0;
long int tmp;
NMIP4Route *route;
char *next_hop;
struct in_addr tmp_addr;
if (!*o)
return o; /* no prefix */
tmp = strtol (*o, NULL, 10);
if (tmp < 0 || tmp > 32) /* 32 == max IP4 prefix length */
return o;
route = nm_ip4_route_new ();
nm_ip4_route_set_prefix (route, (guint32) tmp);
o++;
if (tmp > 0)
addr_len = ((tmp - 1) / 8) + 1;
/* ensure there's at least the address + next hop left */
if (g_strv_length ((char **) o) < addr_len + 4)
goto error;
if (tmp) {
const char *addr[4] = { "0", "0", "0", "0" };
char *str_addr;
for (i = 0; i < addr_len; i++)
addr[i] = *o++;
str_addr = g_strjoin (".", addr[0], addr[1], addr[2], addr[3], NULL);
if (inet_pton (AF_INET, str_addr, &tmp_addr) <= 0) {
g_free (str_addr);
goto error;
}
tmp_addr.s_addr &= nm_utils_ip4_prefix_to_netmask ((guint32) tmp);
nm_ip4_route_set_dest (route, tmp_addr.s_addr);
}
/* Handle next hop */
next_hop = g_strjoin (".", o[0], o[1], o[2], o[3], NULL);
if (inet_pton (AF_INET, next_hop, &tmp_addr) <= 0) {
g_free (next_hop);
goto error;
}
nm_ip4_route_set_next_hop (route, tmp_addr.s_addr);
g_free (next_hop);
*out_route = route;
return o + 4; /* advance to past the next hop */
error:
nm_ip4_route_unref (route);
return o;
}
gboolean
nm_dhcp_client_process_classless_routes (GHashTable *options,
NMIP4Config *ip4_config,
guint32 *gwaddr)
{
const char *str;
char **octets, **o;
gboolean have_routes = FALSE;
NMIP4Route *route = NULL;
/* dhclient doesn't have actual support for rfc3442 classless static routes
* upstream. Thus, people resort to defining the option in dhclient.conf
* and using arbitrary formats like so:
*
* option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;
*
* See https://lists.isc.org/pipermail/dhcp-users/2008-December/007629.html
*/
str = g_hash_table_lookup (options, "new_rfc3442_classless_static_routes");
/* Microsoft version; same as rfc3442 but with a different option # (249) */
if (!str)
str = g_hash_table_lookup (options, "new_ms_classless_static_routes");
if (!str || !strlen (str))
return FALSE;
o = octets = g_strsplit (str, " ", 0);
if (g_strv_length (octets) < 5) {
nm_warning ("Ignoring invalid classless static routes '%s'", str);
goto out;
}
while (*o) {
route = NULL;
o = (char **) process_rfc3442_route ((const char **) o, &route);
if (!route) {
nm_warning ("Ignoring invalid classless static routes");
break;
}
have_routes = TRUE;
if (nm_ip4_route_get_prefix (route) == 0) {
/* gateway passed as classless static route */
*gwaddr = nm_ip4_route_get_next_hop (route);
nm_ip4_route_unref (route);
} else {
char addr[INET_ADDRSTRLEN + 1];
char nh[INET_ADDRSTRLEN + 1];
struct in_addr tmp;
/* normal route */
nm_ip4_config_take_route (ip4_config, route);
tmp.s_addr = nm_ip4_route_get_dest (route);
inet_ntop (AF_INET, &tmp, addr, sizeof (addr));
tmp.s_addr = nm_ip4_route_get_next_hop (route);
inet_ntop (AF_INET, &tmp, nh, sizeof (nh));
nm_info (" classless static route %s/%d gw %s",
addr, nm_ip4_route_get_prefix (route), nh);
}
}
out:
g_strfreev (octets);
return have_routes;
}

View File

@@ -28,6 +28,8 @@
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "nm-dhcp-manager.h"
#include "nm-utils.h"
@@ -118,3 +120,80 @@ out:
g_ptr_array_free (argv, TRUE);
return success;
}
gboolean
nm_dhcp_client_process_classless_routes (GHashTable *options,
NMIP4Config *ip4_config,
guint32 *gwaddr)
{
const char *str;
char **routes, **r;
gboolean have_routes = FALSE;
/* Classless static routes over-ride any static routes and routers
* provided. We should also check for MS classless static routes as
* they implemented the draft RFC using their own code.
*/
str = g_hash_table_lookup (options, "new_classless_static_routes");
if (!str)
str = g_hash_table_lookup (options, "new_ms_classless_static_routes");
if (!str || !strlen (str))
return FALSE;
routes = g_strsplit (str, " ", 0);
if (g_strv_length (routes) == 0)
goto out;
if ((g_strv_length (routes) % 2) != 0) {
nm_info (" classless static routes provided, but invalid");
goto out;
}
for (r = routes; *r; r += 2) {
char *slash;
NMIP4Route *route;
int rt_cidr = 32;
struct in_addr rt_addr;
struct in_addr rt_route;
slash = strchr(*r, '/');
if (slash) {
*slash = '\0';
errno = 0;
rt_cidr = strtol (slash + 1, NULL, 10);
if ((errno == EINVAL) || (errno == ERANGE)) {
nm_warning ("DHCP provided invalid classless static route cidr: '%s'", slash + 1);
continue;
}
}
if (inet_pton (AF_INET, *r, &rt_addr) <= 0) {
nm_warning ("DHCP provided invalid classless static route address: '%s'", *r);
continue;
}
if (inet_pton (AF_INET, *(r + 1), &rt_route) <= 0) {
nm_warning ("DHCP provided invalid classless static route gateway: '%s'", *(r + 1));
continue;
}
have_routes = TRUE;
if (rt_cidr == 0 && rt_addr.s_addr == 0) {
/* FIXME: how to handle multiple routers? */
*gwaddr = rt_addr.s_addr;
} else {
route = nm_ip4_route_new ();
nm_ip4_route_set_dest (route, (guint32) rt_addr.s_addr);
nm_ip4_route_set_prefix (route, rt_cidr);
nm_ip4_route_set_next_hop (route, (guint32) rt_route.s_addr);
nm_ip4_config_take_route (ip4_config, route);
nm_info (" classless static route %s/%d gw %s", *r, rt_cidr, *(r + 1));
}
}
out:
g_strfreev (routes);
return have_routes;
}

View File

@@ -720,6 +720,209 @@ nm_dhcp_manager_cancel_transaction (NMDHCPManager *manager,
nm_dhcp_manager_cancel_transaction_real (device);
}
static void
process_classful_routes (GHashTable *options, NMIP4Config *ip4_config)
{
const char *str;
char **searches, **s;
str = g_hash_table_lookup (options, "new_static_routes");
if (!str)
return;
searches = g_strsplit (str, " ", 0);
if ((g_strv_length (searches) % 2)) {
nm_info (" static routes provided, but invalid");
goto out;
}
for (s = searches; *s; s += 2) {
NMIP4Route *route;
struct in_addr rt_addr;
struct in_addr rt_route;
if (inet_pton (AF_INET, *s, &rt_addr) <= 0) {
nm_warning ("DHCP provided invalid static route address: '%s'", *s);
continue;
}
if (inet_pton (AF_INET, *(s + 1), &rt_route) <= 0) {
nm_warning ("DHCP provided invalid static route gateway: '%s'", *(s + 1));
continue;
}
// FIXME: ensure the IP addresse and route are sane
route = nm_ip4_route_new ();
nm_ip4_route_set_dest (route, (guint32) rt_addr.s_addr);
nm_ip4_route_set_prefix (route, 32); /* 255.255.255.255 */
nm_ip4_route_set_next_hop (route, (guint32) rt_route.s_addr);
nm_ip4_config_take_route (ip4_config, route);
nm_info (" static route %s gw %s", *s, *(s + 1));
}
out:
g_strfreev (searches);
}
/* Given a table of DHCP options from the client, convert into an IP4Config */
NMIP4Config *
nm_dhcp_manager_options_to_ip4_config (const char *iface, GHashTable *options)
{
NMIP4Config *ip4_config = NULL;
struct in_addr tmp_addr;
NMIP4Address *addr = NULL;
char *str = NULL;
guint32 gwaddr = 0;
gboolean have_classless = FALSE;
g_return_val_if_fail (iface != NULL, NULL);
g_return_val_if_fail (options != NULL, NULL);
ip4_config = nm_ip4_config_new ();
if (!ip4_config) {
nm_warning ("%s: couldn't allocate memory for an IP4Config!", iface);
return NULL;
}
addr = nm_ip4_address_new ();
if (!addr) {
nm_warning ("%s: couldn't allocate memory for an IP4 Address!", iface);
goto error;
}
str = g_hash_table_lookup (options, "new_ip_address");
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
nm_ip4_address_set_address (addr, tmp_addr.s_addr);
nm_info (" address %s", str);
} else
goto error;
str = g_hash_table_lookup (options, "new_subnet_mask");
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
nm_ip4_address_set_prefix (addr, nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr));
nm_info (" prefix %d (%s)", nm_ip4_address_get_prefix (addr), str);
}
/* Routes: if the server returns classless static routes, we MUST ignore
* the 'static_routes' option.
*/
have_classless = nm_dhcp_client_process_classless_routes (options, ip4_config, &gwaddr);
if (!have_classless) {
gwaddr = 0; /* Ensure client code doesn't lie */
process_classful_routes (options, ip4_config);
}
if (gwaddr) {
char buf[INET_ADDRSTRLEN + 1];
inet_ntop (AF_INET, &gwaddr, buf, sizeof (buf));
nm_info (" gateway %s", buf);
nm_ip4_address_set_gateway (addr, gwaddr);
} else {
/* If the gateway wasn't provided as a classless static route with a
* subnet length of 0, try to find it using the old-style 'routers' option.
*/
str = g_hash_table_lookup (options, "new_routers");
if (str) {
char **routers = g_strsplit (str, " ", 0);
char **s;
for (s = routers; *s; s++) {
/* FIXME: how to handle multiple routers? */
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_address_set_gateway (addr, tmp_addr.s_addr);
nm_info (" gateway %s", *s);
break;
} else
nm_warning ("Ignoring invalid gateway '%s'", *s);
}
g_strfreev (routers);
}
}
nm_ip4_config_take_address (ip4_config, addr);
addr = NULL;
str = g_hash_table_lookup (options, "new_host_name");
if (str)
nm_info (" hostname '%s'", str);
str = g_hash_table_lookup (options, "new_domain_name_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_nameserver (ip4_config, tmp_addr.s_addr);
nm_info (" nameserver '%s'", *s);
} else
nm_warning ("Ignoring invalid nameserver '%s'", *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (options, "new_domain_name");
if (str) {
char **domains = g_strsplit (str, " ", 0);
char **s;
for (s = domains; *s; s++) {
nm_info (" domain name '%s'", *s);
nm_ip4_config_add_domain (ip4_config, *s);
}
g_strfreev (domains);
}
str = g_hash_table_lookup (options, "new_domain_search");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
nm_info (" domain search '%s'", *s);
nm_ip4_config_add_search (ip4_config, *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (options, "new_netbios_name_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_wins (ip4_config, tmp_addr.s_addr);
nm_info (" wins '%s'", *s);
} else
nm_warning ("Ignoring invalid WINS server '%s'", *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (options, "new_interface_mtu");
if (str) {
int int_mtu;
errno = 0;
int_mtu = strtol (str, NULL, 10);
if ((errno == EINVAL) || (errno == ERANGE))
goto error;
if (int_mtu)
nm_ip4_config_set_mtu (ip4_config, int_mtu);
}
return ip4_config;
error:
if (addr)
nm_ip4_address_unref (addr);
g_object_unref (ip4_config);
return NULL;
}
/*
* nm_dhcp_manager_get_ip4_config
@@ -733,10 +936,6 @@ nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager,
{
NMDHCPManagerPrivate *priv;
NMDHCPDevice *device;
NMIP4Config *ip4_config = NULL;
struct in_addr tmp_addr;
NMIP4Address *addr = NULL;
char *str = NULL;
g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL);
g_return_val_if_fail (iface != NULL, NULL);
@@ -754,179 +953,23 @@ nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager,
return NULL;
}
ip4_config = nm_ip4_config_new ();
if (!ip4_config) {
nm_warning ("%s: couldn't allocate memory for an IP4Config!", device->iface);
return NULL;
}
addr = nm_ip4_address_new ();
if (!addr) {
nm_warning ("%s: couldn't allocate memory for an IP4 Address!", device->iface);
goto error;
}
str = g_hash_table_lookup (device->options, "new_ip_address");
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
nm_ip4_address_set_address (addr, tmp_addr.s_addr);
nm_info (" address %s", str);
} else
goto error;
str = g_hash_table_lookup (device->options, "new_subnet_mask");
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
nm_ip4_address_set_prefix (addr, nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr));
nm_info (" prefix %d (%s)", nm_ip4_address_get_prefix (addr), str);
}
str = g_hash_table_lookup (device->options, "new_routers");
if (str) {
char **routers = g_strsplit (str, " ", 0);
char **s;
for (s = routers; *s; s++) {
/* FIXME: how to handle multiple routers? */
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_address_set_gateway (addr, tmp_addr.s_addr);
nm_info (" gateway %s", *s);
break;
} else
nm_warning ("Ignoring invalid gateway '%s'", *s);
}
g_strfreev (routers);
}
nm_ip4_config_take_address (ip4_config, addr);
addr = NULL;
str = g_hash_table_lookup (device->options, "new_host_name");
if (str)
nm_info (" hostname '%s'", str);
str = g_hash_table_lookup (device->options, "new_domain_name_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_nameserver (ip4_config, tmp_addr.s_addr);
nm_info (" nameserver '%s'", *s);
} else
nm_warning ("Ignoring invalid nameserver '%s'", *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (device->options, "new_domain_name");
if (str) {
char **domains = g_strsplit (str, " ", 0);
char **s;
for (s = domains; *s; s++) {
nm_info (" domain name '%s'", *s);
nm_ip4_config_add_domain (ip4_config, *s);
}
g_strfreev (domains);
}
str = g_hash_table_lookup (device->options, "new_domain_search");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
nm_info (" domain search '%s'", *s);
nm_ip4_config_add_search (ip4_config, *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (device->options, "new_netbios_name_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_wins (ip4_config, tmp_addr.s_addr);
nm_info (" wins '%s'", *s);
} else
nm_warning ("Ignoring invalid WINS server '%s'", *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (device->options, "new_static_routes");
if (str) {
char **searches = g_strsplit (str, " ", 0);
if ((g_strv_length (searches) % 2) == 0) {
char **s;
for (s = searches; *s; s += 2) {
NMIP4Route *route;
struct in_addr rt_addr;
struct in_addr rt_route;
if (inet_pton (AF_INET, *s, &rt_addr) <= 0) {
nm_warning ("DHCP provided invalid static route address: '%s'", *s);
continue;
}
if (inet_pton (AF_INET, *(s + 1), &rt_route) <= 0) {
nm_warning ("DHCP provided invalid static route gateway: '%s'", *(s + 1));
continue;
}
// FIXME: ensure the IP addresse and route are sane
route = nm_ip4_route_new ();
nm_ip4_route_set_dest (route, (guint32) rt_addr.s_addr);
nm_ip4_route_set_prefix (route, 32); /* 255.255.255.255 */
nm_ip4_route_set_next_hop (route, (guint32) rt_route.s_addr);
nm_ip4_config_take_route (ip4_config, route);
nm_info (" static route %s gw %s", *s, *(s + 1));
}
} else {
nm_info (" static routes provided, but invalid");
}
g_strfreev (searches);
}
str = g_hash_table_lookup (device->options, "new_interface_mtu");
if (str) {
int int_mtu;
errno = 0;
int_mtu = strtol (str, NULL, 10);
if ((errno == EINVAL) || (errno == ERANGE))
goto error;
if (int_mtu)
nm_ip4_config_set_mtu (ip4_config, int_mtu);
}
return ip4_config;
error:
if (addr)
g_free (addr);
g_object_unref (ip4_config);
return NULL;
return nm_dhcp_manager_options_to_ip4_config (iface, device->options);
}
#define NEW_TAG "new_"
#define OLD_TAG "old_"
typedef struct {
GHFunc func;
gpointer user_data;
} Dhcp4ForeachInfo;
static void
copy_dhcp4_config_option (gpointer key,
gpointer value,
gpointer user_data)
iterate_dhcp4_config_option (gpointer key,
gpointer value,
gpointer user_data)
{
NMDHCP4Config *config = NM_DHCP4_CONFIG (user_data);
Dhcp4ForeachInfo *info = (Dhcp4ForeachInfo *) user_data;
char *tmp_key = NULL;
const char **p;
static const char *filter_options[] = {
@@ -947,21 +990,23 @@ copy_dhcp4_config_option (gpointer key,
else
tmp_key = g_strdup ((const char *) key);
nm_dhcp4_config_add_option (config, tmp_key, (const char *) value);
(*info->func) ((gpointer) tmp_key, value, info->user_data);
g_free (tmp_key);
}
gboolean
nm_dhcp_manager_set_dhcp4_config (NMDHCPManager *self,
const char *iface,
NMDHCP4Config *config)
nm_dhcp_manager_foreach_dhcp4_option (NMDHCPManager *self,
const char *iface,
GHFunc func,
gpointer user_data)
{
NMDHCPManagerPrivate *priv;
NMDHCPDevice *device;
Dhcp4ForeachInfo info = { NULL, NULL };
g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), FALSE);
g_return_val_if_fail (iface != NULL, FALSE);
g_return_val_if_fail (config != NULL, FALSE);
g_return_val_if_fail (func != NULL, FALSE);
priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
@@ -976,8 +1021,9 @@ nm_dhcp_manager_set_dhcp4_config (NMDHCPManager *self,
return FALSE;
}
nm_dhcp4_config_reset (config);
g_hash_table_foreach (device->options, copy_dhcp4_config_option, config);
info.func = func;
info.user_data = user_data;
g_hash_table_foreach (device->options, iterate_dhcp4_config_option, &info);
return TRUE;
}

View File

@@ -96,13 +96,21 @@ void nm_dhcp_manager_cancel_transaction (NMDHCPManager *manager,
NMIP4Config * nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, const char *iface);
NMDHCPState nm_dhcp_manager_get_state_for_device (NMDHCPManager *manager, const char *iface);
gboolean nm_dhcp_manager_set_dhcp4_config (NMDHCPManager *manager,
gboolean nm_dhcp_manager_foreach_dhcp4_option (NMDHCPManager *self,
const char *iface,
NMDHCP4Config *config);
gboolean nm_dhcp_manager_process_signal (NMDHCPManager *manager, DBusMessage *message);
GHFunc func,
gpointer user_data);
/* The following are implemented by the DHCP client backends */
gboolean nm_dhcp_client_start (NMDHCPDevice *device, NMSettingIP4Config *s_ip4);
void nm_dhcp_client_stop (const char *iface, pid_t pid);
gboolean nm_dhcp_client_process_classless_routes (GHashTable *options,
NMIP4Config *ip4_config,
guint32 *gwaddr);
/* Test functions */
NMIP4Config *nm_dhcp_manager_options_to_ip4_config (const char *iface,
GHashTable *options);
#endif /* NM_DHCP_MANAGER_H */