P2P NFC: Report connection handover as trigger for P2P
[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 "eapol_supp/eapol_supp_sm.h"
18 #include "crypto/dh_group5.h"
19 #include "ap/hostapd.h"
20 #include "ap/ap_config.h"
21 #include "ap/ap_drv_ops.h"
22 #ifdef NEED_AP_MLME
23 #include "ap/ieee802_11.h"
24 #endif /* NEED_AP_MLME */
25 #include "ap/beacon.h"
26 #include "ap/ieee802_1x.h"
27 #include "ap/wps_hostapd.h"
28 #include "ap/ctrl_iface_ap.h"
29 #include "wps/wps.h"
30 #include "common/ieee802_11_defs.h"
31 #include "config_ssid.h"
32 #include "config.h"
33 #include "wpa_supplicant_i.h"
34 #include "driver_i.h"
35 #include "p2p_supplicant.h"
36 #include "ap.h"
37 #include "ap/sta_info.h"
38 #include "notify.h"
39
40
41 #ifdef CONFIG_WPS
42 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx);
43 #endif /* CONFIG_WPS */
44
45
46 #ifdef CONFIG_IEEE80211N
47 static void wpas_conf_ap_vht(struct wpa_supplicant *wpa_s,
48                              struct hostapd_config *conf,
49                              struct hostapd_hw_modes *mode)
50 {
51         u8 center_chan = 0;
52         u8 channel = conf->channel;
53
54         if (!conf->secondary_channel)
55                 goto no_vht;
56
57         center_chan = wpas_p2p_get_vht80_center(wpa_s, mode, channel);
58         if (!center_chan)
59                 goto no_vht;
60
61         /* Use 80 MHz channel */
62         conf->vht_oper_chwidth = 1;
63         conf->vht_oper_centr_freq_seg0_idx = center_chan;
64         return;
65
66 no_vht:
67         conf->vht_oper_centr_freq_seg0_idx =
68                 channel + conf->secondary_channel * 2;
69 }
70 #endif /* CONFIG_IEEE80211N */
71
72
73 static int wpa_supplicant_conf_ap(struct wpa_supplicant *wpa_s,
74                                   struct wpa_ssid *ssid,
75                                   struct hostapd_config *conf)
76 {
77         struct hostapd_bss_config *bss = conf->bss[0];
78
79         conf->driver = wpa_s->driver;
80
81         os_strlcpy(bss->iface, wpa_s->ifname, sizeof(bss->iface));
82
83         conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
84                                                &conf->channel);
85         if (conf->hw_mode == NUM_HOSTAPD_MODES) {
86                 wpa_printf(MSG_ERROR, "Unsupported AP mode frequency: %d MHz",
87                            ssid->frequency);
88                 return -1;
89         }
90
91         /* TODO: enable HT40 if driver supports it;
92          * drop to 11b if driver does not support 11g */
93
94 #ifdef CONFIG_IEEE80211N
95         /*
96          * Enable HT20 if the driver supports it, by setting conf->ieee80211n
97          * and a mask of allowed capabilities within conf->ht_capab.
98          * Using default config settings for: conf->ht_op_mode_fixed,
99          * conf->secondary_channel, conf->require_ht
100          */
101         if (wpa_s->hw.modes) {
102                 struct hostapd_hw_modes *mode = NULL;
103                 int i, no_ht = 0;
104                 for (i = 0; i < wpa_s->hw.num_modes; i++) {
105                         if (wpa_s->hw.modes[i].mode == conf->hw_mode) {
106                                 mode = &wpa_s->hw.modes[i];
107                                 break;
108                         }
109                 }
110
111 #ifdef CONFIG_HT_OVERRIDES
112                 if (ssid->disable_ht) {
113                         conf->ieee80211n = 0;
114                         conf->ht_capab = 0;
115                         no_ht = 1;
116                 }
117 #endif /* CONFIG_HT_OVERRIDES */
118
119                 if (!no_ht && mode && mode->ht_capab) {
120                         conf->ieee80211n = 1;
121 #ifdef CONFIG_P2P
122                         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A &&
123                             (mode->ht_capab &
124                              HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
125                             ssid->ht40)
126                                 conf->secondary_channel =
127                                         wpas_p2p_get_ht40_mode(wpa_s, mode,
128                                                                conf->channel);
129                         if (conf->secondary_channel)
130                                 conf->ht_capab |=
131                                         HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
132 #endif /* CONFIG_P2P */
133
134                         /*
135                          * white-list capabilities that won't cause issues
136                          * to connecting stations, while leaving the current
137                          * capabilities intact (currently disabled SMPS).
138                          */
139                         conf->ht_capab |= mode->ht_capab &
140                                 (HT_CAP_INFO_GREEN_FIELD |
141                                  HT_CAP_INFO_SHORT_GI20MHZ |
142                                  HT_CAP_INFO_SHORT_GI40MHZ |
143                                  HT_CAP_INFO_RX_STBC_MASK |
144                                  HT_CAP_INFO_MAX_AMSDU_SIZE);
145
146                         if (mode->vht_capab && ssid->vht) {
147                                 conf->ieee80211ac = 1;
148                                 wpas_conf_ap_vht(wpa_s, conf, mode);
149                         }
150                 }
151         }
152 #endif /* CONFIG_IEEE80211N */
153
154 #ifdef CONFIG_P2P
155         if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G &&
156             (ssid->mode == WPAS_MODE_P2P_GO ||
157              ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)) {
158                 /* Remove 802.11b rates from supported and basic rate sets */
159                 int *list = os_malloc(4 * sizeof(int));
160                 if (list) {
161                         list[0] = 60;
162                         list[1] = 120;
163                         list[2] = 240;
164                         list[3] = -1;
165                 }
166                 conf->basic_rates = list;
167
168                 list = os_malloc(9 * sizeof(int));
169                 if (list) {
170                         list[0] = 60;
171                         list[1] = 90;
172                         list[2] = 120;
173                         list[3] = 180;
174                         list[4] = 240;
175                         list[5] = 360;
176                         list[6] = 480;
177                         list[7] = 540;
178                         list[8] = -1;
179                 }
180                 conf->supported_rates = list;
181         }
182
183         bss->isolate = !wpa_s->conf->p2p_intra_bss;
184         bss->force_per_enrollee_psk = wpa_s->global->p2p_per_sta_psk;
185 #endif /* CONFIG_P2P */
186
187         if (ssid->ssid_len == 0) {
188                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
189                 return -1;
190         }
191         os_memcpy(bss->ssid.ssid, ssid->ssid, ssid->ssid_len);
192         bss->ssid.ssid_len = ssid->ssid_len;
193         bss->ssid.ssid_set = 1;
194
195         bss->ignore_broadcast_ssid = ssid->ignore_broadcast_ssid;
196
197         if (ssid->auth_alg)
198                 bss->auth_algs = ssid->auth_alg;
199
200         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt))
201                 bss->wpa = ssid->proto;
202         bss->wpa_key_mgmt = ssid->key_mgmt;
203         bss->wpa_pairwise = ssid->pairwise_cipher;
204         if (ssid->psk_set) {
205                 os_free(bss->ssid.wpa_psk);
206                 bss->ssid.wpa_psk = os_zalloc(sizeof(struct hostapd_wpa_psk));
207                 if (bss->ssid.wpa_psk == NULL)
208                         return -1;
209                 os_memcpy(bss->ssid.wpa_psk->psk, ssid->psk, PMK_LEN);
210                 bss->ssid.wpa_psk->group = 1;
211         } else if (ssid->passphrase) {
212                 bss->ssid.wpa_passphrase = os_strdup(ssid->passphrase);
213         } else if (ssid->wep_key_len[0] || ssid->wep_key_len[1] ||
214                    ssid->wep_key_len[2] || ssid->wep_key_len[3]) {
215                 struct hostapd_wep_keys *wep = &bss->ssid.wep;
216                 int i;
217                 for (i = 0; i < NUM_WEP_KEYS; i++) {
218                         if (ssid->wep_key_len[i] == 0)
219                                 continue;
220                         wep->key[i] = os_malloc(ssid->wep_key_len[i]);
221                         if (wep->key[i] == NULL)
222                                 return -1;
223                         os_memcpy(wep->key[i], ssid->wep_key[i],
224                                   ssid->wep_key_len[i]);
225                         wep->len[i] = ssid->wep_key_len[i];
226                 }
227                 wep->idx = ssid->wep_tx_keyidx;
228                 wep->keys_set = 1;
229         }
230
231         if (ssid->ap_max_inactivity)
232                 bss->ap_max_inactivity = ssid->ap_max_inactivity;
233
234         if (ssid->dtim_period)
235                 bss->dtim_period = ssid->dtim_period;
236         else if (wpa_s->conf->dtim_period)
237                 bss->dtim_period = wpa_s->conf->dtim_period;
238
239         if (ssid->beacon_int)
240                 conf->beacon_int = ssid->beacon_int;
241         else if (wpa_s->conf->beacon_int)
242                 conf->beacon_int = wpa_s->conf->beacon_int;
243
244         if ((bss->wpa & 2) && bss->rsn_pairwise == 0)
245                 bss->rsn_pairwise = bss->wpa_pairwise;
246         bss->wpa_group = wpa_select_ap_group_cipher(bss->wpa, bss->wpa_pairwise,
247                                                     bss->rsn_pairwise);
248
249         if (bss->wpa && bss->ieee802_1x)
250                 bss->ssid.security_policy = SECURITY_WPA;
251         else if (bss->wpa)
252                 bss->ssid.security_policy = SECURITY_WPA_PSK;
253         else if (bss->ieee802_1x) {
254                 int cipher = WPA_CIPHER_NONE;
255                 bss->ssid.security_policy = SECURITY_IEEE_802_1X;
256                 bss->ssid.wep.default_len = bss->default_wep_key_len;
257                 if (bss->default_wep_key_len)
258                         cipher = bss->default_wep_key_len >= 13 ?
259                                 WPA_CIPHER_WEP104 : WPA_CIPHER_WEP40;
260                 bss->wpa_group = cipher;
261                 bss->wpa_pairwise = cipher;
262                 bss->rsn_pairwise = cipher;
263         } else if (bss->ssid.wep.keys_set) {
264                 int cipher = WPA_CIPHER_WEP40;
265                 if (bss->ssid.wep.len[0] >= 13)
266                         cipher = WPA_CIPHER_WEP104;
267                 bss->ssid.security_policy = SECURITY_STATIC_WEP;
268                 bss->wpa_group = cipher;
269                 bss->wpa_pairwise = cipher;
270                 bss->rsn_pairwise = cipher;
271         } else {
272                 bss->ssid.security_policy = SECURITY_PLAINTEXT;
273                 bss->wpa_group = WPA_CIPHER_NONE;
274                 bss->wpa_pairwise = WPA_CIPHER_NONE;
275                 bss->rsn_pairwise = WPA_CIPHER_NONE;
276         }
277
278         if (bss->wpa_group_rekey < 86400 && (bss->wpa & 2) &&
279             (bss->wpa_group == WPA_CIPHER_CCMP ||
280              bss->wpa_group == WPA_CIPHER_GCMP ||
281              bss->wpa_group == WPA_CIPHER_CCMP_256 ||
282              bss->wpa_group == WPA_CIPHER_GCMP_256)) {
283                 /*
284                  * Strong ciphers do not need frequent rekeying, so increase
285                  * the default GTK rekeying period to 24 hours.
286                  */
287                 bss->wpa_group_rekey = 86400;
288         }
289
290 #ifdef CONFIG_WPS
291         /*
292          * Enable WPS by default for open and WPA/WPA2-Personal network, but
293          * require user interaction to actually use it. Only the internal
294          * Registrar is supported.
295          */
296         if (bss->ssid.security_policy != SECURITY_WPA_PSK &&
297             bss->ssid.security_policy != SECURITY_PLAINTEXT)
298                 goto no_wps;
299 #ifdef CONFIG_WPS2
300         if (bss->ssid.security_policy == SECURITY_WPA_PSK &&
301             (!(bss->rsn_pairwise & WPA_CIPHER_CCMP) || !(bss->wpa & 2)))
302                 goto no_wps; /* WPS2 does not allow WPA/TKIP-only
303                               * configuration */
304 #endif /* CONFIG_WPS2 */
305         bss->eap_server = 1;
306
307         if (!ssid->ignore_broadcast_ssid)
308                 bss->wps_state = 2;
309
310         bss->ap_setup_locked = 2;
311         if (wpa_s->conf->config_methods)
312                 bss->config_methods = os_strdup(wpa_s->conf->config_methods);
313         os_memcpy(bss->device_type, wpa_s->conf->device_type,
314                   WPS_DEV_TYPE_LEN);
315         if (wpa_s->conf->device_name) {
316                 bss->device_name = os_strdup(wpa_s->conf->device_name);
317                 bss->friendly_name = os_strdup(wpa_s->conf->device_name);
318         }
319         if (wpa_s->conf->manufacturer)
320                 bss->manufacturer = os_strdup(wpa_s->conf->manufacturer);
321         if (wpa_s->conf->model_name)
322                 bss->model_name = os_strdup(wpa_s->conf->model_name);
323         if (wpa_s->conf->model_number)
324                 bss->model_number = os_strdup(wpa_s->conf->model_number);
325         if (wpa_s->conf->serial_number)
326                 bss->serial_number = os_strdup(wpa_s->conf->serial_number);
327         if (is_nil_uuid(wpa_s->conf->uuid))
328                 os_memcpy(bss->uuid, wpa_s->wps->uuid, WPS_UUID_LEN);
329         else
330                 os_memcpy(bss->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
331         os_memcpy(bss->os_version, wpa_s->conf->os_version, 4);
332         bss->pbc_in_m1 = wpa_s->conf->pbc_in_m1;
333 no_wps:
334 #endif /* CONFIG_WPS */
335
336         if (wpa_s->max_stations &&
337             wpa_s->max_stations < wpa_s->conf->max_num_sta)
338                 bss->max_num_sta = wpa_s->max_stations;
339         else
340                 bss->max_num_sta = wpa_s->conf->max_num_sta;
341
342         bss->disassoc_low_ack = wpa_s->conf->disassoc_low_ack;
343
344         if (wpa_s->conf->ap_vendor_elements) {
345                 bss->vendor_elements =
346                         wpabuf_dup(wpa_s->conf->ap_vendor_elements);
347         }
348
349         return 0;
350 }
351
352
353 static void ap_public_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
354 {
355 #ifdef CONFIG_P2P
356         struct wpa_supplicant *wpa_s = ctx;
357         const struct ieee80211_mgmt *mgmt;
358         size_t hdr_len;
359
360         mgmt = (const struct ieee80211_mgmt *) buf;
361         hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
362         if (hdr_len > len)
363                 return;
364         if (mgmt->u.action.category != WLAN_ACTION_PUBLIC)
365                 return;
366         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
367                            mgmt->u.action.category,
368                            &mgmt->u.action.u.vs_public_action.action,
369                            len - hdr_len, freq);
370 #endif /* CONFIG_P2P */
371 }
372
373
374 static void ap_wps_event_cb(void *ctx, enum wps_event event,
375                             union wps_event_data *data)
376 {
377 #ifdef CONFIG_P2P
378         struct wpa_supplicant *wpa_s = ctx;
379
380         if (event == WPS_EV_FAIL) {
381                 struct wps_event_fail *fail = &data->fail;
382
383                 if (wpa_s->parent && wpa_s->parent != wpa_s &&
384                     wpa_s == wpa_s->global->p2p_group_formation) {
385                         /*
386                          * src/ap/wps_hostapd.c has already sent this on the
387                          * main interface, so only send on the parent interface
388                          * here if needed.
389                          */
390                         wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
391                                 "msg=%d config_error=%d",
392                                 fail->msg, fail->config_error);
393                 }
394                 wpas_p2p_wps_failed(wpa_s, fail);
395         }
396 #endif /* CONFIG_P2P */
397 }
398
399
400 static void ap_sta_authorized_cb(void *ctx, const u8 *mac_addr,
401                                  int authorized, const u8 *p2p_dev_addr)
402 {
403         wpas_notify_sta_authorized(ctx, mac_addr, authorized, p2p_dev_addr);
404 }
405
406
407 #ifdef CONFIG_P2P
408 static void ap_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *p2p_dev_addr,
409                           const u8 *psk, size_t psk_len)
410 {
411
412         struct wpa_supplicant *wpa_s = ctx;
413         if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL)
414                 return;
415         wpas_p2p_new_psk_cb(wpa_s, mac_addr, p2p_dev_addr, psk, psk_len);
416 }
417 #endif /* CONFIG_P2P */
418
419
420 static int ap_vendor_action_rx(void *ctx, const u8 *buf, size_t len, int freq)
421 {
422 #ifdef CONFIG_P2P
423         struct wpa_supplicant *wpa_s = ctx;
424         const struct ieee80211_mgmt *mgmt;
425         size_t hdr_len;
426
427         mgmt = (const struct ieee80211_mgmt *) buf;
428         hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
429         if (hdr_len > len)
430                 return -1;
431         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
432                            mgmt->u.action.category,
433                            &mgmt->u.action.u.vs_public_action.action,
434                            len - hdr_len, freq);
435 #endif /* CONFIG_P2P */
436         return 0;
437 }
438
439
440 static int ap_probe_req_rx(void *ctx, const u8 *sa, const u8 *da,
441                            const u8 *bssid, const u8 *ie, size_t ie_len,
442                            int ssi_signal)
443 {
444 #ifdef CONFIG_P2P
445         struct wpa_supplicant *wpa_s = ctx;
446         return wpas_p2p_probe_req_rx(wpa_s, sa, da, bssid, ie, ie_len,
447                                      ssi_signal);
448 #else /* CONFIG_P2P */
449         return 0;
450 #endif /* CONFIG_P2P */
451 }
452
453
454 static void ap_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
455                                   const u8 *uuid_e)
456 {
457 #ifdef CONFIG_P2P
458         struct wpa_supplicant *wpa_s = ctx;
459         wpas_p2p_wps_success(wpa_s, mac_addr, 1);
460 #endif /* CONFIG_P2P */
461 }
462
463
464 static void wpas_ap_configured_cb(void *ctx)
465 {
466         struct wpa_supplicant *wpa_s = ctx;
467
468         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
469
470         if (wpa_s->ap_configured_cb)
471                 wpa_s->ap_configured_cb(wpa_s->ap_configured_cb_ctx,
472                                         wpa_s->ap_configured_cb_data);
473 }
474
475
476 int wpa_supplicant_create_ap(struct wpa_supplicant *wpa_s,
477                              struct wpa_ssid *ssid)
478 {
479         struct wpa_driver_associate_params params;
480         struct hostapd_iface *hapd_iface;
481         struct hostapd_config *conf;
482         size_t i;
483
484         if (ssid->ssid == NULL || ssid->ssid_len == 0) {
485                 wpa_printf(MSG_ERROR, "No SSID configured for AP mode");
486                 return -1;
487         }
488
489         wpa_supplicant_ap_deinit(wpa_s);
490
491         wpa_printf(MSG_DEBUG, "Setting up AP (SSID='%s')",
492                    wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
493
494         os_memset(&params, 0, sizeof(params));
495         params.ssid = ssid->ssid;
496         params.ssid_len = ssid->ssid_len;
497         switch (ssid->mode) {
498         case WPAS_MODE_INFRA:
499                 params.mode = IEEE80211_MODE_INFRA;
500                 break;
501         case WPAS_MODE_IBSS:
502                 params.mode = IEEE80211_MODE_IBSS;
503                 break;
504         case WPAS_MODE_AP:
505         case WPAS_MODE_P2P_GO:
506         case WPAS_MODE_P2P_GROUP_FORMATION:
507                 params.mode = IEEE80211_MODE_AP;
508                 break;
509         }
510         if (ssid->frequency == 0)
511                 ssid->frequency = 2462; /* default channel 11 */
512         params.freq = ssid->frequency;
513
514         params.wpa_proto = ssid->proto;
515         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
516                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
517         else
518                 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
519         params.key_mgmt_suite = wpa_s->key_mgmt;
520
521         wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher,
522                                                           1);
523         if (wpa_s->pairwise_cipher < 0) {
524                 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
525                            "cipher.");
526                 return -1;
527         }
528         params.pairwise_suite = wpa_s->pairwise_cipher;
529         params.group_suite = params.pairwise_suite;
530
531 #ifdef CONFIG_P2P
532         if (ssid->mode == WPAS_MODE_P2P_GO ||
533             ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
534                 params.p2p = 1;
535 #endif /* CONFIG_P2P */
536
537         if (wpa_s->parent->set_ap_uapsd)
538                 params.uapsd = wpa_s->parent->ap_uapsd;
539         else
540                 params.uapsd = -1;
541
542         if (wpa_drv_associate(wpa_s, &params) < 0) {
543                 wpa_msg(wpa_s, MSG_INFO, "Failed to start AP functionality");
544                 return -1;
545         }
546
547         wpa_s->ap_iface = hapd_iface = os_zalloc(sizeof(*wpa_s->ap_iface));
548         if (hapd_iface == NULL)
549                 return -1;
550         hapd_iface->owner = wpa_s;
551         hapd_iface->drv_flags = wpa_s->drv_flags;
552         hapd_iface->probe_resp_offloads = wpa_s->probe_resp_offloads;
553         hapd_iface->extended_capa = wpa_s->extended_capa;
554         hapd_iface->extended_capa_mask = wpa_s->extended_capa_mask;
555         hapd_iface->extended_capa_len = wpa_s->extended_capa_len;
556
557         wpa_s->ap_iface->conf = conf = hostapd_config_defaults();
558         if (conf == NULL) {
559                 wpa_supplicant_ap_deinit(wpa_s);
560                 return -1;
561         }
562
563         os_memcpy(wpa_s->ap_iface->conf->wmm_ac_params,
564                   wpa_s->conf->wmm_ac_params,
565                   sizeof(wpa_s->conf->wmm_ac_params));
566
567         if (params.uapsd > 0) {
568                 conf->bss[0]->wmm_enabled = 1;
569                 conf->bss[0]->wmm_uapsd = 1;
570         }
571
572         if (wpa_supplicant_conf_ap(wpa_s, ssid, conf)) {
573                 wpa_printf(MSG_ERROR, "Failed to create AP configuration");
574                 wpa_supplicant_ap_deinit(wpa_s);
575                 return -1;
576         }
577
578 #ifdef CONFIG_P2P
579         if (ssid->mode == WPAS_MODE_P2P_GO)
580                 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
581         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
582                 conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
583                         P2P_GROUP_FORMATION;
584 #endif /* CONFIG_P2P */
585
586         hapd_iface->num_bss = conf->num_bss;
587         hapd_iface->bss = os_calloc(conf->num_bss,
588                                     sizeof(struct hostapd_data *));
589         if (hapd_iface->bss == NULL) {
590                 wpa_supplicant_ap_deinit(wpa_s);
591                 return -1;
592         }
593
594         for (i = 0; i < conf->num_bss; i++) {
595                 hapd_iface->bss[i] =
596                         hostapd_alloc_bss_data(hapd_iface, conf,
597                                                conf->bss[i]);
598                 if (hapd_iface->bss[i] == NULL) {
599                         wpa_supplicant_ap_deinit(wpa_s);
600                         return -1;
601                 }
602
603                 hapd_iface->bss[i]->msg_ctx = wpa_s;
604                 hapd_iface->bss[i]->msg_ctx_parent = wpa_s->parent;
605                 hapd_iface->bss[i]->public_action_cb = ap_public_action_rx;
606                 hapd_iface->bss[i]->public_action_cb_ctx = wpa_s;
607                 hapd_iface->bss[i]->vendor_action_cb = ap_vendor_action_rx;
608                 hapd_iface->bss[i]->vendor_action_cb_ctx = wpa_s;
609                 hostapd_register_probereq_cb(hapd_iface->bss[i],
610                                              ap_probe_req_rx, wpa_s);
611                 hapd_iface->bss[i]->wps_reg_success_cb = ap_wps_reg_success_cb;
612                 hapd_iface->bss[i]->wps_reg_success_cb_ctx = wpa_s;
613                 hapd_iface->bss[i]->wps_event_cb = ap_wps_event_cb;
614                 hapd_iface->bss[i]->wps_event_cb_ctx = wpa_s;
615                 hapd_iface->bss[i]->sta_authorized_cb = ap_sta_authorized_cb;
616                 hapd_iface->bss[i]->sta_authorized_cb_ctx = wpa_s;
617 #ifdef CONFIG_P2P
618                 hapd_iface->bss[i]->new_psk_cb = ap_new_psk_cb;
619                 hapd_iface->bss[i]->new_psk_cb_ctx = wpa_s;
620                 hapd_iface->bss[i]->p2p = wpa_s->global->p2p;
621                 hapd_iface->bss[i]->p2p_group = wpas_p2p_group_init(wpa_s,
622                                                                     ssid);
623 #endif /* CONFIG_P2P */
624                 hapd_iface->bss[i]->setup_complete_cb = wpas_ap_configured_cb;
625                 hapd_iface->bss[i]->setup_complete_cb_ctx = wpa_s;
626         }
627
628         os_memcpy(hapd_iface->bss[0]->own_addr, wpa_s->own_addr, ETH_ALEN);
629         hapd_iface->bss[0]->driver = wpa_s->driver;
630         hapd_iface->bss[0]->drv_priv = wpa_s->drv_priv;
631
632         wpa_s->current_ssid = ssid;
633         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
634         os_memcpy(wpa_s->bssid, wpa_s->own_addr, ETH_ALEN);
635         wpa_s->assoc_freq = ssid->frequency;
636
637         if (hostapd_setup_interface(wpa_s->ap_iface)) {
638                 wpa_printf(MSG_ERROR, "Failed to initialize AP interface");
639                 wpa_supplicant_ap_deinit(wpa_s);
640                 return -1;
641         }
642
643         return 0;
644 }
645
646
647 void wpa_supplicant_ap_deinit(struct wpa_supplicant *wpa_s)
648 {
649 #ifdef CONFIG_WPS
650         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
651 #endif /* CONFIG_WPS */
652
653         if (wpa_s->ap_iface == NULL)
654                 return;
655
656         wpa_s->current_ssid = NULL;
657         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
658         wpa_s->assoc_freq = 0;
659 #ifdef CONFIG_P2P
660         if (wpa_s->ap_iface->bss)
661                 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
662         wpas_p2p_group_deinit(wpa_s);
663 #endif /* CONFIG_P2P */
664         hostapd_interface_deinit(wpa_s->ap_iface);
665         hostapd_interface_free(wpa_s->ap_iface);
666         wpa_s->ap_iface = NULL;
667         wpa_drv_deinit_ap(wpa_s);
668 }
669
670
671 void ap_tx_status(void *ctx, const u8 *addr,
672                   const u8 *buf, size_t len, int ack)
673 {
674 #ifdef NEED_AP_MLME
675         struct wpa_supplicant *wpa_s = ctx;
676         hostapd_tx_status(wpa_s->ap_iface->bss[0], addr, buf, len, ack);
677 #endif /* NEED_AP_MLME */
678 }
679
680
681 void ap_eapol_tx_status(void *ctx, const u8 *dst,
682                         const u8 *data, size_t len, int ack)
683 {
684 #ifdef NEED_AP_MLME
685         struct wpa_supplicant *wpa_s = ctx;
686         if (!wpa_s->ap_iface)
687                 return;
688         hostapd_tx_status(wpa_s->ap_iface->bss[0], dst, data, len, ack);
689 #endif /* NEED_AP_MLME */
690 }
691
692
693 void ap_client_poll_ok(void *ctx, const u8 *addr)
694 {
695 #ifdef NEED_AP_MLME
696         struct wpa_supplicant *wpa_s = ctx;
697         if (wpa_s->ap_iface)
698                 hostapd_client_poll_ok(wpa_s->ap_iface->bss[0], addr);
699 #endif /* NEED_AP_MLME */
700 }
701
702
703 void ap_rx_from_unknown_sta(void *ctx, const u8 *addr, int wds)
704 {
705 #ifdef NEED_AP_MLME
706         struct wpa_supplicant *wpa_s = ctx;
707         ieee802_11_rx_from_unknown(wpa_s->ap_iface->bss[0], addr, wds);
708 #endif /* NEED_AP_MLME */
709 }
710
711
712 void ap_mgmt_rx(void *ctx, struct rx_mgmt *rx_mgmt)
713 {
714 #ifdef NEED_AP_MLME
715         struct wpa_supplicant *wpa_s = ctx;
716         struct hostapd_frame_info fi;
717         os_memset(&fi, 0, sizeof(fi));
718         fi.datarate = rx_mgmt->datarate;
719         fi.ssi_signal = rx_mgmt->ssi_signal;
720         ieee802_11_mgmt(wpa_s->ap_iface->bss[0], rx_mgmt->frame,
721                         rx_mgmt->frame_len, &fi);
722 #endif /* NEED_AP_MLME */
723 }
724
725
726 void ap_mgmt_tx_cb(void *ctx, const u8 *buf, size_t len, u16 stype, int ok)
727 {
728 #ifdef NEED_AP_MLME
729         struct wpa_supplicant *wpa_s = ctx;
730         ieee802_11_mgmt_cb(wpa_s->ap_iface->bss[0], buf, len, stype, ok);
731 #endif /* NEED_AP_MLME */
732 }
733
734
735 void wpa_supplicant_ap_rx_eapol(struct wpa_supplicant *wpa_s,
736                                 const u8 *src_addr, const u8 *buf, size_t len)
737 {
738         ieee802_1x_receive(wpa_s->ap_iface->bss[0], src_addr, buf, len);
739 }
740
741
742 #ifdef CONFIG_WPS
743
744 int wpa_supplicant_ap_wps_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
745                               const u8 *p2p_dev_addr)
746 {
747         if (!wpa_s->ap_iface)
748                 return -1;
749         return hostapd_wps_button_pushed(wpa_s->ap_iface->bss[0],
750                                          p2p_dev_addr);
751 }
752
753
754 int wpa_supplicant_ap_wps_cancel(struct wpa_supplicant *wpa_s)
755 {
756         struct wps_registrar *reg;
757         int reg_sel = 0, wps_sta = 0;
758
759         if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0]->wps)
760                 return -1;
761
762         reg = wpa_s->ap_iface->bss[0]->wps->registrar;
763         reg_sel = wps_registrar_wps_cancel(reg);
764         wps_sta = ap_for_each_sta(wpa_s->ap_iface->bss[0],
765                                   ap_sta_wps_cancel, NULL);
766
767         if (!reg_sel && !wps_sta) {
768                 wpa_printf(MSG_DEBUG, "No WPS operation in progress at this "
769                            "time");
770                 return -1;
771         }
772
773         /*
774          * There are 2 cases to return wps cancel as success:
775          * 1. When wps cancel was initiated but no connection has been
776          *    established with client yet.
777          * 2. Client is in the middle of exchanging WPS messages.
778          */
779
780         return 0;
781 }
782
783
784 int wpa_supplicant_ap_wps_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
785                               const char *pin, char *buf, size_t buflen,
786                               int timeout)
787 {
788         int ret, ret_len = 0;
789
790         if (!wpa_s->ap_iface)
791                 return -1;
792
793         if (pin == NULL) {
794                 unsigned int rpin = wps_generate_pin();
795                 ret_len = os_snprintf(buf, buflen, "%08d", rpin);
796                 pin = buf;
797         } else
798                 ret_len = os_snprintf(buf, buflen, "%s", pin);
799
800         ret = hostapd_wps_add_pin(wpa_s->ap_iface->bss[0], bssid, "any", pin,
801                                   timeout);
802         if (ret)
803                 return -1;
804         return ret_len;
805 }
806
807
808 static void wpas_wps_ap_pin_timeout(void *eloop_data, void *user_ctx)
809 {
810         struct wpa_supplicant *wpa_s = eloop_data;
811         wpa_printf(MSG_DEBUG, "WPS: AP PIN timed out");
812         wpas_wps_ap_pin_disable(wpa_s);
813 }
814
815
816 static void wpas_wps_ap_pin_enable(struct wpa_supplicant *wpa_s, int timeout)
817 {
818         struct hostapd_data *hapd;
819
820         if (wpa_s->ap_iface == NULL)
821                 return;
822         hapd = wpa_s->ap_iface->bss[0];
823         wpa_printf(MSG_DEBUG, "WPS: Enabling AP PIN (timeout=%d)", timeout);
824         hapd->ap_pin_failures = 0;
825         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
826         if (timeout > 0)
827                 eloop_register_timeout(timeout, 0,
828                                        wpas_wps_ap_pin_timeout, wpa_s, NULL);
829 }
830
831
832 void wpas_wps_ap_pin_disable(struct wpa_supplicant *wpa_s)
833 {
834         struct hostapd_data *hapd;
835
836         if (wpa_s->ap_iface == NULL)
837                 return;
838         wpa_printf(MSG_DEBUG, "WPS: Disabling AP PIN");
839         hapd = wpa_s->ap_iface->bss[0];
840         os_free(hapd->conf->ap_pin);
841         hapd->conf->ap_pin = NULL;
842         eloop_cancel_timeout(wpas_wps_ap_pin_timeout, wpa_s, NULL);
843 }
844
845
846 const char * wpas_wps_ap_pin_random(struct wpa_supplicant *wpa_s, int timeout)
847 {
848         struct hostapd_data *hapd;
849         unsigned int pin;
850         char pin_txt[9];
851
852         if (wpa_s->ap_iface == NULL)
853                 return NULL;
854         hapd = wpa_s->ap_iface->bss[0];
855         pin = wps_generate_pin();
856         os_snprintf(pin_txt, sizeof(pin_txt), "%08u", pin);
857         os_free(hapd->conf->ap_pin);
858         hapd->conf->ap_pin = os_strdup(pin_txt);
859         if (hapd->conf->ap_pin == NULL)
860                 return NULL;
861         wpas_wps_ap_pin_enable(wpa_s, timeout);
862
863         return hapd->conf->ap_pin;
864 }
865
866
867 const char * wpas_wps_ap_pin_get(struct wpa_supplicant *wpa_s)
868 {
869         struct hostapd_data *hapd;
870         if (wpa_s->ap_iface == NULL)
871                 return NULL;
872         hapd = wpa_s->ap_iface->bss[0];
873         return hapd->conf->ap_pin;
874 }
875
876
877 int wpas_wps_ap_pin_set(struct wpa_supplicant *wpa_s, const char *pin,
878                         int timeout)
879 {
880         struct hostapd_data *hapd;
881         char pin_txt[9];
882         int ret;
883
884         if (wpa_s->ap_iface == NULL)
885                 return -1;
886         hapd = wpa_s->ap_iface->bss[0];
887         ret = os_snprintf(pin_txt, sizeof(pin_txt), "%s", pin);
888         if (ret < 0 || ret >= (int) sizeof(pin_txt))
889                 return -1;
890         os_free(hapd->conf->ap_pin);
891         hapd->conf->ap_pin = os_strdup(pin_txt);
892         if (hapd->conf->ap_pin == NULL)
893                 return -1;
894         wpas_wps_ap_pin_enable(wpa_s, timeout);
895
896         return 0;
897 }
898
899
900 void wpa_supplicant_ap_pwd_auth_fail(struct wpa_supplicant *wpa_s)
901 {
902         struct hostapd_data *hapd;
903
904         if (wpa_s->ap_iface == NULL)
905                 return;
906         hapd = wpa_s->ap_iface->bss[0];
907
908         /*
909          * Registrar failed to prove its knowledge of the AP PIN. Disable AP
910          * PIN if this happens multiple times to slow down brute force attacks.
911          */
912         hapd->ap_pin_failures++;
913         wpa_printf(MSG_DEBUG, "WPS: AP PIN authentication failure number %u",
914                    hapd->ap_pin_failures);
915         if (hapd->ap_pin_failures < 3)
916                 return;
917
918         wpa_printf(MSG_DEBUG, "WPS: Disable AP PIN");
919         hapd->ap_pin_failures = 0;
920         os_free(hapd->conf->ap_pin);
921         hapd->conf->ap_pin = NULL;
922 }
923
924
925 #ifdef CONFIG_WPS_NFC
926
927 struct wpabuf * wpas_ap_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
928                                              int ndef)
929 {
930         struct hostapd_data *hapd;
931
932         if (wpa_s->ap_iface == NULL)
933                 return NULL;
934         hapd = wpa_s->ap_iface->bss[0];
935         return hostapd_wps_nfc_config_token(hapd, ndef);
936 }
937
938
939 struct wpabuf * wpas_ap_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
940                                              int ndef)
941 {
942         struct hostapd_data *hapd;
943
944         if (wpa_s->ap_iface == NULL)
945                 return NULL;
946         hapd = wpa_s->ap_iface->bss[0];
947         return hostapd_wps_nfc_hs_cr(hapd, ndef);
948 }
949
950
951 int wpas_ap_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
952                                     const struct wpabuf *req,
953                                     const struct wpabuf *sel)
954 {
955         struct hostapd_data *hapd;
956
957         if (wpa_s->ap_iface == NULL)
958                 return -1;
959         hapd = wpa_s->ap_iface->bss[0];
960         return hostapd_wps_nfc_report_handover(hapd, req, sel);
961 }
962
963 #endif /* CONFIG_WPS_NFC */
964
965 #endif /* CONFIG_WPS */
966
967
968 #ifdef CONFIG_CTRL_IFACE
969
970 int ap_ctrl_iface_sta_first(struct wpa_supplicant *wpa_s,
971                             char *buf, size_t buflen)
972 {
973         if (wpa_s->ap_iface == NULL)
974                 return -1;
975         return hostapd_ctrl_iface_sta_first(wpa_s->ap_iface->bss[0],
976                                             buf, buflen);
977 }
978
979
980 int ap_ctrl_iface_sta(struct wpa_supplicant *wpa_s, const char *txtaddr,
981                       char *buf, size_t buflen)
982 {
983         if (wpa_s->ap_iface == NULL)
984                 return -1;
985         return hostapd_ctrl_iface_sta(wpa_s->ap_iface->bss[0], txtaddr,
986                                       buf, buflen);
987 }
988
989
990 int ap_ctrl_iface_sta_next(struct wpa_supplicant *wpa_s, const char *txtaddr,
991                            char *buf, size_t buflen)
992 {
993         if (wpa_s->ap_iface == NULL)
994                 return -1;
995         return hostapd_ctrl_iface_sta_next(wpa_s->ap_iface->bss[0], txtaddr,
996                                            buf, buflen);
997 }
998
999
1000 int ap_ctrl_iface_sta_disassociate(struct wpa_supplicant *wpa_s,
1001                                    const char *txtaddr)
1002 {
1003         if (wpa_s->ap_iface == NULL)
1004                 return -1;
1005         return hostapd_ctrl_iface_disassociate(wpa_s->ap_iface->bss[0],
1006                                                txtaddr);
1007 }
1008
1009
1010 int ap_ctrl_iface_sta_deauthenticate(struct wpa_supplicant *wpa_s,
1011                                      const char *txtaddr)
1012 {
1013         if (wpa_s->ap_iface == NULL)
1014                 return -1;
1015         return hostapd_ctrl_iface_deauthenticate(wpa_s->ap_iface->bss[0],
1016                                                  txtaddr);
1017 }
1018
1019
1020 int ap_ctrl_iface_wpa_get_status(struct wpa_supplicant *wpa_s, char *buf,
1021                                  size_t buflen, int verbose)
1022 {
1023         char *pos = buf, *end = buf + buflen;
1024         int ret;
1025         struct hostapd_bss_config *conf;
1026
1027         if (wpa_s->ap_iface == NULL)
1028                 return -1;
1029
1030         conf = wpa_s->ap_iface->bss[0]->conf;
1031         if (conf->wpa == 0)
1032                 return 0;
1033
1034         ret = os_snprintf(pos, end - pos,
1035                           "pairwise_cipher=%s\n"
1036                           "group_cipher=%s\n"
1037                           "key_mgmt=%s\n",
1038                           wpa_cipher_txt(conf->rsn_pairwise),
1039                           wpa_cipher_txt(conf->wpa_group),
1040                           wpa_key_mgmt_txt(conf->wpa_key_mgmt,
1041                                            conf->wpa));
1042         if (ret < 0 || ret >= end - pos)
1043                 return pos - buf;
1044         pos += ret;
1045         return pos - buf;
1046 }
1047
1048 #endif /* CONFIG_CTRL_IFACE */
1049
1050
1051 int wpa_supplicant_ap_update_beacon(struct wpa_supplicant *wpa_s)
1052 {
1053         struct hostapd_iface *iface = wpa_s->ap_iface;
1054         struct wpa_ssid *ssid = wpa_s->current_ssid;
1055         struct hostapd_data *hapd;
1056
1057         if (ssid == NULL || wpa_s->ap_iface == NULL ||
1058             ssid->mode == WPAS_MODE_INFRA ||
1059             ssid->mode == WPAS_MODE_IBSS)
1060                 return -1;
1061
1062 #ifdef CONFIG_P2P
1063         if (ssid->mode == WPAS_MODE_P2P_GO)
1064                 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER;
1065         else if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1066                 iface->conf->bss[0]->p2p = P2P_ENABLED | P2P_GROUP_OWNER |
1067                         P2P_GROUP_FORMATION;
1068 #endif /* CONFIG_P2P */
1069
1070         hapd = iface->bss[0];
1071         if (hapd->drv_priv == NULL)
1072                 return -1;
1073         ieee802_11_set_beacons(iface);
1074         hostapd_set_ap_wps_ie(hapd);
1075
1076         return 0;
1077 }
1078
1079
1080 int ap_switch_channel(struct wpa_supplicant *wpa_s,
1081                       struct csa_settings *settings)
1082 {
1083 #ifdef NEED_AP_MLME
1084         if (!wpa_s->ap_iface || !wpa_s->ap_iface->bss[0])
1085                 return -1;
1086
1087         return hostapd_switch_channel(wpa_s->ap_iface->bss[0], settings);
1088 #else /* NEED_AP_MLME */
1089         return -1;
1090 #endif /* NEED_AP_MLME */
1091 }
1092
1093
1094 int ap_ctrl_iface_chanswitch(struct wpa_supplicant *wpa_s, const char *pos)
1095 {
1096         struct csa_settings settings;
1097         int ret = hostapd_parse_csa_settings(pos, &settings);
1098
1099         if (ret)
1100                 return ret;
1101
1102         return ap_switch_channel(wpa_s, &settings);
1103 }
1104
1105
1106 void wpas_ap_ch_switch(struct wpa_supplicant *wpa_s, int freq, int ht,
1107                        int offset, int width, int cf1, int cf2)
1108 {
1109         if (!wpa_s->ap_iface)
1110                 return;
1111
1112         wpa_s->assoc_freq = freq;
1113         hostapd_event_ch_switch(wpa_s->ap_iface->bss[0], freq, ht, offset, width, cf1, cf1);
1114 }
1115
1116
1117 int wpa_supplicant_ap_mac_addr_filter(struct wpa_supplicant *wpa_s,
1118                                       const u8 *addr)
1119 {
1120         struct hostapd_data *hapd;
1121         struct hostapd_bss_config *conf;
1122
1123         if (!wpa_s->ap_iface)
1124                 return -1;
1125
1126         if (addr)
1127                 wpa_printf(MSG_DEBUG, "AP: Set MAC address filter: " MACSTR,
1128                            MAC2STR(addr));
1129         else
1130                 wpa_printf(MSG_DEBUG, "AP: Clear MAC address filter");
1131
1132         hapd = wpa_s->ap_iface->bss[0];
1133         conf = hapd->conf;
1134
1135         os_free(conf->accept_mac);
1136         conf->accept_mac = NULL;
1137         conf->num_accept_mac = 0;
1138         os_free(conf->deny_mac);
1139         conf->deny_mac = NULL;
1140         conf->num_deny_mac = 0;
1141
1142         if (addr == NULL) {
1143                 conf->macaddr_acl = ACCEPT_UNLESS_DENIED;
1144                 return 0;
1145         }
1146
1147         conf->macaddr_acl = DENY_UNLESS_ACCEPTED;
1148         conf->accept_mac = os_zalloc(sizeof(struct mac_acl_entry));
1149         if (conf->accept_mac == NULL)
1150                 return -1;
1151         os_memcpy(conf->accept_mac[0].addr, addr, ETH_ALEN);
1152         conf->num_accept_mac = 1;
1153
1154         return 0;
1155 }
1156
1157
1158 #ifdef CONFIG_WPS_NFC
1159 int wpas_ap_wps_add_nfc_pw(struct wpa_supplicant *wpa_s, u16 pw_id,
1160                            const struct wpabuf *pw, const u8 *pubkey_hash)
1161 {
1162         struct hostapd_data *hapd;
1163         struct wps_context *wps;
1164
1165         if (!wpa_s->ap_iface)
1166                 return -1;
1167         hapd = wpa_s->ap_iface->bss[0];
1168         wps = hapd->wps;
1169
1170         if (wpa_s->parent->conf->wps_nfc_dh_pubkey == NULL ||
1171             wpa_s->parent->conf->wps_nfc_dh_privkey == NULL) {
1172                 wpa_printf(MSG_DEBUG, "P2P: No NFC DH key known");
1173                 return -1;
1174         }
1175
1176         dh5_free(wps->dh_ctx);
1177         wpabuf_free(wps->dh_pubkey);
1178         wpabuf_free(wps->dh_privkey);
1179         wps->dh_privkey = wpabuf_dup(
1180                 wpa_s->parent->conf->wps_nfc_dh_privkey);
1181         wps->dh_pubkey = wpabuf_dup(
1182                 wpa_s->parent->conf->wps_nfc_dh_pubkey);
1183         if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
1184                 wps->dh_ctx = NULL;
1185                 wpabuf_free(wps->dh_pubkey);
1186                 wps->dh_pubkey = NULL;
1187                 wpabuf_free(wps->dh_privkey);
1188                 wps->dh_privkey = NULL;
1189                 return -1;
1190         }
1191         wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
1192         if (wps->dh_ctx == NULL)
1193                 return -1;
1194
1195         return wps_registrar_add_nfc_pw_token(hapd->wps->registrar, pubkey_hash,
1196                                               pw_id,
1197                                               pw ? wpabuf_head(pw) : NULL,
1198                                               pw ? wpabuf_len(pw) : 0, 1);
1199 }
1200 #endif /* CONFIG_WPS_NFC */