core.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 sk_buff *scan_msg;
  55. struct cfg80211_sched_scan_request *sched_scan_req;
  56. unsigned long suspend_at;
  57. struct work_struct scan_done_wk;
  58. struct work_struct sched_scan_results_wk;
  59. struct genl_info *cur_cmd_info;
  60. struct work_struct conn_work;
  61. struct work_struct event_work;
  62. struct delayed_work dfs_update_channels_wk;
  63. /* netlink port which started critical protocol (0 means not started) */
  64. u32 crit_proto_nlportid;
  65. struct cfg80211_coalesce *coalesce;
  66. spinlock_t destroy_list_lock;
  67. struct list_head destroy_list;
  68. struct work_struct destroy_work;
  69. /* must be last because of the way we do wiphy_priv(),
  70. * and it should at least be aligned to NETDEV_ALIGN */
  71. struct wiphy wiphy __aligned(NETDEV_ALIGN);
  72. };
  73. static inline
  74. struct cfg80211_registered_device *wiphy_to_rdev(struct wiphy *wiphy)
  75. {
  76. BUG_ON(!wiphy);
  77. return container_of(wiphy, struct cfg80211_registered_device, wiphy);
  78. }
  79. static inline void
  80. cfg80211_rdev_free_wowlan(struct cfg80211_registered_device *rdev)
  81. {
  82. #ifdef CONFIG_PM
  83. int i;
  84. if (!rdev->wiphy.wowlan_config)
  85. return;
  86. for (i = 0; i < rdev->wiphy.wowlan_config->n_patterns; i++)
  87. kfree(rdev->wiphy.wowlan_config->patterns[i].mask);
  88. kfree(rdev->wiphy.wowlan_config->patterns);
  89. if (rdev->wiphy.wowlan_config->tcp &&
  90. rdev->wiphy.wowlan_config->tcp->sock)
  91. sock_release(rdev->wiphy.wowlan_config->tcp->sock);
  92. kfree(rdev->wiphy.wowlan_config->tcp);
  93. kfree(rdev->wiphy.wowlan_config->nd_config);
  94. kfree(rdev->wiphy.wowlan_config);
  95. #endif
  96. }
  97. extern struct workqueue_struct *cfg80211_wq;
  98. extern struct list_head cfg80211_rdev_list;
  99. extern int cfg80211_rdev_list_generation;
  100. struct cfg80211_internal_bss {
  101. struct list_head list;
  102. struct list_head hidden_list;
  103. struct rb_node rbn;
  104. unsigned long ts;
  105. unsigned long refcount;
  106. atomic_t hold;
  107. /* must be last because of priv member */
  108. struct cfg80211_bss pub;
  109. };
  110. static inline struct cfg80211_internal_bss *bss_from_pub(struct cfg80211_bss *pub)
  111. {
  112. return container_of(pub, struct cfg80211_internal_bss, pub);
  113. }
  114. static inline void cfg80211_hold_bss(struct cfg80211_internal_bss *bss)
  115. {
  116. atomic_inc(&bss->hold);
  117. }
  118. static inline void cfg80211_unhold_bss(struct cfg80211_internal_bss *bss)
  119. {
  120. int r = atomic_dec_return(&bss->hold);
  121. WARN_ON(r < 0);
  122. }
  123. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx);
  124. int get_wiphy_idx(struct wiphy *wiphy);
  125. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx);
  126. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  127. struct net *net);
  128. static inline void wdev_lock(struct wireless_dev *wdev)
  129. __acquires(wdev)
  130. {
  131. mutex_lock(&wdev->mtx);
  132. __acquire(wdev->mtx);
  133. }
  134. static inline void wdev_unlock(struct wireless_dev *wdev)
  135. __releases(wdev)
  136. {
  137. __release(wdev->mtx);
  138. mutex_unlock(&wdev->mtx);
  139. }
  140. #define ASSERT_WDEV_LOCK(wdev) lockdep_assert_held(&(wdev)->mtx)
  141. static inline bool cfg80211_has_monitors_only(struct cfg80211_registered_device *rdev)
  142. {
  143. ASSERT_RTNL();
  144. return rdev->num_running_ifaces == rdev->num_running_monitor_ifaces &&
  145. rdev->num_running_ifaces > 0;
  146. }
  147. enum cfg80211_event_type {
  148. EVENT_CONNECT_RESULT,
  149. EVENT_ROAMED,
  150. EVENT_DISCONNECTED,
  151. EVENT_IBSS_JOINED,
  152. EVENT_STOPPED,
  153. };
  154. struct cfg80211_event {
  155. struct list_head list;
  156. enum cfg80211_event_type type;
  157. union {
  158. struct {
  159. u8 bssid[ETH_ALEN];
  160. const u8 *req_ie;
  161. const u8 *resp_ie;
  162. size_t req_ie_len;
  163. size_t resp_ie_len;
  164. u16 status;
  165. } cr;
  166. struct {
  167. const u8 *req_ie;
  168. const u8 *resp_ie;
  169. size_t req_ie_len;
  170. size_t resp_ie_len;
  171. struct cfg80211_bss *bss;
  172. } rm;
  173. struct {
  174. const u8 *ie;
  175. size_t ie_len;
  176. u16 reason;
  177. } dc;
  178. struct {
  179. u8 bssid[ETH_ALEN];
  180. struct ieee80211_channel *channel;
  181. } ij;
  182. };
  183. };
  184. struct cfg80211_cached_keys {
  185. struct key_params params[6];
  186. u8 data[6][WLAN_MAX_KEY_LEN];
  187. int def, defmgmt;
  188. };
  189. enum cfg80211_chan_mode {
  190. CHAN_MODE_UNDEFINED,
  191. CHAN_MODE_SHARED,
  192. CHAN_MODE_EXCLUSIVE,
  193. };
  194. struct cfg80211_beacon_registration {
  195. struct list_head list;
  196. u32 nlportid;
  197. };
  198. struct cfg80211_iface_destroy {
  199. struct list_head list;
  200. u32 nlportid;
  201. };
  202. void cfg80211_destroy_ifaces(struct cfg80211_registered_device *rdev);
  203. /* free object */
  204. void cfg80211_dev_free(struct cfg80211_registered_device *rdev);
  205. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  206. char *newname);
  207. void ieee80211_set_bitrate_flags(struct wiphy *wiphy);
  208. void cfg80211_bss_expire(struct cfg80211_registered_device *rdev);
  209. void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
  210. unsigned long age_secs);
  211. /* IBSS */
  212. int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  213. struct net_device *dev,
  214. struct cfg80211_ibss_params *params,
  215. struct cfg80211_cached_keys *connkeys);
  216. void cfg80211_clear_ibss(struct net_device *dev, bool nowext);
  217. int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  218. struct net_device *dev, bool nowext);
  219. int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  220. struct net_device *dev, bool nowext);
  221. void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
  222. struct ieee80211_channel *channel);
  223. int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
  224. struct wireless_dev *wdev);
  225. /* mesh */
  226. extern const struct mesh_config default_mesh_config;
  227. extern const struct mesh_setup default_mesh_setup;
  228. int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  229. struct net_device *dev,
  230. struct mesh_setup *setup,
  231. const struct mesh_config *conf);
  232. int cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  233. struct net_device *dev,
  234. struct mesh_setup *setup,
  235. const struct mesh_config *conf);
  236. int __cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  237. struct net_device *dev);
  238. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  239. struct net_device *dev);
  240. int cfg80211_set_mesh_channel(struct cfg80211_registered_device *rdev,
  241. struct wireless_dev *wdev,
  242. struct cfg80211_chan_def *chandef);
  243. /* OCB */
  244. int __cfg80211_join_ocb(struct cfg80211_registered_device *rdev,
  245. struct net_device *dev,
  246. struct ocb_setup *setup);
  247. int cfg80211_join_ocb(struct cfg80211_registered_device *rdev,
  248. struct net_device *dev,
  249. struct ocb_setup *setup);
  250. int __cfg80211_leave_ocb(struct cfg80211_registered_device *rdev,
  251. struct net_device *dev);
  252. int cfg80211_leave_ocb(struct cfg80211_registered_device *rdev,
  253. struct net_device *dev);
  254. /* AP */
  255. int __cfg80211_stop_ap(struct cfg80211_registered_device *rdev,
  256. struct net_device *dev, bool notify);
  257. int cfg80211_stop_ap(struct cfg80211_registered_device *rdev,
  258. struct net_device *dev, bool notify);
  259. /* MLME */
  260. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  261. struct net_device *dev,
  262. struct ieee80211_channel *chan,
  263. enum nl80211_auth_type auth_type,
  264. const u8 *bssid,
  265. const u8 *ssid, int ssid_len,
  266. const u8 *ie, int ie_len,
  267. const u8 *key, int key_len, int key_idx,
  268. const u8 *sae_data, int sae_data_len);
  269. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  270. struct net_device *dev,
  271. struct ieee80211_channel *chan,
  272. const u8 *bssid,
  273. const u8 *ssid, int ssid_len,
  274. struct cfg80211_assoc_request *req);
  275. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  276. struct net_device *dev, const u8 *bssid,
  277. const u8 *ie, int ie_len, u16 reason,
  278. bool local_state_change);
  279. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  280. struct net_device *dev, const u8 *bssid,
  281. const u8 *ie, int ie_len, u16 reason,
  282. bool local_state_change);
  283. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  284. struct net_device *dev);
  285. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
  286. u16 frame_type, const u8 *match_data,
  287. int match_len);
  288. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid);
  289. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev);
  290. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  291. struct wireless_dev *wdev,
  292. struct cfg80211_mgmt_tx_params *params,
  293. u64 *cookie);
  294. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  295. const struct ieee80211_ht_cap *ht_capa_mask);
  296. void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
  297. const struct ieee80211_vht_cap *vht_capa_mask);
  298. /* SME events */
  299. int cfg80211_connect(struct cfg80211_registered_device *rdev,
  300. struct net_device *dev,
  301. struct cfg80211_connect_params *connect,
  302. struct cfg80211_cached_keys *connkeys,
  303. const u8 *prev_bssid);
  304. void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
  305. const u8 *req_ie, size_t req_ie_len,
  306. const u8 *resp_ie, size_t resp_ie_len,
  307. u16 status, bool wextev,
  308. struct cfg80211_bss *bss);
  309. void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
  310. size_t ie_len, u16 reason, bool from_ap);
  311. int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  312. struct net_device *dev, u16 reason,
  313. bool wextev);
  314. void __cfg80211_roamed(struct wireless_dev *wdev,
  315. struct cfg80211_bss *bss,
  316. const u8 *req_ie, size_t req_ie_len,
  317. const u8 *resp_ie, size_t resp_ie_len);
  318. int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
  319. struct wireless_dev *wdev);
  320. /* SME implementation */
  321. void cfg80211_conn_work(struct work_struct *work);
  322. void cfg80211_sme_scan_done(struct net_device *dev);
  323. bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status);
  324. void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len);
  325. void cfg80211_sme_disassoc(struct wireless_dev *wdev);
  326. void cfg80211_sme_deauth(struct wireless_dev *wdev);
  327. void cfg80211_sme_auth_timeout(struct wireless_dev *wdev);
  328. void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev);
  329. /* internal helpers */
  330. bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher);
  331. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  332. struct key_params *params, int key_idx,
  333. bool pairwise, const u8 *mac_addr);
  334. void __cfg80211_scan_done(struct work_struct *wk);
  335. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
  336. bool send_message);
  337. void __cfg80211_sched_scan_results(struct work_struct *wk);
  338. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  339. bool driver_initiated);
  340. void cfg80211_upload_connect_keys(struct wireless_dev *wdev);
  341. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  342. struct net_device *dev, enum nl80211_iftype ntype,
  343. u32 *flags, struct vif_params *params);
  344. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev);
  345. void cfg80211_process_wdev_events(struct wireless_dev *wdev);
  346. int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
  347. struct wireless_dev *wdev,
  348. enum nl80211_iftype iftype,
  349. struct ieee80211_channel *chan,
  350. enum cfg80211_chan_mode chanmode,
  351. u8 radar_detect);
  352. /**
  353. * cfg80211_chandef_dfs_usable - checks if chandef is DFS usable
  354. * @wiphy: the wiphy to validate against
  355. * @chandef: the channel definition to check
  356. *
  357. * Checks if chandef is usable and we can/need start CAC on such channel.
  358. *
  359. * Return: Return true if all channels available and at least
  360. * one channel require CAC (NL80211_DFS_USABLE)
  361. */
  362. bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
  363. const struct cfg80211_chan_def *chandef);
  364. void cfg80211_set_dfs_state(struct wiphy *wiphy,
  365. const struct cfg80211_chan_def *chandef,
  366. enum nl80211_dfs_state dfs_state);
  367. void cfg80211_dfs_channels_update_work(struct work_struct *work);
  368. unsigned int
  369. cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
  370. const struct cfg80211_chan_def *chandef);
  371. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  372. {
  373. unsigned long end = jiffies;
  374. if (end >= start)
  375. return jiffies_to_msecs(end - start);
  376. return jiffies_to_msecs(end + (ULONG_MAX - start) + 1);
  377. }
  378. void
  379. cfg80211_get_chan_state(struct wireless_dev *wdev,
  380. struct ieee80211_channel **chan,
  381. enum cfg80211_chan_mode *chanmode,
  382. u8 *radar_detect);
  383. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  384. struct cfg80211_chan_def *chandef);
  385. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  386. const u8 *rates, unsigned int n_rates,
  387. u32 *mask);
  388. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  389. u32 beacon_int);
  390. void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
  391. enum nl80211_iftype iftype, int num);
  392. void __cfg80211_leave(struct cfg80211_registered_device *rdev,
  393. struct wireless_dev *wdev);
  394. void cfg80211_leave(struct cfg80211_registered_device *rdev,
  395. struct wireless_dev *wdev);
  396. void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
  397. struct wireless_dev *wdev);
  398. #define CFG80211_MAX_NUM_DIFFERENT_CHANNELS 10
  399. #ifdef CONFIG_CFG80211_DEVELOPER_WARNINGS
  400. #define CFG80211_DEV_WARN_ON(cond) WARN_ON(cond)
  401. #else
  402. /*
  403. * Trick to enable using it as a condition,
  404. * and also not give a warning when it's
  405. * not used that way.
  406. */
  407. #define CFG80211_DEV_WARN_ON(cond) ({bool __r = (cond); __r; })
  408. #endif
  409. #endif /* __NET_WIRELESS_CORE_H */