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