Home | History | Annotate | Line # | Download | only in wpa_supplicant
ibss_rsn.c revision 1.1.1.7.2.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.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.7.2.1    martin static void supp_deauthenticate(void * ctx, u16 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.2.1    martin 			       size_t *psk_len, int *vlan_id)
    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.1.7.2.1    martin 	if (vlan_id)
    270  1.1.1.7.2.1    martin 		*vlan_id = 0;
    271          1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
    272          1.1  christos 		   __func__, MAC2STR(addr), prev_psk);
    273          1.1  christos 	if (prev_psk)
    274          1.1  christos 		return NULL;
    275          1.1  christos 	return ibss_rsn->psk;
    276          1.1  christos }
    277          1.1  christos 
    278          1.1  christos 
    279          1.1  christos static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
    280          1.1  christos 			   size_t data_len, int encrypt)
    281          1.1  christos {
    282          1.1  christos 	struct ibss_rsn *ibss_rsn = ctx;
    283          1.1  christos 	struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
    284          1.1  christos 
    285          1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
    286          1.1  christos 		   "encrypt=%d)",
    287          1.1  christos 		   __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
    288          1.1  christos 
    289          1.1  christos 	if (wpa_s->l2)
    290          1.1  christos 		return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
    291          1.1  christos 				      data_len);
    292          1.1  christos 
    293      1.1.1.5  christos 	return -1;
    294          1.1  christos }
    295          1.1  christos 
    296          1.1  christos 
    297          1.1  christos static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
    298          1.1  christos 			const u8 *addr, int idx, u8 *key, size_t key_len)
    299          1.1  christos {
    300          1.1  christos 	struct ibss_rsn *ibss_rsn = ctx;
    301          1.1  christos 	u8 seq[6];
    302          1.1  christos 
    303          1.1  christos 	os_memset(seq, 0, sizeof(seq));
    304          1.1  christos 
    305          1.1  christos 	if (addr) {
    306          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
    307          1.1  christos 			   " key_idx=%d)",
    308          1.1  christos 			   __func__, alg, MAC2STR(addr), idx);
    309          1.1  christos 	} else {
    310          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
    311          1.1  christos 			   __func__, alg, idx);
    312          1.1  christos 	}
    313          1.1  christos 	wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
    314          1.1  christos 
    315          1.1  christos 	if (idx == 0) {
    316      1.1.1.4  christos 		if (addr) {
    317      1.1.1.4  christos 			struct ibss_rsn_peer *peer;
    318      1.1.1.4  christos 			peer = ibss_rsn_get_peer(ibss_rsn, addr);
    319      1.1.1.4  christos 			if (peer) {
    320      1.1.1.4  christos 				peer->authentication_status |=
    321      1.1.1.4  christos 					IBSS_RSN_SET_PTK_AUTH;
    322      1.1.1.4  christos 				ibss_check_rsn_completed(peer);
    323      1.1.1.4  christos 			}
    324      1.1.1.4  christos 		}
    325          1.1  christos 		/*
    326          1.1  christos 		 * In IBSS RSN, the pairwise key from the 4-way handshake
    327          1.1  christos 		 * initiated by the peer with highest MAC address is used.
    328          1.1  christos 		 */
    329          1.1  christos 		if (addr == NULL ||
    330          1.1  christos 		    os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
    331          1.1  christos 			wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
    332          1.1  christos 			return 0;
    333          1.1  christos 		}
    334          1.1  christos 	}
    335          1.1  christos 
    336          1.1  christos 	return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
    337          1.1  christos 			       1, seq, 6, key, key_len);
    338          1.1  christos }
    339          1.1  christos 
    340          1.1  christos 
    341      1.1.1.4  christos static void ibss_rsn_disconnect(void *ctx, const u8 *addr, u16 reason)
    342      1.1.1.4  christos {
    343      1.1.1.4  christos 	struct ibss_rsn *ibss_rsn = ctx;
    344      1.1.1.4  christos 	wpa_drv_sta_deauth(ibss_rsn->wpa_s, addr, reason);
    345      1.1.1.4  christos }
    346      1.1.1.4  christos 
    347      1.1.1.4  christos 
    348      1.1.1.2  christos static int auth_for_each_sta(void *ctx, int (*cb)(struct wpa_state_machine *sm,
    349      1.1.1.2  christos 						  void *ctx),
    350      1.1.1.2  christos 			     void *cb_ctx)
    351      1.1.1.2  christos {
    352      1.1.1.2  christos 	struct ibss_rsn *ibss_rsn = ctx;
    353      1.1.1.2  christos 	struct ibss_rsn_peer *peer;
    354      1.1.1.2  christos 
    355      1.1.1.2  christos 	wpa_printf(MSG_DEBUG, "AUTH: for_each_sta");
    356      1.1.1.2  christos 
    357      1.1.1.2  christos 	for (peer = ibss_rsn->peers; peer; peer = peer->next) {
    358      1.1.1.2  christos 		if (peer->auth && cb(peer->auth, cb_ctx))
    359      1.1.1.2  christos 			return 1;
    360      1.1.1.2  christos 	}
    361      1.1.1.2  christos 
    362      1.1.1.2  christos 	return 0;
    363      1.1.1.2  christos }
    364      1.1.1.2  christos 
    365      1.1.1.2  christos 
    366      1.1.1.3  christos static void ibss_set_sta_authorized(struct ibss_rsn *ibss_rsn,
    367      1.1.1.3  christos 				    struct ibss_rsn_peer *peer, int authorized)
    368      1.1.1.3  christos {
    369      1.1.1.3  christos 	int res;
    370      1.1.1.3  christos 
    371      1.1.1.3  christos 	if (authorized) {
    372      1.1.1.3  christos 		res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
    373      1.1.1.3  christos 					    WPA_STA_AUTHORIZED,
    374      1.1.1.3  christos 					    WPA_STA_AUTHORIZED, ~0);
    375      1.1.1.3  christos 		wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " authorizing port",
    376      1.1.1.3  christos 			   MAC2STR(peer->addr));
    377      1.1.1.3  christos 	} else {
    378      1.1.1.3  christos 		res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
    379      1.1.1.3  christos 					    0, 0, ~WPA_STA_AUTHORIZED);
    380      1.1.1.3  christos 		wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " unauthorizing port",
    381      1.1.1.3  christos 			   MAC2STR(peer->addr));
    382      1.1.1.3  christos 	}
    383      1.1.1.3  christos 
    384      1.1.1.3  christos 	if (res && errno != ENOENT) {
    385      1.1.1.3  christos 		wpa_printf(MSG_DEBUG, "Could not set station " MACSTR " flags "
    386      1.1.1.3  christos 			   "for kernel driver (errno=%d)",
    387      1.1.1.3  christos 			   MAC2STR(peer->addr), errno);
    388      1.1.1.3  christos 	}
    389      1.1.1.3  christos }
    390      1.1.1.3  christos 
    391      1.1.1.3  christos 
    392      1.1.1.3  christos static void auth_set_eapol(void *ctx, const u8 *addr,
    393      1.1.1.3  christos 				       wpa_eapol_variable var, int value)
    394      1.1.1.3  christos {
    395      1.1.1.3  christos 	struct ibss_rsn *ibss_rsn = ctx;
    396      1.1.1.3  christos 	struct ibss_rsn_peer *peer = ibss_rsn_get_peer(ibss_rsn, addr);
    397      1.1.1.3  christos 
    398      1.1.1.3  christos 	if (peer == NULL)
    399      1.1.1.3  christos 		return;
    400      1.1.1.3  christos 
    401      1.1.1.3  christos 	switch (var) {
    402      1.1.1.3  christos 	case WPA_EAPOL_authorized:
    403      1.1.1.3  christos 		ibss_set_sta_authorized(ibss_rsn, peer, value);
    404      1.1.1.3  christos 		break;
    405      1.1.1.3  christos 	default:
    406      1.1.1.3  christos 		/* do not handle any other event */
    407      1.1.1.3  christos 		wpa_printf(MSG_DEBUG, "AUTH: eapol event not handled %d", var);
    408      1.1.1.3  christos 		break;
    409      1.1.1.3  christos 	}
    410      1.1.1.3  christos }
    411      1.1.1.3  christos 
    412      1.1.1.3  christos 
    413          1.1  christos static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
    414      1.1.1.6  christos 				    const u8 *own_addr, struct wpa_ssid *ssid)
    415          1.1  christos {
    416          1.1  christos 	struct wpa_auth_config conf;
    417      1.1.1.7  christos 	static const struct wpa_auth_callbacks cb = {
    418      1.1.1.7  christos 		.logger = auth_logger,
    419      1.1.1.7  christos 		.set_eapol = auth_set_eapol,
    420      1.1.1.7  christos 		.send_eapol = auth_send_eapol,
    421      1.1.1.7  christos 		.get_psk = auth_get_psk,
    422      1.1.1.7  christos 		.set_key = auth_set_key,
    423      1.1.1.7  christos 		.for_each_sta = auth_for_each_sta,
    424      1.1.1.7  christos 		.disconnect = ibss_rsn_disconnect,
    425      1.1.1.7  christos 	};
    426          1.1  christos 
    427          1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
    428          1.1  christos 
    429          1.1  christos 	os_memset(&conf, 0, sizeof(conf));
    430          1.1  christos 	conf.wpa = 2;
    431          1.1  christos 	conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
    432          1.1  christos 	conf.wpa_pairwise = WPA_CIPHER_CCMP;
    433          1.1  christos 	conf.rsn_pairwise = WPA_CIPHER_CCMP;
    434          1.1  christos 	conf.wpa_group = WPA_CIPHER_CCMP;
    435          1.1  christos 	conf.eapol_version = 2;
    436      1.1.1.6  christos 	conf.wpa_group_rekey = ssid->group_rekey ? ssid->group_rekey : 600;
    437      1.1.1.7  christos 	conf.wpa_group_update_count = 4;
    438      1.1.1.7  christos 	conf.wpa_pairwise_update_count = 4;
    439          1.1  christos 
    440      1.1.1.7  christos 	ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb, ibss_rsn);
    441          1.1  christos 	if (ibss_rsn->auth_group == NULL) {
    442          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
    443          1.1  christos 		return -1;
    444          1.1  christos 	}
    445          1.1  christos 
    446      1.1.1.2  christos 	wpa_init_keys(ibss_rsn->auth_group);
    447      1.1.1.2  christos 
    448          1.1  christos 	return 0;
    449          1.1  christos }
    450          1.1  christos 
    451          1.1  christos 
    452          1.1  christos static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
    453          1.1  christos 			      struct ibss_rsn_peer *peer)
    454          1.1  christos {
    455      1.1.1.4  christos 	peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr, NULL);
    456          1.1  christos 	if (peer->auth == NULL) {
    457          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
    458          1.1  christos 		return -1;
    459          1.1  christos 	}
    460          1.1  christos 
    461          1.1  christos 	/* TODO: get peer RSN IE with Probe Request */
    462  1.1.1.7.2.1    martin 	if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth, 0,
    463          1.1  christos 				(u8 *) "\x30\x14\x01\x00"
    464          1.1  christos 				"\x00\x0f\xac\x04"
    465          1.1  christos 				"\x01\x00\x00\x0f\xac\x04"
    466          1.1  christos 				"\x01\x00\x00\x0f\xac\x02"
    467      1.1.1.7  christos 				"\x00\x00", 22, NULL, 0, NULL, 0) !=
    468          1.1  christos 	    WPA_IE_OK) {
    469          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
    470          1.1  christos 		return -1;
    471          1.1  christos 	}
    472          1.1  christos 
    473          1.1  christos 	if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
    474          1.1  christos 		return -1;
    475          1.1  christos 
    476          1.1  christos 	if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
    477          1.1  christos 		return -1;
    478          1.1  christos 
    479          1.1  christos 	return 0;
    480          1.1  christos }
    481          1.1  christos 
    482          1.1  christos 
    483      1.1.1.4  christos static int ibss_rsn_send_auth(struct ibss_rsn *ibss_rsn, const u8 *da, int seq)
    484          1.1  christos {
    485      1.1.1.4  christos 	struct ieee80211_mgmt auth;
    486      1.1.1.4  christos 	const size_t auth_length = IEEE80211_HDRLEN + sizeof(auth.u.auth);
    487      1.1.1.4  christos 	struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
    488          1.1  christos 
    489      1.1.1.4  christos 	if (wpa_s->driver->send_frame == NULL)
    490      1.1.1.2  christos 		return -1;
    491      1.1.1.2  christos 
    492      1.1.1.4  christos 	os_memset(&auth, 0, sizeof(auth));
    493      1.1.1.4  christos 
    494      1.1.1.4  christos 	auth.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
    495      1.1.1.4  christos 					  WLAN_FC_STYPE_AUTH);
    496      1.1.1.4  christos 	os_memcpy(auth.da, da, ETH_ALEN);
    497      1.1.1.4  christos 	os_memcpy(auth.sa, wpa_s->own_addr, ETH_ALEN);
    498      1.1.1.4  christos 	os_memcpy(auth.bssid, wpa_s->bssid, ETH_ALEN);
    499      1.1.1.4  christos 
    500      1.1.1.4  christos 	auth.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
    501      1.1.1.4  christos 	auth.u.auth.auth_transaction = host_to_le16(seq);
    502      1.1.1.4  christos 	auth.u.auth.status_code = host_to_le16(WLAN_STATUS_SUCCESS);
    503      1.1.1.4  christos 
    504      1.1.1.4  christos 	wpa_printf(MSG_DEBUG, "RSN: IBSS TX Auth frame (SEQ %d) to " MACSTR,
    505      1.1.1.4  christos 		   seq, MAC2STR(da));
    506      1.1.1.4  christos 
    507      1.1.1.4  christos 	return wpa_s->driver->send_frame(wpa_s->drv_priv, (u8 *) &auth,
    508      1.1.1.4  christos 					 auth_length, 0);
    509      1.1.1.4  christos }
    510      1.1.1.4  christos 
    511      1.1.1.4  christos 
    512      1.1.1.4  christos static int ibss_rsn_is_auth_started(struct ibss_rsn_peer * peer)
    513      1.1.1.4  christos {
    514      1.1.1.4  christos 	return peer->authentication_status &
    515      1.1.1.4  christos 	       (IBSS_RSN_AUTH_BY_US | IBSS_RSN_AUTH_EAPOL_BY_US);
    516      1.1.1.4  christos }
    517      1.1.1.4  christos 
    518      1.1.1.4  christos 
    519      1.1.1.4  christos static struct ibss_rsn_peer *
    520      1.1.1.4  christos ibss_rsn_peer_init(struct ibss_rsn *ibss_rsn, const u8 *addr)
    521      1.1.1.4  christos {
    522      1.1.1.4  christos 	struct ibss_rsn_peer *peer;
    523      1.1.1.4  christos 	if (ibss_rsn == NULL)
    524      1.1.1.4  christos 		return NULL;
    525      1.1.1.4  christos 
    526      1.1.1.4  christos 	peer = ibss_rsn_get_peer(ibss_rsn, addr);
    527      1.1.1.4  christos 	if (peer) {
    528      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Supplicant for peer "MACSTR
    529      1.1.1.4  christos 			   " already running", MAC2STR(addr));
    530      1.1.1.4  christos 		return peer;
    531      1.1.1.2  christos 	}
    532      1.1.1.2  christos 
    533      1.1.1.4  christos 	wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Supplicant for peer "MACSTR,
    534      1.1.1.4  christos 		   MAC2STR(addr));
    535          1.1  christos 
    536          1.1  christos 	peer = os_zalloc(sizeof(*peer));
    537      1.1.1.4  christos 	if (peer == NULL) {
    538      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: Could not allocate memory.");
    539      1.1.1.4  christos 		return NULL;
    540      1.1.1.4  christos 	}
    541          1.1  christos 
    542          1.1  christos 	peer->ibss_rsn = ibss_rsn;
    543          1.1  christos 	os_memcpy(peer->addr, addr, ETH_ALEN);
    544      1.1.1.4  christos 	peer->authentication_status = IBSS_RSN_AUTH_NOT_AUTHENTICATED;
    545          1.1  christos 
    546      1.1.1.4  christos 	if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr,
    547      1.1.1.4  christos 			       ibss_rsn->psk) < 0) {
    548          1.1  christos 		ibss_rsn_free(peer);
    549      1.1.1.4  christos 		return NULL;
    550      1.1.1.4  christos 	}
    551      1.1.1.4  christos 
    552      1.1.1.4  christos 	peer->next = ibss_rsn->peers;
    553      1.1.1.4  christos 	ibss_rsn->peers = peer;
    554      1.1.1.4  christos 
    555      1.1.1.4  christos 	return peer;
    556      1.1.1.4  christos }
    557      1.1.1.4  christos 
    558      1.1.1.4  christos 
    559      1.1.1.4  christos static void ibss_rsn_auth_timeout(void *eloop_ctx, void *timeout_ctx)
    560      1.1.1.4  christos {
    561      1.1.1.4  christos 	struct ibss_rsn_peer *peer = eloop_ctx;
    562      1.1.1.4  christos 
    563      1.1.1.4  christos 	/*
    564      1.1.1.4  christos 	 * Assume peer does not support Authentication exchange or the frame was
    565      1.1.1.4  christos 	 * lost somewhere - start EAPOL Authenticator.
    566      1.1.1.4  christos 	 */
    567      1.1.1.4  christos 	wpa_printf(MSG_DEBUG,
    568      1.1.1.4  christos 		   "RSN: Timeout on waiting Authentication frame response from "
    569      1.1.1.4  christos 		   MACSTR " - start authenticator", MAC2STR(peer->addr));
    570      1.1.1.4  christos 
    571      1.1.1.4  christos 	peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
    572      1.1.1.4  christos 	ibss_rsn_auth_init(peer->ibss_rsn, peer);
    573      1.1.1.4  christos }
    574      1.1.1.4  christos 
    575      1.1.1.4  christos 
    576      1.1.1.4  christos int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
    577      1.1.1.4  christos {
    578      1.1.1.4  christos 	struct ibss_rsn_peer *peer;
    579      1.1.1.4  christos 	int res;
    580      1.1.1.4  christos 
    581      1.1.1.6  christos 	if (!ibss_rsn)
    582      1.1.1.6  christos 		return -1;
    583      1.1.1.6  christos 
    584      1.1.1.4  christos 	/* if the peer already exists, exit immediately */
    585      1.1.1.4  christos 	peer = ibss_rsn_get_peer(ibss_rsn, addr);
    586      1.1.1.4  christos 	if (peer)
    587      1.1.1.4  christos 		return 0;
    588      1.1.1.4  christos 
    589      1.1.1.4  christos 	peer = ibss_rsn_peer_init(ibss_rsn, addr);
    590      1.1.1.4  christos 	if (peer == NULL)
    591          1.1  christos 		return -1;
    592      1.1.1.4  christos 
    593      1.1.1.4  christos 	/* Open Authentication: send first Authentication frame */
    594      1.1.1.4  christos 	res = ibss_rsn_send_auth(ibss_rsn, addr, 1);
    595      1.1.1.4  christos 	if (res) {
    596      1.1.1.4  christos 		/*
    597      1.1.1.4  christos 		 * The driver may not support Authentication frame exchange in
    598      1.1.1.4  christos 		 * IBSS. Ignore authentication and go through EAPOL exchange.
    599      1.1.1.4  christos 		 */
    600      1.1.1.4  christos 		peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
    601      1.1.1.4  christos 		return ibss_rsn_auth_init(ibss_rsn, peer);
    602      1.1.1.4  christos 	} else {
    603      1.1.1.4  christos 		os_get_reltime(&peer->own_auth_tx);
    604      1.1.1.4  christos 		eloop_register_timeout(1, 0, ibss_rsn_auth_timeout, peer, NULL);
    605          1.1  christos 	}
    606          1.1  christos 
    607      1.1.1.4  christos 	return 0;
    608      1.1.1.4  christos }
    609      1.1.1.4  christos 
    610      1.1.1.4  christos 
    611      1.1.1.4  christos static int ibss_rsn_peer_authenticated(struct ibss_rsn *ibss_rsn,
    612      1.1.1.4  christos 				       struct ibss_rsn_peer *peer, int reason)
    613      1.1.1.4  christos {
    614      1.1.1.4  christos 	int already_started;
    615      1.1.1.4  christos 
    616      1.1.1.4  christos 	if (ibss_rsn == NULL || peer == NULL)
    617          1.1  christos 		return -1;
    618      1.1.1.4  christos 
    619      1.1.1.4  christos 	already_started = ibss_rsn_is_auth_started(peer);
    620      1.1.1.4  christos 	peer->authentication_status |= reason;
    621      1.1.1.4  christos 
    622      1.1.1.4  christos 	if (already_started) {
    623      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator already "
    624      1.1.1.4  christos 			   "started for peer " MACSTR, MAC2STR(peer->addr));
    625      1.1.1.4  christos 		return 0;
    626          1.1  christos 	}
    627          1.1  christos 
    628      1.1.1.4  christos 	wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator "
    629      1.1.1.4  christos 		   "for now-authenticated peer " MACSTR, MAC2STR(peer->addr));
    630          1.1  christos 
    631      1.1.1.4  christos 	return ibss_rsn_auth_init(ibss_rsn, peer);
    632          1.1  christos }
    633          1.1  christos 
    634          1.1  christos 
    635      1.1.1.2  christos void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
    636      1.1.1.2  christos {
    637      1.1.1.2  christos 	struct ibss_rsn_peer *peer, *prev;
    638      1.1.1.2  christos 
    639      1.1.1.2  christos 	if (ibss_rsn == NULL)
    640      1.1.1.2  christos 		return;
    641      1.1.1.2  christos 
    642      1.1.1.2  christos 	if (peermac == NULL) {
    643      1.1.1.2  christos 		/* remove all peers */
    644      1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
    645      1.1.1.2  christos 		peer = ibss_rsn->peers;
    646      1.1.1.2  christos 		while (peer) {
    647      1.1.1.2  christos 			prev = peer;
    648      1.1.1.2  christos 			peer = peer->next;
    649      1.1.1.2  christos 			ibss_rsn_free(prev);
    650      1.1.1.2  christos 			ibss_rsn->peers = peer;
    651      1.1.1.2  christos 		}
    652      1.1.1.2  christos 	} else {
    653      1.1.1.2  christos 		/* remove specific peer */
    654      1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
    655      1.1.1.2  christos 			   __func__, MAC2STR(peermac));
    656      1.1.1.2  christos 
    657      1.1.1.2  christos 		for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
    658      1.1.1.2  christos 		     prev = peer, peer = peer->next) {
    659      1.1.1.2  christos 			if (os_memcmp(peermac, peer->addr, ETH_ALEN) == 0) {
    660      1.1.1.2  christos 				if (prev == NULL)
    661      1.1.1.2  christos 					ibss_rsn->peers = peer->next;
    662      1.1.1.2  christos 				else
    663      1.1.1.2  christos 					prev->next = peer->next;
    664      1.1.1.2  christos 				ibss_rsn_free(peer);
    665      1.1.1.2  christos 				wpa_printf(MSG_DEBUG, "%s: Successfully "
    666      1.1.1.2  christos 					   "removed a specific peer",
    667      1.1.1.2  christos 					   __func__);
    668      1.1.1.2  christos 				break;
    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.2  christos 
    674      1.1.1.2  christos 
    675      1.1.1.6  christos struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s,
    676      1.1.1.6  christos 				struct wpa_ssid *ssid)
    677          1.1  christos {
    678          1.1  christos 	struct ibss_rsn *ibss_rsn;
    679          1.1  christos 
    680          1.1  christos 	ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
    681          1.1  christos 	if (ibss_rsn == NULL)
    682          1.1  christos 		return NULL;
    683          1.1  christos 	ibss_rsn->wpa_s = wpa_s;
    684          1.1  christos 
    685      1.1.1.6  christos 	if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr, ssid) < 0) {
    686          1.1  christos 		ibss_rsn_deinit(ibss_rsn);
    687          1.1  christos 		return NULL;
    688          1.1  christos 	}
    689          1.1  christos 
    690          1.1  christos 	return ibss_rsn;
    691          1.1  christos }
    692          1.1  christos 
    693          1.1  christos 
    694          1.1  christos void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
    695          1.1  christos {
    696          1.1  christos 	struct ibss_rsn_peer *peer, *prev;
    697          1.1  christos 
    698          1.1  christos 	if (ibss_rsn == NULL)
    699          1.1  christos 		return;
    700          1.1  christos 
    701          1.1  christos 	peer = ibss_rsn->peers;
    702          1.1  christos 	while (peer) {
    703          1.1  christos 		prev = peer;
    704          1.1  christos 		peer = peer->next;
    705          1.1  christos 		ibss_rsn_free(prev);
    706          1.1  christos 	}
    707          1.1  christos 
    708      1.1.1.6  christos 	if (ibss_rsn->auth_group)
    709      1.1.1.6  christos 		wpa_deinit(ibss_rsn->auth_group);
    710          1.1  christos 	os_free(ibss_rsn);
    711          1.1  christos 
    712          1.1  christos }
    713          1.1  christos 
    714          1.1  christos 
    715          1.1  christos static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
    716          1.1  christos {
    717          1.1  christos 	const struct ieee802_1x_hdr *hdr;
    718          1.1  christos 	const struct wpa_eapol_key *key;
    719          1.1  christos 	u16 key_info;
    720          1.1  christos 	size_t plen;
    721          1.1  christos 
    722          1.1  christos 	/* TODO: Support other EAPOL packets than just EAPOL-Key */
    723          1.1  christos 
    724          1.1  christos 	if (len < sizeof(*hdr) + sizeof(*key))
    725          1.1  christos 		return -1;
    726          1.1  christos 
    727          1.1  christos 	hdr = (const struct ieee802_1x_hdr *) buf;
    728          1.1  christos 	key = (const struct wpa_eapol_key *) (hdr + 1);
    729          1.1  christos 	plen = be_to_host16(hdr->length);
    730          1.1  christos 
    731          1.1  christos 	if (hdr->version < EAPOL_VERSION) {
    732          1.1  christos 		/* TODO: backwards compatibility */
    733          1.1  christos 	}
    734          1.1  christos 	if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
    735          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
    736          1.1  christos 			"not a Key frame", hdr->type);
    737          1.1  christos 		return -1;
    738          1.1  christos 	}
    739          1.1  christos 	if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
    740          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
    741          1.1  christos 			   "invalid (frame size %lu)",
    742          1.1  christos 			   (unsigned long) plen, (unsigned long) len);
    743          1.1  christos 		return -1;
    744          1.1  christos 	}
    745          1.1  christos 
    746          1.1  christos 	if (key->type != EAPOL_KEY_TYPE_RSN) {
    747          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
    748          1.1  christos 			   "discarded", key->type);
    749          1.1  christos 		return -1;
    750          1.1  christos 	}
    751          1.1  christos 
    752          1.1  christos 	key_info = WPA_GET_BE16(key->key_info);
    753          1.1  christos 
    754          1.1  christos 	return !!(key_info & WPA_KEY_INFO_ACK);
    755          1.1  christos }
    756          1.1  christos 
    757          1.1  christos 
    758          1.1  christos static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
    759          1.1  christos 				     struct ibss_rsn_peer *peer,
    760          1.1  christos 				     const u8 *buf, size_t len)
    761          1.1  christos {
    762          1.1  christos 	int supp;
    763          1.1  christos 	u8 *tmp;
    764          1.1  christos 
    765          1.1  christos 	supp = ibss_rsn_eapol_dst_supp(buf, len);
    766          1.1  christos 	if (supp < 0)
    767          1.1  christos 		return -1;
    768          1.1  christos 
    769      1.1.1.7  christos 	tmp = os_memdup(buf, len);
    770          1.1  christos 	if (tmp == NULL)
    771          1.1  christos 		return -1;
    772          1.1  christos 	if (supp) {
    773      1.1.1.4  christos 		peer->authentication_status |= IBSS_RSN_AUTH_EAPOL_BY_PEER;
    774      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant from "
    775      1.1.1.4  christos 			   MACSTR, MAC2STR(peer->addr));
    776          1.1  christos 		wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
    777          1.1  christos 	} else {
    778      1.1.1.4  christos 		if (ibss_rsn_is_auth_started(peer) == 0) {
    779      1.1.1.4  christos 			wpa_printf(MSG_DEBUG, "RSN: IBSS EAPOL for "
    780      1.1.1.4  christos 				   "Authenticator dropped as " MACSTR " is not "
    781      1.1.1.4  christos 				   "authenticated", MAC2STR(peer->addr));
    782      1.1.1.4  christos 			os_free(tmp);
    783      1.1.1.4  christos 			return -1;
    784      1.1.1.4  christos 		}
    785      1.1.1.4  christos 
    786      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator "
    787      1.1.1.4  christos 			   "from "MACSTR, MAC2STR(peer->addr));
    788          1.1  christos 		wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
    789          1.1  christos 	}
    790          1.1  christos 	os_free(tmp);
    791          1.1  christos 
    792          1.1  christos 	return 1;
    793          1.1  christos }
    794          1.1  christos 
    795          1.1  christos 
    796          1.1  christos int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
    797          1.1  christos 		      const u8 *buf, size_t len)
    798          1.1  christos {
    799          1.1  christos 	struct ibss_rsn_peer *peer;
    800          1.1  christos 
    801      1.1.1.2  christos 	if (ibss_rsn == NULL)
    802      1.1.1.2  christos 		return -1;
    803      1.1.1.2  christos 
    804      1.1.1.3  christos 	peer = ibss_rsn_get_peer(ibss_rsn, src_addr);
    805      1.1.1.3  christos 	if (peer)
    806      1.1.1.3  christos 		return ibss_rsn_process_rx_eapol(ibss_rsn, peer, buf, len);
    807          1.1  christos 
    808          1.1  christos 	if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
    809          1.1  christos 		/*
    810          1.1  christos 		 * Create new IBSS peer based on an EAPOL message from the peer
    811          1.1  christos 		 * Authenticator.
    812          1.1  christos 		 */
    813      1.1.1.4  christos 		peer = ibss_rsn_peer_init(ibss_rsn, src_addr);
    814      1.1.1.4  christos 		if (peer == NULL)
    815          1.1  christos 			return -1;
    816      1.1.1.4  christos 
    817      1.1.1.4  christos 		/* assume the peer is authenticated already */
    818      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Not using IBSS Auth for peer "
    819      1.1.1.4  christos 			   MACSTR, MAC2STR(src_addr));
    820      1.1.1.4  christos 		ibss_rsn_peer_authenticated(ibss_rsn, peer,
    821      1.1.1.4  christos 					    IBSS_RSN_AUTH_EAPOL_BY_US);
    822      1.1.1.4  christos 
    823          1.1  christos 		return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
    824          1.1  christos 						 buf, len);
    825          1.1  christos 	}
    826          1.1  christos 
    827          1.1  christos 	return 0;
    828          1.1  christos }
    829          1.1  christos 
    830          1.1  christos void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
    831          1.1  christos {
    832      1.1.1.2  christos 	if (ibss_rsn == NULL)
    833      1.1.1.2  christos 		return;
    834          1.1  christos 	os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
    835          1.1  christos }
    836      1.1.1.4  christos 
    837      1.1.1.4  christos 
    838      1.1.1.4  christos static void ibss_rsn_handle_auth_1_of_2(struct ibss_rsn *ibss_rsn,
    839      1.1.1.4  christos 					struct ibss_rsn_peer *peer,
    840      1.1.1.4  christos 					const u8* addr)
    841      1.1.1.4  christos {
    842      1.1.1.4  christos 	wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 1) from " MACSTR,
    843      1.1.1.4  christos 		   MAC2STR(addr));
    844      1.1.1.4  christos 
    845      1.1.1.4  christos 	if (peer &&
    846      1.1.1.7  christos 	    peer->authentication_status & (IBSS_RSN_SET_PTK_SUPP |
    847      1.1.1.7  christos 					   IBSS_RSN_SET_PTK_AUTH)) {
    848      1.1.1.7  christos 		/* Clear the TK for this pair to allow recovery from the case
    849      1.1.1.7  christos 		 * where the peer STA has restarted and lost its key while we
    850      1.1.1.7  christos 		 * still have a pairwise key configured. */
    851      1.1.1.7  christos 		wpa_printf(MSG_DEBUG, "RSN: Clear pairwise key for peer "
    852      1.1.1.7  christos 			   MACSTR, MAC2STR(addr));
    853      1.1.1.7  christos 		wpa_drv_set_key(ibss_rsn->wpa_s, WPA_ALG_NONE, addr, 0, 0,
    854      1.1.1.7  christos 				NULL, 0, NULL, 0);
    855      1.1.1.7  christos 	}
    856      1.1.1.7  christos 
    857      1.1.1.7  christos 	if (peer &&
    858      1.1.1.4  christos 	    peer->authentication_status & IBSS_RSN_AUTH_EAPOL_BY_PEER) {
    859      1.1.1.4  christos 		if (peer->own_auth_tx.sec) {
    860      1.1.1.4  christos 			struct os_reltime now, diff;
    861      1.1.1.4  christos 			os_get_reltime(&now);
    862      1.1.1.4  christos 			os_reltime_sub(&now, &peer->own_auth_tx, &diff);
    863      1.1.1.4  christos 			if (diff.sec == 0 && diff.usec < 500000) {
    864      1.1.1.4  christos 				wpa_printf(MSG_DEBUG, "RSN: Skip IBSS reinit since only %u usec from own Auth frame TX",
    865      1.1.1.4  christos 					   (int) diff.usec);
    866      1.1.1.4  christos 				goto skip_reinit;
    867      1.1.1.4  christos 			}
    868      1.1.1.4  christos 		}
    869      1.1.1.4  christos 		/*
    870      1.1.1.4  christos 		 * A peer sent us an Authentication frame even though it already
    871      1.1.1.4  christos 		 * started an EAPOL session. We should reinit state machines
    872      1.1.1.4  christos 		 * here, but it's much more complicated than just deleting and
    873      1.1.1.4  christos 		 * recreating the state machine
    874      1.1.1.4  christos 		 */
    875      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Reinitializing station "
    876      1.1.1.4  christos 			   MACSTR, MAC2STR(addr));
    877      1.1.1.4  christos 
    878      1.1.1.4  christos 		ibss_rsn_stop(ibss_rsn, addr);
    879      1.1.1.4  christos 		peer = NULL;
    880      1.1.1.4  christos 	}
    881      1.1.1.4  christos 
    882      1.1.1.4  christos 	if (!peer) {
    883      1.1.1.4  christos 		peer = ibss_rsn_peer_init(ibss_rsn, addr);
    884      1.1.1.4  christos 		if (!peer)
    885      1.1.1.4  christos 			return;
    886      1.1.1.4  christos 
    887      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Auth started by peer " MACSTR,
    888      1.1.1.4  christos 			   MAC2STR(addr));
    889      1.1.1.4  christos 	}
    890      1.1.1.4  christos 
    891      1.1.1.4  christos skip_reinit:
    892      1.1.1.4  christos 	/* reply with an Authentication frame now, before sending an EAPOL */
    893      1.1.1.4  christos 	ibss_rsn_send_auth(ibss_rsn, addr, 2);
    894      1.1.1.4  christos 	/* no need to start another AUTH challenge in the other way.. */
    895      1.1.1.4  christos 	ibss_rsn_peer_authenticated(ibss_rsn, peer, IBSS_RSN_AUTH_EAPOL_BY_US);
    896      1.1.1.4  christos }
    897      1.1.1.4  christos 
    898      1.1.1.4  christos 
    899      1.1.1.4  christos void ibss_rsn_handle_auth(struct ibss_rsn *ibss_rsn, const u8 *auth_frame,
    900      1.1.1.4  christos 			  size_t len)
    901      1.1.1.4  christos {
    902      1.1.1.4  christos 	const struct ieee80211_mgmt *header;
    903      1.1.1.4  christos 	struct ibss_rsn_peer *peer;
    904      1.1.1.4  christos 	size_t auth_length;
    905      1.1.1.4  christos 
    906      1.1.1.4  christos 	header = (const struct ieee80211_mgmt *) auth_frame;
    907      1.1.1.4  christos 	auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
    908      1.1.1.4  christos 
    909      1.1.1.4  christos 	if (ibss_rsn == NULL || len < auth_length)
    910      1.1.1.4  christos 		return;
    911      1.1.1.4  christos 
    912      1.1.1.4  christos 	if (le_to_host16(header->u.auth.auth_alg) != WLAN_AUTH_OPEN ||
    913      1.1.1.4  christos 	    le_to_host16(header->u.auth.status_code) != WLAN_STATUS_SUCCESS)
    914      1.1.1.4  christos 		return;
    915      1.1.1.4  christos 
    916      1.1.1.4  christos 	peer = ibss_rsn_get_peer(ibss_rsn, header->sa);
    917      1.1.1.4  christos 
    918      1.1.1.4  christos 	switch (le_to_host16(header->u.auth.auth_transaction)) {
    919      1.1.1.4  christos 	case 1:
    920      1.1.1.4  christos 		ibss_rsn_handle_auth_1_of_2(ibss_rsn, peer, header->sa);
    921      1.1.1.4  christos 		break;
    922      1.1.1.4  christos 	case 2:
    923      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 2) from "
    924      1.1.1.4  christos 			   MACSTR, MAC2STR(header->sa));
    925      1.1.1.4  christos 		if (!peer) {
    926      1.1.1.4  christos 			wpa_printf(MSG_DEBUG, "RSN: Received Auth seq 2 from "
    927      1.1.1.4  christos 				   "unknown STA " MACSTR, MAC2STR(header->sa));
    928      1.1.1.4  christos 			break;
    929      1.1.1.4  christos 		}
    930      1.1.1.4  christos 
    931      1.1.1.4  christos 		/* authentication has been completed */
    932      1.1.1.4  christos 		eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
    933      1.1.1.4  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS Auth completed with " MACSTR,
    934      1.1.1.4  christos 			   MAC2STR(header->sa));
    935      1.1.1.4  christos 		ibss_rsn_peer_authenticated(ibss_rsn, peer,
    936      1.1.1.4  christos 					    IBSS_RSN_AUTH_BY_US);
    937      1.1.1.4  christos 		break;
    938      1.1.1.4  christos 	}
    939      1.1.1.4  christos }
    940