op_classes.c 8.0 KB

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