core.h 15 KB

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