xref: /titanic_44/usr/src/cmd/cmd-inet/usr.lib/wpad/driver_wifi.c (revision 990b4856d0eaada6f8140335733a1b1771ed2746)
1 /*
2  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 2004, Sam Leffler <sam@errno.com>
8  * Sun elects to license this software under the BSD license.
9  * See README for more details.
10  */
11 
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <stdarg.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <stropts.h>
21 #include <string.h>
22 #include <stddef.h>
23 
24 #include "wpa_impl.h"
25 #include "driver.h"
26 
27 #define	WPA_STATUS(status)	(status == DLADM_STATUS_OK? 0 : -1)
28 
29 /*
30  * get_bssid - get the current BSSID
31  * @ifname: interface name, e.g., wlan0
32  * @bssid: buffer for BSSID (IEEE80211_ADDR_LEN = 6 bytes)
33  *
34  * Returns: 0 on success, -1 on failure
35  *
36  * Query kernel driver for the current BSSID and copy it to @bssid.
37  * Setting @bssid to 00:00:00:00:00:00 is recommended if the STA is not
38  * associated.
39  */
40 int
41 wpa_driver_wifi_get_bssid(const char *ifname, char *bssid)
42 {
43 	int ret;
44 	dladm_wlan_linkattr_t attr;
45 	dladm_wlan_attr_t *wl_attrp;
46 
47 	ret = dladm_wlan_get_linkattr(ifname, &attr);
48 	if (ret != DLADM_STATUS_OK)
49 		return (-1);
50 
51 	wl_attrp = &attr.la_wlan_attr;
52 	if ((attr.la_valid & DLADM_WLAN_LINKATTR_WLAN) == 0 ||
53 	    (wl_attrp->wa_valid & DLADM_WLAN_ATTR_BSSID) == 0)
54 		return (-1);
55 
56 	(void) memcpy(bssid, wl_attrp->wa_bssid.wb_bytes, DLADM_WLAN_BSSID_LEN);
57 
58 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_get_bssid: " MACSTR,
59 	    MAC2STR((unsigned char *)bssid));
60 
61 	return (WPA_STATUS(ret));
62 }
63 
64 /*
65  * get_ssid - get the current SSID
66  * @ifname: interface name, e.g., wlan0
67  * @ssid: buffer for SSID (at least 32 bytes)
68  *
69  * Returns: length of the SSID on success, -1 on failure
70  *
71  * Query kernel driver for the current SSID and copy it to @ssid.
72  * Returning zero is recommended if the STA is not associated.
73  */
74 int
75 wpa_driver_wifi_get_ssid(const char *ifname, char *ssid)
76 {
77 	int ret;
78 	dladm_wlan_linkattr_t attr;
79 	dladm_wlan_attr_t *wl_attrp;
80 
81 	ret = dladm_wlan_get_linkattr(ifname, &attr);
82 	if (ret != DLADM_STATUS_OK)
83 		return (-1);
84 
85 	wl_attrp = &attr.la_wlan_attr;
86 	if ((attr.la_valid & DLADM_WLAN_LINKATTR_WLAN) == 0 ||
87 	    (wl_attrp->wa_valid & DLADM_WLAN_ATTR_ESSID) == 0)
88 		return (-1);
89 
90 	(void) memcpy(ssid, wl_attrp->wa_essid.we_bytes, MAX_ESSID_LENGTH);
91 	ret = strlen(ssid);
92 
93 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_get_ssid: ssid=%s len=%d",
94 	    ssid, ret);
95 
96 	return (ret);
97 }
98 
99 static int
100 wpa_driver_wifi_set_wpa_ie(const char *ifname,
101     uint8_t *wpa_ie, uint32_t wpa_ie_len)
102 {
103 	int ret;
104 
105 	wpa_printf(MSG_DEBUG, "%s", "wpa_driver_wifi_set_wpa_ie");
106 	ret = dladm_wlan_wpa_set_ie(ifname, wpa_ie, wpa_ie_len);
107 
108 	return (WPA_STATUS(ret));
109 }
110 
111 /*
112  * set_wpa - enable/disable WPA support
113  * @ifname: interface name, e.g., wlan0
114  * @enabled: 1 = enable, 0 = disable
115  *
116  * Returns: 0 on success, -1 on failure
117  *
118  * Configure the kernel driver to enable/disable WPA support. This may
119  * be empty function, if WPA support is always enabled. Common
120  * configuration items are WPA IE (clearing it when WPA support is
121  * disabled), Privacy flag for capability field, roaming mode (need to
122  * allow wpa_supplicant to control roaming).
123  */
124 static int
125 wpa_driver_wifi_set_wpa(const char *ifname, boolean_t enabled)
126 {
127 	int ret;
128 
129 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_wpa: enable=%d", enabled);
130 
131 	if (!enabled && wpa_driver_wifi_set_wpa_ie(ifname, NULL, 0) < 0)
132 		return (-1);
133 
134 	ret = dladm_wlan_wpa_set_wpa(ifname, enabled);
135 
136 	return (WPA_STATUS(ret));
137 }
138 
139 static int
140 wpa_driver_wifi_del_key(const char *ifname, int key_idx, unsigned char *addr)
141 {
142 	int ret;
143 	dladm_wlan_bssid_t bss;
144 
145 	wpa_printf(MSG_DEBUG, "%s: id=%d", "wpa_driver_wifi_del_key",
146 	    key_idx);
147 
148 	(void) memcpy(bss.wb_bytes, addr, DLADM_WLAN_BSSID_LEN);
149 	ret = dladm_wlan_wpa_del_key(ifname, key_idx, &bss);
150 
151 	return (WPA_STATUS(ret));
152 }
153 
154 /*
155  * set_key - configure encryption key
156  * @ifname: interface name, e.g., wlan0
157  * @alg: encryption algorithm (%WPA_ALG_NONE, %WPA_ALG_WEP,
158  *	%WPA_ALG_TKIP, %WPA_ALG_CCMP); %WPA_ALG_NONE clears the key.
159  * @addr: address of the peer STA or ff:ff:ff:ff:ff:ff for
160  *	broadcast/default keys
161  * @key_idx: key index (0..3), always 0 for unicast keys
162  * @set_tx: configure this key as the default Tx key (only used when
163  *	driver does not support separate unicast/individual key
164  * @seq: sequence number/packet number, @seq_len octets, the next
165  *	packet number to be used for in replay protection; configured
166  *	for Rx keys (in most cases, this is only used with broadcast
167  *	keys and set to zero for unicast keys)
168  * @seq_len: length of the @seq, depends on the algorithm:
169  *	TKIP: 6 octets, CCMP: 6 octets
170  * @key: key buffer; TKIP: 16-byte temporal key, 8-byte Tx Mic key,
171  *	8-byte Rx Mic Key
172  * @key_len: length of the key buffer in octets (WEP: 5 or 13,
173  *	TKIP: 32, CCMP: 16)
174  *
175  * Returns: 0 on success, -1 on failure
176  *
177  * Configure the given key for the kernel driver. If the driver
178  * supports separate individual keys (4 default keys + 1 individual),
179  * @addr can be used to determine whether the key is default or
180  * individual. If only 4 keys are supported, the default key with key
181  * index 0 is used as the individual key. STA must be configured to use
182  * it as the default Tx key (@set_tx is set) and accept Rx for all the
183  * key indexes. In most cases, WPA uses only key indexes 1 and 2 for
184  * broadcast keys, so key index 0 is available for this kind of
185  * configuration.
186  */
187 static int
188 wpa_driver_wifi_set_key(const char *ifname, wpa_alg alg,
189     unsigned char *addr, int key_idx,
190     boolean_t set_tx, uint8_t *seq, uint32_t seq_len,
191     uint8_t *key, uint32_t key_len)
192 {
193 	char *alg_name;
194 	dladm_wlan_cipher_t cipher;
195 	dladm_wlan_bssid_t bss;
196 	int ret;
197 
198 	wpa_printf(MSG_DEBUG, "%s", "wpa_driver_wifi_set_key");
199 	if (alg == WPA_ALG_NONE)
200 		return (wpa_driver_wifi_del_key(ifname, key_idx, addr));
201 
202 	switch (alg) {
203 	case WPA_ALG_WEP:
204 		alg_name = "WEP";
205 		cipher = DLADM_WLAN_CIPHER_WEP;
206 		break;
207 	case WPA_ALG_TKIP:
208 		alg_name = "TKIP";
209 		cipher = DLADM_WLAN_CIPHER_TKIP;
210 		break;
211 	case WPA_ALG_CCMP:
212 		alg_name = "CCMP";
213 		cipher = DLADM_WLAN_CIPHER_AES_CCM;
214 		break;
215 	default:
216 		wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_key:"
217 		    " unknown/unsupported algorithm %d", alg);
218 		return (-1);
219 	}
220 
221 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_key: alg=%s key_idx=%d"
222 	    " set_tx=%d seq_len=%d seq=%d key_len=%d",
223 	    alg_name, key_idx, set_tx,
224 	    seq_len, *(uint64_t *)seq, key_len);
225 
226 	if (seq_len > sizeof (uint64_t)) {
227 		wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_key:"
228 		    " seq_len %d too big", seq_len);
229 		return (-1);
230 	}
231 	(void) memcpy(bss.wb_bytes, addr, DLADM_WLAN_BSSID_LEN);
232 
233 	ret = dladm_wlan_wpa_set_key(ifname, cipher, &bss, set_tx,
234 	    *(uint64_t *)seq, key_idx, key, key_len);
235 
236 	return (WPA_STATUS(ret));
237 }
238 
239 /*
240  * disassociate - request driver to disassociate
241  * @ifname: interface name, e.g., wlan0
242  * @reason_code: 16-bit reason code to be sent in the disassociation
243  * frame
244  *
245  * Return: 0 on success, -1 on failure
246  */
247 static int
248 wpa_driver_wifi_disassociate(const char *ifname, int reason_code)
249 {
250 	int ret;
251 
252 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_disassociate");
253 
254 	ret = dladm_wlan_wpa_set_mlme(ifname, DLADM_WLAN_MLME_DISASSOC,
255 	    reason_code, NULL);
256 
257 	return (WPA_STATUS(ret));
258 }
259 
260 /*
261  * associate - request driver to associate
262  * @ifname: interface name, e.g., wlan0
263  * @bssid: BSSID of the selected AP
264  * @wpa_ie: WPA information element to be included in (Re)Association
265  *	Request (including information element id and length). Use of
266  *	this WPA IE is optional. If the driver generates the WPA IE, it
267  *	can use @pairwise_suite, @group_suite, and @key_mgmt_suite
268  *	to select proper algorithms. In this case, the driver has to
269  *	notify wpa_supplicant about the used WPA IE by generating an
270  *	event that the interface code will convert into EVENT_ASSOCINFO
271  *	data (see wpa_supplicant.h). When using WPA2/IEEE 802.11i,
272  *	@wpa_ie is used for RSN IE instead. The driver can determine
273  *	which version is used by looking at the first byte of the IE
274  *	(0xdd for WPA, 0x30 for WPA2/RSN).
275  * @wpa_ie_len: length of the @wpa_ie
276  *
277  * Return: 0 on success, -1 on failure
278  */
279 static int
280 wpa_driver_wifi_associate(const char *ifname, const char *bssid,
281     uint8_t *wpa_ie, uint32_t wpa_ie_len)
282 {
283 	int ret;
284 	dladm_wlan_bssid_t bss;
285 
286 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_associate : "
287 	    MACSTR, MAC2STR(bssid));
288 
289 	/*
290 	 * NB: Don't need to set the freq or cipher-related state as
291 	 * this is implied by the bssid which is used to locate
292 	 * the scanned node state which holds it.
293 	 */
294 	if (wpa_driver_wifi_set_wpa_ie(ifname, wpa_ie, wpa_ie_len) < 0)
295 		return (-1);
296 
297 	(void) memcpy(bss.wb_bytes, bssid, DLADM_WLAN_BSSID_LEN);
298 	ret = dladm_wlan_wpa_set_mlme(ifname, DLADM_WLAN_MLME_ASSOC,
299 	    0, &bss);
300 
301 	return (WPA_STATUS(ret));
302 }
303 
304 /*
305  * scan - request the driver to initiate scan
306  * @ifname: interface name, e.g., wlan0
307  *
308  * Return: 0 on success, -1 on failure
309  *
310  * Once the scan results are ready, the driver should report scan
311  * results event for wpa_supplicant which will eventually request the
312  * results with wpa_driver_get_scan_results().
313  */
314 static int
315 wpa_driver_wifi_scan(const char *ifname)
316 {
317 	int ret;
318 
319 	wpa_printf(MSG_DEBUG, "%s", "wpa_driver_wifi_scan");
320 	/*
321 	 * We force the state to INIT before calling ieee80211_new_state
322 	 * to get ieee80211_begin_scan called.  We really want to scan w/o
323 	 * altering the current state but that's not possible right now.
324 	 */
325 	(void) wpa_driver_wifi_disassociate(ifname,
326 	    DLADM_WLAN_REASON_DISASSOC_LEAVING);
327 
328 	ret = dladm_wlan_scan(ifname, NULL, NULL);
329 
330 	wpa_printf(MSG_DEBUG, "%s: return", "wpa_driver_wifi_scan");
331 	return (WPA_STATUS(ret));
332 }
333 
334 /*
335  * get_scan_results - fetch the latest scan results
336  * @ifname: interface name, e.g., wlan0
337  * @results: pointer to buffer for scan results
338  * @max_size: maximum number of entries (buffer size)
339  *
340  * Return: number of scan result entries used on success, -1 on failure
341  *
342  * If scan results include more than @max_size BSSes, @max_size will be
343  * returned and the remaining entries will not be included in the
344  * buffer.
345  */
346 int
347 wpa_driver_wifi_get_scan_results(const char *ifname,
348     dladm_wlan_ess_t *results, uint32_t max_size)
349 {
350 	uint_t ret;
351 
352 	wpa_printf(MSG_DEBUG, "%s: interface name =%s max size=%d\n",
353 		"wpa_driver_wifi_get_scan_results", ifname, max_size);
354 
355 	if (dladm_wlan_wpa_get_sr(ifname, results, max_size, &ret)
356 	    != DLADM_STATUS_OK) {
357 		return (-1);
358 	}
359 
360 	return (ret);
361 }
362 
363 struct wpa_driver_ops wpa_driver_wifi_ops = {
364 	wpa_driver_wifi_get_bssid,
365 	wpa_driver_wifi_get_ssid,
366 	wpa_driver_wifi_set_wpa,
367 	wpa_driver_wifi_set_key,
368 	wpa_driver_wifi_scan,
369 	wpa_driver_wifi_get_scan_results,
370 	wpa_driver_wifi_disassociate,
371 	wpa_driver_wifi_associate
372 };
373