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