NetworkManagerWireless.c 4.79 KB
Newer Older
Dan Williams's avatar
Dan Williams committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* 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>
24
25
26
27
28
29
#include "config.h"
#ifdef HAVE_GCRYPT
#include <gcrypt.h>
#else
#include "gnome-keyring-md5.h"
#endif
Dan Williams's avatar
Dan Williams committed
30
31
32
33
34
35
36
#include "NetworkManager.h"
#include "NetworkManagerDevice.h"
#include "NetworkManagerWireless.h"
#include "NetworkManagerPolicy.h"
#include "NetworkManagerUtils.h"


37
38
39
40
41
42
43
44
45
46
47
/*
 * nm_wireless_md5_digest_to_ascii
 *
 * Convert an MD5 digest into an ascii string suitable for use
 * as a WEP key.
 *
 * Code originally by Alex Larsson <alexl@redhat.com> and
 *  copyright Red Hat, Inc. under terms of the LGPL.
 *
 */
static char *nm_wireless_md5_digest_to_ascii (unsigned char digest[16])
48
{
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	static char	 hex_digits[] = "0123456789abcdef";
	unsigned char	*res;
	int			 i;

	res = g_malloc (33);
	for (i = 0; i < 16; i++)
	{
		res[2*i] = hex_digits[digest[i] >> 4];
		res[2*i+1] = hex_digits[digest[i] & 0xf];
	}

	/* We chomp it at byte 26, since WEP keys only use 104 bits */
	res[26] = 0;

	return (res);
64
}
Dan Williams's avatar
Dan Williams committed
65

66

67
68
69
70
71
72
73
/*
 * nm_wireless_128bit_key_from_passphrase
 *
 * From a passphrase, generate a standard 128-bit WEP key using
 * MD5 algorithm.
 *
 */
74
char *nm_wireless_128bit_key_from_passphrase	(const char *passphrase)
75
{
76
77
78
79
	char		 	md5_data[65];
	unsigned char	digest[16];
	int			passphrase_len;
	int			i;
80
81

	g_return_val_if_fail (passphrase != NULL, NULL);
82

83
	passphrase_len = strlen (passphrase);
84
85
86
87
	if (passphrase_len < 1)
		return (NULL);

	/* Get at least 64 bits */
88
	for (i = 0; i < 64; i++)
89
90
91
92
93
94
95
96
97
		md5_data [i] = passphrase [i % passphrase_len];

	/* Null terminate md5 data-to-hash and hash it */
	md5_data[64] = 0;
#ifdef HAVE_GCRYPT
	gcry_md_hash_buffer (GCRY_MD_MD5, digest, md5_data, 64);
#else	
	gnome_keyring_md5_string (md5_data, digest);
#endif
98

99
	return (nm_wireless_md5_digest_to_ascii (digest));
100
101
102
}


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * nm_wireless_stats_to_percent
 *
 * Convert an iw_stats structure from a scan or the card into
 * a magical signal strength percentage.
 *
 */
int nm_wireless_qual_to_percent (NMDevice *dev, const struct iw_quality *qual)
{
	int	percent = -1;

	g_return_val_if_fail (dev != NULL, -1);
	g_return_val_if_fail (qual != NULL, -1);

	/* Try using the card's idea of the signal quality first */
	if (qual->qual >= 1)
	{
		percent = (int)rint ((log (qual->qual) / log (94)) * 100.0);
		percent = CLAMP (percent, 0, 100);
	}

	/* If that failed, try to calculate the signal quality based on other
	 * values, like Signal-to-Noise ratio.
	 */
	if (((percent == -1) || (percent == 0)))
	{
		/* If the statistics are in dBm or relative */
		if(qual->level > nm_device_get_max_quality (dev))
		{
			#define	BEST_SIGNAL	85		/* In dBm, stuck card next to AP, this is what I got :) */

			/* Values in dBm  (absolute power measurement) */
			if (qual->level > 0)
				percent = (int)rint ((double)(((256 - qual->level) / (double)BEST_SIGNAL) * 100));
		}
		else
		{
/* FIXME
 * Not quite sure what to do here...  Above we have a "100% strength" number
 * empirically derived, but I don't have any cards that trigger this code below...
 */
#if 0
			/* Relative values (0 -> max) */
			qual_rel = qual->level;
			qual_max_rel = range->max_qual.level;
			noise_rel = qual->noise;
			noise_max_rel = range->max_qual.noise;
#else
			percent = -1;
#endif
		}
	}

	return (percent);
}


Dan Williams's avatar
Dan Williams committed
160
161
162
163
164
165
166
167
168
/*
 * 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;
169
170
	GSList	*element;
	NMDevice	*dev;
Dan Williams's avatar
Dan Williams committed
171
172

	g_return_val_if_fail (data != NULL, TRUE);
173
174

	/* Attempt to acquire mutex so that data->active_device sticks around.
Dan Williams's avatar
Dan Williams committed
175
176
	 * If the acquire fails, just ignore the scan completely.
	 */
177
	if (!nm_try_acquire_mutex (data->dev_list_mutex, __FUNCTION__))
Dan Williams's avatar
Dan Williams committed
178
	{
179
180
181
		syslog (LOG_ERR, "nm_wireless_scan_monitor() could not acquire device list mutex." );
		return (TRUE);
	}
Dan Williams's avatar
Dan Williams committed
182

183
184
185
186
187
188
	element = data->dev_list;
	while (element)
	{
		if ((dev = (NMDevice *)(element->data)) && nm_device_is_wireless (dev))
			nm_device_do_wireless_scan (dev);
		element = g_slist_next (element);
Dan Williams's avatar
Dan Williams committed
189
	}
190
191

	nm_unlock_mutex (data->dev_list_mutex, __FUNCTION__);
Dan Williams's avatar
Dan Williams committed
192
193
194
	
	return (TRUE);
}