Home | History | Annotate | Line # | Download | only in wpa_supplicant
ibss_rsn.c revision 1.1.1.1.6.1
      1          1.1  christos /*
      2          1.1  christos  * wpa_supplicant - IBSS RSN
      3          1.1  christos  * Copyright (c) 2009, Jouni Malinen <j (at) w1.fi>
      4          1.1  christos  *
      5          1.1  christos  * This program is free software; you can redistribute it and/or modify
      6          1.1  christos  * it under the terms of the GNU General Public License version 2 as
      7          1.1  christos  * published by the Free Software Foundation.
      8          1.1  christos  *
      9          1.1  christos  * Alternatively, this software may be distributed under the terms of BSD
     10          1.1  christos  * license.
     11          1.1  christos  *
     12          1.1  christos  * See README and COPYING for more details.
     13          1.1  christos  */
     14          1.1  christos 
     15          1.1  christos #include "includes.h"
     16          1.1  christos 
     17          1.1  christos #include "common.h"
     18          1.1  christos #include "l2_packet/l2_packet.h"
     19          1.1  christos #include "rsn_supp/wpa.h"
     20          1.1  christos #include "rsn_supp/wpa_ie.h"
     21          1.1  christos #include "ap/wpa_auth.h"
     22          1.1  christos #include "wpa_supplicant_i.h"
     23          1.1  christos #include "driver_i.h"
     24          1.1  christos #include "ibss_rsn.h"
     25          1.1  christos 
     26          1.1  christos 
     27          1.1  christos static void ibss_rsn_free(struct ibss_rsn_peer *peer)
     28          1.1  christos {
     29          1.1  christos 	wpa_auth_sta_deinit(peer->auth);
     30          1.1  christos 	wpa_sm_deinit(peer->supp);
     31          1.1  christos 	os_free(peer);
     32          1.1  christos }
     33          1.1  christos 
     34          1.1  christos 
     35          1.1  christos static void supp_set_state(void *ctx, enum wpa_states state)
     36          1.1  christos {
     37          1.1  christos 	struct ibss_rsn_peer *peer = ctx;
     38          1.1  christos 	peer->supp_state = state;
     39          1.1  christos }
     40          1.1  christos 
     41          1.1  christos 
     42  1.1.1.1.6.1      yamt static enum wpa_states supp_get_state(void *ctx)
     43  1.1.1.1.6.1      yamt {
     44  1.1.1.1.6.1      yamt 	struct ibss_rsn_peer *peer = ctx;
     45  1.1.1.1.6.1      yamt 	return peer->supp_state;
     46  1.1.1.1.6.1      yamt }
     47  1.1.1.1.6.1      yamt 
     48  1.1.1.1.6.1      yamt 
     49          1.1  christos static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
     50          1.1  christos 			   size_t len)
     51          1.1  christos {
     52          1.1  christos 	struct ibss_rsn_peer *peer = ctx;
     53          1.1  christos 	struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
     54          1.1  christos 
     55          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
     56          1.1  christos 		   "len=%lu)",
     57          1.1  christos 		   __func__, MAC2STR(dest), proto, (unsigned long) len);
     58          1.1  christos 
     59          1.1  christos 	if (wpa_s->l2)
     60          1.1  christos 		return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
     61          1.1  christos 
     62          1.1  christos 	return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
     63          1.1  christos }
     64          1.1  christos 
     65          1.1  christos 
     66          1.1  christos static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
     67          1.1  christos 			     u16 data_len, size_t *msg_len, void **data_pos)
     68          1.1  christos {
     69          1.1  christos 	struct ieee802_1x_hdr *hdr;
     70          1.1  christos 
     71          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
     72          1.1  christos 		   __func__, type, data_len);
     73          1.1  christos 
     74          1.1  christos 	*msg_len = sizeof(*hdr) + data_len;
     75          1.1  christos 	hdr = os_malloc(*msg_len);
     76          1.1  christos 	if (hdr == NULL)
     77          1.1  christos 		return NULL;
     78          1.1  christos 
     79          1.1  christos 	hdr->version = 2;
     80          1.1  christos 	hdr->type = type;
     81          1.1  christos 	hdr->length = host_to_be16(data_len);
     82          1.1  christos 
     83          1.1  christos 	if (data)
     84          1.1  christos 		os_memcpy(hdr + 1, data, data_len);
     85          1.1  christos 	else
     86          1.1  christos 		os_memset(hdr + 1, 0, data_len);
     87          1.1  christos 
     88          1.1  christos 	if (data_pos)
     89          1.1  christos 		*data_pos = hdr + 1;
     90          1.1  christos 
     91          1.1  christos 	return (u8 *) hdr;
     92          1.1  christos }
     93          1.1  christos 
     94          1.1  christos 
     95          1.1  christos static int supp_get_beacon_ie(void *ctx)
     96          1.1  christos {
     97          1.1  christos 	struct ibss_rsn_peer *peer = ctx;
     98          1.1  christos 
     99          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
    100          1.1  christos 	/* TODO: get correct RSN IE */
    101          1.1  christos 	return wpa_sm_set_ap_rsn_ie(peer->supp,
    102          1.1  christos 				    (u8 *) "\x30\x14\x01\x00"
    103          1.1  christos 				    "\x00\x0f\xac\x04"
    104          1.1  christos 				    "\x01\x00\x00\x0f\xac\x04"
    105          1.1  christos 				    "\x01\x00\x00\x0f\xac\x02"
    106          1.1  christos 				    "\x00\x00", 22);
    107          1.1  christos }
    108          1.1  christos 
    109          1.1  christos 
    110          1.1  christos static int supp_set_key(void *ctx, enum wpa_alg alg,
    111          1.1  christos 			const u8 *addr, int key_idx, int set_tx,
    112          1.1  christos 			const u8 *seq, size_t seq_len,
    113          1.1  christos 			const u8 *key, size_t key_len)
    114          1.1  christos {
    115          1.1  christos 	struct ibss_rsn_peer *peer = ctx;
    116          1.1  christos 
    117          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
    118          1.1  christos 		   "set_tx=%d)",
    119          1.1  christos 		   __func__, alg, MAC2STR(addr), key_idx, set_tx);
    120          1.1  christos 	wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
    121          1.1  christos 	wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
    122          1.1  christos 
    123          1.1  christos 	if (key_idx == 0) {
    124          1.1  christos 		/*
    125          1.1  christos 		 * In IBSS RSN, the pairwise key from the 4-way handshake
    126          1.1  christos 		 * initiated by the peer with highest MAC address is used.
    127          1.1  christos 		 */
    128          1.1  christos 		if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
    129          1.1  christos 			      ETH_ALEN) > 0) {
    130          1.1  christos 			wpa_printf(MSG_DEBUG, "SUPP: Do not use this PTK");
    131          1.1  christos 			return 0;
    132          1.1  christos 		}
    133          1.1  christos 	}
    134          1.1  christos 
    135  1.1.1.1.6.1      yamt 	if (is_broadcast_ether_addr(addr))
    136  1.1.1.1.6.1      yamt 		addr = peer->addr;
    137          1.1  christos 	return wpa_drv_set_key(peer->ibss_rsn->wpa_s, alg, addr, key_idx,
    138          1.1  christos 			       set_tx, seq, seq_len, key, key_len);
    139          1.1  christos }
    140          1.1  christos 
    141          1.1  christos 
    142          1.1  christos static void * supp_get_network_ctx(void *ctx)
    143          1.1  christos {
    144          1.1  christos 	struct ibss_rsn_peer *peer = ctx;
    145          1.1  christos 	return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
    146          1.1  christos }
    147          1.1  christos 
    148          1.1  christos 
    149          1.1  christos static int supp_mlme_setprotection(void *ctx, const u8 *addr,
    150          1.1  christos 				   int protection_type, int key_type)
    151          1.1  christos {
    152          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
    153          1.1  christos 		   "key_type=%d)",
    154          1.1  christos 		   __func__, MAC2STR(addr), protection_type, key_type);
    155          1.1  christos 	return 0;
    156          1.1  christos }
    157          1.1  christos 
    158          1.1  christos 
    159          1.1  christos static void supp_cancel_auth_timeout(void *ctx)
    160          1.1  christos {
    161          1.1  christos 	wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
    162          1.1  christos }
    163          1.1  christos 
    164          1.1  christos 
    165  1.1.1.1.6.1      yamt static void supp_deauthenticate(void * ctx, int reason_code)
    166  1.1.1.1.6.1      yamt {
    167  1.1.1.1.6.1      yamt 	wpa_printf(MSG_DEBUG, "SUPP: %s (TODO)", __func__);
    168  1.1.1.1.6.1      yamt }
    169  1.1.1.1.6.1      yamt 
    170  1.1.1.1.6.1      yamt 
    171          1.1  christos int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
    172          1.1  christos 		       const u8 *psk)
    173          1.1  christos {
    174          1.1  christos 	struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
    175          1.1  christos 	if (ctx == NULL)
    176          1.1  christos 		return -1;
    177          1.1  christos 
    178          1.1  christos 	ctx->ctx = peer;
    179          1.1  christos 	ctx->msg_ctx = peer->ibss_rsn->wpa_s;
    180          1.1  christos 	ctx->set_state = supp_set_state;
    181  1.1.1.1.6.1      yamt 	ctx->get_state = supp_get_state;
    182          1.1  christos 	ctx->ether_send = supp_ether_send;
    183          1.1  christos 	ctx->get_beacon_ie = supp_get_beacon_ie;
    184          1.1  christos 	ctx->alloc_eapol = supp_alloc_eapol;
    185          1.1  christos 	ctx->set_key = supp_set_key;
    186          1.1  christos 	ctx->get_network_ctx = supp_get_network_ctx;
    187          1.1  christos 	ctx->mlme_setprotection = supp_mlme_setprotection;
    188          1.1  christos 	ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
    189  1.1.1.1.6.1      yamt 	ctx->deauthenticate = supp_deauthenticate;
    190          1.1  christos 	peer->supp = wpa_sm_init(ctx);
    191          1.1  christos 	if (peer->supp == NULL) {
    192          1.1  christos 		wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
    193          1.1  christos 		return -1;
    194          1.1  christos 	}
    195          1.1  christos 
    196          1.1  christos 	wpa_sm_set_own_addr(peer->supp, own_addr);
    197          1.1  christos 	wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
    198          1.1  christos 	wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
    199          1.1  christos 	wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
    200          1.1  christos 	wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
    201          1.1  christos 	wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
    202          1.1  christos 	wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
    203          1.1  christos 
    204          1.1  christos 	peer->supp_ie_len = sizeof(peer->supp_ie);
    205          1.1  christos 	if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
    206          1.1  christos 					    &peer->supp_ie_len) < 0) {
    207          1.1  christos 		wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
    208          1.1  christos 			   " failed");
    209          1.1  christos 		return -1;
    210          1.1  christos 	}
    211          1.1  christos 
    212          1.1  christos 	wpa_sm_notify_assoc(peer->supp, peer->addr);
    213          1.1  christos 
    214          1.1  christos 	return 0;
    215          1.1  christos }
    216          1.1  christos 
    217          1.1  christos 
    218          1.1  christos static void auth_logger(void *ctx, const u8 *addr, logger_level level,
    219          1.1  christos 			const char *txt)
    220          1.1  christos {
    221          1.1  christos 	if (addr)
    222          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
    223          1.1  christos 			   MAC2STR(addr), txt);
    224          1.1  christos 	else
    225          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
    226          1.1  christos }
    227          1.1  christos 
    228          1.1  christos 
    229          1.1  christos static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
    230          1.1  christos {
    231          1.1  christos 	struct ibss_rsn *ibss_rsn = ctx;
    232          1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
    233          1.1  christos 		   __func__, MAC2STR(addr), prev_psk);
    234          1.1  christos 	if (prev_psk)
    235          1.1  christos 		return NULL;
    236          1.1  christos 	return ibss_rsn->psk;
    237          1.1  christos }
    238          1.1  christos 
    239          1.1  christos 
    240          1.1  christos static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
    241          1.1  christos 			   size_t data_len, int encrypt)
    242          1.1  christos {
    243          1.1  christos 	struct ibss_rsn *ibss_rsn = ctx;
    244          1.1  christos 	struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
    245          1.1  christos 
    246          1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
    247          1.1  christos 		   "encrypt=%d)",
    248          1.1  christos 		   __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
    249          1.1  christos 
    250          1.1  christos 	if (wpa_s->l2)
    251          1.1  christos 		return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
    252          1.1  christos 				      data_len);
    253          1.1  christos 
    254          1.1  christos 	return wpa_drv_send_eapol(wpa_s, addr, ETH_P_EAPOL, data, data_len);
    255          1.1  christos }
    256          1.1  christos 
    257          1.1  christos 
    258          1.1  christos static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
    259          1.1  christos 			const u8 *addr, int idx, u8 *key, size_t key_len)
    260          1.1  christos {
    261          1.1  christos 	struct ibss_rsn *ibss_rsn = ctx;
    262          1.1  christos 	u8 seq[6];
    263          1.1  christos 
    264          1.1  christos 	os_memset(seq, 0, sizeof(seq));
    265          1.1  christos 
    266          1.1  christos 	if (addr) {
    267          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
    268          1.1  christos 			   " key_idx=%d)",
    269          1.1  christos 			   __func__, alg, MAC2STR(addr), idx);
    270          1.1  christos 	} else {
    271          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
    272          1.1  christos 			   __func__, alg, idx);
    273          1.1  christos 	}
    274          1.1  christos 	wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
    275          1.1  christos 
    276          1.1  christos 	if (idx == 0) {
    277          1.1  christos 		/*
    278          1.1  christos 		 * In IBSS RSN, the pairwise key from the 4-way handshake
    279          1.1  christos 		 * initiated by the peer with highest MAC address is used.
    280          1.1  christos 		 */
    281          1.1  christos 		if (addr == NULL ||
    282          1.1  christos 		    os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
    283          1.1  christos 			wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
    284          1.1  christos 			return 0;
    285          1.1  christos 		}
    286          1.1  christos 	}
    287          1.1  christos 
    288          1.1  christos 	return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
    289          1.1  christos 			       1, seq, 6, key, key_len);
    290          1.1  christos }
    291          1.1  christos 
    292          1.1  christos 
    293  1.1.1.1.6.1      yamt static int auth_for_each_sta(void *ctx, int (*cb)(struct wpa_state_machine *sm,
    294  1.1.1.1.6.1      yamt 						  void *ctx),
    295  1.1.1.1.6.1      yamt 			     void *cb_ctx)
    296  1.1.1.1.6.1      yamt {
    297  1.1.1.1.6.1      yamt 	struct ibss_rsn *ibss_rsn = ctx;
    298  1.1.1.1.6.1      yamt 	struct ibss_rsn_peer *peer;
    299  1.1.1.1.6.1      yamt 
    300  1.1.1.1.6.1      yamt 	wpa_printf(MSG_DEBUG, "AUTH: for_each_sta");
    301  1.1.1.1.6.1      yamt 
    302  1.1.1.1.6.1      yamt 	for (peer = ibss_rsn->peers; peer; peer = peer->next) {
    303  1.1.1.1.6.1      yamt 		if (peer->auth && cb(peer->auth, cb_ctx))
    304  1.1.1.1.6.1      yamt 			return 1;
    305  1.1.1.1.6.1      yamt 	}
    306  1.1.1.1.6.1      yamt 
    307  1.1.1.1.6.1      yamt 	return 0;
    308  1.1.1.1.6.1      yamt }
    309  1.1.1.1.6.1      yamt 
    310  1.1.1.1.6.1      yamt 
    311          1.1  christos static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
    312          1.1  christos 				    const u8 *own_addr)
    313          1.1  christos {
    314          1.1  christos 	struct wpa_auth_config conf;
    315          1.1  christos 	struct wpa_auth_callbacks cb;
    316          1.1  christos 
    317          1.1  christos 	wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
    318          1.1  christos 
    319          1.1  christos 	os_memset(&conf, 0, sizeof(conf));
    320          1.1  christos 	conf.wpa = 2;
    321          1.1  christos 	conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
    322          1.1  christos 	conf.wpa_pairwise = WPA_CIPHER_CCMP;
    323          1.1  christos 	conf.rsn_pairwise = WPA_CIPHER_CCMP;
    324          1.1  christos 	conf.wpa_group = WPA_CIPHER_CCMP;
    325          1.1  christos 	conf.eapol_version = 2;
    326  1.1.1.1.6.1      yamt 	conf.wpa_group_rekey = 600;
    327          1.1  christos 
    328          1.1  christos 	os_memset(&cb, 0, sizeof(cb));
    329          1.1  christos 	cb.ctx = ibss_rsn;
    330          1.1  christos 	cb.logger = auth_logger;
    331          1.1  christos 	cb.send_eapol = auth_send_eapol;
    332          1.1  christos 	cb.get_psk = auth_get_psk;
    333          1.1  christos 	cb.set_key = auth_set_key;
    334  1.1.1.1.6.1      yamt 	cb.for_each_sta = auth_for_each_sta;
    335          1.1  christos 
    336          1.1  christos 	ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
    337          1.1  christos 	if (ibss_rsn->auth_group == NULL) {
    338          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
    339          1.1  christos 		return -1;
    340          1.1  christos 	}
    341          1.1  christos 
    342  1.1.1.1.6.1      yamt 	wpa_init_keys(ibss_rsn->auth_group);
    343  1.1.1.1.6.1      yamt 
    344          1.1  christos 	return 0;
    345          1.1  christos }
    346          1.1  christos 
    347          1.1  christos 
    348          1.1  christos static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
    349          1.1  christos 			      struct ibss_rsn_peer *peer)
    350          1.1  christos {
    351          1.1  christos 	peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr);
    352          1.1  christos 	if (peer->auth == NULL) {
    353          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
    354          1.1  christos 		return -1;
    355          1.1  christos 	}
    356          1.1  christos 
    357          1.1  christos 	/* TODO: get peer RSN IE with Probe Request */
    358          1.1  christos 	if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
    359          1.1  christos 				(u8 *) "\x30\x14\x01\x00"
    360          1.1  christos 				"\x00\x0f\xac\x04"
    361          1.1  christos 				"\x01\x00\x00\x0f\xac\x04"
    362          1.1  christos 				"\x01\x00\x00\x0f\xac\x02"
    363          1.1  christos 				"\x00\x00", 22, NULL, 0) !=
    364          1.1  christos 	    WPA_IE_OK) {
    365          1.1  christos 		wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
    366          1.1  christos 		return -1;
    367          1.1  christos 	}
    368          1.1  christos 
    369          1.1  christos 	if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
    370          1.1  christos 		return -1;
    371          1.1  christos 
    372          1.1  christos 	if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
    373          1.1  christos 		return -1;
    374          1.1  christos 
    375          1.1  christos 	return 0;
    376          1.1  christos }
    377          1.1  christos 
    378          1.1  christos 
    379          1.1  christos int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
    380          1.1  christos {
    381          1.1  christos 	struct ibss_rsn_peer *peer;
    382          1.1  christos 
    383  1.1.1.1.6.1      yamt 	if (ibss_rsn == NULL)
    384  1.1.1.1.6.1      yamt 		return -1;
    385  1.1.1.1.6.1      yamt 
    386  1.1.1.1.6.1      yamt 	for (peer = ibss_rsn->peers; peer; peer = peer->next) {
    387  1.1.1.1.6.1      yamt 		if (os_memcmp(addr, peer->addr, ETH_ALEN) == 0) {
    388  1.1.1.1.6.1      yamt 			wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator and "
    389  1.1.1.1.6.1      yamt 				   "Supplicant for peer " MACSTR " already "
    390  1.1.1.1.6.1      yamt 				   "running", MAC2STR(addr));
    391  1.1.1.1.6.1      yamt 			return 0;
    392  1.1.1.1.6.1      yamt 		}
    393  1.1.1.1.6.1      yamt 	}
    394  1.1.1.1.6.1      yamt 
    395          1.1  christos 	wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator and "
    396          1.1  christos 		   "Supplicant for peer " MACSTR, MAC2STR(addr));
    397          1.1  christos 
    398          1.1  christos 	peer = os_zalloc(sizeof(*peer));
    399          1.1  christos 	if (peer == NULL)
    400          1.1  christos 		return -1;
    401          1.1  christos 
    402          1.1  christos 	peer->ibss_rsn = ibss_rsn;
    403          1.1  christos 	os_memcpy(peer->addr, addr, ETH_ALEN);
    404          1.1  christos 
    405          1.1  christos 	if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr, ibss_rsn->psk)
    406          1.1  christos 	    < 0) {
    407          1.1  christos 		ibss_rsn_free(peer);
    408          1.1  christos 		return -1;
    409          1.1  christos 	}
    410          1.1  christos 
    411          1.1  christos 	if (ibss_rsn_auth_init(ibss_rsn, peer) < 0) {
    412          1.1  christos 		ibss_rsn_free(peer);
    413          1.1  christos 		return -1;
    414          1.1  christos 	}
    415          1.1  christos 
    416          1.1  christos 	peer->next = ibss_rsn->peers;
    417          1.1  christos 	ibss_rsn->peers = peer;
    418          1.1  christos 
    419          1.1  christos 	return 0;
    420          1.1  christos }
    421          1.1  christos 
    422          1.1  christos 
    423  1.1.1.1.6.1      yamt void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
    424  1.1.1.1.6.1      yamt {
    425  1.1.1.1.6.1      yamt 	struct ibss_rsn_peer *peer, *prev;
    426  1.1.1.1.6.1      yamt 
    427  1.1.1.1.6.1      yamt 	if (ibss_rsn == NULL)
    428  1.1.1.1.6.1      yamt 		return;
    429  1.1.1.1.6.1      yamt 
    430  1.1.1.1.6.1      yamt 	if (peermac == NULL) {
    431  1.1.1.1.6.1      yamt 		/* remove all peers */
    432  1.1.1.1.6.1      yamt 		wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
    433  1.1.1.1.6.1      yamt 		peer = ibss_rsn->peers;
    434  1.1.1.1.6.1      yamt 		while (peer) {
    435  1.1.1.1.6.1      yamt 			prev = peer;
    436  1.1.1.1.6.1      yamt 			peer = peer->next;
    437  1.1.1.1.6.1      yamt 			ibss_rsn_free(prev);
    438  1.1.1.1.6.1      yamt 			ibss_rsn->peers = peer;
    439  1.1.1.1.6.1      yamt 		}
    440  1.1.1.1.6.1      yamt 	} else {
    441  1.1.1.1.6.1      yamt 		/* remove specific peer */
    442  1.1.1.1.6.1      yamt 		wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
    443  1.1.1.1.6.1      yamt 			   __func__, MAC2STR(peermac));
    444  1.1.1.1.6.1      yamt 
    445  1.1.1.1.6.1      yamt 		for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
    446  1.1.1.1.6.1      yamt 		     prev = peer, peer = peer->next) {
    447  1.1.1.1.6.1      yamt 			if (os_memcmp(peermac, peer->addr, ETH_ALEN) == 0) {
    448  1.1.1.1.6.1      yamt 				if (prev == NULL)
    449  1.1.1.1.6.1      yamt 					ibss_rsn->peers = peer->next;
    450  1.1.1.1.6.1      yamt 				else
    451  1.1.1.1.6.1      yamt 					prev->next = peer->next;
    452  1.1.1.1.6.1      yamt 				ibss_rsn_free(peer);
    453  1.1.1.1.6.1      yamt 				wpa_printf(MSG_DEBUG, "%s: Successfully "
    454  1.1.1.1.6.1      yamt 					   "removed a specific peer",
    455  1.1.1.1.6.1      yamt 					   __func__);
    456  1.1.1.1.6.1      yamt 				break;
    457  1.1.1.1.6.1      yamt 			}
    458  1.1.1.1.6.1      yamt 		}
    459  1.1.1.1.6.1      yamt 	}
    460  1.1.1.1.6.1      yamt }
    461  1.1.1.1.6.1      yamt 
    462  1.1.1.1.6.1      yamt 
    463          1.1  christos struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
    464          1.1  christos {
    465          1.1  christos 	struct ibss_rsn *ibss_rsn;
    466          1.1  christos 
    467          1.1  christos 	ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
    468          1.1  christos 	if (ibss_rsn == NULL)
    469          1.1  christos 		return NULL;
    470          1.1  christos 	ibss_rsn->wpa_s = wpa_s;
    471          1.1  christos 
    472          1.1  christos 	if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
    473          1.1  christos 		ibss_rsn_deinit(ibss_rsn);
    474          1.1  christos 		return NULL;
    475          1.1  christos 	}
    476          1.1  christos 
    477          1.1  christos 	return ibss_rsn;
    478          1.1  christos }
    479          1.1  christos 
    480          1.1  christos 
    481          1.1  christos void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
    482          1.1  christos {
    483          1.1  christos 	struct ibss_rsn_peer *peer, *prev;
    484          1.1  christos 
    485          1.1  christos 	if (ibss_rsn == NULL)
    486          1.1  christos 		return;
    487          1.1  christos 
    488          1.1  christos 	peer = ibss_rsn->peers;
    489          1.1  christos 	while (peer) {
    490          1.1  christos 		prev = peer;
    491          1.1  christos 		peer = peer->next;
    492          1.1  christos 		ibss_rsn_free(prev);
    493          1.1  christos 	}
    494          1.1  christos 
    495          1.1  christos 	wpa_deinit(ibss_rsn->auth_group);
    496          1.1  christos 	os_free(ibss_rsn);
    497          1.1  christos 
    498          1.1  christos }
    499          1.1  christos 
    500          1.1  christos 
    501          1.1  christos static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
    502          1.1  christos {
    503          1.1  christos 	const struct ieee802_1x_hdr *hdr;
    504          1.1  christos 	const struct wpa_eapol_key *key;
    505          1.1  christos 	u16 key_info;
    506          1.1  christos 	size_t plen;
    507          1.1  christos 
    508          1.1  christos 	/* TODO: Support other EAPOL packets than just EAPOL-Key */
    509          1.1  christos 
    510          1.1  christos 	if (len < sizeof(*hdr) + sizeof(*key))
    511          1.1  christos 		return -1;
    512          1.1  christos 
    513          1.1  christos 	hdr = (const struct ieee802_1x_hdr *) buf;
    514          1.1  christos 	key = (const struct wpa_eapol_key *) (hdr + 1);
    515          1.1  christos 	plen = be_to_host16(hdr->length);
    516          1.1  christos 
    517          1.1  christos 	if (hdr->version < EAPOL_VERSION) {
    518          1.1  christos 		/* TODO: backwards compatibility */
    519          1.1  christos 	}
    520          1.1  christos 	if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
    521          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
    522          1.1  christos 			"not a Key frame", hdr->type);
    523          1.1  christos 		return -1;
    524          1.1  christos 	}
    525          1.1  christos 	if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
    526          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
    527          1.1  christos 			   "invalid (frame size %lu)",
    528          1.1  christos 			   (unsigned long) plen, (unsigned long) len);
    529          1.1  christos 		return -1;
    530          1.1  christos 	}
    531          1.1  christos 
    532          1.1  christos 	if (key->type != EAPOL_KEY_TYPE_RSN) {
    533          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
    534          1.1  christos 			   "discarded", key->type);
    535          1.1  christos 		return -1;
    536          1.1  christos 	}
    537          1.1  christos 
    538          1.1  christos 	key_info = WPA_GET_BE16(key->key_info);
    539          1.1  christos 
    540          1.1  christos 	return !!(key_info & WPA_KEY_INFO_ACK);
    541          1.1  christos }
    542          1.1  christos 
    543          1.1  christos 
    544          1.1  christos static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
    545          1.1  christos 				     struct ibss_rsn_peer *peer,
    546          1.1  christos 				     const u8 *buf, size_t len)
    547          1.1  christos {
    548          1.1  christos 	int supp;
    549          1.1  christos 	u8 *tmp;
    550          1.1  christos 
    551          1.1  christos 	supp = ibss_rsn_eapol_dst_supp(buf, len);
    552          1.1  christos 	if (supp < 0)
    553          1.1  christos 		return -1;
    554          1.1  christos 
    555          1.1  christos 	tmp = os_malloc(len);
    556          1.1  christos 	if (tmp == NULL)
    557          1.1  christos 		return -1;
    558          1.1  christos 	os_memcpy(tmp, buf, len);
    559          1.1  christos 	if (supp) {
    560          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant");
    561          1.1  christos 		wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
    562          1.1  christos 	} else {
    563          1.1  christos 		wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator");
    564          1.1  christos 		wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
    565          1.1  christos 	}
    566          1.1  christos 	os_free(tmp);
    567          1.1  christos 
    568          1.1  christos 	return 1;
    569          1.1  christos }
    570          1.1  christos 
    571          1.1  christos 
    572          1.1  christos int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
    573          1.1  christos 		      const u8 *buf, size_t len)
    574          1.1  christos {
    575          1.1  christos 	struct ibss_rsn_peer *peer;
    576          1.1  christos 
    577  1.1.1.1.6.1      yamt 	if (ibss_rsn == NULL)
    578  1.1.1.1.6.1      yamt 		return -1;
    579  1.1.1.1.6.1      yamt 
    580          1.1  christos 	for (peer = ibss_rsn->peers; peer; peer = peer->next) {
    581          1.1  christos 		if (os_memcmp(src_addr, peer->addr, ETH_ALEN) == 0)
    582          1.1  christos 			return ibss_rsn_process_rx_eapol(ibss_rsn, peer,
    583          1.1  christos 							 buf, len);
    584          1.1  christos 	}
    585          1.1  christos 
    586          1.1  christos 	if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
    587          1.1  christos 		/*
    588          1.1  christos 		 * Create new IBSS peer based on an EAPOL message from the peer
    589          1.1  christos 		 * Authenticator.
    590          1.1  christos 		 */
    591          1.1  christos 		if (ibss_rsn_start(ibss_rsn, src_addr) < 0)
    592          1.1  christos 			return -1;
    593          1.1  christos 		return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
    594          1.1  christos 						 buf, len);
    595          1.1  christos 	}
    596          1.1  christos 
    597          1.1  christos 	return 0;
    598          1.1  christos }
    599          1.1  christos 
    600          1.1  christos 
    601          1.1  christos void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
    602          1.1  christos {
    603  1.1.1.1.6.1      yamt 	if (ibss_rsn == NULL)
    604  1.1.1.1.6.1      yamt 		return;
    605          1.1  christos 	os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
    606          1.1  christos }
    607