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