2010-07-18 23:30:25 +02:00
|
|
|
/*
|
|
|
|
* P2P - generic helper functions
|
|
|
|
* Copyright (c) 2009, Atheros Communications
|
|
|
|
*
|
2012-02-11 15:31:36 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2010-07-18 23:30:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
2015-07-27 21:24:18 +02:00
|
|
|
#include "common/defs.h"
|
2014-11-22 18:48:08 +01:00
|
|
|
#include "common/ieee802_11_common.h"
|
2010-07-18 23:30:25 +02:00
|
|
|
#include "p2p_i.h"
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* p2p_random - Generate random string for SSID and passphrase
|
|
|
|
* @buf: Buffer for returning the result
|
|
|
|
* @len: Number of octets to write to the buffer
|
|
|
|
* Returns: 0 on success, -1 on failure
|
|
|
|
*
|
|
|
|
* This function generates a random string using the following character set:
|
|
|
|
* 'A'-'Z', 'a'-'z', '0'-'9'.
|
|
|
|
*/
|
|
|
|
int p2p_random(char *buf, size_t len)
|
|
|
|
{
|
|
|
|
u8 val;
|
|
|
|
size_t i;
|
|
|
|
u8 letters = 'Z' - 'A' + 1;
|
|
|
|
u8 numbers = 10;
|
|
|
|
|
|
|
|
if (os_get_random((unsigned char *) buf, len))
|
|
|
|
return -1;
|
|
|
|
/* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
val = buf[i];
|
|
|
|
val %= 2 * letters + numbers;
|
|
|
|
if (val < letters)
|
|
|
|
buf[i] = 'A' + val;
|
|
|
|
else if (val < 2 * letters)
|
|
|
|
buf[i] = 'a' + (val - letters);
|
|
|
|
else
|
|
|
|
buf[i] = '0' + (val - 2 * letters);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-27 20:03:16 +02:00
|
|
|
/**
|
|
|
|
* p2p_channel_to_freq - Convert channel info to frequency
|
|
|
|
* @op_class: Operating class
|
|
|
|
* @channel: Channel number
|
|
|
|
* Returns: Frequency in MHz or -1 if the specified channel is unknown
|
|
|
|
*/
|
|
|
|
int p2p_channel_to_freq(int op_class, int channel)
|
2010-07-18 23:30:25 +02:00
|
|
|
{
|
2014-11-22 18:48:08 +01:00
|
|
|
return ieee80211_chan_to_freq(NULL, op_class, channel);
|
2010-07-18 23:30:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* p2p_freq_to_channel - Convert frequency into channel info
|
2013-04-27 20:03:16 +02:00
|
|
|
* @op_class: Buffer for returning operating class
|
2010-07-18 23:30:25 +02:00
|
|
|
* @channel: Buffer for returning channel number
|
|
|
|
* Returns: 0 on success, -1 if the specified frequency is unknown
|
|
|
|
*/
|
2013-04-27 20:03:16 +02:00
|
|
|
int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel)
|
2010-07-18 23:30:25 +02:00
|
|
|
{
|
2015-07-27 21:24:18 +02:00
|
|
|
if (ieee80211_freq_to_channel_ext(freq, 0, 0, op_class, channel) ==
|
|
|
|
NUM_HOSTAPD_MODES)
|
|
|
|
return -1;
|
2014-09-25 11:42:07 +02:00
|
|
|
|
2015-07-27 21:24:18 +02:00
|
|
|
return 0;
|
2010-07-18 23:30:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void p2p_reg_class_intersect(const struct p2p_reg_class *a,
|
|
|
|
const struct p2p_reg_class *b,
|
|
|
|
struct p2p_reg_class *res)
|
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
res->reg_class = a->reg_class;
|
|
|
|
|
|
|
|
for (i = 0; i < a->channels; i++) {
|
|
|
|
for (j = 0; j < b->channels; j++) {
|
|
|
|
if (a->channel[i] != b->channel[j])
|
|
|
|
continue;
|
|
|
|
res->channel[res->channels] = a->channel[i];
|
|
|
|
res->channels++;
|
|
|
|
if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* p2p_channels_intersect - Intersection of supported channel lists
|
|
|
|
* @a: First set of supported channels
|
|
|
|
* @b: Second set of supported channels
|
|
|
|
* @res: Data structure for returning the intersection of support channels
|
|
|
|
*
|
|
|
|
* This function can be used to find a common set of supported channels. Both
|
|
|
|
* input channels sets are assumed to use the same country code. If different
|
|
|
|
* country codes are used, the regulatory class numbers may not be matched
|
|
|
|
* correctly and results are undefined.
|
|
|
|
*/
|
|
|
|
void p2p_channels_intersect(const struct p2p_channels *a,
|
|
|
|
const struct p2p_channels *b,
|
|
|
|
struct p2p_channels *res)
|
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
os_memset(res, 0, sizeof(*res));
|
|
|
|
|
|
|
|
for (i = 0; i < a->reg_classes; i++) {
|
|
|
|
const struct p2p_reg_class *a_reg = &a->reg_class[i];
|
|
|
|
for (j = 0; j < b->reg_classes; j++) {
|
|
|
|
const struct p2p_reg_class *b_reg = &b->reg_class[j];
|
|
|
|
if (a_reg->reg_class != b_reg->reg_class)
|
|
|
|
continue;
|
|
|
|
p2p_reg_class_intersect(
|
|
|
|
a_reg, b_reg,
|
|
|
|
&res->reg_class[res->reg_classes]);
|
|
|
|
if (res->reg_class[res->reg_classes].channels) {
|
|
|
|
res->reg_classes++;
|
|
|
|
if (res->reg_classes == P2P_MAX_REG_CLASSES)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-22 20:00:49 +02:00
|
|
|
static void p2p_op_class_union(struct p2p_reg_class *cl,
|
|
|
|
const struct p2p_reg_class *b_cl)
|
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < b_cl->channels; i++) {
|
|
|
|
for (j = 0; j < cl->channels; j++) {
|
|
|
|
if (b_cl->channel[i] == cl->channel[j])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (j == cl->channels) {
|
|
|
|
if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
|
|
|
|
return;
|
|
|
|
cl->channel[cl->channels++] = b_cl->channel[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-12-05 21:34:27 +01:00
|
|
|
* p2p_channels_union_inplace - Inplace union of channel lists
|
|
|
|
* @res: Input data and place for returning union of the channel sets
|
2013-10-22 20:00:49 +02:00
|
|
|
* @b: Second set of channels
|
|
|
|
*/
|
2014-12-05 21:34:27 +01:00
|
|
|
void p2p_channels_union_inplace(struct p2p_channels *res,
|
|
|
|
const struct p2p_channels *b)
|
2013-10-22 20:00:49 +02:00
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < res->reg_classes; i++) {
|
|
|
|
struct p2p_reg_class *cl = &res->reg_class[i];
|
|
|
|
for (j = 0; j < b->reg_classes; j++) {
|
|
|
|
const struct p2p_reg_class *b_cl = &b->reg_class[j];
|
|
|
|
if (cl->reg_class != b_cl->reg_class)
|
|
|
|
continue;
|
|
|
|
p2p_op_class_union(cl, b_cl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < b->reg_classes; j++) {
|
|
|
|
const struct p2p_reg_class *b_cl = &b->reg_class[j];
|
|
|
|
|
|
|
|
for (i = 0; i < res->reg_classes; i++) {
|
|
|
|
struct p2p_reg_class *cl = &res->reg_class[i];
|
|
|
|
if (cl->reg_class == b_cl->reg_class)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == res->reg_classes) {
|
|
|
|
if (res->reg_classes == P2P_MAX_REG_CLASSES)
|
|
|
|
return;
|
|
|
|
os_memcpy(&res->reg_class[res->reg_classes++],
|
|
|
|
b_cl, sizeof(struct p2p_reg_class));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-05 21:34:27 +01:00
|
|
|
/**
|
|
|
|
* p2p_channels_union - Union of channel lists
|
|
|
|
* @a: First set of channels
|
|
|
|
* @b: Second set of channels
|
|
|
|
* @res: Data structure for returning the union of channels
|
|
|
|
*/
|
|
|
|
void p2p_channels_union(const struct p2p_channels *a,
|
|
|
|
const struct p2p_channels *b,
|
|
|
|
struct p2p_channels *res)
|
|
|
|
{
|
|
|
|
os_memcpy(res, a, sizeof(*res));
|
|
|
|
p2p_channels_union_inplace(res, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-22 18:45:46 +02:00
|
|
|
void p2p_channels_remove_freqs(struct p2p_channels *chan,
|
|
|
|
const struct wpa_freq_range_list *list)
|
|
|
|
{
|
|
|
|
size_t o, c;
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
o = 0;
|
|
|
|
while (o < chan->reg_classes) {
|
|
|
|
struct p2p_reg_class *op = &chan->reg_class[o];
|
|
|
|
|
|
|
|
c = 0;
|
|
|
|
while (c < op->channels) {
|
|
|
|
int freq = p2p_channel_to_freq(op->reg_class,
|
|
|
|
op->channel[c]);
|
|
|
|
if (freq > 0 && freq_range_list_includes(list, freq)) {
|
|
|
|
op->channels--;
|
|
|
|
os_memmove(&op->channel[c],
|
|
|
|
&op->channel[c + 1],
|
|
|
|
op->channels - c);
|
|
|
|
} else
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op->channels == 0) {
|
|
|
|
chan->reg_classes--;
|
|
|
|
os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1],
|
|
|
|
(chan->reg_classes - o) *
|
|
|
|
sizeof(struct p2p_reg_class));
|
|
|
|
} else
|
|
|
|
o++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-18 23:30:25 +02:00
|
|
|
/**
|
|
|
|
* p2p_channels_includes - Check whether a channel is included in the list
|
|
|
|
* @channels: List of supported channels
|
|
|
|
* @reg_class: Regulatory class of the channel to search
|
|
|
|
* @channel: Channel number of the channel to search
|
|
|
|
* Returns: 1 if channel was found or 0 if not
|
|
|
|
*/
|
|
|
|
int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
|
|
|
|
u8 channel)
|
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
for (i = 0; i < channels->reg_classes; i++) {
|
|
|
|
const struct p2p_reg_class *reg = &channels->reg_class[i];
|
|
|
|
if (reg->reg_class != reg_class)
|
|
|
|
continue;
|
|
|
|
for (j = 0; j < reg->channels; j++) {
|
|
|
|
if (reg->channel[j] == channel)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-14 13:24:56 +02:00
|
|
|
|
|
|
|
|
2013-03-01 13:22:29 +01:00
|
|
|
int p2p_channels_includes_freq(const struct p2p_channels *channels,
|
|
|
|
unsigned int freq)
|
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
for (i = 0; i < channels->reg_classes; i++) {
|
|
|
|
const struct p2p_reg_class *reg = &channels->reg_class[i];
|
|
|
|
for (j = 0; j < reg->channels; j++) {
|
2013-04-27 20:03:16 +02:00
|
|
|
if (p2p_channel_to_freq(reg->reg_class,
|
|
|
|
reg->channel[j]) == (int) freq)
|
2013-03-01 13:22:29 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-14 13:24:56 +02:00
|
|
|
int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
|
|
|
|
{
|
|
|
|
u8 op_reg_class, op_channel;
|
2013-04-27 20:03:16 +02:00
|
|
|
if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
|
2010-10-14 13:24:56 +02:00
|
|
|
return 0;
|
|
|
|
return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
|
|
|
|
op_channel);
|
|
|
|
}
|
2013-05-17 10:27:02 +02:00
|
|
|
|
|
|
|
|
2013-10-22 18:45:46 +02:00
|
|
|
int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
|
|
|
|
{
|
|
|
|
u8 op_reg_class, op_channel;
|
|
|
|
if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
|
|
|
|
return 0;
|
|
|
|
return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
|
|
|
|
op_channel) &&
|
|
|
|
!freq_range_list_includes(&p2p->no_go_freq, freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-22 20:00:49 +02:00
|
|
|
int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
|
|
|
|
{
|
|
|
|
u8 op_reg_class, op_channel;
|
|
|
|
if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
|
|
|
|
return 0;
|
|
|
|
return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
|
|
|
|
op_channel) ||
|
|
|
|
p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
|
|
|
|
op_channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-17 10:27:02 +02:00
|
|
|
unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
|
|
|
|
const struct p2p_channels *channels)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2014-05-23 19:03:01 +02:00
|
|
|
int freq = 0;
|
|
|
|
const struct p2p_channels *tmpc = channels ?
|
|
|
|
channels : &p2p->cfg->channels;
|
|
|
|
|
|
|
|
if (tmpc == NULL)
|
|
|
|
return 0;
|
2013-05-17 10:27:02 +02:00
|
|
|
|
|
|
|
for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
|
|
|
|
freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class,
|
|
|
|
p2p->cfg->pref_chan[i].chan);
|
2014-05-23 19:03:01 +02:00
|
|
|
if (p2p_channels_includes_freq(tmpc, freq))
|
2013-05-17 10:27:02 +02:00
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-22 17:44:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
void p2p_channels_dump(struct p2p_data *p2p, const char *title,
|
|
|
|
const struct p2p_channels *chan)
|
|
|
|
{
|
|
|
|
char buf[500], *pos, *end;
|
|
|
|
size_t i, j;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pos = buf;
|
|
|
|
end = pos + sizeof(buf);
|
|
|
|
|
|
|
|
for (i = 0; i < chan->reg_classes; i++) {
|
|
|
|
const struct p2p_reg_class *c;
|
|
|
|
c = &chan->reg_class[i];
|
|
|
|
ret = os_snprintf(pos, end - pos, " %u:", c->reg_class);
|
Check os_snprintf() result more consistently - automatic 1
This converts os_snprintf() result validation cases to use
os_snprintf_error() where the exact rule used in os_snprintf_error() was
used. These changes were done automatically with spatch using the
following semantic patch:
@@
identifier E1;
expression E2,E3,E4,E5,E6;
statement S1;
@@
(
E1 = os_snprintf(E2, E3, ...);
|
int E1 = os_snprintf(E2, E3, ...);
|
if (E5)
E1 = os_snprintf(E2, E3, ...);
else
E1 = os_snprintf(E2, E3, ...);
|
if (E5)
E1 = os_snprintf(E2, E3, ...);
else if (E6)
E1 = os_snprintf(E2, E3, ...);
else
E1 = 0;
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
return -1;
}
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else if (E6) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
return -1;
}
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
E1 = os_snprintf(E2, E3, ...);
}
)
? os_free(E4);
- if (E1 < 0 || \( E1 >= E3 \| (size_t) E1 >= E3 \| (unsigned int) E1 >= E3 \| E1 >= (int) E3 \))
+ if (os_snprintf_error(E3, E1))
(
S1
|
{ ... }
)
Signed-off-by: Jouni Malinen <j@w1.fi>
2014-12-08 10:15:51 +01:00
|
|
|
if (os_snprintf_error(end - pos, ret))
|
2013-10-22 17:44:05 +02:00
|
|
|
break;
|
|
|
|
pos += ret;
|
|
|
|
|
|
|
|
for (j = 0; j < c->channels; j++) {
|
|
|
|
ret = os_snprintf(pos, end - pos, "%s%u",
|
|
|
|
j == 0 ? "" : ",",
|
|
|
|
c->channel[j]);
|
Check os_snprintf() result more consistently - automatic 1
This converts os_snprintf() result validation cases to use
os_snprintf_error() where the exact rule used in os_snprintf_error() was
used. These changes were done automatically with spatch using the
following semantic patch:
@@
identifier E1;
expression E2,E3,E4,E5,E6;
statement S1;
@@
(
E1 = os_snprintf(E2, E3, ...);
|
int E1 = os_snprintf(E2, E3, ...);
|
if (E5)
E1 = os_snprintf(E2, E3, ...);
else
E1 = os_snprintf(E2, E3, ...);
|
if (E5)
E1 = os_snprintf(E2, E3, ...);
else if (E6)
E1 = os_snprintf(E2, E3, ...);
else
E1 = 0;
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
return -1;
}
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else if (E6) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
return -1;
}
|
if (E5) {
...
E1 = os_snprintf(E2, E3, ...);
} else {
...
E1 = os_snprintf(E2, E3, ...);
}
)
? os_free(E4);
- if (E1 < 0 || \( E1 >= E3 \| (size_t) E1 >= E3 \| (unsigned int) E1 >= E3 \| E1 >= (int) E3 \))
+ if (os_snprintf_error(E3, E1))
(
S1
|
{ ... }
)
Signed-off-by: Jouni Malinen <j@w1.fi>
2014-12-08 10:15:51 +01:00
|
|
|
if (os_snprintf_error(end - pos, ret))
|
2013-10-22 17:44:05 +02:00
|
|
|
break;
|
|
|
|
pos += ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
p2p_dbg(p2p, "%s:%s", title, buf);
|
|
|
|
}
|
2013-11-18 16:41:39 +01:00
|
|
|
|
|
|
|
|
2014-03-23 10:01:59 +01:00
|
|
|
static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
|
|
|
|
{
|
|
|
|
unsigned int r;
|
2014-09-13 15:27:52 +02:00
|
|
|
if (os_get_random((u8 *) &r, sizeof(r)) < 0)
|
|
|
|
r = 0;
|
2014-03-23 10:01:59 +01:00
|
|
|
r %= num_channels;
|
|
|
|
return channels[r];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-18 16:41:39 +01:00
|
|
|
int p2p_channel_select(struct p2p_channels *chans, const int *classes,
|
|
|
|
u8 *op_class, u8 *op_channel)
|
|
|
|
{
|
2014-03-23 10:01:59 +01:00
|
|
|
unsigned int i, j;
|
2013-11-18 16:41:39 +01:00
|
|
|
|
2014-03-23 10:01:59 +01:00
|
|
|
for (j = 0; classes == NULL || classes[j]; j++) {
|
2013-11-19 12:04:21 +01:00
|
|
|
for (i = 0; i < chans->reg_classes; i++) {
|
|
|
|
struct p2p_reg_class *c = &chans->reg_class[i];
|
2013-11-18 16:41:39 +01:00
|
|
|
|
2013-11-19 12:04:21 +01:00
|
|
|
if (c->channels == 0)
|
|
|
|
continue;
|
2013-11-18 16:41:39 +01:00
|
|
|
|
2014-03-23 10:01:59 +01:00
|
|
|
if (classes == NULL || c->reg_class == classes[j]) {
|
2013-11-19 12:04:21 +01:00
|
|
|
/*
|
|
|
|
* Pick one of the available channels in the
|
|
|
|
* operating class at random.
|
|
|
|
*/
|
|
|
|
*op_class = c->reg_class;
|
2014-03-23 10:01:59 +01:00
|
|
|
*op_channel = p2p_channel_pick_random(
|
|
|
|
c->channel, c->channels);
|
2013-11-19 12:04:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-11-18 16:41:39 +01:00
|
|
|
}
|
2014-03-23 10:01:59 +01:00
|
|
|
if (classes == NULL)
|
|
|
|
break;
|
2013-11-18 16:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-23 10:01:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
|
2018-12-28 13:42:00 +01:00
|
|
|
u8 *op_channel,
|
|
|
|
struct wpa_freq_range_list *avoid_list,
|
|
|
|
struct wpa_freq_range_list *disallow_list)
|
2014-03-23 10:01:59 +01:00
|
|
|
{
|
2014-09-25 11:42:07 +02:00
|
|
|
u8 chan[4];
|
2014-03-23 10:01:59 +01:00
|
|
|
unsigned int num_channels = 0;
|
|
|
|
|
2018-12-28 13:42:00 +01:00
|
|
|
/* Try to find available social channels from 2.4 GHz.
|
|
|
|
* If the avoid_list includes any of the 2.4 GHz social channels, that
|
|
|
|
* channel is not allowed by p2p_channels_includes() rules. However, it
|
|
|
|
* is assumed to allow minimal traffic for P2P negotiation, so allow it
|
|
|
|
* here for social channel selection unless explicitly disallowed in the
|
|
|
|
* disallow_list. */
|
|
|
|
if (p2p_channels_includes(chans, 81, 1) ||
|
|
|
|
(freq_range_list_includes(avoid_list, 2412) &&
|
|
|
|
!freq_range_list_includes(disallow_list, 2412)))
|
2014-03-23 10:01:59 +01:00
|
|
|
chan[num_channels++] = 1;
|
2018-12-28 13:42:00 +01:00
|
|
|
if (p2p_channels_includes(chans, 81, 6) ||
|
|
|
|
(freq_range_list_includes(avoid_list, 2437) &&
|
|
|
|
!freq_range_list_includes(disallow_list, 2437)))
|
2014-03-23 10:01:59 +01:00
|
|
|
chan[num_channels++] = 6;
|
2018-12-28 13:42:00 +01:00
|
|
|
if (p2p_channels_includes(chans, 81, 11) ||
|
|
|
|
(freq_range_list_includes(avoid_list, 2462) &&
|
|
|
|
!freq_range_list_includes(disallow_list, 2462)))
|
2014-03-23 10:01:59 +01:00
|
|
|
chan[num_channels++] = 11;
|
|
|
|
|
2014-09-25 11:42:07 +02:00
|
|
|
/* Try to find available social channels from 60 GHz */
|
|
|
|
if (p2p_channels_includes(chans, 180, 2))
|
|
|
|
chan[num_channels++] = 2;
|
|
|
|
|
2014-03-23 10:01:59 +01:00
|
|
|
if (num_channels == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*op_channel = p2p_channel_pick_random(chan, num_channels);
|
2014-09-25 11:42:07 +02:00
|
|
|
if (*op_channel == 2)
|
|
|
|
*op_class = 180;
|
|
|
|
else
|
|
|
|
*op_class = 81;
|
2014-03-23 10:01:59 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-07 13:21:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
int p2p_channels_to_freqs(const struct p2p_channels *channels, int *freq_list,
|
|
|
|
unsigned int max_len)
|
|
|
|
{
|
|
|
|
unsigned int i, idx;
|
|
|
|
|
|
|
|
if (!channels || max_len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0, idx = 0; i < channels->reg_classes; i++) {
|
|
|
|
const struct p2p_reg_class *c = &channels->reg_class[i];
|
|
|
|
unsigned int j;
|
|
|
|
|
|
|
|
if (idx + 1 == max_len)
|
|
|
|
break;
|
|
|
|
for (j = 0; j < c->channels; j++) {
|
|
|
|
int freq;
|
2015-09-08 11:46:09 +02:00
|
|
|
unsigned int k;
|
|
|
|
|
2014-07-07 13:21:01 +02:00
|
|
|
if (idx + 1 == max_len)
|
|
|
|
break;
|
|
|
|
freq = p2p_channel_to_freq(c->reg_class,
|
|
|
|
c->channel[j]);
|
|
|
|
if (freq < 0)
|
|
|
|
continue;
|
2015-09-08 11:46:09 +02:00
|
|
|
|
|
|
|
for (k = 0; k < idx; k++) {
|
|
|
|
if (freq_list[k] == freq)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k < idx)
|
|
|
|
continue;
|
2014-07-07 13:21:01 +02:00
|
|
|
freq_list[idx++] = freq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freq_list[idx] = 0;
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|