2 * wpa_supplicant - IBSS RSN
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "l2_packet/l2_packet.h"
13 #include "rsn_supp/wpa.h"
14 #include "rsn_supp/wpa_ie.h"
15 #include "ap/wpa_auth.h"
16 #include "wpa_supplicant_i.h"
21 static struct ibss_rsn_peer * ibss_rsn_get_peer(struct ibss_rsn *ibss_rsn,
24 struct ibss_rsn_peer *peer;
26 for (peer = ibss_rsn->peers; peer; peer = peer->next)
27 if (os_memcmp(addr, peer->addr, ETH_ALEN) == 0)
33 static void ibss_rsn_free(struct ibss_rsn_peer *peer)
35 wpa_auth_sta_deinit(peer->auth);
36 wpa_sm_deinit(peer->supp);
41 static void supp_set_state(void *ctx, enum wpa_states state)
43 struct ibss_rsn_peer *peer = ctx;
44 peer->supp_state = state;
48 static enum wpa_states supp_get_state(void *ctx)
50 struct ibss_rsn_peer *peer = ctx;
51 return peer->supp_state;
55 static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
58 struct ibss_rsn_peer *peer = ctx;
59 struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
61 wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
63 __func__, MAC2STR(dest), proto, (unsigned long) len);
66 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
68 return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
72 static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
73 u16 data_len, size_t *msg_len, void **data_pos)
75 struct ieee802_1x_hdr *hdr;
77 wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
78 __func__, type, data_len);
80 *msg_len = sizeof(*hdr) + data_len;
81 hdr = os_malloc(*msg_len);
87 hdr->length = host_to_be16(data_len);
90 os_memcpy(hdr + 1, data, data_len);
92 os_memset(hdr + 1, 0, data_len);
101 static int supp_get_beacon_ie(void *ctx)
103 struct ibss_rsn_peer *peer = ctx;
105 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
106 /* TODO: get correct RSN IE */
107 return wpa_sm_set_ap_rsn_ie(peer->supp,
108 (u8 *) "\x30\x14\x01\x00"
110 "\x01\x00\x00\x0f\xac\x04"
111 "\x01\x00\x00\x0f\xac\x02"
116 static int supp_set_key(void *ctx, enum wpa_alg alg,
117 const u8 *addr, int key_idx, int set_tx,
118 const u8 *seq, size_t seq_len,
119 const u8 *key, size_t key_len)
121 struct ibss_rsn_peer *peer = ctx;
123 wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
125 __func__, alg, MAC2STR(addr), key_idx, set_tx);
126 wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
127 wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
131 * In IBSS RSN, the pairwise key from the 4-way handshake
132 * initiated by the peer with highest MAC address is used.
134 if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
136 wpa_printf(MSG_DEBUG, "SUPP: Do not use this PTK");
141 if (is_broadcast_ether_addr(addr))
143 return wpa_drv_set_key(peer->ibss_rsn->wpa_s, alg, addr, key_idx,
144 set_tx, seq, seq_len, key, key_len);
148 static void * supp_get_network_ctx(void *ctx)
150 struct ibss_rsn_peer *peer = ctx;
151 return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
155 static int supp_mlme_setprotection(void *ctx, const u8 *addr,
156 int protection_type, int key_type)
158 wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
160 __func__, MAC2STR(addr), protection_type, key_type);
165 static void supp_cancel_auth_timeout(void *ctx)
167 wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
171 static void supp_deauthenticate(void * ctx, int reason_code)
173 wpa_printf(MSG_DEBUG, "SUPP: %s (TODO)", __func__);
177 static int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
180 struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
185 ctx->msg_ctx = peer->ibss_rsn->wpa_s;
186 ctx->set_state = supp_set_state;
187 ctx->get_state = supp_get_state;
188 ctx->ether_send = supp_ether_send;
189 ctx->get_beacon_ie = supp_get_beacon_ie;
190 ctx->alloc_eapol = supp_alloc_eapol;
191 ctx->set_key = supp_set_key;
192 ctx->get_network_ctx = supp_get_network_ctx;
193 ctx->mlme_setprotection = supp_mlme_setprotection;
194 ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
195 ctx->deauthenticate = supp_deauthenticate;
196 peer->supp = wpa_sm_init(ctx);
197 if (peer->supp == NULL) {
198 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
202 wpa_sm_set_own_addr(peer->supp, own_addr);
203 wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
204 wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
205 wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
206 wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
207 wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
208 wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
210 peer->supp_ie_len = sizeof(peer->supp_ie);
211 if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
212 &peer->supp_ie_len) < 0) {
213 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
218 wpa_sm_notify_assoc(peer->supp, peer->addr);
224 static void auth_logger(void *ctx, const u8 *addr, logger_level level,
228 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
231 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
235 static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
237 struct ibss_rsn *ibss_rsn = ctx;
238 wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
239 __func__, MAC2STR(addr), prev_psk);
242 return ibss_rsn->psk;
246 static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
247 size_t data_len, int encrypt)
249 struct ibss_rsn *ibss_rsn = ctx;
250 struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
252 wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
254 __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
257 return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
260 return wpa_drv_send_eapol(wpa_s, addr, ETH_P_EAPOL, data, data_len);
264 static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
265 const u8 *addr, int idx, u8 *key, size_t key_len)
267 struct ibss_rsn *ibss_rsn = ctx;
270 os_memset(seq, 0, sizeof(seq));
273 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
275 __func__, alg, MAC2STR(addr), idx);
277 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
280 wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
284 * In IBSS RSN, the pairwise key from the 4-way handshake
285 * initiated by the peer with highest MAC address is used.
288 os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
289 wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
294 return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
295 1, seq, 6, key, key_len);
299 static void ibss_rsn_disconnect(void *ctx, const u8 *addr, u16 reason)
301 struct ibss_rsn *ibss_rsn = ctx;
302 wpa_drv_sta_deauth(ibss_rsn->wpa_s, addr, reason);
306 static int auth_for_each_sta(void *ctx, int (*cb)(struct wpa_state_machine *sm,
310 struct ibss_rsn *ibss_rsn = ctx;
311 struct ibss_rsn_peer *peer;
313 wpa_printf(MSG_DEBUG, "AUTH: for_each_sta");
315 for (peer = ibss_rsn->peers; peer; peer = peer->next) {
316 if (peer->auth && cb(peer->auth, cb_ctx))
324 static void ibss_set_sta_authorized(struct ibss_rsn *ibss_rsn,
325 struct ibss_rsn_peer *peer, int authorized)
330 res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
332 WPA_STA_AUTHORIZED, ~0);
333 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " authorizing port",
334 MAC2STR(peer->addr));
336 res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
337 0, 0, ~WPA_STA_AUTHORIZED);
338 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " unauthorizing port",
339 MAC2STR(peer->addr));
342 if (res && errno != ENOENT) {
343 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR " flags "
344 "for kernel driver (errno=%d)",
345 MAC2STR(peer->addr), errno);
350 static void auth_set_eapol(void *ctx, const u8 *addr,
351 wpa_eapol_variable var, int value)
353 struct ibss_rsn *ibss_rsn = ctx;
354 struct ibss_rsn_peer *peer = ibss_rsn_get_peer(ibss_rsn, addr);
360 case WPA_EAPOL_authorized:
361 ibss_set_sta_authorized(ibss_rsn, peer, value);
364 /* do not handle any other event */
365 wpa_printf(MSG_DEBUG, "AUTH: eapol event not handled %d", var);
371 static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
374 struct wpa_auth_config conf;
375 struct wpa_auth_callbacks cb;
377 wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
379 os_memset(&conf, 0, sizeof(conf));
381 conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
382 conf.wpa_pairwise = WPA_CIPHER_CCMP;
383 conf.rsn_pairwise = WPA_CIPHER_CCMP;
384 conf.wpa_group = WPA_CIPHER_CCMP;
385 conf.eapol_version = 2;
386 conf.wpa_group_rekey = 600;
388 os_memset(&cb, 0, sizeof(cb));
390 cb.logger = auth_logger;
391 cb.set_eapol = auth_set_eapol;
392 cb.send_eapol = auth_send_eapol;
393 cb.get_psk = auth_get_psk;
394 cb.set_key = auth_set_key;
395 cb.for_each_sta = auth_for_each_sta;
396 cb.disconnect = ibss_rsn_disconnect;
398 ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
399 if (ibss_rsn->auth_group == NULL) {
400 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
404 wpa_init_keys(ibss_rsn->auth_group);
410 static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
411 struct ibss_rsn_peer *peer)
413 peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr);
414 if (peer->auth == NULL) {
415 wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
419 /* TODO: get peer RSN IE with Probe Request */
420 if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
421 (u8 *) "\x30\x14\x01\x00"
423 "\x01\x00\x00\x0f\xac\x04"
424 "\x01\x00\x00\x0f\xac\x02"
425 "\x00\x00", 22, NULL, 0) !=
427 wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
431 if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
434 if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
441 int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
443 struct ibss_rsn_peer *peer;
445 if (ibss_rsn == NULL)
448 if (ibss_rsn_get_peer(ibss_rsn, addr)) {
449 wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator and Supplicant "
450 "for peer " MACSTR " already running",
455 wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator and "
456 "Supplicant for peer " MACSTR, MAC2STR(addr));
458 peer = os_zalloc(sizeof(*peer));
462 peer->ibss_rsn = ibss_rsn;
463 os_memcpy(peer->addr, addr, ETH_ALEN);
465 if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr, ibss_rsn->psk)
471 if (ibss_rsn_auth_init(ibss_rsn, peer) < 0) {
476 peer->next = ibss_rsn->peers;
477 ibss_rsn->peers = peer;
483 void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
485 struct ibss_rsn_peer *peer, *prev;
487 if (ibss_rsn == NULL)
490 if (peermac == NULL) {
491 /* remove all peers */
492 wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
493 peer = ibss_rsn->peers;
498 ibss_rsn->peers = peer;
501 /* remove specific peer */
502 wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
503 __func__, MAC2STR(peermac));
505 for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
506 prev = peer, peer = peer->next) {
507 if (os_memcmp(peermac, peer->addr, ETH_ALEN) == 0) {
509 ibss_rsn->peers = peer->next;
511 prev->next = peer->next;
513 wpa_printf(MSG_DEBUG, "%s: Successfully "
514 "removed a specific peer",
523 struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
525 struct ibss_rsn *ibss_rsn;
527 ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
528 if (ibss_rsn == NULL)
530 ibss_rsn->wpa_s = wpa_s;
532 if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
533 ibss_rsn_deinit(ibss_rsn);
541 void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
543 struct ibss_rsn_peer *peer, *prev;
545 if (ibss_rsn == NULL)
548 peer = ibss_rsn->peers;
555 wpa_deinit(ibss_rsn->auth_group);
561 static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
563 const struct ieee802_1x_hdr *hdr;
564 const struct wpa_eapol_key *key;
568 /* TODO: Support other EAPOL packets than just EAPOL-Key */
570 if (len < sizeof(*hdr) + sizeof(*key))
573 hdr = (const struct ieee802_1x_hdr *) buf;
574 key = (const struct wpa_eapol_key *) (hdr + 1);
575 plen = be_to_host16(hdr->length);
577 if (hdr->version < EAPOL_VERSION) {
578 /* TODO: backwards compatibility */
580 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
581 wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
582 "not a Key frame", hdr->type);
585 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
586 wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
587 "invalid (frame size %lu)",
588 (unsigned long) plen, (unsigned long) len);
592 if (key->type != EAPOL_KEY_TYPE_RSN) {
593 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
594 "discarded", key->type);
598 key_info = WPA_GET_BE16(key->key_info);
600 return !!(key_info & WPA_KEY_INFO_ACK);
604 static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
605 struct ibss_rsn_peer *peer,
606 const u8 *buf, size_t len)
611 supp = ibss_rsn_eapol_dst_supp(buf, len);
615 tmp = os_malloc(len);
618 os_memcpy(tmp, buf, len);
620 wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant");
621 wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
623 wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator");
624 wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
632 int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
633 const u8 *buf, size_t len)
635 struct ibss_rsn_peer *peer;
637 if (ibss_rsn == NULL)
640 peer = ibss_rsn_get_peer(ibss_rsn, src_addr);
642 return ibss_rsn_process_rx_eapol(ibss_rsn, peer, buf, len);
644 if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
646 * Create new IBSS peer based on an EAPOL message from the peer
649 if (ibss_rsn_start(ibss_rsn, src_addr) < 0)
651 return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
659 void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
661 if (ibss_rsn == NULL)
663 os_memcpy(ibss_rsn->psk, psk, PMK_LEN);