1 /* 2 * P2P - generic helper functions 3 * Copyright (c) 2009, Atheros Communications 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #include "includes.h" 16 17 #include "common.h" 18 #include "p2p_i.h" 19 20 21 /** 22 * p2p_random - Generate random string for SSID and passphrase 23 * @buf: Buffer for returning the result 24 * @len: Number of octets to write to the buffer 25 * Returns: 0 on success, -1 on failure 26 * 27 * This function generates a random string using the following character set: 28 * 'A'-'Z', 'a'-'z', '0'-'9'. 29 */ 30 int p2p_random(char *buf, size_t len) 31 { 32 u8 val; 33 size_t i; 34 u8 letters = 'Z' - 'A' + 1; 35 u8 numbers = 10; 36 37 if (os_get_random((unsigned char *) buf, len)) 38 return -1; 39 /* Character set: 'A'-'Z', 'a'-'z', '0'-'9' */ 40 for (i = 0; i < len; i++) { 41 val = buf[i]; 42 val %= 2 * letters + numbers; 43 if (val < letters) 44 buf[i] = 'A' + val; 45 else if (val < 2 * letters) 46 buf[i] = 'a' + (val - letters); 47 else 48 buf[i] = '0' + (val - 2 * letters); 49 } 50 51 return 0; 52 } 53 54 55 static int p2p_channel_to_freq_j4(int reg_class, int channel) 56 { 57 /* Table J-4 in P802.11REVmb/D4.0 - Global operating classes */ 58 /* TODO: more regulatory classes */ 59 switch (reg_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_channel_to_freq - Convert channel info to frequency 104 * @country: Country code 105 * @reg_class: Regulatory class 106 * @channel: Channel number 107 * Returns: Frequency in MHz or -1 if the specified channel is unknown 108 */ 109 int p2p_channel_to_freq(const char *country, int reg_class, int channel) 110 { 111 if (country[2] == 0x04) 112 return p2p_channel_to_freq_j4(reg_class, channel); 113 114 /* These are mainly for backwards compatibility; to be removed */ 115 switch (reg_class) { 116 case 1: /* US/1, EU/1, JP/1 = 5 GHz, channels 36,40,44,48 */ 117 if (channel < 36 || channel > 48) 118 return -1; 119 return 5000 + 5 * channel; 120 case 3: /* US/3 = 5 GHz, channels 149,153,157,161 */ 121 case 5: /* US/5 = 5 GHz, channels 149,153,157,161 */ 122 if (channel < 149 || channel > 161) 123 return -1; 124 return 5000 + 5 * channel; 125 case 4: /* EU/4 = 2.407 GHz, channels 1..13 */ 126 case 12: /* US/12 = 2.407 GHz, channels 1..11 */ 127 case 30: /* JP/30 = 2.407 GHz, channels 1..13 */ 128 if (channel < 1 || channel > 13) 129 return -1; 130 return 2407 + 5 * channel; 131 case 31: /* JP/31 = 2.414 GHz, channel 14 */ 132 if (channel != 14) 133 return -1; 134 return 2414 + 5 * channel; 135 } 136 137 return -1; 138 } 139 140 141 /** 142 * p2p_freq_to_channel - Convert frequency into channel info 143 * @country: Country code 144 * @reg_class: Buffer for returning regulatory class 145 * @channel: Buffer for returning channel number 146 * Returns: 0 on success, -1 if the specified frequency is unknown 147 */ 148 int p2p_freq_to_channel(const char *country, unsigned int freq, u8 *reg_class, 149 u8 *channel) 150 { 151 /* TODO: more operating classes */ 152 if (freq >= 2412 && freq <= 2472) { 153 *reg_class = 81; /* 2.407 GHz, channels 1..13 */ 154 *channel = (freq - 2407) / 5; 155 return 0; 156 } 157 158 if (freq == 2484) { 159 *reg_class = 82; /* channel 14 */ 160 *channel = 14; 161 return 0; 162 } 163 164 if (freq >= 5180 && freq <= 5240) { 165 *reg_class = 115; /* 5 GHz, channels 36..48 */ 166 *channel = (freq - 5000) / 5; 167 return 0; 168 } 169 170 if (freq >= 5745 && freq <= 5805) { 171 *reg_class = 124; /* 5 GHz, channels 149..161 */ 172 *channel = (freq - 5000) / 5; 173 return 0; 174 } 175 176 return -1; 177 } 178 179 180 static void p2p_reg_class_intersect(const struct p2p_reg_class *a, 181 const struct p2p_reg_class *b, 182 struct p2p_reg_class *res) 183 { 184 size_t i, j; 185 186 res->reg_class = a->reg_class; 187 188 for (i = 0; i < a->channels; i++) { 189 for (j = 0; j < b->channels; j++) { 190 if (a->channel[i] != b->channel[j]) 191 continue; 192 res->channel[res->channels] = a->channel[i]; 193 res->channels++; 194 if (res->channels == P2P_MAX_REG_CLASS_CHANNELS) 195 return; 196 } 197 } 198 } 199 200 201 /** 202 * p2p_channels_intersect - Intersection of supported channel lists 203 * @a: First set of supported channels 204 * @b: Second set of supported channels 205 * @res: Data structure for returning the intersection of support channels 206 * 207 * This function can be used to find a common set of supported channels. Both 208 * input channels sets are assumed to use the same country code. If different 209 * country codes are used, the regulatory class numbers may not be matched 210 * correctly and results are undefined. 211 */ 212 void p2p_channels_intersect(const struct p2p_channels *a, 213 const struct p2p_channels *b, 214 struct p2p_channels *res) 215 { 216 size_t i, j; 217 218 os_memset(res, 0, sizeof(*res)); 219 220 for (i = 0; i < a->reg_classes; i++) { 221 const struct p2p_reg_class *a_reg = &a->reg_class[i]; 222 for (j = 0; j < b->reg_classes; j++) { 223 const struct p2p_reg_class *b_reg = &b->reg_class[j]; 224 if (a_reg->reg_class != b_reg->reg_class) 225 continue; 226 p2p_reg_class_intersect( 227 a_reg, b_reg, 228 &res->reg_class[res->reg_classes]); 229 if (res->reg_class[res->reg_classes].channels) { 230 res->reg_classes++; 231 if (res->reg_classes == P2P_MAX_REG_CLASSES) 232 return; 233 } 234 } 235 } 236 } 237 238 239 /** 240 * p2p_channels_includes - Check whether a channel is included in the list 241 * @channels: List of supported channels 242 * @reg_class: Regulatory class of the channel to search 243 * @channel: Channel number of the channel to search 244 * Returns: 1 if channel was found or 0 if not 245 */ 246 int p2p_channels_includes(const struct p2p_channels *channels, u8 reg_class, 247 u8 channel) 248 { 249 size_t i, j; 250 for (i = 0; i < channels->reg_classes; i++) { 251 const struct p2p_reg_class *reg = &channels->reg_class[i]; 252 if (reg->reg_class != reg_class) 253 continue; 254 for (j = 0; j < reg->channels; j++) { 255 if (reg->channel[j] == channel) 256 return 1; 257 } 258 } 259 return 0; 260 } 261 262 263 int p2p_supported_freq(struct p2p_data *p2p, unsigned int freq) 264 { 265 u8 op_reg_class, op_channel; 266 if (p2p_freq_to_channel(p2p->cfg->country, freq, 267 &op_reg_class, &op_channel) < 0) 268 return 0; 269 return p2p_channels_includes(&p2p->cfg->channels, op_reg_class, 270 op_channel); 271 } 272