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