P2P: Add control interface commands for P2P
[libeap.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 "wpa_supplicant_i.h"
26 #include "driver_i.h"
27 #include "ap.h"
28 #include "config_ssid.h"
29 #include "config.h"
30 #include "mlme.h"
31 #include "notify.h"
32 #include "scan.h"
33 #include "bss.h"
34 #include "wps_supplicant.h"
35 #include "p2p_supplicant.h"
36
37
38 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
39 static struct wpa_supplicant *
40 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
41                          int go);
42 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
43 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
44
45
46 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
47                                       struct wpa_scan_results *scan_res)
48 {
49         size_t i;
50
51         if (wpa_s->global->p2p_disabled)
52                 return;
53
54         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
55                    (int) scan_res->num);
56
57         for (i = 0; i < scan_res->num; i++) {
58                 struct wpa_scan_res *bss = scan_res->res[i];
59                 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
60                                          bss->freq, bss->level,
61                                          (const u8 *) (bss + 1),
62                                          bss->ie_len) > 0)
63                         return;
64         }
65
66         p2p_scan_res_handled(wpa_s->global->p2p);
67 }
68
69
70 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq)
71 {
72         struct wpa_supplicant *wpa_s = ctx;
73         struct wpa_driver_scan_params params;
74         int ret;
75         struct wpabuf *wps_ie, *ies;
76         int social_channels[] = { 2412, 2437, 2462, 0, 0 };
77
78         os_memset(&params, 0, sizeof(params));
79
80         /* P2P Wildcard SSID */
81         params.num_ssids = 1;
82         params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
83         params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
84
85         wpa_s->wps->dev.p2p = 1;
86         wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
87                                         WPS_REQ_ENROLLEE);
88         if (wps_ie == NULL)
89                 return -1;
90
91         ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
92         if (ies == NULL) {
93                 wpabuf_free(wps_ie);
94                 return -1;
95         }
96         wpabuf_put_buf(ies, wps_ie);
97         wpabuf_free(wps_ie);
98
99         p2p_scan_ie(wpa_s->global->p2p, ies);
100
101         params.extra_ies = wpabuf_head(ies);
102         params.extra_ies_len = wpabuf_len(ies);
103
104         switch (type) {
105         case P2P_SCAN_SOCIAL:
106                 params.freqs = social_channels;
107                 break;
108         case P2P_SCAN_FULL:
109                 break;
110         case P2P_SCAN_SPECIFIC:
111                 social_channels[0] = freq;
112                 social_channels[1] = 0;
113                 params.freqs = social_channels;
114                 break;
115         case P2P_SCAN_SOCIAL_PLUS_ONE:
116                 social_channels[3] = freq;
117                 params.freqs = social_channels;
118                 break;
119         }
120
121         wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
122         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
123                 ret = ieee80211_sta_req_scan(wpa_s, &params);
124         else
125                 ret = wpa_drv_scan(wpa_s, &params);
126
127         wpabuf_free(ies);
128
129         return ret;
130 }
131
132
133 #ifdef CONFIG_CLIENT_MLME
134 static void p2p_rx_action_mlme(void *ctx, const u8 *buf, size_t len, int freq)
135 {
136         struct wpa_supplicant *wpa_s = ctx;
137         const struct ieee80211_mgmt *mgmt;
138         size_t hdr_len;
139
140         if (wpa_s->global->p2p_disabled)
141                 return;
142         mgmt = (const struct ieee80211_mgmt *) buf;
143         hdr_len = (const u8 *) &mgmt->u.action.u.vs_public_action.action - buf;
144         if (hdr_len > len)
145                 return;
146         p2p_rx_action(wpa_s->global->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
147                       mgmt->u.action.category,
148                       &mgmt->u.action.u.vs_public_action.action,
149                       len - hdr_len, freq);
150 }
151 #endif /* CONFIG_CLIENT_MLME */
152
153
154 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
155 {
156         switch (p2p_group_interface) {
157         case P2P_GROUP_INTERFACE_PENDING:
158                 return WPA_IF_P2P_GROUP;
159         case P2P_GROUP_INTERFACE_GO:
160                 return WPA_IF_P2P_GO;
161         case P2P_GROUP_INTERFACE_CLIENT:
162                 return WPA_IF_P2P_CLIENT;
163         }
164
165         return WPA_IF_P2P_GROUP;
166 }
167
168
169 static void wpas_p2p_group_delete(struct wpa_supplicant *wpa_s)
170 {
171         struct wpa_ssid *ssid;
172         char *gtype;
173
174         ssid = wpa_s->current_ssid;
175         if (ssid == NULL) {
176                 /*
177                  * The current SSID was not known, but there may still be a
178                  * pending P2P group interface waiting for provisioning.
179                  */
180                 ssid = wpa_s->conf->ssid;
181                 while (ssid) {
182                         if (ssid->p2p_group &&
183                             (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
184                              (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
185                                 break;
186                         ssid = ssid->next;
187                 }
188         }
189         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
190                 gtype = "GO";
191         else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
192                  (ssid && ssid->mode == WPAS_MODE_INFRA)) {
193                 wpa_s->reassociate = 0;
194                 wpa_s->disconnected = 1;
195                 wpa_supplicant_deauthenticate(wpa_s,
196                                               WLAN_REASON_DEAUTH_LEAVING);
197                 gtype = "client";
198         } else
199                 gtype = "GO";
200         wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_REMOVED "%s %s",
201                 wpa_s->ifname, gtype);
202         if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
203                 struct wpa_global *global;
204                 char *ifname;
205                 enum wpa_driver_if_type type;
206                 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
207                         wpa_s->ifname);
208                 global = wpa_s->global;
209                 ifname = os_strdup(wpa_s->ifname);
210                 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
211                 wpa_supplicant_remove_iface(wpa_s->global, wpa_s);
212                 wpa_s = global->ifaces;
213                 if (wpa_s && ifname)
214                         wpa_drv_if_remove(wpa_s, type, ifname);
215                 os_free(ifname);
216                 return;
217         }
218
219         wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
220         if (ssid && (ssid->p2p_group ||
221                      ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
222                      (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
223                 int id = ssid->id;
224                 if (ssid == wpa_s->current_ssid)
225                         wpa_s->current_ssid = NULL;
226                 wpas_notify_network_removed(wpa_s, ssid);
227                 wpa_config_remove_network(wpa_s->conf, id);
228                 wpa_supplicant_clear_status(wpa_s);
229         } else {
230                 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
231                            "found");
232         }
233         wpa_supplicant_ap_deinit(wpa_s);
234 }
235
236
237 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
238                                      u8 *go_dev_addr,
239                                      const u8 *ssid, size_t ssid_len)
240 {
241         struct wpa_bss *bss;
242         const u8 *bssid;
243         struct wpabuf *p2p;
244         u8 group_capab;
245         const u8 *addr;
246
247         if (wpa_s->go_params)
248                 bssid = wpa_s->go_params->peer_interface_addr;
249         else
250                 bssid = wpa_s->bssid;
251
252         bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
253         if (bss == NULL) {
254                 u8 iface_addr[ETH_ALEN];
255                 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
256                                            iface_addr) == 0)
257                         bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
258         }
259         if (bss == NULL) {
260                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
261                            "group is persistent - BSS " MACSTR " not found",
262                            MAC2STR(bssid));
263                 return 0;
264         }
265
266         p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
267         if (p2p == NULL) {
268                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
269                            "group is persistent - BSS " MACSTR
270                            " did not include P2P IE", MAC2STR(bssid));
271                 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
272                             (u8 *) (bss + 1), bss->ie_len);
273                 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
274                             ((u8 *) bss + 1) + bss->ie_len,
275                             bss->beacon_ie_len);
276                 return 0;
277         }
278
279         group_capab = p2p_get_group_capab(p2p);
280         addr = p2p_get_go_dev_addr(p2p);
281         wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
282                    "group_capab=0x%x", group_capab);
283         if (addr) {
284                 os_memcpy(go_dev_addr, addr, ETH_ALEN);
285                 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
286                            MAC2STR(addr));
287         } else
288                 os_memset(go_dev_addr, 0, ETH_ALEN);
289         wpabuf_free(p2p);
290
291         wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
292                    "go_dev_addr=" MACSTR,
293                    MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
294
295         return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
296 }
297
298
299 static void wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
300                                             struct wpa_ssid *ssid,
301                                             const u8 *go_dev_addr)
302 {
303         struct wpa_ssid *s;
304         int changed = 0;
305
306         wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
307                    "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
308         for (s = wpa_s->conf->ssid; s; s = s->next) {
309                 if (s->disabled == 2 &&
310                     os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
311                     s->ssid_len == ssid->ssid_len &&
312                     os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
313                         break;
314         }
315
316         if (s) {
317                 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
318                            "entry");
319                 if (ssid->passphrase && !s->passphrase)
320                         changed = 1;
321                 else if (ssid->passphrase && s->passphrase &&
322                          os_strcmp(ssid->passphrase, s->passphrase) != 0)
323                         changed = 1;
324         } else {
325                 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
326                            "entry");
327                 changed = 1;
328                 s = wpa_config_add_network(wpa_s->conf);
329                 if (s == NULL)
330                         return;
331                 wpa_config_set_network_defaults(s);
332         }
333
334         s->p2p_group = 1;
335         s->p2p_persistent_group = 1;
336         s->disabled = 2;
337         s->bssid_set = 1;
338         os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
339         s->mode = ssid->mode;
340         s->auth_alg = WPA_AUTH_ALG_OPEN;
341         s->key_mgmt = WPA_KEY_MGMT_PSK;
342         s->proto = WPA_PROTO_RSN;
343         s->pairwise_cipher = WPA_CIPHER_CCMP;
344         if (ssid->passphrase) {
345                 os_free(s->passphrase);
346                 s->passphrase = os_strdup(ssid->passphrase);
347         }
348         if (ssid->psk_set) {
349                 s->psk_set = 1;
350                 os_memcpy(s->psk, ssid->psk, 32);
351         }
352         if (s->passphrase && !s->psk_set)
353                 wpa_config_update_psk(s);
354         if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
355                 os_free(s->ssid);
356                 s->ssid = os_malloc(ssid->ssid_len);
357         }
358         if (s->ssid) {
359                 s->ssid_len = ssid->ssid_len;
360                 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
361         }
362
363 #ifndef CONFIG_NO_CONFIG_WRITE
364         if (changed && wpa_s->conf->update_config &&
365             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
366                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
367         }
368 #endif /* CONFIG_NO_CONFIG_WRITE */
369 }
370
371
372 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
373                                            int success)
374 {
375         struct wpa_ssid *ssid;
376         const char *ssid_txt;
377         int client;
378         int persistent;
379         u8 go_dev_addr[ETH_ALEN];
380
381         /*
382          * This callback is likely called for the main interface. Update wpa_s
383          * to use the group interface if a new interface was created for the
384          * group.
385          */
386         if (wpa_s->global->p2p_group_formation)
387                 wpa_s = wpa_s->global->p2p_group_formation;
388         wpa_s->p2p_in_provisioning = 0;
389
390         if (!success) {
391                 wpa_msg(wpa_s->parent, MSG_INFO,
392                         P2P_EVENT_GROUP_FORMATION_FAILURE);
393                 wpas_p2p_group_delete(wpa_s);
394                 return;
395         }
396
397         wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
398
399         ssid = wpa_s->current_ssid;
400         if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
401                 ssid->mode = WPAS_MODE_P2P_GO;
402                 p2p_group_notif_formation_done(wpa_s->p2p_group);
403                 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
404         }
405
406         persistent = 0;
407         if (ssid) {
408                 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
409                 client = ssid->mode == WPAS_MODE_INFRA;
410                 if (ssid->mode == WPAS_MODE_P2P_GO) {
411                         persistent = ssid->p2p_persistent_group;
412                         os_memcpy(go_dev_addr, wpa_s->parent->own_addr,
413                                   ETH_ALEN);
414                 } else
415                         persistent = wpas_p2p_persistent_group(wpa_s,
416                                                                go_dev_addr,
417                                                                ssid->ssid,
418                                                                ssid->ssid_len);
419         } else {
420                 ssid_txt = "";
421                 client = wpa_s->p2p_group_interface ==
422                         P2P_GROUP_INTERFACE_CLIENT;
423         }
424
425         wpa_s->show_group_started = 0;
426         if (client) {
427                 /*
428                  * Indicate event only after successfully completed 4-way
429                  * handshake, i.e., when the interface is ready for data
430                  * packets.
431                  */
432                 wpa_s->show_group_started = 1;
433         } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
434                 char psk[65];
435                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
436                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
437                         "%s GO ssid=\"%s\" psk=%s go_dev_addr=" MACSTR "%s",
438                         wpa_s->ifname, ssid_txt, psk, MAC2STR(go_dev_addr),
439                         persistent ? " [PERSISTENT]" : "");
440         } else {
441                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
442                         "%s GO ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
443                         MACSTR "%s",
444                         wpa_s->ifname, ssid_txt,
445                         ssid && ssid->passphrase ? ssid->passphrase : "",
446                         MAC2STR(go_dev_addr),
447                         persistent ? " [PERSISTENT]" : "");
448         }
449
450         if (persistent)
451                 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
452                                                 go_dev_addr);
453 }
454
455
456 static void wpas_send_action_cb(void *eloop_ctx, void *timeout_ctx)
457 {
458         struct wpa_supplicant *wpa_s = eloop_ctx;
459         struct wpa_supplicant *iface;
460         int res;
461         int without_roc;
462
463         without_roc = wpa_s->pending_action_without_roc;
464         wpa_s->pending_action_without_roc = 0;
465
466         if (wpa_s->pending_action_tx == NULL)
467                 return;
468
469         if (wpa_s->off_channel_freq != wpa_s->pending_action_freq &&
470             wpa_s->pending_action_freq != 0) {
471                 wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
472                            "waiting for another freq=%u (off_channel_freq=%u)",
473                            wpa_s->pending_action_freq,
474                            wpa_s->off_channel_freq);
475                 if (without_roc && wpa_s->off_channel_freq == 0) {
476                         /*
477                          * We may get here if wpas_send_action() found us to be
478                          * on the correct channel, but remain-on-channel cancel
479                          * event was received before getting here.
480                          */
481                         wpa_printf(MSG_DEBUG, "P2P: Schedule "
482                                    "remain-on-channel to send Action frame");
483                         if (wpa_drv_remain_on_channel(
484                                     wpa_s, wpa_s->pending_action_freq, 200) <
485                             0) {
486                                 wpa_printf(MSG_DEBUG, "P2P: Failed to request "
487                                            "driver to remain on channel (%u "
488                                            "MHz) for Action Frame TX",
489                                            wpa_s->pending_action_freq);
490                         }
491                 }
492                 return;
493         }
494
495         /*
496          * This call is likely going to be on the P2P device instance if the
497          * driver uses a separate interface for that purpose. However, some
498          * Action frames are actually sent within a P2P Group and when that is
499          * the case, we need to follow power saving (e.g., GO buffering the
500          * frame for a client in PS mode or a client following the adverised
501          * NoA from its GO). To make that easier for the driver, select the
502          * correct group interface here.
503          */
504         if (os_memcmp(wpa_s->pending_action_src, wpa_s->own_addr, ETH_ALEN) !=
505             0) {
506                 /*
507                  * Try to find a group interface that matches with the source
508                  * address.
509                  */
510                 iface = wpa_s->global->ifaces;
511                 while (iface) {
512                         if (os_memcmp(wpa_s->pending_action_src,
513                                       iface->own_addr, ETH_ALEN) == 0)
514                                 break;
515                 }
516                 if (iface) {
517                         wpa_printf(MSG_DEBUG, "P2P: Use group interface %s "
518                                    "instead of interface %s for Action TX",
519                                    iface->ifname, wpa_s->ifname);
520                 } else
521                         iface = wpa_s;
522         } else
523                 iface = wpa_s;
524
525         wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
526                    MACSTR " using interface %s",
527                    MAC2STR(wpa_s->pending_action_dst), iface->ifname);
528         res = wpa_drv_send_action(iface, wpa_s->pending_action_freq,
529                                   wpa_s->pending_action_dst,
530                                   wpa_s->pending_action_src,
531                                   wpa_s->pending_action_bssid,
532                                   wpabuf_head(wpa_s->pending_action_tx),
533                                   wpabuf_len(wpa_s->pending_action_tx));
534         if (res) {
535                 wpa_printf(MSG_DEBUG, "P2P: Failed to send the pending "
536                            "Action frame");
537                 /*
538                  * Use fake TX status event to allow P2P state machine to
539                  * continue.
540                  */
541                 wpas_send_action_tx_status(
542                         wpa_s, wpa_s->pending_action_dst,
543                         wpabuf_head(wpa_s->pending_action_tx),
544                         wpabuf_len(wpa_s->pending_action_tx), 0);
545         }
546 }
547
548
549 void wpas_send_action_tx_status(struct wpa_supplicant *wpa_s, const u8 *dst,
550                                 const u8 *data, size_t data_len, int ack)
551 {
552         if (wpa_s->global->p2p_disabled)
553                 return;
554
555         if (wpa_s->pending_action_tx == NULL) {
556                 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - no "
557                            "pending operation");
558                 return;
559         }
560
561         if (os_memcmp(dst, wpa_s->pending_action_dst, ETH_ALEN) != 0) {
562                 wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - unknown "
563                            "destination address");
564                 return;
565         }
566
567         wpabuf_free(wpa_s->pending_action_tx);
568         wpa_s->pending_action_tx = NULL;
569
570         p2p_send_action_cb(wpa_s->global->p2p, wpa_s->pending_action_freq,
571                            wpa_s->pending_action_dst,
572                            wpa_s->pending_action_src,
573                            wpa_s->pending_action_bssid,
574                            ack);
575
576         if (wpa_s->pending_pd_before_join &&
577             (os_memcmp(wpa_s->pending_action_dst, wpa_s->pending_join_dev_addr,
578                        ETH_ALEN) == 0 ||
579              os_memcmp(wpa_s->pending_action_dst,
580                        wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
581                 wpa_s->pending_pd_before_join = 0;
582                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
583                            "join-existing-group operation");
584                 wpas_p2p_join_start(wpa_s);
585         }
586 }
587
588
589 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
590                             const u8 *src, const u8 *bssid, const u8 *buf,
591                             size_t len, unsigned int wait_time)
592 {
593         struct wpa_supplicant *wpa_s = ctx;
594
595         wpa_printf(MSG_DEBUG, "P2P: Send action frame: freq=%d dst=" MACSTR
596                    " src=" MACSTR " bssid=" MACSTR,
597                    freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid));
598
599         if (wpa_s->pending_action_tx) {
600                 wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
601                            "to " MACSTR, MAC2STR(wpa_s->pending_action_dst));
602                 wpabuf_free(wpa_s->pending_action_tx);
603         }
604         wpa_s->pending_action_tx = wpabuf_alloc(len);
605         if (wpa_s->pending_action_tx == NULL)
606                 return -1;
607         wpabuf_put_data(wpa_s->pending_action_tx, buf, len);
608         os_memcpy(wpa_s->pending_action_src, src, ETH_ALEN);
609         os_memcpy(wpa_s->pending_action_dst, dst, ETH_ALEN);
610         os_memcpy(wpa_s->pending_action_bssid, bssid, ETH_ALEN);
611         wpa_s->pending_action_freq = freq;
612
613         if (wpa_s->off_channel_freq == freq || freq == 0) {
614                 /* Already on requested channel; send immediately */
615                 /* TODO: Would there ever be need to extend the current
616                  * duration on the channel? */
617                 wpa_s->pending_action_without_roc = 1;
618                 eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
619                 eloop_register_timeout(0, 0, wpas_send_action_cb, wpa_s, NULL);
620                 return 0;
621         }
622         wpa_s->pending_action_without_roc = 0;
623
624         wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
625                    "once the driver gets to the requested channel");
626         if (wait_time > wpa_s->max_remain_on_chan)
627                 wait_time = wpa_s->max_remain_on_chan;
628         if (wpa_drv_remain_on_channel(wpa_s, freq, wait_time) < 0) {
629                 wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
630                            "to remain on channel (%u MHz) for Action "
631                            "Frame TX", freq);
632                 return -1;
633         }
634
635         return 0;
636 }
637
638
639 static void wpas_send_action_done(void *ctx)
640 {
641         struct wpa_supplicant *wpa_s = ctx;
642         wpa_printf(MSG_DEBUG, "P2P: Action frame sequence done notification");
643         wpabuf_free(wpa_s->pending_action_tx);
644         wpa_s->pending_action_tx = NULL;
645         if (wpa_s->off_channel_freq) {
646                 wpa_drv_cancel_remain_on_channel(wpa_s);
647                 wpa_s->off_channel_freq = 0;
648         }
649 }
650
651
652 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
653                                     struct p2p_go_neg_results *params)
654 {
655         if (wpa_s->go_params == NULL) {
656                 wpa_s->go_params = os_malloc(sizeof(*params));
657                 if (wpa_s->go_params == NULL)
658                         return -1;
659         }
660         os_memcpy(wpa_s->go_params, params, sizeof(*params));
661         return 0;
662 }
663
664
665 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
666                                     struct p2p_go_neg_results *res)
667 {
668         wpa_supplicant_ap_deinit(wpa_s);
669         wpas_copy_go_neg_results(wpa_s, res);
670         if (res->wps_method == WPS_PBC)
671                 wpas_wps_start_pbc(wpa_s, NULL /* res->peer_interface_addr */,
672                                    1);
673         else
674                 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
675                                    wpa_s->p2p_pin, 1);
676 }
677
678
679 static void p2p_go_configured(void *ctx, void *data)
680 {
681         struct wpa_supplicant *wpa_s = ctx;
682         struct p2p_go_neg_results *params = data;
683         struct wpa_ssid *ssid;
684
685         ssid = wpa_s->current_ssid;
686         if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
687                 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
688                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
689                         "%s GO ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
690                         MACSTR "%s",
691                         wpa_s->ifname,
692                         wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
693                         params->passphrase ? params->passphrase : "",
694                         MAC2STR(wpa_s->parent->own_addr),
695                         params->persistent_group ? " [PERSISTENT]" : "");
696                 if (params->persistent_group)
697                         wpas_p2p_store_persistent_group(
698                                 wpa_s->parent, ssid,
699                                 wpa_s->parent->own_addr);
700                 return;
701         }
702
703         wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
704         if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
705                                               params->peer_interface_addr)) {
706                 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
707                            "filtering");
708                 return;
709         }
710         if (params->wps_method == WPS_PBC)
711                 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr);
712         else if (wpa_s->p2p_pin[0])
713                 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
714                                           wpa_s->p2p_pin, NULL, 0);
715         os_free(wpa_s->go_params);
716         wpa_s->go_params = NULL;
717 }
718
719
720 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
721                               struct p2p_go_neg_results *params,
722                               int group_formation)
723 {
724         struct wpa_ssid *ssid;
725
726         if (wpas_copy_go_neg_results(wpa_s, params) < 0)
727                 return;
728
729         ssid = wpa_config_add_network(wpa_s->conf);
730         if (ssid == NULL)
731                 return;
732
733         wpa_config_set_network_defaults(ssid);
734         ssid->temporary = 1;
735         ssid->p2p_group = 1;
736         ssid->p2p_persistent_group = params->persistent_group;
737         ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
738                 WPAS_MODE_P2P_GO;
739         ssid->frequency = params->freq;
740         ssid->ssid = os_zalloc(params->ssid_len + 1);
741         if (ssid->ssid) {
742                 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
743                 ssid->ssid_len = params->ssid_len;
744         }
745         ssid->auth_alg = WPA_AUTH_ALG_OPEN;
746         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
747         ssid->proto = WPA_PROTO_RSN;
748         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
749         ssid->passphrase = os_strdup(params->passphrase);
750
751         wpa_s->ap_configured_cb = p2p_go_configured;
752         wpa_s->ap_configured_cb_ctx = wpa_s;
753         wpa_s->ap_configured_cb_data = wpa_s->go_params;
754         wpa_s->connect_without_scan = 1;
755         wpa_s->reassociate = 1;
756         wpa_s->disconnected = 0;
757         wpa_supplicant_req_scan(wpa_s, 0, 0);
758 }
759
760
761 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
762                                   const struct wpa_supplicant *src)
763 {
764         struct wpa_config *d;
765         const struct wpa_config *s;
766
767         d = dst->conf;
768         s = src->conf;
769
770 #define C(n) if (s->n) d->n = os_strdup(s->n)
771         C(device_name);
772         C(manufacturer);
773         C(model_name);
774         C(model_number);
775         C(serial_number);
776         C(device_type);
777         C(config_methods);
778 #undef C
779 }
780
781
782 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
783                                         enum wpa_driver_if_type type)
784 {
785         char ifname[120], force_ifname[120];
786
787         if (wpa_s->pending_interface_name[0]) {
788                 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
789                            "- skip creation of a new one");
790                 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
791                         wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
792                                    "unknown?! ifname='%s'",
793                                    wpa_s->pending_interface_name);
794                         return -1;
795                 }
796                 return 0;
797         }
798
799         os_snprintf(ifname, sizeof(ifname), "%s-p2p-%d", wpa_s->ifname,
800                     wpa_s->p2p_group_idx);
801         force_ifname[0] = '\0';
802
803         wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
804                    ifname);
805         wpa_s->p2p_group_idx++;
806
807         wpa_s->pending_interface_type = type;
808         if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
809                            wpa_s->pending_interface_addr) < 0) {
810                 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
811                            "interface");
812                 return -1;
813         }
814
815         if (force_ifname[0]) {
816                 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
817                            force_ifname);
818                 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
819                            sizeof(wpa_s->pending_interface_name));
820         } else
821                 os_strlcpy(wpa_s->pending_interface_name, ifname,
822                            sizeof(wpa_s->pending_interface_name));
823         wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
824                    MACSTR, wpa_s->pending_interface_name,
825                    MAC2STR(wpa_s->pending_interface_addr));
826
827         return 0;
828 }
829
830
831 static void wpas_p2p_remove_pending_group_interface(
832         struct wpa_supplicant *wpa_s)
833 {
834         if (!wpa_s->pending_interface_name[0] ||
835             is_zero_ether_addr(wpa_s->pending_interface_addr))
836                 return; /* No pending virtual interface */
837
838         wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
839                    wpa_s->pending_interface_name);
840         wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
841                           wpa_s->pending_interface_name);
842         os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
843         wpa_s->pending_interface_name[0] = '\0';
844 }
845
846
847 static struct wpa_supplicant *
848 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
849 {
850         struct wpa_interface iface;
851         struct wpa_supplicant *group_wpa_s;
852
853         if (!wpa_s->pending_interface_name[0]) {
854                 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
855                 return NULL;
856         }
857
858         os_memset(&iface, 0, sizeof(iface));
859         iface.ifname = wpa_s->pending_interface_name;
860         iface.driver = wpa_s->driver->name;
861         iface.ctrl_interface = wpa_s->conf->ctrl_interface;
862         iface.driver_param = wpa_s->conf->driver_param;
863         group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
864         if (group_wpa_s == NULL) {
865                 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
866                            "wpa_supplicant interface");
867                 return NULL;
868         }
869         wpa_s->pending_interface_name[0] = '\0';
870         group_wpa_s->parent = wpa_s;
871         group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
872                 P2P_GROUP_INTERFACE_CLIENT;
873         wpa_s->global->p2p_group_formation = group_wpa_s;
874
875         wpas_p2p_clone_config(group_wpa_s, wpa_s);
876
877         return group_wpa_s;
878 }
879
880
881 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
882                                              void *timeout_ctx)
883 {
884         struct wpa_supplicant *wpa_s = eloop_ctx;
885         wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
886         if (wpa_s->global->p2p)
887                 p2p_group_formation_failed(wpa_s->global->p2p);
888         wpas_group_formation_completed(wpa_s, 0);
889 }
890
891
892 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
893 {
894         struct wpa_supplicant *wpa_s = ctx;
895
896         if (wpa_s->off_channel_freq) {
897                 wpa_drv_cancel_remain_on_channel(wpa_s);
898                 wpa_s->off_channel_freq = 0;
899         }
900
901         if (res->status) {
902                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
903                         res->status);
904                 wpas_p2p_remove_pending_group_interface(wpa_s);
905                 return;
906         }
907
908         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
909
910         if (wpa_s->create_p2p_iface) {
911                 struct wpa_supplicant *group_wpa_s =
912                         wpas_p2p_init_group_interface(wpa_s, res->role_go);
913                 if (group_wpa_s == NULL) {
914                         wpas_p2p_remove_pending_group_interface(wpa_s);
915                         return;
916                 }
917                 if (group_wpa_s != wpa_s)
918                         os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
919                                   sizeof(group_wpa_s->p2p_pin));
920                 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
921                 wpa_s->pending_interface_name[0] = '\0';
922                 group_wpa_s->p2p_in_provisioning = 1;
923
924                 if (res->role_go)
925                         wpas_start_wps_go(group_wpa_s, res, 1);
926                 else
927                         wpas_start_wps_enrollee(group_wpa_s, res);
928         } else {
929                 wpa_s->p2p_in_provisioning = 1;
930                 wpa_s->global->p2p_group_formation = wpa_s;
931
932                 if (res->role_go)
933                         wpas_start_wps_go(wpa_s, res, 1);
934                 else
935                         wpas_start_wps_enrollee(ctx, res);
936         }
937
938         wpa_s->p2p_long_listen = 0;
939         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
940
941         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
942         /* TODO: add peer Config Timeout */
943         eloop_register_timeout(15, 0, wpas_p2p_group_formation_timeout, wpa_s,
944                                NULL);
945 }
946
947
948 void wpas_go_neg_req_rx(void *ctx, const u8 *src)
949 {
950         struct wpa_supplicant *wpa_s = ctx;
951         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR,
952                 MAC2STR(src));
953 }
954
955
956 void wpas_dev_found(void *ctx, const u8 *addr, const u8 *dev_addr,
957                     const u8 *pri_dev_type, const char *dev_name,
958                     u16 config_methods, u8 dev_capab, u8 group_capab)
959 {
960         struct wpa_supplicant *wpa_s = ctx;
961         char devtype[WPS_DEV_TYPE_BUFSIZE];
962         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
963                 " p2p_dev_addr=" MACSTR
964                 " pri_dev_type=%s name='%s' config_methods=0x%x "
965                 "dev_capab=0x%x group_capab=0x%x",
966                 MAC2STR(addr), MAC2STR(dev_addr),
967                 wps_dev_type_bin2str(pri_dev_type, devtype, sizeof(devtype)),
968                 dev_name, config_methods, dev_capab, group_capab);
969 }
970
971
972 static int wpas_start_listen(void *ctx, unsigned int freq,
973                              unsigned int duration,
974                              const struct wpabuf *probe_resp_ie)
975 {
976         struct wpa_supplicant *wpa_s = ctx;
977
978         wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie);
979
980         if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
981                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
982                            "report received Probe Request frames");
983                 return -1;
984         }
985
986         wpa_s->pending_listen_freq = freq;
987         wpa_s->pending_listen_duration = duration;
988
989         if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
990                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
991                            "to remain on channel (%u MHz) for Listen "
992                            "state", freq);
993                 wpa_s->pending_listen_freq = 0;
994                 return -1;
995         }
996
997         return 0;
998 }
999
1000
1001 static void wpas_stop_listen(void *ctx)
1002 {
1003         struct wpa_supplicant *wpa_s = ctx;
1004         if (wpa_s->off_channel_freq) {
1005                 wpa_drv_cancel_remain_on_channel(wpa_s);
1006                 wpa_s->off_channel_freq = 0;
1007         }
1008         wpa_drv_probe_req_report(wpa_s, 0);
1009 }
1010
1011
1012 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1013 {
1014         struct wpa_supplicant *wpa_s = ctx;
1015         return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf));
1016 }
1017
1018
1019 static struct p2p_srv_bonjour *
1020 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1021                              const struct wpabuf *query)
1022 {
1023         struct p2p_srv_bonjour *bsrv;
1024         size_t len;
1025
1026         len = wpabuf_len(query);
1027         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1028                          struct p2p_srv_bonjour, list) {
1029                 if (len == wpabuf_len(bsrv->query) &&
1030                     os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1031                               len) == 0)
1032                         return bsrv;
1033         }
1034         return NULL;
1035 }
1036
1037
1038 static struct p2p_srv_upnp *
1039 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1040                           const char *service)
1041 {
1042         struct p2p_srv_upnp *usrv;
1043
1044         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1045                          struct p2p_srv_upnp, list) {
1046                 if (version == usrv->version &&
1047                     os_strcmp(service, usrv->service) == 0)
1048                         return usrv;
1049         }
1050         return NULL;
1051 }
1052
1053
1054 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1055                                         u8 srv_trans_id)
1056 {
1057         u8 *len_pos;
1058
1059         if (wpabuf_tailroom(resp) < 5)
1060                 return;
1061
1062         /* Length (to be filled) */
1063         len_pos = wpabuf_put(resp, 2);
1064         wpabuf_put_u8(resp, srv_proto);
1065         wpabuf_put_u8(resp, srv_trans_id);
1066         /* Status Code */
1067         wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1068         /* Response Data: empty */
1069         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1070 }
1071
1072
1073 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1074                                 struct wpabuf *resp, u8 srv_trans_id)
1075 {
1076         struct p2p_srv_bonjour *bsrv;
1077         u8 *len_pos;
1078
1079         wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1080
1081         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1082                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1083                 return;
1084         }
1085
1086         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1087                          struct p2p_srv_bonjour, list) {
1088                 if (wpabuf_tailroom(resp) <
1089                     5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1090                         return;
1091                 /* Length (to be filled) */
1092                 len_pos = wpabuf_put(resp, 2);
1093                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1094                 wpabuf_put_u8(resp, srv_trans_id);
1095                 /* Status Code */
1096                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1097                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1098                                   wpabuf_head(bsrv->resp),
1099                                   wpabuf_len(bsrv->resp));
1100                 /* Response Data */
1101                 wpabuf_put_buf(resp, bsrv->query); /* Key */
1102                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1103                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1104                              2);
1105         }
1106 }
1107
1108
1109 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1110                                 struct wpabuf *resp, u8 srv_trans_id,
1111                                 const u8 *query, size_t query_len)
1112 {
1113         struct p2p_srv_bonjour *bsrv;
1114         struct wpabuf buf;
1115         u8 *len_pos;
1116
1117         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1118                           query, query_len);
1119         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1120                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1121                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1122                                             srv_trans_id);
1123                 return;
1124         }
1125
1126         if (query_len == 0) {
1127                 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1128                 return;
1129         }
1130
1131         if (wpabuf_tailroom(resp) < 5)
1132                 return;
1133         /* Length (to be filled) */
1134         len_pos = wpabuf_put(resp, 2);
1135         wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1136         wpabuf_put_u8(resp, srv_trans_id);
1137
1138         wpabuf_set(&buf, query, query_len);
1139         bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1140         if (bsrv == NULL) {
1141                 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1142                            "available");
1143
1144                 /* Status Code */
1145                 wpabuf_put_u8(resp, P2P_SD_QUERY_DATA_NOT_AVAILABLE);
1146                 /* Response Data: empty */
1147                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1148                              2);
1149                 return;
1150         }
1151
1152         /* Status Code */
1153         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1154         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1155                           wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1156
1157         if (wpabuf_tailroom(resp) >=
1158             wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1159                 /* Response Data */
1160                 wpabuf_put_buf(resp, bsrv->query); /* Key */
1161                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1162         }
1163         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1164 }
1165
1166
1167 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1168                              struct wpabuf *resp, u8 srv_trans_id)
1169 {
1170         struct p2p_srv_upnp *usrv;
1171         u8 *len_pos;
1172
1173         wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1174
1175         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1176                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1177                 return;
1178         }
1179
1180         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1181                          struct p2p_srv_upnp, list) {
1182                 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1183                         return;
1184
1185                 /* Length (to be filled) */
1186                 len_pos = wpabuf_put(resp, 2);
1187                 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1188                 wpabuf_put_u8(resp, srv_trans_id);
1189
1190                 /* Status Code */
1191                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1192                 /* Response Data */
1193                 wpabuf_put_u8(resp, usrv->version);
1194                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1195                            usrv->service);
1196                 wpabuf_put_str(resp, usrv->service);
1197                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1198                              2);
1199         }
1200 }
1201
1202
1203 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1204                              struct wpabuf *resp, u8 srv_trans_id,
1205                              const u8 *query, size_t query_len)
1206 {
1207         struct p2p_srv_upnp *usrv;
1208         u8 *len_pos;
1209         u8 version;
1210         char *str;
1211         int count = 0;
1212
1213         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1214                           query, query_len);
1215
1216         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1217                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1218                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1219                                             srv_trans_id);
1220                 return;
1221         }
1222
1223         if (query_len == 0) {
1224                 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1225                 return;
1226         }
1227
1228         version = query[0];
1229         str = os_malloc(query_len);
1230         if (str == NULL)
1231                 return;
1232         os_memcpy(str, query + 1, query_len - 1);
1233         str[query_len - 1] = '\0';
1234
1235         if (wpabuf_tailroom(resp) < 5)
1236                 return;
1237
1238         /* Length (to be filled) */
1239         len_pos = wpabuf_put(resp, 2);
1240         wpabuf_put_u8(resp, P2P_SERV_UPNP);
1241         wpabuf_put_u8(resp, srv_trans_id);
1242
1243         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1244                          struct p2p_srv_upnp, list) {
1245                 if (version != usrv->version)
1246                         continue;
1247
1248                 if (os_strcmp(str, "ssdp:all") != 0 &&
1249                     os_strstr(usrv->service, str) == NULL)
1250                         continue;
1251
1252                 if (wpabuf_tailroom(resp) < 2)
1253                         break;
1254                 if (count == 0) {
1255                         /* Status Code */
1256                         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1257                         /* Response Data */
1258                         wpabuf_put_u8(resp, version);
1259                 } else
1260                         wpabuf_put_u8(resp, ',');
1261
1262                 count++;
1263
1264                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1265                            usrv->service);
1266                 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1267                         break;
1268                 wpabuf_put_str(resp, usrv->service);
1269         }
1270
1271         if (count == 0) {
1272                 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1273                            "available");
1274                 /* Status Code */
1275                 wpabuf_put_u8(resp, P2P_SD_QUERY_DATA_NOT_AVAILABLE);
1276                 /* Response Data: empty */
1277         }
1278
1279         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1280 }
1281
1282
1283 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1284                      u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1285 {
1286         struct wpa_supplicant *wpa_s = ctx;
1287         const u8 *pos = tlvs;
1288         const u8 *end = tlvs + tlvs_len;
1289         const u8 *tlv_end;
1290         u16 slen;
1291         struct wpabuf *resp;
1292         u8 srv_proto, srv_trans_id;
1293         size_t buf_len;
1294         char *buf;
1295
1296         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1297                     tlvs, tlvs_len);
1298         buf_len = 2 * tlvs_len + 1;
1299         buf = os_malloc(buf_len);
1300         if (buf) {
1301                 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1302                 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1303                              MACSTR " %u %u %s",
1304                              freq, MAC2STR(sa), dialog_token, update_indic,
1305                              buf);
1306                 os_free(buf);
1307         }
1308
1309         if (wpa_s->p2p_sd_over_ctrl_iface)
1310                 return; /* to be processed by an external program */
1311
1312         resp = wpabuf_alloc(10000);
1313         if (resp == NULL)
1314                 return;
1315
1316         while (pos + 1 < end) {
1317                 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1318                 slen = WPA_GET_LE16(pos);
1319                 pos += 2;
1320                 if (pos + slen > end || slen < 2) {
1321                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1322                                    "length");
1323                         wpabuf_free(resp);
1324                         return;
1325                 }
1326                 tlv_end = pos + slen;
1327
1328                 srv_proto = *pos++;
1329                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1330                            srv_proto);
1331                 srv_trans_id = *pos++;
1332                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1333                            srv_trans_id);
1334
1335                 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1336                             pos, tlv_end - pos);
1337
1338                 switch (srv_proto) {
1339                 case P2P_SERV_ALL_SERVICES:
1340                         wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1341                                    "for all services");
1342                         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1343                             dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1344                                 wpa_printf(MSG_DEBUG, "P2P: No service "
1345                                            "discovery protocols available");
1346                                 wpas_sd_add_proto_not_avail(
1347                                         resp, P2P_SERV_ALL_SERVICES,
1348                                         srv_trans_id);
1349                                 break;
1350                         }
1351                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1352                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1353                         break;
1354                 case P2P_SERV_BONJOUR:
1355                         wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1356                                             pos, tlv_end - pos);
1357                         break;
1358                 case P2P_SERV_UPNP:
1359                         wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1360                                          pos, tlv_end - pos);
1361                         break;
1362                 default:
1363                         wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1364                                    "protocol %u", srv_proto);
1365                         wpas_sd_add_proto_not_avail(resp, srv_proto,
1366                                                     srv_trans_id);
1367                         break;
1368                 }
1369
1370                 pos = tlv_end;
1371         }
1372
1373         wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1374
1375         wpabuf_free(resp);
1376 }
1377
1378
1379 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1380                       const u8 *tlvs, size_t tlvs_len)
1381 {
1382         struct wpa_supplicant *wpa_s = ctx;
1383         const u8 *pos = tlvs;
1384         const u8 *end = tlvs + tlvs_len;
1385         const u8 *tlv_end;
1386         u16 slen;
1387         size_t buf_len;
1388         char *buf;
1389
1390         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1391                     tlvs, tlvs_len);
1392         buf_len = 2 * tlvs_len + 1;
1393         buf = os_malloc(buf_len);
1394         if (buf) {
1395                 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1396                 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_RESP MACSTR
1397                              " %u %s",
1398                              MAC2STR(sa), update_indic, buf);
1399                 os_free(buf);
1400         }
1401
1402         while (pos < end) {
1403                 u8 srv_proto, srv_trans_id, status;
1404
1405                 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1406                 slen = WPA_GET_LE16(pos);
1407                 pos += 2;
1408                 if (pos + slen > end || slen < 3) {
1409                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1410                                    "length");
1411                         return;
1412                 }
1413                 tlv_end = pos + slen;
1414
1415                 srv_proto = *pos++;
1416                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1417                            srv_proto);
1418                 srv_trans_id = *pos++;
1419                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1420                            srv_trans_id);
1421                 status = *pos++;
1422                 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1423                            status);
1424
1425                 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1426                             pos, tlv_end - pos);
1427
1428                 pos = tlv_end;
1429         }
1430 }
1431
1432
1433 void * wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1434                            const struct wpabuf *tlvs)
1435 {
1436         return p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1437 }
1438
1439
1440 void * wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1441                                 u8 version, const char *query)
1442 {
1443         struct wpabuf *tlvs;
1444         void *ret;
1445
1446         tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1447         if (tlvs == NULL)
1448                 return NULL;
1449         wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1450         wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1451         wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1452         wpabuf_put_u8(tlvs, version);
1453         wpabuf_put_str(tlvs, query);
1454         ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1455         wpabuf_free(tlvs);
1456         return ret;
1457 }
1458
1459
1460 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, void *req)
1461 {
1462         return p2p_sd_cancel_request(wpa_s->global->p2p, req);
1463 }
1464
1465
1466 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1467                           const u8 *dst, u8 dialog_token,
1468                           const struct wpabuf *resp_tlvs)
1469 {
1470         p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1471                         resp_tlvs);
1472 }
1473
1474
1475 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1476 {
1477         p2p_sd_service_update(wpa_s->global->p2p);
1478 }
1479
1480
1481 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1482 {
1483         dl_list_del(&bsrv->list);
1484         wpabuf_free(bsrv->query);
1485         wpabuf_free(bsrv->resp);
1486         os_free(bsrv);
1487 }
1488
1489
1490 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1491 {
1492         dl_list_del(&usrv->list);
1493         os_free(usrv->service);
1494         os_free(usrv);
1495 }
1496
1497
1498 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1499 {
1500         struct p2p_srv_bonjour *bsrv, *bn;
1501         struct p2p_srv_upnp *usrv, *un;
1502
1503         dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1504                               struct p2p_srv_bonjour, list)
1505                 wpas_p2p_srv_bonjour_free(bsrv);
1506
1507         dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1508                               struct p2p_srv_upnp, list)
1509                 wpas_p2p_srv_upnp_free(usrv);
1510
1511         wpas_p2p_sd_service_update(wpa_s);
1512 }
1513
1514
1515 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1516                                  struct wpabuf *query, struct wpabuf *resp)
1517 {
1518         struct p2p_srv_bonjour *bsrv;
1519
1520         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1521         if (bsrv) {
1522                 wpabuf_free(query);
1523                 wpabuf_free(bsrv->resp);
1524                 bsrv->resp = resp;
1525                 return 0;
1526         }
1527
1528         bsrv = os_zalloc(sizeof(*bsrv));
1529         if (bsrv == NULL)
1530                 return -1;
1531         bsrv->query = query;
1532         bsrv->resp = resp;
1533         dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1534
1535         wpas_p2p_sd_service_update(wpa_s);
1536         return 0;
1537 }
1538
1539
1540 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1541                                  const struct wpabuf *query)
1542 {
1543         struct p2p_srv_bonjour *bsrv;
1544
1545         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1546         if (bsrv == NULL)
1547                 return -1;
1548         wpas_p2p_srv_bonjour_free(bsrv);
1549         wpas_p2p_sd_service_update(wpa_s);
1550         return 0;
1551 }
1552
1553
1554 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
1555                               const char *service)
1556 {
1557         struct p2p_srv_upnp *usrv;
1558
1559         if (wpas_p2p_service_get_upnp(wpa_s, version, service))
1560                 return 0; /* Already listed */
1561         usrv = os_zalloc(sizeof(*usrv));
1562         if (usrv == NULL)
1563                 return -1;
1564         usrv->version = version;
1565         usrv->service = os_strdup(service);
1566         if (usrv->service == NULL) {
1567                 os_free(usrv);
1568                 return -1;
1569         }
1570         dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
1571
1572         wpas_p2p_sd_service_update(wpa_s);
1573         return 0;
1574 }
1575
1576
1577 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
1578                               const char *service)
1579 {
1580         struct p2p_srv_upnp *usrv;
1581
1582         usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
1583         if (usrv == NULL)
1584                 return -1;
1585         wpas_p2p_srv_upnp_free(usrv);
1586         wpas_p2p_sd_service_update(wpa_s);
1587         return 0;
1588 }
1589
1590
1591 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
1592                                          const u8 *peer, const char *params)
1593 {
1594         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
1595                 MAC2STR(peer), wps_generate_pin(), params);
1596 }
1597
1598
1599 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
1600                                         const u8 *peer, const char *params)
1601 {
1602         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
1603                 MAC2STR(peer), params);
1604 }
1605
1606
1607 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
1608                         const u8 *dev_addr, const u8 *pri_dev_type,
1609                         const char *dev_name, u16 supp_config_methods,
1610                         u8 dev_capab, u8 group_capab)
1611 {
1612         struct wpa_supplicant *wpa_s = ctx;
1613         char devtype[WPS_DEV_TYPE_BUFSIZE];
1614         char params[200];
1615         u8 empty_dev_type[8];
1616
1617         if (pri_dev_type == NULL) {
1618                 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
1619                 pri_dev_type = empty_dev_type;
1620         }
1621         os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
1622                     " pri_dev_type=%s name='%s' config_methods=0x%x "
1623                     "dev_capab=0x%x group_capab=0x%x",
1624                     MAC2STR(dev_addr),
1625                     wps_dev_type_bin2str(pri_dev_type, devtype,
1626                                          sizeof(devtype)),
1627                     dev_name, supp_config_methods, dev_capab, group_capab);
1628         params[sizeof(params) - 1] = '\0';
1629
1630         if (config_methods & WPS_CONFIG_DISPLAY)
1631                 wpas_prov_disc_local_display(wpa_s, peer, params);
1632         else if (config_methods & WPS_CONFIG_KEYPAD)
1633                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
1634         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1635                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
1636                         "%s", MAC2STR(peer), params);
1637 }
1638
1639
1640 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
1641 {
1642         struct wpa_supplicant *wpa_s = ctx;
1643         if (config_methods & WPS_CONFIG_DISPLAY)
1644                 wpas_prov_disc_local_keypad(wpa_s, peer, "");
1645         else if (config_methods & WPS_CONFIG_KEYPAD)
1646                 wpas_prov_disc_local_display(wpa_s, peer, "");
1647         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
1648                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR,
1649                         MAC2STR(peer));
1650 }
1651
1652
1653 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
1654                                   const u8 *go_dev_addr, const u8 *ssid,
1655                                   size_t ssid_len, int *go, u8 *group_bssid,
1656                                   int *force_freq, int persistent_group)
1657 {
1658         struct wpa_supplicant *wpa_s = ctx;
1659         struct wpa_ssid *s;
1660         u8 cur_bssid[ETH_ALEN];
1661         int res;
1662
1663         if (!persistent_group) {
1664                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1665                            " to join an active group", MAC2STR(sa));
1666                 /*
1667                  * Do not accept the invitation automatically; notify user and
1668                  * request approval.
1669                  */
1670                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1671         }
1672
1673         if (!wpa_s->conf->persistent_reconnect)
1674                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
1675
1676         for (s = wpa_s->conf->ssid; s; s = s->next) {
1677                 if (s->disabled == 2 &&
1678                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
1679                     s->ssid_len == ssid_len &&
1680                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
1681                         break;
1682         }
1683
1684         if (!s) {
1685                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
1686                            " requested reinvocation of an unknown group",
1687                            MAC2STR(sa));
1688                 return P2P_SC_FAIL_UNKNOWN_GROUP;
1689         }
1690
1691         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
1692                 *go = 1;
1693                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1694                         wpa_printf(MSG_DEBUG, "P2P: The only available "
1695                                    "interface is already in use - reject "
1696                                    "invitation");
1697                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1698                 }
1699                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
1700         } else if (s->mode == WPAS_MODE_P2P_GO) {
1701                 *go = 1;
1702                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
1703                 {
1704                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
1705                                    "interface address for the group");
1706                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
1707                 }
1708                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
1709                           ETH_ALEN);
1710         }
1711
1712         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
1713             wpa_s->assoc_freq) {
1714                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1715                            "the channel we are already using");
1716                 *force_freq = wpa_s->assoc_freq;
1717         }
1718
1719         res = wpa_drv_shared_freq(wpa_s);
1720         if (res > 0) {
1721                 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
1722                            "with the channel we are already using on a "
1723                            "shared interface");
1724                 *force_freq = res;
1725         }
1726
1727         return P2P_SC_SUCCESS;
1728 }
1729
1730
1731 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
1732                                      const u8 *ssid, size_t ssid_len,
1733                                      const u8 *go_dev_addr, u8 status,
1734                                      int op_freq)
1735 {
1736         struct wpa_supplicant *wpa_s = ctx;
1737         struct wpa_ssid *s;
1738
1739         for (s = wpa_s->conf->ssid; s; s = s->next) {
1740                 if (s->disabled == 2 &&
1741                     s->ssid_len == ssid_len &&
1742                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
1743                         break;
1744         }
1745
1746         if (status == P2P_SC_SUCCESS) {
1747                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1748                            " was accepted; op_freq=%d MHz",
1749                            MAC2STR(sa), op_freq);
1750                 if (s) {
1751                         wpas_p2p_group_add_persistent(
1752                                 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0);
1753                 }
1754                 return;
1755         }
1756
1757         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
1758                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
1759                            " was rejected (status %u)", MAC2STR(sa), status);
1760                 return;
1761         }
1762
1763         if (!s) {
1764                 if (bssid) {
1765                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1766                                 "sa=" MACSTR " go_dev_addr=" MACSTR
1767                                 " bssid=" MACSTR " unknown-network",
1768                                 MAC2STR(sa), MAC2STR(go_dev_addr),
1769                                 MAC2STR(bssid));
1770                 } else {
1771                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
1772                                 "sa=" MACSTR " go_dev_addr=" MACSTR
1773                                 " unknown-network",
1774                                 MAC2STR(sa), MAC2STR(go_dev_addr));
1775                 }
1776                 return;
1777         }
1778
1779         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
1780                 " persistent=%d", MAC2STR(sa), s->id);
1781 }
1782
1783
1784 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
1785 {
1786         struct wpa_supplicant *wpa_s = ctx;
1787         struct wpa_ssid *ssid;
1788
1789         if (bssid) {
1790                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1791                         "status=%d " MACSTR,
1792                         status, MAC2STR(bssid));
1793         } else {
1794                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
1795                         "status=%d ", status);
1796         }
1797
1798         if (status != P2P_SC_SUCCESS) {
1799                 wpas_p2p_remove_pending_group_interface(wpa_s);
1800                 return;
1801         }
1802
1803         ssid = wpa_config_get_network(wpa_s->conf,
1804                                       wpa_s->pending_invite_ssid_id);
1805         if (ssid == NULL) {
1806                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
1807                            "data matching with invitation");
1808                 return;
1809         }
1810
1811         wpas_p2p_group_add_persistent(wpa_s, ssid,
1812                                       ssid->mode == WPAS_MODE_P2P_GO, 0);
1813 }
1814
1815
1816 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
1817                                    struct p2p_config *p2p)
1818 {
1819         struct hostapd_hw_modes *modes;
1820         u16 num_modes, flags;
1821         int i, cla;
1822         int band24 = 0, band5_low = 0, band5_high = 0;
1823
1824         /* TODO: more detailed selection of channels within reg_class based on
1825          * driver capabilities */
1826
1827         modes = wpa_drv_get_hw_feature_data(wpa_s, &num_modes, &flags);
1828         if (modes == NULL) {
1829                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
1830                            "of all supported channels; assume dualband "
1831                            "support");
1832                 band24 = band5_low = band5_high = 1;
1833         } else {
1834                 for (i = 0; i < num_modes; i++) {
1835                         struct hostapd_hw_modes *mode;
1836                         mode = &modes[i];
1837                         if (mode->mode == HOSTAPD_MODE_IEEE80211G) {
1838                                 band24 = 1;
1839                         } else if (mode->mode == HOSTAPD_MODE_IEEE80211A) {
1840                                 int j;
1841                                 for (j = 0; j < mode->num_channels; j++) {
1842                                         struct hostapd_channel_data *ch;
1843                                         ch = &mode->channels[j];
1844                                         if (ch->chan == 36)
1845                                                 band5_low = 1;
1846                                         else if (ch->chan == 157)
1847                                                 band5_high = 1;
1848                                 }
1849                         }
1850                 }
1851         }
1852
1853         cla = 0;
1854
1855         if (band24) {
1856                 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1857                            "2.4 GHz band");
1858
1859                 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1860                 p2p->channels.reg_class[cla].reg_class = 81;
1861                 p2p->channels.reg_class[cla].channels = 13;
1862                 for (i = 0; i < 13; i++)
1863                         p2p->channels.reg_class[cla].channel[i] = i + 1;
1864                 cla++;
1865
1866                 /* Operating class 82 - 2.4 GHz band channel 14 */
1867                 p2p->channels.reg_class[cla].reg_class = 82;
1868                 p2p->channels.reg_class[cla].channels = 1;
1869                 p2p->channels.reg_class[cla].channel[0] = 14;
1870                 cla++;
1871
1872 #if 0
1873                 /* Operating class 83 - 2.4 GHz band channels 1..9; 40 MHz */
1874                 p2p->channels.reg_class[cla].reg_class = 83;
1875                 p2p->channels.reg_class[cla].channels = 9;
1876                 for (i = 0; i < 9; i++)
1877                         p2p->channels.reg_class[cla].channel[i] = i + 1;
1878                 cla++;
1879
1880                 /* Operating class 84 - 2.4 GHz band channels 5..13; 40 MHz */
1881                 p2p->channels.reg_class[cla].reg_class = 84;
1882                 p2p->channels.reg_class[cla].channels = 9;
1883                 for (i = 0; i < 9; i++)
1884                         p2p->channels.reg_class[cla].channel[i] = i + 5;
1885                 cla++;
1886 #endif
1887         }
1888
1889         if (band5_low) {
1890                 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1891                            "lower 5 GHz band");
1892
1893                 /* Operating class 115 - 5 GHz, channels 36-48 */
1894                 p2p->channels.reg_class[cla].reg_class = 115;
1895                 p2p->channels.reg_class[cla].channels = 4;
1896                 p2p->channels.reg_class[cla].channel[0] = 36;
1897                 p2p->channels.reg_class[cla].channel[1] = 40;
1898                 p2p->channels.reg_class[cla].channel[2] = 44;
1899                 p2p->channels.reg_class[cla].channel[3] = 48;
1900                 cla++;
1901
1902 #if 0
1903                 /* Operating class 116 - 5 GHz, channels 36,44; 40 MHz */
1904                 p2p->channels.reg_class[cla].reg_class = 116;
1905                 p2p->channels.reg_class[cla].channels = 2;
1906                 p2p->channels.reg_class[cla].channel[0] = 36;
1907                 p2p->channels.reg_class[cla].channel[1] = 44;
1908                 cla++;
1909
1910                 /* Operating class 117 - 5 GHz, channels 40,48; 40 MHz */
1911                 p2p->channels.reg_class[cla].reg_class = 117;
1912                 p2p->channels.reg_class[cla].channels = 2;
1913                 p2p->channels.reg_class[cla].channel[0] = 40;
1914                 p2p->channels.reg_class[cla].channel[1] = 48;
1915                 cla++;
1916 #endif
1917         }
1918
1919         if (band5_high) {
1920                 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for "
1921                            "higher 5 GHz band");
1922
1923                 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1924                 p2p->channels.reg_class[cla].reg_class = 124;
1925                 p2p->channels.reg_class[cla].channels = 4;
1926                 p2p->channels.reg_class[cla].channel[0] = 149;
1927                 p2p->channels.reg_class[cla].channel[1] = 153;
1928                 p2p->channels.reg_class[cla].channel[2] = 157;
1929                 p2p->channels.reg_class[cla].channel[3] = 161;
1930                 cla++;
1931
1932 #if 0
1933                 /* Operating class 126 - 5 GHz, channels 149,157; 40 MHz */
1934                 p2p->channels.reg_class[cla].reg_class = 126;
1935                 p2p->channels.reg_class[cla].channels = 2;
1936                 p2p->channels.reg_class[cla].channel[0] = 149;
1937                 p2p->channels.reg_class[cla].channel[1] = 157;
1938                 cla++;
1939
1940                 /* Operating class 127 - 5 GHz, channels 153,161; 40 MHz */
1941                 p2p->channels.reg_class[cla].reg_class = 127;
1942                 p2p->channels.reg_class[cla].channels = 2;
1943                 p2p->channels.reg_class[cla].channel[0] = 153;
1944                 p2p->channels.reg_class[cla].channel[1] = 161;
1945                 cla++;
1946 #endif
1947         }
1948
1949         p2p->channels.reg_classes = cla;
1950
1951         if (modes)
1952                 ieee80211_sta_free_hw_features(modes, num_modes);
1953
1954         return 0;
1955 }
1956
1957
1958 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
1959                         size_t buf_len)
1960 {
1961         struct wpa_supplicant *wpa_s = ctx;
1962
1963         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1964                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
1965                         break;
1966         }
1967         if (wpa_s == NULL)
1968                 return -1;
1969
1970         return wpa_drv_get_noa(wpa_s, buf, buf_len);
1971 }
1972
1973
1974 /**
1975  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
1976  * @global: Pointer to global data from wpa_supplicant_init()
1977  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
1978  * Returns: 0 on success, -1 on failure
1979  */
1980 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
1981 {
1982         struct p2p_config p2p;
1983         unsigned int r;
1984         int i;
1985
1986         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
1987                 return 0;
1988
1989 #ifdef CONFIG_CLIENT_MLME
1990         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) {
1991                 wpa_s->mlme.public_action_cb = p2p_rx_action_mlme;
1992                 wpa_s->mlme.public_action_cb_ctx = wpa_s;
1993         }
1994 #endif /* CONFIG_CLIENT_MLME */
1995
1996         if (wpa_drv_disable_11b_rates(wpa_s, 1) < 0) {
1997                 wpa_printf(MSG_DEBUG, "P2P: Failed to disable 11b rates");
1998                 /* Continue anyway; this is not really a fatal error */
1999         }
2000
2001         if (global->p2p)
2002                 return 0;
2003
2004         os_memset(&p2p, 0, sizeof(p2p));
2005         p2p.msg_ctx = wpa_s;
2006         p2p.cb_ctx = wpa_s;
2007         p2p.p2p_scan = wpas_p2p_scan;
2008         p2p.send_action = wpas_send_action;
2009         p2p.send_action_done = wpas_send_action_done;
2010         p2p.go_neg_completed = wpas_go_neg_completed;
2011         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2012         p2p.dev_found = wpas_dev_found;
2013         p2p.start_listen = wpas_start_listen;
2014         p2p.stop_listen = wpas_stop_listen;
2015         p2p.send_probe_resp = wpas_send_probe_resp;
2016         p2p.sd_request = wpas_sd_request;
2017         p2p.sd_response = wpas_sd_response;
2018         p2p.prov_disc_req = wpas_prov_disc_req;
2019         p2p.prov_disc_resp = wpas_prov_disc_resp;
2020         p2p.invitation_process = wpas_invitation_process;
2021         p2p.invitation_received = wpas_invitation_received;
2022         p2p.invitation_result = wpas_invitation_result;
2023         p2p.get_noa = wpas_get_noa;
2024
2025         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2026         os_memcpy(p2p.dev_addr, wpa_s->own_addr, ETH_ALEN);
2027         p2p.dev_name = wpa_s->conf->device_name;
2028
2029         if (wpa_s->conf->p2p_listen_reg_class &&
2030             wpa_s->conf->p2p_listen_channel) {
2031                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2032                 p2p.channel = wpa_s->conf->p2p_listen_channel;
2033         } else {
2034                 p2p.reg_class = 81;
2035                 /*
2036                  * Pick one of the social channels randomly as the listen
2037                  * channel.
2038                  */
2039                 os_get_random((u8 *) &r, sizeof(r));
2040                 p2p.channel = 1 + (r % 3) * 5;
2041         }
2042
2043         if (wpa_s->conf->p2p_oper_reg_class &&
2044             wpa_s->conf->p2p_oper_channel) {
2045                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2046                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2047         } else {
2048                 p2p.op_reg_class = 81;
2049                 /*
2050                  * For initial tests, pick the operation channel randomly.
2051                  * TODO: Use scan results (etc.) to select the best channel.
2052                  */
2053                 p2p.op_channel = 1 + r % 11;
2054         }
2055         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d  "
2056                    "Own preferred operation channel: %d",
2057                    p2p.channel, p2p.op_channel);
2058         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2059                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
2060                 p2p.country[2] = 0x04;
2061         } else
2062                 os_memcpy(p2p.country, "US\x04", 3);
2063
2064         if (wpas_p2p_setup_channels(wpa_s, &p2p)) {
2065                 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2066                            "channel list");
2067                 return -1;
2068         }
2069
2070         if (wpa_s->conf->device_type &&
2071             wps_dev_type_str2bin(wpa_s->conf->device_type, p2p.pri_dev_type) <
2072             0) {
2073                 wpa_printf(MSG_ERROR, "P2P: Invalid device_type");
2074                 return -1;
2075         }
2076
2077         for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
2078                 if (wpa_s->conf->sec_device_type[i] == NULL)
2079                         continue;
2080                 if (wps_dev_type_str2bin(
2081                             wpa_s->conf->sec_device_type[i],
2082                             p2p.sec_dev_type[p2p.num_sec_dev_types]) < 0) {
2083                         wpa_printf(MSG_ERROR, "P2P: Invalid sec_device_type");
2084                         return -1;
2085                 }
2086                 p2p.num_sec_dev_types++;
2087                 if (p2p.num_sec_dev_types == P2P_SEC_DEVICE_TYPES)
2088                         break;
2089         }
2090
2091         p2p.concurrent_operations = !!(wpa_s->drv_flags &
2092                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2093
2094         p2p.max_peers = 100;
2095
2096         if (wpa_s->conf->p2p_ssid_postfix) {
2097                 p2p.ssid_postfix_len =
2098                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
2099                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2100                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2101                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2102                           p2p.ssid_postfix_len);
2103         }
2104
2105         global->p2p = p2p_init(&p2p);
2106         if (global->p2p == NULL)
2107                 return -1;
2108
2109         return 0;
2110 }
2111
2112
2113 /**
2114  * wpas_p2p_deinit - Deinitialize per-interface P2P data
2115  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2116  *
2117  * This function deinitialize per-interface P2P data.
2118  */
2119 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2120 {
2121         if (wpa_s->driver && wpa_s->drv_priv)
2122                 wpa_drv_probe_req_report(wpa_s, 0);
2123         os_free(wpa_s->go_params);
2124         wpa_s->go_params = NULL;
2125         wpabuf_free(wpa_s->pending_action_tx);
2126         wpa_s->pending_action_tx = NULL;
2127         eloop_cancel_timeout(wpas_send_action_cb, wpa_s, NULL);
2128         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2129         wpa_s->p2p_long_listen = 0;
2130         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2131         wpas_p2p_remove_pending_group_interface(wpa_s);
2132
2133         /* TODO: remove group interface from the driver if this wpa_s instance
2134          * is on top of a P2P group interface */
2135 }
2136
2137
2138 /**
2139  * wpas_p2p_deinit_global - Deinitialize global P2P module
2140  * @global: Pointer to global data from wpa_supplicant_init()
2141  *
2142  * This function deinitializes the global (per device) P2P module.
2143  */
2144 void wpas_p2p_deinit_global(struct wpa_global *global)
2145 {
2146         struct wpa_supplicant *wpa_s, *tmp;
2147         char *ifname;
2148
2149         if (global->p2p == NULL)
2150                 return;
2151
2152         /* Remove remaining P2P group interfaces */
2153         wpa_s = global->ifaces;
2154         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2155                 wpa_s = wpa_s->next;
2156         while (wpa_s) {
2157                 enum wpa_driver_if_type type;
2158                 tmp = global->ifaces;
2159                 while (tmp &&
2160                        (tmp == wpa_s ||
2161                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2162                         tmp = tmp->next;
2163                 }
2164                 if (tmp == NULL)
2165                         break;
2166                 ifname = os_strdup(tmp->ifname);
2167                 type = wpas_p2p_if_type(tmp->p2p_group_interface);
2168                 wpa_supplicant_remove_iface(global, tmp);
2169                 if (ifname)
2170                         wpa_drv_if_remove(wpa_s, type, ifname);
2171                 os_free(ifname);
2172         }
2173
2174         p2p_deinit(global->p2p);
2175         global->p2p = NULL;
2176 }
2177
2178
2179 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2180 {
2181         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
2182                 return 1; /* P2P group requires a new interface in every case
2183                            */
2184         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2185                 return 0; /* driver does not support concurrent operations */
2186         if (wpa_s->global->ifaces->next)
2187                 return 1; /* more that one interface already in use */
2188         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2189                 return 1; /* this interface is already in use */
2190         return 0;
2191 }
2192
2193
2194 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2195                                  const u8 *peer_addr,
2196                                  enum p2p_wps_method wps_method,
2197                                  int go_intent, const u8 *own_interface_addr,
2198                                  unsigned int force_freq, int persistent_group)
2199 {
2200         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2201                            go_intent, own_interface_addr, force_freq,
2202                            persistent_group);
2203 }
2204
2205
2206 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2207                                 const u8 *peer_addr,
2208                                 enum p2p_wps_method wps_method,
2209                                 int go_intent, const u8 *own_interface_addr,
2210                                 unsigned int force_freq, int persistent_group)
2211 {
2212         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2213                              go_intent, own_interface_addr, force_freq,
2214                              persistent_group);
2215 }
2216
2217
2218 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
2219                          const u8 *dev_addr, enum p2p_wps_method wps_method)
2220 {
2221         struct wpa_bss *bss;
2222
2223         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
2224                    MACSTR " dev " MACSTR ")",
2225                    MAC2STR(iface_addr), MAC2STR(dev_addr));
2226
2227         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
2228         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
2229         wpa_s->pending_join_wps_method = wps_method;
2230
2231         /* Make sure we are not running find during connection establishment */
2232         wpas_p2p_stop_find(wpa_s);
2233
2234         bss = wpa_bss_get_bssid(wpa_s, iface_addr);
2235         if (bss) {
2236                 u16 method;
2237
2238                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
2239                            "prior to joining an existing group (GO " MACSTR
2240                            " freq=%u MHz)",
2241                            MAC2STR(dev_addr), bss->freq);
2242                 wpa_s->pending_pd_before_join = 1;
2243
2244                 switch (wps_method) {
2245                 case WPS_PIN_LABEL:
2246                 case WPS_PIN_DISPLAY:
2247                         method = WPS_CONFIG_KEYPAD;
2248                         break;
2249                 case WPS_PIN_KEYPAD:
2250                         method = WPS_CONFIG_DISPLAY;
2251                         break;
2252                 case WPS_PBC:
2253                         method = WPS_CONFIG_PUSHBUTTON;
2254                         break;
2255                 default:
2256                         method = 0;
2257                         break;
2258                 }
2259
2260                 if (p2p_prov_disc_req(wpa_s->global->p2p, dev_addr, method, 1)
2261                     < 0) {
2262                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
2263                                    "Discovery Request before joining an "
2264                                    "existing group");
2265                         wpa_s->pending_pd_before_join = 0;
2266                         goto start;
2267                 }
2268
2269                 /*
2270                  * Actual join operation will be started from the Action frame
2271                  * TX status callback.
2272                  */
2273                 return 0;
2274         }
2275
2276         wpa_printf(MSG_DEBUG, "P2P: Target BSS/GO not yet in BSS table - "
2277                    "cannot send Provision Discovery Request");
2278
2279 start:
2280         /* Start join operation immediately */
2281         return wpas_p2p_join_start(wpa_s);
2282 }
2283
2284
2285 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
2286 {
2287         struct wpa_supplicant *group;
2288         struct p2p_go_neg_results res;
2289
2290         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
2291         if (group == NULL)
2292                 return -1;
2293         if (group != wpa_s)
2294                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
2295                           sizeof(group->p2p_pin));
2296
2297         group->p2p_in_provisioning = 1;
2298
2299         os_memset(&res, 0, sizeof(res));
2300         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
2301                   ETH_ALEN);
2302         res.wps_method = wpa_s->pending_join_wps_method;
2303         wpas_start_wps_enrollee(group, &res);
2304
2305         return 0;
2306 }
2307
2308
2309 /**
2310  * wpas_p2p_connect - Request P2P Group Formation to be started
2311  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2312  * @peer_addr: Address of the peer P2P Device
2313  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
2314  * @persistent_group: Whether to create a persistent group
2315  * @join: Whether to join an existing group (as a client) instead of starting
2316  *      Group Owner negotiation; @peer_addr is BSSID in that case
2317  * @auth: Whether to only authorize the connection instead of doing that and
2318  *      initiating Group Owner negotiation
2319  * @go_intent: GO Intent or -1 to use default
2320  * @freq: Frequency for the group or 0 for auto-selection
2321  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on failure
2322  */
2323 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2324                      const char *pin, enum p2p_wps_method wps_method,
2325                      int persistent_group, int join, int auth, int go_intent,
2326                      int freq)
2327 {
2328         int force_freq = 0;
2329         u8 bssid[ETH_ALEN];
2330         int ret = 0;
2331         enum wpa_driver_if_type iftype;
2332
2333         if (go_intent < 0)
2334                 go_intent = wpa_s->conf->p2p_go_intent;
2335
2336         if (!auth)
2337                 wpa_s->p2p_long_listen = 0;
2338
2339         if (pin)
2340                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
2341         else if (wps_method == WPS_PIN_DISPLAY) {
2342                 ret = wps_generate_pin();
2343                 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
2344                             ret);
2345                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
2346                            wpa_s->p2p_pin);
2347         } else
2348                 wpa_s->p2p_pin[0] = '\0';
2349
2350         if (join) {
2351                 u8 iface_addr[ETH_ALEN];
2352                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
2353                                            iface_addr) < 0)
2354                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
2355                 if (wpas_p2p_join(wpa_s, iface_addr, peer_addr, wps_method) <
2356                     0)
2357                         return -1;
2358                 return ret;
2359         }
2360
2361         if (freq > 0)
2362                 force_freq = freq;
2363         else if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2364                  wpa_s->assoc_freq)
2365                 force_freq = wpa_s->assoc_freq;
2366         else {
2367                 force_freq = wpa_drv_shared_freq(wpa_s);
2368                 if (force_freq < 0)
2369                         force_freq = 0;
2370         }
2371
2372         if (force_freq > 0) {
2373                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
2374                            "channel we are already using (%u MHz) on another "
2375                            "interface", force_freq);
2376         }
2377
2378         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
2379
2380         if (!wpa_s->create_p2p_iface) {
2381                 if (auth) {
2382                         if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2383                                                  go_intent, wpa_s->own_addr,
2384                                                  force_freq, persistent_group)
2385                             < 0)
2386                                 return -1;
2387                         return ret;
2388                 }
2389                 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
2390                                           go_intent, wpa_s->own_addr,
2391                                           force_freq, persistent_group) < 0)
2392                         return -1;
2393                 return ret;
2394         }
2395
2396         /* Prepare to add a new interface for the group */
2397         iftype = WPA_IF_P2P_GROUP;
2398         if (join)
2399                 iftype = WPA_IF_P2P_CLIENT;
2400         else if (go_intent == 15)
2401                 iftype = WPA_IF_P2P_GO;
2402         if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
2403                 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2404                            "interface for the group");
2405                 return -1;
2406         }
2407
2408         if (auth) {
2409                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
2410                                          go_intent,
2411                                          wpa_s->pending_interface_addr,
2412                                          force_freq, persistent_group) < 0)
2413                         return -1;
2414                 return ret;
2415         }
2416         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, go_intent,
2417                                   wpa_s->pending_interface_addr,
2418                                   force_freq, persistent_group) < 0) {
2419                 wpas_p2p_remove_pending_group_interface(wpa_s);
2420                 return -1;
2421         }
2422         return ret;
2423 }
2424
2425
2426 /**
2427  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
2428  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2429  * @freq: Frequency of the channel in MHz
2430  * @duration: Duration of the stay on the channel in milliseconds
2431  *
2432  * This callback is called when the driver indicates that it has started the
2433  * requested remain-on-channel duration.
2434  */
2435 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2436                                    unsigned int freq, unsigned int duration)
2437 {
2438         wpa_s->off_channel_freq = freq;
2439         wpas_send_action_cb(wpa_s, NULL);
2440         if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
2441                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
2442                               wpa_s->pending_listen_duration);
2443                 wpa_s->pending_listen_freq = 0;
2444         }
2445 }
2446
2447
2448 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
2449                                  unsigned int timeout)
2450 {
2451         /* Limit maximum Listen state time based on driver limitation. */
2452         if (timeout > wpa_s->max_remain_on_chan)
2453                 timeout = wpa_s->max_remain_on_chan;
2454
2455         return p2p_listen(wpa_s->global->p2p, timeout);
2456 }
2457
2458
2459 /**
2460  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
2461  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2462  * @freq: Frequency of the channel in MHz
2463  *
2464  * This callback is called when the driver indicates that a remain-on-channel
2465  * operation has been completed, i.e., the duration on the requested channel
2466  * has timed out.
2467  */
2468 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
2469                                           unsigned int freq)
2470 {
2471         wpa_s->off_channel_freq = 0;
2472         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
2473                 return; /* P2P module started a new operation */
2474         if (wpa_s->pending_action_tx)
2475                 return;
2476         if (wpa_s->p2p_long_listen > 0)
2477                 wpa_s->p2p_long_listen -= 5;
2478         if (wpa_s->p2p_long_listen > 0) {
2479                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
2480                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen * 1000);
2481         }
2482 }
2483
2484
2485 /**
2486  * wpas_p2p_group_remove - Remove a P2P group
2487  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2488  * @ifname: Network interface name of the group interface or "*" to remove all
2489  *      groups
2490  * Returns: 0 on success, -1 on failure
2491  *
2492  * This function is used to remove a P2P group. This can be used to disconnect
2493  * from a group in which the local end is a P2P Client or to end a P2P Group in
2494  * case the local end is the Group Owner. If a virtual network interface was
2495  * created for this group, that interface will be removed. Otherwise, only the
2496  * configured P2P group network will be removed from the interface.
2497  */
2498 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
2499 {
2500         struct wpa_global *global = wpa_s->global;
2501
2502         if (os_strcmp(ifname, "*") == 0) {
2503                 struct wpa_supplicant *prev;
2504                 wpa_s = global->ifaces;
2505                 while (wpa_s) {
2506                         prev = wpa_s;
2507                         wpa_s = wpa_s->next;
2508                         wpas_p2p_group_delete(prev);
2509                 }
2510                 return 0;
2511         }
2512
2513         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2514                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2515                         break;
2516         }
2517
2518         if (wpa_s == NULL)
2519                 return -1;
2520
2521         wpas_p2p_group_delete(wpa_s);
2522
2523         return 0;
2524 }
2525
2526
2527 static void wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
2528                                     struct p2p_go_neg_results *params,
2529                                     int freq)
2530 {
2531         u8 bssid[ETH_ALEN];
2532         int res;
2533
2534         os_memset(params, 0, sizeof(*params));
2535         params->role_go = 1;
2536         params->freq = 2412;
2537         if (freq)
2538                 params->freq = freq;
2539         else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
2540                  wpa_s->conf->p2p_oper_channel >= 1 &&
2541                  wpa_s->conf->p2p_oper_channel <= 11)
2542                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
2543         else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
2544                  wpa_s->conf->p2p_oper_reg_class == 118)
2545                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
2546         if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
2547             wpa_s->assoc_freq && !freq) {
2548                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2549                            "already using");
2550                 params->freq = wpa_s->assoc_freq;
2551         }
2552
2553         res = wpa_drv_shared_freq(wpa_s);
2554         if (res > 0 && !freq) {
2555                 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
2556                            "already using on a shared interface");
2557                 params->freq = res;
2558         }
2559 }
2560
2561
2562 static struct wpa_supplicant *
2563 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
2564                          int go)
2565 {
2566         struct wpa_supplicant *group_wpa_s;
2567
2568         if (!wpas_p2p_create_iface(wpa_s))
2569                 return wpa_s;
2570
2571         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2572                                          WPA_IF_P2P_CLIENT) < 0)
2573                 return NULL;
2574         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
2575         if (group_wpa_s == NULL) {
2576                 wpas_p2p_remove_pending_group_interface(wpa_s);
2577                 return NULL;
2578         }
2579
2580         return group_wpa_s;
2581 }
2582
2583
2584 /**
2585  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
2586  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2587  * @persistent_group: Whether to create a persistent group
2588  * @freq: Frequency for the group or 0 to indicate no hardcoding
2589  * Returns: 0 on success, -1 on failure
2590  *
2591  * This function creates a new P2P group with the local end as the Group Owner,
2592  * i.e., without using Group Owner Negotiation.
2593  */
2594 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
2595                        int freq)
2596 {
2597         struct p2p_go_neg_results params;
2598
2599         wpas_p2p_init_go_params(wpa_s, &params, freq);
2600         p2p_go_params(wpa_s->global->p2p, &params);
2601         params.persistent_group = persistent_group;
2602
2603         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
2604         if (wpa_s == NULL)
2605                 return -1;
2606         wpas_start_wps_go(wpa_s, &params, 0);
2607
2608         return 0;
2609 }
2610
2611
2612 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
2613                                  struct wpa_ssid *params, int addr_allocated)
2614 {
2615         struct wpa_ssid *ssid;
2616
2617         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
2618         if (wpa_s == NULL)
2619                 return -1;
2620
2621         wpa_supplicant_ap_deinit(wpa_s);
2622
2623         ssid = wpa_config_add_network(wpa_s->conf);
2624         if (ssid == NULL)
2625                 return -1;
2626         wpas_notify_network_added(wpa_s, ssid);
2627         wpa_config_set_network_defaults(ssid);
2628         ssid->temporary = 1;
2629         ssid->proto = WPA_PROTO_RSN;
2630         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2631         ssid->group_cipher = WPA_CIPHER_CCMP;
2632         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2633         ssid->ssid = os_malloc(params->ssid_len);
2634         if (ssid->ssid == NULL) {
2635                 wpas_notify_network_removed(wpa_s, ssid);
2636                 wpa_config_remove_network(wpa_s->conf, ssid->id);
2637                 return -1;
2638         }
2639         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2640         ssid->ssid_len = params->ssid_len;
2641         ssid->p2p_group = 1;
2642         if (params->psk_set) {
2643                 os_memcpy(ssid->psk, params->psk, 32);
2644                 ssid->psk_set = 1;
2645         }
2646         if (params->passphrase)
2647                 ssid->passphrase = os_strdup(params->passphrase);
2648
2649         wpa_supplicant_select_network(wpa_s, ssid);
2650
2651         wpa_s->show_group_started = 1;
2652
2653         return 0;
2654 }
2655
2656
2657 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
2658                                   struct wpa_ssid *ssid, int addr_allocated,
2659                                   int freq)
2660 {
2661         struct p2p_go_neg_results params;
2662
2663         if (ssid->disabled != 2 || ssid->ssid == NULL)
2664                 return -1;
2665
2666         wpa_s->p2p_long_listen = 0;
2667         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2668
2669         if (ssid->mode == WPAS_MODE_INFRA)
2670                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
2671
2672         if (ssid->mode != WPAS_MODE_P2P_GO)
2673                 return -1;
2674
2675         wpas_p2p_init_go_params(wpa_s, &params, freq);
2676
2677         params.role_go = 1;
2678         if (ssid->passphrase == NULL ||
2679             os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
2680                 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
2681                            "group");
2682                 return -1;
2683         }
2684         os_strlcpy(params.passphrase, ssid->passphrase,
2685                    sizeof(params.passphrase));
2686         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
2687         params.ssid_len = ssid->ssid_len;
2688         params.persistent_group = 1;
2689
2690         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
2691         if (wpa_s == NULL)
2692                 return -1;
2693
2694         wpas_start_wps_go(wpa_s, &params, 0);
2695
2696         return 0;
2697 }
2698
2699
2700 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
2701                                struct wpabuf *proberesp_ies)
2702 {
2703         struct wpa_supplicant *wpa_s = ctx;
2704         if (wpa_s->ap_iface) {
2705                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
2706                 if (beacon_ies) {
2707                         wpabuf_free(hapd->p2p_beacon_ie);
2708                         hapd->p2p_beacon_ie = beacon_ies;
2709                 }
2710                 wpabuf_free(hapd->p2p_probe_resp_ie);
2711                 hapd->p2p_probe_resp_ie = proberesp_ies;
2712         } else {
2713                 wpabuf_free(beacon_ies);
2714                 wpabuf_free(proberesp_ies);
2715         }
2716         wpa_supplicant_ap_update_beacon(wpa_s);
2717 }
2718
2719
2720 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
2721                                        int persistent_group,
2722                                        int group_formation)
2723 {
2724         struct p2p_group *group;
2725         struct p2p_group_config *cfg;
2726
2727         cfg = os_zalloc(sizeof(*cfg));
2728         if (cfg == NULL)
2729                 return NULL;
2730
2731         cfg->persistent_group = persistent_group;
2732         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
2733         cfg->cb_ctx = wpa_s;
2734         cfg->ie_update = wpas_p2p_ie_update;
2735
2736         group = p2p_group_init(wpa_s->global->p2p, cfg);
2737         if (group == NULL)
2738                 os_free(cfg);
2739         if (!group_formation)
2740                 p2p_group_notif_formation_done(group);
2741         wpa_s->p2p_group = group;
2742         return group;
2743 }
2744
2745
2746 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2747                           int registrar)
2748 {
2749         if (!wpa_s->p2p_in_provisioning) {
2750                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
2751                            "provisioning not in progress");
2752                 return;
2753         }
2754
2755         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
2756                              NULL);
2757         if (wpa_s->global->p2p)
2758                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
2759         wpas_group_formation_completed(wpa_s, 1);
2760 }
2761
2762
2763 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2764                        const char *config_method)
2765 {
2766         u16 config_methods;
2767
2768         if (os_strcmp(config_method, "display") == 0)
2769                 config_methods = WPS_CONFIG_DISPLAY;
2770         else if (os_strcmp(config_method, "keypad") == 0)
2771                 config_methods = WPS_CONFIG_KEYPAD;
2772         else if (os_strcmp(config_method, "pbc") == 0 ||
2773                  os_strcmp(config_method, "pushbutton") == 0)
2774                 config_methods = WPS_CONFIG_PUSHBUTTON;
2775         else
2776                 return -1;
2777
2778         if (wpa_s->global->p2p == NULL)
2779                 return -1;
2780
2781         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
2782                                  config_methods, 0);
2783 }
2784
2785
2786 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
2787                               char *end)
2788 {
2789         return p2p_scan_result_text(ies, ies_len, buf, end);
2790 }
2791
2792
2793 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
2794                   enum p2p_discovery_type type)
2795 {
2796         wpa_s->p2p_long_listen = 0;
2797
2798         return p2p_find(wpa_s->global->p2p, timeout, type);
2799 }
2800
2801
2802 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
2803 {
2804         wpa_s->p2p_long_listen = 0;
2805
2806         p2p_stop_find(wpa_s->global->p2p);
2807
2808         wpas_p2p_remove_pending_group_interface(wpa_s);
2809 }
2810
2811
2812 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
2813 {
2814         struct wpa_supplicant *wpa_s = eloop_ctx;
2815         wpa_s->p2p_long_listen = 0;
2816 }
2817
2818
2819 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
2820 {
2821         int res;
2822
2823         if (timeout == 0) {
2824                 /*
2825                  * This is a request for unlimited Listen state. However, at
2826                  * least for now, this is mapped to a Listen state for one
2827                  * hour.
2828                  */
2829                 timeout = 3600;
2830         }
2831         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2832         wpa_s->p2p_long_listen = 0;
2833
2834         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
2835         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
2836                 wpa_s->p2p_long_listen = timeout;
2837                 eloop_register_timeout(timeout, 0,
2838                                        wpas_p2p_long_listen_timeout,
2839                                        wpa_s, NULL);
2840         }
2841
2842         return res;
2843 }
2844
2845
2846 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, const u8 *bssid,
2847                           u8 *buf, size_t len, int p2p_group)
2848 {
2849         if (wpa_s->global->p2p_disabled)
2850                 return -1;
2851         if (wpa_s->global->p2p == NULL)
2852                 return -1;
2853
2854         return p2p_assoc_req_ie(wpa_s->global->p2p, bssid, buf, len,
2855                                 p2p_group);
2856 }
2857
2858
2859 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
2860                           const u8 *ie, size_t ie_len)
2861 {
2862         if (wpa_s->global->p2p_disabled)
2863                 return 0;
2864         if (wpa_s->global->p2p == NULL)
2865                 return 0;
2866
2867         return p2p_probe_req_rx(wpa_s->global->p2p, addr, ie, ie_len);
2868 }
2869
2870
2871 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
2872                         const u8 *sa, const u8 *bssid,
2873                         u8 category, const u8 *data, size_t len, int freq)
2874 {
2875         if (wpa_s->global->p2p_disabled)
2876                 return;
2877         if (wpa_s->global->p2p == NULL)
2878                 return;
2879
2880         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
2881                       freq);
2882 }
2883
2884
2885 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
2886 {
2887         if (wpa_s->global->p2p_disabled)
2888                 return;
2889         if (wpa_s->global->p2p == NULL)
2890                 return;
2891
2892         p2p_scan_ie(wpa_s->global->p2p, ies);
2893 }
2894
2895
2896 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
2897 {
2898         p2p_group_deinit(wpa_s->p2p_group);
2899         wpa_s->p2p_group = NULL;
2900 }
2901
2902
2903 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
2904 {
2905         wpa_s->p2p_long_listen = 0;
2906
2907         return p2p_reject(wpa_s->global->p2p, addr);
2908 }
2909
2910
2911 /* Invite to reinvoke a persistent group */
2912 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
2913                     struct wpa_ssid *ssid, const u8 *go_dev_addr)
2914 {
2915         enum p2p_invite_role role;
2916         u8 *bssid = NULL;
2917
2918         if (ssid->mode == WPAS_MODE_P2P_GO) {
2919                 role = P2P_INVITE_ROLE_GO;
2920                 if (peer_addr == NULL) {
2921                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
2922                                    "address in invitation command");
2923                         return -1;
2924                 }
2925                 if (wpas_p2p_create_iface(wpa_s)) {
2926                         if (wpas_p2p_add_group_interface(wpa_s,
2927                                                          WPA_IF_P2P_GO) < 0) {
2928                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
2929                                            "allocate a new interface for the "
2930                                            "group");
2931                                 return -1;
2932                         }
2933                         bssid = wpa_s->pending_interface_addr;
2934                 } else
2935                         bssid = wpa_s->own_addr;
2936         } else {
2937                 role = P2P_INVITE_ROLE_CLIENT;
2938                 peer_addr = ssid->bssid;
2939         }
2940         wpa_s->pending_invite_ssid_id = ssid->id;
2941
2942         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
2943                           ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
2944 }
2945
2946
2947 /* Invite to join an active group */
2948 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
2949                           const u8 *peer_addr, const u8 *go_dev_addr)
2950 {
2951         struct wpa_global *global = wpa_s->global;
2952         enum p2p_invite_role role;
2953         u8 *bssid = NULL;
2954         struct wpa_ssid *ssid;
2955
2956         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2957                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2958                         break;
2959         }
2960         if (wpa_s == NULL) {
2961                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
2962                 return -1;
2963         }
2964
2965         ssid = wpa_s->current_ssid;
2966         if (ssid == NULL) {
2967                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
2968                            "invitation");
2969                 return -1;
2970         }
2971
2972         if (ssid->mode == WPAS_MODE_P2P_GO) {
2973                 role = P2P_INVITE_ROLE_ACTIVE_GO;
2974                 bssid = wpa_s->own_addr;
2975                 if (go_dev_addr == NULL)
2976                         go_dev_addr = wpa_s->own_addr;
2977         } else {
2978                 role = P2P_INVITE_ROLE_CLIENT;
2979                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
2980                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
2981                                    "invite to current group");
2982                         return -1;
2983                 }
2984                 bssid = wpa_s->bssid;
2985                 if (go_dev_addr == NULL &&
2986                     !is_zero_ether_addr(wpa_s->go_dev_addr))
2987                         go_dev_addr = wpa_s->go_dev_addr;
2988         }
2989         wpa_s->pending_invite_ssid_id = -1;
2990
2991         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
2992                           ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 0);
2993 }
2994
2995
2996 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
2997 {
2998         struct wpa_ssid *ssid = wpa_s->current_ssid;
2999         const char *ssid_txt;
3000         u8 go_dev_addr[ETH_ALEN];
3001         int persistent;
3002
3003         if (!wpa_s->show_group_started || !ssid)
3004                 return;
3005
3006         wpa_s->show_group_started = 0;
3007
3008         ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
3009         os_memset(go_dev_addr, 0, ETH_ALEN);
3010         if (ssid->bssid_set)
3011                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
3012         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
3013                                                ssid->ssid_len);
3014         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
3015
3016         if (ssid->passphrase == NULL && ssid->psk_set) {
3017                 char psk[65];
3018                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
3019                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3020                         "%s client ssid=\"%s\" psk=%s go_dev_addr=" MACSTR
3021                         "%s",
3022                         wpa_s->ifname, ssid_txt, psk, MAC2STR(go_dev_addr),
3023                         persistent ? " [PERSISTENT]" : "");
3024         } else {
3025                 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
3026                         "%s client ssid=\"%s\" passphrase=\"%s\" go_dev_addr="
3027                         MACSTR "%s",
3028                         wpa_s->ifname, ssid_txt,
3029                         ssid->passphrase ? ssid->passphrase : "",
3030                         MAC2STR(go_dev_addr),
3031                         persistent ? " [PERSISTENT]" : "");
3032         }
3033
3034         if (persistent)
3035                 wpas_p2p_store_persistent_group(wpa_s->parent, ssid,
3036                                                 go_dev_addr);
3037 }
3038
3039
3040 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
3041                           u32 interval1, u32 duration2, u32 interval2)
3042 {
3043         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3044             wpa_s->current_ssid == NULL ||
3045             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
3046                 return -1;
3047
3048         return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
3049                                 wpa_s->own_addr, wpa_s->assoc_freq,
3050                                 duration1, interval1, duration2, interval2);
3051 }
3052
3053
3054 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
3055                         unsigned int interval)
3056 {
3057         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
3058 }
3059
3060
3061 void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3062                            u16 reason_code, const u8 *ie, size_t ie_len)
3063 {
3064         if (wpa_s->global->p2p_disabled)
3065                 return;
3066
3067         p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3068 }
3069
3070
3071 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
3072                              u16 reason_code, const u8 *ie, size_t ie_len)
3073 {
3074         if (wpa_s->global->p2p_disabled)
3075                 return;
3076
3077         p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, ie_len);
3078 }
3079
3080
3081 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
3082 {
3083         struct p2p_data *p2p = wpa_s->global->p2p;
3084
3085         if (p2p == NULL)
3086                 return;
3087
3088         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
3089                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
3090
3091         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
3092                 u8 pri_dev_type[8];
3093                 if (wpa_s->conf->device_type) {
3094                         if (wps_dev_type_str2bin(wpa_s->conf->device_type,
3095                                                  pri_dev_type) < 0) {
3096                                 wpa_printf(MSG_ERROR, "P2P: Invalid "
3097                                            "device_type");
3098                         } else
3099                                 p2p_set_pri_dev_type(p2p, pri_dev_type);
3100                 }
3101         }
3102
3103         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
3104                 u8 sec_dev_type[P2P_SEC_DEVICE_TYPES][8];
3105                 size_t num = 0;
3106                 int i;
3107                 for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
3108                         if (wpa_s->conf->sec_device_type[i] == NULL)
3109                                 continue;
3110                         if (wps_dev_type_str2bin(
3111                                     wpa_s->conf->sec_device_type[i],
3112                                     sec_dev_type[num]) < 0) {
3113                                 wpa_printf(MSG_ERROR, "P2P: Invalid "
3114                                            "sec_device_type");
3115                                 continue;
3116                         }
3117                         num++;
3118                         if (num == P2P_SEC_DEVICE_TYPES)
3119                                 break;
3120                 }
3121                 p2p_set_sec_dev_types(p2p, (void *) sec_dev_type, num);
3122         }
3123
3124         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3125             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3126                 char country[3];
3127                 country[0] = wpa_s->conf->country[0];
3128                 country[1] = wpa_s->conf->country[1];
3129                 country[2] = 0x04;
3130                 p2p_set_country(p2p, country);
3131         }
3132
3133         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
3134                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
3135                                      wpa_s->conf->p2p_ssid_postfix ?
3136                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
3137                                      0);
3138         }
3139 }