P2P: More complete persistent group management over D-Bus
[mech_eap.git] / wpa_supplicant / p2p_supplicant.c
1 /*
2  * wpa_supplicant - P2P
3  * Copyright (c) 2009-2010, Atheros Communications
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "common/ieee802_11_common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/wpa_ctrl.h"
22 #include "wps/wps_i.h"
23 #include "p2p/p2p.h"
24 #include "ap/hostapd.h"
25 #include "ap/p2p_hostapd.h"
26 #include "wpa_supplicant_i.h"
27 #include "driver_i.h"
28 #include "ap.h"
29 #include "config_ssid.h"
30 #include "config.h"
31 #include "mlme.h"
32 #include "notify.h"
33 #include "scan.h"
34 #include "bss.h"
35 #include "wps_supplicant.h"
36 #include "p2p_supplicant.h"
37
38
39 /*
40  * How many times to try to scan to find the GO before giving up on join
41  * request.
42  */
43 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
44
45
46 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
47 static struct wpa_supplicant *
48 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
49                          int go);
50 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
51 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
52 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
53                          const u8 *dev_addr, enum p2p_wps_method wps_method);
54 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
55 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
56 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
57 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
58
59
60 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
61                                       struct wpa_scan_results *scan_res)
62 {
63         size_t i;
64
65         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
66                 return;
67
68         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
69                    (int) scan_res->num);
70
71         for (i = 0; i < scan_res->num; i++) {
72                 struct wpa_scan_res *bss = scan_res->res[i];
73                 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
74                                          bss->freq, bss->level,
75                                          (const u8 *) (bss + 1),
76                                          bss->ie_len) > 0)
77                         break;
78         }
79
80         p2p_scan_res_handled(wpa_s->global->p2p);
81 }
82
83
84 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
85                          unsigned int num_req_dev_types,
86                          const u8 *req_dev_types)
87 {
88         struct wpa_supplicant *wpa_s = ctx;
89         struct wpa_driver_scan_params params;
90         int ret;
91         struct wpabuf *wps_ie, *ies;
92         int social_channels[] = { 2412, 2437, 2462, 0, 0 };
93
94         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
95                 return -1;
96
97         os_memset(&params, 0, sizeof(params));
98
99         /* P2P Wildcard SSID */
100         params.num_ssids = 1;
101         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
102         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
103
104         wpa_s->wps->dev.p2p = 1;
105         wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
106                                         WPS_REQ_ENROLLEE,
107                                         num_req_dev_types, req_dev_types);
108         if (wps_ie == NULL)
109                 return -1;
110
111         ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
112         if (ies == NULL) {
113                 wpabuf_free(wps_ie);
114                 return -1;
115         }
116         wpabuf_put_buf(ies, wps_ie);
117         wpabuf_free(wps_ie);
118
119         p2p_scan_ie(wpa_s->global->p2p, ies);
120
121         params.extra_ies = wpabuf_head(ies);
122         params.extra_ies_len = wpabuf_len(ies);
123
124         switch (type) {
125         case P2P_SCAN_SOCIAL:
126                 params.freqs = social_channels;
127                 break;
128         case P2P_SCAN_FULL:
129                 break;
130         case P2P_SCAN_SPECIFIC:
131                 social_channels[0] = freq;
132                 social_channels[1] = 0;
133                 params.freqs = social_channels;
134                 break;
135         case P2P_SCAN_SOCIAL_PLUS_ONE:
136                 social_channels[3] = freq;
137                 params.freqs = social_channels;
138                 break;
139         }
140
141         wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
142         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
143                 ret = ieee80211_sta_req_scan(wpa_s, &params);
144         else
145                 ret = wpa_drv_scan(wpa_s, &params);
146
147         wpabuf_free(ies);
148
149         return ret;
150 }
151
152
153 #ifdef CONFIG_CLIENT_MLME
154 static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq)
155 {
156         struct wpa_supplicant *wpa_s = ctx;
157         const struct ieee80211_mgmt *mgmt;
158         size_t hdr_len;
159
160         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
161                 return;
162         mgmt = (const struct ieee80211_mgmt *) buf;
163         hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
164         if (hdr_len > len)
165                 return;
166         p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
167                       mgmt->u.action.category,
168                       &mgmt->u.action.u.vs_public_action.action,
169                       len - hdr_len, freq);
170 }
171 #endif /* CONFIG_CLIENT_MLME */
172
173
174 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
175 {
176         switch (p2p_group_interface) {
177         case P2P_GROUP_INTERFACE_PENDING:
178                 return WPA_IF_P2P_GROUP;
179         case P2P_GROUP_INTERFACE_GO:
180                 return WPA_IF_P2P_GO;
181         case P2P_GROUP_INTERFACE_CLIENT:
182                 return WPA_IF_P2P_CLIENT;
183         }
184
185         return WPA_IF_P2P_GROUP;
186 }
187
188
189 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
190                                                   const u8 *ssid,
191                                                   size_t ssid_len, int *go)
192 {
193         struct wpa_ssid *s;
194
195         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
196                 for (s = wpa_s->conf->ssid; s; s = s->next) {
197                         if (s->disabled != 0 || !s->p2p_group ||
198                             s->ssid_len != ssid_len ||
199                             os_memcmp(ssid, s->ssid, ssid_len) != 0)
200                                 continue;
201                         if (s->mode == WPAS_MODE_P2P_GO &&
202                             s != wpa_s->current_ssid)
203                                 continue;
204                         if (go)
205                                 *go = s->mode == WPAS_MODE_P2P_GO;
206                         return wpa_s;
207                 }
208         }
209
210         return NULL;
211 }
212
213
214 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
215 {
216         struct wpa_ssid *ssid;
217         char *gtype;
218         const char *reason;
219
220         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
221
222         ssid = wpa_s->current_ssid;
223         if (ssid == NULL) {
224                 /*
225                  * The current SSID was not known, but there may still be a
226                  * pending P2P group interface waiting for provisioning.
227                  */
228                 ssid = wpa_s->conf->ssid;
229                 while (ssid) {
230                         if (ssid->p2p_group &&
231                             (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
232                              (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
233                                 break;
234                         ssid = ssid->next;
235                 }
236         }
237         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
238                 gtype = "GO";
239         else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
240                  (ssid && ssid->mode == WPAS_MODE_INFRA)) {
241                 wpa_s->reassociate = 0;
242                 wpa_s->disconnected = 1;
243                 wpa_supplicant_deauthenticate(wpa_s,
244                                               WLAN_REASON_DEAUTH_LEAVING);
245                 gtype = "client";
246         } else
247                 gtype = "GO";
248         if (wpa_s->cross_connect_in_use) {
249                 wpa_s->cross_connect_in_use = 0;
250                 wpa_msg(wpa_s->parent, MSG_INFO,
251                         P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
252                         wpa_s->ifname, wpa_s->cross_connect_uplink);
253         }
254         switch (wpa_s->removal_reason) {
255         case P2P_GROUP_REMOVAL_REQUESTED:
256                 reason = " reason=REQUESTED";
257                 break;
258         case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
259                 reason = " reason=IDLE";
260                 break;
261         case P2P_GROUP_REMOVAL_UNAVAILABLE:
262                 reason = " reason=UNAVAILABLE";
263                 break;
264         default:
265                 reason = "";
266                 break;
267         }
268         wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s%s",
269                 wpa_s->ifname, gtype, reason);
270
271         if (ssid)
272                 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
273
274         if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
275                 struct wpa_global *global;
276                 char *ifname;
277                 enum wpa_driver_if_type type;
278                 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
279                         wpa_s->ifname);
280                 global = wpa_s->global;
281                 ifname = os_strdup(wpa_s->ifname);
282                 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
283                 wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
284                 wpa_s = global->ifaces;
285                 if (wpa_s && ifname)
286                         wpa_drv_if_remove(wpa_s, type, ifname);
287                 os_free(ifname);
288                 return;
289         }
290
291         wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
292         if (ssid && (ssid->p2p_group ||
293                      ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
294                      (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
295                 int id = ssid->id;
296                 if (ssid == wpa_s->current_ssid)
297                         wpa_s->current_ssid = NULL;
298                 /*
299                  * Networks objects created during any P2P activities are not
300                  * exposed out as they might/will confuse certain non-P2P aware
301                  * applications since these network objects won't behave like
302                  * regular ones.
303                  *
304                  * Likewise, we don't send out network removed signals for such
305                  * network objects.
306                  */
307                 wpa_config_remove_network(wpa_s->conf, id);
308                 wpa_supplicant_clear_status(wpa_s);
309         } else {
310                 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
311                            "found");
312         }
313         wpa_supplicant_ap_deinit(wpa_s);
314 }
315
316
317 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
318                                      u8 *go_dev_addr,
319                                      const u8 *ssid, size_t ssid_len)
320 {
321         struct wpa_bss *bss;
322         const u8 *bssid;
323         struct wpabuf *p2p;
324         u8 group_capab;
325         const u8 *addr;
326
327         if (wpa_s->go_params)
328                 bssid = wpa_s->go_params->peer_interface_addr;
329         else
330                 bssid = wpa_s->bssid;
331
332         bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
333         if (bss == NULL) {
334                 u8 iface_addr[ETH_ALEN];
335                 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
336                                            iface_addr) == 0)
337                         bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
338         }
339         if (bss == NULL) {
340                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
341                            "group is persistent - BSS " MACSTR " not found",
342                            MAC2STR(bssid));
343                 return 0;
344         }
345
346         p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
347         if (p2p == NULL) {
348                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
349                            "group is persistent - BSS " MACSTR
350                            " did not include P2P IE", MAC2STR(bssid));
351                 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
352                             (u8 *) (bss + 1), bss->ie_len);
353                 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
354                             ((u8 *) bss + 1) + bss->ie_len,
355                             bss->beacon_ie_len);
356                 return 0;
357         }
358
359         group_capab = p2p_get_group_capab(p2p);
360         addr = p2p_get_go_dev_addr(p2p);
361         wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
362                    "group_capab=0x%x", group_capab);
363         if (addr) {
364                 os_memcpy(go_dev_addr, addr, ETH_ALEN);
365                 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
366                            MAC2STR(addr));
367         } else
368                 os_memset(go_dev_addr, 0, ETH_ALEN);
369         wpabuf_free(p2p);
370
371         wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
372                    "go_dev_addr=" MACSTR,
373                    MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
374
375         return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
376 }
377
378
379 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
380                                            struct wpa_ssid *ssid,
381                                            const u8 *go_dev_addr)
382 {
383         struct wpa_ssid *s;
384         int changed = 0;
385
386         wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
387                    "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
388         for (s = wpa_s->conf->ssid; s; s = s->next) {
389                 if (s->disabled == 2 &&
390                     os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
391                     s->ssid_len == ssid->ssid_len &&
392                     os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
393                         break;
394         }
395
396         if (s) {
397                 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
398                            "entry");
399                 if (ssid->passphrase && !s->passphrase)
400                         changed = 1;
401                 else if (ssid->passphrase && s->passphrase &&
402                          os_strcmp(ssid->passphrase, s->passphrase) != 0)
403                         changed = 1;
404         } else {
405                 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
406                            "entry");
407                 changed = 1;
408                 s = wpa_config_add_network(wpa_s->conf);
409                 if (s == NULL)
410                         return -1;
411
412                 /*
413                  * Instead of network_added we emit persistent_group_added
414                  * notification. Also to keep the defense checks in
415                  * persistent_group obj registration method, we set the
416                  * relevant flags in s to designate it as a persistent group.
417                  */
418                 s->p2p_group = 1;
419                 s->p2p_persistent_group = 1;
420                 wpas_notify_persistent_group_added(wpa_s, s);
421                 wpa_config_set_network_defaults(s);
422         }
423
424         s->p2p_group = 1;
425         s->p2p_persistent_group = 1;
426         s->disabled = 2;
427         s->bssid_set = 1;
428         os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
429         s->mode = ssid->mode;
430         s->auth_alg = WPA_AUTH_ALG_OPEN;
431         s->key_mgmt = WPA_KEY_MGMT_PSK;
432         s->proto = WPA_PROTO_RSN;
433         s->pairwise_cipher = WPA_CIPHER_CCMP;
434         s->export_keys = 1;
435         if (ssid->passphrase) {
436                 os_free(s->passphrase);
437                 s->passphrase = os_strdup(ssid->passphrase);
438         }
439         if (ssid->psk_set) {
440                 s->psk_set = 1;
441                 os_memcpy(s->psk, ssid->psk, 32);
442         }
443         if (s->passphrase && !s->psk_set)
444                 wpa_config_update_psk(s);
445         if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
446                 os_free(s->ssid);
447                 s->ssid = os_malloc(ssid->ssid_len);
448         }
449         if (s->ssid) {
450                 s->ssid_len = ssid->ssid_len;
451                 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
452         }
453
454 #ifndef CONFIG_NO_CONFIG_WRITE
455         if (changed && wpa_s->conf->update_config &&
456             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
457                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
458         }
459 #endif /* CONFIG_NO_CONFIG_WRITE */
460
461         return s->id;
462 }
463
464
465 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
466                                            int success)
467 {
468         struct wpa_ssid *ssid;
469         const char *ssid_txt;
470         int client;
471         int persistent;
472         u8 go_dev_addr[ETH_ALEN];
473         int network_id = -1;
474
475         /*
476          * This callback is likely called for the main interface. Update wpa_s
477          * to use the group interface if a new interface was created for the
478          * group.
479          */
480         if (wpa_s->global->p2p_group_formation)
481                 wpa_s = wpa_s->global->p2p_group_formation;
482         wpa_s->global->p2p_group_formation = NULL;
483         wpa_s->p2p_in_provisioning = 0;
484
485         if (!success) {
486                 wpa_msg(wpa_s->parent, MSG_INFO,
487                         P2P_EVENT_GROUP_FORMATION_FAILURE);
488                 wpas_p2p_group_delete(wpa_s);
489                 return;
490         }
491
492         wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
493
494         ssid = wpa_s->current_ssid;
495         if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
496                 ssid->mode = WPAS_MODE_P2P_GO;
497                 p2p_group_notif_formation_done(wpa_s->p2p_group);
498                 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
499         }
500
501         persistent = 0;
502         if (ssid) {
503                 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
504                 client = ssid->mode == WPAS_MODE_INFRA;
505                 if (ssid->mode == WPAS_MODE_P2P_GO) {
506                         persistent = ssid->p2p_persistent_group;
507                         os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
508                                   ETH_ALEN);
509                 } else
510                         persistent = wpas_p2p_persistent_group(wpa_s,
511                                                                go_dev_addr,
512                                                                ssid->ssid,
513                                                                ssid->ssid_len);
514         } else {
515                 ssid_txt = "";
516                 client = wpa_s->p2p_group_interface ==
517                         P2P_GROUP_INTERFACE_CLIENT;
518                 os_memset(go_dev_addr, 0, ETH_ALEN);
519         }
520
521         wpa_s->show_group_started = 0;
522         if (client) {
523                 /*
524                  * Indicate event only after successfully completed 4-way
525                  * handshake, i.e., when the interface is ready for data
526                  * packets.
527                  */
528                 wpa_s->show_group_started = 1;
529         } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
530                 char psk[65];
531                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
532                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
533                         "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
534                         "%s",
535                         wpa_s->ifname, ssid_txt, ssid->frequency, psk,
536                         MAC2STR(go_dev_addr),
537                         persistent ? " [PERSISTENT]" : "");
538                 wpas_p2p_cross_connect_setup(wpa_s);
539                 wpas_p2p_set_group_idle_timeout(wpa_s);
540         } else {
541                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
542                         "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
543                         "go_dev_addr=" MACSTR "%s",
544                         wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
545                         ssid && ssid->passphrase ? ssid->passphrase : "",
546                         MAC2STR(go_dev_addr),
547                         persistent ? " [PERSISTENT]" : "");
548                 wpas_p2p_cross_connect_setup(wpa_s);
549                 wpas_p2p_set_group_idle_timeout(wpa_s);
550         }
551
552         if (persistent)
553                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
554                                                              ssid, go_dev_addr);
555         if (network_id < 0)
556                 network_id = ssid->id;
557         if (!client)
558                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
559 }
560
561
562 static struct wpa_supplicant *
563 wpas_get_tx_interface(struct wpa_supplicant *wpa_s, const u8 *src)
564 {
565         struct wpa_supplicant *iface;
566
567         if (os_memcmp(src, wpa_s->own_addr, ETH_ALEN) == 0)
568                 return wpa_s;
569
570         /*
571          * Try to find a group interface that matches with the source address.
572          */
573         iface = wpa_s->global->ifaces;
574         while (iface) {
575                 if (os_memcmp(wpa_s->pending_action_src,
576                               iface->own_addr, ETH_ALEN) == 0)
577                         break;
578                 iface = iface->next;
579         }
580         if (iface) {
581                 wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
582                            "instead of interface %s for Action TX",
583                            iface->ifname, wpa_s->ifname);
584                 return iface;
585         }
586
587         return wpa_s;
588 }
589
590
591 static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
592 {
593         struct wpa_supplicant *wpa_s = eloop_ctx;
594         struct wpa_supplicant *iface;
595         int res;
596         int without_roc;
597
598         without_roc = wpa_s->pending_action_without_roc;
599         wpa_s->pending_action_without_roc = 0;
600         wpa_printf(MSG_DEBUG, "P2P: Send Action callback (without_roc=%d "
601                    "pending_action_tx=%p)",
602                    without_roc, wpa_s->pending_action_tx);
603
604         if (wpa_s->pending_action_tx == NULL)
605                 return;
606
607         /*
608          * This call is likely going to be on the P2P device instance if the
609          * driver uses a separate interface for that purpose. However, some
610          * Action frames are actually sent within a P2P Group and when that is
611          * the case, we need to follow power saving (e.g., GO buffering the
612          * frame for a client in PS mode or a client following the advertised
613          * NoA from its GO). To make that easier for the driver, select the
614          * correct group interface here.
615          */
616         iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
617
618         if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
619             wpa_s->pending_action_freq != 0 &&
620             wpa_s->pending_action_freq != iface->assoc_freq) {
621                 wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
622                            "waiting for another freq=%u (off_channel_freq=%u "
623                            "assoc_freq=%u)",
624                            wpa_s->pending_action_freq,
625                            wpa_s->off_channel_freq,
626                            iface->assoc_freq);
627                 if (without_roc && wpa_s->off_channel_freq == 0) {
628                         /*
629                          * We may get here if wpas_send_action() found us to be
630                          * on the correct channel, but remain-on-channel cancel
631                          * event was received before getting here.
632                          */
633                         wpa_printf(MSG_DEBUG, "P2P: Schedule "
634                                    "remain-on-channel to send Action frame");
635                         if (wpa_drv_remain_on_channel(
636                                     wpa_s, wpa_s->pending_action_freq, 200) <
637                             0) {
638                                 wpa_printf(MSG_DEBUG, "P2P: Failed to request "
639                                            "driver to remain on channel (%u "
640                                            "MHz) for Action Frame TX",
641                                            wpa_s->pending_action_freq);
642                         } else {
643                                 wpa_s->off_channel_freq = 0;
644                                 wpa_s->roc_waiting_drv_freq =
645                                         wpa_s->pending_action_freq;
646                         }
647                 }
648                 return;
649         }
650
651         wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
652                    MACSTR " using interface %s",
653                    MAC2STR(wpa_s->pending_action_dst), iface->ifname);
654         res = wpa_drv_send_action(iface, wpa_s->pending_action_freq, 0,
655                                   wpa_s->pending_action_dst,
656                                   wpa_s->pending_action_src,
657                                   wpa_s->pending_action_bssid,
658                                   wpabuf_head(wpa_s->pending_action_tx),
659                                   wpabuf_len(wpa_s->pending_action_tx));
660         if (res) {
661                 wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending "
662                            "Action frame");
663                 /*
664                  * Use fake TX status event to allow P2P state machine to
665                  * continue.
666                  */
667                 wpas_send_action_tx_status(
668                         wpa_s, wpa_s->pending_action_dst,
669                         wpabuf_head(wpa_s->pending_action_tx),
670                         wpabuf_len(wpa_s->pending_action_tx),
671                         P2P_SEND_ACTION_FAILED);
672         }
673 }
674
675
676 void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
677                                 const u8 *data, size_t data_len,
678                                 enum p2p_send_action_result result)
679 {
680         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
681                 return;
682         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
683                 return;
684
685         if (wpa_s->pending_action_tx == NULL) {
686                 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no "
687                            "pending operation");
688                 return;
689         }
690
691         if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
692                 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown "
693                            "destination address");
694                 return;
695         }
696
697         wpabuf_free(wpa_s->pending_action_tx);
698         wpa_s->pending_action_tx = NULL;
699
700         p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq,
701                            wpa_s->pending_action_dst,
702                            wpa_s->pending_action_src,
703                            wpa_s->pending_action_bssid,
704                            result);
705
706         if (wpa_s->pending_pd_before_join &&
707             (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
708                        ETH_ALEN) == 0 ||
709              os_memcmp(wpa_s->pending_action_dst,
710                        wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
711                 wpa_s->pending_pd_before_join = 0;
712                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
713                            "join-existing-group operation");
714                 wpas_p2p_join_start(wpa_s);
715         }
716 }
717
718
719 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
720                             const u8 *src, const u8 *bssid, const u8 *buf,
721                             size_t len, unsigned int wait_time)
722 {
723         struct wpa_supplicant *wpa_s = ctx;
724
725         wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR
726                    " src=" MACSTR " bssid=" MACSTR " len=%d",
727                    freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
728                    (int) len);
729
730         if (wpa_s->pending_action_tx) {
731                 wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
732                            "to " MACSTR, MAC2STR(wpa_s->pending_action_dst));
733                 wpabuf_free(wpa_s->pending_action_tx);
734         }
735         wpa_s->pending_action_tx = wpabuf_alloc(len);
736         if (wpa_s->pending_action_tx == NULL) {
737                 wpa_printf(MSG_DEBUG, "P2P: Failed to allocate Action frame "
738                            "TX buffer (len=%llu)", (unsigned long long) len);
739                 return -1;
740         }
741         wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
742         os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
743         os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
744         os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
745         wpa_s->pending_action_freq = freq;
746
747         if (freq != 0 && wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
748                 struct wpa_supplicant *iface;
749
750                 iface = wpas_get_tx_interface(wpa_s, wpa_s->pending_action_src);
751                 wpa_s->action_tx_wait_time = wait_time;
752
753                 return wpa_drv_send_action(iface, wpa_s->pending_action_freq,
754                                         wait_time, wpa_s->pending_action_dst,
755                                         wpa_s->pending_action_src,
756                                         wpa_s->pending_action_bssid,
757                                         wpabuf_head(wpa_s->pending_action_tx),
758                                         wpabuf_len(wpa_s->pending_action_tx));
759         }
760
761         if (freq) {
762                 struct wpa_supplicant *tx_iface;
763                 tx_iface = wpas_get_tx_interface(wpa_s, src);
764                 if (tx_iface->assoc_freq == freq) {
765                         wpa_printf(MSG_DEBUG, "P2P: Already on requested "
766                                    "channel (TX interface operating channel)");
767                         freq = 0;
768                 }
769         }
770
771         if (wpa_s->off_channel_freq == freq || freq == 0) {
772                 wpa_printf(MSG_DEBUG, "P2P: Already on requested channel; "
773                            "send Action frame immediately");
774                 /* TODO: Would there ever be need to extend the current
775                  * duration on the channel? */
776                 wpa_s->pending_action_without_roc = 1;
777                 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
778                 eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
779                 return 0;
780         }
781         wpa_s->pending_action_without_roc = 0;
782
783         if (wpa_s->roc_waiting_drv_freq == freq) {
784                 wpa_printf(MSG_DEBUG, "P2P: Already waiting for driver to get "
785                            "to frequency %u MHz; continue waiting to send the "
786                            "Action frame", freq);
787                 return 0;
788         }
789
790         wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
791                    "once the driver gets to the requested channel");
792         if (wait_time > wpa_s->max_remain_on_chan)
793                 wait_time = wpa_s->max_remain_on_chan;
794         if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
795                 wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
796                            "to remain on channel (%u MHz) for Action "
797                            "Frame TX", freq);
798                 return -1;
799         }
800         wpa_s->off_channel_freq = 0;
801         wpa_s->roc_waiting_drv_freq = freq;
802
803         return 0;
804 }
805
806
807 static void wpas_send_action_done(void *ctx)
808 {
809         struct wpa_supplicant *wpa_s = ctx;
810         wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
811         wpabuf_free(wpa_s->pending_action_tx);
812         wpa_s->pending_action_tx = NULL;
813         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) {
814                 if (wpa_s->action_tx_wait_time)
815                         wpa_drv_send_action_cancel_wait(wpa_s);
816                 wpa_s->off_channel_freq = 0;
817         } else if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
818                 wpa_drv_cancel_remain_on_channel(wpa_s);
819                 wpa_s->off_channel_freq = 0;
820                 wpa_s->roc_waiting_drv_freq = 0;
821         }
822 }
823
824
825 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
826                                     struct p2p_go_neg_results *params)
827 {
828         if (wpa_s->go_params == NULL) {
829                 wpa_s->go_params = os_malloc(sizeof(*params));
830                 if (wpa_s->go_params == NULL)
831                         return -1;
832         }
833         os_memcpy(wpa_s->go_params, params, sizeof(*params));
834         return 0;
835 }
836
837
838 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
839                                     struct p2p_go_neg_results *res)
840 {
841         wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
842                    MAC2STR(res->peer_interface_addr));
843         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
844                           res->ssid, res->ssid_len);
845         wpa_supplicant_ap_deinit(wpa_s);
846         wpas_copy_go_neg_results(wpa_s, res);
847         if (res->wps_method == WPS_PBC)
848                 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
849         else {
850                 u16 dev_pw_id = DEV_PW_DEFAULT;
851                 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
852                         dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
853                 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
854                                    wpa_s->p2p_pin, 1, dev_pw_id);
855         }
856 }
857
858
859 static void p2p_go_configured(void *ctx, void *data)
860 {
861         struct wpa_supplicant *wpa_s = ctx;
862         struct p2p_go_neg_results *params = data;
863         struct wpa_ssid *ssid;
864         int network_id = -1;
865
866         ssid = wpa_s->current_ssid;
867         if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
868                 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
869                 if (wpa_s->global->p2p_group_formation == wpa_s)
870                         wpa_s->global->p2p_group_formation = NULL;
871                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
872                         "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
873                         "go_dev_addr=" MACSTR "%s",
874                         wpa_s->ifname,
875                         wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
876                         ssid->frequency,
877                         params->passphrase ? params->passphrase : "",
878                         MAC2STR(wpa_s->parent->own_addr),
879                         params->persistent_group ? " [PERSISTENT]" : "");
880
881                 if (params->persistent_group)
882                         network_id = wpas_p2p_store_persistent_group(
883                                 wpa_s->parent, ssid,
884                                 wpa_s->parent->own_addr);
885                 if (network_id < 0)
886                         network_id = ssid->id;
887                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
888                 wpas_p2p_cross_connect_setup(wpa_s);
889                 wpas_p2p_set_group_idle_timeout(wpa_s);
890                 return;
891         }
892
893         wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
894         if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
895                                               params->peer_interface_addr)) {
896                 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
897                            "filtering");
898                 return;
899         }
900         if (params->wps_method == WPS_PBC)
901                 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
902                                           NULL);
903         else if (wpa_s->p2p_pin[0])
904                 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
905                                           wpa_s->p2p_pin, NULL, 0);
906         os_free(wpa_s->go_params);
907         wpa_s->go_params = NULL;
908 }
909
910
911 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
912                               struct p2p_go_neg_results *params,
913                               int group_formation)
914 {
915         struct wpa_ssid *ssid;
916
917         if (wpas_copy_go_neg_results(wpa_s, params) < 0)
918                 return;
919
920         ssid = wpa_config_add_network(wpa_s->conf);
921         if (ssid == NULL)
922                 return;
923
924         wpa_config_set_network_defaults(ssid);
925         ssid->temporary = 1;
926         ssid->p2p_group = 1;
927         ssid->p2p_persistent_group = params->persistent_group;
928         ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
929                 WPAS_MODE_P2P_GO;
930         ssid->frequency = params->freq;
931         ssid->ssid = os_zalloc(params->ssid_len + 1);
932         if (ssid->ssid) {
933                 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
934                 ssid->ssid_len = params->ssid_len;
935         }
936         ssid->auth_alg = WPA_AUTH_ALG_OPEN;
937         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
938         ssid->proto = WPA_PROTO_RSN;
939         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
940         ssid->passphrase = os_strdup(params->passphrase);
941
942         wpa_s->ap_configured_cb = p2p_go_configured;
943         wpa_s->ap_configured_cb_ctx = wpa_s;
944         wpa_s->ap_configured_cb_data = wpa_s->go_params;
945         wpa_s->connect_without_scan = ssid;
946         wpa_s->reassociate = 1;
947         wpa_s->disconnected = 0;
948         wpa_supplicant_req_scan(wpa_s, 0, 0);
949 }
950
951
952 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
953                                   const struct wpa_supplicant *src)
954 {
955         struct wpa_config *d;
956         const struct wpa_config *s;
957
958         d = dst->conf;
959         s = src->conf;
960
961 #define C(n) if (s->n) d->n = os_strdup(s->n)
962         C(device_name);
963         C(manufacturer);
964         C(model_name);
965         C(model_number);
966         C(serial_number);
967         C(config_methods);
968 #undef C
969
970         os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
971         os_memcpy(d->sec_device_type, s->sec_device_type,
972                   sizeof(d->sec_device_type));
973         d->num_sec_device_types = s->num_sec_device_types;
974
975         d->p2p_group_idle = s->p2p_group_idle;
976         d->p2p_intra_bss = s->p2p_intra_bss;
977 }
978
979
980 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
981                                         enum wpa_driver_if_type type)
982 {
983         char ifname[120], force_ifname[120];
984
985         if (wpa_s->pending_interface_name[0]) {
986                 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
987                            "- skip creation of a new one");
988                 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
989                         wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
990                                    "unknown?! ifname='%s'",
991                                    wpa_s->pending_interface_name);
992                         return -1;
993                 }
994                 return 0;
995         }
996
997         os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
998                     wpa_s->p2p_group_idx);
999         if (os_strlen(ifname) >= IFNAMSIZ &&
1000             os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1001                 /* Try to avoid going over the IFNAMSIZ length limit */
1002                 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1003                             wpa_s->p2p_group_idx);
1004         }
1005         force_ifname[0] = '\0';
1006
1007         wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1008                    ifname);
1009         wpa_s->p2p_group_idx++;
1010
1011         wpa_s->pending_interface_type = type;
1012         if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1013                            wpa_s->pending_interface_addr, NULL) < 0) {
1014                 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1015                            "interface");
1016                 return -1;
1017         }
1018
1019         if (force_ifname[0]) {
1020                 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1021                            force_ifname);
1022                 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1023                            sizeof(wpa_s->pending_interface_name));
1024         } else
1025                 os_strlcpy(wpa_s->pending_interface_name, ifname,
1026                            sizeof(wpa_s->pending_interface_name));
1027         wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1028                    MACSTR, wpa_s->pending_interface_name,
1029                    MAC2STR(wpa_s->pending_interface_addr));
1030
1031         return 0;
1032 }
1033
1034
1035 static void wpas_p2p_remove_pending_group_interface(
1036         struct wpa_supplicant *wpa_s)
1037 {
1038         if (!wpa_s->pending_interface_name[0] ||
1039             is_zero_ether_addr(wpa_s->pending_interface_addr))
1040                 return; /* No pending virtual interface */
1041
1042         wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1043                    wpa_s->pending_interface_name);
1044         wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1045                           wpa_s->pending_interface_name);
1046         os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1047         wpa_s->pending_interface_name[0] = '\0';
1048 }
1049
1050
1051 static struct wpa_supplicant *
1052 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1053 {
1054         struct wpa_interface iface;
1055         struct wpa_supplicant *group_wpa_s;
1056
1057         if (!wpa_s->pending_interface_name[0]) {
1058                 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1059                 if (!wpas_p2p_create_iface(wpa_s))
1060                         return NULL;
1061                 /*
1062                  * Something has forced us to remove the pending interface; try
1063                  * to create a new one and hope for the best that we will get
1064                  * the same local address.
1065                  */
1066                 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1067                                                  WPA_IF_P2P_CLIENT) < 0)
1068                         return NULL;
1069         }
1070
1071         os_memset(&iface, 0, sizeof(iface));
1072         iface.ifname = wpa_s->pending_interface_name;
1073         iface.driver = wpa_s->driver->name;
1074         iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1075         iface.driver_param = wpa_s->conf->driver_param;
1076         group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1077         if (group_wpa_s == NULL) {
1078                 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1079                            "wpa_supplicant interface");
1080                 return NULL;
1081         }
1082         wpa_s->pending_interface_name[0] = '\0';
1083         group_wpa_s->parent = wpa_s;
1084         group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1085                 P2P_GROUP_INTERFACE_CLIENT;
1086         wpa_s->global->p2p_group_formation = group_wpa_s;
1087
1088         wpas_p2p_clone_config(group_wpa_s, wpa_s);
1089
1090         return group_wpa_s;
1091 }
1092
1093
1094 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1095                                              void *timeout_ctx)
1096 {
1097         struct wpa_supplicant *wpa_s = eloop_ctx;
1098         wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1099         if (wpa_s->global->p2p)
1100                 p2p_group_formation_failed(wpa_s->global->p2p);
1101         else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1102                 wpa_drv_p2p_group_formation_failed(wpa_s);
1103         wpas_group_formation_completed(wpa_s, 0);
1104 }
1105
1106
1107 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1108 {
1109         struct wpa_supplicant *wpa_s = ctx;
1110
1111         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1112                 wpa_drv_cancel_remain_on_channel(wpa_s);
1113                 wpa_s->off_channel_freq = 0;
1114                 wpa_s->roc_waiting_drv_freq = 0;
1115         }
1116
1117         if (res->status) {
1118                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1119                         res->status);
1120                 wpas_notify_p2p_go_neg_completed(wpa_s, res->status);
1121                 wpas_p2p_remove_pending_group_interface(wpa_s);
1122                 return;
1123         }
1124
1125         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1126         wpas_notify_p2p_go_neg_completed(wpa_s, P2P_SC_SUCCESS);
1127
1128         if (wpa_s->create_p2p_iface) {
1129                 struct wpa_supplicant *group_wpa_s =
1130                         wpas_p2p_init_group_interface(wpa_s, res->role_go);
1131                 if (group_wpa_s == NULL) {
1132                         wpas_p2p_remove_pending_group_interface(wpa_s);
1133                         return;
1134                 }
1135                 if (group_wpa_s != wpa_s) {
1136                         os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1137                                   sizeof(group_wpa_s->p2p_pin));
1138                         group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1139                 }
1140                 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1141                 wpa_s->pending_interface_name[0] = '\0';
1142                 group_wpa_s->p2p_in_provisioning = 1;
1143
1144                 if (res->role_go)
1145                         wpas_start_wps_go(group_wpa_s, res, 1);
1146                 else
1147                         wpas_start_wps_enrollee(group_wpa_s, res);
1148         } else {
1149                 wpa_s->p2p_in_provisioning = 1;
1150                 wpa_s->global->p2p_group_formation = wpa_s;
1151
1152                 if (res->role_go)
1153                         wpas_start_wps_go(wpa_s, res, 1);
1154                 else
1155                         wpas_start_wps_enrollee(ctx, res);
1156         }
1157
1158         wpa_s->p2p_long_listen = 0;
1159         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1160
1161         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1162         eloop_register_timeout(15 + res->peer_config_timeout / 100,
1163                                (res->peer_config_timeout % 100) * 10000,
1164                                wpas_p2p_group_formation_timeout, wpa_s, NULL);
1165 }
1166
1167
1168 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1169 {
1170         struct wpa_supplicant *wpa_s = ctx;
1171         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1172                 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1173
1174         wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1175 }
1176
1177
1178 void wpas_dev_found(void *ctx, const u8 *addr,
1179                     const struct p2p_peer_info *info,
1180                     int new_device)
1181 {
1182         struct wpa_supplicant *wpa_s = ctx;
1183         char devtype[WPS_DEV_TYPE_BUFSIZE];
1184
1185         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1186                 " p2p_dev_addr=" MACSTR
1187                 " pri_dev_type=%s name='%s' config_methods=0x%x "
1188                 "dev_capab=0x%x group_capab=0x%x",
1189                 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1190                 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1191                                      sizeof(devtype)),
1192                 info->device_name, info->config_methods,
1193                 info->dev_capab, info->group_capab);
1194
1195         wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1196 }
1197
1198
1199 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1200 {
1201         struct wpa_supplicant *wpa_s = ctx;
1202
1203         wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1204 }
1205
1206
1207 static int wpas_start_listen(void *ctx, unsigned int freq,
1208                              unsigned int duration,
1209                              const struct wpabuf *probe_resp_ie)
1210 {
1211         struct wpa_supplicant *wpa_s = ctx;
1212
1213         wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1214
1215         if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1216                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1217                            "report received Probe Request frames");
1218                 return -1;
1219         }
1220
1221         wpa_s->pending_listen_freq = freq;
1222         wpa_s->pending_listen_duration = duration;
1223
1224         if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1225                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1226                            "to remain on channel (%u MHz) for Listen "
1227                            "state", freq);
1228                 wpa_s->pending_listen_freq = 0;
1229                 return -1;
1230         }
1231         wpa_s->off_channel_freq = 0;
1232         wpa_s->roc_waiting_drv_freq = freq;
1233
1234         return 0;
1235 }
1236
1237
1238 static void wpas_stop_listen(void *ctx)
1239 {
1240         struct wpa_supplicant *wpa_s = ctx;
1241         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1242                 wpa_drv_cancel_remain_on_channel(wpa_s);
1243                 wpa_s->off_channel_freq = 0;
1244                 wpa_s->roc_waiting_drv_freq = 0;
1245         }
1246         wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1247         wpa_drv_probe_req_report(wpa_s, 0);
1248 }
1249
1250
1251 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1252 {
1253         struct wpa_supplicant *wpa_s = ctx;
1254         return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1255 }
1256
1257
1258 static struct p2p_srv_bonjour *
1259 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1260                              const struct wpabuf *query)
1261 {
1262         struct p2p_srv_bonjour *bsrv;
1263         size_t len;
1264
1265         len = wpabuf_len(query);
1266         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1267                          struct p2p_srv_bonjour, list) {
1268                 if (len == wpabuf_len(bsrv->query) &&
1269                     os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1270                               len) == 0)
1271                         return bsrv;
1272         }
1273         return NULL;
1274 }
1275
1276
1277 static struct p2p_srv_upnp *
1278 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1279                           const char *service)
1280 {
1281         struct p2p_srv_upnp *usrv;
1282
1283         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1284                          struct p2p_srv_upnp, list) {
1285                 if (version == usrv->version &&
1286                     os_strcmp(service, usrv->service) == 0)
1287                         return usrv;
1288         }
1289         return NULL;
1290 }
1291
1292
1293 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1294                                         u8 srv_trans_id)
1295 {
1296         u8 *len_pos;
1297
1298         if (wpabuf_tailroom(resp) < 5)
1299                 return;
1300
1301         /* Length (to be filled) */
1302         len_pos = wpabuf_put(resp, 2);
1303         wpabuf_put_u8(resp, srv_proto);
1304         wpabuf_put_u8(resp, srv_trans_id);
1305         /* Status Code */
1306         wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1307         /* Response Data: empty */
1308         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1309 }
1310
1311
1312 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1313                                 struct wpabuf *resp, u8 srv_trans_id)
1314 {
1315         struct p2p_srv_bonjour *bsrv;
1316         u8 *len_pos;
1317
1318         wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1319
1320         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1321                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1322                 return;
1323         }
1324
1325         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1326                          struct p2p_srv_bonjour, list) {
1327                 if (wpabuf_tailroom(resp) <
1328                     5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1329                         return;
1330                 /* Length (to be filled) */
1331                 len_pos = wpabuf_put(resp, 2);
1332                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1333                 wpabuf_put_u8(resp, srv_trans_id);
1334                 /* Status Code */
1335                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1336                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1337                                   wpabuf_head(bsrv->resp),
1338                                   wpabuf_len(bsrv->resp));
1339                 /* Response Data */
1340                 wpabuf_put_buf(resp, bsrv->query); /* Key */
1341                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1342                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1343                              2);
1344         }
1345 }
1346
1347
1348 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1349                                 struct wpabuf *resp, u8 srv_trans_id,
1350                                 const u8 *query, size_t query_len)
1351 {
1352         struct p2p_srv_bonjour *bsrv;
1353         struct wpabuf buf;
1354         u8 *len_pos;
1355
1356         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1357                           query, query_len);
1358         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1359                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1360                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1361                                             srv_trans_id);
1362                 return;
1363         }
1364
1365         if (query_len == 0) {
1366                 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1367                 return;
1368         }
1369
1370         if (wpabuf_tailroom(resp) < 5)
1371                 return;
1372         /* Length (to be filled) */
1373         len_pos = wpabuf_put(resp, 2);
1374         wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1375         wpabuf_put_u8(resp, srv_trans_id);
1376
1377         wpabuf_set(&buf, query, query_len);
1378         bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1379         if (bsrv == NULL) {
1380                 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1381                            "available");
1382
1383                 /* Status Code */
1384                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1385                 /* Response Data: empty */
1386                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1387                              2);
1388                 return;
1389         }
1390
1391         /* Status Code */
1392         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1393         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1394                           wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1395
1396         if (wpabuf_tailroom(resp) >=
1397             wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1398                 /* Response Data */
1399                 wpabuf_put_buf(resp, bsrv->query); /* Key */
1400                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1401         }
1402         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1403 }
1404
1405
1406 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1407                              struct wpabuf *resp, u8 srv_trans_id)
1408 {
1409         struct p2p_srv_upnp *usrv;
1410         u8 *len_pos;
1411
1412         wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1413
1414         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1415                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1416                 return;
1417         }
1418
1419         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1420                          struct p2p_srv_upnp, list) {
1421                 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1422                         return;
1423
1424                 /* Length (to be filled) */
1425                 len_pos = wpabuf_put(resp, 2);
1426                 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1427                 wpabuf_put_u8(resp, srv_trans_id);
1428
1429                 /* Status Code */
1430                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1431                 /* Response Data */
1432                 wpabuf_put_u8(resp, usrv->version);
1433                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1434                            usrv->service);
1435                 wpabuf_put_str(resp, usrv->service);
1436                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1437                              2);
1438         }
1439 }
1440
1441
1442 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1443                              struct wpabuf *resp, u8 srv_trans_id,
1444                              const u8 *query, size_t query_len)
1445 {
1446         struct p2p_srv_upnp *usrv;
1447         u8 *len_pos;
1448         u8 version;
1449         char *str;
1450         int count = 0;
1451
1452         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1453                           query, query_len);
1454
1455         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1456                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1457                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1458                                             srv_trans_id);
1459                 return;
1460         }
1461
1462         if (query_len == 0) {
1463                 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1464                 return;
1465         }
1466
1467         if (wpabuf_tailroom(resp) < 5)
1468                 return;
1469
1470         /* Length (to be filled) */
1471         len_pos = wpabuf_put(resp, 2);
1472         wpabuf_put_u8(resp, P2P_SERV_UPNP);
1473         wpabuf_put_u8(resp, srv_trans_id);
1474
1475         version = query[0];
1476         str = os_malloc(query_len);
1477         if (str == NULL)
1478                 return;
1479         os_memcpy(str, query + 1, query_len - 1);
1480         str[query_len - 1] = '\0';
1481
1482         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1483                          struct p2p_srv_upnp, list) {
1484                 if (version != usrv->version)
1485                         continue;
1486
1487                 if (os_strcmp(str, "ssdp:all") != 0 &&
1488                     os_strstr(usrv->service, str) == NULL)
1489                         continue;
1490
1491                 if (wpabuf_tailroom(resp) < 2)
1492                         break;
1493                 if (count == 0) {
1494                         /* Status Code */
1495                         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1496                         /* Response Data */
1497                         wpabuf_put_u8(resp, version);
1498                 } else
1499                         wpabuf_put_u8(resp, ',');
1500
1501                 count++;
1502
1503                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1504                            usrv->service);
1505                 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1506                         break;
1507                 wpabuf_put_str(resp, usrv->service);
1508         }
1509         os_free(str);
1510
1511         if (count == 0) {
1512                 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1513                            "available");
1514                 /* Status Code */
1515                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1516                 /* Response Data: empty */
1517         }
1518
1519         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1520 }
1521
1522
1523 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1524                      u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1525 {
1526         struct wpa_supplicant *wpa_s = ctx;
1527         const u8 *pos = tlvs;
1528         const u8 *end = tlvs + tlvs_len;
1529         const u8 *tlv_end;
1530         u16 slen;
1531         struct wpabuf *resp;
1532         u8 srv_proto, srv_trans_id;
1533         size_t buf_len;
1534         char *buf;
1535
1536         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1537                     tlvs, tlvs_len);
1538         buf_len = 2 * tlvs_len + 1;
1539         buf = os_malloc(buf_len);
1540         if (buf) {
1541                 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1542                 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1543                              MACSTR " %u %u %s",
1544                              freq, MAC2STR(sa), dialog_token, update_indic,
1545                              buf);
1546                 os_free(buf);
1547         }
1548
1549         if (wpa_s->p2p_sd_over_ctrl_iface)
1550                 return; /* to be processed by an external program */
1551
1552         resp = wpabuf_alloc(10000);
1553         if (resp == NULL)
1554                 return;
1555
1556         while (pos + 1 < end) {
1557                 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1558                 slen = WPA_GET_LE16(pos);
1559                 pos += 2;
1560                 if (pos + slen > end || slen < 2) {
1561                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1562                                    "length");
1563                         wpabuf_free(resp);
1564                         return;
1565                 }
1566                 tlv_end = pos + slen;
1567
1568                 srv_proto = *pos++;
1569                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1570                            srv_proto);
1571                 srv_trans_id = *pos++;
1572                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1573                            srv_trans_id);
1574
1575                 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1576                             pos, tlv_end - pos);
1577
1578
1579                 if (wpa_s->force_long_sd) {
1580                         wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1581                                    "response");
1582                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1583                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1584                         goto done;
1585                 }
1586
1587                 switch (srv_proto) {
1588                 case P2P_SERV_ALL_SERVICES:
1589                         wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1590                                    "for all services");
1591                         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1592                             dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1593                                 wpa_printf(MSG_DEBUG, "P2P: No service "
1594                                            "discovery protocols available");
1595                                 wpas_sd_add_proto_not_avail(
1596                                         resp, P2P_SERV_ALL_SERVICES,
1597                                         srv_trans_id);
1598                                 break;
1599                         }
1600                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1601                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1602                         break;
1603                 case P2P_SERV_BONJOUR:
1604                         wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1605                                             pos, tlv_end - pos);
1606                         break;
1607                 case P2P_SERV_UPNP:
1608                         wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1609                                          pos, tlv_end - pos);
1610                         break;
1611                 default:
1612                         wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1613                                    "protocol %u", srv_proto);
1614                         wpas_sd_add_proto_not_avail(resp, srv_proto,
1615                                                     srv_trans_id);
1616                         break;
1617                 }
1618
1619                 pos = tlv_end;
1620         }
1621
1622 done:
1623         wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1624                                    update_indic, tlvs, tlvs_len);
1625
1626         wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1627
1628         wpabuf_free(resp);
1629 }
1630
1631
1632 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1633                       const u8 *tlvs, size_t tlvs_len)
1634 {
1635         struct wpa_supplicant *wpa_s = ctx;
1636         const u8 *pos = tlvs;
1637         const u8 *end = tlvs + tlvs_len;
1638         const u8 *tlv_end;
1639         u16 slen;
1640         size_t buf_len;
1641         char *buf;
1642
1643         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1644                     tlvs, tlvs_len);
1645         if (tlvs_len > 1500) {
1646                 /* TODO: better way for handling this */
1647                 wpa_msg_ctrl(wpa_s, MSG_INFO,
1648                              P2P_EVENT_SERV_DISC_RESP MACSTR
1649                              " %u <long response: %u bytes>",
1650                              MAC2STR(sa), update_indic,
1651                              (unsigned int) tlvs_len);
1652         } else {
1653                 buf_len = 2 * tlvs_len + 1;
1654                 buf = os_malloc(buf_len);
1655                 if (buf) {
1656                         wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1657                         wpa_msg_ctrl(wpa_s, MSG_INFO,
1658                                      P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1659                                      MAC2STR(sa), update_indic, buf);
1660                         os_free(buf);
1661                 }
1662         }
1663
1664         while (pos < end) {
1665                 u8 srv_proto, srv_trans_id, status;
1666
1667                 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1668                 slen = WPA_GET_LE16(pos);
1669                 pos += 2;
1670                 if (pos + slen > end || slen < 3) {
1671                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1672                                    "length");
1673                         return;
1674                 }
1675                 tlv_end = pos + slen;
1676
1677                 srv_proto = *pos++;
1678                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1679                            srv_proto);
1680                 srv_trans_id = *pos++;
1681                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1682                            srv_trans_id);
1683                 status = *pos++;
1684                 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1685                            status);
1686
1687                 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1688                             pos, tlv_end - pos);
1689
1690                 pos = tlv_end;
1691         }
1692
1693         wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1694 }
1695
1696
1697 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1698                            const struct wpabuf *tlvs)
1699 {
1700         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1701                 return (void *) wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1702         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1703                 return NULL;
1704         return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1705 }
1706
1707
1708 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1709                                 u8 version, const char *query)
1710 {
1711         struct wpabuf *tlvs;
1712         void *ret;
1713
1714         tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1715         if (tlvs == NULL)
1716                 return NULL;
1717         wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1718         wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1719         wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1720         wpabuf_put_u8(tlvs, version);
1721         wpabuf_put_str(tlvs, query);
1722         ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1723         wpabuf_free(tlvs);
1724         return ret;
1725 }
1726
1727
1728 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1729 {
1730         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1731                 return wpa_drv_p2p_sd_cancel_request(wpa_s, (u64) req);
1732         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1733                 return -1;
1734         return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1735 }
1736
1737
1738 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1739                           const u8 *dst, u8 dialog_token,
1740                           const struct wpabuf *resp_tlvs)
1741 {
1742         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1743                 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1744                                         resp_tlvs);
1745                 return;
1746         }
1747         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1748                 return;
1749         p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1750                         resp_tlvs);
1751 }
1752
1753
1754 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1755 {
1756         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1757                 wpa_drv_p2p_service_update(wpa_s);
1758                 return;
1759         }
1760         if (wpa_s->global->p2p)
1761                 p2p_sd_service_update(wpa_s->global->p2p);
1762 }
1763
1764
1765 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1766 {
1767         dl_list_del(&bsrv->list);
1768         wpabuf_free(bsrv->query);
1769         wpabuf_free(bsrv->resp);
1770         os_free(bsrv);
1771 }
1772
1773
1774 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1775 {
1776         dl_list_del(&usrv->list);
1777         os_free(usrv->service);
1778         os_free(usrv);
1779 }
1780
1781
1782 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1783 {
1784         struct p2p_srv_bonjour *bsrv, *bn;
1785         struct p2p_srv_upnp *usrv, *un;
1786
1787         dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1788                               struct p2p_srv_bonjour, list)
1789                 wpas_p2p_srv_bonjour_free(bsrv);
1790
1791         dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1792                               struct p2p_srv_upnp, list)
1793                 wpas_p2p_srv_upnp_free(usrv);
1794
1795         wpas_p2p_sd_service_update(wpa_s);
1796 }
1797
1798
1799 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1800                                  struct wpabuf *query, struct wpabuf *resp)
1801 {
1802         struct p2p_srv_bonjour *bsrv;
1803
1804         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1805         if (bsrv) {
1806                 wpabuf_free(query);
1807                 wpabuf_free(bsrv->resp);
1808                 bsrv->resp = resp;
1809                 return 0;
1810         }
1811
1812         bsrv = os_zalloc(sizeof(*bsrv));
1813         if (bsrv == NULL)
1814                 return -1;
1815         bsrv->query = query;
1816         bsrv->resp = resp;
1817         dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1818
1819         wpas_p2p_sd_service_update(wpa_s);
1820         return 0;
1821 }
1822
1823
1824 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1825                                  const struct wpabuf *query)
1826 {
1827         struct p2p_srv_bonjour *bsrv;
1828
1829         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1830         if (bsrv == NULL)
1831                 return -1;
1832         wpas_p2p_srv_bonjour_free(bsrv);
1833         wpas_p2p_sd_service_update(wpa_s);
1834         return 0;
1835 }
1836
1837
1838 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1839                               const char *service)
1840 {
1841         struct p2p_srv_upnp *usrv;
1842
1843         if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1844                 return 0; /* Already listed */
1845         usrv = os_zalloc(sizeof(*usrv));
1846         if (usrv == NULL)
1847                 return -1;
1848         usrv->version = version;
1849         usrv->service = os_strdup(service);
1850         if (usrv->service == NULL) {
1851                 os_free(usrv);
1852                 return -1;
1853         }
1854         dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1855
1856         wpas_p2p_sd_service_update(wpa_s);
1857         return 0;
1858 }
1859
1860
1861 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1862                               const char *service)
1863 {
1864         struct p2p_srv_upnp *usrv;
1865
1866         usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1867         if (usrv == NULL)
1868                 return -1;
1869         wpas_p2p_srv_upnp_free(usrv);
1870         wpas_p2p_sd_service_update(wpa_s);
1871         return 0;
1872 }
1873
1874
1875 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1876                                          const u8 *peer, const char *params,
1877                                          unsigned int generated_pin)
1878 {
1879         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1880                 MAC2STR(peer), generated_pin, params);
1881 }
1882
1883
1884 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1885                                         const u8 *peer, const char *params)
1886 {
1887         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1888                 MAC2STR(peer), params);
1889 }
1890
1891
1892 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1893                         const u8 *dev_addr, const u8 *pri_dev_type,
1894                         const char *dev_name, u16 supp_config_methods,
1895                         u8 dev_capab, u8 group_capab)
1896 {
1897         struct wpa_supplicant *wpa_s = ctx;
1898         char devtype[WPS_DEV_TYPE_BUFSIZE];
1899         char params[200];
1900         u8 empty_dev_type[8];
1901         unsigned int generated_pin = 0;
1902
1903         if (pri_dev_type == NULL) {
1904                 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1905                 pri_dev_type = empty_dev_type;
1906         }
1907         os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1908                     " pri_dev_type=%s name='%s' config_methods=0x%x "
1909                     "dev_capab=0x%x group_capab=0x%x",
1910                     MAC2STR(dev_addr),
1911                     wps_dev_type_bin2str(pri_dev_type, devtype,
1912                                          sizeof(devtype)),
1913                     dev_name, supp_config_methods, dev_capab, group_capab);
1914         params[sizeof(params) - 1] = '\0';
1915
1916         if (config_methods & WPS_CONFIG_DISPLAY) {
1917                 generated_pin = wps_generate_pin();
1918                 wpas_prov_disc_local_display(wpa_s, peer, params,
1919                                              generated_pin);
1920         } else if (config_methods & WPS_CONFIG_KEYPAD)
1921                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1922         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1923                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1924                         "%s", MAC2STR(peer), params);
1925
1926         wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
1927                                             P2P_PROV_DISC_SUCCESS,
1928                                             config_methods, generated_pin);
1929 }
1930
1931
1932 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1933 {
1934         struct wpa_supplicant *wpa_s = ctx;
1935         unsigned int generated_pin = 0;
1936
1937         if (config_methods & WPS_CONFIG_DISPLAY)
1938                 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1939         else if (config_methods & WPS_CONFIG_KEYPAD) {
1940                 generated_pin = wps_generate_pin();
1941                 wpas_prov_disc_local_display(wpa_s, peer, "", generated_pin);
1942         } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1943                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1944                         MAC2STR(peer));
1945
1946         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1947                                             P2P_PROV_DISC_SUCCESS,
1948                                             config_methods, generated_pin);
1949
1950         if (wpa_s->pending_pd_before_join &&
1951             (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1952              os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
1953                 wpa_s->pending_pd_before_join = 0;
1954                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
1955                            "join-existing-group operation");
1956                 wpas_p2p_join_start(wpa_s);
1957         }
1958 }
1959
1960
1961 void wpas_prov_disc_fail(void *ctx, const u8 *peer,
1962                          enum p2p_prov_disc_status status)
1963 {
1964         struct wpa_supplicant *wpa_s = ctx;
1965
1966         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
1967                                             status, 0, 0);
1968 }
1969
1970
1971 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1972                                   const u8 *go_dev_addr, const u8 *ssid,
1973                                   size_t ssid_len, int *go, u8 *group_bssid,
1974                                   int *force_freq, int persistent_group)
1975 {
1976         struct wpa_supplicant *wpa_s = ctx;
1977         struct wpa_ssid *s;
1978         u8 cur_bssid[ETH_ALEN];
1979         int res;
1980         struct wpa_supplicant *grp;
1981
1982         if (!persistent_group) {
1983                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1984                            " to join an active group", MAC2STR(sa));
1985                 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
1986                     (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
1987                      == 0 ||
1988                      os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
1989                         wpa_printf(MSG_DEBUG, "P2P: Accept previously "
1990                                    "authorized invitation");
1991                         goto accept_inv;
1992                 }
1993                 /*
1994                  * Do not accept the invitation automatically; notify user and
1995                  * request approval.
1996                  */
1997                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1998         }
1999
2000         grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2001         if (grp) {
2002                 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2003                            "running persistent group");
2004                 if (*go)
2005                         os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2006                 goto accept_inv;
2007         }
2008
2009         if (!wpa_s->conf->persistent_reconnect)
2010                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2011
2012         for (s = wpa_s->conf->ssid; s; s = s->next) {
2013                 if (s->disabled == 2 &&
2014                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2015                     s->ssid_len == ssid_len &&
2016                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2017                         break;
2018         }
2019
2020         if (!s) {
2021                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2022                            " requested reinvocation of an unknown group",
2023                            MAC2STR(sa));
2024                 return P2P_SC_FAIL_UNKNOWN_GROUP;
2025         }
2026
2027         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2028                 *go = 1;
2029                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2030                         wpa_printf(MSG_DEBUG, "P2P: The only available "
2031                                    "interface is already in use - reject "
2032                                    "invitation");
2033                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2034                 }
2035                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2036         } else if (s->mode == WPAS_MODE_P2P_GO) {
2037                 *go = 1;
2038                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2039                 {
2040                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2041                                    "interface address for the group");
2042                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2043                 }
2044                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2045                           ETH_ALEN);
2046         }
2047
2048 accept_inv:
2049         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2050             wpa_s->assoc_freq) {
2051                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2052                            "the channel we are already using");
2053                 *force_freq = wpa_s->assoc_freq;
2054         }
2055
2056         res = wpa_drv_shared_freq(wpa_s);
2057         if (res > 0) {
2058                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2059                            "with the channel we are already using on a "
2060                            "shared interface");
2061                 *force_freq = res;
2062         }
2063
2064         return P2P_SC_SUCCESS;
2065 }
2066
2067
2068 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2069                                      const u8 *ssid, size_t ssid_len,
2070                                      const u8 *go_dev_addr, u8 status,
2071                                      int op_freq)
2072 {
2073         struct wpa_supplicant *wpa_s = ctx;
2074         struct wpa_ssid *s;
2075
2076         for (s = wpa_s->conf->ssid; s; s = s->next) {
2077                 if (s->disabled == 2 &&
2078                     s->ssid_len == ssid_len &&
2079                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
2080                         break;
2081         }
2082
2083         if (status == P2P_SC_SUCCESS) {
2084                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2085                            " was accepted; op_freq=%d MHz",
2086                            MAC2STR(sa), op_freq);
2087                 if (s) {
2088                         wpas_p2p_group_add_persistent(
2089                                 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
2090                 } else if (bssid) {
2091                         wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2092                                       wpa_s->p2p_wps_method);
2093                 }
2094                 return;
2095         }
2096
2097         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2098                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2099                            " was rejected (status %u)", MAC2STR(sa), status);
2100                 return;
2101         }
2102
2103         if (!s) {
2104                 if (bssid) {
2105                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2106                                 "sa=" MACSTR " go_dev_addr=" MACSTR
2107                                 " bssid=" MACSTR " unknown-network",
2108                                 MAC2STR(sa), MAC2STR(go_dev_addr),
2109                                 MAC2STR(bssid));
2110                 } else {
2111                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2112                                 "sa=" MACSTR " go_dev_addr=" MACSTR
2113                                 " unknown-network",
2114                                 MAC2STR(sa), MAC2STR(go_dev_addr));
2115                 }
2116                 return;
2117         }
2118
2119         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2120                 " persistent=%d", MAC2STR(sa), s->id);
2121 }
2122
2123
2124 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2125 {
2126         struct wpa_supplicant *wpa_s = ctx;
2127         struct wpa_ssid *ssid;
2128
2129         if (bssid) {
2130                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2131                         "status=%d " MACSTR,
2132                         status, MAC2STR(bssid));
2133         } else {
2134                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2135                         "status=%d ", status);
2136         }
2137         wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2138
2139         if (wpa_s->pending_invite_ssid_id == -1)
2140                 return; /* Invitation to active group */
2141
2142         if (status != P2P_SC_SUCCESS) {
2143                 wpas_p2p_remove_pending_group_interface(wpa_s);
2144                 return;
2145         }
2146
2147         ssid = wpa_config_get_network(wpa_s->conf,
2148                                       wpa_s->pending_invite_ssid_id);
2149         if (ssid == NULL) {
2150                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2151                            "data matching with invitation");
2152                 return;
2153         }
2154
2155         wpas_p2p_group_add_persistent(wpa_s, ssid,
2156                                       ssid->mode == WPAS_MODE_P2P_GO, 0);
2157 }
2158
2159
2160 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2161                                      struct p2p_channels *chan)
2162 {
2163         int i, cla = 0;
2164
2165         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2166                    "band");
2167
2168         /* Operating class 81 - 2.4 GHz band channels 1..13 */
2169         chan->reg_class[cla].reg_class = 81;
2170         chan->reg_class[cla].channels = 11;
2171         for (i = 0; i < 11; i++)
2172                 chan->reg_class[cla].channel[i] = i + 1;
2173         cla++;
2174
2175         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2176                    "band");
2177
2178         /* Operating class 115 - 5 GHz, channels 36-48 */
2179         chan->reg_class[cla].reg_class = 115;
2180         chan->reg_class[cla].channels = 4;
2181         chan->reg_class[cla].channel[0] = 36;
2182         chan->reg_class[cla].channel[1] = 40;
2183         chan->reg_class[cla].channel[2] = 44;
2184         chan->reg_class[cla].channel[3] = 48;
2185         cla++;
2186
2187         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2188                    "band");
2189
2190         /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2191         chan->reg_class[cla].reg_class = 124;
2192         chan->reg_class[cla].channels = 4;
2193         chan->reg_class[cla].channel[0] = 149;
2194         chan->reg_class[cla].channel[1] = 153;
2195         chan->reg_class[cla].channel[2] = 157;
2196         chan->reg_class[cla].channel[3] = 161;
2197         cla++;
2198
2199         chan->reg_classes = cla;
2200         return 0;
2201 }
2202
2203
2204 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2205                                           u16 num_modes,
2206                                           enum hostapd_hw_mode mode)
2207 {
2208         u16 i;
2209
2210         for (i = 0; i < num_modes; i++) {
2211                 if (modes[i].mode == mode)
2212                         return &modes[i];
2213         }
2214
2215         return NULL;
2216 }
2217
2218
2219 static int has_channel(struct hostapd_hw_modes *mode, u8 chan, int *flags)
2220 {
2221         int i;
2222
2223         for (i = 0; i < mode->num_channels; i++) {
2224                 if (mode->channels[i].chan == chan) {
2225                         if (flags)
2226                                 *flags = mode->channels[i].flag;
2227                         return !(mode->channels[i].flag &
2228                                  (HOSTAPD_CHAN_DISABLED |
2229                                   HOSTAPD_CHAN_PASSIVE_SCAN |
2230                                   HOSTAPD_CHAN_NO_IBSS |
2231                                   HOSTAPD_CHAN_RADAR));
2232                 }
2233         }
2234
2235         return 0;
2236 }
2237
2238
2239 struct p2p_oper_class_map {
2240         enum hostapd_hw_mode mode;
2241         u8 op_class;
2242         u8 min_chan;
2243         u8 max_chan;
2244         u8 inc;
2245         enum { BW20, BW40PLUS, BW40MINUS } bw;
2246 };
2247
2248 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2249                                    struct p2p_channels *chan)
2250 {
2251         struct hostapd_hw_modes *modes, *mode;
2252         u16 num_modes, flags;
2253         int cla, op;
2254         struct p2p_oper_class_map op_class[] = {
2255                 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2256                 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2257 #if 0 /* Do not enable HT40 on 2 GHz for now */
2258                 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2259                 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2260 #endif
2261                 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2262                 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2263                 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2264                 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2265                 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2266                 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2267                 { -1, 0, 0, 0, 0, BW20 }
2268         };
2269
2270         modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
2271         if (modes == NULL) {
2272                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2273                            "of all supported channels; assume dualband "
2274                            "support");
2275                 return wpas_p2p_default_channels(wpa_s, chan);
2276         }
2277
2278         cla = 0;
2279
2280         for (op = 0; op_class[op].op_class; op++) {
2281                 struct p2p_oper_class_map *o = &op_class[op];
2282                 u8 ch;
2283                 struct p2p_reg_class *reg = NULL;
2284
2285                 mode = get_mode(modes, num_modes, o->mode);
2286                 if (mode == NULL)
2287                         continue;
2288                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2289                         int flag;
2290                         if (!has_channel(mode, ch, &flag))
2291                                 continue;
2292                         if (o->bw == BW40MINUS &&
2293                             (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2294                              !has_channel(mode, ch - 4, NULL)))
2295                                 continue;
2296                         if (o->bw == BW40PLUS &&
2297                             (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2298                              !has_channel(mode, ch + 4, NULL)))
2299                                 continue;
2300                         if (reg == NULL) {
2301                                 wpa_printf(MSG_DEBUG, "P2P: Add operating "
2302                                            "class %u", o->op_class);
2303                                 reg = &chan->reg_class[cla];
2304                                 cla++;
2305                                 reg->reg_class = o->op_class;
2306                         }
2307                         reg->channel[reg->channels] = ch;
2308                         reg->channels++;
2309                 }
2310                 if (reg) {
2311                         wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2312                                     reg->channel, reg->channels);
2313                 }
2314         }
2315
2316         chan->reg_classes = cla;
2317
2318         ieee80211_sta_free_hw_features(modes, num_modes);
2319
2320         return 0;
2321 }
2322
2323
2324 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2325                         size_t buf_len)
2326 {
2327         struct wpa_supplicant *wpa_s = ctx;
2328
2329         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2330                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2331                         break;
2332         }
2333         if (wpa_s == NULL)
2334                 return -1;
2335
2336         return wpa_drv_get_noa(wpa_s, buf, buf_len);
2337 }
2338
2339
2340 /**
2341  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2342  * @global: Pointer to global data from wpa_supplicant_init()
2343  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2344  * Returns: 0 on success, -1 on failure
2345  */
2346 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2347 {
2348         struct p2p_config p2p;
2349         unsigned int r;
2350         int i;
2351
2352         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2353                 return 0;
2354
2355 #ifdef CONFIG_CLIENT_MLME
2356         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
2357                 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
2358                 wpa_s->mlme.public_action_cb_ctx = wpa_s;
2359         }
2360 #endif /* CONFIG_CLIENT_MLME */
2361
2362         if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
2363                 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
2364                 /* Continue anyway; this is not really a fatal error */
2365         }
2366
2367         if (global->p2p)
2368                 return 0;
2369
2370         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2371                 struct p2p_params params;
2372
2373                 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2374                 os_memset(&params, 0, sizeof(params));
2375                 params.dev_name = wpa_s->conf->device_name;
2376                 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2377                           WPS_DEV_TYPE_LEN);
2378                 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2379                 os_memcpy(params.sec_dev_type,
2380                           wpa_s->conf->sec_device_type,
2381                           params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2382
2383                 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2384                         return -1;
2385
2386                 return 0;
2387         }
2388
2389         os_memset(&p2p, 0, sizeof(p2p));
2390         p2p.msg_ctx = wpa_s;
2391         p2p.cb_ctx = wpa_s;
2392         p2p.p2p_scan = wpas_p2p_scan;
2393         p2p.send_action = wpas_send_action;
2394         p2p.send_action_done = wpas_send_action_done;
2395         p2p.go_neg_completed = wpas_go_neg_completed;
2396         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2397         p2p.dev_found = wpas_dev_found;
2398         p2p.dev_lost = wpas_dev_lost;
2399         p2p.start_listen = wpas_start_listen;
2400         p2p.stop_listen = wpas_stop_listen;
2401         p2p.send_probe_resp = wpas_send_probe_resp;
2402         p2p.sd_request = wpas_sd_request;
2403         p2p.sd_response = wpas_sd_response;
2404         p2p.prov_disc_req = wpas_prov_disc_req;
2405         p2p.prov_disc_resp = wpas_prov_disc_resp;
2406         p2p.prov_disc_fail = wpas_prov_disc_fail;
2407         p2p.invitation_process = wpas_invitation_process;
2408         p2p.invitation_received = wpas_invitation_received;
2409         p2p.invitation_result = wpas_invitation_result;
2410         p2p.get_noa = wpas_get_noa;
2411
2412         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2413         os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2414         p2p.dev_name = wpa_s->conf->device_name;
2415         p2p.manufacturer = wpa_s->conf->manufacturer;
2416         p2p.model_name = wpa_s->conf->model_name;
2417         p2p.model_number = wpa_s->conf->model_number;
2418         p2p.serial_number = wpa_s->conf->serial_number;
2419         if (wpa_s->wps) {
2420                 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2421                 p2p.config_methods = wpa_s->wps->config_methods;
2422         }
2423
2424         if (wpa_s->conf->p2p_listen_reg_class &&
2425             wpa_s->conf->p2p_listen_channel) {
2426                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2427                 p2p.channel = wpa_s->conf->p2p_listen_channel;
2428         } else {
2429                 p2p.reg_class = 81;
2430                 /*
2431                  * Pick one of the social channels randomly as the listen
2432                  * channel.
2433                  */
2434                 os_get_random((u8 *) &r, sizeof(r));
2435                 p2p.channel = 1 + (r % 3) * 5;
2436         }
2437         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2438
2439         if (wpa_s->conf->p2p_oper_reg_class &&
2440             wpa_s->conf->p2p_oper_channel) {
2441                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2442                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2443                 p2p.cfg_op_channel = 1;
2444                 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2445                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
2446
2447         } else {
2448                 p2p.op_reg_class = 81;
2449                 /*
2450                  * Use random operation channel from (1, 6, 11) if no other
2451                  * preference is indicated.
2452                  */
2453                 os_get_random((u8 *) &r, sizeof(r));
2454                 p2p.op_channel = 1 + (r % 3) * 5;
2455                 p2p.cfg_op_channel = 0;
2456                 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2457                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
2458         }
2459         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2460                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2461                 p2p.country[2] = 0x04;
2462         } else
2463                 os_memcpy(p2p.country, "XX\x04", 3);
2464
2465         if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2466                 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2467                            "channel list");
2468                 return -1;
2469         }
2470
2471         os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2472                   WPS_DEV_TYPE_LEN);
2473
2474         p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2475         os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2476                   p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2477
2478         p2p.concurrent_operations = !!(wpa_s->drv_flags &
2479                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2480
2481         p2p.max_peers = 100;
2482
2483         if (wpa_s->conf->p2p_ssid_postfix) {
2484                 p2p.ssid_postfix_len =
2485                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
2486                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2487                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2488                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2489                           p2p.ssid_postfix_len);
2490         }
2491
2492         p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2493
2494         global->p2p = p2p_init(&p2p);
2495         if (global->p2p == NULL)
2496                 return -1;
2497
2498         for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2499                 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2500                         continue;
2501                 p2p_add_wps_vendor_extension(
2502                         global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2503         }
2504
2505         return 0;
2506 }
2507
2508
2509 /**
2510  * wpas_p2p_deinit - Deinitialize per-interface P2P data
2511  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2512  *
2513  * This function deinitialize per-interface P2P data.
2514  */
2515 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2516 {
2517         if (wpa_s->driver && wpa_s->drv_priv)
2518                 wpa_drv_probe_req_report(wpa_s, 0);
2519         os_free(wpa_s->go_params);
2520         wpa_s->go_params = NULL;
2521         wpabuf_free(wpa_s->pending_action_tx);
2522         wpa_s->pending_action_tx = NULL;
2523         eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2524         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2525         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2526         wpa_s->p2p_long_listen = 0;
2527         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2528         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2529         wpas_p2p_remove_pending_group_interface(wpa_s);
2530
2531         /* TODO: remove group interface from the driver if this wpa_s instance
2532          * is on top of a P2P group interface */
2533 }
2534
2535
2536 /**
2537  * wpas_p2p_deinit_global - Deinitialize global P2P module
2538  * @global: Pointer to global data from wpa_supplicant_init()
2539  *
2540  * This function deinitializes the global (per device) P2P module.
2541  */
2542 void wpas_p2p_deinit_global(struct wpa_global *global)
2543 {
2544         struct wpa_supplicant *wpa_s, *tmp;
2545         char *ifname;
2546
2547         if (global->p2p == NULL)
2548                 return;
2549
2550         /* Remove remaining P2P group interfaces */
2551         wpa_s = global->ifaces;
2552         if (wpa_s)
2553                 wpas_p2p_service_flush(wpa_s);
2554         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2555                 wpa_s = wpa_s->next;
2556         while (wpa_s) {
2557                 enum wpa_driver_if_type type;
2558                 tmp = global->ifaces;
2559                 while (tmp &&
2560                        (tmp == wpa_s ||
2561                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2562                         tmp = tmp->next;
2563                 }
2564                 if (tmp == NULL)
2565                         break;
2566                 ifname = os_strdup(tmp->ifname);
2567                 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2568                 wpa_supplicant_remove_iface(global, tmp);
2569                 if (ifname)
2570                         wpa_drv_if_remove(wpa_s, type, ifname);
2571                 os_free(ifname);
2572         }
2573
2574         /*
2575          * Deinit GO data on any possibly remaining interface (if main
2576          * interface is used as GO).
2577          */
2578         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2579                 if (wpa_s->ap_iface)
2580                         wpas_p2p_group_deinit(wpa_s);
2581         }
2582
2583         p2p_deinit(global->p2p);
2584         global->p2p = NULL;
2585 }
2586
2587
2588 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2589 {
2590         if (wpa_s->drv_flags &
2591             (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2592              WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2593                 return 1; /* P2P group requires a new interface in every case
2594                            */
2595         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2596                 return 0; /* driver does not support concurrent operations */
2597         if (wpa_s->global->ifaces->next)
2598                 return 1; /* more that one interface already in use */
2599         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2600                 return 1; /* this interface is already in use */
2601         return 0;
2602 }
2603
2604
2605 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2606                                  const u8 *peer_addr,
2607                                  enum p2p_wps_method wps_method,
2608                                  int go_intent, const u8 *own_interface_addr,
2609                                  unsigned int force_freq, int persistent_group)
2610 {
2611         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2612                 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2613                                            go_intent, own_interface_addr,
2614                                            force_freq, persistent_group);
2615         }
2616
2617         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2618                            go_intent, own_interface_addr, force_freq,
2619                            persistent_group);
2620 }
2621
2622
2623 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2624                                 const u8 *peer_addr,
2625                                 enum p2p_wps_method wps_method,
2626                                 int go_intent, const u8 *own_interface_addr,
2627                                 unsigned int force_freq, int persistent_group)
2628 {
2629         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2630                 return -1;
2631
2632         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2633                              go_intent, own_interface_addr, force_freq,
2634                              persistent_group);
2635 }
2636
2637
2638 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2639 {
2640         wpa_s->p2p_join_scan_count++;
2641         wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2642                    wpa_s->p2p_join_scan_count);
2643         if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2644                 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2645                            " for join operationg - stop join attempt",
2646                            MAC2STR(wpa_s->pending_join_iface_addr));
2647                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2648                 wpa_msg(wpa_s->parent, MSG_INFO,
2649                         P2P_EVENT_GROUP_FORMATION_FAILURE);
2650         }
2651 }
2652
2653
2654 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
2655                                    struct wpa_scan_results *scan_res)
2656 {
2657         struct wpa_bss *bss;
2658         int freq;
2659         u8 iface_addr[ETH_ALEN];
2660
2661         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2662
2663         if (wpa_s->global->p2p_disabled)
2664                 return;
2665
2666         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for join",
2667                    scan_res ? (int) scan_res->num : -1);
2668
2669         if (scan_res)
2670                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
2671
2672         freq = p2p_get_oper_freq(wpa_s->global->p2p,
2673                                  wpa_s->pending_join_iface_addr);
2674         if (freq < 0 &&
2675             p2p_get_interface_addr(wpa_s->global->p2p,
2676                                    wpa_s->pending_join_dev_addr,
2677                                    iface_addr) == 0 &&
2678             os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
2679         {
2680                 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
2681                            "address for join from " MACSTR " to " MACSTR
2682                            " based on newly discovered P2P peer entry",
2683                            MAC2STR(wpa_s->pending_join_iface_addr),
2684                            MAC2STR(iface_addr));
2685                 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
2686                           ETH_ALEN);
2687
2688                 freq = p2p_get_oper_freq(wpa_s->global->p2p,
2689                                          wpa_s->pending_join_iface_addr);
2690         }
2691         if (freq >= 0) {
2692                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2693                            "from P2P peer table: %d MHz", freq);
2694         }
2695         bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
2696         if (bss) {
2697                 freq = bss->freq;
2698                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
2699                            "from BSS table: %d MHz", freq);
2700         }
2701         if (freq > 0) {
2702                 u16 method;
2703
2704                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2705                            "prior to joining an existing group (GO " MACSTR
2706                            " freq=%u MHz)",
2707                            MAC2STR(wpa_s->pending_join_dev_addr), freq);
2708                 wpa_s->pending_pd_before_join = 1;
2709
2710                 switch (wpa_s->pending_join_wps_method) {
2711                 case WPS_PIN_LABEL:
2712                 case WPS_PIN_DISPLAY:
2713                         method = WPS_CONFIG_KEYPAD;
2714                         break;
2715                 case WPS_PIN_KEYPAD:
2716                         method = WPS_CONFIG_DISPLAY;
2717                         break;
2718                 case WPS_PBC:
2719                         method = WPS_CONFIG_PUSHBUTTON;
2720                         break;
2721                 default:
2722                         method = 0;
2723                         break;
2724                 }
2725
2726                 if (p2p_prov_disc_req(wpa_s->global->p2p,
2727                                       wpa_s->pending_join_dev_addr, method, 1)
2728                     < 0) {
2729                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2730                                    "Discovery Request before joining an "
2731                                    "existing group");
2732                         wpa_s->pending_pd_before_join = 0;
2733                         goto start;
2734                 }
2735
2736                 /*
2737                  * Actual join operation will be started from the Action frame
2738                  * TX status callback.
2739                  */
2740                 return;
2741         }
2742
2743         wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
2744         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2745         eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2746         wpas_p2p_check_join_scan_limit(wpa_s);
2747         return;
2748
2749 start:
2750         /* Start join operation immediately */
2751         wpas_p2p_join_start(wpa_s);
2752 }
2753
2754
2755 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
2756 {
2757         struct wpa_supplicant *wpa_s = eloop_ctx;
2758         int ret;
2759         struct wpa_driver_scan_params params;
2760         struct wpabuf *wps_ie, *ies;
2761
2762         os_memset(&params, 0, sizeof(params));
2763
2764         /* P2P Wildcard SSID */
2765         params.num_ssids = 1;
2766         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2767         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2768
2769         wpa_s->wps->dev.p2p = 1;
2770         wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2771                                         WPS_REQ_ENROLLEE, 0, NULL);
2772         if (wps_ie == NULL) {
2773                 wpas_p2p_scan_res_join(wpa_s, NULL);
2774                 return;
2775         }
2776
2777         ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
2778         if (ies == NULL) {
2779                 wpabuf_free(wps_ie);
2780                 wpas_p2p_scan_res_join(wpa_s, NULL);
2781                 return;
2782         }
2783         wpabuf_put_buf(ies, wps_ie);
2784         wpabuf_free(wps_ie);
2785
2786         p2p_scan_ie(wpa_s->global->p2p, ies);
2787
2788         params.extra_ies = wpabuf_head(ies);
2789         params.extra_ies_len = wpabuf_len(ies);
2790
2791         /*
2792          * Run a scan to update BSS table and start Provision Discovery once
2793          * the new scan results become available.
2794          */
2795         wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
2796         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
2797                 ret = ieee80211_sta_req_scan(wpa_s, &params);
2798         else
2799                 ret = wpa_drv_scan(wpa_s, &params);
2800
2801         wpabuf_free(ies);
2802
2803         if (ret) {
2804                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
2805                            "try again later");
2806                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2807                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
2808                 wpas_p2p_check_join_scan_limit(wpa_s);
2809         }
2810 }
2811
2812
2813 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2814                          const u8 *dev_addr, enum p2p_wps_method wps_method)
2815 {
2816         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2817                    MACSTR " dev " MACSTR ")",
2818                    MAC2STR(iface_addr), MAC2STR(dev_addr));
2819
2820         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2821         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2822         wpa_s->pending_join_wps_method = wps_method;
2823
2824         /* Make sure we are not running find during connection establishment */
2825         wpas_p2p_stop_find(wpa_s);
2826
2827         wpa_s->p2p_join_scan_count = 0;
2828         wpas_p2p_join_scan(wpa_s, NULL);
2829         return 0;
2830 }
2831
2832
2833 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2834 {
2835         struct wpa_supplicant *group;
2836         struct p2p_go_neg_results res;
2837
2838         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2839         if (group == NULL)
2840                 return -1;
2841         if (group != wpa_s) {
2842                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2843                           sizeof(group->p2p_pin));
2844                 group->p2p_wps_method = wpa_s->p2p_wps_method;
2845         }
2846
2847         group->p2p_in_provisioning = 1;
2848
2849         os_memset(&res, 0, sizeof(res));
2850         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2851                   ETH_ALEN);
2852         res.wps_method = wpa_s->pending_join_wps_method;
2853         wpas_start_wps_enrollee(group, &res);
2854
2855         /*
2856          * Allow a longer timeout for join-a-running-group than normal 15
2857          * second group formation timeout since the GO may not have authorized
2858          * our connection yet.
2859          */
2860         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2861         eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
2862                                wpa_s, NULL);
2863
2864         return 0;
2865 }
2866
2867
2868 /**
2869  * wpas_p2p_connect - Request P2P Group Formation to be started
2870  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2871  * @peer_addr: Address of the peer P2P Device
2872  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2873  * @persistent_group: Whether to create a persistent group
2874  * @join: Whether to join an existing group (as a client) instead of starting
2875  *      Group Owner negotiation; @peer_addr is BSSID in that case
2876  * @auth: Whether to only authorize the connection instead of doing that and
2877  *      initiating Group Owner negotiation
2878  * @go_intent: GO Intent or -1 to use default
2879  * @freq: Frequency for the group or 0 for auto-selection
2880  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
2881  *      failure, -2 on failure due to channel not currently available,
2882  *      -3 if forced channel is not supported
2883  */
2884 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2885                      const char *pin, enum p2p_wps_method wps_method,
2886                      int persistent_group, int join, int auth, int go_intent,
2887                      int freq)
2888 {
2889         int force_freq = 0, oper_freq = 0;
2890         u8 bssid[ETH_ALEN];
2891         int ret = 0;
2892         enum wpa_driver_if_type iftype;
2893
2894         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2895                 return -1;
2896
2897         if (go_intent < 0)
2898                 go_intent = wpa_s->conf->p2p_go_intent;
2899
2900         if (!auth)
2901                 wpa_s->p2p_long_listen = 0;
2902
2903         wpa_s->p2p_wps_method = wps_method;
2904
2905         if (pin)
2906                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2907         else if (wps_method == WPS_PIN_DISPLAY) {
2908                 ret = wps_generate_pin();
2909                 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2910                             ret);
2911                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2912                            wpa_s->p2p_pin);
2913         } else
2914                 wpa_s->p2p_pin[0] = '\0';
2915
2916         if (join) {
2917                 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
2918                 if (auth) {
2919                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
2920                                    "connect a running group from " MACSTR,
2921                                    MAC2STR(peer_addr));
2922                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
2923                         return ret;
2924                 }
2925                 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
2926                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2927                                            iface_addr) < 0) {
2928                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2929                         p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
2930                                          dev_addr);
2931                 }
2932                 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method) <
2933                     0)
2934                         return -1;
2935                 return ret;
2936         }
2937
2938         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2939             wpa_s->assoc_freq)
2940                 oper_freq = wpa_s->assoc_freq;
2941         else {
2942                 oper_freq = wpa_drv_shared_freq(wpa_s);
2943                 if (oper_freq < 0)
2944                         oper_freq = 0;
2945         }
2946
2947         if (freq > 0) {
2948                 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
2949                         wpa_printf(MSG_DEBUG, "P2P: The forced channel "
2950                                    "(%u MHz) is not supported for P2P uses",
2951                                    freq);
2952                         return -3;
2953                 }
2954
2955                 if (oper_freq > 0 && freq != oper_freq &&
2956                     !(wpa_s->drv_flags &
2957                       WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2958                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2959                                    "on %u MHz while connected on another "
2960                                    "channel (%u MHz)", freq, oper_freq);
2961                         return -2;
2962                 }
2963                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2964                            "requested channel (%u MHz)", freq);
2965                 force_freq = freq;
2966         } else if (oper_freq > 0 &&
2967                    !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
2968                 if (!(wpa_s->drv_flags &
2969                       WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
2970                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
2971                                    "while connected on non-P2P supported "
2972                                    "channel (%u MHz)", oper_freq);
2973                         return -2;
2974                 }
2975                 wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
2976                            "(%u MHz) not available for P2P - try to use "
2977                            "another channel", oper_freq);
2978                 force_freq = 0;
2979         } else if (oper_freq > 0) {
2980                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2981                            "channel we are already using (%u MHz) on another "
2982                            "interface", oper_freq);
2983                 force_freq = oper_freq;
2984         }
2985
2986         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2987
2988         if (!wpa_s->create_p2p_iface) {
2989                 if (auth) {
2990                         if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2991                                                  go_intent, wpa_s->own_addr,
2992                                                  force_freq, persistent_group)
2993                             < 0)
2994                                 return -1;
2995                         return ret;
2996                 }
2997                 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2998                                           go_intent, wpa_s->own_addr,
2999                                           force_freq, persistent_group) < 0)
3000                         return -1;
3001                 return ret;
3002         }
3003
3004         /* Prepare to add a new interface for the group */
3005         iftype = WPA_IF_P2P_GROUP;
3006         if (join)
3007                 iftype = WPA_IF_P2P_CLIENT;
3008         else if (go_intent == 15)
3009                 iftype = WPA_IF_P2P_GO;
3010         if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3011                 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3012                            "interface for the group");
3013                 return -1;
3014         }
3015
3016         if (auth) {
3017                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3018                                          go_intent,
3019                                          wpa_s->pending_interface_addr,
3020                                          force_freq, persistent_group) < 0)
3021                         return -1;
3022                 return ret;
3023         }
3024         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
3025                                   wpa_s->pending_interface_addr,
3026                                   force_freq, persistent_group) < 0) {
3027                 wpas_p2p_remove_pending_group_interface(wpa_s);
3028                 return -1;
3029         }
3030         return ret;
3031 }
3032
3033
3034 /**
3035  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3036  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3037  * @freq: Frequency of the channel in MHz
3038  * @duration: Duration of the stay on the channel in milliseconds
3039  *
3040  * This callback is called when the driver indicates that it has started the
3041  * requested remain-on-channel duration.
3042  */
3043 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3044                                    unsigned int freq, unsigned int duration)
3045 {
3046         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3047                 return;
3048         wpa_s->roc_waiting_drv_freq = 0;
3049         wpa_s->off_channel_freq = freq;
3050         wpas_send_action_cb(wpa_s, NULL);
3051         if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3052                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3053                               wpa_s->pending_listen_duration);
3054                 wpa_s->pending_listen_freq = 0;
3055         }
3056 }
3057
3058
3059 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3060                                  unsigned int timeout)
3061 {
3062         /* Limit maximum Listen state time based on driver limitation. */
3063         if (timeout > wpa_s->max_remain_on_chan)
3064                 timeout = wpa_s->max_remain_on_chan;
3065
3066         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3067                 return wpa_drv_p2p_listen(wpa_s, timeout);
3068
3069         return p2p_listen(wpa_s->global->p2p, timeout);
3070 }
3071
3072
3073 /**
3074  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3075  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3076  * @freq: Frequency of the channel in MHz
3077  *
3078  * This callback is called when the driver indicates that a remain-on-channel
3079  * operation has been completed, i.e., the duration on the requested channel
3080  * has timed out.
3081  */
3082 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3083                                           unsigned int freq)
3084 {
3085         wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3086                    "(p2p_long_listen=%d ms pending_action_tx=%p)",
3087                    wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3088         wpa_s->off_channel_freq = 0;
3089         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3090                 return;
3091         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3092                 return; /* P2P module started a new operation */
3093         if (wpa_s->pending_action_tx)
3094                 return;
3095         if (wpa_s->p2p_long_listen > 0)
3096                 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3097         if (wpa_s->p2p_long_listen > 0) {
3098                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3099                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3100         }
3101 }
3102
3103
3104 /**
3105  * wpas_p2p_group_remove - Remove a P2P group
3106  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3107  * @ifname: Network interface name of the group interface or "*" to remove all
3108  *      groups
3109  * Returns: 0 on success, -1 on failure
3110  *
3111  * This function is used to remove a P2P group. This can be used to disconnect
3112  * from a group in which the local end is a P2P Client or to end a P2P Group in
3113  * case the local end is the Group Owner. If a virtual network interface was
3114  * created for this group, that interface will be removed. Otherwise, only the
3115  * configured P2P group network will be removed from the interface.
3116  */
3117 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3118 {
3119         struct wpa_global *global = wpa_s->global;
3120
3121         if (os_strcmp(ifname, "*") == 0) {
3122                 struct wpa_supplicant *prev;
3123                 wpa_s = global->ifaces;
3124                 while (wpa_s) {
3125                         prev = wpa_s;
3126                         wpa_s = wpa_s->next;
3127                         wpas_p2p_disconnect(prev);
3128                 }
3129                 return 0;
3130         }
3131
3132         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3133                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3134                         break;
3135         }
3136
3137         return wpas_p2p_disconnect(wpa_s);
3138 }
3139
3140
3141 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3142                                     struct p2p_go_neg_results *params,
3143                                     int freq)
3144 {
3145         u8 bssid[ETH_ALEN];
3146         int res;
3147
3148         os_memset(params, 0, sizeof(*params));
3149         params->role_go = 1;
3150         if (freq) {
3151                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3152                            "frequency %d MHz", freq);
3153                 params->freq = freq;
3154         } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3155                    wpa_s->conf->p2p_oper_channel >= 1 &&
3156                    wpa_s->conf->p2p_oper_channel <= 11) {
3157                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3158                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3159                            "frequency %d MHz", params->freq);
3160         } else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3161                    wpa_s->conf->p2p_oper_reg_class == 124) {
3162                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3163                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3164                            "frequency %d MHz", params->freq);
3165         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3166                    wpa_s->best_overall_freq > 0 &&
3167                    p2p_supported_freq(wpa_s->global->p2p,
3168                                       wpa_s->best_overall_freq)) {
3169                 params->freq = wpa_s->best_overall_freq;
3170                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3171                            "channel %d MHz", params->freq);
3172         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3173                    wpa_s->best_24_freq > 0 &&
3174                    p2p_supported_freq(wpa_s->global->p2p,
3175                                       wpa_s->best_24_freq)) {
3176                 params->freq = wpa_s->best_24_freq;
3177                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3178                            "channel %d MHz", params->freq);
3179         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
3180                    wpa_s->best_5_freq > 0 &&
3181                    p2p_supported_freq(wpa_s->global->p2p,
3182                                       wpa_s->best_5_freq)) {
3183                 params->freq = wpa_s->best_5_freq;
3184                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3185                            "channel %d MHz", params->freq);
3186         } else {
3187                 params->freq = 2412;
3188                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3189                            "known)", params->freq);
3190         }
3191
3192         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3193             wpa_s->assoc_freq && !freq) {
3194                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3195                            "already using");
3196                 params->freq = wpa_s->assoc_freq;
3197         }
3198
3199         res = wpa_drv_shared_freq(wpa_s);
3200         if (res > 0 && !freq) {
3201                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3202                            "already using on a shared interface");
3203                 params->freq = res;
3204         }
3205 }
3206
3207
3208 static struct wpa_supplicant *
3209 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3210                          int go)
3211 {
3212         struct wpa_supplicant *group_wpa_s;
3213
3214         if (!wpas_p2p_create_iface(wpa_s))
3215                 return wpa_s;
3216
3217         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3218                                          WPA_IF_P2P_CLIENT) < 0)
3219                 return NULL;
3220         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3221         if (group_wpa_s == NULL) {
3222                 wpas_p2p_remove_pending_group_interface(wpa_s);
3223                 return NULL;
3224         }
3225
3226         return group_wpa_s;
3227 }
3228
3229
3230 /**
3231  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3232  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3233  * @persistent_group: Whether to create a persistent group
3234  * @freq: Frequency for the group or 0 to indicate no hardcoding
3235  * Returns: 0 on success, -1 on failure
3236  *
3237  * This function creates a new P2P group with the local end as the Group Owner,
3238  * i.e., without using Group Owner Negotiation.
3239  */
3240 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3241                        int freq)
3242 {
3243         struct p2p_go_neg_results params;
3244         unsigned int r;
3245
3246         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3247                 return -1;
3248
3249         if (freq == 2) {
3250                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3251                            "band");
3252                 if (wpa_s->best_24_freq > 0 &&
3253                     p2p_supported_freq(wpa_s->global->p2p,
3254                                        wpa_s->best_24_freq)) {
3255                         freq = wpa_s->best_24_freq;
3256                         wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3257                                    "channel: %d MHz", freq);
3258                 } else {
3259                         os_get_random((u8 *) &r, sizeof(r));
3260                         freq = 2412 + (r % 3) * 25;
3261                         wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3262                                    "channel: %d MHz", freq);
3263                 }
3264         }
3265
3266         if (freq == 5) {
3267                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3268                            "band");
3269                 if (wpa_s->best_5_freq > 0 &&
3270                     p2p_supported_freq(wpa_s->global->p2p,
3271                                        wpa_s->best_5_freq)) {
3272                         freq = wpa_s->best_5_freq;
3273                         wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3274                                    "channel: %d MHz", freq);
3275                 } else {
3276                         os_get_random((u8 *) &r, sizeof(r));
3277                         freq = 5180 + (r % 4) * 20;
3278                         if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3279                                 wpa_printf(MSG_DEBUG, "P2P: Could not select "
3280                                            "5 GHz channel for P2P group");
3281                                 return -1;
3282                         }
3283                         wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3284                                    "channel: %d MHz", freq);
3285                 }
3286         }
3287
3288         if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3289                 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3290                            "(%u MHz) is not supported for P2P uses",
3291                            freq);
3292                 return -1;
3293         }
3294
3295         wpas_p2p_init_go_params(wpa_s, &params, freq);
3296         p2p_go_params(wpa_s->global->p2p, &params);
3297         params.persistent_group = persistent_group;
3298
3299         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3300         if (wpa_s == NULL)
3301                 return -1;
3302         wpas_start_wps_go(wpa_s, &params, 0);
3303
3304         return 0;
3305 }
3306
3307
3308 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3309                                  struct wpa_ssid *params, int addr_allocated)
3310 {
3311         struct wpa_ssid *ssid;
3312
3313         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3314         if (wpa_s == NULL)
3315                 return -1;
3316
3317         wpa_supplicant_ap_deinit(wpa_s);
3318
3319         ssid = wpa_config_add_network(wpa_s->conf);
3320         if (ssid == NULL)
3321                 return -1;
3322         wpa_config_set_network_defaults(ssid);
3323         ssid->temporary = 1;
3324         ssid->proto = WPA_PROTO_RSN;
3325         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3326         ssid->group_cipher = WPA_CIPHER_CCMP;
3327         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3328         ssid->ssid = os_malloc(params->ssid_len);
3329         if (ssid->ssid == NULL) {
3330                 wpa_config_remove_network(wpa_s->conf, ssid->id);
3331                 return -1;
3332         }
3333         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3334         ssid->ssid_len = params->ssid_len;
3335         ssid->p2p_group = 1;
3336         ssid->export_keys = 1;
3337         if (params->psk_set) {
3338                 os_memcpy(ssid->psk, params->psk, 32);
3339                 ssid->psk_set = 1;
3340         }
3341         if (params->passphrase)
3342                 ssid->passphrase = os_strdup(params->passphrase);
3343
3344         wpa_supplicant_select_network(wpa_s, ssid);
3345
3346         wpa_s->show_group_started = 1;
3347
3348         return 0;
3349 }
3350
3351
3352 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3353                                   struct wpa_ssid *ssid, int addr_allocated,
3354                                   int freq)
3355 {
3356         struct p2p_go_neg_results params;
3357         int go = 0;
3358
3359         if (ssid->disabled != 2 || ssid->ssid == NULL)
3360                 return -1;
3361
3362         if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3363             go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3364                 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3365                            "already running");
3366                 return 0;
3367         }
3368
3369         /* Make sure we are not running find during connection establishment */
3370         wpas_p2p_stop_find(wpa_s);
3371
3372         if (ssid->mode == WPAS_MODE_INFRA)
3373                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3374
3375         if (ssid->mode != WPAS_MODE_P2P_GO)
3376                 return -1;
3377
3378         wpas_p2p_init_go_params(wpa_s, &params, freq);
3379
3380         params.role_go = 1;
3381         if (ssid->passphrase == NULL ||
3382             os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3383                 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
3384                            "group");
3385                 return -1;
3386         }
3387         os_strlcpy(params.passphrase, ssid->passphrase,
3388                    sizeof(params.passphrase));
3389         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
3390         params.ssid_len = ssid->ssid_len;
3391         params.persistent_group = 1;
3392
3393         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
3394         if (wpa_s == NULL)
3395                 return -1;
3396
3397         wpas_start_wps_go(wpa_s, &params, 0);
3398
3399         return 0;
3400 }
3401
3402
3403 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
3404                                struct wpabuf *proberesp_ies)
3405 {
3406         struct wpa_supplicant *wpa_s = ctx;
3407         if (wpa_s->ap_iface) {
3408                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
3409                 if (beacon_ies) {
3410                         wpabuf_free(hapd->p2p_beacon_ie);
3411                         hapd->p2p_beacon_ie = beacon_ies;
3412                 }
3413                 wpabuf_free(hapd->p2p_probe_resp_ie);
3414                 hapd->p2p_probe_resp_ie = proberesp_ies;
3415         } else {
3416                 wpabuf_free(beacon_ies);
3417                 wpabuf_free(proberesp_ies);
3418         }
3419         wpa_supplicant_ap_update_beacon(wpa_s);
3420 }
3421
3422
3423 static void wpas_p2p_idle_update(void *ctx, int idle)
3424 {
3425         struct wpa_supplicant *wpa_s = ctx;
3426         if (!wpa_s->ap_iface)
3427                 return;
3428         wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
3429         if (idle)
3430                 wpas_p2p_set_group_idle_timeout(wpa_s);
3431         else
3432                 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3433 }
3434
3435
3436 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
3437                                        int persistent_group,
3438                                        int group_formation)
3439 {
3440         struct p2p_group *group;
3441         struct p2p_group_config *cfg;
3442
3443         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3444                 return NULL;
3445         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3446                 return NULL;
3447
3448         cfg = os_zalloc(sizeof(*cfg));
3449         if (cfg == NULL)
3450                 return NULL;
3451
3452         cfg->persistent_group = persistent_group;
3453         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
3454         if (wpa_s->max_stations &&
3455             wpa_s->max_stations < wpa_s->conf->max_num_sta)
3456                 cfg->max_clients = wpa_s->max_stations;
3457         else
3458                 cfg->max_clients = wpa_s->conf->max_num_sta;
3459         cfg->cb_ctx = wpa_s;
3460         cfg->ie_update = wpas_p2p_ie_update;
3461         cfg->idle_update = wpas_p2p_idle_update;
3462
3463         group = p2p_group_init(wpa_s->global->p2p, cfg);
3464         if (group == NULL)
3465                 os_free(cfg);
3466         if (!group_formation)
3467                 p2p_group_notif_formation_done(group);
3468         wpa_s->p2p_group = group;
3469         return group;
3470 }
3471
3472
3473 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3474                           int registrar)
3475 {
3476         if (!wpa_s->p2p_in_provisioning) {
3477                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
3478                            "provisioning not in progress");
3479                 return;
3480         }
3481
3482         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
3483                              NULL);
3484         if (wpa_s->global->p2p)
3485                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
3486         else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3487                 wpa_drv_wps_success_cb(wpa_s, peer_addr);
3488         wpas_group_formation_completed(wpa_s, 1);
3489 }
3490
3491
3492 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3493                        const char *config_method)
3494 {
3495         u16 config_methods;
3496
3497         if (os_strcmp(config_method, "display") == 0)
3498                 config_methods = WPS_CONFIG_DISPLAY;
3499         else if (os_strcmp(config_method, "keypad") == 0)
3500                 config_methods = WPS_CONFIG_KEYPAD;
3501         else if (os_strcmp(config_method, "pbc") == 0 ||
3502                  os_strcmp(config_method, "pushbutton") == 0)
3503                 config_methods = WPS_CONFIG_PUSHBUTTON;
3504         else
3505                 return -1;
3506
3507         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3508                 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
3509                                                  config_methods);
3510         }
3511
3512         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
3513                 return -1;
3514
3515         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
3516                                  config_methods, 0);
3517 }
3518
3519
3520 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
3521                               char *end)
3522 {
3523         return p2p_scan_result_text(ies, ies_len, buf, end);
3524 }
3525
3526
3527 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
3528 {
3529         if (!wpa_s->pending_action_tx)
3530                 return;
3531
3532         wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
3533                    "operation request");
3534         wpabuf_free(wpa_s->pending_action_tx);
3535         wpa_s->pending_action_tx = NULL;
3536 }
3537
3538
3539 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
3540                   enum p2p_discovery_type type,
3541                   unsigned int num_req_dev_types, const u8 *req_dev_types)
3542 {
3543         wpas_p2p_clear_pending_action_tx(wpa_s);
3544         wpa_s->p2p_long_listen = 0;
3545
3546         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3547                 return wpa_drv_p2p_find(wpa_s, timeout, type);
3548
3549         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3550                 return -1;
3551
3552         return p2p_find(wpa_s->global->p2p, timeout, type,
3553                         num_req_dev_types, req_dev_types);
3554 }
3555
3556
3557 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
3558 {
3559         wpas_p2p_clear_pending_action_tx(wpa_s);
3560         wpa_s->p2p_long_listen = 0;
3561         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3562         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3563
3564         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3565                 wpa_drv_p2p_stop_find(wpa_s);
3566                 return;
3567         }
3568
3569         if (wpa_s->global->p2p)
3570                 p2p_stop_find(wpa_s->global->p2p);
3571
3572         wpas_p2p_remove_pending_group_interface(wpa_s);
3573 }
3574
3575
3576 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3577 {
3578         struct wpa_supplicant *wpa_s = eloop_ctx;
3579         wpa_s->p2p_long_listen = 0;
3580 }
3581
3582
3583 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
3584 {
3585         int res;
3586
3587         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3588                 return -1;
3589
3590         wpas_p2p_clear_pending_action_tx(wpa_s);
3591
3592         if (timeout == 0) {
3593                 /*
3594                  * This is a request for unlimited Listen state. However, at
3595                  * least for now, this is mapped to a Listen state for one
3596                  * hour.
3597                  */
3598                 timeout = 3600;
3599         }
3600         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3601         wpa_s->p2p_long_listen = 0;
3602
3603         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
3604         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
3605                 wpa_s->p2p_long_listen = timeout * 1000;
3606                 eloop_register_timeout(timeout, 0,
3607                                        wpas_p2p_long_listen_timeout,
3608                                        wpa_s, NULL);
3609         }
3610
3611         return res;
3612 }
3613
3614
3615 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
3616                           u8 *buf, size_t len, int p2p_group)
3617 {
3618         struct wpabuf *p2p_ie;
3619         int ret;
3620
3621         if (wpa_s->global->p2p_disabled)
3622                 return -1;
3623         if (wpa_s->global->p2p == NULL)
3624                 return -1;
3625         if (bss == NULL)
3626                 return -1;
3627
3628         p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
3629         ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
3630                                p2p_group, p2p_ie);
3631         wpabuf_free(p2p_ie);
3632
3633         return ret;
3634 }
3635
3636
3637 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
3638                           const u8 *ie, size_t ie_len)
3639 {
3640         if (wpa_s->global->p2p_disabled)
3641                 return 0;
3642         if (wpa_s->global->p2p == NULL)
3643                 return 0;
3644
3645         return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
3646 }
3647
3648
3649 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
3650                         const u8 *sa, const u8 *bssid,
3651                         u8 category, const u8 *data, size_t len, int freq)
3652 {
3653         if (wpa_s->global->p2p_disabled)
3654                 return;
3655         if (wpa_s->global->p2p == NULL)
3656                 return;
3657
3658         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
3659                       freq);
3660 }
3661
3662
3663 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
3664 {
3665         if (wpa_s->global->p2p_disabled)
3666                 return;
3667         if (wpa_s->global->p2p == NULL)
3668                 return;
3669
3670         p2p_scan_ie(wpa_s->global->p2p, ies);
3671 }
3672
3673
3674 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
3675 {
3676         p2p_group_deinit(wpa_s->p2p_group);
3677         wpa_s->p2p_group = NULL;
3678 }
3679
3680
3681 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
3682 {
3683         wpa_s->p2p_long_listen = 0;
3684
3685         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3686                 return wpa_drv_p2p_reject(wpa_s, addr);
3687
3688         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3689                 return -1;
3690
3691         return p2p_reject(wpa_s->global->p2p, addr);
3692 }
3693
3694
3695 /* Invite to reinvoke a persistent group */
3696 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3697                     struct wpa_ssid *ssid, const u8 *go_dev_addr)
3698 {
3699         enum p2p_invite_role role;
3700         u8 *bssid = NULL;
3701
3702         if (ssid->mode == WPAS_MODE_P2P_GO) {
3703                 role = P2P_INVITE_ROLE_GO;
3704                 if (peer_addr == NULL) {
3705                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
3706                                    "address in invitation command");
3707                         return -1;
3708                 }
3709                 if (wpas_p2p_create_iface(wpa_s)) {
3710                         if (wpas_p2p_add_group_interface(wpa_s,
3711                                                          WPA_IF_P2P_GO) < 0) {
3712                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
3713                                            "allocate a new interface for the "
3714                                            "group");
3715                                 return -1;
3716                         }
3717                         bssid = wpa_s->pending_interface_addr;
3718                 } else
3719                         bssid = wpa_s->own_addr;
3720         } else {
3721                 role = P2P_INVITE_ROLE_CLIENT;
3722                 peer_addr = ssid->bssid;
3723         }
3724         wpa_s->pending_invite_ssid_id = ssid->id;
3725
3726         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3727                 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3728                                           ssid->ssid, ssid->ssid_len,
3729                                           go_dev_addr, 1);
3730
3731         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3732                 return -1;
3733
3734         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3735                           ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
3736 }
3737
3738
3739 /* Invite to join an active group */
3740 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
3741                           const u8 *peer_addr, const u8 *go_dev_addr)
3742 {
3743         struct wpa_global *global = wpa_s->global;
3744         enum p2p_invite_role role;
3745         u8 *bssid = NULL;
3746         struct wpa_ssid *ssid;
3747
3748         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3749                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3750                         break;
3751         }
3752         if (wpa_s == NULL) {
3753                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
3754                 return -1;
3755         }
3756
3757         ssid = wpa_s->current_ssid;
3758         if (ssid == NULL) {
3759                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
3760                            "invitation");
3761                 return -1;
3762         }
3763
3764         if (ssid->mode == WPAS_MODE_P2P_GO) {
3765                 role = P2P_INVITE_ROLE_ACTIVE_GO;
3766                 bssid = wpa_s->own_addr;
3767                 if (go_dev_addr == NULL)
3768                         go_dev_addr = wpa_s->parent->own_addr;
3769         } else {
3770                 role = P2P_INVITE_ROLE_CLIENT;
3771                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
3772                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
3773                                    "invite to current group");
3774                         return -1;
3775                 }
3776                 bssid = wpa_s->bssid;
3777                 if (go_dev_addr == NULL &&
3778                     !is_zero_ether_addr(wpa_s->go_dev_addr))
3779                         go_dev_addr = wpa_s->go_dev_addr;
3780         }
3781         wpa_s->parent->pending_invite_ssid_id = -1;
3782
3783         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3784                 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
3785                                           ssid->ssid, ssid->ssid_len,
3786                                           go_dev_addr, 0);
3787
3788         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3789                 return -1;
3790
3791         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
3792                           ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
3793                           go_dev_addr, 0);
3794 }
3795
3796
3797 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
3798 {
3799         struct wpa_ssid *ssid = wpa_s->current_ssid;
3800         const char *ssid_txt;
3801         u8 go_dev_addr[ETH_ALEN];
3802         int network_id = -1;
3803         int persistent;
3804
3805         if (!wpa_s->show_group_started || !ssid)
3806                 return;
3807
3808         wpa_s->show_group_started = 0;
3809
3810         ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3811         os_memset(go_dev_addr, 0, ETH_ALEN);
3812         if (ssid->bssid_set)
3813                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3814         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3815                                                ssid->ssid_len);
3816         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3817
3818         if (wpa_s->global->p2p_group_formation == wpa_s)
3819                 wpa_s->global->p2p_group_formation = NULL;
3820
3821         if (ssid->passphrase == NULL && ssid->psk_set) {
3822                 char psk[65];
3823                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3824                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3825                         "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
3826                         MACSTR "%s",
3827                         wpa_s->ifname, ssid_txt, ssid->frequency, psk,
3828                         MAC2STR(go_dev_addr),
3829                         persistent ? " [PERSISTENT]" : "");
3830         } else {
3831                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3832                         "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
3833                         "go_dev_addr=" MACSTR "%s",
3834                         wpa_s->ifname, ssid_txt, ssid->frequency,
3835                         ssid->passphrase ? ssid->passphrase : "",
3836                         MAC2STR(go_dev_addr),
3837                         persistent ? " [PERSISTENT]" : "");
3838         }
3839
3840         if (persistent)
3841                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
3842                                                              ssid, go_dev_addr);
3843         if (network_id < 0)
3844                 network_id = ssid->id;
3845         wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
3846 }
3847
3848
3849 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3850                           u32 interval1, u32 duration2, u32 interval2)
3851 {
3852         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3853                 return -1;
3854         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3855                 return -1;
3856
3857         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3858             wpa_s->current_ssid == NULL ||
3859             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3860                 return -1;
3861
3862         return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3863                                 wpa_s->own_addr, wpa_s->assoc_freq,
3864                                 duration1, interval1, duration2, interval2);
3865 }
3866
3867
3868 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3869                         unsigned int interval)
3870 {
3871         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3872                 return -1;
3873
3874         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3875                 return -1;
3876
3877         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3878 }
3879
3880
3881 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
3882 {
3883         struct wpa_supplicant *wpa_s = eloop_ctx;
3884
3885         if (wpa_s->conf->p2p_group_idle == 0) {
3886                 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
3887                            "disabled");
3888                 return;
3889         }
3890
3891         wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
3892                    "group");
3893         wpa_s->removal_reason = P2P_GROUP_REMOVAL_IDLE_TIMEOUT;
3894         wpas_p2p_group_delete(wpa_s);
3895 }
3896
3897
3898 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
3899 {
3900         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3901         if (wpa_s->conf->p2p_group_idle == 0)
3902                 return;
3903
3904         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
3905                 return;
3906
3907         wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
3908                    wpa_s->conf->p2p_group_idle);
3909         eloop_register_timeout(wpa_s->conf->p2p_group_idle, 0,
3910                                wpas_p2p_group_idle_timeout, wpa_s, NULL);
3911 }
3912
3913
3914 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3915                            u16 reason_code, const u8 *ie, size_t ie_len)
3916 {
3917         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3918                 return;
3919         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3920                 return;
3921
3922         p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3923 }
3924
3925
3926 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3927                              u16 reason_code, const u8 *ie, size_t ie_len)
3928 {
3929         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3930                 return;
3931         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3932                 return;
3933
3934         p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3935 }
3936
3937
3938 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3939 {
3940         struct p2p_data *p2p = wpa_s->global->p2p;
3941
3942         if (p2p == NULL)
3943                 return;
3944
3945         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3946                 return;
3947
3948         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3949                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3950
3951         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
3952                 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
3953
3954         if (wpa_s->wps &&
3955             (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
3956                 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
3957
3958         if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
3959                 p2p_set_uuid(p2p, wpa_s->wps->uuid);
3960
3961         if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
3962                 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
3963                 p2p_set_model_name(p2p, wpa_s->conf->model_name);
3964                 p2p_set_model_number(p2p, wpa_s->conf->model_number);
3965                 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
3966         }
3967
3968         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
3969                 p2p_set_sec_dev_types(p2p,
3970                                       (void *) wpa_s->conf->sec_device_type,
3971                                       wpa_s->conf->num_sec_device_types);
3972
3973         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
3974                 int i;
3975                 p2p_remove_wps_vendor_extensions(p2p);
3976                 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3977                         if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3978                                 continue;
3979                         p2p_add_wps_vendor_extension(
3980                                 p2p, wpa_s->conf->wps_vendor_ext[i]);
3981                 }
3982         }
3983
3984         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3985             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3986                 char country[3];
3987                 country[0] = wpa_s->conf->country[0];
3988                 country[1] = wpa_s->conf->country[1];
3989                 country[2] = 0x04;
3990                 p2p_set_country(p2p, country);
3991         }
3992
3993         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3994                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3995                                      wpa_s->conf->p2p_ssid_postfix ?
3996                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
3997                                      0);
3998         }
3999
4000         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4001                 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4002 }
4003
4004
4005 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4006                      int duration)
4007 {
4008         if (!wpa_s->ap_iface)
4009                 return -1;
4010         return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4011                                    duration);
4012 }
4013
4014
4015 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4016 {
4017         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4018                 return -1;
4019         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4020                 return -1;
4021
4022         wpa_s->global->cross_connection = enabled;
4023         p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4024
4025         if (!enabled) {
4026                 struct wpa_supplicant *iface;
4027
4028                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4029                 {
4030                         if (iface->cross_connect_enabled == 0)
4031                                 continue;
4032
4033                         iface->cross_connect_enabled = 0;
4034                         iface->cross_connect_in_use = 0;
4035                         wpa_msg(iface->parent, MSG_INFO,
4036                                 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4037                                 iface->ifname, iface->cross_connect_uplink);
4038                 }
4039         }
4040
4041         return 0;
4042 }
4043
4044
4045 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4046 {
4047         struct wpa_supplicant *iface;
4048
4049         if (!uplink->global->cross_connection)
4050                 return;
4051
4052         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4053                 if (!iface->cross_connect_enabled)
4054                         continue;
4055                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4056                     0)
4057                         continue;
4058                 if (iface->ap_iface == NULL)
4059                         continue;
4060                 if (iface->cross_connect_in_use)
4061                         continue;
4062
4063                 iface->cross_connect_in_use = 1;
4064                 wpa_msg(iface->parent, MSG_INFO,
4065                         P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4066                         iface->ifname, iface->cross_connect_uplink);
4067         }
4068 }
4069
4070
4071 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4072 {
4073         struct wpa_supplicant *iface;
4074
4075         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4076                 if (!iface->cross_connect_enabled)
4077                         continue;
4078                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4079                     0)
4080                         continue;
4081                 if (!iface->cross_connect_in_use)
4082                         continue;
4083
4084                 wpa_msg(iface->parent, MSG_INFO,
4085                         P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4086                         iface->ifname, iface->cross_connect_uplink);
4087                 iface->cross_connect_in_use = 0;
4088         }
4089 }
4090
4091
4092 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4093 {
4094         if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4095             wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4096             wpa_s->cross_connect_disallowed)
4097                 wpas_p2p_disable_cross_connect(wpa_s);
4098         else
4099                 wpas_p2p_enable_cross_connect(wpa_s);
4100         if (!wpa_s->ap_iface)
4101                 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4102 }
4103
4104
4105 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4106 {
4107         wpas_p2p_disable_cross_connect(wpa_s);
4108         if (!wpa_s->ap_iface)
4109                 wpas_p2p_set_group_idle_timeout(wpa_s);
4110 }
4111
4112
4113 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4114 {
4115         struct wpa_supplicant *iface;
4116
4117         if (!wpa_s->global->cross_connection)
4118                 return;
4119
4120         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4121                 if (iface == wpa_s)
4122                         continue;
4123                 if (iface->drv_flags &
4124                     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4125                         continue;
4126                 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
4127                         continue;
4128
4129                 wpa_s->cross_connect_enabled = 1;
4130                 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
4131                            sizeof(wpa_s->cross_connect_uplink));
4132                 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
4133                            "%s to %s whenever uplink is available",
4134                            wpa_s->ifname, wpa_s->cross_connect_uplink);
4135
4136                 if (iface->ap_iface || iface->current_ssid == NULL ||
4137                     iface->current_ssid->mode != WPAS_MODE_INFRA ||
4138                     iface->cross_connect_disallowed ||
4139                     iface->wpa_state != WPA_COMPLETED)
4140                         break;
4141
4142                 wpa_s->cross_connect_in_use = 1;
4143                 wpa_msg(wpa_s->parent, MSG_INFO,
4144                         P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4145                         wpa_s->ifname, wpa_s->cross_connect_uplink);
4146                 break;
4147         }
4148 }
4149
4150
4151 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
4152 {
4153         if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
4154             !wpa_s->p2p_in_provisioning)
4155                 return 0; /* not P2P client operation */
4156
4157         wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
4158                    "session overlap");
4159         if (wpa_s != wpa_s->parent)
4160                 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
4161
4162         if (wpa_s->global->p2p)
4163                 p2p_group_formation_failed(wpa_s->global->p2p);
4164
4165         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4166                              wpa_s->parent, NULL);
4167
4168         wpas_group_formation_completed(wpa_s, 0);
4169         return 1;
4170 }
4171
4172
4173 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
4174 {
4175         struct p2p_channels chan;
4176
4177         if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
4178                 return;
4179
4180         os_memset(&chan, 0, sizeof(chan));
4181         if (wpas_p2p_setup_channels(wpa_s, &chan)) {
4182                 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
4183                            "channel list");
4184                 return;
4185         }
4186
4187         p2p_update_channel_list(wpa_s->global->p2p, &chan);
4188 }
4189
4190
4191 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
4192 {
4193         struct wpa_global *global = wpa_s->global;
4194         int found = 0;
4195         const u8 *peer;
4196
4197         if (global->p2p == NULL)
4198                 return -1;
4199
4200         wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
4201
4202         if (wpa_s->pending_interface_name[0] &&
4203             !is_zero_ether_addr(wpa_s->pending_interface_addr))
4204                 found = 1;
4205
4206         peer = p2p_get_go_neg_peer(global->p2p);
4207         if (peer) {
4208                 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
4209                            MACSTR, MAC2STR(peer));
4210                 p2p_unauthorize(global->p2p, peer);
4211         }
4212
4213         wpas_p2p_stop_find(wpa_s);
4214
4215         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4216                 if (wpa_s == global->p2p_group_formation &&
4217                     (wpa_s->p2p_in_provisioning ||
4218                      wpa_s->parent->pending_interface_type ==
4219                      WPA_IF_P2P_CLIENT)) {
4220                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
4221                                    "formation found - cancelling",
4222                                    wpa_s->ifname);
4223                         found = 1;
4224                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4225                                              wpa_s->parent, NULL);
4226                         wpas_p2p_group_delete(wpa_s);
4227                         break;
4228                 }
4229         }
4230
4231         if (!found) {
4232                 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
4233                 return -1;
4234         }
4235
4236         return 0;
4237 }
4238
4239
4240 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
4241 {
4242         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4243                 return;
4244
4245         wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
4246                    "being available anymore");
4247         wpa_s->removal_reason = P2P_GROUP_REMOVAL_UNAVAILABLE;
4248         wpas_p2p_group_delete(wpa_s);
4249 }
4250
4251
4252 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
4253                                    int freq_24, int freq_5, int freq_overall)
4254 {
4255         struct p2p_data *p2p = wpa_s->global->p2p;
4256         if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4257                 return;
4258         p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
4259 }
4260
4261
4262 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
4263 {
4264         u8 peer[ETH_ALEN];
4265         struct p2p_data *p2p = wpa_s->global->p2p;
4266
4267         if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
4268                 return -1;
4269
4270         if (hwaddr_aton(addr, peer))
4271                 return -1;
4272
4273         return p2p_unauthorize(p2p, peer);
4274 }
4275
4276
4277 /**
4278  * wpas_p2p_disconnect - Disconnect from a P2P Group
4279  * @wpa_s: Pointer to wpa_supplicant data
4280  * Returns: 0 on success, -1 on failure
4281  *
4282  * This can be used to disconnect from a group in which the local end is a P2P
4283  * Client or to end a P2P Group in case the local end is the Group Owner. If a
4284  * virtual network interface was created for this group, that interface will be
4285  * removed. Otherwise, only the configured P2P group network will be removed
4286  * from the interface.
4287  */
4288 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
4289 {
4290
4291         if (wpa_s == NULL)
4292                 return -1;
4293
4294         wpa_s->removal_reason = P2P_GROUP_REMOVAL_REQUESTED;
4295         wpas_p2p_group_delete(wpa_s);
4296
4297         return 0;
4298 }