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