NetworkManager/src/NetworkManagerWireless.c

161 lines
4.5 KiB
C
Raw Normal View History

/* 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 <iwlib.h>
#include <openssl/md5.h>
#include "NetworkManager.h"
#include "NetworkManagerDevice.h"
#include "NetworkManagerWireless.h"
#include "NetworkManagerPolicy.h"
#include "NetworkManagerUtils.h"
extern gboolean debug;
/*
* nm_wireless_128bit_key_from_passphrase
*
* From a passphrase, generate a standard 128-bit WEP key using
* MD5 algorithm.
*
*/
char *nm_wireless_128bit_key_from_passphrase (char *passphrase)
{
char temp_buf [65];
char *ascii_key = g_new0 (char, 32);
char *raw_key = g_new0 (char, 16);
int passphrase_len;
MD5_CTX md5_ctx;
int i;
g_return_val_if_fail (passphrase != NULL, NULL);
/* Get at least 64 bits */
passphrase_len = strlen (passphrase);
for (i = 0; i < 64; i++)
temp_buf [i] = passphrase [i % passphrase_len];
/* Generate the actual WEP key */
MD5_Init (&md5_ctx);
MD5_Update (&md5_ctx, (const void *)temp_buf, 64);
MD5_Final (raw_key, &md5_ctx);
/* Convert raw key into ASCII key. Unfortunately, we must do this
* because we cannot deal with raw keys quite yet.
*/
for (i = 0; i < 16; i++)
{
char *temp = g_strdup_printf ("%02x", raw_key [i]);
strncat (ascii_key, temp+(strlen(temp)-2), 2);
g_free (temp);
}
ascii_key [26] = '\0';
return (ascii_key);
}
/*
* nm_wireless_is_most_prefered_ap
*
* For a given AP, filter it through the allowed list and return TRUE if its
* both allowed _and_ has a better priority than highest_priority.
*
*/
gboolean nm_wireless_is_most_prefered_ap (NMData *data, NMAccessPoint *ap, int *highest_priority)
{
GSList *element;
gboolean is_most_preferred = FALSE;
g_return_val_if_fail (data != NULL, FALSE);
g_return_val_if_fail (ap != NULL, FALSE);
g_return_val_if_fail (highest_priority != NULL, FALSE);
2004-08-02 Dan Williams <dcbw@redhat.com> * TODO - new task: proper logging support * info-daemon/NetworkManagerInfo.c - Correct spelling of "canceled" - Correct casting of objects for g_signal_connect() * info-daemon/NetworkManagerInfoDbus.c - Add defines for NetworkManager namespace and object path, and use them - Add filter function to trap new signals from NetworkManager: WirelessNetworkAppeared, WirelessNetworkDisappeared * info-daemon/passphrase.glade - Change name of "ok" button to "Login to Network..." - Mark invisible * src/NetworkManager.c - Code and debug message cleanups - Rename "nm_add_current_devices"->"nm_add_initial_devices" - (nm_add_initial_devices) Check returned string array of devices and don't try to add devices if array is NULL - (main) Initialize libhal a bit later, make code a bit clearer * src/NetworkManagerAP.[ch] - New accessor and data member "matched": used to speed up AP list diffing - New accessor and data member "enc_method": will be used during key fallback to cache which passphrase->key conversion actually works so we don't have to do it every time * src/NetworkManagerAPList.[ch] - (nm_ap_list_find_ap_in_list) New: find an AP by essid in an AP list - (nm_ap_list_diff) New: given two lists of access points, find the differences between them, and send WirelessNetworkAppeared/Disappeared signals over dbus in response to those differences * src/NetworkManagerDbus.[ch] - (nm_dbus_get_object_path_from_ap) New: given a device and an access point, make an object path for that access point (NOTE that we don't yet check to make sure that access point is actually in the device's AP list yet) - (nm_dbus_get_ap_from_object_path) Renamed from nm_dbus_get_network_from_object_path - (nm_dbus_signal_wireless_network_appeared, nm_dbus_signal_wireless_network_disappeared) New: signal appearance/disappearance of wireless networks - (nm_dbus_set_user_key_for_network) Mark the network/ap as invalid if the user cancelled key entry * src/NetworkManagerDevice.[ch] - (nm_device_ap_list_clear) Use nm_ap_list_free rather than doing it ourselves - (nm_device_ap_list_get) New: return the AP list (static function) - (nm_device_do_normal_scan) Destroy old AP list later, so that we can diff the new one resulting from the scan with the old one * src/NetworkManagerWireless.c - (nm_wireless_is_most_prefered_ap) "invalid" access points cannot be "best" access points * test/nminfotest.c - #define object paths and namespaces and use the #defines rather than static strings - Test out user-key functionality of NetworkManagerInfo too git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@33 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2004-08-02 21:12:40 +00:00
/* If the AP is marked as invalid, of course its not prefered */
if (nm_ap_get_invalid (ap))
return (FALSE);
/* Attempt to acquire mutex for device list iteration.
* If the acquire fails, just ignore the scan completely.
*/
if (nm_try_acquire_mutex (data->allowed_ap_list_mutex, __FUNCTION__))
{
element = data->allowed_ap_list;
2004-07-27 Dan Williams <dcbw@redhat.com> * src/NetworkManagerAPList.[ch] src/Makefile.am - Add. Deal with allowed network list additions, deletions, and updates * dispatcher-daemon/NetworkManagerDispatcher.c - Add missing <dbus/dbus.h> header * info-daemon/NetworkManagerInfo.[ch] - Add missing <dbus/dbus.h> header - Implement the GConf notify callback to signal NetworkManager of an allowed network change - Better error checking * info-daemon/NetworkManagerInfoDbus.[ch] - Add missing <dbus/dbus.h> header - Convert to using dbus_message_append_args/dbus_message_get_args - Implement nmi_dbus_signal_update_allowed_network() to signal NetworkManager that an allowed network changed. We don't want to signal on individual keys _inside_ an allowed network really, just want NM to query the info daemon for updated info on all keys. - Better error checking * src/NetworkManager.[ch] - Add missing <dbus/dbus.h> header - Move allowed_ap_list free functions to NetworkManagerAPList.[ch] - Zero out NMData structure on free - No longer use a thread for allowed_ap_list updating, instead its now done through dbus queries against NetworkManagerInfo - Populate allowed_ap_list initially before adding existing network devices to the device list, so wireless devices can get their "best" AP * src/NetworkManagerDbus.[ch] - Convert to using dbus_message_append_args/dbus_message_get_args - Better error checking - Implement Allowed Network info functions to request allowed network info from NetworkManagerInfo - Implement the filter function to process signals from NetworkManagerInfo about changing allowed networks * src/NetworkManagerDevice.c - Fix file descriptor leak in nm_device_update_ip4_address() `CVS: Modified Files: git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@22 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2004-07-27 16:15:36 +00:00
while (element)
{
NMAccessPoint *allowed_ap = (NMAccessPoint *)(element->data);
/* If the essid of the scanned ap matches one in our allowed list, and this AP is
* a higher priority than one we may possibly have already found.
*/
if ( allowed_ap
&& (nm_null_safe_strcmp (nm_ap_get_essid (allowed_ap), nm_ap_get_essid (ap)) == 0)
&& (nm_ap_get_priority (allowed_ap) < *highest_priority))
{
2004-07-15 Dan Williams <dcbw@redhat.com> * src/Makefile.am - Turn on warnings * src/NetworkManager.c - nm_create_device_and_add_to_list(): call nm_device_deactivate() rather that doing the deactivation ourselves - Cancel an pending actions on a device if its being removed - Break up link state checking a bit, make non-active wireless cards deactivated to save power - Remove unused variables * src/NetworkManager.h - Add support for "pending" device * src/NetworkManagerAP.h src/NetworkManagerAP.c - Add support for determining whether and AP has encryption enabled or not - AP address is now "struct ether_addr" rather than a string * src/NetworkManagerDbus.h src/NetworkManagerDbus.c - Add signal NeedKeyForNetwork, method SetKeyForNetwork (testing only) - Changes for AP address from struct ether_addr->string * src/NetworkManagerDevice.h src/NetworkManagerDevice.c - Remove unused variables, fix warnings - Add support for Pending Actions (things that block a device from being "active" until they are completed). - First pending action: Get a WEP key from the user - Add nm_device_is_wire[d|less](), rename nm_device_is_wireless() - Clean up explicit testing of dev->iface_type to use nm_device_is_wireless() - Update wireless link checking to try to determine if the AP we are associated with is correct, but the WEP key we are using is just wrong. If its wrong, trigger the GetUserKey pending action on the device - If dhclient can't get an IP address, it brings the device down. Bring it back up in that case, otherwise we can't scan or link-check on it - Add IP address change notifications at appropriate points (still needs some work) - Add nm_device_need_ap_switch(), checks whether we need to switch access points or not * src/NetworkManagerPolicy.h src/NetworkManagerPolicy.c - Split out "best" access point determiniation into separate function - Make device activation 2-stage: first the device is pending, then in the next iteration through it becomes "active" unless it has pending actions * src/NetworkManagerUtils.h src/NetworkManagerUtils.c - Clean up unused variables and warnings - Wrap our debug macros in {} to prevent possible confusion * src/NetworkManagerWireless.c - Forgot to return current best priority, which lead to last available AP always being chosen no matter what its priority was. Corrected. git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@15 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2004-07-15 16:51:48 +00:00
*highest_priority = nm_ap_get_priority (allowed_ap);
is_most_preferred = TRUE;
break;
}
element = g_slist_next (element);
}
nm_unlock_mutex (data->allowed_ap_list_mutex, __FUNCTION__);
}
else
NM_DEBUG_PRINT( "nm_wireless_is_most_prefered_ap() could not acquire allowed access point mutex.\n" );
return (is_most_preferred);
}
/*
* nm_wireless_scan_monitor
*
* Called every 10s to get a list of access points.
*
*/
gboolean nm_wireless_scan_monitor (gpointer user_data)
{
NMData *data = (NMData *)user_data;
g_return_val_if_fail (data != NULL, TRUE);
if (!data->active_device)
return (TRUE);
/* Attempt to acquire mutex so that data->active_device sticks around.
* If the acquire fails, just ignore the scan completely.
*/
if (nm_try_acquire_mutex (data->dev_list_mutex, __FUNCTION__))
{
if (data->active_device && (nm_device_get_iface_type (data->active_device) == NM_IFACE_TYPE_WIRELESS_ETHERNET))
nm_device_do_wireless_scan (data->active_device);
nm_unlock_mutex (data->dev_list_mutex, __FUNCTION__);
}
else
NM_DEBUG_PRINT( "nm_wireless_scan_monitor() could not acquire device list mutex.\n" );
return (TRUE);
}