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