]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/ap.c
Ignore duplicated SSID element when parsing
[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 "crypto/dh_group5.h"
19 #include "ap/hostapd.h"
20 #include "ap/ap_config.h"
21 #include "ap/ap_drv_ops.h"
22 #ifdef NEED_AP_MLME
23 #include "ap/ieee802_11.h"
24 #endif /* NEED_AP_MLME */
25 #include "ap/beacon.h"
26 #include "ap/ieee802_1x.h"
27 #include "ap/wps_hostapd.h"
28 #include "ap/ctrl_iface_ap.h"
29 #include "ap/dfs.h"
30 #include "wps/wps.h"
31 #include "common/ieee802_11_defs.h"
32 #include "config_ssid.h"
33 #include "config.h"
34 #include "wpa_supplicant_i.h"
35 #include "driver_i.h"
36 #include "p2p_supplicant.h"
37 #include "ap.h"
38 #include "ap/sta_info.h"
39 #include "notify.h"
40
41
42 #ifdef CONFIG_WPS
43 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
44 #endif /* CONFIG_WPS */
45
46
47 static void wpas_conf_ap_vht(struct wpa_supplicant *wpa_s,
48 struct wpa_ssid *ssid,
49 struct hostapd_config *conf,
50 struct hostapd_hw_modes *mode)
51 {
52 #ifdef CONFIG_P2P
53 u8 center_chan = 0;
54 u8 channel = conf->channel;
55 #endif /* CONFIG_P2P */
56
57 if (!conf->secondary_channel)
58 goto no_vht;
59
60 /* Use the maximum oper channel width if it's given. */
61 if (ssid->max_oper_chwidth)
62 conf->vht_oper_chwidth = ssid->max_oper_chwidth;
63
64 ieee80211_freq_to_chan(ssid->vht_center_freq2,
65 &conf->vht_oper_centr_freq_seg1_idx);
66
67 if (!ssid->p2p_group) {
68 if (!ssid->vht_center_freq1 ||
69 conf->vht_oper_chwidth == CHANWIDTH_USE_HT)
70 goto no_vht;
71 ieee80211_freq_to_chan(ssid->vht_center_freq1,
72 &conf->vht_oper_centr_freq_seg0_idx);
73 wpa_printf(MSG_DEBUG, "VHT seg0 index %d for AP",
74 conf->vht_oper_centr_freq_seg0_idx);
75 return;
76 }
77
78 #ifdef CONFIG_P2P
79 switch (conf->vht_oper_chwidth) {
80 case CHANWIDTH_80MHZ:
81 case CHANWIDTH_80P80MHZ:
82 center_chan = wpas_p2p_get_vht80_center(wpa_s, mode, channel);
83 wpa_printf(MSG_DEBUG,
84 "VHT center channel %u for 80 or 80+80 MHz bandwidth",
85 center_chan);
86 break;
87 case CHANWIDTH_160MHZ:
88 center_chan = wpas_p2p_get_vht160_center(wpa_s, mode, channel);
89 wpa_printf(MSG_DEBUG,
90 "VHT center channel %u for 160 MHz bandwidth",
91 center_chan);
92 break;
93 default:
94 /*
95 * conf->vht_oper_chwidth might not be set for non-P2P GO cases,
96 * try oper_cwidth 160 MHz first then VHT 80 MHz, if 160 MHz is
97 * not supported.
98 */
99 conf->vht_oper_chwidth = CHANWIDTH_160MHZ;
100 center_chan = wpas_p2p_get_vht160_center(wpa_s, mode, channel);
101 if (center_chan) {
102 wpa_printf(MSG_DEBUG,
103 "VHT center channel %u for auto-selected 160 MHz bandwidth",
104 center_chan);
105 } else {
106 conf->vht_oper_chwidth = CHANWIDTH_80MHZ;
107 center_chan = wpas_p2p_get_vht80_center(wpa_s, mode,
108 channel);
109 wpa_printf(MSG_DEBUG,
110 "VHT center channel %u for auto-selected 80 MHz bandwidth",
111 center_chan);
112 }
113 break;
114 }
115 if (!center_chan)
116 goto no_vht;
117
118 conf->vht_oper_centr_freq_seg0_idx = center_chan;
119 wpa_printf(MSG_DEBUG, "VHT seg0 index %d for P2P GO",
120 conf->vht_oper_centr_freq_seg0_idx);
121 return;
122 #endif /* CONFIG_P2P */
123
124 no_vht:
125 wpa_printf(MSG_DEBUG,
126 "No VHT higher bandwidth support for the selected channel %d",
127 conf->channel);
128 conf->vht_oper_centr_freq_seg0_idx =
129 conf->channel + conf->secondary_channel * 2;
130 conf->vht_oper_chwidth = CHANWIDTH_USE_HT;
131 }
132
133
134 int wpa_supplicant_conf_ap_ht(struct wpa_supplicant *wpa_s,
135 struct wpa_ssid *ssid,
136 struct hostapd_config *conf)
137 {
138 conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
139 &conf->channel);
140
141 if (conf->hw_mode == NUM_HOSTAPD_MODES) {
142 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
143 ssid->frequency);
144 return -1;
145 }
146
147 /* TODO: enable HT40 if driver supports it;
148 * drop to 11b if driver does not support 11g */
149
150 /*
151 * Enable HT20 if the driver supports it, by setting conf->ieee80211n
152 * and a mask of allowed capabilities within conf->ht_capab.
153 * Using default config settings for: conf->ht_op_mode_fixed,
154 * conf->secondary_channel, conf->require_ht
155 */
156 if (wpa_s->hw.modes) {
157 struct hostapd_hw_modes *mode = NULL;
158 int i, no_ht = 0;
159
160 wpa_printf(MSG_DEBUG,
161 "Determining HT/VHT options based on driver capabilities (freq=%u chan=%u)",
162 ssid->frequency, conf->channel);
163
164 for (i = 0; i < wpa_s->hw.num_modes; i++) {
165 if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
166 mode = &wpa_s->hw.modes[i];
167 break;
168 }
169 }
170
171 #ifdef CONFIG_HT_OVERRIDES
172 if (ssid->disable_ht)
173 ssid->ht = 0;
174 #endif /* CONFIG_HT_OVERRIDES */
175
176 if (!ssid->ht) {
177 wpa_printf(MSG_DEBUG,
178 "HT not enabled in network profile");
179 conf->ieee80211n = 0;
180 conf->ht_capab = 0;
181 no_ht = 1;
182 }
183
184 if (!no_ht && mode && mode->ht_capab) {
185 wpa_printf(MSG_DEBUG,
186 "Enable HT support (p2p_group=%d 11a=%d ht40_hw_capab=%d ssid->ht40=%d)",
187 ssid->p2p_group,
188 conf->hw_mode == HOSTAPD_MODE_IEEE80211A,
189 !!(mode->ht_capab &
190 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET),
191 ssid->ht40);
192 conf->ieee80211n = 1;
193 #ifdef CONFIG_P2P
194 if (ssid->p2p_group &&
195 conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
196 (mode->ht_capab &
197 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
198 ssid->ht40) {
199 conf->secondary_channel =
200 wpas_p2p_get_ht40_mode(wpa_s, mode,
201 conf->channel);
202 wpa_printf(MSG_DEBUG,
203 "HT secondary channel offset %d for P2P group",
204 conf->secondary_channel);
205 }
206 #endif /* CONFIG_P2P */
207
208 if (!ssid->p2p_group &&
209 (mode->ht_capab &
210 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
211 conf->secondary_channel = ssid->ht40;
212 wpa_printf(MSG_DEBUG,
213 "HT secondary channel offset %d for AP",
214 conf->secondary_channel);
215 }
216
217 if (conf->secondary_channel)
218 conf->ht_capab |=
219 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
220
221 /*
222 * white-list capabilities that won't cause issues
223 * to connecting stations, while leaving the current
224 * capabilities intact (currently disabled SMPS).
225 */
226 conf->ht_capab |= mode->ht_capab &
227 (HT_CAP_INFO_GREEN_FIELD |
228 HT_CAP_INFO_SHORT_GI20MHZ |
229 HT_CAP_INFO_SHORT_GI40MHZ |
230 HT_CAP_INFO_RX_STBC_MASK |
231 HT_CAP_INFO_TX_STBC |
232 HT_CAP_INFO_MAX_AMSDU_SIZE);
233
234 if (mode->vht_capab && ssid->vht) {
235 conf->ieee80211ac = 1;
236 conf->vht_capab |= mode->vht_capab;
237 wpas_conf_ap_vht(wpa_s, ssid, conf, mode);
238 }
239
240 if (mode->he_capab[wpas_mode_to_ieee80211_mode(
241 ssid->mode)].he_supported &&
242 ssid->he)
243 conf->ieee80211ax = 1;
244 }
245 }
246
247 if (conf->secondary_channel) {
248 struct wpa_supplicant *iface;
249
250 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
251 {
252 if (iface == wpa_s ||
253 iface->wpa_state < WPA_AUTHENTICATING ||
254 (int) iface->assoc_freq != ssid->frequency)
255 continue;
256
257 /*
258 * Do not allow 40 MHz co-ex PRI/SEC switch to force us
259 * to change our PRI channel since we have an existing,
260 * concurrent connection on that channel and doing
261 * multi-channel concurrency is likely to cause more
262 * harm than using different PRI/SEC selection in
263 * environment with multiple BSSes on these two channels
264 * with mixed 20 MHz or PRI channel selection.
265 */
266 conf->no_pri_sec_switch = 1;
267 }
268 }
269
270 return 0;
271 }
272
273
274 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
275 struct wpa_ssid *ssid,
276 struct hostapd_config *conf)
277 {
278 struct hostapd_bss_config *bss = conf->bss[0];
279
280 conf->driver = wpa_s->driver;
281
282 os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
283
284 if (wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf))
285 return -1;
286
287 if (ssid->pbss > 1) {
288 wpa_printf(MSG_ERROR, "Invalid pbss value(%d) for AP mode",
289 ssid->pbss);
290 return -1;
291 }
292 bss->pbss = ssid->pbss;
293
294 #ifdef CONFIG_ACS
295 if (ssid->acs) {
296 /* Setting channel to 0 in order to enable ACS */
297 conf->channel = 0;
298 wpa_printf(MSG_DEBUG, "Use automatic channel selection");
299 }
300 #endif /* CONFIG_ACS */
301
302 if (ieee80211_is_dfs(ssid->frequency, wpa_s->hw.modes,
303 wpa_s->hw.num_modes) && wpa_s->conf->country[0]) {
304 conf->ieee80211h = 1;
305 conf->ieee80211d = 1;
306 conf->country[0] = wpa_s->conf->country[0];
307 conf->country[1] = wpa_s->conf->country[1];
308 conf->country[2] = ' ';
309 }
310
311 #ifdef CONFIG_P2P
312 if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
313 (ssid->mode == WPAS_MODE_P2P_GO ||
314 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
315 /* Remove 802.11b rates from supported and basic rate sets */
316 int *list = os_malloc(4 * sizeof(int));
317 if (list) {
318 list[0] = 60;
319 list[1] = 120;
320 list[2] = 240;
321 list[3] = -1;
322 }
323 conf->basic_rates = list;
324
325 list = os_malloc(9 * sizeof(int));
326 if (list) {
327 list[0] = 60;
328 list[1] = 90;
329 list[2] = 120;
330 list[3] = 180;
331 list[4] = 240;
332 list[5] = 360;
333 list[6] = 480;
334 list[7] = 540;
335 list[8] = -1;
336 }
337 conf->supported_rates = list;
338 }
339
340 #ifdef CONFIG_IEEE80211AX
341 if (ssid->mode == WPAS_MODE_P2P_GO ||
342 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
343 conf->ieee80211ax = ssid->he;
344 #endif /* CONFIG_IEEE80211AX */
345
346 bss->isolate = !wpa_s->conf->p2p_intra_bss;
347 bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
348 bss->wpa_deny_ptk0_rekey = ssid->wpa_deny_ptk0_rekey;
349
350 if (ssid->p2p_group) {
351 os_memcpy(bss->ip_addr_go, wpa_s->p2pdev->conf->ip_addr_go, 4);
352 os_memcpy(bss->ip_addr_mask, wpa_s->p2pdev->conf->ip_addr_mask,
353 4);
354 os_memcpy(bss->ip_addr_start,
355 wpa_s->p2pdev->conf->ip_addr_start, 4);
356 os_memcpy(bss->ip_addr_end, wpa_s->p2pdev->conf->ip_addr_end,
357 4);
358 }
359 #endif /* CONFIG_P2P */
360
361 if (ssid->ssid_len == 0) {
362 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
363 return -1;
364 }
365 os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
366 bss->ssid.ssid_len = ssid->ssid_len;
367 bss->ssid.ssid_set = 1;
368
369 bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
370
371 if (ssid->auth_alg)
372 bss->auth_algs = ssid->auth_alg;
373
374 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
375 bss->wpa = ssid->proto;
376 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
377 bss->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
378 else
379 bss->wpa_key_mgmt = ssid->key_mgmt;
380 bss->wpa_pairwise = ssid->pairwise_cipher;
381 if (wpa_key_mgmt_sae(bss->wpa_key_mgmt) && ssid->passphrase) {
382 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
383 } else if (ssid->psk_set) {
384 bin_clear_free(bss->ssid.wpa_psk, sizeof(*bss->ssid.wpa_psk));
385 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
386 if (bss->ssid.wpa_psk == NULL)
387 return -1;
388 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
389 bss->ssid.wpa_psk->group = 1;
390 bss->ssid.wpa_psk_set = 1;
391 } else if (ssid->passphrase) {
392 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
393 #ifdef CONFIG_WEP
394 } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
395 ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
396 struct hostapd_wep_keys *wep = &bss->ssid.wep;
397 int i;
398 for (i = 0; i < NUM_WEP_KEYS; i++) {
399 if (ssid->wep_key_len[i] == 0)
400 continue;
401 wep->key[i] = os_memdup(ssid->wep_key[i],
402 ssid->wep_key_len[i]);
403 if (wep->key[i] == NULL)
404 return -1;
405 wep->len[i] = ssid->wep_key_len[i];
406 }
407 wep->idx = ssid->wep_tx_keyidx;
408 wep->keys_set = 1;
409 #endif /* CONFIG_WEP */
410 }
411 #ifdef CONFIG_SAE
412 if (ssid->sae_password) {
413 struct sae_password_entry *pw;
414
415 pw = os_zalloc(sizeof(*pw));
416 if (!pw)
417 return -1;
418 os_memset(pw->peer_addr, 0xff, ETH_ALEN);
419 pw->password = os_strdup(ssid->sae_password);
420 if (!pw->password) {
421 os_free(pw);
422 return -1;
423 }
424 if (ssid->sae_password_id) {
425 pw->identifier = os_strdup(ssid->sae_password_id);
426 if (!pw->identifier) {
427 str_clear_free(pw->password);
428 os_free(pw);
429 return -1;
430 }
431 }
432
433 pw->next = bss->sae_passwords;
434 bss->sae_passwords = pw;
435 }
436
437 bss->sae_pwe = wpa_s->conf->sae_pwe;
438 #endif /* CONFIG_SAE */
439
440 if (wpa_s->conf->go_interworking) {
441 wpa_printf(MSG_DEBUG,
442 "P2P: Enable Interworking with access_network_type: %d",
443 wpa_s->conf->go_access_network_type);
444 bss->interworking = wpa_s->conf->go_interworking;
445 bss->access_network_type = wpa_s->conf->go_access_network_type;
446 bss->internet = wpa_s->conf->go_internet;
447 if (wpa_s->conf->go_venue_group) {
448 wpa_printf(MSG_DEBUG,
449 "P2P: Venue group: %d Venue type: %d",
450 wpa_s->conf->go_venue_group,
451 wpa_s->conf->go_venue_type);
452 bss->venue_group = wpa_s->conf->go_venue_group;
453 bss->venue_type = wpa_s->conf->go_venue_type;
454 bss->venue_info_set = 1;
455 }
456 }
457
458 if (ssid->ap_max_inactivity)
459 bss->ap_max_inactivity = ssid->ap_max_inactivity;
460
461 if (ssid->dtim_period)
462 bss->dtim_period = ssid->dtim_period;
463 else if (wpa_s->conf->dtim_period)
464 bss->dtim_period = wpa_s->conf->dtim_period;
465
466 if (ssid->beacon_int)
467 conf->beacon_int = ssid->beacon_int;
468 else if (wpa_s->conf->beacon_int)
469 conf->beacon_int = wpa_s->conf->beacon_int;
470
471 #ifdef CONFIG_P2P
472 if (ssid->mode == WPAS_MODE_P2P_GO ||
473 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
474 if (wpa_s->conf->p2p_go_ctwindow > conf->beacon_int) {
475 wpa_printf(MSG_INFO,
476 "CTWindow (%d) is bigger than beacon interval (%d) - avoid configuring it",
477 wpa_s->conf->p2p_go_ctwindow,
478 conf->beacon_int);
479 conf->p2p_go_ctwindow = 0;
480 } else {
481 conf->p2p_go_ctwindow = wpa_s->conf->p2p_go_ctwindow;
482 }
483 }
484 #endif /* CONFIG_P2P */
485
486 if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
487 bss->rsn_pairwise = bss->wpa_pairwise;
488 bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
489 bss->rsn_pairwise);
490
491 if (bss->wpa && bss->ieee802_1x) {
492 bss->ssid.security_policy = SECURITY_WPA;
493 } else if (bss->wpa) {
494 bss->ssid.security_policy = SECURITY_WPA_PSK;
495 #ifdef CONFIG_WEP
496 } else if (bss->ieee802_1x) {
497 int cipher = WPA_CIPHER_NONE;
498 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
499 bss->ssid.wep.default_len = bss->default_wep_key_len;
500 if (bss->default_wep_key_len)
501 cipher = bss->default_wep_key_len >= 13 ?
502 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
503 bss->wpa_group = cipher;
504 bss->wpa_pairwise = cipher;
505 bss->rsn_pairwise = cipher;
506 } else if (bss->ssid.wep.keys_set) {
507 int cipher = WPA_CIPHER_WEP40;
508 if (bss->ssid.wep.len[0] >= 13)
509 cipher = WPA_CIPHER_WEP104;
510 bss->ssid.security_policy = SECURITY_STATIC_WEP;
511 bss->wpa_group = cipher;
512 bss->wpa_pairwise = cipher;
513 bss->rsn_pairwise = cipher;
514 #endif /* CONFIG_WEP */
515 } else {
516 bss->ssid.security_policy = SECURITY_PLAINTEXT;
517 bss->wpa_group = WPA_CIPHER_NONE;
518 bss->wpa_pairwise = WPA_CIPHER_NONE;
519 bss->rsn_pairwise = WPA_CIPHER_NONE;
520 }
521
522 if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
523 (bss->wpa_group == WPA_CIPHER_CCMP ||
524 bss->wpa_group == WPA_CIPHER_GCMP ||
525 bss->wpa_group == WPA_CIPHER_CCMP_256 ||
526 bss->wpa_group == WPA_CIPHER_GCMP_256)) {
527 /*
528 * Strong ciphers do not need frequent rekeying, so increase
529 * the default GTK rekeying period to 24 hours.
530 */
531 bss->wpa_group_rekey = 86400;
532 }
533
534 if (ssid->ieee80211w != MGMT_FRAME_PROTECTION_DEFAULT)
535 bss->ieee80211w = ssid->ieee80211w;
536
537 #ifdef CONFIG_OCV
538 bss->ocv = ssid->ocv;
539 #endif /* CONFIG_OCV */
540
541 #ifdef CONFIG_WPS
542 /*
543 * Enable WPS by default for open and WPA/WPA2-Personal network, but
544 * require user interaction to actually use it. Only the internal
545 * Registrar is supported.
546 */
547 if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
548 bss->ssid.security_policy != SECURITY_PLAINTEXT)
549 goto no_wps;
550 if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
551 (!(bss->rsn_pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) ||
552 !(bss->wpa & 2)))
553 goto no_wps; /* WPS2 does not allow WPA/TKIP-only
554 * configuration */
555 if (ssid->wps_disabled)
556 goto no_wps;
557 bss->eap_server = 1;
558
559 if (!ssid->ignore_broadcast_ssid)
560 bss->wps_state = 2;
561
562 bss->ap_setup_locked = 2;
563 if (wpa_s->conf->config_methods)
564 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
565 os_memcpy(bss->device_type, wpa_s->conf->device_type,
566 WPS_DEV_TYPE_LEN);
567 if (wpa_s->conf->device_name) {
568 bss->device_name = os_strdup(wpa_s->conf->device_name);
569 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
570 }
571 if (wpa_s->conf->manufacturer)
572 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
573 if (wpa_s->conf->model_name)
574 bss->model_name = os_strdup(wpa_s->conf->model_name);
575 if (wpa_s->conf->model_number)
576 bss->model_number = os_strdup(wpa_s->conf->model_number);
577 if (wpa_s->conf->serial_number)
578 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
579 if (is_nil_uuid(wpa_s->conf->uuid))
580 os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
581 else
582 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
583 os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
584 bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
585 if (ssid->eap.fragment_size != DEFAULT_FRAGMENT_SIZE)
586 bss->fragment_size = ssid->eap.fragment_size;
587 no_wps:
588 #endif /* CONFIG_WPS */
589
590 if (wpa_s->max_stations &&
591 wpa_s->max_stations < wpa_s->conf->max_num_sta)
592 bss->max_num_sta = wpa_s->max_stations;
593 else
594 bss->max_num_sta = wpa_s->conf->max_num_sta;
595
596 if (!bss->isolate)
597 bss->isolate = wpa_s->conf->ap_isolate;
598
599 bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
600
601 if (wpa_s->conf->ap_vendor_elements) {
602 bss->vendor_elements =
603 wpabuf_dup(wpa_s->conf->ap_vendor_elements);
604 }
605
606 bss->ftm_responder = wpa_s->conf->ftm_responder;
607 bss->ftm_initiator = wpa_s->conf->ftm_initiator;
608
609 return 0;
610 }
611
612
613 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
614 {
615 #ifdef CONFIG_P2P
616 struct wpa_supplicant *wpa_s = ctx;
617 const struct ieee80211_mgmt *mgmt;
618
619 mgmt = (const struct ieee80211_mgmt *) buf;
620 if (len < IEEE80211_HDRLEN + 1)
621 return;
622 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
623 return;
624 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
625 mgmt->u.action.category,
626 buf + IEEE80211_HDRLEN + 1,
627 len - IEEE80211_HDRLEN - 1, freq);
628 #endif /* CONFIG_P2P */
629 }
630
631
632 static void ap_wps_event_cb(void *ctx, enum wps_event event,
633 union wps_event_data *data)
634 {
635 #ifdef CONFIG_P2P
636 struct wpa_supplicant *wpa_s = ctx;
637
638 if (event == WPS_EV_FAIL) {
639 struct wps_event_fail *fail = &data->fail;
640
641 if (wpa_s->p2pdev && wpa_s->p2pdev != wpa_s &&
642 wpa_s == wpa_s->global->p2p_group_formation) {
643 /*
644 * src/ap/wps_hostapd.c has already sent this on the
645 * main interface, so only send on the parent interface
646 * here if needed.
647 */
648 wpa_msg(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_FAIL
649 "msg=%d config_error=%d",
650 fail->msg, fail->config_error);
651 }
652 wpas_p2p_wps_failed(wpa_s, fail);
653 }
654 #endif /* CONFIG_P2P */
655 }
656
657
658 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
659 int authorized, const u8 *p2p_dev_addr)
660 {
661 wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
662 }
663
664
665 #ifdef CONFIG_P2P
666 static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
667 const u8 *psk, size_t psk_len)
668 {
669
670 struct wpa_supplicant *wpa_s = ctx;
671 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
672 return;
673 wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
674 }
675 #endif /* CONFIG_P2P */
676
677
678 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
679 {
680 #ifdef CONFIG_P2P
681 struct wpa_supplicant *wpa_s = ctx;
682 const struct ieee80211_mgmt *mgmt;
683
684 mgmt = (const struct ieee80211_mgmt *) buf;
685 if (len < IEEE80211_HDRLEN + 1)
686 return -1;
687 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
688 mgmt->u.action.category,
689 buf + IEEE80211_HDRLEN + 1,
690 len - IEEE80211_HDRLEN - 1, freq);
691 #endif /* CONFIG_P2P */
692 return 0;
693 }
694
695
696 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
697 const u8 *bssid, const u8 *ie, size_t ie_len,
698 int ssi_signal)
699 {
700 struct wpa_supplicant *wpa_s = ctx;
701 unsigned int freq = 0;
702
703 if (wpa_s->ap_iface)
704 freq = wpa_s->ap_iface->freq;
705
706 return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
707 freq, ssi_signal);
708 }
709
710
711 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
712 const u8 *uuid_e)
713 {
714 struct wpa_supplicant *wpa_s = ctx;
715 wpas_p2p_wps_success(wpa_s, mac_addr, 1);
716 }
717
718
719 static void wpas_ap_configured_cb(void *ctx)
720 {
721 struct wpa_supplicant *wpa_s = ctx;
722
723 wpa_printf(MSG_DEBUG, "AP interface setup completed - state %s",
724 hostapd_state_text(wpa_s->ap_iface->state));
725 if (wpa_s->ap_iface->state == HAPD_IFACE_DISABLED) {
726 wpa_supplicant_ap_deinit(wpa_s);
727 return;
728 }
729
730 #ifdef CONFIG_ACS
731 if (wpa_s->current_ssid && wpa_s->current_ssid->acs) {
732 wpa_s->assoc_freq = wpa_s->ap_iface->freq;
733 wpa_s->current_ssid->frequency = wpa_s->ap_iface->freq;
734 }
735 #endif /* CONFIG_ACS */
736
737 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
738
739 if (wpa_s->ap_configured_cb)
740 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
741 wpa_s->ap_configured_cb_data);
742 }
743
744
745 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
746 struct wpa_ssid *ssid)
747 {
748 struct wpa_driver_associate_params params;
749 struct hostapd_iface *hapd_iface;
750 struct hostapd_config *conf;
751 size_t i;
752
753 if (ssid->ssid == NULL || ssid->ssid_len == 0) {
754 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
755 return -1;
756 }
757
758 wpa_supplicant_ap_deinit(wpa_s);
759
760 wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
761 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
762
763 os_memset(&params, 0, sizeof(params));
764 params.ssid = ssid->ssid;
765 params.ssid_len = ssid->ssid_len;
766 switch (ssid->mode) {
767 case WPAS_MODE_AP:
768 case WPAS_MODE_P2P_GO:
769 case WPAS_MODE_P2P_GROUP_FORMATION:
770 params.mode = IEEE80211_MODE_AP;
771 break;
772 default:
773 return -1;
774 }
775 if (ssid->frequency == 0)
776 ssid->frequency = 2462; /* default channel 11 */
777 params.freq.freq = ssid->frequency;
778
779 if ((ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO) &&
780 ssid->enable_edmg) {
781 u8 primary_channel;
782
783 if (ieee80211_freq_to_chan(ssid->frequency, &primary_channel) ==
784 NUM_HOSTAPD_MODES) {
785 wpa_printf(MSG_WARNING,
786 "EDMG: Failed to get the primary channel");
787 return -1;
788 }
789
790 hostapd_encode_edmg_chan(ssid->enable_edmg, ssid->edmg_channel,
791 primary_channel, &params.freq.edmg);
792 }
793
794 params.wpa_proto = ssid->proto;
795 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
796 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
797 else
798 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
799 params.key_mgmt_suite = wpa_s->key_mgmt;
800
801 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
802 1);
803 if (wpa_s->pairwise_cipher < 0) {
804 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
805 "cipher.");
806 return -1;
807 }
808 params.pairwise_suite = wpa_s->pairwise_cipher;
809 params.group_suite = params.pairwise_suite;
810
811 #ifdef CONFIG_P2P
812 if (ssid->mode == WPAS_MODE_P2P_GO ||
813 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
814 params.p2p = 1;
815 #endif /* CONFIG_P2P */
816
817 if (wpa_s->p2pdev->set_ap_uapsd)
818 params.uapsd = wpa_s->p2pdev->ap_uapsd;
819 else if (params.p2p && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_UAPSD))
820 params.uapsd = 1; /* mandatory for P2P GO */
821 else
822 params.uapsd = -1;
823
824 if (ieee80211_is_dfs(params.freq.freq, wpa_s->hw.modes,
825 wpa_s->hw.num_modes))
826 params.freq.freq = 0; /* set channel after CAC */
827
828 if (params.p2p)
829 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_GO);
830 else
831 wpa_drv_get_ext_capa(wpa_s, WPA_IF_AP_BSS);
832
833 if (wpa_drv_associate(wpa_s, &params) < 0) {
834 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
835 return -1;
836 }
837
838 wpa_s->ap_iface = hapd_iface = hostapd_alloc_iface();
839 if (hapd_iface == NULL)
840 return -1;
841 hapd_iface->owner = wpa_s;
842 hapd_iface->drv_flags = wpa_s->drv_flags;
843 hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
844 hapd_iface->extended_capa = wpa_s->extended_capa;
845 hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
846 hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
847
848 wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
849 if (conf == NULL) {
850 wpa_supplicant_ap_deinit(wpa_s);
851 return -1;
852 }
853
854 os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
855 wpa_s->conf->wmm_ac_params,
856 sizeof(wpa_s->conf->wmm_ac_params));
857
858 if (params.uapsd > 0) {
859 conf->bss[0]->wmm_enabled = 1;
860 conf->bss[0]->wmm_uapsd = 1;
861 }
862
863 if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
864 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
865 wpa_supplicant_ap_deinit(wpa_s);
866 return -1;
867 }
868
869 #ifdef CONFIG_P2P
870 if (ssid->mode == WPAS_MODE_P2P_GO)
871 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
872 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
873 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
874 P2P_GROUP_FORMATION;
875 #endif /* CONFIG_P2P */
876
877 hapd_iface->num_bss = conf->num_bss;
878 hapd_iface->bss = os_calloc(conf->num_bss,
879 sizeof(struct hostapd_data *));
880 if (hapd_iface->bss == NULL) {
881 wpa_supplicant_ap_deinit(wpa_s);
882 return -1;
883 }
884
885 for (i = 0; i < conf->num_bss; i++) {
886 hapd_iface->bss[i] =
887 hostapd_alloc_bss_data(hapd_iface, conf,
888 conf->bss[i]);
889 if (hapd_iface->bss[i] == NULL) {
890 wpa_supplicant_ap_deinit(wpa_s);
891 return -1;
892 }
893
894 hapd_iface->bss[i]->msg_ctx = wpa_s;
895 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->p2pdev;
896 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
897 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
898 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
899 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
900 hostapd_register_probereq_cb(hapd_iface->bss[i],
901 ap_probe_req_rx, wpa_s);
902 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
903 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
904 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
905 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
906 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
907 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
908 #ifdef CONFIG_P2P
909 hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
910 hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
911 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
912 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
913 ssid);
914 #endif /* CONFIG_P2P */
915 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
916 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
917 #ifdef CONFIG_TESTING_OPTIONS
918 hapd_iface->bss[i]->ext_eapol_frame_io =
919 wpa_s->ext_eapol_frame_io;
920 #endif /* CONFIG_TESTING_OPTIONS */
921 }
922
923 os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
924 hapd_iface->bss[0]->driver = wpa_s->driver;
925 hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
926
927 wpa_s->current_ssid = ssid;
928 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
929 os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
930 wpa_s->assoc_freq = ssid->frequency;
931 wpa_s->ap_iface->conf->enable_edmg = ssid->enable_edmg;
932 wpa_s->ap_iface->conf->edmg_channel = ssid->edmg_channel;
933
934 #if defined(CONFIG_P2P) && defined(CONFIG_ACS)
935 if (wpa_s->p2p_go_do_acs) {
936 wpa_s->ap_iface->conf->channel = 0;
937 wpa_s->ap_iface->conf->hw_mode = wpa_s->p2p_go_acs_band;
938 ssid->acs = 1;
939 }
940 #endif /* CONFIG_P2P && CONFIG_ACS */
941
942 if (hostapd_setup_interface(wpa_s->ap_iface)) {
943 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
944 wpa_supplicant_ap_deinit(wpa_s);
945 return -1;
946 }
947
948 return 0;
949 }
950
951
952 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
953 {
954 #ifdef CONFIG_WPS
955 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
956 #endif /* CONFIG_WPS */
957
958 if (wpa_s->ap_iface == NULL)
959 return;
960
961 wpa_s->current_ssid = NULL;
962 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
963 wpa_s->assoc_freq = 0;
964 wpas_p2p_ap_deinit(wpa_s);
965 wpa_s->ap_iface->driver_ap_teardown =
966 !!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT);
967
968 hostapd_interface_deinit(wpa_s->ap_iface);
969 hostapd_interface_free(wpa_s->ap_iface);
970 wpa_s->ap_iface = NULL;
971 wpa_drv_deinit_ap(wpa_s);
972 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
973 " reason=%d locally_generated=1",
974 MAC2STR(wpa_s->own_addr), WLAN_REASON_DEAUTH_LEAVING);
975 }
976
977
978 void ap_tx_status(void *ctx, const u8 *addr,
979 const u8 *buf, size_t len, int ack)
980 {
981 #ifdef NEED_AP_MLME
982 struct wpa_supplicant *wpa_s = ctx;
983 hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
984 #endif /* NEED_AP_MLME */
985 }
986
987
988 void ap_eapol_tx_status(void *ctx, const u8 *dst,
989 const u8 *data, size_t len, int ack)
990 {
991 #ifdef NEED_AP_MLME
992 struct wpa_supplicant *wpa_s = ctx;
993 if (!wpa_s->ap_iface)
994 return;
995 hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
996 #endif /* NEED_AP_MLME */
997 }
998
999
1000 void ap_client_poll_ok(void *ctx, const u8 *addr)
1001 {
1002 #ifdef NEED_AP_MLME
1003 struct wpa_supplicant *wpa_s = ctx;
1004 if (wpa_s->ap_iface)
1005 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
1006 #endif /* NEED_AP_MLME */
1007 }
1008
1009
1010 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
1011 {
1012 #ifdef NEED_AP_MLME
1013 struct wpa_supplicant *wpa_s = ctx;
1014 ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
1015 #endif /* NEED_AP_MLME */
1016 }
1017
1018
1019 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
1020 {
1021 #ifdef NEED_AP_MLME
1022 struct wpa_supplicant *wpa_s = ctx;
1023 struct hostapd_frame_info fi;
1024 os_memset(&fi, 0, sizeof(fi));
1025 fi.datarate = rx_mgmt->datarate;
1026 fi.ssi_signal = rx_mgmt->ssi_signal;
1027 ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
1028 rx_mgmt->frame_len, &fi);
1029 #endif /* NEED_AP_MLME */
1030 }
1031
1032
1033 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
1034 {
1035 #ifdef NEED_AP_MLME
1036 struct wpa_supplicant *wpa_s = ctx;
1037 ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
1038 #endif /* NEED_AP_MLME */
1039 }
1040
1041
1042 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
1043 const u8 *src_addr, const u8 *buf, size_t len)
1044 {
1045 ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
1046 }
1047
1048
1049 #ifdef CONFIG_WPS
1050
1051 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
1052 const u8 *p2p_dev_addr)
1053 {
1054 if (!wpa_s->ap_iface)
1055 return -1;
1056 return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
1057 p2p_dev_addr);
1058 }
1059
1060
1061 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
1062 {
1063 struct wps_registrar *reg;
1064 int reg_sel = 0, wps_sta = 0;
1065
1066 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
1067 return -1;
1068
1069 reg = wpa_s->ap_iface->bss[0]->wps->registrar;
1070 reg_sel = wps_registrar_wps_cancel(reg);
1071 wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
1072 ap_sta_wps_cancel, NULL);
1073
1074 if (!reg_sel && !wps_sta) {
1075 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
1076 "time");
1077 return -1;
1078 }
1079
1080 /*
1081 * There are 2 cases to return wps cancel as success:
1082 * 1. When wps cancel was initiated but no connection has been
1083 * established with client yet.
1084 * 2. Client is in the middle of exchanging WPS messages.
1085 */
1086
1087 return 0;
1088 }
1089
1090
1091 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
1092 const char *pin, char *buf, size_t buflen,
1093 int timeout)
1094 {
1095 int ret, ret_len = 0;
1096
1097 if (!wpa_s->ap_iface)
1098 return -1;
1099
1100 if (pin == NULL) {
1101 unsigned int rpin;
1102
1103 if (wps_generate_pin(&rpin) < 0)
1104 return -1;
1105 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
1106 if (os_snprintf_error(buflen, ret_len))
1107 return -1;
1108 pin = buf;
1109 } else if (buf) {
1110 ret_len = os_snprintf(buf, buflen, "%s", pin);
1111 if (os_snprintf_error(buflen, ret_len))
1112 return -1;
1113 }
1114
1115 ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
1116 timeout);
1117 if (ret)
1118 return -1;
1119 return ret_len;
1120 }
1121
1122
1123 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
1124 {
1125 struct wpa_supplicant *wpa_s = eloop_data;
1126 wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
1127 wpas_wps_ap_pin_disable(wpa_s);
1128 }
1129
1130
1131 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
1132 {
1133 struct hostapd_data *hapd;
1134
1135 if (wpa_s->ap_iface == NULL)
1136 return;
1137 hapd = wpa_s->ap_iface->bss[0];
1138 wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
1139 hapd->ap_pin_failures = 0;
1140 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
1141 if (timeout > 0)
1142 eloop_register_timeout(timeout, 0,
1143 wpas_wps_ap_pin_timeout, wpa_s, NULL);
1144 }
1145
1146
1147 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
1148 {
1149 struct hostapd_data *hapd;
1150
1151 if (wpa_s->ap_iface == NULL)
1152 return;
1153 wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
1154 hapd = wpa_s->ap_iface->bss[0];
1155 os_free(hapd->conf->ap_pin);
1156 hapd->conf->ap_pin = NULL;
1157 eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
1158 }
1159
1160
1161 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
1162 {
1163 struct hostapd_data *hapd;
1164 unsigned int pin;
1165 char pin_txt[9];
1166
1167 if (wpa_s->ap_iface == NULL)
1168 return NULL;
1169 hapd = wpa_s->ap_iface->bss[0];
1170 if (wps_generate_pin(&pin) < 0)
1171 return NULL;
1172 os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
1173 os_free(hapd->conf->ap_pin);
1174 hapd->conf->ap_pin = os_strdup(pin_txt);
1175 if (hapd->conf->ap_pin == NULL)
1176 return NULL;
1177 wpas_wps_ap_pin_enable(wpa_s, timeout);
1178
1179 return hapd->conf->ap_pin;
1180 }
1181
1182
1183 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
1184 {
1185 struct hostapd_data *hapd;
1186 if (wpa_s->ap_iface == NULL)
1187 return NULL;
1188 hapd = wpa_s->ap_iface->bss[0];
1189 return hapd->conf->ap_pin;
1190 }
1191
1192
1193 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
1194 int timeout)
1195 {
1196 struct hostapd_data *hapd;
1197 char pin_txt[9];
1198 int ret;
1199
1200 if (wpa_s->ap_iface == NULL)
1201 return -1;
1202 hapd = wpa_s->ap_iface->bss[0];
1203 ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
1204 if (os_snprintf_error(sizeof(pin_txt), ret))
1205 return -1;
1206 os_free(hapd->conf->ap_pin);
1207 hapd->conf->ap_pin = os_strdup(pin_txt);
1208 if (hapd->conf->ap_pin == NULL)
1209 return -1;
1210 wpas_wps_ap_pin_enable(wpa_s, timeout);
1211
1212 return 0;
1213 }
1214
1215
1216 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
1217 {
1218 struct hostapd_data *hapd;
1219
1220 if (wpa_s->ap_iface == NULL)
1221 return;
1222 hapd = wpa_s->ap_iface->bss[0];
1223
1224 /*
1225 * Registrar failed to prove its knowledge of the AP PIN. Disable AP
1226 * PIN if this happens multiple times to slow down brute force attacks.
1227 */
1228 hapd->ap_pin_failures++;
1229 wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
1230 hapd->ap_pin_failures);
1231 if (hapd->ap_pin_failures < 3)
1232 return;
1233
1234 wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
1235 hapd->ap_pin_failures = 0;
1236 os_free(hapd->conf->ap_pin);
1237 hapd->conf->ap_pin = NULL;
1238 }
1239
1240
1241 #ifdef CONFIG_WPS_NFC
1242
1243 struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
1244 int ndef)
1245 {
1246 struct hostapd_data *hapd;
1247
1248 if (wpa_s->ap_iface == NULL)
1249 return NULL;
1250 hapd = wpa_s->ap_iface->bss[0];
1251 return hostapd_wps_nfc_config_token(hapd, ndef);
1252 }
1253
1254
1255 struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
1256 int ndef)
1257 {
1258 struct hostapd_data *hapd;
1259
1260 if (wpa_s->ap_iface == NULL)
1261 return NULL;
1262 hapd = wpa_s->ap_iface->bss[0];
1263 return hostapd_wps_nfc_hs_cr(hapd, ndef);
1264 }
1265
1266
1267 int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
1268 const struct wpabuf *req,
1269 const struct wpabuf *sel)
1270 {
1271 struct hostapd_data *hapd;
1272
1273 if (wpa_s->ap_iface == NULL)
1274 return -1;
1275 hapd = wpa_s->ap_iface->bss[0];
1276 return hostapd_wps_nfc_report_handover(hapd, req, sel);
1277 }
1278
1279 #endif /* CONFIG_WPS_NFC */
1280
1281 #endif /* CONFIG_WPS */
1282
1283
1284 #ifdef CONFIG_CTRL_IFACE
1285
1286 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
1287 char *buf, size_t buflen)
1288 {
1289 struct hostapd_data *hapd;
1290
1291 if (wpa_s->ap_iface)
1292 hapd = wpa_s->ap_iface->bss[0];
1293 else if (wpa_s->ifmsh)
1294 hapd = wpa_s->ifmsh->bss[0];
1295 else
1296 return -1;
1297 return hostapd_ctrl_iface_sta_first(hapd, buf, buflen);
1298 }
1299
1300
1301 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
1302 char *buf, size_t buflen)
1303 {
1304 struct hostapd_data *hapd;
1305
1306 if (wpa_s->ap_iface)
1307 hapd = wpa_s->ap_iface->bss[0];
1308 else if (wpa_s->ifmsh)
1309 hapd = wpa_s->ifmsh->bss[0];
1310 else
1311 return -1;
1312 return hostapd_ctrl_iface_sta(hapd, txtaddr, buf, buflen);
1313 }
1314
1315
1316 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
1317 char *buf, size_t buflen)
1318 {
1319 struct hostapd_data *hapd;
1320
1321 if (wpa_s->ap_iface)
1322 hapd = wpa_s->ap_iface->bss[0];
1323 else if (wpa_s->ifmsh)
1324 hapd = wpa_s->ifmsh->bss[0];
1325 else
1326 return -1;
1327 return hostapd_ctrl_iface_sta_next(hapd, txtaddr, buf, buflen);
1328 }
1329
1330
1331 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
1332 const char *txtaddr)
1333 {
1334 if (wpa_s->ap_iface == NULL)
1335 return -1;
1336 return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
1337 txtaddr);
1338 }
1339
1340
1341 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
1342 const char *txtaddr)
1343 {
1344 if (wpa_s->ap_iface == NULL)
1345 return -1;
1346 return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
1347 txtaddr);
1348 }
1349
1350
1351 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
1352 size_t buflen, int verbose)
1353 {
1354 char *pos = buf, *end = buf + buflen;
1355 int ret;
1356 struct hostapd_bss_config *conf;
1357
1358 if (wpa_s->ap_iface == NULL)
1359 return -1;
1360
1361 conf = wpa_s->ap_iface->bss[0]->conf;
1362 if (conf->wpa == 0)
1363 return 0;
1364
1365 ret = os_snprintf(pos, end - pos,
1366 "pairwise_cipher=%s\n"
1367 "group_cipher=%s\n"
1368 "key_mgmt=%s\n",
1369 wpa_cipher_txt(conf->rsn_pairwise),
1370 wpa_cipher_txt(conf->wpa_group),
1371 wpa_key_mgmt_txt(conf->wpa_key_mgmt,
1372 conf->wpa));
1373 if (os_snprintf_error(end - pos, ret))
1374 return pos - buf;
1375 pos += ret;
1376 return pos - buf;
1377 }
1378
1379 #endif /* CONFIG_CTRL_IFACE */
1380
1381
1382 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
1383 {
1384 struct hostapd_iface *iface = wpa_s->ap_iface;
1385 struct wpa_ssid *ssid = wpa_s->current_ssid;
1386 struct hostapd_data *hapd;
1387
1388 if (ssid == NULL || wpa_s->ap_iface == NULL ||
1389 ssid->mode == WPAS_MODE_INFRA ||
1390 ssid->mode == WPAS_MODE_IBSS)
1391 return -1;
1392
1393 #ifdef CONFIG_P2P
1394 if (ssid->mode == WPAS_MODE_P2P_GO)
1395 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
1396 else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1397 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
1398 P2P_GROUP_FORMATION;
1399 #endif /* CONFIG_P2P */
1400
1401 hapd = iface->bss[0];
1402 if (hapd->drv_priv == NULL)
1403 return -1;
1404 ieee802_11_set_beacons(iface);
1405 hostapd_set_ap_wps_ie(hapd);
1406
1407 return 0;
1408 }
1409
1410
1411 int ap_switch_channel(struct wpa_supplicant *wpa_s,
1412 struct csa_settings *settings)
1413 {
1414 #ifdef NEED_AP_MLME
1415 if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1416 return -1;
1417
1418 return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
1419 #else /* NEED_AP_MLME */
1420 return -1;
1421 #endif /* NEED_AP_MLME */
1422 }
1423
1424
1425 #ifdef CONFIG_CTRL_IFACE
1426 int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
1427 {
1428 struct csa_settings settings;
1429 int ret = hostapd_parse_csa_settings(pos, &settings);
1430
1431 if (ret)
1432 return ret;
1433
1434 return ap_switch_channel(wpa_s, &settings);
1435 }
1436 #endif /* CONFIG_CTRL_IFACE */
1437
1438
1439 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
1440 int offset, int width, int cf1, int cf2, int finished)
1441 {
1442 struct hostapd_iface *iface = wpa_s->ap_iface;
1443
1444 if (!iface)
1445 iface = wpa_s->ifmsh;
1446 if (!iface)
1447 return;
1448 wpa_s->assoc_freq = freq;
1449 if (wpa_s->current_ssid)
1450 wpa_s->current_ssid->frequency = freq;
1451 hostapd_event_ch_switch(iface->bss[0], freq, ht,
1452 offset, width, cf1, cf2, finished);
1453 }
1454
1455
1456 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1457 const u8 *addr)
1458 {
1459 struct hostapd_data *hapd;
1460 struct hostapd_bss_config *conf;
1461
1462 if (!wpa_s->ap_iface)
1463 return -1;
1464
1465 if (addr)
1466 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1467 MAC2STR(addr));
1468 else
1469 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1470
1471 hapd = wpa_s->ap_iface->bss[0];
1472 conf = hapd->conf;
1473
1474 os_free(conf->accept_mac);
1475 conf->accept_mac = NULL;
1476 conf->num_accept_mac = 0;
1477 os_free(conf->deny_mac);
1478 conf->deny_mac = NULL;
1479 conf->num_deny_mac = 0;
1480
1481 if (addr == NULL) {
1482 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1483 return 0;
1484 }
1485
1486 conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1487 conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1488 if (conf->accept_mac == NULL)
1489 return -1;
1490 os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1491 conf->num_accept_mac = 1;
1492
1493 return 0;
1494 }
1495
1496
1497 #ifdef CONFIG_WPS_NFC
1498 int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
1499 const struct wpabuf *pw, const u8 *pubkey_hash)
1500 {
1501 struct hostapd_data *hapd;
1502 struct wps_context *wps;
1503
1504 if (!wpa_s->ap_iface)
1505 return -1;
1506 hapd = wpa_s->ap_iface->bss[0];
1507 wps = hapd->wps;
1508
1509 if (wpa_s->p2pdev->conf->wps_nfc_dh_pubkey == NULL ||
1510 wpa_s->p2pdev->conf->wps_nfc_dh_privkey == NULL) {
1511 wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
1512 return -1;
1513 }
1514
1515 dh5_free(wps->dh_ctx);
1516 wpabuf_free(wps->dh_pubkey);
1517 wpabuf_free(wps->dh_privkey);
1518 wps->dh_privkey = wpabuf_dup(
1519 wpa_s->p2pdev->conf->wps_nfc_dh_privkey);
1520 wps->dh_pubkey = wpabuf_dup(
1521 wpa_s->p2pdev->conf->wps_nfc_dh_pubkey);
1522 if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
1523 wps->dh_ctx = NULL;
1524 wpabuf_free(wps->dh_pubkey);
1525 wps->dh_pubkey = NULL;
1526 wpabuf_free(wps->dh_privkey);
1527 wps->dh_privkey = NULL;
1528 return -1;
1529 }
1530 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
1531 if (wps->dh_ctx == NULL)
1532 return -1;
1533
1534 return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
1535 pw_id,
1536 pw ? wpabuf_head(pw) : NULL,
1537 pw ? wpabuf_len(pw) : 0, 1);
1538 }
1539 #endif /* CONFIG_WPS_NFC */
1540
1541
1542 #ifdef CONFIG_CTRL_IFACE
1543 int wpas_ap_stop_ap(struct wpa_supplicant *wpa_s)
1544 {
1545 struct hostapd_data *hapd;
1546
1547 if (!wpa_s->ap_iface)
1548 return -1;
1549 hapd = wpa_s->ap_iface->bss[0];
1550 return hostapd_ctrl_iface_stop_ap(hapd);
1551 }
1552
1553
1554 int wpas_ap_pmksa_cache_list(struct wpa_supplicant *wpa_s, char *buf,
1555 size_t len)
1556 {
1557 size_t reply_len = 0, i;
1558 char ap_delimiter[] = "---- AP ----\n";
1559 char mesh_delimiter[] = "---- mesh ----\n";
1560 size_t dlen;
1561
1562 if (wpa_s->ap_iface) {
1563 dlen = os_strlen(ap_delimiter);
1564 if (dlen > len - reply_len)
1565 return reply_len;
1566 os_memcpy(&buf[reply_len], ap_delimiter, dlen);
1567 reply_len += dlen;
1568
1569 for (i = 0; i < wpa_s->ap_iface->num_bss; i++) {
1570 reply_len += hostapd_ctrl_iface_pmksa_list(
1571 wpa_s->ap_iface->bss[i],
1572 &buf[reply_len], len - reply_len);
1573 }
1574 }
1575
1576 if (wpa_s->ifmsh) {
1577 dlen = os_strlen(mesh_delimiter);
1578 if (dlen > len - reply_len)
1579 return reply_len;
1580 os_memcpy(&buf[reply_len], mesh_delimiter, dlen);
1581 reply_len += dlen;
1582
1583 reply_len += hostapd_ctrl_iface_pmksa_list(
1584 wpa_s->ifmsh->bss[0], &buf[reply_len],
1585 len - reply_len);
1586 }
1587
1588 return reply_len;
1589 }
1590
1591
1592 void wpas_ap_pmksa_cache_flush(struct wpa_supplicant *wpa_s)
1593 {
1594 size_t i;
1595
1596 if (wpa_s->ap_iface) {
1597 for (i = 0; i < wpa_s->ap_iface->num_bss; i++)
1598 hostapd_ctrl_iface_pmksa_flush(wpa_s->ap_iface->bss[i]);
1599 }
1600
1601 if (wpa_s->ifmsh)
1602 hostapd_ctrl_iface_pmksa_flush(wpa_s->ifmsh->bss[0]);
1603 }
1604
1605
1606 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
1607 #ifdef CONFIG_MESH
1608
1609 int wpas_ap_pmksa_cache_list_mesh(struct wpa_supplicant *wpa_s, const u8 *addr,
1610 char *buf, size_t len)
1611 {
1612 return hostapd_ctrl_iface_pmksa_list_mesh(wpa_s->ifmsh->bss[0], addr,
1613 &buf[0], len);
1614 }
1615
1616
1617 int wpas_ap_pmksa_cache_add_external(struct wpa_supplicant *wpa_s, char *cmd)
1618 {
1619 struct external_pmksa_cache *entry;
1620 void *pmksa_cache;
1621
1622 pmksa_cache = hostapd_ctrl_iface_pmksa_create_entry(wpa_s->own_addr,
1623 cmd);
1624 if (!pmksa_cache)
1625 return -1;
1626
1627 entry = os_zalloc(sizeof(struct external_pmksa_cache));
1628 if (!entry)
1629 return -1;
1630
1631 entry->pmksa_cache = pmksa_cache;
1632
1633 dl_list_add(&wpa_s->mesh_external_pmksa_cache, &entry->list);
1634
1635 return 0;
1636 }
1637
1638 #endif /* CONFIG_MESH */
1639 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
1640
1641 #endif /* CONFIG_CTRL_IFACE */
1642
1643
1644 #ifdef NEED_AP_MLME
1645 void wpas_ap_event_dfs_radar_detected(struct wpa_supplicant *wpa_s,
1646 struct dfs_event *radar)
1647 {
1648 struct hostapd_iface *iface = wpa_s->ap_iface;
1649
1650 if (!iface)
1651 iface = wpa_s->ifmsh;
1652 if (!iface || !iface->bss[0])
1653 return;
1654 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
1655 hostapd_dfs_radar_detected(iface, radar->freq,
1656 radar->ht_enabled, radar->chan_offset,
1657 radar->chan_width,
1658 radar->cf1, radar->cf2);
1659 }
1660
1661
1662 void wpas_ap_event_dfs_cac_started(struct wpa_supplicant *wpa_s,
1663 struct dfs_event *radar)
1664 {
1665 struct hostapd_iface *iface = wpa_s->ap_iface;
1666
1667 if (!iface)
1668 iface = wpa_s->ifmsh;
1669 if (!iface || !iface->bss[0])
1670 return;
1671 wpa_printf(MSG_DEBUG, "DFS CAC started on %d MHz", radar->freq);
1672 hostapd_dfs_start_cac(iface, radar->freq,
1673 radar->ht_enabled, radar->chan_offset,
1674 radar->chan_width, radar->cf1, radar->cf2);
1675 }
1676
1677
1678 void wpas_ap_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
1679 struct dfs_event *radar)
1680 {
1681 struct hostapd_iface *iface = wpa_s->ap_iface;
1682
1683 if (!iface)
1684 iface = wpa_s->ifmsh;
1685 if (!iface || !iface->bss[0])
1686 return;
1687 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
1688 hostapd_dfs_complete_cac(iface, 1, radar->freq,
1689 radar->ht_enabled, radar->chan_offset,
1690 radar->chan_width, radar->cf1, radar->cf2);
1691 }
1692
1693
1694 void wpas_ap_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
1695 struct dfs_event *radar)
1696 {
1697 struct hostapd_iface *iface = wpa_s->ap_iface;
1698
1699 if (!iface)
1700 iface = wpa_s->ifmsh;
1701 if (!iface || !iface->bss[0])
1702 return;
1703 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
1704 hostapd_dfs_complete_cac(iface, 0, radar->freq,
1705 radar->ht_enabled, radar->chan_offset,
1706 radar->chan_width, radar->cf1, radar->cf2);
1707 }
1708
1709
1710 void wpas_ap_event_dfs_cac_nop_finished(struct wpa_supplicant *wpa_s,
1711 struct dfs_event *radar)
1712 {
1713 struct hostapd_iface *iface = wpa_s->ap_iface;
1714
1715 if (!iface)
1716 iface = wpa_s->ifmsh;
1717 if (!iface || !iface->bss[0])
1718 return;
1719 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
1720 hostapd_dfs_nop_finished(iface, radar->freq,
1721 radar->ht_enabled, radar->chan_offset,
1722 radar->chan_width, radar->cf1, radar->cf2);
1723 }
1724 #endif /* NEED_AP_MLME */
1725
1726
1727 void ap_periodic(struct wpa_supplicant *wpa_s)
1728 {
1729 if (wpa_s->ap_iface)
1730 hostapd_periodic_iface(wpa_s->ap_iface);
1731 }