[systemd-devel] [PATCH resend] Add support for DHCP static route options in networkd
Eugene Yakubovich
eyakubovich at gmail.com
Wed Jun 25 22:39:11 PDT 2014
This adds support for DHCP options 33 and 121: Static Route and
Classless Static Route. To enable this feature, set UseRoutes=true
in .network file. Returned routes are added to the routing table.
---
man/systemd.network.xml | 7 ++
src/libsystemd-network/dhcp-lease-internal.h | 9 +-
src/libsystemd-network/dhcp-protocol.h | 1 +
src/libsystemd-network/network-internal.c | 88 ++++++++++++++++
src/libsystemd-network/network-internal.h | 7 ++
src/libsystemd-network/sd-dhcp-lease.c | 152 ++++++++++++++++++++++++++-
src/network/networkd-link.c | 83 ++++++++++++++-
src/network/networkd-network-gperf.gperf | 1 +
src/network/networkd.h | 1 +
src/network/test-network.c | 23 ++++
src/systemd/sd-dhcp-lease.h | 3 +-
11 files changed, 369 insertions(+), 6 deletions(-)
diff --git a/man/systemd.network.xml b/man/systemd.network.xml
index efe6ce7..2105cb9 100644
--- a/man/systemd.network.xml
+++ b/man/systemd.network.xml
@@ -329,6 +329,13 @@
</listitem>
</varlistentry>
<varlistentry>
+ <term><varname>UseRoutes=</varname></term>
+ <listitem>
+ <para>When true, the static routes received from the DHCP server
+ will be added to the routing table.</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
<term><varname>CriticalConnection=</varname></term>
<listitem>
<para>When true, the connection will never be torn down even if the DHCP lease
diff --git a/src/libsystemd-network/dhcp-lease-internal.h b/src/libsystemd-network/dhcp-lease-internal.h
index ff09583..d4675f3 100644
--- a/src/libsystemd-network/dhcp-lease-internal.h
+++ b/src/libsystemd-network/dhcp-lease-internal.h
@@ -32,6 +32,12 @@
#include "sd-dhcp-client.h"
+struct sd_dhcp_route {
+ struct in_addr dst_addr;
+ struct in_addr gw_addr;
+ uint8_t dst_prefixlen;
+};
+
struct sd_dhcp_lease {
RefCount n_ref;
@@ -52,8 +58,9 @@ struct sd_dhcp_lease {
size_t ntp_size;
struct in_addr *policy_filter;
size_t policy_filter_size;
- struct in_addr *static_route;
+ struct sd_dhcp_route *static_route;
size_t static_route_size;
+ size_t static_route_allocated;
uint16_t boot_file_size;
uint16_t mdr;
uint16_t mtu;
diff --git a/src/libsystemd-network/dhcp-protocol.h b/src/libsystemd-network/dhcp-protocol.h
index 4d87891..8cbd98e 100644
--- a/src/libsystemd-network/dhcp-protocol.h
+++ b/src/libsystemd-network/dhcp-protocol.h
@@ -132,5 +132,6 @@ enum {
DHCP_OPTION_RENEWAL_T1_TIME = 58,
DHCP_OPTION_REBINDING_T2_TIME = 59,
DHCP_OPTION_CLIENT_IDENTIFIER = 61,
+ DHCP_OPTION_CLASSLESS_STATIC_ROUTE = 121,
DHCP_OPTION_END = 255,
};
diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c
index 97217c1..663ed7f 100644
--- a/src/libsystemd-network/network-internal.c
+++ b/src/libsystemd-network/network-internal.c
@@ -28,6 +28,7 @@
#include "siphash24.h"
#include "libudev-private.h"
#include "network-internal.h"
+#include "dhcp-lease-internal.h"
#include "log.h"
#include "utf8.h"
#include "util.h"
@@ -432,3 +433,90 @@ int deserialize_in6_addrs(struct in6_addr **ret, size_t *ret_size, const char *s
return 0;
}
+
+void serialize_dhcp_routes(FILE *f, const char *key, struct sd_dhcp_route *routes, size_t size) {
+ unsigned i;
+
+ assert(f);
+ assert(key);
+ assert(routes);
+ assert(size);
+
+ fprintf(f, "%s=", key);
+
+ for (i = 0; i < size; i++)
+ fprintf(f, "%s/%" PRIu8 ",%s%s", inet_ntoa(routes[i].dst_addr),
+ routes[i].dst_prefixlen, inet_ntoa(routes[i].gw_addr),
+ (i < (size - 1)) ? " ": "");
+
+ fputs("\n", f);
+}
+
+int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, const char *string) {
+ _cleanup_free_ struct sd_dhcp_route *routes = NULL;
+ size_t size = 0;
+ char *word, *state;
+ size_t len;
+
+ assert(ret);
+ assert(ret_size);
+ assert(string);
+
+ FOREACH_WORD(word, len, string, state) {
+ /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
+ _cleanup_free_ char* entry;
+ char *tok, *tok_end;
+ struct sd_dhcp_route *new_routes;
+ unsigned n;
+ int r;
+
+ new_routes = realloc(routes, (size + 1) * sizeof(struct sd_dhcp_route));
+ if (!new_routes)
+ return -ENOMEM;
+ else
+ routes = new_routes;
+
+ entry = strndup(word, len);
+
+ tok = entry;
+
+ /* get the subnet */
+ tok_end = strchr(tok, '/');
+ if (!tok_end)
+ continue;
+ *tok_end = '\0';
+
+ r = inet_aton(tok, &new_routes[size].dst_addr);
+ if (r == 0)
+ continue;
+
+ tok = tok_end + 1;
+
+ /* get the prefixlen */
+ tok_end = strchr(tok, ',');
+ if (!tok_end)
+ continue;
+
+ *tok_end = '\0';
+
+ r = safe_atou(tok, &n);
+ if (r < 0 || n > 32)
+ continue;
+
+ new_routes[size].dst_prefixlen = (uint8_t) n;
+ tok = tok_end + 1;
+
+ /* get the gateway */
+ r = inet_aton(tok, &new_routes[size].gw_addr);
+ if (r == 0)
+ continue;
+
+ size++;
+ }
+
+ *ret_size = size;
+ *ret = routes;
+ routes = NULL;
+
+ return 0;
+}
diff --git a/src/libsystemd-network/network-internal.h b/src/libsystemd-network/network-internal.h
index db48c2c..2387d8f 100644
--- a/src/libsystemd-network/network-internal.h
+++ b/src/libsystemd-network/network-internal.h
@@ -70,3 +70,10 @@ const char *net_get_name(struct udev_device *device);
void serialize_in_addrs(FILE *f, const char *key, struct in_addr *addresses, size_t size);
int deserialize_in_addrs(struct in_addr **addresses, size_t *size, const char *string);
int deserialize_in6_addrs(struct in6_addr **addresses, size_t *size, const char *string);
+
+
+/* don't include "dhcp-lease-internal.h" as it causes conflicts between netinet/ip.h and linux/ip.h */
+struct sd_dhcp_route;
+
+void serialize_dhcp_routes(FILE *f, const char *key, struct sd_dhcp_route *routes, size_t size);
+int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, const char *string);
diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c
index 3203b7a..8a57391 100644
--- a/src/libsystemd-network/sd-dhcp-lease.c
+++ b/src/libsystemd-network/sd-dhcp-lease.c
@@ -162,6 +162,23 @@ int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr) {
return 0;
}
+int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routes,
+ size_t *routes_size) {
+
+ assert_return(lease, -EINVAL);
+ assert_return(routes, -EINVAL);
+ assert_return(routes_size, -EINVAL);
+
+ if (lease->static_route_size) {
+ *routes = lease->static_route;
+ *routes_size = lease->static_route_size;
+ }
+ else
+ return -ENOENT;
+
+ return 0;
+}
+
sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) {
if (lease)
assert_se(REFCNT_INC(lease->n_ref) >= 2);
@@ -175,6 +192,7 @@ sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
free(lease->domainname);
free(lease->dns);
free(lease->ntp);
+ free(lease->static_route);
free(lease);
}
@@ -261,6 +279,16 @@ static int lease_parse_string(const uint8_t *option, size_t len, char **ret) {
return 0;
}
+static void lease_parse_compact_in_addr(const uint8_t *option, size_t len, uint8_t dst_octets, be32_t *ret) {
+ assert(option);
+ assert(ret);
+
+ if (dst_octets <= 4 && dst_octets <= len) {
+ *ret = 0;
+ memcpy(ret, option, dst_octets);
+ }
+}
+
static int lease_parse_in_addrs_aux(const uint8_t *option, size_t len, struct in_addr **ret, size_t *ret_size, size_t mult) {
assert(option);
assert(ret);
@@ -292,6 +320,104 @@ static int lease_parse_in_addrs_pairs(const uint8_t *option, size_t len, struct
return lease_parse_in_addrs_aux(option, len, ret, ret_size, 2);
}
+static int class_prefixlen(uint8_t msb_octet, uint8_t *ret) {
+ if (msb_octet < 128)
+ *ret = 8; // Class A
+ else if (msb_octet < 192)
+ *ret = 16; // Class B
+ else if (msb_octet < 224)
+ *ret = 24; // Class C
+ else
+ return -ERANGE; // Class D or E -- no subnet mask
+
+ return 0;
+}
+
+static int lease_parse_routes(const uint8_t *option, size_t len, struct sd_dhcp_route **routes,
+ size_t *routes_size, size_t *routes_allocated) {
+
+ struct in_addr addr;
+
+ assert(option);
+ assert(routes);
+ assert(routes_size);
+ assert(routes_allocated);
+
+ if (!len)
+ return 0;
+
+ if (len % 8 != 0)
+ return -EINVAL;
+
+ if (!GREEDY_REALLOC(*routes, *routes_allocated, *routes_size + (len / 8)))
+ return -ENOMEM;
+
+ while (len >= 8) {
+ struct sd_dhcp_route *route = *routes + *routes_size;
+
+ if (class_prefixlen(*option, &route->dst_prefixlen) < 0) {
+ log_error("Failed to determine destination prefix length from class based IP, ignoring");
+ continue;
+ }
+
+ lease_parse_be32(option, 4, &addr.s_addr);
+ route->dst_addr = inet_makeaddr(inet_netof(addr), 0);
+ option += 4;
+
+ lease_parse_be32(option, 4, &route->gw_addr.s_addr);
+ option += 4;
+
+ len -= 8;
+ (*routes_size)++;
+ }
+
+ return 0;
+}
+
+// parses RFC3442 Classless Static Route Option
+// option format: (subnet-mask-width significant-subnet-octets gateway-ip)*
+static int lease_parse_classless_routes(const uint8_t *option, size_t len, struct sd_dhcp_route **routes,
+ size_t *routes_size, size_t *routes_allocated) {
+
+ assert(option);
+ assert(routes);
+ assert(routes_size);
+ assert(routes_allocated);
+
+ while (len > 0) {
+ uint8_t dst_octets;
+ struct sd_dhcp_route *route;
+
+ if (!GREEDY_REALLOC(*routes, *routes_allocated, *routes_size + 1))
+ return -ENOMEM;
+
+ route = *routes + *routes_size;
+
+ dst_octets = (*option == 0 ? 0 : ((*option - 1) / 8) + 1);
+ route->dst_prefixlen = *option;
+ option++;
+ len--;
+
+ if (len < dst_octets)
+ return -EINVAL;
+
+ lease_parse_compact_in_addr(option, len, dst_octets, &route->dst_addr.s_addr);
+ option += dst_octets;
+ len -= dst_octets;
+
+ if (len < 4)
+ return -EINVAL;
+
+ lease_parse_be32(option, len, &route->gw_addr.s_addr);
+ option += 4;
+ len -= 4;
+
+ (*routes_size)++;
+ }
+
+ return 0;
+}
+
int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
void *user_data) {
sd_dhcp_lease *lease = user_data;
@@ -358,7 +484,8 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
break;
case DHCP_OPTION_STATIC_ROUTE:
- r = lease_parse_in_addrs_pairs(option, len, &lease->static_route, &lease->static_route_size);
+ r = lease_parse_routes(option, len, &lease->static_route, &lease->static_route_size,
+ &lease->static_route_allocated);
if (r < 0)
return r;
@@ -424,6 +551,14 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
lease_parse_bool(option, len, &lease->ip_forward_non_local);
break;
+
+ case DHCP_OPTION_CLASSLESS_STATIC_ROUTE:
+ r = lease_parse_classless_routes(option, len, &lease->static_route, &lease->static_route_size,
+ &lease->static_route_allocated);
+ if (r < 0)
+ return r;
+
+ break;
}
return 0;
@@ -451,6 +586,8 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
size_t addresses_size;
const char *string;
uint16_t mtu;
+ struct sd_dhcp_route *routes;
+ size_t routes_size;
int r;
assert(lease);
@@ -513,6 +650,10 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
if (r >= 0)
fprintf(f, "ROOT_PATH=%s\n", string);
+ r = sd_dhcp_lease_get_routes(lease, &routes, &routes_size);
+ if (r >= 0)
+ serialize_dhcp_routes(f, "ROUTES", routes, routes_size);
+
r = 0;
fflush(f);
@@ -534,7 +675,7 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
_cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
_cleanup_free_ char *address = NULL, *router = NULL, *netmask = NULL,
*server_address = NULL, *next_server = NULL,
- *dns = NULL, *ntp = NULL, *mtu = NULL;
+ *dns = NULL, *ntp = NULL, *mtu = NULL, *routes = NULL;
struct in_addr addr;
int r;
@@ -557,6 +698,7 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
"DOMAINNAME", &lease->domainname,
"HOSTNAME", &lease->hostname,
"ROOT_PATH", &lease->root_path,
+ "ROUTES", &routes,
NULL);
if (r < 0) {
if (r == -ENOENT)
@@ -620,6 +762,12 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
lease->mtu = u;
}
+ if (routes) {
+ r = deserialize_dhcp_routes(&lease->static_route, &lease->static_route_size, routes);
+ if (r < 0)
+ return r;
+ }
+
*ret = lease;
lease = NULL;
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 44147b2..806b0d4 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -392,6 +392,50 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
return 1;
}
+static int link_set_dhcp_routes(Link *link) {
+ struct sd_dhcp_route *static_routes;
+ size_t static_routes_size;
+ int r;
+ unsigned i;
+
+ assert(link);
+
+ r = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes, &static_routes_size);
+ if (r < 0) {
+ if (r != -ENOENT)
+ log_warning_link(link, "DHCP error: %s", strerror(-r));
+ return r;
+ }
+
+ for (i = 0; i < static_routes_size; i++) {
+ _cleanup_route_free_ Route *route = NULL;
+
+ r = route_new_dynamic(&route);
+ if (r < 0) {
+ log_error_link(link, "Could not allocate route: %s",
+ strerror(-r));
+ return r;
+ }
+
+ route->family = AF_INET;
+ route->in_addr.in = static_routes[i].gw_addr;
+ route->dst_addr.in = static_routes[i].dst_addr;
+ route->dst_prefixlen = static_routes[i].dst_prefixlen;
+
+ r = route_configure(route, link, &route_handler);
+ if (r < 0) {
+ log_warning_link(link,
+ "could not set host route: %s", strerror(-r));
+ return r;
+ }
+
+ link_ref(link);
+ link->route_messages ++;
+ }
+
+ return 0;
+}
+
static int link_enter_set_routes(Link *link) {
Route *rt;
int r;
@@ -515,6 +559,9 @@ static int link_enter_set_routes(Link *link) {
link_ref(link);
link->route_messages ++;
}
+
+ if (link->network->dhcp_routes)
+ link_set_dhcp_routes(link);
}
if (link->route_messages == 0) {
@@ -878,12 +925,13 @@ static int link_set_mtu(Link *link, uint32_t mtu) {
static int dhcp_lease_lost(Link *link) {
_cleanup_address_free_ Address *address = NULL;
- _cleanup_route_free_ Route *route_gw = NULL;
- _cleanup_route_free_ Route *route = NULL;
struct in_addr addr;
struct in_addr netmask;
struct in_addr gateway;
unsigned prefixlen;
+ struct sd_dhcp_route *static_routes;
+ size_t static_routes_size;
+ unsigned i;
int r;
assert(link);
@@ -891,10 +939,33 @@ static int dhcp_lease_lost(Link *link) {
log_warning_link(link, "DHCP lease lost");
+ if (link->network->dhcp_routes) {
+ r = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes, &static_routes_size);
+ if (r >= 0) {
+ for (i = 0; i < static_routes_size; i++) {
+ _cleanup_route_free_ Route *route = NULL;
+
+ r = route_new_dynamic(&route);
+ if (r >= 0) {
+ route->family = AF_INET;
+ route->in_addr.in = static_routes[i].gw_addr;
+ route->dst_addr.in = static_routes[i].dst_addr;
+ route->dst_prefixlen = static_routes[i].dst_prefixlen;
+
+ route_drop(route, link, &route_drop_handler);
+ link_ref(link);
+ }
+ }
+ }
+ }
+
r = address_new_dynamic(&address);
if (r >= 0) {
r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
if (r >= 0) {
+ _cleanup_route_free_ Route *route_gw = NULL;
+ _cleanup_route_free_ Route *route = NULL;
+
r = route_new_dynamic(&route_gw);
if (r >= 0) {
route_gw->family = AF_INET;
@@ -1903,6 +1974,14 @@ static int link_configure(Link *link) {
if (r < 0)
return r;
}
+ if (link->network->dhcp_routes) {
+ r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_STATIC_ROUTE);
+ if (r < 0)
+ return r;
+ r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
+ if (r < 0)
+ return r;
+ }
}
if (link->network->dhcp_server) {
diff --git a/src/network/networkd-network-gperf.gperf b/src/network/networkd-network-gperf.gperf
index 469e028..a5ce7c0 100644
--- a/src/network/networkd-network-gperf.gperf
+++ b/src/network/networkd-network-gperf.gperf
@@ -48,4 +48,5 @@ DHCPv4.UseDNS, config_parse_bool, 0,
DHCPv4.UseMTU, config_parse_bool, 0, offsetof(Network, dhcp_mtu)
DHCPv4.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp_hostname)
DHCPv4.UseDomainName, config_parse_bool, 0, offsetof(Network, dhcp_domainname)
+DHCPv4.UseRoutes, config_parse_bool, 0, offsetof(Network, dhcp_routes)
DHCPv4.CriticalConnection, config_parse_bool, 0, offsetof(Network, dhcp_critical)
diff --git a/src/network/networkd.h b/src/network/networkd.h
index b7b1d90..aed60fb 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -160,6 +160,7 @@ struct Network {
bool dhcp_hostname;
bool dhcp_domainname;
bool dhcp_critical;
+ bool dhcp_routes;
bool ipv4ll;
bool dhcp6;
diff --git a/src/network/test-network.c b/src/network/test-network.c
index eb6ebe7..0905047 100644
--- a/src/network/test-network.c
+++ b/src/network/test-network.c
@@ -21,6 +21,7 @@
#include "networkd.h"
#include "network-internal.h"
+#include "dhcp-lease-internal.h"
static void test_deserialize_in_addr(void) {
_cleanup_free_ struct in_addr *addresses = NULL;
@@ -53,6 +54,27 @@ static void test_deserialize_in_addr(void) {
assert_se(!memcmp(&f, &addresses6[2], sizeof(struct in6_addr)));
}
+static void test_deserialize_dhcp_routes(void) {
+ _cleanup_free_ struct sd_dhcp_route *routes;
+ size_t size;
+ const char *routes_string = "192.168.0.0/16,192.168.0.1 10.1.2.0/24,10.1.2.1 0.0.0.0/0,10.0.1.1";
+
+ assert_se(deserialize_dhcp_routes(&routes, &size, routes_string) >= 0);
+
+ assert_se(size == 3);
+ assert_se(routes[0].dst_addr.s_addr == inet_addr("192.168.0.0"));
+ assert_se(routes[0].gw_addr.s_addr == inet_addr("192.168.0.1"));
+ assert_se(routes[0].dst_prefixlen == 16);
+
+ assert_se(routes[1].dst_addr.s_addr == inet_addr("10.1.2.0"));
+ assert_se(routes[1].gw_addr.s_addr == inet_addr("10.1.2.1"));
+ assert_se(routes[1].dst_prefixlen == 24);
+
+ assert_se(routes[2].dst_addr.s_addr == inet_addr("0.0.0.0"));
+ assert_se(routes[2].gw_addr.s_addr == inet_addr("10.0.1.1"));
+ assert_se(routes[2].dst_prefixlen == 0);
+}
+
static void test_load_config(Manager *manager) {
/* TODO: should_reload, is false if the config dirs do not exist, so
* so we can't do this test here, move it to a test for paths_check_timestamps
@@ -125,6 +147,7 @@ int main(void) {
struct udev_device *loopback;
test_deserialize_in_addr();
+ test_deserialize_dhcp_routes();
test_address_equality();
assert_se(manager_new(&manager) >= 0);
diff --git a/src/systemd/sd-dhcp-lease.h b/src/systemd/sd-dhcp-lease.h
index 252c093..c05c536 100644
--- a/src/systemd/sd-dhcp-lease.h
+++ b/src/systemd/sd-dhcp-lease.h
@@ -27,6 +27,7 @@
#include <net/ethernet.h>
typedef struct sd_dhcp_lease sd_dhcp_lease;
+struct sd_dhcp_route;
sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease);
sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease);
@@ -41,5 +42,5 @@ int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu);
int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname);
int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname);
int sd_dhcp_lease_get_root_path(sd_dhcp_lease *lease, const char **root_path);
-
+int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routes, size_t *routes_size);
#endif
--
1.9.1
More information about the systemd-devel
mailing list