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