core.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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/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. /* 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. struct list_head beacon_registrations;
  52. spinlock_t beacon_registrations_lock;
  53. /* protected by RTNL only */
  54. int num_running_ifaces;
  55. int num_running_monitor_ifaces;
  56. /* BSSes/scanning */
  57. spinlock_t bss_lock;
  58. struct list_head bss_list;
  59. struct rb_root bss_tree;
  60. u32 bss_generation;
  61. struct cfg80211_scan_request *scan_req; /* protected by RTNL */
  62. struct cfg80211_sched_scan_request *sched_scan_req;
  63. unsigned long suspend_at;
  64. struct work_struct scan_done_wk;
  65. struct work_struct sched_scan_results_wk;
  66. struct mutex sched_scan_mtx;
  67. #ifdef CONFIG_NL80211_TESTMODE
  68. struct genl_info *testmode_info;
  69. #endif
  70. struct work_struct conn_work;
  71. struct work_struct event_work;
  72. struct cfg80211_wowlan *wowlan;
  73. struct delayed_work dfs_update_channels_wk;
  74. /* netlink port which started critical protocol (0 means not started) */
  75. u32 crit_proto_nlportid;
  76. /* must be last because of the way we do wiphy_priv(),
  77. * and it should at least be aligned to NETDEV_ALIGN */
  78. struct wiphy wiphy __aligned(NETDEV_ALIGN);
  79. };
  80. static inline
  81. struct cfg80211_registered_device *wiphy_to_dev(struct wiphy *wiphy)
  82. {
  83. BUG_ON(!wiphy);
  84. return container_of(wiphy, struct cfg80211_registered_device, wiphy);
  85. }
  86. static inline void
  87. cfg80211_rdev_free_wowlan(struct cfg80211_registered_device *rdev)
  88. {
  89. int i;
  90. if (!rdev->wowlan)
  91. return;
  92. for (i = 0; i < rdev->wowlan->n_patterns; i++)
  93. kfree(rdev->wowlan->patterns[i].mask);
  94. kfree(rdev->wowlan->patterns);
  95. if (rdev->wowlan->tcp && rdev->wowlan->tcp->sock)
  96. sock_release(rdev->wowlan->tcp->sock);
  97. kfree(rdev->wowlan->tcp);
  98. kfree(rdev->wowlan);
  99. }
  100. extern struct workqueue_struct *cfg80211_wq;
  101. extern struct mutex cfg80211_mutex;
  102. extern struct list_head cfg80211_rdev_list;
  103. extern int cfg80211_rdev_list_generation;
  104. static inline void assert_cfg80211_lock(void)
  105. {
  106. lockdep_assert_held(&cfg80211_mutex);
  107. }
  108. struct cfg80211_internal_bss {
  109. struct list_head list;
  110. struct list_head hidden_list;
  111. struct rb_node rbn;
  112. unsigned long ts;
  113. unsigned long refcount;
  114. atomic_t hold;
  115. /* must be last because of priv member */
  116. struct cfg80211_bss pub;
  117. };
  118. static inline struct cfg80211_internal_bss *bss_from_pub(struct cfg80211_bss *pub)
  119. {
  120. return container_of(pub, struct cfg80211_internal_bss, pub);
  121. }
  122. static inline void cfg80211_hold_bss(struct cfg80211_internal_bss *bss)
  123. {
  124. atomic_inc(&bss->hold);
  125. }
  126. static inline void cfg80211_unhold_bss(struct cfg80211_internal_bss *bss)
  127. {
  128. int r = atomic_dec_return(&bss->hold);
  129. WARN_ON(r < 0);
  130. }
  131. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx);
  132. int get_wiphy_idx(struct wiphy *wiphy);
  133. /* requires cfg80211_rdev_mutex to be held! */
  134. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx);
  135. /* identical to cfg80211_get_dev_from_info but only operate on ifindex */
  136. extern struct cfg80211_registered_device *
  137. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex);
  138. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  139. struct net *net);
  140. static inline void cfg80211_lock_rdev(struct cfg80211_registered_device *rdev)
  141. {
  142. mutex_lock(&rdev->mtx);
  143. }
  144. static inline void cfg80211_unlock_rdev(struct cfg80211_registered_device *rdev)
  145. {
  146. BUG_ON(IS_ERR(rdev) || !rdev);
  147. mutex_unlock(&rdev->mtx);
  148. }
  149. static inline void wdev_lock(struct wireless_dev *wdev)
  150. __acquires(wdev)
  151. {
  152. mutex_lock(&wdev->mtx);
  153. __acquire(wdev->mtx);
  154. }
  155. static inline void wdev_unlock(struct wireless_dev *wdev)
  156. __releases(wdev)
  157. {
  158. __release(wdev->mtx);
  159. mutex_unlock(&wdev->mtx);
  160. }
  161. #define ASSERT_RDEV_LOCK(rdev) lockdep_assert_held(&(rdev)->mtx)
  162. #define ASSERT_WDEV_LOCK(wdev) lockdep_assert_held(&(wdev)->mtx)
  163. static inline bool cfg80211_has_monitors_only(struct cfg80211_registered_device *rdev)
  164. {
  165. ASSERT_RTNL();
  166. return rdev->num_running_ifaces == rdev->num_running_monitor_ifaces &&
  167. rdev->num_running_ifaces > 0;
  168. }
  169. enum cfg80211_event_type {
  170. EVENT_CONNECT_RESULT,
  171. EVENT_ROAMED,
  172. EVENT_DISCONNECTED,
  173. EVENT_IBSS_JOINED,
  174. };
  175. struct cfg80211_event {
  176. struct list_head list;
  177. enum cfg80211_event_type type;
  178. union {
  179. struct {
  180. u8 bssid[ETH_ALEN];
  181. const u8 *req_ie;
  182. const u8 *resp_ie;
  183. size_t req_ie_len;
  184. size_t resp_ie_len;
  185. u16 status;
  186. } cr;
  187. struct {
  188. const u8 *req_ie;
  189. const u8 *resp_ie;
  190. size_t req_ie_len;
  191. size_t resp_ie_len;
  192. struct cfg80211_bss *bss;
  193. } rm;
  194. struct {
  195. const u8 *ie;
  196. size_t ie_len;
  197. u16 reason;
  198. } dc;
  199. struct {
  200. u8 bssid[ETH_ALEN];
  201. } ij;
  202. };
  203. };
  204. struct cfg80211_cached_keys {
  205. struct key_params params[6];
  206. u8 data[6][WLAN_MAX_KEY_LEN];
  207. int def, defmgmt;
  208. };
  209. enum cfg80211_chan_mode {
  210. CHAN_MODE_UNDEFINED,
  211. CHAN_MODE_SHARED,
  212. CHAN_MODE_EXCLUSIVE,
  213. };
  214. struct cfg80211_beacon_registration {
  215. struct list_head list;
  216. u32 nlportid;
  217. };
  218. /* free object */
  219. extern void cfg80211_dev_free(struct cfg80211_registered_device *rdev);
  220. extern int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  221. char *newname);
  222. void ieee80211_set_bitrate_flags(struct wiphy *wiphy);
  223. void cfg80211_bss_expire(struct cfg80211_registered_device *dev);
  224. void cfg80211_bss_age(struct cfg80211_registered_device *dev,
  225. unsigned long age_secs);
  226. /* IBSS */
  227. int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  228. struct net_device *dev,
  229. struct cfg80211_ibss_params *params,
  230. struct cfg80211_cached_keys *connkeys);
  231. int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  232. struct net_device *dev,
  233. struct cfg80211_ibss_params *params,
  234. struct cfg80211_cached_keys *connkeys);
  235. void cfg80211_clear_ibss(struct net_device *dev, bool nowext);
  236. int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  237. struct net_device *dev, bool nowext);
  238. int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  239. struct net_device *dev, bool nowext);
  240. void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid);
  241. int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
  242. struct wireless_dev *wdev);
  243. /* mesh */
  244. extern const struct mesh_config default_mesh_config;
  245. extern const struct mesh_setup default_mesh_setup;
  246. int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  247. struct net_device *dev,
  248. struct mesh_setup *setup,
  249. const struct mesh_config *conf);
  250. int cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  251. struct net_device *dev,
  252. struct mesh_setup *setup,
  253. const struct mesh_config *conf);
  254. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  255. struct net_device *dev);
  256. int cfg80211_set_mesh_channel(struct cfg80211_registered_device *rdev,
  257. struct wireless_dev *wdev,
  258. struct cfg80211_chan_def *chandef);
  259. /* AP */
  260. int cfg80211_stop_ap(struct cfg80211_registered_device *rdev,
  261. struct net_device *dev);
  262. /* MLME */
  263. int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  264. struct net_device *dev,
  265. struct ieee80211_channel *chan,
  266. enum nl80211_auth_type auth_type,
  267. const u8 *bssid,
  268. const u8 *ssid, int ssid_len,
  269. const u8 *ie, int ie_len,
  270. const u8 *key, int key_len, int key_idx,
  271. const u8 *sae_data, int sae_data_len);
  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. const u8 *sae_data, int sae_data_len);
  279. int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  280. struct net_device *dev,
  281. struct ieee80211_channel *chan,
  282. const u8 *bssid,
  283. const u8 *ssid, int ssid_len,
  284. struct cfg80211_assoc_request *req);
  285. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  286. struct net_device *dev,
  287. struct ieee80211_channel *chan,
  288. const u8 *bssid,
  289. const u8 *ssid, int ssid_len,
  290. struct cfg80211_assoc_request *req);
  291. int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  292. struct net_device *dev, const u8 *bssid,
  293. const u8 *ie, int ie_len, u16 reason,
  294. bool local_state_change);
  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_disassoc(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. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  304. struct net_device *dev);
  305. void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
  306. const u8 *req_ie, size_t req_ie_len,
  307. const u8 *resp_ie, size_t resp_ie_len,
  308. u16 status, bool wextev,
  309. struct cfg80211_bss *bss);
  310. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
  311. u16 frame_type, const u8 *match_data,
  312. int match_len);
  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 ieee80211_channel *chan, bool offchan,
  318. unsigned int wait, const u8 *buf, size_t len,
  319. bool no_cck, bool dont_wait_for_ack, u64 *cookie);
  320. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  321. const struct ieee80211_ht_cap *ht_capa_mask);
  322. void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
  323. const struct ieee80211_vht_cap *vht_capa_mask);
  324. /* SME */
  325. int __cfg80211_connect(struct cfg80211_registered_device *rdev,
  326. struct net_device *dev,
  327. struct cfg80211_connect_params *connect,
  328. struct cfg80211_cached_keys *connkeys,
  329. const u8 *prev_bssid);
  330. int cfg80211_connect(struct cfg80211_registered_device *rdev,
  331. struct net_device *dev,
  332. struct cfg80211_connect_params *connect,
  333. struct cfg80211_cached_keys *connkeys);
  334. int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  335. struct net_device *dev, u16 reason,
  336. bool wextev);
  337. int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  338. struct net_device *dev, u16 reason,
  339. bool wextev);
  340. void __cfg80211_roamed(struct wireless_dev *wdev,
  341. struct cfg80211_bss *bss,
  342. const u8 *req_ie, size_t req_ie_len,
  343. const u8 *resp_ie, size_t resp_ie_len);
  344. int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
  345. struct wireless_dev *wdev);
  346. void cfg80211_conn_work(struct work_struct *work);
  347. void cfg80211_sme_failed_assoc(struct wireless_dev *wdev);
  348. bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev);
  349. /* internal helpers */
  350. bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher);
  351. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  352. struct key_params *params, int key_idx,
  353. bool pairwise, const u8 *mac_addr);
  354. void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
  355. size_t ie_len, u16 reason, bool from_ap);
  356. void cfg80211_sme_scan_done(struct net_device *dev);
  357. void cfg80211_sme_rx_auth(struct net_device *dev, const u8 *buf, size_t len);
  358. void cfg80211_sme_disassoc(struct net_device *dev,
  359. struct cfg80211_internal_bss *bss);
  360. void __cfg80211_scan_done(struct work_struct *wk);
  361. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak);
  362. void __cfg80211_sched_scan_results(struct work_struct *wk);
  363. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  364. bool driver_initiated);
  365. void cfg80211_upload_connect_keys(struct wireless_dev *wdev);
  366. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  367. struct net_device *dev, enum nl80211_iftype ntype,
  368. u32 *flags, struct vif_params *params);
  369. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev);
  370. void cfg80211_process_wdev_events(struct wireless_dev *wdev);
  371. int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
  372. struct wireless_dev *wdev,
  373. enum nl80211_iftype iftype,
  374. struct ieee80211_channel *chan,
  375. enum cfg80211_chan_mode chanmode,
  376. u8 radar_detect);
  377. /**
  378. * cfg80211_chandef_dfs_required - checks if radar detection is required
  379. * @wiphy: the wiphy to validate against
  380. * @chandef: the channel definition to check
  381. * Return: 1 if radar detection is required, 0 if it is not, < 0 on error
  382. */
  383. int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
  384. const struct cfg80211_chan_def *c);
  385. void cfg80211_set_dfs_state(struct wiphy *wiphy,
  386. const struct cfg80211_chan_def *chandef,
  387. enum nl80211_dfs_state dfs_state);
  388. void cfg80211_dfs_channels_update_work(struct work_struct *work);
  389. static inline int
  390. cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
  391. struct wireless_dev *wdev,
  392. enum nl80211_iftype iftype)
  393. {
  394. return cfg80211_can_use_iftype_chan(rdev, wdev, iftype, NULL,
  395. CHAN_MODE_UNDEFINED, 0);
  396. }
  397. static inline int
  398. cfg80211_can_add_interface(struct cfg80211_registered_device *rdev,
  399. enum nl80211_iftype iftype)
  400. {
  401. return cfg80211_can_change_interface(rdev, NULL, iftype);
  402. }
  403. static inline int
  404. cfg80211_can_use_chan(struct cfg80211_registered_device *rdev,
  405. struct wireless_dev *wdev,
  406. struct ieee80211_channel *chan,
  407. enum cfg80211_chan_mode chanmode)
  408. {
  409. return cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
  410. chan, chanmode, 0);
  411. }
  412. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  413. {
  414. unsigned long end = jiffies;
  415. if (end >= start)
  416. return jiffies_to_msecs(end - start);
  417. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  418. }
  419. void
  420. cfg80211_get_chan_state(struct wireless_dev *wdev,
  421. struct ieee80211_channel **chan,
  422. enum cfg80211_chan_mode *chanmode);
  423. int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
  424. struct cfg80211_chan_def *chandef);
  425. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  426. const u8 *rates, unsigned int n_rates,
  427. u32 *mask);
  428. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  429. u32 beacon_int);
  430. void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
  431. enum nl80211_iftype iftype, int num);
  432. void cfg80211_leave(struct cfg80211_registered_device *rdev,
  433. struct wireless_dev *wdev);
  434. void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev,
  435. struct wireless_dev *wdev);
  436. #define CFG80211_MAX_NUM_DIFFERENT_CHANNELS 10
  437. #ifdef CONFIG_CFG80211_DEVELOPER_WARNINGS
  438. #define CFG80211_DEV_WARN_ON(cond) WARN_ON(cond)
  439. #else
  440. /*
  441. * Trick to enable using it as a condition,
  442. * and also not give a warning when it's
  443. * not used that way.
  444. */
  445. #define CFG80211_DEV_WARN_ON(cond) ({bool __r = (cond); __r; })
  446. #endif
  447. #endif /* __NET_WIRELESS_CORE_H */