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