reg.c 70 KB

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