Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * P2P - generic helper functions |
| 3 | * Copyright (c) 2009, 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" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 12 | #include "common/defs.h" |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 13 | #include "common/ieee802_11_common.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 14 | #include "p2p_i.h" |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * p2p_random - Generate random string for SSID and passphrase |
| 19 | * @buf: Buffer for returning the result |
| 20 | * @len: Number of octets to write to the buffer |
| 21 | * Returns: 0 on success, -1 on failure |
| 22 | * |
| 23 | * This function generates a random string using the following character set: |
| 24 | * 'A'-'Z', 'a'-'z', '0'-'9'. |
| 25 | */ |
| 26 | int p2p_random(char *buf, size_t len) |
| 27 | { |
| 28 | u8 val; |
| 29 | size_t i; |
| 30 | u8 letters = 'Z' - 'A' + 1; |
| 31 | u8 numbers = 10; |
| 32 | |
| 33 | if (os_get_random((unsigned char *) buf, len)) |
| 34 | return -1; |
| 35 | /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */ |
| 36 | for (i = 0; i < len; i++) { |
| 37 | val = buf[i]; |
| 38 | val %= 2 * letters + numbers; |
| 39 | if (val < letters) |
| 40 | buf[i] = 'A' + val; |
| 41 | else if (val < 2 * letters) |
| 42 | buf[i] = 'a' + (val - letters); |
| 43 | else |
| 44 | buf[i] = '0' + (val - 2 * letters); |
| 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 51 | /** |
| 52 | * p2p_channel_to_freq - Convert channel info to frequency |
| 53 | * @op_class: Operating class |
| 54 | * @channel: Channel number |
| 55 | * Returns: Frequency in MHz or -1 if the specified channel is unknown |
| 56 | */ |
| 57 | int p2p_channel_to_freq(int op_class, int channel) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 58 | { |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 59 | return ieee80211_chan_to_freq(NULL, op_class, channel); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 64 | * p2p_freq_to_channel - Convert frequency into channel info |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 65 | * @op_class: Buffer for returning operating class |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 66 | * @channel: Buffer for returning channel number |
| 67 | * Returns: 0 on success, -1 if the specified frequency is unknown |
| 68 | */ |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 69 | int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 70 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 71 | if (ieee80211_freq_to_channel_ext(freq, 0, 0, op_class, channel) == |
| 72 | NUM_HOSTAPD_MODES) |
| 73 | return -1; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 74 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 75 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | static void p2p_reg_class_intersect(const struct p2p_reg_class *a, |
| 80 | const struct p2p_reg_class *b, |
| 81 | struct p2p_reg_class *res) |
| 82 | { |
| 83 | size_t i, j; |
| 84 | |
| 85 | res->reg_class = a->reg_class; |
| 86 | |
| 87 | for (i = 0; i < a->channels; i++) { |
| 88 | for (j = 0; j < b->channels; j++) { |
| 89 | if (a->channel[i] != b->channel[j]) |
| 90 | continue; |
| 91 | res->channel[res->channels] = a->channel[i]; |
| 92 | res->channels++; |
| 93 | if (res->channels == P2P_MAX_REG_CLASS_CHANNELS) |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * p2p_channels_intersect - Intersection of supported channel lists |
| 102 | * @a: First set of supported channels |
| 103 | * @b: Second set of supported channels |
| 104 | * @res: Data structure for returning the intersection of support channels |
| 105 | * |
| 106 | * This function can be used to find a common set of supported channels. Both |
| 107 | * input channels sets are assumed to use the same country code. If different |
| 108 | * country codes are used, the regulatory class numbers may not be matched |
| 109 | * correctly and results are undefined. |
| 110 | */ |
| 111 | void p2p_channels_intersect(const struct p2p_channels *a, |
| 112 | const struct p2p_channels *b, |
| 113 | struct p2p_channels *res) |
| 114 | { |
| 115 | size_t i, j; |
| 116 | |
| 117 | os_memset(res, 0, sizeof(*res)); |
| 118 | |
| 119 | for (i = 0; i < a->reg_classes; i++) { |
| 120 | const struct p2p_reg_class *a_reg = &a->reg_class[i]; |
| 121 | for (j = 0; j < b->reg_classes; j++) { |
| 122 | const struct p2p_reg_class *b_reg = &b->reg_class[j]; |
| 123 | if (a_reg->reg_class != b_reg->reg_class) |
| 124 | continue; |
| 125 | p2p_reg_class_intersect( |
| 126 | a_reg, b_reg, |
| 127 | &res->reg_class[res->reg_classes]); |
| 128 | if (res->reg_class[res->reg_classes].channels) { |
| 129 | res->reg_classes++; |
| 130 | if (res->reg_classes == P2P_MAX_REG_CLASSES) |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 138 | static void p2p_op_class_union(struct p2p_reg_class *cl, |
| 139 | const struct p2p_reg_class *b_cl) |
| 140 | { |
| 141 | size_t i, j; |
| 142 | |
| 143 | for (i = 0; i < b_cl->channels; i++) { |
| 144 | for (j = 0; j < cl->channels; j++) { |
| 145 | if (b_cl->channel[i] == cl->channel[j]) |
| 146 | break; |
| 147 | } |
| 148 | if (j == cl->channels) { |
| 149 | if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS) |
| 150 | return; |
| 151 | cl->channel[cl->channels++] = b_cl->channel[i]; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 158 | * p2p_channels_union_inplace - Inplace union of channel lists |
| 159 | * @res: Input data and place for returning union of the channel sets |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 160 | * @b: Second set of channels |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 161 | */ |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 162 | void p2p_channels_union_inplace(struct p2p_channels *res, |
| 163 | const struct p2p_channels *b) |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 164 | { |
| 165 | size_t i, j; |
| 166 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 167 | for (i = 0; i < res->reg_classes; i++) { |
| 168 | struct p2p_reg_class *cl = &res->reg_class[i]; |
| 169 | for (j = 0; j < b->reg_classes; j++) { |
| 170 | const struct p2p_reg_class *b_cl = &b->reg_class[j]; |
| 171 | if (cl->reg_class != b_cl->reg_class) |
| 172 | continue; |
| 173 | p2p_op_class_union(cl, b_cl); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | for (j = 0; j < b->reg_classes; j++) { |
| 178 | const struct p2p_reg_class *b_cl = &b->reg_class[j]; |
| 179 | |
| 180 | for (i = 0; i < res->reg_classes; i++) { |
| 181 | struct p2p_reg_class *cl = &res->reg_class[i]; |
| 182 | if (cl->reg_class == b_cl->reg_class) |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | if (i == res->reg_classes) { |
| 187 | if (res->reg_classes == P2P_MAX_REG_CLASSES) |
| 188 | return; |
| 189 | os_memcpy(&res->reg_class[res->reg_classes++], |
| 190 | b_cl, sizeof(struct p2p_reg_class)); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 196 | /** |
| 197 | * p2p_channels_union - Union of channel lists |
| 198 | * @a: First set of channels |
| 199 | * @b: Second set of channels |
| 200 | * @res: Data structure for returning the union of channels |
| 201 | */ |
| 202 | void p2p_channels_union(const struct p2p_channels *a, |
| 203 | const struct p2p_channels *b, |
| 204 | struct p2p_channels *res) |
| 205 | { |
| 206 | os_memcpy(res, a, sizeof(*res)); |
| 207 | p2p_channels_union_inplace(res, b); |
| 208 | } |
| 209 | |
| 210 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 211 | void p2p_channels_remove_freqs(struct p2p_channels *chan, |
| 212 | const struct wpa_freq_range_list *list) |
| 213 | { |
| 214 | size_t o, c; |
| 215 | |
| 216 | if (list == NULL) |
| 217 | return; |
| 218 | |
| 219 | o = 0; |
| 220 | while (o < chan->reg_classes) { |
| 221 | struct p2p_reg_class *op = &chan->reg_class[o]; |
| 222 | |
| 223 | c = 0; |
| 224 | while (c < op->channels) { |
| 225 | int freq = p2p_channel_to_freq(op->reg_class, |
| 226 | op->channel[c]); |
| 227 | if (freq > 0 && freq_range_list_includes(list, freq)) { |
| 228 | op->channels--; |
| 229 | os_memmove(&op->channel[c], |
| 230 | &op->channel[c + 1], |
| 231 | op->channels - c); |
| 232 | } else |
| 233 | c++; |
| 234 | } |
| 235 | |
| 236 | if (op->channels == 0) { |
| 237 | chan->reg_classes--; |
| 238 | os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1], |
| 239 | (chan->reg_classes - o) * |
| 240 | sizeof(struct p2p_reg_class)); |
| 241 | } else |
| 242 | o++; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 247 | /** |
| 248 | * p2p_channels_includes - Check whether a channel is included in the list |
| 249 | * @channels: List of supported channels |
| 250 | * @reg_class: Regulatory class of the channel to search |
| 251 | * @channel: Channel number of the channel to search |
| 252 | * Returns: 1 if channel was found or 0 if not |
| 253 | */ |
| 254 | int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class, |
| 255 | u8 channel) |
| 256 | { |
| 257 | size_t i, j; |
| 258 | for (i = 0; i < channels->reg_classes; i++) { |
| 259 | const struct p2p_reg_class *reg = &channels->reg_class[i]; |
| 260 | if (reg->reg_class != reg_class) |
| 261 | continue; |
| 262 | for (j = 0; j < reg->channels; j++) { |
| 263 | if (reg->channel[j] == channel) |
| 264 | return 1; |
| 265 | } |
| 266 | } |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 271 | int p2p_channels_includes_freq(const struct p2p_channels *channels, |
| 272 | unsigned int freq) |
| 273 | { |
| 274 | size_t i, j; |
| 275 | for (i = 0; i < channels->reg_classes; i++) { |
| 276 | const struct p2p_reg_class *reg = &channels->reg_class[i]; |
| 277 | for (j = 0; j < reg->channels; j++) { |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 278 | if (p2p_channel_to_freq(reg->reg_class, |
| 279 | reg->channel[j]) == (int) freq) |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 280 | return 1; |
| 281 | } |
| 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 287 | int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq) |
| 288 | { |
| 289 | u8 op_reg_class, op_channel; |
Dmitry Shmidt | 4b06059 | 2013-04-29 16:42:49 -0700 | [diff] [blame] | 290 | if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 291 | return 0; |
| 292 | return p2p_channels_includes(&p2p->cfg->channels, op_reg_class, |
| 293 | op_channel); |
| 294 | } |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 295 | |
| 296 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 297 | int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq) |
| 298 | { |
| 299 | u8 op_reg_class, op_channel; |
| 300 | if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0) |
| 301 | return 0; |
| 302 | return p2p_channels_includes(&p2p->cfg->channels, op_reg_class, |
| 303 | op_channel) && |
| 304 | !freq_range_list_includes(&p2p->no_go_freq, freq); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq) |
| 309 | { |
| 310 | u8 op_reg_class, op_channel; |
| 311 | if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0) |
| 312 | return 0; |
| 313 | return p2p_channels_includes(&p2p->cfg->channels, op_reg_class, |
| 314 | op_channel) || |
| 315 | p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class, |
| 316 | op_channel); |
| 317 | } |
| 318 | |
| 319 | |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 320 | unsigned int p2p_get_pref_freq(struct p2p_data *p2p, |
| 321 | const struct p2p_channels *channels) |
| 322 | { |
| 323 | unsigned int i; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 324 | int freq = 0; |
| 325 | const struct p2p_channels *tmpc = channels ? |
| 326 | channels : &p2p->cfg->channels; |
| 327 | |
| 328 | if (tmpc == NULL) |
| 329 | return 0; |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 330 | |
| 331 | for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) { |
| 332 | freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class, |
| 333 | p2p->cfg->pref_chan[i].chan); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 334 | if (p2p_channels_includes_freq(tmpc, freq)) |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 335 | return freq; |
| 336 | } |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 337 | return 0; |
| 338 | } |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 339 | |
| 340 | |
| 341 | void p2p_channels_dump(struct p2p_data *p2p, const char *title, |
| 342 | const struct p2p_channels *chan) |
| 343 | { |
| 344 | char buf[500], *pos, *end; |
| 345 | size_t i, j; |
| 346 | int ret; |
| 347 | |
| 348 | pos = buf; |
| 349 | end = pos + sizeof(buf); |
| 350 | |
| 351 | for (i = 0; i < chan->reg_classes; i++) { |
| 352 | const struct p2p_reg_class *c; |
| 353 | c = &chan->reg_class[i]; |
| 354 | ret = os_snprintf(pos, end - pos, " %u:", c->reg_class); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 355 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 356 | break; |
| 357 | pos += ret; |
| 358 | |
| 359 | for (j = 0; j < c->channels; j++) { |
| 360 | ret = os_snprintf(pos, end - pos, "%s%u", |
| 361 | j == 0 ? "" : ",", |
| 362 | c->channel[j]); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 363 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 364 | break; |
| 365 | pos += ret; |
| 366 | } |
| 367 | } |
| 368 | *pos = '\0'; |
| 369 | |
| 370 | p2p_dbg(p2p, "%s:%s", title, buf); |
| 371 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 372 | |
| 373 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 374 | static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels) |
| 375 | { |
| 376 | unsigned int r; |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 377 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
| 378 | r = 0; |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 379 | r %= num_channels; |
| 380 | return channels[r]; |
| 381 | } |
| 382 | |
| 383 | |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 384 | int p2p_channel_select(struct p2p_channels *chans, const int *classes, |
| 385 | u8 *op_class, u8 *op_channel) |
| 386 | { |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 387 | unsigned int i, j; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 388 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 389 | for (j = 0; classes == NULL || classes[j]; j++) { |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 390 | for (i = 0; i < chans->reg_classes; i++) { |
| 391 | struct p2p_reg_class *c = &chans->reg_class[i]; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 392 | |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 393 | if (c->channels == 0) |
| 394 | continue; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 395 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 396 | if (classes == NULL || c->reg_class == classes[j]) { |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 397 | /* |
| 398 | * Pick one of the available channels in the |
| 399 | * operating class at random. |
| 400 | */ |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 401 | *op_class = c->reg_class; |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 402 | *op_channel = p2p_channel_pick_random( |
| 403 | c->channel, c->channels); |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 404 | return 0; |
| 405 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 406 | } |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 407 | if (classes == NULL) |
| 408 | break; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | return -1; |
| 412 | } |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 413 | |
| 414 | |
| 415 | int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class, |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 416 | u8 *op_channel, |
| 417 | struct wpa_freq_range_list *avoid_list, |
| 418 | struct wpa_freq_range_list *disallow_list) |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 419 | { |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 420 | u8 chan[4]; |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 421 | unsigned int num_channels = 0; |
| 422 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 423 | /* Try to find available social channels from 2.4 GHz. |
| 424 | * If the avoid_list includes any of the 2.4 GHz social channels, that |
| 425 | * channel is not allowed by p2p_channels_includes() rules. However, it |
| 426 | * is assumed to allow minimal traffic for P2P negotiation, so allow it |
| 427 | * here for social channel selection unless explicitly disallowed in the |
| 428 | * disallow_list. */ |
| 429 | if (p2p_channels_includes(chans, 81, 1) || |
| 430 | (freq_range_list_includes(avoid_list, 2412) && |
| 431 | !freq_range_list_includes(disallow_list, 2412))) |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 432 | chan[num_channels++] = 1; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 433 | if (p2p_channels_includes(chans, 81, 6) || |
| 434 | (freq_range_list_includes(avoid_list, 2437) && |
| 435 | !freq_range_list_includes(disallow_list, 2437))) |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 436 | chan[num_channels++] = 6; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 437 | if (p2p_channels_includes(chans, 81, 11) || |
| 438 | (freq_range_list_includes(avoid_list, 2462) && |
| 439 | !freq_range_list_includes(disallow_list, 2462))) |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 440 | chan[num_channels++] = 11; |
| 441 | |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 442 | /* Try to find available social channels from 60 GHz */ |
| 443 | if (p2p_channels_includes(chans, 180, 2)) |
| 444 | chan[num_channels++] = 2; |
| 445 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 446 | if (num_channels == 0) |
| 447 | return -1; |
| 448 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 449 | *op_channel = p2p_channel_pick_random(chan, num_channels); |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 450 | if (*op_channel == 2) |
| 451 | *op_class = 180; |
| 452 | else |
| 453 | *op_class = 81; |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 454 | |
| 455 | return 0; |
| 456 | } |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 457 | |
| 458 | |
| 459 | int p2p_channels_to_freqs(const struct p2p_channels *channels, int *freq_list, |
| 460 | unsigned int max_len) |
| 461 | { |
| 462 | unsigned int i, idx; |
| 463 | |
| 464 | if (!channels || max_len == 0) |
| 465 | return 0; |
| 466 | |
| 467 | for (i = 0, idx = 0; i < channels->reg_classes; i++) { |
| 468 | const struct p2p_reg_class *c = &channels->reg_class[i]; |
| 469 | unsigned int j; |
| 470 | |
| 471 | if (idx + 1 == max_len) |
| 472 | break; |
| 473 | for (j = 0; j < c->channels; j++) { |
| 474 | int freq; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 475 | unsigned int k; |
| 476 | |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 477 | if (idx + 1 == max_len) |
| 478 | break; |
| 479 | freq = p2p_channel_to_freq(c->reg_class, |
| 480 | c->channel[j]); |
| 481 | if (freq < 0) |
| 482 | continue; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 483 | |
| 484 | for (k = 0; k < idx; k++) { |
| 485 | if (freq_list[k] == freq) |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | if (k < idx) |
| 490 | continue; |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 491 | freq_list[idx++] = freq; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | freq_list[idx] = 0; |
| 496 | |
| 497 | return idx; |
| 498 | } |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 499 | |
| 500 | |
| 501 | void p2p_copy_channels(struct p2p_channels *dst, |
| 502 | const struct p2p_channels *src, bool allow_6ghz) |
| 503 | { |
| 504 | size_t i, j; |
| 505 | |
| 506 | if (allow_6ghz) { |
| 507 | os_memcpy(dst, src, sizeof(struct p2p_channels)); |
| 508 | return; |
| 509 | } |
| 510 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame^] | 511 | for (i = 0, j = 0; i < src->reg_classes; i++) { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 512 | if (is_6ghz_op_class(src->reg_class[i].reg_class)) |
| 513 | continue; |
| 514 | os_memcpy(&dst->reg_class[j], &src->reg_class[i], |
| 515 | sizeof(struct p2p_reg_class)); |
| 516 | j++; |
| 517 | } |
| 518 | dst->reg_classes = j; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | int p2p_remove_6ghz_channels(unsigned int *pref_freq_list, int size) |
| 523 | { |
| 524 | int i; |
| 525 | |
| 526 | for (i = 0; i < size; i++) { |
| 527 | if (is_6ghz_freq(pref_freq_list[i])) { |
| 528 | wpa_printf(MSG_DEBUG, "P2P: Remove 6 GHz channel %d", |
| 529 | pref_freq_list[i]); |
| 530 | size--; |
| 531 | os_memmove(&pref_freq_list[i], &pref_freq_list[i + 1], |
| 532 | (size - i) * sizeof(pref_freq_list[0])); |
| 533 | i--; |
| 534 | } |
| 535 | } |
| 536 | return i; |
| 537 | } |