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