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 */
25int p2p_random(char *buf, size_t len)
26{
27	u8 val;
28	size_t i;
29	u8 letters = 'Z' - 'A' + 1;
30	u8 numbers = 10;
31
32	if (os_get_random((unsigned char *) buf, len))
33		return -1;
34	/* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */
35	for (i = 0; i < len; i++) {
36		val = buf[i];
37		val %= 2 * letters + numbers;
38		if (val < letters)
39			buf[i] = 'A' + val;
40		else if (val < 2 * letters)
41			buf[i] = 'a' + (val - letters);
42		else
43			buf[i] = '0' + (val - 2 * letters);
44	}
45
46	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 */
56int p2p_channel_to_freq(int op_class, int channel)
57{
58	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 */
68int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel)
69{
70	/* TODO: more operating classes */
71	if (freq >= 2412 && freq <= 2472) {
72		if ((freq - 2407) % 5)
73			return -1;
74
75		*op_class = 81; /* 2.407 GHz, channels 1..13 */
76		*channel = (freq - 2407) / 5;
77		return 0;
78	}
79
80	if (freq == 2484) {
81		*op_class = 82; /* channel 14 */
82		*channel = 14;
83		return 0;
84	}
85
86	if (freq >= 5180 && freq <= 5240) {
87		if ((freq - 5000) % 5)
88			return -1;
89
90		*op_class = 115; /* 5 GHz, channels 36..48 */
91		*channel = (freq - 5000) / 5;
92		return 0;
93	}
94
95	if (freq >= 5745 && freq <= 5805) {
96		if ((freq - 5000) % 5)
97			return -1;
98
99		*op_class = 124; /* 5 GHz, channels 149..161 */
100		*channel = (freq - 5000) / 5;
101		return 0;
102	}
103
104	if (freq >= 5745 && freq <= 5845) {
105		if ((freq - 5000) % 5)
106			return -1;
107
108		*op_class = 125; /* 5 GHz, channels 149..169 */
109		*channel = (freq - 5000) / 5;
110		return 0;
111	}
112
113	if (freq >= 58320 && freq <= 64800) {
114		if ((freq - 58320) % 2160)
115			return -1;
116
117		*op_class = 180; /* 60 GHz, channels 1..4 */
118		*channel = (freq - 56160) / 2160;
119		return 0;
120	}
121
122	return -1;
123}
124
125
126static void p2p_reg_class_intersect(const struct p2p_reg_class *a,
127				    const struct p2p_reg_class *b,
128				    struct p2p_reg_class *res)
129{
130	size_t i, j;
131
132	res->reg_class = a->reg_class;
133
134	for (i = 0; i < a->channels; i++) {
135		for (j = 0; j < b->channels; j++) {
136			if (a->channel[i] != b->channel[j])
137				continue;
138			res->channel[res->channels] = a->channel[i];
139			res->channels++;
140			if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
141				return;
142		}
143	}
144}
145
146
147/**
148 * p2p_channels_intersect - Intersection of supported channel lists
149 * @a: First set of supported channels
150 * @b: Second set of supported channels
151 * @res: Data structure for returning the intersection of support channels
152 *
153 * This function can be used to find a common set of supported channels. Both
154 * input channels sets are assumed to use the same country code. If different
155 * country codes are used, the regulatory class numbers may not be matched
156 * correctly and results are undefined.
157 */
158void p2p_channels_intersect(const struct p2p_channels *a,
159			    const struct p2p_channels *b,
160			    struct p2p_channels *res)
161{
162	size_t i, j;
163
164	os_memset(res, 0, sizeof(*res));
165
166	for (i = 0; i < a->reg_classes; i++) {
167		const struct p2p_reg_class *a_reg = &a->reg_class[i];
168		for (j = 0; j < b->reg_classes; j++) {
169			const struct p2p_reg_class *b_reg = &b->reg_class[j];
170			if (a_reg->reg_class != b_reg->reg_class)
171				continue;
172			p2p_reg_class_intersect(
173				a_reg, b_reg,
174				&res->reg_class[res->reg_classes]);
175			if (res->reg_class[res->reg_classes].channels) {
176				res->reg_classes++;
177				if (res->reg_classes == P2P_MAX_REG_CLASSES)
178					return;
179			}
180		}
181	}
182}
183
184
185static void p2p_op_class_union(struct p2p_reg_class *cl,
186			       const struct p2p_reg_class *b_cl)
187{
188	size_t i, j;
189
190	for (i = 0; i < b_cl->channels; i++) {
191		for (j = 0; j < cl->channels; j++) {
192			if (b_cl->channel[i] == cl->channel[j])
193				break;
194		}
195		if (j == cl->channels) {
196			if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
197				return;
198			cl->channel[cl->channels++] = b_cl->channel[i];
199		}
200	}
201}
202
203
204/**
205 * p2p_channels_union_inplace - Inplace union of channel lists
206 * @res: Input data and place for returning union of the channel sets
207 * @b: Second set of channels
208 */
209void p2p_channels_union_inplace(struct p2p_channels *res,
210				const struct p2p_channels *b)
211{
212	size_t i, j;
213
214	for (i = 0; i < res->reg_classes; i++) {
215		struct p2p_reg_class *cl = &res->reg_class[i];
216		for (j = 0; j < b->reg_classes; j++) {
217			const struct p2p_reg_class *b_cl = &b->reg_class[j];
218			if (cl->reg_class != b_cl->reg_class)
219				continue;
220			p2p_op_class_union(cl, b_cl);
221		}
222	}
223
224	for (j = 0; j < b->reg_classes; j++) {
225		const struct p2p_reg_class *b_cl = &b->reg_class[j];
226
227		for (i = 0; i < res->reg_classes; i++) {
228			struct p2p_reg_class *cl = &res->reg_class[i];
229			if (cl->reg_class == b_cl->reg_class)
230				break;
231		}
232
233		if (i == res->reg_classes) {
234			if (res->reg_classes == P2P_MAX_REG_CLASSES)
235				return;
236			os_memcpy(&res->reg_class[res->reg_classes++],
237				  b_cl, sizeof(struct p2p_reg_class));
238		}
239	}
240}
241
242
243/**
244 * p2p_channels_union - Union of channel lists
245 * @a: First set of channels
246 * @b: Second set of channels
247 * @res: Data structure for returning the union of channels
248 */
249void p2p_channels_union(const struct p2p_channels *a,
250			const struct p2p_channels *b,
251			struct p2p_channels *res)
252{
253	os_memcpy(res, a, sizeof(*res));
254	p2p_channels_union_inplace(res, b);
255}
256
257
258void p2p_channels_remove_freqs(struct p2p_channels *chan,
259			       const struct wpa_freq_range_list *list)
260{
261	size_t o, c;
262
263	if (list == NULL)
264		return;
265
266	o = 0;
267	while (o < chan->reg_classes) {
268		struct p2p_reg_class *op = &chan->reg_class[o];
269
270		c = 0;
271		while (c < op->channels) {
272			int freq = p2p_channel_to_freq(op->reg_class,
273						       op->channel[c]);
274			if (freq > 0 && freq_range_list_includes(list, freq)) {
275				op->channels--;
276				os_memmove(&op->channel[c],
277					   &op->channel[c + 1],
278					   op->channels - c);
279			} else
280				c++;
281		}
282
283		if (op->channels == 0) {
284			chan->reg_classes--;
285			os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1],
286				   (chan->reg_classes - o) *
287				   sizeof(struct p2p_reg_class));
288		} else
289			o++;
290	}
291}
292
293
294/**
295 * p2p_channels_includes - Check whether a channel is included in the list
296 * @channels: List of supported channels
297 * @reg_class: Regulatory class of the channel to search
298 * @channel: Channel number of the channel to search
299 * Returns: 1 if channel was found or 0 if not
300 */
301int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
302			  u8 channel)
303{
304	size_t i, j;
305	for (i = 0; i < channels->reg_classes; i++) {
306		const struct p2p_reg_class *reg = &channels->reg_class[i];
307		if (reg->reg_class != reg_class)
308			continue;
309		for (j = 0; j < reg->channels; j++) {
310			if (reg->channel[j] == channel)
311				return 1;
312		}
313	}
314	return 0;
315}
316
317
318int p2p_channels_includes_freq(const struct p2p_channels *channels,
319			       unsigned int freq)
320{
321	size_t i, j;
322	for (i = 0; i < channels->reg_classes; i++) {
323		const struct p2p_reg_class *reg = &channels->reg_class[i];
324		for (j = 0; j < reg->channels; j++) {
325			if (p2p_channel_to_freq(reg->reg_class,
326						reg->channel[j]) == (int) freq)
327				return 1;
328		}
329	}
330	return 0;
331}
332
333
334int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
335{
336	u8 op_reg_class, op_channel;
337	if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
338		return 0;
339	return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
340				     op_channel);
341}
342
343
344int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
345{
346	u8 op_reg_class, op_channel;
347	if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
348		return 0;
349	return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
350				     op_channel) &&
351		!freq_range_list_includes(&p2p->no_go_freq, freq);
352}
353
354
355int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
356{
357	u8 op_reg_class, op_channel;
358	if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
359		return 0;
360	return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
361				     op_channel) ||
362		p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
363				      op_channel);
364}
365
366
367unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
368			       const struct p2p_channels *channels)
369{
370	unsigned int i;
371	int freq = 0;
372	const struct p2p_channels *tmpc = channels ?
373		channels : &p2p->cfg->channels;
374
375	if (tmpc == NULL)
376		return 0;
377
378	for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
379		freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class,
380					   p2p->cfg->pref_chan[i].chan);
381		if (p2p_channels_includes_freq(tmpc, freq))
382			return freq;
383	}
384	return 0;
385}
386
387
388void p2p_channels_dump(struct p2p_data *p2p, const char *title,
389		       const struct p2p_channels *chan)
390{
391	char buf[500], *pos, *end;
392	size_t i, j;
393	int ret;
394
395	pos = buf;
396	end = pos + sizeof(buf);
397
398	for (i = 0; i < chan->reg_classes; i++) {
399		const struct p2p_reg_class *c;
400		c = &chan->reg_class[i];
401		ret = os_snprintf(pos, end - pos, " %u:", c->reg_class);
402		if (os_snprintf_error(end - pos, ret))
403			break;
404		pos += ret;
405
406		for (j = 0; j < c->channels; j++) {
407			ret = os_snprintf(pos, end - pos, "%s%u",
408					  j == 0 ? "" : ",",
409					  c->channel[j]);
410			if (os_snprintf_error(end - pos, ret))
411				break;
412			pos += ret;
413		}
414	}
415	*pos = '\0';
416
417	p2p_dbg(p2p, "%s:%s", title, buf);
418}
419
420
421static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
422{
423	unsigned int r;
424	if (os_get_random((u8 *) &r, sizeof(r)) < 0)
425		r = 0;
426	r %= num_channels;
427	return channels[r];
428}
429
430
431int p2p_channel_select(struct p2p_channels *chans, const int *classes,
432		       u8 *op_class, u8 *op_channel)
433{
434	unsigned int i, j;
435
436	for (j = 0; classes == NULL || classes[j]; j++) {
437		for (i = 0; i < chans->reg_classes; i++) {
438			struct p2p_reg_class *c = &chans->reg_class[i];
439
440			if (c->channels == 0)
441				continue;
442
443			if (classes == NULL || c->reg_class == classes[j]) {
444				/*
445				 * Pick one of the available channels in the
446				 * operating class at random.
447				 */
448				*op_class = c->reg_class;
449				*op_channel = p2p_channel_pick_random(
450					c->channel, c->channels);
451				return 0;
452			}
453		}
454		if (classes == NULL)
455			break;
456	}
457
458	return -1;
459}
460
461
462int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
463			      u8 *op_channel)
464{
465	u8 chan[4];
466	unsigned int num_channels = 0;
467
468	/* Try to find available social channels from 2.4 GHz */
469	if (p2p_channels_includes(chans, 81, 1))
470		chan[num_channels++] = 1;
471	if (p2p_channels_includes(chans, 81, 6))
472		chan[num_channels++] = 6;
473	if (p2p_channels_includes(chans, 81, 11))
474		chan[num_channels++] = 11;
475
476	/* Try to find available social channels from 60 GHz */
477	if (p2p_channels_includes(chans, 180, 2))
478		chan[num_channels++] = 2;
479
480	if (num_channels == 0)
481		return -1;
482
483	*op_channel = p2p_channel_pick_random(chan, num_channels);
484	if (*op_channel == 2)
485		*op_class = 180;
486	else
487		*op_class = 81;
488
489	return 0;
490}
491
492
493int p2p_channels_to_freqs(const struct p2p_channels *channels, int *freq_list,
494			  unsigned int max_len)
495{
496	unsigned int i, idx;
497
498	if (!channels || max_len == 0)
499		return 0;
500
501	for (i = 0, idx = 0; i < channels->reg_classes; i++) {
502		const struct p2p_reg_class *c = &channels->reg_class[i];
503		unsigned int j;
504
505		if (idx + 1 == max_len)
506			break;
507		for (j = 0; j < c->channels; j++) {
508			int freq;
509			if (idx + 1 == max_len)
510				break;
511			freq = p2p_channel_to_freq(c->reg_class,
512						   c->channel[j]);
513			if (freq < 0)
514				continue;
515			freq_list[idx++] = freq;
516		}
517	}
518
519	freq_list[idx] = 0;
520
521	return idx;
522}
523