1 /*
2 * Wireless configuration interface internals.
3 *
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 */
6 #ifndef __NET_WIRELESS_CORE_H
7 #define __NET_WIRELESS_CORE_H
8 #include <linux/mutex.h>
9 #include <linux/list.h>
10 #include <linux/netdevice.h>
11 #include <linux/rbtree.h>
12 #include <linux/debugfs.h>
13 #include <linux/rfkill.h>
14 #include <linux/workqueue.h>
15 #include <linux/rtnetlink.h>
16 #include <net/genetlink.h>
17 #include <net/cfg80211.h>
18 #include "reg.h"
19
20
21 #define WIPHY_IDX_INVALID -1
22
23 struct cfg80211_registered_device {
24 const struct cfg80211_ops *ops;
25 struct list_head list;
26 /* we hold this mutex during any call so that
27 * we cannot do multiple calls at once, and also
28 * to avoid the deregister call to proceed while
29 * any call is in progress */
30 struct mutex mtx;
31
32 /* rfkill support */
33 struct rfkill_ops rfkill_ops;
34 struct rfkill *rfkill;
35 struct work_struct rfkill_sync;
36
37 /* ISO / IEC 3166 alpha2 for which this device is receiving
38 * country IEs on, this can help disregard country IEs from APs
39 * on the same alpha2 quickly. The alpha2 may differ from
40 * cfg80211_regdomain's alpha2 when an intersection has occurred.
41 * If the AP is reconfigured this can also be used to tell us if
42 * the country on the country IE changed. */
43 char country_ie_alpha2[2];
44
45 /* If a Country IE has been received this tells us the environment
46 * which its telling us its in. This defaults to ENVIRON_ANY */
47 enum environment_cap env;
48
49 /* wiphy index, internal only */
50 int wiphy_idx;
51
52 /* associated wireless interfaces */
53 struct mutex devlist_mtx;
54 /* protected by devlist_mtx or RCU */
55 struct list_head wdev_list;
56 int devlist_generation, wdev_id;
57 int opencount; /* also protected by devlist_mtx */
58 wait_queue_head_t dev_wait;
59
60 struct list_head beacon_registrations;
61 spinlock_t beacon_registrations_lock;
62
63 /* protected by RTNL only */
64 int num_running_ifaces;
65 int num_running_monitor_ifaces;
66
67 /* BSSes/scanning */
68 spinlock_t bss_lock;
69 struct list_head bss_list;
70 struct rb_root bss_tree;
71 u32 bss_generation;
72 struct cfg80211_scan_request *scan_req; /* protected by RTNL */
73 struct cfg80211_sched_scan_request *sched_scan_req;
74 unsigned long suspend_at;
75 struct work_struct scan_done_wk;
76 struct work_struct sched_scan_results_wk;
77
78 struct mutex sched_scan_mtx;
79
80 struct genl_info *cur_cmd_info;
81
82 struct work_struct conn_work;
83 struct work_struct event_work;
84
85 struct cfg80211_wowlan *wowlan;
86
87 struct delayed_work dfs_update_channels_wk;
88
89 /* netlink port which started critical protocol (0 means not started) */
90 u32 crit_proto_nlportid;
91
92 spinlock_t destroy_list_lock;
93 struct list_head destroy_list;
94 struct work_struct destroy_work;
95
96 struct work_struct sched_scan_stop_wk;
97
98 /* must be last because of the way we do wiphy_priv(),
99 * and it should at least be aligned to NETDEV_ALIGN */
100 struct wiphy wiphy __aligned(NETDEV_ALIGN);
101 };
102
103 static inline
wiphy_to_dev(struct wiphy * wiphy)104 struct cfg80211_registered_device *wiphy_to_dev(struct wiphy *wiphy)
105 {
106 BUG_ON(!wiphy);
107 return container_of(wiphy, struct cfg80211_registered_device, wiphy);
108 }
109
110 static inline void
cfg80211_rdev_free_wowlan(struct cfg80211_registered_device * rdev)111 cfg80211_rdev_free_wowlan(struct cfg80211_registered_device *rdev)
112 {
113 int i;
114
115 if (!rdev->wowlan)
116 return;
117 for (i = 0; i < rdev->wowlan->n_patterns; i++)
118 kfree(rdev->wowlan->patterns[i].mask);
119 kfree(rdev->wowlan->patterns);
120 if (rdev->wowlan->tcp && rdev->wowlan->tcp->sock)
121 sock_release(rdev->wowlan->tcp->sock);
122 kfree(rdev->wowlan->tcp);
123 kfree(rdev->wowlan);
124 }
125
126 extern struct workqueue_struct *cfg80211_wq;
127 extern struct mutex cfg80211_mutex;
128 extern struct list_head cfg80211_rdev_list;
129 extern int cfg80211_rdev_list_generation;
130
assert_cfg80211_lock(void)131 static inline void assert_cfg80211_lock(void)
132 {
133 lockdep_assert_held(&cfg80211_mutex);
134 }
135
136 struct cfg80211_internal_bss {
137 struct list_head list;
138 struct list_head hidden_list;
139 struct rb_node rbn;
140 unsigned long ts;
141 unsigned long refcount;
142 atomic_t hold;
143
144 /* must be last because of priv member */
145 struct cfg80211_bss pub;
146 };
147
bss_from_pub(struct cfg80211_bss * pub)148 static inline struct cfg80211_internal_bss *bss_from_pub(struct cfg80211_bss *pub)
149 {
150 return container_of(pub, struct cfg80211_internal_bss, pub);
151 }
152
cfg80211_hold_bss(struct cfg80211_internal_bss * bss)153 static inline void cfg80211_hold_bss(struct cfg80211_internal_bss *bss)
154 {
155 atomic_inc(&bss->hold);
156 }
157
cfg80211_unhold_bss(struct cfg80211_internal_bss * bss)158 static inline void cfg80211_unhold_bss(struct cfg80211_internal_bss *bss)
159 {
160 int r = atomic_dec_return(&bss->hold);
161 WARN_ON(r < 0);
162 }
163
164
165 struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx);
166 int get_wiphy_idx(struct wiphy *wiphy);
167
168 /* requires cfg80211_rdev_mutex to be held! */
169 struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx);
170
171 /* identical to cfg80211_get_dev_from_info but only operate on ifindex */
172 extern struct cfg80211_registered_device *
173 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex);
174
175 int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
176 struct net *net);
177
cfg80211_lock_rdev(struct cfg80211_registered_device * rdev)178 static inline void cfg80211_lock_rdev(struct cfg80211_registered_device *rdev)
179 {
180 mutex_lock(&rdev->mtx);
181 }
182
cfg80211_unlock_rdev(struct cfg80211_registered_device * rdev)183 static inline void cfg80211_unlock_rdev(struct cfg80211_registered_device *rdev)
184 {
185 BUG_ON(IS_ERR(rdev) || !rdev);
186 mutex_unlock(&rdev->mtx);
187 }
188
wdev_lock(struct wireless_dev * wdev)189 static inline void wdev_lock(struct wireless_dev *wdev)
190 __acquires(wdev)
191 {
192 mutex_lock(&wdev->mtx);
193 __acquire(wdev->mtx);
194 }
195
wdev_unlock(struct wireless_dev * wdev)196 static inline void wdev_unlock(struct wireless_dev *wdev)
197 __releases(wdev)
198 {
199 __release(wdev->mtx);
200 mutex_unlock(&wdev->mtx);
201 }
202
203 #define ASSERT_RDEV_LOCK(rdev) lockdep_assert_held(&(rdev)->mtx)
204 #define ASSERT_WDEV_LOCK(wdev) lockdep_assert_held(&(wdev)->mtx)
205
cfg80211_has_monitors_only(struct cfg80211_registered_device * rdev)206 static inline bool cfg80211_has_monitors_only(struct cfg80211_registered_device *rdev)
207 {
208 ASSERT_RTNL();
209
210 return rdev->num_running_ifaces == rdev->num_running_monitor_ifaces &&
211 rdev->num_running_ifaces > 0;
212 }
213
214 enum cfg80211_event_type {
215 EVENT_CONNECT_RESULT,
216 EVENT_ROAMED,
217 EVENT_DISCONNECTED,
218 EVENT_IBSS_JOINED,
219 };
220
221 struct cfg80211_event {
222 struct list_head list;
223 enum cfg80211_event_type type;
224
225 union {
226 struct {
227 u8 bssid[ETH_ALEN];
228 const u8 *req_ie;
229 const u8 *resp_ie;
230 size_t req_ie_len;
231 size_t resp_ie_len;
232 u16 status;
233 } cr;
234 struct {
235 const u8 *req_ie;
236 const u8 *resp_ie;
237 size_t req_ie_len;
238 size_t resp_ie_len;
239 struct cfg80211_bss *bss;
240 } rm;
241 struct {
242 const u8 *ie;
243 size_t ie_len;
244 u16 reason;
245 } dc;
246 struct {
247 u8 bssid[ETH_ALEN];
248 } ij;
249 };
250 };
251
252 struct cfg80211_cached_keys {
253 struct key_params params[6];
254 u8 data[6][WLAN_MAX_KEY_LEN];
255 int def, defmgmt;
256 };
257
258 enum cfg80211_chan_mode {
259 CHAN_MODE_UNDEFINED,
260 CHAN_MODE_SHARED,
261 CHAN_MODE_EXCLUSIVE,
262 };
263
264 struct cfg80211_beacon_registration {
265 struct list_head list;
266 u32 nlportid;
267 };
268
269 struct cfg80211_iface_destroy {
270 struct list_head list;
271 u32 nlportid;
272 };
273
274 void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev);
275
276 /* free object */
277 extern void cfg80211_dev_free(struct cfg80211_registered_device *rdev);
278
279 extern int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
280 char *newname);
281
282 void ieee80211_set_bitrate_flags(struct wiphy *wiphy);
283
284 void cfg80211_bss_expire(struct cfg80211_registered_device *dev);
285 void cfg80211_bss_age(struct cfg80211_registered_device *dev,
286 unsigned long age_secs);
287
288 /* IBSS */
289 int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
290 struct net_device *dev,
291 struct cfg80211_ibss_params *params,
292 struct cfg80211_cached_keys *connkeys);
293 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
294 struct net_device *dev,
295 struct cfg80211_ibss_params *params,
296 struct cfg80211_cached_keys *connkeys);
297 void cfg80211_clear_ibss(struct net_device *dev, bool nowext);
298 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
299 struct net_device *dev, bool nowext);
300 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
301 struct net_device *dev, bool nowext);
302 void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid);
303 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
304 struct wireless_dev *wdev);
305
306 /* mesh */
307 extern const struct mesh_config default_mesh_config;
308 extern const struct mesh_setup default_mesh_setup;
309 int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
310 struct net_device *dev,
311 struct mesh_setup *setup,
312 const struct mesh_config *conf);
313 int cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
314 struct net_device *dev,
315 struct mesh_setup *setup,
316 const struct mesh_config *conf);
317 int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
318 struct net_device *dev);
319 int cfg80211_set_mesh_channel(struct cfg80211_registered_device *rdev,
320 struct wireless_dev *wdev,
321 struct cfg80211_chan_def *chandef);
322
323 /* AP */
324 int cfg80211_stop_ap(struct cfg80211_registered_device *rdev,
325 struct net_device *dev);
326
327 /* MLME */
328 int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
329 struct net_device *dev,
330 struct ieee80211_channel *chan,
331 enum nl80211_auth_type auth_type,
332 const u8 *bssid,
333 const u8 *ssid, int ssid_len,
334 const u8 *ie, int ie_len,
335 const u8 *key, int key_len, int key_idx,
336 const u8 *sae_data, int sae_data_len);
337 int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
338 struct net_device *dev, struct ieee80211_channel *chan,
339 enum nl80211_auth_type auth_type, const u8 *bssid,
340 const u8 *ssid, int ssid_len,
341 const u8 *ie, int ie_len,
342 const u8 *key, int key_len, int key_idx,
343 const u8 *sae_data, int sae_data_len);
344 int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
345 struct net_device *dev,
346 struct ieee80211_channel *chan,
347 const u8 *bssid,
348 const u8 *ssid, int ssid_len,
349 struct cfg80211_assoc_request *req);
350 int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
351 struct net_device *dev,
352 struct ieee80211_channel *chan,
353 const u8 *bssid,
354 const u8 *ssid, int ssid_len,
355 struct cfg80211_assoc_request *req);
356 int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
357 struct net_device *dev, const u8 *bssid,
358 const u8 *ie, int ie_len, u16 reason,
359 bool local_state_change);
360 int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
361 struct net_device *dev, const u8 *bssid,
362 const u8 *ie, int ie_len, u16 reason,
363 bool local_state_change);
364 int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
365 struct net_device *dev, const u8 *bssid,
366 const u8 *ie, int ie_len, u16 reason,
367 bool local_state_change);
368 void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
369 struct net_device *dev);
370 void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
371 const u8 *req_ie, size_t req_ie_len,
372 const u8 *resp_ie, size_t resp_ie_len,
373 u16 status, bool wextev,
374 struct cfg80211_bss *bss);
375 int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
376 u16 frame_type, const u8 *match_data,
377 int match_len);
378 void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid);
379 void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev);
380 int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
381 struct wireless_dev *wdev,
382 struct ieee80211_channel *chan, bool offchan,
383 unsigned int wait, const u8 *buf, size_t len,
384 bool no_cck, bool dont_wait_for_ack, u64 *cookie);
385 void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
386 const struct ieee80211_ht_cap *ht_capa_mask);
387 void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
388 const struct ieee80211_vht_cap *vht_capa_mask);
389
390 /* SME */
391 int __cfg80211_connect(struct cfg80211_registered_device *rdev,
392 struct net_device *dev,
393 struct cfg80211_connect_params *connect,
394 struct cfg80211_cached_keys *connkeys,
395 const u8 *prev_bssid);
396 int cfg80211_connect(struct cfg80211_registered_device *rdev,
397 struct net_device *dev,
398 struct cfg80211_connect_params *connect,
399 struct cfg80211_cached_keys *connkeys);
400 int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
401 struct net_device *dev, u16 reason,
402 bool wextev);
403 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
404 struct net_device *dev, u16 reason,
405 bool wextev);
406 void __cfg80211_roamed(struct wireless_dev *wdev,
407 struct cfg80211_bss *bss,
408 const u8 *req_ie, size_t req_ie_len,
409 const u8 *resp_ie, size_t resp_ie_len);
410 int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
411 struct wireless_dev *wdev);
412
413 void cfg80211_conn_work(struct work_struct *work);
414 void cfg80211_sme_failed_assoc(struct wireless_dev *wdev);
415 bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev);
416
417 /* internal helpers */
418 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher);
419 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
420 struct key_params *params, int key_idx,
421 bool pairwise, const u8 *mac_addr);
422 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
423 size_t ie_len, u16 reason, bool from_ap);
424 void cfg80211_sme_scan_done(struct net_device *dev);
425 void cfg80211_sme_rx_auth(struct net_device *dev, const u8 *buf, size_t len);
426 void cfg80211_sme_disassoc(struct net_device *dev,
427 struct cfg80211_internal_bss *bss);
428 void __cfg80211_scan_done(struct work_struct *wk);
429 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak);
430 void __cfg80211_sched_scan_results(struct work_struct *wk);
431 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
432 bool driver_initiated);
433 void cfg80211_upload_connect_keys(struct wireless_dev *wdev);
434 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
435 struct net_device *dev, enum nl80211_iftype ntype,
436 u32 *flags, struct vif_params *params);
437 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev);
438 void cfg80211_process_wdev_events(struct wireless_dev *wdev);
439
440 int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
441 struct wireless_dev *wdev,
442 enum nl80211_iftype iftype,
443 struct ieee80211_channel *chan,
444 enum cfg80211_chan_mode chanmode,
445 u8 radar_detect);
446
447 /**
448 * cfg80211_chandef_dfs_required - checks if radar detection is required
449 * @wiphy: the wiphy to validate against
450 * @chandef: the channel definition to check
451 * Return: 1 if radar detection is required, 0 if it is not, < 0 on error
452 */
453 int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
454 const struct cfg80211_chan_def *c);
455
456 void cfg80211_set_dfs_state(struct wiphy *wiphy,
457 const struct cfg80211_chan_def *chandef,
458 enum nl80211_dfs_state dfs_state);
459
460 void cfg80211_dfs_channels_update_work(struct work_struct *work);
461
462
463 static inline int
cfg80211_can_change_interface(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,enum nl80211_iftype iftype)464 cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
465 struct wireless_dev *wdev,
466 enum nl80211_iftype iftype)
467 {
468 return cfg80211_can_use_iftype_chan(rdev, wdev, iftype, NULL,
469 CHAN_MODE_UNDEFINED, 0);
470 }
471
472 static inline int
cfg80211_can_add_interface(struct cfg80211_registered_device * rdev,enum nl80211_iftype iftype)473 cfg80211_can_add_interface(struct cfg80211_registered_device *rdev,
474 enum nl80211_iftype iftype)
475 {
476 return cfg80211_can_change_interface(rdev, NULL, iftype);
477 }
478
479 static inline int
cfg80211_can_use_chan(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,struct ieee80211_channel * chan,enum cfg80211_chan_mode chanmode)480 cfg80211_can_use_chan(struct cfg80211_registered_device *rdev,
481 struct wireless_dev *wdev,
482 struct ieee80211_channel *chan,
483 enum cfg80211_chan_mode chanmode)
484 {
485 return cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
486 chan, chanmode, 0);
487 }
488
elapsed_jiffies_msecs(unsigned long start)489 static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
490 {
491 unsigned long end = jiffies;
492
493 if (end >= start)
494 return jiffies_to_msecs(end - start);
495
496 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
497 }
498
499 void
500 cfg80211_get_chan_state(struct wireless_dev *wdev,
501 struct ieee80211_channel **chan,
502 enum cfg80211_chan_mode *chanmode);
503
504 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
505 struct cfg80211_chan_def *chandef);
506
507 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
508 const u8 *rates, unsigned int n_rates,
509 u32 *mask);
510
511 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
512 u32 beacon_int);
513
514 void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
515 enum nl80211_iftype iftype, int num);
516
517 void cfg80211_leave(struct cfg80211_registered_device *rdev,
518 struct wireless_dev *wdev);
519
520 void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
521 struct wireless_dev *wdev);
522
523 #define CFG80211_MAX_NUM_DIFFERENT_CHANNELS 10
524
525 #ifdef CONFIG_CFG80211_DEVELOPER_WARNINGS
526 #define CFG80211_DEV_WARN_ON(cond) WARN_ON(cond)
527 #else
528 /*
529 * Trick to enable using it as a condition,
530 * and also not give a warning when it's
531 * not used that way.
532 */
533 #define CFG80211_DEV_WARN_ON(cond) ({bool __r = (cond); __r; })
534 #endif
535
536 #endif /* __NET_WIRELESS_CORE_H */
537