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