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