NetworkManager/src/NetworkManagerPolicy.c
Dan Williams 4de66efbd4 2004-08-31 Dan Williams <dcbw@redhat.com>
* Remove 'debug' extern global from all files since we now
		use syslog()

	* src/NetworkManager.[ch]
		- Break out routine that get the net.interface property from HAL,
			removing that logic from nm_create_device_and_add_to_list()
		- (nm_create_device_and_add_to_list): make this a bit more general so
			it doesn't do the talking to HAL.  Also add arguments to facilitate
			the create of test devices.
		- (nm_data_mark_state_changed): rename from nm_data_set_state_modified()
		- (nm_data_new, main, nm_print_usage): add new argument "--enable-test-devices"
			which makes NetworkManager listen for dbus commands to create test
			devices, which have no backing hardware.  Use when you're on a plane
			for example, and/or forgot your wireless card at home.  Test devices
			_cannot_ be created unless NM is started with --enable-test-devices.

	* src/NetworkManagerDbus.[ch]
		- New "getLinkActive" method for devices
		- New "setLinkActive" method for devices (only works on test devices)
		- New "createTestDevice" method on NetworkManager object to create a test
			device of a specified type (ie wired, wireless).  UDI is created from
			scratch, as is the interface name.  Only works when NM is started with
			--enable-test-devices switch.
		- New "removeTestDevice" method on NetworkManager object which removes a
			test device.  Only works when NM is started with --enable-test-devices

	* src/NetworkManagerDevice.[ch]
		- Logic to facilitate test devices.  Add variables to NMDevice struct to indicate
			whether a device is a test device or not, and what its link status is.
		- Deal with test devices in most functions.  For those that work directly on hardware
			special-case test devices.
		- (nm_device_new): don't create a test device if test devices weren't enabled on the
			command-line.
		- (nm_device_update_link_active): split out logic for wired and wireless device link
			checking to separate functions to facilitate test device link checking.
		- (nm_device_set_enc_key): Since some drivers for wireless cards are daft and
			don't make a distinction between System Authentication and Encryption
			(namely Cisco aironet), we use Open System auth when setting a WEP key
			on the card.  We don't deal with Shared Key auth yet.
		- (nm_device_activation_worker): split the activation cancel check logic out into
			a separate routine nm_device_activation_cancel_if_needed()
		- (nm_device_activation_signal_cancel): rename from nm_device_activation_cancel()
		- (nm_device_fake_ap_list): Test wireless devices obviously cannot scan, so create
			a list of fake access points that they can "see"
		- (nm_device_is_test_device): return whether or not a device is a test device

	* src/NetworkManagerPolicy.c
		- (nm_policy_get_best_device): attempt to deal with wireless network selection,
			previously if you "locked"/forced NM to use a wireless device but then
			selected a wireless network for NM to use, it would switch to a wired device.
			So, if the active device is wireless and it has a "forced" best AP, use it
			if the "forced" best AP is still valid
		- (nm_state_modification_monitor): deal with NULL best devices, for example
			there were no usable network devices, or the last one was removed

	* src/backends/NetworkManager*.c
		- Deal with test devices, mostly just return success for operations like getting
			a DHCP address

	* test/nmtestdevices.c
		- Test tool to create/remove/link-switch test devices


git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@112 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2004-08-31 16:09:15 +00:00

472 lines
12 KiB
C

/* NetworkManager -- Network link manager
*
* Dan Williams <dcbw@redhat.com>
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* (C) Copyright 2004 Red Hat, Inc.
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/select.h>
#include "NetworkManagerPolicy.h"
#include "NetworkManagerUtils.h"
#include "NetworkManagerAP.h"
#include "NetworkManagerAPList.h"
#include "NetworkManagerDbus.h"
gboolean allowed_ap_worker_exit = FALSE;
/*
* nm_policy_auto_get_best_device
*
* Find the best device to use, regardless of whether we are
* "locked" on one device at this time.
*
*/
static NMDevice * nm_policy_auto_get_best_device (NMData *data)
{
GSList *element;
NMDevice *best_wired_dev = NULL;
guint best_wired_prio = 0;
NMDevice *best_wireless_dev = NULL;
guint best_wireless_prio = 0;
NMDevice *highest_priority_dev = NULL;
g_return_val_if_fail (data != NULL, NULL);
element = data->dev_list;
while (element)
{
NMDevice *dev = NULL;
guint dev_type;
gboolean link_active;
guint prio = 0;
dev = (NMDevice *)(element->data);
dev_type = nm_device_get_type (dev);
link_active = nm_device_get_link_active (dev);
if (dev_type == DEVICE_TYPE_WIRED_ETHERNET)
{
if (link_active)
prio += 1;
if ( data->active_device
&& (dev == data->active_device)
&& link_active)
prio += 1;
if (prio > best_wired_prio)
{
best_wired_dev = dev;
best_wired_prio = prio;
}
}
else if (dev_type == DEVICE_TYPE_WIRELESS_ETHERNET)
{
NMAccessPoint *best_ap = nm_device_get_best_ap (dev);
/* This deals with the case where the WEP key we have
* for an access point is wrong. In that case, the
* MAC address of the associated AP will be invalid,
* so link_active will be FALSE. However, we still want
* to use this card and AP, just need to get the correct
* WEP key from the user via NetworkManagerInfo.
*/
if ( !link_active
&& !nm_device_need_ap_switch (dev)
&& best_ap
&& nm_ap_get_encrypted (best_ap))
link_active = TRUE;
if (link_active)
prio += 1;
if (nm_device_get_supports_wireless_scan (dev))
prio += 2;
else
prio += 1;
if ( data->active_device
&& (dev == data->active_device)
&& link_active)
prio += 3;
if (prio > best_wireless_prio)
{
best_wireless_dev = dev;
best_wireless_prio = prio;
}
}
element = g_slist_next (element);
}
syslog (LOG_NOTICE, "AUTO: Best wired device = %s", best_wired_dev ? nm_device_get_iface (best_wired_dev) : "(null)");
syslog (LOG_NOTICE, "AUTO: Best wireless device = %s (%s)", best_wireless_dev ? nm_device_get_iface (best_wireless_dev) : "(null)",
best_wireless_dev ? nm_device_get_essid (best_wireless_dev) : "null" );
if (best_wireless_dev || best_wired_dev)
{
if (best_wired_dev)
highest_priority_dev = best_wired_dev;
else
highest_priority_dev = best_wireless_dev;
}
return (highest_priority_dev);
}
/*
* nm_policy_get_best_device
*
* Find the best device to use, taking into account if we are
* "locked" on one device or not. That lock may also be cleared
* under certain conditions.
*
*/
static NMDevice * nm_policy_get_best_device (NMData *data)
{
NMDevice *best_dev = NULL;
g_return_val_if_fail (data != NULL, NULL);
/* Can't lock the active device if you don't have one */
if (!data->active_device)
data->active_device_locked = FALSE;
/* If the user told us to switch to a particular device, do it now */
if (nm_try_acquire_mutex (data->user_device_mutex, __FUNCTION__))
{
if (data->user_device)
{
best_dev = data->user_device;
nm_device_unref (data->user_device);
data->user_device = NULL;
}
nm_unlock_mutex (data->user_device_mutex, __FUNCTION__);
}
/* Determine whether we need to clear the active device and unlock it.
* This occurs if the best device is removed, for example.
*/
if (!best_dev && data->active_device_locked)
{
switch (nm_device_get_type (data->active_device))
{
/* If the active device was a wired device, and it no
* longer has a link, switch to auto mode.
*/
case (DEVICE_TYPE_WIRED_ETHERNET):
if (nm_device_get_link_active (data->active_device))
best_dev = data->active_device;
break;
/* For wireless devices, we only "unlock" them if they are
* removed from the system.
*/
case (DEVICE_TYPE_WIRELESS_ETHERNET):
best_dev = data->active_device;
break;
default:
break;
}
}
/* Or, if the current active device is wireless and its "best" access
* point is locked, use that device still. This happens when the user
* forces a specific wireless network choice. The "best" ap will have
* already been set and locked by the dbus message handler, so we just
* need to test for a locked "best" ap.
*/
if (data->active_device && nm_device_is_wireless (data->active_device))
{
/* Give ourselves a chance to clear the "best" access point if
* its gone out of range and no longer in the device's ap list.
*/
nm_device_update_best_ap (data->active_device);
if (nm_device_get_best_ap_frozen (data->active_device))
best_dev = data->active_device;
}
/* Fall back to automatic device picking */
if (!best_dev)
{
data->active_device_locked = FALSE;
best_dev = nm_policy_auto_get_best_device (data);
}
return (best_dev);
}
/*
* nm_state_modification_monitor
*
* Called every 2s and figures out which interface to switch the active
* network connection to if our global network state has changed.
* Global network state changes are triggered by:
* 1) insertion/deletion of interfaces
* 2) link state change of an interface
* 3) appearance/disappearance of an allowed wireless access point
*
*/
gboolean nm_state_modification_monitor (gpointer user_data)
{
NMData *data = (NMData *)user_data;
gboolean modified = FALSE;
g_return_val_if_fail (data != NULL, TRUE);
/* If the info daemon is now running, get our trusted/preferred ap lists from it */
if (data->info_daemon_avail && data->update_ap_lists)
{
/* Query info daemon for network lists if its now running */
if (data->trusted_ap_list)
nm_ap_list_unref (data->trusted_ap_list);
data->trusted_ap_list = nm_ap_list_new (NETWORK_TYPE_TRUSTED);
if (data->trusted_ap_list)
nm_ap_list_populate (data->trusted_ap_list, data);
if (data->preferred_ap_list)
nm_ap_list_unref (data->preferred_ap_list);
data->preferred_ap_list = nm_ap_list_new (NETWORK_TYPE_PREFERRED);
if (data->preferred_ap_list)
nm_ap_list_populate (data->preferred_ap_list, data);
data->update_ap_lists = FALSE;
}
/* Check global state modified variable, and reset it with
* appropriate locking.
*/
g_mutex_lock (data->state_modified_mutex);
modified = data->state_modified;
if (data->state_modified)
data->state_modified = FALSE;
g_mutex_unlock (data->state_modified_mutex);
/* If any modifications to the data model were made, update
* network state based on policy applied to the data model.
*/
if (modified)
{
if (nm_try_acquire_mutex (data->dev_list_mutex, __FUNCTION__))
{
NMDevice *best_dev = NULL;
if ((best_dev = nm_policy_get_best_device (data)))
nm_device_ref (best_dev);
/* Only do a switch when:
* 1) the best_dev is different from data->active_device, OR
* 2) best_dev is wireless and its access point is not the "best" ap, OR
* 3) best_dev is wireless and its access point is the best, but it doesn't have an IP address
*/
if ( best_dev != data->active_device
|| ( best_dev && nm_device_is_wireless (best_dev) && !nm_device_activating (best_dev)
&& (nm_device_need_ap_switch (best_dev) || (nm_device_get_ip4_address (best_dev) == 0))))
{
/* Deactivate the old device */
if (data->active_device)
{
nm_device_deactivate (data->active_device, FALSE);
nm_device_unref (data->active_device);
data->active_device = NULL;
}
if (best_dev)
{
/* Begin activation on the new device */
syslog (LOG_INFO, "nm_state_modification_monitor(): beginning activation for device '%s'", nm_device_get_iface (best_dev));
nm_device_ref (best_dev);
data->active_device = best_dev;
nm_device_activation_begin (data->active_device);
}
}
nm_unlock_mutex (data->dev_list_mutex, __FUNCTION__);
}
else
syslog( LOG_ERR, "nm_state_modification_monitor() could not get device list mutex");
}
else if (data->active_device && nm_device_just_activated (data->active_device))
{
nm_dbus_signal_device_status_change (data->dbus_connection, data->active_device, DEVICE_NOW_ACTIVE);
syslog (LOG_INFO, "nm_state_modification_monitor() activated device %s", nm_device_get_iface (data->active_device));
}
return (TRUE);
}
#if 0
/*
* nm_policy_allowed_ap_refresh_worker
*
* Worker thread function to periodically refresh the allowed
* access point list with updated data.
*
*/
gpointer nm_policy_allowed_ap_refresh_worker (gpointer user_data)
{
NMData *data = (NMData *)(user_data);
struct timeval timeout;
g_return_val_if_fail (data != NULL, NULL);
/* Simply loop and every 20s update the available allowed ap data */
while (!allowed_ap_worker_exit)
{
int err;
timeout.tv_sec = 20;
timeout.tv_usec = 0;
/* Wait, but don't execute the update if select () returned an error,
* since it may have immediately returned, so that we don't hammer
* GConf (or the hard drive).
*/
err = select (0, NULL, NULL, NULL, &timeout);
if (err >= 0)
nm_policy_update_allowed_access_points (data);
}
g_thread_exit (0);
return (NULL);
}
/*
* nm_policy_update_allowed_access_points
*
* Grabs a list of allowed access points from the user's preferences
*
*/
void nm_policy_update_allowed_access_points (NMData *data)
{
#define NM_ALLOWED_AP_FILE "/etc/sysconfig/networking/allowed_access_points"
FILE *ap_file;
g_return_if_fail (data != NULL);
if (nm_try_acquire_mutex (data->allowed_ap_list_mutex, __FUNCTION__))
{
ap_file = fopen (NM_ALLOWED_AP_FILE, "r");
if (ap_file)
{
gchar line[ 500 ];
gchar prio[ 20 ];
gchar essid[ 50 ];
gchar wep_key[ 50 ];
/* Free the old list of allowed access points */
// nm_data_allowed_ap_list_free (data);
while (fgets (line, 499, ap_file))
{
guint len = strnlen (line, 499);
gchar *p = &line[0];
gchar *end = strchr (line, '\n');
guint op = 0;
strcpy (prio, "\0");
strcpy (essid, "\0");
strcpy (wep_key, "\0");
if (end)
*end = '\0';
else
end = p + len - 1;
while ((end-p > 0) && (*p=='\t'))
p++;
while (end-p > 0)
{
switch (op)
{
case 0:
strncat (prio, p, 1);
break;
case 1:
strncat (essid, p, 1);
break;
case 2:
strncat (wep_key, p, 1);
break;
default:
break;
}
p++;
if ((end-p > 0) && (*p=='\t'))
{
op++;
while ((end-p > 0) && (*p=='\t'))
p++;
}
}
/* Create a new entry for this essid */
if (strlen (essid) > 0)
{
NMAccessPoint *ap;
guint prio_num = atoi (prio);
if (prio_num < 1)
prio_num = NM_AP_PRIORITY_WORST;
else if (prio_num > NM_AP_PRIORITY_WORST)
prio_num = NM_AP_PRIORITY_WORST;
ap = nm_ap_new ();
nm_ap_set_priority (ap, prio_num);
nm_ap_set_essid (ap, essid);
if (strlen (wep_key) > 0)
nm_ap_set_wep_key (ap, wep_key);
data->allowed_ap_list = g_slist_append (data->allowed_ap_list, ap);
/*
syslog( LOG_DEBUG, "FOUND: allowed ap, prio=%d essid=%s wep_key=%s", prio_num, essid, wep_key );
*/
}
}
fclose (ap_file);
}
else
syslog( LOG_WARNING, "nm_policy_update_allowed_access_points() could not open allowed ap list file %s. errno %d", NM_ALLOWED_AP_FILE, errno );
nm_unlock_mutex (data->allowed_ap_list_mutex, __FUNCTION__);
}
else
syslog( LOG_ERR, "nm_policy_update_allowed_access_points() could not lock allowed ap list mutex" );
}
#endif