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 1426431149 Lines: 214 235 91.1 %
Date: 2015-03-15 Functions: 20 20 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 "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         545 : int p2p_random(char *buf, size_t len)
      26             : {
      27             :         u8 val;
      28             :         size_t i;
      29         545 :         u8 letters = 'Z' - 'A' + 1;
      30         545 :         u8 numbers = 10;
      31             : 
      32         545 :         if (os_get_random((unsigned char *) buf, len))
      33           0 :                 return -1;
      34             :         /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */
      35        3248 :         for (i = 0; i < len; i++) {
      36        2703 :                 val = buf[i];
      37        2703 :                 val %= 2 * letters + numbers;
      38        2703 :                 if (val < letters)
      39        1245 :                         buf[i] = 'A' + val;
      40        1458 :                 else if (val < 2 * letters)
      41        1032 :                         buf[i] = 'a' + (val - letters);
      42             :                 else
      43         426 :                         buf[i] = '0' + (val - 2 * letters);
      44             :         }
      45             : 
      46         545 :         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        9212 : int p2p_channel_to_freq(int op_class, int channel)
      57             : {
      58        9212 :         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        1418 : int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel)
      69             : {
      70             :         /* TODO: more operating classes */
      71        1418 :         if (freq >= 2412 && freq <= 2472) {
      72         658 :                 if ((freq - 2407) % 5)
      73           0 :                         return -1;
      74             : 
      75         658 :                 *op_class = 81; /* 2.407 GHz, channels 1..13 */
      76         658 :                 *channel = (freq - 2407) / 5;
      77         658 :                 return 0;
      78             :         }
      79             : 
      80         760 :         if (freq == 2484) {
      81           0 :                 *op_class = 82; /* channel 14 */
      82           0 :                 *channel = 14;
      83           0 :                 return 0;
      84             :         }
      85             : 
      86         760 :         if (freq >= 5180 && freq <= 5240) {
      87          15 :                 if ((freq - 5000) % 5)
      88           0 :                         return -1;
      89             : 
      90          15 :                 *op_class = 115; /* 5 GHz, channels 36..48 */
      91          15 :                 *channel = (freq - 5000) / 5;
      92          15 :                 return 0;
      93             :         }
      94             : 
      95         745 :         if (freq >= 5745 && freq <= 5805) {
      96           1 :                 if ((freq - 5000) % 5)
      97           0 :                         return -1;
      98             : 
      99           1 :                 *op_class = 124; /* 5 GHz, channels 149..161 */
     100           1 :                 *channel = (freq - 5000) / 5;
     101           1 :                 return 0;
     102             :         }
     103             : 
     104         744 :         if (freq >= 58320 && freq <= 64800) {
     105         742 :                 if ((freq - 58320) % 2160)
     106           0 :                         return -1;
     107             : 
     108         742 :                 *op_class = 180; /* 60 GHz, channels 1..4 */
     109         742 :                 *channel = (freq - 56160) / 2160;
     110         742 :                 return 0;
     111             :         }
     112             : 
     113           2 :         return -1;
     114             : }
     115             : 
     116             : 
     117        1112 : 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        1112 :         res->reg_class = a->reg_class;
     124             : 
     125       10424 :         for (i = 0; i < a->channels; i++) {
     126       97936 :                 for (j = 0; j < b->channels; j++) {
     127       88624 :                         if (a->channel[i] != b->channel[j])
     128       80172 :                                 continue;
     129        8452 :                         res->channel[res->channels] = a->channel[i];
     130        8452 :                         res->channels++;
     131        8452 :                         if (res->channels == P2P_MAX_REG_CLASS_CHANNELS)
     132        1112 :                                 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         988 : 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         988 :         os_memset(res, 0, sizeof(*res));
     156             : 
     157        2236 :         for (i = 0; i < a->reg_classes; i++) {
     158        1248 :                 const struct p2p_reg_class *a_reg = &a->reg_class[i];
     159        4012 :                 for (j = 0; j < b->reg_classes; j++) {
     160        2764 :                         const struct p2p_reg_class *b_reg = &b->reg_class[j];
     161        2764 :                         if (a_reg->reg_class != b_reg->reg_class)
     162        1652 :                                 continue;
     163        1112 :                         p2p_reg_class_intersect(
     164             :                                 a_reg, b_reg,
     165        1112 :                                 &res->reg_class[res->reg_classes]);
     166        1112 :                         if (res->reg_class[res->reg_classes].channels) {
     167        1109 :                                 res->reg_classes++;
     168        1109 :                                 if (res->reg_classes == P2P_MAX_REG_CLASSES)
     169         988 :                                         return;
     170             :                         }
     171             :                 }
     172             :         }
     173             : }
     174             : 
     175             : 
     176          10 : 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          30 :         for (i = 0; i < b_cl->channels; i++) {
     182         250 :                 for (j = 0; j < cl->channels; j++) {
     183         230 :                         if (b_cl->channel[i] == cl->channel[j])
     184           0 :                                 break;
     185             :                 }
     186          20 :                 if (j == cl->channels) {
     187          20 :                         if (cl->channels == P2P_MAX_REG_CLASS_CHANNELS)
     188          10 :                                 return;
     189          20 :                         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         201 : void p2p_channels_union_inplace(struct p2p_channels *res,
     201             :                                 const struct p2p_channels *b)
     202             : {
     203             :         size_t i, j;
     204             : 
     205         451 :         for (i = 0; i < res->reg_classes; i++) {
     206         250 :                 struct p2p_reg_class *cl = &res->reg_class[i];
     207         330 :                 for (j = 0; j < b->reg_classes; j++) {
     208          80 :                         const struct p2p_reg_class *b_cl = &b->reg_class[j];
     209          80 :                         if (cl->reg_class != b_cl->reg_class)
     210          70 :                                 continue;
     211          10 :                         p2p_op_class_union(cl, b_cl);
     212             :                 }
     213             :         }
     214             : 
     215         281 :         for (j = 0; j < b->reg_classes; j++) {
     216          80 :                 const struct p2p_reg_class *b_cl = &b->reg_class[j];
     217             : 
     218         360 :                 for (i = 0; i < res->reg_classes; i++) {
     219         290 :                         struct p2p_reg_class *cl = &res->reg_class[i];
     220         290 :                         if (cl->reg_class == b_cl->reg_class)
     221          10 :                                 break;
     222             :                 }
     223             : 
     224          80 :                 if (i == res->reg_classes) {
     225          70 :                         if (res->reg_classes == P2P_MAX_REG_CLASSES)
     226         201 :                                 return;
     227          70 :                         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          48 : void p2p_channels_union(const struct p2p_channels *a,
     241             :                         const struct p2p_channels *b,
     242             :                         struct p2p_channels *res)
     243             : {
     244          48 :         os_memcpy(res, a, sizeof(*res));
     245          48 :         p2p_channels_union_inplace(res, b);
     246          48 : }
     247             : 
     248             : 
     249         335 : 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         335 :         if (list == NULL)
     255         335 :                 return;
     256             : 
     257         335 :         o = 0;
     258        1051 :         while (o < chan->reg_classes) {
     259         381 :                 struct p2p_reg_class *op = &chan->reg_class[o];
     260             : 
     261         381 :                 c = 0;
     262        3721 :                 while (c < op->channels) {
     263        2959 :                         int freq = p2p_channel_to_freq(op->reg_class,
     264        2959 :                                                        op->channel[c]);
     265        2959 :                         if (freq > 0 && freq_range_list_includes(list, freq)) {
     266          13 :                                 op->channels--;
     267          13 :                                 os_memmove(&op->channel[c],
     268             :                                            &op->channel[c + 1],
     269             :                                            op->channels - c);
     270             :                         } else
     271        2946 :                                 c++;
     272             :                 }
     273             : 
     274         381 :                 if (op->channels == 0) {
     275           3 :                         chan->reg_classes--;
     276           3 :                         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         378 :                         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        2581 : int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class,
     293             :                           u8 channel)
     294             : {
     295             :         size_t i, j;
     296        3648 :         for (i = 0; i < channels->reg_classes; i++) {
     297        2697 :                 const struct p2p_reg_class *reg = &channels->reg_class[i];
     298        2697 :                 if (reg->reg_class != reg_class)
     299        1038 :                         continue;
     300        7223 :                 for (j = 0; j < reg->channels; j++) {
     301        7194 :                         if (reg->channel[j] == channel)
     302        1630 :                                 return 1;
     303             :                 }
     304             :         }
     305         951 :         return 0;
     306             : }
     307             : 
     308             : 
     309         272 : int p2p_channels_includes_freq(const struct p2p_channels *channels,
     310             :                                unsigned int freq)
     311             : {
     312             :         size_t i, j;
     313         319 :         for (i = 0; i < channels->reg_classes; i++) {
     314         288 :                 const struct p2p_reg_class *reg = &channels->reg_class[i];
     315        1450 :                 for (j = 0; j < reg->channels; j++) {
     316        2806 :                         if (p2p_channel_to_freq(reg->reg_class,
     317        2806 :                                                 reg->channel[j]) == (int) freq)
     318         241 :                                 return 1;
     319             :                 }
     320             :         }
     321          31 :         return 0;
     322             : }
     323             : 
     324             : 
     325        1005 : int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq)
     326             : {
     327             :         u8 op_reg_class, op_channel;
     328        1005 :         if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
     329           1 :                 return 0;
     330        1004 :         return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
     331             :                                      op_channel);
     332             : }
     333             : 
     334             : 
     335         313 : int p2p_supported_freq_go(struct p2p_data *p2p, unsigned int freq)
     336             : {
     337             :         u8 op_reg_class, op_channel;
     338         313 :         if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
     339           0 :                 return 0;
     340         626 :         return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
     341         626 :                                      op_channel) &&
     342         313 :                 !freq_range_list_includes(&p2p->no_go_freq, freq);
     343             : }
     344             : 
     345             : 
     346          31 : int p2p_supported_freq_cli(struct p2p_data *p2p, unsigned int freq)
     347             : {
     348             :         u8 op_reg_class, op_channel;
     349          31 :         if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0)
     350           1 :                 return 0;
     351          60 :         return p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
     352          31 :                                      op_channel) ||
     353           1 :                 p2p_channels_includes(&p2p->cfg->cli_channels, op_reg_class,
     354             :                                       op_channel);
     355             : }
     356             : 
     357             : 
     358          50 : unsigned int p2p_get_pref_freq(struct p2p_data *p2p,
     359             :                                const struct p2p_channels *channels)
     360             : {
     361             :         unsigned int i;
     362          50 :         int freq = 0;
     363          50 :         const struct p2p_channels *tmpc = channels ?
     364          50 :                 channels : &p2p->cfg->channels;
     365             : 
     366          50 :         if (tmpc == NULL)
     367           0 :                 return 0;
     368             : 
     369          76 :         for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
     370          29 :                 freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class,
     371          29 :                                            p2p->cfg->pref_chan[i].chan);
     372          29 :                 if (p2p_channels_includes_freq(tmpc, freq))
     373           3 :                         return freq;
     374             :         }
     375          47 :         return 0;
     376             : }
     377             : 
     378             : 
     379       16752 : 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       16752 :         pos = buf;
     387       16752 :         end = pos + sizeof(buf);
     388             : 
     389       28486 :         for (i = 0; i < chan->reg_classes; i++) {
     390             :                 const struct p2p_reg_class *c;
     391       11734 :                 c = &chan->reg_class[i];
     392       11734 :                 ret = os_snprintf(pos, end - pos, " %u:", c->reg_class);
     393       11734 :                 if (os_snprintf_error(end - pos, ret))
     394           0 :                         break;
     395       11734 :                 pos += ret;
     396             : 
     397      117613 :                 for (j = 0; j < c->channels; j++) {
     398      105879 :                         ret = os_snprintf(pos, end - pos, "%s%u",
     399             :                                           j == 0 ? "" : ",",
     400      105879 :                                           c->channel[j]);
     401      105879 :                         if (os_snprintf_error(end - pos, ret))
     402           0 :                                 break;
     403      105879 :                         pos += ret;
     404             :                 }
     405             :         }
     406       16752 :         *pos = '\0';
     407             : 
     408       16752 :         p2p_dbg(p2p, "%s:%s", title, buf);
     409       16752 : }
     410             : 
     411             : 
     412         190 : static u8 p2p_channel_pick_random(const u8 *channels, unsigned int num_channels)
     413             : {
     414             :         unsigned int r;
     415         190 :         if (os_get_random((u8 *) &r, sizeof(r)) < 0)
     416           0 :                 r = 0;
     417         190 :         r %= num_channels;
     418         190 :         return channels[r];
     419             : }
     420             : 
     421             : 
     422         953 : 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        3139 :         for (j = 0; classes == NULL || classes[j]; j++) {
     428        4474 :                 for (i = 0; i < chans->reg_classes; i++) {
     429        2288 :                         struct p2p_reg_class *c = &chans->reg_class[i];
     430             : 
     431        2288 :                         if (c->channels == 0)
     432           0 :                                 continue;
     433             : 
     434        2288 :                         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          15 :                                 *op_class = c->reg_class;
     440          30 :                                 *op_channel = p2p_channel_pick_random(
     441          30 :                                         c->channel, c->channels);
     442          15 :                                 return 0;
     443             :                         }
     444             :                 }
     445        2186 :                 if (classes == NULL)
     446           0 :                         break;
     447             :         }
     448             : 
     449         938 :         return -1;
     450             : }
     451             : 
     452             : 
     453         177 : int p2p_channel_random_social(struct p2p_channels *chans, u8 *op_class,
     454             :                               u8 *op_channel)
     455             : {
     456             :         u8 chan[4];
     457         177 :         unsigned int num_channels = 0;
     458             : 
     459             :         /* Try to find available social channels from 2.4 GHz */
     460         177 :         if (p2p_channels_includes(chans, 81, 1))
     461         173 :                 chan[num_channels++] = 1;
     462         177 :         if (p2p_channels_includes(chans, 81, 6))
     463         175 :                 chan[num_channels++] = 6;
     464         177 :         if (p2p_channels_includes(chans, 81, 11))
     465         175 :                 chan[num_channels++] = 11;
     466             : 
     467             :         /* Try to find available social channels from 60 GHz */
     468         177 :         if (p2p_channels_includes(chans, 180, 2))
     469           0 :                 chan[num_channels++] = 2;
     470             : 
     471         177 :         if (num_channels == 0)
     472           2 :                 return -1;
     473             : 
     474         175 :         *op_channel = p2p_channel_pick_random(chan, num_channels);
     475         175 :         if (*op_channel == 2)
     476           0 :                 *op_class = 180;
     477             :         else
     478         175 :                 *op_class = 81;
     479             : 
     480         175 :         return 0;
     481             : }
     482             : 
     483             : 
     484         237 : 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         237 :         if (!channels || max_len == 0)
     490          18 :                 return 0;
     491             : 
     492         486 :         for (i = 0, idx = 0; i < channels->reg_classes; i++) {
     493         267 :                 const struct p2p_reg_class *c = &channels->reg_class[i];
     494             :                 unsigned int j;
     495             : 
     496         267 :                 if (idx + 1 == max_len)
     497           0 :                         break;
     498        2328 :                 for (j = 0; j < c->channels; j++) {
     499             :                         int freq;
     500        2061 :                         if (idx + 1 == max_len)
     501           0 :                                 break;
     502        2061 :                         freq = p2p_channel_to_freq(c->reg_class,
     503        2061 :                                                    c->channel[j]);
     504        2061 :                         if (freq < 0)
     505           0 :                                 continue;
     506        2061 :                         freq_list[idx++] = freq;
     507             :                 }
     508             :         }
     509             : 
     510         219 :         freq_list[idx] = 0;
     511             : 
     512         219 :         return idx;
     513             : }

Generated by: LCOV version 1.10