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