dfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * DFS - Dynamic Frequency Selection
  3. * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2013, Qualcomm Atheros, Inc.
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "utils/includes.h"
  10. #include "utils/common.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "hostapd.h"
  13. #include "ap_drv_ops.h"
  14. #include "drivers/driver.h"
  15. #include "dfs.h"
  16. static int dfs_get_used_n_chans(struct hostapd_data *hapd)
  17. {
  18. int n_chans = 1;
  19. if (hapd->iconf->ieee80211n && hapd->iconf->secondary_channel)
  20. n_chans = 2;
  21. if (hapd->iconf->ieee80211ac) {
  22. switch (hapd->iconf->vht_oper_chwidth) {
  23. case VHT_CHANWIDTH_USE_HT:
  24. break;
  25. case VHT_CHANWIDTH_80MHZ:
  26. n_chans = 4;
  27. break;
  28. case VHT_CHANWIDTH_160MHZ:
  29. n_chans = 8;
  30. break;
  31. default:
  32. break;
  33. }
  34. }
  35. return n_chans;
  36. }
  37. static int dfs_channel_available(struct hostapd_channel_data *chan)
  38. {
  39. if (chan->flag & HOSTAPD_CHAN_DISABLED)
  40. return 0;
  41. if ((chan->flag & HOSTAPD_CHAN_RADAR) &&
  42. ((chan->flag & HOSTAPD_CHAN_DFS_MASK) ==
  43. HOSTAPD_CHAN_DFS_UNAVAILABLE))
  44. return 0;
  45. return 1;
  46. }
  47. static int dfs_is_chan_allowed(struct hostapd_channel_data *chan, int n_chans)
  48. {
  49. /*
  50. * The tables contain first valid channel number based on channel width.
  51. * We will also choose this first channel as the control one.
  52. */
  53. int allowed_40[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
  54. 184, 192 };
  55. /*
  56. * VHT80, valid channels based on center frequency:
  57. * 42, 58, 106, 122, 138, 155
  58. */
  59. int allowed_80[] = { 36, 52, 100, 116, 132, 149 };
  60. int *allowed = allowed_40;
  61. unsigned int i, allowed_no = 0;
  62. switch (n_chans) {
  63. case 2:
  64. allowed = allowed_40;
  65. allowed_no = sizeof(allowed_40) / sizeof(allowed_40[0]);
  66. break;
  67. case 4:
  68. allowed = allowed_80;
  69. allowed_no = sizeof(allowed_80) / sizeof(allowed_80[0]);
  70. break;
  71. default:
  72. wpa_printf(MSG_DEBUG, "Unknown width for %d channels", n_chans);
  73. break;
  74. }
  75. for (i = 0; i < allowed_no; i++) {
  76. if (chan->chan == allowed[i])
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. static int dfs_find_channel(struct hostapd_data *hapd,
  82. struct hostapd_channel_data **ret_chan,
  83. int idx)
  84. {
  85. struct hostapd_hw_modes *mode;
  86. struct hostapd_channel_data *chan, *next_chan;
  87. int i, j, channel_idx = 0, n_chans;
  88. mode = hapd->iface->current_mode;
  89. n_chans = dfs_get_used_n_chans(hapd);
  90. wpa_printf(MSG_DEBUG, "DFS new chan checking %d channels", n_chans);
  91. for (i = 0; i < mode->num_channels; i++) {
  92. chan = &mode->channels[i];
  93. /* Skip not available channels */
  94. if (!dfs_channel_available(chan))
  95. continue;
  96. /* Skip HT40/VHT uncompatible channels */
  97. if (hapd->iconf->ieee80211n &&
  98. hapd->iconf->secondary_channel) {
  99. if (!dfs_is_chan_allowed(chan, n_chans))
  100. continue;
  101. for (j = 1; j < n_chans; j++) {
  102. next_chan = &mode->channels[i + j];
  103. if (!dfs_channel_available(next_chan))
  104. break;
  105. }
  106. if (j != n_chans)
  107. continue;
  108. /* Set HT40+ */
  109. hapd->iconf->secondary_channel = 1;
  110. }
  111. if (ret_chan && idx == channel_idx) {
  112. wpa_printf(MSG_DEBUG, "Selected ch. #%d", chan->chan);
  113. *ret_chan = chan;
  114. return idx;
  115. }
  116. wpa_printf(MSG_DEBUG, "Adding channel: %d", chan->chan);
  117. channel_idx++;
  118. }
  119. return channel_idx;
  120. }
  121. static void dfs_adjust_vht_center_freq(struct hostapd_data *hapd,
  122. struct hostapd_channel_data *chan)
  123. {
  124. if (!hapd->iconf->ieee80211ac)
  125. return;
  126. if (!chan)
  127. return;
  128. switch (hapd->iconf->vht_oper_chwidth) {
  129. case VHT_CHANWIDTH_USE_HT:
  130. hapd->iconf->vht_oper_centr_freq_seg0_idx = chan->chan + 2;
  131. break;
  132. case VHT_CHANWIDTH_80MHZ:
  133. hapd->iconf->vht_oper_centr_freq_seg0_idx = chan->chan + 6;
  134. break;
  135. case VHT_CHANWIDTH_160MHZ:
  136. hapd->iconf->vht_oper_centr_freq_seg0_idx =
  137. chan->chan + 14;
  138. break;
  139. default:
  140. wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now");
  141. break;
  142. }
  143. wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d",
  144. hapd->iconf->vht_oper_centr_freq_seg0_idx);
  145. }
  146. /* Return start channel idx we will use for mode->channels[idx] */
  147. static int dfs_get_start_chan_idx(struct hostapd_data *hapd)
  148. {
  149. struct hostapd_hw_modes *mode;
  150. struct hostapd_channel_data *chan;
  151. int channel_no = hapd->iconf->channel;
  152. int res = -1, i;
  153. /* HT40- */
  154. if (hapd->iconf->ieee80211n && hapd->iconf->secondary_channel == -1)
  155. channel_no -= 4;
  156. /* VHT */
  157. if (hapd->iconf->ieee80211ac) {
  158. switch (hapd->iconf->vht_oper_chwidth) {
  159. case VHT_CHANWIDTH_USE_HT:
  160. break;
  161. case VHT_CHANWIDTH_80MHZ:
  162. channel_no =
  163. hapd->iconf->vht_oper_centr_freq_seg0_idx - 6;
  164. break;
  165. case VHT_CHANWIDTH_160MHZ:
  166. channel_no =
  167. hapd->iconf->vht_oper_centr_freq_seg0_idx - 14;
  168. break;
  169. default:
  170. wpa_printf(MSG_INFO,
  171. "DFS only VHT20/40/80/160 is supported now");
  172. channel_no = -1;
  173. break;
  174. }
  175. }
  176. /* Get idx */
  177. mode = hapd->iface->current_mode;
  178. for (i = 0; i < mode->num_channels; i++) {
  179. chan = &mode->channels[i];
  180. if (chan->chan == channel_no) {
  181. res = i;
  182. break;
  183. }
  184. }
  185. if (res == -1)
  186. wpa_printf(MSG_DEBUG, "DFS chan_idx seems wrong: -1");
  187. return res;
  188. }
  189. /* At least one channel have radar flag */
  190. static int dfs_check_chans_radar(struct hostapd_data *hapd, int start_chan_idx,
  191. int n_chans)
  192. {
  193. struct hostapd_channel_data *channel;
  194. struct hostapd_hw_modes *mode;
  195. int i, res = 0;
  196. mode = hapd->iface->current_mode;
  197. for (i = 0; i < n_chans; i++) {
  198. channel = &mode->channels[start_chan_idx + i];
  199. if (channel->flag & HOSTAPD_CHAN_RADAR)
  200. res++;
  201. }
  202. return res;
  203. }
  204. /* All channels available */
  205. static int dfs_check_chans_available(struct hostapd_data *hapd,
  206. int start_chan_idx, int n_chans)
  207. {
  208. struct hostapd_channel_data *channel;
  209. struct hostapd_hw_modes *mode;
  210. int i;
  211. mode = hapd->iface->current_mode;
  212. for(i = 0; i < n_chans; i++) {
  213. channel = &mode->channels[start_chan_idx + i];
  214. if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
  215. HOSTAPD_CHAN_DFS_AVAILABLE)
  216. break;
  217. }
  218. return i == n_chans;
  219. }
  220. /* At least one channel unavailable */
  221. static int dfs_check_chans_unavailable(struct hostapd_data *hapd,
  222. int start_chan_idx,
  223. int n_chans)
  224. {
  225. struct hostapd_channel_data *channel;
  226. struct hostapd_hw_modes *mode;
  227. int i, res = 0;
  228. mode = hapd->iface->current_mode;
  229. for(i = 0; i < n_chans; i++) {
  230. channel = &mode->channels[start_chan_idx + i];
  231. if (channel->flag & HOSTAPD_CHAN_DISABLED)
  232. res++;
  233. if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
  234. HOSTAPD_CHAN_DFS_UNAVAILABLE)
  235. res++;
  236. }
  237. return res;
  238. }
  239. static struct hostapd_channel_data * dfs_get_valid_channel(
  240. struct hostapd_data *hapd)
  241. {
  242. struct hostapd_hw_modes *mode;
  243. struct hostapd_channel_data *chan = NULL;
  244. int channel_idx, new_channel_idx;
  245. u32 _rand;
  246. wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
  247. if (hapd->iface->current_mode == NULL)
  248. return NULL;
  249. mode = hapd->iface->current_mode;
  250. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  251. return NULL;
  252. /* get random available channel */
  253. channel_idx = dfs_find_channel(hapd, NULL, 0);
  254. if (channel_idx > 0) {
  255. os_get_random((u8 *) &_rand, sizeof(_rand));
  256. new_channel_idx = _rand % channel_idx;
  257. dfs_find_channel(hapd, &chan, new_channel_idx);
  258. }
  259. /* VHT */
  260. dfs_adjust_vht_center_freq(hapd, chan);
  261. return chan;
  262. }
  263. static int set_dfs_state_freq(struct hostapd_data *hapd, int freq, u32 state)
  264. {
  265. struct hostapd_hw_modes *mode;
  266. struct hostapd_channel_data *chan = NULL;
  267. int i;
  268. mode = hapd->iface->current_mode;
  269. if (mode == NULL)
  270. return 0;
  271. wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
  272. for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
  273. chan = &hapd->iface->current_mode->channels[i];
  274. if (chan->freq == freq) {
  275. if (chan->flag & HOSTAPD_CHAN_RADAR) {
  276. chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
  277. chan->flag |= state;
  278. return 1; /* Channel found */
  279. }
  280. }
  281. }
  282. wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
  283. return 0;
  284. }
  285. static int set_dfs_state(struct hostapd_data *hapd, int freq, int ht_enabled,
  286. int chan_offset, int chan_width, int cf1,
  287. int cf2, u32 state)
  288. {
  289. int n_chans = 1, i;
  290. struct hostapd_hw_modes *mode;
  291. int frequency = freq;
  292. int ret = 0;
  293. mode = hapd->iface->current_mode;
  294. if (mode == NULL)
  295. return 0;
  296. if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
  297. wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
  298. return 0;
  299. }
  300. /* Seems cf1 and chan_width is enough here */
  301. switch (chan_width) {
  302. case CHAN_WIDTH_20_NOHT:
  303. case CHAN_WIDTH_20:
  304. n_chans = 1;
  305. frequency = cf1;
  306. break;
  307. case CHAN_WIDTH_40:
  308. n_chans = 2;
  309. frequency = cf1 - 10;
  310. break;
  311. case CHAN_WIDTH_80:
  312. n_chans = 4;
  313. frequency = cf1 - 30;
  314. break;
  315. case CHAN_WIDTH_160:
  316. n_chans = 8;
  317. frequency = cf1 - 70;
  318. break;
  319. default:
  320. wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
  321. chan_width);
  322. break;
  323. }
  324. wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
  325. n_chans);
  326. for (i = 0; i < n_chans; i++) {
  327. ret += set_dfs_state_freq(hapd, frequency, state);
  328. frequency = frequency + 20;
  329. }
  330. return ret;
  331. }
  332. static int dfs_are_channels_overlapped(struct hostapd_data *hapd, int freq,
  333. int chan_width, int cf1, int cf2)
  334. {
  335. int start_chan_idx;
  336. struct hostapd_hw_modes *mode;
  337. struct hostapd_channel_data *chan;
  338. int n_chans, i, j, frequency = freq, radar_n_chans = 1;
  339. u8 radar_chan;
  340. int res = 0;
  341. if (hapd->iface->freq == freq)
  342. res++;
  343. /* Our configuration */
  344. mode = hapd->iface->current_mode;
  345. start_chan_idx = dfs_get_start_chan_idx(hapd);
  346. n_chans = dfs_get_used_n_chans(hapd);
  347. /* Reported via radar event */
  348. switch (chan_width) {
  349. case CHAN_WIDTH_20_NOHT:
  350. case CHAN_WIDTH_20:
  351. radar_n_chans = 1;
  352. frequency = cf1;
  353. break;
  354. case CHAN_WIDTH_40:
  355. radar_n_chans = 2;
  356. frequency = cf1 - 10;
  357. break;
  358. case CHAN_WIDTH_80:
  359. radar_n_chans = 4;
  360. frequency = cf1 - 30;
  361. break;
  362. case CHAN_WIDTH_160:
  363. radar_n_chans = 8;
  364. frequency = cf1 - 70;
  365. break;
  366. default:
  367. wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
  368. chan_width);
  369. break;
  370. }
  371. ieee80211_freq_to_chan(frequency, &radar_chan);
  372. for (i = 0; i < n_chans; i++) {
  373. chan = &mode->channels[start_chan_idx + i];
  374. for (j = 0; j < radar_n_chans; j++) {
  375. wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
  376. chan->chan, radar_chan + j * 4);
  377. if (chan->chan == radar_chan + j * 4)
  378. res++;
  379. }
  380. }
  381. wpa_printf(MSG_DEBUG, "overlapped: %d", res);
  382. return res;
  383. }
  384. /*
  385. * Main DFS handler
  386. * 1 - continue channel/ap setup
  387. * 0 - channel/ap setup will be continued after CAC
  388. * -1 - hit critical error
  389. */
  390. int hostapd_handle_dfs(struct hostapd_data *hapd)
  391. {
  392. struct hostapd_channel_data *channel;
  393. int res, n_chans, start_chan_idx;
  394. do {
  395. /* Get start (first) channel for current configuration */
  396. start_chan_idx = dfs_get_start_chan_idx(hapd);
  397. if (start_chan_idx == -1)
  398. return -1;
  399. /* Get number of used channels, depend on width */
  400. n_chans = dfs_get_used_n_chans(hapd);
  401. /* Check if any of configured channels require DFS */
  402. res = dfs_check_chans_radar(hapd, start_chan_idx, n_chans);
  403. wpa_printf(MSG_DEBUG,
  404. "DFS %d channels required radar detection",
  405. res);
  406. if (!res)
  407. return 1;
  408. /* Check if all channels are DFS available */
  409. res = dfs_check_chans_available(hapd, start_chan_idx, n_chans);
  410. wpa_printf(MSG_DEBUG,
  411. "DFS all channels available, (SKIP CAC): %s",
  412. res ? "yes" : "no");
  413. if (res)
  414. return 1;
  415. /* Check if any of configured channels is unavailable */
  416. res = dfs_check_chans_unavailable(hapd, start_chan_idx,
  417. n_chans);
  418. wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
  419. res, res ? "yes": "no");
  420. if (res) {
  421. channel = dfs_get_valid_channel(hapd);
  422. if (!channel) {
  423. wpa_printf(MSG_ERROR, "could not get valid channel");
  424. return -1;
  425. }
  426. hapd->iconf->channel = channel->chan;
  427. hapd->iface->freq = channel->freq;
  428. }
  429. } while (res);
  430. /* Finally start CAC */
  431. wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", hapd->iface->freq);
  432. if (hostapd_start_dfs_cac(hapd, hapd->iconf->hw_mode,
  433. hapd->iface->freq,
  434. hapd->iconf->channel,
  435. hapd->iconf->ieee80211n,
  436. hapd->iconf->ieee80211ac,
  437. hapd->iconf->secondary_channel,
  438. hapd->iconf->vht_oper_chwidth,
  439. hapd->iconf->vht_oper_centr_freq_seg0_idx,
  440. hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
  441. wpa_printf(MSG_DEBUG, "DFS start_dfs_cac() failed");
  442. return -1;
  443. }
  444. return 0;
  445. }
  446. int hostapd_dfs_complete_cac(struct hostapd_data *hapd, int success, int freq,
  447. int ht_enabled, int chan_offset, int chan_width,
  448. int cf1, int cf2)
  449. {
  450. struct hostapd_channel_data *channel;
  451. int err = 1;
  452. if (success) {
  453. /* Complete iface/ap configuration */
  454. set_dfs_state(hapd, freq, ht_enabled, chan_offset,
  455. chan_width, cf1, cf2,
  456. HOSTAPD_CHAN_DFS_AVAILABLE);
  457. hostapd_setup_interface_complete(hapd->iface, 0);
  458. } else {
  459. /* Switch to new channel */
  460. set_dfs_state(hapd, freq, ht_enabled, chan_offset,
  461. chan_width, cf1, cf2,
  462. HOSTAPD_CHAN_DFS_UNAVAILABLE);
  463. channel = dfs_get_valid_channel(hapd);
  464. if (channel) {
  465. hapd->iconf->channel = channel->chan;
  466. hapd->iface->freq = channel->freq;
  467. err = 0;
  468. } else
  469. wpa_printf(MSG_ERROR, "No valid channel available");
  470. hostapd_setup_interface_complete(hapd->iface, err);
  471. }
  472. return 0;
  473. }
  474. static int hostapd_dfs_start_channel_switch(struct hostapd_data *hapd)
  475. {
  476. struct hostapd_channel_data *channel;
  477. int err = 1;
  478. wpa_printf(MSG_DEBUG, "%s called", __func__);
  479. channel = dfs_get_valid_channel(hapd);
  480. if (channel) {
  481. hapd->iconf->channel = channel->chan;
  482. hapd->iface->freq = channel->freq;
  483. err = 0;
  484. }
  485. hapd->driver->stop_ap(hapd->drv_priv);
  486. hostapd_setup_interface_complete(hapd->iface, err);
  487. return 0;
  488. }
  489. int hostapd_dfs_radar_detected(struct hostapd_data *hapd, int freq,
  490. int ht_enabled, int chan_offset, int chan_width,
  491. int cf1, int cf2)
  492. {
  493. int res;
  494. if (!hapd->iconf->ieee80211h)
  495. return 0;
  496. /* mark radar frequency as invalid */
  497. res = set_dfs_state(hapd, freq, ht_enabled, chan_offset,
  498. chan_width, cf1, cf2,
  499. HOSTAPD_CHAN_DFS_UNAVAILABLE);
  500. /* Skip if reported radar event not overlapped our channels */
  501. res = dfs_are_channels_overlapped(hapd, freq, chan_width, cf1, cf2);
  502. if (!res)
  503. return 0;
  504. /* we are working on non-DFS channel - skip event */
  505. if (res == 0)
  506. return 0;
  507. /* radar detected while operating, switch the channel. */
  508. res = hostapd_dfs_start_channel_switch(hapd);
  509. return res;
  510. }
  511. int hostapd_dfs_nop_finished(struct hostapd_data *hapd, int freq,
  512. int ht_enabled, int chan_offset, int chan_width,
  513. int cf1, int cf2)
  514. {
  515. /* TODO add correct implementation here */
  516. set_dfs_state(hapd, freq, ht_enabled, chan_offset, chan_width, cf1, cf2,
  517. HOSTAPD_CHAN_DFS_USABLE);
  518. return 0;
  519. }