1f05cddf9SRui Paulo /*
2f05cddf9SRui Paulo * P2P - generic helper functions
3f05cddf9SRui Paulo * Copyright (c) 2009, Atheros Communications
4f05cddf9SRui Paulo *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
7f05cddf9SRui Paulo */
8f05cddf9SRui Paulo
9f05cddf9SRui Paulo #include "includes.h"
10f05cddf9SRui Paulo
11f05cddf9SRui Paulo #include "common.h"
12325151a3SRui Paulo #include "common/defs.h"
135b9c547cSRui Paulo #include "common/ieee802_11_common.h"
14f05cddf9SRui Paulo #include "p2p_i.h"
15f05cddf9SRui Paulo
16f05cddf9SRui Paulo
17f05cddf9SRui Paulo /**
18f05cddf9SRui Paulo * p2p_random - Generate random string for SSID and passphrase
19f05cddf9SRui Paulo * @buf: Buffer for returning the result
20f05cddf9SRui Paulo * @len: Number of octets to write to the buffer
21f05cddf9SRui Paulo * Returns: 0 on success, -1 on failure
22f05cddf9SRui Paulo *
23f05cddf9SRui Paulo * This function generates a random string using the following character set:
24f05cddf9SRui Paulo * 'A'-'Z', 'a'-'z', '0'-'9'.
25f05cddf9SRui Paulo */
p2p_random(char * buf,size_t len)26f05cddf9SRui Paulo int p2p_random(char *buf, size_t len)
27f05cddf9SRui Paulo {
28f05cddf9SRui Paulo u8 val;
29f05cddf9SRui Paulo size_t i;
30f05cddf9SRui Paulo u8 letters = 'Z' - 'A' + 1;
31f05cddf9SRui Paulo u8 numbers = 10;
32f05cddf9SRui Paulo
33f05cddf9SRui Paulo if (os_get_random((unsigned char *) buf, len))
34f05cddf9SRui Paulo return -1;
35f05cddf9SRui Paulo /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */
36f05cddf9SRui Paulo for (i = 0; i < len; i++) {
37f05cddf9SRui Paulo val = buf[i];
38f05cddf9SRui Paulo val %= 2 * letters + numbers;
39f05cddf9SRui Paulo if (val < letters)
40f05cddf9SRui Paulo buf[i] = 'A' + val;
41f05cddf9SRui Paulo else if (val < 2 * letters)
42f05cddf9SRui Paulo buf[i] = 'a' + (val - letters);
43f05cddf9SRui Paulo else
44f05cddf9SRui Paulo buf[i] = '0' + (val - 2 * letters);
45f05cddf9SRui Paulo }
46f05cddf9SRui Paulo
47f05cddf9SRui Paulo return 0;
48f05cddf9SRui Paulo }
49f05cddf9SRui Paulo
50f05cddf9SRui Paulo
51f05cddf9SRui Paulo /**
52f05cddf9SRui Paulo * p2p_channel_to_freq - Convert channel info to frequency
535b9c547cSRui Paulo * @op_class: Operating class
54f05cddf9SRui Paulo * @channel: Channel number
55f05cddf9SRui Paulo * Returns: Frequency in MHz or -1 if the specified channel is unknown
56f05cddf9SRui Paulo */
p2p_channel_to_freq(int op_class,int channel)575b9c547cSRui Paulo int p2p_channel_to_freq(int op_class, int channel)
58f05cddf9SRui Paulo {
595b9c547cSRui Paulo return ieee80211_chan_to_freq(NULL, op_class, channel);
60f05cddf9SRui Paulo }
61f05cddf9SRui Paulo
62f05cddf9SRui Paulo
63f05cddf9SRui Paulo /**
64f05cddf9SRui Paulo * p2p_freq_to_channel - Convert frequency into channel info
655b9c547cSRui Paulo * @op_class: Buffer for returning operating class
66f05cddf9SRui Paulo * @channel: Buffer for returning channel number
67f05cddf9SRui Paulo * Returns: 0 on success, -1 if the specified frequency is unknown
68f05cddf9SRui Paulo */
p2p_freq_to_channel(unsigned int freq,u8 * op_class,u8 * channel)695b9c547cSRui Paulo int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel)
70f05cddf9SRui Paulo {
71325151a3SRui Paulo if (ieee80211_freq_to_channel_ext(freq, 0, 0, op_class, channel) ==
72325151a3SRui Paulo NUM_HOSTAPD_MODES)
735b9c547cSRui Paulo return -1;
745b9c547cSRui Paulo
75f05cddf9SRui Paulo return 0;
76f05cddf9SRui Paulo }
77f05cddf9SRui Paulo
78f05cddf9SRui Paulo
p2p_reg_class_intersect(const struct p2p_reg_class * a,const struct p2p_reg_class * b,struct p2p_reg_class * res)79f05cddf9SRui Paulo static void p2p_reg_class_intersect(const struct p2p_reg_class *a,
80f05cddf9SRui Paulo const struct p2p_reg_class *b,
81f05cddf9SRui Paulo struct p2p_reg_class *res)
82f05cddf9SRui Paulo {
83f05cddf9SRui Paulo size_t i, j;
84f05cddf9SRui Paulo
85f05cddf9SRui Paulo res->reg_class = a->reg_class;
86f05cddf9SRui Paulo
87f05cddf9SRui Paulo for (i = 0; i < a->channels; i++) {
88f05cddf9SRui Paulo for (j = 0; j < b->channels; j++) {
89f05cddf9SRui Paulo if (a->channel[i] != b->channel[j])
90f05cddf9SRui Paulo continue;
91f05cddf9SRui Paulo res->channel[res->channels] = a->channel[i];
92f05cddf9SRui Paulo res->channels++;
93f05cddf9SRui Paulo if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
94f05cddf9SRui Paulo return;
95f05cddf9SRui Paulo }
96f05cddf9SRui Paulo }
97f05cddf9SRui Paulo }
98f05cddf9SRui Paulo
99f05cddf9SRui Paulo
100f05cddf9SRui Paulo /**
101f05cddf9SRui Paulo * p2p_channels_intersect - Intersection of supported channel lists
102f05cddf9SRui Paulo * @a: First set of supported channels
103f05cddf9SRui Paulo * @b: Second set of supported channels
104f05cddf9SRui Paulo * @res: Data structure for returning the intersection of support channels
105f05cddf9SRui Paulo *
106f05cddf9SRui Paulo * This function can be used to find a common set of supported channels. Both
107f05cddf9SRui Paulo * input channels sets are assumed to use the same country code. If different
108f05cddf9SRui Paulo * country codes are used, the regulatory class numbers may not be matched
109f05cddf9SRui Paulo * correctly and results are undefined.
110f05cddf9SRui Paulo */
p2p_channels_intersect(const struct p2p_channels * a,const struct p2p_channels * b,struct p2p_channels * res)111f05cddf9SRui Paulo void p2p_channels_intersect(const struct p2p_channels *a,
112f05cddf9SRui Paulo const struct p2p_channels *b,
113f05cddf9SRui Paulo struct p2p_channels *res)
114f05cddf9SRui Paulo {
115f05cddf9SRui Paulo size_t i, j;
116f05cddf9SRui Paulo
117f05cddf9SRui Paulo os_memset(res, 0, sizeof(*res));
118f05cddf9SRui Paulo
119f05cddf9SRui Paulo for (i = 0; i < a->reg_classes; i++) {
120f05cddf9SRui Paulo const struct p2p_reg_class *a_reg = &a->reg_class[i];
121f05cddf9SRui Paulo for (j = 0; j < b->reg_classes; j++) {
122f05cddf9SRui Paulo const struct p2p_reg_class *b_reg = &b->reg_class[j];
123f05cddf9SRui Paulo if (a_reg->reg_class != b_reg->reg_class)
124f05cddf9SRui Paulo continue;
125f05cddf9SRui Paulo p2p_reg_class_intersect(
126f05cddf9SRui Paulo a_reg, b_reg,
127f05cddf9SRui Paulo &res->reg_class[res->reg_classes]);
128f05cddf9SRui Paulo if (res->reg_class[res->reg_classes].channels) {
129f05cddf9SRui Paulo res->reg_classes++;
130f05cddf9SRui Paulo if (res->reg_classes == P2P_MAX_REG_CLASSES)
131f05cddf9SRui Paulo return;
132f05cddf9SRui Paulo }
133f05cddf9SRui Paulo }
134f05cddf9SRui Paulo }
135f05cddf9SRui Paulo }
136f05cddf9SRui Paulo
137f05cddf9SRui Paulo
p2p_op_class_union(struct p2p_reg_class * cl,const struct p2p_reg_class * b_cl)1385b9c547cSRui Paulo static void p2p_op_class_union(struct p2p_reg_class *cl,
1395b9c547cSRui Paulo const struct p2p_reg_class *b_cl)
1405b9c547cSRui Paulo {
1415b9c547cSRui Paulo size_t i, j;
1425b9c547cSRui Paulo
1435b9c547cSRui Paulo for (i = 0; i < b_cl->channels; i++) {
1445b9c547cSRui Paulo for (j = 0; j < cl->channels; j++) {
1455b9c547cSRui Paulo if (b_cl->channel[i] == cl->channel[j])
1465b9c547cSRui Paulo break;
1475b9c547cSRui Paulo }
1485b9c547cSRui Paulo if (j == cl->channels) {
1495b9c547cSRui Paulo if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
1505b9c547cSRui Paulo return;
1515b9c547cSRui Paulo cl->channel[cl->channels++] = b_cl->channel[i];
1525b9c547cSRui Paulo }
1535b9c547cSRui Paulo }
1545b9c547cSRui Paulo }
1555b9c547cSRui Paulo
1565b9c547cSRui Paulo
1575b9c547cSRui Paulo /**
1585b9c547cSRui Paulo * p2p_channels_union_inplace - Inplace union of channel lists
1595b9c547cSRui Paulo * @res: Input data and place for returning union of the channel sets
1605b9c547cSRui Paulo * @b: Second set of channels
1615b9c547cSRui Paulo */
p2p_channels_union_inplace(struct p2p_channels * res,const struct p2p_channels * b)1625b9c547cSRui Paulo void p2p_channels_union_inplace(struct p2p_channels *res,
1635b9c547cSRui Paulo const struct p2p_channels *b)
1645b9c547cSRui Paulo {
1655b9c547cSRui Paulo size_t i, j;
1665b9c547cSRui Paulo
1675b9c547cSRui Paulo for (i = 0; i < res->reg_classes; i++) {
1685b9c547cSRui Paulo struct p2p_reg_class *cl = &res->reg_class[i];
1695b9c547cSRui Paulo for (j = 0; j < b->reg_classes; j++) {
1705b9c547cSRui Paulo const struct p2p_reg_class *b_cl = &b->reg_class[j];
1715b9c547cSRui Paulo if (cl->reg_class != b_cl->reg_class)
1725b9c547cSRui Paulo continue;
1735b9c547cSRui Paulo p2p_op_class_union(cl, b_cl);
1745b9c547cSRui Paulo }
1755b9c547cSRui Paulo }
1765b9c547cSRui Paulo
1775b9c547cSRui Paulo for (j = 0; j < b->reg_classes; j++) {
1785b9c547cSRui Paulo const struct p2p_reg_class *b_cl = &b->reg_class[j];
1795b9c547cSRui Paulo
1805b9c547cSRui Paulo for (i = 0; i < res->reg_classes; i++) {
1815b9c547cSRui Paulo struct p2p_reg_class *cl = &res->reg_class[i];
1825b9c547cSRui Paulo if (cl->reg_class == b_cl->reg_class)
1835b9c547cSRui Paulo break;
1845b9c547cSRui Paulo }
1855b9c547cSRui Paulo
1865b9c547cSRui Paulo if (i == res->reg_classes) {
1875b9c547cSRui Paulo if (res->reg_classes == P2P_MAX_REG_CLASSES)
1885b9c547cSRui Paulo return;
1895b9c547cSRui Paulo os_memcpy(&res->reg_class[res->reg_classes++],
1905b9c547cSRui Paulo b_cl, sizeof(struct p2p_reg_class));
1915b9c547cSRui Paulo }
1925b9c547cSRui Paulo }
1935b9c547cSRui Paulo }
1945b9c547cSRui Paulo
1955b9c547cSRui Paulo
1965b9c547cSRui Paulo /**
1975b9c547cSRui Paulo * p2p_channels_union - Union of channel lists
1985b9c547cSRui Paulo * @a: First set of channels
1995b9c547cSRui Paulo * @b: Second set of channels
2005b9c547cSRui Paulo * @res: Data structure for returning the union of channels
2015b9c547cSRui Paulo */
p2p_channels_union(const struct p2p_channels * a,const struct p2p_channels * b,struct p2p_channels * res)2025b9c547cSRui Paulo void p2p_channels_union(const struct p2p_channels *a,
2035b9c547cSRui Paulo const struct p2p_channels *b,
2045b9c547cSRui Paulo struct p2p_channels *res)
2055b9c547cSRui Paulo {
2065b9c547cSRui Paulo os_memcpy(res, a, sizeof(*res));
2075b9c547cSRui Paulo p2p_channels_union_inplace(res, b);
2085b9c547cSRui Paulo }
2095b9c547cSRui Paulo
2105b9c547cSRui Paulo
p2p_channels_remove_freqs(struct p2p_channels * chan,const struct wpa_freq_range_list * list)2115b9c547cSRui Paulo void p2p_channels_remove_freqs(struct p2p_channels *chan,
2125b9c547cSRui Paulo const struct wpa_freq_range_list *list)
2135b9c547cSRui Paulo {
2145b9c547cSRui Paulo size_t o, c;
2155b9c547cSRui Paulo
2165b9c547cSRui Paulo if (list == NULL)
2175b9c547cSRui Paulo return;
2185b9c547cSRui Paulo
2195b9c547cSRui Paulo o = 0;
2205b9c547cSRui Paulo while (o < chan->reg_classes) {
2215b9c547cSRui Paulo struct p2p_reg_class *op = &chan->reg_class[o];
2225b9c547cSRui Paulo
2235b9c547cSRui Paulo c = 0;
2245b9c547cSRui Paulo while (c < op->channels) {
2255b9c547cSRui Paulo int freq = p2p_channel_to_freq(op->reg_class,
2265b9c547cSRui Paulo op->channel[c]);
2275b9c547cSRui Paulo if (freq > 0 && freq_range_list_includes(list, freq)) {
2285b9c547cSRui Paulo op->channels--;
2295b9c547cSRui Paulo os_memmove(&op->channel[c],
2305b9c547cSRui Paulo &op->channel[c + 1],
2315b9c547cSRui Paulo op->channels - c);
2325b9c547cSRui Paulo } else
2335b9c547cSRui Paulo c++;
2345b9c547cSRui Paulo }
2355b9c547cSRui Paulo
2365b9c547cSRui Paulo if (op->channels == 0) {
2375b9c547cSRui Paulo chan->reg_classes--;
2385b9c547cSRui Paulo os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1],
2395b9c547cSRui Paulo (chan->reg_classes - o) *
2405b9c547cSRui Paulo sizeof(struct p2p_reg_class));
2415b9c547cSRui Paulo } else
2425b9c547cSRui Paulo o++;
2435b9c547cSRui Paulo }
2445b9c547cSRui Paulo }
2455b9c547cSRui Paulo
2465b9c547cSRui Paulo
247f05cddf9SRui Paulo /**
248f05cddf9SRui Paulo * p2p_channels_includes - Check whether a channel is included in the list
249f05cddf9SRui Paulo * @channels: List of supported channels
250f05cddf9SRui Paulo * @reg_class: Regulatory class of the channel to search
251f05cddf9SRui Paulo * @channel: Channel number of the channel to search
252f05cddf9SRui Paulo * Returns: 1 if channel was found or 0 if not
253f05cddf9SRui Paulo */
p2p_channels_includes(const struct p2p_channels * channels,u8 reg_class,u8 channel)254f05cddf9SRui Paulo int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
255f05cddf9SRui Paulo u8 channel)
256f05cddf9SRui Paulo {
257f05cddf9SRui Paulo size_t i, j;
258f05cddf9SRui Paulo for (i = 0; i < channels->reg_classes; i++) {
259f05cddf9SRui Paulo const struct p2p_reg_class *reg = &channels->reg_class[i];
260f05cddf9SRui Paulo if (reg->reg_class != reg_class)
261f05cddf9SRui Paulo continue;
262f05cddf9SRui Paulo for (j = 0; j < reg->channels; j++) {
263f05cddf9SRui Paulo if (reg->channel[j] == channel)
264f05cddf9SRui Paulo return 1;
265f05cddf9SRui Paulo }
266f05cddf9SRui Paulo }
267f05cddf9SRui Paulo return 0;
268f05cddf9SRui Paulo }
269f05cddf9SRui Paulo
270f05cddf9SRui Paulo
p2p_channels_includes_freq(const struct p2p_channels * channels,unsigned int freq)2715b9c547cSRui Paulo int p2p_channels_includes_freq(const struct p2p_channels *channels,
2725b9c547cSRui Paulo unsigned int freq)
2735b9c547cSRui Paulo {
2745b9c547cSRui Paulo size_t i, j;
2755b9c547cSRui Paulo for (i = 0; i < channels->reg_classes; i++) {
2765b9c547cSRui Paulo const struct p2p_reg_class *reg = &channels->reg_class[i];
2775b9c547cSRui Paulo for (j = 0; j < reg->channels; j++) {
2785b9c547cSRui Paulo if (p2p_channel_to_freq(reg->reg_class,
2795b9c547cSRui Paulo reg->channel[j]) == (int) freq)
2805b9c547cSRui Paulo return 1;
2815b9c547cSRui Paulo }
2825b9c547cSRui Paulo }
2835b9c547cSRui Paulo return 0;
2845b9c547cSRui Paulo }
2855b9c547cSRui Paulo
2865b9c547cSRui Paulo
p2p_supported_freq(struct p2p_data * p2p,unsigned int freq)287f05cddf9SRui Paulo int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
288f05cddf9SRui Paulo {
289f05cddf9SRui Paulo u8 op_reg_class, op_channel;
2905b9c547cSRui Paulo if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
291f05cddf9SRui Paulo return 0;
292f05cddf9SRui Paulo return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
293f05cddf9SRui Paulo op_channel);
294f05cddf9SRui Paulo }
2955b9c547cSRui Paulo
2965b9c547cSRui Paulo
p2p_supported_freq_go(struct p2p_data * p2p,unsigned int freq)2975b9c547cSRui Paulo int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
2985b9c547cSRui Paulo {
2995b9c547cSRui Paulo u8 op_reg_class, op_channel;
3005b9c547cSRui Paulo if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
3015b9c547cSRui Paulo return 0;
3025b9c547cSRui Paulo return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
3035b9c547cSRui Paulo op_channel) &&
3045b9c547cSRui Paulo !freq_range_list_includes(&p2p->no_go_freq, freq);
3055b9c547cSRui Paulo }
3065b9c547cSRui Paulo
3075b9c547cSRui Paulo
p2p_supported_freq_cli(struct p2p_data * p2p,unsigned int freq)3085b9c547cSRui Paulo int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
3095b9c547cSRui Paulo {
3105b9c547cSRui Paulo u8 op_reg_class, op_channel;
3115b9c547cSRui Paulo if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
3125b9c547cSRui Paulo return 0;
3135b9c547cSRui Paulo return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
3145b9c547cSRui Paulo op_channel) ||
3155b9c547cSRui Paulo p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
3165b9c547cSRui Paulo op_channel);
3175b9c547cSRui Paulo }
3185b9c547cSRui Paulo
3195b9c547cSRui Paulo
p2p_get_pref_freq(struct p2p_data * p2p,const struct p2p_channels * channels)3205b9c547cSRui Paulo unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
3215b9c547cSRui Paulo const struct p2p_channels *channels)
3225b9c547cSRui Paulo {
3235b9c547cSRui Paulo unsigned int i;
3245b9c547cSRui Paulo int freq = 0;
3255b9c547cSRui Paulo const struct p2p_channels *tmpc = channels ?
3265b9c547cSRui Paulo channels : &p2p->cfg->channels;
3275b9c547cSRui Paulo
3285b9c547cSRui Paulo if (tmpc == NULL)
3295b9c547cSRui Paulo return 0;
3305b9c547cSRui Paulo
3315b9c547cSRui Paulo for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
3325b9c547cSRui Paulo freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class,
3335b9c547cSRui Paulo p2p->cfg->pref_chan[i].chan);
3345b9c547cSRui Paulo if (p2p_channels_includes_freq(tmpc, freq))
3355b9c547cSRui Paulo return freq;
3365b9c547cSRui Paulo }
3375b9c547cSRui Paulo return 0;
3385b9c547cSRui Paulo }
3395b9c547cSRui Paulo
3405b9c547cSRui Paulo
p2p_channels_dump(struct p2p_data * p2p,const char * title,const struct p2p_channels * chan)3415b9c547cSRui Paulo void p2p_channels_dump(struct p2p_data *p2p, const char *title,
3425b9c547cSRui Paulo const struct p2p_channels *chan)
3435b9c547cSRui Paulo {
3445b9c547cSRui Paulo char buf[500], *pos, *end;
3455b9c547cSRui Paulo size_t i, j;
3465b9c547cSRui Paulo int ret;
3475b9c547cSRui Paulo
3485b9c547cSRui Paulo pos = buf;
3495b9c547cSRui Paulo end = pos + sizeof(buf);
3505b9c547cSRui Paulo
3515b9c547cSRui Paulo for (i = 0; i < chan->reg_classes; i++) {
3525b9c547cSRui Paulo const struct p2p_reg_class *c;
3535b9c547cSRui Paulo c = &chan->reg_class[i];
3545b9c547cSRui Paulo ret = os_snprintf(pos, end - pos, " %u:", c->reg_class);
3555b9c547cSRui Paulo if (os_snprintf_error(end - pos, ret))
3565b9c547cSRui Paulo break;
3575b9c547cSRui Paulo pos += ret;
3585b9c547cSRui Paulo
3595b9c547cSRui Paulo for (j = 0; j < c->channels; j++) {
3605b9c547cSRui Paulo ret = os_snprintf(pos, end - pos, "%s%u",
3615b9c547cSRui Paulo j == 0 ? "" : ",",
3625b9c547cSRui Paulo c->channel[j]);
3635b9c547cSRui Paulo if (os_snprintf_error(end - pos, ret))
3645b9c547cSRui Paulo break;
3655b9c547cSRui Paulo pos += ret;
3665b9c547cSRui Paulo }
3675b9c547cSRui Paulo }
3685b9c547cSRui Paulo *pos = '\0';
3695b9c547cSRui Paulo
3705b9c547cSRui Paulo p2p_dbg(p2p, "%s:%s", title, buf);
3715b9c547cSRui Paulo }
3725b9c547cSRui Paulo
3735b9c547cSRui Paulo
p2p_channel_pick_random(const u8 * channels,unsigned int num_channels)3745b9c547cSRui Paulo static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
3755b9c547cSRui Paulo {
3765b9c547cSRui Paulo unsigned int r;
3775b9c547cSRui Paulo if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3785b9c547cSRui Paulo r = 0;
3795b9c547cSRui Paulo r %= num_channels;
3805b9c547cSRui Paulo return channels[r];
3815b9c547cSRui Paulo }
3825b9c547cSRui Paulo
3835b9c547cSRui Paulo
p2p_channel_select(struct p2p_channels * chans,const int * classes,u8 * op_class,u8 * op_channel)3845b9c547cSRui Paulo int p2p_channel_select(struct p2p_channels *chans, const int *classes,
3855b9c547cSRui Paulo u8 *op_class, u8 *op_channel)
3865b9c547cSRui Paulo {
3875b9c547cSRui Paulo unsigned int i, j;
3885b9c547cSRui Paulo
3895b9c547cSRui Paulo for (j = 0; classes == NULL || classes[j]; j++) {
3905b9c547cSRui Paulo for (i = 0; i < chans->reg_classes; i++) {
3915b9c547cSRui Paulo struct p2p_reg_class *c = &chans->reg_class[i];
3925b9c547cSRui Paulo
3935b9c547cSRui Paulo if (c->channels == 0)
3945b9c547cSRui Paulo continue;
3955b9c547cSRui Paulo
3965b9c547cSRui Paulo if (classes == NULL || c->reg_class == classes[j]) {
3975b9c547cSRui Paulo /*
3985b9c547cSRui Paulo * Pick one of the available channels in the
3995b9c547cSRui Paulo * operating class at random.
4005b9c547cSRui Paulo */
4015b9c547cSRui Paulo *op_class = c->reg_class;
4025b9c547cSRui Paulo *op_channel = p2p_channel_pick_random(
4035b9c547cSRui Paulo c->channel, c->channels);
4045b9c547cSRui Paulo return 0;
4055b9c547cSRui Paulo }
4065b9c547cSRui Paulo }
4075b9c547cSRui Paulo if (classes == NULL)
4085b9c547cSRui Paulo break;
4095b9c547cSRui Paulo }
4105b9c547cSRui Paulo
4115b9c547cSRui Paulo return -1;
4125b9c547cSRui Paulo }
4135b9c547cSRui Paulo
4145b9c547cSRui Paulo
p2p_channel_random_social(struct p2p_channels * chans,u8 * op_class,u8 * op_channel,struct wpa_freq_range_list * avoid_list,struct wpa_freq_range_list * disallow_list)4155b9c547cSRui Paulo int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
4164bc52338SCy Schubert u8 *op_channel,
4174bc52338SCy Schubert struct wpa_freq_range_list *avoid_list,
4184bc52338SCy Schubert struct wpa_freq_range_list *disallow_list)
4195b9c547cSRui Paulo {
4205b9c547cSRui Paulo u8 chan[4];
4215b9c547cSRui Paulo unsigned int num_channels = 0;
4225b9c547cSRui Paulo
4234bc52338SCy Schubert /* Try to find available social channels from 2.4 GHz.
4244bc52338SCy Schubert * If the avoid_list includes any of the 2.4 GHz social channels, that
4254bc52338SCy Schubert * channel is not allowed by p2p_channels_includes() rules. However, it
4264bc52338SCy Schubert * is assumed to allow minimal traffic for P2P negotiation, so allow it
4274bc52338SCy Schubert * here for social channel selection unless explicitly disallowed in the
4284bc52338SCy Schubert * disallow_list. */
4294bc52338SCy Schubert if (p2p_channels_includes(chans, 81, 1) ||
4304bc52338SCy Schubert (freq_range_list_includes(avoid_list, 2412) &&
4314bc52338SCy Schubert !freq_range_list_includes(disallow_list, 2412)))
4325b9c547cSRui Paulo chan[num_channels++] = 1;
4334bc52338SCy Schubert if (p2p_channels_includes(chans, 81, 6) ||
4344bc52338SCy Schubert (freq_range_list_includes(avoid_list, 2437) &&
4354bc52338SCy Schubert !freq_range_list_includes(disallow_list, 2437)))
4365b9c547cSRui Paulo chan[num_channels++] = 6;
4374bc52338SCy Schubert if (p2p_channels_includes(chans, 81, 11) ||
4384bc52338SCy Schubert (freq_range_list_includes(avoid_list, 2462) &&
4394bc52338SCy Schubert !freq_range_list_includes(disallow_list, 2462)))
4405b9c547cSRui Paulo chan[num_channels++] = 11;
4415b9c547cSRui Paulo
4425b9c547cSRui Paulo /* Try to find available social channels from 60 GHz */
4435b9c547cSRui Paulo if (p2p_channels_includes(chans, 180, 2))
4445b9c547cSRui Paulo chan[num_channels++] = 2;
4455b9c547cSRui Paulo
4465b9c547cSRui Paulo if (num_channels == 0)
4475b9c547cSRui Paulo return -1;
4485b9c547cSRui Paulo
4495b9c547cSRui Paulo *op_channel = p2p_channel_pick_random(chan, num_channels);
4505b9c547cSRui Paulo if (*op_channel == 2)
4515b9c547cSRui Paulo *op_class = 180;
4525b9c547cSRui Paulo else
4535b9c547cSRui Paulo *op_class = 81;
4545b9c547cSRui Paulo
4555b9c547cSRui Paulo return 0;
4565b9c547cSRui Paulo }
4575b9c547cSRui Paulo
4585b9c547cSRui Paulo
p2p_channels_to_freqs(const struct p2p_channels * channels,int * freq_list,unsigned int max_len)4595b9c547cSRui Paulo int p2p_channels_to_freqs(const struct p2p_channels *channels, int *freq_list,
4605b9c547cSRui Paulo unsigned int max_len)
4615b9c547cSRui Paulo {
4625b9c547cSRui Paulo unsigned int i, idx;
4635b9c547cSRui Paulo
4645b9c547cSRui Paulo if (!channels || max_len == 0)
4655b9c547cSRui Paulo return 0;
4665b9c547cSRui Paulo
4675b9c547cSRui Paulo for (i = 0, idx = 0; i < channels->reg_classes; i++) {
4685b9c547cSRui Paulo const struct p2p_reg_class *c = &channels->reg_class[i];
4695b9c547cSRui Paulo unsigned int j;
4705b9c547cSRui Paulo
4715b9c547cSRui Paulo if (idx + 1 == max_len)
4725b9c547cSRui Paulo break;
4735b9c547cSRui Paulo for (j = 0; j < c->channels; j++) {
4745b9c547cSRui Paulo int freq;
475325151a3SRui Paulo unsigned int k;
476325151a3SRui Paulo
4775b9c547cSRui Paulo if (idx + 1 == max_len)
4785b9c547cSRui Paulo break;
4795b9c547cSRui Paulo freq = p2p_channel_to_freq(c->reg_class,
4805b9c547cSRui Paulo c->channel[j]);
4815b9c547cSRui Paulo if (freq < 0)
4825b9c547cSRui Paulo continue;
483325151a3SRui Paulo
484325151a3SRui Paulo for (k = 0; k < idx; k++) {
485325151a3SRui Paulo if (freq_list[k] == freq)
486325151a3SRui Paulo break;
487325151a3SRui Paulo }
488325151a3SRui Paulo
489325151a3SRui Paulo if (k < idx)
490325151a3SRui Paulo continue;
4915b9c547cSRui Paulo freq_list[idx++] = freq;
4925b9c547cSRui Paulo }
4935b9c547cSRui Paulo }
4945b9c547cSRui Paulo
4955b9c547cSRui Paulo freq_list[idx] = 0;
4965b9c547cSRui Paulo
4975b9c547cSRui Paulo return idx;
4985b9c547cSRui Paulo }
499c1d255d3SCy Schubert
500c1d255d3SCy Schubert
p2p_copy_channels(struct p2p_channels * dst,const struct p2p_channels * src,bool allow_6ghz)501c1d255d3SCy Schubert void p2p_copy_channels(struct p2p_channels *dst,
502c1d255d3SCy Schubert const struct p2p_channels *src, bool allow_6ghz)
503c1d255d3SCy Schubert {
504c1d255d3SCy Schubert size_t i, j;
505c1d255d3SCy Schubert
506c1d255d3SCy Schubert if (allow_6ghz) {
507c1d255d3SCy Schubert os_memcpy(dst, src, sizeof(struct p2p_channels));
508c1d255d3SCy Schubert return;
509c1d255d3SCy Schubert }
510c1d255d3SCy Schubert
511*a90b9d01SCy Schubert for (i = 0, j = 0; i < src->reg_classes; i++) {
512c1d255d3SCy Schubert if (is_6ghz_op_class(src->reg_class[i].reg_class))
513c1d255d3SCy Schubert continue;
514c1d255d3SCy Schubert os_memcpy(&dst->reg_class[j], &src->reg_class[i],
515c1d255d3SCy Schubert sizeof(struct p2p_reg_class));
516c1d255d3SCy Schubert j++;
517c1d255d3SCy Schubert }
518c1d255d3SCy Schubert dst->reg_classes = j;
519c1d255d3SCy Schubert }
520c1d255d3SCy Schubert
521c1d255d3SCy Schubert
p2p_remove_6ghz_channels(struct weighted_pcl * pref_freq_list,int size)522*a90b9d01SCy Schubert int p2p_remove_6ghz_channels(struct weighted_pcl *pref_freq_list, int size)
523c1d255d3SCy Schubert {
524c1d255d3SCy Schubert int i;
525c1d255d3SCy Schubert
526c1d255d3SCy Schubert for (i = 0; i < size; i++) {
527*a90b9d01SCy Schubert if (is_6ghz_freq(pref_freq_list[i].freq)) {
528c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "P2P: Remove 6 GHz channel %d",
529*a90b9d01SCy Schubert pref_freq_list[i].freq);
530c1d255d3SCy Schubert size--;
531c1d255d3SCy Schubert os_memmove(&pref_freq_list[i], &pref_freq_list[i + 1],
532c1d255d3SCy Schubert (size - i) * sizeof(pref_freq_list[0]));
533c1d255d3SCy Schubert i--;
534c1d255d3SCy Schubert }
535c1d255d3SCy Schubert }
536c1d255d3SCy Schubert return i;
537c1d255d3SCy Schubert }
538*a90b9d01SCy Schubert
539*a90b9d01SCy Schubert
540*a90b9d01SCy Schubert /**
541*a90b9d01SCy Schubert * p2p_pref_freq_allowed - Based on the flags set, check if the preferred
542*a90b9d01SCy Schubert * frequency is allowed
543*a90b9d01SCy Schubert * @freq_list: Weighted preferred channel list
544*a90b9d01SCy Schubert * @go: Whether the local device is the group owner
545*a90b9d01SCy Schubert * Returns: Whether the preferred frequency is allowed
546*a90b9d01SCy Schubert */
p2p_pref_freq_allowed(const struct weighted_pcl * freq_list,bool go)547*a90b9d01SCy Schubert bool p2p_pref_freq_allowed(const struct weighted_pcl *freq_list, bool go)
548*a90b9d01SCy Schubert {
549*a90b9d01SCy Schubert if (freq_list->flag & WEIGHTED_PCL_EXCLUDE)
550*a90b9d01SCy Schubert return false;
551*a90b9d01SCy Schubert if (!(freq_list->flag & WEIGHTED_PCL_CLI) && !go)
552*a90b9d01SCy Schubert return false;
553*a90b9d01SCy Schubert if (!(freq_list->flag & WEIGHTED_PCL_GO) && go)
554*a90b9d01SCy Schubert return false;
555*a90b9d01SCy Schubert return true;
556*a90b9d01SCy Schubert }
557*a90b9d01SCy Schubert
558*a90b9d01SCy Schubert
p2p_check_pref_channel(int channel,u8 op_class,const struct weighted_pcl * freq_list,unsigned int num_channels,bool go)559*a90b9d01SCy Schubert static int p2p_check_pref_channel(int channel, u8 op_class,
560*a90b9d01SCy Schubert const struct weighted_pcl *freq_list,
561*a90b9d01SCy Schubert unsigned int num_channels, bool go)
562*a90b9d01SCy Schubert {
563*a90b9d01SCy Schubert unsigned int i;
564*a90b9d01SCy Schubert
565*a90b9d01SCy Schubert /* If the channel is present in the preferred channel list, check if it
566*a90b9d01SCy Schubert * has appropriate flags for the role.
567*a90b9d01SCy Schubert */
568*a90b9d01SCy Schubert for (i = 0; i < num_channels; i++) {
569*a90b9d01SCy Schubert if (p2p_channel_to_freq(op_class, channel) !=
570*a90b9d01SCy Schubert (int) freq_list[i].freq)
571*a90b9d01SCy Schubert continue;
572*a90b9d01SCy Schubert if (!p2p_pref_freq_allowed(&freq_list[i], go))
573*a90b9d01SCy Schubert return -1;
574*a90b9d01SCy Schubert break;
575*a90b9d01SCy Schubert }
576*a90b9d01SCy Schubert
577*a90b9d01SCy Schubert return 0;
578*a90b9d01SCy Schubert }
579*a90b9d01SCy Schubert
580*a90b9d01SCy Schubert
p2p_pref_channel_filter(const struct p2p_channels * p2p_chan,const struct weighted_pcl * freq_list,unsigned int num_channels,struct p2p_channels * res,bool go)581*a90b9d01SCy Schubert void p2p_pref_channel_filter(const struct p2p_channels *p2p_chan,
582*a90b9d01SCy Schubert const struct weighted_pcl *freq_list,
583*a90b9d01SCy Schubert unsigned int num_channels,
584*a90b9d01SCy Schubert struct p2p_channels *res, bool go)
585*a90b9d01SCy Schubert {
586*a90b9d01SCy Schubert size_t i, j;
587*a90b9d01SCy Schubert
588*a90b9d01SCy Schubert os_memset(res, 0, sizeof(*res));
589*a90b9d01SCy Schubert
590*a90b9d01SCy Schubert for (i = 0; i < p2p_chan->reg_classes; i++) {
591*a90b9d01SCy Schubert const struct p2p_reg_class *reg = &p2p_chan->reg_class[i];
592*a90b9d01SCy Schubert struct p2p_reg_class *res_reg = &res->reg_class[i];
593*a90b9d01SCy Schubert
594*a90b9d01SCy Schubert if (num_channels > 0) {
595*a90b9d01SCy Schubert for (j = 0; j < reg->channels; j++) {
596*a90b9d01SCy Schubert if (p2p_check_pref_channel(reg->channel[j],
597*a90b9d01SCy Schubert reg->reg_class,
598*a90b9d01SCy Schubert freq_list,
599*a90b9d01SCy Schubert num_channels,
600*a90b9d01SCy Schubert go) < 0)
601*a90b9d01SCy Schubert continue;
602*a90b9d01SCy Schubert
603*a90b9d01SCy Schubert res_reg->channel[res_reg->channels++] =
604*a90b9d01SCy Schubert reg->channel[j];
605*a90b9d01SCy Schubert }
606*a90b9d01SCy Schubert }
607*a90b9d01SCy Schubert
608*a90b9d01SCy Schubert if (res_reg->channels == 0)
609*a90b9d01SCy Schubert continue;
610*a90b9d01SCy Schubert res->reg_classes++;
611*a90b9d01SCy Schubert res_reg->reg_class = reg->reg_class;
612*a90b9d01SCy Schubert }
613*a90b9d01SCy Schubert }
614