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