14126Szf162725 /*
2*5895Syz147064  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
34126Szf162725  * Use is subject to license terms.
44126Szf162725  */
54126Szf162725 
64126Szf162725 /*
74126Szf162725  * Copyright (c) 2004, Sam Leffler <sam@errno.com>
84126Szf162725  * Sun elects to license this software under the BSD license.
94126Szf162725  * See README for more details.
104126Szf162725  */
114126Szf162725 
124126Szf162725 #pragma ident	"%Z%%M%	%I%	%E% SMI"
134126Szf162725 
144126Szf162725 #include <stdio.h>
154126Szf162725 #include <stdlib.h>
164126Szf162725 #include <errno.h>
174126Szf162725 #include <stdarg.h>
184126Szf162725 #include <fcntl.h>
194126Szf162725 #include <unistd.h>
204126Szf162725 #include <stropts.h>
214126Szf162725 #include <string.h>
224126Szf162725 #include <stddef.h>
234126Szf162725 
244126Szf162725 #include "wpa_impl.h"
254126Szf162725 #include "driver.h"
264126Szf162725 
274126Szf162725 #define	WPA_STATUS(status)	(status == DLADM_STATUS_OK? 0 : -1)
284126Szf162725 
294126Szf162725 /*
304126Szf162725  * get_bssid - get the current BSSID
31*5895Syz147064  * @linkid: linkid of the given interface
324126Szf162725  * @bssid: buffer for BSSID (IEEE80211_ADDR_LEN = 6 bytes)
334126Szf162725  *
344126Szf162725  * Returns: 0 on success, -1 on failure
354126Szf162725  *
364126Szf162725  * Query kernel driver for the current BSSID and copy it to @bssid.
374126Szf162725  * Setting @bssid to 00:00:00:00:00:00 is recommended if the STA is not
384126Szf162725  * associated.
394126Szf162725  */
404126Szf162725 int
41*5895Syz147064 wpa_driver_wifi_get_bssid(datalink_id_t linkid, char *bssid)
424126Szf162725 {
43*5895Syz147064 	dladm_status_t status;
444126Szf162725 	dladm_wlan_linkattr_t attr;
454126Szf162725 	dladm_wlan_attr_t *wl_attrp;
464126Szf162725 
47*5895Syz147064 	status = dladm_wlan_get_linkattr(linkid, &attr);
48*5895Syz147064 	if (status != DLADM_STATUS_OK)
494126Szf162725 		return (-1);
504126Szf162725 
514126Szf162725 	wl_attrp = &attr.la_wlan_attr;
524126Szf162725 	if ((attr.la_valid & DLADM_WLAN_LINKATTR_WLAN) == 0 ||
534126Szf162725 	    (wl_attrp->wa_valid & DLADM_WLAN_ATTR_BSSID) == 0)
544126Szf162725 		return (-1);
554126Szf162725 
564126Szf162725 	(void) memcpy(bssid, wl_attrp->wa_bssid.wb_bytes, DLADM_WLAN_BSSID_LEN);
574126Szf162725 
584126Szf162725 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_get_bssid: " MACSTR,
594126Szf162725 	    MAC2STR((unsigned char *)bssid));
604126Szf162725 
61*5895Syz147064 	return (WPA_STATUS(status));
624126Szf162725 }
634126Szf162725 
644126Szf162725 /*
654126Szf162725  * get_ssid - get the current SSID
66*5895Syz147064  * @linkid: linkid of the given interface
674126Szf162725  * @ssid: buffer for SSID (at least 32 bytes)
684126Szf162725  *
694126Szf162725  * Returns: length of the SSID on success, -1 on failure
704126Szf162725  *
714126Szf162725  * Query kernel driver for the current SSID and copy it to @ssid.
724126Szf162725  * Returning zero is recommended if the STA is not associated.
734126Szf162725  */
744126Szf162725 int
75*5895Syz147064 wpa_driver_wifi_get_ssid(datalink_id_t linkid, char *ssid)
764126Szf162725 {
774126Szf162725 	int ret;
78*5895Syz147064 	dladm_status_t status;
794126Szf162725 	dladm_wlan_linkattr_t attr;
804126Szf162725 	dladm_wlan_attr_t *wl_attrp;
814126Szf162725 
82*5895Syz147064 	status = dladm_wlan_get_linkattr(linkid, &attr);
83*5895Syz147064 	if (status != DLADM_STATUS_OK)
844126Szf162725 		return (-1);
854126Szf162725 
864126Szf162725 	wl_attrp = &attr.la_wlan_attr;
874126Szf162725 	if ((attr.la_valid & DLADM_WLAN_LINKATTR_WLAN) == 0 ||
884126Szf162725 	    (wl_attrp->wa_valid & DLADM_WLAN_ATTR_ESSID) == 0)
894126Szf162725 		return (-1);
904126Szf162725 
914126Szf162725 	(void) memcpy(ssid, wl_attrp->wa_essid.we_bytes, MAX_ESSID_LENGTH);
924126Szf162725 	ret = strlen(ssid);
934126Szf162725 
944126Szf162725 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_get_ssid: ssid=%s len=%d",
954126Szf162725 	    ssid, ret);
964126Szf162725 
974126Szf162725 	return (ret);
984126Szf162725 }
994126Szf162725 
1004126Szf162725 static int
101*5895Syz147064 wpa_driver_wifi_set_wpa_ie(datalink_id_t linkid, uint8_t *wpa_ie,
102*5895Syz147064     uint32_t wpa_ie_len)
1034126Szf162725 {
104*5895Syz147064 	dladm_status_t status;
1054126Szf162725 
1064126Szf162725 	wpa_printf(MSG_DEBUG, "%s", "wpa_driver_wifi_set_wpa_ie");
107*5895Syz147064 	status = dladm_wlan_wpa_set_ie(linkid, wpa_ie, wpa_ie_len);
1084126Szf162725 
109*5895Syz147064 	return (WPA_STATUS(status));
1104126Szf162725 }
1114126Szf162725 
1124126Szf162725 /*
1134126Szf162725  * set_wpa - enable/disable WPA support
114*5895Syz147064  * @linkid: linkid of the given interface
1154126Szf162725  * @enabled: 1 = enable, 0 = disable
1164126Szf162725  *
1174126Szf162725  * Returns: 0 on success, -1 on failure
1184126Szf162725  *
1194126Szf162725  * Configure the kernel driver to enable/disable WPA support. This may
1204126Szf162725  * be empty function, if WPA support is always enabled. Common
1214126Szf162725  * configuration items are WPA IE (clearing it when WPA support is
1224126Szf162725  * disabled), Privacy flag for capability field, roaming mode (need to
1234126Szf162725  * allow wpa_supplicant to control roaming).
1244126Szf162725  */
1254126Szf162725 static int
126*5895Syz147064 wpa_driver_wifi_set_wpa(datalink_id_t linkid, boolean_t enabled)
1274126Szf162725 {
128*5895Syz147064 	dladm_status_t status;
1294126Szf162725 
1304126Szf162725 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_wpa: enable=%d", enabled);
1314126Szf162725 
132*5895Syz147064 	if (!enabled && wpa_driver_wifi_set_wpa_ie(linkid, NULL, 0) < 0)
1334126Szf162725 		return (-1);
1344126Szf162725 
135*5895Syz147064 	status = dladm_wlan_wpa_set_wpa(linkid, enabled);
1364126Szf162725 
137*5895Syz147064 	return (WPA_STATUS(status));
1384126Szf162725 }
1394126Szf162725 
1404126Szf162725 static int
141*5895Syz147064 wpa_driver_wifi_del_key(datalink_id_t linkid, int key_idx, unsigned char *addr)
1424126Szf162725 {
143*5895Syz147064 	dladm_status_t status;
1444126Szf162725 	dladm_wlan_bssid_t bss;
1454126Szf162725 
1464126Szf162725 	wpa_printf(MSG_DEBUG, "%s: id=%d", "wpa_driver_wifi_del_key",
1474126Szf162725 	    key_idx);
1484126Szf162725 
1494126Szf162725 	(void) memcpy(bss.wb_bytes, addr, DLADM_WLAN_BSSID_LEN);
150*5895Syz147064 	status = dladm_wlan_wpa_del_key(linkid, key_idx, &bss);
1514126Szf162725 
152*5895Syz147064 	return (WPA_STATUS(status));
1534126Szf162725 }
1544126Szf162725 
1554126Szf162725 /*
1564126Szf162725  * set_key - configure encryption key
157*5895Syz147064  * @linkid: linkid of the given interface
1584126Szf162725  * @alg: encryption algorithm (%WPA_ALG_NONE, %WPA_ALG_WEP,
1594126Szf162725  *	%WPA_ALG_TKIP, %WPA_ALG_CCMP); %WPA_ALG_NONE clears the key.
1604126Szf162725  * @addr: address of the peer STA or ff:ff:ff:ff:ff:ff for
1614126Szf162725  *	broadcast/default keys
1624126Szf162725  * @key_idx: key index (0..3), always 0 for unicast keys
1634126Szf162725  * @set_tx: configure this key as the default Tx key (only used when
1644126Szf162725  *	driver does not support separate unicast/individual key
1654126Szf162725  * @seq: sequence number/packet number, @seq_len octets, the next
1664126Szf162725  *	packet number to be used for in replay protection; configured
1674126Szf162725  *	for Rx keys (in most cases, this is only used with broadcast
1684126Szf162725  *	keys and set to zero for unicast keys)
1694126Szf162725  * @seq_len: length of the @seq, depends on the algorithm:
1704126Szf162725  *	TKIP: 6 octets, CCMP: 6 octets
1714126Szf162725  * @key: key buffer; TKIP: 16-byte temporal key, 8-byte Tx Mic key,
1724126Szf162725  *	8-byte Rx Mic Key
1734126Szf162725  * @key_len: length of the key buffer in octets (WEP: 5 or 13,
1744126Szf162725  *	TKIP: 32, CCMP: 16)
1754126Szf162725  *
1764126Szf162725  * Returns: 0 on success, -1 on failure
1774126Szf162725  *
1784126Szf162725  * Configure the given key for the kernel driver. If the driver
1794126Szf162725  * supports separate individual keys (4 default keys + 1 individual),
1804126Szf162725  * @addr can be used to determine whether the key is default or
1814126Szf162725  * individual. If only 4 keys are supported, the default key with key
1824126Szf162725  * index 0 is used as the individual key. STA must be configured to use
1834126Szf162725  * it as the default Tx key (@set_tx is set) and accept Rx for all the
1844126Szf162725  * key indexes. In most cases, WPA uses only key indexes 1 and 2 for
1854126Szf162725  * broadcast keys, so key index 0 is available for this kind of
1864126Szf162725  * configuration.
1874126Szf162725  */
1884126Szf162725 static int
189*5895Syz147064 wpa_driver_wifi_set_key(datalink_id_t linkid, wpa_alg alg,
190*5895Syz147064     unsigned char *addr, int key_idx, boolean_t set_tx, uint8_t *seq,
191*5895Syz147064     uint32_t seq_len, uint8_t *key, uint32_t key_len)
1924126Szf162725 {
1934126Szf162725 	char *alg_name;
1944126Szf162725 	dladm_wlan_cipher_t cipher;
1954126Szf162725 	dladm_wlan_bssid_t bss;
196*5895Syz147064 	dladm_status_t status;
1974126Szf162725 
1984126Szf162725 	wpa_printf(MSG_DEBUG, "%s", "wpa_driver_wifi_set_key");
1994126Szf162725 	if (alg == WPA_ALG_NONE)
200*5895Syz147064 		return (wpa_driver_wifi_del_key(linkid, key_idx, addr));
2014126Szf162725 
2024126Szf162725 	switch (alg) {
2034126Szf162725 	case WPA_ALG_WEP:
2044126Szf162725 		alg_name = "WEP";
2054126Szf162725 		cipher = DLADM_WLAN_CIPHER_WEP;
2064126Szf162725 		break;
2074126Szf162725 	case WPA_ALG_TKIP:
2084126Szf162725 		alg_name = "TKIP";
2094126Szf162725 		cipher = DLADM_WLAN_CIPHER_TKIP;
2104126Szf162725 		break;
2114126Szf162725 	case WPA_ALG_CCMP:
2124126Szf162725 		alg_name = "CCMP";
2134126Szf162725 		cipher = DLADM_WLAN_CIPHER_AES_CCM;
2144126Szf162725 		break;
2154126Szf162725 	default:
2164126Szf162725 		wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_key:"
2174126Szf162725 		    " unknown/unsupported algorithm %d", alg);
2184126Szf162725 		return (-1);
2194126Szf162725 	}
2204126Szf162725 
2214126Szf162725 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_key: alg=%s key_idx=%d"
2224126Szf162725 	    " set_tx=%d seq_len=%d seq=%d key_len=%d",
2234126Szf162725 	    alg_name, key_idx, set_tx,
2244126Szf162725 	    seq_len, *(uint64_t *)seq, key_len);
2254126Szf162725 
2264126Szf162725 	if (seq_len > sizeof (uint64_t)) {
2274126Szf162725 		wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_key:"
2284126Szf162725 		    " seq_len %d too big", seq_len);
2294126Szf162725 		return (-1);
2304126Szf162725 	}
2314126Szf162725 	(void) memcpy(bss.wb_bytes, addr, DLADM_WLAN_BSSID_LEN);
2324126Szf162725 
233*5895Syz147064 	status = dladm_wlan_wpa_set_key(linkid, cipher, &bss, set_tx,
2344126Szf162725 	    *(uint64_t *)seq, key_idx, key, key_len);
2354126Szf162725 
236*5895Syz147064 	return (WPA_STATUS(status));
2374126Szf162725 }
2384126Szf162725 
2394126Szf162725 /*
2404126Szf162725  * disassociate - request driver to disassociate
241*5895Syz147064  * @linkid: linkid of the given interface
2424126Szf162725  * @reason_code: 16-bit reason code to be sent in the disassociation
2434126Szf162725  * frame
2444126Szf162725  *
2454126Szf162725  * Return: 0 on success, -1 on failure
2464126Szf162725  */
2474126Szf162725 static int
248*5895Syz147064 wpa_driver_wifi_disassociate(datalink_id_t linkid, int reason_code)
2494126Szf162725 {
250*5895Syz147064 	dladm_status_t status;
2514126Szf162725 
2524126Szf162725 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_disassociate");
2534126Szf162725 
254*5895Syz147064 	status = dladm_wlan_wpa_set_mlme(linkid, DLADM_WLAN_MLME_DISASSOC,
2554126Szf162725 	    reason_code, NULL);
2564126Szf162725 
257*5895Syz147064 	return (WPA_STATUS(status));
2584126Szf162725 }
2594126Szf162725 
2604126Szf162725 /*
2614126Szf162725  * associate - request driver to associate
262*5895Syz147064  * @linkid: linkid of the given interface
2634126Szf162725  * @bssid: BSSID of the selected AP
2644126Szf162725  * @wpa_ie: WPA information element to be included in (Re)Association
2654126Szf162725  *	Request (including information element id and length). Use of
2664126Szf162725  *	this WPA IE is optional. If the driver generates the WPA IE, it
2674126Szf162725  *	can use @pairwise_suite, @group_suite, and @key_mgmt_suite
2684126Szf162725  *	to select proper algorithms. In this case, the driver has to
2694126Szf162725  *	notify wpa_supplicant about the used WPA IE by generating an
2704126Szf162725  *	event that the interface code will convert into EVENT_ASSOCINFO
2714126Szf162725  *	data (see wpa_supplicant.h). When using WPA2/IEEE 802.11i,
2724126Szf162725  *	@wpa_ie is used for RSN IE instead. The driver can determine
2734126Szf162725  *	which version is used by looking at the first byte of the IE
2744126Szf162725  *	(0xdd for WPA, 0x30 for WPA2/RSN).
2754126Szf162725  * @wpa_ie_len: length of the @wpa_ie
2764126Szf162725  *
2774126Szf162725  * Return: 0 on success, -1 on failure
2784126Szf162725  */
2794126Szf162725 static int
280*5895Syz147064 wpa_driver_wifi_associate(datalink_id_t linkid, const char *bssid,
2814126Szf162725     uint8_t *wpa_ie, uint32_t wpa_ie_len)
2824126Szf162725 {
283*5895Syz147064 	dladm_status_t status;
2844126Szf162725 	dladm_wlan_bssid_t bss;
2854126Szf162725 
2864126Szf162725 	wpa_printf(MSG_DEBUG, "wpa_driver_wifi_associate : "
2874126Szf162725 	    MACSTR, MAC2STR(bssid));
2884126Szf162725 
2894126Szf162725 	/*
2904126Szf162725 	 * NB: Don't need to set the freq or cipher-related state as
2914126Szf162725 	 * this is implied by the bssid which is used to locate
2924126Szf162725 	 * the scanned node state which holds it.
2934126Szf162725 	 */
294*5895Syz147064 	if (wpa_driver_wifi_set_wpa_ie(linkid, wpa_ie, wpa_ie_len) < 0)
2954126Szf162725 		return (-1);
2964126Szf162725 
2974126Szf162725 	(void) memcpy(bss.wb_bytes, bssid, DLADM_WLAN_BSSID_LEN);
298*5895Syz147064 	status = dladm_wlan_wpa_set_mlme(linkid, DLADM_WLAN_MLME_ASSOC,
2994126Szf162725 	    0, &bss);
3004126Szf162725 
301*5895Syz147064 	return (WPA_STATUS(status));
3024126Szf162725 }
3034126Szf162725 
3044126Szf162725 /*
3054126Szf162725  * scan - request the driver to initiate scan
306*5895Syz147064  * @linkid: linkid of the given interface
3074126Szf162725  *
3084126Szf162725  * Return: 0 on success, -1 on failure
3094126Szf162725  *
3104126Szf162725  * Once the scan results are ready, the driver should report scan
3114126Szf162725  * results event for wpa_supplicant which will eventually request the
3124126Szf162725  * results with wpa_driver_get_scan_results().
3134126Szf162725  */
3144126Szf162725 static int
315*5895Syz147064 wpa_driver_wifi_scan(datalink_id_t linkid)
3164126Szf162725 {
317*5895Syz147064 	dladm_status_t status;
3184126Szf162725 
3194126Szf162725 	wpa_printf(MSG_DEBUG, "%s", "wpa_driver_wifi_scan");
3204126Szf162725 	/*
3214126Szf162725 	 * We force the state to INIT before calling ieee80211_new_state
3224126Szf162725 	 * to get ieee80211_begin_scan called.  We really want to scan w/o
3234126Szf162725 	 * altering the current state but that's not possible right now.
3244126Szf162725 	 */
325*5895Syz147064 	(void) wpa_driver_wifi_disassociate(linkid,
3264126Szf162725 	    DLADM_WLAN_REASON_DISASSOC_LEAVING);
3274126Szf162725 
328*5895Syz147064 	status = dladm_wlan_scan(linkid, NULL, NULL);
3294126Szf162725 
3304126Szf162725 	wpa_printf(MSG_DEBUG, "%s: return", "wpa_driver_wifi_scan");
331*5895Syz147064 	return (WPA_STATUS(status));
3324126Szf162725 }
3334126Szf162725 
3344126Szf162725 /*
3354126Szf162725  * get_scan_results - fetch the latest scan results
336*5895Syz147064  * @linkid: linkid of the given interface
3374126Szf162725  * @results: pointer to buffer for scan results
3384126Szf162725  * @max_size: maximum number of entries (buffer size)
3394126Szf162725  *
3404126Szf162725  * Return: number of scan result entries used on success, -1 on failure
3414126Szf162725  *
3424126Szf162725  * If scan results include more than @max_size BSSes, @max_size will be
3434126Szf162725  * returned and the remaining entries will not be included in the
3444126Szf162725  * buffer.
3454126Szf162725  */
3464126Szf162725 int
347*5895Syz147064 wpa_driver_wifi_get_scan_results(datalink_id_t linkid,
3484126Szf162725     dladm_wlan_ess_t *results, uint32_t max_size)
3494126Szf162725 {
3504126Szf162725 	uint_t ret;
3514126Szf162725 
352*5895Syz147064 	wpa_printf(MSG_DEBUG, "%s: max size=%d\n",
353*5895Syz147064 	    "wpa_driver_wifi_get_scan_results", max_size);
3544126Szf162725 
355*5895Syz147064 	if (dladm_wlan_wpa_get_sr(linkid, results, max_size, &ret)
3564126Szf162725 	    != DLADM_STATUS_OK) {
3574126Szf162725 		return (-1);
3584126Szf162725 	}
3594126Szf162725 
3604126Szf162725 	return (ret);
3614126Szf162725 }
3624126Szf162725 
3634126Szf162725 struct wpa_driver_ops wpa_driver_wifi_ops = {
3644126Szf162725 	wpa_driver_wifi_get_bssid,
3654126Szf162725 	wpa_driver_wifi_get_ssid,
3664126Szf162725 	wpa_driver_wifi_set_wpa,
3674126Szf162725 	wpa_driver_wifi_set_key,
3684126Szf162725 	wpa_driver_wifi_scan,
3694126Szf162725 	wpa_driver_wifi_get_scan_results,
3704126Szf162725 	wpa_driver_wifi_disassociate,
3714126Szf162725 	wpa_driver_wifi_associate
3724126Szf162725 };
373