blob: eee3c5a6e16de8e33916af9c5fd636ef3ce1710b [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 Shmidta3dc3092015-06-23 11:21:28 -0700104 if (freq >= 5745 && freq <= 5845) {
105 if ((freq - 5000) % 5)
106 return -1;
107
108 *op_class = 125; /* 5 GHz, channels 149..169 */
109 *channel = (freq - 5000) / 5;
110 return 0;
111 }
112
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700113 if (freq >= 58320 && freq <= 64800) {
114 if ((freq - 58320) % 2160)
115 return -1;
116
117 *op_class = 180; /* 60 GHz, channels 1..4 */
118 *channel = (freq - 56160) / 2160;
119 return 0;
120 }
121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 return -1;
123}
124
125
126static void p2p_reg_class_intersect(const struct p2p_reg_class *a,
127 const struct p2p_reg_class *b,
128 struct p2p_reg_class *res)
129{
130 size_t i, j;
131
132 res->reg_class = a->reg_class;
133
134 for (i = 0; i < a->channels; i++) {
135 for (j = 0; j < b->channels; j++) {
136 if (a->channel[i] != b->channel[j])
137 continue;
138 res->channel[res->channels] = a->channel[i];
139 res->channels++;
140 if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
141 return;
142 }
143 }
144}
145
146
147/**
148 * p2p_channels_intersect - Intersection of supported channel lists
149 * @a: First set of supported channels
150 * @b: Second set of supported channels
151 * @res: Data structure for returning the intersection of support channels
152 *
153 * This function can be used to find a common set of supported channels. Both
154 * input channels sets are assumed to use the same country code. If different
155 * country codes are used, the regulatory class numbers may not be matched
156 * correctly and results are undefined.
157 */
158void p2p_channels_intersect(const struct p2p_channels *a,
159 const struct p2p_channels *b,
160 struct p2p_channels *res)
161{
162 size_t i, j;
163
164 os_memset(res, 0, sizeof(*res));
165
166 for (i = 0; i < a->reg_classes; i++) {
167 const struct p2p_reg_class *a_reg = &a->reg_class[i];
168 for (j = 0; j < b->reg_classes; j++) {
169 const struct p2p_reg_class *b_reg = &b->reg_class[j];
170 if (a_reg->reg_class != b_reg->reg_class)
171 continue;
172 p2p_reg_class_intersect(
173 a_reg, b_reg,
174 &res->reg_class[res->reg_classes]);
175 if (res->reg_class[res->reg_classes].channels) {
176 res->reg_classes++;
177 if (res->reg_classes == P2P_MAX_REG_CLASSES)
178 return;
179 }
180 }
181 }
182}
183
184
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700185static void p2p_op_class_union(struct p2p_reg_class *cl,
186 const struct p2p_reg_class *b_cl)
187{
188 size_t i, j;
189
190 for (i = 0; i < b_cl->channels; i++) {
191 for (j = 0; j < cl->channels; j++) {
192 if (b_cl->channel[i] == cl->channel[j])
193 break;
194 }
195 if (j == cl->channels) {
196 if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
197 return;
198 cl->channel[cl->channels++] = b_cl->channel[i];
199 }
200 }
201}
202
203
204/**
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800205 * p2p_channels_union_inplace - Inplace union of channel lists
206 * @res: Input data and place for returning union of the channel sets
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700207 * @b: Second set of channels
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700208 */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800209void p2p_channels_union_inplace(struct p2p_channels *res,
210 const struct p2p_channels *b)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700211{
212 size_t i, j;
213
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700214 for (i = 0; i < res->reg_classes; i++) {
215 struct p2p_reg_class *cl = &res->reg_class[i];
216 for (j = 0; j < b->reg_classes; j++) {
217 const struct p2p_reg_class *b_cl = &b->reg_class[j];
218 if (cl->reg_class != b_cl->reg_class)
219 continue;
220 p2p_op_class_union(cl, b_cl);
221 }
222 }
223
224 for (j = 0; j < b->reg_classes; j++) {
225 const struct p2p_reg_class *b_cl = &b->reg_class[j];
226
227 for (i = 0; i < res->reg_classes; i++) {
228 struct p2p_reg_class *cl = &res->reg_class[i];
229 if (cl->reg_class == b_cl->reg_class)
230 break;
231 }
232
233 if (i == res->reg_classes) {
234 if (res->reg_classes == P2P_MAX_REG_CLASSES)
235 return;
236 os_memcpy(&res->reg_class[res->reg_classes++],
237 b_cl, sizeof(struct p2p_reg_class));
238 }
239 }
240}
241
242
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800243/**
244 * p2p_channels_union - Union of channel lists
245 * @a: First set of channels
246 * @b: Second set of channels
247 * @res: Data structure for returning the union of channels
248 */
249void p2p_channels_union(const struct p2p_channels *a,
250 const struct p2p_channels *b,
251 struct p2p_channels *res)
252{
253 os_memcpy(res, a, sizeof(*res));
254 p2p_channels_union_inplace(res, b);
255}
256
257
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700258void p2p_channels_remove_freqs(struct p2p_channels *chan,
259 const struct wpa_freq_range_list *list)
260{
261 size_t o, c;
262
263 if (list == NULL)
264 return;
265
266 o = 0;
267 while (o < chan->reg_classes) {
268 struct p2p_reg_class *op = &chan->reg_class[o];
269
270 c = 0;
271 while (c < op->channels) {
272 int freq = p2p_channel_to_freq(op->reg_class,
273 op->channel[c]);
274 if (freq > 0 && freq_range_list_includes(list, freq)) {
275 op->channels--;
276 os_memmove(&op->channel[c],
277 &op->channel[c + 1],
278 op->channels - c);
279 } else
280 c++;
281 }
282
283 if (op->channels == 0) {
284 chan->reg_classes--;
285 os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1],
286 (chan->reg_classes - o) *
287 sizeof(struct p2p_reg_class));
288 } else
289 o++;
290 }
291}
292
293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294/**
295 * p2p_channels_includes - Check whether a channel is included in the list
296 * @channels: List of supported channels
297 * @reg_class: Regulatory class of the channel to search
298 * @channel: Channel number of the channel to search
299 * Returns: 1 if channel was found or 0 if not
300 */
301int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
302 u8 channel)
303{
304 size_t i, j;
305 for (i = 0; i < channels->reg_classes; i++) {
306 const struct p2p_reg_class *reg = &channels->reg_class[i];
307 if (reg->reg_class != reg_class)
308 continue;
309 for (j = 0; j < reg->channels; j++) {
310 if (reg->channel[j] == channel)
311 return 1;
312 }
313 }
314 return 0;
315}
316
317
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800318int p2p_channels_includes_freq(const struct p2p_channels *channels,
319 unsigned int freq)
320{
321 size_t i, j;
322 for (i = 0; i < channels->reg_classes; i++) {
323 const struct p2p_reg_class *reg = &channels->reg_class[i];
324 for (j = 0; j < reg->channels; j++) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700325 if (p2p_channel_to_freq(reg->reg_class,
326 reg->channel[j]) == (int) freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800327 return 1;
328 }
329 }
330 return 0;
331}
332
333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
335{
336 u8 op_reg_class, op_channel;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700337 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 return 0;
339 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
340 op_channel);
341}
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700342
343
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700344int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
345{
346 u8 op_reg_class, op_channel;
347 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
348 return 0;
349 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
350 op_channel) &&
351 !freq_range_list_includes(&p2p->no_go_freq, freq);
352}
353
354
355int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
356{
357 u8 op_reg_class, op_channel;
358 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
359 return 0;
360 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
361 op_channel) ||
362 p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
363 op_channel);
364}
365
366
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700367unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
368 const struct p2p_channels *channels)
369{
370 unsigned int i;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700371 int freq = 0;
372 const struct p2p_channels *tmpc = channels ?
373 channels : &p2p->cfg->channels;
374
375 if (tmpc == NULL)
376 return 0;
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700377
378 for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
379 freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class,
380 p2p->cfg->pref_chan[i].chan);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700381 if (p2p_channels_includes_freq(tmpc, freq))
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700382 return freq;
383 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700384 return 0;
385}
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700386
387
388void p2p_channels_dump(struct p2p_data *p2p, const char *title,
389 const struct p2p_channels *chan)
390{
391 char buf[500], *pos, *end;
392 size_t i, j;
393 int ret;
394
395 pos = buf;
396 end = pos + sizeof(buf);
397
398 for (i = 0; i < chan->reg_classes; i++) {
399 const struct p2p_reg_class *c;
400 c = &chan->reg_class[i];
401 ret = os_snprintf(pos, end - pos, " %u:", c->reg_class);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800402 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700403 break;
404 pos += ret;
405
406 for (j = 0; j < c->channels; j++) {
407 ret = os_snprintf(pos, end - pos, "%s%u",
408 j == 0 ? "" : ",",
409 c->channel[j]);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800410 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700411 break;
412 pos += ret;
413 }
414 }
415 *pos = '\0';
416
417 p2p_dbg(p2p, "%s:%s", title, buf);
418}
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800419
420
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700421static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
422{
423 unsigned int r;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700424 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
425 r = 0;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700426 r %= num_channels;
427 return channels[r];
428}
429
430
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800431int p2p_channel_select(struct p2p_channels *chans, const int *classes,
432 u8 *op_class, u8 *op_channel)
433{
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700434 unsigned int i, j;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800435
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700436 for (j = 0; classes == NULL || classes[j]; j++) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800437 for (i = 0; i < chans->reg_classes; i++) {
438 struct p2p_reg_class *c = &chans->reg_class[i];
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800439
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800440 if (c->channels == 0)
441 continue;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800442
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700443 if (classes == NULL || c->reg_class == classes[j]) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800444 /*
445 * Pick one of the available channels in the
446 * operating class at random.
447 */
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800448 *op_class = c->reg_class;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700449 *op_channel = p2p_channel_pick_random(
450 c->channel, c->channels);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800451 return 0;
452 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800453 }
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700454 if (classes == NULL)
455 break;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800456 }
457
458 return -1;
459}
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700460
461
462int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
463 u8 *op_channel)
464{
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700465 u8 chan[4];
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700466 unsigned int num_channels = 0;
467
468 /* Try to find available social channels from 2.4 GHz */
469 if (p2p_channels_includes(chans, 81, 1))
470 chan[num_channels++] = 1;
471 if (p2p_channels_includes(chans, 81, 6))
472 chan[num_channels++] = 6;
473 if (p2p_channels_includes(chans, 81, 11))
474 chan[num_channels++] = 11;
475
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700476 /* Try to find available social channels from 60 GHz */
477 if (p2p_channels_includes(chans, 180, 2))
478 chan[num_channels++] = 2;
479
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700480 if (num_channels == 0)
481 return -1;
482
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700483 *op_channel = p2p_channel_pick_random(chan, num_channels);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700484 if (*op_channel == 2)
485 *op_class = 180;
486 else
487 *op_class = 81;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700488
489 return 0;
490}
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800491
492
493int p2p_channels_to_freqs(const struct p2p_channels *channels, int *freq_list,
494 unsigned int max_len)
495{
496 unsigned int i, idx;
497
498 if (!channels || max_len == 0)
499 return 0;
500
501 for (i = 0, idx = 0; i < channels->reg_classes; i++) {
502 const struct p2p_reg_class *c = &channels->reg_class[i];
503 unsigned int j;
504
505 if (idx + 1 == max_len)
506 break;
507 for (j = 0; j < c->channels; j++) {
508 int freq;
509 if (idx + 1 == max_len)
510 break;
511 freq = p2p_channel_to_freq(c->reg_class,
512 c->channel[j]);
513 if (freq < 0)
514 continue;
515 freq_list[idx++] = freq;
516 }
517 }
518
519 freq_list[idx] = 0;
520
521 return idx;
522}