From 4f882b2a5007e51032459e29d15a86df6b5ea9f4 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Mon, 13 Jan 2014 23:07:59 +0100 Subject: [PATCH] sd-dhcp-client/networkd: add interface MTU support --- man/systemd-networkd.service.xml | 7 +++ src/libsystemd/dhcp-protocol.h | 1 + src/libsystemd/sd-dhcp-client.c | 47 ++++++++++++++++++-- src/network/networkd-gperf.gperf | 1 + src/network/networkd-link.c | 93 +++++++++++++++++++++++++++++++++++++++- src/network/networkd-network.c | 1 + src/network/networkd.h | 2 + src/systemd/sd-dhcp-client.h | 1 + 8 files changed, 149 insertions(+), 4 deletions(-) diff --git a/man/systemd-networkd.service.xml b/man/systemd-networkd.service.xml index 2c1bbf7..02a211e 100644 --- a/man/systemd-networkd.service.xml +++ b/man/systemd-networkd.service.xml @@ -253,6 +253,13 @@ be used, and take precedence over any statically configured ones. + + UseMTU + + When true (the default) the interface maximum transmission unit + from the DHCP server will be used on the current link. + + diff --git a/src/libsystemd/dhcp-protocol.h b/src/libsystemd/dhcp-protocol.h index 76621f5..abdfe8d 100644 --- a/src/libsystemd/dhcp-protocol.h +++ b/src/libsystemd/dhcp-protocol.h @@ -104,6 +104,7 @@ enum { DHCP_OPTION_DOMAIN_NAME_SERVER = 6, DHCP_OPTION_HOST_NAME = 12, DHCP_OPTION_DOMAIN_NAME = 15, + DHCP_OPTION_INTERFACE_MTU = 26, DHCP_OPTION_NTP_SERVER = 42, DHCP_OPTION_REQUESTED_IP_ADDRESS = 50, DHCP_OPTION_IP_ADDRESS_LEASE_TIME = 51, diff --git a/src/libsystemd/sd-dhcp-client.c b/src/libsystemd/sd-dhcp-client.c index 1df89dc..76abcbd 100644 --- a/src/libsystemd/sd-dhcp-client.c +++ b/src/libsystemd/sd-dhcp-client.c @@ -42,6 +42,7 @@ struct DHCPLease { be32_t subnet_mask; be32_t router; struct in_addr **dns; + uint16_t mtu; }; typedef struct DHCPLease DHCPLease; @@ -186,10 +187,10 @@ int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr) return 0; } -int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr) +int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu) { assert_return(client, -EINVAL); - assert_return(addr, -EINVAL); + assert_return(mtu, -EINVAL); switch (client->state) { case DHCP_STATE_INIT: @@ -202,7 +203,10 @@ int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr) case DHCP_STATE_BOUND: case DHCP_STATE_RENEWING: case DHCP_STATE_REBINDING: - addr->s_addr = client->lease->subnet_mask; + if (client->lease->mtu) + *mtu = client->lease->mtu; + else + return -ENOENT; break; } @@ -277,6 +281,30 @@ int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr) return 0; } +int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr) +{ + assert_return(client, -EINVAL); + assert_return(addr, -EINVAL); + + switch (client->state) { + case DHCP_STATE_INIT: + case DHCP_STATE_SELECTING: + case DHCP_STATE_INIT_REBOOT: + case DHCP_STATE_REBOOTING: + case DHCP_STATE_REQUESTING: + return -EADDRNOTAVAIL; + + case DHCP_STATE_BOUND: + case DHCP_STATE_RENEWING: + case DHCP_STATE_REBINDING: + addr->s_addr = client->lease->subnet_mask; + + break; + } + + return 0; +} + static int client_notify(sd_dhcp_client *client, int event) { if (client->cb) @@ -788,6 +816,19 @@ static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option, break; + case DHCP_OPTION_INTERFACE_MTU: + if (len >= 2) { + be16_t mtu; + + memcpy(&mtu, option, 2); + lease->mtu = be16toh(mtu); + + if (lease->mtu < 68) + lease->mtu = 0; + } + + break; + case DHCP_OPTION_RENEWAL_T1_TIME: if (len == 4) { memcpy(&val, option, 4); diff --git a/src/network/networkd-gperf.gperf b/src/network/networkd-gperf.gperf index 3e20681..d4c7a26 100644 --- a/src/network/networkd-gperf.gperf +++ b/src/network/networkd-gperf.gperf @@ -31,5 +31,6 @@ Address.Label, config_parse_label, 0, 0 Route.Gateway, config_parse_gateway, 0, 0 Route.Destination, config_parse_destination, 0, 0 DHCPv4.UseDNS, config_parse_bool, 0, offsetof(Network, dhcp_dns) +DHCPv4.UseMTU, config_parse_bool, 0, offsetof(Network, dhcp_mtu) Bridge.Description, config_parse_string, 0, offsetof(Bridge, description) Bridge.Name, config_parse_ifname, 0, offsetof(Bridge, name) diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 6186f7f..993931d 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -310,6 +310,56 @@ static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdat return 1; } +static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { + Link *link = userdata; + int r; + + assert(m); + assert(link); + assert(link->ifname); + + if (link->state == LINK_STATE_FAILED) + return 1; + + r = sd_rtnl_message_get_errno(m); + if (r < 0 && r != -EEXIST) + log_warning_link(link, "Could not set MTU: %s", strerror(-r)); + + return 1; +} + +static int link_set_mtu(Link *link, uint32_t mtu) { + _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL; + int r; + + assert(link); + assert(link->manager); + assert(link->manager->rtnl); + + log_debug_link(link, "setting MTU: %" PRIu32, mtu); + + r = sd_rtnl_message_link_new(RTM_SETLINK, link->ifindex, &req); + if (r < 0) { + log_error_link(link, "Could not allocate RTM_SETLINK message"); + return r; + } + + r = sd_rtnl_message_append_u32(req, IFLA_MTU, mtu); + if (r < 0) { + log_error_link(link, "Could not append MTU: %s", strerror(-r)); + return r; + } + + r = sd_rtnl_call_async(link->manager->rtnl, req, set_mtu_handler, link, 0, NULL); + if (r < 0) { + log_error_link(link, + "Could not send rtnetlink message: %s", strerror(-r)); + return r; + } + + return 0; +} + static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { Link *link = userdata; struct in_addr address; @@ -346,6 +396,20 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { route_free(link->dhcp_route); link->dhcp_route = NULL; } + + if (link->network->dhcp_mtu) { + uint16_t mtu; + + r = sd_dhcp_client_get_mtu(client, &mtu); + if (r >= 0 && link->original_mtu != mtu) { + r = link_set_mtu(link, link->original_mtu); + if (r < 0) { + log_warning_link(link, "DHCP error: could not reset MTU"); + link_enter_failed(link); + return; + } + } + } } r = sd_dhcp_client_get_address(client, &address); @@ -431,6 +495,18 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { } } + if (link->network->dhcp_mtu) { + uint16_t mtu; + + r = sd_dhcp_client_get_mtu(client, &mtu); + if (r >= 0) { + r = link_set_mtu(link, mtu); + if (r < 0) + log_error_link(link, "Failed to set MTU " + "to %" PRIu16, mtu); + } + } + link_enter_set_addresses(link); } @@ -466,6 +542,12 @@ static int link_acquire_conf(Link *link) { log_debug_link(link, "acquiring DHCPv4 lease"); + if (link->network->dhcp_mtu) { + r = sd_dhcp_client_set_request_option(link->dhcp, 26); + if (r < 0) + return r; + } + r = sd_dhcp_client_start(link->dhcp); if (r < 0) return r; @@ -727,6 +809,8 @@ int link_configure(Link *link) { int link_update(Link *link, sd_rtnl_message *m) { unsigned flags; + void *data; + uint16_t type; int r; assert(link); @@ -737,9 +821,16 @@ int link_update(Link *link, sd_rtnl_message *m) { r = sd_rtnl_message_link_get_flags(m, &flags); if (r < 0) { - log_warning_link(link, "could not get link flags"); + log_warning_link(link, "Could not get link flags"); return r; } + while (sd_rtnl_message_read(m, &type, &data) > 0) { + if (type == IFLA_MTU && link->network->dhcp_mtu && !link->original_mtu) { + link->original_mtu = *(uint16_t *) data; + log_debug_link(link, "saved original MTU: %" PRIu16, link->original_mtu); + } + } + return link_update_flags(link, flags); } diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index 498dea6..02ffdb1 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -66,6 +66,7 @@ static int network_load_one(Manager *manager, const char *filename) { return log_oom(); network->dhcp_dns = true; + network->dhcp_mtu = true; r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0DHCPv4\0", config_item_perf_lookup, (void*) network_gperf_lookup, false, false, network); diff --git a/src/network/networkd.h b/src/network/networkd.h index 26d7f6b..5bd7e75 100644 --- a/src/network/networkd.h +++ b/src/network/networkd.h @@ -86,6 +86,7 @@ struct Network { Bridge *bridge; bool dhcp; bool dhcp_dns; + bool dhcp_mtu; LIST_HEAD(Address, static_addresses); LIST_HEAD(Route, static_routes); @@ -160,6 +161,7 @@ struct Link { Route *dhcp_route; Address *dhcp_address; Address *dns; + uint16_t original_mtu; LinkState state; diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h index 62af4fa..a148084 100644 --- a/src/systemd/sd-dhcp-client.h +++ b/src/systemd/sd-dhcp-client.h @@ -55,6 +55,7 @@ int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr); int sd_dhcp_client_prefixlen(const struct in_addr *addr); int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr); int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr ***addr); +int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu); int sd_dhcp_client_stop(sd_dhcp_client *client); int sd_dhcp_client_start(sd_dhcp_client *client); -- 2.7.4