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