dfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. if (hapd->iconf->secondary_channel == 1)
  131. hapd->iconf->vht_oper_centr_freq_seg0_idx =
  132. chan->chan + 2;
  133. else if (hapd->iconf->secondary_channel == -1)
  134. hapd->iconf->vht_oper_centr_freq_seg0_idx =
  135. chan->chan - 2;
  136. else
  137. hapd->iconf->vht_oper_centr_freq_seg0_idx = chan->chan;
  138. break;
  139. case VHT_CHANWIDTH_80MHZ:
  140. hapd->iconf->vht_oper_centr_freq_seg0_idx = chan->chan + 6;
  141. break;
  142. case VHT_CHANWIDTH_160MHZ:
  143. hapd->iconf->vht_oper_centr_freq_seg0_idx =
  144. chan->chan + 14;
  145. break;
  146. default:
  147. wpa_printf(MSG_INFO, "DFS only VHT20/40/80/160 is supported now");
  148. break;
  149. }
  150. wpa_printf(MSG_DEBUG, "DFS adjusting VHT center frequency: %d",
  151. hapd->iconf->vht_oper_centr_freq_seg0_idx);
  152. }
  153. /* Return start channel idx we will use for mode->channels[idx] */
  154. static int dfs_get_start_chan_idx(struct hostapd_data *hapd)
  155. {
  156. struct hostapd_hw_modes *mode;
  157. struct hostapd_channel_data *chan;
  158. int channel_no = hapd->iconf->channel;
  159. int res = -1, i;
  160. /* HT40- */
  161. if (hapd->iconf->ieee80211n && hapd->iconf->secondary_channel == -1)
  162. channel_no -= 4;
  163. /* VHT */
  164. if (hapd->iconf->ieee80211ac) {
  165. switch (hapd->iconf->vht_oper_chwidth) {
  166. case VHT_CHANWIDTH_USE_HT:
  167. break;
  168. case VHT_CHANWIDTH_80MHZ:
  169. channel_no =
  170. hapd->iconf->vht_oper_centr_freq_seg0_idx - 6;
  171. break;
  172. case VHT_CHANWIDTH_160MHZ:
  173. channel_no =
  174. hapd->iconf->vht_oper_centr_freq_seg0_idx - 14;
  175. break;
  176. default:
  177. wpa_printf(MSG_INFO,
  178. "DFS only VHT20/40/80/160 is supported now");
  179. channel_no = -1;
  180. break;
  181. }
  182. }
  183. /* Get idx */
  184. mode = hapd->iface->current_mode;
  185. for (i = 0; i < mode->num_channels; i++) {
  186. chan = &mode->channels[i];
  187. if (chan->chan == channel_no) {
  188. res = i;
  189. break;
  190. }
  191. }
  192. if (res == -1)
  193. wpa_printf(MSG_DEBUG, "DFS chan_idx seems wrong: -1");
  194. return res;
  195. }
  196. /* At least one channel have radar flag */
  197. static int dfs_check_chans_radar(struct hostapd_data *hapd, int start_chan_idx,
  198. int n_chans)
  199. {
  200. struct hostapd_channel_data *channel;
  201. struct hostapd_hw_modes *mode;
  202. int i, res = 0;
  203. mode = hapd->iface->current_mode;
  204. for (i = 0; i < n_chans; i++) {
  205. channel = &mode->channels[start_chan_idx + i];
  206. if (channel->flag & HOSTAPD_CHAN_RADAR)
  207. res++;
  208. }
  209. return res;
  210. }
  211. /* All channels available */
  212. static int dfs_check_chans_available(struct hostapd_data *hapd,
  213. int start_chan_idx, int n_chans)
  214. {
  215. struct hostapd_channel_data *channel;
  216. struct hostapd_hw_modes *mode;
  217. int i;
  218. mode = hapd->iface->current_mode;
  219. for(i = 0; i < n_chans; i++) {
  220. channel = &mode->channels[start_chan_idx + i];
  221. if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) !=
  222. HOSTAPD_CHAN_DFS_AVAILABLE)
  223. break;
  224. }
  225. return i == n_chans;
  226. }
  227. /* At least one channel unavailable */
  228. static int dfs_check_chans_unavailable(struct hostapd_data *hapd,
  229. int start_chan_idx,
  230. int n_chans)
  231. {
  232. struct hostapd_channel_data *channel;
  233. struct hostapd_hw_modes *mode;
  234. int i, res = 0;
  235. mode = hapd->iface->current_mode;
  236. for(i = 0; i < n_chans; i++) {
  237. channel = &mode->channels[start_chan_idx + i];
  238. if (channel->flag & HOSTAPD_CHAN_DISABLED)
  239. res++;
  240. if ((channel->flag & HOSTAPD_CHAN_DFS_MASK) ==
  241. HOSTAPD_CHAN_DFS_UNAVAILABLE)
  242. res++;
  243. }
  244. return res;
  245. }
  246. static struct hostapd_channel_data * dfs_get_valid_channel(
  247. struct hostapd_data *hapd)
  248. {
  249. struct hostapd_hw_modes *mode;
  250. struct hostapd_channel_data *chan = NULL;
  251. int channel_idx, new_channel_idx;
  252. u32 _rand;
  253. wpa_printf(MSG_DEBUG, "DFS: Selecting random channel");
  254. if (hapd->iface->current_mode == NULL)
  255. return NULL;
  256. mode = hapd->iface->current_mode;
  257. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  258. return NULL;
  259. /* get random available channel */
  260. channel_idx = dfs_find_channel(hapd, NULL, 0);
  261. if (channel_idx > 0) {
  262. os_get_random((u8 *) &_rand, sizeof(_rand));
  263. new_channel_idx = _rand % channel_idx;
  264. dfs_find_channel(hapd, &chan, new_channel_idx);
  265. }
  266. /* VHT */
  267. dfs_adjust_vht_center_freq(hapd, chan);
  268. return chan;
  269. }
  270. static int set_dfs_state_freq(struct hostapd_data *hapd, int freq, u32 state)
  271. {
  272. struct hostapd_hw_modes *mode;
  273. struct hostapd_channel_data *chan = NULL;
  274. int i;
  275. mode = hapd->iface->current_mode;
  276. if (mode == NULL)
  277. return 0;
  278. wpa_printf(MSG_DEBUG, "set_dfs_state 0x%X for %d MHz", state, freq);
  279. for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
  280. chan = &hapd->iface->current_mode->channels[i];
  281. if (chan->freq == freq) {
  282. if (chan->flag & HOSTAPD_CHAN_RADAR) {
  283. chan->flag &= ~HOSTAPD_CHAN_DFS_MASK;
  284. chan->flag |= state;
  285. return 1; /* Channel found */
  286. }
  287. }
  288. }
  289. wpa_printf(MSG_WARNING, "Can't set DFS state for freq %d MHz", freq);
  290. return 0;
  291. }
  292. static int set_dfs_state(struct hostapd_data *hapd, int freq, int ht_enabled,
  293. int chan_offset, int chan_width, int cf1,
  294. int cf2, u32 state)
  295. {
  296. int n_chans = 1, i;
  297. struct hostapd_hw_modes *mode;
  298. int frequency = freq;
  299. int ret = 0;
  300. mode = hapd->iface->current_mode;
  301. if (mode == NULL)
  302. return 0;
  303. if (mode->mode != HOSTAPD_MODE_IEEE80211A) {
  304. wpa_printf(MSG_WARNING, "current_mode != IEEE80211A");
  305. return 0;
  306. }
  307. /* Seems cf1 and chan_width is enough here */
  308. switch (chan_width) {
  309. case CHAN_WIDTH_20_NOHT:
  310. case CHAN_WIDTH_20:
  311. n_chans = 1;
  312. frequency = cf1;
  313. break;
  314. case CHAN_WIDTH_40:
  315. n_chans = 2;
  316. frequency = cf1 - 10;
  317. break;
  318. case CHAN_WIDTH_80:
  319. n_chans = 4;
  320. frequency = cf1 - 30;
  321. break;
  322. case CHAN_WIDTH_160:
  323. n_chans = 8;
  324. frequency = cf1 - 70;
  325. break;
  326. default:
  327. wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
  328. chan_width);
  329. break;
  330. }
  331. wpa_printf(MSG_DEBUG, "DFS freq: %dMHz, n_chans: %d", frequency,
  332. n_chans);
  333. for (i = 0; i < n_chans; i++) {
  334. ret += set_dfs_state_freq(hapd, frequency, state);
  335. frequency = frequency + 20;
  336. }
  337. return ret;
  338. }
  339. static int dfs_are_channels_overlapped(struct hostapd_data *hapd, int freq,
  340. int chan_width, int cf1, int cf2)
  341. {
  342. int start_chan_idx;
  343. struct hostapd_hw_modes *mode;
  344. struct hostapd_channel_data *chan;
  345. int n_chans, i, j, frequency = freq, radar_n_chans = 1;
  346. u8 radar_chan;
  347. int res = 0;
  348. if (hapd->iface->freq == freq)
  349. res++;
  350. /* Our configuration */
  351. mode = hapd->iface->current_mode;
  352. start_chan_idx = dfs_get_start_chan_idx(hapd);
  353. n_chans = dfs_get_used_n_chans(hapd);
  354. /* Reported via radar event */
  355. switch (chan_width) {
  356. case CHAN_WIDTH_20_NOHT:
  357. case CHAN_WIDTH_20:
  358. radar_n_chans = 1;
  359. frequency = cf1;
  360. break;
  361. case CHAN_WIDTH_40:
  362. radar_n_chans = 2;
  363. frequency = cf1 - 10;
  364. break;
  365. case CHAN_WIDTH_80:
  366. radar_n_chans = 4;
  367. frequency = cf1 - 30;
  368. break;
  369. case CHAN_WIDTH_160:
  370. radar_n_chans = 8;
  371. frequency = cf1 - 70;
  372. break;
  373. default:
  374. wpa_printf(MSG_INFO, "DFS chan_width %d not supported",
  375. chan_width);
  376. break;
  377. }
  378. ieee80211_freq_to_chan(frequency, &radar_chan);
  379. for (i = 0; i < n_chans; i++) {
  380. chan = &mode->channels[start_chan_idx + i];
  381. for (j = 0; j < radar_n_chans; j++) {
  382. wpa_printf(MSG_DEBUG, "checking our: %d, radar: %d",
  383. chan->chan, radar_chan + j * 4);
  384. if (chan->chan == radar_chan + j * 4)
  385. res++;
  386. }
  387. }
  388. wpa_printf(MSG_DEBUG, "overlapped: %d", res);
  389. return res;
  390. }
  391. /*
  392. * Main DFS handler
  393. * 1 - continue channel/ap setup
  394. * 0 - channel/ap setup will be continued after CAC
  395. * -1 - hit critical error
  396. */
  397. int hostapd_handle_dfs(struct hostapd_data *hapd)
  398. {
  399. struct hostapd_channel_data *channel;
  400. int res, n_chans, start_chan_idx;
  401. do {
  402. /* Get start (first) channel for current configuration */
  403. start_chan_idx = dfs_get_start_chan_idx(hapd);
  404. if (start_chan_idx == -1)
  405. return -1;
  406. /* Get number of used channels, depend on width */
  407. n_chans = dfs_get_used_n_chans(hapd);
  408. /* Check if any of configured channels require DFS */
  409. res = dfs_check_chans_radar(hapd, start_chan_idx, n_chans);
  410. wpa_printf(MSG_DEBUG,
  411. "DFS %d channels required radar detection",
  412. res);
  413. if (!res)
  414. return 1;
  415. /* Check if all channels are DFS available */
  416. res = dfs_check_chans_available(hapd, start_chan_idx, n_chans);
  417. wpa_printf(MSG_DEBUG,
  418. "DFS all channels available, (SKIP CAC): %s",
  419. res ? "yes" : "no");
  420. if (res)
  421. return 1;
  422. /* Check if any of configured channels is unavailable */
  423. res = dfs_check_chans_unavailable(hapd, start_chan_idx,
  424. n_chans);
  425. wpa_printf(MSG_DEBUG, "DFS %d chans unavailable - choose other channel: %s",
  426. res, res ? "yes": "no");
  427. if (res) {
  428. channel = dfs_get_valid_channel(hapd);
  429. if (!channel) {
  430. wpa_printf(MSG_ERROR, "could not get valid channel");
  431. return -1;
  432. }
  433. hapd->iconf->channel = channel->chan;
  434. hapd->iface->freq = channel->freq;
  435. }
  436. } while (res);
  437. /* Finally start CAC */
  438. wpa_printf(MSG_DEBUG, "DFS start CAC on %d MHz", hapd->iface->freq);
  439. if (hostapd_start_dfs_cac(hapd, hapd->iconf->hw_mode,
  440. hapd->iface->freq,
  441. hapd->iconf->channel,
  442. hapd->iconf->ieee80211n,
  443. hapd->iconf->ieee80211ac,
  444. hapd->iconf->secondary_channel,
  445. hapd->iconf->vht_oper_chwidth,
  446. hapd->iconf->vht_oper_centr_freq_seg0_idx,
  447. hapd->iconf->vht_oper_centr_freq_seg1_idx)) {
  448. wpa_printf(MSG_DEBUG, "DFS start_dfs_cac() failed");
  449. return -1;
  450. }
  451. return 0;
  452. }
  453. int hostapd_dfs_complete_cac(struct hostapd_data *hapd, int success, int freq,
  454. int ht_enabled, int chan_offset, int chan_width,
  455. int cf1, int cf2)
  456. {
  457. struct hostapd_channel_data *channel;
  458. int err = 1;
  459. if (success) {
  460. /* Complete iface/ap configuration */
  461. set_dfs_state(hapd, freq, ht_enabled, chan_offset,
  462. chan_width, cf1, cf2,
  463. HOSTAPD_CHAN_DFS_AVAILABLE);
  464. hostapd_setup_interface_complete(hapd->iface, 0);
  465. } else {
  466. /* Switch to new channel */
  467. set_dfs_state(hapd, freq, ht_enabled, chan_offset,
  468. chan_width, cf1, cf2,
  469. HOSTAPD_CHAN_DFS_UNAVAILABLE);
  470. channel = dfs_get_valid_channel(hapd);
  471. if (channel) {
  472. hapd->iconf->channel = channel->chan;
  473. hapd->iface->freq = channel->freq;
  474. err = 0;
  475. } else
  476. wpa_printf(MSG_ERROR, "No valid channel available");
  477. hostapd_setup_interface_complete(hapd->iface, err);
  478. }
  479. return 0;
  480. }
  481. static int hostapd_dfs_start_channel_switch(struct hostapd_data *hapd)
  482. {
  483. struct hostapd_channel_data *channel;
  484. int err = 1;
  485. wpa_printf(MSG_DEBUG, "%s called", __func__);
  486. channel = dfs_get_valid_channel(hapd);
  487. if (channel) {
  488. hapd->iconf->channel = channel->chan;
  489. hapd->iface->freq = channel->freq;
  490. err = 0;
  491. }
  492. hapd->driver->stop_ap(hapd->drv_priv);
  493. hostapd_setup_interface_complete(hapd->iface, err);
  494. return 0;
  495. }
  496. int hostapd_dfs_radar_detected(struct hostapd_data *hapd, int freq,
  497. int ht_enabled, int chan_offset, int chan_width,
  498. int cf1, int cf2)
  499. {
  500. int res;
  501. if (!hapd->iconf->ieee80211h)
  502. return 0;
  503. /* mark radar frequency as invalid */
  504. res = set_dfs_state(hapd, freq, ht_enabled, chan_offset,
  505. chan_width, cf1, cf2,
  506. HOSTAPD_CHAN_DFS_UNAVAILABLE);
  507. /* Skip if reported radar event not overlapped our channels */
  508. res = dfs_are_channels_overlapped(hapd, freq, chan_width, cf1, cf2);
  509. if (!res)
  510. return 0;
  511. /* we are working on non-DFS channel - skip event */
  512. if (res == 0)
  513. return 0;
  514. /* radar detected while operating, switch the channel. */
  515. res = hostapd_dfs_start_channel_switch(hapd);
  516. return res;
  517. }
  518. int hostapd_dfs_nop_finished(struct hostapd_data *hapd, int freq,
  519. int ht_enabled, int chan_offset, int chan_width,
  520. int cf1, int cf2)
  521. {
  522. /* TODO add correct implementation here */
  523. set_dfs_state(hapd, freq, ht_enabled, chan_offset, chan_width, cf1, cf2,
  524. HOSTAPD_CHAN_DFS_USABLE);
  525. return 0;
  526. }