Add wps_cancel for hostapd_cli
[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 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
663 {
664         struct wps_registrar *reg;
665         int reg_sel = 0, wps_sta = 0;
666
667         if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
668                 return -1;
669
670         reg = wpa_s->ap_iface->bss[0]->wps->registrar;
671         reg_sel = wps_registrar_wps_cancel(reg);
672         wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
673                                   ap_sta_wps_cancel, NULL);
674
675         if (!reg_sel && !wps_sta) {
676                 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
677                            "time");
678                 return -1;
679         }
680
681         /*
682          * There are 2 cases to return wps cancel as success:
683          * 1. When wps cancel was initiated but no connection has been
684          *    established with client yet.
685          * 2. Client is in the middle of exchanging WPS messages.
686          */
687
688         return 0;
689 }
690
691
692 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
693                               const char *pin, char *buf, size_t buflen)
694 {
695         int ret, ret_len = 0;
696
697         if (!wpa_s->ap_iface)
698                 return -1;
699
700         if (pin == NULL) {
701                 unsigned int rpin = wps_generate_pin();
702                 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
703                 pin = buf;
704         } else
705                 ret_len = os_snprintf(buf, buflen, "%s", pin);
706
707         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
708                                   0);
709         if (ret)
710                 return -1;
711         return ret_len;
712 }
713
714
715 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
716 {
717         struct wpa_supplicant *wpa_s = eloop_data;
718         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
719         wpas_wps_ap_pin_disable(wpa_s);
720 }
721
722
723 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
724 {
725         struct hostapd_data *hapd;
726
727         if (wpa_s->ap_iface == NULL)
728                 return;
729         hapd = wpa_s->ap_iface->bss[0];
730         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
731         hapd->ap_pin_failures = 0;
732         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
733         if (timeout > 0)
734                 eloop_register_timeout(timeout, 0,
735                                        wpas_wps_ap_pin_timeout, wpa_s, NULL);
736 }
737
738
739 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
740 {
741         struct hostapd_data *hapd;
742
743         if (wpa_s->ap_iface == NULL)
744                 return;
745         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
746         hapd = wpa_s->ap_iface->bss[0];
747         os_free(hapd->conf->ap_pin);
748         hapd->conf->ap_pin = NULL;
749         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
750 }
751
752
753 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
754 {
755         struct hostapd_data *hapd;
756         unsigned int pin;
757         char pin_txt[9];
758
759         if (wpa_s->ap_iface == NULL)
760                 return NULL;
761         hapd = wpa_s->ap_iface->bss[0];
762         pin = wps_generate_pin();
763         os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
764         os_free(hapd->conf->ap_pin);
765         hapd->conf->ap_pin = os_strdup(pin_txt);
766         if (hapd->conf->ap_pin == NULL)
767                 return NULL;
768         wpas_wps_ap_pin_enable(wpa_s, timeout);
769
770         return hapd->conf->ap_pin;
771 }
772
773
774 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
775 {
776         struct hostapd_data *hapd;
777         if (wpa_s->ap_iface == NULL)
778                 return NULL;
779         hapd = wpa_s->ap_iface->bss[0];
780         return hapd->conf->ap_pin;
781 }
782
783
784 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
785                         int timeout)
786 {
787         struct hostapd_data *hapd;
788         char pin_txt[9];
789         int ret;
790
791         if (wpa_s->ap_iface == NULL)
792                 return -1;
793         hapd = wpa_s->ap_iface->bss[0];
794         ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
795         if (ret < 0 || ret >= (int) sizeof(pin_txt))
796                 return -1;
797         os_free(hapd->conf->ap_pin);
798         hapd->conf->ap_pin = os_strdup(pin_txt);
799         if (hapd->conf->ap_pin == NULL)
800                 return -1;
801         wpas_wps_ap_pin_enable(wpa_s, timeout);
802
803         return 0;
804 }
805
806
807 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
808 {
809         struct hostapd_data *hapd;
810
811         if (wpa_s->ap_iface == NULL)
812                 return;
813         hapd = wpa_s->ap_iface->bss[0];
814
815         /*
816          * Registrar failed to prove its knowledge of the AP PIN. Disable AP
817          * PIN if this happens multiple times to slow down brute force attacks.
818          */
819         hapd->ap_pin_failures++;
820         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
821                    hapd->ap_pin_failures);
822         if (hapd->ap_pin_failures < 3)
823                 return;
824
825         wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
826         hapd->ap_pin_failures = 0;
827         os_free(hapd->conf->ap_pin);
828         hapd->conf->ap_pin = NULL;
829 }
830
831 #endif /* CONFIG_WPS */
832
833
834 #ifdef CONFIG_CTRL_IFACE
835
836 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
837                             char *buf, size_t buflen)
838 {
839         if (wpa_s->ap_iface == NULL)
840                 return -1;
841         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
842                                             buf, buflen);
843 }
844
845
846 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
847                       char *buf, size_t buflen)
848 {
849         if (wpa_s->ap_iface == NULL)
850                 return -1;
851         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
852                                       buf, buflen);
853 }
854
855
856 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
857                            char *buf, size_t buflen)
858 {
859         if (wpa_s->ap_iface == NULL)
860                 return -1;
861         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
862                                            buf, buflen);
863 }
864
865
866 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
867                                    const char *txtaddr)
868 {
869         if (wpa_s->ap_iface == NULL)
870                 return -1;
871         return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
872                                                txtaddr);
873 }
874
875
876 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
877                                      const char *txtaddr)
878 {
879         if (wpa_s->ap_iface == NULL)
880                 return -1;
881         return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
882                                                  txtaddr);
883 }
884
885
886 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
887                                  size_t buflen, int verbose)
888 {
889         char *pos = buf, *end = buf + buflen;
890         int ret;
891         struct hostapd_bss_config *conf;
892
893         if (wpa_s->ap_iface == NULL)
894                 return -1;
895
896         conf = wpa_s->ap_iface->bss[0]->conf;
897         if (conf->wpa == 0)
898                 return 0;
899
900         ret = os_snprintf(pos, end - pos,
901                           "pairwise_cipher=%s\n"
902                           "group_cipher=%s\n"
903                           "key_mgmt=%s\n",
904                           wpa_cipher_txt(conf->rsn_pairwise),
905                           wpa_cipher_txt(conf->wpa_group),
906                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
907                                            conf->wpa));
908         if (ret < 0 || ret >= end - pos)
909                 return pos - buf;
910         pos += ret;
911         return pos - buf;
912 }
913
914 #endif /* CONFIG_CTRL_IFACE */
915
916
917 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
918 {
919         struct hostapd_iface *iface = wpa_s->ap_iface;
920         struct wpa_ssid *ssid = wpa_s->current_ssid;
921         struct hostapd_data *hapd;
922
923         if (ssid == NULL || wpa_s->ap_iface == NULL ||
924             ssid->mode == WPAS_MODE_INFRA ||
925             ssid->mode == WPAS_MODE_IBSS)
926                 return -1;
927
928 #ifdef CONFIG_P2P
929         if (ssid->mode == WPAS_MODE_P2P_GO)
930                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER;
931         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
932                 iface->conf->bss[0].p2p = P2P_ENABLED | P2P_GROUP_OWNER |
933                         P2P_GROUP_FORMATION;
934 #endif /* CONFIG_P2P */
935
936         hapd = iface->bss[0];
937         if (hapd->drv_priv == NULL)
938                 return -1;
939         ieee802_11_set_beacons(iface);
940         hostapd_set_ap_wps_ie(hapd);
941
942         return 0;
943 }
944
945
946 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
947                                       const u8 *addr)
948 {
949         struct hostapd_data *hapd;
950         struct hostapd_bss_config *conf;
951
952         if (!wpa_s->ap_iface)
953                 return -1;
954
955         if (addr)
956                 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
957                            MAC2STR(addr));
958         else
959                 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
960
961         hapd = wpa_s->ap_iface->bss[0];
962         conf = hapd->conf;
963
964         os_free(conf->accept_mac);
965         conf->accept_mac = NULL;
966         conf->num_accept_mac = 0;
967         os_free(conf->deny_mac);
968         conf->deny_mac = NULL;
969         conf->num_deny_mac = 0;
970
971         if (addr == NULL) {
972                 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
973                 return 0;
974         }
975
976         conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
977         conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
978         if (conf->accept_mac == NULL)
979                 return -1;
980         os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
981         conf->num_accept_mac = 1;
982
983         return 0;
984 }