reg.c 70 KB

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