reg.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  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. * Some APs may send a country IE triplet for each channel they
  411. * support and while this is completely overkill and silly we still
  412. * need to support it. We avoid making a single rule for each channel
  413. * though and to help us with this we use this helper to find the
  414. * actual subband end channel. These type of country IE triplet
  415. * scenerios are handled then, all yielding two regulaotry rules from
  416. * parsing a country IE:
  417. *
  418. * [1]
  419. * [2]
  420. * [36]
  421. * [40]
  422. *
  423. * [1]
  424. * [2-4]
  425. * [5-12]
  426. * [36]
  427. * [40-44]
  428. *
  429. * [1-4]
  430. * [5-7]
  431. * [36-44]
  432. * [48-64]
  433. *
  434. * [36-36]
  435. * [40-40]
  436. * [44-44]
  437. * [48-48]
  438. * [52-52]
  439. * [56-56]
  440. * [60-60]
  441. * [64-64]
  442. * [100-100]
  443. * [104-104]
  444. * [108-108]
  445. * [112-112]
  446. * [116-116]
  447. * [120-120]
  448. * [124-124]
  449. * [128-128]
  450. * [132-132]
  451. * [136-136]
  452. * [140-140]
  453. *
  454. * Returns 0 if the IE has been found to be invalid in the middle
  455. * somewhere.
  456. */
  457. static int max_subband_chan(int orig_cur_chan,
  458. int orig_end_channel,
  459. s8 orig_max_power,
  460. u8 **country_ie,
  461. u8 *country_ie_len)
  462. {
  463. u8 *triplets_start = *country_ie;
  464. u8 len_at_triplet = *country_ie_len;
  465. int end_subband_chan = orig_end_channel;
  466. enum ieee80211_band band;
  467. /*
  468. * We'll deal with padding for the caller unless
  469. * its not immediate and we don't process any channels
  470. */
  471. if (*country_ie_len == 1) {
  472. *country_ie += 1;
  473. *country_ie_len -= 1;
  474. return orig_end_channel;
  475. }
  476. /* Move to the next triplet and then start search */
  477. *country_ie += 3;
  478. *country_ie_len -= 3;
  479. if (orig_cur_chan <= 14)
  480. band = IEEE80211_BAND_2GHZ;
  481. else
  482. band = IEEE80211_BAND_5GHZ;
  483. while (*country_ie_len >= 3) {
  484. int end_channel = 0;
  485. struct ieee80211_country_ie_triplet *triplet =
  486. (struct ieee80211_country_ie_triplet *) *country_ie;
  487. int cur_channel = 0, next_expected_chan;
  488. enum ieee80211_band next_band = IEEE80211_BAND_2GHZ;
  489. /* means last triplet is completely unrelated to this one */
  490. if (triplet->ext.reg_extension_id >=
  491. IEEE80211_COUNTRY_EXTENSION_ID) {
  492. *country_ie -= 3;
  493. *country_ie_len += 3;
  494. break;
  495. }
  496. if (triplet->chans.first_channel == 0) {
  497. *country_ie += 1;
  498. *country_ie_len -= 1;
  499. if (*country_ie_len != 0)
  500. return 0;
  501. break;
  502. }
  503. if (triplet->chans.num_channels == 0)
  504. return 0;
  505. /* Monitonically increasing channel order */
  506. if (triplet->chans.first_channel <= end_subband_chan)
  507. return 0;
  508. /* 2 GHz */
  509. if (triplet->chans.first_channel <= 14) {
  510. end_channel = triplet->chans.first_channel +
  511. triplet->chans.num_channels - 1;
  512. }
  513. else {
  514. end_channel = triplet->chans.first_channel +
  515. (4 * (triplet->chans.num_channels - 1));
  516. next_band = IEEE80211_BAND_5GHZ;
  517. }
  518. if (band != next_band) {
  519. *country_ie -= 3;
  520. *country_ie_len += 3;
  521. break;
  522. }
  523. if (orig_max_power != triplet->chans.max_power) {
  524. *country_ie -= 3;
  525. *country_ie_len += 3;
  526. break;
  527. }
  528. cur_channel = triplet->chans.first_channel;
  529. /* The key is finding the right next expected channel */
  530. if (band == IEEE80211_BAND_2GHZ)
  531. next_expected_chan = end_subband_chan + 1;
  532. else
  533. next_expected_chan = end_subband_chan + 4;
  534. if (cur_channel != next_expected_chan) {
  535. *country_ie -= 3;
  536. *country_ie_len += 3;
  537. break;
  538. }
  539. end_subband_chan = end_channel;
  540. /* Move to the next one */
  541. *country_ie += 3;
  542. *country_ie_len -= 3;
  543. /*
  544. * Padding needs to be dealt with if we processed
  545. * some channels.
  546. */
  547. if (*country_ie_len == 1) {
  548. *country_ie += 1;
  549. *country_ie_len -= 1;
  550. break;
  551. }
  552. /* If seen, the IE is invalid */
  553. if (*country_ie_len == 2)
  554. return 0;
  555. }
  556. if (end_subband_chan == orig_end_channel) {
  557. *country_ie = triplets_start;
  558. *country_ie_len = len_at_triplet;
  559. return orig_end_channel;
  560. }
  561. return end_subband_chan;
  562. }
  563. /*
  564. * Converts a country IE to a regulatory domain. A regulatory domain
  565. * structure has a lot of information which the IE doesn't yet have,
  566. * so for the other values we use upper max values as we will intersect
  567. * with our userspace regulatory agent to get lower bounds.
  568. */
  569. static struct ieee80211_regdomain *country_ie_2_rd(
  570. u8 *country_ie,
  571. u8 country_ie_len,
  572. u32 *checksum)
  573. {
  574. struct ieee80211_regdomain *rd = NULL;
  575. unsigned int i = 0;
  576. char alpha2[2];
  577. u32 flags = 0;
  578. u32 num_rules = 0, size_of_regd = 0;
  579. u8 *triplets_start = NULL;
  580. u8 len_at_triplet = 0;
  581. /* the last channel we have registered in a subband (triplet) */
  582. int last_sub_max_channel = 0;
  583. *checksum = 0xDEADBEEF;
  584. /* Country IE requirements */
  585. BUG_ON(country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN ||
  586. country_ie_len & 0x01);
  587. alpha2[0] = country_ie[0];
  588. alpha2[1] = country_ie[1];
  589. /*
  590. * Third octet can be:
  591. * 'I' - Indoor
  592. * 'O' - Outdoor
  593. *
  594. * anything else we assume is no restrictions
  595. */
  596. if (country_ie[2] == 'I')
  597. flags = NL80211_RRF_NO_OUTDOOR;
  598. else if (country_ie[2] == 'O')
  599. flags = NL80211_RRF_NO_INDOOR;
  600. country_ie += 3;
  601. country_ie_len -= 3;
  602. triplets_start = country_ie;
  603. len_at_triplet = country_ie_len;
  604. *checksum ^= ((flags ^ alpha2[0] ^ alpha2[1]) << 8);
  605. /*
  606. * We need to build a reg rule for each triplet, but first we must
  607. * calculate the number of reg rules we will need. We will need one
  608. * for each channel subband
  609. */
  610. while (country_ie_len >= 3) {
  611. int end_channel = 0;
  612. struct ieee80211_country_ie_triplet *triplet =
  613. (struct ieee80211_country_ie_triplet *) country_ie;
  614. int cur_sub_max_channel = 0, cur_channel = 0;
  615. if (triplet->ext.reg_extension_id >=
  616. IEEE80211_COUNTRY_EXTENSION_ID) {
  617. country_ie += 3;
  618. country_ie_len -= 3;
  619. continue;
  620. }
  621. /*
  622. * APs can add padding to make length divisible
  623. * by two, required by the spec.
  624. */
  625. if (triplet->chans.first_channel == 0) {
  626. country_ie++;
  627. country_ie_len--;
  628. /* This is expected to be at the very end only */
  629. if (country_ie_len != 0)
  630. return NULL;
  631. break;
  632. }
  633. if (triplet->chans.num_channels == 0)
  634. return NULL;
  635. /* 2 GHz */
  636. if (triplet->chans.first_channel <= 14)
  637. end_channel = triplet->chans.first_channel +
  638. triplet->chans.num_channels - 1;
  639. else
  640. /*
  641. * 5 GHz -- For example in country IEs if the first
  642. * channel given is 36 and the number of channels is 4
  643. * then the individual channel numbers defined for the
  644. * 5 GHz PHY by these parameters are: 36, 40, 44, and 48
  645. * and not 36, 37, 38, 39.
  646. *
  647. * See: http://tinyurl.com/11d-clarification
  648. */
  649. end_channel = triplet->chans.first_channel +
  650. (4 * (triplet->chans.num_channels - 1));
  651. cur_channel = triplet->chans.first_channel;
  652. /*
  653. * Enhancement for APs that send a triplet for every channel
  654. * or for whatever reason sends triplets with multiple channels
  655. * separated when in fact they should be together.
  656. */
  657. end_channel = max_subband_chan(cur_channel,
  658. end_channel,
  659. triplet->chans.max_power,
  660. &country_ie,
  661. &country_ie_len);
  662. if (!end_channel)
  663. return NULL;
  664. cur_sub_max_channel = end_channel;
  665. /* Basic sanity check */
  666. if (cur_sub_max_channel < cur_channel)
  667. return NULL;
  668. /*
  669. * Do not allow overlapping channels. Also channels
  670. * passed in each subband must be monotonically
  671. * increasing
  672. */
  673. if (last_sub_max_channel) {
  674. if (cur_channel <= last_sub_max_channel)
  675. return NULL;
  676. if (cur_sub_max_channel <= last_sub_max_channel)
  677. return NULL;
  678. }
  679. /*
  680. * When dot11RegulatoryClassesRequired is supported
  681. * we can throw ext triplets as part of this soup,
  682. * for now we don't care when those change as we
  683. * don't support them
  684. */
  685. *checksum ^= ((cur_channel ^ cur_sub_max_channel) << 8) |
  686. ((cur_sub_max_channel ^ cur_sub_max_channel) << 16) |
  687. ((triplet->chans.max_power ^ cur_sub_max_channel) << 24);
  688. last_sub_max_channel = cur_sub_max_channel;
  689. num_rules++;
  690. if (country_ie_len >= 3) {
  691. country_ie += 3;
  692. country_ie_len -= 3;
  693. }
  694. /*
  695. * Note: this is not a IEEE requirement but
  696. * simply a memory requirement
  697. */
  698. if (num_rules > NL80211_MAX_SUPP_REG_RULES)
  699. return NULL;
  700. }
  701. country_ie = triplets_start;
  702. country_ie_len = len_at_triplet;
  703. size_of_regd = sizeof(struct ieee80211_regdomain) +
  704. (num_rules * sizeof(struct ieee80211_reg_rule));
  705. rd = kzalloc(size_of_regd, GFP_KERNEL);
  706. if (!rd)
  707. return NULL;
  708. rd->n_reg_rules = num_rules;
  709. rd->alpha2[0] = alpha2[0];
  710. rd->alpha2[1] = alpha2[1];
  711. /* This time around we fill in the rd */
  712. while (country_ie_len >= 3) {
  713. int end_channel = 0;
  714. struct ieee80211_country_ie_triplet *triplet =
  715. (struct ieee80211_country_ie_triplet *) country_ie;
  716. struct ieee80211_reg_rule *reg_rule = NULL;
  717. struct ieee80211_freq_range *freq_range = NULL;
  718. struct ieee80211_power_rule *power_rule = NULL;
  719. /*
  720. * Must parse if dot11RegulatoryClassesRequired is true,
  721. * we don't support this yet
  722. */
  723. if (triplet->ext.reg_extension_id >=
  724. IEEE80211_COUNTRY_EXTENSION_ID) {
  725. country_ie += 3;
  726. country_ie_len -= 3;
  727. continue;
  728. }
  729. if (triplet->chans.first_channel == 0) {
  730. country_ie++;
  731. country_ie_len--;
  732. break;
  733. }
  734. reg_rule = &rd->reg_rules[i];
  735. freq_range = &reg_rule->freq_range;
  736. power_rule = &reg_rule->power_rule;
  737. reg_rule->flags = flags;
  738. /* 2 GHz */
  739. if (triplet->chans.first_channel <= 14)
  740. end_channel = triplet->chans.first_channel +
  741. triplet->chans.num_channels -1;
  742. else
  743. end_channel = triplet->chans.first_channel +
  744. (4 * (triplet->chans.num_channels - 1));
  745. end_channel = max_subband_chan(triplet->chans.first_channel,
  746. end_channel,
  747. triplet->chans.max_power,
  748. &country_ie,
  749. &country_ie_len);
  750. /*
  751. * The +10 is since the regulatory domain expects
  752. * the actual band edge, not the center of freq for
  753. * its start and end freqs, assuming 20 MHz bandwidth on
  754. * the channels passed
  755. */
  756. freq_range->start_freq_khz =
  757. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  758. triplet->chans.first_channel) - 10);
  759. freq_range->end_freq_khz =
  760. MHZ_TO_KHZ(ieee80211_channel_to_frequency(
  761. end_channel) + 10);
  762. /*
  763. * These are large arbitrary values we use to intersect later.
  764. * Increment this if we ever support >= 40 MHz channels
  765. * in IEEE 802.11
  766. */
  767. freq_range->max_bandwidth_khz = MHZ_TO_KHZ(40);
  768. power_rule->max_antenna_gain = DBI_TO_MBI(100);
  769. power_rule->max_eirp = DBM_TO_MBM(triplet->chans.max_power);
  770. i++;
  771. if (country_ie_len >= 3) {
  772. country_ie += 3;
  773. country_ie_len -= 3;
  774. }
  775. BUG_ON(i > NL80211_MAX_SUPP_REG_RULES);
  776. }
  777. return rd;
  778. }
  779. /*
  780. * Helper for regdom_intersect(), this does the real
  781. * mathematical intersection fun
  782. */
  783. static int reg_rules_intersect(
  784. const struct ieee80211_reg_rule *rule1,
  785. const struct ieee80211_reg_rule *rule2,
  786. struct ieee80211_reg_rule *intersected_rule)
  787. {
  788. const struct ieee80211_freq_range *freq_range1, *freq_range2;
  789. struct ieee80211_freq_range *freq_range;
  790. const struct ieee80211_power_rule *power_rule1, *power_rule2;
  791. struct ieee80211_power_rule *power_rule;
  792. u32 freq_diff;
  793. freq_range1 = &rule1->freq_range;
  794. freq_range2 = &rule2->freq_range;
  795. freq_range = &intersected_rule->freq_range;
  796. power_rule1 = &rule1->power_rule;
  797. power_rule2 = &rule2->power_rule;
  798. power_rule = &intersected_rule->power_rule;
  799. freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
  800. freq_range2->start_freq_khz);
  801. freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
  802. freq_range2->end_freq_khz);
  803. freq_range->max_bandwidth_khz = min(freq_range1->max_bandwidth_khz,
  804. freq_range2->max_bandwidth_khz);
  805. freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  806. if (freq_range->max_bandwidth_khz > freq_diff)
  807. freq_range->max_bandwidth_khz = freq_diff;
  808. power_rule->max_eirp = min(power_rule1->max_eirp,
  809. power_rule2->max_eirp);
  810. power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  811. power_rule2->max_antenna_gain);
  812. intersected_rule->flags = (rule1->flags | rule2->flags);
  813. if (!is_valid_reg_rule(intersected_rule))
  814. return -EINVAL;
  815. return 0;
  816. }
  817. /**
  818. * regdom_intersect - do the intersection between two regulatory domains
  819. * @rd1: first regulatory domain
  820. * @rd2: second regulatory domain
  821. *
  822. * Use this function to get the intersection between two regulatory domains.
  823. * Once completed we will mark the alpha2 for the rd as intersected, "98",
  824. * as no one single alpha2 can represent this regulatory domain.
  825. *
  826. * Returns a pointer to the regulatory domain structure which will hold the
  827. * resulting intersection of rules between rd1 and rd2. We will
  828. * kzalloc() this structure for you.
  829. */
  830. static struct ieee80211_regdomain *regdom_intersect(
  831. const struct ieee80211_regdomain *rd1,
  832. const struct ieee80211_regdomain *rd2)
  833. {
  834. int r, size_of_regd;
  835. unsigned int x, y;
  836. unsigned int num_rules = 0, rule_idx = 0;
  837. const struct ieee80211_reg_rule *rule1, *rule2;
  838. struct ieee80211_reg_rule *intersected_rule;
  839. struct ieee80211_regdomain *rd;
  840. /* This is just a dummy holder to help us count */
  841. struct ieee80211_reg_rule irule;
  842. /* Uses the stack temporarily for counter arithmetic */
  843. intersected_rule = &irule;
  844. memset(intersected_rule, 0, sizeof(struct ieee80211_reg_rule));
  845. if (!rd1 || !rd2)
  846. return NULL;
  847. /*
  848. * First we get a count of the rules we'll need, then we actually
  849. * build them. This is to so we can malloc() and free() a
  850. * regdomain once. The reason we use reg_rules_intersect() here
  851. * is it will return -EINVAL if the rule computed makes no sense.
  852. * All rules that do check out OK are valid.
  853. */
  854. for (x = 0; x < rd1->n_reg_rules; x++) {
  855. rule1 = &rd1->reg_rules[x];
  856. for (y = 0; y < rd2->n_reg_rules; y++) {
  857. rule2 = &rd2->reg_rules[y];
  858. if (!reg_rules_intersect(rule1, rule2,
  859. intersected_rule))
  860. num_rules++;
  861. memset(intersected_rule, 0,
  862. sizeof(struct ieee80211_reg_rule));
  863. }
  864. }
  865. if (!num_rules)
  866. return NULL;
  867. size_of_regd = sizeof(struct ieee80211_regdomain) +
  868. ((num_rules + 1) * sizeof(struct ieee80211_reg_rule));
  869. rd = kzalloc(size_of_regd, GFP_KERNEL);
  870. if (!rd)
  871. return NULL;
  872. for (x = 0; x < rd1->n_reg_rules; x++) {
  873. rule1 = &rd1->reg_rules[x];
  874. for (y = 0; y < rd2->n_reg_rules; y++) {
  875. rule2 = &rd2->reg_rules[y];
  876. /*
  877. * This time around instead of using the stack lets
  878. * write to the target rule directly saving ourselves
  879. * a memcpy()
  880. */
  881. intersected_rule = &rd->reg_rules[rule_idx];
  882. r = reg_rules_intersect(rule1, rule2,
  883. intersected_rule);
  884. /*
  885. * No need to memset here the intersected rule here as
  886. * we're not using the stack anymore
  887. */
  888. if (r)
  889. continue;
  890. rule_idx++;
  891. }
  892. }
  893. if (rule_idx != num_rules) {
  894. kfree(rd);
  895. return NULL;
  896. }
  897. rd->n_reg_rules = num_rules;
  898. rd->alpha2[0] = '9';
  899. rd->alpha2[1] = '8';
  900. return rd;
  901. }
  902. /*
  903. * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
  904. * want to just have the channel structure use these
  905. */
  906. static u32 map_regdom_flags(u32 rd_flags)
  907. {
  908. u32 channel_flags = 0;
  909. if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
  910. channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  911. if (rd_flags & NL80211_RRF_NO_IBSS)
  912. channel_flags |= IEEE80211_CHAN_NO_IBSS;
  913. if (rd_flags & NL80211_RRF_DFS)
  914. channel_flags |= IEEE80211_CHAN_RADAR;
  915. return channel_flags;
  916. }
  917. static int freq_reg_info_regd(struct wiphy *wiphy,
  918. u32 center_freq,
  919. u32 desired_bw_khz,
  920. const struct ieee80211_reg_rule **reg_rule,
  921. const struct ieee80211_regdomain *custom_regd)
  922. {
  923. int i;
  924. bool band_rule_found = false;
  925. const struct ieee80211_regdomain *regd;
  926. bool bw_fits = false;
  927. if (!desired_bw_khz)
  928. desired_bw_khz = MHZ_TO_KHZ(20);
  929. regd = custom_regd ? custom_regd : cfg80211_regdomain;
  930. /*
  931. * Follow the driver's regulatory domain, if present, unless a country
  932. * IE has been processed or a user wants to help complaince further
  933. */
  934. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  935. last_request->initiator != NL80211_REGDOM_SET_BY_USER &&
  936. wiphy->regd)
  937. regd = wiphy->regd;
  938. if (!regd)
  939. return -EINVAL;
  940. for (i = 0; i < regd->n_reg_rules; i++) {
  941. const struct ieee80211_reg_rule *rr;
  942. const struct ieee80211_freq_range *fr = NULL;
  943. const struct ieee80211_power_rule *pr = NULL;
  944. rr = &regd->reg_rules[i];
  945. fr = &rr->freq_range;
  946. pr = &rr->power_rule;
  947. /*
  948. * We only need to know if one frequency rule was
  949. * was in center_freq's band, that's enough, so lets
  950. * not overwrite it once found
  951. */
  952. if (!band_rule_found)
  953. band_rule_found = freq_in_rule_band(fr, center_freq);
  954. bw_fits = reg_does_bw_fit(fr,
  955. center_freq,
  956. desired_bw_khz);
  957. if (band_rule_found && bw_fits) {
  958. *reg_rule = rr;
  959. return 0;
  960. }
  961. }
  962. if (!band_rule_found)
  963. return -ERANGE;
  964. return -EINVAL;
  965. }
  966. EXPORT_SYMBOL(freq_reg_info);
  967. int freq_reg_info(struct wiphy *wiphy,
  968. u32 center_freq,
  969. u32 desired_bw_khz,
  970. const struct ieee80211_reg_rule **reg_rule)
  971. {
  972. assert_cfg80211_lock();
  973. return freq_reg_info_regd(wiphy,
  974. center_freq,
  975. desired_bw_khz,
  976. reg_rule,
  977. NULL);
  978. }
  979. /*
  980. * Note that right now we assume the desired channel bandwidth
  981. * is always 20 MHz for each individual channel (HT40 uses 20 MHz
  982. * per channel, the primary and the extension channel). To support
  983. * smaller custom bandwidths such as 5 MHz or 10 MHz we'll need a
  984. * new ieee80211_channel.target_bw and re run the regulatory check
  985. * on the wiphy with the target_bw specified. Then we can simply use
  986. * that below for the desired_bw_khz below.
  987. */
  988. static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
  989. unsigned int chan_idx)
  990. {
  991. int r;
  992. u32 flags, bw_flags = 0;
  993. u32 desired_bw_khz = MHZ_TO_KHZ(20);
  994. const struct ieee80211_reg_rule *reg_rule = NULL;
  995. const struct ieee80211_power_rule *power_rule = NULL;
  996. const struct ieee80211_freq_range *freq_range = NULL;
  997. struct ieee80211_supported_band *sband;
  998. struct ieee80211_channel *chan;
  999. struct wiphy *request_wiphy = NULL;
  1000. assert_cfg80211_lock();
  1001. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1002. sband = wiphy->bands[band];
  1003. BUG_ON(chan_idx >= sband->n_channels);
  1004. chan = &sband->channels[chan_idx];
  1005. flags = chan->orig_flags;
  1006. r = freq_reg_info(wiphy,
  1007. MHZ_TO_KHZ(chan->center_freq),
  1008. desired_bw_khz,
  1009. &reg_rule);
  1010. if (r) {
  1011. /*
  1012. * This means no regulatory rule was found in the country IE
  1013. * with a frequency range on the center_freq's band, since
  1014. * IEEE-802.11 allows for a country IE to have a subset of the
  1015. * regulatory information provided in a country we ignore
  1016. * disabling the channel unless at least one reg rule was
  1017. * found on the center_freq's band. For details see this
  1018. * clarification:
  1019. *
  1020. * http://tinyurl.com/11d-clarification
  1021. */
  1022. if (r == -ERANGE &&
  1023. last_request->initiator ==
  1024. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1025. REG_DBG_PRINT("cfg80211: Leaving channel %d MHz "
  1026. "intact on %s - no rule found in band on "
  1027. "Country IE\n",
  1028. chan->center_freq, wiphy_name(wiphy));
  1029. } else {
  1030. /*
  1031. * In this case we know the country IE has at least one reg rule
  1032. * for the band so we respect its band definitions
  1033. */
  1034. if (last_request->initiator ==
  1035. NL80211_REGDOM_SET_BY_COUNTRY_IE)
  1036. REG_DBG_PRINT("cfg80211: Disabling "
  1037. "channel %d MHz on %s due to "
  1038. "Country IE\n",
  1039. chan->center_freq, wiphy_name(wiphy));
  1040. flags |= IEEE80211_CHAN_DISABLED;
  1041. chan->flags = flags;
  1042. }
  1043. return;
  1044. }
  1045. power_rule = &reg_rule->power_rule;
  1046. freq_range = &reg_rule->freq_range;
  1047. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
  1048. bw_flags = IEEE80211_CHAN_NO_HT40;
  1049. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1050. request_wiphy && request_wiphy == wiphy &&
  1051. request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
  1052. /*
  1053. * This gaurantees the driver's requested regulatory domain
  1054. * will always be used as a base for further regulatory
  1055. * settings
  1056. */
  1057. chan->flags = chan->orig_flags =
  1058. map_regdom_flags(reg_rule->flags) | bw_flags;
  1059. chan->max_antenna_gain = chan->orig_mag =
  1060. (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  1061. chan->max_power = chan->orig_mpwr =
  1062. (int) MBM_TO_DBM(power_rule->max_eirp);
  1063. return;
  1064. }
  1065. chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
  1066. chan->max_antenna_gain = min(chan->orig_mag,
  1067. (int) MBI_TO_DBI(power_rule->max_antenna_gain));
  1068. if (chan->orig_mpwr)
  1069. chan->max_power = min(chan->orig_mpwr,
  1070. (int) MBM_TO_DBM(power_rule->max_eirp));
  1071. else
  1072. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  1073. }
  1074. static void handle_band(struct wiphy *wiphy, enum ieee80211_band band)
  1075. {
  1076. unsigned int i;
  1077. struct ieee80211_supported_band *sband;
  1078. BUG_ON(!wiphy->bands[band]);
  1079. sband = wiphy->bands[band];
  1080. for (i = 0; i < sband->n_channels; i++)
  1081. handle_channel(wiphy, band, i);
  1082. }
  1083. static bool ignore_reg_update(struct wiphy *wiphy,
  1084. enum nl80211_reg_initiator initiator)
  1085. {
  1086. if (!last_request)
  1087. return true;
  1088. if (initiator == NL80211_REGDOM_SET_BY_CORE &&
  1089. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
  1090. return true;
  1091. /*
  1092. * wiphy->regd will be set once the device has its own
  1093. * desired regulatory domain set
  1094. */
  1095. if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
  1096. !is_world_regdom(last_request->alpha2))
  1097. return true;
  1098. return false;
  1099. }
  1100. static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
  1101. {
  1102. struct cfg80211_registered_device *rdev;
  1103. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  1104. wiphy_update_regulatory(&rdev->wiphy, initiator);
  1105. }
  1106. static void handle_reg_beacon(struct wiphy *wiphy,
  1107. unsigned int chan_idx,
  1108. struct reg_beacon *reg_beacon)
  1109. {
  1110. struct ieee80211_supported_band *sband;
  1111. struct ieee80211_channel *chan;
  1112. bool channel_changed = false;
  1113. struct ieee80211_channel chan_before;
  1114. assert_cfg80211_lock();
  1115. sband = wiphy->bands[reg_beacon->chan.band];
  1116. chan = &sband->channels[chan_idx];
  1117. if (likely(chan->center_freq != reg_beacon->chan.center_freq))
  1118. return;
  1119. if (chan->beacon_found)
  1120. return;
  1121. chan->beacon_found = true;
  1122. if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
  1123. return;
  1124. chan_before.center_freq = chan->center_freq;
  1125. chan_before.flags = chan->flags;
  1126. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
  1127. chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  1128. channel_changed = true;
  1129. }
  1130. if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
  1131. chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
  1132. channel_changed = true;
  1133. }
  1134. if (channel_changed)
  1135. nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
  1136. }
  1137. /*
  1138. * Called when a scan on a wiphy finds a beacon on
  1139. * new channel
  1140. */
  1141. static void wiphy_update_new_beacon(struct wiphy *wiphy,
  1142. struct reg_beacon *reg_beacon)
  1143. {
  1144. unsigned int i;
  1145. struct ieee80211_supported_band *sband;
  1146. assert_cfg80211_lock();
  1147. if (!wiphy->bands[reg_beacon->chan.band])
  1148. return;
  1149. sband = wiphy->bands[reg_beacon->chan.band];
  1150. for (i = 0; i < sband->n_channels; i++)
  1151. handle_reg_beacon(wiphy, i, reg_beacon);
  1152. }
  1153. /*
  1154. * Called upon reg changes or a new wiphy is added
  1155. */
  1156. static void wiphy_update_beacon_reg(struct wiphy *wiphy)
  1157. {
  1158. unsigned int i;
  1159. struct ieee80211_supported_band *sband;
  1160. struct reg_beacon *reg_beacon;
  1161. assert_cfg80211_lock();
  1162. if (list_empty(&reg_beacon_list))
  1163. return;
  1164. list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
  1165. if (!wiphy->bands[reg_beacon->chan.band])
  1166. continue;
  1167. sband = wiphy->bands[reg_beacon->chan.band];
  1168. for (i = 0; i < sband->n_channels; i++)
  1169. handle_reg_beacon(wiphy, i, reg_beacon);
  1170. }
  1171. }
  1172. static bool reg_is_world_roaming(struct wiphy *wiphy)
  1173. {
  1174. if (is_world_regdom(cfg80211_regdomain->alpha2) ||
  1175. (wiphy->regd && is_world_regdom(wiphy->regd->alpha2)))
  1176. return true;
  1177. if (last_request &&
  1178. last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1179. wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
  1180. return true;
  1181. return false;
  1182. }
  1183. /* Reap the advantages of previously found beacons */
  1184. static void reg_process_beacons(struct wiphy *wiphy)
  1185. {
  1186. /*
  1187. * Means we are just firing up cfg80211, so no beacons would
  1188. * have been processed yet.
  1189. */
  1190. if (!last_request)
  1191. return;
  1192. if (!reg_is_world_roaming(wiphy))
  1193. return;
  1194. wiphy_update_beacon_reg(wiphy);
  1195. }
  1196. static bool is_ht40_not_allowed(struct ieee80211_channel *chan)
  1197. {
  1198. if (!chan)
  1199. return true;
  1200. if (chan->flags & IEEE80211_CHAN_DISABLED)
  1201. return true;
  1202. /* This would happen when regulatory rules disallow HT40 completely */
  1203. if (IEEE80211_CHAN_NO_HT40 == (chan->flags & (IEEE80211_CHAN_NO_HT40)))
  1204. return true;
  1205. return false;
  1206. }
  1207. static void reg_process_ht_flags_channel(struct wiphy *wiphy,
  1208. enum ieee80211_band band,
  1209. unsigned int chan_idx)
  1210. {
  1211. struct ieee80211_supported_band *sband;
  1212. struct ieee80211_channel *channel;
  1213. struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
  1214. unsigned int i;
  1215. assert_cfg80211_lock();
  1216. sband = wiphy->bands[band];
  1217. BUG_ON(chan_idx >= sband->n_channels);
  1218. channel = &sband->channels[chan_idx];
  1219. if (is_ht40_not_allowed(channel)) {
  1220. channel->flags |= IEEE80211_CHAN_NO_HT40;
  1221. return;
  1222. }
  1223. /*
  1224. * We need to ensure the extension channels exist to
  1225. * be able to use HT40- or HT40+, this finds them (or not)
  1226. */
  1227. for (i = 0; i < sband->n_channels; i++) {
  1228. struct ieee80211_channel *c = &sband->channels[i];
  1229. if (c->center_freq == (channel->center_freq - 20))
  1230. channel_before = c;
  1231. if (c->center_freq == (channel->center_freq + 20))
  1232. channel_after = c;
  1233. }
  1234. /*
  1235. * Please note that this assumes target bandwidth is 20 MHz,
  1236. * if that ever changes we also need to change the below logic
  1237. * to include that as well.
  1238. */
  1239. if (is_ht40_not_allowed(channel_before))
  1240. channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
  1241. else
  1242. channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
  1243. if (is_ht40_not_allowed(channel_after))
  1244. channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
  1245. else
  1246. channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
  1247. }
  1248. static void reg_process_ht_flags_band(struct wiphy *wiphy,
  1249. enum ieee80211_band band)
  1250. {
  1251. unsigned int i;
  1252. struct ieee80211_supported_band *sband;
  1253. BUG_ON(!wiphy->bands[band]);
  1254. sband = wiphy->bands[band];
  1255. for (i = 0; i < sband->n_channels; i++)
  1256. reg_process_ht_flags_channel(wiphy, band, i);
  1257. }
  1258. static void reg_process_ht_flags(struct wiphy *wiphy)
  1259. {
  1260. enum ieee80211_band band;
  1261. if (!wiphy)
  1262. return;
  1263. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1264. if (wiphy->bands[band])
  1265. reg_process_ht_flags_band(wiphy, band);
  1266. }
  1267. }
  1268. void wiphy_update_regulatory(struct wiphy *wiphy,
  1269. enum nl80211_reg_initiator initiator)
  1270. {
  1271. enum ieee80211_band band;
  1272. if (ignore_reg_update(wiphy, initiator))
  1273. goto out;
  1274. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1275. if (wiphy->bands[band])
  1276. handle_band(wiphy, band);
  1277. }
  1278. out:
  1279. reg_process_beacons(wiphy);
  1280. reg_process_ht_flags(wiphy);
  1281. if (wiphy->reg_notifier)
  1282. wiphy->reg_notifier(wiphy, last_request);
  1283. }
  1284. static void handle_channel_custom(struct wiphy *wiphy,
  1285. enum ieee80211_band band,
  1286. unsigned int chan_idx,
  1287. const struct ieee80211_regdomain *regd)
  1288. {
  1289. int r;
  1290. u32 desired_bw_khz = MHZ_TO_KHZ(20);
  1291. u32 bw_flags = 0;
  1292. const struct ieee80211_reg_rule *reg_rule = NULL;
  1293. const struct ieee80211_power_rule *power_rule = NULL;
  1294. const struct ieee80211_freq_range *freq_range = NULL;
  1295. struct ieee80211_supported_band *sband;
  1296. struct ieee80211_channel *chan;
  1297. assert_reg_lock();
  1298. sband = wiphy->bands[band];
  1299. BUG_ON(chan_idx >= sband->n_channels);
  1300. chan = &sband->channels[chan_idx];
  1301. r = freq_reg_info_regd(wiphy,
  1302. MHZ_TO_KHZ(chan->center_freq),
  1303. desired_bw_khz,
  1304. &reg_rule,
  1305. regd);
  1306. if (r) {
  1307. chan->flags = IEEE80211_CHAN_DISABLED;
  1308. return;
  1309. }
  1310. power_rule = &reg_rule->power_rule;
  1311. freq_range = &reg_rule->freq_range;
  1312. if (freq_range->max_bandwidth_khz < MHZ_TO_KHZ(40))
  1313. bw_flags = IEEE80211_CHAN_NO_HT40;
  1314. chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
  1315. chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
  1316. chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
  1317. }
  1318. static void handle_band_custom(struct wiphy *wiphy, enum ieee80211_band band,
  1319. const struct ieee80211_regdomain *regd)
  1320. {
  1321. unsigned int i;
  1322. struct ieee80211_supported_band *sband;
  1323. BUG_ON(!wiphy->bands[band]);
  1324. sband = wiphy->bands[band];
  1325. for (i = 0; i < sband->n_channels; i++)
  1326. handle_channel_custom(wiphy, band, i, regd);
  1327. }
  1328. /* Used by drivers prior to wiphy registration */
  1329. void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  1330. const struct ieee80211_regdomain *regd)
  1331. {
  1332. enum ieee80211_band band;
  1333. unsigned int bands_set = 0;
  1334. mutex_lock(&reg_mutex);
  1335. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1336. if (!wiphy->bands[band])
  1337. continue;
  1338. handle_band_custom(wiphy, band, regd);
  1339. bands_set++;
  1340. }
  1341. mutex_unlock(&reg_mutex);
  1342. /*
  1343. * no point in calling this if it won't have any effect
  1344. * on your device's supportd bands.
  1345. */
  1346. WARN_ON(!bands_set);
  1347. }
  1348. EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
  1349. /*
  1350. * Return value which can be used by ignore_request() to indicate
  1351. * it has been determined we should intersect two regulatory domains
  1352. */
  1353. #define REG_INTERSECT 1
  1354. /* This has the logic which determines when a new request
  1355. * should be ignored. */
  1356. static int ignore_request(struct wiphy *wiphy,
  1357. struct regulatory_request *pending_request)
  1358. {
  1359. struct wiphy *last_wiphy = NULL;
  1360. assert_cfg80211_lock();
  1361. /* All initial requests are respected */
  1362. if (!last_request)
  1363. return 0;
  1364. switch (pending_request->initiator) {
  1365. case NL80211_REGDOM_SET_BY_CORE:
  1366. return -EINVAL;
  1367. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  1368. last_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1369. if (unlikely(!is_an_alpha2(pending_request->alpha2)))
  1370. return -EINVAL;
  1371. if (last_request->initiator ==
  1372. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1373. if (last_wiphy != wiphy) {
  1374. /*
  1375. * Two cards with two APs claiming different
  1376. * Country IE alpha2s. We could
  1377. * intersect them, but that seems unlikely
  1378. * to be correct. Reject second one for now.
  1379. */
  1380. if (regdom_changes(pending_request->alpha2))
  1381. return -EOPNOTSUPP;
  1382. return -EALREADY;
  1383. }
  1384. /*
  1385. * Two consecutive Country IE hints on the same wiphy.
  1386. * This should be picked up early by the driver/stack
  1387. */
  1388. if (WARN_ON(regdom_changes(pending_request->alpha2)))
  1389. return 0;
  1390. return -EALREADY;
  1391. }
  1392. return REG_INTERSECT;
  1393. case NL80211_REGDOM_SET_BY_DRIVER:
  1394. if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
  1395. if (regdom_changes(pending_request->alpha2))
  1396. return 0;
  1397. return -EALREADY;
  1398. }
  1399. /*
  1400. * This would happen if you unplug and plug your card
  1401. * back in or if you add a new device for which the previously
  1402. * loaded card also agrees on the regulatory domain.
  1403. */
  1404. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1405. !regdom_changes(pending_request->alpha2))
  1406. return -EALREADY;
  1407. return REG_INTERSECT;
  1408. case NL80211_REGDOM_SET_BY_USER:
  1409. if (last_request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  1410. return REG_INTERSECT;
  1411. /*
  1412. * If the user knows better the user should set the regdom
  1413. * to their country before the IE is picked up
  1414. */
  1415. if (last_request->initiator == NL80211_REGDOM_SET_BY_USER &&
  1416. last_request->intersect)
  1417. return -EOPNOTSUPP;
  1418. /*
  1419. * Process user requests only after previous user/driver/core
  1420. * requests have been processed
  1421. */
  1422. if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE ||
  1423. last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  1424. last_request->initiator == NL80211_REGDOM_SET_BY_USER) {
  1425. if (regdom_changes(last_request->alpha2))
  1426. return -EAGAIN;
  1427. }
  1428. if (!regdom_changes(pending_request->alpha2))
  1429. return -EALREADY;
  1430. return 0;
  1431. }
  1432. return -EINVAL;
  1433. }
  1434. /**
  1435. * __regulatory_hint - hint to the wireless core a regulatory domain
  1436. * @wiphy: if the hint comes from country information from an AP, this
  1437. * is required to be set to the wiphy that received the information
  1438. * @pending_request: the regulatory request currently being processed
  1439. *
  1440. * The Wireless subsystem can use this function to hint to the wireless core
  1441. * what it believes should be the current regulatory domain.
  1442. *
  1443. * Returns zero if all went fine, %-EALREADY if a regulatory domain had
  1444. * already been set or other standard error codes.
  1445. *
  1446. * Caller must hold &cfg80211_mutex and &reg_mutex
  1447. */
  1448. static int __regulatory_hint(struct wiphy *wiphy,
  1449. struct regulatory_request *pending_request)
  1450. {
  1451. bool intersect = false;
  1452. int r = 0;
  1453. assert_cfg80211_lock();
  1454. r = ignore_request(wiphy, pending_request);
  1455. if (r == REG_INTERSECT) {
  1456. if (pending_request->initiator ==
  1457. NL80211_REGDOM_SET_BY_DRIVER) {
  1458. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1459. if (r) {
  1460. kfree(pending_request);
  1461. return r;
  1462. }
  1463. }
  1464. intersect = true;
  1465. } else if (r) {
  1466. /*
  1467. * If the regulatory domain being requested by the
  1468. * driver has already been set just copy it to the
  1469. * wiphy
  1470. */
  1471. if (r == -EALREADY &&
  1472. pending_request->initiator ==
  1473. NL80211_REGDOM_SET_BY_DRIVER) {
  1474. r = reg_copy_regd(&wiphy->regd, cfg80211_regdomain);
  1475. if (r) {
  1476. kfree(pending_request);
  1477. return r;
  1478. }
  1479. r = -EALREADY;
  1480. goto new_request;
  1481. }
  1482. kfree(pending_request);
  1483. return r;
  1484. }
  1485. new_request:
  1486. kfree(last_request);
  1487. last_request = pending_request;
  1488. last_request->intersect = intersect;
  1489. pending_request = NULL;
  1490. /* When r == REG_INTERSECT we do need to call CRDA */
  1491. if (r < 0) {
  1492. /*
  1493. * Since CRDA will not be called in this case as we already
  1494. * have applied the requested regulatory domain before we just
  1495. * inform userspace we have processed the request
  1496. */
  1497. if (r == -EALREADY)
  1498. nl80211_send_reg_change_event(last_request);
  1499. return r;
  1500. }
  1501. return call_crda(last_request->alpha2);
  1502. }
  1503. /* This processes *all* regulatory hints */
  1504. static void reg_process_hint(struct regulatory_request *reg_request)
  1505. {
  1506. int r = 0;
  1507. struct wiphy *wiphy = NULL;
  1508. BUG_ON(!reg_request->alpha2);
  1509. mutex_lock(&cfg80211_mutex);
  1510. mutex_lock(&reg_mutex);
  1511. if (wiphy_idx_valid(reg_request->wiphy_idx))
  1512. wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
  1513. if (reg_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  1514. !wiphy) {
  1515. kfree(reg_request);
  1516. goto out;
  1517. }
  1518. r = __regulatory_hint(wiphy, reg_request);
  1519. /* This is required so that the orig_* parameters are saved */
  1520. if (r == -EALREADY && wiphy &&
  1521. wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
  1522. wiphy_update_regulatory(wiphy, reg_request->initiator);
  1523. out:
  1524. mutex_unlock(&reg_mutex);
  1525. mutex_unlock(&cfg80211_mutex);
  1526. }
  1527. /* Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_* */
  1528. static void reg_process_pending_hints(void)
  1529. {
  1530. struct regulatory_request *reg_request;
  1531. spin_lock(&reg_requests_lock);
  1532. while (!list_empty(&reg_requests_list)) {
  1533. reg_request = list_first_entry(&reg_requests_list,
  1534. struct regulatory_request,
  1535. list);
  1536. list_del_init(&reg_request->list);
  1537. spin_unlock(&reg_requests_lock);
  1538. reg_process_hint(reg_request);
  1539. spin_lock(&reg_requests_lock);
  1540. }
  1541. spin_unlock(&reg_requests_lock);
  1542. }
  1543. /* Processes beacon hints -- this has nothing to do with country IEs */
  1544. static void reg_process_pending_beacon_hints(void)
  1545. {
  1546. struct cfg80211_registered_device *rdev;
  1547. struct reg_beacon *pending_beacon, *tmp;
  1548. /*
  1549. * No need to hold the reg_mutex here as we just touch wiphys
  1550. * and do not read or access regulatory variables.
  1551. */
  1552. mutex_lock(&cfg80211_mutex);
  1553. /* This goes through the _pending_ beacon list */
  1554. spin_lock_bh(&reg_pending_beacons_lock);
  1555. if (list_empty(&reg_pending_beacons)) {
  1556. spin_unlock_bh(&reg_pending_beacons_lock);
  1557. goto out;
  1558. }
  1559. list_for_each_entry_safe(pending_beacon, tmp,
  1560. &reg_pending_beacons, list) {
  1561. list_del_init(&pending_beacon->list);
  1562. /* Applies the beacon hint to current wiphys */
  1563. list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  1564. wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
  1565. /* Remembers the beacon hint for new wiphys or reg changes */
  1566. list_add_tail(&pending_beacon->list, &reg_beacon_list);
  1567. }
  1568. spin_unlock_bh(&reg_pending_beacons_lock);
  1569. out:
  1570. mutex_unlock(&cfg80211_mutex);
  1571. }
  1572. static void reg_todo(struct work_struct *work)
  1573. {
  1574. reg_process_pending_hints();
  1575. reg_process_pending_beacon_hints();
  1576. }
  1577. static DECLARE_WORK(reg_work, reg_todo);
  1578. static void queue_regulatory_request(struct regulatory_request *request)
  1579. {
  1580. spin_lock(&reg_requests_lock);
  1581. list_add_tail(&request->list, &reg_requests_list);
  1582. spin_unlock(&reg_requests_lock);
  1583. schedule_work(&reg_work);
  1584. }
  1585. /* Core regulatory hint -- happens once during cfg80211_init() */
  1586. static int regulatory_hint_core(const char *alpha2)
  1587. {
  1588. struct regulatory_request *request;
  1589. BUG_ON(last_request);
  1590. request = kzalloc(sizeof(struct regulatory_request),
  1591. GFP_KERNEL);
  1592. if (!request)
  1593. return -ENOMEM;
  1594. request->alpha2[0] = alpha2[0];
  1595. request->alpha2[1] = alpha2[1];
  1596. request->initiator = NL80211_REGDOM_SET_BY_CORE;
  1597. queue_regulatory_request(request);
  1598. /*
  1599. * This ensures last_request is populated once modules
  1600. * come swinging in and calling regulatory hints and
  1601. * wiphy_apply_custom_regulatory().
  1602. */
  1603. flush_scheduled_work();
  1604. return 0;
  1605. }
  1606. /* User hints */
  1607. int regulatory_hint_user(const char *alpha2)
  1608. {
  1609. struct regulatory_request *request;
  1610. BUG_ON(!alpha2);
  1611. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1612. if (!request)
  1613. return -ENOMEM;
  1614. request->wiphy_idx = WIPHY_IDX_STALE;
  1615. request->alpha2[0] = alpha2[0];
  1616. request->alpha2[1] = alpha2[1];
  1617. request->initiator = NL80211_REGDOM_SET_BY_USER;
  1618. queue_regulatory_request(request);
  1619. return 0;
  1620. }
  1621. /* Driver hints */
  1622. int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  1623. {
  1624. struct regulatory_request *request;
  1625. BUG_ON(!alpha2);
  1626. BUG_ON(!wiphy);
  1627. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1628. if (!request)
  1629. return -ENOMEM;
  1630. request->wiphy_idx = get_wiphy_idx(wiphy);
  1631. /* Must have registered wiphy first */
  1632. BUG_ON(!wiphy_idx_valid(request->wiphy_idx));
  1633. request->alpha2[0] = alpha2[0];
  1634. request->alpha2[1] = alpha2[1];
  1635. request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
  1636. queue_regulatory_request(request);
  1637. return 0;
  1638. }
  1639. EXPORT_SYMBOL(regulatory_hint);
  1640. /* Caller must hold reg_mutex */
  1641. static bool reg_same_country_ie_hint(struct wiphy *wiphy,
  1642. u32 country_ie_checksum)
  1643. {
  1644. struct wiphy *request_wiphy;
  1645. assert_reg_lock();
  1646. if (unlikely(last_request->initiator !=
  1647. NL80211_REGDOM_SET_BY_COUNTRY_IE))
  1648. return false;
  1649. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1650. if (!request_wiphy)
  1651. return false;
  1652. if (likely(request_wiphy != wiphy))
  1653. return !country_ie_integrity_changes(country_ie_checksum);
  1654. /*
  1655. * We should not have let these through at this point, they
  1656. * should have been picked up earlier by the first alpha2 check
  1657. * on the device
  1658. */
  1659. if (WARN_ON(!country_ie_integrity_changes(country_ie_checksum)))
  1660. return true;
  1661. return false;
  1662. }
  1663. /*
  1664. * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
  1665. * therefore cannot iterate over the rdev list here.
  1666. */
  1667. void regulatory_hint_11d(struct wiphy *wiphy,
  1668. u8 *country_ie,
  1669. u8 country_ie_len)
  1670. {
  1671. struct ieee80211_regdomain *rd = NULL;
  1672. char alpha2[2];
  1673. u32 checksum = 0;
  1674. enum environment_cap env = ENVIRON_ANY;
  1675. struct regulatory_request *request;
  1676. mutex_lock(&reg_mutex);
  1677. if (unlikely(!last_request))
  1678. goto out;
  1679. /* IE len must be evenly divisible by 2 */
  1680. if (country_ie_len & 0x01)
  1681. goto out;
  1682. if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1683. goto out;
  1684. /*
  1685. * Pending country IE processing, this can happen after we
  1686. * call CRDA and wait for a response if a beacon was received before
  1687. * we were able to process the last regulatory_hint_11d() call
  1688. */
  1689. if (country_ie_regdomain)
  1690. goto out;
  1691. alpha2[0] = country_ie[0];
  1692. alpha2[1] = country_ie[1];
  1693. if (country_ie[2] == 'I')
  1694. env = ENVIRON_INDOOR;
  1695. else if (country_ie[2] == 'O')
  1696. env = ENVIRON_OUTDOOR;
  1697. /*
  1698. * We will run this only upon a successful connection on cfg80211.
  1699. * We leave conflict resolution to the workqueue, where can hold
  1700. * cfg80211_mutex.
  1701. */
  1702. if (likely(last_request->initiator ==
  1703. NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  1704. wiphy_idx_valid(last_request->wiphy_idx)))
  1705. goto out;
  1706. rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
  1707. if (!rd) {
  1708. REG_DBG_PRINT("cfg80211: Ignoring bogus country IE\n");
  1709. goto out;
  1710. }
  1711. /*
  1712. * This will not happen right now but we leave it here for the
  1713. * the future when we want to add suspend/resume support and having
  1714. * the user move to another country after doing so, or having the user
  1715. * move to another AP. Right now we just trust the first AP.
  1716. *
  1717. * If we hit this before we add this support we want to be informed of
  1718. * it as it would indicate a mistake in the current design
  1719. */
  1720. if (WARN_ON(reg_same_country_ie_hint(wiphy, checksum)))
  1721. goto free_rd_out;
  1722. request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  1723. if (!request)
  1724. goto free_rd_out;
  1725. /*
  1726. * We keep this around for when CRDA comes back with a response so
  1727. * we can intersect with that
  1728. */
  1729. country_ie_regdomain = rd;
  1730. request->wiphy_idx = get_wiphy_idx(wiphy);
  1731. request->alpha2[0] = rd->alpha2[0];
  1732. request->alpha2[1] = rd->alpha2[1];
  1733. request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
  1734. request->country_ie_checksum = checksum;
  1735. request->country_ie_env = env;
  1736. mutex_unlock(&reg_mutex);
  1737. queue_regulatory_request(request);
  1738. return;
  1739. free_rd_out:
  1740. kfree(rd);
  1741. out:
  1742. mutex_unlock(&reg_mutex);
  1743. }
  1744. static bool freq_is_chan_12_13_14(u16 freq)
  1745. {
  1746. if (freq == ieee80211_channel_to_frequency(12) ||
  1747. freq == ieee80211_channel_to_frequency(13) ||
  1748. freq == ieee80211_channel_to_frequency(14))
  1749. return true;
  1750. return false;
  1751. }
  1752. int regulatory_hint_found_beacon(struct wiphy *wiphy,
  1753. struct ieee80211_channel *beacon_chan,
  1754. gfp_t gfp)
  1755. {
  1756. struct reg_beacon *reg_beacon;
  1757. if (likely((beacon_chan->beacon_found ||
  1758. (beacon_chan->flags & IEEE80211_CHAN_RADAR) ||
  1759. (beacon_chan->band == IEEE80211_BAND_2GHZ &&
  1760. !freq_is_chan_12_13_14(beacon_chan->center_freq)))))
  1761. return 0;
  1762. reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
  1763. if (!reg_beacon)
  1764. return -ENOMEM;
  1765. REG_DBG_PRINT("cfg80211: Found new beacon on "
  1766. "frequency: %d MHz (Ch %d) on %s\n",
  1767. beacon_chan->center_freq,
  1768. ieee80211_frequency_to_channel(beacon_chan->center_freq),
  1769. wiphy_name(wiphy));
  1770. memcpy(&reg_beacon->chan, beacon_chan,
  1771. sizeof(struct ieee80211_channel));
  1772. /*
  1773. * Since we can be called from BH or and non-BH context
  1774. * we must use spin_lock_bh()
  1775. */
  1776. spin_lock_bh(&reg_pending_beacons_lock);
  1777. list_add_tail(&reg_beacon->list, &reg_pending_beacons);
  1778. spin_unlock_bh(&reg_pending_beacons_lock);
  1779. schedule_work(&reg_work);
  1780. return 0;
  1781. }
  1782. static void print_rd_rules(const struct ieee80211_regdomain *rd)
  1783. {
  1784. unsigned int i;
  1785. const struct ieee80211_reg_rule *reg_rule = NULL;
  1786. const struct ieee80211_freq_range *freq_range = NULL;
  1787. const struct ieee80211_power_rule *power_rule = NULL;
  1788. printk(KERN_INFO " (start_freq - end_freq @ bandwidth), "
  1789. "(max_antenna_gain, max_eirp)\n");
  1790. for (i = 0; i < rd->n_reg_rules; i++) {
  1791. reg_rule = &rd->reg_rules[i];
  1792. freq_range = &reg_rule->freq_range;
  1793. power_rule = &reg_rule->power_rule;
  1794. /*
  1795. * There may not be documentation for max antenna gain
  1796. * in certain regions
  1797. */
  1798. if (power_rule->max_antenna_gain)
  1799. printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
  1800. "(%d mBi, %d mBm)\n",
  1801. freq_range->start_freq_khz,
  1802. freq_range->end_freq_khz,
  1803. freq_range->max_bandwidth_khz,
  1804. power_rule->max_antenna_gain,
  1805. power_rule->max_eirp);
  1806. else
  1807. printk(KERN_INFO " (%d KHz - %d KHz @ %d KHz), "
  1808. "(N/A, %d mBm)\n",
  1809. freq_range->start_freq_khz,
  1810. freq_range->end_freq_khz,
  1811. freq_range->max_bandwidth_khz,
  1812. power_rule->max_eirp);
  1813. }
  1814. }
  1815. static void print_regdomain(const struct ieee80211_regdomain *rd)
  1816. {
  1817. if (is_intersected_alpha2(rd->alpha2)) {
  1818. if (last_request->initiator ==
  1819. NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1820. struct cfg80211_registered_device *rdev;
  1821. rdev = cfg80211_rdev_by_wiphy_idx(
  1822. last_request->wiphy_idx);
  1823. if (rdev) {
  1824. printk(KERN_INFO "cfg80211: Current regulatory "
  1825. "domain updated by AP to: %c%c\n",
  1826. rdev->country_ie_alpha2[0],
  1827. rdev->country_ie_alpha2[1]);
  1828. } else
  1829. printk(KERN_INFO "cfg80211: Current regulatory "
  1830. "domain intersected: \n");
  1831. } else
  1832. printk(KERN_INFO "cfg80211: Current regulatory "
  1833. "domain intersected: \n");
  1834. } else if (is_world_regdom(rd->alpha2))
  1835. printk(KERN_INFO "cfg80211: World regulatory "
  1836. "domain updated:\n");
  1837. else {
  1838. if (is_unknown_alpha2(rd->alpha2))
  1839. printk(KERN_INFO "cfg80211: Regulatory domain "
  1840. "changed to driver built-in settings "
  1841. "(unknown country)\n");
  1842. else
  1843. printk(KERN_INFO "cfg80211: Regulatory domain "
  1844. "changed to country: %c%c\n",
  1845. rd->alpha2[0], rd->alpha2[1]);
  1846. }
  1847. print_rd_rules(rd);
  1848. }
  1849. static void print_regdomain_info(const struct ieee80211_regdomain *rd)
  1850. {
  1851. printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
  1852. rd->alpha2[0], rd->alpha2[1]);
  1853. print_rd_rules(rd);
  1854. }
  1855. #ifdef CONFIG_CFG80211_REG_DEBUG
  1856. static void reg_country_ie_process_debug(
  1857. const struct ieee80211_regdomain *rd,
  1858. const struct ieee80211_regdomain *country_ie_regdomain,
  1859. const struct ieee80211_regdomain *intersected_rd)
  1860. {
  1861. printk(KERN_DEBUG "cfg80211: Received country IE:\n");
  1862. print_regdomain_info(country_ie_regdomain);
  1863. printk(KERN_DEBUG "cfg80211: CRDA thinks this should applied:\n");
  1864. print_regdomain_info(rd);
  1865. if (intersected_rd) {
  1866. printk(KERN_DEBUG "cfg80211: We intersect both of these "
  1867. "and get:\n");
  1868. print_regdomain_info(intersected_rd);
  1869. return;
  1870. }
  1871. printk(KERN_DEBUG "cfg80211: Intersection between both failed\n");
  1872. }
  1873. #else
  1874. static inline void reg_country_ie_process_debug(
  1875. const struct ieee80211_regdomain *rd,
  1876. const struct ieee80211_regdomain *country_ie_regdomain,
  1877. const struct ieee80211_regdomain *intersected_rd)
  1878. {
  1879. }
  1880. #endif
  1881. /* Takes ownership of rd only if it doesn't fail */
  1882. static int __set_regdom(const struct ieee80211_regdomain *rd)
  1883. {
  1884. const struct ieee80211_regdomain *intersected_rd = NULL;
  1885. struct cfg80211_registered_device *rdev = NULL;
  1886. struct wiphy *request_wiphy;
  1887. /* Some basic sanity checks first */
  1888. if (is_world_regdom(rd->alpha2)) {
  1889. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1890. return -EINVAL;
  1891. update_world_regdomain(rd);
  1892. return 0;
  1893. }
  1894. if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  1895. !is_unknown_alpha2(rd->alpha2))
  1896. return -EINVAL;
  1897. if (!last_request)
  1898. return -EINVAL;
  1899. /*
  1900. * Lets only bother proceeding on the same alpha2 if the current
  1901. * rd is non static (it means CRDA was present and was used last)
  1902. * and the pending request came in from a country IE
  1903. */
  1904. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1905. /*
  1906. * If someone else asked us to change the rd lets only bother
  1907. * checking if the alpha2 changes if CRDA was already called
  1908. */
  1909. if (!regdom_changes(rd->alpha2))
  1910. return -EINVAL;
  1911. }
  1912. /*
  1913. * Now lets set the regulatory domain, update all driver channels
  1914. * and finally inform them of what we have done, in case they want
  1915. * to review or adjust their own settings based on their own
  1916. * internal EEPROM data
  1917. */
  1918. if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
  1919. return -EINVAL;
  1920. if (!is_valid_rd(rd)) {
  1921. printk(KERN_ERR "cfg80211: Invalid "
  1922. "regulatory domain detected:\n");
  1923. print_regdomain_info(rd);
  1924. return -EINVAL;
  1925. }
  1926. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  1927. if (!last_request->intersect) {
  1928. int r;
  1929. if (last_request->initiator != NL80211_REGDOM_SET_BY_DRIVER) {
  1930. reset_regdomains();
  1931. cfg80211_regdomain = rd;
  1932. return 0;
  1933. }
  1934. /*
  1935. * For a driver hint, lets copy the regulatory domain the
  1936. * driver wanted to the wiphy to deal with conflicts
  1937. */
  1938. /*
  1939. * Userspace could have sent two replies with only
  1940. * one kernel request.
  1941. */
  1942. if (request_wiphy->regd)
  1943. return -EALREADY;
  1944. r = reg_copy_regd(&request_wiphy->regd, rd);
  1945. if (r)
  1946. return r;
  1947. reset_regdomains();
  1948. cfg80211_regdomain = rd;
  1949. return 0;
  1950. }
  1951. /* Intersection requires a bit more work */
  1952. if (last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  1953. intersected_rd = regdom_intersect(rd, cfg80211_regdomain);
  1954. if (!intersected_rd)
  1955. return -EINVAL;
  1956. /*
  1957. * We can trash what CRDA provided now.
  1958. * However if a driver requested this specific regulatory
  1959. * domain we keep it for its private use
  1960. */
  1961. if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER)
  1962. request_wiphy->regd = rd;
  1963. else
  1964. kfree(rd);
  1965. rd = NULL;
  1966. reset_regdomains();
  1967. cfg80211_regdomain = intersected_rd;
  1968. return 0;
  1969. }
  1970. /*
  1971. * Country IE requests are handled a bit differently, we intersect
  1972. * the country IE rd with what CRDA believes that country should have
  1973. */
  1974. /*
  1975. * Userspace could have sent two replies with only
  1976. * one kernel request. By the second reply we would have
  1977. * already processed and consumed the country_ie_regdomain.
  1978. */
  1979. if (!country_ie_regdomain)
  1980. return -EALREADY;
  1981. BUG_ON(rd == country_ie_regdomain);
  1982. /*
  1983. * Intersect what CRDA returned and our what we
  1984. * had built from the Country IE received
  1985. */
  1986. intersected_rd = regdom_intersect(rd, country_ie_regdomain);
  1987. reg_country_ie_process_debug(rd,
  1988. country_ie_regdomain,
  1989. intersected_rd);
  1990. kfree(country_ie_regdomain);
  1991. country_ie_regdomain = NULL;
  1992. if (!intersected_rd)
  1993. return -EINVAL;
  1994. rdev = wiphy_to_dev(request_wiphy);
  1995. rdev->country_ie_alpha2[0] = rd->alpha2[0];
  1996. rdev->country_ie_alpha2[1] = rd->alpha2[1];
  1997. rdev->env = last_request->country_ie_env;
  1998. BUG_ON(intersected_rd == rd);
  1999. kfree(rd);
  2000. rd = NULL;
  2001. reset_regdomains();
  2002. cfg80211_regdomain = intersected_rd;
  2003. return 0;
  2004. }
  2005. /*
  2006. * Use this call to set the current regulatory domain. Conflicts with
  2007. * multiple drivers can be ironed out later. Caller must've already
  2008. * kmalloc'd the rd structure. Caller must hold cfg80211_mutex
  2009. */
  2010. int set_regdom(const struct ieee80211_regdomain *rd)
  2011. {
  2012. int r;
  2013. assert_cfg80211_lock();
  2014. mutex_lock(&reg_mutex);
  2015. /* Note that this doesn't update the wiphys, this is done below */
  2016. r = __set_regdom(rd);
  2017. if (r) {
  2018. kfree(rd);
  2019. mutex_unlock(&reg_mutex);
  2020. return r;
  2021. }
  2022. /* This would make this whole thing pointless */
  2023. if (!last_request->intersect)
  2024. BUG_ON(rd != cfg80211_regdomain);
  2025. /* update all wiphys now with the new established regulatory domain */
  2026. update_all_wiphy_regulatory(last_request->initiator);
  2027. print_regdomain(cfg80211_regdomain);
  2028. nl80211_send_reg_change_event(last_request);
  2029. mutex_unlock(&reg_mutex);
  2030. return r;
  2031. }
  2032. /* Caller must hold cfg80211_mutex */
  2033. void reg_device_remove(struct wiphy *wiphy)
  2034. {
  2035. struct wiphy *request_wiphy = NULL;
  2036. assert_cfg80211_lock();
  2037. mutex_lock(&reg_mutex);
  2038. kfree(wiphy->regd);
  2039. if (last_request)
  2040. request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
  2041. if (!request_wiphy || request_wiphy != wiphy)
  2042. goto out;
  2043. last_request->wiphy_idx = WIPHY_IDX_STALE;
  2044. last_request->country_ie_env = ENVIRON_ANY;
  2045. out:
  2046. mutex_unlock(&reg_mutex);
  2047. }
  2048. int regulatory_init(void)
  2049. {
  2050. int err = 0;
  2051. reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  2052. if (IS_ERR(reg_pdev))
  2053. return PTR_ERR(reg_pdev);
  2054. spin_lock_init(&reg_requests_lock);
  2055. spin_lock_init(&reg_pending_beacons_lock);
  2056. cfg80211_regdomain = cfg80211_world_regdom;
  2057. /* We always try to get an update for the static regdomain */
  2058. err = regulatory_hint_core(cfg80211_regdomain->alpha2);
  2059. if (err) {
  2060. if (err == -ENOMEM)
  2061. return err;
  2062. /*
  2063. * N.B. kobject_uevent_env() can fail mainly for when we're out
  2064. * memory which is handled and propagated appropriately above
  2065. * but it can also fail during a netlink_broadcast() or during
  2066. * early boot for call_usermodehelper(). For now treat these
  2067. * errors as non-fatal.
  2068. */
  2069. printk(KERN_ERR "cfg80211: kobject_uevent_env() was unable "
  2070. "to call CRDA during init");
  2071. #ifdef CONFIG_CFG80211_REG_DEBUG
  2072. /* We want to find out exactly why when debugging */
  2073. WARN_ON(err);
  2074. #endif
  2075. }
  2076. /*
  2077. * Finally, if the user set the module parameter treat it
  2078. * as a user hint.
  2079. */
  2080. if (!is_world_regdom(ieee80211_regdom))
  2081. regulatory_hint_user(ieee80211_regdom);
  2082. return 0;
  2083. }
  2084. void regulatory_exit(void)
  2085. {
  2086. struct regulatory_request *reg_request, *tmp;
  2087. struct reg_beacon *reg_beacon, *btmp;
  2088. cancel_work_sync(&reg_work);
  2089. mutex_lock(&cfg80211_mutex);
  2090. mutex_lock(&reg_mutex);
  2091. reset_regdomains();
  2092. kfree(country_ie_regdomain);
  2093. country_ie_regdomain = NULL;
  2094. kfree(last_request);
  2095. platform_device_unregister(reg_pdev);
  2096. spin_lock_bh(&reg_pending_beacons_lock);
  2097. if (!list_empty(&reg_pending_beacons)) {
  2098. list_for_each_entry_safe(reg_beacon, btmp,
  2099. &reg_pending_beacons, list) {
  2100. list_del(&reg_beacon->list);
  2101. kfree(reg_beacon);
  2102. }
  2103. }
  2104. spin_unlock_bh(&reg_pending_beacons_lock);
  2105. if (!list_empty(&reg_beacon_list)) {
  2106. list_for_each_entry_safe(reg_beacon, btmp,
  2107. &reg_beacon_list, list) {
  2108. list_del(&reg_beacon->list);
  2109. kfree(reg_beacon);
  2110. }
  2111. }
  2112. spin_lock(&reg_requests_lock);
  2113. if (!list_empty(&reg_requests_list)) {
  2114. list_for_each_entry_safe(reg_request, tmp,
  2115. &reg_requests_list, list) {
  2116. list_del(&reg_request->list);
  2117. kfree(reg_request);
  2118. }
  2119. }
  2120. spin_unlock(&reg_requests_lock);
  2121. mutex_unlock(&reg_mutex);
  2122. mutex_unlock(&cfg80211_mutex);
  2123. }