14126Szf162725 /* 25895Syz147064 * 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 #include <stdio.h> 134126Szf162725 #include <stdlib.h> 144126Szf162725 #include <errno.h> 154126Szf162725 #include <stdarg.h> 164126Szf162725 #include <fcntl.h> 174126Szf162725 #include <unistd.h> 184126Szf162725 #include <stropts.h> 194126Szf162725 #include <string.h> 204126Szf162725 #include <stddef.h> 214126Szf162725 224126Szf162725 #include "wpa_impl.h" 234126Szf162725 #include "driver.h" 244126Szf162725 254126Szf162725 #define WPA_STATUS(status) (status == DLADM_STATUS_OK? 0 : -1) 264126Szf162725 274126Szf162725 /* 284126Szf162725 * get_bssid - get the current BSSID 295895Syz147064 * @linkid: linkid of the given interface 304126Szf162725 * @bssid: buffer for BSSID (IEEE80211_ADDR_LEN = 6 bytes) 314126Szf162725 * 324126Szf162725 * Returns: 0 on success, -1 on failure 334126Szf162725 * 344126Szf162725 * Query kernel driver for the current BSSID and copy it to @bssid. 354126Szf162725 * Setting @bssid to 00:00:00:00:00:00 is recommended if the STA is not 364126Szf162725 * associated. 374126Szf162725 */ 384126Szf162725 int 39*8453SAnurag.Maskey@Sun.COM wpa_driver_wifi_get_bssid(dladm_handle_t handle, datalink_id_t linkid, 40*8453SAnurag.Maskey@Sun.COM char *bssid) 414126Szf162725 { 425895Syz147064 dladm_status_t status; 434126Szf162725 dladm_wlan_linkattr_t attr; 444126Szf162725 dladm_wlan_attr_t *wl_attrp; 454126Szf162725 46*8453SAnurag.Maskey@Sun.COM status = dladm_wlan_get_linkattr(handle, linkid, &attr); 475895Syz147064 if (status != DLADM_STATUS_OK) 484126Szf162725 return (-1); 494126Szf162725 504126Szf162725 wl_attrp = &attr.la_wlan_attr; 514126Szf162725 if ((attr.la_valid & DLADM_WLAN_LINKATTR_WLAN) == 0 || 524126Szf162725 (wl_attrp->wa_valid & DLADM_WLAN_ATTR_BSSID) == 0) 534126Szf162725 return (-1); 544126Szf162725 554126Szf162725 (void) memcpy(bssid, wl_attrp->wa_bssid.wb_bytes, DLADM_WLAN_BSSID_LEN); 564126Szf162725 574126Szf162725 wpa_printf(MSG_DEBUG, "wpa_driver_wifi_get_bssid: " MACSTR, 584126Szf162725 MAC2STR((unsigned char *)bssid)); 594126Szf162725 605895Syz147064 return (WPA_STATUS(status)); 614126Szf162725 } 624126Szf162725 634126Szf162725 /* 644126Szf162725 * get_ssid - get the current SSID 655895Syz147064 * @linkid: linkid of the given interface 664126Szf162725 * @ssid: buffer for SSID (at least 32 bytes) 674126Szf162725 * 684126Szf162725 * Returns: length of the SSID on success, -1 on failure 694126Szf162725 * 704126Szf162725 * Query kernel driver for the current SSID and copy it to @ssid. 714126Szf162725 * Returning zero is recommended if the STA is not associated. 724126Szf162725 */ 734126Szf162725 int 74*8453SAnurag.Maskey@Sun.COM wpa_driver_wifi_get_ssid(dladm_handle_t handle, datalink_id_t linkid, 75*8453SAnurag.Maskey@Sun.COM char *ssid) 764126Szf162725 { 774126Szf162725 int ret; 785895Syz147064 dladm_status_t status; 794126Szf162725 dladm_wlan_linkattr_t attr; 804126Szf162725 dladm_wlan_attr_t *wl_attrp; 814126Szf162725 82*8453SAnurag.Maskey@Sun.COM status = dladm_wlan_get_linkattr(handle, linkid, &attr); 835895Syz147064 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*8453SAnurag.Maskey@Sun.COM wpa_driver_wifi_set_wpa_ie(dladm_handle_t handle, datalink_id_t linkid, 102*8453SAnurag.Maskey@Sun.COM uint8_t *wpa_ie, uint32_t wpa_ie_len) 1034126Szf162725 { 1045895Syz147064 dladm_status_t status; 1054126Szf162725 1064126Szf162725 wpa_printf(MSG_DEBUG, "%s", "wpa_driver_wifi_set_wpa_ie"); 107*8453SAnurag.Maskey@Sun.COM status = dladm_wlan_wpa_set_ie(handle, linkid, wpa_ie, wpa_ie_len); 1084126Szf162725 1095895Syz147064 return (WPA_STATUS(status)); 1104126Szf162725 } 1114126Szf162725 1124126Szf162725 /* 1134126Szf162725 * set_wpa - enable/disable WPA support 1145895Syz147064 * @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*8453SAnurag.Maskey@Sun.COM wpa_driver_wifi_set_wpa(dladm_handle_t handle, datalink_id_t linkid, 127*8453SAnurag.Maskey@Sun.COM boolean_t enabled) 1284126Szf162725 { 1295895Syz147064 dladm_status_t status; 1304126Szf162725 1314126Szf162725 wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_wpa: enable=%d", enabled); 1324126Szf162725 133*8453SAnurag.Maskey@Sun.COM if (!enabled && wpa_driver_wifi_set_wpa_ie(handle, linkid, NULL, 0) < 0) 1344126Szf162725 return (-1); 1354126Szf162725 136*8453SAnurag.Maskey@Sun.COM status = dladm_wlan_wpa_set_wpa(handle, linkid, enabled); 1374126Szf162725 1385895Syz147064 return (WPA_STATUS(status)); 1394126Szf162725 } 1404126Szf162725 1414126Szf162725 static int 142*8453SAnurag.Maskey@Sun.COM wpa_driver_wifi_del_key(dladm_handle_t handle, datalink_id_t linkid, 143*8453SAnurag.Maskey@Sun.COM int key_idx, unsigned char *addr) 1444126Szf162725 { 1455895Syz147064 dladm_status_t status; 1464126Szf162725 dladm_wlan_bssid_t bss; 1474126Szf162725 1484126Szf162725 wpa_printf(MSG_DEBUG, "%s: id=%d", "wpa_driver_wifi_del_key", 1494126Szf162725 key_idx); 1504126Szf162725 1514126Szf162725 (void) memcpy(bss.wb_bytes, addr, DLADM_WLAN_BSSID_LEN); 152*8453SAnurag.Maskey@Sun.COM status = dladm_wlan_wpa_del_key(handle, linkid, key_idx, &bss); 1534126Szf162725 1545895Syz147064 return (WPA_STATUS(status)); 1554126Szf162725 } 1564126Szf162725 1574126Szf162725 /* 1584126Szf162725 * set_key - configure encryption key 1595895Syz147064 * @linkid: linkid of the given interface 1604126Szf162725 * @alg: encryption algorithm (%WPA_ALG_NONE, %WPA_ALG_WEP, 1614126Szf162725 * %WPA_ALG_TKIP, %WPA_ALG_CCMP); %WPA_ALG_NONE clears the key. 1624126Szf162725 * @addr: address of the peer STA or ff:ff:ff:ff:ff:ff for 1634126Szf162725 * broadcast/default keys 1644126Szf162725 * @key_idx: key index (0..3), always 0 for unicast keys 1654126Szf162725 * @set_tx: configure this key as the default Tx key (only used when 1664126Szf162725 * driver does not support separate unicast/individual key 1674126Szf162725 * @seq: sequence number/packet number, @seq_len octets, the next 1684126Szf162725 * packet number to be used for in replay protection; configured 1694126Szf162725 * for Rx keys (in most cases, this is only used with broadcast 1704126Szf162725 * keys and set to zero for unicast keys) 1714126Szf162725 * @seq_len: length of the @seq, depends on the algorithm: 1724126Szf162725 * TKIP: 6 octets, CCMP: 6 octets 1734126Szf162725 * @key: key buffer; TKIP: 16-byte temporal key, 8-byte Tx Mic key, 1744126Szf162725 * 8-byte Rx Mic Key 1754126Szf162725 * @key_len: length of the key buffer in octets (WEP: 5 or 13, 1764126Szf162725 * TKIP: 32, CCMP: 16) 1774126Szf162725 * 1784126Szf162725 * Returns: 0 on success, -1 on failure 1794126Szf162725 * 1804126Szf162725 * Configure the given key for the kernel driver. If the driver 1814126Szf162725 * supports separate individual keys (4 default keys + 1 individual), 1824126Szf162725 * @addr can be used to determine whether the key is default or 1834126Szf162725 * individual. If only 4 keys are supported, the default key with key 1844126Szf162725 * index 0 is used as the individual key. STA must be configured to use 1854126Szf162725 * it as the default Tx key (@set_tx is set) and accept Rx for all the 1864126Szf162725 * key indexes. In most cases, WPA uses only key indexes 1 and 2 for 1874126Szf162725 * broadcast keys, so key index 0 is available for this kind of 1884126Szf162725 * configuration. 1894126Szf162725 */ 1904126Szf162725 static int 191*8453SAnurag.Maskey@Sun.COM wpa_driver_wifi_set_key(dladm_handle_t handle, datalink_id_t linkid, 192*8453SAnurag.Maskey@Sun.COM wpa_alg alg, unsigned char *addr, int key_idx, boolean_t set_tx, 193*8453SAnurag.Maskey@Sun.COM uint8_t *seq, uint32_t seq_len, uint8_t *key, uint32_t key_len) 1944126Szf162725 { 1954126Szf162725 char *alg_name; 1964126Szf162725 dladm_wlan_cipher_t cipher; 1974126Szf162725 dladm_wlan_bssid_t bss; 1985895Syz147064 dladm_status_t status; 1994126Szf162725 2004126Szf162725 wpa_printf(MSG_DEBUG, "%s", "wpa_driver_wifi_set_key"); 2014126Szf162725 if (alg == WPA_ALG_NONE) 202*8453SAnurag.Maskey@Sun.COM return (wpa_driver_wifi_del_key(handle, linkid, key_idx, addr)); 2034126Szf162725 2044126Szf162725 switch (alg) { 2054126Szf162725 case WPA_ALG_WEP: 2064126Szf162725 alg_name = "WEP"; 2074126Szf162725 cipher = DLADM_WLAN_CIPHER_WEP; 2084126Szf162725 break; 2094126Szf162725 case WPA_ALG_TKIP: 2104126Szf162725 alg_name = "TKIP"; 2114126Szf162725 cipher = DLADM_WLAN_CIPHER_TKIP; 2124126Szf162725 break; 2134126Szf162725 case WPA_ALG_CCMP: 2144126Szf162725 alg_name = "CCMP"; 2154126Szf162725 cipher = DLADM_WLAN_CIPHER_AES_CCM; 2164126Szf162725 break; 2174126Szf162725 default: 2184126Szf162725 wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_key:" 2194126Szf162725 " unknown/unsupported algorithm %d", alg); 2204126Szf162725 return (-1); 2214126Szf162725 } 2224126Szf162725 2234126Szf162725 wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_key: alg=%s key_idx=%d" 2244126Szf162725 " set_tx=%d seq_len=%d seq=%d key_len=%d", 2254126Szf162725 alg_name, key_idx, set_tx, 2267249Sff224033 seq_len, *(uint64_t *)(uintptr_t)seq, key_len); 2274126Szf162725 2284126Szf162725 if (seq_len > sizeof (uint64_t)) { 2294126Szf162725 wpa_printf(MSG_DEBUG, "wpa_driver_wifi_set_key:" 2304126Szf162725 " seq_len %d too big", seq_len); 2314126Szf162725 return (-1); 2324126Szf162725 } 2334126Szf162725 (void) memcpy(bss.wb_bytes, addr, DLADM_WLAN_BSSID_LEN); 2344126Szf162725 235*8453SAnurag.Maskey@Sun.COM status = dladm_wlan_wpa_set_key(handle, linkid, cipher, &bss, set_tx, 2367249Sff224033 *(uint64_t *)(uintptr_t)seq, key_idx, key, key_len); 2374126Szf162725 2385895Syz147064 return (WPA_STATUS(status)); 2394126Szf162725 } 2404126Szf162725 2414126Szf162725 /* 2424126Szf162725 * disassociate - request driver to disassociate 2435895Syz147064 * @linkid: linkid of the given interface 2444126Szf162725 * @reason_code: 16-bit reason code to be sent in the disassociation 2454126Szf162725 * frame 2464126Szf162725 * 2474126Szf162725 * Return: 0 on success, -1 on failure 2484126Szf162725 */ 2494126Szf162725 static int 250*8453SAnurag.Maskey@Sun.COM wpa_driver_wifi_disassociate(dladm_handle_t handle, datalink_id_t linkid, 251*8453SAnurag.Maskey@Sun.COM int reason_code) 2524126Szf162725 { 2535895Syz147064 dladm_status_t status; 2544126Szf162725 2554126Szf162725 wpa_printf(MSG_DEBUG, "wpa_driver_wifi_disassociate"); 2564126Szf162725 257*8453SAnurag.Maskey@Sun.COM status = dladm_wlan_wpa_set_mlme(handle, linkid, 258*8453SAnurag.Maskey@Sun.COM DLADM_WLAN_MLME_DISASSOC, reason_code, NULL); 2594126Szf162725 2605895Syz147064 return (WPA_STATUS(status)); 2614126Szf162725 } 2624126Szf162725 2634126Szf162725 /* 2644126Szf162725 * associate - request driver to associate 2655895Syz147064 * @linkid: linkid of the given interface 2664126Szf162725 * @bssid: BSSID of the selected AP 2674126Szf162725 * @wpa_ie: WPA information element to be included in (Re)Association 2684126Szf162725 * Request (including information element id and length). Use of 2694126Szf162725 * this WPA IE is optional. If the driver generates the WPA IE, it 2704126Szf162725 * can use @pairwise_suite, @group_suite, and @key_mgmt_suite 2714126Szf162725 * to select proper algorithms. In this case, the driver has to 2724126Szf162725 * notify wpa_supplicant about the used WPA IE by generating an 2734126Szf162725 * event that the interface code will convert into EVENT_ASSOCINFO 2744126Szf162725 * data (see wpa_supplicant.h). When using WPA2/IEEE 802.11i, 2754126Szf162725 * @wpa_ie is used for RSN IE instead. The driver can determine 2764126Szf162725 * which version is used by looking at the first byte of the IE 2774126Szf162725 * (0xdd for WPA, 0x30 for WPA2/RSN). 2784126Szf162725 * @wpa_ie_len: length of the @wpa_ie 2794126Szf162725 * 2804126Szf162725 * Return: 0 on success, -1 on failure 2814126Szf162725 */ 2824126Szf162725 static int 283*8453SAnurag.Maskey@Sun.COM wpa_driver_wifi_associate(dladm_handle_t handle, datalink_id_t linkid, 284*8453SAnurag.Maskey@Sun.COM const char *bssid, uint8_t *wpa_ie, uint32_t wpa_ie_len) 2854126Szf162725 { 2865895Syz147064 dladm_status_t status; 2874126Szf162725 dladm_wlan_bssid_t bss; 2884126Szf162725 2894126Szf162725 wpa_printf(MSG_DEBUG, "wpa_driver_wifi_associate : " 2904126Szf162725 MACSTR, MAC2STR(bssid)); 2914126Szf162725 2924126Szf162725 /* 2934126Szf162725 * NB: Don't need to set the freq or cipher-related state as 2944126Szf162725 * this is implied by the bssid which is used to locate 2954126Szf162725 * the scanned node state which holds it. 2964126Szf162725 */ 297*8453SAnurag.Maskey@Sun.COM if (wpa_driver_wifi_set_wpa_ie(handle, linkid, wpa_ie, wpa_ie_len) < 0) 2984126Szf162725 return (-1); 2994126Szf162725 3004126Szf162725 (void) memcpy(bss.wb_bytes, bssid, DLADM_WLAN_BSSID_LEN); 301*8453SAnurag.Maskey@Sun.COM status = dladm_wlan_wpa_set_mlme(handle, linkid, DLADM_WLAN_MLME_ASSOC, 3024126Szf162725 0, &bss); 3034126Szf162725 3045895Syz147064 return (WPA_STATUS(status)); 3054126Szf162725 } 3064126Szf162725 3074126Szf162725 /* 3084126Szf162725 * scan - request the driver to initiate scan 3095895Syz147064 * @linkid: linkid of the given interface 3104126Szf162725 * 3114126Szf162725 * Return: 0 on success, -1 on failure 3124126Szf162725 * 3134126Szf162725 * Once the scan results are ready, the driver should report scan 3144126Szf162725 * results event for wpa_supplicant which will eventually request the 3154126Szf162725 * results with wpa_driver_get_scan_results(). 3164126Szf162725 */ 3174126Szf162725 static int 318*8453SAnurag.Maskey@Sun.COM wpa_driver_wifi_scan(dladm_handle_t handle, datalink_id_t linkid) 3194126Szf162725 { 3205895Syz147064 dladm_status_t status; 3214126Szf162725 3224126Szf162725 wpa_printf(MSG_DEBUG, "%s", "wpa_driver_wifi_scan"); 3234126Szf162725 /* 3244126Szf162725 * We force the state to INIT before calling ieee80211_new_state 3254126Szf162725 * to get ieee80211_begin_scan called. We really want to scan w/o 3264126Szf162725 * altering the current state but that's not possible right now. 3274126Szf162725 */ 328*8453SAnurag.Maskey@Sun.COM (void) wpa_driver_wifi_disassociate(handle, linkid, 3294126Szf162725 DLADM_WLAN_REASON_DISASSOC_LEAVING); 3304126Szf162725 331*8453SAnurag.Maskey@Sun.COM status = dladm_wlan_scan(handle, linkid, NULL, NULL); 3324126Szf162725 3334126Szf162725 wpa_printf(MSG_DEBUG, "%s: return", "wpa_driver_wifi_scan"); 3345895Syz147064 return (WPA_STATUS(status)); 3354126Szf162725 } 3364126Szf162725 3374126Szf162725 /* 3384126Szf162725 * get_scan_results - fetch the latest scan results 3395895Syz147064 * @linkid: linkid of the given interface 3404126Szf162725 * @results: pointer to buffer for scan results 3414126Szf162725 * @max_size: maximum number of entries (buffer size) 3424126Szf162725 * 3434126Szf162725 * Return: number of scan result entries used on success, -1 on failure 3444126Szf162725 * 3454126Szf162725 * If scan results include more than @max_size BSSes, @max_size will be 3464126Szf162725 * returned and the remaining entries will not be included in the 3474126Szf162725 * buffer. 3484126Szf162725 */ 3494126Szf162725 int 350*8453SAnurag.Maskey@Sun.COM wpa_driver_wifi_get_scan_results(dladm_handle_t handle, datalink_id_t linkid, 3514126Szf162725 dladm_wlan_ess_t *results, uint32_t max_size) 3524126Szf162725 { 3534126Szf162725 uint_t ret; 3544126Szf162725 3555895Syz147064 wpa_printf(MSG_DEBUG, "%s: max size=%d\n", 3565895Syz147064 "wpa_driver_wifi_get_scan_results", max_size); 3574126Szf162725 358*8453SAnurag.Maskey@Sun.COM if (dladm_wlan_wpa_get_sr(handle, linkid, results, max_size, &ret) 3594126Szf162725 != DLADM_STATUS_OK) { 3604126Szf162725 return (-1); 3614126Szf162725 } 3624126Szf162725 3634126Szf162725 return (ret); 3644126Szf162725 } 3654126Szf162725 3664126Szf162725 struct wpa_driver_ops wpa_driver_wifi_ops = { 3674126Szf162725 wpa_driver_wifi_get_bssid, 3684126Szf162725 wpa_driver_wifi_get_ssid, 3694126Szf162725 wpa_driver_wifi_set_wpa, 3704126Szf162725 wpa_driver_wifi_set_key, 3714126Szf162725 wpa_driver_wifi_scan, 3724126Szf162725 wpa_driver_wifi_get_scan_results, 3734126Szf162725 wpa_driver_wifi_disassociate, 3744126Szf162725 wpa_driver_wifi_associate 3754126Szf162725 }; 376