mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-25 06:00:08 +01:00
dhcp/nettools: refactor parsing of DHCP lease (mtu)
This commit is contained in:
parent
fc83acbd99
commit
de14a376ff
3 changed files with 51 additions and 40 deletions
|
|
@ -326,27 +326,6 @@ lease_get_in_addr(NDhcp4ClientLease *lease, guint8 option, struct in_addr *addrp
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
lease_get_u16(NDhcp4ClientLease *lease, uint8_t option, uint16_t *u16p)
|
||||
{
|
||||
uint8_t *data;
|
||||
size_t n_data;
|
||||
uint16_t be16;
|
||||
int r;
|
||||
|
||||
r = n_dhcp4_client_lease_query(lease, option, &data, &n_data);
|
||||
if (r)
|
||||
return FALSE;
|
||||
|
||||
if (n_data != sizeof(be16))
|
||||
return FALSE;
|
||||
|
||||
memcpy(&be16, data, sizeof(be16));
|
||||
|
||||
*u16p = ntohs(be16);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
lease_parse_address(NDhcp4ClientLease *lease,
|
||||
NMIP4Config * ip4_config,
|
||||
|
|
@ -710,24 +689,6 @@ lease_parse_routes(NDhcp4ClientLease *lease,
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
lease_parse_mtu(NDhcp4ClientLease *lease, NMIP4Config *ip4_config, GHashTable *options)
|
||||
{
|
||||
uint16_t mtu;
|
||||
|
||||
if (!lease_get_u16(lease, NM_DHCP_OPTION_DHCP4_INTERFACE_MTU, &mtu))
|
||||
return;
|
||||
|
||||
if (mtu < 68)
|
||||
return;
|
||||
|
||||
nm_dhcp_option_add_option_u64(options,
|
||||
_nm_dhcp_option_dhcp4_options,
|
||||
NM_DHCP_OPTION_DHCP4_INTERFACE_MTU,
|
||||
mtu);
|
||||
nm_ip4_config_set_mtu(ip4_config, mtu, NM_IP_CONFIG_SOURCE_DHCP);
|
||||
}
|
||||
|
||||
static void
|
||||
lease_parse_metered(NDhcp4ClientLease *lease, NMIP4Config *ip4_config, GHashTable *options)
|
||||
{
|
||||
|
|
@ -997,6 +958,10 @@ lease_to_ip4_config(NMDedupMultiIndex *multi_idx,
|
|||
{
|
||||
gs_unref_object NMIP4Config *ip4_config = NULL;
|
||||
gs_unref_hashtable GHashTable *options = NULL;
|
||||
guint8 * l_data;
|
||||
gsize l_data_len;
|
||||
guint16 v_u16;
|
||||
int r;
|
||||
|
||||
g_return_val_if_fail(lease != NULL, NULL);
|
||||
|
||||
|
|
@ -1012,7 +977,16 @@ lease_to_ip4_config(NMDedupMultiIndex *multi_idx,
|
|||
lease_parse_address_list(lease, ip4_config, NM_DHCP_OPTION_DHCP4_DOMAIN_NAME_SERVER, options);
|
||||
lease_parse_domainname(lease, ip4_config, options);
|
||||
lease_parse_search_domains(lease, ip4_config, options);
|
||||
lease_parse_mtu(lease, ip4_config, options);
|
||||
|
||||
r = n_dhcp4_client_lease_query(lease, NM_DHCP_OPTION_DHCP4_INTERFACE_MTU, &l_data, &l_data_len);
|
||||
if (r == 0 && nm_dhcp_lease_data_parse_mtu(l_data, l_data_len, &v_u16)) {
|
||||
nm_dhcp_option_add_option_u64(options,
|
||||
_nm_dhcp_option_dhcp4_options,
|
||||
NM_DHCP_OPTION_DHCP4_INTERFACE_MTU,
|
||||
v_u16);
|
||||
nm_ip4_config_set_mtu(ip4_config, v_u16, NM_IP_CONFIG_SOURCE_DHCP);
|
||||
}
|
||||
|
||||
lease_parse_metered(lease, ip4_config, options);
|
||||
|
||||
lease_parse_hostname(lease, options);
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#include <arpa/inet.h>
|
||||
|
||||
#include "nm-glib-aux/nm-dedup-multi.h"
|
||||
#include "nm-std-aux/unaligned.h"
|
||||
|
||||
#include "nm-dhcp-utils.h"
|
||||
#include "nm-utils.h"
|
||||
|
|
@ -834,3 +835,34 @@ nm_dhcp_utils_get_dhcp6_event_id(GHashTable *lease)
|
|||
|
||||
return g_strdup_printf("%s|%s", iaid, start);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
gboolean
|
||||
nm_dhcp_lease_data_parse_u16(const guint8 *data, gsize n_data, uint16_t *out_val)
|
||||
{
|
||||
if (n_data != 2)
|
||||
return FALSE;
|
||||
|
||||
*out_val = unaligned_read_be16(data);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
nm_dhcp_lease_data_parse_mtu(const guint8 *data, gsize n_data, uint16_t *out_val)
|
||||
{
|
||||
uint16_t mtu;
|
||||
|
||||
if (!nm_dhcp_lease_data_parse_u16(data, n_data, &mtu))
|
||||
return FALSE;
|
||||
|
||||
if (mtu < 68) {
|
||||
/* https://tools.ietf.org/html/rfc2132#section-5.1:
|
||||
*
|
||||
* The minimum legal value for the MTU is 68. */
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
*out_val = mtu;
|
||||
return TRUE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,4 +40,9 @@ char **nm_dhcp_parse_search_list(guint8 *data, size_t n_data);
|
|||
|
||||
char *nm_dhcp_utils_get_dhcp6_event_id(GHashTable *lease);
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
gboolean nm_dhcp_lease_data_parse_u16(const guint8 *data, gsize n_data, guint16 *out_val);
|
||||
gboolean nm_dhcp_lease_data_parse_mtu(const guint8 *data, gsize n_data, guint16 *out_val);
|
||||
|
||||
#endif /* __NETWORKMANAGER_DHCP_UTILS_H__ */
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue