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