core.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Wireless configuration interface internals.
  4. *
  5. * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
  6. */
  7. #ifndef __NET_WIRELESS_CORE_H
  8. #define __NET_WIRELESS_CORE_H
  9. #include <linux/list.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/rbtree.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/rfkill.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/rtnetlink.h>
  16. #include <net/genetlink.h>
  17. #include <net/cfg80211.h>
  18. #include "reg.h"
  19. #define WIPHY_IDX_INVALID -1
  20. struct cfg80211_registered_device {
  21. const struct cfg80211_ops *ops;
  22. struct list_head list;
  23. /* rfkill support */
  24. struct rfkill_ops rfkill_ops;
  25. struct rfkill *rfkill;
  26. struct work_struct rfkill_sync;
  27. /* ISO / IEC 3166 alpha2 for which this device is receiving
  28. * country IEs on, this can help disregard country IEs from APs
  29. * on the same alpha2 quickly. The alpha2 may differ from
  30. * cfg80211_regdomain's alpha2 when an intersection has occurred.
  31. * If the AP is reconfigured this can also be used to tell us if
  32. * the country on the country IE changed. */
  33. char country_ie_alpha2[2];
  34. /*
  35. * the driver requests the regulatory core to set this regulatory
  36. * domain as the wiphy's. Only used for %REGULATORY_WIPHY_SELF_MANAGED
  37. * devices using the regulatory_set_wiphy_regd() API
  38. */
  39. const struct ieee80211_regdomain *requested_regd;
  40. /* If a Country IE has been received this tells us the environment
  41. * which its telling us its in. This defaults to ENVIRON_ANY */
  42. enum environment_cap env;
  43. /* wiphy index, internal only */
  44. int wiphy_idx;
  45. /* protected by RTNL */
  46. int devlist_generation, wdev_id;
  47. int opencount;
  48. wait_queue_head_t dev_wait;
  49. struct list_head beacon_registrations;
  50. spinlock_t beacon_registrations_lock;
  51. struct list_head mlme_unreg;
  52. spinlock_t mlme_unreg_lock;
  53. struct work_struct mlme_unreg_wk;
  54. /* protected by RTNL only */
  55. int num_running_ifaces;
  56. int num_running_monitor_ifaces;
  57. u64 cookie_counter;
  58. /* BSSes/scanning */
  59. spinlock_t bss_lock;
  60. struct list_head bss_list;
  61. struct rb_root bss_tree;
  62. u32 bss_generation;
  63. u32 bss_entries;
  64. struct cfg80211_scan_request *scan_req; /* protected by RTNL */
  65. struct sk_buff *scan_msg;
  66. struct list_head sched_scan_req_list;
  67. time64_t suspend_at;
  68. struct work_struct scan_done_wk;
  69. struct genl_info *cur_cmd_info;
  70. struct work_struct conn_work;
  71. struct work_struct event_work;
  72. struct delayed_work dfs_update_channels_wk;
  73. /* netlink port which started critical protocol (0 means not started) */
  74. u32 crit_proto_nlportid;
  75. struct cfg80211_coalesce *coalesce;
  76. struct work_struct destroy_work;
  77. struct work_struct sched_scan_stop_wk;
  78. struct work_struct sched_scan_res_wk;
  79. struct cfg80211_chan_def radar_chandef;
  80. struct work_struct propagate_radar_detect_wk;
  81. struct cfg80211_chan_def cac_done_chandef;
  82. struct work_struct propagate_cac_done_wk;
  83. /* must be last because of the way we do wiphy_priv(),
  84. * and it should at least be aligned to NETDEV_ALIGN */
  85. struct wiphy wiphy __aligned(NETDEV_ALIGN);
  86. };
  87. static inline
  88. struct cfg80211_registered_device *wiphy_to_rdev(struct wiphy *wiphy)
  89. {
  90. BUG_ON(!wiphy);
  91. return container_of(wiphy, struct cfg80211_registered_device, wiphy);
  92. }
  93. static inline void
  94. cfg80211_rdev_free_wowlan(struct cfg80211_registered_device *rdev)
  95. {
  96. #ifdef CONFIG_PM
  97. int i;
  98. if (!rdev->wiphy.wowlan_config)
  99. return;
  100. for (i = 0; i < rdev->wiphy.wowlan_config->n_patterns; i++)
  101. kfree(rdev->wiphy.wowlan_config->patterns[i].mask);
  102. kfree(rdev->wiphy.wowlan_config->patterns);
  103. if (rdev->wiphy.wowlan_config->tcp &&
  104. rdev->wiphy.wowlan_config->tcp->sock)
  105. sock_release(rdev->wiphy.wowlan_config->tcp->sock);
  106. kfree(rdev->wiphy.wowlan_config->tcp);
  107. kfree(rdev->wiphy.wowlan_config->nd_config);
  108. kfree(rdev->wiphy.wowlan_config);
  109. #endif
  110. }
  111. static inline u64 cfg80211_assign_cookie(struct cfg80211_registered_device *rdev)
  112. {
  113. u64 r = ++rdev->cookie_counter;
  114. if (WARN_ON(r == 0))
  115. r = ++rdev->cookie_counter;
  116. return r;
  117. }
  118. extern struct workqueue_struct *cfg80211_wq;
  119. extern struct list_head cfg80211_rdev_list;
  120. extern int cfg80211_rdev_list_generation;
  121. struct cfg80211_internal_bss {
  122. struct list_head list;
  123. struct list_head hidden_list;
  124. struct rb_node rbn;
  125. u64 ts_boottime;
  126. unsigned long ts;
  127. unsigned long refcount;
  128. atomic_t hold;
  129. /* time at the start of the reception of the first octet of the
  130. * timestamp field of the last beacon/probe received for this BSS.
  131. * The time is the TSF of the BSS specified by %parent_bssid.
  132. */
  133. u64 parent_tsf;
  134. /* the BSS according to which %parent_tsf is set. This is set to
  135. * the BSS that the interface that requested the scan was connected to
  136. * when the beacon/probe was received.
  137. */
  138. u8 parent_bssid[ETH_ALEN] __aligned(2);
  139. /* must be last because of priv member */
  140. struct cfg80211_bss pub;
  141. };
  142. static inline struct cfg80211_internal_bss *bss_from_pub(struct cfg80211_bss *pub)
  143. {
  144. return container_of(pub, struct cfg80211_internal_bss, pub);
  145. }
  146. static inline void cfg80211_hold_bss(struct cfg80211_internal_bss *bss)
  147. {
  148. atomic_inc(&bss->hold);
  149. }
  150. static inline void cfg80211_unhold_bss(struct cfg80211_internal_bss *bss)
  151. {
  152. int r = atomic_dec_return(&bss->hold);
  153. WARN_ON(r < 0);
  154. }
  155. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx);
  156. int get_wiphy_idx(struct wiphy *wiphy);
  157. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx);
  158. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  159. struct net *net);
  160. void cfg80211_init_wdev(struct cfg80211_registered_device *rdev,
  161. struct wireless_dev *wdev);
  162. static inline void wdev_lock(struct wireless_dev *wdev)
  163. __acquires(wdev)
  164. {
  165. mutex_lock(&wdev->mtx);
  166. __acquire(wdev->mtx);
  167. }
  168. static inline void wdev_unlock(struct wireless_dev *wdev)
  169. __releases(wdev)
  170. {
  171. __release(wdev->mtx);
  172. mutex_unlock(&wdev->mtx);
  173. }
  174. #define ASSERT_WDEV_LOCK(wdev) lockdep_assert_held(&(wdev)->mtx)
  175. static inline bool cfg80211_has_monitors_only(struct cfg80211_registered_device *rdev)
  176. {
  177. ASSERT_RTNL();
  178. return rdev->num_running_ifaces == rdev->num_running_monitor_ifaces &&
  179. rdev->num_running_ifaces > 0;
  180. }
  181. enum cfg80211_event_type {
  182. EVENT_CONNECT_RESULT,
  183. EVENT_ROAMED,
  184. EVENT_DISCONNECTED,
  185. EVENT_IBSS_JOINED,
  186. EVENT_STOPPED,
  187. EVENT_PORT_AUTHORIZED,
  188. };
  189. struct cfg80211_event {
  190. struct list_head list;
  191. enum cfg80211_event_type type;
  192. union {
  193. struct cfg80211_connect_resp_params cr;
  194. struct cfg80211_roam_info rm;
  195. struct {
  196. const u8 *ie;
  197. size_t ie_len;
  198. u16 reason;
  199. bool locally_generated;
  200. } dc;
  201. struct {
  202. u8 bssid[ETH_ALEN];
  203. struct ieee80211_channel *channel;
  204. } ij;
  205. struct {
  206. u8 bssid[ETH_ALEN];
  207. } pa;
  208. };
  209. };
  210. struct cfg80211_cached_keys {
  211. struct key_params params[CFG80211_MAX_WEP_KEYS];
  212. u8 data[CFG80211_MAX_WEP_KEYS][WLAN_KEY_LEN_WEP104];
  213. int def;
  214. };
  215. enum cfg80211_chan_mode {
  216. CHAN_MODE_UNDEFINED,
  217. CHAN_MODE_SHARED,
  218. CHAN_MODE_EXCLUSIVE,
  219. };
  220. struct cfg80211_beacon_registration {
  221. struct list_head list;
  222. u32 nlportid;
  223. };
  224. struct cfg80211_cqm_config {
  225. u32 rssi_hyst;
  226. s32 last_rssi_event_value;
  227. int n_rssi_thresholds;
  228. s32 rssi_thresholds[0];
  229. };
  230. void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev);
  231. /* free object */
  232. void cfg80211_dev_free(struct cfg80211_registered_device *rdev);
  233. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  234. char *newname);
  235. void ieee80211_set_bitrate_flags(struct wiphy *wiphy);
  236. void cfg80211_bss_expire(struct cfg80211_registered_device *rdev);
  237. void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
  238. unsigned long age_secs);
  239. /* IBSS */
  240. int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  241. struct net_device *dev,
  242. struct cfg80211_ibss_params *params,
  243. struct cfg80211_cached_keys *connkeys);
  244. void cfg80211_clear_ibss(struct net_device *dev, bool nowext);
  245. int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  246. struct net_device *dev, bool nowext);
  247. int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  248. struct net_device *dev, bool nowext);
  249. void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
  250. struct ieee80211_channel *channel);
  251. int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
  252. struct wireless_dev *wdev);
  253. /* mesh */
  254. extern const struct mesh_config default_mesh_config;
  255. extern const struct mesh_setup default_mesh_setup;
  256. int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  257. struct net_device *dev,
  258. struct mesh_setup *setup,
  259. const struct mesh_config *conf);
  260. int __cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  261. struct net_device *dev);
  262. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  263. struct net_device *dev);
  264. int cfg80211_set_mesh_channel(struct cfg80211_registered_device *rdev,
  265. struct wireless_dev *wdev,
  266. struct cfg80211_chan_def *chandef);
  267. /* OCB */
  268. int __cfg80211_join_ocb(struct cfg80211_registered_device *rdev,
  269. struct net_device *dev,
  270. struct ocb_setup *setup);
  271. int cfg80211_join_ocb(struct cfg80211_registered_device *rdev,
  272. struct net_device *dev,
  273. struct ocb_setup *setup);
  274. int __cfg80211_leave_ocb(struct cfg80211_registered_device *rdev,
  275. struct net_device *dev);
  276. int cfg80211_leave_ocb(struct cfg80211_registered_device *rdev,
  277. struct net_device *dev);
  278. /* AP */
  279. int __cfg80211_stop_ap(struct cfg80211_registered_device *rdev,
  280. struct net_device *dev, bool notify);
  281. int cfg80211_stop_ap(struct cfg80211_registered_device *rdev,
  282. struct net_device *dev, bool notify);
  283. /* MLME */
  284. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  285. struct net_device *dev,
  286. struct ieee80211_channel *chan,
  287. enum nl80211_auth_type auth_type,
  288. const u8 *bssid,
  289. const u8 *ssid, int ssid_len,
  290. const u8 *ie, int ie_len,
  291. const u8 *key, int key_len, int key_idx,
  292. const u8 *auth_data, int auth_data_len);
  293. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  294. struct net_device *dev,
  295. struct ieee80211_channel *chan,
  296. const u8 *bssid,
  297. const u8 *ssid, int ssid_len,
  298. struct cfg80211_assoc_request *req);
  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. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
  310. u16 frame_type, const u8 *match_data,
  311. int match_len);
  312. void cfg80211_mlme_unreg_wk(struct work_struct *wk);
  313. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid);
  314. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev);
  315. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  316. struct wireless_dev *wdev,
  317. struct cfg80211_mgmt_tx_params *params,
  318. u64 *cookie);
  319. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  320. const struct ieee80211_ht_cap *ht_capa_mask);
  321. void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
  322. const struct ieee80211_vht_cap *vht_capa_mask);
  323. /* SME events */
  324. int cfg80211_connect(struct cfg80211_registered_device *rdev,
  325. struct net_device *dev,
  326. struct cfg80211_connect_params *connect,
  327. struct cfg80211_cached_keys *connkeys,
  328. const u8 *prev_bssid);
  329. void __cfg80211_connect_result(struct net_device *dev,
  330. struct cfg80211_connect_resp_params *params,
  331. bool wextev);
  332. void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
  333. size_t ie_len, u16 reason, bool from_ap);
  334. int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  335. struct net_device *dev, u16 reason,
  336. bool wextev);
  337. void __cfg80211_roamed(struct wireless_dev *wdev,
  338. struct cfg80211_roam_info *info);
  339. void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid);
  340. int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
  341. struct wireless_dev *wdev);
  342. void cfg80211_autodisconnect_wk(struct work_struct *work);
  343. /* SME implementation */
  344. void cfg80211_conn_work(struct work_struct *work);
  345. void cfg80211_sme_scan_done(struct net_device *dev);
  346. bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status);
  347. void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len);
  348. void cfg80211_sme_disassoc(struct wireless_dev *wdev);
  349. void cfg80211_sme_deauth(struct wireless_dev *wdev);
  350. void cfg80211_sme_auth_timeout(struct wireless_dev *wdev);
  351. void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev);
  352. void cfg80211_sme_abandon_assoc(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_scan_done(struct work_struct *wk);
  359. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
  360. bool send_message);
  361. void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
  362. struct cfg80211_sched_scan_request *req);
  363. int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
  364. bool want_multi);
  365. void cfg80211_sched_scan_results_wk(struct work_struct *work);
  366. int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
  367. struct cfg80211_sched_scan_request *req,
  368. bool driver_initiated);
  369. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  370. u64 reqid, 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. struct vif_params *params);
  375. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev);
  376. void cfg80211_process_wdev_events(struct wireless_dev *wdev);
  377. bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
  378. u32 center_freq_khz, u32 bw_khz);
  379. /**
  380. * cfg80211_chandef_dfs_usable - checks if chandef is DFS usable
  381. * @wiphy: the wiphy to validate against
  382. * @chandef: the channel definition to check
  383. *
  384. * Checks if chandef is usable and we can/need start CAC on such channel.
  385. *
  386. * Return: Return true if all channels available and at least
  387. * one channel require CAC (NL80211_DFS_USABLE)
  388. */
  389. bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
  390. const struct cfg80211_chan_def *chandef);
  391. void cfg80211_set_dfs_state(struct wiphy *wiphy,
  392. const struct cfg80211_chan_def *chandef,
  393. enum nl80211_dfs_state dfs_state);
  394. void cfg80211_dfs_channels_update_work(struct work_struct *work);
  395. unsigned int
  396. cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
  397. const struct cfg80211_chan_def *chandef);
  398. void cfg80211_sched_dfs_chan_update(struct cfg80211_registered_device *rdev);
  399. bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
  400. struct ieee80211_channel *chan);
  401. bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev);
  402. bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
  403. struct ieee80211_channel *chan);
  404. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  405. {
  406. unsigned long end = jiffies;
  407. if (end >= start)
  408. return jiffies_to_msecs(end - start);
  409. return jiffies_to_msecs(end + (ULONG_MAX - start) + 1);
  410. }
  411. void
  412. cfg80211_get_chan_state(struct wireless_dev *wdev,
  413. struct ieee80211_channel **chan,
  414. enum cfg80211_chan_mode *chanmode,
  415. u8 *radar_detect);
  416. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  417. struct cfg80211_chan_def *chandef);
  418. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  419. const u8 *rates, unsigned int n_rates,
  420. u32 *mask);
  421. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  422. enum nl80211_iftype iftype, u32 beacon_int);
  423. void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
  424. enum nl80211_iftype iftype, int num);
  425. void __cfg80211_leave(struct cfg80211_registered_device *rdev,
  426. struct wireless_dev *wdev);
  427. void cfg80211_leave(struct cfg80211_registered_device *rdev,
  428. struct wireless_dev *wdev);
  429. void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
  430. struct wireless_dev *wdev);
  431. void cfg80211_stop_nan(struct cfg80211_registered_device *rdev,
  432. struct wireless_dev *wdev);
  433. #ifdef CONFIG_CFG80211_DEVELOPER_WARNINGS
  434. #define CFG80211_DEV_WARN_ON(cond) WARN_ON(cond)
  435. #else
  436. /*
  437. * Trick to enable using it as a condition,
  438. * and also not give a warning when it's
  439. * not used that way.
  440. */
  441. #define CFG80211_DEV_WARN_ON(cond) ({bool __r = (cond); __r; })
  442. #endif
  443. void cfg80211_cqm_config_free(struct wireless_dev *wdev);
  444. #endif /* __NET_WIRELESS_CORE_H */