]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/ap.c
Create a common drivers makefile snippet
[thirdparty/hostap.git] / wpa_supplicant / ap.c
CommitLineData
f1a48710
JM
1/*
2 * WPA Supplicant - Basic AP mode support routines
3 * Copyright (c) 2003-2009, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2009, Atheros Communications
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Alternatively, this software may be distributed under the terms of BSD
11 * license.
12 *
13 * See README and COPYING for more details.
14 */
15
16#include "includes.h"
17
18#include "common.h"
19#include "../hostapd/hostapd.h"
089757c6 20#include "../hostapd/config.h"
0915d02c
JM
21#ifdef NEED_MLME
22#include "../hostapd/ieee802_11.h"
23#endif /* NEED_MLME */
3ec97afe 24#include "../hostapd/wps_hostapd.h"
f1a48710
JM
25#include "eap_common/eap_defs.h"
26#include "eap_server/eap_methods.h"
27#include "eap_common/eap_wsc_common.h"
3ec97afe 28#include "wps/wps.h"
1f1b62a0
JM
29#include "config_ssid.h"
30#include "wpa_supplicant_i.h"
2d5b792d
JM
31#include "driver_i.h"
32#include "ap.h"
f1a48710
JM
33
34
2d5b792d
JM
35int hostapd_for_each_interface(int (*cb)(struct hostapd_iface *iface,
36 void *ctx), void *ctx)
f1a48710
JM
37{
38 /* TODO */
2d5b792d 39 return 0;
f1a48710
JM
40}
41
42
2d5b792d 43int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
f1a48710 44{
f1a48710
JM
45 return 0;
46}
47
48
2d5b792d 49void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
f1a48710 50{
2d5b792d 51}
f1a48710 52
f1a48710 53
2d5b792d
JM
54struct ap_driver_data {
55 struct hostapd_data *hapd;
56};
f1a48710 57
2d5b792d 58
92f475b4
JM
59static void * ap_driver_init(struct hostapd_data *hapd,
60 struct wpa_init_params *params)
2d5b792d
JM
61{
62 struct ap_driver_data *drv;
0892aaaf 63 struct wpa_supplicant *wpa_s = hapd->iface->owner;
2d5b792d
JM
64
65 drv = os_zalloc(sizeof(struct ap_driver_data));
66 if (drv == NULL) {
67 wpa_printf(MSG_ERROR, "Could not allocate memory for AP "
68 "driver data");
69 return NULL;
70 }
71 drv->hapd = hapd;
0892aaaf 72 os_memcpy(hapd->own_addr, wpa_s->own_addr, ETH_ALEN);
2d5b792d
JM
73
74 return drv;
f1a48710
JM
75}
76
77
2d5b792d 78static void ap_driver_deinit(void *priv)
f1a48710 79{
2d5b792d
JM
80 struct ap_driver_data *drv = priv;
81
82 os_free(drv);
f1a48710
JM
83}
84
85
2d5b792d
JM
86static int ap_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
87 u16 proto, const u8 *data, size_t data_len)
f1a48710 88{
0f2b2c19
JM
89 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
90 return -1;
91}
92
93
94static int ap_driver_set_key(const char *iface, void *priv, wpa_alg alg,
95 const u8 *addr, int key_idx, int set_tx,
96 const u8 *seq, size_t seq_len, const u8 *key,
97 size_t key_len)
98{
99 struct ap_driver_data *drv = priv;
100 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
101 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
102 key, key_len);
103}
104
105
106static int ap_driver_get_seqnum(const char *iface, void *priv, const u8 *addr,
107 int idx, u8 *seq)
108{
109 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
110 return -1;
111}
112
113
114static int ap_driver_flush(void *priv)
115{
116 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
117 return -1;
118}
119
120
121static int ap_driver_read_sta_data(void *priv,
122 struct hostap_sta_driver_data *data,
123 const u8 *addr)
124{
125 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
126 return -1;
127}
128
129
130static int ap_driver_sta_set_flags(void *priv, const u8 *addr, int total_flags,
131 int flags_or, int flags_and)
132{
a8d6ffa4
JM
133 struct ap_driver_data *drv = priv;
134 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
135 return wpa_drv_sta_set_flags(wpa_s, addr, total_flags, flags_or,
136 flags_and);
0f2b2c19
JM
137}
138
139
be301e56
JM
140static int ap_driver_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
141 int reason)
0f2b2c19
JM
142{
143 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
144 return -1;
145}
146
147
be301e56
JM
148static int ap_driver_sta_disassoc(void *priv, const u8 *own_addr,
149 const u8 *addr, int reason)
0f2b2c19
JM
150{
151 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
152 return -1;
153}
154
155
156static int ap_driver_sta_remove(void *priv, const u8 *addr)
157{
0f4e8b4f
JM
158 struct ap_driver_data *drv = priv;
159 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
160 return wpa_drv_sta_remove(wpa_s, addr);
0f2b2c19
JM
161}
162
163
be301e56 164static int ap_driver_send_mlme(void *priv, const u8 *data, size_t len)
0f2b2c19 165{
2c2010ac
JM
166 struct ap_driver_data *drv = priv;
167 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
168 return wpa_drv_send_mlme(wpa_s, data, len);
0f2b2c19
JM
169}
170
171
172static int ap_driver_sta_add(const char *ifname, void *priv,
173 struct hostapd_sta_add_params *params)
174{
0f4e8b4f
JM
175 struct ap_driver_data *drv = priv;
176 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
177 return wpa_drv_sta_add(wpa_s, params);
0f2b2c19
JM
178}
179
180
181static int ap_driver_get_inact_sec(void *priv, const u8 *addr)
182{
183 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
184 return -1;
185}
186
187
188static int ap_driver_set_freq(void *priv, struct hostapd_freq_params *freq)
189{
190 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
f1a48710
JM
191 return 0;
192}
193
194
0f2b2c19 195static int ap_driver_set_beacon(const char *iface, void *priv,
d2440ba0
JM
196 const u8 *head, size_t head_len,
197 const u8 *tail, size_t tail_len,
198 int dtim_period)
0f2b2c19 199{
d2440ba0
JM
200 struct ap_driver_data *drv = priv;
201 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
202 return wpa_drv_set_beacon(wpa_s, head, head_len, tail, tail_len,
203 dtim_period);
0f2b2c19
JM
204}
205
206
207static int ap_driver_set_beacon_int(void *priv, int value)
208{
d2440ba0
JM
209 struct ap_driver_data *drv = priv;
210 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
211 return wpa_drv_set_beacon_int(wpa_s, value);
0f2b2c19
JM
212}
213
214
215static int ap_driver_set_cts_protect(void *priv, int value)
216{
217 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
218 return -1;
219}
220
221
222static int ap_driver_set_preamble(void *priv, int value)
223{
224 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
225 return -1;
226}
227
228
229static int ap_driver_set_short_slot_time(void *priv, int value)
230{
231 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
232 return -1;
233}
234
235
236static int ap_driver_set_tx_queue_params(void *priv, int queue, int aifs,
237 int cw_min, int cw_max,
238 int burst_time)
239{
240 wpa_printf(MSG_DEBUG, "AP TODO: %s", __func__);
241 return -1;
242}
243
244
245static struct hostapd_hw_modes *ap_driver_get_hw_feature_data(void *priv,
246 u16 *num_modes,
247 u16 *flags)
248{
282d5590
JM
249 struct ap_driver_data *drv = priv;
250 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
251 return wpa_drv_get_hw_feature_data(wpa_s, num_modes, flags);
0f2b2c19
JM
252}
253
254
db149ac9
JM
255static int ap_driver_hapd_send_eapol(void *priv, const u8 *addr,
256 const u8 *data, size_t data_len,
257 int encrypt, const u8 *own_addr)
258{
259 struct ap_driver_data *drv = priv;
260 struct wpa_supplicant *wpa_s = drv->hapd->iface->owner;
261 return wpa_drv_hapd_send_eapol(wpa_s, addr, data, data_len, encrypt,
262 own_addr);
263}
264
265
c5121837 266struct wpa_driver_ops ap_driver_ops =
f1a48710 267{
2d5b792d 268 .name = "wpa_supplicant",
c5121837
JM
269 .hapd_init = ap_driver_init,
270 .hapd_deinit = ap_driver_deinit,
2d5b792d 271 .send_ether = ap_driver_send_ether,
c5121837 272 .hapd_set_key = ap_driver_set_key,
0f2b2c19
JM
273 .get_seqnum = ap_driver_get_seqnum,
274 .flush = ap_driver_flush,
275 .read_sta_data = ap_driver_read_sta_data,
276 .sta_set_flags = ap_driver_sta_set_flags,
277 .sta_deauth = ap_driver_sta_deauth,
278 .sta_disassoc = ap_driver_sta_disassoc,
279 .sta_remove = ap_driver_sta_remove,
be301e56 280 .send_mlme = ap_driver_send_mlme,
0f2b2c19
JM
281 .sta_add = ap_driver_sta_add,
282 .get_inact_sec = ap_driver_get_inact_sec,
283 .set_freq = ap_driver_set_freq,
c5121837 284 .hapd_set_beacon = ap_driver_set_beacon,
be301e56 285 .set_beacon_int = ap_driver_set_beacon_int,
0f2b2c19
JM
286 .set_cts_protect = ap_driver_set_cts_protect,
287 .set_preamble = ap_driver_set_preamble,
288 .set_short_slot_time = ap_driver_set_short_slot_time,
289 .set_tx_queue_params = ap_driver_set_tx_queue_params,
c3965310 290 .get_hw_feature_data = ap_driver_get_hw_feature_data,
db149ac9 291 .hapd_send_eapol = ap_driver_hapd_send_eapol,
2d5b792d 292};
f1a48710 293
f1a48710 294
c5121837 295extern struct wpa_driver_ops *wpa_drivers[];
07f117ed
JM
296
297static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
298 struct wpa_ssid *ssid,
299 struct hostapd_config *conf)
300{
301 struct hostapd_bss_config *bss = &conf->bss[0];
07d9a552 302 int j, pairwise;
c5121837
JM
303
304 for (j = 0; wpa_drivers[j]; j++) {
305 if (os_strcmp("wpa_supplicant", wpa_drivers[j]->name) == 0) {
306 conf->driver = wpa_drivers[j];
307 break;
308 }
309 }
310 if (conf->driver == NULL) {
311 wpa_printf(MSG_ERROR, "No AP driver ops found");
312 return -1;
313 }
07f117ed
JM
314
315 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
316
317 if (ssid->frequency == 0) {
318 /* default channel 11 */
319 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
320 conf->channel = 11;
321 } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
322 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
323 conf->channel = (ssid->frequency - 2407) / 5;
324 } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
325 (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
b615a25e 326 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
07f117ed
JM
327 conf->channel = (ssid->frequency - 5000) / 5;
328 } else {
329 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
330 ssid->frequency);
331 return -1;
332 }
333
334 /* TODO: enable HT if driver supports it;
335 * drop to 11b if driver does not support 11g */
336
337 if (ssid->ssid_len == 0) {
338 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
339 return -1;
340 }
341 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
342 bss->ssid.ssid[ssid->ssid_len] = '\0';
343 bss->ssid.ssid_len = ssid->ssid_len;
344 bss->ssid.ssid_set = 1;
345
346 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
347 bss->wpa = ssid->proto;
348 bss->wpa_key_mgmt = ssid->key_mgmt;
349 bss->wpa_pairwise = ssid->pairwise_cipher;
350 if (ssid->passphrase) {
351 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
07f117ed
JM
352 } else if (ssid->psk_set) {
353 os_free(bss->ssid.wpa_psk);
354 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
355 if (bss->ssid.wpa_psk == NULL)
356 return -1;
357 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
358 bss->ssid.wpa_psk->group = 1;
359 }
360
07d9a552
JM
361 /* Select group cipher based on the enabled pairwise cipher suites */
362 pairwise = 0;
363 if (bss->wpa & 1)
364 pairwise |= bss->wpa_pairwise;
365 if (bss->wpa & 2) {
366 if (bss->rsn_pairwise == 0)
367 bss->rsn_pairwise = bss->wpa_pairwise;
368 pairwise |= bss->rsn_pairwise;
369 }
370 if (pairwise & WPA_CIPHER_TKIP)
371 bss->wpa_group = WPA_CIPHER_TKIP;
372 else
373 bss->wpa_group = WPA_CIPHER_CCMP;
374
375 if (bss->wpa && bss->ieee802_1x)
376 bss->ssid.security_policy = SECURITY_WPA;
377 else if (bss->wpa)
378 bss->ssid.security_policy = SECURITY_WPA_PSK;
379 else if (bss->ieee802_1x) {
380 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
381 bss->ssid.wep.default_len = bss->default_wep_key_len;
382 } else if (bss->ssid.wep.keys_set)
383 bss->ssid.security_policy = SECURITY_STATIC_WEP;
384 else
385 bss->ssid.security_policy = SECURITY_PLAINTEXT;
386
3ec97afe
JM
387#ifdef CONFIG_WPS
388 /*
389 * Enable WPS by default, but require user interaction to actually use
390 * it. Only the internal Registrar is supported.
391 */
392 bss->eap_server = 1;
393 bss->wps_state = 2;
394 bss->ap_setup_locked = 1;
395 bss->config_methods = os_strdup("display push_button");
396#endif /* CONFIG_WPS */
397
07f117ed
JM
398 return 0;
399}
400
401
2d5b792d
JM
402int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
403 struct wpa_ssid *ssid)
404{
405 struct wpa_driver_associate_params params;
406 struct hostapd_iface *hapd_iface;
407 struct hostapd_config *conf;
408 size_t i;
f1a48710 409
2d5b792d
JM
410 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
411 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
412 return -1;
f1a48710
JM
413 }
414
2d5b792d 415 wpa_supplicant_ap_deinit(wpa_s);
d2440ba0
JM
416
417 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
418 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
419
420 os_memset(&params, 0, sizeof(params));
421 params.ssid = ssid->ssid;
422 params.ssid_len = ssid->ssid_len;
423 params.mode = ssid->mode;
424 params.freq = ssid->frequency;
425
426 if (wpa_drv_associate(wpa_s, &params) < 0) {
427 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
428 return -1;
429 }
430
2d5b792d
JM
431 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
432 if (hapd_iface == NULL)
433 return -1;
0f2b2c19 434 hapd_iface->owner = wpa_s;
1f1b62a0 435
2d5b792d
JM
436 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
437 if (conf == NULL) {
438 wpa_supplicant_ap_deinit(wpa_s);
439 return -1;
440 }
1f1b62a0 441
07f117ed
JM
442 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
443 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
444 wpa_supplicant_ap_deinit(wpa_s);
445 return -1;
446 }
447
2d5b792d
JM
448 hapd_iface->num_bss = conf->num_bss;
449 hapd_iface->bss = os_zalloc(conf->num_bss *
450 sizeof(struct hostapd_data *));
451 if (hapd_iface->bss == NULL) {
452 wpa_supplicant_ap_deinit(wpa_s);
453 return -1;
454 }
1f1b62a0 455
2d5b792d
JM
456 for (i = 0; i < conf->num_bss; i++) {
457 hapd_iface->bss[i] =
458 hostapd_alloc_bss_data(hapd_iface, conf,
459 &conf->bss[i]);
460 if (hapd_iface->bss[i] == NULL) {
461 wpa_supplicant_ap_deinit(wpa_s);
462 return -1;
463 }
464 }
465
466 if (hostapd_setup_interface(wpa_s->ap_iface)) {
467 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
468 wpa_supplicant_ap_deinit(wpa_s);
469 return -1;
1f1b62a0
JM
470 }
471
137fb724
WS
472 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
473
2d5b792d
JM
474 return 0;
475}
476
477
478void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
479{
480 if (wpa_s->ap_iface == NULL)
481 return;
482
483 hostapd_interface_deinit(wpa_s->ap_iface);
484 wpa_s->ap_iface = NULL;
1f1b62a0 485}
0915d02c
JM
486
487
488void ap_tx_status(void *ctx, const u8 *addr,
489 const u8 *buf, size_t len, int ack)
490{
491 struct wpa_supplicant *wpa_s = ctx;
492 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
493}
494
495
496void ap_rx_from_unknown_sta(void *ctx, const u8 *addr)
497{
498 struct wpa_supplicant *wpa_s = ctx;
499 ap_rx_from_unknown_sta(wpa_s->ap_iface->bss[0], addr);
500}
501
502
503#ifdef NEED_MLME
504void ap_mgmt_rx(void *ctx, u8 *buf, size_t len, u16 stype,
505 struct hostapd_frame_info *fi)
506{
507 struct wpa_supplicant *wpa_s = ctx;
508 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], buf, len, stype, fi);
509}
510
511
512void ap_mgmt_tx_cb(void *ctx, u8 *buf, size_t len, u16 stype, int ok)
513{
514 struct wpa_supplicant *wpa_s = ctx;
515 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
516}
517#endif /* NEED_MLME */
db149ac9
JM
518
519
520void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
521 const u8 *src_addr, const u8 *buf, size_t len)
522{
523 hostapd_eapol_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
524}
3ec97afe
JM
525
526
527#ifdef CONFIG_WPS
528
529int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
530{
531 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
532}
533
534
535int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
536 const char *pin, char *buf, size_t buflen)
537{
538 int ret, ret_len = 0;
539
540 if (pin == NULL) {
541 unsigned int rpin = wps_generate_pin();
542 ret_len = os_snprintf(buf, buflen, "%d", rpin);
543 pin = buf;
544 }
545
077a781f 546 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], "any", pin, 0);
3ec97afe
JM
547 if (ret)
548 return -1;
549 return ret_len;
550}
551
552#endif /* CONFIG_WPS */