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