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