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