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