reg.c 64 KB

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