xref: /titanic_50/usr/src/cmd/cmd-inet/usr.lib/wpad/driver_wifi.c (revision d62bc4badc1c1f1549c961cfb8b420e650e1272b)
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 #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  * @linkid: linkid of the given interface
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(datalink_id_t linkid, char *bssid)
42 {
43 	dladm_status_t status;
44 	dladm_wlan_linkattr_t attr;
45 	dladm_wlan_attr_t *wl_attrp;
46 
47 	status = dladm_wlan_get_linkattr(linkid, &attr);
48 	if (status != 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(status));
62 }
63 
64 /*
65  * get_ssid - get the current SSID
66  * @linkid: linkid of the given interface
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(datalink_id_t linkid, 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(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
101 wpa_driver_wifi_set_wpa_ie(datalink_id_t linkid, uint8_t *wpa_ie,
102     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(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
126 wpa_driver_wifi_set_wpa(datalink_id_t linkid, boolean_t enabled)
127 {
128 	dladm_status_t status;
129 
130 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_wpa: enable=%d", enabled);
131 
132 	if (!enabled && wpa_driver_wifi_set_wpa_ie(linkid, NULL, 0) < 0)
133 		return (-1);
134 
135 	status = dladm_wlan_wpa_set_wpa(linkid, enabled);
136 
137 	return (WPA_STATUS(status));
138 }
139 
140 static int
141 wpa_driver_wifi_del_key(datalink_id_t linkid, int key_idx, unsigned char *addr)
142 {
143 	dladm_status_t status;
144 	dladm_wlan_bssid_t bss;
145 
146 	wpa_printf(MSG_DEBUG, "%s: id=%d", "wpa_driver_wifi_del_key",
147 	    key_idx);
148 
149 	(void) memcpy(bss.wb_bytes, addr, DLADM_WLAN_BSSID_LEN);
150 	status = dladm_wlan_wpa_del_key(linkid, key_idx, &bss);
151 
152 	return (WPA_STATUS(status));
153 }
154 
155 /*
156  * set_key - configure encryption key
157  * @linkid: linkid of the given interface
158  * @alg: encryption algorithm (%WPA_ALG_NONE, %WPA_ALG_WEP,
159  *	%WPA_ALG_TKIP, %WPA_ALG_CCMP); %WPA_ALG_NONE clears the key.
160  * @addr: address of the peer STA or ff:ff:ff:ff:ff:ff for
161  *	broadcast/default keys
162  * @key_idx: key index (0..3), always 0 for unicast keys
163  * @set_tx: configure this key as the default Tx key (only used when
164  *	driver does not support separate unicast/individual key
165  * @seq: sequence number/packet number, @seq_len octets, the next
166  *	packet number to be used for in replay protection; configured
167  *	for Rx keys (in most cases, this is only used with broadcast
168  *	keys and set to zero for unicast keys)
169  * @seq_len: length of the @seq, depends on the algorithm:
170  *	TKIP: 6 octets, CCMP: 6 octets
171  * @key: key buffer; TKIP: 16-byte temporal key, 8-byte Tx Mic key,
172  *	8-byte Rx Mic Key
173  * @key_len: length of the key buffer in octets (WEP: 5 or 13,
174  *	TKIP: 32, CCMP: 16)
175  *
176  * Returns: 0 on success, -1 on failure
177  *
178  * Configure the given key for the kernel driver. If the driver
179  * supports separate individual keys (4 default keys + 1 individual),
180  * @addr can be used to determine whether the key is default or
181  * individual. If only 4 keys are supported, the default key with key
182  * index 0 is used as the individual key. STA must be configured to use
183  * it as the default Tx key (@set_tx is set) and accept Rx for all the
184  * key indexes. In most cases, WPA uses only key indexes 1 and 2 for
185  * broadcast keys, so key index 0 is available for this kind of
186  * configuration.
187  */
188 static int
189 wpa_driver_wifi_set_key(datalink_id_t linkid, wpa_alg alg,
190     unsigned char *addr, int key_idx, boolean_t set_tx, uint8_t *seq,
191     uint32_t seq_len, 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 	dladm_status_t status;
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(linkid, 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 	status = dladm_wlan_wpa_set_key(linkid, cipher, &bss, set_tx,
234 	    *(uint64_t *)seq, key_idx, key, key_len);
235 
236 	return (WPA_STATUS(status));
237 }
238 
239 /*
240  * disassociate - request driver to disassociate
241  * @linkid: linkid of the given interface
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(datalink_id_t linkid, int reason_code)
249 {
250 	dladm_status_t status;
251 
252 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_disassociate");
253 
254 	status = dladm_wlan_wpa_set_mlme(linkid, DLADM_WLAN_MLME_DISASSOC,
255 	    reason_code, NULL);
256 
257 	return (WPA_STATUS(status));
258 }
259 
260 /*
261  * associate - request driver to associate
262  * @linkid: linkid of the given interface
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(datalink_id_t linkid, const char *bssid,
281     uint8_t *wpa_ie, uint32_t wpa_ie_len)
282 {
283 	dladm_status_t status;
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(linkid, wpa_ie, wpa_ie_len) < 0)
295 		return (-1);
296 
297 	(void) memcpy(bss.wb_bytes, bssid, DLADM_WLAN_BSSID_LEN);
298 	status = dladm_wlan_wpa_set_mlme(linkid, DLADM_WLAN_MLME_ASSOC,
299 	    0, &bss);
300 
301 	return (WPA_STATUS(status));
302 }
303 
304 /*
305  * scan - request the driver to initiate scan
306  * @linkid: linkid of the given interface
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(datalink_id_t linkid)
316 {
317 	dladm_status_t status;
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(linkid,
326 	    DLADM_WLAN_REASON_DISASSOC_LEAVING);
327 
328 	status = dladm_wlan_scan(linkid, NULL, NULL);
329 
330 	wpa_printf(MSG_DEBUG, "%s: return", "wpa_driver_wifi_scan");
331 	return (WPA_STATUS(status));
332 }
333 
334 /*
335  * get_scan_results - fetch the latest scan results
336  * @linkid: linkid of the given interface
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(datalink_id_t linkid,
348     dladm_wlan_ess_t *results, uint32_t max_size)
349 {
350 	uint_t ret;
351 
352 	wpa_printf(MSG_DEBUG, "%s: max size=%d\n",
353 	    "wpa_driver_wifi_get_scan_results", max_size);
354 
355 	if (dladm_wlan_wpa_get_sr(linkid, 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