chiark / gitweb /
sd-dhcp-client/networkd: add interface MTU support
authorTom Gundersen <teg@jklm.no>
Mon, 13 Jan 2014 22:07:59 +0000 (23:07 +0100)
committerTom Gundersen <teg@jklm.no>
Thu, 16 Jan 2014 00:44:19 +0000 (01:44 +0100)
man/systemd-networkd.service.xml
src/libsystemd/dhcp-protocol.h
src/libsystemd/sd-dhcp-client.c
src/network/networkd-gperf.gperf
src/network/networkd-link.c
src/network/networkd-network.c
src/network/networkd.h
src/systemd/sd-dhcp-client.h

index 2c1bbf7..02a211e 100644 (file)
                                                 be used, and take precedence over any statically configured ones.</para>
                                         </listitem>
                                 </varlistentry>
+                                <varlistentry>
+                                        <term><varname>UseMTU</varname></term>
+                                        <listitem>
+                                                <para>When true (the default) the interface maximum transmission unit
+                                                from the DHCP server will be used on the current link.</para>
+                                        </listitem>
+                                </varlistentry>
                        </variablelist>
 
                 </refsect2>
index 76621f5..abdfe8d 100644 (file)
@@ -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,
index 1df89dc..76abcbd 100644 (file)
@@ -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);
index 3e20681..d4c7a26 100644 (file)
@@ -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)
index 6186f7f..993931d 100644 (file)
@@ -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);
 }
index 498dea6..02ffdb1 100644 (file)
@@ -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);
index 26d7f6b..5bd7e75 100644 (file)
@@ -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;
 
index 62af4fa..a148084 100644 (file)
@@ -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);