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