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