d18e3f1c28ddef08abbd6117aee3955067063d17
[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 {
362 #ifdef CONFIG_P2P
363         struct wpa_supplicant *wpa_s = ctx;
364         return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len);
365 #else /* CONFIG_P2P */
366         return 0;
367 #endif /* CONFIG_P2P */
368 }
369
370
371 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
372                                   const u8 *uuid_e)
373 {
374 #ifdef CONFIG_P2P
375         struct wpa_supplicant *wpa_s = ctx;
376         wpas_p2p_wps_success(wpa_s, mac_addr, 1);
377 #endif /* CONFIG_P2P */
378 }
379
380
381 static void wpas_ap_configured_cb(void *ctx)
382 {
383         struct wpa_supplicant *wpa_s = ctx;
384
385         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
386
387         if (wpa_s->ap_configured_cb)
388                 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
389                                         wpa_s->ap_configured_cb_data);
390 }
391
392
393 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
394                              struct wpa_ssid *ssid)
395 {
396         struct wpa_driver_associate_params params;
397         struct hostapd_iface *hapd_iface;
398         struct hostapd_config *conf;
399         size_t i;
400
401         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
402                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
403                 return -1;
404         }
405
406         wpa_supplicant_ap_deinit(wpa_s);
407
408         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
409                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
410
411         os_memset(&params, 0, sizeof(params));
412         params.ssid = ssid->ssid;
413         params.ssid_len = ssid->ssid_len;
414         switch (ssid->mode) {
415         case WPAS_MODE_INFRA:
416                 params.mode = IEEE80211_MODE_INFRA;
417                 break;
418         case WPAS_MODE_IBSS:
419                 params.mode = IEEE80211_MODE_IBSS;
420                 break;
421         case WPAS_MODE_AP:
422         case WPAS_MODE_P2P_GO:
423         case WPAS_MODE_P2P_GROUP_FORMATION:
424                 params.mode = IEEE80211_MODE_AP;
425                 break;
426         }
427         params.freq = ssid->frequency;
428
429         params.wpa_proto = ssid->proto;
430         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
431                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
432         else
433                 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
434         params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
435
436         if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
437                 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
438         else if (ssid->pairwise_cipher & WPA_CIPHER_TKIP)
439                 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
440         else if (ssid->pairwise_cipher & WPA_CIPHER_NONE)
441                 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
442         else {
443                 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
444                            "cipher.");
445                 return -1;
446         }
447         params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
448         params.group_suite = params.pairwise_suite;
449
450 #ifdef CONFIG_P2P
451         if (ssid->mode == WPAS_MODE_P2P_GO ||
452             ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
453                 params.p2p = 1;
454 #endif /* CONFIG_P2P */
455
456         if (wpa_s->parent->set_ap_uapsd)
457                 params.uapsd = wpa_s->parent->ap_uapsd;
458         else
459                 params.uapsd = -1;
460
461         if (wpa_drv_associate(wpa_s, &params) < 0) {
462                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
463                 return -1;
464         }
465
466         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
467         if (hapd_iface == NULL)
468                 return -1;
469         hapd_iface->owner = wpa_s;
470         hapd_iface->drv_flags = wpa_s->drv_flags;
471         hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
472
473         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
474         if (conf == NULL) {
475                 wpa_supplicant_ap_deinit(wpa_s);
476                 return -1;
477         }
478
479         if (params.uapsd > 0) {
480                 conf->bss->wmm_enabled = 1;
481                 conf->bss->wmm_uapsd = 1;
482         }
483
484         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
485                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
486                 wpa_supplicant_ap_deinit(wpa_s);
487                 return -1;
488         }
489
490 #ifdef CONFIG_P2P
491         if (ssid->mode == WPAS_MODE_P2P_GO)
492                 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
493         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
494                 conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
495                         P2P_GROUP_FORMATION;
496 #endif /* CONFIG_P2P */
497
498         hapd_iface->num_bss = conf->num_bss;
499         hapd_iface->bss = os_zalloc(conf->num_bss *
500                                     sizeof(struct hostapd_data *));
501         if (hapd_iface->bss == NULL) {
502                 wpa_supplicant_ap_deinit(wpa_s);
503                 return -1;
504         }
505
506         for (i = 0; i < conf->num_bss; i++) {
507                 hapd_iface->bss[i] =
508                         hostapd_alloc_bss_data(hapd_iface, conf,
509                                                &conf->bss[i]);
510                 if (hapd_iface->bss[i] == NULL) {
511                         wpa_supplicant_ap_deinit(wpa_s);
512                         return -1;
513                 }
514
515                 hapd_iface->bss[i]->msg_ctx = wpa_s;
516                 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
517                 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
518                 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
519                 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
520                 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
521                 hostapd_register_probereq_cb(hapd_iface->bss[i],
522                                              ap_probe_req_rx, wpa_s);
523                 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
524                 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
525                 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
526                 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
527                 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
528                 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
529 #ifdef CONFIG_P2P
530                 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
531                 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(
532                         wpa_s, ssid->p2p_persistent_group,
533                         ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION);
534 #endif /* CONFIG_P2P */
535                 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
536                 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
537         }
538
539         os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
540         hapd_iface->bss[0]->driver = wpa_s->driver;
541         hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
542
543         wpa_s->current_ssid = ssid;
544         os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
545         wpa_s->assoc_freq = ssid->frequency;
546
547         if (hostapd_setup_interface(wpa_s->ap_iface)) {
548                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
549                 wpa_supplicant_ap_deinit(wpa_s);
550                 return -1;
551         }
552
553         return 0;
554 }
555
556
557 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
558 {
559 #ifdef CONFIG_WPS
560         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
561 #endif /* CONFIG_WPS */
562
563         if (wpa_s->ap_iface == NULL)
564                 return;
565
566         wpa_s->current_ssid = NULL;
567         wpa_s->assoc_freq = 0;
568         wpa_s->reassociated_connection = 0;
569 #ifdef CONFIG_P2P
570         if (wpa_s->ap_iface->bss)
571                 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
572         wpas_p2p_group_deinit(wpa_s);
573 #endif /* CONFIG_P2P */
574         hostapd_interface_deinit(wpa_s->ap_iface);
575         hostapd_interface_free(wpa_s->ap_iface);
576         wpa_s->ap_iface = NULL;
577         wpa_drv_deinit_ap(wpa_s);
578 }
579
580
581 void ap_tx_status(void *ctx, const u8 *addr,
582                   const u8 *buf, size_t len, int ack)
583 {
584 #ifdef NEED_AP_MLME
585         struct wpa_supplicant *wpa_s = ctx;
586         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
587 #endif /* NEED_AP_MLME */
588 }
589
590
591 void ap_eapol_tx_status(void *ctx, const u8 *dst,
592                         const u8 *data, size_t len, int ack)
593 {
594 #ifdef NEED_AP_MLME
595         struct wpa_supplicant *wpa_s = ctx;
596         hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
597 #endif /* NEED_AP_MLME */
598 }
599
600
601 void ap_client_poll_ok(void *ctx, const u8 *addr)
602 {
603 #ifdef NEED_AP_MLME
604         struct wpa_supplicant *wpa_s = ctx;
605         if (wpa_s->ap_iface)
606                 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
607 #endif /* NEED_AP_MLME */
608 }
609
610
611 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
612 {
613 #ifdef NEED_AP_MLME
614         struct wpa_supplicant *wpa_s = ctx;
615         ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
616 #endif /* NEED_AP_MLME */
617 }
618
619
620 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
621 {
622 #ifdef NEED_AP_MLME
623         struct wpa_supplicant *wpa_s = ctx;
624         struct hostapd_frame_info fi;
625         os_memset(&fi, 0, sizeof(fi));
626         fi.datarate = rx_mgmt->datarate;
627         fi.ssi_signal = rx_mgmt->ssi_signal;
628         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
629                         rx_mgmt->frame_len, &fi);
630 #endif /* NEED_AP_MLME */
631 }
632
633
634 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
635 {
636 #ifdef NEED_AP_MLME
637         struct wpa_supplicant *wpa_s = ctx;
638         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
639 #endif /* NEED_AP_MLME */
640 }
641
642
643 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
644                                 const u8 *src_addr, const u8 *buf, size_t len)
645 {
646         ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
647 }
648
649
650 #ifdef CONFIG_WPS
651
652 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
653                               const u8 *p2p_dev_addr)
654 {
655         if (!wpa_s->ap_iface)
656                 return -1;
657         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
658                                          p2p_dev_addr);
659 }
660
661
662 static int wpa_supplicant_ap_wps_sta_cancel(struct hostapd_data *hapd,
663                                             struct sta_info *sta, void *ctx)
664 {
665         if (sta && (sta->flags & WLAN_STA_WPS)) {
666                 ap_sta_deauthenticate(hapd, sta,
667                                       WLAN_REASON_PREV_AUTH_NOT_VALID);
668                 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
669                            __func__, MAC2STR(sta->addr));
670                 return 1;
671         }
672
673         return 0;
674 }
675
676
677 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
678 {
679         struct wps_registrar *reg;
680         int reg_sel = 0, wps_sta = 0;
681
682         if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
683                 return -1;
684
685         reg = wpa_s->ap_iface->bss[0]->wps->registrar;
686         reg_sel = wps_registrar_wps_cancel(reg);
687         wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
688                                   wpa_supplicant_ap_wps_sta_cancel, NULL);
689
690         if (!reg_sel && !wps_sta) {
691                 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
692                            "time");
693                 return -1;
694         }
695
696         /*
697          * There are 2 cases to return wps cancel as success:
698          * 1. When wps cancel was initiated but no connection has been
699          *    established with client yet.
700          * 2. Client is in the middle of exchanging WPS messages.
701          */
702
703         return 0;
704 }
705
706
707 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
708                               const char *pin, char *buf, size_t buflen)
709 {
710         int ret, ret_len = 0;
711
712         if (!wpa_s->ap_iface)
713                 return -1;
714
715         if (pin == NULL) {
716                 unsigned int rpin = wps_generate_pin();
717                 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
718                 pin = buf;
719         } else
720                 ret_len = os_snprintf(buf, buflen, "%s", pin);
721
722         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
723                                   0);
724         if (ret)
725                 return -1;
726         return ret_len;
727 }
728
729
730 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
731 {
732         struct wpa_supplicant *wpa_s = eloop_data;
733         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
734         wpas_wps_ap_pin_disable(wpa_s);
735 }
736
737
738 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
739 {
740         struct hostapd_data *hapd;
741
742         if (wpa_s->ap_iface == NULL)
743                 return;
744         hapd = wpa_s->ap_iface->bss[0];
745         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
746         hapd->ap_pin_failures = 0;
747         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
748         if (timeout > 0)
749                 eloop_register_timeout(timeout, 0,
750                                        wpas_wps_ap_pin_timeout, wpa_s, NULL);
751 }
752
753
754 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
755 {
756         struct hostapd_data *hapd;
757
758         if (wpa_s->ap_iface == NULL)
759                 return;
760         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
761         hapd = wpa_s->ap_iface->bss[0];
762         os_free(hapd->conf->ap_pin);
763         hapd->conf->ap_pin = NULL;
764         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
765 }
766
767
768 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
769 {
770         struct hostapd_data *hapd;
771         unsigned int pin;
772         char pin_txt[9];
773
774         if (wpa_s->ap_iface == NULL)
775                 return NULL;
776         hapd = wpa_s->ap_iface->bss[0];
777         pin = wps_generate_pin();
778         os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
779         os_free(hapd->conf->ap_pin);
780         hapd->conf->ap_pin = os_strdup(pin_txt);
781         if (hapd->conf->ap_pin == NULL)
782                 return NULL;
783         wpas_wps_ap_pin_enable(wpa_s, timeout);
784
785         return hapd->conf->ap_pin;
786 }
787
788
789 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
790 {
791         struct hostapd_data *hapd;
792         if (wpa_s->ap_iface == NULL)
793                 return NULL;
794         hapd = wpa_s->ap_iface->bss[0];
795         return hapd->conf->ap_pin;
796 }
797
798
799 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
800                         int timeout)
801 {
802         struct hostapd_data *hapd;
803         char pin_txt[9];
804         int ret;
805
806         if (wpa_s->ap_iface == NULL)
807                 return -1;
808         hapd = wpa_s->ap_iface->bss[0];
809         ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
810         if (ret < 0 || ret >= (int) sizeof(pin_txt))
811                 return -1;
812         os_free(hapd->conf->ap_pin);
813         hapd->conf->ap_pin = os_strdup(pin_txt);
814         if (hapd->conf->ap_pin == NULL)
815                 return -1;
816         wpas_wps_ap_pin_enable(wpa_s, timeout);
817
818         return 0;
819 }
820
821
822 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
823 {
824         struct hostapd_data *hapd;
825
826         if (wpa_s->ap_iface == NULL)
827                 return;
828         hapd = wpa_s->ap_iface->bss[0];
829
830         /*
831          * Registrar failed to prove its knowledge of the AP PIN. Disable AP
832          * PIN if this happens multiple times to slow down brute force attacks.
833          */
834         hapd->ap_pin_failures++;
835         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
836                    hapd->ap_pin_failures);
837         if (hapd->ap_pin_failures < 3)
838                 return;
839
840         wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
841         hapd->ap_pin_failures = 0;
842         os_free(hapd->conf->ap_pin);
843         hapd->conf->ap_pin = NULL;
844 }
845
846 #endif /* CONFIG_WPS */
847
848
849 #ifdef CONFIG_CTRL_IFACE
850
851 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
852                             char *buf, size_t buflen)
853 {
854         if (wpa_s->ap_iface == NULL)
855                 return -1;
856         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
857                                             buf, buflen);
858 }
859
860
861 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
862                       char *buf, size_t buflen)
863 {
864         if (wpa_s->ap_iface == NULL)
865                 return -1;
866         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
867                                       buf, buflen);
868 }
869
870
871 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
872                            char *buf, size_t buflen)
873 {
874         if (wpa_s->ap_iface == NULL)
875                 return -1;
876         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
877                                            buf, buflen);
878 }
879
880
881 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
882                                    const char *txtaddr)
883 {
884         if (wpa_s->ap_iface == NULL)
885                 return -1;
886         return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
887                                                txtaddr);
888 }
889
890
891 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
892                                      const char *txtaddr)
893 {
894         if (wpa_s->ap_iface == NULL)
895                 return -1;
896         return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
897                                                  txtaddr);
898 }
899
900
901 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
902                                  size_t buflen, int verbose)
903 {
904         char *pos = buf, *end = buf + buflen;
905         int ret;
906         struct hostapd_bss_config *conf;
907
908         if (wpa_s->ap_iface == NULL)
909                 return -1;
910
911         conf = wpa_s->ap_iface->bss[0]->conf;
912         if (conf->wpa == 0)
913                 return 0;
914
915         ret = os_snprintf(pos, end - pos,
916                           "pairwise_cipher=%s\n"
917                           "group_cipher=%s\n"
918                           "key_mgmt=%s\n",
919                           wpa_cipher_txt(conf->rsn_pairwise),
920                           wpa_cipher_txt(conf->wpa_group),
921                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
922                                            conf->wpa));
923         if (ret < 0 || ret >= end - pos)
924                 return pos - buf;
925         pos += ret;
926         return pos - buf;
927 }
928
929 #endif /* CONFIG_CTRL_IFACE */
930
931
932 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
933 {
934         struct hostapd_iface *iface = wpa_s->ap_iface;
935         struct wpa_ssid *ssid = wpa_s->current_ssid;
936         struct hostapd_data *hapd;
937
938         if (ssid == NULL || wpa_s->ap_iface == NULL ||
939             ssid->mode == WPAS_MODE_INFRA ||
940             ssid->mode == WPAS_MODE_IBSS)
941                 return -1;
942
943 #ifdef CONFIG_P2P
944         if (ssid->mode == WPAS_MODE_P2P_GO)
945                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
946         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
947                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
948                         P2P_GROUP_FORMATION;
949 #endif /* CONFIG_P2P */
950
951         hapd = iface->bss[0];
952         if (hapd->drv_priv == NULL)
953                 return -1;
954         ieee802_11_set_beacons(iface);
955         hostapd_set_ap_wps_ie(hapd);
956
957         return 0;
958 }
959
960
961 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
962                                       const u8 *addr)
963 {
964         struct hostapd_data *hapd;
965         struct hostapd_bss_config *conf;
966
967         if (!wpa_s->ap_iface)
968                 return -1;
969
970         if (addr)
971                 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
972                            MAC2STR(addr));
973         else
974                 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
975
976         hapd = wpa_s->ap_iface->bss[0];
977         conf = hapd->conf;
978
979         os_free(conf->accept_mac);
980         conf->accept_mac = NULL;
981         conf->num_accept_mac = 0;
982         os_free(conf->deny_mac);
983         conf->deny_mac = NULL;
984         conf->num_deny_mac = 0;
985
986         if (addr == NULL) {
987                 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
988                 return 0;
989         }
990
991         conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
992         conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
993         if (conf->accept_mac == NULL)
994                 return -1;
995         os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
996         conf->num_accept_mac = 1;
997
998         return 0;
999 }