IBSS RSN: Add peer restart detection
[mech_eap.git] / wpa_supplicant / ibss_rsn.c
1 /*
2  * wpa_supplicant - IBSS RSN
3  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
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"
17 #include "driver_i.h"
18 #include "common/ieee802_11_defs.h"
19 #include "ibss_rsn.h"
20
21
22 static struct ibss_rsn_peer * ibss_rsn_get_peer(struct ibss_rsn *ibss_rsn,
23                                                 const u8 *addr)
24 {
25         struct ibss_rsn_peer *peer;
26
27         for (peer = ibss_rsn->peers; peer; peer = peer->next)
28                 if (os_memcmp(addr, peer->addr, ETH_ALEN) == 0)
29                         break;
30         return peer;
31 }
32
33
34 static void ibss_rsn_free(struct ibss_rsn_peer *peer)
35 {
36         wpa_auth_sta_deinit(peer->auth);
37         wpa_sm_deinit(peer->supp);
38         os_free(peer);
39 }
40
41
42 static void supp_set_state(void *ctx, enum wpa_states state)
43 {
44         struct ibss_rsn_peer *peer = ctx;
45         peer->supp_state = state;
46 }
47
48
49 static enum wpa_states supp_get_state(void *ctx)
50 {
51         struct ibss_rsn_peer *peer = ctx;
52         return peer->supp_state;
53 }
54
55
56 static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
57                            size_t len)
58 {
59         struct ibss_rsn_peer *peer = ctx;
60         struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
61
62         wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
63                    "len=%lu)",
64                    __func__, MAC2STR(dest), proto, (unsigned long) len);
65
66         if (wpa_s->l2)
67                 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
68
69         return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
70 }
71
72
73 static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
74                              u16 data_len, size_t *msg_len, void **data_pos)
75 {
76         struct ieee802_1x_hdr *hdr;
77
78         wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
79                    __func__, type, data_len);
80
81         *msg_len = sizeof(*hdr) + data_len;
82         hdr = os_malloc(*msg_len);
83         if (hdr == NULL)
84                 return NULL;
85
86         hdr->version = 2;
87         hdr->type = type;
88         hdr->length = host_to_be16(data_len);
89
90         if (data)
91                 os_memcpy(hdr + 1, data, data_len);
92         else
93                 os_memset(hdr + 1, 0, data_len);
94
95         if (data_pos)
96                 *data_pos = hdr + 1;
97
98         return (u8 *) hdr;
99 }
100
101
102 static int supp_get_beacon_ie(void *ctx)
103 {
104         struct ibss_rsn_peer *peer = ctx;
105
106         wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
107         /* TODO: get correct RSN IE */
108         return wpa_sm_set_ap_rsn_ie(peer->supp,
109                                     (u8 *) "\x30\x14\x01\x00"
110                                     "\x00\x0f\xac\x04"
111                                     "\x01\x00\x00\x0f\xac\x04"
112                                     "\x01\x00\x00\x0f\xac\x02"
113                                     "\x00\x00", 22);
114 }
115
116
117 static int supp_set_key(void *ctx, enum wpa_alg alg,
118                         const u8 *addr, int key_idx, int set_tx,
119                         const u8 *seq, size_t seq_len,
120                         const u8 *key, size_t key_len)
121 {
122         struct ibss_rsn_peer *peer = ctx;
123
124         wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
125                    "set_tx=%d)",
126                    __func__, alg, MAC2STR(addr), key_idx, set_tx);
127         wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
128         wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
129
130         if (key_idx == 0) {
131                 /*
132                  * In IBSS RSN, the pairwise key from the 4-way handshake
133                  * initiated by the peer with highest MAC address is used.
134                  */
135                 if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
136                               ETH_ALEN) > 0) {
137                         wpa_printf(MSG_DEBUG, "SUPP: Do not use this PTK");
138                         return 0;
139                 }
140         }
141
142         if (is_broadcast_ether_addr(addr))
143                 addr = peer->addr;
144         return wpa_drv_set_key(peer->ibss_rsn->wpa_s, alg, addr, key_idx,
145                                set_tx, seq, seq_len, key, key_len);
146 }
147
148
149 static void * supp_get_network_ctx(void *ctx)
150 {
151         struct ibss_rsn_peer *peer = ctx;
152         return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
153 }
154
155
156 static int supp_mlme_setprotection(void *ctx, const u8 *addr,
157                                    int protection_type, int key_type)
158 {
159         wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
160                    "key_type=%d)",
161                    __func__, MAC2STR(addr), protection_type, key_type);
162         return 0;
163 }
164
165
166 static void supp_cancel_auth_timeout(void *ctx)
167 {
168         wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
169 }
170
171
172 static void supp_deauthenticate(void * ctx, int reason_code)
173 {
174         wpa_printf(MSG_DEBUG, "SUPP: %s (TODO)", __func__);
175 }
176
177
178 static int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
179                               const u8 *psk)
180 {
181         struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
182         if (ctx == NULL)
183                 return -1;
184
185         ctx->ctx = peer;
186         ctx->msg_ctx = peer->ibss_rsn->wpa_s;
187         ctx->set_state = supp_set_state;
188         ctx->get_state = supp_get_state;
189         ctx->ether_send = supp_ether_send;
190         ctx->get_beacon_ie = supp_get_beacon_ie;
191         ctx->alloc_eapol = supp_alloc_eapol;
192         ctx->set_key = supp_set_key;
193         ctx->get_network_ctx = supp_get_network_ctx;
194         ctx->mlme_setprotection = supp_mlme_setprotection;
195         ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
196         ctx->deauthenticate = supp_deauthenticate;
197         peer->supp = wpa_sm_init(ctx);
198         if (peer->supp == NULL) {
199                 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
200                 return -1;
201         }
202
203         wpa_sm_set_own_addr(peer->supp, own_addr);
204         wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
205         wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
206         wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
207         wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
208         wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
209         wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
210
211         peer->supp_ie_len = sizeof(peer->supp_ie);
212         if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
213                                             &peer->supp_ie_len) < 0) {
214                 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
215                            " failed");
216                 return -1;
217         }
218
219         wpa_sm_notify_assoc(peer->supp, peer->addr);
220
221         return 0;
222 }
223
224
225 static void auth_logger(void *ctx, const u8 *addr, logger_level level,
226                         const char *txt)
227 {
228         if (addr)
229                 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
230                            MAC2STR(addr), txt);
231         else
232                 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
233 }
234
235
236 static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
237 {
238         struct ibss_rsn *ibss_rsn = ctx;
239         wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
240                    __func__, MAC2STR(addr), prev_psk);
241         if (prev_psk)
242                 return NULL;
243         return ibss_rsn->psk;
244 }
245
246
247 static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
248                            size_t data_len, int encrypt)
249 {
250         struct ibss_rsn *ibss_rsn = ctx;
251         struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
252
253         wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
254                    "encrypt=%d)",
255                    __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
256
257         if (wpa_s->l2)
258                 return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
259                                       data_len);
260
261         return wpa_drv_send_eapol(wpa_s, addr, ETH_P_EAPOL, data, data_len);
262 }
263
264
265 static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
266                         const u8 *addr, int idx, u8 *key, size_t key_len)
267 {
268         struct ibss_rsn *ibss_rsn = ctx;
269         u8 seq[6];
270
271         os_memset(seq, 0, sizeof(seq));
272
273         if (addr) {
274                 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
275                            " key_idx=%d)",
276                            __func__, alg, MAC2STR(addr), idx);
277         } else {
278                 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
279                            __func__, alg, idx);
280         }
281         wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
282
283         if (idx == 0) {
284                 /*
285                  * In IBSS RSN, the pairwise key from the 4-way handshake
286                  * initiated by the peer with highest MAC address is used.
287                  */
288                 if (addr == NULL ||
289                     os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
290                         wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
291                         return 0;
292                 }
293         }
294
295         return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
296                                1, seq, 6, key, key_len);
297 }
298
299
300 static void ibss_rsn_disconnect(void *ctx, const u8 *addr, u16 reason)
301 {
302         struct ibss_rsn *ibss_rsn = ctx;
303         wpa_drv_sta_deauth(ibss_rsn->wpa_s, addr, reason);
304 }
305
306
307 static int auth_for_each_sta(void *ctx, int (*cb)(struct wpa_state_machine *sm,
308                                                   void *ctx),
309                              void *cb_ctx)
310 {
311         struct ibss_rsn *ibss_rsn = ctx;
312         struct ibss_rsn_peer *peer;
313
314         wpa_printf(MSG_DEBUG, "AUTH: for_each_sta");
315
316         for (peer = ibss_rsn->peers; peer; peer = peer->next) {
317                 if (peer->auth && cb(peer->auth, cb_ctx))
318                         return 1;
319         }
320
321         return 0;
322 }
323
324
325 static void ibss_set_sta_authorized(struct ibss_rsn *ibss_rsn,
326                                     struct ibss_rsn_peer *peer, int authorized)
327 {
328         int res;
329
330         if (authorized) {
331                 res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
332                                             WPA_STA_AUTHORIZED,
333                                             WPA_STA_AUTHORIZED, ~0);
334                 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " authorizing port",
335                            MAC2STR(peer->addr));
336         } else {
337                 res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
338                                             0, 0, ~WPA_STA_AUTHORIZED);
339                 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " unauthorizing port",
340                            MAC2STR(peer->addr));
341         }
342
343         if (res && errno != ENOENT) {
344                 wpa_printf(MSG_DEBUG, "Could not set station " MACSTR " flags "
345                            "for kernel driver (errno=%d)",
346                            MAC2STR(peer->addr), errno);
347         }
348 }
349
350
351 static void auth_set_eapol(void *ctx, const u8 *addr,
352                                        wpa_eapol_variable var, int value)
353 {
354         struct ibss_rsn *ibss_rsn = ctx;
355         struct ibss_rsn_peer *peer = ibss_rsn_get_peer(ibss_rsn, addr);
356
357         if (peer == NULL)
358                 return;
359
360         switch (var) {
361         case WPA_EAPOL_authorized:
362                 ibss_set_sta_authorized(ibss_rsn, peer, value);
363                 break;
364         default:
365                 /* do not handle any other event */
366                 wpa_printf(MSG_DEBUG, "AUTH: eapol event not handled %d", var);
367                 break;
368         }
369 }
370
371
372 static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
373                                     const u8 *own_addr)
374 {
375         struct wpa_auth_config conf;
376         struct wpa_auth_callbacks cb;
377
378         wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
379
380         os_memset(&conf, 0, sizeof(conf));
381         conf.wpa = 2;
382         conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
383         conf.wpa_pairwise = WPA_CIPHER_CCMP;
384         conf.rsn_pairwise = WPA_CIPHER_CCMP;
385         conf.wpa_group = WPA_CIPHER_CCMP;
386         conf.eapol_version = 2;
387         conf.wpa_group_rekey = 600;
388
389         os_memset(&cb, 0, sizeof(cb));
390         cb.ctx = ibss_rsn;
391         cb.logger = auth_logger;
392         cb.set_eapol = auth_set_eapol;
393         cb.send_eapol = auth_send_eapol;
394         cb.get_psk = auth_get_psk;
395         cb.set_key = auth_set_key;
396         cb.for_each_sta = auth_for_each_sta;
397         cb.disconnect = ibss_rsn_disconnect;
398
399         ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
400         if (ibss_rsn->auth_group == NULL) {
401                 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
402                 return -1;
403         }
404
405         wpa_init_keys(ibss_rsn->auth_group);
406
407         return 0;
408 }
409
410
411 static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
412                               struct ibss_rsn_peer *peer)
413 {
414         peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr);
415         if (peer->auth == NULL) {
416                 wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
417                 return -1;
418         }
419
420         /* TODO: get peer RSN IE with Probe Request */
421         if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
422                                 (u8 *) "\x30\x14\x01\x00"
423                                 "\x00\x0f\xac\x04"
424                                 "\x01\x00\x00\x0f\xac\x04"
425                                 "\x01\x00\x00\x0f\xac\x02"
426                                 "\x00\x00", 22, NULL, 0) !=
427             WPA_IE_OK) {
428                 wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
429                 return -1;
430         }
431
432         if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
433                 return -1;
434
435         if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
436                 return -1;
437
438         return 0;
439 }
440
441
442 static int ibss_rsn_send_auth(struct ibss_rsn *ibss_rsn, const u8 *da, int seq)
443 {
444         struct ieee80211_mgmt auth;
445         const size_t auth_length = IEEE80211_HDRLEN + sizeof(auth.u.auth);
446         struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
447
448         if (wpa_s->driver->send_frame == NULL)
449                 return -1;
450
451         os_memset(&auth, 0, sizeof(auth));
452
453         auth.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
454                                           WLAN_FC_STYPE_AUTH);
455         os_memcpy(auth.da, da, ETH_ALEN);
456         os_memcpy(auth.sa, wpa_s->own_addr, ETH_ALEN);
457         os_memcpy(auth.bssid, wpa_s->bssid, ETH_ALEN);
458
459         auth.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
460         auth.u.auth.auth_transaction = host_to_le16(seq);
461         auth.u.auth.status_code = host_to_le16(WLAN_STATUS_SUCCESS);
462
463         wpa_printf(MSG_DEBUG, "RSN: IBSS TX Auth frame (SEQ %d) to " MACSTR,
464                    seq, MAC2STR(da));
465
466         return wpa_s->driver->send_frame(wpa_s->drv_priv, (u8 *) &auth,
467                                          auth_length, 0);
468 }
469
470
471 static int ibss_rsn_is_auth_started(struct ibss_rsn_peer * peer)
472 {
473         return peer->authentication_status &
474                (IBSS_RSN_AUTH_BY_US | IBSS_RSN_AUTH_EAPOL_BY_US);
475 }
476
477
478 static struct ibss_rsn_peer *
479 ibss_rsn_peer_init(struct ibss_rsn *ibss_rsn, const u8 *addr)
480 {
481         struct ibss_rsn_peer *peer;
482         if (ibss_rsn == NULL)
483                 return NULL;
484
485         peer = ibss_rsn_get_peer(ibss_rsn, addr);
486         if (peer) {
487                 wpa_printf(MSG_DEBUG, "RSN: IBSS Supplicant for peer "MACSTR
488                            " already running", MAC2STR(addr));
489                 return peer;
490         }
491
492         wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Supplicant for peer "MACSTR,
493                    MAC2STR(addr));
494
495         peer = os_zalloc(sizeof(*peer));
496         if (peer == NULL) {
497                 wpa_printf(MSG_DEBUG, "RSN: Could not allocate memory.");
498                 return NULL;
499         }
500
501         peer->ibss_rsn = ibss_rsn;
502         os_memcpy(peer->addr, addr, ETH_ALEN);
503         peer->authentication_status = IBSS_RSN_AUTH_NOT_AUTHENTICATED;
504
505         if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr,
506                                ibss_rsn->psk) < 0) {
507                 ibss_rsn_free(peer);
508                 return NULL;
509         }
510
511         peer->next = ibss_rsn->peers;
512         ibss_rsn->peers = peer;
513
514         return peer;
515 }
516
517
518 int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
519 {
520         struct ibss_rsn_peer *peer;
521         int res;
522
523         /* if the peer already exists, exit immediately */
524         peer = ibss_rsn_get_peer(ibss_rsn, addr);
525         if (peer)
526                 return 0;
527
528         peer = ibss_rsn_peer_init(ibss_rsn, addr);
529         if (peer == NULL)
530                 return -1;
531
532         /* Open Authentication: send first Authentication frame */
533         res = ibss_rsn_send_auth(ibss_rsn, addr, 1);
534         if (res) {
535                 /*
536                  * The driver may not support Authentication frame exchange in
537                  * IBSS. Ignore authentication and go through EAPOL exchange.
538                  */
539                 peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
540                 return ibss_rsn_auth_init(ibss_rsn, peer);
541         }
542
543         return 0;
544 }
545
546
547 static int ibss_rsn_peer_authenticated(struct ibss_rsn *ibss_rsn,
548                                        struct ibss_rsn_peer *peer, int reason)
549 {
550         int already_started;
551
552         if (ibss_rsn == NULL || peer == NULL)
553                 return -1;
554
555         already_started = ibss_rsn_is_auth_started(peer);
556         peer->authentication_status |= reason;
557
558         if (already_started) {
559                 wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator already "
560                            "started for peer " MACSTR, MAC2STR(peer->addr));
561                 return 0;
562         }
563
564         wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator "
565                    "for now-authenticated peer " MACSTR, MAC2STR(peer->addr));
566
567         return ibss_rsn_auth_init(ibss_rsn, peer);
568 }
569
570
571 void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
572 {
573         struct ibss_rsn_peer *peer, *prev;
574
575         if (ibss_rsn == NULL)
576                 return;
577
578         if (peermac == NULL) {
579                 /* remove all peers */
580                 wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
581                 peer = ibss_rsn->peers;
582                 while (peer) {
583                         prev = peer;
584                         peer = peer->next;
585                         ibss_rsn_free(prev);
586                         ibss_rsn->peers = peer;
587                 }
588         } else {
589                 /* remove specific peer */
590                 wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
591                            __func__, MAC2STR(peermac));
592
593                 for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
594                      prev = peer, peer = peer->next) {
595                         if (os_memcmp(peermac, peer->addr, ETH_ALEN) == 0) {
596                                 if (prev == NULL)
597                                         ibss_rsn->peers = peer->next;
598                                 else
599                                         prev->next = peer->next;
600                                 ibss_rsn_free(peer);
601                                 wpa_printf(MSG_DEBUG, "%s: Successfully "
602                                            "removed a specific peer",
603                                            __func__);
604                                 break;
605                         }
606                 }
607         }
608 }
609
610
611 struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
612 {
613         struct ibss_rsn *ibss_rsn;
614
615         ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
616         if (ibss_rsn == NULL)
617                 return NULL;
618         ibss_rsn->wpa_s = wpa_s;
619
620         if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
621                 ibss_rsn_deinit(ibss_rsn);
622                 return NULL;
623         }
624
625         return ibss_rsn;
626 }
627
628
629 void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
630 {
631         struct ibss_rsn_peer *peer, *prev;
632
633         if (ibss_rsn == NULL)
634                 return;
635
636         peer = ibss_rsn->peers;
637         while (peer) {
638                 prev = peer;
639                 peer = peer->next;
640                 ibss_rsn_free(prev);
641         }
642
643         wpa_deinit(ibss_rsn->auth_group);
644         os_free(ibss_rsn);
645
646 }
647
648
649 static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
650 {
651         const struct ieee802_1x_hdr *hdr;
652         const struct wpa_eapol_key *key;
653         u16 key_info;
654         size_t plen;
655
656         /* TODO: Support other EAPOL packets than just EAPOL-Key */
657
658         if (len < sizeof(*hdr) + sizeof(*key))
659                 return -1;
660
661         hdr = (const struct ieee802_1x_hdr *) buf;
662         key = (const struct wpa_eapol_key *) (hdr + 1);
663         plen = be_to_host16(hdr->length);
664
665         if (hdr->version < EAPOL_VERSION) {
666                 /* TODO: backwards compatibility */
667         }
668         if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
669                 wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
670                         "not a Key frame", hdr->type);
671                 return -1;
672         }
673         if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
674                 wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
675                            "invalid (frame size %lu)",
676                            (unsigned long) plen, (unsigned long) len);
677                 return -1;
678         }
679
680         if (key->type != EAPOL_KEY_TYPE_RSN) {
681                 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
682                            "discarded", key->type);
683                 return -1;
684         }
685
686         key_info = WPA_GET_BE16(key->key_info);
687
688         return !!(key_info & WPA_KEY_INFO_ACK);
689 }
690
691
692 static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
693                                      struct ibss_rsn_peer *peer,
694                                      const u8 *buf, size_t len)
695 {
696         int supp;
697         u8 *tmp;
698
699         supp = ibss_rsn_eapol_dst_supp(buf, len);
700         if (supp < 0)
701                 return -1;
702
703         tmp = os_malloc(len);
704         if (tmp == NULL)
705                 return -1;
706         os_memcpy(tmp, buf, len);
707         if (supp) {
708                 peer->authentication_status |= IBSS_RSN_AUTH_EAPOL_BY_PEER;
709                 wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant from "
710                            MACSTR, MAC2STR(peer->addr));
711                 wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
712         } else {
713                 if (ibss_rsn_is_auth_started(peer) == 0) {
714                         wpa_printf(MSG_DEBUG, "RSN: IBSS EAPOL for "
715                                    "Authenticator dropped as " MACSTR " is not "
716                                    "authenticated", MAC2STR(peer->addr));
717                         os_free(tmp);
718                         return -1;
719                 }
720
721                 wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator "
722                            "from "MACSTR, MAC2STR(peer->addr));
723                 wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
724         }
725         os_free(tmp);
726
727         return 1;
728 }
729
730
731 int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
732                       const u8 *buf, size_t len)
733 {
734         struct ibss_rsn_peer *peer;
735
736         if (ibss_rsn == NULL)
737                 return -1;
738
739         peer = ibss_rsn_get_peer(ibss_rsn, src_addr);
740         if (peer)
741                 return ibss_rsn_process_rx_eapol(ibss_rsn, peer, buf, len);
742
743         if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
744                 /*
745                  * Create new IBSS peer based on an EAPOL message from the peer
746                  * Authenticator.
747                  */
748                 peer = ibss_rsn_peer_init(ibss_rsn, src_addr);
749                 if (peer == NULL)
750                         return -1;
751
752                 /* assume the peer is authenticated already */
753                 wpa_printf(MSG_DEBUG, "RSN: IBSS Not using IBSS Auth for peer "
754                            MACSTR, MAC2STR(src_addr));
755                 ibss_rsn_peer_authenticated(ibss_rsn, peer,
756                                             IBSS_RSN_AUTH_EAPOL_BY_US);
757
758                 return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
759                                                  buf, len);
760         }
761
762         return 0;
763 }
764
765 void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
766 {
767         if (ibss_rsn == NULL)
768                 return;
769         os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
770 }
771
772
773 static void ibss_rsn_handle_auth_1_of_2(struct ibss_rsn *ibss_rsn,
774                                         struct ibss_rsn_peer *peer,
775                                         const u8* addr)
776 {
777         wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 1) from " MACSTR,
778                    MAC2STR(addr));
779
780         if (peer &&
781             peer->authentication_status & IBSS_RSN_AUTH_EAPOL_BY_PEER) {
782                 /*
783                  * A peer sent us an Authentication frame even though it already
784                  * started an EAPOL session. We should reinit state machines
785                  * here, but it's much more complicated than just deleting and
786                  * recreating the state machine
787                  */
788                 wpa_printf(MSG_DEBUG, "RSN: IBSS Reinitializing station "
789                            MACSTR, MAC2STR(addr));
790
791                 ibss_rsn_stop(ibss_rsn, addr);
792                 peer = NULL;
793         }
794
795         if (!peer) {
796                 peer = ibss_rsn_peer_init(ibss_rsn, addr);
797                 if (!peer)
798                         return;
799
800                 wpa_printf(MSG_DEBUG, "RSN: IBSS Auth started by peer " MACSTR,
801                            MAC2STR(addr));
802         }
803
804         /* reply with an Authentication frame now, before sending an EAPOL */
805         ibss_rsn_send_auth(ibss_rsn, addr, 2);
806         /* no need to start another AUTH challenge in the other way.. */
807         ibss_rsn_peer_authenticated(ibss_rsn, peer, IBSS_RSN_AUTH_EAPOL_BY_US);
808 }
809
810
811 void ibss_rsn_handle_auth(struct ibss_rsn *ibss_rsn, const u8 *auth_frame,
812                           size_t len)
813 {
814         const struct ieee80211_mgmt *header;
815         struct ibss_rsn_peer *peer;
816         size_t auth_length;
817
818         header = (const struct ieee80211_mgmt *) auth_frame;
819         auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
820
821         if (ibss_rsn == NULL || len < auth_length)
822                 return;
823
824         if (le_to_host16(header->u.auth.auth_alg) != WLAN_AUTH_OPEN ||
825             le_to_host16(header->u.auth.status_code) != WLAN_STATUS_SUCCESS)
826                 return;
827
828         peer = ibss_rsn_get_peer(ibss_rsn, header->sa);
829
830         switch (le_to_host16(header->u.auth.auth_transaction)) {
831         case 1:
832                 ibss_rsn_handle_auth_1_of_2(ibss_rsn, peer, header->sa);
833                 break;
834         case 2:
835                 wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 2) from "
836                            MACSTR, MAC2STR(header->sa));
837                 if (!peer) {
838                         wpa_printf(MSG_DEBUG, "RSN: Received Auth seq 2 from "
839                                    "unknown STA " MACSTR, MAC2STR(header->sa));
840                         break;
841                 }
842
843                 /* authentication has been completed */
844                 wpa_printf(MSG_DEBUG, "RSN: IBSS Auth completed with "MACSTR,
845                            MAC2STR(header->sa));
846                 ibss_rsn_peer_authenticated(ibss_rsn, peer,
847                                             IBSS_RSN_AUTH_BY_US);
848                 break;
849         }
850 }