From d2d1f2e9e0ac5b27d93d466460aeea4f86fcd42d Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 22 Jul 2011 13:37:29 -0500 Subject: [PATCH] core: use interface indexes more --- src/backends/NetworkManagerGeneric.c | 4 +- src/modem-manager/nm-modem.c | 12 +-- src/nm-device-ethernet.c | 4 +- src/nm-device-olpc-mesh.c | 4 +- src/nm-device-wifi.c | 6 +- src/nm-device.c | 9 +- src/nm-system.c | 123 +++++++++------------------ src/nm-system.h | 15 ++-- src/vpn-manager/nm-vpn-connection.c | 19 +++-- src/wimax/nm-device-wimax.c | 4 +- 10 files changed, 82 insertions(+), 118 deletions(-) diff --git a/src/backends/NetworkManagerGeneric.c b/src/backends/NetworkManagerGeneric.c index 8f16066fa3..90a41bba97 100644 --- a/src/backends/NetworkManagerGeneric.c +++ b/src/backends/NetworkManagerGeneric.c @@ -57,8 +57,6 @@ void nm_generic_enable_loopback (void) int iface_idx = -1; int err; - nm_system_device_set_up_down_with_iface ("lo", TRUE, NULL); - nlh = nm_netlink_get_default_handle (); if (!nlh) return; @@ -67,6 +65,8 @@ void nm_generic_enable_loopback (void) if (iface_idx < 0) return; + nm_system_iface_set_up (iface_idx, TRUE, NULL); + addr = rtnl_addr_alloc (); if (!addr) return; diff --git a/src/modem-manager/nm-modem.c b/src/modem-manager/nm-modem.c index 33f10789ce..3a27a17ff7 100644 --- a/src/modem-manager/nm-modem.c +++ b/src/modem-manager/nm-modem.c @@ -629,7 +629,7 @@ static void real_deactivate (NMModem *self, NMDevice *device) { NMModemPrivate *priv; - const char *iface; + int ifindex; g_return_if_fail (self != NULL); g_return_if_fail (NM_IS_MODEM (self)); @@ -668,11 +668,11 @@ real_deactivate (NMModem *self, NMDevice *device) break; case MM_MODEM_IP_METHOD_STATIC: case MM_MODEM_IP_METHOD_DHCP: - iface = nm_device_get_ip_iface (device); + ifindex = nm_device_get_ip_ifindex (device); /* FIXME: use AF_UNSPEC here when we have IPv6 support */ - nm_system_device_flush_routes_with_iface (iface, AF_INET); - nm_system_device_flush_addresses_with_iface (iface); - nm_system_device_set_up_down_with_iface (iface, FALSE, NULL); + nm_system_iface_flush_routes (ifindex, AF_INET); + nm_system_iface_flush_addresses (ifindex, AF_UNSPEC); + nm_system_iface_set_up (ifindex, FALSE, NULL); break; default: nm_log_err (LOGD_MB, "unknown IP method %d", priv->ip_method); @@ -781,7 +781,7 @@ nm_modem_hw_bring_up (NMModem *self, NMDevice *device, gboolean *no_firmware) state = nm_device_interface_get_state (NM_DEVICE_INTERFACE (device)); if (priv->pending_ip4_config || _state_is_active (state)) - return nm_system_device_set_up_down (device, TRUE, no_firmware); + return nm_system_iface_set_up (nm_device_get_ip_ifindex (device), TRUE, no_firmware); } return TRUE; diff --git a/src/nm-device-ethernet.c b/src/nm-device-ethernet.c index 35b7367050..1e5ea0fbfb 100644 --- a/src/nm-device-ethernet.c +++ b/src/nm-device-ethernet.c @@ -552,13 +552,13 @@ real_hw_is_up (NMDevice *device) static gboolean real_hw_bring_up (NMDevice *dev, gboolean *no_firmware) { - return nm_system_device_set_up_down (dev, TRUE, no_firmware); + return nm_system_iface_set_up (nm_device_get_ip_ifindex (dev), TRUE, no_firmware); } static void real_hw_take_down (NMDevice *dev) { - nm_system_device_set_up_down (dev, FALSE, NULL); + nm_system_iface_set_up (nm_device_get_ip_ifindex (dev), FALSE, NULL); } NMDevice * diff --git a/src/nm-device-olpc-mesh.c b/src/nm-device-olpc-mesh.c index 8832234427..2125db4102 100644 --- a/src/nm-device-olpc-mesh.c +++ b/src/nm-device-olpc-mesh.c @@ -277,13 +277,13 @@ real_hw_is_up (NMDevice *device) static gboolean real_hw_bring_up (NMDevice *dev, gboolean *no_firmware) { - return nm_system_device_set_up_down (dev, TRUE, no_firmware); + return nm_system_iface_set_up (nm_device_get_ip_ifindex (dev), TRUE, no_firmware); } static void real_hw_take_down (NMDevice *dev) { - nm_system_device_set_up_down (dev, FALSE, NULL); + nm_system_iface_set_up (nm_device_get_ip_ifindex (dev), FALSE, NULL); } static gboolean diff --git a/src/nm-device-wifi.c b/src/nm-device-wifi.c index 278d9a0631..b70a575983 100644 --- a/src/nm-device-wifi.c +++ b/src/nm-device-wifi.c @@ -1142,13 +1142,13 @@ real_hw_bring_up (NMDevice *device, gboolean *no_firmware) if (!NM_DEVICE_WIFI_GET_PRIVATE (device)->enabled) return FALSE; - return nm_system_device_set_up_down (device, TRUE, no_firmware); + return nm_system_iface_set_up (nm_device_get_ip_ifindex (device), TRUE, no_firmware); } static void -real_hw_take_down (NMDevice *dev) +real_hw_take_down (NMDevice *device) { - nm_system_device_set_up_down (dev, FALSE, NULL); + nm_system_iface_set_up (nm_device_get_ip_ifindex (device), FALSE, NULL); } static gboolean diff --git a/src/nm-device.c b/src/nm-device.c index 1301197acf..04727b85aa 100644 --- a/src/nm-device.c +++ b/src/nm-device.c @@ -1600,7 +1600,7 @@ real_act_stage3_ip4_config_start (NMDevice *self, NMDeviceStateReason *reason) /* Make sure the interface is up before trying to do anything with it */ if (!nm_system_device_is_up_with_iface (ip_iface)) - nm_system_device_set_up_down_with_iface (ip_iface, TRUE, NULL); + nm_system_iface_set_up (priv->ip_ifindex, TRUE, NULL); req = nm_device_get_act_request (self); connection = nm_act_request_get_connection (req); @@ -2820,6 +2820,7 @@ nm_device_deactivate (NMDeviceInterface *device, NMDeviceStateReason reason) NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); NMDeviceStateReason ignored = NM_DEVICE_STATE_REASON_NONE; gboolean tried_ipv6 = FALSE; + int ifindex, family; g_return_if_fail (self != NULL); @@ -2857,8 +2858,10 @@ nm_device_deactivate (NMDeviceInterface *device, NMDeviceStateReason reason) clear_act_request (self); /* Take out any entries in the routing table and any IP address the device had. */ - nm_system_device_flush_routes (self, tried_ipv6 ? AF_UNSPEC : AF_INET); - nm_system_device_flush_addresses (self, tried_ipv6 ? AF_UNSPEC : AF_INET); + ifindex = nm_device_get_ip_ifindex (self); + family = tried_ipv6 ? AF_UNSPEC : AF_INET; + nm_system_iface_flush_routes (ifindex, family); + nm_system_iface_flush_addresses (ifindex, family); nm_device_update_ip4_address (self); /* Clean up nameservers and addresses */ diff --git a/src/nm-system.c b/src/nm-system.c index 88db65964a..017e57abe0 100644 --- a/src/nm-system.c +++ b/src/nm-system.c @@ -659,46 +659,39 @@ nm_system_apply_ip6_config (const char *iface, return TRUE; } -/* - * nm_system_device_set_up_down +/** + * nm_system_iface_set_up: + * @ifindex: interface index + * @up: %TRUE to bring interface up, or %FALSE to take it down + * @no_firmware: on return, %TRUE if the operation may have failed due to + * missing firmware * - * Mark the device as up or down. + * Bring the interface up or take it down. * - */ + * Returns: %TRUE on success, %FALSE on failure + **/ gboolean -nm_system_device_set_up_down (NMDevice *dev, - gboolean up, - gboolean *no_firmware) -{ - g_return_val_if_fail (dev != NULL, FALSE); - - return nm_system_device_set_up_down_with_iface (nm_device_get_ip_iface (dev), up, no_firmware); -} - -gboolean -nm_system_device_set_up_down_with_iface (const char *iface, - gboolean up, - gboolean *no_firmware) +nm_system_iface_set_up (int ifindex, + gboolean up, + gboolean *no_firmware) { struct rtnl_link *request = NULL, *old = NULL; struct nl_handle *nlh; gboolean success = FALSE; - guint32 idx; - g_return_val_if_fail (iface != NULL, FALSE); + g_return_val_if_fail (ifindex > 0, FALSE); if (no_firmware) g_return_val_if_fail (*no_firmware == FALSE, FALSE); if (!(request = rtnl_link_alloc ())) - goto out; + return FALSE; if (up) rtnl_link_set_flags (request, IFF_UP); else rtnl_link_unset_flags (request, IFF_UP); - idx = nm_netlink_iface_to_index (iface); - old = nm_netlink_index_to_rtnl_link (idx); + old = nm_netlink_index_to_rtnl_link (ifindex); if (old) { nlh = nm_netlink_get_default_handle (); if (nlh) { @@ -711,8 +704,6 @@ nm_system_device_set_up_down_with_iface (const char *iface, rtnl_link_put (old); rtnl_link_put (request); - -out: return success; } @@ -1187,39 +1178,21 @@ nm_system_replace_default_ip6_route (const char *iface, const struct in6_addr *g return success; } -static void flush_addresses (const char *iface, int family) -{ - int iface_idx; - - g_return_if_fail (iface != NULL); - iface_idx = nm_netlink_iface_to_index (iface); - if (iface_idx >= 0) - sync_addresses (iface, iface_idx, family, NULL, 0); -} - /* - * nm_system_device_flush_addresses + * nm_system_iface_flush_addresses * * Flush all network addresses associated with a network device * */ -void nm_system_device_flush_addresses (NMDevice *dev, int family) +gboolean +nm_system_iface_flush_addresses (int ifindex, int family) { - g_return_if_fail (dev != NULL); + const char *iface; - flush_addresses (nm_device_get_ip_iface (dev), family); -} + g_return_val_if_fail (ifindex > 0, FALSE); - -/* - * nm_system_device_flush_addresses_with_iface - * - * Flush all network addresses associated with a network device - * - */ -void nm_system_device_flush_addresses_with_iface (const char *iface) -{ - flush_addresses (iface, AF_UNSPEC); + iface = nm_netlink_index_to_iface (ifindex); + return iface ? sync_addresses (iface, ifindex, family, NULL, 0) : FALSE; } @@ -1335,20 +1308,29 @@ check_one_route (struct nl_object *object, void *user_data) } } -static void flush_routes (int ifindex, const char *iface, int family) +/** + * nm_system_iface_flush_routes: + * @ifindex: interface index + * @family: address family, i.e. AF_INET, AF_INET6, or AF_UNSPEC + * + * Flush all network addresses associated with a network device. + * + * Returns: %TRUE on success, %FALSE on failure + **/ +gboolean +nm_system_iface_flush_routes (int ifindex, int family) { RouteCheckData check_data; guint32 log_level = LOGD_IP4 | LOGD_IP6; const char *sf = "UNSPEC"; + const char *iface; - g_return_if_fail (iface != NULL); + g_return_val_if_fail (ifindex > 0, FALSE); - if (ifindex < 0) { - ifindex = nm_netlink_iface_to_index (iface); - if (ifindex < 0) { - nm_log_dbg (LOGD_DEVICE, "(%s) failed to lookup interface index", iface); - return; - } + iface = nm_netlink_index_to_iface (ifindex); + if (iface < 0) { + nm_log_warn (LOGD_DEVICE, "(%d) failed to lookup interface name", ifindex); + return FALSE; } if (family == AF_INET) { @@ -1366,33 +1348,8 @@ static void flush_routes (int ifindex, const char *iface, int family) check_data.iface_idx = ifindex; check_data.family = family; foreach_route (check_one_route, &check_data); -} -/* - * nm_system_device_flush_routes - * - * Flush all network addresses associated with a network device - * - */ -void nm_system_device_flush_routes (NMDevice *dev, int family) -{ - g_return_if_fail (dev != NULL); - - flush_routes (nm_device_get_ip_ifindex (dev), - nm_device_get_ip_iface (dev), - family); -} - -/* - * nm_system_device_flush_routes_with_iface - * - * Flush all routes associated with a network device. 'family' is an - * address family, either AF_INET, AF_INET6, or AF_UNSPEC. - * - */ -void nm_system_device_flush_routes_with_iface (const char *iface, int family) -{ - flush_routes (-1, iface, family); + return TRUE; } typedef struct { diff --git a/src/nm-system.h b/src/nm-system.h index 094f40efb1..02e8d0e920 100644 --- a/src/nm-system.h +++ b/src/nm-system.h @@ -35,8 +35,7 @@ * implemented in the backend files in backends/ directory */ -void nm_system_device_flush_routes (NMDevice *dev, int family); -void nm_system_device_flush_routes_with_iface (const char *iface, int family); +gboolean nm_system_iface_flush_routes (int ifindex, int family); gboolean nm_system_replace_default_ip4_route (const char *iface, guint32 gw, @@ -55,8 +54,7 @@ gboolean nm_system_replace_default_ip4_route_vpn (const char *iface, struct rtnl_route *nm_system_add_ip4_vpn_gateway_route (NMDevice *parent_device, NMIP4Config *vpn_config); -void nm_system_device_flush_addresses (NMDevice *dev, int family); -void nm_system_device_flush_addresses_with_iface (const char *iface); +gboolean nm_system_iface_flush_addresses (int ifindex, int family); void nm_system_enable_loopback (void); void nm_system_update_dns (void); @@ -81,12 +79,9 @@ gboolean nm_system_apply_ip6_config (const char *iface, int priority, NMIP6ConfigCompareFlags flags); -gboolean nm_system_device_set_up_down (NMDevice *dev, - gboolean up, - gboolean *no_firmware); -gboolean nm_system_device_set_up_down_with_iface (const char *iface, - gboolean up, - gboolean *no_firmware); +gboolean nm_system_iface_set_up (int ifindex, + gboolean up, + gboolean *no_firmware); gboolean nm_system_device_is_up (NMDevice *device); gboolean nm_system_device_is_up_with_iface (const char *iface); diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index 067b2dce3e..452498850f 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -86,6 +86,7 @@ typedef struct { NMIP4Config *ip4_config; guint32 ip4_internal_gw; char *ip_iface; + int ip_ifindex; char *banner; struct rtnl_route *gw_route; @@ -424,6 +425,13 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy, goto error; } + /* Grab the interface index for address/routing operations */ + priv->ip_ifindex = nm_netlink_iface_to_index (priv->ip_iface); + if (!priv->ip_ifindex) { + nm_log_err (LOGD_VPN, "(%s): failed to look up VPN interface index", priv->ip_iface); + goto error; + } + addr = nm_ip4_address_new (); nm_ip4_address_set_prefix (addr, 24); /* default to class C */ @@ -529,7 +537,7 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy, s_ip4 = NM_SETTING_IP4_CONFIG (nm_connection_get_setting (priv->connection, NM_TYPE_SETTING_IP4_CONFIG)); nm_utils_merge_ip4_config (config, s_ip4); - nm_system_device_set_up_down_with_iface (priv->ip_iface, TRUE, NULL); + nm_system_iface_set_up (priv->ip_ifindex, TRUE, NULL); if (nm_system_apply_ip4_config (priv->ip_iface, config, 0, NM_IP4_COMPARE_FLAG_ALL)) { NMDnsManager *dns_mgr; @@ -940,11 +948,11 @@ vpn_cleanup (NMVPNConnection *connection) { NMVPNConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection); - if (priv->ip_iface) { - nm_system_device_set_up_down_with_iface (priv->ip_iface, FALSE, NULL); + if (priv->ip_ifindex) { + nm_system_iface_set_up (priv->ip_ifindex, FALSE, NULL); /* FIXME: use AF_UNSPEC here when we have IPv6 support */ - nm_system_device_flush_routes_with_iface (priv->ip_iface, AF_INET); - nm_system_device_flush_addresses_with_iface (priv->ip_iface); + nm_system_iface_flush_routes (priv->ip_ifindex, AF_INET); + nm_system_iface_flush_addresses (priv->ip_ifindex, AF_UNSPEC); } if (priv->ip4_config) { @@ -982,6 +990,7 @@ vpn_cleanup (NMVPNConnection *connection) g_free (priv->ip_iface); priv->ip_iface = NULL; + priv->ip_ifindex = 0; /* Clear out connection secrets to ensure that the settings service * gets asked for them next time the connection is activated. diff --git a/src/wimax/nm-device-wimax.c b/src/wimax/nm-device-wimax.c index 9dff503d81..cc38cc3ce1 100644 --- a/src/wimax/nm-device-wimax.c +++ b/src/wimax/nm-device-wimax.c @@ -401,13 +401,13 @@ real_hw_bring_up (NMDevice *dev, gboolean *no_firmware) if (!priv->enabled || !priv->wimaxd_enabled) return FALSE; - return nm_system_device_set_up_down (dev, TRUE, no_firmware); + return nm_system_iface_set_up (nm_device_get_ip_ifindex (dev), TRUE, no_firmware); } static void real_hw_take_down (NMDevice *dev) { - nm_system_device_set_up_down (dev, FALSE, NULL); + nm_system_iface_set_up (nm_device_get_ip_ifindex (dev), FALSE, NULL); } static void