45 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47 #include <linux/kernel.h>
48 #include <linux/export.h>
49 #include <linux/slab.h>
50 #include <linux/list.h>
51 #include <linux/random.h>
52 #include <linux/ctype.h>
62 #ifdef CONFIG_CFG80211_REG_DEBUG
63 #define REG_DBG_PRINT(format, args...) \
64 printk(KERN_DEBUG pr_fmt(format), ##args)
66 #define REG_DBG_PRINT(args...)
108 static int reg_num_devs_support_basehint;
110 static inline void assert_reg_lock(
void)
143 REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
145 REG_RULE(2467-10, 2472+10, 40, 6, 20,
150 REG_RULE(2484-10, 2484+10, 20, 6, 20,
155 REG_RULE(5180-10, 5240+10, 40, 6, 20,
162 REG_RULE(5745-10, 5825+10, 40, 6, 20,
167 REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
174 static char *ieee80211_regdom =
"00";
175 static char user_alpha2[2];
180 static void reset_regdomains(
bool full_reset)
183 if (cfg80211_regdomain == cfg80211_world_regdom)
184 cfg80211_regdomain =
NULL;
185 if (cfg80211_world_regdom == &world_regdom)
186 cfg80211_world_regdom =
NULL;
187 if (cfg80211_regdomain == &world_regdom)
188 cfg80211_regdomain =
NULL;
190 kfree(cfg80211_regdomain);
191 kfree(cfg80211_world_regdom);
193 cfg80211_world_regdom = &world_regdom;
194 cfg80211_regdomain =
NULL;
199 if (last_request != &core_request_world)
201 last_request = &core_request_world;
212 reset_regdomains(
false);
214 cfg80211_world_regdom =
rd;
215 cfg80211_regdomain =
rd;
222 if (alpha2[0] ==
'0' && alpha2[1] ==
'0')
227 static bool is_alpha2_set(
const char *
alpha2)
231 if (alpha2[0] != 0 && alpha2[1] != 0)
236 static bool is_unknown_alpha2(
const char *alpha2)
244 if (alpha2[0] ==
'9' && alpha2[1] ==
'9')
249 static bool is_intersected_alpha2(
const char *alpha2)
258 if (alpha2[0] ==
'9' && alpha2[1] ==
'8')
263 static bool is_an_alpha2(
const char *alpha2)
272 static bool alpha2_equal(
const char *alpha2_x,
const char *alpha2_y)
274 if (!alpha2_x || !alpha2_y)
276 if (alpha2_x[0] == alpha2_y[0] &&
277 alpha2_x[1] == alpha2_y[1])
282 static bool regdom_changes(
const char *alpha2)
284 assert_cfg80211_lock();
286 if (!cfg80211_regdomain)
288 if (alpha2_equal(cfg80211_regdomain->
alpha2, alpha2))
298 static bool is_user_regdom_saved(
void)
300 if (user_alpha2[0] ==
'9' && user_alpha2[1] ==
'7')
305 !is_an_alpha2(user_alpha2)),
306 "Unexpected user alpha2: %c%c\n",
318 int size_of_regd = 0;
338 #ifdef CONFIG_CFG80211_INTERNAL_REGDB
339 struct reg_regdb_search_request {
349 struct reg_regdb_search_request *
request;
352 bool set_reg =
false;
357 while (!list_empty(®_regdb_search_list)) {
359 struct reg_regdb_search_request,
367 r = reg_copy_regd(®dom, curdom);
387 static void reg_regdb_query(
const char *alpha2)
389 struct reg_regdb_search_request *
request;
394 request = kzalloc(
sizeof(
struct reg_regdb_search_request),
GFP_KERNEL);
398 memcpy(request->alpha2, alpha2, 2);
408 static void reg_regdb_size_check(
void)
411 WARN_ONCE(!reg_regdb_size,
"db.txt is empty, you should update it...");
414 static inline void reg_regdb_size_check(
void) {}
415 static inline void reg_regdb_query(
const char *alpha2) {}
423 static int call_crda(
const char *alpha2)
426 pr_info(
"Calling CRDA for country: %c%c\n",
427 alpha2[0], alpha2[1]);
429 pr_info(
"Calling CRDA to update world regulatory domain\n");
432 reg_regdb_query(alpha2);
440 assert_cfg80211_lock();
445 return alpha2_equal(last_request->
alpha2, alpha2);
482 if (!is_valid_reg_rule(reg_rule))
493 u32 start_freq_khz, end_freq_khz;
495 start_freq_khz = center_freq_khz - (bw_khz/2);
496 end_freq_khz = center_freq_khz + (bw_khz/2);
499 end_freq_khz <= freq_range->end_freq_khz)
523 #define ONE_GHZ_IN_KHZ 1000000
536 #undef ONE_GHZ_IN_KHZ
543 static int reg_rules_intersect(
580 if (!is_valid_reg_rule(intersected_rule))
605 unsigned int num_rules = 0, rule_idx = 0;
613 intersected_rule = &irule;
632 if (!reg_rules_intersect(rule1, rule2,
635 memset(intersected_rule, 0,
659 intersected_rule = &rd->
reg_rules[rule_idx];
660 r = reg_rules_intersect(rule1, rule2,
672 if (rule_idx != num_rules) {
688 static u32 map_regdom_flags(
u32 rd_flags)
690 u32 channel_flags = 0;
699 return channel_flags;
702 static int freq_reg_info_regd(
struct wiphy *
wiphy,
709 bool band_rule_found =
false;
711 bool bw_fits =
false;
743 if (!band_rule_found)
744 band_rule_found = freq_in_rule_band(fr, center_freq);
746 bw_fits = reg_does_bw_fit(fr,
750 if (band_rule_found && bw_fits) {
756 if (!band_rule_found)
767 assert_cfg80211_lock();
768 return freq_reg_info_regd(wiphy,
776 #ifdef CONFIG_CFG80211_REG_DEBUG
781 return "Set by core";
783 return "Set by user";
785 return "Set by driver";
787 return "Set by country IE";
800 char max_antenna_gain[32];
806 snprintf(max_antenna_gain, 32,
"N/A");
811 "for a %d MHz width channel with regulatory rule:\n",
815 REG_DBG_PRINT(
"%d KHz - %d KHz @ %d KHz), (%s mBi, %d mBm)\n",
840 static void handle_channel(
struct wiphy *wiphy,
843 unsigned int chan_idx)
853 struct wiphy *request_wiphy =
NULL;
855 assert_cfg80211_lock();
859 sband = wiphy->
bands[band];
890 chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
899 request_wiphy && request_wiphy == wiphy &&
907 map_regdom_flags(reg_rule->
flags) | bw_flags;
916 chan->
flags = flags | bw_flags | map_regdom_flags(reg_rule->
flags);
937 static void handle_band(
struct wiphy *wiphy,
948 handle_channel(wiphy, initiator, band, i);
963 assert_cfg80211_lock();
966 val = reg_request_cell_base(last_request);
971 #ifdef CONFIG_CFG80211_CERTIFICATION_ONUS
976 if (!reg_num_devs_support_basehint)
979 if (reg_request_cell_base(last_request)) {
980 if (!regdom_changes(pending_request->
alpha2))
988 static bool reg_dev_ignore_cell_hint(
struct wiphy *wiphy)
999 static int reg_dev_ignore_cell_hint(
struct wiphy *wiphy)
1006 static bool ignore_reg_update(
struct wiphy *wiphy,
1009 if (!last_request) {
1011 "last_request is not set\n",
1012 reg_initiator_name(initiator));
1019 "since the driver uses its own custom "
1020 "regulatory domain\n",
1021 reg_initiator_name(initiator));
1033 "since the driver requires its own regulatory "
1034 "domain to be set first\n",
1035 reg_initiator_name(initiator));
1039 if (reg_request_cell_base(last_request))
1040 return reg_dev_ignore_cell_hint(wiphy);
1045 static void handle_reg_beacon(
struct wiphy *wiphy,
1046 unsigned int chan_idx,
1051 bool channel_changed =
false;
1054 assert_cfg80211_lock();
1056 sband = wiphy->
bands[reg_beacon->
chan.band];
1071 chan_before.flags = chan->
flags;
1075 channel_changed =
true;
1080 channel_changed =
true;
1083 if (channel_changed)
1091 static void wiphy_update_new_beacon(
struct wiphy *wiphy,
1092 struct reg_beacon *reg_beacon)
1097 assert_cfg80211_lock();
1099 if (!wiphy->
bands[reg_beacon->
chan.band])
1102 sband = wiphy->
bands[reg_beacon->
chan.band];
1105 handle_reg_beacon(wiphy, i, reg_beacon);
1111 static void wiphy_update_beacon_reg(
struct wiphy *wiphy)
1115 struct reg_beacon *reg_beacon;
1117 assert_cfg80211_lock();
1119 if (list_empty(®_beacon_list))
1123 if (!wiphy->
bands[reg_beacon->
chan.band])
1125 sband = wiphy->
bands[reg_beacon->
chan.band];
1127 handle_reg_beacon(wiphy, i, reg_beacon);
1131 static bool reg_is_world_roaming(
struct wiphy *wiphy)
1144 static void reg_process_beacons(
struct wiphy *wiphy)
1152 if (!reg_is_world_roaming(wiphy))
1154 wiphy_update_beacon_reg(wiphy);
1169 static void reg_process_ht_flags_channel(
struct wiphy *wiphy,
1171 unsigned int chan_idx)
1178 assert_cfg80211_lock();
1182 channel = &sband->
channels[chan_idx];
1184 if (is_ht40_not_allowed(channel)) {
1206 if (is_ht40_not_allowed(channel_before))
1211 if (is_ht40_not_allowed(channel_after))
1217 static void reg_process_ht_flags_band(
struct wiphy *wiphy,
1227 reg_process_ht_flags_channel(wiphy, band, i);
1230 static void reg_process_ht_flags(
struct wiphy *wiphy)
1238 if (wiphy->
bands[band])
1239 reg_process_ht_flags_band(wiphy, band);
1244 static void wiphy_update_regulatory(
struct wiphy *wiphy,
1251 if (ignore_reg_update(wiphy, initiator))
1257 if (wiphy->
bands[band])
1258 handle_band(wiphy, band, initiator);
1261 reg_process_beacons(wiphy);
1262 reg_process_ht_flags(wiphy);
1270 struct wiphy *wiphy;
1273 wiphy = &rdev->wiphy;
1274 wiphy_update_regulatory(wiphy, initiator);
1287 static void handle_channel_custom(
struct wiphy *wiphy,
1289 unsigned int chan_idx,
1307 r = freq_reg_info_regd(wiphy,
1315 "regd has no rule that fits a %d MHz "
1323 chan_reg_rule_print_dbg(chan, desired_bw_khz, reg_rule);
1331 chan->
flags |= map_regdom_flags(reg_rule->
flags) | bw_flags;
1337 static void handle_band_custom(
struct wiphy *wiphy,
enum ieee80211_band band,
1347 handle_channel_custom(wiphy, band, i, regd);
1355 unsigned int bands_set = 0;
1359 if (!wiphy->
bands[band])
1361 handle_band_custom(wiphy, band, regd);
1378 #define REG_INTERSECT 1
1382 static int ignore_request(
struct wiphy *wiphy,
1385 struct wiphy *last_wiphy =
NULL;
1387 assert_cfg80211_lock();
1398 if (reg_request_cell_base(last_request)) {
1400 if (regdom_changes(pending_request->
alpha2))
1411 if (last_wiphy != wiphy) {
1418 if (regdom_changes(pending_request->
alpha2))
1433 if (regdom_changes(pending_request->
alpha2))
1444 !regdom_changes(pending_request->
alpha2))
1449 if (reg_request_cell_base(pending_request))
1450 return reg_ignore_cell_hint(pending_request);
1452 if (reg_request_cell_base(last_request))
1471 if (regdom_changes(last_request->
alpha2))
1475 if (!regdom_changes(pending_request->
alpha2))
1484 static void reg_set_request_processed(
void)
1486 bool need_more_processing =
false;
1490 spin_lock(®_requests_lock);
1491 if (!list_empty(®_requests_list))
1492 need_more_processing =
true;
1493 spin_unlock(®_requests_lock);
1498 if (need_more_processing)
1516 static int __regulatory_hint(
struct wiphy *wiphy,
1519 bool intersect =
false;
1522 assert_cfg80211_lock();
1524 r = ignore_request(wiphy, pending_request);
1529 r = reg_copy_regd(&wiphy->
regd, cfg80211_regdomain);
1531 kfree(pending_request);
1545 r = reg_copy_regd(&wiphy->
regd, cfg80211_regdomain);
1547 kfree(pending_request);
1553 kfree(pending_request);
1558 if (last_request != &core_request_world)
1559 kfree(last_request);
1561 last_request = pending_request;
1564 pending_request =
NULL;
1567 user_alpha2[0] = last_request->
alpha2[0];
1568 user_alpha2[1] = last_request->
alpha2[1];
1580 reg_set_request_processed();
1585 return call_crda(last_request->
alpha2);
1593 struct wiphy *wiphy =
NULL;
1597 if (wiphy_idx_valid(reg_request->
wiphy_idx))
1606 r = __regulatory_hint(wiphy, reg_request);
1610 wiphy_update_regulatory(wiphy, reg_initiator);
1628 static void reg_process_pending_hints(
void)
1636 if (last_request && !last_request->
processed) {
1638 "for it to be processed...\n");
1642 spin_lock(®_requests_lock);
1644 if (list_empty(®_requests_list)) {
1645 spin_unlock(®_requests_lock);
1652 list_del_init(®_request->
list);
1654 spin_unlock(®_requests_lock);
1656 reg_process_hint(reg_request, reg_request->
initiator);
1664 static void reg_process_pending_beacon_hints(
void)
1667 struct reg_beacon *pending_beacon, *
tmp;
1676 spin_lock_bh(®_pending_beacons_lock);
1678 if (list_empty(®_pending_beacons)) {
1679 spin_unlock_bh(®_pending_beacons_lock);
1684 ®_pending_beacons,
list) {
1686 list_del_init(&pending_beacon->
list);
1690 wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
1696 spin_unlock_bh(®_pending_beacons_lock);
1703 reg_process_pending_hints();
1704 reg_process_pending_beacon_hints();
1714 spin_lock(®_requests_lock);
1716 spin_unlock(®_requests_lock);
1725 static int regulatory_hint_core(
const char *alpha2)
1734 request->
alpha2[0] = alpha2[0];
1735 request->
alpha2[1] = alpha2[1];
1738 queue_regulatory_request(request);
1756 request->
alpha2[0] = alpha2[0];
1757 request->
alpha2[1] = alpha2[1];
1761 queue_regulatory_request(request);
1783 request->
alpha2[0] = alpha2[0];
1784 request->
alpha2[1] = alpha2[1];
1787 queue_regulatory_request(request);
1812 if (country_ie_len & 0x01)
1818 alpha2[0] = country_ie[0];
1819 alpha2[1] = country_ie[1];
1821 if (country_ie[2] ==
'I')
1823 else if (country_ie[2] ==
'O')
1833 wiphy_idx_valid(last_request->
wiphy_idx)))
1841 request->
alpha2[0] = alpha2[0];
1842 request->
alpha2[1] = alpha2[1];
1848 queue_regulatory_request(request);
1856 static void restore_alpha2(
char *alpha2,
bool reset_user)
1863 if (is_user_regdom_saved()) {
1867 "including user preference\n");
1868 user_alpha2[0] =
'9';
1869 user_alpha2[1] =
'7';
1878 "module parameter ieee80211_regdom: %c%c\n",
1879 ieee80211_regdom[0],
1880 ieee80211_regdom[1]);
1881 alpha2[0] = ieee80211_regdom[0];
1882 alpha2[1] = ieee80211_regdom[1];
1886 "while preserving user preference for: %c%c\n",
1889 alpha2[0] = user_alpha2[0];
1890 alpha2[1] = user_alpha2[1];
1894 "module parameter ieee80211_regdom: %c%c\n",
1895 ieee80211_regdom[0],
1896 ieee80211_regdom[1]);
1897 alpha2[0] = ieee80211_regdom[0];
1898 alpha2[1] = ieee80211_regdom[1];
1903 static void restore_custom_reg_settings(
struct wiphy *wiphy)
1939 static void restore_regulatory_settings(
bool reset_user)
1942 char world_alpha2[2];
1943 struct reg_beacon *reg_beacon, *btmp;
1951 reset_regdomains(
true);
1952 restore_alpha2(alpha2, reset_user);
1960 spin_lock(®_requests_lock);
1961 if (!list_empty(®_requests_list)) {
1963 ®_requests_list,
list) {
1967 list_move_tail(®_request->
list, &tmp_reg_req_list);
1970 spin_unlock(®_requests_lock);
1973 spin_lock_bh(®_pending_beacons_lock);
1974 if (!list_empty(®_pending_beacons)) {
1976 ®_pending_beacons,
list) {
1981 spin_unlock_bh(®_pending_beacons_lock);
1983 if (!list_empty(®_beacon_list)) {
1985 ®_beacon_list,
list) {
1992 cfg80211_regdomain = cfg80211_world_regdom;
1993 world_alpha2[0] = cfg80211_regdomain->
alpha2[0];
1994 world_alpha2[1] = cfg80211_regdomain->
alpha2[1];
1998 restore_custom_reg_settings(&rdev->wiphy);
2004 regulatory_hint_core(world_alpha2);
2011 if (is_an_alpha2(alpha2))
2014 if (list_empty(&tmp_reg_req_list))
2020 spin_lock(®_requests_lock);
2026 list_move_tail(®_request->
list, ®_requests_list);
2028 spin_unlock(®_requests_lock);
2041 "restore regulatory settings\n");
2042 restore_regulatory_settings(
false);
2045 static bool freq_is_chan_12_13_14(
u16 freq)
2058 struct reg_beacon *reg_beacon;
2063 !freq_is_chan_12_13_14(beacon_chan->
center_freq)))))
2066 reg_beacon = kzalloc(
sizeof(
struct reg_beacon), gfp);
2071 "frequency: %d MHz (Ch %d) on %s\n",
2084 spin_lock_bh(®_pending_beacons_lock);
2086 spin_unlock_bh(®_pending_beacons_lock);
2100 pr_info(
" (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)\n");
2112 pr_info(
" (%d KHz - %d KHz @ %d KHz), (%d mBi, %d mBm)\n",
2119 pr_info(
" (%d KHz - %d KHz @ %d KHz), (N/A, %d mBm)\n",
2129 switch (dfs_region) {
2142 static void print_dfs_region(
u8 dfs_region)
2147 switch (dfs_region) {
2149 pr_info(
" DFS Master region FCC");
2152 pr_info(
" DFS Master region ETSI");
2155 pr_info(
" DFS Master region JP");
2158 pr_info(
" DFS Master region Uknown");
2166 if (is_intersected_alpha2(rd->
alpha2)) {
2174 pr_info(
"Current regulatory domain updated by AP to: %c%c\n",
2178 pr_info(
"Current regulatory domain intersected:\n");
2180 pr_info(
"Current regulatory domain intersected:\n");
2182 pr_info(
"World regulatory domain updated:\n");
2184 if (is_unknown_alpha2(rd->
alpha2))
2185 pr_info(
"Regulatory domain changed to driver built-in settings (unknown country)\n");
2187 if (reg_request_cell_base(last_request))
2188 pr_info(
"Regulatory domain changed "
2189 "to country: %c%c by Cell Station\n",
2192 pr_info(
"Regulatory domain changed "
2193 "to country: %c%c\n",
2211 struct wiphy *request_wiphy;
2217 update_world_regdomain(rd);
2221 if (!is_alpha2_set(rd->
alpha2) && !is_an_alpha2(rd->
alpha2) &&
2222 !is_unknown_alpha2(rd->
alpha2))
2238 if (!regdom_changes(rd->
alpha2))
2252 if (!is_valid_rd(rd)) {
2253 pr_err(
"Invalid regulatory domain detected:\n");
2254 print_regdomain_info(rd);
2259 if (!request_wiphy &&
2270 reset_regdomains(
false);
2271 cfg80211_regdomain =
rd;
2284 if (request_wiphy->
regd)
2287 r = reg_copy_regd(&request_wiphy->
regd, rd);
2291 reset_regdomains(
false);
2292 cfg80211_regdomain =
rd;
2300 intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
2301 if (!intersected_rd)
2310 request_wiphy->
regd =
rd;
2316 reset_regdomains(
false);
2317 cfg80211_regdomain = intersected_rd;
2335 assert_cfg80211_lock();
2340 r = __set_regdom(rd);
2343 reg_set_request_processed();
2352 BUG_ON(rd != cfg80211_regdomain);
2355 update_all_wiphy_regulatory(last_request->
initiator);
2357 print_regdomain(cfg80211_regdomain);
2361 reg_set_request_processed();
2368 #ifdef CONFIG_HOTPLUG
2371 if (last_request && !last_request->
processed) {
2374 last_request->
alpha2[1]))
2389 assert_cfg80211_lock();
2393 if (!reg_dev_ignore_cell_hint(wiphy))
2394 reg_num_devs_support_basehint++;
2404 struct wiphy *request_wiphy =
NULL;
2406 assert_cfg80211_lock();
2410 if (!reg_dev_ignore_cell_hint(wiphy))
2411 reg_num_devs_support_basehint--;
2418 if (!request_wiphy || request_wiphy != wiphy)
2427 static void reg_timeout_work(
struct work_struct *work)
2430 "restoring regulatory settings\n");
2431 restore_regulatory_settings(
true);
2438 reg_pdev = platform_device_register_simple(
"regulatory", 0,
NULL, 0);
2439 if (IS_ERR(reg_pdev))
2440 return PTR_ERR(reg_pdev);
2442 reg_pdev->
dev.type = ®_device_type;
2447 reg_regdb_size_check();
2449 cfg80211_regdomain = cfg80211_world_regdom;
2451 user_alpha2[0] =
'9';
2452 user_alpha2[1] =
'7';
2455 err = regulatory_hint_core(cfg80211_regdomain->
alpha2);
2466 pr_err(
"kobject_uevent_env() was unable to call CRDA during init\n");
2467 #ifdef CONFIG_CFG80211_REG_DEBUG
2487 struct reg_beacon *reg_beacon, *btmp;
2495 reset_regdomains(
true);
2497 dev_set_uevent_suppress(®_pdev->
dev,
true);
2501 spin_lock_bh(®_pending_beacons_lock);
2502 if (!list_empty(®_pending_beacons)) {
2504 ®_pending_beacons,
list) {
2509 spin_unlock_bh(®_pending_beacons_lock);
2511 if (!list_empty(®_beacon_list)) {
2513 ®_beacon_list,
list) {
2519 spin_lock(®_requests_lock);
2520 if (!list_empty(®_requests_list)) {
2522 ®_requests_list,
list) {
2527 spin_unlock(®_requests_lock);