core.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 <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. /* protected by devlist_mtx or RCU */
  46. struct list_head netdev_list;
  47. int devlist_generation;
  48. int opencount; /* also protected by devlist_mtx */
  49. wait_queue_head_t dev_wait;
  50. /* BSSes/scanning */
  51. spinlock_t bss_lock;
  52. struct list_head bss_list;
  53. struct rb_root bss_tree;
  54. u32 bss_generation;
  55. struct cfg80211_scan_request *scan_req; /* protected by RTNL */
  56. unsigned long suspend_at;
  57. struct work_struct scan_done_wk;
  58. #ifdef CONFIG_NL80211_TESTMODE
  59. struct genl_info *testmode_info;
  60. #endif
  61. struct work_struct conn_work;
  62. struct work_struct event_work;
  63. /* must be last because of the way we do wiphy_priv(),
  64. * and it should at least be aligned to NETDEV_ALIGN */
  65. struct wiphy wiphy __attribute__((__aligned__(NETDEV_ALIGN)));
  66. };
  67. static inline
  68. struct cfg80211_registered_device *wiphy_to_dev(struct wiphy *wiphy)
  69. {
  70. BUG_ON(!wiphy);
  71. return container_of(wiphy, struct cfg80211_registered_device, wiphy);
  72. }
  73. /* Note 0 is valid, hence phy0 */
  74. static inline
  75. bool wiphy_idx_valid(int wiphy_idx)
  76. {
  77. return (wiphy_idx >= 0);
  78. }
  79. extern struct workqueue_struct *cfg80211_wq;
  80. extern struct mutex cfg80211_mutex;
  81. extern struct list_head cfg80211_rdev_list;
  82. extern int cfg80211_rdev_list_generation;
  83. #define assert_cfg80211_lock() WARN_ON(!mutex_is_locked(&cfg80211_mutex))
  84. /*
  85. * You can use this to mark a wiphy_idx as not having an associated wiphy.
  86. * It guarantees cfg80211_rdev_by_wiphy_idx(wiphy_idx) will return NULL
  87. */
  88. #define WIPHY_IDX_STALE -1
  89. struct cfg80211_internal_bss {
  90. struct list_head list;
  91. struct rb_node rbn;
  92. unsigned long ts;
  93. struct kref ref;
  94. atomic_t hold;
  95. bool beacon_ies_allocated;
  96. bool proberesp_ies_allocated;
  97. /* must be last because of priv member */
  98. struct cfg80211_bss pub;
  99. };
  100. static inline struct cfg80211_internal_bss *bss_from_pub(struct cfg80211_bss *pub)
  101. {
  102. return container_of(pub, struct cfg80211_internal_bss, pub);
  103. }
  104. static inline void cfg80211_ref_bss(struct cfg80211_internal_bss *bss)
  105. {
  106. kref_get(&bss->ref);
  107. }
  108. static inline void cfg80211_hold_bss(struct cfg80211_internal_bss *bss)
  109. {
  110. atomic_inc(&bss->hold);
  111. }
  112. static inline void cfg80211_unhold_bss(struct cfg80211_internal_bss *bss)
  113. {
  114. int r = atomic_dec_return(&bss->hold);
  115. WARN_ON(r < 0);
  116. }
  117. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx);
  118. int get_wiphy_idx(struct wiphy *wiphy);
  119. struct cfg80211_registered_device *
  120. __cfg80211_rdev_from_info(struct genl_info *info);
  121. /*
  122. * This function returns a pointer to the driver
  123. * that the genl_info item that is passed refers to.
  124. * If successful, it returns non-NULL and also locks
  125. * the driver's mutex!
  126. *
  127. * This means that you need to call cfg80211_unlock_rdev()
  128. * before being allowed to acquire &cfg80211_mutex!
  129. *
  130. * This is necessary because we need to lock the global
  131. * mutex to get an item off the list safely, and then
  132. * we lock the rdev mutex so it doesn't go away under us.
  133. *
  134. * We don't want to keep cfg80211_mutex locked
  135. * for all the time in order to allow requests on
  136. * other interfaces to go through at the same time.
  137. *
  138. * The result of this can be a PTR_ERR and hence must
  139. * be checked with IS_ERR() for errors.
  140. */
  141. extern struct cfg80211_registered_device *
  142. cfg80211_get_dev_from_info(struct genl_info *info);
  143. /* requires cfg80211_rdev_mutex to be held! */
  144. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx);
  145. /* identical to cfg80211_get_dev_from_info but only operate on ifindex */
  146. extern struct cfg80211_registered_device *
  147. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex);
  148. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  149. struct net *net);
  150. static inline void cfg80211_lock_rdev(struct cfg80211_registered_device *rdev)
  151. {
  152. mutex_lock(&rdev->mtx);
  153. }
  154. static inline void cfg80211_unlock_rdev(struct cfg80211_registered_device *rdev)
  155. {
  156. BUG_ON(IS_ERR(rdev) || !rdev);
  157. mutex_unlock(&rdev->mtx);
  158. }
  159. static inline void wdev_lock(struct wireless_dev *wdev)
  160. __acquires(wdev)
  161. {
  162. mutex_lock(&wdev->mtx);
  163. __acquire(wdev->mtx);
  164. }
  165. static inline void wdev_unlock(struct wireless_dev *wdev)
  166. __releases(wdev)
  167. {
  168. __release(wdev->mtx);
  169. mutex_unlock(&wdev->mtx);
  170. }
  171. #define ASSERT_RDEV_LOCK(rdev) WARN_ON(!mutex_is_locked(&(rdev)->mtx));
  172. #define ASSERT_WDEV_LOCK(wdev) WARN_ON(!mutex_is_locked(&(wdev)->mtx));
  173. enum cfg80211_event_type {
  174. EVENT_CONNECT_RESULT,
  175. EVENT_ROAMED,
  176. EVENT_DISCONNECTED,
  177. EVENT_IBSS_JOINED,
  178. };
  179. struct cfg80211_event {
  180. struct list_head list;
  181. enum cfg80211_event_type type;
  182. union {
  183. struct {
  184. u8 bssid[ETH_ALEN];
  185. const u8 *req_ie;
  186. const u8 *resp_ie;
  187. size_t req_ie_len;
  188. size_t resp_ie_len;
  189. u16 status;
  190. } cr;
  191. struct {
  192. u8 bssid[ETH_ALEN];
  193. const u8 *req_ie;
  194. const u8 *resp_ie;
  195. size_t req_ie_len;
  196. size_t resp_ie_len;
  197. } rm;
  198. struct {
  199. const u8 *ie;
  200. size_t ie_len;
  201. u16 reason;
  202. } dc;
  203. struct {
  204. u8 bssid[ETH_ALEN];
  205. } ij;
  206. };
  207. };
  208. struct cfg80211_cached_keys {
  209. struct key_params params[6];
  210. u8 data[6][WLAN_MAX_KEY_LEN];
  211. int def, defmgmt;
  212. };
  213. /* free object */
  214. extern void cfg80211_dev_free(struct cfg80211_registered_device *rdev);
  215. extern int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  216. char *newname);
  217. void ieee80211_set_bitrate_flags(struct wiphy *wiphy);
  218. void wiphy_update_regulatory(struct wiphy *wiphy,
  219. enum nl80211_reg_initiator setby);
  220. void cfg80211_bss_expire(struct cfg80211_registered_device *dev);
  221. void cfg80211_bss_age(struct cfg80211_registered_device *dev,
  222. unsigned long age_secs);
  223. /* IBSS */
  224. int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  225. struct net_device *dev,
  226. struct cfg80211_ibss_params *params,
  227. struct cfg80211_cached_keys *connkeys);
  228. int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  229. struct net_device *dev,
  230. struct cfg80211_ibss_params *params,
  231. struct cfg80211_cached_keys *connkeys);
  232. void cfg80211_clear_ibss(struct net_device *dev, bool nowext);
  233. int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  234. struct net_device *dev, bool nowext);
  235. int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  236. struct net_device *dev, bool nowext);
  237. void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid);
  238. int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
  239. struct wireless_dev *wdev);
  240. /* MLME */
  241. int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  242. struct net_device *dev,
  243. struct ieee80211_channel *chan,
  244. enum nl80211_auth_type auth_type,
  245. const u8 *bssid,
  246. const u8 *ssid, int ssid_len,
  247. const u8 *ie, int ie_len,
  248. const u8 *key, int key_len, int key_idx,
  249. bool local_state_change);
  250. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  251. struct net_device *dev, struct ieee80211_channel *chan,
  252. enum nl80211_auth_type auth_type, 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. bool local_state_change);
  257. int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  258. struct net_device *dev,
  259. struct ieee80211_channel *chan,
  260. const u8 *bssid, const u8 *prev_bssid,
  261. const u8 *ssid, int ssid_len,
  262. const u8 *ie, int ie_len, bool use_mfp,
  263. struct cfg80211_crypto_settings *crypt);
  264. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  265. struct net_device *dev, struct ieee80211_channel *chan,
  266. const u8 *bssid, const u8 *prev_bssid,
  267. const u8 *ssid, int ssid_len,
  268. const u8 *ie, int ie_len, bool use_mfp,
  269. struct cfg80211_crypto_settings *crypt);
  270. int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  271. struct net_device *dev, const u8 *bssid,
  272. const u8 *ie, int ie_len, u16 reason,
  273. bool local_state_change);
  274. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  275. struct net_device *dev, const u8 *bssid,
  276. const u8 *ie, int ie_len, u16 reason,
  277. bool local_state_change);
  278. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  279. struct net_device *dev, const u8 *bssid,
  280. const u8 *ie, int ie_len, u16 reason,
  281. bool local_state_change);
  282. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  283. struct net_device *dev);
  284. void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
  285. const u8 *req_ie, size_t req_ie_len,
  286. const u8 *resp_ie, size_t resp_ie_len,
  287. u16 status, bool wextev,
  288. struct cfg80211_bss *bss);
  289. int cfg80211_mlme_register_action(struct wireless_dev *wdev, u32 snd_pid,
  290. const u8 *match_data, int match_len);
  291. void cfg80211_mlme_unregister_actions(struct wireless_dev *wdev, u32 nlpid);
  292. void cfg80211_mlme_purge_actions(struct wireless_dev *wdev);
  293. int cfg80211_mlme_action(struct cfg80211_registered_device *rdev,
  294. struct net_device *dev,
  295. struct ieee80211_channel *chan,
  296. enum nl80211_channel_type channel_type,
  297. bool channel_type_valid,
  298. const u8 *buf, size_t len, u64 *cookie);
  299. /* SME */
  300. int __cfg80211_connect(struct cfg80211_registered_device *rdev,
  301. struct net_device *dev,
  302. struct cfg80211_connect_params *connect,
  303. struct cfg80211_cached_keys *connkeys,
  304. const u8 *prev_bssid);
  305. int cfg80211_connect(struct cfg80211_registered_device *rdev,
  306. struct net_device *dev,
  307. struct cfg80211_connect_params *connect,
  308. struct cfg80211_cached_keys *connkeys);
  309. int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  310. struct net_device *dev, u16 reason,
  311. bool wextev);
  312. int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  313. struct net_device *dev, u16 reason,
  314. bool wextev);
  315. void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
  316. const u8 *req_ie, size_t req_ie_len,
  317. const u8 *resp_ie, size_t resp_ie_len);
  318. int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
  319. struct wireless_dev *wdev);
  320. void cfg80211_conn_work(struct work_struct *work);
  321. void cfg80211_sme_failed_assoc(struct wireless_dev *wdev);
  322. bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev);
  323. /* internal helpers */
  324. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  325. struct key_params *params, int key_idx,
  326. const u8 *mac_addr);
  327. void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
  328. size_t ie_len, u16 reason, bool from_ap);
  329. void cfg80211_sme_scan_done(struct net_device *dev);
  330. void cfg80211_sme_rx_auth(struct net_device *dev, const u8 *buf, size_t len);
  331. void cfg80211_sme_disassoc(struct net_device *dev, int idx);
  332. void __cfg80211_scan_done(struct work_struct *wk);
  333. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak);
  334. void cfg80211_upload_connect_keys(struct wireless_dev *wdev);
  335. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  336. struct net_device *dev, enum nl80211_iftype ntype,
  337. u32 *flags, struct vif_params *params);
  338. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev);
  339. struct ieee80211_channel *
  340. rdev_freq_to_chan(struct cfg80211_registered_device *rdev,
  341. int freq, enum nl80211_channel_type channel_type);
  342. int cfg80211_set_freq(struct cfg80211_registered_device *rdev,
  343. struct wireless_dev *wdev, int freq,
  344. enum nl80211_channel_type channel_type);
  345. u16 cfg80211_calculate_bitrate(struct rate_info *rate);
  346. #ifdef CONFIG_CFG80211_DEVELOPER_WARNINGS
  347. #define CFG80211_DEV_WARN_ON(cond) WARN_ON(cond)
  348. #else
  349. /*
  350. * Trick to enable using it as a condition,
  351. * and also not give a warning when it's
  352. * not used that way.
  353. */
  354. #define CFG80211_DEV_WARN_ON(cond) ({bool __r = (cond); __r; })
  355. #endif
  356. #endif /* __NET_WIRELESS_CORE_H */