Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Wi-Fi Direct - P2P Invitation procedure |
| 3 | * Copyright (c) 2010, Atheros Communications |
| 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | |
| 11 | #include "common.h" |
| 12 | #include "common/ieee802_11_defs.h" |
Dmitry Shmidt | 2e67f06 | 2014-07-16 09:55:28 -0700 | [diff] [blame] | 13 | #include "common/wpa_ctrl.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 14 | #include "p2p_i.h" |
| 15 | #include "p2p.h" |
| 16 | |
| 17 | |
| 18 | static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p, |
| 19 | struct p2p_device *peer, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 20 | const u8 *go_dev_addr, |
| 21 | int dev_pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 22 | { |
| 23 | struct wpabuf *buf; |
| 24 | u8 *len; |
| 25 | const u8 *dev_addr; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 26 | size_t extra = 0; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 27 | bool is_6ghz_capab; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 28 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 29 | #ifdef CONFIG_WIFI_DISPLAY |
| 30 | struct wpabuf *wfd_ie = p2p->wfd_ie_invitation; |
| 31 | if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) { |
| 32 | size_t i; |
| 33 | for (i = 0; i < p2p->num_groups; i++) { |
| 34 | struct p2p_group *g = p2p->groups[i]; |
| 35 | struct wpabuf *ie; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 36 | if (!ether_addr_equal(p2p_group_get_interface_addr(g), |
| 37 | p2p->inv_bssid)) |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 38 | continue; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 39 | ie = p2p_group_get_wfd_ie(g); |
| 40 | if (ie) { |
| 41 | wfd_ie = ie; |
| 42 | break; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | if (wfd_ie) |
| 47 | extra = wpabuf_len(wfd_ie); |
| 48 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 49 | |
Dmitry Shmidt | 2e67f06 | 2014-07-16 09:55:28 -0700 | [diff] [blame] | 50 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]) |
| 51 | extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]); |
| 52 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 53 | buf = wpabuf_alloc(1000 + extra); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 54 | if (buf == NULL) |
| 55 | return NULL; |
| 56 | |
| 57 | peer->dialog_token++; |
| 58 | if (peer->dialog_token == 0) |
| 59 | peer->dialog_token = 1; |
| 60 | p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ, |
| 61 | peer->dialog_token); |
| 62 | |
| 63 | len = p2p_buf_add_ie_hdr(buf); |
| 64 | if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent) |
| 65 | p2p_buf_add_config_timeout(buf, 0, 0); |
| 66 | else |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 67 | p2p_buf_add_config_timeout(buf, p2p->go_timeout, |
| 68 | p2p->client_timeout); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 69 | p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ? |
| 70 | P2P_INVITATION_FLAGS_TYPE : 0); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 71 | if (p2p->inv_role != P2P_INVITE_ROLE_CLIENT || |
| 72 | !(peer->flags & P2P_DEV_NO_PREF_CHAN)) |
| 73 | p2p_buf_add_operating_channel(buf, p2p->cfg->country, |
| 74 | p2p->op_reg_class, |
| 75 | p2p->op_channel); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 76 | if (p2p->inv_bssid_set) |
| 77 | p2p_buf_add_group_bssid(buf, p2p->inv_bssid); |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 78 | is_6ghz_capab = is_p2p_6ghz_capable(p2p) && |
| 79 | p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr); |
| 80 | p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels, |
| 81 | is_6ghz_capab); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 82 | if (go_dev_addr) |
| 83 | dev_addr = go_dev_addr; |
| 84 | else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT) |
| 85 | dev_addr = peer->info.p2p_device_addr; |
| 86 | else |
| 87 | dev_addr = p2p->cfg->dev_addr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 88 | p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len); |
| 89 | p2p_buf_add_device_info(buf, p2p, peer); |
| 90 | p2p_buf_update_ie_hdr(buf, len); |
| 91 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 92 | p2p_buf_add_pref_channel_list(buf, p2p->pref_freq_list, |
| 93 | p2p->num_pref_freq); |
| 94 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 95 | #ifdef CONFIG_WIFI_DISPLAY |
| 96 | if (wfd_ie) |
| 97 | wpabuf_put_buf(buf, wfd_ie); |
| 98 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 99 | |
Dmitry Shmidt | 2e67f06 | 2014-07-16 09:55:28 -0700 | [diff] [blame] | 100 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]) |
| 101 | wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_INV_REQ]); |
| 102 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 103 | if (dev_pw_id >= 0) { |
| 104 | /* WSC IE in Invitation Request for NFC static handover */ |
| 105 | p2p_build_wps_ie(p2p, buf, dev_pw_id, 0); |
| 106 | } |
| 107 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 108 | return buf; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p, |
| 113 | struct p2p_device *peer, |
| 114 | u8 dialog_token, u8 status, |
| 115 | const u8 *group_bssid, |
| 116 | u8 reg_class, u8 channel, |
| 117 | struct p2p_channels *channels) |
| 118 | { |
| 119 | struct wpabuf *buf; |
| 120 | u8 *len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 121 | size_t extra = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 122 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 123 | #ifdef CONFIG_WIFI_DISPLAY |
| 124 | struct wpabuf *wfd_ie = p2p->wfd_ie_invitation; |
| 125 | if (wfd_ie && group_bssid) { |
| 126 | size_t i; |
| 127 | for (i = 0; i < p2p->num_groups; i++) { |
| 128 | struct p2p_group *g = p2p->groups[i]; |
| 129 | struct wpabuf *ie; |
Sunil Ravi | b0ac25f | 2024-07-12 01:42:03 +0000 | [diff] [blame] | 130 | if (!ether_addr_equal(p2p_group_get_interface_addr(g), |
| 131 | group_bssid)) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 132 | continue; |
| 133 | ie = p2p_group_get_wfd_ie(g); |
| 134 | if (ie) { |
| 135 | wfd_ie = ie; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | if (wfd_ie) |
| 141 | extra = wpabuf_len(wfd_ie); |
| 142 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 143 | |
Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 144 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP]) |
| 145 | extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP]); |
| 146 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 147 | buf = wpabuf_alloc(1000 + extra); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 148 | if (buf == NULL) |
| 149 | return NULL; |
| 150 | |
| 151 | p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP, |
| 152 | dialog_token); |
| 153 | |
| 154 | len = p2p_buf_add_ie_hdr(buf); |
| 155 | p2p_buf_add_status(buf, status); |
| 156 | p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */ |
| 157 | if (reg_class && channel) |
| 158 | p2p_buf_add_operating_channel(buf, p2p->cfg->country, |
| 159 | reg_class, channel); |
| 160 | if (group_bssid) |
| 161 | p2p_buf_add_group_bssid(buf, group_bssid); |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 162 | if (channels) { |
| 163 | bool is_6ghz_capab; |
| 164 | |
| 165 | is_6ghz_capab = is_p2p_6ghz_capable(p2p) && |
| 166 | p2p_is_peer_6ghz_capab(p2p, peer->info.p2p_device_addr); |
| 167 | p2p_buf_add_channel_list(buf, p2p->cfg->country, channels, |
| 168 | is_6ghz_capab); |
| 169 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 170 | p2p_buf_update_ie_hdr(buf, len); |
| 171 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 172 | #ifdef CONFIG_WIFI_DISPLAY |
| 173 | if (wfd_ie) |
| 174 | wpabuf_put_buf(buf, wfd_ie); |
| 175 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 176 | |
Dmitry Shmidt | a3dc309 | 2015-06-23 11:21:28 -0700 | [diff] [blame] | 177 | if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP]) |
| 178 | wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_INV_RESP]); |
| 179 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 180 | return buf; |
| 181 | } |
| 182 | |
| 183 | |
Sunil Ravi | c0f5d41 | 2024-09-11 22:12:49 +0000 | [diff] [blame] | 184 | struct wpabuf * p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa, |
| 185 | const u8 *data, size_t len, |
| 186 | int rx_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 187 | { |
| 188 | struct p2p_device *dev; |
| 189 | struct p2p_message msg; |
| 190 | struct wpabuf *resp = NULL; |
| 191 | u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 192 | int go = 0; |
| 193 | u8 group_bssid[ETH_ALEN], *bssid; |
| 194 | int op_freq = 0; |
| 195 | u8 reg_class = 0, channel = 0; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 196 | struct p2p_channels all_channels, intersection, *channels = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 197 | int persistent; |
| 198 | |
| 199 | os_memset(group_bssid, 0, sizeof(group_bssid)); |
| 200 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 201 | p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 202 | MAC2STR(sa), rx_freq); |
| 203 | |
| 204 | if (p2p_parse(data, len, &msg)) |
Sunil Ravi | c0f5d41 | 2024-09-11 22:12:49 +0000 | [diff] [blame] | 205 | return NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 206 | |
| 207 | dev = p2p_get_device(p2p, sa); |
| 208 | if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 209 | p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR, |
| 210 | MAC2STR(sa)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 211 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 212 | if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1, |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 213 | 0)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 214 | p2p_dbg(p2p, "Invitation Request add device failed " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 215 | MACSTR, MAC2STR(sa)); |
| 216 | status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 217 | goto fail; |
| 218 | } |
| 219 | |
| 220 | dev = p2p_get_device(p2p, sa); |
| 221 | if (dev == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 222 | p2p_dbg(p2p, "Reject Invitation Request from unknown peer " |
| 223 | MACSTR, MAC2STR(sa)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 224 | status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 225 | goto fail; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (!msg.group_id || !msg.channel_list) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 230 | p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from " |
| 231 | MACSTR, MAC2STR(sa)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 232 | status = P2P_SC_FAIL_INVALID_PARAMS; |
| 233 | goto fail; |
| 234 | } |
| 235 | |
| 236 | if (msg.invitation_flags) |
| 237 | persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE; |
| 238 | else { |
| 239 | /* Invitation Flags is a mandatory attribute starting from P2P |
| 240 | * spec 1.06. As a backwards compatibility mechanism, assume |
| 241 | * the request was for a persistent group if the attribute is |
| 242 | * missing. |
| 243 | */ |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 244 | p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 245 | persistent = 1; |
| 246 | } |
| 247 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 248 | p2p_channels_union(&p2p->cfg->channels, &p2p->cfg->cli_channels, |
| 249 | &all_channels); |
| 250 | |
| 251 | if (p2p_peer_channels_check(p2p, &all_channels, dev, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 252 | msg.channel_list, msg.channel_list_len) < |
| 253 | 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 254 | p2p_dbg(p2p, "No common channels found"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 255 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 256 | goto fail; |
| 257 | } |
| 258 | |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 259 | p2p_channels_dump(p2p, "own channels", &p2p->cfg->channels); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 260 | p2p_channels_dump(p2p, "own client channels", &all_channels); |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 261 | p2p_channels_dump(p2p, "peer channels", &dev->channels); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 262 | p2p_channels_intersect(&all_channels, &dev->channels, |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 263 | &intersection); |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 264 | p2p_channels_dump(p2p, "intersection", &intersection); |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 265 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 266 | if (p2p->cfg->invitation_process) { |
| 267 | status = p2p->cfg->invitation_process( |
| 268 | p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id, |
| 269 | msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 270 | &go, group_bssid, &op_freq, persistent, &intersection, |
| 271 | msg.dev_password_id_present ? msg.dev_password_id : -1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 274 | if (go) { |
| 275 | p2p_channels_intersect(&p2p->cfg->channels, &dev->channels, |
| 276 | &intersection); |
| 277 | p2p_channels_dump(p2p, "intersection(GO)", &intersection); |
| 278 | if (intersection.reg_classes == 0) { |
| 279 | p2p_dbg(p2p, "No common channels found (GO)"); |
| 280 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 281 | goto fail; |
| 282 | } |
| 283 | } |
| 284 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 285 | if (op_freq) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 286 | p2p_dbg(p2p, "Invitation processing forced frequency %d MHz", |
| 287 | op_freq); |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 288 | if (p2p_freq_to_channel(op_freq, ®_class, &channel) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 289 | p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()", |
| 290 | op_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 291 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 292 | goto fail; |
| 293 | } |
| 294 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 295 | if (!p2p_channels_includes(&intersection, reg_class, channel)) |
| 296 | { |
Dmitry Shmidt | b70d0bb | 2015-11-16 10:43:06 -0800 | [diff] [blame] | 297 | p2p_dbg(p2p, "forced freq %d MHz not in the supported channels intersection", |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 298 | op_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 299 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 300 | goto fail; |
| 301 | } |
| 302 | |
| 303 | if (status == P2P_SC_SUCCESS) |
| 304 | channels = &intersection; |
| 305 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 306 | p2p_dbg(p2p, "No forced channel from invitation processing - figure out best one to use"); |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 307 | |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 308 | /* Default to own configuration as a starting point */ |
| 309 | p2p->op_reg_class = p2p->cfg->op_reg_class; |
| 310 | p2p->op_channel = p2p->cfg->op_channel; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 311 | p2p_dbg(p2p, "Own default op_class %d channel %d", |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 312 | p2p->op_reg_class, p2p->op_channel); |
| 313 | |
| 314 | /* Use peer preference if specified and compatible */ |
| 315 | if (msg.operating_channel) { |
| 316 | int req_freq; |
| 317 | req_freq = p2p_channel_to_freq( |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 318 | msg.operating_channel[3], |
| 319 | msg.operating_channel[4]); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 320 | p2p_dbg(p2p, "Peer operating channel preference: %d MHz", |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 321 | req_freq); |
| 322 | if (req_freq > 0 && |
| 323 | p2p_channels_includes(&intersection, |
| 324 | msg.operating_channel[3], |
| 325 | msg.operating_channel[4])) { |
| 326 | p2p->op_reg_class = msg.operating_channel[3]; |
| 327 | p2p->op_channel = msg.operating_channel[4]; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 328 | p2p_dbg(p2p, "Use peer preference op_class %d channel %d", |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 329 | p2p->op_reg_class, p2p->op_channel); |
| 330 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 331 | p2p_dbg(p2p, "Cannot use peer channel preference"); |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 335 | /* Reselect the channel only for the case of the GO */ |
| 336 | if (go && |
| 337 | !p2p_channels_includes(&intersection, p2p->op_reg_class, |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 338 | p2p->op_channel)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 339 | p2p_dbg(p2p, "Initially selected channel (op_class %d channel %d) not in channel intersection - try to reselect", |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 340 | p2p->op_reg_class, p2p->op_channel); |
| 341 | p2p_reselect_channel(p2p, &intersection); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 342 | p2p_dbg(p2p, "Re-selection result: op_class %d channel %d", |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 343 | p2p->op_reg_class, p2p->op_channel); |
| 344 | if (!p2p_channels_includes(&intersection, |
| 345 | p2p->op_reg_class, |
| 346 | p2p->op_channel)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 347 | p2p_dbg(p2p, "Peer does not support selected operating channel (reg_class=%u channel=%u)", |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 348 | p2p->op_reg_class, p2p->op_channel); |
| 349 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 350 | goto fail; |
| 351 | } |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 352 | } else if (go && !(dev->flags & P2P_DEV_FORCE_FREQ) && |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 353 | !p2p->cfg->cfg_op_channel) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 354 | p2p_dbg(p2p, "Try to reselect channel selection with peer information received; previously selected op_class %u channel %u", |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 355 | p2p->op_reg_class, p2p->op_channel); |
| 356 | p2p_reselect_channel(p2p, &intersection); |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 359 | /* |
| 360 | * Use the driver preferred frequency list extension if |
| 361 | * supported. |
| 362 | */ |
| 363 | p2p_check_pref_chan(p2p, go, dev, &msg); |
| 364 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 365 | op_freq = p2p_channel_to_freq(p2p->op_reg_class, |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 366 | p2p->op_channel); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 367 | if (op_freq < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 368 | p2p_dbg(p2p, "Unknown operational channel (country=%c%c reg_class=%u channel=%u)", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 369 | p2p->cfg->country[0], p2p->cfg->country[1], |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 370 | p2p->op_reg_class, p2p->op_channel); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 371 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 372 | goto fail; |
| 373 | } |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 374 | p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 375 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 376 | if (status == P2P_SC_SUCCESS) { |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 377 | reg_class = p2p->op_reg_class; |
| 378 | channel = p2p->op_channel; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 379 | channels = &intersection; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | fail: |
| 384 | if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid)) |
| 385 | bssid = group_bssid; |
| 386 | else |
| 387 | bssid = NULL; |
| 388 | resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status, |
| 389 | bssid, reg_class, channel, channels); |
| 390 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 391 | /* |
| 392 | * Store copy of invitation data to be used when processing TX status |
| 393 | * callback for the Acton frame. |
| 394 | */ |
| 395 | os_memcpy(p2p->inv_sa, sa, ETH_ALEN); |
| 396 | if (msg.group_bssid) { |
| 397 | os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN); |
| 398 | p2p->inv_group_bssid_ptr = p2p->inv_group_bssid; |
| 399 | } else |
| 400 | p2p->inv_group_bssid_ptr = NULL; |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 401 | if (msg.group_id) { |
Dmitry Shmidt | 9d9e602 | 2015-04-23 10:34:55 -0700 | [diff] [blame] | 402 | if (msg.group_id_len - ETH_ALEN <= SSID_MAX_LEN) { |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 403 | os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN, |
| 404 | msg.group_id_len - ETH_ALEN); |
| 405 | p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN; |
| 406 | } |
| 407 | os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN); |
| 408 | } else { |
| 409 | p2p->inv_ssid_len = 0; |
| 410 | os_memset(p2p->inv_go_dev_addr, 0, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 411 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 412 | p2p->inv_status = status; |
| 413 | p2p->inv_op_freq = op_freq; |
Sunil Ravi | c0f5d41 | 2024-09-11 22:12:49 +0000 | [diff] [blame] | 414 | p2p_parse_free(&msg); |
| 415 | return resp; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | void p2p_handle_invitation_req(struct p2p_data *p2p, const u8 *sa, |
| 420 | const u8 *data, size_t len, int rx_freq) |
| 421 | { |
| 422 | int freq; |
| 423 | struct wpabuf *resp; |
| 424 | |
| 425 | resp = p2p_process_invitation_req(p2p, sa, data, len, rx_freq); |
| 426 | if (!resp) |
| 427 | return; |
| 428 | |
| 429 | if (rx_freq > 0) |
| 430 | freq = rx_freq; |
| 431 | else |
| 432 | freq = p2p_channel_to_freq(p2p->cfg->reg_class, |
| 433 | p2p->cfg->channel); |
| 434 | if (freq < 0) |
| 435 | p2p_dbg(p2p, "Unknown regulatory class/channel"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 436 | |
| 437 | p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE; |
| 438 | if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, |
| 439 | p2p->cfg->dev_addr, |
Dmitry Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 440 | wpabuf_head(resp), wpabuf_len(resp), 50) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 441 | p2p_dbg(p2p, "Failed to send Action frame"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 444 | wpabuf_free(resp); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | |
| 448 | void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa, |
| 449 | const u8 *data, size_t len) |
| 450 | { |
| 451 | struct p2p_device *dev; |
| 452 | struct p2p_message msg; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 453 | struct p2p_channels intersection, *channels = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 454 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 455 | p2p_dbg(p2p, "Received Invitation Response from " MACSTR, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 456 | MAC2STR(sa)); |
| 457 | |
| 458 | dev = p2p_get_device(p2p, sa); |
| 459 | if (dev == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 460 | p2p_dbg(p2p, "Ignore Invitation Response from unknown peer " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 461 | MACSTR, MAC2STR(sa)); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 462 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
| 466 | if (dev != p2p->invite_peer) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 467 | p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 468 | MACSTR, MAC2STR(sa)); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 469 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 473 | if (p2p_parse(data, len, &msg)) { |
| 474 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 475 | return; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 476 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 477 | |
| 478 | if (!msg.status) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 479 | p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from " |
| 480 | MACSTR, MAC2STR(sa)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 481 | p2p_parse_free(&msg); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 482 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 483 | return; |
| 484 | } |
| 485 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 486 | /* |
| 487 | * We should not really receive a replayed response twice since |
| 488 | * duplicate frames are supposed to be dropped. However, not all drivers |
| 489 | * do that for pre-association frames. We did not use to verify dialog |
| 490 | * token matches for invitation response frames, but that check can be |
| 491 | * safely used to drop a replayed response to the previous Invitation |
| 492 | * Request in case the suggested operating channel was changed. This |
| 493 | * allows a duplicated reject frame to be dropped with the assumption |
| 494 | * that the real response follows after it. |
| 495 | */ |
| 496 | if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS && |
| 497 | p2p->retry_invite_req_sent && |
| 498 | msg.dialog_token != dev->dialog_token) { |
| 499 | p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)", |
| 500 | msg.dialog_token, dev->dialog_token); |
| 501 | p2p_parse_free(&msg); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | if (*msg.status == P2P_SC_FAIL_NO_COMMON_CHANNELS && |
| 506 | p2p->retry_invite_req && |
| 507 | p2p_channel_random_social(&p2p->cfg->channels, &p2p->op_reg_class, |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 508 | &p2p->op_channel, NULL, NULL) == 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 509 | p2p->retry_invite_req = 0; |
| 510 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
| 511 | p2p->cfg->stop_listen(p2p->cfg->cb_ctx); |
| 512 | p2p_set_state(p2p, P2P_INVITE); |
| 513 | p2p_dbg(p2p, "Resend Invitation Request setting op_class %u channel %u as operating channel", |
| 514 | p2p->op_reg_class, p2p->op_channel); |
| 515 | p2p->retry_invite_req_sent = 1; |
| 516 | p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr, |
| 517 | p2p->invite_dev_pw_id); |
| 518 | p2p_parse_free(&msg); |
| 519 | return; |
| 520 | } |
| 521 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
| 522 | p2p->retry_invite_req = 0; |
| 523 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 524 | if (!msg.channel_list && *msg.status == P2P_SC_SUCCESS) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 525 | p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from " |
| 526 | MACSTR, MAC2STR(sa)); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 527 | #ifdef CONFIG_P2P_STRICT |
| 528 | p2p_parse_free(&msg); |
| 529 | return; |
| 530 | #endif /* CONFIG_P2P_STRICT */ |
| 531 | /* Try to survive without peer channel list */ |
| 532 | channels = &p2p->channels; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 533 | } else if (!msg.channel_list) { |
| 534 | /* Non-success cases are not required to include Channel List */ |
| 535 | channels = &p2p->channels; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 536 | } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev, |
| 537 | msg.channel_list, |
| 538 | msg.channel_list_len) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 539 | p2p_dbg(p2p, "No common channels found"); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 540 | p2p_parse_free(&msg); |
| 541 | return; |
| 542 | } else { |
| 543 | p2p_channels_intersect(&p2p->channels, &dev->channels, |
| 544 | &intersection); |
| 545 | channels = &intersection; |
| 546 | } |
| 547 | |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 548 | if (p2p->cfg->invitation_result) { |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 549 | int peer_oper_freq = 0; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 550 | int freq = p2p_channel_to_freq(p2p->op_reg_class, |
| 551 | p2p->op_channel); |
| 552 | if (freq < 0) |
| 553 | freq = 0; |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 554 | |
| 555 | if (msg.operating_channel) { |
| 556 | peer_oper_freq = p2p_channel_to_freq( |
| 557 | msg.operating_channel[3], |
| 558 | msg.operating_channel[4]); |
| 559 | if (peer_oper_freq < 0) |
| 560 | peer_oper_freq = 0; |
| 561 | } |
| 562 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 563 | /* |
| 564 | * Use the driver preferred frequency list extension if |
| 565 | * supported. |
| 566 | */ |
| 567 | p2p_check_pref_chan(p2p, 0, dev, &msg); |
| 568 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 569 | p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status, |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 570 | msg.group_bssid, channels, sa, |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 571 | freq, peer_oper_freq); |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 572 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 573 | |
| 574 | p2p_parse_free(&msg); |
| 575 | |
| 576 | p2p_clear_timeout(p2p); |
| 577 | p2p_set_state(p2p, P2P_IDLE); |
| 578 | p2p->invite_peer = NULL; |
| 579 | } |
| 580 | |
| 581 | |
| 582 | int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 583 | const u8 *go_dev_addr, int dev_pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 584 | { |
| 585 | struct wpabuf *req; |
| 586 | int freq; |
| 587 | |
| 588 | freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 589 | if (freq <= 0) |
| 590 | freq = dev->oob_go_neg_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 591 | if (freq <= 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 592 | p2p_dbg(p2p, "No Listen/Operating frequency known for the peer " |
| 593 | MACSTR " to send Invitation Request", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 594 | MAC2STR(dev->info.p2p_device_addr)); |
| 595 | return -1; |
| 596 | } |
| 597 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 598 | req = p2p_build_invitation_req(p2p, dev, go_dev_addr, dev_pw_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 599 | if (req == NULL) |
| 600 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 601 | if (p2p->state != P2P_IDLE) |
| 602 | p2p_stop_listen_for_freq(p2p, freq); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 603 | p2p_dbg(p2p, "Sending Invitation Request"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 604 | p2p_set_state(p2p, P2P_INVITE); |
| 605 | p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST; |
| 606 | p2p->invite_peer = dev; |
| 607 | dev->invitation_reqs++; |
Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 608 | |
| 609 | /* In case of an active P2P GO use a shorter wait time to avoid |
| 610 | * issues if not sending out multiple consecutive Beacon frames. */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 611 | if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr, |
| 612 | p2p->cfg->dev_addr, dev->info.p2p_device_addr, |
Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 613 | wpabuf_head(req), wpabuf_len(req), |
| 614 | p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO ? |
| 615 | 150 : 500) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 616 | p2p_dbg(p2p, "Failed to send Action frame"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 617 | /* Use P2P find to recover and retry */ |
| 618 | p2p_set_timeout(p2p, 0, 0); |
Dmitry Shmidt | 3c47937 | 2014-02-04 10:50:36 -0800 | [diff] [blame] | 619 | } else { |
| 620 | dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | wpabuf_free(req); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | void p2p_invitation_req_cb(struct p2p_data *p2p, int success) |
| 630 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 631 | p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 632 | |
| 633 | if (p2p->invite_peer == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 634 | p2p_dbg(p2p, "No pending Invite"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 635 | return; |
| 636 | } |
| 637 | |
Dmitry Shmidt | 3c47937 | 2014-02-04 10:50:36 -0800 | [diff] [blame] | 638 | if (success) |
| 639 | p2p->invite_peer->flags &= ~P2P_DEV_WAIT_INV_REQ_ACK; |
| 640 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 641 | /* |
| 642 | * Use P2P find, if needed, to find the other device from its listen |
| 643 | * channel. |
| 644 | */ |
| 645 | p2p_set_state(p2p, P2P_INVITE); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 646 | p2p_set_timeout(p2p, 0, success ? 500000 : 100000); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | |
| 650 | void p2p_invitation_resp_cb(struct p2p_data *p2p, int success) |
| 651 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 652 | p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 653 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
| 654 | |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 655 | if (!success) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 656 | p2p_dbg(p2p, "Assume Invitation Response was actually received by the peer even though Ack was not reported"); |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 657 | |
| 658 | if (p2p->cfg->invitation_received) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 659 | p2p->cfg->invitation_received(p2p->cfg->cb_ctx, |
| 660 | p2p->inv_sa, |
| 661 | p2p->inv_group_bssid_ptr, |
| 662 | p2p->inv_ssid, p2p->inv_ssid_len, |
| 663 | p2p->inv_go_dev_addr, |
| 664 | p2p->inv_status, |
| 665 | p2p->inv_op_freq); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | |
| 670 | int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role, |
| 671 | const u8 *bssid, const u8 *ssid, size_t ssid_len, |
| 672 | unsigned int force_freq, const u8 *go_dev_addr, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 673 | int persistent_group, unsigned int pref_freq, int dev_pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 674 | { |
| 675 | struct p2p_device *dev; |
| 676 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 677 | p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d " |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 678 | "force_freq=%u allow_6ghz=%d", |
| 679 | MAC2STR(peer), role, persistent_group, force_freq, |
| 680 | p2p->allow_6ghz); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 681 | if (bssid) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 682 | p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 683 | if (go_dev_addr) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 684 | p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 685 | MAC2STR(go_dev_addr)); |
| 686 | os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN); |
| 687 | p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf; |
| 688 | } else |
| 689 | p2p->invite_go_dev_addr = NULL; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 690 | wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 691 | ssid, ssid_len); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 692 | if (dev_pw_id >= 0) { |
| 693 | p2p_dbg(p2p, "Invitation to use Device Password ID %d", |
| 694 | dev_pw_id); |
| 695 | } |
| 696 | p2p->invite_dev_pw_id = dev_pw_id; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 697 | p2p->retry_invite_req = role == P2P_INVITE_ROLE_GO && |
| 698 | persistent_group && !force_freq; |
| 699 | p2p->retry_invite_req_sent = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 700 | |
| 701 | dev = p2p_get_device(p2p, peer); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 702 | if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0 && |
| 703 | dev->oob_go_neg_freq <= 0)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 704 | p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 705 | MAC2STR(peer)); |
| 706 | return -1; |
| 707 | } |
| 708 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 709 | if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, |
| 710 | role != P2P_INVITE_ROLE_CLIENT) < 0) |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 711 | return -1; |
| 712 | |
| 713 | if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq && |
| 714 | !pref_freq) |
| 715 | dev->flags |= P2P_DEV_NO_PREF_CHAN; |
| 716 | else |
| 717 | dev->flags &= ~P2P_DEV_NO_PREF_CHAN; |
| 718 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 719 | if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) { |
| 720 | if (!(dev->info.dev_capab & |
| 721 | P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 722 | p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 723 | " that is in a group and is not discoverable", |
| 724 | MAC2STR(peer)); |
| 725 | } |
| 726 | /* TODO: use device discoverability request through GO */ |
| 727 | } |
| 728 | |
| 729 | dev->invitation_reqs = 0; |
| 730 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 731 | if (p2p->state != P2P_IDLE) |
| 732 | p2p_stop_find(p2p); |
| 733 | |
| 734 | p2p->inv_role = role; |
| 735 | p2p->inv_bssid_set = bssid != NULL; |
| 736 | if (bssid) |
| 737 | os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN); |
| 738 | os_memcpy(p2p->inv_ssid, ssid, ssid_len); |
| 739 | p2p->inv_ssid_len = ssid_len; |
| 740 | p2p->inv_persistent = persistent_group; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 741 | return p2p_invite_send(p2p, dev, go_dev_addr, dev_pw_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 742 | } |