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" |
| 13 | #include "p2p_i.h" |
| 14 | #include "p2p.h" |
| 15 | |
| 16 | |
| 17 | static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p, |
| 18 | struct p2p_device *peer, |
| 19 | const u8 *go_dev_addr) |
| 20 | { |
| 21 | struct wpabuf *buf; |
| 22 | u8 *len; |
| 23 | const u8 *dev_addr; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 24 | size_t extra = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 25 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 26 | #ifdef CONFIG_WIFI_DISPLAY |
| 27 | struct wpabuf *wfd_ie = p2p->wfd_ie_invitation; |
| 28 | if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) { |
| 29 | size_t i; |
| 30 | for (i = 0; i < p2p->num_groups; i++) { |
| 31 | struct p2p_group *g = p2p->groups[i]; |
| 32 | struct wpabuf *ie; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 33 | if (os_memcmp(p2p_group_get_interface_addr(g), |
| 34 | p2p->inv_bssid, ETH_ALEN) != 0) |
| 35 | continue; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 36 | ie = p2p_group_get_wfd_ie(g); |
| 37 | if (ie) { |
| 38 | wfd_ie = ie; |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | if (wfd_ie) |
| 44 | extra = wpabuf_len(wfd_ie); |
| 45 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 46 | |
| 47 | buf = wpabuf_alloc(1000 + extra); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 48 | if (buf == NULL) |
| 49 | return NULL; |
| 50 | |
| 51 | peer->dialog_token++; |
| 52 | if (peer->dialog_token == 0) |
| 53 | peer->dialog_token = 1; |
| 54 | p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ, |
| 55 | peer->dialog_token); |
| 56 | |
| 57 | len = p2p_buf_add_ie_hdr(buf); |
| 58 | if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent) |
| 59 | p2p_buf_add_config_timeout(buf, 0, 0); |
| 60 | else |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 61 | p2p_buf_add_config_timeout(buf, p2p->go_timeout, |
| 62 | p2p->client_timeout); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 63 | p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ? |
| 64 | P2P_INVITATION_FLAGS_TYPE : 0); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 65 | if (p2p->inv_role != P2P_INVITE_ROLE_CLIENT || |
| 66 | !(peer->flags & P2P_DEV_NO_PREF_CHAN)) |
| 67 | p2p_buf_add_operating_channel(buf, p2p->cfg->country, |
| 68 | p2p->op_reg_class, |
| 69 | p2p->op_channel); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 70 | if (p2p->inv_bssid_set) |
| 71 | p2p_buf_add_group_bssid(buf, p2p->inv_bssid); |
Irfan Sheriff | 069fa2c | 2012-09-24 23:59:04 -0700 | [diff] [blame] | 72 | p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 73 | if (go_dev_addr) |
| 74 | dev_addr = go_dev_addr; |
| 75 | else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT) |
| 76 | dev_addr = peer->info.p2p_device_addr; |
| 77 | else |
| 78 | dev_addr = p2p->cfg->dev_addr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 79 | p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len); |
| 80 | p2p_buf_add_device_info(buf, p2p, peer); |
| 81 | p2p_buf_update_ie_hdr(buf, len); |
| 82 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 83 | #ifdef CONFIG_WIFI_DISPLAY |
| 84 | if (wfd_ie) |
| 85 | wpabuf_put_buf(buf, wfd_ie); |
| 86 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 87 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 88 | return buf; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p, |
| 93 | struct p2p_device *peer, |
| 94 | u8 dialog_token, u8 status, |
| 95 | const u8 *group_bssid, |
| 96 | u8 reg_class, u8 channel, |
| 97 | struct p2p_channels *channels) |
| 98 | { |
| 99 | struct wpabuf *buf; |
| 100 | u8 *len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 101 | size_t extra = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 102 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 103 | #ifdef CONFIG_WIFI_DISPLAY |
| 104 | struct wpabuf *wfd_ie = p2p->wfd_ie_invitation; |
| 105 | if (wfd_ie && group_bssid) { |
| 106 | size_t i; |
| 107 | for (i = 0; i < p2p->num_groups; i++) { |
| 108 | struct p2p_group *g = p2p->groups[i]; |
| 109 | struct wpabuf *ie; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 110 | if (os_memcmp(p2p_group_get_interface_addr(g), |
| 111 | group_bssid, ETH_ALEN) != 0) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 112 | continue; |
| 113 | ie = p2p_group_get_wfd_ie(g); |
| 114 | if (ie) { |
| 115 | wfd_ie = ie; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | if (wfd_ie) |
| 121 | extra = wpabuf_len(wfd_ie); |
| 122 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 123 | |
| 124 | buf = wpabuf_alloc(1000 + extra); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 125 | if (buf == NULL) |
| 126 | return NULL; |
| 127 | |
| 128 | p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP, |
| 129 | dialog_token); |
| 130 | |
| 131 | len = p2p_buf_add_ie_hdr(buf); |
| 132 | p2p_buf_add_status(buf, status); |
| 133 | p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */ |
| 134 | if (reg_class && channel) |
| 135 | p2p_buf_add_operating_channel(buf, p2p->cfg->country, |
| 136 | reg_class, channel); |
| 137 | if (group_bssid) |
| 138 | p2p_buf_add_group_bssid(buf, group_bssid); |
Irfan Sheriff | 069fa2c | 2012-09-24 23:59:04 -0700 | [diff] [blame] | 139 | if (channels) |
| 140 | p2p_buf_add_channel_list(buf, p2p->cfg->country, channels); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 141 | p2p_buf_update_ie_hdr(buf, len); |
| 142 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 143 | #ifdef CONFIG_WIFI_DISPLAY |
| 144 | if (wfd_ie) |
| 145 | wpabuf_put_buf(buf, wfd_ie); |
| 146 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 147 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 148 | return buf; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa, |
| 153 | const u8 *data, size_t len, int rx_freq) |
| 154 | { |
| 155 | struct p2p_device *dev; |
| 156 | struct p2p_message msg; |
| 157 | struct wpabuf *resp = NULL; |
| 158 | u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 159 | int freq; |
| 160 | int go = 0; |
| 161 | u8 group_bssid[ETH_ALEN], *bssid; |
| 162 | int op_freq = 0; |
| 163 | u8 reg_class = 0, channel = 0; |
| 164 | struct p2p_channels intersection, *channels = NULL; |
| 165 | int persistent; |
| 166 | |
| 167 | os_memset(group_bssid, 0, sizeof(group_bssid)); |
| 168 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 169 | p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 170 | MAC2STR(sa), rx_freq); |
| 171 | |
| 172 | if (p2p_parse(data, len, &msg)) |
| 173 | return; |
| 174 | |
| 175 | dev = p2p_get_device(p2p, sa); |
| 176 | if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 177 | p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR, |
| 178 | MAC2STR(sa)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 179 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 180 | 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] | 181 | 0)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 182 | p2p_dbg(p2p, "Invitation Request add device failed " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 183 | MACSTR, MAC2STR(sa)); |
| 184 | status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 185 | goto fail; |
| 186 | } |
| 187 | |
| 188 | dev = p2p_get_device(p2p, sa); |
| 189 | if (dev == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 190 | p2p_dbg(p2p, "Reject Invitation Request from unknown peer " |
| 191 | MACSTR, MAC2STR(sa)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 192 | status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 193 | goto fail; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (!msg.group_id || !msg.channel_list) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 198 | p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from " |
| 199 | MACSTR, MAC2STR(sa)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 200 | status = P2P_SC_FAIL_INVALID_PARAMS; |
| 201 | goto fail; |
| 202 | } |
| 203 | |
| 204 | if (msg.invitation_flags) |
| 205 | persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE; |
| 206 | else { |
| 207 | /* Invitation Flags is a mandatory attribute starting from P2P |
| 208 | * spec 1.06. As a backwards compatibility mechanism, assume |
| 209 | * the request was for a persistent group if the attribute is |
| 210 | * missing. |
| 211 | */ |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 212 | p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 213 | persistent = 1; |
| 214 | } |
| 215 | |
| 216 | if (p2p_peer_channels_check(p2p, &p2p->cfg->channels, dev, |
| 217 | msg.channel_list, msg.channel_list_len) < |
| 218 | 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 219 | p2p_dbg(p2p, "No common channels found"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 220 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 221 | goto fail; |
| 222 | } |
| 223 | |
| 224 | if (p2p->cfg->invitation_process) { |
| 225 | status = p2p->cfg->invitation_process( |
| 226 | p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id, |
| 227 | msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN, |
| 228 | &go, group_bssid, &op_freq, persistent); |
| 229 | } |
| 230 | |
| 231 | if (op_freq) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 232 | p2p_dbg(p2p, "Invitation processing forced frequency %d MHz", |
| 233 | op_freq); |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 234 | if (p2p_freq_to_channel(op_freq, ®_class, &channel) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 235 | p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()", |
| 236 | op_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 237 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 238 | goto fail; |
| 239 | } |
| 240 | |
| 241 | p2p_channels_intersect(&p2p->cfg->channels, &dev->channels, |
| 242 | &intersection); |
| 243 | if (!p2p_channels_includes(&intersection, reg_class, channel)) |
| 244 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 245 | p2p_dbg(p2p, "forced freq %d MHz not in the supported channels interaction", |
| 246 | op_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 247 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 248 | goto fail; |
| 249 | } |
| 250 | |
| 251 | if (status == P2P_SC_SUCCESS) |
| 252 | channels = &intersection; |
| 253 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 254 | 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] | 255 | |
| 256 | p2p_channels_intersect(&p2p->cfg->channels, &dev->channels, |
| 257 | &intersection); |
| 258 | /* Default to own configuration as a starting point */ |
| 259 | p2p->op_reg_class = p2p->cfg->op_reg_class; |
| 260 | p2p->op_channel = p2p->cfg->op_channel; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 261 | p2p_dbg(p2p, "Own default op_class %d channel %d", |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 262 | p2p->op_reg_class, p2p->op_channel); |
| 263 | |
| 264 | /* Use peer preference if specified and compatible */ |
| 265 | if (msg.operating_channel) { |
| 266 | int req_freq; |
| 267 | req_freq = p2p_channel_to_freq( |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 268 | msg.operating_channel[3], |
| 269 | msg.operating_channel[4]); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 270 | p2p_dbg(p2p, "Peer operating channel preference: %d MHz", |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 271 | req_freq); |
| 272 | if (req_freq > 0 && |
| 273 | p2p_channels_includes(&intersection, |
| 274 | msg.operating_channel[3], |
| 275 | msg.operating_channel[4])) { |
| 276 | p2p->op_reg_class = msg.operating_channel[3]; |
| 277 | p2p->op_channel = msg.operating_channel[4]; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 278 | p2p_dbg(p2p, "Use peer preference op_class %d channel %d", |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 279 | p2p->op_reg_class, p2p->op_channel); |
| 280 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 281 | p2p_dbg(p2p, "Cannot use peer channel preference"); |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | if (!p2p_channels_includes(&intersection, p2p->op_reg_class, |
| 286 | p2p->op_channel)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 287 | 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] | 288 | p2p->op_reg_class, p2p->op_channel); |
| 289 | p2p_reselect_channel(p2p, &intersection); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 290 | p2p_dbg(p2p, "Re-selection result: op_class %d channel %d", |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 291 | p2p->op_reg_class, p2p->op_channel); |
| 292 | if (!p2p_channels_includes(&intersection, |
| 293 | p2p->op_reg_class, |
| 294 | p2p->op_channel)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 295 | 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] | 296 | p2p->op_reg_class, p2p->op_channel); |
| 297 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 298 | goto fail; |
| 299 | } |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 300 | } else if (!(dev->flags & P2P_DEV_FORCE_FREQ) && |
| 301 | !p2p->cfg->cfg_op_channel) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 302 | 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] | 303 | p2p->op_reg_class, p2p->op_channel); |
| 304 | p2p_reselect_channel(p2p, &intersection); |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 307 | op_freq = p2p_channel_to_freq(p2p->op_reg_class, |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 308 | p2p->op_channel); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 309 | if (op_freq < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 310 | 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] | 311 | p2p->cfg->country[0], p2p->cfg->country[1], |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 312 | p2p->op_reg_class, p2p->op_channel); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 313 | status = P2P_SC_FAIL_NO_COMMON_CHANNELS; |
| 314 | goto fail; |
| 315 | } |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 316 | p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 317 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 318 | if (status == P2P_SC_SUCCESS) { |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 319 | reg_class = p2p->op_reg_class; |
| 320 | channel = p2p->op_channel; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 321 | channels = &intersection; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | fail: |
| 326 | if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid)) |
| 327 | bssid = group_bssid; |
| 328 | else |
| 329 | bssid = NULL; |
| 330 | resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status, |
| 331 | bssid, reg_class, channel, channels); |
| 332 | |
| 333 | if (resp == NULL) |
| 334 | goto out; |
| 335 | |
| 336 | if (rx_freq > 0) |
| 337 | freq = rx_freq; |
| 338 | else |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 339 | freq = p2p_channel_to_freq(p2p->cfg->reg_class, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 340 | p2p->cfg->channel); |
| 341 | if (freq < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 342 | p2p_dbg(p2p, "Unknown regulatory class/channel"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 343 | goto out; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Store copy of invitation data to be used when processing TX status |
| 348 | * callback for the Acton frame. |
| 349 | */ |
| 350 | os_memcpy(p2p->inv_sa, sa, ETH_ALEN); |
| 351 | if (msg.group_bssid) { |
| 352 | os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN); |
| 353 | p2p->inv_group_bssid_ptr = p2p->inv_group_bssid; |
| 354 | } else |
| 355 | p2p->inv_group_bssid_ptr = NULL; |
| 356 | if (msg.group_id_len - ETH_ALEN <= 32) { |
| 357 | os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN, |
| 358 | msg.group_id_len - ETH_ALEN); |
| 359 | p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN; |
| 360 | } |
| 361 | os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN); |
| 362 | p2p->inv_status = status; |
| 363 | p2p->inv_op_freq = op_freq; |
| 364 | |
| 365 | p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE; |
| 366 | if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, |
| 367 | p2p->cfg->dev_addr, |
| 368 | wpabuf_head(resp), wpabuf_len(resp), 200) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 369 | p2p_dbg(p2p, "Failed to send Action frame"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | out: |
| 373 | wpabuf_free(resp); |
| 374 | p2p_parse_free(&msg); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa, |
| 379 | const u8 *data, size_t len) |
| 380 | { |
| 381 | struct p2p_device *dev; |
| 382 | struct p2p_message msg; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 383 | struct p2p_channels intersection, *channels = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 384 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 385 | p2p_dbg(p2p, "Received Invitation Response from " MACSTR, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 386 | MAC2STR(sa)); |
| 387 | |
| 388 | dev = p2p_get_device(p2p, sa); |
| 389 | if (dev == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 390 | p2p_dbg(p2p, "Ignore Invitation Response from unknown peer " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 391 | MACSTR, MAC2STR(sa)); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | if (dev != p2p->invite_peer) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 396 | p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 397 | MACSTR, MAC2STR(sa)); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | if (p2p_parse(data, len, &msg)) |
| 402 | return; |
| 403 | |
| 404 | if (!msg.status) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 405 | p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from " |
| 406 | MACSTR, MAC2STR(sa)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 407 | p2p_parse_free(&msg); |
| 408 | return; |
| 409 | } |
| 410 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 411 | if (!msg.channel_list) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 412 | p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from " |
| 413 | MACSTR, MAC2STR(sa)); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 414 | #ifdef CONFIG_P2P_STRICT |
| 415 | p2p_parse_free(&msg); |
| 416 | return; |
| 417 | #endif /* CONFIG_P2P_STRICT */ |
| 418 | /* Try to survive without peer channel list */ |
| 419 | channels = &p2p->channels; |
| 420 | } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev, |
| 421 | msg.channel_list, |
| 422 | msg.channel_list_len) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 423 | p2p_dbg(p2p, "No common channels found"); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 424 | p2p_parse_free(&msg); |
| 425 | return; |
| 426 | } else { |
| 427 | p2p_channels_intersect(&p2p->channels, &dev->channels, |
| 428 | &intersection); |
| 429 | channels = &intersection; |
| 430 | } |
| 431 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 432 | if (p2p->cfg->invitation_result) |
| 433 | p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status, |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 434 | msg.group_bssid, channels, sa); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 435 | |
| 436 | p2p_parse_free(&msg); |
| 437 | |
| 438 | p2p_clear_timeout(p2p); |
| 439 | p2p_set_state(p2p, P2P_IDLE); |
| 440 | p2p->invite_peer = NULL; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev, |
| 445 | const u8 *go_dev_addr) |
| 446 | { |
| 447 | struct wpabuf *req; |
| 448 | int freq; |
| 449 | |
| 450 | freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq; |
| 451 | if (freq <= 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 452 | p2p_dbg(p2p, "No Listen/Operating frequency known for the peer " |
| 453 | MACSTR " to send Invitation Request", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 454 | MAC2STR(dev->info.p2p_device_addr)); |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | req = p2p_build_invitation_req(p2p, dev, go_dev_addr); |
| 459 | if (req == NULL) |
| 460 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 461 | if (p2p->state != P2P_IDLE) |
| 462 | p2p_stop_listen_for_freq(p2p, freq); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 463 | p2p_dbg(p2p, "Sending Invitation Request"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 464 | p2p_set_state(p2p, P2P_INVITE); |
| 465 | p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST; |
| 466 | p2p->invite_peer = dev; |
| 467 | dev->invitation_reqs++; |
| 468 | if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr, |
| 469 | p2p->cfg->dev_addr, dev->info.p2p_device_addr, |
| 470 | wpabuf_head(req), wpabuf_len(req), 200) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 471 | p2p_dbg(p2p, "Failed to send Action frame"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 472 | /* Use P2P find to recover and retry */ |
| 473 | p2p_set_timeout(p2p, 0, 0); |
| 474 | } |
| 475 | |
| 476 | wpabuf_free(req); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | |
| 482 | void p2p_invitation_req_cb(struct p2p_data *p2p, int success) |
| 483 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 484 | p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 485 | |
| 486 | if (p2p->invite_peer == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 487 | p2p_dbg(p2p, "No pending Invite"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Use P2P find, if needed, to find the other device from its listen |
| 493 | * channel. |
| 494 | */ |
| 495 | p2p_set_state(p2p, P2P_INVITE); |
Irfan Sheriff | 1a2ce11 | 2012-10-30 22:22:52 -0700 | [diff] [blame] | 496 | #ifdef ANDROID_P2P |
| 497 | p2p_set_timeout(p2p, 0, 350000); |
| 498 | #else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 499 | p2p_set_timeout(p2p, 0, 100000); |
Irfan Sheriff | 1a2ce11 | 2012-10-30 22:22:52 -0700 | [diff] [blame] | 500 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | |
| 504 | void p2p_invitation_resp_cb(struct p2p_data *p2p, int success) |
| 505 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 506 | p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 507 | p2p->cfg->send_action_done(p2p->cfg->cb_ctx); |
| 508 | |
Dmitry Shmidt | 0ccb66e | 2013-03-29 16:41:28 -0700 | [diff] [blame] | 509 | if (!success) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 510 | 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] | 511 | |
| 512 | if (p2p->cfg->invitation_received) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 513 | p2p->cfg->invitation_received(p2p->cfg->cb_ctx, |
| 514 | p2p->inv_sa, |
| 515 | p2p->inv_group_bssid_ptr, |
| 516 | p2p->inv_ssid, p2p->inv_ssid_len, |
| 517 | p2p->inv_go_dev_addr, |
| 518 | p2p->inv_status, |
| 519 | p2p->inv_op_freq); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | |
| 524 | int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role, |
| 525 | const u8 *bssid, const u8 *ssid, size_t ssid_len, |
| 526 | unsigned int force_freq, const u8 *go_dev_addr, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 527 | int persistent_group, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 528 | { |
| 529 | struct p2p_device *dev; |
| 530 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 531 | p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 532 | "force_freq=%u", |
| 533 | MAC2STR(peer), role, persistent_group, force_freq); |
| 534 | if (bssid) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 535 | p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 536 | if (go_dev_addr) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 537 | p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 538 | MAC2STR(go_dev_addr)); |
| 539 | os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN); |
| 540 | p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf; |
| 541 | } else |
| 542 | p2p->invite_go_dev_addr = NULL; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 543 | wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 544 | ssid, ssid_len); |
| 545 | |
| 546 | dev = p2p_get_device(p2p, peer); |
| 547 | if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 548 | p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 549 | MAC2STR(peer)); |
| 550 | return -1; |
| 551 | } |
| 552 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 553 | if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq) < 0) |
| 554 | return -1; |
| 555 | |
| 556 | if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq && |
| 557 | !pref_freq) |
| 558 | dev->flags |= P2P_DEV_NO_PREF_CHAN; |
| 559 | else |
| 560 | dev->flags &= ~P2P_DEV_NO_PREF_CHAN; |
| 561 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 562 | if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) { |
| 563 | if (!(dev->info.dev_capab & |
| 564 | P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame^] | 565 | p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 566 | " that is in a group and is not discoverable", |
| 567 | MAC2STR(peer)); |
| 568 | } |
| 569 | /* TODO: use device discoverability request through GO */ |
| 570 | } |
| 571 | |
| 572 | dev->invitation_reqs = 0; |
| 573 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 574 | if (p2p->state != P2P_IDLE) |
| 575 | p2p_stop_find(p2p); |
| 576 | |
| 577 | p2p->inv_role = role; |
| 578 | p2p->inv_bssid_set = bssid != NULL; |
| 579 | if (bssid) |
| 580 | os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN); |
| 581 | os_memcpy(p2p->inv_ssid, ssid, ssid_len); |
| 582 | p2p->inv_ssid_len = ssid_len; |
| 583 | p2p->inv_persistent = persistent_group; |
| 584 | return p2p_invite_send(p2p, dev, go_dev_addr); |
| 585 | } |