mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-04-28 05:20:40 +02:00
core: move IP config matching to NMDevice superclass
It's useful for more than just NMDeviceWired and subclasses.
This commit is contained in:
parent
57b77b10b6
commit
29672c3f7d
3 changed files with 93 additions and 80 deletions
|
|
@ -65,4 +65,6 @@ void nm_device_set_dhcp_anycast_address (NMDevice *device, guint8 *addr);
|
|||
|
||||
gboolean nm_device_dhcp4_renew (NMDevice *device, gboolean release);
|
||||
|
||||
gboolean nm_device_match_ip_config (NMDevice *device, NMConnection *connection);
|
||||
|
||||
#endif /* NM_DEVICE_PRIVATE_H */
|
||||
|
|
|
|||
|
|
@ -416,85 +416,6 @@ real_is_available (NMDevice *dev)
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
ip4_match_config (NMDevice *self, NMConnection *connection)
|
||||
{
|
||||
NMSettingIP4Config *s_ip4;
|
||||
int i, num;
|
||||
GSList *leases, *iter;
|
||||
NMDHCPManager *dhcp_mgr;
|
||||
const char *method;
|
||||
|
||||
s_ip4 = nm_connection_get_setting_ip4_config (connection);
|
||||
if (!s_ip4)
|
||||
return FALSE;
|
||||
|
||||
/* Get any saved leases that apply to this connection */
|
||||
dhcp_mgr = nm_dhcp_manager_get ();
|
||||
leases = nm_dhcp_manager_get_lease_config (dhcp_mgr,
|
||||
nm_device_get_iface (self),
|
||||
nm_connection_get_uuid (connection));
|
||||
g_object_unref (dhcp_mgr);
|
||||
|
||||
method = nm_setting_ip4_config_get_method (s_ip4);
|
||||
if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) {
|
||||
gboolean found = FALSE;
|
||||
|
||||
/* Find at least one lease's address on the device */
|
||||
for (iter = leases; iter; iter = g_slist_next (iter)) {
|
||||
NMIP4Config *ip4_config = iter->data;
|
||||
NMIP4Address *addr = nm_ip4_config_get_address (ip4_config, 0);
|
||||
struct in_addr tmp = { .s_addr = nm_ip4_address_get_address (addr) };
|
||||
|
||||
if (addr && nm_netlink_find_address (nm_device_get_ip_ifindex (self),
|
||||
AF_INET,
|
||||
&tmp,
|
||||
nm_ip4_address_get_prefix (addr))) {
|
||||
found = TRUE; /* Yay, device has same address as a lease */
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_slist_foreach (leases, (GFunc) g_object_unref, NULL);
|
||||
g_slist_free (leases);
|
||||
return found;
|
||||
} else {
|
||||
/* Maybe the connection used to be DHCP and there are stale leases; ignore them */
|
||||
g_slist_foreach (leases, (GFunc) g_object_unref, NULL);
|
||||
g_slist_free (leases);
|
||||
}
|
||||
|
||||
if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) {
|
||||
// FIXME: Enforce no ipv4 addresses?
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* 'shared' and 'link-local' aren't supported methods because 'shared'
|
||||
* requires too much iptables and dnsmasq state to be reclaimed, and
|
||||
* avahi-autoipd isn't smart enough to allow the link-local address to be
|
||||
* determined at any point other than when it was first assigned.
|
||||
*/
|
||||
if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL))
|
||||
return FALSE;
|
||||
|
||||
/* Everything below for static addressing */
|
||||
|
||||
/* Find all IP4 addresses of this connection on the device */
|
||||
num = nm_setting_ip4_config_get_num_addresses (s_ip4);
|
||||
for (i = 0; i < num; i++) {
|
||||
NMIP4Address *addr = nm_setting_ip4_config_get_address (s_ip4, i);
|
||||
struct in_addr tmp = { .s_addr = nm_ip4_address_get_address (addr) };
|
||||
|
||||
if (!nm_netlink_find_address (nm_device_get_ip_ifindex (self),
|
||||
AF_INET,
|
||||
&tmp,
|
||||
nm_ip4_address_get_prefix (addr)))
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Success; all the connection's static IP addresses are assigned to the device */
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static NMConnection *
|
||||
connection_match_config (NMDevice *self, const GSList *connections)
|
||||
{
|
||||
|
|
@ -503,7 +424,7 @@ connection_match_config (NMDevice *self, const GSList *connections)
|
|||
for (iter = connections; iter; iter = iter->next) {
|
||||
NMConnection *candidate = NM_CONNECTION (iter->data);
|
||||
|
||||
if (!ip4_match_config (self, candidate))
|
||||
if (!nm_device_match_ip_config (self, candidate))
|
||||
continue;
|
||||
|
||||
return candidate;
|
||||
|
|
|
|||
|
|
@ -4421,6 +4421,96 @@ nm_device_spec_match_list (NMDevice *device, const GSList *specs)
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
ip4_match_config (NMDevice *self, NMConnection *connection)
|
||||
{
|
||||
NMSettingIP4Config *s_ip4;
|
||||
int i, num;
|
||||
GSList *leases, *iter;
|
||||
NMDHCPManager *dhcp_mgr;
|
||||
const char *method;
|
||||
|
||||
s_ip4 = nm_connection_get_setting_ip4_config (connection);
|
||||
if (!s_ip4)
|
||||
return FALSE;
|
||||
|
||||
/* Get any saved leases that apply to this connection */
|
||||
dhcp_mgr = nm_dhcp_manager_get ();
|
||||
leases = nm_dhcp_manager_get_lease_config (dhcp_mgr,
|
||||
nm_device_get_iface (self),
|
||||
nm_connection_get_uuid (connection));
|
||||
g_object_unref (dhcp_mgr);
|
||||
|
||||
method = nm_setting_ip4_config_get_method (s_ip4);
|
||||
if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) {
|
||||
gboolean found = FALSE;
|
||||
|
||||
/* Find at least one lease's address on the device */
|
||||
for (iter = leases; iter; iter = g_slist_next (iter)) {
|
||||
NMIP4Config *ip4_config = iter->data;
|
||||
NMIP4Address *addr = nm_ip4_config_get_address (ip4_config, 0);
|
||||
struct in_addr tmp = { .s_addr = nm_ip4_address_get_address (addr) };
|
||||
|
||||
if (addr && nm_netlink_find_address (nm_device_get_ip_ifindex (self),
|
||||
AF_INET,
|
||||
&tmp,
|
||||
nm_ip4_address_get_prefix (addr))) {
|
||||
found = TRUE; /* Yay, device has same address as a lease */
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_slist_foreach (leases, (GFunc) g_object_unref, NULL);
|
||||
g_slist_free (leases);
|
||||
return found;
|
||||
} else {
|
||||
/* Maybe the connection used to be DHCP and there are stale leases; ignore them */
|
||||
g_slist_foreach (leases, (GFunc) g_object_unref, NULL);
|
||||
g_slist_free (leases);
|
||||
}
|
||||
|
||||
if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) {
|
||||
// FIXME: Enforce no ipv4 addresses?
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* 'shared' and 'link-local' aren't supported methods because 'shared'
|
||||
* requires too much iptables and dnsmasq state to be reclaimed, and
|
||||
* avahi-autoipd isn't smart enough to allow the link-local address to be
|
||||
* determined at any point other than when it was first assigned.
|
||||
*/
|
||||
if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL))
|
||||
return FALSE;
|
||||
|
||||
/* Everything below for static addressing */
|
||||
|
||||
/* Find all IP4 addresses of this connection on the device */
|
||||
num = nm_setting_ip4_config_get_num_addresses (s_ip4);
|
||||
for (i = 0; i < num; i++) {
|
||||
NMIP4Address *addr = nm_setting_ip4_config_get_address (s_ip4, i);
|
||||
struct in_addr tmp = { .s_addr = nm_ip4_address_get_address (addr) };
|
||||
|
||||
if (!nm_netlink_find_address (nm_device_get_ip_ifindex (self),
|
||||
AF_INET,
|
||||
&tmp,
|
||||
nm_ip4_address_get_prefix (addr)))
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Success; all the connection's static IP addresses are assigned to the device */
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
nm_device_match_ip_config (NMDevice *device, NMConnection *connection)
|
||||
{
|
||||
if (!ip4_match_config (device, connection))
|
||||
return FALSE;
|
||||
|
||||
/* FIXME: match IPv6 config */
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
NMConnection *
|
||||
nm_device_connection_match_config (NMDevice *device, const GSList *connections)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue