From fca1c0d88fa927e1bbd030cbfb8b6d978fb70397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C5=A0imerda?= Date: Fri, 5 Jul 2013 16:41:51 +0200 Subject: [PATCH] trivial: move vpn/bonding functions out of nm-system As nm-system only contained auxiliary functions which fit in their respective modules better, it can be safely removed. --- src/Makefile.am | 2 - src/devices/nm-device-bond.c | 128 ++++++++++++- src/nm-system.c | 286 ---------------------------- src/nm-system.h | 36 ---- src/vpn-manager/nm-vpn-connection.c | 114 ++++++++++- 5 files changed, 235 insertions(+), 331 deletions(-) delete mode 100644 src/nm-system.c delete mode 100644 src/nm-system.h diff --git a/src/Makefile.am b/src/Makefile.am index 58c4769cf2..95ac7bc98d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -249,8 +249,6 @@ nm_sources = \ nm-session-utils.c \ nm-session-utils.h \ nm-sleep-monitor.h \ - nm-system.c \ - nm-system.h \ nm-types.h \ nm-wifi-ap-utils.c \ nm-wifi-ap-utils.h \ diff --git a/src/devices/nm-device-bond.c b/src/devices/nm-device-bond.c index c6b965920d..dde8089d10 100644 --- a/src/devices/nm-device-bond.c +++ b/src/devices/nm-device-bond.c @@ -34,7 +34,6 @@ #include "nm-dbus-glib-types.h" #include "nm-dbus-manager.h" #include "nm-enum-types.h" -#include "nm-system.h" #include "nm-device-bond-glue.h" @@ -180,6 +179,131 @@ match_l2_config (NMDevice *self, NMConnection *connection) /******************************************************************/ +static const struct { + const char *option; + const char *default_value; +} bonding_defaults[] = { + { "mode", "balance-rr" }, + { "arp_interval", "0" }, + { "miimon", "0" }, + + { "ad_select", "stable" }, + { "arp_validate", "none" }, + { "downdelay", "0" }, + { "fail_over_mac", "none" }, + { "lacp_rate", "slow" }, + { "min_links", "0" }, + { "num_grat_arp", "1" }, + { "num_unsol_na", "1" }, + { "primary", "" }, + { "primary_reselect", "always" }, + { "resend_igmp", "1" }, + { "updelay", "0" }, + { "use_carrier", "1" }, + { "xmit_hash_policy", "layer2" }, + { NULL, NULL } +}; + +static void +remove_bonding_entries (const char *iface, const char *path) +{ + char cmd[20]; + char *value, **entries; + gboolean ret; + int i; + + if (!g_file_get_contents (path, &value, NULL, NULL)) + return; + + entries = g_strsplit (value, " ", -1); + for (i = 0; entries[i]; i++) { + snprintf (cmd, sizeof (cmd), "-%s", g_strstrip (entries[i])); + ret = nm_utils_do_sysctl (path, cmd); + if (!ret) { + nm_log_warn (LOGD_HW, "(%s): failed to remove entry '%s' from '%s'", + iface, entries[i], path); + } + } + g_strfreev (entries); +} + +static gboolean +option_valid_for_nm_setting (const char *option, const char **valid_opts) +{ + while (*valid_opts) { + if (strcmp (option, *valid_opts) == 0) + return TRUE; + valid_opts++; + } + return FALSE; +} + +static gboolean +apply_bonding_config (const char *iface, NMSettingBond *s_bond) +{ + const char **valid_opts; + const char *option, *value; + char path[FILENAME_MAX]; + char *current, *space; + gboolean ret; + int i; + + g_return_val_if_fail (iface != NULL, FALSE); + + /* Remove old slaves and arp_ip_targets */ + snprintf (path, sizeof (path), "/sys/class/net/%s/bonding/arp_ip_target", iface); + remove_bonding_entries (iface, path); + snprintf (path, sizeof (path), "/sys/class/net/%s/bonding/slaves", iface); + remove_bonding_entries (iface, path); + + /* Apply config/defaults */ + valid_opts = nm_setting_bond_get_valid_options (s_bond); + for (i = 0; bonding_defaults[i].option; i++) { + option = bonding_defaults[i].option; + if (option_valid_for_nm_setting (option, valid_opts)) + value = nm_setting_bond_get_option_by_name (s_bond, option); + else + value = NULL; + if (!value) + value = bonding_defaults[i].default_value; + + snprintf (path, sizeof (path), "/sys/class/net/%s/bonding/%s", iface, option); + if (g_file_get_contents (path, ¤t, NULL, NULL)) { + g_strstrip (current); + space = strchr (current, ' '); + if (space) + *space = '\0'; + if (strcmp (current, value) != 0) { + ret = nm_utils_do_sysctl (path, value); + if (!ret) { + nm_log_warn (LOGD_HW, "(%s): failed to set bonding attribute " + "'%s' to '%s'", iface, option, value); + } + } + } + } + + /* Handle arp_ip_target */ + value = nm_setting_bond_get_option_by_name (s_bond, "arp_ip_target"); + if (value) { + char **addresses, cmd[20]; + + snprintf (path, sizeof (path), "/sys/class/net/%s/bonding/arp_ip_target", iface); + addresses = g_strsplit (value, ",", -1); + for (i = 0; addresses[i]; i++) { + snprintf (cmd, sizeof (cmd), "+%s", g_strstrip (addresses[i])); + ret = nm_utils_do_sysctl (path, cmd); + if (!ret) { + nm_log_warn (LOGD_HW, "(%s): failed to add arp_ip_target '%s'", + iface, addresses[i]); + } + } + g_strfreev (addresses); + } + + return TRUE; +} + static NMActStageReturn act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason) { @@ -200,7 +324,7 @@ act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason) /* Interface must be down to set bond options */ nm_device_take_down (dev, TRUE); - if (!nm_system_apply_bonding_config (nm_device_get_ip_iface (dev), s_bond)) + if (!apply_bonding_config (nm_device_get_ip_iface (dev), s_bond)) ret = NM_ACT_STAGE_RETURN_FAILURE; nm_device_bring_up (dev, TRUE, &no_firmware); diff --git a/src/nm-system.c b/src/nm-system.c deleted file mode 100644 index 4db903a2db..0000000000 --- a/src/nm-system.c +++ /dev/null @@ -1,286 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* NetworkManager -- Network link manager - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2004 - 2012 Red Hat, Inc. - * Copyright (C) 2005 - 2008 Novell, Inc. - * Copyright (C) 1996 - 1997 Yoichi Hariguchi - * Copyright (C) January, 1998 Sergei Viznyuk - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "nm-system.h" -#include "nm-platform.h" -#include "nm-device.h" -#include "NetworkManagerUtils.h" -#include "nm-utils.h" -#include "nm-logging.h" - -NMPlatformIP4Route * -nm_system_add_ip4_vpn_gateway_route (NMDevice *parent_device, guint32 vpn_gw) -{ - NMIP4Config *parent_config; - guint32 parent_gw; - NMPlatformIP4Route *route = g_new0 (NMPlatformIP4Route, 1); - - g_return_val_if_fail (NM_IS_DEVICE (parent_device), NULL); - g_return_val_if_fail (vpn_gw != 0, NULL); - - /* Set up a route to the VPN gateway's public IP address through the default - * network device if the VPN gateway is on a different subnet. - */ - - parent_config = nm_device_get_ip4_config (parent_device); - g_return_val_if_fail (parent_config != NULL, NULL); - parent_gw = nm_ip4_config_get_gateway (parent_config); - - if (!parent_gw) { - g_free (route); - return NULL; - } - - route->ifindex = nm_device_get_ip_ifindex (parent_device); - route->network = vpn_gw; - route->plen = 32; - route->gateway = parent_gw; - route->metric = 1024; - route->mss = nm_ip4_config_get_mss (parent_config); - - /* If the VPN gateway is in the same subnet as one of the parent device's - * IP addresses, don't add the host route to it, but a route through the - * parent device. - */ - if (nm_ip4_config_destination_is_direct (parent_config, vpn_gw, 32)) - route->gateway = 0; - - if (!nm_platform_ip4_route_add (route->ifindex, - route->network, - route->plen, - route->gateway, - route->metric, - route->mss)) { - g_free (route); - nm_log_err (LOGD_DEVICE | LOGD_IP4, - "(%s): failed to add IPv4 route to VPN gateway: %s", - nm_device_get_iface (parent_device), - nm_platform_get_error_msg ()); - return NULL; - } - - return route; -} - -NMPlatformIP6Route * -nm_system_add_ip6_vpn_gateway_route (NMDevice *parent_device, - const struct in6_addr *vpn_gw) -{ - NMIP6Config *parent_config; - const struct in6_addr *parent_gw; - NMPlatformIP6Route *route = g_new0 (NMPlatformIP6Route, 1); - - g_return_val_if_fail (NM_IS_DEVICE (parent_device), NULL); - g_return_val_if_fail (vpn_gw != NULL, NULL); - - /* This is all just the same as - * nm_system_add_ip4_vpn_gateway_route(), except with an IPv6 - * address for the VPN gateway. - */ - - parent_config = nm_device_get_ip6_config (parent_device); - g_return_val_if_fail (parent_config != NULL, NULL); - parent_gw = nm_ip6_config_get_gateway (parent_config); - - if (!parent_gw) { - g_free (route); - return NULL; - } - - route->ifindex = nm_device_get_ip_ifindex (parent_device); - route->network = *vpn_gw; - route->plen = 128; - route->gateway = *parent_gw; - route->metric = 1024; - route->mss = nm_ip6_config_get_mss (parent_config); - - /* If the VPN gateway is in the same subnet as one of the parent device's - * IP addresses, don't add the host route to it, but a route through the - * parent device. - */ - if (nm_ip6_config_destination_is_direct (parent_config, vpn_gw, 128)) - route->gateway = in6addr_any; - - if (!nm_platform_ip6_route_add (route->ifindex, - route->network, - route->plen, - route->gateway, - route->metric, - route->mss)) { - g_free (route); - nm_log_err (LOGD_DEVICE | LOGD_IP6, - "(%s): failed to add IPv6 route to VPN gateway: %s", - nm_device_get_iface (parent_device), - nm_platform_get_error_msg ()); - return NULL; - } - - return route; -} - -static const struct { - const char *option; - const char *default_value; -} bonding_defaults[] = { - { "mode", "balance-rr" }, - { "arp_interval", "0" }, - { "miimon", "0" }, - - { "ad_select", "stable" }, - { "arp_validate", "none" }, - { "downdelay", "0" }, - { "fail_over_mac", "none" }, - { "lacp_rate", "slow" }, - { "min_links", "0" }, - { "num_grat_arp", "1" }, - { "num_unsol_na", "1" }, - { "primary", "" }, - { "primary_reselect", "always" }, - { "resend_igmp", "1" }, - { "updelay", "0" }, - { "use_carrier", "1" }, - { "xmit_hash_policy", "layer2" }, - { NULL, NULL } -}; - -static void -remove_bonding_entries (const char *iface, const char *path) -{ - char cmd[20]; - char *value, **entries; - gboolean ret; - int i; - - if (!g_file_get_contents (path, &value, NULL, NULL)) - return; - - entries = g_strsplit (value, " ", -1); - for (i = 0; entries[i]; i++) { - snprintf (cmd, sizeof (cmd), "-%s", g_strstrip (entries[i])); - ret = nm_utils_do_sysctl (path, cmd); - if (!ret) { - nm_log_warn (LOGD_HW, "(%s): failed to remove entry '%s' from '%s'", - iface, entries[i], path); - } - } - g_strfreev (entries); -} - -static gboolean -option_valid_for_nm_setting (const char *option, const char **valid_opts) -{ - while (*valid_opts) { - if (strcmp (option, *valid_opts) == 0) - return TRUE; - valid_opts++; - } - return FALSE; -} - -gboolean -nm_system_apply_bonding_config (const char *iface, NMSettingBond *s_bond) -{ - const char **valid_opts; - const char *option, *value; - char path[FILENAME_MAX]; - char *current, *space; - gboolean ret; - int i; - - g_return_val_if_fail (iface != NULL, FALSE); - - /* Remove old slaves and arp_ip_targets */ - snprintf (path, sizeof (path), "/sys/class/net/%s/bonding/arp_ip_target", iface); - remove_bonding_entries (iface, path); - snprintf (path, sizeof (path), "/sys/class/net/%s/bonding/slaves", iface); - remove_bonding_entries (iface, path); - - /* Apply config/defaults */ - valid_opts = nm_setting_bond_get_valid_options (s_bond); - for (i = 0; bonding_defaults[i].option; i++) { - option = bonding_defaults[i].option; - if (option_valid_for_nm_setting (option, valid_opts)) - value = nm_setting_bond_get_option_by_name (s_bond, option); - else - value = NULL; - if (!value) - value = bonding_defaults[i].default_value; - - snprintf (path, sizeof (path), "/sys/class/net/%s/bonding/%s", iface, option); - if (g_file_get_contents (path, ¤t, NULL, NULL)) { - g_strstrip (current); - space = strchr (current, ' '); - if (space) - *space = '\0'; - if (strcmp (current, value) != 0) { - ret = nm_utils_do_sysctl (path, value); - if (!ret) { - nm_log_warn (LOGD_HW, "(%s): failed to set bonding attribute " - "'%s' to '%s'", iface, option, value); - } - } - } - } - - /* Handle arp_ip_target */ - value = nm_setting_bond_get_option_by_name (s_bond, "arp_ip_target"); - if (value) { - char **addresses, cmd[20]; - - snprintf (path, sizeof (path), "/sys/class/net/%s/bonding/arp_ip_target", iface); - addresses = g_strsplit (value, ",", -1); - for (i = 0; addresses[i]; i++) { - snprintf (cmd, sizeof (cmd), "+%s", g_strstrip (addresses[i])); - ret = nm_utils_do_sysctl (path, cmd); - if (!ret) { - nm_log_warn (LOGD_HW, "(%s): failed to add arp_ip_target '%s'", - iface, addresses[i]); - } - } - g_strfreev (addresses); - } - - return TRUE; -} diff --git a/src/nm-system.h b/src/nm-system.h deleted file mode 100644 index 450407ee83..0000000000 --- a/src/nm-system.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* NetworkManager -- Network link manager - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2004 - 2010 Red Hat, Inc. - * Copyright (C) 2005 - 2008 Novell, Inc. - */ - -#ifndef NETWORK_MANAGER_SYSTEM_H -#define NETWORK_MANAGER_SYSTEM_H - -#include "nm-platform.h" -#include "nm-device.h" -#include "nm-setting-bond.h" - -NMPlatformIP4Route *nm_system_add_ip4_vpn_gateway_route (NMDevice *parent_device, - guint32 vpn_gw); -NMPlatformIP6Route *nm_system_add_ip6_vpn_gateway_route (NMDevice *parent_device, - const struct in6_addr *vpn_gw); - -gboolean nm_system_apply_bonding_config (const char *iface, - NMSettingBond *s_bond); -#endif diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index 2bd1ffc447..1c154aed51 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -35,7 +35,6 @@ #include "nm-setting-ip4-config.h" #include "nm-dbus-manager.h" #include "nm-platform.h" -#include "nm-system.h" #include "nm-logging.h" #include "nm-utils.h" #include "nm-active-connection.h" @@ -307,6 +306,60 @@ device_state_changed (NMDevice *device, } } +static NMPlatformIP4Route * +add_ip4_vpn_gateway_route (NMDevice *parent_device, guint32 vpn_gw) +{ + NMIP4Config *parent_config; + guint32 parent_gw; + NMPlatformIP4Route *route = g_new0 (NMPlatformIP4Route, 1); + + g_return_val_if_fail (NM_IS_DEVICE (parent_device), NULL); + g_return_val_if_fail (vpn_gw != 0, NULL); + + /* Set up a route to the VPN gateway's public IP address through the default + * network device if the VPN gateway is on a different subnet. + */ + + parent_config = nm_device_get_ip4_config (parent_device); + g_return_val_if_fail (parent_config != NULL, NULL); + parent_gw = nm_ip4_config_get_gateway (parent_config); + + if (!parent_gw) { + g_free (route); + return NULL; + } + + route->ifindex = nm_device_get_ip_ifindex (parent_device); + route->network = vpn_gw; + route->plen = 32; + route->gateway = parent_gw; + route->metric = 1024; + route->mss = nm_ip4_config_get_mss (parent_config); + + /* If the VPN gateway is in the same subnet as one of the parent device's + * IP addresses, don't add the host route to it, but a route through the + * parent device. + */ + if (nm_ip4_config_destination_is_direct (parent_config, vpn_gw, 32)) + route->gateway = 0; + + if (!nm_platform_ip4_route_add (route->ifindex, + route->network, + route->plen, + route->gateway, + route->metric, + route->mss)) { + g_free (route); + nm_log_err (LOGD_DEVICE | LOGD_IP4, + "(%s): failed to add IPv4 route to VPN gateway: %s", + nm_device_get_iface (parent_device), + nm_platform_get_error_msg ()); + return NULL; + } + + return route; +} + static void device_ip4_config_changed (NMDevice *device, GParamSpec *pspec, @@ -322,11 +375,62 @@ device_ip4_config_changed (NMDevice *device, /* Re-add the VPN gateway route */ if (priv->ip4_external_gw) { g_free (&priv->ip4_gw_route); - priv->ip4_gw_route = nm_system_add_ip4_vpn_gateway_route (priv->parent_dev, + priv->ip4_gw_route = add_ip4_vpn_gateway_route (priv->parent_dev, priv->ip4_external_gw); } } +static NMPlatformIP6Route * +add_ip6_vpn_gateway_route (NMDevice *parent_device, + const struct in6_addr *vpn_gw) +{ + NMIP6Config *parent_config; + const struct in6_addr *parent_gw; + NMPlatformIP6Route *route = g_new0 (NMPlatformIP6Route, 1); + + g_return_val_if_fail (NM_IS_DEVICE (parent_device), NULL); + g_return_val_if_fail (vpn_gw != NULL, NULL); + + parent_config = nm_device_get_ip6_config (parent_device); + g_return_val_if_fail (parent_config != NULL, NULL); + parent_gw = nm_ip6_config_get_gateway (parent_config); + + if (!parent_gw) { + g_free (route); + return NULL; + } + + route->ifindex = nm_device_get_ip_ifindex (parent_device); + route->network = *vpn_gw; + route->plen = 128; + route->gateway = *parent_gw; + route->metric = 1024; + route->mss = nm_ip6_config_get_mss (parent_config); + + /* If the VPN gateway is in the same subnet as one of the parent device's + * IP addresses, don't add the host route to it, but a route through the + * parent device. + */ + if (nm_ip6_config_destination_is_direct (parent_config, vpn_gw, 128)) + route->gateway = in6addr_any; + + if (!nm_platform_ip6_route_add (route->ifindex, + route->network, + route->plen, + route->gateway, + route->metric, + route->mss)) { + g_free (route); + nm_log_err (LOGD_DEVICE | LOGD_IP6, + "(%s): failed to add IPv6 route to VPN gateway: %s", + nm_device_get_iface (parent_device), + nm_platform_get_error_msg ()); + return NULL; + } + + return route; +} + static void device_ip6_config_changed (NMDevice *device, GParamSpec *pspec, @@ -342,7 +446,7 @@ device_ip6_config_changed (NMDevice *device, /* Re-add the VPN gateway route */ if (priv->ip6_external_gw) { g_free (priv->ip6_gw_route); - priv->ip6_gw_route = nm_system_add_ip6_vpn_gateway_route (priv->parent_dev, + priv->ip6_gw_route = add_ip6_vpn_gateway_route (priv->parent_dev, priv->ip6_external_gw); } } @@ -623,10 +727,10 @@ nm_vpn_connection_apply_config (NMVPNConnection *connection) g_clear_pointer (&priv->ip4_gw_route, g_free); g_clear_pointer (&priv->ip6_gw_route, g_free); if (priv->ip4_external_gw) { - priv->ip4_gw_route = nm_system_add_ip4_vpn_gateway_route (priv->parent_dev, + priv->ip4_gw_route = add_ip4_vpn_gateway_route (priv->parent_dev, priv->ip4_external_gw); } else if (priv->ip6_external_gw) { - priv->ip6_gw_route = nm_system_add_ip6_vpn_gateway_route (priv->parent_dev, + priv->ip6_gw_route = add_ip6_vpn_gateway_route (priv->parent_dev, priv->ip6_external_gw); }