2 * wpa_supplicant - IBSS RSN
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "wpa_supplicant_i.h"
21 #include "../hostapd/wpa.h"
25 static void ibss_rsn_free(struct ibss_rsn_peer *peer)
27 wpa_auth_sta_deinit(peer->auth);
28 wpa_sm_deinit(peer->supp);
33 static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
36 /* struct ibss_rsn_peer *peer = ctx; */
38 wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
40 __func__, MAC2STR(dest), proto, (unsigned long) len);
42 /* TODO: send EAPOL frame */
48 static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
49 u16 data_len, size_t *msg_len, void **data_pos)
51 struct ieee802_1x_hdr *hdr;
53 wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
54 __func__, type, data_len);
56 *msg_len = sizeof(*hdr) + data_len;
57 hdr = os_malloc(*msg_len);
63 hdr->length = host_to_be16(data_len);
66 os_memcpy(hdr + 1, data, data_len);
68 os_memset(hdr + 1, 0, data_len);
77 static int supp_get_beacon_ie(void *ctx)
79 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
85 static int supp_set_key(void *ctx, wpa_alg alg,
86 const u8 *addr, int key_idx, int set_tx,
87 const u8 *seq, size_t seq_len,
88 const u8 *key, size_t key_len)
90 wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
92 __func__, alg, MAC2STR(addr), key_idx, set_tx);
93 wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
94 wpa_hexdump(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
99 static int supp_mlme_setprotection(void *ctx, const u8 *addr,
100 int protection_type, int key_type)
102 wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
104 __func__, MAC2STR(addr), protection_type, key_type);
109 static void supp_cancel_auth_timeout(void *ctx)
111 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
115 int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
118 struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
123 ctx->ether_send = supp_ether_send;
124 ctx->get_beacon_ie = supp_get_beacon_ie;
125 ctx->alloc_eapol = supp_alloc_eapol;
126 ctx->set_key = supp_set_key;
127 ctx->mlme_setprotection = supp_mlme_setprotection;
128 ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
129 peer->supp = wpa_sm_init(ctx);
130 if (peer->supp == NULL) {
131 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
135 wpa_sm_set_own_addr(peer->supp, own_addr);
136 wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
137 wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
138 wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
139 wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
140 wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
141 wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
144 peer->supp_ie_len = sizeof(peer->supp_ie);
145 if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
146 &peer->supp_ie_len) < 0) {
147 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
153 wpa_sm_notify_assoc(peer->supp, peer->addr);
159 static void auth_logger(void *ctx, const u8 *addr, logger_level level,
163 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
166 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
170 static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
172 struct ibss_rsn *ibss_rsn = ctx;
173 wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
174 __func__, MAC2STR(addr), prev_psk);
177 return ibss_rsn->psk;
181 static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
182 size_t data_len, int encrypt)
184 /* struct ibss_rsn *ibss_rsn = ctx; */
186 wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
188 __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
190 /* TODO: send EAPOL frame */
196 static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
199 struct wpa_auth_config conf;
200 struct wpa_auth_callbacks cb;
202 wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
204 os_memset(&conf, 0, sizeof(conf));
206 conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
207 conf.wpa_pairwise = WPA_CIPHER_CCMP;
208 conf.rsn_pairwise = WPA_CIPHER_CCMP;
209 conf.wpa_group = WPA_CIPHER_CCMP;
210 conf.eapol_version = 2;
212 os_memset(&cb, 0, sizeof(cb));
214 cb.logger = auth_logger;
215 cb.send_eapol = auth_send_eapol;
216 cb.get_psk = auth_get_psk;
218 ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
219 if (ibss_rsn->auth_group == NULL) {
220 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
228 static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
229 struct ibss_rsn_peer *peer)
231 peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr);
232 if (peer->auth == NULL) {
233 wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
237 #if 0 /* TODO: get peer RSN IE with Probe Request */
238 if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth, PEER_IE,
239 PEER_IE_LEN, NULL, 0) != WPA_IE_OK) {
240 wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
245 wpa_auth_sm_event(peer->auth, WPA_ASSOC);
247 wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth);
253 int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
255 struct ibss_rsn_peer *peer;
257 wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator and "
258 "Supplicant for peer " MACSTR, MAC2STR(addr));
260 peer = os_zalloc(sizeof(*peer));
264 os_memcpy(peer->addr, addr, ETH_ALEN);
266 if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr, ibss_rsn->psk)
272 if (ibss_rsn_auth_init(ibss_rsn, peer) < 0) {
277 peer->next = ibss_rsn->peers;
278 ibss_rsn->peers = peer;
284 struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
286 struct ibss_rsn *ibss_rsn;
288 ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
289 if (ibss_rsn == NULL)
291 ibss_rsn->wpa_s = wpa_s;
293 if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
294 ibss_rsn_deinit(ibss_rsn);
302 void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
304 struct ibss_rsn_peer *peer, *prev;
306 if (ibss_rsn == NULL)
309 peer = ibss_rsn->peers;
316 wpa_deinit(ibss_rsn->auth_group);