]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/ap.c
P2P: Maintain list of per-client PSKs for persistent groups
[thirdparty/hostap.git] / wpa_supplicant / ap.c
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 software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #include "utils/includes.h"
11
12 #include "utils/common.h"
13 #include "utils/eloop.h"
14 #include "utils/uuid.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/wpa_ctrl.h"
17 #include "eapol_supp/eapol_supp_sm.h"
18 #include "ap/hostapd.h"
19 #include "ap/ap_config.h"
20 #include "ap/ap_drv_ops.h"
21 #ifdef NEED_AP_MLME
22 #include "ap/ieee802_11.h"
23 #endif /* NEED_AP_MLME */
24 #include "ap/beacon.h"
25 #include "ap/ieee802_1x.h"
26 #include "ap/wps_hostapd.h"
27 #include "ap/ctrl_iface_ap.h"
28 #include "wps/wps.h"
29 #include "common/ieee802_11_defs.h"
30 #include "config_ssid.h"
31 #include "config.h"
32 #include "wpa_supplicant_i.h"
33 #include "driver_i.h"
34 #include "p2p_supplicant.h"
35 #include "ap.h"
36 #include "ap/sta_info.h"
37 #include "notify.h"
38
39
40 #ifdef CONFIG_WPS
41 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
42 #endif /* CONFIG_WPS */
43
44
45 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
46 struct wpa_ssid *ssid,
47 struct hostapd_config *conf)
48 {
49 struct hostapd_bss_config *bss = &conf->bss[0];
50
51 conf->driver = wpa_s->driver;
52
53 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
54
55 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
56 &conf->channel);
57 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
58 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
59 ssid->frequency);
60 return -1;
61 }
62
63 /* TODO: enable HT40 if driver supports it;
64 * drop to 11b if driver does not support 11g */
65
66 #ifdef CONFIG_IEEE80211N
67 /*
68 * Enable HT20 if the driver supports it, by setting conf->ieee80211n
69 * and a mask of allowed capabilities within conf->ht_capab.
70 * Using default config settings for: conf->ht_op_mode_fixed,
71 * conf->secondary_channel, conf->require_ht
72 */
73 if (wpa_s->hw.modes) {
74 struct hostapd_hw_modes *mode = NULL;
75 int i, no_ht = 0;
76 for (i = 0; i < wpa_s->hw.num_modes; i++) {
77 if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
78 mode = &wpa_s->hw.modes[i];
79 break;
80 }
81 }
82
83 #ifdef CONFIG_HT_OVERRIDES
84 if (ssid->disable_ht) {
85 conf->ieee80211n = 0;
86 conf->ht_capab = 0;
87 no_ht = 1;
88 }
89 #endif /* CONFIG_HT_OVERRIDES */
90
91 if (!no_ht && mode && mode->ht_capab) {
92 conf->ieee80211n = 1;
93 #ifdef CONFIG_P2P
94 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
95 (mode->ht_capab &
96 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
97 ssid->ht40)
98 conf->secondary_channel =
99 wpas_p2p_get_ht40_mode(wpa_s, mode,
100 conf->channel);
101 if (conf->secondary_channel)
102 conf->ht_capab |=
103 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
104 #endif /* CONFIG_P2P */
105
106 /*
107 * white-list capabilities that won't cause issues
108 * to connecting stations, while leaving the current
109 * capabilities intact (currently disabled SMPS).
110 */
111 conf->ht_capab |= mode->ht_capab &
112 (HT_CAP_INFO_GREEN_FIELD |
113 HT_CAP_INFO_SHORT_GI20MHZ |
114 HT_CAP_INFO_SHORT_GI40MHZ |
115 HT_CAP_INFO_RX_STBC_MASK |
116 HT_CAP_INFO_MAX_AMSDU_SIZE);
117 }
118 }
119 #endif /* CONFIG_IEEE80211N */
120
121 #ifdef CONFIG_P2P
122 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
123 (ssid->mode == WPAS_MODE_P2P_GO ||
124 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
125 /* Remove 802.11b rates from supported and basic rate sets */
126 int *list = os_malloc(4 * sizeof(int));
127 if (list) {
128 list[0] = 60;
129 list[1] = 120;
130 list[2] = 240;
131 list[3] = -1;
132 }
133 conf->basic_rates = list;
134
135 list = os_malloc(9 * sizeof(int));
136 if (list) {
137 list[0] = 60;
138 list[1] = 90;
139 list[2] = 120;
140 list[3] = 180;
141 list[4] = 240;
142 list[5] = 360;
143 list[6] = 480;
144 list[7] = 540;
145 list[8] = -1;
146 }
147 conf->supported_rates = list;
148 }
149
150 bss->isolate = !wpa_s->conf->p2p_intra_bss;
151 bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
152 #endif /* CONFIG_P2P */
153
154 if (ssid->ssid_len == 0) {
155 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
156 return -1;
157 }
158 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
159 bss->ssid.ssid_len = ssid->ssid_len;
160 bss->ssid.ssid_set = 1;
161
162 bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
163
164 if (ssid->auth_alg)
165 bss->auth_algs = ssid->auth_alg;
166
167 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
168 bss->wpa = ssid->proto;
169 bss->wpa_key_mgmt = ssid->key_mgmt;
170 bss->wpa_pairwise = ssid->pairwise_cipher;
171 if (ssid->psk_set) {
172 os_free(bss->ssid.wpa_psk);
173 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
174 if (bss->ssid.wpa_psk == NULL)
175 return -1;
176 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
177 bss->ssid.wpa_psk->group = 1;
178 } else if (ssid->passphrase) {
179 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
180 } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
181 ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
182 struct hostapd_wep_keys *wep = &bss->ssid.wep;
183 int i;
184 for (i = 0; i < NUM_WEP_KEYS; i++) {
185 if (ssid->wep_key_len[i] == 0)
186 continue;
187 wep->key[i] = os_malloc(ssid->wep_key_len[i]);
188 if (wep->key[i] == NULL)
189 return -1;
190 os_memcpy(wep->key[i], ssid->wep_key[i],
191 ssid->wep_key_len[i]);
192 wep->len[i] = ssid->wep_key_len[i];
193 }
194 wep->idx = ssid->wep_tx_keyidx;
195 wep->keys_set = 1;
196 }
197
198 if (ssid->ap_max_inactivity)
199 bss->ap_max_inactivity = ssid->ap_max_inactivity;
200
201 if (ssid->dtim_period)
202 bss->dtim_period = ssid->dtim_period;
203 else if (wpa_s->conf->dtim_period)
204 bss->dtim_period = wpa_s->conf->dtim_period;
205
206 if (ssid->beacon_int)
207 conf->beacon_int = ssid->beacon_int;
208 else if (wpa_s->conf->beacon_int)
209 conf->beacon_int = wpa_s->conf->beacon_int;
210
211 if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
212 bss->rsn_pairwise = bss->wpa_pairwise;
213 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
214 bss->rsn_pairwise);
215
216 if (bss->wpa && bss->ieee802_1x)
217 bss->ssid.security_policy = SECURITY_WPA;
218 else if (bss->wpa)
219 bss->ssid.security_policy = SECURITY_WPA_PSK;
220 else if (bss->ieee802_1x) {
221 int cipher = WPA_CIPHER_NONE;
222 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
223 bss->ssid.wep.default_len = bss->default_wep_key_len;
224 if (bss->default_wep_key_len)
225 cipher = bss->default_wep_key_len >= 13 ?
226 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
227 bss->wpa_group = cipher;
228 bss->wpa_pairwise = cipher;
229 bss->rsn_pairwise = cipher;
230 } else if (bss->ssid.wep.keys_set) {
231 int cipher = WPA_CIPHER_WEP40;
232 if (bss->ssid.wep.len[0] >= 13)
233 cipher = WPA_CIPHER_WEP104;
234 bss->ssid.security_policy = SECURITY_STATIC_WEP;
235 bss->wpa_group = cipher;
236 bss->wpa_pairwise = cipher;
237 bss->rsn_pairwise = cipher;
238 } else {
239 bss->ssid.security_policy = SECURITY_PLAINTEXT;
240 bss->wpa_group = WPA_CIPHER_NONE;
241 bss->wpa_pairwise = WPA_CIPHER_NONE;
242 bss->rsn_pairwise = WPA_CIPHER_NONE;
243 }
244
245 if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
246 (bss->wpa_group == WPA_CIPHER_CCMP ||
247 bss->wpa_group == WPA_CIPHER_GCMP)) {
248 /*
249 * Strong ciphers do not need frequent rekeying, so increase
250 * the default GTK rekeying period to 24 hours.
251 */
252 bss->wpa_group_rekey = 86400;
253 }
254
255 #ifdef CONFIG_WPS
256 /*
257 * Enable WPS by default for open and WPA/WPA2-Personal network, but
258 * require user interaction to actually use it. Only the internal
259 * Registrar is supported.
260 */
261 if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
262 bss->ssid.security_policy != SECURITY_PLAINTEXT)
263 goto no_wps;
264 #ifdef CONFIG_WPS2
265 if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
266 (!(bss->rsn_pairwise & WPA_CIPHER_CCMP) || !(bss->wpa & 2)))
267 goto no_wps; /* WPS2 does not allow WPA/TKIP-only
268 * configuration */
269 #endif /* CONFIG_WPS2 */
270 bss->eap_server = 1;
271
272 if (!ssid->ignore_broadcast_ssid)
273 bss->wps_state = 2;
274
275 bss->ap_setup_locked = 2;
276 if (wpa_s->conf->config_methods)
277 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
278 os_memcpy(bss->device_type, wpa_s->conf->device_type,
279 WPS_DEV_TYPE_LEN);
280 if (wpa_s->conf->device_name) {
281 bss->device_name = os_strdup(wpa_s->conf->device_name);
282 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
283 }
284 if (wpa_s->conf->manufacturer)
285 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
286 if (wpa_s->conf->model_name)
287 bss->model_name = os_strdup(wpa_s->conf->model_name);
288 if (wpa_s->conf->model_number)
289 bss->model_number = os_strdup(wpa_s->conf->model_number);
290 if (wpa_s->conf->serial_number)
291 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
292 if (is_nil_uuid(wpa_s->conf->uuid))
293 os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
294 else
295 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
296 os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
297 bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
298 no_wps:
299 #endif /* CONFIG_WPS */
300
301 if (wpa_s->max_stations &&
302 wpa_s->max_stations < wpa_s->conf->max_num_sta)
303 bss->max_num_sta = wpa_s->max_stations;
304 else
305 bss->max_num_sta = wpa_s->conf->max_num_sta;
306
307 bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
308
309 if (wpa_s->conf->ap_vendor_elements) {
310 bss->vendor_elements =
311 wpabuf_dup(wpa_s->conf->ap_vendor_elements);
312 }
313
314 return 0;
315 }
316
317
318 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
319 {
320 #ifdef CONFIG_P2P
321 struct wpa_supplicant *wpa_s = ctx;
322 const struct ieee80211_mgmt *mgmt;
323 size_t hdr_len;
324
325 mgmt = (const struct ieee80211_mgmt *) buf;
326 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
327 if (hdr_len > len)
328 return;
329 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
330 mgmt->u.action.category,
331 &mgmt->u.action.u.vs_public_action.action,
332 len - hdr_len, freq);
333 #endif /* CONFIG_P2P */
334 }
335
336
337 static void ap_wps_event_cb(void *ctx, enum wps_event event,
338 union wps_event_data *data)
339 {
340 #ifdef CONFIG_P2P
341 struct wpa_supplicant *wpa_s = ctx;
342
343 if (event == WPS_EV_FAIL) {
344 struct wps_event_fail *fail = &data->fail;
345
346 if (wpa_s->parent && wpa_s->parent != wpa_s &&
347 wpa_s == wpa_s->global->p2p_group_formation) {
348 /*
349 * src/ap/wps_hostapd.c has already sent this on the
350 * main interface, so only send on the parent interface
351 * here if needed.
352 */
353 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
354 "msg=%d config_error=%d",
355 fail->msg, fail->config_error);
356 }
357 wpas_p2p_wps_failed(wpa_s, fail);
358 }
359 #endif /* CONFIG_P2P */
360 }
361
362
363 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
364 int authorized, const u8 *p2p_dev_addr)
365 {
366 wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
367 }
368
369
370 #ifdef CONFIG_P2P
371 static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
372 const u8 *psk, size_t psk_len)
373 {
374
375 struct wpa_supplicant *wpa_s = ctx;
376 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
377 return;
378 wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
379 }
380 #endif /* CONFIG_P2P */
381
382
383 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
384 {
385 #ifdef CONFIG_P2P
386 struct wpa_supplicant *wpa_s = ctx;
387 const struct ieee80211_mgmt *mgmt;
388 size_t hdr_len;
389
390 mgmt = (const struct ieee80211_mgmt *) buf;
391 hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
392 if (hdr_len > len)
393 return -1;
394 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
395 mgmt->u.action.category,
396 &mgmt->u.action.u.vs_public_action.action,
397 len - hdr_len, freq);
398 #endif /* CONFIG_P2P */
399 return 0;
400 }
401
402
403 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
404 const u8 *bssid, const u8 *ie, size_t ie_len,
405 int ssi_signal)
406 {
407 #ifdef CONFIG_P2P
408 struct wpa_supplicant *wpa_s = ctx;
409 return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
410 ssi_signal);
411 #else /* CONFIG_P2P */
412 return 0;
413 #endif /* CONFIG_P2P */
414 }
415
416
417 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
418 const u8 *uuid_e)
419 {
420 #ifdef CONFIG_P2P
421 struct wpa_supplicant *wpa_s = ctx;
422 wpas_p2p_wps_success(wpa_s, mac_addr, 1);
423 #endif /* CONFIG_P2P */
424 }
425
426
427 static void wpas_ap_configured_cb(void *ctx)
428 {
429 struct wpa_supplicant *wpa_s = ctx;
430
431 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
432
433 if (wpa_s->ap_configured_cb)
434 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
435 wpa_s->ap_configured_cb_data);
436 }
437
438
439 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
440 struct wpa_ssid *ssid)
441 {
442 struct wpa_driver_associate_params params;
443 struct hostapd_iface *hapd_iface;
444 struct hostapd_config *conf;
445 size_t i;
446
447 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
448 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
449 return -1;
450 }
451
452 wpa_supplicant_ap_deinit(wpa_s);
453
454 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
455 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
456
457 os_memset(&params, 0, sizeof(params));
458 params.ssid = ssid->ssid;
459 params.ssid_len = ssid->ssid_len;
460 switch (ssid->mode) {
461 case WPAS_MODE_INFRA:
462 params.mode = IEEE80211_MODE_INFRA;
463 break;
464 case WPAS_MODE_IBSS:
465 params.mode = IEEE80211_MODE_IBSS;
466 break;
467 case WPAS_MODE_AP:
468 case WPAS_MODE_P2P_GO:
469 case WPAS_MODE_P2P_GROUP_FORMATION:
470 params.mode = IEEE80211_MODE_AP;
471 break;
472 }
473 if (ssid->frequency == 0)
474 ssid->frequency = 2462; /* default channel 11 */
475 params.freq = ssid->frequency;
476
477 params.wpa_proto = ssid->proto;
478 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
479 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
480 else
481 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
482 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
483
484 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
485 1);
486 if (wpa_s->pairwise_cipher < 0) {
487 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
488 "cipher.");
489 return -1;
490 }
491 params.pairwise_suite =
492 wpa_cipher_to_suite_driver(wpa_s->pairwise_cipher);
493 params.group_suite = params.pairwise_suite;
494
495 #ifdef CONFIG_P2P
496 if (ssid->mode == WPAS_MODE_P2P_GO ||
497 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
498 params.p2p = 1;
499 #endif /* CONFIG_P2P */
500
501 if (wpa_s->parent->set_ap_uapsd)
502 params.uapsd = wpa_s->parent->ap_uapsd;
503 else
504 params.uapsd = -1;
505
506 if (wpa_drv_associate(wpa_s, &params) < 0) {
507 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
508 #ifdef CONFIG_P2P
509 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION &&
510 wpa_s->global->p2p_group_formation == wpa_s)
511 wpas_p2p_group_formation_failed(wpa_s->parent);
512 #endif /* CONFIG_P2P */
513 return -1;
514 }
515
516 wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
517 if (hapd_iface == NULL)
518 return -1;
519 hapd_iface->owner = wpa_s;
520 hapd_iface->drv_flags = wpa_s->drv_flags;
521 hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
522 hapd_iface->extended_capa = wpa_s->extended_capa;
523 hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
524 hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
525
526 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
527 if (conf == NULL) {
528 wpa_supplicant_ap_deinit(wpa_s);
529 return -1;
530 }
531
532 os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
533 wpa_s->conf->wmm_ac_params,
534 sizeof(wpa_s->conf->wmm_ac_params));
535
536 if (params.uapsd > 0) {
537 conf->bss->wmm_enabled = 1;
538 conf->bss->wmm_uapsd = 1;
539 }
540
541 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
542 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
543 wpa_supplicant_ap_deinit(wpa_s);
544 return -1;
545 }
546
547 #ifdef CONFIG_P2P
548 if (ssid->mode == WPAS_MODE_P2P_GO)
549 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
550 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
551 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
552 P2P_GROUP_FORMATION;
553 #endif /* CONFIG_P2P */
554
555 hapd_iface->num_bss = conf->num_bss;
556 hapd_iface->bss = os_calloc(conf->num_bss,
557 sizeof(struct hostapd_data *));
558 if (hapd_iface->bss == NULL) {
559 wpa_supplicant_ap_deinit(wpa_s);
560 return -1;
561 }
562
563 for (i = 0; i < conf->num_bss; i++) {
564 hapd_iface->bss[i] =
565 hostapd_alloc_bss_data(hapd_iface, conf,
566 &conf->bss[i]);
567 if (hapd_iface->bss[i] == NULL) {
568 wpa_supplicant_ap_deinit(wpa_s);
569 return -1;
570 }
571
572 hapd_iface->bss[i]->msg_ctx = wpa_s;
573 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
574 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
575 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
576 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
577 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
578 hostapd_register_probereq_cb(hapd_iface->bss[i],
579 ap_probe_req_rx, wpa_s);
580 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
581 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
582 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
583 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
584 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
585 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
586 #ifdef CONFIG_P2P
587 hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
588 hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
589 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
590 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
591 ssid);
592 #endif /* CONFIG_P2P */
593 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
594 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
595 }
596
597 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
598 hapd_iface->bss[0]->driver = wpa_s->driver;
599 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
600
601 wpa_s->current_ssid = ssid;
602 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
603 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
604 wpa_s->assoc_freq = ssid->frequency;
605
606 if (hostapd_setup_interface(wpa_s->ap_iface)) {
607 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
608 wpa_supplicant_ap_deinit(wpa_s);
609 return -1;
610 }
611
612 return 0;
613 }
614
615
616 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
617 {
618 #ifdef CONFIG_WPS
619 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
620 #endif /* CONFIG_WPS */
621
622 if (wpa_s->ap_iface == NULL)
623 return;
624
625 wpa_s->current_ssid = NULL;
626 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
627 wpa_s->assoc_freq = 0;
628 #ifdef CONFIG_P2P
629 if (wpa_s->ap_iface->bss)
630 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
631 wpas_p2p_group_deinit(wpa_s);
632 #endif /* CONFIG_P2P */
633 hostapd_interface_deinit(wpa_s->ap_iface);
634 hostapd_interface_free(wpa_s->ap_iface);
635 wpa_s->ap_iface = NULL;
636 wpa_drv_deinit_ap(wpa_s);
637 }
638
639
640 void ap_tx_status(void *ctx, const u8 *addr,
641 const u8 *buf, size_t len, int ack)
642 {
643 #ifdef NEED_AP_MLME
644 struct wpa_supplicant *wpa_s = ctx;
645 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
646 #endif /* NEED_AP_MLME */
647 }
648
649
650 void ap_eapol_tx_status(void *ctx, const u8 *dst,
651 const u8 *data, size_t len, int ack)
652 {
653 #ifdef NEED_AP_MLME
654 struct wpa_supplicant *wpa_s = ctx;
655 hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
656 #endif /* NEED_AP_MLME */
657 }
658
659
660 void ap_client_poll_ok(void *ctx, const u8 *addr)
661 {
662 #ifdef NEED_AP_MLME
663 struct wpa_supplicant *wpa_s = ctx;
664 if (wpa_s->ap_iface)
665 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
666 #endif /* NEED_AP_MLME */
667 }
668
669
670 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
671 {
672 #ifdef NEED_AP_MLME
673 struct wpa_supplicant *wpa_s = ctx;
674 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
675 #endif /* NEED_AP_MLME */
676 }
677
678
679 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
680 {
681 #ifdef NEED_AP_MLME
682 struct wpa_supplicant *wpa_s = ctx;
683 struct hostapd_frame_info fi;
684 os_memset(&fi, 0, sizeof(fi));
685 fi.datarate = rx_mgmt->datarate;
686 fi.ssi_signal = rx_mgmt->ssi_signal;
687 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
688 rx_mgmt->frame_len, &fi);
689 #endif /* NEED_AP_MLME */
690 }
691
692
693 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
694 {
695 #ifdef NEED_AP_MLME
696 struct wpa_supplicant *wpa_s = ctx;
697 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
698 #endif /* NEED_AP_MLME */
699 }
700
701
702 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
703 const u8 *src_addr, const u8 *buf, size_t len)
704 {
705 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
706 }
707
708
709 #ifdef CONFIG_WPS
710
711 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
712 const u8 *p2p_dev_addr)
713 {
714 if (!wpa_s->ap_iface)
715 return -1;
716 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
717 p2p_dev_addr);
718 }
719
720
721 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
722 {
723 struct wps_registrar *reg;
724 int reg_sel = 0, wps_sta = 0;
725
726 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
727 return -1;
728
729 reg = wpa_s->ap_iface->bss[0]->wps->registrar;
730 reg_sel = wps_registrar_wps_cancel(reg);
731 wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
732 ap_sta_wps_cancel, NULL);
733
734 if (!reg_sel && !wps_sta) {
735 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
736 "time");
737 return -1;
738 }
739
740 /*
741 * There are 2 cases to return wps cancel as success:
742 * 1. When wps cancel was initiated but no connection has been
743 * established with client yet.
744 * 2. Client is in the middle of exchanging WPS messages.
745 */
746
747 return 0;
748 }
749
750
751 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
752 const char *pin, char *buf, size_t buflen,
753 int timeout)
754 {
755 int ret, ret_len = 0;
756
757 if (!wpa_s->ap_iface)
758 return -1;
759
760 if (pin == NULL) {
761 unsigned int rpin = wps_generate_pin();
762 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
763 pin = buf;
764 } else
765 ret_len = os_snprintf(buf, buflen, "%s", pin);
766
767 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
768 timeout);
769 if (ret)
770 return -1;
771 return ret_len;
772 }
773
774
775 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
776 {
777 struct wpa_supplicant *wpa_s = eloop_data;
778 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
779 wpas_wps_ap_pin_disable(wpa_s);
780 }
781
782
783 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
784 {
785 struct hostapd_data *hapd;
786
787 if (wpa_s->ap_iface == NULL)
788 return;
789 hapd = wpa_s->ap_iface->bss[0];
790 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
791 hapd->ap_pin_failures = 0;
792 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
793 if (timeout > 0)
794 eloop_register_timeout(timeout, 0,
795 wpas_wps_ap_pin_timeout, wpa_s, NULL);
796 }
797
798
799 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
800 {
801 struct hostapd_data *hapd;
802
803 if (wpa_s->ap_iface == NULL)
804 return;
805 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
806 hapd = wpa_s->ap_iface->bss[0];
807 os_free(hapd->conf->ap_pin);
808 hapd->conf->ap_pin = NULL;
809 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
810 }
811
812
813 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
814 {
815 struct hostapd_data *hapd;
816 unsigned int pin;
817 char pin_txt[9];
818
819 if (wpa_s->ap_iface == NULL)
820 return NULL;
821 hapd = wpa_s->ap_iface->bss[0];
822 pin = wps_generate_pin();
823 os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
824 os_free(hapd->conf->ap_pin);
825 hapd->conf->ap_pin = os_strdup(pin_txt);
826 if (hapd->conf->ap_pin == NULL)
827 return NULL;
828 wpas_wps_ap_pin_enable(wpa_s, timeout);
829
830 return hapd->conf->ap_pin;
831 }
832
833
834 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
835 {
836 struct hostapd_data *hapd;
837 if (wpa_s->ap_iface == NULL)
838 return NULL;
839 hapd = wpa_s->ap_iface->bss[0];
840 return hapd->conf->ap_pin;
841 }
842
843
844 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
845 int timeout)
846 {
847 struct hostapd_data *hapd;
848 char pin_txt[9];
849 int ret;
850
851 if (wpa_s->ap_iface == NULL)
852 return -1;
853 hapd = wpa_s->ap_iface->bss[0];
854 ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
855 if (ret < 0 || ret >= (int) sizeof(pin_txt))
856 return -1;
857 os_free(hapd->conf->ap_pin);
858 hapd->conf->ap_pin = os_strdup(pin_txt);
859 if (hapd->conf->ap_pin == NULL)
860 return -1;
861 wpas_wps_ap_pin_enable(wpa_s, timeout);
862
863 return 0;
864 }
865
866
867 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
868 {
869 struct hostapd_data *hapd;
870
871 if (wpa_s->ap_iface == NULL)
872 return;
873 hapd = wpa_s->ap_iface->bss[0];
874
875 /*
876 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
877 * PIN if this happens multiple times to slow down brute force attacks.
878 */
879 hapd->ap_pin_failures++;
880 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
881 hapd->ap_pin_failures);
882 if (hapd->ap_pin_failures < 3)
883 return;
884
885 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
886 hapd->ap_pin_failures = 0;
887 os_free(hapd->conf->ap_pin);
888 hapd->conf->ap_pin = NULL;
889 }
890
891
892 #ifdef CONFIG_WPS_NFC
893
894 struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
895 int ndef)
896 {
897 struct hostapd_data *hapd;
898
899 if (wpa_s->ap_iface == NULL)
900 return NULL;
901 hapd = wpa_s->ap_iface->bss[0];
902 return hostapd_wps_nfc_config_token(hapd, ndef);
903 }
904
905
906 struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
907 int ndef)
908 {
909 struct hostapd_data *hapd;
910
911 if (wpa_s->ap_iface == NULL)
912 return NULL;
913 hapd = wpa_s->ap_iface->bss[0];
914 return hostapd_wps_nfc_hs_cr(hapd, ndef);
915 }
916
917 #endif /* CONFIG_WPS_NFC */
918
919 #endif /* CONFIG_WPS */
920
921
922 #ifdef CONFIG_CTRL_IFACE
923
924 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
925 char *buf, size_t buflen)
926 {
927 if (wpa_s->ap_iface == NULL)
928 return -1;
929 return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
930 buf, buflen);
931 }
932
933
934 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
935 char *buf, size_t buflen)
936 {
937 if (wpa_s->ap_iface == NULL)
938 return -1;
939 return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
940 buf, buflen);
941 }
942
943
944 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
945 char *buf, size_t buflen)
946 {
947 if (wpa_s->ap_iface == NULL)
948 return -1;
949 return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
950 buf, buflen);
951 }
952
953
954 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
955 const char *txtaddr)
956 {
957 if (wpa_s->ap_iface == NULL)
958 return -1;
959 return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
960 txtaddr);
961 }
962
963
964 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
965 const char *txtaddr)
966 {
967 if (wpa_s->ap_iface == NULL)
968 return -1;
969 return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
970 txtaddr);
971 }
972
973
974 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
975 size_t buflen, int verbose)
976 {
977 char *pos = buf, *end = buf + buflen;
978 int ret;
979 struct hostapd_bss_config *conf;
980
981 if (wpa_s->ap_iface == NULL)
982 return -1;
983
984 conf = wpa_s->ap_iface->bss[0]->conf;
985 if (conf->wpa == 0)
986 return 0;
987
988 ret = os_snprintf(pos, end - pos,
989 "pairwise_cipher=%s\n"
990 "group_cipher=%s\n"
991 "key_mgmt=%s\n",
992 wpa_cipher_txt(conf->rsn_pairwise),
993 wpa_cipher_txt(conf->wpa_group),
994 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
995 conf->wpa));
996 if (ret < 0 || ret >= end - pos)
997 return pos - buf;
998 pos += ret;
999 return pos - buf;
1000 }
1001
1002 #endif /* CONFIG_CTRL_IFACE */
1003
1004
1005 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
1006 {
1007 struct hostapd_iface *iface = wpa_s->ap_iface;
1008 struct wpa_ssid *ssid = wpa_s->current_ssid;
1009 struct hostapd_data *hapd;
1010
1011 if (ssid == NULL || wpa_s->ap_iface == NULL ||
1012 ssid->mode == WPAS_MODE_INFRA ||
1013 ssid->mode == WPAS_MODE_IBSS)
1014 return -1;
1015
1016 #ifdef CONFIG_P2P
1017 if (ssid->mode == WPAS_MODE_P2P_GO)
1018 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
1019 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1020 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
1021 P2P_GROUP_FORMATION;
1022 #endif /* CONFIG_P2P */
1023
1024 hapd = iface->bss[0];
1025 if (hapd->drv_priv == NULL)
1026 return -1;
1027 ieee802_11_set_beacons(iface);
1028 hostapd_set_ap_wps_ie(hapd);
1029
1030 return 0;
1031 }
1032
1033
1034 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
1035 int offset)
1036 {
1037 if (!wpa_s->ap_iface)
1038 return;
1039
1040 wpa_s->assoc_freq = freq;
1041 hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset);
1042 }
1043
1044
1045 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1046 const u8 *addr)
1047 {
1048 struct hostapd_data *hapd;
1049 struct hostapd_bss_config *conf;
1050
1051 if (!wpa_s->ap_iface)
1052 return -1;
1053
1054 if (addr)
1055 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1056 MAC2STR(addr));
1057 else
1058 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1059
1060 hapd = wpa_s->ap_iface->bss[0];
1061 conf = hapd->conf;
1062
1063 os_free(conf->accept_mac);
1064 conf->accept_mac = NULL;
1065 conf->num_accept_mac = 0;
1066 os_free(conf->deny_mac);
1067 conf->deny_mac = NULL;
1068 conf->num_deny_mac = 0;
1069
1070 if (addr == NULL) {
1071 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1072 return 0;
1073 }
1074
1075 conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1076 conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1077 if (conf->accept_mac == NULL)
1078 return -1;
1079 os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1080 conf->num_accept_mac = 1;
1081
1082 return 0;
1083 }