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