blob: f32751d79ca8b451abc186f31c41df6ce7fc1665 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * P2P - generic helper functions
3 * Copyright (c) 2009, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080012#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013#include "p2p_i.h"
14
15
16/**
17 * p2p_random - Generate random string for SSID and passphrase
18 * @buf: Buffer for returning the result
19 * @len: Number of octets to write to the buffer
20 * Returns: 0 on success, -1 on failure
21 *
22 * This function generates a random string using the following character set:
23 * 'A'-'Z', 'a'-'z', '0'-'9'.
24 */
25int p2p_random(char *buf, size_t len)
26{
27 u8 val;
28 size_t i;
29 u8 letters = 'Z' - 'A' + 1;
30 u8 numbers = 10;
31
32 if (os_get_random((unsigned char *) buf, len))
33 return -1;
34 /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */
35 for (i = 0; i < len; i++) {
36 val = buf[i];
37 val %= 2 * letters + numbers;
38 if (val < letters)
39 buf[i] = 'A' + val;
40 else if (val < 2 * letters)
41 buf[i] = 'a' + (val - letters);
42 else
43 buf[i] = '0' + (val - 2 * letters);
44 }
45
46 return 0;
47}
48
49
Dmitry Shmidt4b060592013-04-29 16:42:49 -070050/**
51 * p2p_channel_to_freq - Convert channel info to frequency
52 * @op_class: Operating class
53 * @channel: Channel number
54 * Returns: Frequency in MHz or -1 if the specified channel is unknown
55 */
56int p2p_channel_to_freq(int op_class, int channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057{
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080058 return ieee80211_chan_to_freq(NULL, op_class, channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059}
60
61
62/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 * p2p_freq_to_channel - Convert frequency into channel info
Dmitry Shmidt4b060592013-04-29 16:42:49 -070064 * @op_class: Buffer for returning operating class
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065 * @channel: Buffer for returning channel number
66 * Returns: 0 on success, -1 if the specified frequency is unknown
67 */
Dmitry Shmidt4b060592013-04-29 16:42:49 -070068int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069{
70 /* TODO: more operating classes */
71 if (freq >= 2412 && freq <= 2472) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070072 if ((freq - 2407) % 5)
73 return -1;
74
Dmitry Shmidt4b060592013-04-29 16:42:49 -070075 *op_class = 81; /* 2.407 GHz, channels 1..13 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070076 *channel = (freq - 2407) / 5;
77 return 0;
78 }
79
80 if (freq == 2484) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -070081 *op_class = 82; /* channel 14 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082 *channel = 14;
83 return 0;
84 }
85
86 if (freq >= 5180 && freq <= 5240) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070087 if ((freq - 5000) % 5)
88 return -1;
89
Dmitry Shmidt4b060592013-04-29 16:42:49 -070090 *op_class = 115; /* 5 GHz, channels 36..48 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091 *channel = (freq - 5000) / 5;
92 return 0;
93 }
94
95 if (freq >= 5745 && freq <= 5805) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -070096 if ((freq - 5000) % 5)
97 return -1;
98
Dmitry Shmidt4b060592013-04-29 16:42:49 -070099 *op_class = 124; /* 5 GHz, channels 149..161 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 *channel = (freq - 5000) / 5;
101 return 0;
102 }
103
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700104 if (freq >= 58320 && freq <= 64800) {
105 if ((freq - 58320) % 2160)
106 return -1;
107
108 *op_class = 180; /* 60 GHz, channels 1..4 */
109 *channel = (freq - 56160) / 2160;
110 return 0;
111 }
112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 return -1;
114}
115
116
117static void p2p_reg_class_intersect(const struct p2p_reg_class *a,
118 const struct p2p_reg_class *b,
119 struct p2p_reg_class *res)
120{
121 size_t i, j;
122
123 res->reg_class = a->reg_class;
124
125 for (i = 0; i < a->channels; i++) {
126 for (j = 0; j < b->channels; j++) {
127 if (a->channel[i] != b->channel[j])
128 continue;
129 res->channel[res->channels] = a->channel[i];
130 res->channels++;
131 if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
132 return;
133 }
134 }
135}
136
137
138/**
139 * p2p_channels_intersect - Intersection of supported channel lists
140 * @a: First set of supported channels
141 * @b: Second set of supported channels
142 * @res: Data structure for returning the intersection of support channels
143 *
144 * This function can be used to find a common set of supported channels. Both
145 * input channels sets are assumed to use the same country code. If different
146 * country codes are used, the regulatory class numbers may not be matched
147 * correctly and results are undefined.
148 */
149void p2p_channels_intersect(const struct p2p_channels *a,
150 const struct p2p_channels *b,
151 struct p2p_channels *res)
152{
153 size_t i, j;
154
155 os_memset(res, 0, sizeof(*res));
156
157 for (i = 0; i < a->reg_classes; i++) {
158 const struct p2p_reg_class *a_reg = &a->reg_class[i];
159 for (j = 0; j < b->reg_classes; j++) {
160 const struct p2p_reg_class *b_reg = &b->reg_class[j];
161 if (a_reg->reg_class != b_reg->reg_class)
162 continue;
163 p2p_reg_class_intersect(
164 a_reg, b_reg,
165 &res->reg_class[res->reg_classes]);
166 if (res->reg_class[res->reg_classes].channels) {
167 res->reg_classes++;
168 if (res->reg_classes == P2P_MAX_REG_CLASSES)
169 return;
170 }
171 }
172 }
173}
174
175
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700176static void p2p_op_class_union(struct p2p_reg_class *cl,
177 const struct p2p_reg_class *b_cl)
178{
179 size_t i, j;
180
181 for (i = 0; i < b_cl->channels; i++) {
182 for (j = 0; j < cl->channels; j++) {
183 if (b_cl->channel[i] == cl->channel[j])
184 break;
185 }
186 if (j == cl->channels) {
187 if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
188 return;
189 cl->channel[cl->channels++] = b_cl->channel[i];
190 }
191 }
192}
193
194
195/**
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800196 * p2p_channels_union_inplace - Inplace union of channel lists
197 * @res: Input data and place for returning union of the channel sets
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700198 * @b: Second set of channels
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700199 */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800200void p2p_channels_union_inplace(struct p2p_channels *res,
201 const struct p2p_channels *b)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700202{
203 size_t i, j;
204
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700205 for (i = 0; i < res->reg_classes; i++) {
206 struct p2p_reg_class *cl = &res->reg_class[i];
207 for (j = 0; j < b->reg_classes; j++) {
208 const struct p2p_reg_class *b_cl = &b->reg_class[j];
209 if (cl->reg_class != b_cl->reg_class)
210 continue;
211 p2p_op_class_union(cl, b_cl);
212 }
213 }
214
215 for (j = 0; j < b->reg_classes; j++) {
216 const struct p2p_reg_class *b_cl = &b->reg_class[j];
217
218 for (i = 0; i < res->reg_classes; i++) {
219 struct p2p_reg_class *cl = &res->reg_class[i];
220 if (cl->reg_class == b_cl->reg_class)
221 break;
222 }
223
224 if (i == res->reg_classes) {
225 if (res->reg_classes == P2P_MAX_REG_CLASSES)
226 return;
227 os_memcpy(&res->reg_class[res->reg_classes++],
228 b_cl, sizeof(struct p2p_reg_class));
229 }
230 }
231}
232
233
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800234/**
235 * p2p_channels_union - Union of channel lists
236 * @a: First set of channels
237 * @b: Second set of channels
238 * @res: Data structure for returning the union of channels
239 */
240void p2p_channels_union(const struct p2p_channels *a,
241 const struct p2p_channels *b,
242 struct p2p_channels *res)
243{
244 os_memcpy(res, a, sizeof(*res));
245 p2p_channels_union_inplace(res, b);
246}
247
248
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700249void p2p_channels_remove_freqs(struct p2p_channels *chan,
250 const struct wpa_freq_range_list *list)
251{
252 size_t o, c;
253
254 if (list == NULL)
255 return;
256
257 o = 0;
258 while (o < chan->reg_classes) {
259 struct p2p_reg_class *op = &chan->reg_class[o];
260
261 c = 0;
262 while (c < op->channels) {
263 int freq = p2p_channel_to_freq(op->reg_class,
264 op->channel[c]);
265 if (freq > 0 && freq_range_list_includes(list, freq)) {
266 op->channels--;
267 os_memmove(&op->channel[c],
268 &op->channel[c + 1],
269 op->channels - c);
270 } else
271 c++;
272 }
273
274 if (op->channels == 0) {
275 chan->reg_classes--;
276 os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1],
277 (chan->reg_classes - o) *
278 sizeof(struct p2p_reg_class));
279 } else
280 o++;
281 }
282}
283
284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285/**
286 * p2p_channels_includes - Check whether a channel is included in the list
287 * @channels: List of supported channels
288 * @reg_class: Regulatory class of the channel to search
289 * @channel: Channel number of the channel to search
290 * Returns: 1 if channel was found or 0 if not
291 */
292int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
293 u8 channel)
294{
295 size_t i, j;
296 for (i = 0; i < channels->reg_classes; i++) {
297 const struct p2p_reg_class *reg = &channels->reg_class[i];
298 if (reg->reg_class != reg_class)
299 continue;
300 for (j = 0; j < reg->channels; j++) {
301 if (reg->channel[j] == channel)
302 return 1;
303 }
304 }
305 return 0;
306}
307
308
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800309int p2p_channels_includes_freq(const struct p2p_channels *channels,
310 unsigned int freq)
311{
312 size_t i, j;
313 for (i = 0; i < channels->reg_classes; i++) {
314 const struct p2p_reg_class *reg = &channels->reg_class[i];
315 for (j = 0; j < reg->channels; j++) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700316 if (p2p_channel_to_freq(reg->reg_class,
317 reg->channel[j]) == (int) freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800318 return 1;
319 }
320 }
321 return 0;
322}
323
324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
326{
327 u8 op_reg_class, op_channel;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700328 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 return 0;
330 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
331 op_channel);
332}
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700333
334
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700335int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
336{
337 u8 op_reg_class, op_channel;
338 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
339 return 0;
340 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
341 op_channel) &&
342 !freq_range_list_includes(&p2p->no_go_freq, freq);
343}
344
345
346int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
347{
348 u8 op_reg_class, op_channel;
349 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
350 return 0;
351 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
352 op_channel) ||
353 p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
354 op_channel);
355}
356
357
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700358unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
359 const struct p2p_channels *channels)
360{
361 unsigned int i;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700362 int freq = 0;
363 const struct p2p_channels *tmpc = channels ?
364 channels : &p2p->cfg->channels;
365
366 if (tmpc == NULL)
367 return 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700368
369 for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
370 freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class,
371 p2p->cfg->pref_chan[i].chan);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700372 if (p2p_channels_includes_freq(tmpc, freq))
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700373 return freq;
374 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700375 return 0;
376}
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700377
378
379void p2p_channels_dump(struct p2p_data *p2p, const char *title,
380 const struct p2p_channels *chan)
381{
382 char buf[500], *pos, *end;
383 size_t i, j;
384 int ret;
385
386 pos = buf;
387 end = pos + sizeof(buf);
388
389 for (i = 0; i < chan->reg_classes; i++) {
390 const struct p2p_reg_class *c;
391 c = &chan->reg_class[i];
392 ret = os_snprintf(pos, end - pos, " %u:", c->reg_class);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800393 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700394 break;
395 pos += ret;
396
397 for (j = 0; j < c->channels; j++) {
398 ret = os_snprintf(pos, end - pos, "%s%u",
399 j == 0 ? "" : ",",
400 c->channel[j]);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800401 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700402 break;
403 pos += ret;
404 }
405 }
406 *pos = '\0';
407
408 p2p_dbg(p2p, "%s:%s", title, buf);
409}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800410
411
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700412static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
413{
414 unsigned int r;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700415 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
416 r = 0;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700417 r %= num_channels;
418 return channels[r];
419}
420
421
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800422int p2p_channel_select(struct p2p_channels *chans, const int *classes,
423 u8 *op_class, u8 *op_channel)
424{
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700425 unsigned int i, j;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800426
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700427 for (j = 0; classes == NULL || classes[j]; j++) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800428 for (i = 0; i < chans->reg_classes; i++) {
429 struct p2p_reg_class *c = &chans->reg_class[i];
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800430
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800431 if (c->channels == 0)
432 continue;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800433
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700434 if (classes == NULL || c->reg_class == classes[j]) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800435 /*
436 * Pick one of the available channels in the
437 * operating class at random.
438 */
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800439 *op_class = c->reg_class;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700440 *op_channel = p2p_channel_pick_random(
441 c->channel, c->channels);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800442 return 0;
443 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800444 }
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700445 if (classes == NULL)
446 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800447 }
448
449 return -1;
450}
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700451
452
453int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
454 u8 *op_channel)
455{
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700456 u8 chan[4];
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700457 unsigned int num_channels = 0;
458
459 /* Try to find available social channels from 2.4 GHz */
460 if (p2p_channels_includes(chans, 81, 1))
461 chan[num_channels++] = 1;
462 if (p2p_channels_includes(chans, 81, 6))
463 chan[num_channels++] = 6;
464 if (p2p_channels_includes(chans, 81, 11))
465 chan[num_channels++] = 11;
466
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700467 /* Try to find available social channels from 60 GHz */
468 if (p2p_channels_includes(chans, 180, 2))
469 chan[num_channels++] = 2;
470
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700471 if (num_channels == 0)
472 return -1;
473
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700474 *op_channel = p2p_channel_pick_random(chan, num_channels);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700475 if (*op_channel == 2)
476 *op_class = 180;
477 else
478 *op_class = 81;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700479
480 return 0;
481}
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800482
483
484int p2p_channels_to_freqs(const struct p2p_channels *channels, int *freq_list,
485 unsigned int max_len)
486{
487 unsigned int i, idx;
488
489 if (!channels || max_len == 0)
490 return 0;
491
492 for (i = 0, idx = 0; i < channels->reg_classes; i++) {
493 const struct p2p_reg_class *c = &channels->reg_class[i];
494 unsigned int j;
495
496 if (idx + 1 == max_len)
497 break;
498 for (j = 0; j < c->channels; j++) {
499 int freq;
500 if (idx + 1 == max_len)
501 break;
502 freq = p2p_channel_to_freq(c->reg_class,
503 c->channel[j]);
504 if (freq < 0)
505 continue;
506 freq_list[idx++] = freq;
507 }
508 }
509
510 freq_list[idx] = 0;
511
512 return idx;
513}