Home | History | Annotate | Line # | Download | only in wpa_supplicant
ibss_rsn.c revision 1.1.1.8.8.1
      1          1.1  christos /*
      2          1.1  christos  * wpa_supplicant - IBSS RSN
      3      1.1.1.4  christos  * Copyright (c) 2009-2013, Jouni Malinen <j (at) w1.fi>
      4          1.1  christos  *
      5      1.1.1.3  christos  * This software may be distributed under the terms of the BSD license.
      6      1.1.1.3  christos  * See README for more details.
      7          1.1  christos  */
      8          1.1  christos 
      9          1.1  christos #include "includes.h"
     10          1.1  christos 
     11          1.1  christos #include "common.h"
     12      1.1.1.4  christos #include "common/wpa_ctrl.h"
     13      1.1.1.4  christos #include "utils/eloop.h"
     14          1.1  christos #include "l2_packet/l2_packet.h"
     15          1.1  christos #include "rsn_supp/wpa.h"
     16          1.1  christos #include "rsn_supp/wpa_ie.h"
     17          1.1  christos #include "ap/wpa_auth.h"
     18          1.1  christos #include "wpa_supplicant_i.h"
     19          1.1  christos #include "driver_i.h"
     20      1.1.1.4  christos #include "common/ieee802_11_defs.h"
     21          1.1  christos #include "ibss_rsn.h"
     22          1.1  christos 
     23          1.1  christos 
     24      1.1.1.4  christos static void ibss_rsn_auth_timeout(void *eloop_ctx, void *timeout_ctx);
     25      1.1.1.4  christos 
     26      1.1.1.4  christos 
     27      1.1.1.3  christos static struct ibss_rsn_peer * ibss_rsn_get_peer(struct ibss_rsn *ibss_rsn,
     28      1.1.1.3  christos 						const u8 *addr)
     29      1.1.1.3  christos {
     30      1.1.1.3  christos 	struct ibss_rsn_peer *peer;
     31      1.1.1.3  christos 
     32      1.1.1.3  christos 	for (peer = ibss_rsn->peers; peer; peer = peer->next)
     33  1.1.1.8.8.1  perseant 		if (ether_addr_equal(addr, peer->addr))
     34      1.1.1.3  christos 			break;
     35      1.1.1.3  christos 	return peer;
     36      1.1.1.3  christos }
     37      1.1.1.3  christos 
     38      1.1.1.3  christos 
     39          1.1  christos static void ibss_rsn_free(struct ibss_rsn_peer *peer)
     40          1.1  christos {
     41      1.1.1.4  christos 	eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
     42          1.1  christos 	wpa_auth_sta_deinit(peer->auth);
     43          1.1  christos 	wpa_sm_deinit(peer->supp);
     44          1.1  christos 	os_free(peer);
     45          1.1  christos }
     46          1.1  christos 
     47          1.1  christos 
     48          1.1  christos static void supp_set_state(void *ctx, enum wpa_states state)
     49          1.1  christos {
     50          1.1  christos 	struct ibss_rsn_peer *peer = ctx;
     51          1.1  christos 	peer->supp_state = state;
     52          1.1  christos }
     53          1.1  christos 
     54          1.1  christos 
     55      1.1.1.2  christos static enum wpa_states supp_get_state(void *ctx)
     56      1.1.1.2  christos {
     57      1.1.1.2  christos 	struct ibss_rsn_peer *peer = ctx;
     58      1.1.1.2  christos 	return peer->supp_state;
     59      1.1.1.2  christos }
     60      1.1.1.2  christos 
     61      1.1.1.2  christos 
     62          1.1  christos static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
     63          1.1  christos 			   size_t len)
     64          1.1  christos {
     65          1.1  christos 	struct ibss_rsn_peer *peer = ctx;
     66          1.1  christos 	struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
     67  1.1.1.8.8.1  perseant 	int encrypt = peer->authentication_status & IBSS_RSN_REPORTED_PTK;
     68          1.1  christos 
     69  1.1.1.8.8.1  perseant 	wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR
     70  1.1.1.8.8.1  perseant 		   " proto=0x%04x len=%lu no_encrypt=%d)",
     71  1.1.1.8.8.1  perseant 		   __func__, MAC2STR(dest), proto, (unsigned long) len,
     72  1.1.1.8.8.1  perseant 		   !encrypt);
     73  1.1.1.8.8.1  perseant 
     74  1.1.1.8.8.1  perseant 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_CONTROL_PORT)
     75  1.1.1.8.8.1  perseant 		return wpa_drv_tx_control_port(wpa_s, dest, proto, buf, len,
     76  1.1.1.8.8.1  perseant 					       !encrypt);
     77          1.1  christos 
     78          1.1  christos 	if (wpa_s->l2)
     79          1.1  christos 		return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
     80          1.1  christos 
     81      1.1.1.5  christos 	return -1;
     82          1.1  christos }
     83          1.1  christos 
     84          1.1  christos 
     85          1.1  christos static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
     86          1.1  christos 			     u16 data_len, size_t *msg_len, void **data_pos)
     87          1.1  christos {
     88          1.1  christos 	struct ieee802_1x_hdr *hdr;
     89          1.1  christos 
     90          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
     91          1.1  christos 		   __func__, type, data_len);
     92          1.1  christos 
     93          1.1  christos 	*msg_len = sizeof(*hdr) + data_len;
     94          1.1  christos 	hdr = os_malloc(*msg_len);
     95          1.1  christos 	if (hdr == NULL)
     96          1.1  christos 		return NULL;
     97          1.1  christos 
     98          1.1  christos 	hdr->version = 2;
     99          1.1  christos 	hdr->type = type;
    100          1.1  christos 	hdr->length = host_to_be16(data_len);
    101          1.1  christos 
    102          1.1  christos 	if (data)
    103          1.1  christos 		os_memcpy(hdr + 1, data, data_len);
    104          1.1  christos 	else
    105          1.1  christos 		os_memset(hdr + 1, 0, data_len);
    106          1.1  christos 
    107          1.1  christos 	if (data_pos)
    108          1.1  christos 		*data_pos = hdr + 1;
    109          1.1  christos 
    110          1.1  christos 	return (u8 *) hdr;
    111          1.1  christos }
    112          1.1  christos 
    113          1.1  christos 
    114          1.1  christos static int supp_get_beacon_ie(void *ctx)
    115          1.1  christos {
    116          1.1  christos 	struct ibss_rsn_peer *peer = ctx;
    117          1.1  christos 
    118          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
    119          1.1  christos 	/* TODO: get correct RSN IE */
    120  1.1.1.8.8.1  perseant 	wpa_sm_set_ap_rsnxe(peer->supp, NULL, 0);
    121          1.1  christos 	return wpa_sm_set_ap_rsn_ie(peer->supp,
    122          1.1  christos 				    (u8 *) "\x30\x14\x01\x00"
    123          1.1  christos 				    "\x00\x0f\xac\x04"
    124          1.1  christos 				    "\x01\x00\x00\x0f\xac\x04"
    125          1.1  christos 				    "\x01\x00\x00\x0f\xac\x02"
    126          1.1  christos 				    "\x00\x00", 22);
    127          1.1  christos }
    128          1.1  christos 
    129          1.1  christos 
    130      1.1.1.4  christos static void ibss_check_rsn_completed(struct ibss_rsn_peer *peer)
    131      1.1.1.4  christos {
    132      1.1.1.4  christos 	struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
    133      1.1.1.4  christos 
    134      1.1.1.4  christos 	if ((peer->authentication_status &
    135      1.1.1.4  christos 	     (IBSS_RSN_SET_PTK_SUPP | IBSS_RSN_SET_PTK_AUTH)) !=
    136      1.1.1.4  christos 	    (IBSS_RSN_SET_PTK_SUPP | IBSS_RSN_SET_PTK_AUTH))
    137      1.1.1.4  christos 		return;
    138      1.1.1.4  christos 	if (peer->authentication_status & IBSS_RSN_REPORTED_PTK)
    139      1.1.1.4  christos 		return;
    140      1.1.1.4  christos 	peer->authentication_status |= IBSS_RSN_REPORTED_PTK;
    141      1.1.1.4  christos 	wpa_msg(wpa_s, MSG_INFO, IBSS_RSN_COMPLETED MACSTR,
    142      1.1.1.4  christos 		MAC2STR(peer->addr));
    143      1.1.1.4  christos }
    144      1.1.1.4  christos 
    145      1.1.1.4  christos 
    146  1.1.1.8.8.1  perseant static int supp_set_key(void *ctx, int link_id, enum wpa_alg alg,
    147          1.1  christos 			const u8 *addr, int key_idx, int set_tx,
    148          1.1  christos 			const u8 *seq, size_t seq_len,
    149  1.1.1.8.8.1  perseant 			const u8 *key, size_t key_len, enum key_flag key_flag)
    150          1.1  christos {
    151          1.1  christos 	struct ibss_rsn_peer *peer = ctx;
    152          1.1  christos 
    153          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
    154          1.1  christos 		   "set_tx=%d)",
    155          1.1  christos 		   __func__, alg, MAC2STR(addr), key_idx, set_tx);
    156          1.1  christos 	wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
    157          1.1  christos 	wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
    158          1.1  christos 
    159          1.1  christos 	if (key_idx == 0) {
    160      1.1.1.4  christos 		peer->authentication_status |= IBSS_RSN_SET_PTK_SUPP;
    161      1.1.1.4  christos 		ibss_check_rsn_completed(peer);
    162          1.1  christos 		/*
    163          1.1  christos 		 * In IBSS RSN, the pairwise key from the 4-way handshake
    164          1.1  christos 		 * initiated by the peer with highest MAC address is used.
    165          1.1  christos 		 */
    166          1.1  christos 		if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
    167          1.1  christos 			      ETH_ALEN) > 0) {
    168          1.1  christos 			wpa_printf(MSG_DEBUG, "SUPP: Do not use this PTK");
    169          1.1  christos 			return 0;
    170          1.1  christos 		}
    171          1.1  christos 	}
    172          1.1  christos 
    173      1.1.1.2  christos 	if (is_broadcast_ether_addr(addr))
    174      1.1.1.2  christos 		addr = peer->addr;
    175  1.1.1.8.8.1  perseant 	return wpa_drv_set_key(peer->ibss_rsn->wpa_s, link_id, alg, addr,
    176  1.1.1.8.8.1  perseant 			       key_idx, set_tx, seq, seq_len, key, key_len,
    177  1.1.1.8.8.1  perseant 			       key_flag);
    178          1.1  christos }
    179          1.1  christos 
    180          1.1  christos 
    181          1.1  christos static void * supp_get_network_ctx(void *ctx)
    182          1.1  christos {
    183          1.1  christos 	struct ibss_rsn_peer *peer = ctx;
    184          1.1  christos 	return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
    185          1.1  christos }
    186          1.1  christos 
    187          1.1  christos 
    188          1.1  christos static int supp_mlme_setprotection(void *ctx, const u8 *addr,
    189          1.1  christos 				   int protection_type, int key_type)
    190          1.1  christos {
    191          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
    192          1.1  christos 		   "key_type=%d)",
    193          1.1  christos 		   __func__, MAC2STR(addr), protection_type, key_type);
    194          1.1  christos 	return 0;
    195          1.1  christos }
    196          1.1  christos 
    197          1.1  christos 
    198          1.1  christos static void supp_cancel_auth_timeout(void *ctx)
    199          1.1  christos {
    200          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
    201          1.1  christos }
    202          1.1  christos 
    203          1.1  christos 
    204  1.1.1.8.8.1  perseant static void supp_deauthenticate(void *ctx, u16 reason_code)
    205  1.1.1.8.8.1  perseant {
    206  1.1.1.8.8.1  perseant 	wpa_printf(MSG_DEBUG, "SUPP: %s (TODO)", __func__);
    207  1.1.1.8.8.1  perseant }
    208  1.1.1.8.8.1  perseant 
    209  1.1.1.8.8.1  perseant 
    210  1.1.1.8.8.1  perseant static void supp_reconnect(void *ctx)
    211      1.1.1.2  christos {
    212      1.1.1.2  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s (TODO)", __func__);
    213      1.1.1.2  christos }
    214      1.1.1.2  christos 
    215      1.1.1.2  christos 
    216      1.1.1.3  christos static int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
    217      1.1.1.3  christos 			      const u8 *psk)
    218          1.1  christos {
    219          1.1  christos 	struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
    220          1.1  christos 	if (ctx == NULL)
    221          1.1  christos 		return -1;
    222          1.1  christos 
    223          1.1  christos 	ctx->ctx = peer;
    224          1.1  christos 	ctx->msg_ctx = peer->ibss_rsn->wpa_s;
    225          1.1  christos 	ctx->set_state = supp_set_state;
    226      1.1.1.2  christos 	ctx->get_state = supp_get_state;
    227          1.1  christos 	ctx->ether_send = supp_ether_send;
    228          1.1  christos 	ctx->get_beacon_ie = supp_get_beacon_ie;
    229          1.1  christos 	ctx->alloc_eapol = supp_alloc_eapol;
    230          1.1  christos 	ctx->set_key = supp_set_key;
    231          1.1  christos 	ctx->get_network_ctx = supp_get_network_ctx;
    232          1.1  christos 	ctx->mlme_setprotection = supp_mlme_setprotection;
    233          1.1  christos 	ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
    234      1.1.1.2  christos 	ctx->deauthenticate = supp_deauthenticate;
    235  1.1.1.8.8.1  perseant 	ctx->reconnect = supp_reconnect;
    236          1.1  christos 	peer->supp = wpa_sm_init(ctx);
    237          1.1  christos 	if (peer->supp == NULL) {
    238          1.1  christos 		wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
    239      1.1.1.6  christos 		os_free(ctx);
    240          1.1  christos 		return -1;
    241          1.1  christos 	}
    242          1.1  christos 
    243          1.1  christos 	wpa_sm_set_own_addr(peer->supp, own_addr);
    244          1.1  christos 	wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
    245          1.1  christos 	wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
    246          1.1  christos 	wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
    247          1.1  christos 	wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
    248          1.1  christos 	wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
    249      1.1.1.6  christos 	wpa_sm_set_pmk(peer->supp, psk, PMK_LEN, NULL, NULL);
    250          1.1  christos 
    251          1.1  christos 	peer->supp_ie_len = sizeof(peer->supp_ie);
    252          1.1  christos 	if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
    253          1.1  christos 					    &peer->supp_ie_len) < 0) {
    254          1.1  christos 		wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
    255          1.1  christos 			   " failed");
    256          1.1  christos 		return -1;
    257          1.1  christos 	}
    258          1.1  christos 
    259          1.1  christos 	wpa_sm_notify_assoc(peer->supp, peer->addr);
    260          1.1  christos 
    261          1.1  christos 	return 0;
    262          1.1  christos }
    263          1.1  christos 
    264          1.1  christos 
    265          1.1  christos static void auth_logger(void *ctx, const u8 *addr, logger_level level,
    266          1.1  christos 			const char *txt)
    267          1.1  christos {
    268          1.1  christos 	if (addr)
    269          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
    270          1.1  christos 			   MAC2STR(addr), txt);
    271          1.1  christos 	else
    272          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
    273          1.1  christos }
    274          1.1  christos 
    275          1.1  christos 
    276      1.1.1.4  christos static const u8 * auth_get_psk(void *ctx, const u8 *addr,
    277      1.1.1.7  christos 			       const u8 *p2p_dev_addr, const u8 *prev_psk,
    278      1.1.1.8  christos 			       size_t *psk_len, int *vlan_id)
    279          1.1  christos {
    280          1.1  christos 	struct ibss_rsn *ibss_rsn = ctx;
    281      1.1.1.7  christos 
    282      1.1.1.7  christos 	if (psk_len)
    283      1.1.1.7  christos 		*psk_len = PMK_LEN;
    284      1.1.1.8  christos 	if (vlan_id)
    285      1.1.1.8  christos 		*vlan_id = 0;
    286          1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
    287          1.1  christos 		   __func__, MAC2STR(addr), prev_psk);
    288          1.1  christos 	if (prev_psk)
    289          1.1  christos 		return NULL;
    290          1.1  christos 	return ibss_rsn->psk;
    291          1.1  christos }
    292          1.1  christos 
    293          1.1  christos 
    294          1.1  christos static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
    295          1.1  christos 			   size_t data_len, int encrypt)
    296          1.1  christos {
    297          1.1  christos 	struct ibss_rsn *ibss_rsn = ctx;
    298          1.1  christos 	struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
    299          1.1  christos 
    300          1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
    301          1.1  christos 		   "encrypt=%d)",
    302          1.1  christos 		   __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
    303          1.1  christos 
    304  1.1.1.8.8.1  perseant 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_CONTROL_PORT)
    305  1.1.1.8.8.1  perseant 		return wpa_drv_tx_control_port(wpa_s, addr, ETH_P_EAPOL,
    306  1.1.1.8.8.1  perseant 					       data, data_len, !encrypt);
    307  1.1.1.8.8.1  perseant 
    308          1.1  christos 	if (wpa_s->l2)
    309          1.1  christos 		return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
    310          1.1  christos 				      data_len);
    311          1.1  christos 
    312      1.1.1.5  christos 	return -1;
    313          1.1  christos }
    314          1.1  christos 
    315          1.1  christos 
    316          1.1  christos static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
    317  1.1.1.8.8.1  perseant 			const u8 *addr, int idx, u8 *key, size_t key_len,
    318  1.1.1.8.8.1  perseant 			enum key_flag key_flag)
    319          1.1  christos {
    320          1.1  christos 	struct ibss_rsn *ibss_rsn = ctx;
    321          1.1  christos 	u8 seq[6];
    322          1.1  christos 
    323          1.1  christos 	os_memset(seq, 0, sizeof(seq));
    324          1.1  christos 
    325          1.1  christos 	if (addr) {
    326          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
    327          1.1  christos 			   " key_idx=%d)",
    328          1.1  christos 			   __func__, alg, MAC2STR(addr), idx);
    329          1.1  christos 	} else {
    330          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
    331          1.1  christos 			   __func__, alg, idx);
    332          1.1  christos 	}
    333          1.1  christos 	wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
    334          1.1  christos 
    335          1.1  christos 	if (idx == 0) {
    336      1.1.1.4  christos 		if (addr) {
    337      1.1.1.4  christos 			struct ibss_rsn_peer *peer;
    338      1.1.1.4  christos 			peer = ibss_rsn_get_peer(ibss_rsn, addr);
    339      1.1.1.4  christos 			if (peer) {
    340      1.1.1.4  christos 				peer->authentication_status |=
    341      1.1.1.4  christos 					IBSS_RSN_SET_PTK_AUTH;
    342      1.1.1.4  christos 				ibss_check_rsn_completed(peer);
    343      1.1.1.4  christos 			}
    344      1.1.1.4  christos 		}
    345          1.1  christos 		/*
    346          1.1  christos 		 * In IBSS RSN, the pairwise key from the 4-way handshake
    347          1.1  christos 		 * initiated by the peer with highest MAC address is used.
    348          1.1  christos 		 */
    349          1.1  christos 		if (addr == NULL ||
    350          1.1  christos 		    os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
    351          1.1  christos 			wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
    352          1.1  christos 			return 0;
    353          1.1  christos 		}
    354          1.1  christos 	}
    355          1.1  christos 
    356  1.1.1.8.8.1  perseant 	return wpa_drv_set_key(ibss_rsn->wpa_s, -1, alg, addr, idx,
    357  1.1.1.8.8.1  perseant 			       1, seq, 6, key, key_len, key_flag);
    358          1.1  christos }
    359          1.1  christos 
    360          1.1  christos 
    361      1.1.1.4  christos static void ibss_rsn_disconnect(void *ctx, const u8 *addr, u16 reason)
    362      1.1.1.4  christos {
    363      1.1.1.4  christos 	struct ibss_rsn *ibss_rsn = ctx;
    364      1.1.1.4  christos 	wpa_drv_sta_deauth(ibss_rsn->wpa_s, addr, reason);
    365      1.1.1.4  christos }
    366      1.1.1.4  christos 
    367      1.1.1.4  christos 
    368      1.1.1.2  christos static int auth_for_each_sta(void *ctx, int (*cb)(struct wpa_state_machine *sm,
    369      1.1.1.2  christos 						  void *ctx),
    370      1.1.1.2  christos 			     void *cb_ctx)
    371      1.1.1.2  christos {
    372      1.1.1.2  christos 	struct ibss_rsn *ibss_rsn = ctx;
    373      1.1.1.2  christos 	struct ibss_rsn_peer *peer;
    374      1.1.1.2  christos 
    375      1.1.1.2  christos 	wpa_printf(MSG_DEBUG, "AUTH: for_each_sta");
    376      1.1.1.2  christos 
    377      1.1.1.2  christos 	for (peer = ibss_rsn->peers; peer; peer = peer->next) {
    378      1.1.1.2  christos 		if (peer->auth && cb(peer->auth, cb_ctx))
    379      1.1.1.2  christos 			return 1;
    380      1.1.1.2  christos 	}
    381      1.1.1.2  christos 
    382      1.1.1.2  christos 	return 0;
    383      1.1.1.2  christos }
    384      1.1.1.2  christos 
    385      1.1.1.2  christos 
    386      1.1.1.3  christos static void ibss_set_sta_authorized(struct ibss_rsn *ibss_rsn,
    387      1.1.1.3  christos 				    struct ibss_rsn_peer *peer, int authorized)
    388      1.1.1.3  christos {
    389      1.1.1.3  christos 	int res;
    390      1.1.1.3  christos 
    391      1.1.1.3  christos 	if (authorized) {
    392      1.1.1.3  christos 		res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
    393      1.1.1.3  christos 					    WPA_STA_AUTHORIZED,
    394      1.1.1.3  christos 					    WPA_STA_AUTHORIZED, ~0);
    395      1.1.1.3  christos 		wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " authorizing port",
    396      1.1.1.3  christos 			   MAC2STR(peer->addr));
    397      1.1.1.3  christos 	} else {
    398      1.1.1.3  christos 		res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
    399      1.1.1.3  christos 					    0, 0, ~WPA_STA_AUTHORIZED);
    400      1.1.1.3  christos 		wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " unauthorizing port",
    401      1.1.1.3  christos 			   MAC2STR(peer->addr));
    402      1.1.1.3  christos 	}
    403      1.1.1.3  christos 
    404      1.1.1.3  christos 	if (res && errno != ENOENT) {
    405      1.1.1.3  christos 		wpa_printf(MSG_DEBUG, "Could not set station " MACSTR " flags "
    406      1.1.1.3  christos 			   "for kernel driver (errno=%d)",
    407      1.1.1.3  christos 			   MAC2STR(peer->addr), errno);
    408      1.1.1.3  christos 	}
    409      1.1.1.3  christos }
    410      1.1.1.3  christos 
    411      1.1.1.3  christos 
    412      1.1.1.3  christos static void auth_set_eapol(void *ctx, const u8 *addr,
    413      1.1.1.3  christos 				       wpa_eapol_variable var, int value)
    414      1.1.1.3  christos {
    415      1.1.1.3  christos 	struct ibss_rsn *ibss_rsn = ctx;
    416      1.1.1.3  christos 	struct ibss_rsn_peer *peer = ibss_rsn_get_peer(ibss_rsn, addr);
    417      1.1.1.3  christos 
    418      1.1.1.3  christos 	if (peer == NULL)
    419      1.1.1.3  christos 		return;
    420      1.1.1.3  christos 
    421      1.1.1.3  christos 	switch (var) {
    422      1.1.1.3  christos 	case WPA_EAPOL_authorized:
    423      1.1.1.3  christos 		ibss_set_sta_authorized(ibss_rsn, peer, value);
    424      1.1.1.3  christos 		break;
    425      1.1.1.3  christos 	default:
    426      1.1.1.3  christos 		/* do not handle any other event */
    427      1.1.1.3  christos 		wpa_printf(MSG_DEBUG, "AUTH: eapol event not handled %d", var);
    428      1.1.1.3  christos 		break;
    429      1.1.1.3  christos 	}
    430      1.1.1.3  christos }
    431      1.1.1.3  christos 
    432      1.1.1.3  christos 
    433          1.1  christos static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
    434      1.1.1.6  christos 				    const u8 *own_addr, struct wpa_ssid *ssid)
    435          1.1  christos {
    436          1.1  christos 	struct wpa_auth_config conf;
    437      1.1.1.7  christos 	static const struct wpa_auth_callbacks cb = {
    438      1.1.1.7  christos 		.logger = auth_logger,
    439      1.1.1.7  christos 		.set_eapol = auth_set_eapol,
    440      1.1.1.7  christos 		.send_eapol = auth_send_eapol,
    441      1.1.1.7  christos 		.get_psk = auth_get_psk,
    442      1.1.1.7  christos 		.set_key = auth_set_key,
    443      1.1.1.7  christos 		.for_each_sta = auth_for_each_sta,
    444      1.1.1.7  christos 		.disconnect = ibss_rsn_disconnect,
    445      1.1.1.7  christos 	};
    446          1.1  christos 
    447          1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
    448          1.1  christos 
    449          1.1  christos 	os_memset(&conf, 0, sizeof(conf));
    450          1.1  christos 	conf.wpa = 2;
    451          1.1  christos 	conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
    452          1.1  christos 	conf.wpa_pairwise = WPA_CIPHER_CCMP;
    453          1.1  christos 	conf.rsn_pairwise = WPA_CIPHER_CCMP;
    454          1.1  christos 	conf.wpa_group = WPA_CIPHER_CCMP;
    455          1.1  christos 	conf.eapol_version = 2;
    456      1.1.1.6  christos 	conf.wpa_group_rekey = ssid->group_rekey ? ssid->group_rekey : 600;
    457      1.1.1.7  christos 	conf.wpa_group_update_count = 4;
    458      1.1.1.7  christos 	conf.wpa_pairwise_update_count = 4;
    459          1.1  christos 
    460      1.1.1.7  christos 	ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb, ibss_rsn);
    461          1.1  christos 	if (ibss_rsn->auth_group == NULL) {
    462          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
    463          1.1  christos 		return -1;
    464          1.1  christos 	}
    465          1.1  christos 
    466      1.1.1.2  christos 	wpa_init_keys(ibss_rsn->auth_group);
    467      1.1.1.2  christos 
    468          1.1  christos 	return 0;
    469          1.1  christos }
    470          1.1  christos 
    471          1.1  christos 
    472          1.1  christos static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
    473          1.1  christos 			      struct ibss_rsn_peer *peer)
    474          1.1  christos {
    475      1.1.1.4  christos 	peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr, NULL);
    476          1.1  christos 	if (peer->auth == NULL) {
    477          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
    478          1.1  christos 		return -1;
    479          1.1  christos 	}
    480          1.1  christos 
    481          1.1  christos 	/* TODO: get peer RSN IE with Probe Request */
    482      1.1.1.8  christos 	if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth, 0,
    483          1.1  christos 				(u8 *) "\x30\x14\x01\x00"
    484          1.1  christos 				"\x00\x0f\xac\x04"
    485          1.1  christos 				"\x01\x00\x00\x0f\xac\x04"
    486          1.1  christos 				"\x01\x00\x00\x0f\xac\x02"
    487  1.1.1.8.8.1  perseant 				"\x00\x00", 22, NULL, 0, NULL, 0, NULL, 0,
    488  1.1.1.8.8.1  perseant 				NULL) != WPA_IE_OK) {
    489          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
    490          1.1  christos 		return -1;
    491          1.1  christos 	}
    492          1.1  christos 
    493          1.1  christos 	if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
    494          1.1  christos 		return -1;
    495          1.1  christos 
    496          1.1  christos 	if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
    497          1.1  christos 		return -1;
    498          1.1  christos 
    499          1.1  christos 	return 0;
    500          1.1  christos }
    501          1.1  christos 
    502          1.1  christos 
    503      1.1.1.4  christos static int ibss_rsn_send_auth(struct ibss_rsn *ibss_rsn, const u8 *da, int seq)
    504          1.1  christos {
    505      1.1.1.4  christos 	struct ieee80211_mgmt auth;
    506      1.1.1.4  christos 	const size_t auth_length = IEEE80211_HDRLEN + sizeof(auth.u.auth);
    507      1.1.1.4  christos 	struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
    508          1.1  christos 
    509      1.1.1.4  christos 	os_memset(&auth, 0, sizeof(auth));
    510      1.1.1.4  christos 
    511      1.1.1.4  christos 	auth.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    512      1.1.1.4  christos 					  WLAN_FC_STYPE_AUTH);
    513      1.1.1.4  christos 	os_memcpy(auth.da, da, ETH_ALEN);
    514      1.1.1.4  christos 	os_memcpy(auth.sa, wpa_s->own_addr, ETH_ALEN);
    515      1.1.1.4  christos 	os_memcpy(auth.bssid, wpa_s->bssid, ETH_ALEN);
    516      1.1.1.4  christos 
    517      1.1.1.4  christos 	auth.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
    518      1.1.1.4  christos 	auth.u.auth.auth_transaction = host_to_le16(seq);
    519      1.1.1.4  christos 	auth.u.auth.status_code = host_to_le16(WLAN_STATUS_SUCCESS);
    520      1.1.1.4  christos 
    521      1.1.1.4  christos 	wpa_printf(MSG_DEBUG, "RSN: IBSS TX Auth frame (SEQ %d) to " MACSTR,
    522      1.1.1.4  christos 		   seq, MAC2STR(da));
    523      1.1.1.4  christos 
    524  1.1.1.8.8.1  perseant 	return wpa_drv_send_mlme(wpa_s, (u8 *) &auth, auth_length, 0, 0, 0);
    525      1.1.1.4  christos }
    526      1.1.1.4  christos 
    527      1.1.1.4  christos 
    528      1.1.1.4  christos static int ibss_rsn_is_auth_started(struct ibss_rsn_peer * peer)
    529      1.1.1.4  christos {
    530      1.1.1.4  christos 	return peer->authentication_status &
    531      1.1.1.4  christos 	       (IBSS_RSN_AUTH_BY_US | IBSS_RSN_AUTH_EAPOL_BY_US);
    532      1.1.1.4  christos }
    533      1.1.1.4  christos 
    534      1.1.1.4  christos 
    535      1.1.1.4  christos static struct ibss_rsn_peer *
    536      1.1.1.4  christos ibss_rsn_peer_init(struct ibss_rsn *ibss_rsn, const u8 *addr)
    537      1.1.1.4  christos {
    538      1.1.1.4  christos 	struct ibss_rsn_peer *peer;
    539      1.1.1.4  christos 	if (ibss_rsn == NULL)
    540      1.1.1.4  christos 		return NULL;
    541      1.1.1.4  christos 
    542      1.1.1.4  christos 	peer = ibss_rsn_get_peer(ibss_rsn, addr);
    543      1.1.1.4  christos 	if (peer) {
    544      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Supplicant for peer "MACSTR
    545      1.1.1.4  christos 			   " already running", MAC2STR(addr));
    546      1.1.1.4  christos 		return peer;
    547      1.1.1.2  christos 	}
    548      1.1.1.2  christos 
    549      1.1.1.4  christos 	wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Supplicant for peer "MACSTR,
    550      1.1.1.4  christos 		   MAC2STR(addr));
    551          1.1  christos 
    552          1.1  christos 	peer = os_zalloc(sizeof(*peer));
    553      1.1.1.4  christos 	if (peer == NULL) {
    554      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: Could not allocate memory.");
    555      1.1.1.4  christos 		return NULL;
    556      1.1.1.4  christos 	}
    557          1.1  christos 
    558          1.1  christos 	peer->ibss_rsn = ibss_rsn;
    559          1.1  christos 	os_memcpy(peer->addr, addr, ETH_ALEN);
    560      1.1.1.4  christos 	peer->authentication_status = IBSS_RSN_AUTH_NOT_AUTHENTICATED;
    561          1.1  christos 
    562      1.1.1.4  christos 	if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr,
    563      1.1.1.4  christos 			       ibss_rsn->psk) < 0) {
    564          1.1  christos 		ibss_rsn_free(peer);
    565      1.1.1.4  christos 		return NULL;
    566      1.1.1.4  christos 	}
    567      1.1.1.4  christos 
    568      1.1.1.4  christos 	peer->next = ibss_rsn->peers;
    569      1.1.1.4  christos 	ibss_rsn->peers = peer;
    570      1.1.1.4  christos 
    571      1.1.1.4  christos 	return peer;
    572      1.1.1.4  christos }
    573      1.1.1.4  christos 
    574      1.1.1.4  christos 
    575      1.1.1.4  christos static void ibss_rsn_auth_timeout(void *eloop_ctx, void *timeout_ctx)
    576      1.1.1.4  christos {
    577      1.1.1.4  christos 	struct ibss_rsn_peer *peer = eloop_ctx;
    578      1.1.1.4  christos 
    579      1.1.1.4  christos 	/*
    580      1.1.1.4  christos 	 * Assume peer does not support Authentication exchange or the frame was
    581      1.1.1.4  christos 	 * lost somewhere - start EAPOL Authenticator.
    582      1.1.1.4  christos 	 */
    583      1.1.1.4  christos 	wpa_printf(MSG_DEBUG,
    584      1.1.1.4  christos 		   "RSN: Timeout on waiting Authentication frame response from "
    585      1.1.1.4  christos 		   MACSTR " - start authenticator", MAC2STR(peer->addr));
    586      1.1.1.4  christos 
    587      1.1.1.4  christos 	peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
    588      1.1.1.4  christos 	ibss_rsn_auth_init(peer->ibss_rsn, peer);
    589      1.1.1.4  christos }
    590      1.1.1.4  christos 
    591      1.1.1.4  christos 
    592      1.1.1.4  christos int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
    593      1.1.1.4  christos {
    594      1.1.1.4  christos 	struct ibss_rsn_peer *peer;
    595      1.1.1.4  christos 	int res;
    596      1.1.1.4  christos 
    597      1.1.1.6  christos 	if (!ibss_rsn)
    598      1.1.1.6  christos 		return -1;
    599      1.1.1.6  christos 
    600      1.1.1.4  christos 	/* if the peer already exists, exit immediately */
    601      1.1.1.4  christos 	peer = ibss_rsn_get_peer(ibss_rsn, addr);
    602      1.1.1.4  christos 	if (peer)
    603      1.1.1.4  christos 		return 0;
    604      1.1.1.4  christos 
    605      1.1.1.4  christos 	peer = ibss_rsn_peer_init(ibss_rsn, addr);
    606      1.1.1.4  christos 	if (peer == NULL)
    607          1.1  christos 		return -1;
    608      1.1.1.4  christos 
    609      1.1.1.4  christos 	/* Open Authentication: send first Authentication frame */
    610      1.1.1.4  christos 	res = ibss_rsn_send_auth(ibss_rsn, addr, 1);
    611      1.1.1.4  christos 	if (res) {
    612      1.1.1.4  christos 		/*
    613      1.1.1.4  christos 		 * The driver may not support Authentication frame exchange in
    614      1.1.1.4  christos 		 * IBSS. Ignore authentication and go through EAPOL exchange.
    615      1.1.1.4  christos 		 */
    616      1.1.1.4  christos 		peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
    617      1.1.1.4  christos 		return ibss_rsn_auth_init(ibss_rsn, peer);
    618      1.1.1.4  christos 	} else {
    619      1.1.1.4  christos 		os_get_reltime(&peer->own_auth_tx);
    620      1.1.1.4  christos 		eloop_register_timeout(1, 0, ibss_rsn_auth_timeout, peer, NULL);
    621          1.1  christos 	}
    622          1.1  christos 
    623      1.1.1.4  christos 	return 0;
    624      1.1.1.4  christos }
    625      1.1.1.4  christos 
    626      1.1.1.4  christos 
    627      1.1.1.4  christos static int ibss_rsn_peer_authenticated(struct ibss_rsn *ibss_rsn,
    628      1.1.1.4  christos 				       struct ibss_rsn_peer *peer, int reason)
    629      1.1.1.4  christos {
    630      1.1.1.4  christos 	int already_started;
    631      1.1.1.4  christos 
    632      1.1.1.4  christos 	if (ibss_rsn == NULL || peer == NULL)
    633          1.1  christos 		return -1;
    634      1.1.1.4  christos 
    635      1.1.1.4  christos 	already_started = ibss_rsn_is_auth_started(peer);
    636      1.1.1.4  christos 	peer->authentication_status |= reason;
    637      1.1.1.4  christos 
    638      1.1.1.4  christos 	if (already_started) {
    639      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator already "
    640      1.1.1.4  christos 			   "started for peer " MACSTR, MAC2STR(peer->addr));
    641      1.1.1.4  christos 		return 0;
    642          1.1  christos 	}
    643          1.1  christos 
    644      1.1.1.4  christos 	wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator "
    645      1.1.1.4  christos 		   "for now-authenticated peer " MACSTR, MAC2STR(peer->addr));
    646          1.1  christos 
    647      1.1.1.4  christos 	return ibss_rsn_auth_init(ibss_rsn, peer);
    648          1.1  christos }
    649          1.1  christos 
    650          1.1  christos 
    651      1.1.1.2  christos void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
    652      1.1.1.2  christos {
    653      1.1.1.2  christos 	struct ibss_rsn_peer *peer, *prev;
    654      1.1.1.2  christos 
    655      1.1.1.2  christos 	if (ibss_rsn == NULL)
    656      1.1.1.2  christos 		return;
    657      1.1.1.2  christos 
    658      1.1.1.2  christos 	if (peermac == NULL) {
    659      1.1.1.2  christos 		/* remove all peers */
    660      1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
    661      1.1.1.2  christos 		peer = ibss_rsn->peers;
    662      1.1.1.2  christos 		while (peer) {
    663      1.1.1.2  christos 			prev = peer;
    664      1.1.1.2  christos 			peer = peer->next;
    665      1.1.1.2  christos 			ibss_rsn_free(prev);
    666      1.1.1.2  christos 			ibss_rsn->peers = peer;
    667      1.1.1.2  christos 		}
    668      1.1.1.2  christos 	} else {
    669      1.1.1.2  christos 		/* remove specific peer */
    670      1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
    671      1.1.1.2  christos 			   __func__, MAC2STR(peermac));
    672      1.1.1.2  christos 
    673      1.1.1.2  christos 		for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
    674      1.1.1.2  christos 		     prev = peer, peer = peer->next) {
    675  1.1.1.8.8.1  perseant 			if (ether_addr_equal(peermac, peer->addr)) {
    676      1.1.1.2  christos 				if (prev == NULL)
    677      1.1.1.2  christos 					ibss_rsn->peers = peer->next;
    678      1.1.1.2  christos 				else
    679      1.1.1.2  christos 					prev->next = peer->next;
    680      1.1.1.2  christos 				ibss_rsn_free(peer);
    681      1.1.1.2  christos 				wpa_printf(MSG_DEBUG, "%s: Successfully "
    682      1.1.1.2  christos 					   "removed a specific peer",
    683      1.1.1.2  christos 					   __func__);
    684      1.1.1.2  christos 				break;
    685      1.1.1.2  christos 			}
    686      1.1.1.2  christos 		}
    687      1.1.1.2  christos 	}
    688      1.1.1.2  christos }
    689      1.1.1.2  christos 
    690      1.1.1.2  christos 
    691      1.1.1.6  christos struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s,
    692      1.1.1.6  christos 				struct wpa_ssid *ssid)
    693          1.1  christos {
    694          1.1  christos 	struct ibss_rsn *ibss_rsn;
    695          1.1  christos 
    696          1.1  christos 	ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
    697          1.1  christos 	if (ibss_rsn == NULL)
    698          1.1  christos 		return NULL;
    699          1.1  christos 	ibss_rsn->wpa_s = wpa_s;
    700          1.1  christos 
    701      1.1.1.6  christos 	if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr, ssid) < 0) {
    702          1.1  christos 		ibss_rsn_deinit(ibss_rsn);
    703          1.1  christos 		return NULL;
    704          1.1  christos 	}
    705          1.1  christos 
    706          1.1  christos 	return ibss_rsn;
    707          1.1  christos }
    708          1.1  christos 
    709          1.1  christos 
    710          1.1  christos void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
    711          1.1  christos {
    712          1.1  christos 	struct ibss_rsn_peer *peer, *prev;
    713          1.1  christos 
    714          1.1  christos 	if (ibss_rsn == NULL)
    715          1.1  christos 		return;
    716          1.1  christos 
    717          1.1  christos 	peer = ibss_rsn->peers;
    718          1.1  christos 	while (peer) {
    719          1.1  christos 		prev = peer;
    720          1.1  christos 		peer = peer->next;
    721          1.1  christos 		ibss_rsn_free(prev);
    722          1.1  christos 	}
    723          1.1  christos 
    724      1.1.1.6  christos 	if (ibss_rsn->auth_group)
    725      1.1.1.6  christos 		wpa_deinit(ibss_rsn->auth_group);
    726          1.1  christos 	os_free(ibss_rsn);
    727          1.1  christos 
    728          1.1  christos }
    729          1.1  christos 
    730          1.1  christos 
    731          1.1  christos static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
    732          1.1  christos {
    733          1.1  christos 	const struct ieee802_1x_hdr *hdr;
    734          1.1  christos 	const struct wpa_eapol_key *key;
    735          1.1  christos 	u16 key_info;
    736          1.1  christos 	size_t plen;
    737          1.1  christos 
    738          1.1  christos 	/* TODO: Support other EAPOL packets than just EAPOL-Key */
    739          1.1  christos 
    740          1.1  christos 	if (len < sizeof(*hdr) + sizeof(*key))
    741          1.1  christos 		return -1;
    742          1.1  christos 
    743          1.1  christos 	hdr = (const struct ieee802_1x_hdr *) buf;
    744          1.1  christos 	key = (const struct wpa_eapol_key *) (hdr + 1);
    745          1.1  christos 	plen = be_to_host16(hdr->length);
    746          1.1  christos 
    747          1.1  christos 	if (hdr->version < EAPOL_VERSION) {
    748          1.1  christos 		/* TODO: backwards compatibility */
    749          1.1  christos 	}
    750          1.1  christos 	if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
    751          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
    752          1.1  christos 			"not a Key frame", hdr->type);
    753          1.1  christos 		return -1;
    754          1.1  christos 	}
    755          1.1  christos 	if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
    756          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
    757          1.1  christos 			   "invalid (frame size %lu)",
    758          1.1  christos 			   (unsigned long) plen, (unsigned long) len);
    759          1.1  christos 		return -1;
    760          1.1  christos 	}
    761          1.1  christos 
    762          1.1  christos 	if (key->type != EAPOL_KEY_TYPE_RSN) {
    763          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
    764          1.1  christos 			   "discarded", key->type);
    765          1.1  christos 		return -1;
    766          1.1  christos 	}
    767          1.1  christos 
    768          1.1  christos 	key_info = WPA_GET_BE16(key->key_info);
    769          1.1  christos 
    770          1.1  christos 	return !!(key_info & WPA_KEY_INFO_ACK);
    771          1.1  christos }
    772          1.1  christos 
    773          1.1  christos 
    774          1.1  christos static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
    775          1.1  christos 				     struct ibss_rsn_peer *peer,
    776  1.1.1.8.8.1  perseant 				     const u8 *buf, size_t len,
    777  1.1.1.8.8.1  perseant 				     enum frame_encryption encrypted)
    778          1.1  christos {
    779          1.1  christos 	int supp;
    780          1.1  christos 	u8 *tmp;
    781          1.1  christos 
    782          1.1  christos 	supp = ibss_rsn_eapol_dst_supp(buf, len);
    783          1.1  christos 	if (supp < 0)
    784          1.1  christos 		return -1;
    785          1.1  christos 
    786      1.1.1.7  christos 	tmp = os_memdup(buf, len);
    787          1.1  christos 	if (tmp == NULL)
    788          1.1  christos 		return -1;
    789          1.1  christos 	if (supp) {
    790      1.1.1.4  christos 		peer->authentication_status |= IBSS_RSN_AUTH_EAPOL_BY_PEER;
    791      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant from "
    792      1.1.1.4  christos 			   MACSTR, MAC2STR(peer->addr));
    793  1.1.1.8.8.1  perseant 		wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len, encrypted);
    794          1.1  christos 	} else {
    795      1.1.1.4  christos 		if (ibss_rsn_is_auth_started(peer) == 0) {
    796      1.1.1.4  christos 			wpa_printf(MSG_DEBUG, "RSN: IBSS EAPOL for "
    797      1.1.1.4  christos 				   "Authenticator dropped as " MACSTR " is not "
    798      1.1.1.4  christos 				   "authenticated", MAC2STR(peer->addr));
    799      1.1.1.4  christos 			os_free(tmp);
    800      1.1.1.4  christos 			return -1;
    801      1.1.1.4  christos 		}
    802      1.1.1.4  christos 
    803      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator "
    804      1.1.1.4  christos 			   "from "MACSTR, MAC2STR(peer->addr));
    805          1.1  christos 		wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
    806          1.1  christos 	}
    807          1.1  christos 	os_free(tmp);
    808          1.1  christos 
    809          1.1  christos 	return 1;
    810          1.1  christos }
    811          1.1  christos 
    812          1.1  christos 
    813          1.1  christos int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
    814  1.1.1.8.8.1  perseant 		      const u8 *buf, size_t len,
    815  1.1.1.8.8.1  perseant 		      enum frame_encryption encrypted)
    816          1.1  christos {
    817          1.1  christos 	struct ibss_rsn_peer *peer;
    818          1.1  christos 
    819      1.1.1.2  christos 	if (ibss_rsn == NULL)
    820      1.1.1.2  christos 		return -1;
    821      1.1.1.2  christos 
    822      1.1.1.3  christos 	peer = ibss_rsn_get_peer(ibss_rsn, src_addr);
    823      1.1.1.3  christos 	if (peer)
    824  1.1.1.8.8.1  perseant 		return ibss_rsn_process_rx_eapol(ibss_rsn, peer, buf, len,
    825  1.1.1.8.8.1  perseant 						 encrypted);
    826          1.1  christos 
    827          1.1  christos 	if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
    828          1.1  christos 		/*
    829          1.1  christos 		 * Create new IBSS peer based on an EAPOL message from the peer
    830          1.1  christos 		 * Authenticator.
    831          1.1  christos 		 */
    832      1.1.1.4  christos 		peer = ibss_rsn_peer_init(ibss_rsn, src_addr);
    833      1.1.1.4  christos 		if (peer == NULL)
    834          1.1  christos 			return -1;
    835      1.1.1.4  christos 
    836      1.1.1.4  christos 		/* assume the peer is authenticated already */
    837      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Not using IBSS Auth for peer "
    838      1.1.1.4  christos 			   MACSTR, MAC2STR(src_addr));
    839      1.1.1.4  christos 		ibss_rsn_peer_authenticated(ibss_rsn, peer,
    840      1.1.1.4  christos 					    IBSS_RSN_AUTH_EAPOL_BY_US);
    841      1.1.1.4  christos 
    842          1.1  christos 		return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
    843  1.1.1.8.8.1  perseant 						 buf, len, encrypted);
    844          1.1  christos 	}
    845          1.1  christos 
    846          1.1  christos 	return 0;
    847          1.1  christos }
    848          1.1  christos 
    849          1.1  christos void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
    850          1.1  christos {
    851      1.1.1.2  christos 	if (ibss_rsn == NULL)
    852      1.1.1.2  christos 		return;
    853          1.1  christos 	os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
    854          1.1  christos }
    855      1.1.1.4  christos 
    856      1.1.1.4  christos 
    857      1.1.1.4  christos static void ibss_rsn_handle_auth_1_of_2(struct ibss_rsn *ibss_rsn,
    858      1.1.1.4  christos 					struct ibss_rsn_peer *peer,
    859      1.1.1.4  christos 					const u8* addr)
    860      1.1.1.4  christos {
    861      1.1.1.4  christos 	wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 1) from " MACSTR,
    862      1.1.1.4  christos 		   MAC2STR(addr));
    863      1.1.1.4  christos 
    864      1.1.1.4  christos 	if (peer &&
    865      1.1.1.7  christos 	    peer->authentication_status & (IBSS_RSN_SET_PTK_SUPP |
    866      1.1.1.7  christos 					   IBSS_RSN_SET_PTK_AUTH)) {
    867      1.1.1.7  christos 		/* Clear the TK for this pair to allow recovery from the case
    868      1.1.1.7  christos 		 * where the peer STA has restarted and lost its key while we
    869      1.1.1.7  christos 		 * still have a pairwise key configured. */
    870      1.1.1.7  christos 		wpa_printf(MSG_DEBUG, "RSN: Clear pairwise key for peer "
    871      1.1.1.7  christos 			   MACSTR, MAC2STR(addr));
    872  1.1.1.8.8.1  perseant 		wpa_drv_set_key(ibss_rsn->wpa_s, -1, WPA_ALG_NONE, addr, 0, 0,
    873  1.1.1.8.8.1  perseant 				NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
    874      1.1.1.7  christos 	}
    875      1.1.1.7  christos 
    876      1.1.1.7  christos 	if (peer &&
    877      1.1.1.4  christos 	    peer->authentication_status & IBSS_RSN_AUTH_EAPOL_BY_PEER) {
    878      1.1.1.4  christos 		if (peer->own_auth_tx.sec) {
    879      1.1.1.4  christos 			struct os_reltime now, diff;
    880      1.1.1.4  christos 			os_get_reltime(&now);
    881      1.1.1.4  christos 			os_reltime_sub(&now, &peer->own_auth_tx, &diff);
    882      1.1.1.4  christos 			if (diff.sec == 0 && diff.usec < 500000) {
    883      1.1.1.4  christos 				wpa_printf(MSG_DEBUG, "RSN: Skip IBSS reinit since only %u usec from own Auth frame TX",
    884      1.1.1.4  christos 					   (int) diff.usec);
    885      1.1.1.4  christos 				goto skip_reinit;
    886      1.1.1.4  christos 			}
    887      1.1.1.4  christos 		}
    888      1.1.1.4  christos 		/*
    889      1.1.1.4  christos 		 * A peer sent us an Authentication frame even though it already
    890      1.1.1.4  christos 		 * started an EAPOL session. We should reinit state machines
    891      1.1.1.4  christos 		 * here, but it's much more complicated than just deleting and
    892      1.1.1.4  christos 		 * recreating the state machine
    893      1.1.1.4  christos 		 */
    894      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Reinitializing station "
    895      1.1.1.4  christos 			   MACSTR, MAC2STR(addr));
    896      1.1.1.4  christos 
    897      1.1.1.4  christos 		ibss_rsn_stop(ibss_rsn, addr);
    898      1.1.1.4  christos 		peer = NULL;
    899      1.1.1.4  christos 	}
    900      1.1.1.4  christos 
    901      1.1.1.4  christos 	if (!peer) {
    902      1.1.1.4  christos 		peer = ibss_rsn_peer_init(ibss_rsn, addr);
    903      1.1.1.4  christos 		if (!peer)
    904      1.1.1.4  christos 			return;
    905      1.1.1.4  christos 
    906      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Auth started by peer " MACSTR,
    907      1.1.1.4  christos 			   MAC2STR(addr));
    908      1.1.1.4  christos 	}
    909      1.1.1.4  christos 
    910      1.1.1.4  christos skip_reinit:
    911      1.1.1.4  christos 	/* reply with an Authentication frame now, before sending an EAPOL */
    912      1.1.1.4  christos 	ibss_rsn_send_auth(ibss_rsn, addr, 2);
    913      1.1.1.4  christos 	/* no need to start another AUTH challenge in the other way.. */
    914      1.1.1.4  christos 	ibss_rsn_peer_authenticated(ibss_rsn, peer, IBSS_RSN_AUTH_EAPOL_BY_US);
    915      1.1.1.4  christos }
    916      1.1.1.4  christos 
    917      1.1.1.4  christos 
    918      1.1.1.4  christos void ibss_rsn_handle_auth(struct ibss_rsn *ibss_rsn, const u8 *auth_frame,
    919      1.1.1.4  christos 			  size_t len)
    920      1.1.1.4  christos {
    921      1.1.1.4  christos 	const struct ieee80211_mgmt *header;
    922      1.1.1.4  christos 	struct ibss_rsn_peer *peer;
    923      1.1.1.4  christos 	size_t auth_length;
    924      1.1.1.4  christos 
    925      1.1.1.4  christos 	header = (const struct ieee80211_mgmt *) auth_frame;
    926      1.1.1.4  christos 	auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
    927      1.1.1.4  christos 
    928      1.1.1.4  christos 	if (ibss_rsn == NULL || len < auth_length)
    929      1.1.1.4  christos 		return;
    930      1.1.1.4  christos 
    931      1.1.1.4  christos 	if (le_to_host16(header->u.auth.auth_alg) != WLAN_AUTH_OPEN ||
    932      1.1.1.4  christos 	    le_to_host16(header->u.auth.status_code) != WLAN_STATUS_SUCCESS)
    933      1.1.1.4  christos 		return;
    934      1.1.1.4  christos 
    935      1.1.1.4  christos 	peer = ibss_rsn_get_peer(ibss_rsn, header->sa);
    936      1.1.1.4  christos 
    937      1.1.1.4  christos 	switch (le_to_host16(header->u.auth.auth_transaction)) {
    938      1.1.1.4  christos 	case 1:
    939      1.1.1.4  christos 		ibss_rsn_handle_auth_1_of_2(ibss_rsn, peer, header->sa);
    940      1.1.1.4  christos 		break;
    941      1.1.1.4  christos 	case 2:
    942      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 2) from "
    943      1.1.1.4  christos 			   MACSTR, MAC2STR(header->sa));
    944      1.1.1.4  christos 		if (!peer) {
    945      1.1.1.4  christos 			wpa_printf(MSG_DEBUG, "RSN: Received Auth seq 2 from "
    946      1.1.1.4  christos 				   "unknown STA " MACSTR, MAC2STR(header->sa));
    947      1.1.1.4  christos 			break;
    948      1.1.1.4  christos 		}
    949      1.1.1.4  christos 
    950      1.1.1.4  christos 		/* authentication has been completed */
    951      1.1.1.4  christos 		eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
    952      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Auth completed with " MACSTR,
    953      1.1.1.4  christos 			   MAC2STR(header->sa));
    954      1.1.1.4  christos 		ibss_rsn_peer_authenticated(ibss_rsn, peer,
    955      1.1.1.4  christos 					    IBSS_RSN_AUTH_BY_US);
    956      1.1.1.4  christos 		break;
    957      1.1.1.4  christos 	}
    958      1.1.1.4  christos }
    959