core.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Wireless configuration interface internals.
  3. *
  4. * Copyright 2006-2009 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 <net/genetlink.h>
  17. #include <net/cfg80211.h>
  18. #include "reg.h"
  19. struct cfg80211_registered_device {
  20. const struct cfg80211_ops *ops;
  21. struct list_head list;
  22. /* we hold this mutex during any call so that
  23. * we cannot do multiple calls at once, and also
  24. * to avoid the deregister call to proceed while
  25. * any call is in progress */
  26. struct mutex mtx;
  27. /* rfkill support */
  28. struct rfkill_ops rfkill_ops;
  29. struct rfkill *rfkill;
  30. struct work_struct rfkill_sync;
  31. /* ISO / IEC 3166 alpha2 for which this device is receiving
  32. * country IEs on, this can help disregard country IEs from APs
  33. * on the same alpha2 quickly. The alpha2 may differ from
  34. * cfg80211_regdomain's alpha2 when an intersection has occurred.
  35. * If the AP is reconfigured this can also be used to tell us if
  36. * the country on the country IE changed. */
  37. char country_ie_alpha2[2];
  38. /* If a Country IE has been received this tells us the environment
  39. * which its telling us its in. This defaults to ENVIRON_ANY */
  40. enum environment_cap env;
  41. /* wiphy index, internal only */
  42. int wiphy_idx;
  43. /* associate netdev list */
  44. struct mutex devlist_mtx;
  45. struct list_head netdev_list;
  46. int devlist_generation;
  47. int opencount; /* also protected by devlist_mtx */
  48. wait_queue_head_t dev_wait;
  49. /* BSSes/scanning */
  50. spinlock_t bss_lock;
  51. struct list_head bss_list;
  52. struct rb_root bss_tree;
  53. u32 bss_generation;
  54. struct cfg80211_scan_request *scan_req; /* protected by RTNL */
  55. unsigned long suspend_at;
  56. struct work_struct scan_done_wk;
  57. #ifdef CONFIG_NL80211_TESTMODE
  58. struct genl_info *testmode_info;
  59. #endif
  60. struct work_struct conn_work;
  61. struct work_struct event_work;
  62. /* current channel */
  63. struct ieee80211_channel *channel;
  64. #ifdef CONFIG_CFG80211_DEBUGFS
  65. /* Debugfs entries */
  66. struct wiphy_debugfsdentries {
  67. struct dentry *rts_threshold;
  68. struct dentry *fragmentation_threshold;
  69. struct dentry *short_retry_limit;
  70. struct dentry *long_retry_limit;
  71. struct dentry *ht40allow_map;
  72. } debugfs;
  73. #endif
  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 __attribute__((__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. /* Note 0 is valid, hence phy0 */
  85. static inline
  86. bool wiphy_idx_valid(int wiphy_idx)
  87. {
  88. return (wiphy_idx >= 0);
  89. }
  90. extern struct mutex cfg80211_mutex;
  91. extern struct list_head cfg80211_rdev_list;
  92. extern int cfg80211_rdev_list_generation;
  93. #define assert_cfg80211_lock() WARN_ON(!mutex_is_locked(&cfg80211_mutex))
  94. /*
  95. * You can use this to mark a wiphy_idx as not having an associated wiphy.
  96. * It guarantees cfg80211_rdev_by_wiphy_idx(wiphy_idx) will return NULL
  97. */
  98. #define WIPHY_IDX_STALE -1
  99. struct cfg80211_internal_bss {
  100. struct list_head list;
  101. struct rb_node rbn;
  102. unsigned long ts;
  103. struct kref ref;
  104. atomic_t hold;
  105. bool ies_allocated;
  106. /* must be last because of priv member */
  107. struct cfg80211_bss pub;
  108. };
  109. static inline struct cfg80211_internal_bss *bss_from_pub(struct cfg80211_bss *pub)
  110. {
  111. return container_of(pub, struct cfg80211_internal_bss, pub);
  112. }
  113. static inline void cfg80211_ref_bss(struct cfg80211_internal_bss *bss)
  114. {
  115. kref_get(&bss->ref);
  116. }
  117. static inline void cfg80211_hold_bss(struct cfg80211_internal_bss *bss)
  118. {
  119. atomic_inc(&bss->hold);
  120. }
  121. static inline void cfg80211_unhold_bss(struct cfg80211_internal_bss *bss)
  122. {
  123. int r = atomic_dec_return(&bss->hold);
  124. WARN_ON(r < 0);
  125. }
  126. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx);
  127. int get_wiphy_idx(struct wiphy *wiphy);
  128. struct cfg80211_registered_device *
  129. __cfg80211_rdev_from_info(struct genl_info *info);
  130. /*
  131. * This function returns a pointer to the driver
  132. * that the genl_info item that is passed refers to.
  133. * If successful, it returns non-NULL and also locks
  134. * the driver's mutex!
  135. *
  136. * This means that you need to call cfg80211_unlock_rdev()
  137. * before being allowed to acquire &cfg80211_mutex!
  138. *
  139. * This is necessary because we need to lock the global
  140. * mutex to get an item off the list safely, and then
  141. * we lock the rdev mutex so it doesn't go away under us.
  142. *
  143. * We don't want to keep cfg80211_mutex locked
  144. * for all the time in order to allow requests on
  145. * other interfaces to go through at the same time.
  146. *
  147. * The result of this can be a PTR_ERR and hence must
  148. * be checked with IS_ERR() for errors.
  149. */
  150. extern struct cfg80211_registered_device *
  151. cfg80211_get_dev_from_info(struct genl_info *info);
  152. /* requires cfg80211_rdev_mutex to be held! */
  153. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx);
  154. /* identical to cfg80211_get_dev_from_info but only operate on ifindex */
  155. extern struct cfg80211_registered_device *
  156. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex);
  157. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  158. struct net *net);
  159. static inline void cfg80211_lock_rdev(struct cfg80211_registered_device *rdev)
  160. {
  161. mutex_lock(&rdev->mtx);
  162. }
  163. static inline void cfg80211_unlock_rdev(struct cfg80211_registered_device *rdev)
  164. {
  165. BUG_ON(IS_ERR(rdev) || !rdev);
  166. mutex_unlock(&rdev->mtx);
  167. }
  168. static inline void wdev_lock(struct wireless_dev *wdev)
  169. __acquires(wdev)
  170. {
  171. mutex_lock(&wdev->mtx);
  172. __acquire(wdev->mtx);
  173. }
  174. static inline void wdev_unlock(struct wireless_dev *wdev)
  175. __releases(wdev)
  176. {
  177. __release(wdev->mtx);
  178. mutex_unlock(&wdev->mtx);
  179. }
  180. #define ASSERT_RDEV_LOCK(rdev) WARN_ON(!mutex_is_locked(&(rdev)->mtx));
  181. #define ASSERT_WDEV_LOCK(wdev) WARN_ON(!mutex_is_locked(&(wdev)->mtx));
  182. enum cfg80211_event_type {
  183. EVENT_CONNECT_RESULT,
  184. EVENT_ROAMED,
  185. EVENT_DISCONNECTED,
  186. EVENT_IBSS_JOINED,
  187. };
  188. struct cfg80211_event {
  189. struct list_head list;
  190. enum cfg80211_event_type type;
  191. union {
  192. struct {
  193. u8 bssid[ETH_ALEN];
  194. const u8 *req_ie;
  195. const u8 *resp_ie;
  196. size_t req_ie_len;
  197. size_t resp_ie_len;
  198. u16 status;
  199. } cr;
  200. struct {
  201. u8 bssid[ETH_ALEN];
  202. const u8 *req_ie;
  203. const u8 *resp_ie;
  204. size_t req_ie_len;
  205. size_t resp_ie_len;
  206. } rm;
  207. struct {
  208. const u8 *ie;
  209. size_t ie_len;
  210. u16 reason;
  211. } dc;
  212. struct {
  213. u8 bssid[ETH_ALEN];
  214. } ij;
  215. };
  216. };
  217. struct cfg80211_cached_keys {
  218. struct key_params params[6];
  219. u8 data[6][WLAN_MAX_KEY_LEN];
  220. int def, defmgmt;
  221. };
  222. /* free object */
  223. extern void cfg80211_dev_free(struct cfg80211_registered_device *rdev);
  224. extern int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  225. char *newname);
  226. void ieee80211_set_bitrate_flags(struct wiphy *wiphy);
  227. void wiphy_update_regulatory(struct wiphy *wiphy,
  228. enum nl80211_reg_initiator setby);
  229. void cfg80211_bss_expire(struct cfg80211_registered_device *dev);
  230. void cfg80211_bss_age(struct cfg80211_registered_device *dev,
  231. unsigned long age_secs);
  232. /* IBSS */
  233. int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  234. struct net_device *dev,
  235. struct cfg80211_ibss_params *params,
  236. struct cfg80211_cached_keys *connkeys);
  237. int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  238. struct net_device *dev,
  239. struct cfg80211_ibss_params *params,
  240. struct cfg80211_cached_keys *connkeys);
  241. void cfg80211_clear_ibss(struct net_device *dev, bool nowext);
  242. int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  243. struct net_device *dev, bool nowext);
  244. void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid);
  245. int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
  246. struct wireless_dev *wdev);
  247. /* MLME */
  248. int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  249. struct net_device *dev,
  250. struct ieee80211_channel *chan,
  251. enum nl80211_auth_type auth_type,
  252. const u8 *bssid,
  253. const u8 *ssid, int ssid_len,
  254. const u8 *ie, int ie_len,
  255. const u8 *key, int key_len, int key_idx);
  256. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  257. struct net_device *dev, struct ieee80211_channel *chan,
  258. enum nl80211_auth_type auth_type, const u8 *bssid,
  259. const u8 *ssid, int ssid_len,
  260. const u8 *ie, int ie_len,
  261. const u8 *key, int key_len, int key_idx);
  262. int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  263. struct net_device *dev,
  264. struct ieee80211_channel *chan,
  265. const u8 *bssid, const u8 *prev_bssid,
  266. const u8 *ssid, int ssid_len,
  267. const u8 *ie, int ie_len, bool use_mfp,
  268. struct cfg80211_crypto_settings *crypt);
  269. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  270. struct net_device *dev, struct ieee80211_channel *chan,
  271. const u8 *bssid, const u8 *prev_bssid,
  272. const u8 *ssid, int ssid_len,
  273. const u8 *ie, int ie_len, bool use_mfp,
  274. struct cfg80211_crypto_settings *crypt);
  275. int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  276. struct net_device *dev, const u8 *bssid,
  277. const u8 *ie, int ie_len, u16 reason);
  278. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  279. struct net_device *dev, const u8 *bssid,
  280. const u8 *ie, int ie_len, u16 reason);
  281. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  282. struct net_device *dev, const u8 *bssid,
  283. const u8 *ie, int ie_len, u16 reason);
  284. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  285. struct net_device *dev);
  286. void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
  287. const u8 *req_ie, size_t req_ie_len,
  288. const u8 *resp_ie, size_t resp_ie_len,
  289. u16 status, bool wextev,
  290. struct cfg80211_bss *bss);
  291. /* SME */
  292. int __cfg80211_connect(struct cfg80211_registered_device *rdev,
  293. struct net_device *dev,
  294. struct cfg80211_connect_params *connect,
  295. struct cfg80211_cached_keys *connkeys,
  296. const u8 *prev_bssid);
  297. int cfg80211_connect(struct cfg80211_registered_device *rdev,
  298. struct net_device *dev,
  299. struct cfg80211_connect_params *connect,
  300. struct cfg80211_cached_keys *connkeys);
  301. int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  302. struct net_device *dev, u16 reason,
  303. bool wextev);
  304. int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  305. struct net_device *dev, u16 reason,
  306. bool wextev);
  307. void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
  308. const u8 *req_ie, size_t req_ie_len,
  309. const u8 *resp_ie, size_t resp_ie_len);
  310. int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
  311. struct wireless_dev *wdev);
  312. void cfg80211_conn_work(struct work_struct *work);
  313. void cfg80211_sme_failed_assoc(struct wireless_dev *wdev);
  314. bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev);
  315. /* internal helpers */
  316. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  317. struct key_params *params, int key_idx,
  318. const u8 *mac_addr);
  319. void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
  320. size_t ie_len, u16 reason, bool from_ap);
  321. void cfg80211_sme_scan_done(struct net_device *dev);
  322. void cfg80211_sme_rx_auth(struct net_device *dev, const u8 *buf, size_t len);
  323. void cfg80211_sme_disassoc(struct net_device *dev, int idx);
  324. void __cfg80211_scan_done(struct work_struct *wk);
  325. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak);
  326. void cfg80211_upload_connect_keys(struct wireless_dev *wdev);
  327. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  328. struct net_device *dev, enum nl80211_iftype ntype,
  329. u32 *flags, struct vif_params *params);
  330. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev);
  331. struct ieee80211_channel *
  332. rdev_fixed_channel(struct cfg80211_registered_device *rdev,
  333. struct wireless_dev *for_wdev);
  334. int rdev_set_freq(struct cfg80211_registered_device *rdev,
  335. struct wireless_dev *for_wdev,
  336. int freq, enum nl80211_channel_type channel_type);
  337. #ifdef CONFIG_CFG80211_DEVELOPER_WARNINGS
  338. #define CFG80211_DEV_WARN_ON(cond) WARN_ON(cond)
  339. #else
  340. /*
  341. * Trick to enable using it as a condition,
  342. * and also not give a warning when it's
  343. * not used that way.
  344. */
  345. #define CFG80211_DEV_WARN_ON(cond) ({bool __r = (cond); __r; })
  346. #endif
  347. #endif /* __NET_WIRELESS_CORE_H */