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