Merge branch 'moonshot' of ssh://moonshot.suchdamage.org:822/srv/git/libeap into...
[libeap.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 "common/ieee802_11_defs.h"
20 #include "common/wpa_ctrl.h"
21 #include "ap/hostapd.h"
22 #include "ap/ap_config.h"
23 #ifdef NEED_AP_MLME
24 #include "ap/ieee802_11.h"
25 #endif /* NEED_AP_MLME */
26 #include "ap/beacon.h"
27 #include "ap/ieee802_1x.h"
28 #include "ap/wps_hostapd.h"
29 #include "ap/ctrl_iface_ap.h"
30 #include "eap_common/eap_defs.h"
31 #include "eap_server/eap_methods.h"
32 #include "eap_common/eap_wsc_common.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
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         int pairwise;
50
51         conf->driver = wpa_s->driver;
52
53         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
54
55         if (ssid->frequency == 0) {
56                 /* default channel 11 */
57                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
58                 conf->channel = 11;
59         } else if (ssid->frequency >= 2412 && ssid->frequency <= 2472) {
60                 conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
61                 conf->channel = (ssid->frequency - 2407) / 5;
62         } else if ((ssid->frequency >= 5180 && ssid->frequency <= 5240) ||
63                    (ssid->frequency >= 5745 && ssid->frequency <= 5825)) {
64                 conf->hw_mode = HOSTAPD_MODE_IEEE80211A;
65                 conf->channel = (ssid->frequency - 5000) / 5;
66         } else {
67                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
68                            ssid->frequency);
69                 return -1;
70         }
71
72         /* TODO: enable HT if driver supports it;
73          * drop to 11b if driver does not support 11g */
74
75 #ifdef CONFIG_P2P
76         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
77                 /* Remove 802.11b rates from supported and basic rate sets */
78                 int *list = os_malloc(4 * sizeof(int));
79                 if (list) {
80                         list[0] = 60;
81                         list[1] = 120;
82                         list[2] = 240;
83                         list[3] = -1;
84                 }
85                 conf->basic_rates = list;
86
87                 list = os_malloc(9 * sizeof(int));
88                 if (list) {
89                         list[0] = 60;
90                         list[1] = 90;
91                         list[2] = 120;
92                         list[3] = 180;
93                         list[4] = 240;
94                         list[5] = 360;
95                         list[6] = 480;
96                         list[7] = 540;
97                         list[8] = -1;
98                 }
99                 conf->supported_rates = list;
100         }
101 #endif /* CONFIG_P2P */
102
103         if (ssid->ssid_len == 0) {
104                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
105                 return -1;
106         }
107         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
108         bss->ssid.ssid[ssid->ssid_len] = '\0';
109         bss->ssid.ssid_len = ssid->ssid_len;
110         bss->ssid.ssid_set = 1;
111
112         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
113                 bss->wpa = ssid->proto;
114         bss->wpa_key_mgmt = ssid->key_mgmt;
115         bss->wpa_pairwise = ssid->pairwise_cipher;
116         if (ssid->passphrase) {
117                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
118         } else if (ssid->psk_set) {
119                 os_free(bss->ssid.wpa_psk);
120                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
121                 if (bss->ssid.wpa_psk == NULL)
122                         return -1;
123                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
124                 bss->ssid.wpa_psk->group = 1;
125         }
126
127         /* Select group cipher based on the enabled pairwise cipher suites */
128         pairwise = 0;
129         if (bss->wpa & 1)
130                 pairwise |= bss->wpa_pairwise;
131         if (bss->wpa & 2) {
132                 if (bss->rsn_pairwise == 0)
133                         bss->rsn_pairwise = bss->wpa_pairwise;
134                 pairwise |= bss->rsn_pairwise;
135         }
136         if (pairwise & WPA_CIPHER_TKIP)
137                 bss->wpa_group = WPA_CIPHER_TKIP;
138         else
139                 bss->wpa_group = WPA_CIPHER_CCMP;
140
141         if (bss->wpa && bss->ieee802_1x)
142                 bss->ssid.security_policy = SECURITY_WPA;
143         else if (bss->wpa)
144                 bss->ssid.security_policy = SECURITY_WPA_PSK;
145         else if (bss->ieee802_1x) {
146                 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
147                 bss->ssid.wep.default_len = bss->default_wep_key_len;
148         } else if (bss->ssid.wep.keys_set)
149                 bss->ssid.security_policy = SECURITY_STATIC_WEP;
150         else
151                 bss->ssid.security_policy = SECURITY_PLAINTEXT;
152
153 #ifdef CONFIG_WPS
154         /*
155          * Enable WPS by default, but require user interaction to actually use
156          * it. Only the internal Registrar is supported.
157          */
158         bss->eap_server = 1;
159         bss->wps_state = 2;
160         bss->ap_setup_locked = 1;
161         if (wpa_s->conf->config_methods)
162                 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
163         if (wpa_s->conf->device_type)
164                 bss->device_type = os_strdup(wpa_s->conf->device_type);
165         if (wpa_s->conf->device_name) {
166                 bss->device_name = os_strdup(wpa_s->conf->device_name);
167                 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
168         }
169         if (wpa_s->conf->manufacturer)
170                 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
171         if (wpa_s->conf->model_name)
172                 bss->model_name = os_strdup(wpa_s->conf->model_name);
173         if (wpa_s->conf->model_number)
174                 bss->model_number = os_strdup(wpa_s->conf->model_number);
175         if (wpa_s->conf->serial_number)
176                 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
177         os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
178         os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
179 #endif /* CONFIG_WPS */
180
181         return 0;
182 }
183
184
185 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
186 {
187 #ifdef CONFIG_P2P
188         struct wpa_supplicant *wpa_s = ctx;
189         const struct ieee80211_mgmt *mgmt;
190         size_t hdr_len;
191
192         mgmt = (const struct ieee80211_mgmt *) buf;
193         hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
194         if (hdr_len > len)
195                 return;
196         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
197                            mgmt->u.action.category,
198                            &mgmt->u.action.u.vs_public_action.action,
199                            len - hdr_len, freq);
200 #endif /* CONFIG_P2P */
201 }
202
203
204 static void ap_wps_event_cb(void *ctx, enum wps_event event,
205                             union wps_event_data *data)
206 {
207         struct wpa_supplicant *wpa_s = ctx;
208
209         if (event == WPS_EV_FAIL && wpa_s->parent && wpa_s->parent != wpa_s) {
210                 struct wps_event_fail *fail = &data->fail;
211
212                 /*
213                  * src/ap/wps_hostapd.c has already sent this on the main
214                  * interface, so only send on the parent interface here if
215                  * needed.
216                  */
217                 wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
218                         "msg=%d config_error=%d",
219                         fail->msg, fail->config_error);
220         }
221 }
222
223
224 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
225 {
226 #ifdef CONFIG_P2P
227         struct wpa_supplicant *wpa_s = ctx;
228         const struct ieee80211_mgmt *mgmt;
229         size_t hdr_len;
230
231         mgmt = (const struct ieee80211_mgmt *) buf;
232         hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
233         if (hdr_len > len)
234                 return -1;
235         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
236                            mgmt->u.action.category,
237                            &mgmt->u.action.u.vs_public_action.action,
238                            len - hdr_len, freq);
239 #endif /* CONFIG_P2P */
240         return 0;
241 }
242
243
244 static int ap_probe_req_rx(void *ctx, const u8 *addr, const u8 *ie,
245                            size_t ie_len)
246 {
247 #ifdef CONFIG_P2P
248         struct wpa_supplicant *wpa_s = ctx;
249         return wpas_p2p_probe_req_rx(wpa_s, addr, ie, ie_len);
250 #else /* CONFIG_P2P */
251         return 0;
252 #endif /* CONFIG_P2P */
253 }
254
255
256 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
257                                   const u8 *uuid_e)
258 {
259 #ifdef CONFIG_P2P
260         struct wpa_supplicant *wpa_s = ctx;
261         wpas_p2p_wps_success(wpa_s, mac_addr, 1);
262 #endif /* CONFIG_P2P */
263 }
264
265
266 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
267                              struct wpa_ssid *ssid)
268 {
269         struct wpa_driver_associate_params params;
270         struct hostapd_iface *hapd_iface;
271         struct hostapd_config *conf;
272         size_t i;
273
274         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
275                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
276                 return -1;
277         }
278
279         wpa_supplicant_ap_deinit(wpa_s);
280
281         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
282                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
283
284         os_memset(&params, 0, sizeof(params));
285         params.ssid = ssid->ssid;
286         params.ssid_len = ssid->ssid_len;
287         switch (ssid->mode) {
288         case WPAS_MODE_INFRA:
289                 params.mode = IEEE80211_MODE_INFRA;
290                 break;
291         case WPAS_MODE_IBSS:
292                 params.mode = IEEE80211_MODE_IBSS;
293                 break;
294         case WPAS_MODE_AP:
295         case WPAS_MODE_P2P_GO:
296         case WPAS_MODE_P2P_GROUP_FORMATION:
297                 params.mode = IEEE80211_MODE_AP;
298                 break;
299         }
300         params.freq = ssid->frequency;
301
302         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
303                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
304         else
305                 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
306         params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
307
308         if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
309                 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
310         else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
311                 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
312         else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
313                 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
314         else {
315                 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
316                            "cipher.");
317                 return -1;
318         }
319         params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
320         params.group_suite = params.pairwise_suite;
321
322 #ifdef CONFIG_P2P
323         if (ssid->mode == WPAS_MODE_P2P_GO ||
324             ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
325                 params.p2p = 1;
326         wpa_drv_set_intra_bss(wpa_s, wpa_s->conf->p2p_intra_bss);
327 #endif /* CONFIG_P2P */
328
329         if (wpa_s->parent->set_ap_uapsd)
330                 params.uapsd = wpa_s->parent->ap_uapsd;
331         else
332                 params.uapsd = -1;
333
334         if (wpa_drv_associate(wpa_s, &params) < 0) {
335                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
336                 return -1;
337         }
338
339         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
340         if (hapd_iface == NULL)
341                 return -1;
342         hapd_iface->owner = wpa_s;
343
344         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
345         if (conf == NULL) {
346                 wpa_supplicant_ap_deinit(wpa_s);
347                 return -1;
348         }
349
350         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
351                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
352                 wpa_supplicant_ap_deinit(wpa_s);
353                 return -1;
354         }
355
356 #ifdef CONFIG_P2P
357         if (ssid->mode == WPAS_MODE_P2P_GO)
358                 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
359         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
360                 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
361                         P2P_GROUP_FORMATION;
362 #endif /* CONFIG_P2P */
363
364         hapd_iface->num_bss = conf->num_bss;
365         hapd_iface->bss = os_zalloc(conf->num_bss *
366                                     sizeof(struct hostapd_data *));
367         if (hapd_iface->bss == NULL) {
368                 wpa_supplicant_ap_deinit(wpa_s);
369                 return -1;
370         }
371
372         for (i = 0; i < conf->num_bss; i++) {
373                 hapd_iface->bss[i] =
374                         hostapd_alloc_bss_data(hapd_iface, conf,
375                                                &conf->bss[i]);
376                 if (hapd_iface->bss[i] == NULL) {
377                         wpa_supplicant_ap_deinit(wpa_s);
378                         return -1;
379                 }
380
381                 hapd_iface->bss[i]->msg_ctx = wpa_s;
382                 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
383                 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
384                 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
385                 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
386                 hostapd_register_probereq_cb(hapd_iface->bss[i],
387                                              ap_probe_req_rx, wpa_s);
388                 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
389                 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
390                 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
391                 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
392 #ifdef CONFIG_P2P
393                 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
394                 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
395                         wpa_s, ssid->p2p_persistent_group,
396                         ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
397 #endif /* CONFIG_P2P */
398         }
399
400         os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
401         hapd_iface->bss[0]->driver = wpa_s->driver;
402         hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
403
404         if (hostapd_setup_interface(wpa_s->ap_iface)) {
405                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
406                 wpa_supplicant_ap_deinit(wpa_s);
407                 return -1;
408         }
409
410         wpa_s->current_ssid = ssid;
411         os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
412         wpa_s->assoc_freq = ssid->frequency;
413         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
414
415         if (wpa_s->ap_configured_cb)
416                 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
417                                         wpa_s->ap_configured_cb_data);
418
419         return 0;
420 }
421
422
423 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
424 {
425         if (wpa_s->ap_iface == NULL)
426                 return;
427
428         wpa_s->current_ssid = NULL;
429 #ifdef CONFIG_P2P
430         if (wpa_s->ap_iface->bss)
431                 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
432         wpas_p2p_group_deinit(wpa_s);
433 #endif /* CONFIG_P2P */
434         hostapd_interface_deinit(wpa_s->ap_iface);
435         hostapd_interface_free(wpa_s->ap_iface);
436         wpa_s->ap_iface = NULL;
437         wpa_drv_deinit_ap(wpa_s);
438 }
439
440
441 void ap_tx_status(void *ctx, const u8 *addr,
442                   const u8 *buf, size_t len, int ack)
443 {
444 #ifdef NEED_AP_MLME
445         struct wpa_supplicant *wpa_s = ctx;
446         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
447 #endif /* NEED_AP_MLME */
448 }
449
450
451 void ap_rx_from_unknown_sta(void *ctx, const u8 *frame, size_t len)
452 {
453 #ifdef NEED_AP_MLME
454         struct wpa_supplicant *wpa_s = ctx;
455         const struct ieee80211_hdr *hdr =
456                 (const struct ieee80211_hdr *) frame;
457         u16 fc = le_to_host16(hdr->frame_control);
458         ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], hdr->addr2,
459                                    (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
460                                    (WLAN_FC_TODS | WLAN_FC_FROMDS));
461 #endif /* NEED_AP_MLME */
462 }
463
464
465 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
466 {
467 #ifdef NEED_AP_MLME
468         struct wpa_supplicant *wpa_s = ctx;
469         struct hostapd_frame_info fi;
470         os_memset(&fi, 0, sizeof(fi));
471         fi.datarate = rx_mgmt->datarate;
472         fi.ssi_signal = rx_mgmt->ssi_signal;
473         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
474                         rx_mgmt->frame_len, &fi);
475 #endif /* NEED_AP_MLME */
476 }
477
478
479 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
480 {
481 #ifdef NEED_AP_MLME
482         struct wpa_supplicant *wpa_s = ctx;
483         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
484 #endif /* NEED_AP_MLME */
485 }
486
487
488 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
489                                 const u8 *src_addr, const u8 *buf, size_t len)
490 {
491         ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
492 }
493
494
495 #ifdef CONFIG_WPS
496
497 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
498 {
499         if (!wpa_s->ap_iface)
500                 return -1;
501         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0]);
502 }
503
504
505 static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
506                                             struct sta_info *sta, void *ctx)
507 {
508         if (sta && (sta->flags & WLAN_STA_WPS)) {
509                 ap_sta_deauthenticate(hapd, sta,
510                                       WLAN_REASON_PREV_AUTH_NOT_VALID);
511                 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
512                            __func__, MAC2STR(sta->addr));
513                 return 1;
514         }
515
516         return 0;
517 }
518
519
520 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
521 {
522         struct wps_registrar *reg;
523         int reg_sel = 0, wps_sta = 0;
524
525         if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
526                 return -1;
527
528         reg = wpa_s->ap_iface->bss[0]->wps->registrar;
529         reg_sel = wps_registrar_wps_cancel(reg);
530         wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
531                                   wpa_supplicant_ap_wps_sta_cancel, NULL);
532
533         if (!reg_sel && !wps_sta) {
534                 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
535                            "time");
536                 return -1;
537         }
538
539         /*
540          * There are 2 cases to return wps cancel as success:
541          * 1. When wps cancel was initiated but no connection has been
542          *    established with client yet.
543          * 2. Client is in the middle of exchanging WPS messages.
544          */
545
546         return 0;
547 }
548
549
550 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
551                               const char *pin, char *buf, size_t buflen)
552 {
553         int ret, ret_len = 0;
554
555         if (!wpa_s->ap_iface)
556                 return -1;
557
558         if (pin == NULL) {
559                 unsigned int rpin = wps_generate_pin();
560                 ret_len = os_snprintf(buf, buflen, "%d", rpin);
561                 pin = buf;
562         } else
563                 ret_len = os_snprintf(buf, buflen, "%s", pin);
564
565         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
566                                   0);
567         if (ret)
568                 return -1;
569         return ret_len;
570 }
571
572 #endif /* CONFIG_WPS */
573
574
575 #ifdef CONFIG_CTRL_IFACE
576
577 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
578                             char *buf, size_t buflen)
579 {
580         if (wpa_s->ap_iface == NULL)
581                 return -1;
582         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
583                                             buf, buflen);
584 }
585
586
587 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
588                       char *buf, size_t buflen)
589 {
590         if (wpa_s->ap_iface == NULL)
591                 return -1;
592         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
593                                       buf, buflen);
594 }
595
596
597 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
598                            char *buf, size_t buflen)
599 {
600         if (wpa_s->ap_iface == NULL)
601                 return -1;
602         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
603                                            buf, buflen);
604 }
605
606
607 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
608                                  size_t buflen, int verbose)
609 {
610         char *pos = buf, *end = buf + buflen;
611         int ret;
612         struct hostapd_bss_config *conf;
613
614         if (wpa_s->ap_iface == NULL)
615                 return -1;
616
617         conf = wpa_s->ap_iface->bss[0]->conf;
618         if (conf->wpa == 0)
619                 return 0;
620
621         ret = os_snprintf(pos, end - pos,
622                           "pairwise_cipher=%s\n"
623                           "group_cipher=%s\n"
624                           "key_mgmt=%s\n",
625                           wpa_cipher_txt(conf->rsn_pairwise),
626                           wpa_cipher_txt(conf->wpa_group),
627                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
628                                            conf->wpa));
629         if (ret < 0 || ret >= end - pos)
630                 return pos - buf;
631         pos += ret;
632         return pos - buf;
633 }
634
635 #endif /* CONFIG_CTRL_IFACE */
636
637
638 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
639 {
640         struct hostapd_iface *iface = wpa_s->ap_iface;
641         struct wpa_ssid *ssid = wpa_s->current_ssid;
642         struct hostapd_data *hapd;
643
644         if (ssid == NULL || wpa_s->ap_iface == NULL)
645                 return -1;
646
647 #ifdef CONFIG_P2P
648         if (ssid->mode == WPAS_MODE_P2P_GO)
649                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
650         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
651                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
652                         P2P_GROUP_FORMATION;
653 #endif /* CONFIG_P2P */
654
655         ieee802_11_set_beacons(iface);
656         hapd = iface->bss[0];
657         hapd->drv.set_ap_wps_ie(hapd);
658
659         return 0;
660 }
661
662
663 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
664                                       const u8 *addr)
665 {
666         struct hostapd_data *hapd;
667         struct hostapd_bss_config *conf;
668
669         if (!wpa_s->ap_iface)
670                 return -1;
671
672         if (addr)
673                 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
674                            MAC2STR(addr));
675         else
676                 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
677
678         hapd = wpa_s->ap_iface->bss[0];
679         conf = hapd->conf;
680
681         os_free(conf->accept_mac);
682         conf->accept_mac = NULL;
683         conf->num_accept_mac = 0;
684         os_free(conf->deny_mac);
685         conf->deny_mac = NULL;
686         conf->num_deny_mac = 0;
687
688         if (addr == NULL) {
689                 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
690                 return 0;
691         }
692
693         conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
694         conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
695         if (conf->accept_mac == NULL)
696                 return -1;
697         os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
698         conf->num_accept_mac = 1;
699
700         return 0;
701 }