Added initial step for IBSS RSN support
[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 "wpa_supplicant_i.h"
19 #include "wpa.h"
20 #include "wpa_ie.h"
21 #include "../hostapd/wpa.h"
22 #include "ibss_rsn.h"
23
24
25 static void ibss_rsn_free(struct ibss_rsn_peer *peer)
26 {
27         wpa_auth_sta_deinit(peer->auth);
28         wpa_sm_deinit(peer->supp);
29         os_free(peer);
30 }
31
32
33 static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
34                            size_t len)
35 {
36         /* struct ibss_rsn_peer *peer = ctx; */
37
38         wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
39                    "len=%lu)",
40                    __func__, MAC2STR(dest), proto, (unsigned long) len);
41
42         /* TODO: send EAPOL frame */
43
44         return 0;
45 }
46
47
48 static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
49                              u16 data_len, size_t *msg_len, void **data_pos)
50 {
51         struct ieee802_1x_hdr *hdr;
52
53         wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
54                    __func__, type, data_len);
55
56         *msg_len = sizeof(*hdr) + data_len;
57         hdr = os_malloc(*msg_len);
58         if (hdr == NULL)
59                 return NULL;
60
61         hdr->version = 2;
62         hdr->type = type;
63         hdr->length = host_to_be16(data_len);
64
65         if (data)
66                 os_memcpy(hdr + 1, data, data_len);
67         else
68                 os_memset(hdr + 1, 0, data_len);
69
70         if (data_pos)
71                 *data_pos = hdr + 1;
72
73         return (u8 *) hdr;
74 }
75
76
77 static int supp_get_beacon_ie(void *ctx)
78 {
79         wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
80         /* TODO */
81         return -1;
82 }
83
84
85 static int supp_set_key(void *ctx, wpa_alg alg,
86                         const u8 *addr, int key_idx, int set_tx,
87                         const u8 *seq, size_t seq_len,
88                         const u8 *key, size_t key_len)
89 {
90         wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
91                    "set_tx=%d)",
92                    __func__, alg, MAC2STR(addr), key_idx, set_tx);
93         wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
94         wpa_hexdump(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
95         return 0;
96 }
97
98
99 static int supp_mlme_setprotection(void *ctx, const u8 *addr,
100                                    int protection_type, int key_type)
101 {
102         wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
103                    "key_type=%d)",
104                    __func__, MAC2STR(addr), protection_type, key_type);
105         return 0;
106 }
107
108
109 static void supp_cancel_auth_timeout(void *ctx)
110 {
111         wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
112 }
113
114
115 int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
116                        const u8 *psk)
117 {
118         struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
119         if (ctx == NULL)
120                 return -1;
121
122         ctx->ctx = peer;
123         ctx->ether_send = supp_ether_send;
124         ctx->get_beacon_ie = supp_get_beacon_ie;
125         ctx->alloc_eapol = supp_alloc_eapol;
126         ctx->set_key = supp_set_key;
127         ctx->mlme_setprotection = supp_mlme_setprotection;
128         ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
129         peer->supp = wpa_sm_init(ctx);
130         if (peer->supp == NULL) {
131                 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
132                 return -1;
133         }
134
135         wpa_sm_set_own_addr(peer->supp, own_addr);
136         wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
137         wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
138         wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
139         wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
140         wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
141         wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
142
143 #if 0 /* TODO? */
144         peer->supp_ie_len = sizeof(peer->supp_ie);
145         if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
146                                             &peer->supp_ie_len) < 0) {
147                 wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
148                            " failed");
149                 return -1;
150         }
151 #endif
152
153         wpa_sm_notify_assoc(peer->supp, peer->addr);
154
155         return 0;
156 }
157
158
159 static void auth_logger(void *ctx, const u8 *addr, logger_level level,
160                         const char *txt)
161 {
162         if (addr)
163                 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
164                            MAC2STR(addr), txt);
165         else
166                 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
167 }
168
169
170 static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
171 {
172         struct ibss_rsn *ibss_rsn = ctx;
173         wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
174                    __func__, MAC2STR(addr), prev_psk);
175         if (prev_psk)
176                 return NULL;
177         return ibss_rsn->psk;
178 }
179
180
181 static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
182                            size_t data_len, int encrypt)
183 {
184         /* struct ibss_rsn *ibss_rsn = ctx; */
185
186         wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
187                    "encrypt=%d)",
188                    __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
189
190         /* TODO: send EAPOL frame */
191
192         return 0;
193 }
194
195
196 static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
197                                     const u8 *own_addr)
198 {
199         struct wpa_auth_config conf;
200         struct wpa_auth_callbacks cb;
201
202         wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
203
204         os_memset(&conf, 0, sizeof(conf));
205         conf.wpa = 2;
206         conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
207         conf.wpa_pairwise = WPA_CIPHER_CCMP;
208         conf.rsn_pairwise = WPA_CIPHER_CCMP;
209         conf.wpa_group = WPA_CIPHER_CCMP;
210         conf.eapol_version = 2;
211
212         os_memset(&cb, 0, sizeof(cb));
213         cb.ctx = ibss_rsn;
214         cb.logger = auth_logger;
215         cb.send_eapol = auth_send_eapol;
216         cb.get_psk = auth_get_psk;
217
218         ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
219         if (ibss_rsn->auth_group == NULL) {
220                 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
221                 return -1;
222         }
223
224         return 0;
225 }
226
227
228 static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
229                               struct ibss_rsn_peer *peer)
230 {
231         peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr);
232         if (peer->auth == NULL) {
233                 wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
234                 return -1;
235         }
236
237 #if 0 /* TODO: get peer RSN IE with Probe Request */
238         if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth, PEER_IE,
239                                 PEER_IE_LEN, NULL, 0) != WPA_IE_OK) {
240                 wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
241                 return -1;
242         }
243 #endif
244
245         wpa_auth_sm_event(peer->auth, WPA_ASSOC);
246
247         wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth);
248
249         return 0;
250 }
251
252
253 int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
254 {
255         struct ibss_rsn_peer *peer;
256
257         wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator and "
258                    "Supplicant for peer " MACSTR, MAC2STR(addr));
259
260         peer = os_zalloc(sizeof(*peer));
261         if (peer == NULL)
262                 return -1;
263
264         os_memcpy(peer->addr, addr, ETH_ALEN);
265
266         if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr, ibss_rsn->psk)
267             < 0) {
268                 ibss_rsn_free(peer);
269                 return -1;
270         }
271
272         if (ibss_rsn_auth_init(ibss_rsn, peer) < 0) {
273                 ibss_rsn_free(peer);
274                 return -1;
275         }
276
277         peer->next = ibss_rsn->peers;
278         ibss_rsn->peers = peer;
279
280         return 0;
281 }
282
283
284 struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
285 {
286         struct ibss_rsn *ibss_rsn;
287
288         ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
289         if (ibss_rsn == NULL)
290                 return NULL;
291         ibss_rsn->wpa_s = wpa_s;
292
293         if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
294                 ibss_rsn_deinit(ibss_rsn);
295                 return NULL;
296         }
297
298         return ibss_rsn;
299 }
300
301
302 void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
303 {
304         struct ibss_rsn_peer *peer, *prev;
305
306         if (ibss_rsn == NULL)
307                 return;
308
309         peer = ibss_rsn->peers;
310         while (peer) {
311                 prev = peer;
312                 peer = peer->next;
313                 ibss_rsn_free(prev);
314         }
315
316         wpa_deinit(ibss_rsn->auth_group);
317         os_free(ibss_rsn);
318
319 }