core.h 15 KB

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