Line data Source code
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/ieee802_11_common.h"
13 : #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 : */
25 397 : int p2p_random(char *buf, size_t len)
26 : {
27 : u8 val;
28 : size_t i;
29 397 : u8 letters = 'Z' - 'A' + 1;
30 397 : u8 numbers = 10;
31 :
32 397 : if (os_get_random((unsigned char *) buf, len))
33 0 : return -1;
34 : /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */
35 2360 : for (i = 0; i < len; i++) {
36 1963 : val = buf[i];
37 1963 : val %= 2 * letters + numbers;
38 1963 : if (val < letters)
39 867 : buf[i] = 'A' + val;
40 1096 : else if (val < 2 * letters)
41 788 : buf[i] = 'a' + (val - letters);
42 : else
43 308 : buf[i] = '0' + (val - 2 * letters);
44 : }
45 :
46 397 : return 0;
47 : }
48 :
49 :
50 : /**
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 : */
56 9261 : int p2p_channel_to_freq(int op_class, int channel)
57 : {
58 9261 : return ieee80211_chan_to_freq(NULL, op_class, channel);
59 : }
60 :
61 :
62 : /**
63 : * p2p_freq_to_channel - Convert frequency into channel info
64 : * @op_class: Buffer for returning operating class
65 : * @channel: Buffer for returning channel number
66 : * Returns: 0 on success, -1 if the specified frequency is unknown
67 : */
68 1114 : int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel)
69 : {
70 : /* TODO: more operating classes */
71 1114 : if (freq >= 2412 && freq <= 2472) {
72 389 : if ((freq - 2407) % 5)
73 0 : return -1;
74 :
75 389 : *op_class = 81; /* 2.407 GHz, channels 1..13 */
76 389 : *channel = (freq - 2407) / 5;
77 389 : return 0;
78 : }
79 :
80 725 : if (freq == 2484) {
81 0 : *op_class = 82; /* channel 14 */
82 0 : *channel = 14;
83 0 : return 0;
84 : }
85 :
86 725 : if (freq >= 5180 && freq <= 5240) {
87 13 : if ((freq - 5000) % 5)
88 0 : return -1;
89 :
90 13 : *op_class = 115; /* 5 GHz, channels 36..48 */
91 13 : *channel = (freq - 5000) / 5;
92 13 : return 0;
93 : }
94 :
95 712 : if (freq >= 5745 && freq <= 5805) {
96 1 : if ((freq - 5000) % 5)
97 0 : return -1;
98 :
99 1 : *op_class = 124; /* 5 GHz, channels 149..161 */
100 1 : *channel = (freq - 5000) / 5;
101 1 : return 0;
102 : }
103 :
104 711 : if (freq >= 58320 && freq <= 64800) {
105 709 : if ((freq - 58320) % 2160)
106 0 : return -1;
107 :
108 709 : *op_class = 180; /* 60 GHz, channels 1..4 */
109 709 : *channel = (freq - 56160) / 2160;
110 709 : return 0;
111 : }
112 :
113 2 : return -1;
114 : }
115 :
116 :
117 1098 : static 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 1098 : res->reg_class = a->reg_class;
124 :
125 10241 : for (i = 0; i < a->channels; i++) {
126 96098 : for (j = 0; j < b->channels; j++) {
127 86955 : if (a->channel[i] != b->channel[j])
128 78642 : continue;
129 8313 : res->channel[res->channels] = a->channel[i];
130 8313 : res->channels++;
131 8313 : if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
132 1098 : 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 : */
149 967 : void 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 967 : os_memset(res, 0, sizeof(*res));
156 :
157 2215 : for (i = 0; i < a->reg_classes; i++) {
158 1248 : const struct p2p_reg_class *a_reg = &a->reg_class[i];
159 4096 : for (j = 0; j < b->reg_classes; j++) {
160 2848 : const struct p2p_reg_class *b_reg = &b->reg_class[j];
161 2848 : if (a_reg->reg_class != b_reg->reg_class)
162 1750 : continue;
163 1098 : p2p_reg_class_intersect(
164 : a_reg, b_reg,
165 1098 : &res->reg_class[res->reg_classes]);
166 1098 : if (res->reg_class[res->reg_classes].channels) {
167 1095 : res->reg_classes++;
168 1095 : if (res->reg_classes == P2P_MAX_REG_CLASSES)
169 967 : return;
170 : }
171 : }
172 : }
173 : }
174 :
175 :
176 10 : static 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 30 : for (i = 0; i < b_cl->channels; i++) {
182 250 : for (j = 0; j < cl->channels; j++) {
183 230 : if (b_cl->channel[i] == cl->channel[j])
184 0 : break;
185 : }
186 20 : if (j == cl->channels) {
187 20 : if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
188 10 : return;
189 20 : cl->channel[cl->channels++] = b_cl->channel[i];
190 : }
191 : }
192 : }
193 :
194 :
195 : /**
196 : * p2p_channels_union_inplace - Inplace union of channel lists
197 : * @res: Input data and place for returning union of the channel sets
198 : * @b: Second set of channels
199 : */
200 195 : void p2p_channels_union_inplace(struct p2p_channels *res,
201 : const struct p2p_channels *b)
202 : {
203 : size_t i, j;
204 :
205 439 : for (i = 0; i < res->reg_classes; i++) {
206 244 : struct p2p_reg_class *cl = &res->reg_class[i];
207 324 : for (j = 0; j < b->reg_classes; j++) {
208 80 : const struct p2p_reg_class *b_cl = &b->reg_class[j];
209 80 : if (cl->reg_class != b_cl->reg_class)
210 70 : continue;
211 10 : p2p_op_class_union(cl, b_cl);
212 : }
213 : }
214 :
215 275 : for (j = 0; j < b->reg_classes; j++) {
216 80 : const struct p2p_reg_class *b_cl = &b->reg_class[j];
217 :
218 360 : for (i = 0; i < res->reg_classes; i++) {
219 290 : struct p2p_reg_class *cl = &res->reg_class[i];
220 290 : if (cl->reg_class == b_cl->reg_class)
221 10 : break;
222 : }
223 :
224 80 : if (i == res->reg_classes) {
225 70 : if (res->reg_classes == P2P_MAX_REG_CLASSES)
226 195 : return;
227 70 : os_memcpy(&res->reg_class[res->reg_classes++],
228 : b_cl, sizeof(struct p2p_reg_class));
229 : }
230 : }
231 : }
232 :
233 :
234 : /**
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 : */
240 48 : void p2p_channels_union(const struct p2p_channels *a,
241 : const struct p2p_channels *b,
242 : struct p2p_channels *res)
243 : {
244 48 : os_memcpy(res, a, sizeof(*res));
245 48 : p2p_channels_union_inplace(res, b);
246 48 : }
247 :
248 :
249 327 : void p2p_channels_remove_freqs(struct p2p_channels *chan,
250 : const struct wpa_freq_range_list *list)
251 : {
252 : size_t o, c;
253 :
254 327 : if (list == NULL)
255 327 : return;
256 :
257 327 : o = 0;
258 1027 : while (o < chan->reg_classes) {
259 373 : struct p2p_reg_class *op = &chan->reg_class[o];
260 :
261 373 : c = 0;
262 3637 : while (c < op->channels) {
263 2891 : int freq = p2p_channel_to_freq(op->reg_class,
264 2891 : op->channel[c]);
265 2891 : if (freq > 0 && freq_range_list_includes(list, freq)) {
266 13 : op->channels--;
267 13 : os_memmove(&op->channel[c],
268 : &op->channel[c + 1],
269 : op->channels - c);
270 : } else
271 2878 : c++;
272 : }
273 :
274 373 : if (op->channels == 0) {
275 3 : chan->reg_classes--;
276 3 : 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 370 : o++;
281 : }
282 : }
283 :
284 :
285 : /**
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 : */
292 2216 : int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
293 : u8 channel)
294 : {
295 : size_t i, j;
296 3234 : for (i = 0; i < channels->reg_classes; i++) {
297 2332 : const struct p2p_reg_class *reg = &channels->reg_class[i];
298 2332 : if (reg->reg_class != reg_class)
299 993 : continue;
300 7350 : for (j = 0; j < reg->channels; j++) {
301 7325 : if (reg->channel[j] == channel)
302 1314 : return 1;
303 : }
304 : }
305 902 : return 0;
306 : }
307 :
308 :
309 265 : int p2p_channels_includes_freq(const struct p2p_channels *channels,
310 : unsigned int freq)
311 : {
312 : size_t i, j;
313 311 : for (i = 0; i < channels->reg_classes; i++) {
314 280 : const struct p2p_reg_class *reg = &channels->reg_class[i];
315 1737 : for (j = 0; j < reg->channels; j++) {
316 3382 : if (p2p_channel_to_freq(reg->reg_class,
317 3382 : reg->channel[j]) == (int) freq)
318 234 : return 1;
319 : }
320 : }
321 31 : return 0;
322 : }
323 :
324 :
325 843 : int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
326 : {
327 : u8 op_reg_class, op_channel;
328 843 : if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
329 1 : return 0;
330 842 : return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
331 : op_channel);
332 : }
333 :
334 :
335 173 : int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
336 : {
337 : u8 op_reg_class, op_channel;
338 173 : if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
339 0 : return 0;
340 346 : return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
341 346 : op_channel) &&
342 173 : !freq_range_list_includes(&p2p->no_go_freq, freq);
343 : }
344 :
345 :
346 30 : int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
347 : {
348 : u8 op_reg_class, op_channel;
349 30 : if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
350 1 : return 0;
351 58 : return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
352 30 : op_channel) ||
353 1 : p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
354 : op_channel);
355 : }
356 :
357 :
358 48 : unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
359 : const struct p2p_channels *channels)
360 : {
361 : unsigned int i;
362 48 : int freq = 0;
363 48 : const struct p2p_channels *tmpc = channels ?
364 48 : channels : &p2p->cfg->channels;
365 :
366 48 : if (tmpc == NULL)
367 0 : return 0;
368 :
369 74 : for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
370 29 : freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class,
371 29 : p2p->cfg->pref_chan[i].chan);
372 29 : if (p2p_channels_includes_freq(tmpc, freq))
373 3 : return freq;
374 : }
375 45 : return 0;
376 : }
377 :
378 :
379 15982 : void 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 15982 : pos = buf;
387 15982 : end = pos + sizeof(buf);
388 :
389 27237 : for (i = 0; i < chan->reg_classes; i++) {
390 : const struct p2p_reg_class *c;
391 11255 : c = &chan->reg_class[i];
392 11255 : ret = os_snprintf(pos, end - pos, " %u:", c->reg_class);
393 11255 : if (os_snprintf_error(end - pos, ret))
394 0 : break;
395 11255 : pos += ret;
396 :
397 112538 : for (j = 0; j < c->channels; j++) {
398 101283 : ret = os_snprintf(pos, end - pos, "%s%u",
399 : j == 0 ? "" : ",",
400 101283 : c->channel[j]);
401 101283 : if (os_snprintf_error(end - pos, ret))
402 0 : break;
403 101283 : pos += ret;
404 : }
405 : }
406 15982 : *pos = '\0';
407 :
408 15982 : p2p_dbg(p2p, "%s:%s", title, buf);
409 15982 : }
410 :
411 :
412 178 : static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
413 : {
414 : unsigned int r;
415 178 : if (os_get_random((u8 *) &r, sizeof(r)) < 0)
416 0 : r = 0;
417 178 : r %= num_channels;
418 178 : return channels[r];
419 : }
420 :
421 :
422 923 : int p2p_channel_select(struct p2p_channels *chans, const int *classes,
423 : u8 *op_class, u8 *op_channel)
424 : {
425 : unsigned int i, j;
426 :
427 3039 : for (j = 0; classes == NULL || classes[j]; j++) {
428 4334 : for (i = 0; i < chans->reg_classes; i++) {
429 2218 : struct p2p_reg_class *c = &chans->reg_class[i];
430 :
431 2218 : if (c->channels == 0)
432 0 : continue;
433 :
434 2218 : if (classes == NULL || c->reg_class == classes[j]) {
435 : /*
436 : * Pick one of the available channels in the
437 : * operating class at random.
438 : */
439 15 : *op_class = c->reg_class;
440 30 : *op_channel = p2p_channel_pick_random(
441 30 : c->channel, c->channels);
442 15 : return 0;
443 : }
444 : }
445 2116 : if (classes == NULL)
446 0 : break;
447 : }
448 :
449 908 : return -1;
450 : }
451 :
452 :
453 165 : int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
454 : u8 *op_channel)
455 : {
456 : u8 chan[4];
457 165 : unsigned int num_channels = 0;
458 :
459 : /* Try to find available social channels from 2.4 GHz */
460 165 : if (p2p_channels_includes(chans, 81, 1))
461 163 : chan[num_channels++] = 1;
462 165 : if (p2p_channels_includes(chans, 81, 6))
463 163 : chan[num_channels++] = 6;
464 165 : if (p2p_channels_includes(chans, 81, 11))
465 163 : chan[num_channels++] = 11;
466 :
467 : /* Try to find available social channels from 60 GHz */
468 165 : if (p2p_channels_includes(chans, 180, 2))
469 0 : chan[num_channels++] = 2;
470 :
471 165 : if (num_channels == 0)
472 2 : return -1;
473 :
474 163 : *op_channel = p2p_channel_pick_random(chan, num_channels);
475 163 : if (*op_channel == 2)
476 0 : *op_class = 180;
477 : else
478 163 : *op_class = 81;
479 :
480 163 : return 0;
481 : }
482 :
483 :
484 231 : int 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 231 : if (!channels || max_len == 0)
490 18 : return 0;
491 :
492 474 : for (i = 0, idx = 0; i < channels->reg_classes; i++) {
493 261 : const struct p2p_reg_class *c = &channels->reg_class[i];
494 : unsigned int j;
495 :
496 261 : if (idx + 1 == max_len)
497 0 : break;
498 2276 : for (j = 0; j < c->channels; j++) {
499 : int freq;
500 2015 : if (idx + 1 == max_len)
501 0 : break;
502 2015 : freq = p2p_channel_to_freq(c->reg_class,
503 2015 : c->channel[j]);
504 2015 : if (freq < 0)
505 0 : continue;
506 2015 : freq_list[idx++] = freq;
507 : }
508 : }
509 :
510 213 : freq_list[idx] = 0;
511 :
512 213 : return idx;
513 : }
|