LCOV - code coverage report
Current view: top level - src/p2p - p2p_utils.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401264779 Lines: 181 228 79.4 %
Date: 2014-05-28 Functions: 18 18 100.0 %

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

Generated by: LCOV version 1.10