OSDN Git Service

Accumulative patch from commit 9e4d304ed7e2516b92081a5fba7288715ebb
[android-x86/external-wpa_supplicant_8.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 "ap/hostapd.h"
18 #include "ap/ap_config.h"
19 #include "ap/ap_drv_ops.h"
20 #ifdef NEED_AP_MLME
21 #include "ap/ieee802_11.h"
22 #endif /* NEED_AP_MLME */
23 #include "ap/beacon.h"
24 #include "ap/ieee802_1x.h"
25 #include "ap/wps_hostapd.h"
26 #include "ap/ctrl_iface_ap.h"
27 #include "wps/wps.h"
28 #include "common/ieee802_11_defs.h"
29 #include "config_ssid.h"
30 #include "config.h"
31 #include "wpa_supplicant_i.h"
32 #include "driver_i.h"
33 #include "p2p_supplicant.h"
34 #include "ap.h"
35 #include "ap/sta_info.h"
36 #include "notify.h"
37
38
39 #ifdef CONFIG_WPS
40 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
41 #endif /* CONFIG_WPS */
42
43
44 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
45                                   struct wpa_ssid *ssid,
46                                   struct hostapd_config *conf)
47 {
48         struct hostapd_bss_config *bss = &conf->bss[0];
49
50         conf->driver = wpa_s->driver;
51
52         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
53
54         if (ssid->frequency == 0) {
55                 /* default channel 11 */
56                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
57                 conf->channel = 11;
58         } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
59                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
60                 conf->channel = (ssid->frequency - 2407) / 5;
61         } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
62                    (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
63                 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
64                 conf->channel = (ssid->frequency - 5000) / 5;
65         } else if (ssid->frequency >= 56160 + 2160 * 1 &&
66                    ssid->frequency <= 56160 + 2160 * 4) {
67                 conf->hw_mode = HOSTAPD_MODE_IEEE80211AD;
68                 conf->channel = (ssid->frequency - 56160) / 2160;
69         } else {
70                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
71                            ssid->frequency);
72                 return -1;
73         }
74
75         /* TODO: enable HT40 if driver supports it;
76          * drop to 11b if driver does not support 11g */
77
78 #ifdef CONFIG_IEEE80211N
79         /*
80          * Enable HT20 if the driver supports it, by setting conf->ieee80211n
81          * and a mask of allowed capabilities within conf->ht_capab.
82          * Using default config settings for: conf->ht_op_mode_fixed,
83          * conf->secondary_channel, conf->require_ht
84          */
85         if (wpa_s->hw.modes) {
86                 struct hostapd_hw_modes *mode = NULL;
87                 int i, no_ht = 0;
88                 for (i = 0; i < wpa_s->hw.num_modes; i++) {
89                         if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
90                                 mode = &wpa_s->hw.modes[i];
91                                 break;
92                         }
93                 }
94
95 #ifdef CONFIG_HT_OVERRIDES
96                 if (ssid->disable_ht) {
97                         conf->ieee80211n = 0;
98                         conf->ht_capab = 0;
99                         no_ht = 1;
100                 }
101 #endif /* CONFIG_HT_OVERRIDES */
102
103                 if (!no_ht && mode && mode->ht_capab) {
104                         conf->ieee80211n = 1;
105 #ifdef CONFIG_P2P
106                         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
107                             (mode->ht_capab &
108                              HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
109                             ssid->ht40)
110                                 conf->secondary_channel =
111                                         wpas_p2p_get_ht40_mode(wpa_s, mode,
112                                                                conf->channel);
113                         if (conf->secondary_channel)
114                                 conf->ht_capab |=
115                                         HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
116 #endif /* CONFIG_P2P */
117
118                         /*
119                          * white-list capabilities that won't cause issues
120                          * to connecting stations, while leaving the current
121                          * capabilities intact (currently disabled SMPS).
122                          */
123                         conf->ht_capab |= mode->ht_capab &
124                                 (HT_CAP_INFO_GREEN_FIELD |
125                                  HT_CAP_INFO_SHORT_GI20MHZ |
126                                  HT_CAP_INFO_SHORT_GI40MHZ |
127                                  HT_CAP_INFO_RX_STBC_MASK |
128                                  HT_CAP_INFO_MAX_AMSDU_SIZE);
129                 }
130         }
131 #endif /* CONFIG_IEEE80211N */
132
133 #ifdef CONFIG_P2P
134         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
135                 /* Remove 802.11b rates from supported and basic rate sets */
136                 int *list = os_malloc(4 * sizeof(int));
137                 if (list) {
138                         list[0] = 60;
139                         list[1] = 120;
140                         list[2] = 240;
141                         list[3] = -1;
142                 }
143                 conf->basic_rates = list;
144
145                 list = os_malloc(9 * sizeof(int));
146                 if (list) {
147                         list[0] = 60;
148                         list[1] = 90;
149                         list[2] = 120;
150                         list[3] = 180;
151                         list[4] = 240;
152                         list[5] = 360;
153                         list[6] = 480;
154                         list[7] = 540;
155                         list[8] = -1;
156                 }
157                 conf->supported_rates = list;
158         }
159
160         bss->isolate = !wpa_s->conf->p2p_intra_bss;
161 #endif /* CONFIG_P2P */
162
163         if (ssid->ssid_len == 0) {
164                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
165                 return -1;
166         }
167         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
168         bss->ssid.ssid_len = ssid->ssid_len;
169         bss->ssid.ssid_set = 1;
170
171         bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
172
173         if (ssid->auth_alg)
174                 bss->auth_algs = ssid->auth_alg;
175
176         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
177                 bss->wpa = ssid->proto;
178         bss->wpa_key_mgmt = ssid->key_mgmt;
179         bss->wpa_pairwise = ssid->pairwise_cipher;
180         if (ssid->psk_set) {
181                 os_free(bss->ssid.wpa_psk);
182                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
183                 if (bss->ssid.wpa_psk == NULL)
184                         return -1;
185                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
186                 bss->ssid.wpa_psk->group = 1;
187         } else if (ssid->passphrase) {
188                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
189         } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
190                    ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
191                 struct hostapd_wep_keys *wep = &bss->ssid.wep;
192                 int i;
193                 for (i = 0; i < NUM_WEP_KEYS; i++) {
194                         if (ssid->wep_key_len[i] == 0)
195                                 continue;
196                         wep->key[i] = os_malloc(ssid->wep_key_len[i]);
197                         if (wep->key[i] == NULL)
198                                 return -1;
199                         os_memcpy(wep->key[i], ssid->wep_key[i],
200                                   ssid->wep_key_len[i]);
201                         wep->len[i] = ssid->wep_key_len[i];
202                 }
203                 wep->idx = ssid->wep_tx_keyidx;
204                 wep->keys_set = 1;
205         }
206
207         if (ssid->ap_max_inactivity)
208                 bss->ap_max_inactivity = ssid->ap_max_inactivity;
209
210         if (ssid->dtim_period)
211                 bss->dtim_period = ssid->dtim_period;
212
213         if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
214                 bss->rsn_pairwise = bss->wpa_pairwise;
215         bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
216                                                     bss->rsn_pairwise);
217
218         if (bss->wpa && bss->ieee802_1x)
219                 bss->ssid.security_policy = SECURITY_WPA;
220         else if (bss->wpa)
221                 bss->ssid.security_policy = SECURITY_WPA_PSK;
222         else if (bss->ieee802_1x) {
223                 int cipher = WPA_CIPHER_NONE;
224                 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
225                 bss->ssid.wep.default_len = bss->default_wep_key_len;
226                 if (bss->default_wep_key_len)
227                         cipher = bss->default_wep_key_len >= 13 ?
228                                 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
229                 bss->wpa_group = cipher;
230                 bss->wpa_pairwise = cipher;
231                 bss->rsn_pairwise = cipher;
232         } else if (bss->ssid.wep.keys_set) {
233                 int cipher = WPA_CIPHER_WEP40;
234                 if (bss->ssid.wep.len[0] >= 13)
235                         cipher = WPA_CIPHER_WEP104;
236                 bss->ssid.security_policy = SECURITY_STATIC_WEP;
237                 bss->wpa_group = cipher;
238                 bss->wpa_pairwise = cipher;
239                 bss->rsn_pairwise = cipher;
240         } else {
241                 bss->ssid.security_policy = SECURITY_PLAINTEXT;
242                 bss->wpa_group = WPA_CIPHER_NONE;
243                 bss->wpa_pairwise = WPA_CIPHER_NONE;
244                 bss->rsn_pairwise = WPA_CIPHER_NONE;
245         }
246
247 #ifdef CONFIG_WPS
248         /*
249          * Enable WPS by default for open and WPA/WPA2-Personal network, but
250          * require user interaction to actually use it. Only the internal
251          * Registrar is supported.
252          */
253         if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
254             bss->ssid.security_policy != SECURITY_PLAINTEXT)
255                 goto no_wps;
256 #ifdef CONFIG_WPS2
257         if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
258             (!(bss->rsn_pairwise & WPA_CIPHER_CCMP) || !(bss->wpa & 2)))
259                 goto no_wps; /* WPS2 does not allow WPA/TKIP-only
260                               * configuration */
261 #endif /* CONFIG_WPS2 */
262         bss->eap_server = 1;
263
264         if (!ssid->ignore_broadcast_ssid)
265                 bss->wps_state = 2;
266
267         bss->ap_setup_locked = 2;
268         if (wpa_s->conf->config_methods)
269                 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
270         os_memcpy(bss->device_type, wpa_s->conf->device_type,
271                   WPS_DEV_TYPE_LEN);
272         if (wpa_s->conf->device_name) {
273                 bss->device_name = os_strdup(wpa_s->conf->device_name);
274                 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
275         }
276         if (wpa_s->conf->manufacturer)
277                 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
278         if (wpa_s->conf->model_name)
279                 bss->model_name = os_strdup(wpa_s->conf->model_name);
280         if (wpa_s->conf->model_number)
281                 bss->model_number = os_strdup(wpa_s->conf->model_number);
282         if (wpa_s->conf->serial_number)
283                 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
284         if (is_nil_uuid(wpa_s->conf->uuid))
285                 os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
286         else
287                 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
288         os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
289         bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
290 no_wps:
291 #endif /* CONFIG_WPS */
292
293         if (wpa_s->max_stations &&
294             wpa_s->max_stations < wpa_s->conf->max_num_sta)
295                 bss->max_num_sta = wpa_s->max_stations;
296         else
297                 bss->max_num_sta = wpa_s->conf->max_num_sta;
298
299         bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
300
301         return 0;
302 }
303
304
305 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
306 {
307 #ifdef CONFIG_P2P
308         struct wpa_supplicant *wpa_s = ctx;
309         const struct ieee80211_mgmt *mgmt;
310         size_t hdr_len;
311
312         mgmt = (const struct ieee80211_mgmt *) buf;
313         hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
314         if (hdr_len > len)
315                 return;
316         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
317                            mgmt->u.action.category,
318                            &mgmt->u.action.u.vs_public_action.action,
319                            len - hdr_len, freq);
320 #endif /* CONFIG_P2P */
321 }
322
323
324 static void ap_wps_event_cb(void *ctx, enum wps_event event,
325                             union wps_event_data *data)
326 {
327 #ifdef CONFIG_P2P
328         struct wpa_supplicant *wpa_s = ctx;
329
330         if (event == WPS_EV_FAIL) {
331                 struct wps_event_fail *fail = &data->fail;
332
333                 if (wpa_s->parent && wpa_s->parent != wpa_s &&
334                     wpa_s == wpa_s->global->p2p_group_formation) {
335                         /*
336                          * src/ap/wps_hostapd.c has already sent this on the
337                          * main interface, so only send on the parent interface
338                          * here if needed.
339                          */
340                         wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
341                                 "msg=%d config_error=%d",
342                                 fail->msg, fail->config_error);
343                 }
344                 wpas_p2p_wps_failed(wpa_s, fail);
345         }
346 #endif /* CONFIG_P2P */
347 }
348
349
350 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
351                                  int authorized, const u8 *p2p_dev_addr)
352 {
353         wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
354 }
355
356
357 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
358 {
359 #ifdef CONFIG_P2P
360         struct wpa_supplicant *wpa_s = ctx;
361         const struct ieee80211_mgmt *mgmt;
362         size_t hdr_len;
363
364         mgmt = (const struct ieee80211_mgmt *) buf;
365         hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
366         if (hdr_len > len)
367                 return -1;
368         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
369                            mgmt->u.action.category,
370                            &mgmt->u.action.u.vs_public_action.action,
371                            len - hdr_len, freq);
372 #endif /* CONFIG_P2P */
373         return 0;
374 }
375
376
377 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
378                            const u8 *bssid, const u8 *ie, size_t ie_len,
379                            int ssi_signal)
380 {
381 #ifdef CONFIG_P2P
382         struct wpa_supplicant *wpa_s = ctx;
383         return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
384                                      ssi_signal);
385 #else /* CONFIG_P2P */
386         return 0;
387 #endif /* CONFIG_P2P */
388 }
389
390
391 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
392                                   const u8 *uuid_e)
393 {
394 #ifdef CONFIG_P2P
395         struct wpa_supplicant *wpa_s = ctx;
396         wpas_p2p_wps_success(wpa_s, mac_addr, 1);
397 #endif /* CONFIG_P2P */
398 }
399
400
401 static void wpas_ap_configured_cb(void *ctx)
402 {
403         struct wpa_supplicant *wpa_s = ctx;
404
405         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
406
407         if (wpa_s->ap_configured_cb)
408                 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
409                                         wpa_s->ap_configured_cb_data);
410 }
411
412
413 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
414                              struct wpa_ssid *ssid)
415 {
416         struct wpa_driver_associate_params params;
417         struct hostapd_iface *hapd_iface;
418         struct hostapd_config *conf;
419         size_t i;
420
421         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
422                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
423                 return -1;
424         }
425
426         wpa_supplicant_ap_deinit(wpa_s);
427
428         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
429                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
430
431         os_memset(&params, 0, sizeof(params));
432         params.ssid = ssid->ssid;
433         params.ssid_len = ssid->ssid_len;
434         switch (ssid->mode) {
435         case WPAS_MODE_INFRA:
436                 params.mode = IEEE80211_MODE_INFRA;
437                 break;
438         case WPAS_MODE_IBSS:
439                 params.mode = IEEE80211_MODE_IBSS;
440                 break;
441         case WPAS_MODE_AP:
442         case WPAS_MODE_P2P_GO:
443         case WPAS_MODE_P2P_GROUP_FORMATION:
444                 params.mode = IEEE80211_MODE_AP;
445                 break;
446         }
447         params.freq = ssid->frequency;
448
449         params.wpa_proto = ssid->proto;
450         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
451                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
452         else
453                 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
454         params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
455
456         wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
457                                                           1);
458         if (wpa_s->pairwise_cipher < 0) {
459                 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
460                            "cipher.");
461                 return -1;
462         }
463         params.pairwise_suite =
464                 wpa_cipher_to_suite_driver(wpa_s->pairwise_cipher);
465         params.group_suite = params.pairwise_suite;
466
467 #ifdef CONFIG_P2P
468         if (ssid->mode == WPAS_MODE_P2P_GO ||
469             ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
470                 params.p2p = 1;
471 #endif /* CONFIG_P2P */
472
473         if (wpa_s->parent->set_ap_uapsd)
474                 params.uapsd = wpa_s->parent->ap_uapsd;
475         else
476                 params.uapsd = -1;
477
478         if (wpa_drv_associate(wpa_s, &params) < 0) {
479                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
480                 return -1;
481         }
482
483         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
484         if (hapd_iface == NULL)
485                 return -1;
486         hapd_iface->owner = wpa_s;
487         hapd_iface->drv_flags = wpa_s->drv_flags;
488         hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
489
490         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
491         if (conf == NULL) {
492                 wpa_supplicant_ap_deinit(wpa_s);
493                 return -1;
494         }
495
496         os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
497                   wpa_s->conf->wmm_ac_params,
498                   sizeof(wpa_s->conf->wmm_ac_params));
499
500         if (params.uapsd > 0) {
501                 conf->bss->wmm_enabled = 1;
502                 conf->bss->wmm_uapsd = 1;
503         }
504
505         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
506                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
507                 wpa_supplicant_ap_deinit(wpa_s);
508                 return -1;
509         }
510
511 #ifdef CONFIG_P2P
512         if (ssid->mode == WPAS_MODE_P2P_GO)
513                 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
514         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
515                 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
516                         P2P_GROUP_FORMATION;
517 #endif /* CONFIG_P2P */
518
519         hapd_iface->num_bss = conf->num_bss;
520         hapd_iface->bss = os_calloc(conf->num_bss,
521                                     sizeof(struct hostapd_data *));
522         if (hapd_iface->bss == NULL) {
523                 wpa_supplicant_ap_deinit(wpa_s);
524                 return -1;
525         }
526
527         for (i = 0; i < conf->num_bss; i++) {
528                 hapd_iface->bss[i] =
529                         hostapd_alloc_bss_data(hapd_iface, conf,
530                                                &conf->bss[i]);
531                 if (hapd_iface->bss[i] == NULL) {
532                         wpa_supplicant_ap_deinit(wpa_s);
533                         return -1;
534                 }
535
536                 hapd_iface->bss[i]->msg_ctx = wpa_s;
537                 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
538                 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
539                 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
540                 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
541                 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
542                 hostapd_register_probereq_cb(hapd_iface->bss[i],
543                                              ap_probe_req_rx, wpa_s);
544                 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
545                 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
546                 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
547                 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
548                 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
549                 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
550 #ifdef CONFIG_P2P
551                 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
552                 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
553                                                                     ssid);
554 #endif /* CONFIG_P2P */
555                 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
556                 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
557         }
558
559         os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
560         hapd_iface->bss[0]->driver = wpa_s->driver;
561         hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
562
563         wpa_s->current_ssid = ssid;
564         os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
565         wpa_s->assoc_freq = ssid->frequency;
566
567         if (hostapd_setup_interface(wpa_s->ap_iface)) {
568                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
569                 wpa_supplicant_ap_deinit(wpa_s);
570                 return -1;
571         }
572
573         return 0;
574 }
575
576
577 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
578 {
579 #ifdef CONFIG_WPS
580         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
581 #endif /* CONFIG_WPS */
582
583         if (wpa_s->ap_iface == NULL)
584                 return;
585
586         wpa_s->current_ssid = NULL;
587         wpa_s->assoc_freq = 0;
588 #ifdef CONFIG_P2P
589         if (wpa_s->ap_iface->bss)
590                 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
591         wpas_p2p_group_deinit(wpa_s);
592 #endif /* CONFIG_P2P */
593         hostapd_interface_deinit(wpa_s->ap_iface);
594         hostapd_interface_free(wpa_s->ap_iface);
595         wpa_s->ap_iface = NULL;
596         wpa_drv_deinit_ap(wpa_s);
597 }
598
599
600 void ap_tx_status(void *ctx, const u8 *addr,
601                   const u8 *buf, size_t len, int ack)
602 {
603 #ifdef NEED_AP_MLME
604         struct wpa_supplicant *wpa_s = ctx;
605         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
606 #endif /* NEED_AP_MLME */
607 }
608
609
610 void ap_eapol_tx_status(void *ctx, const u8 *dst,
611                         const u8 *data, size_t len, int ack)
612 {
613 #ifdef NEED_AP_MLME
614         struct wpa_supplicant *wpa_s = ctx;
615         hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
616 #endif /* NEED_AP_MLME */
617 }
618
619
620 void ap_client_poll_ok(void *ctx, const u8 *addr)
621 {
622 #ifdef NEED_AP_MLME
623         struct wpa_supplicant *wpa_s = ctx;
624         if (wpa_s->ap_iface)
625                 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
626 #endif /* NEED_AP_MLME */
627 }
628
629
630 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
631 {
632 #ifdef NEED_AP_MLME
633         struct wpa_supplicant *wpa_s = ctx;
634         ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
635 #endif /* NEED_AP_MLME */
636 }
637
638
639 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
640 {
641 #ifdef NEED_AP_MLME
642         struct wpa_supplicant *wpa_s = ctx;
643         struct hostapd_frame_info fi;
644         os_memset(&fi, 0, sizeof(fi));
645         fi.datarate = rx_mgmt->datarate;
646         fi.ssi_signal = rx_mgmt->ssi_signal;
647         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
648                         rx_mgmt->frame_len, &fi);
649 #endif /* NEED_AP_MLME */
650 }
651
652
653 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
654 {
655 #ifdef NEED_AP_MLME
656         struct wpa_supplicant *wpa_s = ctx;
657         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
658 #endif /* NEED_AP_MLME */
659 }
660
661
662 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
663                                 const u8 *src_addr, const u8 *buf, size_t len)
664 {
665         ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
666 }
667
668
669 #ifdef CONFIG_WPS
670
671 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
672                               const u8 *p2p_dev_addr)
673 {
674         if (!wpa_s->ap_iface)
675                 return -1;
676         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
677                                          p2p_dev_addr);
678 }
679
680
681 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
682 {
683         struct wps_registrar *reg;
684         int reg_sel = 0, wps_sta = 0;
685
686         if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
687                 return -1;
688
689         reg = wpa_s->ap_iface->bss[0]->wps->registrar;
690         reg_sel = wps_registrar_wps_cancel(reg);
691         wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
692                                   ap_sta_wps_cancel, NULL);
693
694         if (!reg_sel && !wps_sta) {
695                 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
696                            "time");
697                 return -1;
698         }
699
700         /*
701          * There are 2 cases to return wps cancel as success:
702          * 1. When wps cancel was initiated but no connection has been
703          *    established with client yet.
704          * 2. Client is in the middle of exchanging WPS messages.
705          */
706
707         return 0;
708 }
709
710
711 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
712                               const char *pin, char *buf, size_t buflen,
713                               int timeout)
714 {
715         int ret, ret_len = 0;
716
717         if (!wpa_s->ap_iface)
718                 return -1;
719
720         if (pin == NULL) {
721                 unsigned int rpin = wps_generate_pin();
722                 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
723                 pin = buf;
724         } else
725                 ret_len = os_snprintf(buf, buflen, "%s", pin);
726
727         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
728                                   timeout);
729         if (ret)
730                 return -1;
731         return ret_len;
732 }
733
734
735 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
736 {
737         struct wpa_supplicant *wpa_s = eloop_data;
738         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
739         wpas_wps_ap_pin_disable(wpa_s);
740 }
741
742
743 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
744 {
745         struct hostapd_data *hapd;
746
747         if (wpa_s->ap_iface == NULL)
748                 return;
749         hapd = wpa_s->ap_iface->bss[0];
750         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
751         hapd->ap_pin_failures = 0;
752         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
753         if (timeout > 0)
754                 eloop_register_timeout(timeout, 0,
755                                        wpas_wps_ap_pin_timeout, wpa_s, NULL);
756 }
757
758
759 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
760 {
761         struct hostapd_data *hapd;
762
763         if (wpa_s->ap_iface == NULL)
764                 return;
765         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
766         hapd = wpa_s->ap_iface->bss[0];
767         os_free(hapd->conf->ap_pin);
768         hapd->conf->ap_pin = NULL;
769         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
770 }
771
772
773 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
774 {
775         struct hostapd_data *hapd;
776         unsigned int pin;
777         char pin_txt[9];
778
779         if (wpa_s->ap_iface == NULL)
780                 return NULL;
781         hapd = wpa_s->ap_iface->bss[0];
782         pin = wps_generate_pin();
783         os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
784         os_free(hapd->conf->ap_pin);
785         hapd->conf->ap_pin = os_strdup(pin_txt);
786         if (hapd->conf->ap_pin == NULL)
787                 return NULL;
788         wpas_wps_ap_pin_enable(wpa_s, timeout);
789
790         return hapd->conf->ap_pin;
791 }
792
793
794 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
795 {
796         struct hostapd_data *hapd;
797         if (wpa_s->ap_iface == NULL)
798                 return NULL;
799         hapd = wpa_s->ap_iface->bss[0];
800         return hapd->conf->ap_pin;
801 }
802
803
804 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
805                         int timeout)
806 {
807         struct hostapd_data *hapd;
808         char pin_txt[9];
809         int ret;
810
811         if (wpa_s->ap_iface == NULL)
812                 return -1;
813         hapd = wpa_s->ap_iface->bss[0];
814         ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
815         if (ret < 0 || ret >= (int) sizeof(pin_txt))
816                 return -1;
817         os_free(hapd->conf->ap_pin);
818         hapd->conf->ap_pin = os_strdup(pin_txt);
819         if (hapd->conf->ap_pin == NULL)
820                 return -1;
821         wpas_wps_ap_pin_enable(wpa_s, timeout);
822
823         return 0;
824 }
825
826
827 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
828 {
829         struct hostapd_data *hapd;
830
831         if (wpa_s->ap_iface == NULL)
832                 return;
833         hapd = wpa_s->ap_iface->bss[0];
834
835         /*
836          * Registrar failed to prove its knowledge of the AP PIN. Disable AP
837          * PIN if this happens multiple times to slow down brute force attacks.
838          */
839         hapd->ap_pin_failures++;
840         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
841                    hapd->ap_pin_failures);
842         if (hapd->ap_pin_failures < 3)
843                 return;
844
845         wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
846         hapd->ap_pin_failures = 0;
847         os_free(hapd->conf->ap_pin);
848         hapd->conf->ap_pin = NULL;
849 }
850
851
852 #ifdef CONFIG_WPS_NFC
853
854 struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
855                                              int ndef)
856 {
857         struct hostapd_data *hapd;
858
859         if (wpa_s->ap_iface == NULL)
860                 return NULL;
861         hapd = wpa_s->ap_iface->bss[0];
862         return hostapd_wps_nfc_config_token(hapd, ndef);
863 }
864
865
866 struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
867                                              int ndef)
868 {
869         struct hostapd_data *hapd;
870
871         if (wpa_s->ap_iface == NULL)
872                 return NULL;
873         hapd = wpa_s->ap_iface->bss[0];
874         return hostapd_wps_nfc_hs_cr(hapd, ndef);
875 }
876
877 #endif /* CONFIG_WPS_NFC */
878
879 #endif /* CONFIG_WPS */
880
881
882 #ifdef CONFIG_CTRL_IFACE
883
884 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
885                             char *buf, size_t buflen)
886 {
887         if (wpa_s->ap_iface == NULL)
888                 return -1;
889         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
890                                             buf, buflen);
891 }
892
893
894 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
895                       char *buf, size_t buflen)
896 {
897         if (wpa_s->ap_iface == NULL)
898                 return -1;
899         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
900                                       buf, buflen);
901 }
902
903
904 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
905                            char *buf, size_t buflen)
906 {
907         if (wpa_s->ap_iface == NULL)
908                 return -1;
909         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
910                                            buf, buflen);
911 }
912
913
914 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
915                                    const char *txtaddr)
916 {
917         if (wpa_s->ap_iface == NULL)
918                 return -1;
919         return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
920                                                txtaddr);
921 }
922
923
924 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
925                                      const char *txtaddr)
926 {
927         if (wpa_s->ap_iface == NULL)
928                 return -1;
929         return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
930                                                  txtaddr);
931 }
932
933
934 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
935                                  size_t buflen, int verbose)
936 {
937         char *pos = buf, *end = buf + buflen;
938         int ret;
939         struct hostapd_bss_config *conf;
940
941         if (wpa_s->ap_iface == NULL)
942                 return -1;
943
944         conf = wpa_s->ap_iface->bss[0]->conf;
945         if (conf->wpa == 0)
946                 return 0;
947
948         ret = os_snprintf(pos, end - pos,
949                           "pairwise_cipher=%s\n"
950                           "group_cipher=%s\n"
951                           "key_mgmt=%s\n",
952                           wpa_cipher_txt(conf->rsn_pairwise),
953                           wpa_cipher_txt(conf->wpa_group),
954                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
955                                            conf->wpa));
956         if (ret < 0 || ret >= end - pos)
957                 return pos - buf;
958         pos += ret;
959         return pos - buf;
960 }
961
962 #endif /* CONFIG_CTRL_IFACE */
963
964
965 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
966 {
967         struct hostapd_iface *iface = wpa_s->ap_iface;
968         struct wpa_ssid *ssid = wpa_s->current_ssid;
969         struct hostapd_data *hapd;
970
971         if (ssid == NULL || wpa_s->ap_iface == NULL ||
972             ssid->mode == WPAS_MODE_INFRA ||
973             ssid->mode == WPAS_MODE_IBSS)
974                 return -1;
975
976 #ifdef CONFIG_P2P
977         if (ssid->mode == WPAS_MODE_P2P_GO)
978                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
979         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
980                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
981                         P2P_GROUP_FORMATION;
982 #endif /* CONFIG_P2P */
983
984         hapd = iface->bss[0];
985         if (hapd->drv_priv == NULL)
986                 return -1;
987         ieee802_11_set_beacons(iface);
988         hostapd_set_ap_wps_ie(hapd);
989
990         return 0;
991 }
992
993
994 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
995                        int offset)
996 {
997         if (!wpa_s->ap_iface)
998                 return;
999
1000         wpa_s->assoc_freq = freq;
1001         hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset);
1002 }
1003
1004
1005 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1006                                       const u8 *addr)
1007 {
1008         struct hostapd_data *hapd;
1009         struct hostapd_bss_config *conf;
1010
1011         if (!wpa_s->ap_iface)
1012                 return -1;
1013
1014         if (addr)
1015                 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1016                            MAC2STR(addr));
1017         else
1018                 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1019
1020         hapd = wpa_s->ap_iface->bss[0];
1021         conf = hapd->conf;
1022
1023         os_free(conf->accept_mac);
1024         conf->accept_mac = NULL;
1025         conf->num_accept_mac = 0;
1026         os_free(conf->deny_mac);
1027         conf->deny_mac = NULL;
1028         conf->num_deny_mac = 0;
1029
1030         if (addr == NULL) {
1031                 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1032                 return 0;
1033         }
1034
1035         conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1036         conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1037         if (conf->accept_mac == NULL)
1038                 return -1;
1039         os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1040         conf->num_accept_mac = 1;
1041
1042         return 0;
1043 }