reg.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. /**
  12. * DOC: Wireless regulatory infrastructure
  13. *
  14. * The usual implementation is for a driver to read a device EEPROM to
  15. * determine which regulatory domain it should be operating under, then
  16. * looking up the allowable channels in a driver-local table and finally
  17. * registering those channels in the wiphy structure.
  18. *
  19. * Another set of compliance enforcement is for drivers to use their
  20. * own compliance limits which can be stored on the EEPROM. The host
  21. * driver or firmware may ensure these are used.
  22. *
  23. * In addition to all this we provide an extra layer of regulatory
  24. * conformance. For drivers which do not have any regulatory
  25. * information CRDA provides the complete regulatory solution.
  26. * For others it provides a community effort on further restrictions
  27. * to enhance compliance.
  28. *
  29. * Note: When number of rules --> infinity we will not be able to
  30. * index on alpha2 any more, instead we'll probably have to
  31. * rely on some SHA1 checksum of the regdomain for example.
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/list.h>
  36. #include <linux/random.h>
  37. #include <linux/nl80211.h>
  38. #include <linux/platform_device.h>
  39. #include <net/cfg80211.h>
  40. #include "core.h"
  41. #include "reg.h"
  42. #include "regdb.h"
  43. #include "nl80211.h"
  44. #ifdef CONFIG_CFG80211_REG_DEBUG
  45. #define REG_DBG_PRINT(format, args...) \
  46. do { \
  47. printk(KERN_DEBUG format , ## args); \
  48. } while (0)
  49. #else
  50. #define REG_DBG_PRINT(args...)
  51. #endif
  52. /* Receipt of information from last regulatory request */
  53. static struct regulatory_request *last_request;
  54. /* To trigger userspace events */
  55. static struct platform_device *reg_pdev;
  56. /*
  57. * Central wireless core regulatory domains, we only need two,
  58. * the current one and a world regulatory domain in case we have no
  59. * information to give us an alpha2
  60. */
  61. const struct ieee80211_regdomain *cfg80211_regdomain;
  62. /*
  63. * We use this as a place for the rd structure built from the
  64. * last parsed country IE to rest until CRDA gets back to us with
  65. * what it thinks should apply for the same country
  66. */
  67. static const struct ieee80211_regdomain *country_ie_regdomain;
  68. /*
  69. * Protects static reg.c components:
  70. * - cfg80211_world_regdom
  71. * - cfg80211_regdom
  72. * - country_ie_regdomain
  73. * - last_request
  74. */
  75. DEFINE_MUTEX(reg_mutex);
  76. #define assert_reg_lock() WARN_ON(!mutex_is_locked(&reg_mutex))
  77. /* Used to queue up regulatory hints */
  78. static LIST_HEAD(reg_requests_list);
  79. static spinlock_t reg_requests_lock;
  80. /* Used to queue up beacon hints for review */
  81. static LIST_HEAD(reg_pending_beacons);
  82. static spinlock_t reg_pending_beacons_lock;
  83. /* Used to keep track of processed beacon hints */
  84. static LIST_HEAD(reg_beacon_list);
  85. struct reg_beacon {
  86. struct list_head list;
  87. struct ieee80211_channel chan;
  88. };
  89. /* We keep a static world regulatory domain in case of the absence of CRDA */
  90. static const struct ieee80211_regdomain world_regdom = {
  91. .n_reg_rules = 5,
  92. .alpha2 = "00",
  93. .reg_rules = {
  94. /* IEEE 802.11b/g, channels 1..11 */
  95. REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
  96. /* IEEE 802.11b/g, channels 12..13. No HT40
  97. * channel fits here. */
  98. REG_RULE(2467-10, 2472+10, 20, 6, 20,
  99. NL80211_RRF_PASSIVE_SCAN |
  100. NL80211_RRF_NO_IBSS),
  101. /* IEEE 802.11 channel 14 - Only JP enables
  102. * this and for 802.11b only */
  103. REG_RULE(2484-10, 2484+10, 20, 6, 20,
  104. NL80211_RRF_PASSIVE_SCAN |
  105. NL80211_RRF_NO_IBSS |
  106. NL80211_RRF_NO_OFDM),
  107. /* IEEE 802.11a, channel 36..48 */
  108. REG_RULE(5180-10, 5240+10, 40, 6, 20,
  109. NL80211_RRF_PASSIVE_SCAN |
  110. NL80211_RRF_NO_IBSS),
  111. /* NB: 5260 MHz - 5700 MHz requies DFS */
  112. /* IEEE 802.11a, channel 149..165 */
  113. REG_RULE(5745-10, 5825+10, 40, 6, 20,
  114. NL80211_RRF_PASSIVE_SCAN |
  115. NL80211_RRF_NO_IBSS),
  116. }
  117. };
  118. static const struct ieee80211_regdomain *cfg80211_world_regdom =
  119. &world_regdom;
  120. static char *ieee80211_regdom = "00";
  121. module_param(ieee80211_regdom, charp, 0444);
  122. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  123. static void reset_regdomains(void)
  124. {
  125. /* avoid freeing static information or freeing something twice */
  126. if (cfg80211_regdomain == cfg80211_world_regdom)
  127. cfg80211_regdomain = NULL;
  128. if (cfg80211_world_regdom == &world_regdom)
  129. cfg80211_world_regdom = NULL;
  130. if (cfg80211_regdomain == &world_regdom)
  131. cfg80211_regdomain = NULL;
  132. kfree(cfg80211_regdomain);
  133. kfree(cfg80211_world_regdom);
  134. cfg80211_world_regdom = &world_regdom;
  135. cfg80211_regdomain = NULL;
  136. }
  137. /*
  138. * Dynamic world regulatory domain requested by the wireless
  139. * core upon initialization
  140. */
  141. static void update_world_regdomain(const struct ieee80211_regdomain *rd)
  142. {
  143. BUG_ON(!last_request);
  144. reset_regdomains();
  145. cfg80211_world_regdom = rd;
  146. cfg80211_regdomain = rd;
  147. }
  148. bool is_world_regdom(const char *alpha2)
  149. {
  150. if (!alpha2)
  151. return false;
  152. if (alpha2[0] == '0' && alpha2[1] == '0')
  153. return true;
  154. return false;
  155. }
  156. static bool is_alpha2_set(const char *alpha2)
  157. {
  158. if (!alpha2)
  159. return false;
  160. if (alpha2[0] != 0 && alpha2[1] != 0)
  161. return true;
  162. return false;
  163. }
  164. static bool is_alpha_upper(char letter)
  165. {
  166. /* ASCII A - Z */
  167. if (letter >= 65 && letter <= 90)
  168. return true;
  169. return false;
  170. }
  171. static bool is_unknown_alpha2(const char *alpha2)
  172. {
  173. if (!alpha2)
  174. return false;
  175. /*
  176. * Special case where regulatory domain was built by driver
  177. * but a specific alpha2 cannot be determined
  178. */
  179. if (alpha2[0] == '9' && alpha2[1] == '9')
  180. return true;
  181. return false;
  182. }
  183. static bool is_intersected_alpha2(const char *alpha2)
  184. {
  185. if (!alpha2)
  186. return false;
  187. /*
  188. * Special case where regulatory domain is the
  189. * result of an intersection between two regulatory domain
  190. * structures
  191. */
  192. if (alpha2[0] == '9' && alpha2[1] == '8')
  193. return true;
  194. return false;
  195. }
  196. static bool is_an_alpha2(const char *alpha2)
  197. {
  198. if (!alpha2)
  199. return false;
  200. if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
  201. return true;
  202. return false;
  203. }
  204. static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
  205. {
  206. if (!alpha2_x || !alpha2_y)
  207. return false;
  208. if (alpha2_x[0] == alpha2_y[0] &&
  209. alpha2_x[1] == alpha2_y[1])
  210. return true;
  211. return false;
  212. }
  213. static bool regdom_changes(const char *alpha2)
  214. {
  215. assert_cfg80211_lock();
  216. if (!cfg80211_regdomain)
  217. return true;
  218. if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
  219. return false;
  220. return true;
  221. }
  222. /**
  223. * country_ie_integrity_changes - tells us if the country IE has changed
  224. * @checksum: checksum of country IE of fields we are interested in
  225. *
  226. * If the country IE has not changed you can ignore it safely. This is
  227. * useful to determine if two devices are seeing two different country IEs
  228. * even on the same alpha2. Note that this will return false if no IE has
  229. * been set on the wireless core yet.
  230. */
  231. static bool country_ie_integrity_changes(u32 checksum)
  232. {
  233. /* If no IE has been set then the checksum doesn't change */
  234. if (unlikely(!last_request->country_ie_checksum))
  235. return false;
  236. if (unlikely(last_request->country_ie_checksum != checksum))
  237. return true;
  238. return false;
  239. }
  240. static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
  241. const struct ieee80211_regdomain *src_regd)
  242. {
  243. struct ieee80211_regdomain *regd;
  244. int size_of_regd = 0;
  245. unsigned int i;
  246. size_of_regd = sizeof(struct ieee80211_regdomain) +
  247. ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
  248. regd = kzalloc(size_of_regd, GFP_KERNEL);
  249. if (!regd)
  250. return -ENOMEM;
  251. memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  252. for (i = 0; i < src_regd->n_reg_rules; i++)
  253. memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
  254. sizeof(struct ieee80211_reg_rule));
  255. *dst_regd = regd;
  256. return 0;
  257. }
  258. #ifdef CONFIG_CFG80211_INTERNAL_REGDB
  259. struct reg_regdb_search_request {
  260. char alpha2[2];
  261. struct list_head list;
  262. };
  263. static LIST_HEAD(reg_regdb_search_list);
  264. static DEFINE_SPINLOCK(reg_regdb_search_lock);
  265. static void reg_regdb_search(struct work_struct *work)
  266. {
  267. struct reg_regdb_search_request *request;
  268. const struct ieee80211_regdomain *curdom, *regdom;
  269. int i, r;
  270. spin_lock(&reg_regdb_search_lock);
  271. while (!list_empty(&reg_regdb_search_list)) {
  272. request = list_first_entry(&reg_regdb_search_list,
  273. struct reg_regdb_search_request,
  274. list);
  275. list_del(&request->list);
  276. for (i=0; i<reg_regdb_size; i++) {
  277. curdom = reg_regdb[i];
  278. if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
  279. r = reg_copy_regd(&regdom, curdom);
  280. if (r)
  281. break;
  282. spin_unlock(&reg_regdb_search_lock);
  283. mutex_lock(&cfg80211_mutex);
  284. set_regdom(regdom);
  285. mutex_unlock(&cfg80211_mutex);
  286. spin_lock(&reg_regdb_search_lock);
  287. break;
  288. }
  289. }
  290. kfree(request);
  291. }
  292. spin_unlock(&reg_regdb_search_lock);
  293. }
  294. static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
  295. static void reg_regdb_query(const char *alpha2)
  296. {
  297. struct reg_regdb_search_request *request;
  298. if (!alpha2)
  299. return;
  300. request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
  301. if (!request)
  302. return;
  303. memcpy(request->alpha2, alpha2, 2);
  304. spin_lock(&reg_regdb_search_lock);
  305. list_add_tail(&request->list, &reg_regdb_search_list);
  306. spin_unlock(&reg_regdb_search_lock);
  307. schedule_work(&reg_regdb_work);
  308. }
  309. #else
  310. static inline void reg_regdb_query(const char *alpha2) {}
  311. #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
  312. /*
  313. * This lets us keep regulatory code which is updated on a regulatory
  314. * basis in userspace.
  315. */
  316. static int call_crda(const char *alpha2)
  317. {
  318. char country_env[9 + 2] = "COUNTRY=";
  319. char *envp[] = {
  320. country_env,
  321. NULL
  322. };
  323. if (!is_world_regdom((char *) alpha2))
  324. printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
  325. alpha2[0], alpha2[1]);
  326. else
  327. printk(KERN_INFO "cfg80211: Calling CRDA to update world "
  328. "regulatory domain\n");
  329. /* query internal regulatory database (if it exists) */
  330. reg_regdb_query(alpha2);
  331. country_env[8] = alpha2[0];
  332. country_env[9] = alpha2[1];
  333. return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
  334. }
  335. /* Used by nl80211 before kmalloc'ing our regulatory domain */
  336. bool reg_is_valid_request(const char *alpha2)
  337. {
  338. assert_cfg80211_lock();
  339. if (!last_request)
  340. return false;
  341. return alpha2_equal(last_request->alpha2, alpha2);
  342. }
  343. /* Sanity check on a regulatory rule */
  344. static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
  345. {
  346. const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  347. u32 freq_diff;
  348. if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
  349. return false;
  350. if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  351. return false;
  352. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  353. if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
  354. freq_range->max_bandwidth_khz > freq_diff)
  355. return false;
  356. return true;
  357. }
  358. static bool is_valid_rd(const struct ieee80211_regdomain *rd)
  359. {
  360. const struct ieee80211_reg_rule *reg_rule = NULL;
  361. unsigned int i;
  362. if (!rd->n_reg_rules)
  363. return false;
  364. if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
  365. return false;
  366. for (i = 0; i < rd->n_reg_rules; i++) {
  367. reg_rule = &rd->reg_rules[i];
  368. if (!is_valid_reg_rule(reg_rule))
  369. return false;
  370. }
  371. return true;
  372. }
  373. static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
  374. u32 center_freq_khz,
  375. u32 bw_khz)
  376. {
  377. u32 start_freq_khz, end_freq_khz;
  378. start_freq_khz = center_freq_khz - (bw_khz/2);
  379. end_freq_khz = center_freq_khz + (bw_khz/2);
  380. if (start_freq_khz >= freq_range->start_freq_khz &&
  381. end_freq_khz <= freq_range->end_freq_khz)
  382. return true;
  383. return false;
  384. }
  385. /**
  386. * freq_in_rule_band - tells us if a frequency is in a frequency band
  387. * @freq_range: frequency rule we want to query
  388. * @freq_khz: frequency we are inquiring about
  389. *
  390. * This lets us know if a specific frequency rule is or is not relevant to
  391. * a specific frequency's band. Bands are device specific and artificial
  392. * definitions (the "2.4 GHz band" and the "5 GHz band"), however it is
  393. * safe for now to assume that a frequency rule should not be part of a
  394. * frequency's band if the start freq or end freq are off by more than 2 GHz.
  395. * This resolution can be lowered and should be considered as we add
  396. * regulatory rule support for other "bands".
  397. **/
  398. static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
  399. u32 freq_khz)
  400. {
  401. #define ONE_GHZ_IN_KHZ 1000000
  402. if (abs(freq_khz - freq_range->start_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  403. return true;
  404. if (abs(freq_khz - freq_range->end_freq_khz) <= (2 * ONE_GHZ_IN_KHZ))
  405. return true;
  406. return false;
  407. #undef ONE_GHZ_IN_KHZ
  408. }
  409. /*
  410. * Converts a country IE to a regulatory domain. A regulatory domain
  411. * structure has a lot of information which the IE doesn't yet have,
  412. * so for the other values we use upper max values as we will intersect
  413. * with our userspace regulatory agent to get lower bounds.
  414. */
  415. static struct ieee80211_regdomain *country_ie_2_rd(
  416. u8 *country_ie,
  417. u8 country_ie_len,
  418. u32 *checksum)
  419. {
  420. struct ieee80211_regdomain *rd = NULL;
  421. unsigned int i = 0;
  422. char alpha2[2];
  423. u32 flags = 0;
  424. u32 num_rules = 0, size_of_regd = 0;
  425. u8 *triplets_start = NULL;
  426. u8 len_at_triplet = 0;
  427. /* the last channel we have registered in a subband (triplet) */
  428. int last_sub_max_channel = 0;
  429. *checksum = 0xDEADBEEF;
  430. /* Country IE requirements */
  431. BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
  432. country_ie_len & 0x01);
  433. alpha2[0] = country_ie[0];
  434. alpha2[1] = country_ie[1];
  435. /*
  436. * Third octet can be:
  437. * 'I' - Indoor
  438. * 'O' - Outdoor
  439. *
  440. * anything else we assume is no restrictions
  441. */
  442. if (country_ie[2] == 'I')
  443. flags = NL80211_RRF_NO_OUTDOOR;
  444. else if (country_ie[2] == 'O')
  445. flags = NL80211_RRF_NO_INDOOR;
  446. country_ie += 3;
  447. country_ie_len -= 3;
  448. triplets_start = country_ie;
  449. len_at_triplet = country_ie_len;
  450. *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
  451. /*
  452. * We need to build a reg rule for each triplet, but first we must
  453. * calculate the number of reg rules we will need. We will need one
  454. * for each channel subband
  455. */
  456. while (country_ie_len >= 3) {
  457. int end_channel = 0;
  458. struct ieee80211_country_ie_triplet *triplet =
  459. (struct ieee80211_country_ie_triplet *) country_ie;
  460. int cur_sub_max_channel = 0, cur_channel = 0;
  461. if (triplet->ext.reg_extension_id >=
  462. IEEE80211_COUNTRY_EXTENSION_ID) {
  463. country_ie += 3;
  464. country_ie_len -= 3;
  465. continue;
  466. }
  467. /* 2 GHz */
  468. if (triplet->chans.first_channel <= 14)
  469. end_channel = triplet->chans.first_channel +
  470. triplet->chans.num_channels;
  471. else
  472. /*
  473. * 5 GHz -- For example in country IEs if the first
  474. * channel given is 36 and the number of channels is 4
  475. * then the individual channel numbers defined for the
  476. * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
  477. * and not 36, 37, 38, 39.
  478. *
  479. * See: http://tinyurl.com/11d-clarification
  480. */
  481. end_channel = triplet->chans.first_channel +
  482. (4 * (triplet->chans.num_channels - 1));
  483. cur_channel = triplet->chans.first_channel;
  484. cur_sub_max_channel = end_channel;
  485. /* Basic sanity check */
  486. if (cur_sub_max_channel < cur_channel)
  487. return NULL;
  488. /*
  489. * Do not allow overlapping channels. Also channels
  490. * passed in each subband must be monotonically
  491. * increasing
  492. */
  493. if (last_sub_max_channel) {
  494. if (cur_channel <= last_sub_max_channel)
  495. return NULL;
  496. if (cur_sub_max_channel <= last_sub_max_channel)
  497. return NULL;
  498. }
  499. /*
  500. * When dot11RegulatoryClassesRequired is supported
  501. * we can throw ext triplets as part of this soup,
  502. * for now we don't care when those change as we
  503. * don't support them
  504. */
  505. *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
  506. ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
  507. ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
  508. last_sub_max_channel = cur_sub_max_channel;
  509. country_ie += 3;
  510. country_ie_len -= 3;
  511. num_rules++;
  512. /*
  513. * Note: this is not a IEEE requirement but
  514. * simply a memory requirement
  515. */
  516. if (num_rules > NL80211_MAX_SUPP_REG_RULES)
  517. return NULL;
  518. }
  519. country_ie = triplets_start;
  520. country_ie_len = len_at_triplet;
  521. size_of_regd = sizeof(struct ieee80211_regdomain) +
  522. (num_rules * sizeof(struct ieee80211_reg_rule));
  523. rd = kzalloc(size_of_regd, GFP_KERNEL);
  524. if (!rd)
  525. return NULL;
  526. rd->n_reg_rules = num_rules;
  527. rd->alpha2[0] = alpha2[0];
  528. rd->alpha2[1] = alpha2[1];
  529. /* This time around we fill in the rd */
  530. while (country_ie_len >= 3) {
  531. int end_channel = 0;
  532. struct ieee80211_country_ie_triplet *triplet =
  533. (struct ieee80211_country_ie_triplet *) country_ie;
  534. struct ieee80211_reg_rule *reg_rule = NULL;
  535. struct ieee80211_freq_range *freq_range = NULL;
  536. struct ieee80211_power_rule *power_rule = NULL;
  537. /*
  538. * Must parse if dot11RegulatoryClassesRequired is true,
  539. * we don't support this yet
  540. */
  541. if (triplet->ext.reg_extension_id >=
  542. IEEE80211_COUNTRY_EXTENSION_ID) {
  543. country_ie += 3;
  544. country_ie_len -= 3;
  545. continue;
  546. }
  547. reg_rule = &rd->reg_rules[i];
  548. freq_range = &reg_rule->freq_range;
  549. power_rule = &reg_rule->power_rule;
  550. reg_rule->flags = flags;
  551. /* 2 GHz */
  552. if (triplet->chans.first_channel <= 14)
  553. end_channel = triplet->chans.first_channel +
  554. triplet->chans.num_channels;
  555. else
  556. end_channel = triplet->chans.first_channel +
  557. (4 * (triplet->chans.num_channels - 1));
  558. /*
  559. * The +10 is since the regulatory domain expects
  560. * the actual band edge, not the center of freq for
  561. * its start and end freqs, assuming 20 MHz bandwidth on
  562. * the channels passed
  563. */
  564. freq_range->start_freq_khz =
  565. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  566. triplet->chans.first_channel) - 10);
  567. freq_range->end_freq_khz =
  568. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  569. end_channel) + 10);
  570. /*
  571. * These are large arbitrary values we use to intersect later.
  572. * Increment this if we ever support >= 40 MHz channels
  573. * in IEEE 802.11
  574. */
  575. freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
  576. power_rule->max_antenna_gain = DBI_TO_MBI(100);
  577. power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power);
  578. country_ie += 3;
  579. country_ie_len -= 3;
  580. i++;
  581. BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
  582. }
  583. return rd;
  584. }
  585. /*
  586. * Helper for regdom_intersect(), this does the real
  587. * mathematical intersection fun
  588. */
  589. static int reg_rules_intersect(
  590. const struct ieee80211_reg_rule *rule1,
  591. const struct ieee80211_reg_rule *rule2,
  592. struct ieee80211_reg_rule *intersected_rule)
  593. {
  594. const struct ieee80211_freq_range *freq_range1, *freq_range2;
  595. struct ieee80211_freq_range *freq_range;
  596. const struct ieee80211_power_rule *power_rule1, *power_rule2;
  597. struct ieee80211_power_rule *power_rule;
  598. u32 freq_diff;
  599. freq_range1 = &rule1->freq_range;
  600. freq_range2 = &rule2->freq_range;
  601. freq_range = &intersected_rule->freq_range;
  602. power_rule1 = &rule1->power_rule;
  603. power_rule2 = &rule2->power_rule;
  604. power_rule = &intersected_rule->power_rule;
  605. freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
  606. freq_range2->start_freq_khz);
  607. freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
  608. freq_range2->end_freq_khz);
  609. freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
  610. freq_range2->max_bandwidth_khz);
  611. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  612. if (freq_range->max_bandwidth_khz > freq_diff)
  613. freq_range->max_bandwidth_khz = freq_diff;
  614. power_rule->max_eirp = min(power_rule1->max_eirp,
  615. power_rule2->max_eirp);
  616. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  617. power_rule2->max_antenna_gain);
  618. intersected_rule->flags = (rule1->flags | rule2->flags);
  619. if (!is_valid_reg_rule(intersected_rule))
  620. return -EINVAL;
  621. return 0;
  622. }
  623. /**
  624. * regdom_intersect - do the intersection between two regulatory domains
  625. * @rd1: first regulatory domain
  626. * @rd2: second regulatory domain
  627. *
  628. * Use this function to get the intersection between two regulatory domains.
  629. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  630. * as no one single alpha2 can represent this regulatory domain.
  631. *
  632. * Returns a pointer to the regulatory domain structure which will hold the
  633. * resulting intersection of rules between rd1 and rd2. We will
  634. * kzalloc() this structure for you.
  635. */
  636. static struct ieee80211_regdomain *regdom_intersect(
  637. const struct ieee80211_regdomain *rd1,
  638. const struct ieee80211_regdomain *rd2)
  639. {
  640. int r, size_of_regd;
  641. unsigned int x, y;
  642. unsigned int num_rules = 0, rule_idx = 0;
  643. const struct ieee80211_reg_rule *rule1, *rule2;
  644. struct ieee80211_reg_rule *intersected_rule;
  645. struct ieee80211_regdomain *rd;
  646. /* This is just a dummy holder to help us count */
  647. struct ieee80211_reg_rule irule;
  648. /* Uses the stack temporarily for counter arithmetic */
  649. intersected_rule = &irule;
  650. memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
  651. if (!rd1 || !rd2)
  652. return NULL;
  653. /*
  654. * First we get a count of the rules we'll need, then we actually
  655. * build them. This is to so we can malloc() and free() a
  656. * regdomain once. The reason we use reg_rules_intersect() here
  657. * is it will return -EINVAL if the rule computed makes no sense.
  658. * All rules that do check out OK are valid.
  659. */
  660. for (x = 0; x < rd1->n_reg_rules; x++) {
  661. rule1 = &rd1->reg_rules[x];
  662. for (y = 0; y < rd2->n_reg_rules; y++) {
  663. rule2 = &rd2->reg_rules[y];
  664. if (!reg_rules_intersect(rule1, rule2,
  665. intersected_rule))
  666. num_rules++;
  667. memset(intersected_rule, 0,
  668. sizeof(struct ieee80211_reg_rule));
  669. }
  670. }
  671. if (!num_rules)
  672. return NULL;
  673. size_of_regd = sizeof(struct ieee80211_regdomain) +
  674. ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
  675. rd = kzalloc(size_of_regd, GFP_KERNEL);
  676. if (!rd)
  677. return NULL;
  678. for (x = 0; x < rd1->n_reg_rules; x++) {
  679. rule1 = &rd1->reg_rules[x];
  680. for (y = 0; y < rd2->n_reg_rules; y++) {
  681. rule2 = &rd2->reg_rules[y];
  682. /*
  683. * This time around instead of using the stack lets
  684. * write to the target rule directly saving ourselves
  685. * a memcpy()
  686. */
  687. intersected_rule = &rd->reg_rules[rule_idx];
  688. r = reg_rules_intersect(rule1, rule2,
  689. intersected_rule);
  690. /*
  691. * No need to memset here the intersected rule here as
  692. * we're not using the stack anymore
  693. */
  694. if (r)
  695. continue;
  696. rule_idx++;
  697. }
  698. }
  699. if (rule_idx != num_rules) {
  700. kfree(rd);
  701. return NULL;
  702. }
  703. rd->n_reg_rules = num_rules;
  704. rd->alpha2[0] = '9';
  705. rd->alpha2[1] = '8';
  706. return rd;
  707. }
  708. /*
  709. * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  710. * want to just have the channel structure use these
  711. */
  712. static u32 map_regdom_flags(u32 rd_flags)
  713. {
  714. u32 channel_flags = 0;
  715. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  716. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  717. if (rd_flags & NL80211_RRF_NO_IBSS)
  718. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  719. if (rd_flags & NL80211_RRF_DFS)
  720. channel_flags |= IEEE80211_CHAN_RADAR;
  721. return channel_flags;
  722. }
  723. static int freq_reg_info_regd(struct wiphy *wiphy,
  724. u32 center_freq,
  725. u32 desired_bw_khz,
  726. const struct ieee80211_reg_rule **reg_rule,
  727. const struct ieee80211_regdomain *custom_regd)
  728. {
  729. int i;
  730. bool band_rule_found = false;
  731. const struct ieee80211_regdomain *regd;
  732. bool bw_fits = false;
  733. if (!desired_bw_khz)
  734. desired_bw_khz = MHZ_TO_KHZ(20);
  735. regd = custom_regd ? custom_regd : cfg80211_regdomain;
  736. /*
  737. * Follow the driver's regulatory domain, if present, unless a country
  738. * IE has been processed or a user wants to help complaince further
  739. */
  740. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  741. last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
  742. wiphy->regd)
  743. regd = wiphy->regd;
  744. if (!regd)
  745. return -EINVAL;
  746. for (i = 0; i < regd->n_reg_rules; i++) {
  747. const struct ieee80211_reg_rule *rr;
  748. const struct ieee80211_freq_range *fr = NULL;
  749. const struct ieee80211_power_rule *pr = NULL;
  750. rr = &regd->reg_rules[i];
  751. fr = &rr->freq_range;
  752. pr = &rr->power_rule;
  753. /*
  754. * We only need to know if one frequency rule was
  755. * was in center_freq's band, that's enough, so lets
  756. * not overwrite it once found
  757. */
  758. if (!band_rule_found)
  759. band_rule_found = freq_in_rule_band(fr, center_freq);
  760. bw_fits = reg_does_bw_fit(fr,
  761. center_freq,
  762. desired_bw_khz);
  763. if (band_rule_found && bw_fits) {
  764. *reg_rule = rr;
  765. return 0;
  766. }
  767. }
  768. if (!band_rule_found)
  769. return -ERANGE;
  770. return -EINVAL;
  771. }
  772. EXPORT_SYMBOL(freq_reg_info);
  773. int freq_reg_info(struct wiphy *wiphy,
  774. u32 center_freq,
  775. u32 desired_bw_khz,
  776. const struct ieee80211_reg_rule **reg_rule)
  777. {
  778. assert_cfg80211_lock();
  779. return freq_reg_info_regd(wiphy,
  780. center_freq,
  781. desired_bw_khz,
  782. reg_rule,
  783. NULL);
  784. }
  785. /*
  786. * Note that right now we assume the desired channel bandwidth
  787. * is always 20 MHz for each individual channel (HT40 uses 20 MHz
  788. * per channel, the primary and the extension channel). To support
  789. * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
  790. * new ieee80211_channel.target_bw and re run the regulatory check
  791. * on the wiphy with the target_bw specified. Then we can simply use
  792. * that below for the desired_bw_khz below.
  793. */
  794. static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
  795. unsigned int chan_idx)
  796. {
  797. int r;
  798. u32 flags, bw_flags = 0;
  799. u32 desired_bw_khz = MHZ_TO_KHZ(20);
  800. const struct ieee80211_reg_rule *reg_rule = NULL;
  801. const struct ieee80211_power_rule *power_rule = NULL;
  802. const struct ieee80211_freq_range *freq_range = NULL;
  803. struct ieee80211_supported_band *sband;
  804. struct ieee80211_channel *chan;
  805. struct wiphy *request_wiphy = NULL;
  806. assert_cfg80211_lock();
  807. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  808. sband = wiphy->bands[band];
  809. BUG_ON(chan_idx >= sband->n_channels);
  810. chan = &sband->channels[chan_idx];
  811. flags = chan->orig_flags;
  812. r = freq_reg_info(wiphy,
  813. MHZ_TO_KHZ(chan->center_freq),
  814. desired_bw_khz,
  815. &reg_rule);
  816. if (r) {
  817. /*
  818. * This means no regulatory rule was found in the country IE
  819. * with a frequency range on the center_freq's band, since
  820. * IEEE-802.11 allows for a country IE to have a subset of the
  821. * regulatory information provided in a country we ignore
  822. * disabling the channel unless at least one reg rule was
  823. * found on the center_freq's band. For details see this
  824. * clarification:
  825. *
  826. * http://tinyurl.com/11d-clarification
  827. */
  828. if (r == -ERANGE &&
  829. last_request->initiator ==
  830. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  831. REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
  832. "intact on %s - no rule found in band on "
  833. "Country IE\n",
  834. chan->center_freq, wiphy_name(wiphy));
  835. } else {
  836. /*
  837. * In this case we know the country IE has at least one reg rule
  838. * for the band so we respect its band definitions
  839. */
  840. if (last_request->initiator ==
  841. NL80211_REGDOM_SET_BY_COUNTRY_IE)
  842. REG_DBG_PRINT("cfg80211: Disabling "
  843. "channel %d MHz on %s due to "
  844. "Country IE\n",
  845. chan->center_freq, wiphy_name(wiphy));
  846. flags |= IEEE80211_CHAN_DISABLED;
  847. chan->flags = flags;
  848. }
  849. return;
  850. }
  851. power_rule = &reg_rule->power_rule;
  852. freq_range = &reg_rule->freq_range;
  853. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
  854. bw_flags = IEEE80211_CHAN_NO_HT40;
  855. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  856. request_wiphy && request_wiphy == wiphy &&
  857. request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
  858. /*
  859. * This gaurantees the driver's requested regulatory domain
  860. * will always be used as a base for further regulatory
  861. * settings
  862. */
  863. chan->flags = chan->orig_flags =
  864. map_regdom_flags(reg_rule->flags) | bw_flags;
  865. chan->max_antenna_gain = chan->orig_mag =
  866. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  867. chan->max_power = chan->orig_mpwr =
  868. (int) MBM_TO_DBM(power_rule->max_eirp);
  869. return;
  870. }
  871. chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
  872. chan->max_antenna_gain = min(chan->orig_mag,
  873. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  874. if (chan->orig_mpwr)
  875. chan->max_power = min(chan->orig_mpwr,
  876. (int) MBM_TO_DBM(power_rule->max_eirp));
  877. else
  878. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  879. }
  880. static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
  881. {
  882. unsigned int i;
  883. struct ieee80211_supported_band *sband;
  884. BUG_ON(!wiphy->bands[band]);
  885. sband = wiphy->bands[band];
  886. for (i = 0; i < sband->n_channels; i++)
  887. handle_channel(wiphy, band, i);
  888. }
  889. static bool ignore_reg_update(struct wiphy *wiphy,
  890. enum nl80211_reg_initiator initiator)
  891. {
  892. if (!last_request)
  893. return true;
  894. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  895. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
  896. return true;
  897. /*
  898. * wiphy->regd will be set once the device has its own
  899. * desired regulatory domain set
  900. */
  901. if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
  902. !is_world_regdom(last_request->alpha2))
  903. return true;
  904. return false;
  905. }
  906. static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
  907. {
  908. struct cfg80211_registered_device *rdev;
  909. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  910. wiphy_update_regulatory(&rdev->wiphy, initiator);
  911. }
  912. static void handle_reg_beacon(struct wiphy *wiphy,
  913. unsigned int chan_idx,
  914. struct reg_beacon *reg_beacon)
  915. {
  916. struct ieee80211_supported_band *sband;
  917. struct ieee80211_channel *chan;
  918. bool channel_changed = false;
  919. struct ieee80211_channel chan_before;
  920. assert_cfg80211_lock();
  921. sband = wiphy->bands[reg_beacon->chan.band];
  922. chan = &sband->channels[chan_idx];
  923. if (likely(chan->center_freq != reg_beacon->chan.center_freq))
  924. return;
  925. if (chan->beacon_found)
  926. return;
  927. chan->beacon_found = true;
  928. if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
  929. return;
  930. chan_before.center_freq = chan->center_freq;
  931. chan_before.flags = chan->flags;
  932. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
  933. chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  934. channel_changed = true;
  935. }
  936. if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
  937. chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
  938. channel_changed = true;
  939. }
  940. if (channel_changed)
  941. nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
  942. }
  943. /*
  944. * Called when a scan on a wiphy finds a beacon on
  945. * new channel
  946. */
  947. static void wiphy_update_new_beacon(struct wiphy *wiphy,
  948. struct reg_beacon *reg_beacon)
  949. {
  950. unsigned int i;
  951. struct ieee80211_supported_band *sband;
  952. assert_cfg80211_lock();
  953. if (!wiphy->bands[reg_beacon->chan.band])
  954. return;
  955. sband = wiphy->bands[reg_beacon->chan.band];
  956. for (i = 0; i < sband->n_channels; i++)
  957. handle_reg_beacon(wiphy, i, reg_beacon);
  958. }
  959. /*
  960. * Called upon reg changes or a new wiphy is added
  961. */
  962. static void wiphy_update_beacon_reg(struct wiphy *wiphy)
  963. {
  964. unsigned int i;
  965. struct ieee80211_supported_band *sband;
  966. struct reg_beacon *reg_beacon;
  967. assert_cfg80211_lock();
  968. if (list_empty(&reg_beacon_list))
  969. return;
  970. list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
  971. if (!wiphy->bands[reg_beacon->chan.band])
  972. continue;
  973. sband = wiphy->bands[reg_beacon->chan.band];
  974. for (i = 0; i < sband->n_channels; i++)
  975. handle_reg_beacon(wiphy, i, reg_beacon);
  976. }
  977. }
  978. static bool reg_is_world_roaming(struct wiphy *wiphy)
  979. {
  980. if (is_world_regdom(cfg80211_regdomain->alpha2) ||
  981. (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
  982. return true;
  983. if (last_request &&
  984. last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  985. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
  986. return true;
  987. return false;
  988. }
  989. /* Reap the advantages of previously found beacons */
  990. static void reg_process_beacons(struct wiphy *wiphy)
  991. {
  992. /*
  993. * Means we are just firing up cfg80211, so no beacons would
  994. * have been processed yet.
  995. */
  996. if (!last_request)
  997. return;
  998. if (!reg_is_world_roaming(wiphy))
  999. return;
  1000. wiphy_update_beacon_reg(wiphy);
  1001. }
  1002. static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
  1003. {
  1004. if (!chan)
  1005. return true;
  1006. if (chan->flags & IEEE80211_CHAN_DISABLED)
  1007. return true;
  1008. /* This would happen when regulatory rules disallow HT40 completely */
  1009. if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
  1010. return true;
  1011. return false;
  1012. }
  1013. static void reg_process_ht_flags_channel(struct wiphy *wiphy,
  1014. enum ieee80211_band band,
  1015. unsigned int chan_idx)
  1016. {
  1017. struct ieee80211_supported_band *sband;
  1018. struct ieee80211_channel *channel;
  1019. struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
  1020. unsigned int i;
  1021. assert_cfg80211_lock();
  1022. sband = wiphy->bands[band];
  1023. BUG_ON(chan_idx >= sband->n_channels);
  1024. channel = &sband->channels[chan_idx];
  1025. if (is_ht40_not_allowed(channel)) {
  1026. channel->flags |= IEEE80211_CHAN_NO_HT40;
  1027. return;
  1028. }
  1029. /*
  1030. * We need to ensure the extension channels exist to
  1031. * be able to use HT40- or HT40+, this finds them (or not)
  1032. */
  1033. for (i = 0; i < sband->n_channels; i++) {
  1034. struct ieee80211_channel *c = &sband->channels[i];
  1035. if (c->center_freq == (channel->center_freq - 20))
  1036. channel_before = c;
  1037. if (c->center_freq == (channel->center_freq + 20))
  1038. channel_after = c;
  1039. }
  1040. /*
  1041. * Please note that this assumes target bandwidth is 20 MHz,
  1042. * if that ever changes we also need to change the below logic
  1043. * to include that as well.
  1044. */
  1045. if (is_ht40_not_allowed(channel_before))
  1046. channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
  1047. else
  1048. channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
  1049. if (is_ht40_not_allowed(channel_after))
  1050. channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
  1051. else
  1052. channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
  1053. }
  1054. static void reg_process_ht_flags_band(struct wiphy *wiphy,
  1055. enum ieee80211_band band)
  1056. {
  1057. unsigned int i;
  1058. struct ieee80211_supported_band *sband;
  1059. BUG_ON(!wiphy->bands[band]);
  1060. sband = wiphy->bands[band];
  1061. for (i = 0; i < sband->n_channels; i++)
  1062. reg_process_ht_flags_channel(wiphy, band, i);
  1063. }
  1064. static void reg_process_ht_flags(struct wiphy *wiphy)
  1065. {
  1066. enum ieee80211_band band;
  1067. if (!wiphy)
  1068. return;
  1069. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1070. if (wiphy->bands[band])
  1071. reg_process_ht_flags_band(wiphy, band);
  1072. }
  1073. }
  1074. void wiphy_update_regulatory(struct wiphy *wiphy,
  1075. enum nl80211_reg_initiator initiator)
  1076. {
  1077. enum ieee80211_band band;
  1078. if (ignore_reg_update(wiphy, initiator))
  1079. goto out;
  1080. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1081. if (wiphy->bands[band])
  1082. handle_band(wiphy, band);
  1083. }
  1084. out:
  1085. reg_process_beacons(wiphy);
  1086. reg_process_ht_flags(wiphy);
  1087. if (wiphy->reg_notifier)
  1088. wiphy->reg_notifier(wiphy, last_request);
  1089. }
  1090. static void handle_channel_custom(struct wiphy *wiphy,
  1091. enum ieee80211_band band,
  1092. unsigned int chan_idx,
  1093. const struct ieee80211_regdomain *regd)
  1094. {
  1095. int r;
  1096. u32 desired_bw_khz = MHZ_TO_KHZ(20);
  1097. u32 bw_flags = 0;
  1098. const struct ieee80211_reg_rule *reg_rule = NULL;
  1099. const struct ieee80211_power_rule *power_rule = NULL;
  1100. const struct ieee80211_freq_range *freq_range = NULL;
  1101. struct ieee80211_supported_band *sband;
  1102. struct ieee80211_channel *chan;
  1103. assert_reg_lock();
  1104. sband = wiphy->bands[band];
  1105. BUG_ON(chan_idx >= sband->n_channels);
  1106. chan = &sband->channels[chan_idx];
  1107. r = freq_reg_info_regd(wiphy,
  1108. MHZ_TO_KHZ(chan->center_freq),
  1109. desired_bw_khz,
  1110. &reg_rule,
  1111. regd);
  1112. if (r) {
  1113. chan->flags = IEEE80211_CHAN_DISABLED;
  1114. return;
  1115. }
  1116. power_rule = &reg_rule->power_rule;
  1117. freq_range = &reg_rule->freq_range;
  1118. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
  1119. bw_flags = IEEE80211_CHAN_NO_HT40;
  1120. chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
  1121. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  1122. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  1123. }
  1124. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  1125. const struct ieee80211_regdomain *regd)
  1126. {
  1127. unsigned int i;
  1128. struct ieee80211_supported_band *sband;
  1129. BUG_ON(!wiphy->bands[band]);
  1130. sband = wiphy->bands[band];
  1131. for (i = 0; i < sband->n_channels; i++)
  1132. handle_channel_custom(wiphy, band, i, regd);
  1133. }
  1134. /* Used by drivers prior to wiphy registration */
  1135. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  1136. const struct ieee80211_regdomain *regd)
  1137. {
  1138. enum ieee80211_band band;
  1139. unsigned int bands_set = 0;
  1140. mutex_lock(&reg_mutex);
  1141. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1142. if (!wiphy->bands[band])
  1143. continue;
  1144. handle_band_custom(wiphy, band, regd);
  1145. bands_set++;
  1146. }
  1147. mutex_unlock(&reg_mutex);
  1148. /*
  1149. * no point in calling this if it won't have any effect
  1150. * on your device's supportd bands.
  1151. */
  1152. WARN_ON(!bands_set);
  1153. }
  1154. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  1155. /*
  1156. * Return value which can be used by ignore_request() to indicate
  1157. * it has been determined we should intersect two regulatory domains
  1158. */
  1159. #define REG_INTERSECT 1
  1160. /* This has the logic which determines when a new request
  1161. * should be ignored. */
  1162. static int ignore_request(struct wiphy *wiphy,
  1163. struct regulatory_request *pending_request)
  1164. {
  1165. struct wiphy *last_wiphy = NULL;
  1166. assert_cfg80211_lock();
  1167. /* All initial requests are respected */
  1168. if (!last_request)
  1169. return 0;
  1170. switch (pending_request->initiator) {
  1171. case NL80211_REGDOM_SET_BY_CORE:
  1172. return -EINVAL;
  1173. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  1174. last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1175. if (unlikely(!is_an_alpha2(pending_request->alpha2)))
  1176. return -EINVAL;
  1177. if (last_request->initiator ==
  1178. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1179. if (last_wiphy != wiphy) {
  1180. /*
  1181. * Two cards with two APs claiming different
  1182. * Country IE alpha2s. We could
  1183. * intersect them, but that seems unlikely
  1184. * to be correct. Reject second one for now.
  1185. */
  1186. if (regdom_changes(pending_request->alpha2))
  1187. return -EOPNOTSUPP;
  1188. return -EALREADY;
  1189. }
  1190. /*
  1191. * Two consecutive Country IE hints on the same wiphy.
  1192. * This should be picked up early by the driver/stack
  1193. */
  1194. if (WARN_ON(regdom_changes(pending_request->alpha2)))
  1195. return 0;
  1196. return -EALREADY;
  1197. }
  1198. return REG_INTERSECT;
  1199. case NL80211_REGDOM_SET_BY_DRIVER:
  1200. if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
  1201. if (regdom_changes(pending_request->alpha2))
  1202. return 0;
  1203. return -EALREADY;
  1204. }
  1205. /*
  1206. * This would happen if you unplug and plug your card
  1207. * back in or if you add a new device for which the previously
  1208. * loaded card also agrees on the regulatory domain.
  1209. */
  1210. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1211. !regdom_changes(pending_request->alpha2))
  1212. return -EALREADY;
  1213. return REG_INTERSECT;
  1214. case NL80211_REGDOM_SET_BY_USER:
  1215. if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  1216. return REG_INTERSECT;
  1217. /*
  1218. * If the user knows better the user should set the regdom
  1219. * to their country before the IE is picked up
  1220. */
  1221. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
  1222. last_request->intersect)
  1223. return -EOPNOTSUPP;
  1224. /*
  1225. * Process user requests only after previous user/driver/core
  1226. * requests have been processed
  1227. */
  1228. if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
  1229. last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1230. last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
  1231. if (regdom_changes(last_request->alpha2))
  1232. return -EAGAIN;
  1233. }
  1234. if (!regdom_changes(pending_request->alpha2))
  1235. return -EALREADY;
  1236. return 0;
  1237. }
  1238. return -EINVAL;
  1239. }
  1240. /**
  1241. * __regulatory_hint - hint to the wireless core a regulatory domain
  1242. * @wiphy: if the hint comes from country information from an AP, this
  1243. * is required to be set to the wiphy that received the information
  1244. * @pending_request: the regulatory request currently being processed
  1245. *
  1246. * The Wireless subsystem can use this function to hint to the wireless core
  1247. * what it believes should be the current regulatory domain.
  1248. *
  1249. * Returns zero if all went fine, %-EALREADY if a regulatory domain had
  1250. * already been set or other standard error codes.
  1251. *
  1252. * Caller must hold &cfg80211_mutex and &reg_mutex
  1253. */
  1254. static int __regulatory_hint(struct wiphy *wiphy,
  1255. struct regulatory_request *pending_request)
  1256. {
  1257. bool intersect = false;
  1258. int r = 0;
  1259. assert_cfg80211_lock();
  1260. r = ignore_request(wiphy, pending_request);
  1261. if (r == REG_INTERSECT) {
  1262. if (pending_request->initiator ==
  1263. NL80211_REGDOM_SET_BY_DRIVER) {
  1264. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1265. if (r) {
  1266. kfree(pending_request);
  1267. return r;
  1268. }
  1269. }
  1270. intersect = true;
  1271. } else if (r) {
  1272. /*
  1273. * If the regulatory domain being requested by the
  1274. * driver has already been set just copy it to the
  1275. * wiphy
  1276. */
  1277. if (r == -EALREADY &&
  1278. pending_request->initiator ==
  1279. NL80211_REGDOM_SET_BY_DRIVER) {
  1280. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1281. if (r) {
  1282. kfree(pending_request);
  1283. return r;
  1284. }
  1285. r = -EALREADY;
  1286. goto new_request;
  1287. }
  1288. kfree(pending_request);
  1289. return r;
  1290. }
  1291. new_request:
  1292. kfree(last_request);
  1293. last_request = pending_request;
  1294. last_request->intersect = intersect;
  1295. pending_request = NULL;
  1296. /* When r == REG_INTERSECT we do need to call CRDA */
  1297. if (r < 0) {
  1298. /*
  1299. * Since CRDA will not be called in this case as we already
  1300. * have applied the requested regulatory domain before we just
  1301. * inform userspace we have processed the request
  1302. */
  1303. if (r == -EALREADY)
  1304. nl80211_send_reg_change_event(last_request);
  1305. return r;
  1306. }
  1307. return call_crda(last_request->alpha2);
  1308. }
  1309. /* This processes *all* regulatory hints */
  1310. static void reg_process_hint(struct regulatory_request *reg_request)
  1311. {
  1312. int r = 0;
  1313. struct wiphy *wiphy = NULL;
  1314. BUG_ON(!reg_request->alpha2);
  1315. mutex_lock(&cfg80211_mutex);
  1316. mutex_lock(&reg_mutex);
  1317. if (wiphy_idx_valid(reg_request->wiphy_idx))
  1318. wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
  1319. if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1320. !wiphy) {
  1321. kfree(reg_request);
  1322. goto out;
  1323. }
  1324. r = __regulatory_hint(wiphy, reg_request);
  1325. /* This is required so that the orig_* parameters are saved */
  1326. if (r == -EALREADY && wiphy &&
  1327. wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
  1328. wiphy_update_regulatory(wiphy, reg_request->initiator);
  1329. out:
  1330. mutex_unlock(&reg_mutex);
  1331. mutex_unlock(&cfg80211_mutex);
  1332. }
  1333. /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
  1334. static void reg_process_pending_hints(void)
  1335. {
  1336. struct regulatory_request *reg_request;
  1337. spin_lock(&reg_requests_lock);
  1338. while (!list_empty(&reg_requests_list)) {
  1339. reg_request = list_first_entry(&reg_requests_list,
  1340. struct regulatory_request,
  1341. list);
  1342. list_del_init(&reg_request->list);
  1343. spin_unlock(&reg_requests_lock);
  1344. reg_process_hint(reg_request);
  1345. spin_lock(&reg_requests_lock);
  1346. }
  1347. spin_unlock(&reg_requests_lock);
  1348. }
  1349. /* Processes beacon hints -- this has nothing to do with country IEs */
  1350. static void reg_process_pending_beacon_hints(void)
  1351. {
  1352. struct cfg80211_registered_device *rdev;
  1353. struct reg_beacon *pending_beacon, *tmp;
  1354. /*
  1355. * No need to hold the reg_mutex here as we just touch wiphys
  1356. * and do not read or access regulatory variables.
  1357. */
  1358. mutex_lock(&cfg80211_mutex);
  1359. /* This goes through the _pending_ beacon list */
  1360. spin_lock_bh(&reg_pending_beacons_lock);
  1361. if (list_empty(&reg_pending_beacons)) {
  1362. spin_unlock_bh(&reg_pending_beacons_lock);
  1363. goto out;
  1364. }
  1365. list_for_each_entry_safe(pending_beacon, tmp,
  1366. &reg_pending_beacons, list) {
  1367. list_del_init(&pending_beacon->list);
  1368. /* Applies the beacon hint to current wiphys */
  1369. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  1370. wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
  1371. /* Remembers the beacon hint for new wiphys or reg changes */
  1372. list_add_tail(&pending_beacon->list, &reg_beacon_list);
  1373. }
  1374. spin_unlock_bh(&reg_pending_beacons_lock);
  1375. out:
  1376. mutex_unlock(&cfg80211_mutex);
  1377. }
  1378. static void reg_todo(struct work_struct *work)
  1379. {
  1380. reg_process_pending_hints();
  1381. reg_process_pending_beacon_hints();
  1382. }
  1383. static DECLARE_WORK(reg_work, reg_todo);
  1384. static void queue_regulatory_request(struct regulatory_request *request)
  1385. {
  1386. spin_lock(&reg_requests_lock);
  1387. list_add_tail(&request->list, &reg_requests_list);
  1388. spin_unlock(&reg_requests_lock);
  1389. schedule_work(&reg_work);
  1390. }
  1391. /* Core regulatory hint -- happens once during cfg80211_init() */
  1392. static int regulatory_hint_core(const char *alpha2)
  1393. {
  1394. struct regulatory_request *request;
  1395. BUG_ON(last_request);
  1396. request = kzalloc(sizeof(struct regulatory_request),
  1397. GFP_KERNEL);
  1398. if (!request)
  1399. return -ENOMEM;
  1400. request->alpha2[0] = alpha2[0];
  1401. request->alpha2[1] = alpha2[1];
  1402. request->initiator = NL80211_REGDOM_SET_BY_CORE;
  1403. queue_regulatory_request(request);
  1404. /*
  1405. * This ensures last_request is populated once modules
  1406. * come swinging in and calling regulatory hints and
  1407. * wiphy_apply_custom_regulatory().
  1408. */
  1409. flush_scheduled_work();
  1410. return 0;
  1411. }
  1412. /* User hints */
  1413. int regulatory_hint_user(const char *alpha2)
  1414. {
  1415. struct regulatory_request *request;
  1416. BUG_ON(!alpha2);
  1417. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1418. if (!request)
  1419. return -ENOMEM;
  1420. request->wiphy_idx = WIPHY_IDX_STALE;
  1421. request->alpha2[0] = alpha2[0];
  1422. request->alpha2[1] = alpha2[1];
  1423. request->initiator = NL80211_REGDOM_SET_BY_USER;
  1424. queue_regulatory_request(request);
  1425. return 0;
  1426. }
  1427. /* Driver hints */
  1428. int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1429. {
  1430. struct regulatory_request *request;
  1431. BUG_ON(!alpha2);
  1432. BUG_ON(!wiphy);
  1433. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1434. if (!request)
  1435. return -ENOMEM;
  1436. request->wiphy_idx = get_wiphy_idx(wiphy);
  1437. /* Must have registered wiphy first */
  1438. BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
  1439. request->alpha2[0] = alpha2[0];
  1440. request->alpha2[1] = alpha2[1];
  1441. request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
  1442. queue_regulatory_request(request);
  1443. return 0;
  1444. }
  1445. EXPORT_SYMBOL(regulatory_hint);
  1446. /* Caller must hold reg_mutex */
  1447. static bool reg_same_country_ie_hint(struct wiphy *wiphy,
  1448. u32 country_ie_checksum)
  1449. {
  1450. struct wiphy *request_wiphy;
  1451. assert_reg_lock();
  1452. if (unlikely(last_request->initiator !=
  1453. NL80211_REGDOM_SET_BY_COUNTRY_IE))
  1454. return false;
  1455. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1456. if (!request_wiphy)
  1457. return false;
  1458. if (likely(request_wiphy != wiphy))
  1459. return !country_ie_integrity_changes(country_ie_checksum);
  1460. /*
  1461. * We should not have let these through at this point, they
  1462. * should have been picked up earlier by the first alpha2 check
  1463. * on the device
  1464. */
  1465. if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
  1466. return true;
  1467. return false;
  1468. }
  1469. /*
  1470. * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
  1471. * therefore cannot iterate over the rdev list here.
  1472. */
  1473. void regulatory_hint_11d(struct wiphy *wiphy,
  1474. u8 *country_ie,
  1475. u8 country_ie_len)
  1476. {
  1477. struct ieee80211_regdomain *rd = NULL;
  1478. char alpha2[2];
  1479. u32 checksum = 0;
  1480. enum environment_cap env = ENVIRON_ANY;
  1481. struct regulatory_request *request;
  1482. mutex_lock(&reg_mutex);
  1483. if (unlikely(!last_request))
  1484. goto out;
  1485. /* IE len must be evenly divisible by 2 */
  1486. if (country_ie_len & 0x01)
  1487. goto out;
  1488. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1489. goto out;
  1490. /*
  1491. * Pending country IE processing, this can happen after we
  1492. * call CRDA and wait for a response if a beacon was received before
  1493. * we were able to process the last regulatory_hint_11d() call
  1494. */
  1495. if (country_ie_regdomain)
  1496. goto out;
  1497. alpha2[0] = country_ie[0];
  1498. alpha2[1] = country_ie[1];
  1499. if (country_ie[2] == 'I')
  1500. env = ENVIRON_INDOOR;
  1501. else if (country_ie[2] == 'O')
  1502. env = ENVIRON_OUTDOOR;
  1503. /*
  1504. * We will run this only upon a successful connection on cfg80211.
  1505. * We leave conflict resolution to the workqueue, where can hold
  1506. * cfg80211_mutex.
  1507. */
  1508. if (likely(last_request->initiator ==
  1509. NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1510. wiphy_idx_valid(last_request->wiphy_idx)))
  1511. goto out;
  1512. rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
  1513. if (!rd) {
  1514. REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
  1515. goto out;
  1516. }
  1517. /*
  1518. * This will not happen right now but we leave it here for the
  1519. * the future when we want to add suspend/resume support and having
  1520. * the user move to another country after doing so, or having the user
  1521. * move to another AP. Right now we just trust the first AP.
  1522. *
  1523. * If we hit this before we add this support we want to be informed of
  1524. * it as it would indicate a mistake in the current design
  1525. */
  1526. if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
  1527. goto free_rd_out;
  1528. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1529. if (!request)
  1530. goto free_rd_out;
  1531. /*
  1532. * We keep this around for when CRDA comes back with a response so
  1533. * we can intersect with that
  1534. */
  1535. country_ie_regdomain = rd;
  1536. request->wiphy_idx = get_wiphy_idx(wiphy);
  1537. request->alpha2[0] = rd->alpha2[0];
  1538. request->alpha2[1] = rd->alpha2[1];
  1539. request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
  1540. request->country_ie_checksum = checksum;
  1541. request->country_ie_env = env;
  1542. mutex_unlock(&reg_mutex);
  1543. queue_regulatory_request(request);
  1544. return;
  1545. free_rd_out:
  1546. kfree(rd);
  1547. out:
  1548. mutex_unlock(&reg_mutex);
  1549. }
  1550. static bool freq_is_chan_12_13_14(u16 freq)
  1551. {
  1552. if (freq == ieee80211_channel_to_frequency(12) ||
  1553. freq == ieee80211_channel_to_frequency(13) ||
  1554. freq == ieee80211_channel_to_frequency(14))
  1555. return true;
  1556. return false;
  1557. }
  1558. int regulatory_hint_found_beacon(struct wiphy *wiphy,
  1559. struct ieee80211_channel *beacon_chan,
  1560. gfp_t gfp)
  1561. {
  1562. struct reg_beacon *reg_beacon;
  1563. if (likely((beacon_chan->beacon_found ||
  1564. (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
  1565. (beacon_chan->band == IEEE80211_BAND_2GHZ &&
  1566. !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
  1567. return 0;
  1568. reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
  1569. if (!reg_beacon)
  1570. return -ENOMEM;
  1571. REG_DBG_PRINT("cfg80211: Found new beacon on "
  1572. "frequency: %d MHz (Ch %d) on %s\n",
  1573. beacon_chan->center_freq,
  1574. ieee80211_frequency_to_channel(beacon_chan->center_freq),
  1575. wiphy_name(wiphy));
  1576. memcpy(&reg_beacon->chan, beacon_chan,
  1577. sizeof(struct ieee80211_channel));
  1578. /*
  1579. * Since we can be called from BH or and non-BH context
  1580. * we must use spin_lock_bh()
  1581. */
  1582. spin_lock_bh(&reg_pending_beacons_lock);
  1583. list_add_tail(&reg_beacon->list, &reg_pending_beacons);
  1584. spin_unlock_bh(&reg_pending_beacons_lock);
  1585. schedule_work(&reg_work);
  1586. return 0;
  1587. }
  1588. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1589. {
  1590. unsigned int i;
  1591. const struct ieee80211_reg_rule *reg_rule = NULL;
  1592. const struct ieee80211_freq_range *freq_range = NULL;
  1593. const struct ieee80211_power_rule *power_rule = NULL;
  1594. printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
  1595. "(max_antenna_gain, max_eirp)\n");
  1596. for (i = 0; i < rd->n_reg_rules; i++) {
  1597. reg_rule = &rd->reg_rules[i];
  1598. freq_range = &reg_rule->freq_range;
  1599. power_rule = &reg_rule->power_rule;
  1600. /*
  1601. * There may not be documentation for max antenna gain
  1602. * in certain regions
  1603. */
  1604. if (power_rule->max_antenna_gain)
  1605. printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
  1606. "(%d mBi, %d mBm)\n",
  1607. freq_range->start_freq_khz,
  1608. freq_range->end_freq_khz,
  1609. freq_range->max_bandwidth_khz,
  1610. power_rule->max_antenna_gain,
  1611. power_rule->max_eirp);
  1612. else
  1613. printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
  1614. "(N/A, %d mBm)\n",
  1615. freq_range->start_freq_khz,
  1616. freq_range->end_freq_khz,
  1617. freq_range->max_bandwidth_khz,
  1618. power_rule->max_eirp);
  1619. }
  1620. }
  1621. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1622. {
  1623. if (is_intersected_alpha2(rd->alpha2)) {
  1624. if (last_request->initiator ==
  1625. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1626. struct cfg80211_registered_device *rdev;
  1627. rdev = cfg80211_rdev_by_wiphy_idx(
  1628. last_request->wiphy_idx);
  1629. if (rdev) {
  1630. printk(KERN_INFO "cfg80211: Current regulatory "
  1631. "domain updated by AP to: %c%c\n",
  1632. rdev->country_ie_alpha2[0],
  1633. rdev->country_ie_alpha2[1]);
  1634. } else
  1635. printk(KERN_INFO "cfg80211: Current regulatory "
  1636. "domain intersected: \n");
  1637. } else
  1638. printk(KERN_INFO "cfg80211: Current regulatory "
  1639. "domain intersected: \n");
  1640. } else if (is_world_regdom(rd->alpha2))
  1641. printk(KERN_INFO "cfg80211: World regulatory "
  1642. "domain updated:\n");
  1643. else {
  1644. if (is_unknown_alpha2(rd->alpha2))
  1645. printk(KERN_INFO "cfg80211: Regulatory domain "
  1646. "changed to driver built-in settings "
  1647. "(unknown country)\n");
  1648. else
  1649. printk(KERN_INFO "cfg80211: Regulatory domain "
  1650. "changed to country: %c%c\n",
  1651. rd->alpha2[0], rd->alpha2[1]);
  1652. }
  1653. print_rd_rules(rd);
  1654. }
  1655. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1656. {
  1657. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1658. rd->alpha2[0], rd->alpha2[1]);
  1659. print_rd_rules(rd);
  1660. }
  1661. #ifdef CONFIG_CFG80211_REG_DEBUG
  1662. static void reg_country_ie_process_debug(
  1663. const struct ieee80211_regdomain *rd,
  1664. const struct ieee80211_regdomain *country_ie_regdomain,
  1665. const struct ieee80211_regdomain *intersected_rd)
  1666. {
  1667. printk(KERN_DEBUG "cfg80211: Received country IE:\n");
  1668. print_regdomain_info(country_ie_regdomain);
  1669. printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
  1670. print_regdomain_info(rd);
  1671. if (intersected_rd) {
  1672. printk(KERN_DEBUG "cfg80211: We intersect both of these "
  1673. "and get:\n");
  1674. print_regdomain_info(intersected_rd);
  1675. return;
  1676. }
  1677. printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
  1678. }
  1679. #else
  1680. static inline void reg_country_ie_process_debug(
  1681. const struct ieee80211_regdomain *rd,
  1682. const struct ieee80211_regdomain *country_ie_regdomain,
  1683. const struct ieee80211_regdomain *intersected_rd)
  1684. {
  1685. }
  1686. #endif
  1687. /* Takes ownership of rd only if it doesn't fail */
  1688. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1689. {
  1690. const struct ieee80211_regdomain *intersected_rd = NULL;
  1691. struct cfg80211_registered_device *rdev = NULL;
  1692. struct wiphy *request_wiphy;
  1693. /* Some basic sanity checks first */
  1694. if (is_world_regdom(rd->alpha2)) {
  1695. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1696. return -EINVAL;
  1697. update_world_regdomain(rd);
  1698. return 0;
  1699. }
  1700. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1701. !is_unknown_alpha2(rd->alpha2))
  1702. return -EINVAL;
  1703. if (!last_request)
  1704. return -EINVAL;
  1705. /*
  1706. * Lets only bother proceeding on the same alpha2 if the current
  1707. * rd is non static (it means CRDA was present and was used last)
  1708. * and the pending request came in from a country IE
  1709. */
  1710. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1711. /*
  1712. * If someone else asked us to change the rd lets only bother
  1713. * checking if the alpha2 changes if CRDA was already called
  1714. */
  1715. if (!regdom_changes(rd->alpha2))
  1716. return -EINVAL;
  1717. }
  1718. /*
  1719. * Now lets set the regulatory domain, update all driver channels
  1720. * and finally inform them of what we have done, in case they want
  1721. * to review or adjust their own settings based on their own
  1722. * internal EEPROM data
  1723. */
  1724. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1725. return -EINVAL;
  1726. if (!is_valid_rd(rd)) {
  1727. printk(KERN_ERR "cfg80211: Invalid "
  1728. "regulatory domain detected:\n");
  1729. print_regdomain_info(rd);
  1730. return -EINVAL;
  1731. }
  1732. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1733. if (!last_request->intersect) {
  1734. int r;
  1735. if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
  1736. reset_regdomains();
  1737. cfg80211_regdomain = rd;
  1738. return 0;
  1739. }
  1740. /*
  1741. * For a driver hint, lets copy the regulatory domain the
  1742. * driver wanted to the wiphy to deal with conflicts
  1743. */
  1744. /*
  1745. * Userspace could have sent two replies with only
  1746. * one kernel request.
  1747. */
  1748. if (request_wiphy->regd)
  1749. return -EALREADY;
  1750. r = reg_copy_regd(&request_wiphy->regd, rd);
  1751. if (r)
  1752. return r;
  1753. reset_regdomains();
  1754. cfg80211_regdomain = rd;
  1755. return 0;
  1756. }
  1757. /* Intersection requires a bit more work */
  1758. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1759. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1760. if (!intersected_rd)
  1761. return -EINVAL;
  1762. /*
  1763. * We can trash what CRDA provided now.
  1764. * However if a driver requested this specific regulatory
  1765. * domain we keep it for its private use
  1766. */
  1767. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
  1768. request_wiphy->regd = rd;
  1769. else
  1770. kfree(rd);
  1771. rd = NULL;
  1772. reset_regdomains();
  1773. cfg80211_regdomain = intersected_rd;
  1774. return 0;
  1775. }
  1776. /*
  1777. * Country IE requests are handled a bit differently, we intersect
  1778. * the country IE rd with what CRDA believes that country should have
  1779. */
  1780. /*
  1781. * Userspace could have sent two replies with only
  1782. * one kernel request. By the second reply we would have
  1783. * already processed and consumed the country_ie_regdomain.
  1784. */
  1785. if (!country_ie_regdomain)
  1786. return -EALREADY;
  1787. BUG_ON(rd == country_ie_regdomain);
  1788. /*
  1789. * Intersect what CRDA returned and our what we
  1790. * had built from the Country IE received
  1791. */
  1792. intersected_rd = regdom_intersect(rd, country_ie_regdomain);
  1793. reg_country_ie_process_debug(rd,
  1794. country_ie_regdomain,
  1795. intersected_rd);
  1796. kfree(country_ie_regdomain);
  1797. country_ie_regdomain = NULL;
  1798. if (!intersected_rd)
  1799. return -EINVAL;
  1800. rdev = wiphy_to_dev(request_wiphy);
  1801. rdev->country_ie_alpha2[0] = rd->alpha2[0];
  1802. rdev->country_ie_alpha2[1] = rd->alpha2[1];
  1803. rdev->env = last_request->country_ie_env;
  1804. BUG_ON(intersected_rd == rd);
  1805. kfree(rd);
  1806. rd = NULL;
  1807. reset_regdomains();
  1808. cfg80211_regdomain = intersected_rd;
  1809. return 0;
  1810. }
  1811. /*
  1812. * Use this call to set the current regulatory domain. Conflicts with
  1813. * multiple drivers can be ironed out later. Caller must've already
  1814. * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
  1815. */
  1816. int set_regdom(const struct ieee80211_regdomain *rd)
  1817. {
  1818. int r;
  1819. assert_cfg80211_lock();
  1820. mutex_lock(&reg_mutex);
  1821. /* Note that this doesn't update the wiphys, this is done below */
  1822. r = __set_regdom(rd);
  1823. if (r) {
  1824. kfree(rd);
  1825. mutex_unlock(&reg_mutex);
  1826. return r;
  1827. }
  1828. /* This would make this whole thing pointless */
  1829. if (!last_request->intersect)
  1830. BUG_ON(rd != cfg80211_regdomain);
  1831. /* update all wiphys now with the new established regulatory domain */
  1832. update_all_wiphy_regulatory(last_request->initiator);
  1833. print_regdomain(cfg80211_regdomain);
  1834. nl80211_send_reg_change_event(last_request);
  1835. mutex_unlock(&reg_mutex);
  1836. return r;
  1837. }
  1838. /* Caller must hold cfg80211_mutex */
  1839. void reg_device_remove(struct wiphy *wiphy)
  1840. {
  1841. struct wiphy *request_wiphy = NULL;
  1842. assert_cfg80211_lock();
  1843. mutex_lock(&reg_mutex);
  1844. kfree(wiphy->regd);
  1845. if (last_request)
  1846. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1847. if (!request_wiphy || request_wiphy != wiphy)
  1848. goto out;
  1849. last_request->wiphy_idx = WIPHY_IDX_STALE;
  1850. last_request->country_ie_env = ENVIRON_ANY;
  1851. out:
  1852. mutex_unlock(&reg_mutex);
  1853. }
  1854. int regulatory_init(void)
  1855. {
  1856. int err = 0;
  1857. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  1858. if (IS_ERR(reg_pdev))
  1859. return PTR_ERR(reg_pdev);
  1860. spin_lock_init(&reg_requests_lock);
  1861. spin_lock_init(&reg_pending_beacons_lock);
  1862. cfg80211_regdomain = cfg80211_world_regdom;
  1863. /* We always try to get an update for the static regdomain */
  1864. err = regulatory_hint_core(cfg80211_regdomain->alpha2);
  1865. if (err) {
  1866. if (err == -ENOMEM)
  1867. return err;
  1868. /*
  1869. * N.B. kobject_uevent_env() can fail mainly for when we're out
  1870. * memory which is handled and propagated appropriately above
  1871. * but it can also fail during a netlink_broadcast() or during
  1872. * early boot for call_usermodehelper(). For now treat these
  1873. * errors as non-fatal.
  1874. */
  1875. printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
  1876. "to call CRDA during init");
  1877. #ifdef CONFIG_CFG80211_REG_DEBUG
  1878. /* We want to find out exactly why when debugging */
  1879. WARN_ON(err);
  1880. #endif
  1881. }
  1882. /*
  1883. * Finally, if the user set the module parameter treat it
  1884. * as a user hint.
  1885. */
  1886. if (!is_world_regdom(ieee80211_regdom))
  1887. regulatory_hint_user(ieee80211_regdom);
  1888. return 0;
  1889. }
  1890. void regulatory_exit(void)
  1891. {
  1892. struct regulatory_request *reg_request, *tmp;
  1893. struct reg_beacon *reg_beacon, *btmp;
  1894. cancel_work_sync(&reg_work);
  1895. mutex_lock(&cfg80211_mutex);
  1896. mutex_lock(&reg_mutex);
  1897. reset_regdomains();
  1898. kfree(country_ie_regdomain);
  1899. country_ie_regdomain = NULL;
  1900. kfree(last_request);
  1901. platform_device_unregister(reg_pdev);
  1902. spin_lock_bh(&reg_pending_beacons_lock);
  1903. if (!list_empty(&reg_pending_beacons)) {
  1904. list_for_each_entry_safe(reg_beacon, btmp,
  1905. &reg_pending_beacons, list) {
  1906. list_del(&reg_beacon->list);
  1907. kfree(reg_beacon);
  1908. }
  1909. }
  1910. spin_unlock_bh(&reg_pending_beacons_lock);
  1911. if (!list_empty(&reg_beacon_list)) {
  1912. list_for_each_entry_safe(reg_beacon, btmp,
  1913. &reg_beacon_list, list) {
  1914. list_del(&reg_beacon->list);
  1915. kfree(reg_beacon);
  1916. }
  1917. }
  1918. spin_lock(&reg_requests_lock);
  1919. if (!list_empty(&reg_requests_list)) {
  1920. list_for_each_entry_safe(reg_request, tmp,
  1921. &reg_requests_list, list) {
  1922. list_del(&reg_request->list);
  1923. kfree(reg_request);
  1924. }
  1925. }
  1926. spin_unlock(&reg_requests_lock);
  1927. mutex_unlock(&reg_mutex);
  1928. mutex_unlock(&cfg80211_mutex);
  1929. }