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