Blame view

net/wireless/reg.c 74 KB
8318d78a4   Johannes Berg   cfg80211 API for ...
1
2
3
4
  /*
   * Copyright 2002-2005, Instant802 Networks, Inc.
   * Copyright 2005-2006, Devicescape Software, Inc.
   * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
3b77d5ec0   Luis R. Rodriguez   cfg80211: relicen...
5
   * Copyright 2008-2011	Luis R. Rodriguez <mcgrof@qca.qualcomm.com>
8318d78a4   Johannes Berg   cfg80211 API for ...
6
   *
3b77d5ec0   Luis R. Rodriguez   cfg80211: relicen...
7
8
9
10
11
12
13
14
15
16
17
   * Permission to use, copy, modify, and/or distribute this software for any
   * purpose with or without fee is hereby granted, provided that the above
   * copyright notice and this permission notice appear in all copies.
   *
   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
8318d78a4   Johannes Berg   cfg80211 API for ...
18
   */
3b77d5ec0   Luis R. Rodriguez   cfg80211: relicen...
19

b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
20
21
  /**
   * DOC: Wireless regulatory infrastructure
8318d78a4   Johannes Berg   cfg80211 API for ...
22
23
24
25
26
27
   *
   * The usual implementation is for a driver to read a device EEPROM to
   * determine which regulatory domain it should be operating under, then
   * looking up the allowable channels in a driver-local table and finally
   * registering those channels in the wiphy structure.
   *
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
   * Another set of compliance enforcement is for drivers to use their
   * own compliance limits which can be stored on the EEPROM. The host
   * driver or firmware may ensure these are used.
   *
   * In addition to all this we provide an extra layer of regulatory
   * conformance. For drivers which do not have any regulatory
   * information CRDA provides the complete regulatory solution.
   * For others it provides a community effort on further restrictions
   * to enhance compliance.
   *
   * Note: When number of rules --> infinity we will not be able to
   * index on alpha2 any more, instead we'll probably have to
   * rely on some SHA1 checksum of the regdomain for example.
   *
8318d78a4   Johannes Berg   cfg80211 API for ...
42
   */
e9c0268f0   Joe Perches   net/wireless: Use...
43
44
  
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8318d78a4   Johannes Berg   cfg80211 API for ...
45
  #include <linux/kernel.h>
bc3b2d7fb   Paul Gortmaker   net: Add export.h...
46
  #include <linux/export.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
47
  #include <linux/slab.h>
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
48
  #include <linux/list.h>
c61029c77   John W. Linville   wireless: upcase ...
49
  #include <linux/ctype.h>
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
50
51
  #include <linux/nl80211.h>
  #include <linux/platform_device.h>
d9b938421   Paul Gortmaker   net: add modulepa...
52
  #include <linux/moduleparam.h>
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
53
  #include <net/cfg80211.h>
8318d78a4   Johannes Berg   cfg80211 API for ...
54
  #include "core.h"
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
55
  #include "reg.h"
3b377ea9d   John W. Linville   wireless: support...
56
  #include "regdb.h"
73d54c9e7   Luis R. Rodriguez   cfg80211: add reg...
57
  #include "nl80211.h"
8318d78a4   Johannes Berg   cfg80211 API for ...
58

4113f7518   Luis R. Rodriguez   cfg80211: add a r...
59
  #ifdef CONFIG_CFG80211_REG_DEBUG
12c5ffb5c   Joe Perches   cfg80211: Update ...
60
61
  #define REG_DBG_PRINT(format, args...)			\
  	printk(KERN_DEBUG pr_fmt(format), ##args)
4113f7518   Luis R. Rodriguez   cfg80211: add a r...
62
  #else
8271195e3   John W. Linville   wireless: fix bui...
63
  #define REG_DBG_PRINT(args...)
4113f7518   Luis R. Rodriguez   cfg80211: add a r...
64
  #endif
52616f2b4   Ilan Peer   cfg80211: Add an ...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  /**
   * enum reg_request_treatment - regulatory request treatment
   *
   * @REG_REQ_OK: continue processing the regulatory request
   * @REG_REQ_IGNORE: ignore the regulatory request
   * @REG_REQ_INTERSECT: the regulatory domain resulting from this request should
   *	be intersected with the current one.
   * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
   *	regulatory settings, and no further processing is required.
   * @REG_REQ_USER_HINT_HANDLED: a non alpha2  user hint was handled and no
   *	further processing is required, i.e., not need to update last_request
   *	etc. This should be used for user hints that do not provide an alpha2
   *	but some other type of regulatory hint, i.e., indoor operation.
   */
2f92212b7   Johannes Berg   regulatory: use p...
79
80
81
82
83
  enum reg_request_treatment {
  	REG_REQ_OK,
  	REG_REQ_IGNORE,
  	REG_REQ_INTERSECT,
  	REG_REQ_ALREADY_SET,
52616f2b4   Ilan Peer   cfg80211: Add an ...
84
  	REG_REQ_USER_HINT_HANDLED,
2f92212b7   Johannes Berg   regulatory: use p...
85
  };
a042994dd   Luis R. Rodriguez   cfg80211: fix rac...
86
87
88
89
90
91
92
93
  static struct regulatory_request core_request_world = {
  	.initiator = NL80211_REGDOM_SET_BY_CORE,
  	.alpha2[0] = '0',
  	.alpha2[1] = '0',
  	.intersect = false,
  	.processed = true,
  	.country_ie_env = ENVIRON_ANY,
  };
38fd2143f   Johannes Berg   regulatory: remov...
94
95
96
97
  /*
   * Receipt of information from last regulatory request,
   * protected by RTNL (and can be accessed with RCU protection)
   */
c492db370   Johannes Berg   regulatory: use R...
98
99
  static struct regulatory_request __rcu *last_request =
  	(void __rcu *)&core_request_world;
734366dea   Johannes Berg   cfg80211: clean u...
100

b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
101
102
  /* To trigger userspace events */
  static struct platform_device *reg_pdev;
8318d78a4   Johannes Berg   cfg80211 API for ...
103

fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
104
105
  /*
   * Central wireless core regulatory domains, we only need two,
734366dea   Johannes Berg   cfg80211: clean u...
106
   * the current one and a world regulatory domain in case we have no
e8da2bb4f   Johannes Berg   regulatory: clari...
107
   * information to give us an alpha2.
38fd2143f   Johannes Berg   regulatory: remov...
108
   * (protected by RTNL, can be read under RCU)
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
109
   */
458f4f9e9   Johannes Berg   regulatory: use R...
110
  const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
734366dea   Johannes Berg   cfg80211: clean u...
111

fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
112
  /*
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
113
114
   * Number of devices that registered to the core
   * that support cellular base station regulatory hints
38fd2143f   Johannes Berg   regulatory: remov...
115
   * (protected by RTNL)
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
116
117
   */
  static int reg_num_devs_support_basehint;
52616f2b4   Ilan Peer   cfg80211: Add an ...
118
119
120
121
122
123
124
  /*
   * State variable indicating if the platform on which the devices
   * are attached is operating in an indoor environment. The state variable
   * is relevant for all registered devices.
   * (protected by RTNL)
   */
  static bool reg_is_indoor;
458f4f9e9   Johannes Berg   regulatory: use R...
125
126
  static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
  {
38fd2143f   Johannes Berg   regulatory: remov...
127
  	return rtnl_dereference(cfg80211_regdomain);
458f4f9e9   Johannes Berg   regulatory: use R...
128
129
130
131
  }
  
  static const struct ieee80211_regdomain *get_wiphy_regdom(struct wiphy *wiphy)
  {
38fd2143f   Johannes Berg   regulatory: remov...
132
  	return rtnl_dereference(wiphy->regd);
458f4f9e9   Johannes Berg   regulatory: use R...
133
  }
3ef121b51   Luis R. Rodriguez   cfg80211: replace...
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  static const char *reg_dfs_region_str(enum nl80211_dfs_regions dfs_region)
  {
  	switch (dfs_region) {
  	case NL80211_DFS_UNSET:
  		return "unset";
  	case NL80211_DFS_FCC:
  		return "FCC";
  	case NL80211_DFS_ETSI:
  		return "ETSI";
  	case NL80211_DFS_JP:
  		return "JP";
  	}
  	return "Unknown";
  }
6c474799d   Luis R. Rodriguez   cfg80211: add reg...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy)
  {
  	const struct ieee80211_regdomain *regd = NULL;
  	const struct ieee80211_regdomain *wiphy_regd = NULL;
  
  	regd = get_cfg80211_regdom();
  	if (!wiphy)
  		goto out;
  
  	wiphy_regd = get_wiphy_regdom(wiphy);
  	if (!wiphy_regd)
  		goto out;
  
  	if (wiphy_regd->dfs_region == regd->dfs_region)
  		goto out;
  
  	REG_DBG_PRINT("%s: device specific dfs_region "
  		      "(%s) disagrees with cfg80211's "
  		      "central dfs_region (%s)
  ",
  		      dev_name(&wiphy->dev),
  		      reg_dfs_region_str(wiphy_regd->dfs_region),
  		      reg_dfs_region_str(regd->dfs_region));
  
  out:
  	return regd->dfs_region;
  }
458f4f9e9   Johannes Berg   regulatory: use R...
175
176
177
178
179
180
  static void rcu_free_regdom(const struct ieee80211_regdomain *r)
  {
  	if (!r)
  		return;
  	kfree_rcu((struct ieee80211_regdomain *)r, rcu_head);
  }
c492db370   Johannes Berg   regulatory: use R...
181
182
  static struct regulatory_request *get_last_request(void)
  {
38fd2143f   Johannes Berg   regulatory: remov...
183
  	return rcu_dereference_rtnl(last_request);
c492db370   Johannes Berg   regulatory: use R...
184
  }
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
185
  /* Used to queue up regulatory hints */
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
186
187
  static LIST_HEAD(reg_requests_list);
  static spinlock_t reg_requests_lock;
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
188
189
190
191
192
193
194
195
196
197
198
  /* Used to queue up beacon hints for review */
  static LIST_HEAD(reg_pending_beacons);
  static spinlock_t reg_pending_beacons_lock;
  
  /* Used to keep track of processed beacon hints */
  static LIST_HEAD(reg_beacon_list);
  
  struct reg_beacon {
  	struct list_head list;
  	struct ieee80211_channel chan;
  };
f333a7a2f   Luis R. Rodriguez   cfg80211: move re...
199
200
  static void reg_todo(struct work_struct *work);
  static DECLARE_WORK(reg_work, reg_todo);
a90c7a313   Luis R. Rodriguez   cfg80211: add a t...
201
202
  static void reg_timeout_work(struct work_struct *work);
  static DECLARE_DELAYED_WORK(reg_timeout, reg_timeout_work);
734366dea   Johannes Berg   cfg80211: clean u...
203
204
  /* We keep a static world regulatory domain in case of the absence of CRDA */
  static const struct ieee80211_regdomain world_regdom = {
90cdc6df7   Vladimir Kondratiev   wireless: regulat...
205
  	.n_reg_rules = 6,
734366dea   Johannes Berg   cfg80211: clean u...
206
207
  	.alpha2 =  "00",
  	.reg_rules = {
68798a626   Luis R. Rodriguez   cfg80211: enable ...
208
209
  		/* IEEE 802.11b/g, channels 1..11 */
  		REG_RULE(2412-10, 2462+10, 40, 6, 20, 0),
43c771a19   Johannes Berg   wireless: allow 4...
210
211
  		/* IEEE 802.11b/g, channels 12..13. */
  		REG_RULE(2467-10, 2472+10, 40, 6, 20,
8fe02e167   Luis R. Rodriguez   cfg80211: consoli...
212
  			NL80211_RRF_NO_IR),
611b6a82a   Luis R. Rodriguez   cfg80211: Enable ...
213
214
215
  		/* IEEE 802.11 channel 14 - Only JP enables
  		 * this and for 802.11b only */
  		REG_RULE(2484-10, 2484+10, 20, 6, 20,
8fe02e167   Luis R. Rodriguez   cfg80211: consoli...
216
  			NL80211_RRF_NO_IR |
611b6a82a   Luis R. Rodriguez   cfg80211: Enable ...
217
218
  			NL80211_RRF_NO_OFDM),
  		/* IEEE 802.11a, channel 36..48 */
131a19bc9   Johannes Berg   regulatory: enabl...
219
  		REG_RULE(5180-10, 5240+10, 160, 6, 20,
8fe02e167   Luis R. Rodriguez   cfg80211: consoli...
220
                          NL80211_RRF_NO_IR),
3fc71f775   Luis R. Rodriguez   cfg80211: enable ...
221

131a19bc9   Johannes Berg   regulatory: enabl...
222
223
  		/* IEEE 802.11a, channel 52..64 - DFS required */
  		REG_RULE(5260-10, 5320+10, 160, 6, 20,
8fe02e167   Luis R. Rodriguez   cfg80211: consoli...
224
  			NL80211_RRF_NO_IR |
131a19bc9   Johannes Berg   regulatory: enabl...
225
226
227
228
  			NL80211_RRF_DFS),
  
  		/* IEEE 802.11a, channel 100..144 - DFS required */
  		REG_RULE(5500-10, 5720+10, 160, 6, 20,
8fe02e167   Luis R. Rodriguez   cfg80211: consoli...
229
  			NL80211_RRF_NO_IR |
131a19bc9   Johannes Berg   regulatory: enabl...
230
  			NL80211_RRF_DFS),
3fc71f775   Luis R. Rodriguez   cfg80211: enable ...
231
232
  
  		/* IEEE 802.11a, channel 149..165 */
8ab9d85c6   Johannes Berg   regulatory: allow...
233
  		REG_RULE(5745-10, 5825+10, 80, 6, 20,
8fe02e167   Luis R. Rodriguez   cfg80211: consoli...
234
  			NL80211_RRF_NO_IR),
90cdc6df7   Vladimir Kondratiev   wireless: regulat...
235
236
237
  
  		/* IEEE 802.11ad (60gHz), channels 1..3 */
  		REG_RULE(56160+2160*1-1080, 56160+2160*3+1080, 2160, 0, 0, 0),
734366dea   Johannes Berg   cfg80211: clean u...
238
239
  	}
  };
38fd2143f   Johannes Berg   regulatory: remov...
240
  /* protected by RTNL */
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
241
242
  static const struct ieee80211_regdomain *cfg80211_world_regdom =
  	&world_regdom;
734366dea   Johannes Berg   cfg80211: clean u...
243

6ee7d3305   Luis R. Rodriguez   cfg80211: make re...
244
  static char *ieee80211_regdom = "00";
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
245
  static char user_alpha2[2];
6ee7d3305   Luis R. Rodriguez   cfg80211: make re...
246

734366dea   Johannes Berg   cfg80211: clean u...
247
248
  module_param(ieee80211_regdom, charp, 0444);
  MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
c888393b7   Arik Nemtsov   cfg80211: avoid f...
249
  static void reg_free_request(struct regulatory_request *request)
5ad6ef5e0   Luis R. Rodriguez   cfg80211: add hel...
250
  {
c888393b7   Arik Nemtsov   cfg80211: avoid f...
251
252
253
254
255
256
257
  	if (request != get_last_request())
  		kfree(request);
  }
  
  static void reg_free_last_request(void)
  {
  	struct regulatory_request *lr = get_last_request();
5ad6ef5e0   Luis R. Rodriguez   cfg80211: add hel...
258
259
260
  	if (lr != &core_request_world && lr)
  		kfree_rcu(lr, rcu_head);
  }
05f1a3ea2   Luis R. Rodriguez   cfg80211: add hel...
261
262
  static void reg_update_last_request(struct regulatory_request *request)
  {
255e25b0e   Luis R. Rodriguez   cfg80211: allow r...
263
264
265
266
267
  	struct regulatory_request *lr;
  
  	lr = get_last_request();
  	if (lr == request)
  		return;
c888393b7   Arik Nemtsov   cfg80211: avoid f...
268
  	reg_free_last_request();
05f1a3ea2   Luis R. Rodriguez   cfg80211: add hel...
269
270
  	rcu_assign_pointer(last_request, request);
  }
379b82f4c   Johannes Berg   regulatory: pass ...
271
272
  static void reset_regdomains(bool full_reset,
  			     const struct ieee80211_regdomain *new_regdom)
734366dea   Johannes Berg   cfg80211: clean u...
273
  {
458f4f9e9   Johannes Berg   regulatory: use R...
274
  	const struct ieee80211_regdomain *r;
38fd2143f   Johannes Berg   regulatory: remov...
275
  	ASSERT_RTNL();
e8da2bb4f   Johannes Berg   regulatory: clari...
276

458f4f9e9   Johannes Berg   regulatory: use R...
277
  	r = get_cfg80211_regdom();
942b25cf9   Johannes Berg   cfg80211: clean u...
278
  	/* avoid freeing static information or freeing something twice */
458f4f9e9   Johannes Berg   regulatory: use R...
279
280
  	if (r == cfg80211_world_regdom)
  		r = NULL;
942b25cf9   Johannes Berg   cfg80211: clean u...
281
282
  	if (cfg80211_world_regdom == &world_regdom)
  		cfg80211_world_regdom = NULL;
458f4f9e9   Johannes Berg   regulatory: use R...
283
284
  	if (r == &world_regdom)
  		r = NULL;
942b25cf9   Johannes Berg   cfg80211: clean u...
285

458f4f9e9   Johannes Berg   regulatory: use R...
286
287
  	rcu_free_regdom(r);
  	rcu_free_regdom(cfg80211_world_regdom);
734366dea   Johannes Berg   cfg80211: clean u...
288

a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
289
  	cfg80211_world_regdom = &world_regdom;
458f4f9e9   Johannes Berg   regulatory: use R...
290
  	rcu_assign_pointer(cfg80211_regdomain, new_regdom);
a042994dd   Luis R. Rodriguez   cfg80211: fix rac...
291
292
293
  
  	if (!full_reset)
  		return;
05f1a3ea2   Luis R. Rodriguez   cfg80211: add hel...
294
  	reg_update_last_request(&core_request_world);
734366dea   Johannes Berg   cfg80211: clean u...
295
  }
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
296
297
298
299
  /*
   * Dynamic world regulatory domain requested by the wireless
   * core upon initialization
   */
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
300
  static void update_world_regdomain(const struct ieee80211_regdomain *rd)
734366dea   Johannes Berg   cfg80211: clean u...
301
  {
c492db370   Johannes Berg   regulatory: use R...
302
  	struct regulatory_request *lr;
734366dea   Johannes Berg   cfg80211: clean u...
303

c492db370   Johannes Berg   regulatory: use R...
304
305
306
  	lr = get_last_request();
  
  	WARN_ON(!lr);
734366dea   Johannes Berg   cfg80211: clean u...
307

379b82f4c   Johannes Berg   regulatory: pass ...
308
  	reset_regdomains(false, rd);
734366dea   Johannes Berg   cfg80211: clean u...
309
310
  
  	cfg80211_world_regdom = rd;
734366dea   Johannes Berg   cfg80211: clean u...
311
  }
734366dea   Johannes Berg   cfg80211: clean u...
312

a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
313
  bool is_world_regdom(const char *alpha2)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
314
315
316
  {
  	if (!alpha2)
  		return false;
1a9193185   Johannes Berg   regulatory: code ...
317
  	return alpha2[0] == '0' && alpha2[1] == '0';
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
318
  }
8318d78a4   Johannes Berg   cfg80211 API for ...
319

a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
320
  static bool is_alpha2_set(const char *alpha2)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
321
322
323
  {
  	if (!alpha2)
  		return false;
1a9193185   Johannes Berg   regulatory: code ...
324
  	return alpha2[0] && alpha2[1];
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
325
  }
8318d78a4   Johannes Berg   cfg80211 API for ...
326

a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
327
  static bool is_unknown_alpha2(const char *alpha2)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
328
329
330
  {
  	if (!alpha2)
  		return false;
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
331
332
333
334
  	/*
  	 * Special case where regulatory domain was built by driver
  	 * but a specific alpha2 cannot be determined
  	 */
1a9193185   Johannes Berg   regulatory: code ...
335
  	return alpha2[0] == '9' && alpha2[1] == '9';
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
336
  }
8318d78a4   Johannes Berg   cfg80211 API for ...
337

3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
338
339
340
341
  static bool is_intersected_alpha2(const char *alpha2)
  {
  	if (!alpha2)
  		return false;
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
342
343
  	/*
  	 * Special case where regulatory domain is the
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
344
  	 * result of an intersection between two regulatory domain
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
345
346
  	 * structures
  	 */
1a9193185   Johannes Berg   regulatory: code ...
347
  	return alpha2[0] == '9' && alpha2[1] == '8';
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
348
  }
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
349
  static bool is_an_alpha2(const char *alpha2)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
350
351
352
  {
  	if (!alpha2)
  		return false;
1a9193185   Johannes Berg   regulatory: code ...
353
  	return isalpha(alpha2[0]) && isalpha(alpha2[1]);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
354
  }
8318d78a4   Johannes Berg   cfg80211 API for ...
355

a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
356
  static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
357
358
359
  {
  	if (!alpha2_x || !alpha2_y)
  		return false;
1a9193185   Johannes Berg   regulatory: code ...
360
  	return alpha2_x[0] == alpha2_y[0] && alpha2_x[1] == alpha2_y[1];
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
361
  }
69b1572bd   Luis R. Rodriguez   cfg80211: rename ...
362
  static bool regdom_changes(const char *alpha2)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
363
  {
458f4f9e9   Johannes Berg   regulatory: use R...
364
  	const struct ieee80211_regdomain *r = get_cfg80211_regdom();
761cf7ecf   Luis R. Rodriguez   cfg80211: add ass...
365

458f4f9e9   Johannes Berg   regulatory: use R...
366
  	if (!r)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
367
  		return true;
458f4f9e9   Johannes Berg   regulatory: use R...
368
  	return !alpha2_equal(r->alpha2, alpha2);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
369
  }
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
370
371
372
373
374
375
376
377
378
379
380
  /*
   * The NL80211_REGDOM_SET_BY_USER regdom alpha2 is cached, this lets
   * you know if a valid regulatory hint with NL80211_REGDOM_SET_BY_USER
   * has ever been issued.
   */
  static bool is_user_regdom_saved(void)
  {
  	if (user_alpha2[0] == '9' && user_alpha2[1] == '7')
  		return false;
  
  	/* This would indicate a mistake on the design */
1a9193185   Johannes Berg   regulatory: code ...
381
  	if (WARN(!is_world_regdom(user_alpha2) && !is_an_alpha2(user_alpha2),
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
382
383
  		 "Unexpected user alpha2: %c%c
  ",
1a9193185   Johannes Berg   regulatory: code ...
384
  		 user_alpha2[0], user_alpha2[1]))
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
385
386
387
388
  		return false;
  
  	return true;
  }
e9763c3c2   Johannes Berg   regulatory: clean...
389
390
  static const struct ieee80211_regdomain *
  reg_copy_regd(const struct ieee80211_regdomain *src_regd)
3b377ea9d   John W. Linville   wireless: support...
391
392
  {
  	struct ieee80211_regdomain *regd;
e9763c3c2   Johannes Berg   regulatory: clean...
393
  	int size_of_regd;
3b377ea9d   John W. Linville   wireless: support...
394
  	unsigned int i;
82f208563   Johannes Berg   regulatory: don't...
395
396
397
  	size_of_regd =
  		sizeof(struct ieee80211_regdomain) +
  		src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
3b377ea9d   John W. Linville   wireless: support...
398
399
400
  
  	regd = kzalloc(size_of_regd, GFP_KERNEL);
  	if (!regd)
e9763c3c2   Johannes Berg   regulatory: clean...
401
  		return ERR_PTR(-ENOMEM);
3b377ea9d   John W. Linville   wireless: support...
402
403
404
405
406
  
  	memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
  
  	for (i = 0; i < src_regd->n_reg_rules; i++)
  		memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
e9763c3c2   Johannes Berg   regulatory: clean...
407
  		       sizeof(struct ieee80211_reg_rule));
3b377ea9d   John W. Linville   wireless: support...
408

e9763c3c2   Johannes Berg   regulatory: clean...
409
  	return regd;
3b377ea9d   John W. Linville   wireless: support...
410
411
412
413
414
415
416
417
418
  }
  
  #ifdef CONFIG_CFG80211_INTERNAL_REGDB
  struct reg_regdb_search_request {
  	char alpha2[2];
  	struct list_head list;
  };
  
  static LIST_HEAD(reg_regdb_search_list);
368d06f5b   John W. Linville   wireless: convert...
419
  static DEFINE_MUTEX(reg_regdb_search_mutex);
3b377ea9d   John W. Linville   wireless: support...
420
421
422
423
  
  static void reg_regdb_search(struct work_struct *work)
  {
  	struct reg_regdb_search_request *request;
e9763c3c2   Johannes Berg   regulatory: clean...
424
425
  	const struct ieee80211_regdomain *curdom, *regdom = NULL;
  	int i;
a85d0d7f3   Luis R. Rodriguez   cfg80211: fix pos...
426

5fe231e87   Johannes Berg   cfg80211: vastly ...
427
  	rtnl_lock();
3b377ea9d   John W. Linville   wireless: support...
428

368d06f5b   John W. Linville   wireless: convert...
429
  	mutex_lock(&reg_regdb_search_mutex);
3b377ea9d   John W. Linville   wireless: support...
430
431
432
433
434
  	while (!list_empty(&reg_regdb_search_list)) {
  		request = list_first_entry(&reg_regdb_search_list,
  					   struct reg_regdb_search_request,
  					   list);
  		list_del(&request->list);
1a9193185   Johannes Berg   regulatory: code ...
435
  		for (i = 0; i < reg_regdb_size; i++) {
3b377ea9d   John W. Linville   wireless: support...
436
  			curdom = reg_regdb[i];
1a9193185   Johannes Berg   regulatory: code ...
437
  			if (alpha2_equal(request->alpha2, curdom->alpha2)) {
e9763c3c2   Johannes Berg   regulatory: clean...
438
  				regdom = reg_copy_regd(curdom);
3b377ea9d   John W. Linville   wireless: support...
439
440
441
442
443
444
  				break;
  			}
  		}
  
  		kfree(request);
  	}
368d06f5b   John W. Linville   wireless: convert...
445
  	mutex_unlock(&reg_regdb_search_mutex);
a85d0d7f3   Luis R. Rodriguez   cfg80211: fix pos...
446

e9763c3c2   Johannes Berg   regulatory: clean...
447
  	if (!IS_ERR_OR_NULL(regdom))
a85d0d7f3   Luis R. Rodriguez   cfg80211: fix pos...
448
  		set_regdom(regdom);
5fe231e87   Johannes Berg   cfg80211: vastly ...
449
  	rtnl_unlock();
3b377ea9d   John W. Linville   wireless: support...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  }
  
  static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
  
  static void reg_regdb_query(const char *alpha2)
  {
  	struct reg_regdb_search_request *request;
  
  	if (!alpha2)
  		return;
  
  	request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
  	if (!request)
  		return;
  
  	memcpy(request->alpha2, alpha2, 2);
368d06f5b   John W. Linville   wireless: convert...
466
  	mutex_lock(&reg_regdb_search_mutex);
3b377ea9d   John W. Linville   wireless: support...
467
  	list_add_tail(&request->list, &reg_regdb_search_list);
368d06f5b   John W. Linville   wireless: convert...
468
  	mutex_unlock(&reg_regdb_search_mutex);
3b377ea9d   John W. Linville   wireless: support...
469
470
471
  
  	schedule_work(&reg_regdb_work);
  }
80007efef   Luis R. Rodriguez   cfg80211: warn if...
472
473
474
475
476
477
478
  
  /* Feel free to add any other sanity checks here */
  static void reg_regdb_size_check(void)
  {
  	/* We should ideally BUILD_BUG_ON() but then random builds would fail */
  	WARN_ONCE(!reg_regdb_size, "db.txt is empty, you should update it...");
  }
3b377ea9d   John W. Linville   wireless: support...
479
  #else
80007efef   Luis R. Rodriguez   cfg80211: warn if...
480
  static inline void reg_regdb_size_check(void) {}
3b377ea9d   John W. Linville   wireless: support...
481
482
  static inline void reg_regdb_query(const char *alpha2) {}
  #endif /* CONFIG_CFG80211_INTERNAL_REGDB */
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
483
484
  /*
   * This lets us keep regulatory code which is updated on a regulatory
1226d2587   Johannes Berg   cfg80211: regulat...
485
   * basis in userspace.
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
486
   */
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
487
488
  static int call_crda(const char *alpha2)
  {
1226d2587   Johannes Berg   cfg80211: regulat...
489
490
491
492
493
  	char country[12];
  	char *env[] = { country, NULL };
  
  	snprintf(country, sizeof(country), "COUNTRY=%c%c",
  		 alpha2[0], alpha2[1]);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
494
  	if (!is_world_regdom((char *) alpha2))
e9c0268f0   Joe Perches   net/wireless: Use...
495
496
  		pr_info("Calling CRDA for country: %c%c
  ",
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
497
498
  			alpha2[0], alpha2[1]);
  	else
e9c0268f0   Joe Perches   net/wireless: Use...
499
500
  		pr_info("Calling CRDA to update world regulatory domain
  ");
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
501

3b377ea9d   John W. Linville   wireless: support...
502
503
  	/* query internal regulatory database (if it exists) */
  	reg_regdb_query(alpha2);
1226d2587   Johannes Berg   cfg80211: regulat...
504
  	return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
505
  }
fe6631ff0   Luis R. Rodriguez   cfg80211: add hel...
506
507
508
509
510
511
512
  static enum reg_request_treatment
  reg_call_crda(struct regulatory_request *request)
  {
  	if (call_crda(request->alpha2))
  		return REG_REQ_IGNORE;
  	return REG_REQ_OK;
  }
e438768ff   Luis R. Rodriguez   cfg80211: check r...
513
  bool reg_is_valid_request(const char *alpha2)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
514
  {
c492db370   Johannes Berg   regulatory: use R...
515
  	struct regulatory_request *lr = get_last_request();
61405e977   Luis R. Rodriguez   cfg80211: fix in ...
516

c492db370   Johannes Berg   regulatory: use R...
517
  	if (!lr || lr->processed)
f6037d09e   Johannes Berg   wireless: get rid...
518
  		return false;
c492db370   Johannes Berg   regulatory: use R...
519
  	return alpha2_equal(lr->alpha2, alpha2);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
520
  }
8318d78a4   Johannes Berg   cfg80211 API for ...
521

e3961af1e   Janusz Dziedzic   cfg80211: add hel...
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
  static const struct ieee80211_regdomain *reg_get_regdomain(struct wiphy *wiphy)
  {
  	struct regulatory_request *lr = get_last_request();
  
  	/*
  	 * Follow the driver's regulatory domain, if present, unless a country
  	 * IE has been processed or a user wants to help complaince further
  	 */
  	if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  	    lr->initiator != NL80211_REGDOM_SET_BY_USER &&
  	    wiphy->regd)
  		return get_wiphy_regdom(wiphy);
  
  	return get_cfg80211_regdom();
  }
975248208   Janusz Dziedzic   cfg80211: regulat...
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
  unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
  				   const struct ieee80211_reg_rule *rule)
  {
  	const struct ieee80211_freq_range *freq_range = &rule->freq_range;
  	const struct ieee80211_freq_range *freq_range_tmp;
  	const struct ieee80211_reg_rule *tmp;
  	u32 start_freq, end_freq, idx, no;
  
  	for (idx = 0; idx < rd->n_reg_rules; idx++)
  		if (rule == &rd->reg_rules[idx])
  			break;
  
  	if (idx == rd->n_reg_rules)
  		return 0;
  
  	/* get start_freq */
  	no = idx;
  
  	while (no) {
  		tmp = &rd->reg_rules[--no];
  		freq_range_tmp = &tmp->freq_range;
  
  		if (freq_range_tmp->end_freq_khz < freq_range->start_freq_khz)
  			break;
975248208   Janusz Dziedzic   cfg80211: regulat...
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  		freq_range = freq_range_tmp;
  	}
  
  	start_freq = freq_range->start_freq_khz;
  
  	/* get end_freq */
  	freq_range = &rule->freq_range;
  	no = idx;
  
  	while (no < rd->n_reg_rules - 1) {
  		tmp = &rd->reg_rules[++no];
  		freq_range_tmp = &tmp->freq_range;
  
  		if (freq_range_tmp->start_freq_khz > freq_range->end_freq_khz)
  			break;
975248208   Janusz Dziedzic   cfg80211: regulat...
576
577
578
579
580
581
582
  		freq_range = freq_range_tmp;
  	}
  
  	end_freq = freq_range->end_freq_khz;
  
  	return end_freq - start_freq;
  }
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
583
  /* Sanity check on a regulatory rule */
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
584
  static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
8318d78a4   Johannes Berg   cfg80211 API for ...
585
  {
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
586
  	const struct ieee80211_freq_range *freq_range = &rule->freq_range;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
587
  	u32 freq_diff;
91e990041   Luis R. Rodriguez   cfg80211: mark ne...
588
  	if (freq_range->start_freq_khz <= 0 || freq_range->end_freq_khz <= 0)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
589
590
591
592
593
594
  		return false;
  
  	if (freq_range->start_freq_khz > freq_range->end_freq_khz)
  		return false;
  
  	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
bd05f28e1   Roel Kluin   cfg80211: test be...
595
  	if (freq_range->end_freq_khz <= freq_range->start_freq_khz ||
1a9193185   Johannes Berg   regulatory: code ...
596
  	    freq_range->max_bandwidth_khz > freq_diff)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
597
598
599
600
  		return false;
  
  	return true;
  }
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
601
  static bool is_valid_rd(const struct ieee80211_regdomain *rd)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
602
  {
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
603
  	const struct ieee80211_reg_rule *reg_rule = NULL;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
604
  	unsigned int i;
8318d78a4   Johannes Berg   cfg80211 API for ...
605

b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
606
607
  	if (!rd->n_reg_rules)
  		return false;
8318d78a4   Johannes Berg   cfg80211 API for ...
608

88dc1c3f7   Luis R. Rodriguez   cfg80211: mark re...
609
610
  	if (WARN_ON(rd->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
  		return false;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
611
612
613
614
615
616
617
  	for (i = 0; i < rd->n_reg_rules; i++) {
  		reg_rule = &rd->reg_rules[i];
  		if (!is_valid_reg_rule(reg_rule))
  			return false;
  	}
  
  	return true;
8318d78a4   Johannes Berg   cfg80211 API for ...
618
  }
038659e7c   Luis R. Rodriguez   cfg80211: Process...
619
  static bool reg_does_bw_fit(const struct ieee80211_freq_range *freq_range,
fe7ef5e9b   Johannes Berg   regulatory: remov...
620
  			    u32 center_freq_khz, u32 bw_khz)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
621
  {
038659e7c   Luis R. Rodriguez   cfg80211: Process...
622
623
624
625
626
627
628
629
630
631
  	u32 start_freq_khz, end_freq_khz;
  
  	start_freq_khz = center_freq_khz - (bw_khz/2);
  	end_freq_khz = center_freq_khz + (bw_khz/2);
  
  	if (start_freq_khz >= freq_range->start_freq_khz &&
  	    end_freq_khz <= freq_range->end_freq_khz)
  		return true;
  
  	return false;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
632
  }
8318d78a4   Johannes Berg   cfg80211 API for ...
633

0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
634
635
636
637
638
639
640
  /**
   * freq_in_rule_band - tells us if a frequency is in a frequency band
   * @freq_range: frequency rule we want to query
   * @freq_khz: frequency we are inquiring about
   *
   * This lets us know if a specific frequency rule is or is not relevant to
   * a specific frequency's band. Bands are device specific and artificial
64629b9d4   Vladimir Kondratiev   cfg80211: Fix reg...
641
642
643
644
645
   * definitions (the "2.4 GHz band", the "5 GHz band" and the "60GHz band"),
   * however it is safe for now to assume that a frequency rule should not be
   * part of a frequency's band if the start freq or end freq are off by more
   * than 2 GHz for the 2.4 and 5 GHz bands, and by more than 10 GHz for the
   * 60 GHz band.
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
646
647
648
649
   * This resolution can be lowered and should be considered as we add
   * regulatory rule support for other "bands".
   **/
  static bool freq_in_rule_band(const struct ieee80211_freq_range *freq_range,
1a9193185   Johannes Berg   regulatory: code ...
650
  			      u32 freq_khz)
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
651
652
  {
  #define ONE_GHZ_IN_KHZ	1000000
64629b9d4   Vladimir Kondratiev   cfg80211: Fix reg...
653
654
655
656
657
658
659
660
  	/*
  	 * From 802.11ad: directional multi-gigabit (DMG):
  	 * Pertaining to operation in a frequency band containing a channel
  	 * with the Channel starting frequency above 45 GHz.
  	 */
  	u32 limit = freq_khz > 45 * ONE_GHZ_IN_KHZ ?
  			10 * ONE_GHZ_IN_KHZ : 2 * ONE_GHZ_IN_KHZ;
  	if (abs(freq_khz - freq_range->start_freq_khz) <= limit)
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
661
  		return true;
64629b9d4   Vladimir Kondratiev   cfg80211: Fix reg...
662
  	if (abs(freq_khz - freq_range->end_freq_khz) <= limit)
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
663
664
665
666
  		return true;
  	return false;
  #undef ONE_GHZ_IN_KHZ
  }
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
667
  /*
adbfb0581   Luis R. Rodriguez   cfg80211: interse...
668
669
670
671
672
673
674
675
676
677
678
679
680
681
   * Later on we can perhaps use the more restrictive DFS
   * region but we don't have information for that yet so
   * for now simply disallow conflicts.
   */
  static enum nl80211_dfs_regions
  reg_intersect_dfs_region(const enum nl80211_dfs_regions dfs_region1,
  			 const enum nl80211_dfs_regions dfs_region2)
  {
  	if (dfs_region1 != dfs_region2)
  		return NL80211_DFS_UNSET;
  	return dfs_region1;
  }
  
  /*
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
682
683
684
   * Helper for regdom_intersect(), this does the real
   * mathematical intersection fun
   */
975248208   Janusz Dziedzic   cfg80211: regulat...
685
686
687
  static int reg_rules_intersect(const struct ieee80211_regdomain *rd1,
  			       const struct ieee80211_regdomain *rd2,
  			       const struct ieee80211_reg_rule *rule1,
1a9193185   Johannes Berg   regulatory: code ...
688
689
  			       const struct ieee80211_reg_rule *rule2,
  			       struct ieee80211_reg_rule *intersected_rule)
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
690
691
692
693
694
  {
  	const struct ieee80211_freq_range *freq_range1, *freq_range2;
  	struct ieee80211_freq_range *freq_range;
  	const struct ieee80211_power_rule *power_rule1, *power_rule2;
  	struct ieee80211_power_rule *power_rule;
975248208   Janusz Dziedzic   cfg80211: regulat...
695
  	u32 freq_diff, max_bandwidth1, max_bandwidth2;
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
696
697
698
699
700
701
702
703
704
705
  
  	freq_range1 = &rule1->freq_range;
  	freq_range2 = &rule2->freq_range;
  	freq_range = &intersected_rule->freq_range;
  
  	power_rule1 = &rule1->power_rule;
  	power_rule2 = &rule2->power_rule;
  	power_rule = &intersected_rule->power_rule;
  
  	freq_range->start_freq_khz = max(freq_range1->start_freq_khz,
1a9193185   Johannes Berg   regulatory: code ...
706
  					 freq_range2->start_freq_khz);
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
707
  	freq_range->end_freq_khz = min(freq_range1->end_freq_khz,
1a9193185   Johannes Berg   regulatory: code ...
708
  				       freq_range2->end_freq_khz);
975248208   Janusz Dziedzic   cfg80211: regulat...
709
710
711
  
  	max_bandwidth1 = freq_range1->max_bandwidth_khz;
  	max_bandwidth2 = freq_range2->max_bandwidth_khz;
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
712
713
714
715
  	if (rule1->flags & NL80211_RRF_AUTO_BW)
  		max_bandwidth1 = reg_get_max_bandwidth(rd1, rule1);
  	if (rule2->flags & NL80211_RRF_AUTO_BW)
  		max_bandwidth2 = reg_get_max_bandwidth(rd2, rule2);
975248208   Janusz Dziedzic   cfg80211: regulat...
716
717
  
  	freq_range->max_bandwidth_khz = min(max_bandwidth1, max_bandwidth2);
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
718

b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
  	intersected_rule->flags = rule1->flags | rule2->flags;
  
  	/*
  	 * In case NL80211_RRF_AUTO_BW requested for both rules
  	 * set AUTO_BW in intersected rule also. Next we will
  	 * calculate BW correctly in handle_channel function.
  	 * In other case remove AUTO_BW flag while we calculate
  	 * maximum bandwidth correctly and auto calculation is
  	 * not required.
  	 */
  	if ((rule1->flags & NL80211_RRF_AUTO_BW) &&
  	    (rule2->flags & NL80211_RRF_AUTO_BW))
  		intersected_rule->flags |= NL80211_RRF_AUTO_BW;
  	else
  		intersected_rule->flags &= ~NL80211_RRF_AUTO_BW;
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
734
735
736
737
738
739
740
741
  	freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
  	if (freq_range->max_bandwidth_khz > freq_diff)
  		freq_range->max_bandwidth_khz = freq_diff;
  
  	power_rule->max_eirp = min(power_rule1->max_eirp,
  		power_rule2->max_eirp);
  	power_rule->max_antenna_gain = min(power_rule1->max_antenna_gain,
  		power_rule2->max_antenna_gain);
089027e57   Janusz Dziedzic   cfg80211: regulat...
742
743
  	intersected_rule->dfs_cac_ms = max(rule1->dfs_cac_ms,
  					   rule2->dfs_cac_ms);
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
  	if (!is_valid_reg_rule(intersected_rule))
  		return -EINVAL;
  
  	return 0;
  }
  
  /**
   * regdom_intersect - do the intersection between two regulatory domains
   * @rd1: first regulatory domain
   * @rd2: second regulatory domain
   *
   * Use this function to get the intersection between two regulatory domains.
   * Once completed we will mark the alpha2 for the rd as intersected, "98",
   * as no one single alpha2 can represent this regulatory domain.
   *
   * Returns a pointer to the regulatory domain structure which will hold the
   * resulting intersection of rules between rd1 and rd2. We will
   * kzalloc() this structure for you.
   */
1a9193185   Johannes Berg   regulatory: code ...
763
764
765
  static struct ieee80211_regdomain *
  regdom_intersect(const struct ieee80211_regdomain *rd1,
  		 const struct ieee80211_regdomain *rd2)
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
766
767
768
769
770
771
772
773
  {
  	int r, size_of_regd;
  	unsigned int x, y;
  	unsigned int num_rules = 0, rule_idx = 0;
  	const struct ieee80211_reg_rule *rule1, *rule2;
  	struct ieee80211_reg_rule *intersected_rule;
  	struct ieee80211_regdomain *rd;
  	/* This is just a dummy holder to help us count */
74f53cd8d   Johannes Berg   regulatory: clean...
774
  	struct ieee80211_reg_rule dummy_rule;
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
775
776
777
  
  	if (!rd1 || !rd2)
  		return NULL;
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
778
779
  	/*
  	 * First we get a count of the rules we'll need, then we actually
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
780
781
782
  	 * build them. This is to so we can malloc() and free() a
  	 * regdomain once. The reason we use reg_rules_intersect() here
  	 * is it will return -EINVAL if the rule computed makes no sense.
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
783
784
  	 * All rules that do check out OK are valid.
  	 */
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
785
786
787
788
789
  
  	for (x = 0; x < rd1->n_reg_rules; x++) {
  		rule1 = &rd1->reg_rules[x];
  		for (y = 0; y < rd2->n_reg_rules; y++) {
  			rule2 = &rd2->reg_rules[y];
975248208   Janusz Dziedzic   cfg80211: regulat...
790
791
  			if (!reg_rules_intersect(rd1, rd2, rule1, rule2,
  						 &dummy_rule))
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
792
  				num_rules++;
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
793
794
795
796
797
798
799
  		}
  	}
  
  	if (!num_rules)
  		return NULL;
  
  	size_of_regd = sizeof(struct ieee80211_regdomain) +
82f208563   Johannes Berg   regulatory: don't...
800
  		       num_rules * sizeof(struct ieee80211_reg_rule);
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
801
802
803
804
  
  	rd = kzalloc(size_of_regd, GFP_KERNEL);
  	if (!rd)
  		return NULL;
8a57fff0c   Johannes Berg   regulatory: don't...
805
  	for (x = 0; x < rd1->n_reg_rules && rule_idx < num_rules; x++) {
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
806
  		rule1 = &rd1->reg_rules[x];
8a57fff0c   Johannes Berg   regulatory: don't...
807
  		for (y = 0; y < rd2->n_reg_rules && rule_idx < num_rules; y++) {
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
808
  			rule2 = &rd2->reg_rules[y];
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
809
810
  			/*
  			 * This time around instead of using the stack lets
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
811
  			 * write to the target rule directly saving ourselves
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
812
813
  			 * a memcpy()
  			 */
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
814
  			intersected_rule = &rd->reg_rules[rule_idx];
975248208   Janusz Dziedzic   cfg80211: regulat...
815
816
  			r = reg_rules_intersect(rd1, rd2, rule1, rule2,
  						intersected_rule);
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
817
818
819
820
  			/*
  			 * No need to memset here the intersected rule here as
  			 * we're not using the stack anymore
  			 */
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
821
822
823
824
825
826
827
828
829
830
831
832
833
834
  			if (r)
  				continue;
  			rule_idx++;
  		}
  	}
  
  	if (rule_idx != num_rules) {
  		kfree(rd);
  		return NULL;
  	}
  
  	rd->n_reg_rules = num_rules;
  	rd->alpha2[0] = '9';
  	rd->alpha2[1] = '8';
adbfb0581   Luis R. Rodriguez   cfg80211: interse...
835
836
  	rd->dfs_region = reg_intersect_dfs_region(rd1->dfs_region,
  						  rd2->dfs_region);
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
837
838
839
  
  	return rd;
  }
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
840
841
842
843
  /*
   * XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
   * want to just have the channel structure use these
   */
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
844
845
846
  static u32 map_regdom_flags(u32 rd_flags)
  {
  	u32 channel_flags = 0;
8fe02e167   Luis R. Rodriguez   cfg80211: consoli...
847
848
  	if (rd_flags & NL80211_RRF_NO_IR_ALL)
  		channel_flags |= IEEE80211_CHAN_NO_IR;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
849
850
  	if (rd_flags & NL80211_RRF_DFS)
  		channel_flags |= IEEE80211_CHAN_RADAR;
03f6b0843   Seth Forshee   cfg80211: add cha...
851
852
  	if (rd_flags & NL80211_RRF_NO_OFDM)
  		channel_flags |= IEEE80211_CHAN_NO_OFDM;
570dbde13   David Spinadel   cfg80211: Add ind...
853
854
  	if (rd_flags & NL80211_RRF_NO_OUTDOOR)
  		channel_flags |= IEEE80211_CHAN_INDOOR_ONLY;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
855
856
  	return channel_flags;
  }
361c9c8b0   Johannes Berg   regulatory: use I...
857
858
859
  static const struct ieee80211_reg_rule *
  freq_reg_info_regd(struct wiphy *wiphy, u32 center_freq,
  		   const struct ieee80211_regdomain *regd)
8318d78a4   Johannes Berg   cfg80211 API for ...
860
861
  {
  	int i;
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
862
  	bool band_rule_found = false;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
863
  	bool bw_fits = false;
3e0c3ff36   Luis R. Rodriguez   cfg80211: allow m...
864
  	if (!regd)
361c9c8b0   Johannes Berg   regulatory: use I...
865
  		return ERR_PTR(-EINVAL);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
866

3e0c3ff36   Luis R. Rodriguez   cfg80211: allow m...
867
  	for (i = 0; i < regd->n_reg_rules; i++) {
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
868
869
  		const struct ieee80211_reg_rule *rr;
  		const struct ieee80211_freq_range *fr = NULL;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
870

3e0c3ff36   Luis R. Rodriguez   cfg80211: allow m...
871
  		rr = &regd->reg_rules[i];
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
872
  		fr = &rr->freq_range;
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
873

fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
874
875
  		/*
  		 * We only need to know if one frequency rule was
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
876
  		 * was in center_freq's band, that's enough, so lets
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
877
878
  		 * not overwrite it once found
  		 */
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
879
880
  		if (!band_rule_found)
  			band_rule_found = freq_in_rule_band(fr, center_freq);
8eca1fb69   Rostislav Lisovy   cfg80211: Use 5MH...
881
  		bw_fits = reg_does_bw_fit(fr, center_freq, MHZ_TO_KHZ(5));
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
882

361c9c8b0   Johannes Berg   regulatory: use I...
883
884
  		if (band_rule_found && bw_fits)
  			return rr;
8318d78a4   Johannes Berg   cfg80211 API for ...
885
  	}
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
886
  	if (!band_rule_found)
361c9c8b0   Johannes Berg   regulatory: use I...
887
  		return ERR_PTR(-ERANGE);
0c7dc45d2   Luis R. Rodriguez   cfg80211: Fix reg...
888

361c9c8b0   Johannes Berg   regulatory: use I...
889
  	return ERR_PTR(-EINVAL);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
890
  }
361c9c8b0   Johannes Berg   regulatory: use I...
891
892
  const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
  					       u32 center_freq)
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
893
  {
5d885b999   Johannes Berg   regulatory: simpl...
894
  	const struct ieee80211_regdomain *regd;
1a9193185   Johannes Berg   regulatory: code ...
895

e3961af1e   Janusz Dziedzic   cfg80211: add hel...
896
  	regd = reg_get_regdomain(wiphy);
5d885b999   Johannes Berg   regulatory: simpl...
897

361c9c8b0   Johannes Berg   regulatory: use I...
898
  	return freq_reg_info_regd(wiphy, center_freq, regd);
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
899
  }
4f366c5da   John W. Linville   wireless: only us...
900
  EXPORT_SYMBOL(freq_reg_info);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
901

034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
902
  const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
903
904
905
  {
  	switch (initiator) {
  	case NL80211_REGDOM_SET_BY_CORE:
034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
906
  		return "core";
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
907
  	case NL80211_REGDOM_SET_BY_USER:
034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
908
  		return "user";
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
909
  	case NL80211_REGDOM_SET_BY_DRIVER:
034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
910
  		return "driver";
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
911
  	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
912
  		return "country IE";
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
913
914
  	default:
  		WARN_ON(1);
034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
915
  		return "bug";
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
916
917
  	}
  }
034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
918
  EXPORT_SYMBOL(reg_initiator_name);
e702d3cf2   Luis R. Rodriguez   cfg80211: add deb...
919

034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
920
  #ifdef CONFIG_CFG80211_REG_DEBUG
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
921
922
  static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
  				    struct ieee80211_channel *chan,
e702d3cf2   Luis R. Rodriguez   cfg80211: add deb...
923
924
925
926
  				    const struct ieee80211_reg_rule *reg_rule)
  {
  	const struct ieee80211_power_rule *power_rule;
  	const struct ieee80211_freq_range *freq_range;
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
927
  	char max_antenna_gain[32], bw[32];
e702d3cf2   Luis R. Rodriguez   cfg80211: add deb...
928
929
930
931
932
  
  	power_rule = &reg_rule->power_rule;
  	freq_range = &reg_rule->freq_range;
  
  	if (!power_rule->max_antenna_gain)
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
933
  		snprintf(max_antenna_gain, sizeof(max_antenna_gain), "N/A");
e702d3cf2   Luis R. Rodriguez   cfg80211: add deb...
934
  	else
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
935
936
937
938
939
940
941
942
943
944
  		snprintf(max_antenna_gain, sizeof(max_antenna_gain), "%d",
  			 power_rule->max_antenna_gain);
  
  	if (reg_rule->flags & NL80211_RRF_AUTO_BW)
  		snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
  			 freq_range->max_bandwidth_khz,
  			 reg_get_max_bandwidth(regd, reg_rule));
  	else
  		snprintf(bw, sizeof(bw), "%d KHz",
  			 freq_range->max_bandwidth_khz);
e702d3cf2   Luis R. Rodriguez   cfg80211: add deb...
945

fe7ef5e9b   Johannes Berg   regulatory: remov...
946
947
948
  	REG_DBG_PRINT("Updating information on frequency %d MHz with regulatory rule:
  ",
  		      chan->center_freq);
e702d3cf2   Luis R. Rodriguez   cfg80211: add deb...
949

b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
950
951
  	REG_DBG_PRINT("%d KHz - %d KHz @ %s), (%s mBi, %d mBm)
  ",
1a9193185   Johannes Berg   regulatory: code ...
952
  		      freq_range->start_freq_khz, freq_range->end_freq_khz,
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
953
  		      bw, max_antenna_gain,
e702d3cf2   Luis R. Rodriguez   cfg80211: add deb...
954
955
956
  		      power_rule->max_eirp);
  }
  #else
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
957
958
  static void chan_reg_rule_print_dbg(const struct ieee80211_regdomain *regd,
  				    struct ieee80211_channel *chan,
e702d3cf2   Luis R. Rodriguez   cfg80211: add deb...
959
960
961
962
  				    const struct ieee80211_reg_rule *reg_rule)
  {
  	return;
  }
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
963
  #endif
8eca1fb69   Rostislav Lisovy   cfg80211: Use 5MH...
964
965
966
967
  /* Find an ieee80211_reg_rule such that a 5MHz channel with frequency
   * chan->center_freq fits there.
   * If there is no such reg_rule, disable the channel, otherwise set the
   * flags corresponding to the bandwidths allowed in the particular reg_rule
038659e7c   Luis R. Rodriguez   cfg80211: Process...
968
   */
7ca43d03b   Luis R. Rodriguez   cfg80211: pass th...
969
970
  static void handle_channel(struct wiphy *wiphy,
  			   enum nl80211_reg_initiator initiator,
fdc9d7b28   Johannes Berg   regulatory: remov...
971
  			   struct ieee80211_channel *chan)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
972
  {
038659e7c   Luis R. Rodriguez   cfg80211: Process...
973
  	u32 flags, bw_flags = 0;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
974
975
  	const struct ieee80211_reg_rule *reg_rule = NULL;
  	const struct ieee80211_power_rule *power_rule = NULL;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
976
  	const struct ieee80211_freq_range *freq_range = NULL;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
977
  	struct wiphy *request_wiphy = NULL;
c492db370   Johannes Berg   regulatory: use R...
978
  	struct regulatory_request *lr = get_last_request();
975248208   Janusz Dziedzic   cfg80211: regulat...
979
980
  	const struct ieee80211_regdomain *regd;
  	u32 max_bandwidth_khz;
a92a3ce72   Luis R. Rodriguez   cfg80211: make ha...
981

c492db370   Johannes Berg   regulatory: use R...
982
  	request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
a92a3ce72   Luis R. Rodriguez   cfg80211: make ha...
983
984
  
  	flags = chan->orig_flags;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
985

361c9c8b0   Johannes Berg   regulatory: use I...
986
987
  	reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
  	if (IS_ERR(reg_rule)) {
ca4ffe8f2   Luis R. Rodriguez   cfg80211: fix dis...
988
989
  		/*
  		 * We will disable all channels that do not match our
25985edce   Lucas De Marchi   Fix common misspe...
990
  		 * received regulatory rule unless the hint is coming
ca4ffe8f2   Luis R. Rodriguez   cfg80211: fix dis...
991
992
993
994
995
996
997
998
  		 * from a Country IE and the Country IE had no information
  		 * about a band. The IEEE 802.11 spec allows for an AP
  		 * to send only a subset of the regulatory rules allowed,
  		 * so an AP in the US that only supports 2.4 GHz may only send
  		 * a country IE with information for the 2.4 GHz band
  		 * while 5 GHz is still supported.
  		 */
  		if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
361c9c8b0   Johannes Berg   regulatory: use I...
999
  		    PTR_ERR(reg_rule) == -ERANGE)
ca4ffe8f2   Luis R. Rodriguez   cfg80211: fix dis...
1000
  			return;
cc493e4f5   Luis R. Rodriguez   cfg80211: enforce...
1001
1002
  		if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  		    request_wiphy && request_wiphy == wiphy &&
a2f73b6c5   Luis R. Rodriguez   cfg80211: move re...
1003
  		    request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
cc493e4f5   Luis R. Rodriguez   cfg80211: enforce...
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
  			REG_DBG_PRINT("Disabling freq %d MHz for good
  ",
  				      chan->center_freq);
  			chan->orig_flags |= IEEE80211_CHAN_DISABLED;
  			chan->flags = chan->orig_flags;
  		} else {
  			REG_DBG_PRINT("Disabling freq %d MHz
  ",
  				      chan->center_freq);
  			chan->flags |= IEEE80211_CHAN_DISABLED;
  		}
8318d78a4   Johannes Berg   cfg80211 API for ...
1015
  		return;
ca4ffe8f2   Luis R. Rodriguez   cfg80211: fix dis...
1016
  	}
8318d78a4   Johannes Berg   cfg80211 API for ...
1017

b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
1018
1019
  	regd = reg_get_regdomain(wiphy);
  	chan_reg_rule_print_dbg(regd, chan, reg_rule);
e702d3cf2   Luis R. Rodriguez   cfg80211: add deb...
1020

b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1021
  	power_rule = &reg_rule->power_rule;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1022
  	freq_range = &reg_rule->freq_range;
975248208   Janusz Dziedzic   cfg80211: regulat...
1023
1024
  	max_bandwidth_khz = freq_range->max_bandwidth_khz;
  	/* Check if auto calculation requested */
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
1025
  	if (reg_rule->flags & NL80211_RRF_AUTO_BW)
975248208   Janusz Dziedzic   cfg80211: regulat...
1026
  		max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
975248208   Janusz Dziedzic   cfg80211: regulat...
1027

8eca1fb69   Rostislav Lisovy   cfg80211: Use 5MH...
1028
1029
1030
1031
  	if (max_bandwidth_khz < MHZ_TO_KHZ(10))
  		bw_flags = IEEE80211_CHAN_NO_10MHZ;
  	if (max_bandwidth_khz < MHZ_TO_KHZ(20))
  		bw_flags |= IEEE80211_CHAN_NO_20MHZ;
975248208   Janusz Dziedzic   cfg80211: regulat...
1032
  	if (max_bandwidth_khz < MHZ_TO_KHZ(40))
8eca1fb69   Rostislav Lisovy   cfg80211: Use 5MH...
1033
  		bw_flags |= IEEE80211_CHAN_NO_HT40;
975248208   Janusz Dziedzic   cfg80211: regulat...
1034
  	if (max_bandwidth_khz < MHZ_TO_KHZ(80))
c7a6ee27a   Johannes Berg   cfg80211: allow d...
1035
  		bw_flags |= IEEE80211_CHAN_NO_80MHZ;
975248208   Janusz Dziedzic   cfg80211: regulat...
1036
  	if (max_bandwidth_khz < MHZ_TO_KHZ(160))
c7a6ee27a   Johannes Berg   cfg80211: allow d...
1037
  		bw_flags |= IEEE80211_CHAN_NO_160MHZ;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1038

c492db370   Johannes Berg   regulatory: use R...
1039
  	if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
806a9e396   Luis R. Rodriguez   cfg80211: make re...
1040
  	    request_wiphy && request_wiphy == wiphy &&
a2f73b6c5   Luis R. Rodriguez   cfg80211: move re...
1041
  	    request_wiphy->regulatory_flags & REGULATORY_STRICT_REG) {
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
1042
  		/*
25985edce   Lucas De Marchi   Fix common misspe...
1043
  		 * This guarantees the driver's requested regulatory domain
f976376de   Luis R. Rodriguez   cfg80211: Allow f...
1044
  		 * will always be used as a base for further regulatory
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
1045
1046
  		 * settings
  		 */
f976376de   Luis R. Rodriguez   cfg80211: Allow f...
1047
  		chan->flags = chan->orig_flags =
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1048
  			map_regdom_flags(reg_rule->flags) | bw_flags;
f976376de   Luis R. Rodriguez   cfg80211: Allow f...
1049
1050
  		chan->max_antenna_gain = chan->orig_mag =
  			(int) MBI_TO_DBI(power_rule->max_antenna_gain);
279f0f552   Felix Fietkau   cfg80211: fix ini...
1051
  		chan->max_reg_power = chan->max_power = chan->orig_mpwr =
f976376de   Luis R. Rodriguez   cfg80211: Allow f...
1052
  			(int) MBM_TO_DBM(power_rule->max_eirp);
4f267c119   Janusz Dziedzic   cfg80211: reg: se...
1053
1054
1055
1056
1057
1058
  
  		if (chan->flags & IEEE80211_CHAN_RADAR) {
  			chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
  			if (reg_rule->dfs_cac_ms)
  				chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
  		}
f976376de   Luis R. Rodriguez   cfg80211: Allow f...
1059
1060
  		return;
  	}
04f39047a   Simon Wunderlich   nl80211/cfg80211:...
1061
1062
  	chan->dfs_state = NL80211_DFS_USABLE;
  	chan->dfs_state_entered = jiffies;
aa3d7eef3   Rajkumar Manoharan   wireless: Reset b...
1063
  	chan->beacon_found = false;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1064
  	chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
1a9193185   Johannes Berg   regulatory: code ...
1065
1066
1067
  	chan->max_antenna_gain =
  		min_t(int, chan->orig_mag,
  		      MBI_TO_DBI(power_rule->max_antenna_gain));
eccc068e8   Hong Wu   wireless: Save or...
1068
  	chan->max_reg_power = (int) MBM_TO_DBM(power_rule->max_eirp);
089027e57   Janusz Dziedzic   cfg80211: regulat...
1069
1070
1071
1072
1073
1074
1075
  
  	if (chan->flags & IEEE80211_CHAN_RADAR) {
  		if (reg_rule->dfs_cac_ms)
  			chan->dfs_cac_ms = reg_rule->dfs_cac_ms;
  		else
  			chan->dfs_cac_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
  	}
5e31fc081   Stanislaw Gruszka   wireless: reg: re...
1076
1077
  	if (chan->orig_mpwr) {
  		/*
a09a85a01   Luis R. Rodriguez   cfg80211: add fla...
1078
1079
  		 * Devices that use REGULATORY_COUNTRY_IE_FOLLOW_POWER
  		 * will always follow the passed country IE power settings.
5e31fc081   Stanislaw Gruszka   wireless: reg: re...
1080
1081
  		 */
  		if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
a09a85a01   Luis R. Rodriguez   cfg80211: add fla...
1082
  		    wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_FOLLOW_POWER)
5e31fc081   Stanislaw Gruszka   wireless: reg: re...
1083
1084
1085
1086
1087
1088
  			chan->max_power = chan->max_reg_power;
  		else
  			chan->max_power = min(chan->orig_mpwr,
  					      chan->max_reg_power);
  	} else
  		chan->max_power = chan->max_reg_power;
8318d78a4   Johannes Berg   cfg80211 API for ...
1089
  }
7ca43d03b   Luis R. Rodriguez   cfg80211: pass th...
1090
  static void handle_band(struct wiphy *wiphy,
fdc9d7b28   Johannes Berg   regulatory: remov...
1091
1092
  			enum nl80211_reg_initiator initiator,
  			struct ieee80211_supported_band *sband)
8318d78a4   Johannes Berg   cfg80211 API for ...
1093
  {
a92a3ce72   Luis R. Rodriguez   cfg80211: make ha...
1094
  	unsigned int i;
a92a3ce72   Luis R. Rodriguez   cfg80211: make ha...
1095

fdc9d7b28   Johannes Berg   regulatory: remov...
1096
1097
  	if (!sband)
  		return;
8318d78a4   Johannes Berg   cfg80211 API for ...
1098
1099
  
  	for (i = 0; i < sband->n_channels; i++)
fdc9d7b28   Johannes Berg   regulatory: remov...
1100
  		handle_channel(wiphy, initiator, &sband->channels[i]);
8318d78a4   Johannes Berg   cfg80211 API for ...
1101
  }
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1102
1103
1104
1105
  static bool reg_request_cell_base(struct regulatory_request *request)
  {
  	if (request->initiator != NL80211_REGDOM_SET_BY_USER)
  		return false;
1a9193185   Johannes Berg   regulatory: code ...
1106
  	return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1107
  }
52616f2b4   Ilan Peer   cfg80211: Add an ...
1108
1109
1110
1111
1112
1113
  static bool reg_request_indoor(struct regulatory_request *request)
  {
  	if (request->initiator != NL80211_REGDOM_SET_BY_USER)
  		return false;
  	return request->user_reg_hint_type == NL80211_USER_REG_HINT_INDOOR;
  }
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1114
1115
  bool reg_last_request_cell_base(void)
  {
38fd2143f   Johannes Berg   regulatory: remov...
1116
  	return reg_request_cell_base(get_last_request());
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1117
  }
94fc661f6   Ilan Peer   cfg80211: Add Kco...
1118
  #ifdef CONFIG_CFG80211_REG_CELLULAR_HINTS
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1119
  /* Core specific check */
2f92212b7   Johannes Berg   regulatory: use p...
1120
1121
  static enum reg_request_treatment
  reg_ignore_cell_hint(struct regulatory_request *pending_request)
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1122
  {
c492db370   Johannes Berg   regulatory: use R...
1123
  	struct regulatory_request *lr = get_last_request();
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1124
  	if (!reg_num_devs_support_basehint)
2f92212b7   Johannes Berg   regulatory: use p...
1125
  		return REG_REQ_IGNORE;
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1126

c492db370   Johannes Berg   regulatory: use R...
1127
  	if (reg_request_cell_base(lr) &&
1a9193185   Johannes Berg   regulatory: code ...
1128
  	    !regdom_changes(pending_request->alpha2))
2f92212b7   Johannes Berg   regulatory: use p...
1129
  		return REG_REQ_ALREADY_SET;
1a9193185   Johannes Berg   regulatory: code ...
1130

2f92212b7   Johannes Berg   regulatory: use p...
1131
  	return REG_REQ_OK;
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1132
1133
1134
1135
1136
  }
  
  /* Device specific check */
  static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
  {
1a9193185   Johannes Berg   regulatory: code ...
1137
  	return !(wiphy->features & NL80211_FEATURE_CELL_BASE_REG_HINTS);
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1138
1139
1140
1141
  }
  #else
  static int reg_ignore_cell_hint(struct regulatory_request *pending_request)
  {
2f92212b7   Johannes Berg   regulatory: use p...
1142
  	return REG_REQ_IGNORE;
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1143
  }
1a9193185   Johannes Berg   regulatory: code ...
1144
1145
  
  static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1146
1147
1148
1149
  {
  	return true;
  }
  #endif
fa1fb9cb1   Luis R. Rodriguez   cfg80211: simplif...
1150
1151
  static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
  {
a2f73b6c5   Luis R. Rodriguez   cfg80211: move re...
1152
1153
  	if (wiphy->regulatory_flags & REGULATORY_STRICT_REG &&
  	    !(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
fa1fb9cb1   Luis R. Rodriguez   cfg80211: simplif...
1154
1155
1156
  		return true;
  	return false;
  }
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1157

7db90f4a2   Luis R. Rodriguez   cfg80211: move en...
1158
1159
  static bool ignore_reg_update(struct wiphy *wiphy,
  			      enum nl80211_reg_initiator initiator)
14b9815af   Luis R. Rodriguez   cfg80211: add sup...
1160
  {
c492db370   Johannes Berg   regulatory: use R...
1161
1162
1163
  	struct regulatory_request *lr = get_last_request();
  
  	if (!lr) {
034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
1164
1165
1166
  		REG_DBG_PRINT("Ignoring regulatory request set by %s "
  			      "since last_request is not set
  ",
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
1167
  			      reg_initiator_name(initiator));
14b9815af   Luis R. Rodriguez   cfg80211: add sup...
1168
  		return true;
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
1169
  	}
7db90f4a2   Luis R. Rodriguez   cfg80211: move en...
1170
  	if (initiator == NL80211_REGDOM_SET_BY_CORE &&
a2f73b6c5   Luis R. Rodriguez   cfg80211: move re...
1171
  	    wiphy->regulatory_flags & REGULATORY_CUSTOM_REG) {
034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
1172
1173
1174
1175
  		REG_DBG_PRINT("Ignoring regulatory request set by %s "
  			      "since the driver uses its own custom "
  			      "regulatory domain
  ",
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
1176
  			      reg_initiator_name(initiator));
14b9815af   Luis R. Rodriguez   cfg80211: add sup...
1177
  		return true;
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
1178
  	}
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
1179
1180
1181
1182
  	/*
  	 * wiphy->regd will be set once the device has its own
  	 * desired regulatory domain set
  	 */
fa1fb9cb1   Luis R. Rodriguez   cfg80211: simplif...
1183
  	if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
749b527b2   Luis R. Rodriguez   cfg80211: fix all...
1184
  	    initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
c492db370   Johannes Berg   regulatory: use R...
1185
  	    !is_world_regdom(lr->alpha2)) {
034c6d6e6   Luis R. Rodriguez   cfg80211: export ...
1186
1187
1188
1189
  		REG_DBG_PRINT("Ignoring regulatory request set by %s "
  			      "since the driver requires its own regulatory "
  			      "domain to be set first
  ",
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
1190
  			      reg_initiator_name(initiator));
14b9815af   Luis R. Rodriguez   cfg80211: add sup...
1191
  		return true;
926a0a094   Luis R. Rodriguez   cfg80211: add deb...
1192
  	}
c492db370   Johannes Berg   regulatory: use R...
1193
  	if (reg_request_cell_base(lr))
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1194
  		return reg_dev_ignore_cell_hint(wiphy);
14b9815af   Luis R. Rodriguez   cfg80211: add sup...
1195
1196
  	return false;
  }
3195e489a   Luis R. Rodriguez   cfg80211: move re...
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
  static bool reg_is_world_roaming(struct wiphy *wiphy)
  {
  	const struct ieee80211_regdomain *cr = get_cfg80211_regdom();
  	const struct ieee80211_regdomain *wr = get_wiphy_regdom(wiphy);
  	struct regulatory_request *lr = get_last_request();
  
  	if (is_world_regdom(cr->alpha2) || (wr && is_world_regdom(wr->alpha2)))
  		return true;
  
  	if (lr && lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
a2f73b6c5   Luis R. Rodriguez   cfg80211: move re...
1207
  	    wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
3195e489a   Luis R. Rodriguez   cfg80211: move re...
1208
1209
1210
1211
  		return true;
  
  	return false;
  }
1a9193185   Johannes Berg   regulatory: code ...
1212
  static void handle_reg_beacon(struct wiphy *wiphy, unsigned int chan_idx,
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1213
1214
  			      struct reg_beacon *reg_beacon)
  {
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1215
1216
  	struct ieee80211_supported_band *sband;
  	struct ieee80211_channel *chan;
6bad87666   Luis R. Rodriguez   cfg80211: send re...
1217
1218
  	bool channel_changed = false;
  	struct ieee80211_channel chan_before;
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1219

e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1220
1221
1222
1223
1224
  	sband = wiphy->bands[reg_beacon->chan.band];
  	chan = &sband->channels[chan_idx];
  
  	if (likely(chan->center_freq != reg_beacon->chan.center_freq))
  		return;
6bad87666   Luis R. Rodriguez   cfg80211: send re...
1225
1226
1227
1228
  	if (chan->beacon_found)
  		return;
  
  	chan->beacon_found = true;
0f500a5f6   Luis R. Rodriguez   cfg80211: move wo...
1229
1230
  	if (!reg_is_world_roaming(wiphy))
  		return;
a2f73b6c5   Luis R. Rodriguez   cfg80211: move re...
1231
  	if (wiphy->regulatory_flags & REGULATORY_DISABLE_BEACON_HINTS)
371842448   Luis R. Rodriguez   cfg80211: fix reg...
1232
  		return;
6bad87666   Luis R. Rodriguez   cfg80211: send re...
1233
1234
  	chan_before.center_freq = chan->center_freq;
  	chan_before.flags = chan->flags;
8fe02e167   Luis R. Rodriguez   cfg80211: consoli...
1235
1236
  	if (chan->flags & IEEE80211_CHAN_NO_IR) {
  		chan->flags &= ~IEEE80211_CHAN_NO_IR;
6bad87666   Luis R. Rodriguez   cfg80211: send re...
1237
  		channel_changed = true;
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1238
  	}
6bad87666   Luis R. Rodriguez   cfg80211: send re...
1239
1240
  	if (channel_changed)
  		nl80211_send_beacon_hint_event(wiphy, &chan_before, chan);
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
  }
  
  /*
   * Called when a scan on a wiphy finds a beacon on
   * new channel
   */
  static void wiphy_update_new_beacon(struct wiphy *wiphy,
  				    struct reg_beacon *reg_beacon)
  {
  	unsigned int i;
  	struct ieee80211_supported_band *sband;
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
  	if (!wiphy->bands[reg_beacon->chan.band])
  		return;
  
  	sband = wiphy->bands[reg_beacon->chan.band];
  
  	for (i = 0; i < sband->n_channels; i++)
  		handle_reg_beacon(wiphy, i, reg_beacon);
  }
  
  /*
   * Called upon reg changes or a new wiphy is added
   */
  static void wiphy_update_beacon_reg(struct wiphy *wiphy)
  {
  	unsigned int i;
  	struct ieee80211_supported_band *sband;
  	struct reg_beacon *reg_beacon;
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1269
1270
1271
1272
1273
1274
1275
1276
  	list_for_each_entry(reg_beacon, &reg_beacon_list, list) {
  		if (!wiphy->bands[reg_beacon->chan.band])
  			continue;
  		sband = wiphy->bands[reg_beacon->chan.band];
  		for (i = 0; i < sband->n_channels; i++)
  			handle_reg_beacon(wiphy, i, reg_beacon);
  	}
  }
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1277
1278
1279
  /* Reap the advantages of previously found beacons */
  static void reg_process_beacons(struct wiphy *wiphy)
  {
b1ed8ddd2   Luis R. Rodriguez   cfg80211: fix bug...
1280
1281
1282
1283
1284
1285
  	/*
  	 * Means we are just firing up cfg80211, so no beacons would
  	 * have been processed yet.
  	 */
  	if (!last_request)
  		return;
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1286
1287
  	wiphy_update_beacon_reg(wiphy);
  }
1a9193185   Johannes Berg   regulatory: code ...
1288
  static bool is_ht40_allowed(struct ieee80211_channel *chan)
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1289
1290
  {
  	if (!chan)
1a9193185   Johannes Berg   regulatory: code ...
1291
  		return false;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1292
  	if (chan->flags & IEEE80211_CHAN_DISABLED)
1a9193185   Johannes Berg   regulatory: code ...
1293
  		return false;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1294
  	/* This would happen when regulatory rules disallow HT40 completely */
55b183ad8   Felix Fietkau   wireless: fix reg...
1295
1296
1297
  	if ((chan->flags & IEEE80211_CHAN_NO_HT40) == IEEE80211_CHAN_NO_HT40)
  		return false;
  	return true;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1298
1299
1300
  }
  
  static void reg_process_ht_flags_channel(struct wiphy *wiphy,
fdc9d7b28   Johannes Berg   regulatory: remov...
1301
  					 struct ieee80211_channel *channel)
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1302
  {
fdc9d7b28   Johannes Berg   regulatory: remov...
1303
  	struct ieee80211_supported_band *sband = wiphy->bands[channel->band];
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1304
1305
  	struct ieee80211_channel *channel_before = NULL, *channel_after = NULL;
  	unsigned int i;
1a9193185   Johannes Berg   regulatory: code ...
1306
  	if (!is_ht40_allowed(channel)) {
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
  		channel->flags |= IEEE80211_CHAN_NO_HT40;
  		return;
  	}
  
  	/*
  	 * We need to ensure the extension channels exist to
  	 * be able to use HT40- or HT40+, this finds them (or not)
  	 */
  	for (i = 0; i < sband->n_channels; i++) {
  		struct ieee80211_channel *c = &sband->channels[i];
1a9193185   Johannes Berg   regulatory: code ...
1317

038659e7c   Luis R. Rodriguez   cfg80211: Process...
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
  		if (c->center_freq == (channel->center_freq - 20))
  			channel_before = c;
  		if (c->center_freq == (channel->center_freq + 20))
  			channel_after = c;
  	}
  
  	/*
  	 * Please note that this assumes target bandwidth is 20 MHz,
  	 * if that ever changes we also need to change the below logic
  	 * to include that as well.
  	 */
1a9193185   Johannes Berg   regulatory: code ...
1329
  	if (!is_ht40_allowed(channel_before))
689da1b3b   Luis R. Rodriguez   wireless: rename ...
1330
  		channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1331
  	else
689da1b3b   Luis R. Rodriguez   wireless: rename ...
1332
  		channel->flags &= ~IEEE80211_CHAN_NO_HT40MINUS;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1333

1a9193185   Johannes Berg   regulatory: code ...
1334
  	if (!is_ht40_allowed(channel_after))
689da1b3b   Luis R. Rodriguez   wireless: rename ...
1335
  		channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1336
  	else
689da1b3b   Luis R. Rodriguez   wireless: rename ...
1337
  		channel->flags &= ~IEEE80211_CHAN_NO_HT40PLUS;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1338
1339
1340
  }
  
  static void reg_process_ht_flags_band(struct wiphy *wiphy,
fdc9d7b28   Johannes Berg   regulatory: remov...
1341
  				      struct ieee80211_supported_band *sband)
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1342
1343
  {
  	unsigned int i;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1344

fdc9d7b28   Johannes Berg   regulatory: remov...
1345
1346
  	if (!sband)
  		return;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1347
1348
  
  	for (i = 0; i < sband->n_channels; i++)
fdc9d7b28   Johannes Berg   regulatory: remov...
1349
  		reg_process_ht_flags_channel(wiphy, &sband->channels[i]);
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1350
1351
1352
1353
1354
1355
1356
1357
  }
  
  static void reg_process_ht_flags(struct wiphy *wiphy)
  {
  	enum ieee80211_band band;
  
  	if (!wiphy)
  		return;
fdc9d7b28   Johannes Berg   regulatory: remov...
1358
1359
  	for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  		reg_process_ht_flags_band(wiphy, wiphy->bands[band]);
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1360
  }
0e3802dbf   Luis R. Rodriguez   cfg80211: move co...
1361
1362
1363
1364
1365
1366
  static void reg_call_notifier(struct wiphy *wiphy,
  			      struct regulatory_request *request)
  {
  	if (wiphy->reg_notifier)
  		wiphy->reg_notifier(wiphy, request);
  }
eac03e381   Sven Neumann   cfg80211: hold re...
1367
1368
  static void wiphy_update_regulatory(struct wiphy *wiphy,
  				    enum nl80211_reg_initiator initiator)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1369
1370
  {
  	enum ieee80211_band band;
c492db370   Johannes Berg   regulatory: use R...
1371
  	struct regulatory_request *lr = get_last_request();
eac03e381   Sven Neumann   cfg80211: hold re...
1372

0e3802dbf   Luis R. Rodriguez   cfg80211: move co...
1373
1374
1375
1376
1377
1378
1379
  	if (ignore_reg_update(wiphy, initiator)) {
  		/*
  		 * Regulatory updates set by CORE are ignored for custom
  		 * regulatory cards. Let us notify the changes to the driver,
  		 * as some drivers used this to restore its orig_* reg domain.
  		 */
  		if (initiator == NL80211_REGDOM_SET_BY_CORE &&
a2f73b6c5   Luis R. Rodriguez   cfg80211: move re...
1380
  		    wiphy->regulatory_flags & REGULATORY_CUSTOM_REG)
0e3802dbf   Luis R. Rodriguez   cfg80211: move co...
1381
  			reg_call_notifier(wiphy, lr);
a203c2aa4   Sven Neumann   cfg80211: really ...
1382
  		return;
0e3802dbf   Luis R. Rodriguez   cfg80211: move co...
1383
  	}
a203c2aa4   Sven Neumann   cfg80211: really ...
1384

c492db370   Johannes Berg   regulatory: use R...
1385
  	lr->dfs_region = get_cfg80211_regdom()->dfs_region;
b68e6b3b3   Luis R. Rodriguez   cfg80211: pass DF...
1386

fdc9d7b28   Johannes Berg   regulatory: remov...
1387
1388
  	for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  		handle_band(wiphy, initiator, wiphy->bands[band]);
a203c2aa4   Sven Neumann   cfg80211: really ...
1389

e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1390
  	reg_process_beacons(wiphy);
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1391
  	reg_process_ht_flags(wiphy);
0e3802dbf   Luis R. Rodriguez   cfg80211: move co...
1392
  	reg_call_notifier(wiphy, lr);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1393
  }
d7549cbb9   Sven Neumann   cfg80211: reorder...
1394
1395
1396
  static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
  {
  	struct cfg80211_registered_device *rdev;
4a38994f1   Rajkumar Manoharan   cfg80211: notify ...
1397
  	struct wiphy *wiphy;
d7549cbb9   Sven Neumann   cfg80211: reorder...
1398

5fe231e87   Johannes Berg   cfg80211: vastly ...
1399
  	ASSERT_RTNL();
458f4f9e9   Johannes Berg   regulatory: use R...
1400

4a38994f1   Rajkumar Manoharan   cfg80211: notify ...
1401
1402
1403
  	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  		wiphy = &rdev->wiphy;
  		wiphy_update_regulatory(wiphy, initiator);
4a38994f1   Rajkumar Manoharan   cfg80211: notify ...
1404
  	}
d7549cbb9   Sven Neumann   cfg80211: reorder...
1405
  }
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1406
  static void handle_channel_custom(struct wiphy *wiphy,
fdc9d7b28   Johannes Berg   regulatory: remov...
1407
  				  struct ieee80211_channel *chan,
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1408
1409
  				  const struct ieee80211_regdomain *regd)
  {
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1410
  	u32 bw_flags = 0;
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1411
1412
  	const struct ieee80211_reg_rule *reg_rule = NULL;
  	const struct ieee80211_power_rule *power_rule = NULL;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1413
  	const struct ieee80211_freq_range *freq_range = NULL;
975248208   Janusz Dziedzic   cfg80211: regulat...
1414
  	u32 max_bandwidth_khz;
ac46d48e0   Luis R. Rodriguez   cfg80211: fix rac...
1415

361c9c8b0   Johannes Berg   regulatory: use I...
1416
1417
  	reg_rule = freq_reg_info_regd(wiphy, MHZ_TO_KHZ(chan->center_freq),
  				      regd);
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1418

361c9c8b0   Johannes Berg   regulatory: use I...
1419
  	if (IS_ERR(reg_rule)) {
fe7ef5e9b   Johannes Berg   regulatory: remov...
1420
1421
1422
  		REG_DBG_PRINT("Disabling freq %d MHz as custom regd has no rule that fits it
  ",
  			      chan->center_freq);
cc493e4f5   Luis R. Rodriguez   cfg80211: enforce...
1423
1424
  		chan->orig_flags |= IEEE80211_CHAN_DISABLED;
  		chan->flags = chan->orig_flags;
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1425
1426
  		return;
  	}
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
1427
  	chan_reg_rule_print_dbg(regd, chan, reg_rule);
e702d3cf2   Luis R. Rodriguez   cfg80211: add deb...
1428

1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1429
  	power_rule = &reg_rule->power_rule;
038659e7c   Luis R. Rodriguez   cfg80211: Process...
1430
  	freq_range = &reg_rule->freq_range;
975248208   Janusz Dziedzic   cfg80211: regulat...
1431
1432
  	max_bandwidth_khz = freq_range->max_bandwidth_khz;
  	/* Check if auto calculation requested */
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
1433
  	if (reg_rule->flags & NL80211_RRF_AUTO_BW)
975248208   Janusz Dziedzic   cfg80211: regulat...
1434
  		max_bandwidth_khz = reg_get_max_bandwidth(regd, reg_rule);
8eca1fb69   Rostislav Lisovy   cfg80211: Use 5MH...
1435
1436
1437
1438
  	if (max_bandwidth_khz < MHZ_TO_KHZ(10))
  		bw_flags = IEEE80211_CHAN_NO_10MHZ;
  	if (max_bandwidth_khz < MHZ_TO_KHZ(20))
  		bw_flags |= IEEE80211_CHAN_NO_20MHZ;
975248208   Janusz Dziedzic   cfg80211: regulat...
1439
  	if (max_bandwidth_khz < MHZ_TO_KHZ(40))
8eca1fb69   Rostislav Lisovy   cfg80211: Use 5MH...
1440
  		bw_flags |= IEEE80211_CHAN_NO_HT40;
975248208   Janusz Dziedzic   cfg80211: regulat...
1441
  	if (max_bandwidth_khz < MHZ_TO_KHZ(80))
c7a6ee27a   Johannes Berg   cfg80211: allow d...
1442
  		bw_flags |= IEEE80211_CHAN_NO_80MHZ;
975248208   Janusz Dziedzic   cfg80211: regulat...
1443
  	if (max_bandwidth_khz < MHZ_TO_KHZ(160))
c7a6ee27a   Johannes Berg   cfg80211: allow d...
1444
  		bw_flags |= IEEE80211_CHAN_NO_160MHZ;
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1445

038659e7c   Luis R. Rodriguez   cfg80211: Process...
1446
  	chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1447
  	chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
279f0f552   Felix Fietkau   cfg80211: fix ini...
1448
1449
  	chan->max_reg_power = chan->max_power =
  		(int) MBM_TO_DBM(power_rule->max_eirp);
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1450
  }
fdc9d7b28   Johannes Berg   regulatory: remov...
1451
1452
  static void handle_band_custom(struct wiphy *wiphy,
  			       struct ieee80211_supported_band *sband,
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1453
1454
1455
  			       const struct ieee80211_regdomain *regd)
  {
  	unsigned int i;
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1456

fdc9d7b28   Johannes Berg   regulatory: remov...
1457
1458
  	if (!sband)
  		return;
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1459
1460
  
  	for (i = 0; i < sband->n_channels; i++)
fdc9d7b28   Johannes Berg   regulatory: remov...
1461
  		handle_channel_custom(wiphy, &sband->channels[i], regd);
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1462
1463
1464
1465
1466
1467
1468
  }
  
  /* Used by drivers prior to wiphy registration */
  void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
  				   const struct ieee80211_regdomain *regd)
  {
  	enum ieee80211_band band;
bbcf3f027   Luis R. Rodriguez   cfg80211: warn wh...
1469
  	unsigned int bands_set = 0;
ac46d48e0   Luis R. Rodriguez   cfg80211: fix rac...
1470

a2f73b6c5   Luis R. Rodriguez   cfg80211: move re...
1471
1472
1473
1474
  	WARN(!(wiphy->regulatory_flags & REGULATORY_CUSTOM_REG),
  	     "wiphy should have REGULATORY_CUSTOM_REG
  ");
  	wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
222ea5819   Luis R. Rodriguez   cfg80211: force W...
1475

1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1476
  	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
bbcf3f027   Luis R. Rodriguez   cfg80211: warn wh...
1477
1478
  		if (!wiphy->bands[band])
  			continue;
fdc9d7b28   Johannes Berg   regulatory: remov...
1479
  		handle_band_custom(wiphy, wiphy->bands[band], regd);
bbcf3f027   Luis R. Rodriguez   cfg80211: warn wh...
1480
  		bands_set++;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1481
  	}
bbcf3f027   Luis R. Rodriguez   cfg80211: warn wh...
1482
1483
1484
  
  	/*
  	 * no point in calling this if it won't have any effect
1a9193185   Johannes Berg   regulatory: code ...
1485
  	 * on your device's supported bands.
bbcf3f027   Luis R. Rodriguez   cfg80211: warn wh...
1486
1487
  	 */
  	WARN_ON(!bands_set);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1488
  }
1fa25e413   Luis R. Rodriguez   cfg80211: add wip...
1489
  EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
b2e253cf3   Luis R. Rodriguez   cfg80211: Fix reg...
1490
1491
1492
  static void reg_set_request_processed(void)
  {
  	bool need_more_processing = false;
c492db370   Johannes Berg   regulatory: use R...
1493
  	struct regulatory_request *lr = get_last_request();
b2e253cf3   Luis R. Rodriguez   cfg80211: Fix reg...
1494

c492db370   Johannes Berg   regulatory: use R...
1495
  	lr->processed = true;
b2e253cf3   Luis R. Rodriguez   cfg80211: Fix reg...
1496
1497
1498
1499
1500
  
  	spin_lock(&reg_requests_lock);
  	if (!list_empty(&reg_requests_list))
  		need_more_processing = true;
  	spin_unlock(&reg_requests_lock);
c492db370   Johannes Berg   regulatory: use R...
1501
  	if (lr->initiator == NL80211_REGDOM_SET_BY_USER)
fe20b39ec   Eliad Peller   cfg80211: fix pot...
1502
  		cancel_delayed_work(&reg_timeout);
a90c7a313   Luis R. Rodriguez   cfg80211: add a t...
1503

b2e253cf3   Luis R. Rodriguez   cfg80211: Fix reg...
1504
1505
1506
  	if (need_more_processing)
  		schedule_work(&reg_work);
  }
d1c96a9a2   Luis R. Rodriguez   cfg80211: make __...
1507
  /**
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
   * reg_process_hint_core - process core regulatory requests
   * @pending_request: a pending core regulatory request
   *
   * The wireless subsystem can use this function to process
   * a regulatory request issued by the regulatory core.
   *
   * Returns one of the different reg request treatment values.
   */
  static enum reg_request_treatment
  reg_process_hint_core(struct regulatory_request *core_request)
  {
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1519
1520
1521
  
  	core_request->intersect = false;
  	core_request->processed = false;
5ad6ef5e0   Luis R. Rodriguez   cfg80211: add hel...
1522

05f1a3ea2   Luis R. Rodriguez   cfg80211: add hel...
1523
  	reg_update_last_request(core_request);
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1524

fe6631ff0   Luis R. Rodriguez   cfg80211: add hel...
1525
  	return reg_call_crda(core_request);
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1526
  }
0d97a6191   Luis R. Rodriguez   cfg80211: process...
1527
1528
1529
1530
  static enum reg_request_treatment
  __reg_process_hint_user(struct regulatory_request *user_request)
  {
  	struct regulatory_request *lr = get_last_request();
52616f2b4   Ilan Peer   cfg80211: Add an ...
1531
1532
1533
1534
  	if (reg_request_indoor(user_request)) {
  		reg_is_indoor = true;
  		return REG_REQ_USER_HINT_HANDLED;
  	}
0d97a6191   Luis R. Rodriguez   cfg80211: process...
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
  	if (reg_request_cell_base(user_request))
  		return reg_ignore_cell_hint(user_request);
  
  	if (reg_request_cell_base(lr))
  		return REG_REQ_IGNORE;
  
  	if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
  		return REG_REQ_INTERSECT;
  	/*
  	 * If the user knows better the user should set the regdom
  	 * to their country before the IE is picked up
  	 */
  	if (lr->initiator == NL80211_REGDOM_SET_BY_USER &&
  	    lr->intersect)
  		return REG_REQ_IGNORE;
  	/*
  	 * Process user requests only after previous user/driver/core
  	 * requests have been processed
  	 */
  	if ((lr->initiator == NL80211_REGDOM_SET_BY_CORE ||
  	     lr->initiator == NL80211_REGDOM_SET_BY_DRIVER ||
  	     lr->initiator == NL80211_REGDOM_SET_BY_USER) &&
  	    regdom_changes(lr->alpha2))
  		return REG_REQ_IGNORE;
  
  	if (!regdom_changes(user_request->alpha2))
  		return REG_REQ_ALREADY_SET;
  
  	return REG_REQ_OK;
  }
  
  /**
   * reg_process_hint_user - process user regulatory requests
   * @user_request: a pending user regulatory request
   *
   * The wireless subsystem can use this function to process
   * a regulatory request initiated by userspace.
   *
   * Returns one of the different reg request treatment values.
   */
  static enum reg_request_treatment
  reg_process_hint_user(struct regulatory_request *user_request)
  {
  	enum reg_request_treatment treatment;
0d97a6191   Luis R. Rodriguez   cfg80211: process...
1579
1580
1581
  
  	treatment = __reg_process_hint_user(user_request);
  	if (treatment == REG_REQ_IGNORE ||
52616f2b4   Ilan Peer   cfg80211: Add an ...
1582
1583
  	    treatment == REG_REQ_ALREADY_SET ||
  	    treatment == REG_REQ_USER_HINT_HANDLED) {
c888393b7   Arik Nemtsov   cfg80211: avoid f...
1584
  		reg_free_request(user_request);
0d97a6191   Luis R. Rodriguez   cfg80211: process...
1585
1586
  		return treatment;
  	}
0d97a6191   Luis R. Rodriguez   cfg80211: process...
1587
1588
  	user_request->intersect = treatment == REG_REQ_INTERSECT;
  	user_request->processed = false;
5ad6ef5e0   Luis R. Rodriguez   cfg80211: add hel...
1589

05f1a3ea2   Luis R. Rodriguez   cfg80211: add hel...
1590
  	reg_update_last_request(user_request);
0d97a6191   Luis R. Rodriguez   cfg80211: process...
1591
1592
1593
  
  	user_alpha2[0] = user_request->alpha2[0];
  	user_alpha2[1] = user_request->alpha2[1];
fe6631ff0   Luis R. Rodriguez   cfg80211: add hel...
1594
  	return reg_call_crda(user_request);
0d97a6191   Luis R. Rodriguez   cfg80211: process...
1595
  }
21636c7fa   Luis R. Rodriguez   cfg80211: process...
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
  static enum reg_request_treatment
  __reg_process_hint_driver(struct regulatory_request *driver_request)
  {
  	struct regulatory_request *lr = get_last_request();
  
  	if (lr->initiator == NL80211_REGDOM_SET_BY_CORE) {
  		if (regdom_changes(driver_request->alpha2))
  			return REG_REQ_OK;
  		return REG_REQ_ALREADY_SET;
  	}
  
  	/*
  	 * This would happen if you unplug and plug your card
  	 * back in or if you add a new device for which the previously
  	 * loaded card also agrees on the regulatory domain.
  	 */
  	if (lr->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
  	    !regdom_changes(driver_request->alpha2))
  		return REG_REQ_ALREADY_SET;
  
  	return REG_REQ_INTERSECT;
  }
  
  /**
   * reg_process_hint_driver - process driver regulatory requests
   * @driver_request: a pending driver regulatory request
   *
   * The wireless subsystem can use this function to process
   * a regulatory request issued by an 802.11 driver.
   *
   * Returns one of the different reg request treatment values.
   */
  static enum reg_request_treatment
  reg_process_hint_driver(struct wiphy *wiphy,
  			struct regulatory_request *driver_request)
  {
  	const struct ieee80211_regdomain *regd;
  	enum reg_request_treatment treatment;
21636c7fa   Luis R. Rodriguez   cfg80211: process...
1634
1635
1636
1637
1638
1639
1640
  
  	treatment = __reg_process_hint_driver(driver_request);
  
  	switch (treatment) {
  	case REG_REQ_OK:
  		break;
  	case REG_REQ_IGNORE:
52616f2b4   Ilan Peer   cfg80211: Add an ...
1641
  	case REG_REQ_USER_HINT_HANDLED:
c888393b7   Arik Nemtsov   cfg80211: avoid f...
1642
  		reg_free_request(driver_request);
21636c7fa   Luis R. Rodriguez   cfg80211: process...
1643
1644
1645
1646
1647
1648
  		return treatment;
  	case REG_REQ_INTERSECT:
  		/* fall through */
  	case REG_REQ_ALREADY_SET:
  		regd = reg_copy_regd(get_cfg80211_regdom());
  		if (IS_ERR(regd)) {
c888393b7   Arik Nemtsov   cfg80211: avoid f...
1649
  			reg_free_request(driver_request);
21636c7fa   Luis R. Rodriguez   cfg80211: process...
1650
1651
1652
1653
  			return REG_REQ_IGNORE;
  		}
  		rcu_assign_pointer(wiphy->regd, regd);
  	}
21636c7fa   Luis R. Rodriguez   cfg80211: process...
1654
1655
1656
  
  	driver_request->intersect = treatment == REG_REQ_INTERSECT;
  	driver_request->processed = false;
5ad6ef5e0   Luis R. Rodriguez   cfg80211: add hel...
1657

05f1a3ea2   Luis R. Rodriguez   cfg80211: add hel...
1658
  	reg_update_last_request(driver_request);
21636c7fa   Luis R. Rodriguez   cfg80211: process...
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
  
  	/*
  	 * Since CRDA will not be called in this case as we already
  	 * have applied the requested regulatory domain before we just
  	 * inform userspace we have processed the request
  	 */
  	if (treatment == REG_REQ_ALREADY_SET) {
  		nl80211_send_reg_change_event(driver_request);
  		reg_set_request_processed();
  		return treatment;
  	}
fe6631ff0   Luis R. Rodriguez   cfg80211: add hel...
1670
  	return reg_call_crda(driver_request);
21636c7fa   Luis R. Rodriguez   cfg80211: process...
1671
  }
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
  static enum reg_request_treatment
  __reg_process_hint_country_ie(struct wiphy *wiphy,
  			      struct regulatory_request *country_ie_request)
  {
  	struct wiphy *last_wiphy = NULL;
  	struct regulatory_request *lr = get_last_request();
  
  	if (reg_request_cell_base(lr)) {
  		/* Trust a Cell base station over the AP's country IE */
  		if (regdom_changes(country_ie_request->alpha2))
  			return REG_REQ_IGNORE;
  		return REG_REQ_ALREADY_SET;
2a901468c   Luis R. Rodriguez   cfg80211: add an ...
1684
1685
1686
  	} else {
  		if (wiphy->regulatory_flags & REGULATORY_COUNTRY_IE_IGNORE)
  			return REG_REQ_IGNORE;
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1687
  	}
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1688
1689
  	if (unlikely(!is_an_alpha2(country_ie_request->alpha2)))
  		return -EINVAL;
2f1c6c572   Luis R. Rodriguez   cfg80211: process...
1690
1691
1692
1693
1694
1695
1696
  
  	if (lr->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE)
  		return REG_REQ_OK;
  
  	last_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
  
  	if (last_wiphy != wiphy) {
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1697
  		/*
2f1c6c572   Luis R. Rodriguez   cfg80211: process...
1698
1699
1700
1701
  		 * Two cards with two APs claiming different
  		 * Country IE alpha2s. We could
  		 * intersect them, but that seems unlikely
  		 * to be correct. Reject second one for now.
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1702
  		 */
2f1c6c572   Luis R. Rodriguez   cfg80211: process...
1703
1704
  		if (regdom_changes(country_ie_request->alpha2))
  			return REG_REQ_IGNORE;
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1705
1706
  		return REG_REQ_ALREADY_SET;
  	}
2f1c6c572   Luis R. Rodriguez   cfg80211: process...
1707
1708
1709
1710
1711
1712
1713
  	/*
  	 * Two consecutive Country IE hints on the same wiphy.
  	 * This should be picked up early by the driver/stack
  	 */
  	if (WARN_ON(regdom_changes(country_ie_request->alpha2)))
  		return REG_REQ_OK;
  	return REG_REQ_ALREADY_SET;
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1714
  }
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1715
  /**
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1716
1717
   * reg_process_hint_country_ie - process regulatory requests from country IEs
   * @country_ie_request: a regulatory request from a country IE
d1c96a9a2   Luis R. Rodriguez   cfg80211: make __...
1718
   *
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1719
1720
   * The wireless subsystem can use this function to process
   * a regulatory request issued by a country Information Element.
d1c96a9a2   Luis R. Rodriguez   cfg80211: make __...
1721
   *
2f92212b7   Johannes Berg   regulatory: use p...
1722
   * Returns one of the different reg request treatment values.
d1c96a9a2   Luis R. Rodriguez   cfg80211: make __...
1723
   */
2f92212b7   Johannes Berg   regulatory: use p...
1724
  static enum reg_request_treatment
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1725
1726
  reg_process_hint_country_ie(struct wiphy *wiphy,
  			    struct regulatory_request *country_ie_request)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1727
  {
2f92212b7   Johannes Berg   regulatory: use p...
1728
  	enum reg_request_treatment treatment;
761cf7ecf   Luis R. Rodriguez   cfg80211: add ass...
1729

b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1730
  	treatment = __reg_process_hint_country_ie(wiphy, country_ie_request);
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
1731

2f92212b7   Johannes Berg   regulatory: use p...
1732
  	switch (treatment) {
2f92212b7   Johannes Berg   regulatory: use p...
1733
1734
  	case REG_REQ_OK:
  		break;
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1735
  	case REG_REQ_IGNORE:
52616f2b4   Ilan Peer   cfg80211: Add an ...
1736
  	case REG_REQ_USER_HINT_HANDLED:
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1737
1738
  		/* fall through */
  	case REG_REQ_ALREADY_SET:
c888393b7   Arik Nemtsov   cfg80211: avoid f...
1739
  		reg_free_request(country_ie_request);
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1740
1741
  		return treatment;
  	case REG_REQ_INTERSECT:
c888393b7   Arik Nemtsov   cfg80211: avoid f...
1742
  		reg_free_request(country_ie_request);
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
1743
  		/*
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1744
1745
  		 * This doesn't happen yet, not sure we
  		 * ever want to support it for this case.
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
1746
  		 */
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1747
1748
  		WARN_ONCE(1, "Unexpected intersection for country IEs");
  		return REG_REQ_IGNORE;
3e0c3ff36   Luis R. Rodriguez   cfg80211: allow m...
1749
  	}
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1750

b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1751
1752
  	country_ie_request->intersect = false;
  	country_ie_request->processed = false;
5ad6ef5e0   Luis R. Rodriguez   cfg80211: add hel...
1753

05f1a3ea2   Luis R. Rodriguez   cfg80211: add hel...
1754
  	reg_update_last_request(country_ie_request);
3e0c3ff36   Luis R. Rodriguez   cfg80211: allow m...
1755

fe6631ff0   Luis R. Rodriguez   cfg80211: add hel...
1756
  	return reg_call_crda(country_ie_request);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1757
  }
30a548c72   Luis R. Rodriguez   cfg80211: fix com...
1758
  /* This processes *all* regulatory hints */
1daa37c7b   Luis R. Rodriguez   cfg80211: remove ...
1759
  static void reg_process_hint(struct regulatory_request *reg_request)
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1760
  {
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1761
  	struct wiphy *wiphy = NULL;
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1762
  	enum reg_request_treatment treatment;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1763

f41737669   Johannes Berg   cfg80211: remove ...
1764
  	if (reg_request->wiphy_idx != WIPHY_IDX_INVALID)
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1765
  		wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1766
1767
1768
1769
1770
  	switch (reg_request->initiator) {
  	case NL80211_REGDOM_SET_BY_CORE:
  		reg_process_hint_core(reg_request);
  		return;
  	case NL80211_REGDOM_SET_BY_USER:
0d97a6191   Luis R. Rodriguez   cfg80211: process...
1771
  		treatment = reg_process_hint_user(reg_request);
50c11eb99   Inbal Hacohen   cfg80211: bugfix ...
1772
  		if (treatment == REG_REQ_IGNORE ||
4d3df547e   Arik Nemtsov   cfg80211: don't s...
1773
1774
  		    treatment == REG_REQ_ALREADY_SET ||
  		    treatment == REG_REQ_USER_HINT_HANDLED)
0d97a6191   Luis R. Rodriguez   cfg80211: process...
1775
  			return;
845f3351b   Shaibal Dutta   net: wireless: mo...
1776
1777
  		queue_delayed_work(system_power_efficient_wq,
  				   &reg_timeout, msecs_to_jiffies(3142));
0d97a6191   Luis R. Rodriguez   cfg80211: process...
1778
  		return;
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1779
  	case NL80211_REGDOM_SET_BY_DRIVER:
772f03893   Ilan Peer   cfg80211: fix few...
1780
1781
  		if (!wiphy)
  			goto out_free;
21636c7fa   Luis R. Rodriguez   cfg80211: process...
1782
1783
  		treatment = reg_process_hint_driver(wiphy, reg_request);
  		break;
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1784
  	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
772f03893   Ilan Peer   cfg80211: fix few...
1785
1786
  		if (!wiphy)
  			goto out_free;
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1787
  		treatment = reg_process_hint_country_ie(wiphy, reg_request);
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1788
1789
1790
1791
  		break;
  	default:
  		WARN(1, "invalid initiator %d
  ", reg_request->initiator);
772f03893   Ilan Peer   cfg80211: fix few...
1792
  		goto out_free;
b3eb7f3f5   Luis R. Rodriguez   cfg80211: process...
1793
  	}
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1794
1795
  	/* This is required so that the orig_* parameters are saved */
  	if (treatment == REG_REQ_ALREADY_SET && wiphy &&
a2f73b6c5   Luis R. Rodriguez   cfg80211: move re...
1796
  	    wiphy->regulatory_flags & REGULATORY_STRICT_REG)
b23e7a9e6   Luis R. Rodriguez   cfg80211: process...
1797
  		wiphy_update_regulatory(wiphy, reg_request->initiator);
772f03893   Ilan Peer   cfg80211: fix few...
1798
1799
1800
1801
  
  	return;
  
  out_free:
c888393b7   Arik Nemtsov   cfg80211: avoid f...
1802
  	reg_free_request(reg_request);
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1803
  }
b2e253cf3   Luis R. Rodriguez   cfg80211: Fix reg...
1804
1805
1806
1807
1808
  /*
   * Processes regulatory hints, this is all the NL80211_REGDOM_SET_BY_*
   * Regulatory hints come on a first come first serve basis and we
   * must process each one atomically.
   */
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1809
  static void reg_process_pending_hints(void)
b0e2880b0   Luis R. Rodriguez   cfg80211: move mu...
1810
  {
c492db370   Johannes Berg   regulatory: use R...
1811
  	struct regulatory_request *reg_request, *lr;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1812

c492db370   Johannes Berg   regulatory: use R...
1813
  	lr = get_last_request();
b0e2880b0   Luis R. Rodriguez   cfg80211: move mu...
1814

b2e253cf3   Luis R. Rodriguez   cfg80211: Fix reg...
1815
  	/* When last_request->processed becomes true this will be rescheduled */
c492db370   Johannes Berg   regulatory: use R...
1816
  	if (lr && !lr->processed) {
96cce12ff   Luis R. Rodriguez   cfg80211: fix pro...
1817
  		reg_process_hint(lr);
5fe231e87   Johannes Berg   cfg80211: vastly ...
1818
  		return;
b2e253cf3   Luis R. Rodriguez   cfg80211: Fix reg...
1819
  	}
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1820
  	spin_lock(&reg_requests_lock);
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1821

b2e253cf3   Luis R. Rodriguez   cfg80211: Fix reg...
1822
  	if (list_empty(&reg_requests_list)) {
d951c1dde   Luis R. Rodriguez   cfg80211: do not ...
1823
  		spin_unlock(&reg_requests_lock);
5fe231e87   Johannes Berg   cfg80211: vastly ...
1824
  		return;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1825
  	}
b2e253cf3   Luis R. Rodriguez   cfg80211: Fix reg...
1826
1827
1828
1829
1830
  
  	reg_request = list_first_entry(&reg_requests_list,
  				       struct regulatory_request,
  				       list);
  	list_del_init(&reg_request->list);
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1831
  	spin_unlock(&reg_requests_lock);
b0e2880b0   Luis R. Rodriguez   cfg80211: move mu...
1832

1daa37c7b   Luis R. Rodriguez   cfg80211: remove ...
1833
  	reg_process_hint(reg_request);
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1834
  }
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1835
1836
1837
  /* Processes beacon hints -- this has nothing to do with country IEs */
  static void reg_process_pending_beacon_hints(void)
  {
79c97e97a   Johannes Berg   cfg80211: clean u...
1838
  	struct cfg80211_registered_device *rdev;
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1839
  	struct reg_beacon *pending_beacon, *tmp;
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1840
1841
  	/* This goes through the _pending_ beacon list */
  	spin_lock_bh(&reg_pending_beacons_lock);
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1842
1843
  	list_for_each_entry_safe(pending_beacon, tmp,
  				 &reg_pending_beacons, list) {
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1844
1845
1846
  		list_del_init(&pending_beacon->list);
  
  		/* Applies the beacon hint to current wiphys */
79c97e97a   Johannes Berg   cfg80211: clean u...
1847
1848
  		list_for_each_entry(rdev, &cfg80211_rdev_list, list)
  			wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1849
1850
1851
1852
1853
1854
  
  		/* Remembers the beacon hint for new wiphys or reg changes */
  		list_add_tail(&pending_beacon->list, &reg_beacon_list);
  	}
  
  	spin_unlock_bh(&reg_pending_beacons_lock);
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1855
  }
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1856
1857
  static void reg_todo(struct work_struct *work)
  {
5fe231e87   Johannes Berg   cfg80211: vastly ...
1858
  	rtnl_lock();
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1859
  	reg_process_pending_hints();
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
1860
  	reg_process_pending_beacon_hints();
5fe231e87   Johannes Berg   cfg80211: vastly ...
1861
  	rtnl_unlock();
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1862
  }
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1863
1864
  static void queue_regulatory_request(struct regulatory_request *request)
  {
d4f2c8819   Johannes Berg   regulatory: remov...
1865
1866
  	request->alpha2[0] = toupper(request->alpha2[0]);
  	request->alpha2[1] = toupper(request->alpha2[1]);
c61029c77   John W. Linville   wireless: upcase ...
1867

fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1868
1869
1870
1871
1872
1873
  	spin_lock(&reg_requests_lock);
  	list_add_tail(&request->list, &reg_requests_list);
  	spin_unlock(&reg_requests_lock);
  
  	schedule_work(&reg_work);
  }
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
1874
1875
1876
1877
  /*
   * Core regulatory hint -- happens during cfg80211_init()
   * and when we restore regulatory settings.
   */
ba25c1414   Luis R. Rodriguez   cfg80211: add reg...
1878
1879
1880
  static int regulatory_hint_core(const char *alpha2)
  {
  	struct regulatory_request *request;
1a9193185   Johannes Berg   regulatory: code ...
1881
  	request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
ba25c1414   Luis R. Rodriguez   cfg80211: add reg...
1882
1883
1884
1885
1886
  	if (!request)
  		return -ENOMEM;
  
  	request->alpha2[0] = alpha2[0];
  	request->alpha2[1] = alpha2[1];
7db90f4a2   Luis R. Rodriguez   cfg80211: move en...
1887
  	request->initiator = NL80211_REGDOM_SET_BY_CORE;
ba25c1414   Luis R. Rodriguez   cfg80211: add reg...
1888

31e99729a   Luis R. Rodriguez   cfg80211: put cor...
1889
  	queue_regulatory_request(request);
5078b2e32   Luis R. Rodriguez   cfg80211: fix rac...
1890

fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1891
  	return 0;
ba25c1414   Luis R. Rodriguez   cfg80211: add reg...
1892
  }
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1893
  /* User hints */
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1894
1895
  int regulatory_hint_user(const char *alpha2,
  			 enum nl80211_user_reg_hint_type user_reg_hint_type)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1896
  {
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1897
  	struct regulatory_request *request;
fdc9d7b28   Johannes Berg   regulatory: remov...
1898
1899
  	if (WARN_ON(!alpha2))
  		return -EINVAL;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1900

fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1901
1902
1903
  	request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  	if (!request)
  		return -ENOMEM;
f41737669   Johannes Berg   cfg80211: remove ...
1904
  	request->wiphy_idx = WIPHY_IDX_INVALID;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1905
1906
  	request->alpha2[0] = alpha2[0];
  	request->alpha2[1] = alpha2[1];
e12822e1d   Luis R. Rodriguez   cfg80211: fix syn...
1907
  	request->initiator = NL80211_REGDOM_SET_BY_USER;
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
1908
  	request->user_reg_hint_type = user_reg_hint_type;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1909
1910
1911
1912
1913
  
  	queue_regulatory_request(request);
  
  	return 0;
  }
52616f2b4   Ilan Peer   cfg80211: Add an ...
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
  int regulatory_hint_indoor_user(void)
  {
  	struct regulatory_request *request;
  
  	request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  	if (!request)
  		return -ENOMEM;
  
  	request->wiphy_idx = WIPHY_IDX_INVALID;
  	request->initiator = NL80211_REGDOM_SET_BY_USER;
  	request->user_reg_hint_type = NL80211_USER_REG_HINT_INDOOR;
  	queue_regulatory_request(request);
  
  	return 0;
  }
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1929
1930
1931
1932
  /* Driver hints */
  int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
  {
  	struct regulatory_request *request;
fdc9d7b28   Johannes Berg   regulatory: remov...
1933
1934
  	if (WARN_ON(!alpha2 || !wiphy))
  		return -EINVAL;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1935

4f7b91404   Luis R. Rodriguez   cfg80211: make re...
1936
  	wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1937
1938
1939
1940
1941
  	request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL);
  	if (!request)
  		return -ENOMEM;
  
  	request->wiphy_idx = get_wiphy_idx(wiphy);
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1942
1943
  	request->alpha2[0] = alpha2[0];
  	request->alpha2[1] = alpha2[1];
7db90f4a2   Luis R. Rodriguez   cfg80211: move en...
1944
  	request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1945
1946
1947
1948
  
  	queue_regulatory_request(request);
  
  	return 0;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
1949
1950
  }
  EXPORT_SYMBOL(regulatory_hint);
789fd0333   Luis R. Rodriguez   cfg80211: rename ...
1951
1952
  void regulatory_hint_country_ie(struct wiphy *wiphy, enum ieee80211_band band,
  				const u8 *country_ie, u8 country_ie_len)
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
1953
  {
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
1954
  	char alpha2[2];
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
1955
  	enum environment_cap env = ENVIRON_ANY;
db2424c58   Johannes Berg   regulatory: use R...
1956
  	struct regulatory_request *request = NULL, *lr;
d335fe639   Luis R. Rodriguez   cfg80211: protect...
1957

3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
1958
1959
  	/* IE len must be evenly divisible by 2 */
  	if (country_ie_len & 0x01)
db2424c58   Johannes Berg   regulatory: use R...
1960
  		return;
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
1961
1962
  
  	if (country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
db2424c58   Johannes Berg   regulatory: use R...
1963
1964
1965
1966
1967
  		return;
  
  	request = kzalloc(sizeof(*request), GFP_KERNEL);
  	if (!request)
  		return;
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
1968

3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
1969
1970
1971
1972
1973
1974
1975
  	alpha2[0] = country_ie[0];
  	alpha2[1] = country_ie[1];
  
  	if (country_ie[2] == 'I')
  		env = ENVIRON_INDOOR;
  	else if (country_ie[2] == 'O')
  		env = ENVIRON_OUTDOOR;
db2424c58   Johannes Berg   regulatory: use R...
1976
1977
1978
1979
1980
  	rcu_read_lock();
  	lr = get_last_request();
  
  	if (unlikely(!lr))
  		goto out;
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
1981
  	/*
8b19e6ca3   Luis R. Rodriguez   cfg80211: enable ...
1982
  	 * We will run this only upon a successful connection on cfg80211.
4b44c8bc4   Luis R. Rodriguez   cfg80211: do not ...
1983
  	 * We leave conflict resolution to the workqueue, where can hold
5fe231e87   Johannes Berg   cfg80211: vastly ...
1984
  	 * the RTNL.
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
1985
  	 */
c492db370   Johannes Berg   regulatory: use R...
1986
1987
  	if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  	    lr->wiphy_idx != WIPHY_IDX_INVALID)
4b44c8bc4   Luis R. Rodriguez   cfg80211: do not ...
1988
  		goto out;
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
1989

fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1990
  	request->wiphy_idx = get_wiphy_idx(wiphy);
4f366c5da   John W. Linville   wireless: only us...
1991
1992
  	request->alpha2[0] = alpha2[0];
  	request->alpha2[1] = alpha2[1];
7db90f4a2   Luis R. Rodriguez   cfg80211: move en...
1993
  	request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1994
  	request->country_ie_env = env;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
1995
  	queue_regulatory_request(request);
db2424c58   Johannes Berg   regulatory: use R...
1996
  	request = NULL;
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
1997
  out:
db2424c58   Johannes Berg   regulatory: use R...
1998
1999
  	kfree(request);
  	rcu_read_unlock();
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
2000
  }
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2001

09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
  static void restore_alpha2(char *alpha2, bool reset_user)
  {
  	/* indicates there is no alpha2 to consider for restoration */
  	alpha2[0] = '9';
  	alpha2[1] = '7';
  
  	/* The user setting has precedence over the module parameter */
  	if (is_user_regdom_saved()) {
  		/* Unless we're asked to ignore it and reset it */
  		if (reset_user) {
1a9193185   Johannes Berg   regulatory: code ...
2012
2013
  			REG_DBG_PRINT("Restoring regulatory settings including user preference
  ");
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2014
2015
2016
2017
2018
2019
2020
2021
2022
  			user_alpha2[0] = '9';
  			user_alpha2[1] = '7';
  
  			/*
  			 * If we're ignoring user settings, we still need to
  			 * check the module parameter to ensure we put things
  			 * back as they were for a full restore.
  			 */
  			if (!is_world_regdom(ieee80211_regdom)) {
1a9193185   Johannes Berg   regulatory: code ...
2023
2024
2025
  				REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c
  ",
  					      ieee80211_regdom[0], ieee80211_regdom[1]);
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2026
2027
2028
2029
  				alpha2[0] = ieee80211_regdom[0];
  				alpha2[1] = ieee80211_regdom[1];
  			}
  		} else {
1a9193185   Johannes Berg   regulatory: code ...
2030
2031
2032
  			REG_DBG_PRINT("Restoring regulatory settings while preserving user preference for: %c%c
  ",
  				      user_alpha2[0], user_alpha2[1]);
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2033
2034
2035
2036
  			alpha2[0] = user_alpha2[0];
  			alpha2[1] = user_alpha2[1];
  		}
  	} else if (!is_world_regdom(ieee80211_regdom)) {
1a9193185   Johannes Berg   regulatory: code ...
2037
2038
2039
  		REG_DBG_PRINT("Keeping preference on module parameter ieee80211_regdom: %c%c
  ",
  			      ieee80211_regdom[0], ieee80211_regdom[1]);
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2040
2041
2042
  		alpha2[0] = ieee80211_regdom[0];
  		alpha2[1] = ieee80211_regdom[1];
  	} else
d91e41b69   Luis R. Rodriguez   cfg80211: prefix ...
2043
2044
  		REG_DBG_PRINT("Restoring regulatory settings
  ");
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2045
  }
5ce543d14   Rajkumar Manoharan   cfg80211: Restore...
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
  static void restore_custom_reg_settings(struct wiphy *wiphy)
  {
  	struct ieee80211_supported_band *sband;
  	enum ieee80211_band band;
  	struct ieee80211_channel *chan;
  	int i;
  
  	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  		sband = wiphy->bands[band];
  		if (!sband)
  			continue;
  		for (i = 0; i < sband->n_channels; i++) {
  			chan = &sband->channels[i];
  			chan->flags = chan->orig_flags;
  			chan->max_antenna_gain = chan->orig_mag;
  			chan->max_power = chan->orig_mpwr;
899852af6   Paul Stewart   cfg80211: Clear "...
2062
  			chan->beacon_found = false;
5ce543d14   Rajkumar Manoharan   cfg80211: Restore...
2063
2064
2065
  		}
  	}
  }
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
  /*
   * Restoring regulatory settings involves ingoring any
   * possibly stale country IE information and user regulatory
   * settings if so desired, this includes any beacon hints
   * learned as we could have traveled outside to another country
   * after disconnection. To restore regulatory settings we do
   * exactly what we did at bootup:
   *
   *   - send a core regulatory hint
   *   - send a user regulatory hint if applicable
   *
   * Device drivers that send a regulatory hint for a specific country
   * keep their own regulatory domain on wiphy->regd so that does does
   * not need to be remembered.
   */
  static void restore_regulatory_settings(bool reset_user)
  {
  	char alpha2[2];
cee0bec58   Dmitry Shmidt   wireless: Protect...
2084
  	char world_alpha2[2];
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2085
  	struct reg_beacon *reg_beacon, *btmp;
146095557   Luis R. Rodriguez   cfg80211: fix reg...
2086
2087
  	struct regulatory_request *reg_request, *tmp;
  	LIST_HEAD(tmp_reg_req_list);
5ce543d14   Rajkumar Manoharan   cfg80211: Restore...
2088
  	struct cfg80211_registered_device *rdev;
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2089

5fe231e87   Johannes Berg   cfg80211: vastly ...
2090
  	ASSERT_RTNL();
52616f2b4   Ilan Peer   cfg80211: Add an ...
2091
  	reg_is_indoor = false;
2d3198676   Johannes Berg   regulatory: fix r...
2092
  	reset_regdomains(true, &world_regdom);
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2093
  	restore_alpha2(alpha2, reset_user);
146095557   Luis R. Rodriguez   cfg80211: fix reg...
2094
2095
2096
2097
2098
2099
2100
  	/*
  	 * If there's any pending requests we simply
  	 * stash them to a temporary pending queue and
  	 * add then after we've restored regulatory
  	 * settings.
  	 */
  	spin_lock(&reg_requests_lock);
fea9bcedc   Johannes Berg   regulatory: don't...
2101
2102
2103
2104
  	list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
  		if (reg_request->initiator != NL80211_REGDOM_SET_BY_USER)
  			continue;
  		list_move_tail(&reg_request->list, &tmp_reg_req_list);
146095557   Luis R. Rodriguez   cfg80211: fix reg...
2105
2106
  	}
  	spin_unlock(&reg_requests_lock);
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2107
2108
  	/* Clear beacon hints */
  	spin_lock_bh(&reg_pending_beacons_lock);
fea9bcedc   Johannes Berg   regulatory: don't...
2109
2110
2111
  	list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
  		list_del(&reg_beacon->list);
  		kfree(reg_beacon);
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2112
2113
  	}
  	spin_unlock_bh(&reg_pending_beacons_lock);
fea9bcedc   Johannes Berg   regulatory: don't...
2114
2115
2116
  	list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
  		list_del(&reg_beacon->list);
  		kfree(reg_beacon);
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2117
2118
2119
  	}
  
  	/* First restore to the basic regulatory settings */
379b82f4c   Johannes Berg   regulatory: pass ...
2120
2121
  	world_alpha2[0] = cfg80211_world_regdom->alpha2[0];
  	world_alpha2[1] = cfg80211_world_regdom->alpha2[1];
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2122

5ce543d14   Rajkumar Manoharan   cfg80211: Restore...
2123
  	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
a2f73b6c5   Luis R. Rodriguez   cfg80211: move re...
2124
  		if (rdev->wiphy.regulatory_flags & REGULATORY_CUSTOM_REG)
5ce543d14   Rajkumar Manoharan   cfg80211: Restore...
2125
2126
  			restore_custom_reg_settings(&rdev->wiphy);
  	}
cee0bec58   Dmitry Shmidt   wireless: Protect...
2127
  	regulatory_hint_core(world_alpha2);
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2128
2129
2130
2131
2132
2133
2134
  
  	/*
  	 * This restores the ieee80211_regdom module parameter
  	 * preference or the last user requested regulatory
  	 * settings, user regulatory settings takes precedence.
  	 */
  	if (is_an_alpha2(alpha2))
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
2135
  		regulatory_hint_user(user_alpha2, NL80211_USER_REG_HINT_USER);
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2136

146095557   Luis R. Rodriguez   cfg80211: fix reg...
2137
  	spin_lock(&reg_requests_lock);
11cff96c0   Johannes Berg   regulatory: simpl...
2138
  	list_splice_tail_init(&tmp_reg_req_list, &reg_requests_list);
146095557   Luis R. Rodriguez   cfg80211: fix reg...
2139
  	spin_unlock(&reg_requests_lock);
146095557   Luis R. Rodriguez   cfg80211: fix reg...
2140
2141
2142
2143
2144
  	REG_DBG_PRINT("Kicking the queue
  ");
  
  	schedule_work(&reg_work);
  }
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2145
2146
2147
  
  void regulatory_hint_disconnect(void)
  {
1a9193185   Johannes Berg   regulatory: code ...
2148
2149
  	REG_DBG_PRINT("All devices are disconnected, going to restore regulatory settings
  ");
09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2150
2151
  	restore_regulatory_settings(false);
  }
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2152
2153
  static bool freq_is_chan_12_13_14(u16 freq)
  {
59eb21a65   Bruno Randolf   cfg80211: Extend ...
2154
2155
2156
  	if (freq == ieee80211_channel_to_frequency(12, IEEE80211_BAND_2GHZ) ||
  	    freq == ieee80211_channel_to_frequency(13, IEEE80211_BAND_2GHZ) ||
  	    freq == ieee80211_channel_to_frequency(14, IEEE80211_BAND_2GHZ))
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2157
2158
2159
  		return true;
  	return false;
  }
3ebfa6e76   Luis R. Rodriguez   cfg80211: do not ...
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
  static bool pending_reg_beacon(struct ieee80211_channel *beacon_chan)
  {
  	struct reg_beacon *pending_beacon;
  
  	list_for_each_entry(pending_beacon, &reg_pending_beacons, list)
  		if (beacon_chan->center_freq ==
  		    pending_beacon->chan.center_freq)
  			return true;
  	return false;
  }
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2170
2171
2172
2173
2174
  int regulatory_hint_found_beacon(struct wiphy *wiphy,
  				 struct ieee80211_channel *beacon_chan,
  				 gfp_t gfp)
  {
  	struct reg_beacon *reg_beacon;
3ebfa6e76   Luis R. Rodriguez   cfg80211: do not ...
2175
  	bool processing;
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2176

1a9193185   Johannes Berg   regulatory: code ...
2177
2178
  	if (beacon_chan->beacon_found ||
  	    beacon_chan->flags & IEEE80211_CHAN_RADAR ||
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2179
  	    (beacon_chan->band == IEEE80211_BAND_2GHZ &&
1a9193185   Johannes Berg   regulatory: code ...
2180
  	     !freq_is_chan_12_13_14(beacon_chan->center_freq)))
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2181
  		return 0;
3ebfa6e76   Luis R. Rodriguez   cfg80211: do not ...
2182
2183
2184
2185
2186
  	spin_lock_bh(&reg_pending_beacons_lock);
  	processing = pending_reg_beacon(beacon_chan);
  	spin_unlock_bh(&reg_pending_beacons_lock);
  
  	if (processing)
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2187
2188
2189
2190
2191
  		return 0;
  
  	reg_beacon = kzalloc(sizeof(struct reg_beacon), gfp);
  	if (!reg_beacon)
  		return -ENOMEM;
1a9193185   Johannes Berg   regulatory: code ...
2192
2193
  	REG_DBG_PRINT("Found new beacon on frequency: %d MHz (Ch %d) on %s
  ",
4113f7518   Luis R. Rodriguez   cfg80211: add a r...
2194
2195
2196
  		      beacon_chan->center_freq,
  		      ieee80211_frequency_to_channel(beacon_chan->center_freq),
  		      wiphy_name(wiphy));
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2197
  	memcpy(&reg_beacon->chan, beacon_chan,
1a9193185   Johannes Berg   regulatory: code ...
2198
  	       sizeof(struct ieee80211_channel));
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
  
  	/*
  	 * Since we can be called from BH or and non-BH context
  	 * we must use spin_lock_bh()
  	 */
  	spin_lock_bh(&reg_pending_beacons_lock);
  	list_add_tail(&reg_beacon->list, &reg_pending_beacons);
  	spin_unlock_bh(&reg_pending_beacons_lock);
  
  	schedule_work(&reg_work);
  
  	return 0;
  }
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
2212
  static void print_rd_rules(const struct ieee80211_regdomain *rd)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2213
2214
  {
  	unsigned int i;
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
2215
2216
2217
  	const struct ieee80211_reg_rule *reg_rule = NULL;
  	const struct ieee80211_freq_range *freq_range = NULL;
  	const struct ieee80211_power_rule *power_rule = NULL;
089027e57   Janusz Dziedzic   cfg80211: regulat...
2218
  	char bw[32], cac_time[32];
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2219

089027e57   Janusz Dziedzic   cfg80211: regulat...
2220
2221
  	pr_info("  (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp), (dfs_cac_time)
  ");
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2222
2223
2224
2225
2226
  
  	for (i = 0; i < rd->n_reg_rules; i++) {
  		reg_rule = &rd->reg_rules[i];
  		freq_range = &reg_rule->freq_range;
  		power_rule = &reg_rule->power_rule;
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
2227
2228
2229
  		if (reg_rule->flags & NL80211_RRF_AUTO_BW)
  			snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
  				 freq_range->max_bandwidth_khz,
975248208   Janusz Dziedzic   cfg80211: regulat...
2230
2231
  				 reg_get_max_bandwidth(rd, reg_rule));
  		else
b0dfd2ea1   Janusz Dziedzic   cfg80211: regulat...
2232
  			snprintf(bw, sizeof(bw), "%d KHz",
975248208   Janusz Dziedzic   cfg80211: regulat...
2233
  				 freq_range->max_bandwidth_khz);
089027e57   Janusz Dziedzic   cfg80211: regulat...
2234
2235
2236
2237
2238
  		if (reg_rule->flags & NL80211_RRF_DFS)
  			scnprintf(cac_time, sizeof(cac_time), "%u s",
  				  reg_rule->dfs_cac_ms/1000);
  		else
  			scnprintf(cac_time, sizeof(cac_time), "N/A");
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
2239
2240
2241
2242
  		/*
  		 * There may not be documentation for max antenna gain
  		 * in certain regions
  		 */
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2243
  		if (power_rule->max_antenna_gain)
089027e57   Janusz Dziedzic   cfg80211: regulat...
2244
2245
  			pr_info("  (%d KHz - %d KHz @ %s), (%d mBi, %d mBm), (%s)
  ",
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2246
2247
  				freq_range->start_freq_khz,
  				freq_range->end_freq_khz,
975248208   Janusz Dziedzic   cfg80211: regulat...
2248
  				bw,
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2249
  				power_rule->max_antenna_gain,
089027e57   Janusz Dziedzic   cfg80211: regulat...
2250
2251
  				power_rule->max_eirp,
  				cac_time);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2252
  		else
089027e57   Janusz Dziedzic   cfg80211: regulat...
2253
2254
  			pr_info("  (%d KHz - %d KHz @ %s), (N/A, %d mBm), (%s)
  ",
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2255
2256
  				freq_range->start_freq_khz,
  				freq_range->end_freq_khz,
975248208   Janusz Dziedzic   cfg80211: regulat...
2257
  				bw,
089027e57   Janusz Dziedzic   cfg80211: regulat...
2258
2259
  				power_rule->max_eirp,
  				cac_time);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2260
2261
  	}
  }
4c7d3982a   Luis R. Rodriguez   cfg80211: use enu...
2262
  bool reg_supported_dfs_region(enum nl80211_dfs_regions dfs_region)
8b60b0780   Luis R. Rodriguez   cfg80211: process...
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
  {
  	switch (dfs_region) {
  	case NL80211_DFS_UNSET:
  	case NL80211_DFS_FCC:
  	case NL80211_DFS_ETSI:
  	case NL80211_DFS_JP:
  		return true;
  	default:
  		REG_DBG_PRINT("Ignoring uknown DFS master region: %d
  ",
  			      dfs_region);
  		return false;
  	}
  }
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
2277
  static void print_regdomain(const struct ieee80211_regdomain *rd)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2278
  {
c492db370   Johannes Berg   regulatory: use R...
2279
  	struct regulatory_request *lr = get_last_request();
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2280

3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
2281
  	if (is_intersected_alpha2(rd->alpha2)) {
c492db370   Johannes Berg   regulatory: use R...
2282
  		if (lr->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
79c97e97a   Johannes Berg   cfg80211: clean u...
2283
  			struct cfg80211_registered_device *rdev;
c492db370   Johannes Berg   regulatory: use R...
2284
  			rdev = cfg80211_rdev_by_wiphy_idx(lr->wiphy_idx);
79c97e97a   Johannes Berg   cfg80211: clean u...
2285
  			if (rdev) {
e9c0268f0   Joe Perches   net/wireless: Use...
2286
2287
  				pr_info("Current regulatory domain updated by AP to: %c%c
  ",
79c97e97a   Johannes Berg   cfg80211: clean u...
2288
2289
  					rdev->country_ie_alpha2[0],
  					rdev->country_ie_alpha2[1]);
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
2290
  			} else
e9c0268f0   Joe Perches   net/wireless: Use...
2291
2292
  				pr_info("Current regulatory domain intersected:
  ");
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
2293
  		} else
e9c0268f0   Joe Perches   net/wireless: Use...
2294
2295
  			pr_info("Current regulatory domain intersected:
  ");
1a9193185   Johannes Berg   regulatory: code ...
2296
  	} else if (is_world_regdom(rd->alpha2)) {
e9c0268f0   Joe Perches   net/wireless: Use...
2297
2298
  		pr_info("World regulatory domain updated:
  ");
1a9193185   Johannes Berg   regulatory: code ...
2299
  	} else {
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2300
  		if (is_unknown_alpha2(rd->alpha2))
e9c0268f0   Joe Perches   net/wireless: Use...
2301
2302
  			pr_info("Regulatory domain changed to driver built-in settings (unknown country)
  ");
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
2303
  		else {
c492db370   Johannes Berg   regulatory: use R...
2304
  			if (reg_request_cell_base(lr))
1a9193185   Johannes Berg   regulatory: code ...
2305
2306
  				pr_info("Regulatory domain changed to country: %c%c by Cell Station
  ",
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
2307
2308
  					rd->alpha2[0], rd->alpha2[1]);
  			else
1a9193185   Johannes Berg   regulatory: code ...
2309
2310
  				pr_info("Regulatory domain changed to country: %c%c
  ",
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
2311
2312
  					rd->alpha2[0], rd->alpha2[1]);
  		}
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2313
  	}
1a9193185   Johannes Berg   regulatory: code ...
2314

3ef121b51   Luis R. Rodriguez   cfg80211: replace...
2315
  	pr_info(" DFS Master region: %s", reg_dfs_region_str(rd->dfs_region));
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2316
2317
  	print_rd_rules(rd);
  }
2df78167a   Johannes Berg   wireless: fix a f...
2318
  static void print_regdomain_info(const struct ieee80211_regdomain *rd)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2319
  {
e9c0268f0   Joe Perches   net/wireless: Use...
2320
2321
  	pr_info("Regulatory domain: %c%c
  ", rd->alpha2[0], rd->alpha2[1]);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2322
2323
  	print_rd_rules(rd);
  }
3b9e5aca4   Luis R. Rodriguez   cfg80211: set cor...
2324
2325
2326
2327
2328
2329
2330
  static int reg_set_rd_core(const struct ieee80211_regdomain *rd)
  {
  	if (!is_world_regdom(rd->alpha2))
  		return -EINVAL;
  	update_world_regdomain(rd);
  	return 0;
  }
84721d449   Luis R. Rodriguez   cfg80211: set use...
2331
2332
2333
2334
  static int reg_set_rd_user(const struct ieee80211_regdomain *rd,
  			   struct regulatory_request *user_request)
  {
  	const struct ieee80211_regdomain *intersected_rd = NULL;
84721d449   Luis R. Rodriguez   cfg80211: set use...
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
  	if (!regdom_changes(rd->alpha2))
  		return -EALREADY;
  
  	if (!is_valid_rd(rd)) {
  		pr_err("Invalid regulatory domain detected:
  ");
  		print_regdomain_info(rd);
  		return -EINVAL;
  	}
  
  	if (!user_request->intersect) {
  		reset_regdomains(false, rd);
  		return 0;
  	}
  
  	intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
  	if (!intersected_rd)
  		return -EINVAL;
  
  	kfree(rd);
  	rd = NULL;
  	reset_regdomains(false, intersected_rd);
  
  	return 0;
  }
f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2360
2361
  static int reg_set_rd_driver(const struct ieee80211_regdomain *rd,
  			     struct regulatory_request *driver_request)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2362
  {
e9763c3c2   Johannes Berg   regulatory: clean...
2363
  	const struct ieee80211_regdomain *regd;
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
2364
  	const struct ieee80211_regdomain *intersected_rd = NULL;
f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2365
  	const struct ieee80211_regdomain *tmp;
806a9e396   Luis R. Rodriguez   cfg80211: make re...
2366
  	struct wiphy *request_wiphy;
6913b49a5   Johannes Berg   regulatory: fix r...
2367

f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2368
  	if (is_world_regdom(rd->alpha2))
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2369
  		return -EINVAL;
f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2370
2371
  	if (!regdom_changes(rd->alpha2))
  		return -EALREADY;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2372

8375af3ba   Luis R. Rodriguez   cfg80211: remove ...
2373
  	if (!is_valid_rd(rd)) {
e9c0268f0   Joe Perches   net/wireless: Use...
2374
2375
  		pr_err("Invalid regulatory domain detected:
  ");
8375af3ba   Luis R. Rodriguez   cfg80211: remove ...
2376
2377
  		print_regdomain_info(rd);
  		return -EINVAL;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2378
  	}
f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2379
2380
  	request_wiphy = wiphy_idx_to_wiphy(driver_request->wiphy_idx);
  	if (!request_wiphy) {
845f3351b   Shaibal Dutta   net: wireless: mo...
2381
2382
  		queue_delayed_work(system_power_efficient_wq,
  				   &reg_timeout, 0);
de3584bd6   Johannes Berg   cfg80211: fix reg...
2383
2384
  		return -ENODEV;
  	}
806a9e396   Luis R. Rodriguez   cfg80211: make re...
2385

f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2386
  	if (!driver_request->intersect) {
558f6d322   Luis R. Rodriguez   cfg80211: fix for...
2387
2388
  		if (request_wiphy->regd)
  			return -EALREADY;
3e0c3ff36   Luis R. Rodriguez   cfg80211: allow m...
2389

e9763c3c2   Johannes Berg   regulatory: clean...
2390
2391
2392
  		regd = reg_copy_regd(rd);
  		if (IS_ERR(regd))
  			return PTR_ERR(regd);
3e0c3ff36   Luis R. Rodriguez   cfg80211: allow m...
2393

458f4f9e9   Johannes Berg   regulatory: use R...
2394
  		rcu_assign_pointer(request_wiphy->regd, regd);
379b82f4c   Johannes Berg   regulatory: pass ...
2395
  		reset_regdomains(false, rd);
b8295acdc   Luis R. Rodriguez   cfg80211: separat...
2396
2397
  		return 0;
  	}
f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2398
2399
2400
  	intersected_rd = regdom_intersect(rd, get_cfg80211_regdom());
  	if (!intersected_rd)
  		return -EINVAL;
b8295acdc   Luis R. Rodriguez   cfg80211: separat...
2401

f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2402
2403
2404
2405
2406
2407
2408
2409
  	/*
  	 * We can trash what CRDA provided now.
  	 * However if a driver requested this specific regulatory
  	 * domain we keep it for its private use
  	 */
  	tmp = get_wiphy_regdom(request_wiphy);
  	rcu_assign_pointer(request_wiphy->regd, rd);
  	rcu_free_regdom(tmp);
b8295acdc   Luis R. Rodriguez   cfg80211: separat...
2410

f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2411
  	rd = NULL;
b7566fc36   Larry Finger   cfg80211: Fix mem...
2412

f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2413
  	reset_regdomains(false, intersected_rd);
3e0c3ff36   Luis R. Rodriguez   cfg80211: allow m...
2414

f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2415
2416
  	return 0;
  }
01992406d   Luis R. Rodriguez   cfg80211: rename ...
2417
2418
  static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
  				 struct regulatory_request *country_ie_request)
f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2419
2420
  {
  	struct wiphy *request_wiphy;
b8295acdc   Luis R. Rodriguez   cfg80211: separat...
2421

f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2422
2423
2424
  	if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
  	    !is_unknown_alpha2(rd->alpha2))
  		return -EINVAL;
b8295acdc   Luis R. Rodriguez   cfg80211: separat...
2425

f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
  	/*
  	 * Lets only bother proceeding on the same alpha2 if the current
  	 * rd is non static (it means CRDA was present and was used last)
  	 * and the pending request came in from a country IE
  	 */
  
  	if (!is_valid_rd(rd)) {
  		pr_err("Invalid regulatory domain detected:
  ");
  		print_regdomain_info(rd);
  		return -EINVAL;
9c96477d1   Luis R. Rodriguez   cfg80211: Add reg...
2437
  	}
01992406d   Luis R. Rodriguez   cfg80211: rename ...
2438
  	request_wiphy = wiphy_idx_to_wiphy(country_ie_request->wiphy_idx);
f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2439
  	if (!request_wiphy) {
845f3351b   Shaibal Dutta   net: wireless: mo...
2440
2441
  		queue_delayed_work(system_power_efficient_wq,
  				   &reg_timeout, 0);
f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2442
2443
  		return -ENODEV;
  	}
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2444

01992406d   Luis R. Rodriguez   cfg80211: rename ...
2445
  	if (country_ie_request->intersect)
f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2446
2447
2448
2449
2450
  		return -EINVAL;
  
  	reset_regdomains(false, rd);
  	return 0;
  }
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2451

fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
2452
2453
  /*
   * Use this call to set the current regulatory domain. Conflicts with
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2454
   * multiple drivers can be ironed out later. Caller must've already
458f4f9e9   Johannes Berg   regulatory: use R...
2455
   * kmalloc'd the rd structure.
fb1fc7add   Luis R. Rodriguez   cfg80211: comment...
2456
   */
a3d2eaf0d   Johannes Berg   cfg80211: fix reg...
2457
  int set_regdom(const struct ieee80211_regdomain *rd)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2458
  {
c492db370   Johannes Berg   regulatory: use R...
2459
  	struct regulatory_request *lr;
092008abe   Janusz Dziedzic   cfg80211: regulat...
2460
  	bool user_reset = false;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2461
  	int r;
3b9e5aca4   Luis R. Rodriguez   cfg80211: set cor...
2462
2463
2464
2465
  	if (!reg_is_valid_request(rd->alpha2)) {
  		kfree(rd);
  		return -EINVAL;
  	}
c492db370   Johannes Berg   regulatory: use R...
2466
  	lr = get_last_request();
abc7381bc   Luis R. Rodriguez   cfg80211: decoupl...
2467

b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2468
  	/* Note that this doesn't update the wiphys, this is done below */
3b9e5aca4   Luis R. Rodriguez   cfg80211: set cor...
2469
2470
2471
2472
2473
  	switch (lr->initiator) {
  	case NL80211_REGDOM_SET_BY_CORE:
  		r = reg_set_rd_core(rd);
  		break;
  	case NL80211_REGDOM_SET_BY_USER:
84721d449   Luis R. Rodriguez   cfg80211: set use...
2474
  		r = reg_set_rd_user(rd, lr);
092008abe   Janusz Dziedzic   cfg80211: regulat...
2475
  		user_reset = true;
84721d449   Luis R. Rodriguez   cfg80211: set use...
2476
  		break;
3b9e5aca4   Luis R. Rodriguez   cfg80211: set cor...
2477
  	case NL80211_REGDOM_SET_BY_DRIVER:
f5fe32478   Luis R. Rodriguez   cfg80211: set dri...
2478
2479
  		r = reg_set_rd_driver(rd, lr);
  		break;
3b9e5aca4   Luis R. Rodriguez   cfg80211: set cor...
2480
  	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
01992406d   Luis R. Rodriguez   cfg80211: rename ...
2481
  		r = reg_set_rd_country_ie(rd, lr);
3b9e5aca4   Luis R. Rodriguez   cfg80211: set cor...
2482
2483
2484
2485
2486
2487
  		break;
  	default:
  		WARN(1, "invalid initiator %d
  ", lr->initiator);
  		return -EINVAL;
  	}
d2372b315   Johannes Berg   wireless: make re...
2488
  	if (r) {
092008abe   Janusz Dziedzic   cfg80211: regulat...
2489
2490
  		switch (r) {
  		case -EALREADY:
959085352   Kalle Valo   cfg80211: fix set...
2491
  			reg_set_request_processed();
092008abe   Janusz Dziedzic   cfg80211: regulat...
2492
2493
2494
2495
2496
  			break;
  		default:
  			/* Back to world regulatory in case of errors */
  			restore_regulatory_settings(user_reset);
  		}
959085352   Kalle Valo   cfg80211: fix set...
2497

d2372b315   Johannes Berg   wireless: make re...
2498
  		kfree(rd);
38fd2143f   Johannes Berg   regulatory: remov...
2499
  		return r;
d2372b315   Johannes Berg   wireless: make re...
2500
  	}
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2501

b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2502
  	/* This would make this whole thing pointless */
38fd2143f   Johannes Berg   regulatory: remov...
2503
2504
  	if (WARN_ON(!lr->intersect && rd != get_cfg80211_regdom()))
  		return -EINVAL;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2505
2506
  
  	/* update all wiphys now with the new established regulatory domain */
c492db370   Johannes Berg   regulatory: use R...
2507
  	update_all_wiphy_regulatory(lr->initiator);
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2508

458f4f9e9   Johannes Berg   regulatory: use R...
2509
  	print_regdomain(get_cfg80211_regdom());
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2510

c492db370   Johannes Berg   regulatory: use R...
2511
  	nl80211_send_reg_change_event(lr);
73d54c9e7   Luis R. Rodriguez   cfg80211: add reg...
2512

b2e253cf3   Luis R. Rodriguez   cfg80211: Fix reg...
2513
  	reg_set_request_processed();
38fd2143f   Johannes Berg   regulatory: remov...
2514
  	return 0;
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2515
  }
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
2516
2517
  void wiphy_regulatory_register(struct wiphy *wiphy)
  {
23df0b731   Arik Nemtsov   regulatory: use c...
2518
  	struct regulatory_request *lr;
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
2519
2520
  	if (!reg_dev_ignore_cell_hint(wiphy))
  		reg_num_devs_support_basehint++;
23df0b731   Arik Nemtsov   regulatory: use c...
2521
2522
  	lr = get_last_request();
  	wiphy_update_regulatory(wiphy, lr->initiator);
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
2523
  }
bfead0808   Luis R. Rodriguez   cfg80211: rename ...
2524
  void wiphy_regulatory_deregister(struct wiphy *wiphy)
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
2525
  {
0ad8acaf4   Luis R. Rodriguez   cfg80211: fix NUL...
2526
  	struct wiphy *request_wiphy = NULL;
c492db370   Johannes Berg   regulatory: use R...
2527
  	struct regulatory_request *lr;
761cf7ecf   Luis R. Rodriguez   cfg80211: add ass...
2528

c492db370   Johannes Berg   regulatory: use R...
2529
  	lr = get_last_request();
abc7381bc   Luis R. Rodriguez   cfg80211: decoupl...
2530

57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
2531
2532
  	if (!reg_dev_ignore_cell_hint(wiphy))
  		reg_num_devs_support_basehint--;
458f4f9e9   Johannes Berg   regulatory: use R...
2533
  	rcu_free_regdom(get_wiphy_regdom(wiphy));
34dd886c1   Monam Agarwal   cfg80211: regulat...
2534
  	RCU_INIT_POINTER(wiphy->regd, NULL);
0ef9ccdd9   Chris Wright   cfg80211: remove ...
2535

c492db370   Johannes Berg   regulatory: use R...
2536
2537
  	if (lr)
  		request_wiphy = wiphy_idx_to_wiphy(lr->wiphy_idx);
806a9e396   Luis R. Rodriguez   cfg80211: make re...
2538

0ef9ccdd9   Chris Wright   cfg80211: remove ...
2539
  	if (!request_wiphy || request_wiphy != wiphy)
38fd2143f   Johannes Berg   regulatory: remov...
2540
  		return;
0ef9ccdd9   Chris Wright   cfg80211: remove ...
2541

c492db370   Johannes Berg   regulatory: use R...
2542
2543
  	lr->wiphy_idx = WIPHY_IDX_INVALID;
  	lr->country_ie_env = ENVIRON_ANY;
3f2355cb9   Luis R. Rodriguez   cfg80211/mac80211...
2544
  }
a90c7a313   Luis R. Rodriguez   cfg80211: add a t...
2545
2546
  static void reg_timeout_work(struct work_struct *work)
  {
1a9193185   Johannes Berg   regulatory: code ...
2547
2548
  	REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings
  ");
f77b86d7d   Johannes Berg   regulatory: add m...
2549
  	rtnl_lock();
a90c7a313   Luis R. Rodriguez   cfg80211: add a t...
2550
  	restore_regulatory_settings(true);
f77b86d7d   Johannes Berg   regulatory: add m...
2551
  	rtnl_unlock();
a90c7a313   Luis R. Rodriguez   cfg80211: add a t...
2552
  }
174e0cd28   Ilan Peer   cfg80211: Enable ...
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
  /*
   * See http://www.fcc.gov/document/5-ghz-unlicensed-spectrum-unii, for
   * UNII band definitions
   */
  int cfg80211_get_unii(int freq)
  {
  	/* UNII-1 */
  	if (freq >= 5150 && freq <= 5250)
  		return 0;
  
  	/* UNII-2A */
  	if (freq > 5250 && freq <= 5350)
  		return 1;
  
  	/* UNII-2B */
  	if (freq > 5350 && freq <= 5470)
  		return 2;
  
  	/* UNII-2C */
  	if (freq > 5470 && freq <= 5725)
  		return 3;
  
  	/* UNII-3 */
  	if (freq > 5725 && freq <= 5825)
  		return 4;
  
  	return -EINVAL;
  }
c8866e55a   Ilan Peer   cfg80211: Enable ...
2581
2582
2583
2584
  bool regulatory_indoor_allowed(void)
  {
  	return reg_is_indoor;
  }
2fcc9f731   Uwe Kleine-König   wireless: move re...
2585
  int __init regulatory_init(void)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2586
  {
bcf4f99b7   Luis R. Rodriguez   cfg80211: propaga...
2587
  	int err = 0;
734366dea   Johannes Berg   cfg80211: clean u...
2588

b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2589
2590
2591
  	reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
  	if (IS_ERR(reg_pdev))
  		return PTR_ERR(reg_pdev);
734366dea   Johannes Berg   cfg80211: clean u...
2592

fe33eb390   Luis R. Rodriguez   cfg80211: move al...
2593
  	spin_lock_init(&reg_requests_lock);
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2594
  	spin_lock_init(&reg_pending_beacons_lock);
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
2595

80007efef   Luis R. Rodriguez   cfg80211: warn if...
2596
  	reg_regdb_size_check();
458f4f9e9   Johannes Berg   regulatory: use R...
2597
  	rcu_assign_pointer(cfg80211_regdomain, cfg80211_world_regdom);
734366dea   Johannes Berg   cfg80211: clean u...
2598

09d989d17   Luis R. Rodriguez   cfg80211: add reg...
2599
2600
  	user_alpha2[0] = '9';
  	user_alpha2[1] = '7';
ae9e4b0d1   Luis R. Rodriguez   cfg80211: treat i...
2601
  	/* We always try to get an update for the static regdomain */
458f4f9e9   Johannes Berg   regulatory: use R...
2602
  	err = regulatory_hint_core(cfg80211_world_regdom->alpha2);
ba25c1414   Luis R. Rodriguez   cfg80211: add reg...
2603
  	if (err) {
bcf4f99b7   Luis R. Rodriguez   cfg80211: propaga...
2604
2605
2606
2607
2608
2609
2610
2611
2612
  		if (err == -ENOMEM)
  			return err;
  		/*
  		 * N.B. kobject_uevent_env() can fail mainly for when we're out
  		 * memory which is handled and propagated appropriately above
  		 * but it can also fail during a netlink_broadcast() or during
  		 * early boot for call_usermodehelper(). For now treat these
  		 * errors as non-fatal.
  		 */
e9c0268f0   Joe Perches   net/wireless: Use...
2613
2614
  		pr_err("kobject_uevent_env() was unable to call CRDA during init
  ");
bcf4f99b7   Luis R. Rodriguez   cfg80211: propaga...
2615
  	}
734366dea   Johannes Berg   cfg80211: clean u...
2616

ae9e4b0d1   Luis R. Rodriguez   cfg80211: treat i...
2617
2618
2619
2620
2621
  	/*
  	 * Finally, if the user set the module parameter treat it
  	 * as a user hint.
  	 */
  	if (!is_world_regdom(ieee80211_regdom))
57b5ce072   Luis R. Rodriguez   cfg80211: add cel...
2622
2623
  		regulatory_hint_user(ieee80211_regdom,
  				     NL80211_USER_REG_HINT_USER);
ae9e4b0d1   Luis R. Rodriguez   cfg80211: treat i...
2624

b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2625
2626
  	return 0;
  }
1a9193185   Johannes Berg   regulatory: code ...
2627
  void regulatory_exit(void)
b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2628
  {
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
2629
  	struct regulatory_request *reg_request, *tmp;
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2630
  	struct reg_beacon *reg_beacon, *btmp;
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
2631
2632
  
  	cancel_work_sync(&reg_work);
a90c7a313   Luis R. Rodriguez   cfg80211: add a t...
2633
  	cancel_delayed_work_sync(&reg_timeout);
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
2634

9027b1493   Johannes Berg   regulatory: remov...
2635
  	/* Lock to suppress warnings */
38fd2143f   Johannes Berg   regulatory: remov...
2636
  	rtnl_lock();
379b82f4c   Johannes Berg   regulatory: pass ...
2637
  	reset_regdomains(true, NULL);
38fd2143f   Johannes Berg   regulatory: remov...
2638
  	rtnl_unlock();
734366dea   Johannes Berg   cfg80211: clean u...
2639

58ebacc66   Luis R. Rodriguez   cfg80211: fix bug...
2640
  	dev_set_uevent_suppress(&reg_pdev->dev, true);
f6037d09e   Johannes Berg   wireless: get rid...
2641

b2e1b3029   Luis R. Rodriguez   cfg80211: Add new...
2642
  	platform_device_unregister(reg_pdev);
734366dea   Johannes Berg   cfg80211: clean u...
2643

fea9bcedc   Johannes Berg   regulatory: don't...
2644
2645
2646
  	list_for_each_entry_safe(reg_beacon, btmp, &reg_pending_beacons, list) {
  		list_del(&reg_beacon->list);
  		kfree(reg_beacon);
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2647
  	}
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2648

fea9bcedc   Johannes Berg   regulatory: don't...
2649
2650
2651
  	list_for_each_entry_safe(reg_beacon, btmp, &reg_beacon_list, list) {
  		list_del(&reg_beacon->list);
  		kfree(reg_beacon);
e38f8a7a8   Luis R. Rodriguez   cfg80211: Add AP ...
2652
  	}
fea9bcedc   Johannes Berg   regulatory: don't...
2653
2654
2655
  	list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) {
  		list_del(&reg_request->list);
  		kfree(reg_request);
fe33eb390   Luis R. Rodriguez   cfg80211: move al...
2656
  	}
8318d78a4   Johannes Berg   cfg80211 API for ...
2657
  }