Coverage Report

Created: 2025-04-24 06:18

/src/hostap/src/p2p/p2p_utils.c
Line
Count
Source (jump to first uncovered line)
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/defs.h"
13
#include "common/ieee802_11_common.h"
14
#include "p2p_i.h"
15
16
17
/**
18
 * p2p_random - Generate random string for SSID and passphrase
19
 * @buf: Buffer for returning the result
20
 * @len: Number of octets to write to the buffer
21
 * Returns: 0 on success, -1 on failure
22
 *
23
 * This function generates a random string using the following character set:
24
 * 'A'-'Z', 'a'-'z', '0'-'9'.
25
 */
26
int p2p_random(char *buf, size_t len)
27
0
{
28
0
  u8 val;
29
0
  size_t i;
30
0
  u8 letters = 'Z' - 'A' + 1;
31
0
  u8 numbers = 10;
32
33
0
  if (os_get_random((unsigned char *) buf, len))
34
0
    return -1;
35
  /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */
36
0
  for (i = 0; i < len; i++) {
37
0
    val = buf[i];
38
0
    val %= 2 * letters + numbers;
39
0
    if (val < letters)
40
0
      buf[i] = 'A' + val;
41
0
    else if (val < 2 * letters)
42
0
      buf[i] = 'a' + (val - letters);
43
0
    else
44
0
      buf[i] = '0' + (val - 2 * letters);
45
0
  }
46
47
0
  return 0;
48
0
}
49
50
51
/**
52
 * p2p_channel_to_freq - Convert channel info to frequency
53
 * @op_class: Operating class
54
 * @channel: Channel number
55
 * Returns: Frequency in MHz or -1 if the specified channel is unknown
56
 */
57
int p2p_channel_to_freq(int op_class, int channel)
58
567
{
59
567
  return ieee80211_chan_to_freq(NULL, op_class, channel);
60
567
}
61
62
63
/**
64
 * p2p_freq_to_channel - Convert frequency into channel info
65
 * @op_class: Buffer for returning operating class
66
 * @channel: Buffer for returning channel number
67
 * Returns: 0 on success, -1 if the specified frequency is unknown
68
 */
69
int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel)
70
0
{
71
0
  if (ieee80211_freq_to_channel_ext(freq, 0, 0, op_class, channel) ==
72
0
      NUM_HOSTAPD_MODES)
73
0
    return -1;
74
75
0
  return 0;
76
0
}
77
78
79
static void p2p_reg_class_intersect(const struct p2p_reg_class *a,
80
            const struct p2p_reg_class *b,
81
            struct p2p_reg_class *res)
82
509
{
83
509
  size_t i, j;
84
85
509
  res->reg_class = a->reg_class;
86
87
1.51k
  for (i = 0; i < a->channels; i++) {
88
9.92k
    for (j = 0; j < b->channels; j++) {
89
8.91k
      if (a->channel[i] != b->channel[j])
90
6.59k
        continue;
91
2.32k
      res->channel[res->channels] = a->channel[i];
92
2.32k
      res->channels++;
93
2.32k
      if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
94
6
        return;
95
2.32k
    }
96
1.01k
  }
97
509
}
98
99
100
/**
101
 * p2p_channels_intersect - Intersection of supported channel lists
102
 * @a: First set of supported channels
103
 * @b: Second set of supported channels
104
 * @res: Data structure for returning the intersection of support channels
105
 *
106
 * This function can be used to find a common set of supported channels. Both
107
 * input channels sets are assumed to use the same country code. If different
108
 * country codes are used, the regulatory class numbers may not be matched
109
 * correctly and results are undefined.
110
 */
111
void p2p_channels_intersect(const struct p2p_channels *a,
112
          const struct p2p_channels *b,
113
          struct p2p_channels *res)
114
295
{
115
295
  size_t i, j;
116
117
295
  os_memset(res, 0, sizeof(*res));
118
119
590
  for (i = 0; i < a->reg_classes; i++) {
120
295
    const struct p2p_reg_class *a_reg = &a->reg_class[i];
121
1.25k
    for (j = 0; j < b->reg_classes; j++) {
122
956
      const struct p2p_reg_class *b_reg = &b->reg_class[j];
123
956
      if (a_reg->reg_class != b_reg->reg_class)
124
447
        continue;
125
509
      p2p_reg_class_intersect(
126
509
        a_reg, b_reg,
127
509
        &res->reg_class[res->reg_classes]);
128
509
      if (res->reg_class[res->reg_classes].channels) {
129
414
        res->reg_classes++;
130
414
        if (res->reg_classes == P2P_MAX_REG_CLASSES)
131
0
          return;
132
414
      }
133
509
    }
134
295
  }
135
295
}
136
137
138
static void p2p_op_class_union(struct p2p_reg_class *cl,
139
             const struct p2p_reg_class *b_cl)
140
0
{
141
0
  size_t i, j;
142
143
0
  for (i = 0; i < b_cl->channels; i++) {
144
0
    for (j = 0; j < cl->channels; j++) {
145
0
      if (b_cl->channel[i] == cl->channel[j])
146
0
        break;
147
0
    }
148
0
    if (j == cl->channels) {
149
0
      if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
150
0
        return;
151
0
      cl->channel[cl->channels++] = b_cl->channel[i];
152
0
    }
153
0
  }
154
0
}
155
156
157
/**
158
 * p2p_channels_union_inplace - Inplace union of channel lists
159
 * @res: Input data and place for returning union of the channel sets
160
 * @b: Second set of channels
161
 */
162
void p2p_channels_union_inplace(struct p2p_channels *res,
163
        const struct p2p_channels *b)
164
181
{
165
181
  size_t i, j;
166
167
362
  for (i = 0; i < res->reg_classes; i++) {
168
181
    struct p2p_reg_class *cl = &res->reg_class[i];
169
181
    for (j = 0; j < b->reg_classes; j++) {
170
0
      const struct p2p_reg_class *b_cl = &b->reg_class[j];
171
0
      if (cl->reg_class != b_cl->reg_class)
172
0
        continue;
173
0
      p2p_op_class_union(cl, b_cl);
174
0
    }
175
181
  }
176
177
181
  for (j = 0; j < b->reg_classes; j++) {
178
0
    const struct p2p_reg_class *b_cl = &b->reg_class[j];
179
180
0
    for (i = 0; i < res->reg_classes; i++) {
181
0
      struct p2p_reg_class *cl = &res->reg_class[i];
182
0
      if (cl->reg_class == b_cl->reg_class)
183
0
        break;
184
0
    }
185
186
0
    if (i == res->reg_classes) {
187
0
      if (res->reg_classes == P2P_MAX_REG_CLASSES)
188
0
        return;
189
0
      os_memcpy(&res->reg_class[res->reg_classes++],
190
0
          b_cl, sizeof(struct p2p_reg_class));
191
0
    }
192
0
  }
193
181
}
194
195
196
/**
197
 * p2p_channels_union - Union of channel lists
198
 * @a: First set of channels
199
 * @b: Second set of channels
200
 * @res: Data structure for returning the union of channels
201
 */
202
void p2p_channels_union(const struct p2p_channels *a,
203
      const struct p2p_channels *b,
204
      struct p2p_channels *res)
205
181
{
206
181
  os_memcpy(res, a, sizeof(*res));
207
181
  p2p_channels_union_inplace(res, b);
208
181
}
209
210
211
void p2p_channels_remove_freqs(struct p2p_channels *chan,
212
             const struct wpa_freq_range_list *list)
213
0
{
214
0
  size_t o, c;
215
216
0
  if (list == NULL)
217
0
    return;
218
219
0
  o = 0;
220
0
  while (o < chan->reg_classes) {
221
0
    struct p2p_reg_class *op = &chan->reg_class[o];
222
223
0
    c = 0;
224
0
    while (c < op->channels) {
225
0
      int freq = p2p_channel_to_freq(op->reg_class,
226
0
                   op->channel[c]);
227
0
      if (freq > 0 && freq_range_list_includes(list, freq)) {
228
0
        op->channels--;
229
0
        os_memmove(&op->channel[c],
230
0
             &op->channel[c + 1],
231
0
             op->channels - c);
232
0
      } else
233
0
        c++;
234
0
    }
235
236
0
    if (op->channels == 0) {
237
0
      chan->reg_classes--;
238
0
      os_memmove(&chan->reg_class[o], &chan->reg_class[o + 1],
239
0
           (chan->reg_classes - o) *
240
0
           sizeof(struct p2p_reg_class));
241
0
    } else
242
0
      o++;
243
0
  }
244
0
}
245
246
247
/**
248
 * p2p_channels_includes - Check whether a channel is included in the list
249
 * @channels: List of supported channels
250
 * @reg_class: Regulatory class of the channel to search
251
 * @channel: Channel number of the channel to search
252
 * Returns: 1 if channel was found or 0 if not
253
 */
254
int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
255
        u8 channel)
256
51
{
257
51
  size_t i, j;
258
136
  for (i = 0; i < channels->reg_classes; i++) {
259
88
    const struct p2p_reg_class *reg = &channels->reg_class[i];
260
88
    if (reg->reg_class != reg_class)
261
55
      continue;
262
181
    for (j = 0; j < reg->channels; j++) {
263
151
      if (reg->channel[j] == channel)
264
3
        return 1;
265
151
    }
266
33
  }
267
48
  return 0;
268
51
}
269
270
271
int p2p_channels_includes_freq(const struct p2p_channels *channels,
272
             unsigned int freq)
273
0
{
274
0
  size_t i, j;
275
0
  for (i = 0; i < channels->reg_classes; i++) {
276
0
    const struct p2p_reg_class *reg = &channels->reg_class[i];
277
0
    for (j = 0; j < reg->channels; j++) {
278
0
      if (p2p_channel_to_freq(reg->reg_class,
279
0
            reg->channel[j]) == (int) freq)
280
0
        return 1;
281
0
    }
282
0
  }
283
0
  return 0;
284
0
}
285
286
287
int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
288
0
{
289
0
  u8 op_reg_class, op_channel;
290
0
  if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
291
0
    return 0;
292
0
  return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
293
0
             op_channel);
294
0
}
295
296
297
int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
298
0
{
299
0
  u8 op_reg_class, op_channel;
300
0
  if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
301
0
    return 0;
302
0
  return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
303
0
             op_channel) &&
304
0
    !freq_range_list_includes(&p2p->no_go_freq, freq);
305
0
}
306
307
308
int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
309
0
{
310
0
  u8 op_reg_class, op_channel;
311
0
  if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
312
0
    return 0;
313
0
  return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
314
0
             op_channel) ||
315
0
    p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
316
0
              op_channel);
317
0
}
318
319
320
unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
321
             const struct p2p_channels *channels)
322
0
{
323
0
  unsigned int i;
324
0
  int freq = 0;
325
0
  const struct p2p_channels *tmpc = channels ?
326
0
    channels : &p2p->cfg->channels;
327
328
0
  if (tmpc == NULL)
329
0
    return 0;
330
331
0
  for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
332
0
    freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class,
333
0
             p2p->cfg->pref_chan[i].chan);
334
0
    if (p2p_channels_includes_freq(tmpc, freq))
335
0
      return freq;
336
0
  }
337
0
  return 0;
338
0
}
339
340
341
void p2p_channels_dump(struct p2p_data *p2p, const char *title,
342
           const struct p2p_channels *chan)
343
13.8k
{
344
13.8k
  char buf[500], *pos, *end;
345
13.8k
  size_t i, j;
346
13.8k
  int ret;
347
348
13.8k
  pos = buf;
349
13.8k
  end = pos + sizeof(buf);
350
351
21.3k
  for (i = 0; i < chan->reg_classes; i++) {
352
7.57k
    const struct p2p_reg_class *c;
353
7.57k
    c = &chan->reg_class[i];
354
7.57k
    ret = os_snprintf(pos, end - pos, " %u:", c->reg_class);
355
7.57k
    if (os_snprintf_error(end - pos, ret))
356
3
      break;
357
7.57k
    pos += ret;
358
359
25.6k
    for (j = 0; j < c->channels; j++) {
360
18.1k
      ret = os_snprintf(pos, end - pos, "%s%u",
361
18.1k
            j == 0 ? "" : ",",
362
18.1k
            c->channel[j]);
363
18.1k
      if (os_snprintf_error(end - pos, ret))
364
4
        break;
365
18.1k
      pos += ret;
366
18.1k
    }
367
7.57k
  }
368
13.8k
  *pos = '\0';
369
370
13.8k
  p2p_dbg(p2p, "%s:%s", title, buf);
371
13.8k
}
372
373
374
static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
375
0
{
376
0
  unsigned int r;
377
0
  if (os_get_random((u8 *) &r, sizeof(r)) < 0)
378
0
    r = 0;
379
0
  r %= num_channels;
380
0
  return channels[r];
381
0
}
382
383
384
int p2p_channel_select(struct p2p_channels *chans, const int *classes,
385
           u8 *op_class, u8 *op_channel)
386
0
{
387
0
  unsigned int i, j;
388
389
0
  for (j = 0; classes == NULL || classes[j]; j++) {
390
0
    for (i = 0; i < chans->reg_classes; i++) {
391
0
      struct p2p_reg_class *c = &chans->reg_class[i];
392
393
0
      if (c->channels == 0)
394
0
        continue;
395
396
0
      if (classes == NULL || c->reg_class == classes[j]) {
397
        /*
398
         * Pick one of the available channels in the
399
         * operating class at random.
400
         */
401
0
        *op_class = c->reg_class;
402
0
        *op_channel = p2p_channel_pick_random(
403
0
          c->channel, c->channels);
404
0
        return 0;
405
0
      }
406
0
    }
407
0
    if (classes == NULL)
408
0
      break;
409
0
  }
410
411
0
  return -1;
412
0
}
413
414
415
int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
416
            u8 *op_channel,
417
            struct wpa_freq_range_list *avoid_list,
418
            struct wpa_freq_range_list *disallow_list)
419
0
{
420
0
  u8 chan[4];
421
0
  unsigned int num_channels = 0;
422
423
  /* Try to find available social channels from 2.4 GHz.
424
   * If the avoid_list includes any of the 2.4 GHz social channels, that
425
   * channel is not allowed by p2p_channels_includes() rules. However, it
426
   * is assumed to allow minimal traffic for P2P negotiation, so allow it
427
   * here for social channel selection unless explicitly disallowed in the
428
   * disallow_list. */
429
0
  if (p2p_channels_includes(chans, 81, 1) ||
430
0
      (freq_range_list_includes(avoid_list, 2412) &&
431
0
       !freq_range_list_includes(disallow_list, 2412)))
432
0
    chan[num_channels++] = 1;
433
0
  if (p2p_channels_includes(chans, 81, 6) ||
434
0
      (freq_range_list_includes(avoid_list, 2437) &&
435
0
       !freq_range_list_includes(disallow_list, 2437)))
436
0
    chan[num_channels++] = 6;
437
0
  if (p2p_channels_includes(chans, 81, 11) ||
438
0
      (freq_range_list_includes(avoid_list, 2462) &&
439
0
       !freq_range_list_includes(disallow_list, 2462)))
440
0
    chan[num_channels++] = 11;
441
442
  /* Try to find available social channels from 60 GHz */
443
0
  if (p2p_channels_includes(chans, 180, 2))
444
0
    chan[num_channels++] = 2;
445
446
0
  if (num_channels == 0)
447
0
    return -1;
448
449
0
  *op_channel = p2p_channel_pick_random(chan, num_channels);
450
0
  if (*op_channel == 2)
451
0
    *op_class = 180;
452
0
  else
453
0
    *op_class = 81;
454
455
0
  return 0;
456
0
}
457
458
459
int p2p_channels_to_freqs(const struct p2p_channels *channels, int *freq_list,
460
        unsigned int max_len)
461
0
{
462
0
  unsigned int i, idx;
463
464
0
  if (!channels || max_len == 0)
465
0
    return 0;
466
467
0
  for (i = 0, idx = 0; i < channels->reg_classes; i++) {
468
0
    const struct p2p_reg_class *c = &channels->reg_class[i];
469
0
    unsigned int j;
470
471
0
    if (idx + 1 == max_len)
472
0
      break;
473
0
    for (j = 0; j < c->channels; j++) {
474
0
      int freq;
475
0
      unsigned int k;
476
477
0
      if (idx + 1 == max_len)
478
0
        break;
479
0
      freq = p2p_channel_to_freq(c->reg_class,
480
0
               c->channel[j]);
481
0
      if (freq < 0)
482
0
        continue;
483
484
0
      for (k = 0; k < idx; k++) {
485
0
        if (freq_list[k] == freq)
486
0
          break;
487
0
      }
488
489
0
      if (k < idx)
490
0
        continue;
491
0
      freq_list[idx++] = freq;
492
0
    }
493
0
  }
494
495
0
  freq_list[idx] = 0;
496
497
0
  return idx;
498
0
}
499
500
501
void p2p_copy_channels(struct p2p_channels *dst,
502
           const struct p2p_channels *src, bool allow_6ghz)
503
57
{
504
57
  size_t i, j;
505
506
57
  if (allow_6ghz) {
507
0
    os_memcpy(dst, src, sizeof(struct p2p_channels));
508
0
    return;
509
0
  }
510
511
57
  for (i = 0, j = 0; i < src->reg_classes; i++) {
512
0
    if (is_6ghz_op_class(src->reg_class[i].reg_class))
513
0
      continue;
514
0
    os_memcpy(&dst->reg_class[j], &src->reg_class[i],
515
0
        sizeof(struct p2p_reg_class));
516
0
    j++;
517
0
  }
518
57
  dst->reg_classes = j;
519
57
}
520
521
522
int p2p_remove_6ghz_channels(struct weighted_pcl *pref_freq_list, int size)
523
0
{
524
0
  int i;
525
526
0
  for (i = 0; i < size; i++) {
527
0
    if (is_6ghz_freq(pref_freq_list[i].freq)) {
528
0
      wpa_printf(MSG_DEBUG, "P2P: Remove 6 GHz channel %d",
529
0
           pref_freq_list[i].freq);
530
0
      size--;
531
0
      os_memmove(&pref_freq_list[i], &pref_freq_list[i + 1],
532
0
           (size - i) * sizeof(pref_freq_list[0]));
533
0
      i--;
534
0
    }
535
0
  }
536
0
  return i;
537
0
}
538
539
540
/**
541
 * p2p_pref_freq_allowed - Based on the flags set, check if the preferred
542
 * frequency is allowed
543
 * @freq_list: Weighted preferred channel list
544
 * @go: Whether the local device is the group owner
545
 * Returns: Whether the preferred frequency is allowed
546
 */
547
bool p2p_pref_freq_allowed(const struct weighted_pcl *freq_list, bool go)
548
0
{
549
0
  if (freq_list->flag & WEIGHTED_PCL_EXCLUDE)
550
0
    return false;
551
0
  if (!(freq_list->flag & WEIGHTED_PCL_CLI) && !go)
552
0
    return false;
553
0
  if (!(freq_list->flag & WEIGHTED_PCL_GO) && go)
554
0
    return false;
555
0
  return true;
556
0
}
557
558
559
static int p2p_check_pref_channel(int channel, u8 op_class,
560
          const struct weighted_pcl *freq_list,
561
          unsigned int num_channels, bool go)
562
0
{
563
0
  unsigned int i;
564
565
  /* If the channel is present in the preferred channel list, check if it
566
   * has appropriate flags for the role.
567
   */
568
0
  for (i = 0; i < num_channels; i++) {
569
0
    if (p2p_channel_to_freq(op_class, channel) !=
570
0
        (int) freq_list[i].freq)
571
0
      continue;
572
0
    if (!p2p_pref_freq_allowed(&freq_list[i], go))
573
0
      return -1;
574
0
    break;
575
0
  }
576
577
0
  return 0;
578
0
}
579
580
581
void p2p_pref_channel_filter(const struct p2p_channels *p2p_chan,
582
           const struct weighted_pcl *freq_list,
583
           unsigned int num_channels,
584
           struct p2p_channels *res, bool go)
585
0
{
586
0
  size_t i, j;
587
588
0
  os_memset(res, 0, sizeof(*res));
589
590
0
  for (i = 0; i < p2p_chan->reg_classes; i++) {
591
0
    const struct p2p_reg_class *reg = &p2p_chan->reg_class[i];
592
0
    struct p2p_reg_class *res_reg = &res->reg_class[i];
593
594
0
    if (num_channels > 0) {
595
0
      for (j = 0; j < reg->channels; j++) {
596
0
        if (p2p_check_pref_channel(reg->channel[j],
597
0
                 reg->reg_class,
598
0
                 freq_list,
599
0
                 num_channels,
600
0
                 go) < 0)
601
0
          continue;
602
603
0
        res_reg->channel[res_reg->channels++] =
604
0
          reg->channel[j];
605
0
      }
606
0
    }
607
608
0
    if (res_reg->channels == 0)
609
0
      continue;
610
0
    res->reg_classes++;
611
0
    res_reg->reg_class = reg->reg_class;
612
0
  }
613
0
}