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