2 * P2P - generic helper functions
3 * Copyright (c) 2009, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
16 * p2p_random - Generate random string for SSID and passphrase
17 * @buf: Buffer for returning the result
18 * @len: Number of octets to write to the buffer
19 * Returns: 0 on success, -1 on failure
21 * This function generates a random string using the following character set:
22 * 'A'-'Z', 'a'-'z', '0'-'9'.
24 int p2p_random(char *buf, size_t len)
28 u8 letters = 'Z' - 'A' + 1;
31 if (os_get_random((unsigned char *) buf, len))
33 /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */
34 for (i = 0; i < len; i++) {
36 val %= 2 * letters + numbers;
39 else if (val < 2 * letters)
40 buf[i] = 'a' + (val - letters);
42 buf[i] = '0' + (val - 2 * letters);
50 * p2p_channel_to_freq - Convert channel info to frequency
51 * @op_class: Operating class
52 * @channel: Channel number
53 * Returns: Frequency in MHz or -1 if the specified channel is unknown
55 int p2p_channel_to_freq(int op_class, int channel)
57 /* Table E-4 in IEEE Std 802.11-2012 - Global operating classes */
58 /* TODO: more operating classes */
62 if (channel < 1 || channel > 13)
64 return 2407 + 5 * channel;
69 return 2414 + 5 * channel;
70 case 83: /* channels 1..9; 40 MHz */
71 case 84: /* channels 5..13; 40 MHz */
72 if (channel < 1 || channel > 13)
74 return 2407 + 5 * channel;
75 case 115: /* channels 36,40,44,48; indoor only */
76 case 118: /* channels 52,56,60,64; dfs */
77 if (channel < 36 || channel > 64)
79 return 5000 + 5 * channel;
80 case 124: /* channels 149,153,157,161 */
81 case 125: /* channels 149,153,157,161,165,169 */
82 if (channel < 149 || channel > 161)
84 return 5000 + 5 * channel;
85 case 116: /* channels 36,44; 40 MHz; indoor only */
86 case 117: /* channels 40,48; 40 MHz; indoor only */
87 case 119: /* channels 52,60; 40 MHz; dfs */
88 case 120: /* channels 56,64; 40 MHz; dfs */
89 if (channel < 36 || channel > 64)
91 return 5000 + 5 * channel;
92 case 126: /* channels 149,157; 40 MHz */
93 case 127: /* channels 153,161; 40 MHz */
94 if (channel < 149 || channel > 161)
96 return 5000 + 5 * channel;
97 case 128: /* center freqs 42, 58, 106, 122, 138, 155; 80 MHz */
98 if (channel < 36 || channel > 161)
100 return 5000 + 5 * channel;
101 case 180: /* 60 GHz band, channels 1..4 */
102 if (channel < 1 || channel > 4)
104 return 56160 + 2160 * channel;
111 * p2p_freq_to_channel - Convert frequency into channel info
112 * @op_class: Buffer for returning operating class
113 * @channel: Buffer for returning channel number
114 * Returns: 0 on success, -1 if the specified frequency is unknown
116 int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel)
118 /* TODO: more operating classes */
119 if (freq >= 2412 && freq <= 2472) {
120 if ((freq - 2407) % 5)
123 *op_class = 81; /* 2.407 GHz, channels 1..13 */
124 *channel = (freq - 2407) / 5;
129 *op_class = 82; /* channel 14 */
134 if (freq >= 5180 && freq <= 5240) {
135 if ((freq - 5000) % 5)
138 *op_class = 115; /* 5 GHz, channels 36..48 */
139 *channel = (freq - 5000) / 5;
143 if (freq >= 5745 && freq <= 5805) {
144 if ((freq - 5000) % 5)
147 *op_class = 124; /* 5 GHz, channels 149..161 */
148 *channel = (freq - 5000) / 5;
156 static void p2p_reg_class_intersect(const struct p2p_reg_class *a,
157 const struct p2p_reg_class *b,
158 struct p2p_reg_class *res)
162 res->reg_class = a->reg_class;
164 for (i = 0; i < a->channels; i++) {
165 for (j = 0; j < b->channels; j++) {
166 if (a->channel[i] != b->channel[j])
168 res->channel[res->channels] = a->channel[i];
170 if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
178 * p2p_channels_intersect - Intersection of supported channel lists
179 * @a: First set of supported channels
180 * @b: Second set of supported channels
181 * @res: Data structure for returning the intersection of support channels
183 * This function can be used to find a common set of supported channels. Both
184 * input channels sets are assumed to use the same country code. If different
185 * country codes are used, the regulatory class numbers may not be matched
186 * correctly and results are undefined.
188 void p2p_channels_intersect(const struct p2p_channels *a,
189 const struct p2p_channels *b,
190 struct p2p_channels *res)
194 os_memset(res, 0, sizeof(*res));
196 for (i = 0; i < a->reg_classes; i++) {
197 const struct p2p_reg_class *a_reg = &a->reg_class[i];
198 for (j = 0; j < b->reg_classes; j++) {
199 const struct p2p_reg_class *b_reg = &b->reg_class[j];
200 if (a_reg->reg_class != b_reg->reg_class)
202 p2p_reg_class_intersect(
204 &res->reg_class[res->reg_classes]);
205 if (res->reg_class[res->reg_classes].channels) {
207 if (res->reg_classes == P2P_MAX_REG_CLASSES)
215 static void p2p_op_class_union(struct p2p_reg_class *cl,
216 const struct p2p_reg_class *b_cl)
220 for (i = 0; i < b_cl->channels; i++) {
221 for (j = 0; j < cl->channels; j++) {
222 if (b_cl->channel[i] == cl->channel[j])
225 if (j == cl->channels) {
226 if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
228 cl->channel[cl->channels++] = b_cl->channel[i];
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
240 void p2p_channels_union(const struct p2p_channels *a,
241 const struct p2p_channels *b,
242 struct p2p_channels *res)
247 os_memcpy(res, a, sizeof(*res));
249 for (i = 0; i < res->reg_classes; i++) {
250 struct p2p_reg_class *cl = &res->reg_class[i];
251 for (j = 0; j < b->reg_classes; j++) {
252 const struct p2p_reg_class *b_cl = &b->reg_class[j];
253 if (cl->reg_class != b_cl->reg_class)
255 p2p_op_class_union(cl, b_cl);
259 for (j = 0; j < b->reg_classes; j++) {
260 const struct p2p_reg_class *b_cl = &b->reg_class[j];
262 for (i = 0; i < res->reg_classes; i++) {
263 struct p2p_reg_class *cl = &res->reg_class[i];
264 if (cl->reg_class == b_cl->reg_class)
268 if (i == res->reg_classes) {
269 if (res->reg_classes == P2P_MAX_REG_CLASSES)
271 os_memcpy(&res->reg_class[res->reg_classes++],
272 b_cl, sizeof(struct p2p_reg_class));
278 void p2p_channels_remove_freqs(struct p2p_channels *chan,
279 const struct wpa_freq_range_list *list)
287 while (o < chan->reg_classes) {
288 struct p2p_reg_class *op = &chan->reg_class[o];
291 while (c < op->channels) {
292 int freq = p2p_channel_to_freq(op->reg_class,
294 if (freq > 0 && freq_range_list_includes(list, freq)) {
296 os_memmove(&op->channel[c],
303 if (op->channels == 0) {
305 os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1],
306 (chan->reg_classes - o) *
307 sizeof(struct p2p_reg_class));
315 * p2p_channels_includes - Check whether a channel is included in the list
316 * @channels: List of supported channels
317 * @reg_class: Regulatory class of the channel to search
318 * @channel: Channel number of the channel to search
319 * Returns: 1 if channel was found or 0 if not
321 int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
325 for (i = 0; i < channels->reg_classes; i++) {
326 const struct p2p_reg_class *reg = &channels->reg_class[i];
327 if (reg->reg_class != reg_class)
329 for (j = 0; j < reg->channels; j++) {
330 if (reg->channel[j] == channel)
338 int p2p_channels_includes_freq(const struct p2p_channels *channels,
342 for (i = 0; i < channels->reg_classes; i++) {
343 const struct p2p_reg_class *reg = &channels->reg_class[i];
344 for (j = 0; j < reg->channels; j++) {
345 if (p2p_channel_to_freq(reg->reg_class,
346 reg->channel[j]) == (int) freq)
354 int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
356 u8 op_reg_class, op_channel;
357 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
359 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
364 int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
366 u8 op_reg_class, op_channel;
367 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
369 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
371 !freq_range_list_includes(&p2p->no_go_freq, freq);
375 int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
377 u8 op_reg_class, op_channel;
378 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
380 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
382 p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
387 unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
388 const struct p2p_channels *channels)
392 const struct p2p_channels *tmpc = channels ?
393 channels : &p2p->cfg->channels;
398 for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
399 freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class,
400 p2p->cfg->pref_chan[i].chan);
401 if (p2p_channels_includes_freq(tmpc, freq))
408 void p2p_channels_dump(struct p2p_data *p2p, const char *title,
409 const struct p2p_channels *chan)
411 char buf[500], *pos, *end;
416 end = pos + sizeof(buf);
418 for (i = 0; i < chan->reg_classes; i++) {
419 const struct p2p_reg_class *c;
420 c = &chan->reg_class[i];
421 ret = os_snprintf(pos, end - pos, " %u:", c->reg_class);
422 if (ret < 0 || ret >= end - pos)
426 for (j = 0; j < c->channels; j++) {
427 ret = os_snprintf(pos, end - pos, "%s%u",
430 if (ret < 0 || ret >= end - pos)
437 p2p_dbg(p2p, "%s:%s", title, buf);
441 static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
444 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
451 int p2p_channel_select(struct p2p_channels *chans, const int *classes,
452 u8 *op_class, u8 *op_channel)
456 for (j = 0; classes == NULL || classes[j]; j++) {
457 for (i = 0; i < chans->reg_classes; i++) {
458 struct p2p_reg_class *c = &chans->reg_class[i];
460 if (c->channels == 0)
463 if (classes == NULL || c->reg_class == classes[j]) {
465 * Pick one of the available channels in the
466 * operating class at random.
468 *op_class = c->reg_class;
469 *op_channel = p2p_channel_pick_random(
470 c->channel, c->channels);
482 int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
486 unsigned int num_channels = 0;
488 /* Try to find available social channels from 2.4 GHz */
489 if (p2p_channels_includes(chans, 81, 1))
490 chan[num_channels++] = 1;
491 if (p2p_channels_includes(chans, 81, 6))
492 chan[num_channels++] = 6;
493 if (p2p_channels_includes(chans, 81, 11))
494 chan[num_channels++] = 11;
496 if (num_channels == 0)
500 *op_channel = p2p_channel_pick_random(chan, num_channels);