p2p_utils.c 12 KB

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