core.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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_nlpid;
  52. /* protected by RTNL only */
  53. int num_running_ifaces;
  54. int num_running_monitor_ifaces;
  55. struct ieee80211_channel *monitor_channel;
  56. enum nl80211_channel_type monitor_channel_type;
  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 __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. static inline void
  91. cfg80211_rdev_free_wowlan(struct cfg80211_registered_device *rdev)
  92. {
  93. int i;
  94. if (!rdev->wowlan)
  95. return;
  96. for (i = 0; i < rdev->wowlan->n_patterns; i++)
  97. kfree(rdev->wowlan->patterns[i].mask);
  98. kfree(rdev->wowlan->patterns);
  99. kfree(rdev->wowlan);
  100. }
  101. extern struct workqueue_struct *cfg80211_wq;
  102. extern struct mutex cfg80211_mutex;
  103. extern struct list_head cfg80211_rdev_list;
  104. extern int cfg80211_rdev_list_generation;
  105. static inline void assert_cfg80211_lock(void)
  106. {
  107. lockdep_assert_held(&cfg80211_mutex);
  108. }
  109. /*
  110. * You can use this to mark a wiphy_idx as not having an associated wiphy.
  111. * It guarantees cfg80211_rdev_by_wiphy_idx(wiphy_idx) will return NULL
  112. */
  113. #define WIPHY_IDX_STALE -1
  114. struct cfg80211_internal_bss {
  115. struct list_head list;
  116. struct rb_node rbn;
  117. unsigned long ts;
  118. struct kref ref;
  119. atomic_t hold;
  120. bool beacon_ies_allocated;
  121. bool proberesp_ies_allocated;
  122. /* must be last because of priv member */
  123. struct cfg80211_bss pub;
  124. };
  125. static inline struct cfg80211_internal_bss *bss_from_pub(struct cfg80211_bss *pub)
  126. {
  127. return container_of(pub, struct cfg80211_internal_bss, pub);
  128. }
  129. static inline void cfg80211_hold_bss(struct cfg80211_internal_bss *bss)
  130. {
  131. atomic_inc(&bss->hold);
  132. }
  133. static inline void cfg80211_unhold_bss(struct cfg80211_internal_bss *bss)
  134. {
  135. int r = atomic_dec_return(&bss->hold);
  136. WARN_ON(r < 0);
  137. }
  138. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx);
  139. int get_wiphy_idx(struct wiphy *wiphy);
  140. /* requires cfg80211_rdev_mutex to be held! */
  141. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx);
  142. /* identical to cfg80211_get_dev_from_info but only operate on ifindex */
  143. extern struct cfg80211_registered_device *
  144. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex);
  145. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  146. struct net *net);
  147. static inline void cfg80211_lock_rdev(struct cfg80211_registered_device *rdev)
  148. {
  149. mutex_lock(&rdev->mtx);
  150. }
  151. static inline void cfg80211_unlock_rdev(struct cfg80211_registered_device *rdev)
  152. {
  153. BUG_ON(IS_ERR(rdev) || !rdev);
  154. mutex_unlock(&rdev->mtx);
  155. }
  156. static inline void wdev_lock(struct wireless_dev *wdev)
  157. __acquires(wdev)
  158. {
  159. mutex_lock(&wdev->mtx);
  160. __acquire(wdev->mtx);
  161. }
  162. static inline void wdev_unlock(struct wireless_dev *wdev)
  163. __releases(wdev)
  164. {
  165. __release(wdev->mtx);
  166. mutex_unlock(&wdev->mtx);
  167. }
  168. #define ASSERT_RDEV_LOCK(rdev) lockdep_assert_held(&(rdev)->mtx)
  169. #define ASSERT_WDEV_LOCK(wdev) lockdep_assert_held(&(wdev)->mtx)
  170. static inline bool cfg80211_has_monitors_only(struct cfg80211_registered_device *rdev)
  171. {
  172. ASSERT_RTNL();
  173. return rdev->num_running_ifaces == rdev->num_running_monitor_ifaces &&
  174. rdev->num_running_ifaces > 0;
  175. }
  176. enum cfg80211_event_type {
  177. EVENT_CONNECT_RESULT,
  178. EVENT_ROAMED,
  179. EVENT_DISCONNECTED,
  180. EVENT_IBSS_JOINED,
  181. };
  182. struct cfg80211_event {
  183. struct list_head list;
  184. enum cfg80211_event_type type;
  185. union {
  186. struct {
  187. u8 bssid[ETH_ALEN];
  188. const u8 *req_ie;
  189. const u8 *resp_ie;
  190. size_t req_ie_len;
  191. size_t resp_ie_len;
  192. u16 status;
  193. } cr;
  194. struct {
  195. const u8 *req_ie;
  196. const u8 *resp_ie;
  197. size_t req_ie_len;
  198. size_t resp_ie_len;
  199. struct cfg80211_bss *bss;
  200. } rm;
  201. struct {
  202. const u8 *ie;
  203. size_t ie_len;
  204. u16 reason;
  205. } dc;
  206. struct {
  207. u8 bssid[ETH_ALEN];
  208. } ij;
  209. };
  210. };
  211. struct cfg80211_cached_keys {
  212. struct key_params params[6];
  213. u8 data[6][WLAN_MAX_KEY_LEN];
  214. int def, defmgmt;
  215. };
  216. enum cfg80211_chan_mode {
  217. CHAN_MODE_UNDEFINED,
  218. CHAN_MODE_SHARED,
  219. CHAN_MODE_EXCLUSIVE,
  220. };
  221. /* free object */
  222. extern void cfg80211_dev_free(struct cfg80211_registered_device *rdev);
  223. extern int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  224. char *newname);
  225. void ieee80211_set_bitrate_flags(struct wiphy *wiphy);
  226. void cfg80211_bss_expire(struct cfg80211_registered_device *dev);
  227. void cfg80211_bss_age(struct cfg80211_registered_device *dev,
  228. unsigned long age_secs);
  229. /* IBSS */
  230. int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  231. struct net_device *dev,
  232. struct cfg80211_ibss_params *params,
  233. struct cfg80211_cached_keys *connkeys);
  234. int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  235. struct net_device *dev,
  236. struct cfg80211_ibss_params *params,
  237. struct cfg80211_cached_keys *connkeys);
  238. void cfg80211_clear_ibss(struct net_device *dev, bool nowext);
  239. int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  240. struct net_device *dev, bool nowext);
  241. int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  242. struct net_device *dev, bool nowext);
  243. void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid);
  244. int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
  245. struct wireless_dev *wdev);
  246. /* mesh */
  247. extern const struct mesh_config default_mesh_config;
  248. extern const struct mesh_setup default_mesh_setup;
  249. int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  250. struct net_device *dev,
  251. struct mesh_setup *setup,
  252. const struct mesh_config *conf);
  253. int cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  254. struct net_device *dev,
  255. struct mesh_setup *setup,
  256. const struct mesh_config *conf);
  257. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  258. struct net_device *dev);
  259. int cfg80211_set_mesh_freq(struct cfg80211_registered_device *rdev,
  260. struct wireless_dev *wdev, int freq,
  261. enum nl80211_channel_type channel_type);
  262. /* AP */
  263. int cfg80211_stop_ap(struct cfg80211_registered_device *rdev,
  264. struct net_device *dev);
  265. /* MLME */
  266. int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  267. struct net_device *dev,
  268. struct ieee80211_channel *chan,
  269. enum nl80211_auth_type auth_type,
  270. const u8 *bssid,
  271. const u8 *ssid, int ssid_len,
  272. const u8 *ie, int ie_len,
  273. const u8 *key, int key_len, int key_idx);
  274. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  275. struct net_device *dev, struct ieee80211_channel *chan,
  276. enum nl80211_auth_type auth_type, 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. int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  281. struct net_device *dev,
  282. struct ieee80211_channel *chan,
  283. const u8 *bssid, const u8 *prev_bssid,
  284. const u8 *ssid, int ssid_len,
  285. const u8 *ie, int ie_len, bool use_mfp,
  286. struct cfg80211_crypto_settings *crypt,
  287. u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
  288. struct ieee80211_ht_cap *ht_capa_mask);
  289. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  290. struct net_device *dev, 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. u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
  296. struct ieee80211_ht_cap *ht_capa_mask);
  297. int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  298. struct net_device *dev, const u8 *bssid,
  299. const u8 *ie, int ie_len, u16 reason,
  300. bool local_state_change);
  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_disassoc(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. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  310. struct net_device *dev);
  311. void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
  312. const u8 *req_ie, size_t req_ie_len,
  313. const u8 *resp_ie, size_t resp_ie_len,
  314. u16 status, bool wextev,
  315. struct cfg80211_bss *bss);
  316. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
  317. u16 frame_type, const u8 *match_data,
  318. int match_len);
  319. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid);
  320. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev);
  321. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  322. struct wireless_dev *wdev,
  323. struct ieee80211_channel *chan, bool offchan,
  324. enum nl80211_channel_type channel_type,
  325. bool channel_type_valid, unsigned int wait,
  326. const u8 *buf, size_t len, bool no_cck,
  327. bool dont_wait_for_ack, u64 *cookie);
  328. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  329. const struct ieee80211_ht_cap *ht_capa_mask);
  330. /* SME */
  331. int __cfg80211_connect(struct cfg80211_registered_device *rdev,
  332. struct net_device *dev,
  333. struct cfg80211_connect_params *connect,
  334. struct cfg80211_cached_keys *connkeys,
  335. const u8 *prev_bssid);
  336. int cfg80211_connect(struct cfg80211_registered_device *rdev,
  337. struct net_device *dev,
  338. struct cfg80211_connect_params *connect,
  339. struct cfg80211_cached_keys *connkeys);
  340. int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  341. struct net_device *dev, u16 reason,
  342. bool wextev);
  343. int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  344. struct net_device *dev, u16 reason,
  345. bool wextev);
  346. void __cfg80211_roamed(struct wireless_dev *wdev,
  347. struct cfg80211_bss *bss,
  348. const u8 *req_ie, size_t req_ie_len,
  349. const u8 *resp_ie, size_t resp_ie_len);
  350. int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
  351. struct wireless_dev *wdev);
  352. void cfg80211_conn_work(struct work_struct *work);
  353. void cfg80211_sme_failed_assoc(struct wireless_dev *wdev);
  354. bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev);
  355. /* internal helpers */
  356. bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher);
  357. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  358. struct key_params *params, int key_idx,
  359. bool pairwise, const u8 *mac_addr);
  360. void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
  361. size_t ie_len, u16 reason, bool from_ap);
  362. void cfg80211_sme_scan_done(struct net_device *dev);
  363. void cfg80211_sme_rx_auth(struct net_device *dev, const u8 *buf, size_t len);
  364. void cfg80211_sme_disassoc(struct net_device *dev,
  365. struct cfg80211_internal_bss *bss);
  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_use_iftype_chan(struct cfg80211_registered_device *rdev,
  377. struct wireless_dev *wdev,
  378. enum nl80211_iftype iftype,
  379. struct ieee80211_channel *chan,
  380. enum cfg80211_chan_mode chanmode);
  381. static inline int
  382. cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
  383. struct wireless_dev *wdev,
  384. enum nl80211_iftype iftype)
  385. {
  386. return cfg80211_can_use_iftype_chan(rdev, wdev, iftype, NULL,
  387. CHAN_MODE_UNDEFINED);
  388. }
  389. static inline int
  390. cfg80211_can_add_interface(struct cfg80211_registered_device *rdev,
  391. enum nl80211_iftype iftype)
  392. {
  393. return cfg80211_can_change_interface(rdev, NULL, iftype);
  394. }
  395. static inline int
  396. cfg80211_can_use_chan(struct cfg80211_registered_device *rdev,
  397. struct wireless_dev *wdev,
  398. struct ieee80211_channel *chan,
  399. enum cfg80211_chan_mode chanmode)
  400. {
  401. return cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
  402. chan, chanmode);
  403. }
  404. void
  405. cfg80211_get_chan_state(struct wireless_dev *wdev,
  406. struct ieee80211_channel **chan,
  407. enum cfg80211_chan_mode *chanmode);
  408. struct ieee80211_channel *
  409. rdev_freq_to_chan(struct cfg80211_registered_device *rdev,
  410. int freq, enum nl80211_channel_type channel_type);
  411. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  412. int freq, enum nl80211_channel_type chantype);
  413. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  414. const u8 *rates, unsigned int n_rates,
  415. u32 *mask);
  416. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  417. u32 beacon_int);
  418. void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
  419. enum nl80211_iftype iftype, int num);
  420. #define CFG80211_MAX_NUM_DIFFERENT_CHANNELS 10
  421. #ifdef CONFIG_CFG80211_DEVELOPER_WARNINGS
  422. #define CFG80211_DEV_WARN_ON(cond) WARN_ON(cond)
  423. #else
  424. /*
  425. * Trick to enable using it as a condition,
  426. * and also not give a warning when it's
  427. * not used that way.
  428. */
  429. #define CFG80211_DEV_WARN_ON(cond) ({bool __r = (cond); __r; })
  430. #endif
  431. #endif /* __NET_WIRELESS_CORE_H */