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.
12 #include "common/ieee802_11_common.h"
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
22 * This function generates a random string using the following character set:
23 * 'A'-'Z', 'a'-'z', '0'-'9'.
25 int p2p_random(char *buf, size_t len)
29 u8 letters = 'Z' - 'A' + 1;
32 if (os_get_random((unsigned char *) buf, len))
34 /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */
35 for (i = 0; i < len; i++) {
37 val %= 2 * letters + numbers;
40 else if (val < 2 * letters)
41 buf[i] = 'a' + (val - letters);
43 buf[i] = '0' + (val - 2 * letters);
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
56 int p2p_channel_to_freq(int op_class, int channel)
58 return ieee80211_chan_to_freq(NULL, op_class, channel);
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
68 int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel)
70 /* TODO: more operating classes */
71 if (freq >= 2412 && freq <= 2472) {
72 if ((freq - 2407) % 5)
75 *op_class = 81; /* 2.407 GHz, channels 1..13 */
76 *channel = (freq - 2407) / 5;
81 *op_class = 82; /* channel 14 */
86 if (freq >= 5180 && freq <= 5240) {
87 if ((freq - 5000) % 5)
90 *op_class = 115; /* 5 GHz, channels 36..48 */
91 *channel = (freq - 5000) / 5;
95 if (freq >= 5745 && freq <= 5805) {
96 if ((freq - 5000) % 5)
99 *op_class = 124; /* 5 GHz, channels 149..161 */
100 *channel = (freq - 5000) / 5;
104 if (freq >= 58320 && freq <= 64800) {
105 if ((freq - 58320) % 2160)
108 *op_class = 180; /* 60 GHz, channels 1..4 */
109 *channel = (freq - 56160) / 2160;
117 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)
123 res->reg_class = a->reg_class;
125 for (i = 0; i < a->channels; i++) {
126 for (j = 0; j < b->channels; j++) {
127 if (a->channel[i] != b->channel[j])
129 res->channel[res->channels] = a->channel[i];
131 if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
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
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.
149 void p2p_channels_intersect(const struct p2p_channels *a,
150 const struct p2p_channels *b,
151 struct p2p_channels *res)
155 os_memset(res, 0, sizeof(*res));
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)
163 p2p_reg_class_intersect(
165 &res->reg_class[res->reg_classes]);
166 if (res->reg_class[res->reg_classes].channels) {
168 if (res->reg_classes == P2P_MAX_REG_CLASSES)
176 static void p2p_op_class_union(struct p2p_reg_class *cl,
177 const struct p2p_reg_class *b_cl)
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])
186 if (j == cl->channels) {
187 if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
189 cl->channel[cl->channels++] = b_cl->channel[i];
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
200 void p2p_channels_union_inplace(struct p2p_channels *res,
201 const struct p2p_channels *b)
205 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)
211 p2p_op_class_union(cl, b_cl);
215 for (j = 0; j < b->reg_classes; j++) {
216 const struct p2p_reg_class *b_cl = &b->reg_class[j];
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)
224 if (i == res->reg_classes) {
225 if (res->reg_classes == P2P_MAX_REG_CLASSES)
227 os_memcpy(&res->reg_class[res->reg_classes++],
228 b_cl, sizeof(struct p2p_reg_class));
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)
244 os_memcpy(res, a, sizeof(*res));
245 p2p_channels_union_inplace(res, b);
249 void p2p_channels_remove_freqs(struct p2p_channels *chan,
250 const struct wpa_freq_range_list *list)
258 while (o < chan->reg_classes) {
259 struct p2p_reg_class *op = &chan->reg_class[o];
262 while (c < op->channels) {
263 int freq = p2p_channel_to_freq(op->reg_class,
265 if (freq > 0 && freq_range_list_includes(list, freq)) {
267 os_memmove(&op->channel[c],
274 if (op->channels == 0) {
276 os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1],
277 (chan->reg_classes - o) *
278 sizeof(struct p2p_reg_class));
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
292 int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
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)
300 for (j = 0; j < reg->channels; j++) {
301 if (reg->channel[j] == channel)
309 int p2p_channels_includes_freq(const struct p2p_channels *channels,
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++) {
316 if (p2p_channel_to_freq(reg->reg_class,
317 reg->channel[j]) == (int) freq)
325 int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
327 u8 op_reg_class, op_channel;
328 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
330 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
335 int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
337 u8 op_reg_class, op_channel;
338 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
340 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
342 !freq_range_list_includes(&p2p->no_go_freq, freq);
346 int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
348 u8 op_reg_class, op_channel;
349 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
351 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
353 p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
358 unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
359 const struct p2p_channels *channels)
363 const struct p2p_channels *tmpc = channels ?
364 channels : &p2p->cfg->channels;
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);
372 if (p2p_channels_includes_freq(tmpc, freq))
379 void p2p_channels_dump(struct p2p_data *p2p, const char *title,
380 const struct p2p_channels *chan)
382 char buf[500], *pos, *end;
387 end = pos + sizeof(buf);
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);
393 if (os_snprintf_error(end - pos, ret))
397 for (j = 0; j < c->channels; j++) {
398 ret = os_snprintf(pos, end - pos, "%s%u",
401 if (os_snprintf_error(end - pos, ret))
408 p2p_dbg(p2p, "%s:%s", title, buf);
412 static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
415 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
422 int p2p_channel_select(struct p2p_channels *chans, const int *classes,
423 u8 *op_class, u8 *op_channel)
427 for (j = 0; classes == NULL || classes[j]; j++) {
428 for (i = 0; i < chans->reg_classes; i++) {
429 struct p2p_reg_class *c = &chans->reg_class[i];
431 if (c->channels == 0)
434 if (classes == NULL || c->reg_class == classes[j]) {
436 * Pick one of the available channels in the
437 * operating class at random.
439 *op_class = c->reg_class;
440 *op_channel = p2p_channel_pick_random(
441 c->channel, c->channels);
453 int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
457 unsigned int num_channels = 0;
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;
467 /* Try to find available social channels from 60 GHz */
468 if (p2p_channels_includes(chans, 180, 2))
469 chan[num_channels++] = 2;
471 if (num_channels == 0)
474 *op_channel = p2p_channel_pick_random(chan, num_channels);
475 if (*op_channel == 2)
484 int p2p_channels_to_freqs(const struct p2p_channels *channels, int *freq_list,
485 unsigned int max_len)
489 if (!channels || max_len == 0)
492 for (i = 0, idx = 0; i < channels->reg_classes; i++) {
493 const struct p2p_reg_class *c = &channels->reg_class[i];
496 if (idx + 1 == max_len)
498 for (j = 0; j < c->channels; j++) {
500 if (idx + 1 == max_len)
502 freq = p2p_channel_to_freq(c->reg_class,
506 freq_list[idx++] = freq;