ibss.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Some IBSS support code for cfg80211.
  4. *
  5. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright (C) 2020-2024 Intel Corporation
  7. */
  8. #include <linux/etherdevice.h>
  9. #include <linux/if_arp.h>
  10. #include <linux/slab.h>
  11. #include <linux/export.h>
  12. #include <net/cfg80211.h>
  13. #include "wext-compat.h"
  14. #include "nl80211.h"
  15. #include "rdev-ops.h"
  16. void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
  17. struct ieee80211_channel *channel)
  18. {
  19. struct wireless_dev *wdev = dev->ieee80211_ptr;
  20. struct cfg80211_bss *bss;
  21. #ifdef CONFIG_CFG80211_WEXT
  22. union iwreq_data wrqu;
  23. #endif
  24. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  25. return;
  26. if (!wdev->u.ibss.ssid_len)
  27. return;
  28. bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
  29. IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
  30. if (WARN_ON(!bss))
  31. return;
  32. if (wdev->u.ibss.current_bss) {
  33. cfg80211_unhold_bss(wdev->u.ibss.current_bss);
  34. cfg80211_put_bss(wdev->wiphy, &wdev->u.ibss.current_bss->pub);
  35. }
  36. cfg80211_hold_bss(bss_from_pub(bss));
  37. wdev->u.ibss.current_bss = bss_from_pub(bss);
  38. cfg80211_upload_connect_keys(wdev);
  39. nl80211_send_ibss_bssid(wiphy_to_rdev(wdev->wiphy), dev, bssid,
  40. GFP_KERNEL);
  41. #ifdef CONFIG_CFG80211_WEXT
  42. memset(&wrqu, 0, sizeof(wrqu));
  43. memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
  44. wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
  45. #endif
  46. }
  47. void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
  48. struct ieee80211_channel *channel, gfp_t gfp)
  49. {
  50. struct wireless_dev *wdev = dev->ieee80211_ptr;
  51. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  52. struct cfg80211_event *ev;
  53. unsigned long flags;
  54. trace_cfg80211_ibss_joined(dev, bssid, channel);
  55. if (WARN_ON(!channel))
  56. return;
  57. ev = kzalloc(sizeof(*ev), gfp);
  58. if (!ev)
  59. return;
  60. ev->type = EVENT_IBSS_JOINED;
  61. memcpy(ev->ij.bssid, bssid, ETH_ALEN);
  62. ev->ij.channel = channel;
  63. spin_lock_irqsave(&wdev->event_lock, flags);
  64. list_add_tail(&ev->list, &wdev->event_list);
  65. spin_unlock_irqrestore(&wdev->event_lock, flags);
  66. queue_work(cfg80211_wq, &rdev->event_work);
  67. }
  68. EXPORT_SYMBOL(cfg80211_ibss_joined);
  69. int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  70. struct net_device *dev,
  71. struct cfg80211_ibss_params *params,
  72. struct cfg80211_cached_keys *connkeys)
  73. {
  74. struct wireless_dev *wdev = dev->ieee80211_ptr;
  75. int err;
  76. lockdep_assert_held(&rdev->wiphy.mtx);
  77. if (wdev->links[0].cac_started)
  78. return -EBUSY;
  79. if (wdev->u.ibss.ssid_len)
  80. return -EALREADY;
  81. if (!params->basic_rates) {
  82. /*
  83. * If no rates were explicitly configured,
  84. * use the mandatory rate set for 11b or
  85. * 11a for maximum compatibility.
  86. */
  87. struct ieee80211_supported_band *sband;
  88. enum nl80211_band band;
  89. u32 flag;
  90. int j;
  91. band = params->chandef.chan->band;
  92. if (band == NL80211_BAND_5GHZ ||
  93. band == NL80211_BAND_6GHZ)
  94. flag = IEEE80211_RATE_MANDATORY_A;
  95. else
  96. flag = IEEE80211_RATE_MANDATORY_B;
  97. sband = rdev->wiphy.bands[band];
  98. for (j = 0; j < sband->n_bitrates; j++) {
  99. if (sband->bitrates[j].flags & flag)
  100. params->basic_rates |= BIT(j);
  101. }
  102. }
  103. if (WARN_ON(connkeys && connkeys->def < 0))
  104. return -EINVAL;
  105. if (WARN_ON(wdev->connect_keys))
  106. kfree_sensitive(wdev->connect_keys);
  107. wdev->connect_keys = connkeys;
  108. wdev->u.ibss.chandef = params->chandef;
  109. if (connkeys) {
  110. params->wep_keys = connkeys->params;
  111. params->wep_tx_key = connkeys->def;
  112. }
  113. #ifdef CONFIG_CFG80211_WEXT
  114. wdev->wext.ibss.chandef = params->chandef;
  115. #endif
  116. err = rdev_join_ibss(rdev, dev, params);
  117. if (err) {
  118. wdev->connect_keys = NULL;
  119. return err;
  120. }
  121. memcpy(wdev->u.ibss.ssid, params->ssid, params->ssid_len);
  122. wdev->u.ibss.ssid_len = params->ssid_len;
  123. return 0;
  124. }
  125. void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
  126. {
  127. struct wireless_dev *wdev = dev->ieee80211_ptr;
  128. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  129. int i;
  130. lockdep_assert_wiphy(wdev->wiphy);
  131. kfree_sensitive(wdev->connect_keys);
  132. wdev->connect_keys = NULL;
  133. rdev_set_qos_map(rdev, dev, NULL);
  134. /*
  135. * Delete all the keys ... pairwise keys can't really
  136. * exist any more anyway, but default keys might.
  137. */
  138. if (rdev->ops->del_key)
  139. for (i = 0; i < 6; i++)
  140. rdev_del_key(rdev, dev, -1, i, false, NULL);
  141. if (wdev->u.ibss.current_bss) {
  142. cfg80211_unhold_bss(wdev->u.ibss.current_bss);
  143. cfg80211_put_bss(wdev->wiphy, &wdev->u.ibss.current_bss->pub);
  144. }
  145. wdev->u.ibss.current_bss = NULL;
  146. wdev->u.ibss.ssid_len = 0;
  147. memset(&wdev->u.ibss.chandef, 0, sizeof(wdev->u.ibss.chandef));
  148. #ifdef CONFIG_CFG80211_WEXT
  149. if (!nowext)
  150. wdev->wext.ibss.ssid_len = 0;
  151. #endif
  152. cfg80211_sched_dfs_chan_update(rdev);
  153. }
  154. int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  155. struct net_device *dev, bool nowext)
  156. {
  157. struct wireless_dev *wdev = dev->ieee80211_ptr;
  158. int err;
  159. lockdep_assert_wiphy(wdev->wiphy);
  160. if (!wdev->u.ibss.ssid_len)
  161. return -ENOLINK;
  162. err = rdev_leave_ibss(rdev, dev);
  163. if (err)
  164. return err;
  165. wdev->conn_owner_nlportid = 0;
  166. cfg80211_clear_ibss(dev, nowext);
  167. return 0;
  168. }
  169. #ifdef CONFIG_CFG80211_WEXT
  170. int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
  171. struct wireless_dev *wdev)
  172. {
  173. struct cfg80211_cached_keys *ck = NULL;
  174. enum nl80211_band band;
  175. int i, err;
  176. lockdep_assert_wiphy(wdev->wiphy);
  177. if (!wdev->wext.ibss.beacon_interval)
  178. wdev->wext.ibss.beacon_interval = 100;
  179. /* try to find an IBSS channel if none requested ... */
  180. if (!wdev->wext.ibss.chandef.chan) {
  181. struct ieee80211_channel *new_chan = NULL;
  182. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  183. struct ieee80211_supported_band *sband;
  184. struct ieee80211_channel *chan;
  185. sband = rdev->wiphy.bands[band];
  186. if (!sband)
  187. continue;
  188. for (i = 0; i < sband->n_channels; i++) {
  189. chan = &sband->channels[i];
  190. if (chan->flags & IEEE80211_CHAN_NO_IR)
  191. continue;
  192. if (chan->flags & IEEE80211_CHAN_DISABLED)
  193. continue;
  194. new_chan = chan;
  195. break;
  196. }
  197. if (new_chan)
  198. break;
  199. }
  200. if (!new_chan)
  201. return -EINVAL;
  202. cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
  203. NL80211_CHAN_NO_HT);
  204. }
  205. /* don't join -- SSID is not there */
  206. if (!wdev->wext.ibss.ssid_len)
  207. return 0;
  208. if (!netif_running(wdev->netdev))
  209. return 0;
  210. if (wdev->wext.keys)
  211. wdev->wext.keys->def = wdev->wext.default_key;
  212. wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
  213. if (wdev->wext.keys && wdev->wext.keys->def != -1) {
  214. ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
  215. if (!ck)
  216. return -ENOMEM;
  217. for (i = 0; i < 4; i++)
  218. ck->params[i].key = ck->data[i];
  219. }
  220. err = __cfg80211_join_ibss(rdev, wdev->netdev,
  221. &wdev->wext.ibss, ck);
  222. if (err)
  223. kfree(ck);
  224. return err;
  225. }
  226. int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
  227. struct iw_request_info *info,
  228. struct iw_freq *wextfreq, char *extra)
  229. {
  230. struct wireless_dev *wdev = dev->ieee80211_ptr;
  231. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  232. struct ieee80211_channel *chan = NULL;
  233. int err, freq;
  234. /* call only for ibss! */
  235. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  236. return -EINVAL;
  237. if (!rdev->ops->join_ibss)
  238. return -EOPNOTSUPP;
  239. freq = cfg80211_wext_freq(wextfreq);
  240. if (freq < 0)
  241. return freq;
  242. if (freq) {
  243. chan = ieee80211_get_channel(wdev->wiphy, freq);
  244. if (!chan)
  245. return -EINVAL;
  246. if (chan->flags & IEEE80211_CHAN_NO_IR ||
  247. chan->flags & IEEE80211_CHAN_DISABLED)
  248. return -EINVAL;
  249. }
  250. if (wdev->wext.ibss.chandef.chan == chan)
  251. return 0;
  252. err = 0;
  253. if (wdev->u.ibss.ssid_len)
  254. err = cfg80211_leave_ibss(rdev, dev, true);
  255. if (err)
  256. return err;
  257. if (chan) {
  258. cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
  259. NL80211_CHAN_NO_HT);
  260. wdev->wext.ibss.channel_fixed = true;
  261. } else {
  262. /* cfg80211_ibss_wext_join will pick one if needed */
  263. wdev->wext.ibss.channel_fixed = false;
  264. }
  265. return cfg80211_ibss_wext_join(rdev, wdev);
  266. }
  267. int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
  268. struct iw_request_info *info,
  269. struct iw_freq *freq, char *extra)
  270. {
  271. struct wireless_dev *wdev = dev->ieee80211_ptr;
  272. struct ieee80211_channel *chan = NULL;
  273. /* call only for ibss! */
  274. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  275. return -EINVAL;
  276. if (wdev->u.ibss.current_bss)
  277. chan = wdev->u.ibss.current_bss->pub.channel;
  278. else if (wdev->wext.ibss.chandef.chan)
  279. chan = wdev->wext.ibss.chandef.chan;
  280. if (chan) {
  281. freq->m = chan->center_freq;
  282. freq->e = 6;
  283. return 0;
  284. }
  285. /* no channel if not joining */
  286. return -EINVAL;
  287. }
  288. int cfg80211_ibss_wext_siwessid(struct net_device *dev,
  289. struct iw_request_info *info,
  290. struct iw_point *data, char *ssid)
  291. {
  292. struct wireless_dev *wdev = dev->ieee80211_ptr;
  293. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  294. size_t len = data->length;
  295. int err;
  296. /* call only for ibss! */
  297. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  298. return -EINVAL;
  299. if (!rdev->ops->join_ibss)
  300. return -EOPNOTSUPP;
  301. err = 0;
  302. if (wdev->u.ibss.ssid_len)
  303. err = cfg80211_leave_ibss(rdev, dev, true);
  304. if (err)
  305. return err;
  306. /* iwconfig uses nul termination in SSID.. */
  307. if (len > 0 && ssid[len - 1] == '\0')
  308. len--;
  309. memcpy(wdev->u.ibss.ssid, ssid, len);
  310. wdev->wext.ibss.ssid = wdev->u.ibss.ssid;
  311. wdev->wext.ibss.ssid_len = len;
  312. return cfg80211_ibss_wext_join(rdev, wdev);
  313. }
  314. int cfg80211_ibss_wext_giwessid(struct net_device *dev,
  315. struct iw_request_info *info,
  316. struct iw_point *data, char *ssid)
  317. {
  318. struct wireless_dev *wdev = dev->ieee80211_ptr;
  319. /* call only for ibss! */
  320. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  321. return -EINVAL;
  322. data->flags = 0;
  323. if (wdev->u.ibss.ssid_len) {
  324. data->flags = 1;
  325. data->length = wdev->u.ibss.ssid_len;
  326. memcpy(ssid, wdev->u.ibss.ssid, data->length);
  327. } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
  328. data->flags = 1;
  329. data->length = wdev->wext.ibss.ssid_len;
  330. memcpy(ssid, wdev->wext.ibss.ssid, data->length);
  331. }
  332. return 0;
  333. }
  334. int cfg80211_ibss_wext_siwap(struct net_device *dev,
  335. struct iw_request_info *info,
  336. struct sockaddr *ap_addr, char *extra)
  337. {
  338. struct wireless_dev *wdev = dev->ieee80211_ptr;
  339. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  340. u8 *bssid = ap_addr->sa_data;
  341. int err;
  342. /* call only for ibss! */
  343. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  344. return -EINVAL;
  345. if (!rdev->ops->join_ibss)
  346. return -EOPNOTSUPP;
  347. if (ap_addr->sa_family != ARPHRD_ETHER)
  348. return -EINVAL;
  349. /* automatic mode */
  350. if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
  351. bssid = NULL;
  352. if (bssid && !is_valid_ether_addr(bssid))
  353. return -EINVAL;
  354. /* both automatic */
  355. if (!bssid && !wdev->wext.ibss.bssid)
  356. return 0;
  357. /* fixed already - and no change */
  358. if (wdev->wext.ibss.bssid && bssid &&
  359. ether_addr_equal(bssid, wdev->wext.ibss.bssid))
  360. return 0;
  361. err = 0;
  362. if (wdev->u.ibss.ssid_len)
  363. err = cfg80211_leave_ibss(rdev, dev, true);
  364. if (err)
  365. return err;
  366. if (bssid) {
  367. memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
  368. wdev->wext.ibss.bssid = wdev->wext.bssid;
  369. } else
  370. wdev->wext.ibss.bssid = NULL;
  371. return cfg80211_ibss_wext_join(rdev, wdev);
  372. }
  373. int cfg80211_ibss_wext_giwap(struct net_device *dev,
  374. struct iw_request_info *info,
  375. struct sockaddr *ap_addr, char *extra)
  376. {
  377. struct wireless_dev *wdev = dev->ieee80211_ptr;
  378. /* call only for ibss! */
  379. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  380. return -EINVAL;
  381. ap_addr->sa_family = ARPHRD_ETHER;
  382. if (wdev->u.ibss.current_bss)
  383. memcpy(ap_addr->sa_data, wdev->u.ibss.current_bss->pub.bssid,
  384. ETH_ALEN);
  385. else if (wdev->wext.ibss.bssid)
  386. memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
  387. else
  388. eth_zero_addr(ap_addr->sa_data);
  389. return 0;
  390. }
  391. #endif