Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Some IBSS support code for cfg80211. |
| 4 | * |
| 5 | * Copyright 2009 Johannes Berg <johannes@sipsolutions.net> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/etherdevice.h> |
| 9 | #include <linux/if_arp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 12 | #include <net/cfg80211.h> |
Johannes Berg | 0e82ffe | 2009-07-27 12:01:50 +0200 | [diff] [blame] | 13 | #include "wext-compat.h" |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 14 | #include "nl80211.h" |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 15 | #include "rdev-ops.h" |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 16 | |
| 17 | |
Antonio Quartulli | fe94f3a | 2014-01-29 17:53:43 +0100 | [diff] [blame] | 18 | void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, |
| 19 | struct ieee80211_channel *channel) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 20 | { |
| 21 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 22 | struct cfg80211_bss *bss; |
Johannes Berg | 3d23e34 | 2009-09-29 23:27:28 +0200 | [diff] [blame] | 23 | #ifdef CONFIG_CFG80211_WEXT |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 24 | union iwreq_data wrqu; |
| 25 | #endif |
| 26 | |
| 27 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 28 | return; |
| 29 | |
Johannes Berg | f796996 | 2009-08-21 12:23:49 +0200 | [diff] [blame] | 30 | if (!wdev->ssid_len) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 31 | return; |
| 32 | |
Antonio Quartulli | fe94f3a | 2014-01-29 17:53:43 +0100 | [diff] [blame] | 33 | bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0, |
Dedy Lansky | 6eb1813 | 2015-02-08 15:52:03 +0200 | [diff] [blame] | 34 | IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 35 | |
| 36 | if (WARN_ON(!bss)) |
| 37 | return; |
| 38 | |
| 39 | if (wdev->current_bss) { |
| 40 | cfg80211_unhold_bss(wdev->current_bss); |
Johannes Berg | 5b112d3 | 2013-02-01 01:49:58 +0100 | [diff] [blame] | 41 | cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Johannes Berg | 19957bb | 2009-07-02 17:20:43 +0200 | [diff] [blame] | 44 | cfg80211_hold_bss(bss_from_pub(bss)); |
| 45 | wdev->current_bss = bss_from_pub(bss); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 46 | |
David Spinadel | b867622 | 2016-09-22 23:16:50 +0300 | [diff] [blame] | 47 | if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP)) |
| 48 | cfg80211_upload_connect_keys(wdev); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 49 | |
Zhao, Gang | f26cbf4 | 2014-04-21 12:53:03 +0800 | [diff] [blame] | 50 | nl80211_send_ibss_bssid(wiphy_to_rdev(wdev->wiphy), dev, bssid, |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 51 | GFP_KERNEL); |
Johannes Berg | 3d23e34 | 2009-09-29 23:27:28 +0200 | [diff] [blame] | 52 | #ifdef CONFIG_CFG80211_WEXT |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 53 | memset(&wrqu, 0, sizeof(wrqu)); |
| 54 | memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN); |
| 55 | wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL); |
| 56 | #endif |
| 57 | } |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 58 | |
Antonio Quartulli | fe94f3a | 2014-01-29 17:53:43 +0100 | [diff] [blame] | 59 | void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, |
| 60 | struct ieee80211_channel *channel, gfp_t gfp) |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 61 | { |
| 62 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
Zhao, Gang | f26cbf4 | 2014-04-21 12:53:03 +0800 | [diff] [blame] | 63 | struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 64 | struct cfg80211_event *ev; |
| 65 | unsigned long flags; |
| 66 | |
Antonio Quartulli | fe94f3a | 2014-01-29 17:53:43 +0100 | [diff] [blame] | 67 | trace_cfg80211_ibss_joined(dev, bssid, channel); |
| 68 | |
| 69 | if (WARN_ON(!channel)) |
| 70 | return; |
Beni Lev | 4ee3e06 | 2012-08-27 12:49:39 +0300 | [diff] [blame] | 71 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 72 | ev = kzalloc(sizeof(*ev), gfp); |
| 73 | if (!ev) |
| 74 | return; |
| 75 | |
| 76 | ev->type = EVENT_IBSS_JOINED; |
Antonio Quartulli | fe94f3a | 2014-01-29 17:53:43 +0100 | [diff] [blame] | 77 | memcpy(ev->ij.bssid, bssid, ETH_ALEN); |
| 78 | ev->ij.channel = channel; |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 79 | |
| 80 | spin_lock_irqsave(&wdev->event_lock, flags); |
| 81 | list_add_tail(&ev->list, &wdev->event_list); |
| 82 | spin_unlock_irqrestore(&wdev->event_lock, flags); |
Alban Browaeys | e60d744 | 2009-11-25 15:13:00 +0100 | [diff] [blame] | 83 | queue_work(cfg80211_wq, &rdev->event_work); |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 84 | } |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 85 | EXPORT_SYMBOL(cfg80211_ibss_joined); |
| 86 | |
Denis Kenzior | f8d16d3 | 2018-03-26 12:52:45 -0500 | [diff] [blame] | 87 | int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev, |
| 88 | struct net_device *dev, |
| 89 | struct cfg80211_ibss_params *params, |
| 90 | struct cfg80211_cached_keys *connkeys) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 91 | { |
| 92 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 93 | int err; |
| 94 | |
Denis Kenzior | f8d16d3 | 2018-03-26 12:52:45 -0500 | [diff] [blame] | 95 | ASSERT_RTNL(); |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 96 | ASSERT_WDEV_LOCK(wdev); |
| 97 | |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 98 | if (wdev->ssid_len) |
| 99 | return -EALREADY; |
| 100 | |
Johannes Berg | 93b0523 | 2010-09-28 12:53:14 +0200 | [diff] [blame] | 101 | if (!params->basic_rates) { |
| 102 | /* |
| 103 | * If no rates were explicitly configured, |
| 104 | * use the mandatory rate set for 11b or |
| 105 | * 11a for maximum compatibility. |
| 106 | */ |
| 107 | struct ieee80211_supported_band *sband = |
Johannes Berg | 683b6d3 | 2012-11-08 21:25:48 +0100 | [diff] [blame] | 108 | rdev->wiphy.bands[params->chandef.chan->band]; |
Johannes Berg | 93b0523 | 2010-09-28 12:53:14 +0200 | [diff] [blame] | 109 | int j; |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 110 | u32 flag = params->chandef.chan->band == NL80211_BAND_5GHZ ? |
Johannes Berg | 93b0523 | 2010-09-28 12:53:14 +0200 | [diff] [blame] | 111 | IEEE80211_RATE_MANDATORY_A : |
| 112 | IEEE80211_RATE_MANDATORY_B; |
| 113 | |
| 114 | for (j = 0; j < sband->n_bitrates; j++) { |
| 115 | if (sband->bitrates[j].flags & flag) |
| 116 | params->basic_rates |= BIT(j); |
| 117 | } |
| 118 | } |
| 119 | |
Johannes Berg | f1c1f17 | 2016-09-13 17:08:23 +0200 | [diff] [blame] | 120 | if (WARN_ON(connkeys && connkeys->def < 0)) |
| 121 | return -EINVAL; |
| 122 | |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 123 | if (WARN_ON(wdev->connect_keys)) |
Johannes Berg | b47f610 | 2014-09-10 13:39:54 +0300 | [diff] [blame] | 124 | kzfree(wdev->connect_keys); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 125 | wdev->connect_keys = connkeys; |
| 126 | |
Michal Kazior | c30a3d3 | 2012-06-29 12:46:59 +0200 | [diff] [blame] | 127 | wdev->ibss_fixed = params->channel_fixed; |
Simon Wunderlich | 5336fa8 | 2013-10-07 18:41:05 +0200 | [diff] [blame] | 128 | wdev->ibss_dfs_possible = params->userspace_handles_dfs; |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 129 | wdev->chandef = params->chandef; |
Tova Mussai | 9ae3b17 | 2017-10-29 11:51:11 +0200 | [diff] [blame] | 130 | if (connkeys) { |
| 131 | params->wep_keys = connkeys->params; |
| 132 | params->wep_tx_key = connkeys->def; |
| 133 | } |
| 134 | |
Johannes Berg | 3d23e34 | 2009-09-29 23:27:28 +0200 | [diff] [blame] | 135 | #ifdef CONFIG_CFG80211_WEXT |
Johannes Berg | 683b6d3 | 2012-11-08 21:25:48 +0100 | [diff] [blame] | 136 | wdev->wext.ibss.chandef = params->chandef; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 137 | #endif |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 138 | err = rdev_join_ibss(rdev, dev, params); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 139 | if (err) { |
| 140 | wdev->connect_keys = NULL; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 141 | return err; |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 142 | } |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 143 | |
| 144 | memcpy(wdev->ssid, params->ssid, params->ssid_len); |
| 145 | wdev->ssid_len = params->ssid_len; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 150 | static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext) |
| 151 | { |
| 152 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
Zhao, Gang | f26cbf4 | 2014-04-21 12:53:03 +0800 | [diff] [blame] | 153 | struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 154 | int i; |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 155 | |
| 156 | ASSERT_WDEV_LOCK(wdev); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 157 | |
Johannes Berg | b47f610 | 2014-09-10 13:39:54 +0300 | [diff] [blame] | 158 | kzfree(wdev->connect_keys); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 159 | wdev->connect_keys = NULL; |
| 160 | |
Kyeyoon Park | fa9ffc7 | 2013-12-16 23:01:30 -0800 | [diff] [blame] | 161 | rdev_set_qos_map(rdev, dev, NULL); |
| 162 | |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 163 | /* |
| 164 | * Delete all the keys ... pairwise keys can't really |
| 165 | * exist any more anyway, but default keys might. |
| 166 | */ |
| 167 | if (rdev->ops->del_key) |
| 168 | for (i = 0; i < 6; i++) |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 169 | rdev_del_key(rdev, dev, i, false, NULL); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 170 | |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 171 | if (wdev->current_bss) { |
| 172 | cfg80211_unhold_bss(wdev->current_bss); |
Johannes Berg | 5b112d3 | 2013-02-01 01:49:58 +0100 | [diff] [blame] | 173 | cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | wdev->current_bss = NULL; |
| 177 | wdev->ssid_len = 0; |
Michal Kazior | 9e0e296 | 2014-01-29 14:22:27 +0100 | [diff] [blame] | 178 | memset(&wdev->chandef, 0, sizeof(wdev->chandef)); |
Johannes Berg | 3d23e34 | 2009-09-29 23:27:28 +0200 | [diff] [blame] | 179 | #ifdef CONFIG_CFG80211_WEXT |
Johannes Berg | 9d30842 | 2009-04-20 18:43:46 +0200 | [diff] [blame] | 180 | if (!nowext) |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 181 | wdev->wext.ibss.ssid_len = 0; |
Johannes Berg | 9d30842 | 2009-04-20 18:43:46 +0200 | [diff] [blame] | 182 | #endif |
Vasanthakumar Thiagarajan | b35a51c | 2017-02-27 17:04:33 +0530 | [diff] [blame] | 183 | cfg80211_sched_dfs_chan_update(rdev); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 186 | void cfg80211_clear_ibss(struct net_device *dev, bool nowext) |
| 187 | { |
| 188 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 189 | |
| 190 | wdev_lock(wdev); |
| 191 | __cfg80211_clear_ibss(dev, nowext); |
| 192 | wdev_unlock(wdev); |
| 193 | } |
| 194 | |
Johannes Berg | 98d3a7c | 2009-11-18 13:03:43 +0100 | [diff] [blame] | 195 | int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev, |
| 196 | struct net_device *dev, bool nowext) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 197 | { |
Johannes Berg | 7848547 | 2009-07-07 03:56:05 +0200 | [diff] [blame] | 198 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 199 | int err; |
| 200 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 201 | ASSERT_WDEV_LOCK(wdev); |
| 202 | |
Johannes Berg | 7848547 | 2009-07-07 03:56:05 +0200 | [diff] [blame] | 203 | if (!wdev->ssid_len) |
| 204 | return -ENOLINK; |
| 205 | |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 206 | err = rdev_leave_ibss(rdev, dev); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 207 | |
| 208 | if (err) |
| 209 | return err; |
| 210 | |
Denis Kenzior | f8d16d3 | 2018-03-26 12:52:45 -0500 | [diff] [blame] | 211 | wdev->conn_owner_nlportid = 0; |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 212 | __cfg80211_clear_ibss(dev, nowext); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 217 | int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev, |
| 218 | struct net_device *dev, bool nowext) |
| 219 | { |
| 220 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 221 | int err; |
| 222 | |
| 223 | wdev_lock(wdev); |
| 224 | err = __cfg80211_leave_ibss(rdev, dev, nowext); |
| 225 | wdev_unlock(wdev); |
| 226 | |
| 227 | return err; |
| 228 | } |
| 229 | |
Johannes Berg | 3d23e34 | 2009-09-29 23:27:28 +0200 | [diff] [blame] | 230 | #ifdef CONFIG_CFG80211_WEXT |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 231 | int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev, |
| 232 | struct wireless_dev *wdev) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 233 | { |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 234 | struct cfg80211_cached_keys *ck = NULL; |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 235 | enum nl80211_band band; |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 236 | int i, err; |
| 237 | |
| 238 | ASSERT_WDEV_LOCK(wdev); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 239 | |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 240 | if (!wdev->wext.ibss.beacon_interval) |
| 241 | wdev->wext.ibss.beacon_interval = 100; |
Johannes Berg | 8e30bc5 | 2009-04-22 17:45:38 +0200 | [diff] [blame] | 242 | |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 243 | /* try to find an IBSS channel if none requested ... */ |
Johannes Berg | 683b6d3 | 2012-11-08 21:25:48 +0100 | [diff] [blame] | 244 | if (!wdev->wext.ibss.chandef.chan) { |
Simon Wunderlich | 1fe4517 | 2013-10-30 16:09:33 +0100 | [diff] [blame] | 245 | struct ieee80211_channel *new_chan = NULL; |
Johannes Berg | 683b6d3 | 2012-11-08 21:25:48 +0100 | [diff] [blame] | 246 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 247 | for (band = 0; band < NUM_NL80211_BANDS; band++) { |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 248 | struct ieee80211_supported_band *sband; |
| 249 | struct ieee80211_channel *chan; |
| 250 | |
| 251 | sband = rdev->wiphy.bands[band]; |
| 252 | if (!sband) |
| 253 | continue; |
| 254 | |
| 255 | for (i = 0; i < sband->n_channels; i++) { |
| 256 | chan = &sband->channels[i]; |
Luis R. Rodriguez | 8fe02e1 | 2013-10-21 19:22:25 +0200 | [diff] [blame] | 257 | if (chan->flags & IEEE80211_CHAN_NO_IR) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 258 | continue; |
| 259 | if (chan->flags & IEEE80211_CHAN_DISABLED) |
| 260 | continue; |
Simon Wunderlich | 1fe4517 | 2013-10-30 16:09:33 +0100 | [diff] [blame] | 261 | new_chan = chan; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 262 | break; |
| 263 | } |
| 264 | |
Simon Wunderlich | 1fe4517 | 2013-10-30 16:09:33 +0100 | [diff] [blame] | 265 | if (new_chan) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | |
Simon Wunderlich | 1fe4517 | 2013-10-30 16:09:33 +0100 | [diff] [blame] | 269 | if (!new_chan) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 270 | return -EINVAL; |
Simon Wunderlich | 1fe4517 | 2013-10-30 16:09:33 +0100 | [diff] [blame] | 271 | |
| 272 | cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan, |
| 273 | NL80211_CHAN_NO_HT); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* don't join -- SSID is not there */ |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 277 | if (!wdev->wext.ibss.ssid_len) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 278 | return 0; |
| 279 | |
| 280 | if (!netif_running(wdev->netdev)) |
| 281 | return 0; |
| 282 | |
Johannes Berg | 89b706f | 2016-09-13 16:39:38 +0200 | [diff] [blame] | 283 | if (wdev->wext.keys) |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 284 | wdev->wext.keys->def = wdev->wext.default_key; |
| 285 | |
| 286 | wdev->wext.ibss.privacy = wdev->wext.default_key != -1; |
| 287 | |
Johannes Berg | f1c1f17 | 2016-09-13 17:08:23 +0200 | [diff] [blame] | 288 | if (wdev->wext.keys && wdev->wext.keys->def != -1) { |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 289 | ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL); |
| 290 | if (!ck) |
| 291 | return -ENOMEM; |
David Spinadel | b867622 | 2016-09-22 23:16:50 +0300 | [diff] [blame] | 292 | for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 293 | ck->params[i].key = ck->data[i]; |
| 294 | } |
| 295 | err = __cfg80211_join_ibss(rdev, wdev->netdev, |
| 296 | &wdev->wext.ibss, ck); |
| 297 | if (err) |
| 298 | kfree(ck); |
| 299 | |
| 300 | return err; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | int cfg80211_ibss_wext_siwfreq(struct net_device *dev, |
| 304 | struct iw_request_info *info, |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 305 | struct iw_freq *wextfreq, char *extra) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 306 | { |
| 307 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
Zhao, Gang | f26cbf4 | 2014-04-21 12:53:03 +0800 | [diff] [blame] | 308 | struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 309 | struct ieee80211_channel *chan = NULL; |
| 310 | int err, freq; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 311 | |
| 312 | /* call only for ibss! */ |
| 313 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 314 | return -EINVAL; |
| 315 | |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 316 | if (!rdev->ops->join_ibss) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 317 | return -EOPNOTSUPP; |
| 318 | |
Zhao, Gang | 96998e3 | 2014-04-09 09:28:06 +0800 | [diff] [blame] | 319 | freq = cfg80211_wext_freq(wextfreq); |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 320 | if (freq < 0) |
| 321 | return freq; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 322 | |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 323 | if (freq) { |
| 324 | chan = ieee80211_get_channel(wdev->wiphy, freq); |
| 325 | if (!chan) |
| 326 | return -EINVAL; |
Luis R. Rodriguez | 8fe02e1 | 2013-10-21 19:22:25 +0200 | [diff] [blame] | 327 | if (chan->flags & IEEE80211_CHAN_NO_IR || |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 328 | chan->flags & IEEE80211_CHAN_DISABLED) |
| 329 | return -EINVAL; |
| 330 | } |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 331 | |
Johannes Berg | 683b6d3 | 2012-11-08 21:25:48 +0100 | [diff] [blame] | 332 | if (wdev->wext.ibss.chandef.chan == chan) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 333 | return 0; |
| 334 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 335 | wdev_lock(wdev); |
| 336 | err = 0; |
| 337 | if (wdev->ssid_len) |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 338 | err = __cfg80211_leave_ibss(rdev, dev, true); |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 339 | wdev_unlock(wdev); |
| 340 | |
| 341 | if (err) |
| 342 | return err; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 343 | |
| 344 | if (chan) { |
Simon Wunderlich | 1fe4517 | 2013-10-30 16:09:33 +0100 | [diff] [blame] | 345 | cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan, |
| 346 | NL80211_CHAN_NO_HT); |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 347 | wdev->wext.ibss.channel_fixed = true; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 348 | } else { |
| 349 | /* cfg80211_ibss_wext_join will pick one if needed */ |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 350 | wdev->wext.ibss.channel_fixed = false; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 353 | wdev_lock(wdev); |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 354 | err = cfg80211_ibss_wext_join(rdev, wdev); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 355 | wdev_unlock(wdev); |
| 356 | |
| 357 | return err; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 358 | } |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 359 | |
| 360 | int cfg80211_ibss_wext_giwfreq(struct net_device *dev, |
| 361 | struct iw_request_info *info, |
| 362 | struct iw_freq *freq, char *extra) |
| 363 | { |
| 364 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 365 | struct ieee80211_channel *chan = NULL; |
| 366 | |
| 367 | /* call only for ibss! */ |
| 368 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 369 | return -EINVAL; |
| 370 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 371 | wdev_lock(wdev); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 372 | if (wdev->current_bss) |
Johannes Berg | 19957bb | 2009-07-02 17:20:43 +0200 | [diff] [blame] | 373 | chan = wdev->current_bss->pub.channel; |
Johannes Berg | 683b6d3 | 2012-11-08 21:25:48 +0100 | [diff] [blame] | 374 | else if (wdev->wext.ibss.chandef.chan) |
| 375 | chan = wdev->wext.ibss.chandef.chan; |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 376 | wdev_unlock(wdev); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 377 | |
| 378 | if (chan) { |
| 379 | freq->m = chan->center_freq; |
| 380 | freq->e = 6; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | /* no channel if not joining */ |
| 385 | return -EINVAL; |
| 386 | } |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 387 | |
| 388 | int cfg80211_ibss_wext_siwessid(struct net_device *dev, |
| 389 | struct iw_request_info *info, |
| 390 | struct iw_point *data, char *ssid) |
| 391 | { |
| 392 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
Zhao, Gang | f26cbf4 | 2014-04-21 12:53:03 +0800 | [diff] [blame] | 393 | struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 394 | size_t len = data->length; |
| 395 | int err; |
| 396 | |
| 397 | /* call only for ibss! */ |
| 398 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 399 | return -EINVAL; |
| 400 | |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 401 | if (!rdev->ops->join_ibss) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 402 | return -EOPNOTSUPP; |
| 403 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 404 | wdev_lock(wdev); |
| 405 | err = 0; |
| 406 | if (wdev->ssid_len) |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 407 | err = __cfg80211_leave_ibss(rdev, dev, true); |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 408 | wdev_unlock(wdev); |
| 409 | |
| 410 | if (err) |
| 411 | return err; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 412 | |
| 413 | /* iwconfig uses nul termination in SSID.. */ |
| 414 | if (len > 0 && ssid[len - 1] == '\0') |
| 415 | len--; |
| 416 | |
Johannes Berg | c1e5f47 | 2014-05-19 17:53:16 +0200 | [diff] [blame] | 417 | memcpy(wdev->ssid, ssid, len); |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 418 | wdev->wext.ibss.ssid = wdev->ssid; |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 419 | wdev->wext.ibss.ssid_len = len; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 420 | |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 421 | wdev_lock(wdev); |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 422 | err = cfg80211_ibss_wext_join(rdev, wdev); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 423 | wdev_unlock(wdev); |
| 424 | |
| 425 | return err; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 426 | } |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 427 | |
| 428 | int cfg80211_ibss_wext_giwessid(struct net_device *dev, |
| 429 | struct iw_request_info *info, |
| 430 | struct iw_point *data, char *ssid) |
| 431 | { |
| 432 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 433 | |
| 434 | /* call only for ibss! */ |
| 435 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 436 | return -EINVAL; |
| 437 | |
| 438 | data->flags = 0; |
| 439 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 440 | wdev_lock(wdev); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 441 | if (wdev->ssid_len) { |
| 442 | data->flags = 1; |
| 443 | data->length = wdev->ssid_len; |
| 444 | memcpy(ssid, wdev->ssid, data->length); |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 445 | } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) { |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 446 | data->flags = 1; |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 447 | data->length = wdev->wext.ibss.ssid_len; |
| 448 | memcpy(ssid, wdev->wext.ibss.ssid, data->length); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 449 | } |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 450 | wdev_unlock(wdev); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 451 | |
| 452 | return 0; |
| 453 | } |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 454 | |
| 455 | int cfg80211_ibss_wext_siwap(struct net_device *dev, |
| 456 | struct iw_request_info *info, |
| 457 | struct sockaddr *ap_addr, char *extra) |
| 458 | { |
| 459 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
Zhao, Gang | f26cbf4 | 2014-04-21 12:53:03 +0800 | [diff] [blame] | 460 | struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 461 | u8 *bssid = ap_addr->sa_data; |
| 462 | int err; |
| 463 | |
| 464 | /* call only for ibss! */ |
| 465 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 466 | return -EINVAL; |
| 467 | |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 468 | if (!rdev->ops->join_ibss) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 469 | return -EOPNOTSUPP; |
| 470 | |
| 471 | if (ap_addr->sa_family != ARPHRD_ETHER) |
| 472 | return -EINVAL; |
| 473 | |
| 474 | /* automatic mode */ |
| 475 | if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid)) |
| 476 | bssid = NULL; |
| 477 | |
Johannes Berg | 74f8274 | 2014-04-09 22:32:16 +0200 | [diff] [blame] | 478 | if (bssid && !is_valid_ether_addr(bssid)) |
| 479 | return -EINVAL; |
| 480 | |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 481 | /* both automatic */ |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 482 | if (!bssid && !wdev->wext.ibss.bssid) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 483 | return 0; |
| 484 | |
| 485 | /* fixed already - and no change */ |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 486 | if (wdev->wext.ibss.bssid && bssid && |
Joe Perches | ac422d3 | 2012-05-08 18:56:55 +0000 | [diff] [blame] | 487 | ether_addr_equal(bssid, wdev->wext.ibss.bssid)) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 488 | return 0; |
| 489 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 490 | wdev_lock(wdev); |
| 491 | err = 0; |
| 492 | if (wdev->ssid_len) |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 493 | err = __cfg80211_leave_ibss(rdev, dev, true); |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 494 | wdev_unlock(wdev); |
| 495 | |
| 496 | if (err) |
| 497 | return err; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 498 | |
| 499 | if (bssid) { |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 500 | memcpy(wdev->wext.bssid, bssid, ETH_ALEN); |
| 501 | wdev->wext.ibss.bssid = wdev->wext.bssid; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 502 | } else |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 503 | wdev->wext.ibss.bssid = NULL; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 504 | |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 505 | wdev_lock(wdev); |
Johannes Berg | 59bbb6f | 2009-08-07 17:22:35 +0200 | [diff] [blame] | 506 | err = cfg80211_ibss_wext_join(rdev, wdev); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 507 | wdev_unlock(wdev); |
| 508 | |
| 509 | return err; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 510 | } |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 511 | |
| 512 | int cfg80211_ibss_wext_giwap(struct net_device *dev, |
| 513 | struct iw_request_info *info, |
| 514 | struct sockaddr *ap_addr, char *extra) |
| 515 | { |
| 516 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 517 | |
| 518 | /* call only for ibss! */ |
| 519 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 520 | return -EINVAL; |
| 521 | |
| 522 | ap_addr->sa_family = ARPHRD_ETHER; |
| 523 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 524 | wdev_lock(wdev); |
Johannes Berg | 7ebbe6b | 2009-07-01 21:26:48 +0200 | [diff] [blame] | 525 | if (wdev->current_bss) |
Johannes Berg | 19957bb | 2009-07-02 17:20:43 +0200 | [diff] [blame] | 526 | memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN); |
Zhu Yi | 80e5b06 | 2009-07-09 16:59:49 +0800 | [diff] [blame] | 527 | else if (wdev->wext.ibss.bssid) |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 528 | memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN); |
Zhu Yi | 80e5b06 | 2009-07-09 16:59:49 +0800 | [diff] [blame] | 529 | else |
Joe Perches | d2beae1 | 2015-03-02 19:54:58 -0800 | [diff] [blame] | 530 | eth_zero_addr(ap_addr->sa_data); |
Zhu Yi | 80e5b06 | 2009-07-09 16:59:49 +0800 | [diff] [blame] | 531 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 532 | wdev_unlock(wdev); |
| 533 | |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 534 | return 0; |
| 535 | } |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 536 | #endif |