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 */ 24int p2p_random(char *buf, size_t len) 25{ 26 u8 val; 27 size_t i; 28 u8 letters = 'Z' - 'A' + 1; 29 u8 numbers = 10; 30 31 if (os_get_random((unsigned char *) buf, len)) 32 return -1; 33 /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */ 34 for (i = 0; i < len; i++) { 35 val = buf[i]; 36 val %= 2 * letters + numbers; 37 if (val < letters) 38 buf[i] = 'A' + val; 39 else if (val < 2 * letters) 40 buf[i] = 'a' + (val - letters); 41 else 42 buf[i] = '0' + (val - 2 * letters); 43 } 44 45 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 */ 55int 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 switch (op_class) { 60 case 81: 61 /* channels 1..13 */ 62 if (channel < 1 || channel > 13) 63 return -1; 64 return 2407 + 5 * channel; 65 case 82: 66 /* channel 14 */ 67 if (channel != 14) 68 return -1; 69 return 2414 + 5 * channel; 70 case 83: /* channels 1..9; 40 MHz */ 71 case 84: /* channels 5..13; 40 MHz */ 72 if (channel < 1 || channel > 13) 73 return -1; 74 return 2407 + 5 * channel; 75 case 115: /* channels 36,40,44,48; indoor only */ 76 case 118: /* channels 52,56,60,64; dfs */ 77 if (channel < 36 || channel > 64) 78 return -1; 79 return 5000 + 5 * channel; 80 case 124: /* channels 149,153,157,161 */ 81 case 125: /* channels 149,153,157,161,165,169 */ 82 if (channel < 149 || channel > 161) 83 return -1; 84 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 if (channel < 36 || channel > 64) 90 return -1; 91 return 5000 + 5 * channel; 92 case 126: /* channels 149,157; 40 MHz */ 93 case 127: /* channels 153,161; 40 MHz */ 94 if (channel < 149 || channel > 161) 95 return -1; 96 return 5000 + 5 * channel; 97 } 98 return -1; 99} 100 101 102/** 103 * p2p_freq_to_channel - Convert frequency into channel info 104 * @op_class: Buffer for returning operating class 105 * @channel: Buffer for returning channel number 106 * Returns: 0 on success, -1 if the specified frequency is unknown 107 */ 108int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel) 109{ 110 /* TODO: more operating classes */ 111 if (freq >= 2412 && freq <= 2472) { 112 if ((freq - 2407) % 5) 113 return -1; 114 115 *op_class = 81; /* 2.407 GHz, channels 1..13 */ 116 *channel = (freq - 2407) / 5; 117 return 0; 118 } 119 120 if (freq == 2484) { 121 *op_class = 82; /* channel 14 */ 122 *channel = 14; 123 return 0; 124 } 125 126 if (freq >= 5180 && freq <= 5240) { 127 if ((freq - 5000) % 5) 128 return -1; 129 130 *op_class = 115; /* 5 GHz, channels 36..48 */ 131 *channel = (freq - 5000) / 5; 132 return 0; 133 } 134 135 if (freq >= 5745 && freq <= 5805) { 136 if ((freq - 5000) % 5) 137 return -1; 138 139 *op_class = 124; /* 5 GHz, channels 149..161 */ 140 *channel = (freq - 5000) / 5; 141 return 0; 142 } 143 144 return -1; 145} 146 147 148static void p2p_reg_class_intersect(const struct p2p_reg_class *a, 149 const struct p2p_reg_class *b, 150 struct p2p_reg_class *res) 151{ 152 size_t i, j; 153 154 res->reg_class = a->reg_class; 155 156 for (i = 0; i < a->channels; i++) { 157 for (j = 0; j < b->channels; j++) { 158 if (a->channel[i] != b->channel[j]) 159 continue; 160 res->channel[res->channels] = a->channel[i]; 161 res->channels++; 162 if (res->channels == P2P_MAX_REG_CLASS_CHANNELS) 163 return; 164 } 165 } 166} 167 168 169/** 170 * p2p_channels_intersect - Intersection of supported channel lists 171 * @a: First set of supported channels 172 * @b: Second set of supported channels 173 * @res: Data structure for returning the intersection of support channels 174 * 175 * This function can be used to find a common set of supported channels. Both 176 * input channels sets are assumed to use the same country code. If different 177 * country codes are used, the regulatory class numbers may not be matched 178 * correctly and results are undefined. 179 */ 180void p2p_channels_intersect(const struct p2p_channels *a, 181 const struct p2p_channels *b, 182 struct p2p_channels *res) 183{ 184 size_t i, j; 185 186 os_memset(res, 0, sizeof(*res)); 187 188 for (i = 0; i < a->reg_classes; i++) { 189 const struct p2p_reg_class *a_reg = &a->reg_class[i]; 190 for (j = 0; j < b->reg_classes; j++) { 191 const struct p2p_reg_class *b_reg = &b->reg_class[j]; 192 if (a_reg->reg_class != b_reg->reg_class) 193 continue; 194 p2p_reg_class_intersect( 195 a_reg, b_reg, 196 &res->reg_class[res->reg_classes]); 197 if (res->reg_class[res->reg_classes].channels) { 198 res->reg_classes++; 199 if (res->reg_classes == P2P_MAX_REG_CLASSES) 200 return; 201 } 202 } 203 } 204} 205 206 207/** 208 * p2p_channels_includes - Check whether a channel is included in the list 209 * @channels: List of supported channels 210 * @reg_class: Regulatory class of the channel to search 211 * @channel: Channel number of the channel to search 212 * Returns: 1 if channel was found or 0 if not 213 */ 214int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class, 215 u8 channel) 216{ 217 size_t i, j; 218 for (i = 0; i < channels->reg_classes; i++) { 219 const struct p2p_reg_class *reg = &channels->reg_class[i]; 220 if (reg->reg_class != reg_class) 221 continue; 222 for (j = 0; j < reg->channels; j++) { 223 if (reg->channel[j] == channel) 224 return 1; 225 } 226 } 227 return 0; 228} 229 230 231int p2p_channels_includes_freq(const struct p2p_channels *channels, 232 unsigned int freq) 233{ 234 size_t i, j; 235 for (i = 0; i < channels->reg_classes; i++) { 236 const struct p2p_reg_class *reg = &channels->reg_class[i]; 237 for (j = 0; j < reg->channels; j++) { 238 if (p2p_channel_to_freq(reg->reg_class, 239 reg->channel[j]) == (int) freq) 240 return 1; 241 } 242 } 243 return 0; 244} 245 246 247#ifdef ANDROID_P2P 248static int p2p_block_op_freq(unsigned int freq) 249{ 250 return (freq >= 5170 && freq < 5745); 251} 252 253 254size_t p2p_copy_reg_class(struct p2p_reg_class *dc, struct p2p_reg_class *sc) 255{ 256 unsigned int i; 257 258 dc->reg_class = sc->reg_class; 259 dc->channels = 0; 260 for (i=0; i < sc->channels; i++) { 261 if (!p2p_block_op_freq(p2p_channel_to_freq(sc->reg_class, 262 sc->channel[i]))) { 263 dc->channel[dc->channels] = sc->channel[i]; 264 dc->channels++; 265 } 266 } 267 return dc->channels; 268} 269#endif 270 271 272int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq) 273{ 274 u8 op_reg_class, op_channel; 275 276#ifdef ANDROID_P2P 277 if (p2p_block_op_freq(freq)) 278 return 0; 279#endif 280 if (p2p_freq_to_channel(freq, &op_reg_class, &op_channel) < 0) 281 return 0; 282 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class, 283 op_channel); 284} 285 286 287unsigned int p2p_get_pref_freq(struct p2p_data *p2p, 288 const struct p2p_channels *channels) 289{ 290 unsigned int i; 291 int freq = 0; 292 293 if (channels == NULL) { 294 if (p2p->cfg->num_pref_chan) { 295 freq = p2p_channel_to_freq( 296 p2p->cfg->pref_chan[0].op_class, 297 p2p->cfg->pref_chan[0].chan); 298 if (freq < 0) 299 freq = 0; 300 } 301 return freq; 302 } 303 304 for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) { 305 freq = p2p_channel_to_freq(p2p->cfg->pref_chan[i].op_class, 306 p2p->cfg->pref_chan[i].chan); 307 if (p2p_channels_includes_freq(channels, freq)) 308 return freq; 309 } 310 311 return 0; 312} 313