wpa_cli: Correct return value of wpa_cli_cmd_p2p_peers
[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]->public_action_cb = ap_public_action_rx;
494                 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
495                 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
496                 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
497                 hostapd_register_probereq_cb(hapd_iface->bss[i],
498                                              ap_probe_req_rx, wpa_s);
499                 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
500                 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
501                 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
502                 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
503                 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
504                 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
505 #ifdef CONFIG_P2P
506                 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
507                 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
508                         wpa_s, ssid->p2p_persistent_group,
509                         ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
510 #endif /* CONFIG_P2P */
511                 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
512                 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
513         }
514
515         os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
516         hapd_iface->bss[0]->driver = wpa_s->driver;
517         hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
518
519         wpa_s->current_ssid = ssid;
520         os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
521         wpa_s->assoc_freq = ssid->frequency;
522
523         if (hostapd_setup_interface(wpa_s->ap_iface)) {
524                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
525                 wpa_supplicant_ap_deinit(wpa_s);
526                 return -1;
527         }
528
529         return 0;
530 }
531
532
533 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
534 {
535 #ifdef CONFIG_WPS
536         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
537 #endif /* CONFIG_WPS */
538
539         if (wpa_s->ap_iface == NULL)
540                 return;
541
542         wpa_s->current_ssid = NULL;
543         wpa_s->assoc_freq = 0;
544         wpa_s->reassociated_connection = 0;
545 #ifdef CONFIG_P2P
546         if (wpa_s->ap_iface->bss)
547                 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
548         wpas_p2p_group_deinit(wpa_s);
549 #endif /* CONFIG_P2P */
550         hostapd_interface_deinit(wpa_s->ap_iface);
551         hostapd_interface_free(wpa_s->ap_iface);
552         wpa_s->ap_iface = NULL;
553         wpa_drv_deinit_ap(wpa_s);
554 }
555
556
557 void ap_tx_status(void *ctx, const u8 *addr,
558                   const u8 *buf, size_t len, int ack)
559 {
560 #ifdef NEED_AP_MLME
561         struct wpa_supplicant *wpa_s = ctx;
562         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
563 #endif /* NEED_AP_MLME */
564 }
565
566
567 void ap_eapol_tx_status(void *ctx, const u8 *dst,
568                         const u8 *data, size_t len, int ack)
569 {
570 #ifdef NEED_AP_MLME
571         struct wpa_supplicant *wpa_s = ctx;
572         hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
573 #endif /* NEED_AP_MLME */
574 }
575
576
577 void ap_client_poll_ok(void *ctx, const u8 *addr)
578 {
579 #ifdef NEED_AP_MLME
580         struct wpa_supplicant *wpa_s = ctx;
581         if (wpa_s->ap_iface)
582                 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
583 #endif /* NEED_AP_MLME */
584 }
585
586
587 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
588 {
589 #ifdef NEED_AP_MLME
590         struct wpa_supplicant *wpa_s = ctx;
591         ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
592 #endif /* NEED_AP_MLME */
593 }
594
595
596 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
597 {
598 #ifdef NEED_AP_MLME
599         struct wpa_supplicant *wpa_s = ctx;
600         struct hostapd_frame_info fi;
601         os_memset(&fi, 0, sizeof(fi));
602         fi.datarate = rx_mgmt->datarate;
603         fi.ssi_signal = rx_mgmt->ssi_signal;
604         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
605                         rx_mgmt->frame_len, &fi);
606 #endif /* NEED_AP_MLME */
607 }
608
609
610 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
611 {
612 #ifdef NEED_AP_MLME
613         struct wpa_supplicant *wpa_s = ctx;
614         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
615 #endif /* NEED_AP_MLME */
616 }
617
618
619 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
620                                 const u8 *src_addr, const u8 *buf, size_t len)
621 {
622         ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
623 }
624
625
626 #ifdef CONFIG_WPS
627
628 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
629                               const u8 *p2p_dev_addr)
630 {
631         if (!wpa_s->ap_iface)
632                 return -1;
633         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
634                                          p2p_dev_addr);
635 }
636
637
638 static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
639                                             struct sta_info *sta, void *ctx)
640 {
641         if (sta && (sta->flags & WLAN_STA_WPS)) {
642                 ap_sta_deauthenticate(hapd, sta,
643                                       WLAN_REASON_PREV_AUTH_NOT_VALID);
644                 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
645                            __func__, MAC2STR(sta->addr));
646                 return 1;
647         }
648
649         return 0;
650 }
651
652
653 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
654 {
655         struct wps_registrar *reg;
656         int reg_sel = 0, wps_sta = 0;
657
658         if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
659                 return -1;
660
661         reg = wpa_s->ap_iface->bss[0]->wps->registrar;
662         reg_sel = wps_registrar_wps_cancel(reg);
663         wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
664                                   wpa_supplicant_ap_wps_sta_cancel, NULL);
665
666         if (!reg_sel && !wps_sta) {
667                 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
668                            "time");
669                 return -1;
670         }
671
672         /*
673          * There are 2 cases to return wps cancel as success:
674          * 1. When wps cancel was initiated but no connection has been
675          *    established with client yet.
676          * 2. Client is in the middle of exchanging WPS messages.
677          */
678
679         return 0;
680 }
681
682
683 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
684                               const char *pin, char *buf, size_t buflen)
685 {
686         int ret, ret_len = 0;
687
688         if (!wpa_s->ap_iface)
689                 return -1;
690
691         if (pin == NULL) {
692                 unsigned int rpin = wps_generate_pin();
693                 ret_len = os_snprintf(buf, buflen, "%d", rpin);
694                 pin = buf;
695         } else
696                 ret_len = os_snprintf(buf, buflen, "%s", pin);
697
698         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
699                                   0);
700         if (ret)
701                 return -1;
702         return ret_len;
703 }
704
705
706 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
707 {
708         struct wpa_supplicant *wpa_s = eloop_data;
709         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
710         wpas_wps_ap_pin_disable(wpa_s);
711 }
712
713
714 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
715 {
716         struct hostapd_data *hapd;
717
718         if (wpa_s->ap_iface == NULL)
719                 return;
720         hapd = wpa_s->ap_iface->bss[0];
721         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
722         hapd->ap_pin_failures = 0;
723         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
724         if (timeout > 0)
725                 eloop_register_timeout(timeout, 0,
726                                        wpas_wps_ap_pin_timeout, wpa_s, NULL);
727 }
728
729
730 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
731 {
732         struct hostapd_data *hapd;
733
734         if (wpa_s->ap_iface == NULL)
735                 return;
736         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
737         hapd = wpa_s->ap_iface->bss[0];
738         os_free(hapd->conf->ap_pin);
739         hapd->conf->ap_pin = NULL;
740         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
741 }
742
743
744 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
745 {
746         struct hostapd_data *hapd;
747         unsigned int pin;
748         char pin_txt[9];
749
750         if (wpa_s->ap_iface == NULL)
751                 return NULL;
752         hapd = wpa_s->ap_iface->bss[0];
753         pin = wps_generate_pin();
754         os_snprintf(pin_txt, sizeof(pin_txt), "%u", pin);
755         os_free(hapd->conf->ap_pin);
756         hapd->conf->ap_pin = os_strdup(pin_txt);
757         if (hapd->conf->ap_pin == NULL)
758                 return NULL;
759         wpas_wps_ap_pin_enable(wpa_s, timeout);
760
761         return hapd->conf->ap_pin;
762 }
763
764
765 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
766 {
767         struct hostapd_data *hapd;
768         if (wpa_s->ap_iface == NULL)
769                 return NULL;
770         hapd = wpa_s->ap_iface->bss[0];
771         return hapd->conf->ap_pin;
772 }
773
774
775 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
776                         int timeout)
777 {
778         struct hostapd_data *hapd;
779         char pin_txt[9];
780         int ret;
781
782         if (wpa_s->ap_iface == NULL)
783                 return -1;
784         hapd = wpa_s->ap_iface->bss[0];
785         ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
786         if (ret < 0 || ret >= (int) sizeof(pin_txt))
787                 return -1;
788         os_free(hapd->conf->ap_pin);
789         hapd->conf->ap_pin = os_strdup(pin_txt);
790         if (hapd->conf->ap_pin == NULL)
791                 return -1;
792         wpas_wps_ap_pin_enable(wpa_s, timeout);
793
794         return 0;
795 }
796
797
798 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
799 {
800         struct hostapd_data *hapd;
801
802         if (wpa_s->ap_iface == NULL)
803                 return;
804         hapd = wpa_s->ap_iface->bss[0];
805
806         /*
807          * Registrar failed to prove its knowledge of the AP PIN. Disable AP
808          * PIN if this happens multiple times to slow down brute force attacks.
809          */
810         hapd->ap_pin_failures++;
811         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
812                    hapd->ap_pin_failures);
813         if (hapd->ap_pin_failures < 3)
814                 return;
815
816         wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
817         hapd->ap_pin_failures = 0;
818         os_free(hapd->conf->ap_pin);
819         hapd->conf->ap_pin = NULL;
820 }
821
822 #endif /* CONFIG_WPS */
823
824
825 #ifdef CONFIG_CTRL_IFACE
826
827 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
828                             char *buf, size_t buflen)
829 {
830         if (wpa_s->ap_iface == NULL)
831                 return -1;
832         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
833                                             buf, buflen);
834 }
835
836
837 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
838                       char *buf, size_t buflen)
839 {
840         if (wpa_s->ap_iface == NULL)
841                 return -1;
842         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
843                                       buf, buflen);
844 }
845
846
847 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
848                            char *buf, size_t buflen)
849 {
850         if (wpa_s->ap_iface == NULL)
851                 return -1;
852         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
853                                            buf, buflen);
854 }
855
856
857 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
858                                  size_t buflen, int verbose)
859 {
860         char *pos = buf, *end = buf + buflen;
861         int ret;
862         struct hostapd_bss_config *conf;
863
864         if (wpa_s->ap_iface == NULL)
865                 return -1;
866
867         conf = wpa_s->ap_iface->bss[0]->conf;
868         if (conf->wpa == 0)
869                 return 0;
870
871         ret = os_snprintf(pos, end - pos,
872                           "pairwise_cipher=%s\n"
873                           "group_cipher=%s\n"
874                           "key_mgmt=%s\n",
875                           wpa_cipher_txt(conf->rsn_pairwise),
876                           wpa_cipher_txt(conf->wpa_group),
877                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
878                                            conf->wpa));
879         if (ret < 0 || ret >= end - pos)
880                 return pos - buf;
881         pos += ret;
882         return pos - buf;
883 }
884
885 #endif /* CONFIG_CTRL_IFACE */
886
887
888 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
889 {
890         struct hostapd_iface *iface = wpa_s->ap_iface;
891         struct wpa_ssid *ssid = wpa_s->current_ssid;
892         struct hostapd_data *hapd;
893
894         if (ssid == NULL || wpa_s->ap_iface == NULL)
895                 return -1;
896
897 #ifdef CONFIG_P2P
898         if (ssid->mode == WPAS_MODE_P2P_GO)
899                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
900         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
901                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
902                         P2P_GROUP_FORMATION;
903 #endif /* CONFIG_P2P */
904
905         ieee802_11_set_beacons(iface);
906         hapd = iface->bss[0];
907         hostapd_set_ap_wps_ie(hapd);
908
909         return 0;
910 }
911
912
913 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
914                                       const u8 *addr)
915 {
916         struct hostapd_data *hapd;
917         struct hostapd_bss_config *conf;
918
919         if (!wpa_s->ap_iface)
920                 return -1;
921
922         if (addr)
923                 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
924                            MAC2STR(addr));
925         else
926                 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
927
928         hapd = wpa_s->ap_iface->bss[0];
929         conf = hapd->conf;
930
931         os_free(conf->accept_mac);
932         conf->accept_mac = NULL;
933         conf->num_accept_mac = 0;
934         os_free(conf->deny_mac);
935         conf->deny_mac = NULL;
936         conf->num_deny_mac = 0;
937
938         if (addr == NULL) {
939                 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
940                 return 0;
941         }
942
943         conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
944         conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
945         if (conf->accept_mac == NULL)
946                 return -1;
947         os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
948         conf->num_accept_mac = 1;
949
950         return 0;
951 }