op_classes.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Operating classes
  3. * Copyright(c) 2015 Intel Deutschland GmbH
  4. * Contact Information:
  5. * Intel Linux Wireless <ilw@linux.intel.com>
  6. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  7. *
  8. * This software may be distributed under the terms of the BSD license.
  9. * See README for more details.
  10. */
  11. #include "utils/includes.h"
  12. #include "utils/common.h"
  13. #include "common/ieee802_11_common.h"
  14. #include "wpa_supplicant_i.h"
  15. enum chan_allowed {
  16. NOT_ALLOWED, ALLOWED
  17. };
  18. static enum chan_allowed allow_channel(struct hostapd_hw_modes *mode, u8 chan,
  19. unsigned int *flags)
  20. {
  21. int i;
  22. for (i = 0; i < mode->num_channels; i++) {
  23. if (mode->channels[i].chan == chan)
  24. break;
  25. }
  26. if (i == mode->num_channels ||
  27. (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED))
  28. return NOT_ALLOWED;
  29. if (flags)
  30. *flags = mode->channels[i].flag;
  31. return ALLOWED;
  32. }
  33. static int get_center_80mhz(struct hostapd_hw_modes *mode, u8 channel)
  34. {
  35. u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
  36. size_t i;
  37. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  38. return 0;
  39. for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
  40. /*
  41. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  42. * so the center channel is 6 channels away from the start/end.
  43. */
  44. if (channel >= center_channels[i] - 6 &&
  45. channel <= center_channels[i] + 6)
  46. return center_channels[i];
  47. }
  48. return 0;
  49. }
  50. static enum chan_allowed verify_80mhz(struct hostapd_hw_modes *mode, u8 channel)
  51. {
  52. u8 center_chan;
  53. unsigned int i;
  54. center_chan = get_center_80mhz(mode, channel);
  55. if (!center_chan)
  56. return NOT_ALLOWED;
  57. /* check all the channels are available */
  58. for (i = 0; i < 4; i++) {
  59. unsigned int flags;
  60. u8 adj_chan = center_chan - 6 + i * 4;
  61. if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
  62. return NOT_ALLOWED;
  63. if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) ||
  64. (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) ||
  65. (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) ||
  66. (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)))
  67. return NOT_ALLOWED;
  68. }
  69. return ALLOWED;
  70. }
  71. static int get_center_160mhz(struct hostapd_hw_modes *mode, u8 channel)
  72. {
  73. u8 center_channels[] = { 50, 114 };
  74. unsigned int i;
  75. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  76. return 0;
  77. for (i = 0; i < ARRAY_SIZE(center_channels); i++) {
  78. /*
  79. * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
  80. * so the center channel is 14 channels away from the start/end.
  81. */
  82. if (channel >= center_channels[i] - 14 &&
  83. channel <= center_channels[i] + 14)
  84. return center_channels[i];
  85. }
  86. return 0;
  87. }
  88. static enum chan_allowed verify_160mhz(struct hostapd_hw_modes *mode,
  89. u8 channel)
  90. {
  91. u8 center_chan;
  92. unsigned int i;
  93. center_chan = get_center_160mhz(mode, channel);
  94. if (!center_chan)
  95. return NOT_ALLOWED;
  96. /* Check all the channels are available */
  97. for (i = 0; i < 8; i++) {
  98. unsigned int flags;
  99. u8 adj_chan = center_chan - 14 + i * 4;
  100. if (allow_channel(mode, adj_chan, &flags) == NOT_ALLOWED)
  101. return NOT_ALLOWED;
  102. if ((i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150)) ||
  103. (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130)) ||
  104. (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110)) ||
  105. (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90)) ||
  106. (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70)) ||
  107. (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50)) ||
  108. (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30)) ||
  109. (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10)))
  110. return NOT_ALLOWED;
  111. }
  112. return ALLOWED;
  113. }
  114. static enum chan_allowed verify_channel(struct hostapd_hw_modes *mode,
  115. u8 channel, u8 bw)
  116. {
  117. unsigned int flag = 0;
  118. enum chan_allowed res, res2;
  119. res2 = res = allow_channel(mode, channel, &flag);
  120. if (bw == BW40MINUS) {
  121. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  122. return NOT_ALLOWED;
  123. res2 = allow_channel(mode, channel - 4, NULL);
  124. } else if (bw == BW40PLUS) {
  125. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  126. return NOT_ALLOWED;
  127. res2 = allow_channel(mode, channel + 4, NULL);
  128. } else if (bw == BW80) {
  129. /*
  130. * channel is a center channel and as such, not necessarily a
  131. * valid 20 MHz channels. Override earlier allow_channel()
  132. * result and use only the 80 MHz specific version.
  133. */
  134. res2 = res = verify_80mhz(mode, channel);
  135. } else if (bw == BW160) {
  136. /*
  137. * channel is a center channel and as such, not necessarily a
  138. * valid 20 MHz channels. Override earlier allow_channel()
  139. * result and use only the 160 MHz specific version.
  140. */
  141. res2 = res = verify_160mhz(mode, channel);
  142. } else if (bw == BW80P80) {
  143. /*
  144. * channel is a center channel and as such, not necessarily a
  145. * valid 20 MHz channels. Override earlier allow_channel()
  146. * result and use only the 80 MHz specific version.
  147. */
  148. res2 = res = verify_80mhz(mode, channel);
  149. }
  150. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  151. return NOT_ALLOWED;
  152. return ALLOWED;
  153. }
  154. static int wpas_op_class_supported(struct wpa_supplicant *wpa_s,
  155. const struct oper_class_map *op_class)
  156. {
  157. int chan;
  158. size_t i;
  159. struct hostapd_hw_modes *mode;
  160. int found;
  161. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, op_class->mode);
  162. if (!mode)
  163. return 0;
  164. if (op_class->op_class == 128) {
  165. u8 channels[] = { 42, 58, 106, 122, 138, 155 };
  166. for (i = 0; i < ARRAY_SIZE(channels); i++) {
  167. if (verify_channel(mode, channels[i], op_class->bw) ==
  168. ALLOWED)
  169. return 1;
  170. }
  171. return 0;
  172. }
  173. if (op_class->op_class == 129) {
  174. /* Check if either 160 MHz channels is allowed */
  175. return verify_channel(mode, 50, op_class->bw) == ALLOWED ||
  176. verify_channel(mode, 114, op_class->bw) == ALLOWED;
  177. }
  178. if (op_class->op_class == 130) {
  179. /* Need at least two non-contiguous 80 MHz segments */
  180. found = 0;
  181. if (verify_channel(mode, 42, op_class->bw) == ALLOWED ||
  182. verify_channel(mode, 58, op_class->bw) == ALLOWED)
  183. found++;
  184. if (verify_channel(mode, 106, op_class->bw) == ALLOWED ||
  185. verify_channel(mode, 122, op_class->bw) == ALLOWED ||
  186. verify_channel(mode, 138, op_class->bw) == ALLOWED)
  187. found++;
  188. if (verify_channel(mode, 106, op_class->bw) == ALLOWED &&
  189. verify_channel(mode, 138, op_class->bw) == ALLOWED)
  190. found++;
  191. if (verify_channel(mode, 155, op_class->bw) == ALLOWED)
  192. found++;
  193. if (found >= 2)
  194. return 1;
  195. return 0;
  196. }
  197. found = 0;
  198. for (chan = op_class->min_chan; chan <= op_class->max_chan;
  199. chan += op_class->inc) {
  200. if (verify_channel(mode, chan, op_class->bw) == ALLOWED) {
  201. found = 1;
  202. break;
  203. }
  204. }
  205. return found;
  206. }
  207. size_t wpas_supp_op_class_ie(struct wpa_supplicant *wpa_s, int freq, u8 *pos,
  208. size_t len)
  209. {
  210. struct wpabuf *buf;
  211. u8 op, current, chan;
  212. u8 *ie_len;
  213. size_t res;
  214. /*
  215. * Assume 20 MHz channel for now.
  216. * TODO: Use the secondary channel and VHT channel width that will be
  217. * used after association.
  218. */
  219. if (ieee80211_freq_to_channel_ext(freq, 0, VHT_CHANWIDTH_USE_HT,
  220. &current, &chan) == NUM_HOSTAPD_MODES)
  221. return 0;
  222. /*
  223. * Need 3 bytes for EID, length, and current operating class, plus
  224. * 1 byte for every other supported operating class.
  225. */
  226. buf = wpabuf_alloc(global_op_class_size + 3);
  227. if (!buf)
  228. return 0;
  229. wpabuf_put_u8(buf, WLAN_EID_SUPPORTED_OPERATING_CLASSES);
  230. /* Will set the length later, putting a placeholder */
  231. ie_len = wpabuf_put(buf, 1);
  232. wpabuf_put_u8(buf, current);
  233. for (op = 0; global_op_class[op].op_class; op++) {
  234. if (wpas_op_class_supported(wpa_s, &global_op_class[op]))
  235. wpabuf_put_u8(buf, global_op_class[op].op_class);
  236. }
  237. *ie_len = wpabuf_len(buf) - 2;
  238. if (*ie_len < 2 || wpabuf_len(buf) > len) {
  239. wpa_printf(MSG_ERROR,
  240. "Failed to add supported operating classes IE");
  241. res = 0;
  242. } else {
  243. os_memcpy(pos, wpabuf_head(buf), wpabuf_len(buf));
  244. res = wpabuf_len(buf);
  245. wpa_hexdump_buf(MSG_DEBUG,
  246. "Added supported operating classes IE", buf);
  247. }
  248. wpabuf_free(buf);
  249. return res;
  250. }