core.h 16 KB

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