core.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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/list.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/rbtree.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/rfkill.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/rtnetlink.h>
  15. #include <net/genetlink.h>
  16. #include <net/cfg80211.h>
  17. #include "reg.h"
  18. #define WIPHY_IDX_INVALID -1
  19. struct cfg80211_registered_device {
  20. const struct cfg80211_ops *ops;
  21. struct list_head list;
  22. /* rfkill support */
  23. struct rfkill_ops rfkill_ops;
  24. struct rfkill *rfkill;
  25. struct work_struct rfkill_sync;
  26. /* ISO / IEC 3166 alpha2 for which this device is receiving
  27. * country IEs on, this can help disregard country IEs from APs
  28. * on the same alpha2 quickly. The alpha2 may differ from
  29. * cfg80211_regdomain's alpha2 when an intersection has occurred.
  30. * If the AP is reconfigured this can also be used to tell us if
  31. * the country on the country IE changed. */
  32. char country_ie_alpha2[2];
  33. /* If a Country IE has been received this tells us the environment
  34. * which its telling us its in. This defaults to ENVIRON_ANY */
  35. enum environment_cap env;
  36. /* wiphy index, internal only */
  37. int wiphy_idx;
  38. /* associated wireless interfaces, protected by rtnl or RCU */
  39. struct list_head wdev_list;
  40. int devlist_generation, wdev_id;
  41. int opencount; /* also protected by devlist_mtx */
  42. wait_queue_head_t dev_wait;
  43. struct list_head beacon_registrations;
  44. spinlock_t beacon_registrations_lock;
  45. /* protected by RTNL only */
  46. int num_running_ifaces;
  47. int num_running_monitor_ifaces;
  48. /* BSSes/scanning */
  49. spinlock_t bss_lock;
  50. struct list_head bss_list;
  51. struct rb_root bss_tree;
  52. u32 bss_generation;
  53. struct cfg80211_scan_request *scan_req; /* protected by RTNL */
  54. struct cfg80211_sched_scan_request *sched_scan_req;
  55. unsigned long suspend_at;
  56. struct work_struct scan_done_wk;
  57. struct work_struct sched_scan_results_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. struct cfg80211_wowlan *wowlan;
  64. struct delayed_work dfs_update_channels_wk;
  65. /* netlink port which started critical protocol (0 means not started) */
  66. u32 crit_proto_nlportid;
  67. /* must be last because of the way we do wiphy_priv(),
  68. * and it should at least be aligned to NETDEV_ALIGN */
  69. struct wiphy wiphy __aligned(NETDEV_ALIGN);
  70. };
  71. static inline
  72. struct cfg80211_registered_device *wiphy_to_dev(struct wiphy *wiphy)
  73. {
  74. BUG_ON(!wiphy);
  75. return container_of(wiphy, struct cfg80211_registered_device, wiphy);
  76. }
  77. static inline void
  78. cfg80211_rdev_free_wowlan(struct cfg80211_registered_device *rdev)
  79. {
  80. int i;
  81. if (!rdev->wowlan)
  82. return;
  83. for (i = 0; i < rdev->wowlan->n_patterns; i++)
  84. kfree(rdev->wowlan->patterns[i].mask);
  85. kfree(rdev->wowlan->patterns);
  86. if (rdev->wowlan->tcp && rdev->wowlan->tcp->sock)
  87. sock_release(rdev->wowlan->tcp->sock);
  88. kfree(rdev->wowlan->tcp);
  89. kfree(rdev->wowlan);
  90. }
  91. extern struct workqueue_struct *cfg80211_wq;
  92. extern struct list_head cfg80211_rdev_list;
  93. extern int cfg80211_rdev_list_generation;
  94. struct cfg80211_internal_bss {
  95. struct list_head list;
  96. struct list_head hidden_list;
  97. struct rb_node rbn;
  98. unsigned long ts;
  99. unsigned long refcount;
  100. atomic_t hold;
  101. /* must be last because of priv member */
  102. struct cfg80211_bss pub;
  103. };
  104. static inline struct cfg80211_internal_bss *bss_from_pub(struct cfg80211_bss *pub)
  105. {
  106. return container_of(pub, struct cfg80211_internal_bss, pub);
  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 wiphy *wiphy_idx_to_wiphy(int wiphy_idx);
  120. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  121. struct net *net);
  122. static inline void wdev_lock(struct wireless_dev *wdev)
  123. __acquires(wdev)
  124. {
  125. mutex_lock(&wdev->mtx);
  126. __acquire(wdev->mtx);
  127. }
  128. static inline void wdev_unlock(struct wireless_dev *wdev)
  129. __releases(wdev)
  130. {
  131. __release(wdev->mtx);
  132. mutex_unlock(&wdev->mtx);
  133. }
  134. #define ASSERT_RDEV_LOCK(rdev) ASSERT_RTNL()
  135. #define ASSERT_WDEV_LOCK(wdev) lockdep_assert_held(&(wdev)->mtx)
  136. static inline bool cfg80211_has_monitors_only(struct cfg80211_registered_device *rdev)
  137. {
  138. ASSERT_RTNL();
  139. return rdev->num_running_ifaces == rdev->num_running_monitor_ifaces &&
  140. rdev->num_running_ifaces > 0;
  141. }
  142. enum cfg80211_event_type {
  143. EVENT_CONNECT_RESULT,
  144. EVENT_ROAMED,
  145. EVENT_DISCONNECTED,
  146. EVENT_IBSS_JOINED,
  147. };
  148. struct cfg80211_event {
  149. struct list_head list;
  150. enum cfg80211_event_type type;
  151. union {
  152. struct {
  153. u8 bssid[ETH_ALEN];
  154. const u8 *req_ie;
  155. const u8 *resp_ie;
  156. size_t req_ie_len;
  157. size_t resp_ie_len;
  158. u16 status;
  159. } cr;
  160. struct {
  161. const u8 *req_ie;
  162. const u8 *resp_ie;
  163. size_t req_ie_len;
  164. size_t resp_ie_len;
  165. struct cfg80211_bss *bss;
  166. } rm;
  167. struct {
  168. const u8 *ie;
  169. size_t ie_len;
  170. u16 reason;
  171. } dc;
  172. struct {
  173. u8 bssid[ETH_ALEN];
  174. } ij;
  175. };
  176. };
  177. struct cfg80211_cached_keys {
  178. struct key_params params[6];
  179. u8 data[6][WLAN_MAX_KEY_LEN];
  180. int def, defmgmt;
  181. };
  182. enum cfg80211_chan_mode {
  183. CHAN_MODE_UNDEFINED,
  184. CHAN_MODE_SHARED,
  185. CHAN_MODE_EXCLUSIVE,
  186. };
  187. struct cfg80211_beacon_registration {
  188. struct list_head list;
  189. u32 nlportid;
  190. };
  191. /* free object */
  192. extern void cfg80211_dev_free(struct cfg80211_registered_device *rdev);
  193. extern int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  194. char *newname);
  195. void ieee80211_set_bitrate_flags(struct wiphy *wiphy);
  196. void cfg80211_bss_expire(struct cfg80211_registered_device *dev);
  197. void cfg80211_bss_age(struct cfg80211_registered_device *dev,
  198. unsigned long age_secs);
  199. /* IBSS */
  200. int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  201. struct net_device *dev,
  202. struct cfg80211_ibss_params *params,
  203. struct cfg80211_cached_keys *connkeys);
  204. int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  205. struct net_device *dev,
  206. struct cfg80211_ibss_params *params,
  207. struct cfg80211_cached_keys *connkeys);
  208. void cfg80211_clear_ibss(struct net_device *dev, bool nowext);
  209. int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  210. struct net_device *dev, bool nowext);
  211. int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  212. struct net_device *dev, bool nowext);
  213. void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid);
  214. int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
  215. struct wireless_dev *wdev);
  216. /* mesh */
  217. extern const struct mesh_config default_mesh_config;
  218. extern const struct mesh_setup default_mesh_setup;
  219. int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  220. struct net_device *dev,
  221. struct mesh_setup *setup,
  222. const struct mesh_config *conf);
  223. int cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  224. struct net_device *dev,
  225. struct mesh_setup *setup,
  226. const struct mesh_config *conf);
  227. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  228. struct net_device *dev);
  229. int cfg80211_set_mesh_channel(struct cfg80211_registered_device *rdev,
  230. struct wireless_dev *wdev,
  231. struct cfg80211_chan_def *chandef);
  232. /* AP */
  233. int cfg80211_stop_ap(struct cfg80211_registered_device *rdev,
  234. struct net_device *dev);
  235. /* MLME */
  236. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  237. struct net_device *dev,
  238. struct ieee80211_channel *chan,
  239. enum nl80211_auth_type auth_type,
  240. const u8 *bssid,
  241. const u8 *ssid, int ssid_len,
  242. const u8 *ie, int ie_len,
  243. const u8 *key, int key_len, int key_idx,
  244. const u8 *sae_data, int sae_data_len);
  245. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  246. struct net_device *dev,
  247. struct ieee80211_channel *chan,
  248. const u8 *bssid,
  249. const u8 *ssid, int ssid_len,
  250. struct cfg80211_assoc_request *req);
  251. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  252. struct net_device *dev, const u8 *bssid,
  253. const u8 *ie, int ie_len, u16 reason,
  254. bool local_state_change);
  255. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  256. struct net_device *dev, const u8 *bssid,
  257. const u8 *ie, int ie_len, u16 reason,
  258. bool local_state_change);
  259. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  260. struct net_device *dev);
  261. void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
  262. const u8 *req_ie, size_t req_ie_len,
  263. const u8 *resp_ie, size_t resp_ie_len,
  264. u16 status, bool wextev,
  265. struct cfg80211_bss *bss);
  266. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
  267. u16 frame_type, const u8 *match_data,
  268. int match_len);
  269. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid);
  270. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev);
  271. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  272. struct wireless_dev *wdev,
  273. struct ieee80211_channel *chan, bool offchan,
  274. unsigned int wait, const u8 *buf, size_t len,
  275. bool no_cck, bool dont_wait_for_ack, u64 *cookie);
  276. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  277. const struct ieee80211_ht_cap *ht_capa_mask);
  278. void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
  279. const struct ieee80211_vht_cap *vht_capa_mask);
  280. /* SME */
  281. int __cfg80211_connect(struct cfg80211_registered_device *rdev,
  282. struct net_device *dev,
  283. struct cfg80211_connect_params *connect,
  284. struct cfg80211_cached_keys *connkeys,
  285. const u8 *prev_bssid);
  286. int cfg80211_connect(struct cfg80211_registered_device *rdev,
  287. struct net_device *dev,
  288. struct cfg80211_connect_params *connect,
  289. struct cfg80211_cached_keys *connkeys);
  290. int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  291. struct net_device *dev, u16 reason,
  292. bool wextev);
  293. int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  294. struct net_device *dev, u16 reason,
  295. bool wextev);
  296. void __cfg80211_roamed(struct wireless_dev *wdev,
  297. struct cfg80211_bss *bss,
  298. const u8 *req_ie, size_t req_ie_len,
  299. const u8 *resp_ie, size_t resp_ie_len);
  300. int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
  301. struct wireless_dev *wdev);
  302. void cfg80211_conn_work(struct work_struct *work);
  303. void cfg80211_sme_failed_assoc(struct wireless_dev *wdev);
  304. bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev);
  305. /* internal helpers */
  306. bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher);
  307. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  308. struct key_params *params, int key_idx,
  309. bool pairwise, const u8 *mac_addr);
  310. void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
  311. size_t ie_len, u16 reason, bool from_ap);
  312. void cfg80211_sme_scan_done(struct net_device *dev);
  313. void cfg80211_sme_rx_auth(struct net_device *dev, const u8 *buf, size_t len);
  314. void cfg80211_sme_disassoc(struct net_device *dev,
  315. struct cfg80211_internal_bss *bss);
  316. void __cfg80211_scan_done(struct work_struct *wk);
  317. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak);
  318. void __cfg80211_sched_scan_results(struct work_struct *wk);
  319. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  320. bool driver_initiated);
  321. void cfg80211_upload_connect_keys(struct wireless_dev *wdev);
  322. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  323. struct net_device *dev, enum nl80211_iftype ntype,
  324. u32 *flags, struct vif_params *params);
  325. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev);
  326. void cfg80211_process_wdev_events(struct wireless_dev *wdev);
  327. int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
  328. struct wireless_dev *wdev,
  329. enum nl80211_iftype iftype,
  330. struct ieee80211_channel *chan,
  331. enum cfg80211_chan_mode chanmode,
  332. u8 radar_detect);
  333. /**
  334. * cfg80211_chandef_dfs_required - checks if radar detection is required
  335. * @wiphy: the wiphy to validate against
  336. * @chandef: the channel definition to check
  337. * Return: 1 if radar detection is required, 0 if it is not, < 0 on error
  338. */
  339. int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
  340. const struct cfg80211_chan_def *c);
  341. void cfg80211_set_dfs_state(struct wiphy *wiphy,
  342. const struct cfg80211_chan_def *chandef,
  343. enum nl80211_dfs_state dfs_state);
  344. void cfg80211_dfs_channels_update_work(struct work_struct *work);
  345. static inline int
  346. cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
  347. struct wireless_dev *wdev,
  348. enum nl80211_iftype iftype)
  349. {
  350. return cfg80211_can_use_iftype_chan(rdev, wdev, iftype, NULL,
  351. CHAN_MODE_UNDEFINED, 0);
  352. }
  353. static inline int
  354. cfg80211_can_add_interface(struct cfg80211_registered_device *rdev,
  355. enum nl80211_iftype iftype)
  356. {
  357. return cfg80211_can_change_interface(rdev, NULL, iftype);
  358. }
  359. static inline int
  360. cfg80211_can_use_chan(struct cfg80211_registered_device *rdev,
  361. struct wireless_dev *wdev,
  362. struct ieee80211_channel *chan,
  363. enum cfg80211_chan_mode chanmode)
  364. {
  365. return cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
  366. chan, chanmode, 0);
  367. }
  368. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  369. {
  370. unsigned long end = jiffies;
  371. if (end >= start)
  372. return jiffies_to_msecs(end - start);
  373. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  374. }
  375. void
  376. cfg80211_get_chan_state(struct wireless_dev *wdev,
  377. struct ieee80211_channel **chan,
  378. enum cfg80211_chan_mode *chanmode);
  379. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  380. struct cfg80211_chan_def *chandef);
  381. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  382. const u8 *rates, unsigned int n_rates,
  383. u32 *mask);
  384. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  385. u32 beacon_int);
  386. void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
  387. enum nl80211_iftype iftype, int num);
  388. void cfg80211_leave(struct cfg80211_registered_device *rdev,
  389. struct wireless_dev *wdev);
  390. void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
  391. struct wireless_dev *wdev);
  392. #define CFG80211_MAX_NUM_DIFFERENT_CHANNELS 10
  393. #ifdef CONFIG_CFG80211_DEVELOPER_WARNINGS
  394. #define CFG80211_DEV_WARN_ON(cond) WARN_ON(cond)
  395. #else
  396. /*
  397. * Trick to enable using it as a condition,
  398. * and also not give a warning when it's
  399. * not used that way.
  400. */
  401. #define CFG80211_DEV_WARN_ON(cond) ({bool __r = (cond); __r; })
  402. #endif
  403. #endif /* __NET_WIRELESS_CORE_H */