diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libsystemd-network/dhcp-lease-internal.h | 9 | ||||
-rw-r--r-- | src/libsystemd-network/dhcp-protocol.h | 1 | ||||
-rw-r--r-- | src/libsystemd-network/network-internal.c | 86 | ||||
-rw-r--r-- | src/libsystemd-network/network-internal.h | 7 | ||||
-rw-r--r-- | src/libsystemd-network/sd-dhcp-lease.c | 149 | ||||
-rw-r--r-- | src/network/networkd-link.c | 85 | ||||
-rw-r--r-- | src/network/networkd-network-gperf.gperf | 1 | ||||
-rw-r--r-- | src/network/networkd-network.c | 1 | ||||
-rw-r--r-- | src/network/networkd.h | 2 | ||||
-rw-r--r-- | src/network/test-network.c | 59 | ||||
-rw-r--r-- | src/systemd/sd-dhcp-lease.h | 3 |
11 files changed, 397 insertions, 6 deletions
diff --git a/src/libsystemd-network/dhcp-lease-internal.h b/src/libsystemd-network/dhcp-lease-internal.h index ff09583604..d4675f3e47 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 4d87891ff1..8cbd98e4ec 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 97217c12e5..a5db0c5353 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,88 @@ 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, size_t *ret_allocated, const char *string) { + _cleanup_free_ struct sd_dhcp_route *routes = NULL; + size_t size = 0, allocated = 0; + char *word, *state; + size_t len; + + assert(ret); + assert(ret_size); + assert(ret_allocated); + assert(string); + + FOREACH_WORD(word, len, string, state) { + /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */ + _cleanup_free_ char* entry; + char *tok, *tok_end; + unsigned n; + int r; + + if (!GREEDY_REALLOC(routes, allocated, size + 1)) + return -ENOMEM; + + entry = strndup(word, len); + + tok = entry; + + /* get the subnet */ + tok_end = strchr(tok, '/'); + if (!tok_end) + continue; + *tok_end = '\0'; + + r = inet_aton(tok, &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; + + routes[size].dst_prefixlen = (uint8_t) n; + tok = tok_end + 1; + + /* get the gateway */ + r = inet_aton(tok, &routes[size].gw_addr); + if (r == 0) + continue; + + size++; + } + + *ret_size = size; + *ret_allocated = allocated; + *ret = routes; + routes = NULL; + + return 0; +} diff --git a/src/libsystemd-network/network-internal.h b/src/libsystemd-network/network-internal.h index db48c2c06c..4bde14641e 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, size_t *ret_allocated, const char *string); diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index 94ba283cf0..20a95f6de6 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -171,6 +171,22 @@ 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); @@ -184,6 +200,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); } @@ -301,6 +318,111 @@ 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) + /* Class A */ + *ret = 8; + else if (msb_octet < 192) + /* Class B */ + *ret = 16; + else if (msb_octet < 224) + /* Class C */ + *ret = 24; + else + /* Class D or E -- no subnet mask */ + return -ERANGE; + + 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 */ +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); + + /* option format: (subnet-mask-width significant-subnet-octets gateway-ip)* */ + + 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--; + + /* can't have more than 4 octets in IPv4 */ + if (dst_octets > 4 || len < dst_octets) + return -EINVAL; + + route->dst_addr.s_addr = 0; + memcpy(&route->dst_addr.s_addr, option, dst_octets); + option += dst_octets; + len -= dst_octets; + + if (len < 4) + return -EINVAL; + + lease_parse_be32(option, 4, &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; @@ -367,7 +489,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; @@ -433,6 +556,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; @@ -460,6 +591,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); @@ -522,6 +655,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); @@ -543,7 +680,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; @@ -566,6 +703,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) @@ -629,6 +767,13 @@ 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, + &lease->static_route_allocated, 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 5d280b5b82..82e517e78c 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -402,6 +402,51 @@ 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; + route->metrics = DHCP_STATIC_ROUTE_METRIC; + + 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; @@ -525,6 +570,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) { @@ -901,12 +949,11 @@ 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; + unsigned i; int r; assert(link); @@ -914,10 +961,36 @@ static int dhcp_lease_lost(Link *link) { log_warning_link(link, "DHCP lease lost"); + if (link->network->dhcp_routes) { + struct sd_dhcp_route *routes; + size_t routes_size; + + r = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes, &routes_size); + if (r >= 0) { + for (i = 0; i < 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 = routes[i].gw_addr; + route->dst_addr.in = routes[i].dst_addr; + route->dst_prefixlen = 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; @@ -1952,6 +2025,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 c8610d14f4..b6d52e7335 100644 --- a/src/network/networkd-network-gperf.gperf +++ b/src/network/networkd-network-gperf.gperf @@ -53,4 +53,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-network.c b/src/network/networkd-network.c index 8e7cdf670b..f60f7c812c 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -89,6 +89,7 @@ static int network_load_one(Manager *manager, const char *filename) { network->dhcp_dns = true; network->dhcp_hostname = true; network->dhcp_domainname = true; + network->dhcp_routes = true; r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0DHCPv4\0", config_item_perf_lookup, (void*) network_network_gperf_lookup, false, false, network); diff --git a/src/network/networkd.h b/src/network/networkd.h index 67f70d9e60..11268de70d 100644 --- a/src/network/networkd.h +++ b/src/network/networkd.h @@ -42,6 +42,7 @@ #define CACHE_INFO_INFINITY_LIFE_TIME 0xFFFFFFFFU #define VXLAN_VID_MAX (1u << 24) - 1 +#define DHCP_STATIC_ROUTE_METRIC 1024 typedef struct NetDev NetDev; typedef struct Network Network; @@ -169,6 +170,7 @@ struct Network { bool dhcp_hostname; bool dhcp_domainname; bool dhcp_critical; + bool dhcp_routes; bool ipv4ll; bool dhcp_server; diff --git a/src/network/test-network.c b/src/network/test-network.c index eb6ebe70b3..75e70fae14 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,63 @@ static void test_deserialize_in_addr(void) { assert_se(!memcmp(&f, &addresses6[2], sizeof(struct in6_addr))); } +static void test_deserialize_dhcp_routes(void) { + size_t size, allocated; + + { + _cleanup_free_ struct sd_dhcp_route *routes = NULL; + assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, "") >= 0); + assert_se(size == 0); + } + + { + /* no errors */ + _cleanup_free_ struct sd_dhcp_route *routes = NULL; + 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, &allocated, 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); + } + + { + /* error in second word */ + _cleanup_free_ struct sd_dhcp_route *routes = NULL; + 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, &allocated, routes_string) >= 0); + + assert_se(size == 2); + 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[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); + } + + { + /* error in every word */ + _cleanup_free_ struct sd_dhcp_route *routes = NULL; + const char *routes_string = "192.168.0.0/55,192.168.0.1 10.1.2.0#24,10.1.2.1 0.0.0.0/0,10.0.1.X"; + + assert_se(deserialize_dhcp_routes(&routes, &size, &allocated, routes_string) >= 0); + assert_se(size == 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 +183,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 4e3a054406..c15744d6c1 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); @@ -42,5 +43,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 |