reg.c 65 KB

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