Check os_snprintf() result more consistently - automatic 1
[mech_eap.git] / src / p2p / p2p_utils.c
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  */
25 int 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  */
56 int 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  */
68 int 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 >= 58320 && freq <= 64800) {
105                 if ((freq - 58320) % 2160)
106                         return -1;
107
108                 *op_class = 180; /* 60 GHz, channels 1..4 */
109                 *channel = (freq - 56160) / 2160;
110                 return 0;
111         }
112
113         return -1;
114 }
115
116
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)
120 {
121         size_t i, j;
122
123         res->reg_class = a->reg_class;
124
125         for (i = 0; i < a->channels; i++) {
126                 for (j = 0; j < b->channels; j++) {
127                         if (a->channel[i] != b->channel[j])
128                                 continue;
129                         res->channel[res->channels] = a->channel[i];
130                         res->channels++;
131                         if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
132                                 return;
133                 }
134         }
135 }
136
137
138 /**
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
143  *
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.
148  */
149 void p2p_channels_intersect(const struct p2p_channels *a,
150                             const struct p2p_channels *b,
151                             struct p2p_channels *res)
152 {
153         size_t i, j;
154
155         os_memset(res, 0, sizeof(*res));
156
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)
162                                 continue;
163                         p2p_reg_class_intersect(
164                                 a_reg, b_reg,
165                                 &res->reg_class[res->reg_classes]);
166                         if (res->reg_class[res->reg_classes].channels) {
167                                 res->reg_classes++;
168                                 if (res->reg_classes == P2P_MAX_REG_CLASSES)
169                                         return;
170                         }
171                 }
172         }
173 }
174
175
176 static void p2p_op_class_union(struct p2p_reg_class *cl,
177                                const struct p2p_reg_class *b_cl)
178 {
179         size_t i, j;
180
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])
184                                 break;
185                 }
186                 if (j == cl->channels) {
187                         if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
188                                 return;
189                         cl->channel[cl->channels++] = b_cl->channel[i];
190                 }
191         }
192 }
193
194
195 /**
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
199  */
200 void p2p_channels_union_inplace(struct p2p_channels *res,
201                                 const struct p2p_channels *b)
202 {
203         size_t i, j;
204
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)
210                                 continue;
211                         p2p_op_class_union(cl, b_cl);
212                 }
213         }
214
215         for (j = 0; j < b->reg_classes; j++) {
216                 const struct p2p_reg_class *b_cl = &b->reg_class[j];
217
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)
221                                 break;
222                 }
223
224                 if (i == res->reg_classes) {
225                         if (res->reg_classes == P2P_MAX_REG_CLASSES)
226                                 return;
227                         os_memcpy(&res->reg_class[res->reg_classes++],
228                                   b_cl, sizeof(struct p2p_reg_class));
229                 }
230         }
231 }
232
233
234 /**
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
239  */
240 void p2p_channels_union(const struct p2p_channels *a,
241                         const struct p2p_channels *b,
242                         struct p2p_channels *res)
243 {
244         os_memcpy(res, a, sizeof(*res));
245         p2p_channels_union_inplace(res, b);
246 }
247
248
249 void p2p_channels_remove_freqs(struct p2p_channels *chan,
250                                const struct wpa_freq_range_list *list)
251 {
252         size_t o, c;
253
254         if (list == NULL)
255                 return;
256
257         o = 0;
258         while (o < chan->reg_classes) {
259                 struct p2p_reg_class *op = &chan->reg_class[o];
260
261                 c = 0;
262                 while (c < op->channels) {
263                         int freq = p2p_channel_to_freq(op->reg_class,
264                                                        op->channel[c]);
265                         if (freq > 0 && freq_range_list_includes(list, freq)) {
266                                 op->channels--;
267                                 os_memmove(&op->channel[c],
268                                            &op->channel[c + 1],
269                                            op->channels - c);
270                         } else
271                                 c++;
272                 }
273
274                 if (op->channels == 0) {
275                         chan->reg_classes--;
276                         os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1],
277                                    (chan->reg_classes - o) *
278                                    sizeof(struct p2p_reg_class));
279                 } else
280                         o++;
281         }
282 }
283
284
285 /**
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
291  */
292 int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
293                           u8 channel)
294 {
295         size_t i, j;
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)
299                         continue;
300                 for (j = 0; j < reg->channels; j++) {
301                         if (reg->channel[j] == channel)
302                                 return 1;
303                 }
304         }
305         return 0;
306 }
307
308
309 int p2p_channels_includes_freq(const struct p2p_channels *channels,
310                                unsigned int freq)
311 {
312         size_t i, j;
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)
318                                 return 1;
319                 }
320         }
321         return 0;
322 }
323
324
325 int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
326 {
327         u8 op_reg_class, op_channel;
328         if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
329                 return 0;
330         return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
331                                      op_channel);
332 }
333
334
335 int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
336 {
337         u8 op_reg_class, op_channel;
338         if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
339                 return 0;
340         return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
341                                      op_channel) &&
342                 !freq_range_list_includes(&p2p->no_go_freq, freq);
343 }
344
345
346 int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
347 {
348         u8 op_reg_class, op_channel;
349         if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
350                 return 0;
351         return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
352                                      op_channel) ||
353                 p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
354                                       op_channel);
355 }
356
357
358 unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
359                                const struct p2p_channels *channels)
360 {
361         unsigned int i;
362         int freq = 0;
363         const struct p2p_channels *tmpc = channels ?
364                 channels : &p2p->cfg->channels;
365
366         if (tmpc == NULL)
367                 return 0;
368
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))
373                         return freq;
374         }
375         return 0;
376 }
377
378
379 void p2p_channels_dump(struct p2p_data *p2p, const char *title,
380                        const struct p2p_channels *chan)
381 {
382         char buf[500], *pos, *end;
383         size_t i, j;
384         int ret;
385
386         pos = buf;
387         end = pos + sizeof(buf);
388
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))
394                         break;
395                 pos += ret;
396
397                 for (j = 0; j < c->channels; j++) {
398                         ret = os_snprintf(pos, end - pos, "%s%u",
399                                           j == 0 ? "" : ",",
400                                           c->channel[j]);
401                         if (os_snprintf_error(end - pos, ret))
402                                 break;
403                         pos += ret;
404                 }
405         }
406         *pos = '\0';
407
408         p2p_dbg(p2p, "%s:%s", title, buf);
409 }
410
411
412 static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
413 {
414         unsigned int r;
415         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
416                 r = 0;
417         r %= num_channels;
418         return channels[r];
419 }
420
421
422 int p2p_channel_select(struct p2p_channels *chans, const int *classes,
423                        u8 *op_class, u8 *op_channel)
424 {
425         unsigned int i, j;
426
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];
430
431                         if (c->channels == 0)
432                                 continue;
433
434                         if (classes == NULL || c->reg_class == classes[j]) {
435                                 /*
436                                  * Pick one of the available channels in the
437                                  * operating class at random.
438                                  */
439                                 *op_class = c->reg_class;
440                                 *op_channel = p2p_channel_pick_random(
441                                         c->channel, c->channels);
442                                 return 0;
443                         }
444                 }
445                 if (classes == NULL)
446                         break;
447         }
448
449         return -1;
450 }
451
452
453 int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
454                               u8 *op_channel)
455 {
456         u8 chan[4];
457         unsigned int num_channels = 0;
458
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;
466
467         /* Try to find available social channels from 60 GHz */
468         if (p2p_channels_includes(chans, 180, 2))
469                 chan[num_channels++] = 2;
470
471         if (num_channels == 0)
472                 return -1;
473
474         *op_channel = p2p_channel_pick_random(chan, num_channels);
475         if (*op_channel == 2)
476                 *op_class = 180;
477         else
478                 *op_class = 81;
479
480         return 0;
481 }
482
483
484 int p2p_channels_to_freqs(const struct p2p_channels *channels, int *freq_list,
485                           unsigned int max_len)
486 {
487         unsigned int i, idx;
488
489         if (!channels || max_len == 0)
490                 return 0;
491
492         for (i = 0, idx = 0; i < channels->reg_classes; i++) {
493                 const struct p2p_reg_class *c = &channels->reg_class[i];
494                 unsigned int j;
495
496                 if (idx + 1 == max_len)
497                         break;
498                 for (j = 0; j < c->channels; j++) {
499                         int freq;
500                         if (idx + 1 == max_len)
501                                 break;
502                         freq = p2p_channel_to_freq(c->reg_class,
503                                                    c->channel[j]);
504                         if (freq < 0)
505                                 continue;
506                         freq_list[idx++] = freq;
507                 }
508         }
509
510         freq_list[idx] = 0;
511
512         return idx;
513 }