Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Some IBSS support code for cfg80211. |
| 3 | * |
| 4 | * Copyright 2009 Johannes Berg <johannes@sipsolutions.net> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/etherdevice.h> |
| 8 | #include <linux/if_arp.h> |
| 9 | #include <net/cfg80211.h> |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 10 | #include "nl80211.h" |
| 11 | |
| 12 | |
| 13 | void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp) |
| 14 | { |
| 15 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 16 | struct cfg80211_bss *bss; |
| 17 | #ifdef CONFIG_WIRELESS_EXT |
| 18 | union iwreq_data wrqu; |
| 19 | #endif |
| 20 | |
| 21 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 22 | return; |
| 23 | |
| 24 | if (WARN_ON(!wdev->ssid_len)) |
| 25 | return; |
| 26 | |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 27 | bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid, |
| 28 | wdev->ssid, wdev->ssid_len, |
| 29 | WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS); |
| 30 | |
| 31 | if (WARN_ON(!bss)) |
| 32 | return; |
| 33 | |
| 34 | if (wdev->current_bss) { |
| 35 | cfg80211_unhold_bss(wdev->current_bss); |
Johannes Berg | 19957bb | 2009-07-02 17:20:43 +0200 | [diff] [blame^] | 36 | cfg80211_put_bss(&wdev->current_bss->pub); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Johannes Berg | 19957bb | 2009-07-02 17:20:43 +0200 | [diff] [blame^] | 39 | cfg80211_hold_bss(bss_from_pub(bss)); |
| 40 | wdev->current_bss = bss_from_pub(bss); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 41 | |
| 42 | nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid, gfp); |
| 43 | #ifdef CONFIG_WIRELESS_EXT |
| 44 | memset(&wrqu, 0, sizeof(wrqu)); |
| 45 | memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN); |
| 46 | wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL); |
| 47 | #endif |
| 48 | } |
| 49 | EXPORT_SYMBOL(cfg80211_ibss_joined); |
| 50 | |
| 51 | int cfg80211_join_ibss(struct cfg80211_registered_device *rdev, |
| 52 | struct net_device *dev, |
| 53 | struct cfg80211_ibss_params *params) |
| 54 | { |
| 55 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 56 | int err; |
| 57 | |
| 58 | if (wdev->ssid_len) |
| 59 | return -EALREADY; |
| 60 | |
| 61 | #ifdef CONFIG_WIRELESS_EXT |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 62 | wdev->wext.ibss.channel = params->channel; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 63 | #endif |
| 64 | err = rdev->ops->join_ibss(&rdev->wiphy, dev, params); |
| 65 | |
| 66 | if (err) |
| 67 | return err; |
| 68 | |
| 69 | memcpy(wdev->ssid, params->ssid, params->ssid_len); |
| 70 | wdev->ssid_len = params->ssid_len; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Johannes Berg | 9d30842 | 2009-04-20 18:43:46 +0200 | [diff] [blame] | 75 | void cfg80211_clear_ibss(struct net_device *dev, bool nowext) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 76 | { |
| 77 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 78 | |
| 79 | if (wdev->current_bss) { |
| 80 | cfg80211_unhold_bss(wdev->current_bss); |
Johannes Berg | 19957bb | 2009-07-02 17:20:43 +0200 | [diff] [blame^] | 81 | cfg80211_put_bss(&wdev->current_bss->pub); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | wdev->current_bss = NULL; |
| 85 | wdev->ssid_len = 0; |
Johannes Berg | 9d30842 | 2009-04-20 18:43:46 +0200 | [diff] [blame] | 86 | #ifdef CONFIG_WIRELESS_EXT |
| 87 | if (!nowext) |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 88 | wdev->wext.ibss.ssid_len = 0; |
Johannes Berg | 9d30842 | 2009-04-20 18:43:46 +0200 | [diff] [blame] | 89 | #endif |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev, |
Johannes Berg | 9d30842 | 2009-04-20 18:43:46 +0200 | [diff] [blame] | 93 | struct net_device *dev, bool nowext) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 94 | { |
| 95 | int err; |
| 96 | |
| 97 | err = rdev->ops->leave_ibss(&rdev->wiphy, dev); |
| 98 | |
| 99 | if (err) |
| 100 | return err; |
| 101 | |
Johannes Berg | 9d30842 | 2009-04-20 18:43:46 +0200 | [diff] [blame] | 102 | cfg80211_clear_ibss(dev, nowext); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | #ifdef CONFIG_WIRELESS_EXT |
| 108 | static int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev, |
| 109 | struct wireless_dev *wdev) |
| 110 | { |
| 111 | enum ieee80211_band band; |
| 112 | int i; |
| 113 | |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 114 | if (!wdev->wext.ibss.beacon_interval) |
| 115 | wdev->wext.ibss.beacon_interval = 100; |
Johannes Berg | 8e30bc5 | 2009-04-22 17:45:38 +0200 | [diff] [blame] | 116 | |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 117 | /* try to find an IBSS channel if none requested ... */ |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 118 | if (!wdev->wext.ibss.channel) { |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 119 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 120 | struct ieee80211_supported_band *sband; |
| 121 | struct ieee80211_channel *chan; |
| 122 | |
| 123 | sband = rdev->wiphy.bands[band]; |
| 124 | if (!sband) |
| 125 | continue; |
| 126 | |
| 127 | for (i = 0; i < sband->n_channels; i++) { |
| 128 | chan = &sband->channels[i]; |
| 129 | if (chan->flags & IEEE80211_CHAN_NO_IBSS) |
| 130 | continue; |
| 131 | if (chan->flags & IEEE80211_CHAN_DISABLED) |
| 132 | continue; |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 133 | wdev->wext.ibss.channel = chan; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 137 | if (wdev->wext.ibss.channel) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 141 | if (!wdev->wext.ibss.channel) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 142 | return -EINVAL; |
| 143 | } |
| 144 | |
| 145 | /* don't join -- SSID is not there */ |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 146 | if (!wdev->wext.ibss.ssid_len) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 147 | return 0; |
| 148 | |
| 149 | if (!netif_running(wdev->netdev)) |
| 150 | return 0; |
| 151 | |
| 152 | return cfg80211_join_ibss(wiphy_to_dev(wdev->wiphy), |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 153 | wdev->netdev, &wdev->wext.ibss); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | int cfg80211_ibss_wext_siwfreq(struct net_device *dev, |
| 157 | struct iw_request_info *info, |
| 158 | struct iw_freq *freq, char *extra) |
| 159 | { |
| 160 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 161 | struct ieee80211_channel *chan; |
| 162 | int err; |
| 163 | |
| 164 | /* call only for ibss! */ |
| 165 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 166 | return -EINVAL; |
| 167 | |
| 168 | if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss) |
| 169 | return -EOPNOTSUPP; |
| 170 | |
| 171 | chan = cfg80211_wext_freq(wdev->wiphy, freq); |
| 172 | if (chan && IS_ERR(chan)) |
| 173 | return PTR_ERR(chan); |
| 174 | |
| 175 | if (chan && |
| 176 | (chan->flags & IEEE80211_CHAN_NO_IBSS || |
| 177 | chan->flags & IEEE80211_CHAN_DISABLED)) |
| 178 | return -EINVAL; |
| 179 | |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 180 | if (wdev->wext.ibss.channel == chan) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 181 | return 0; |
| 182 | |
| 183 | if (wdev->ssid_len) { |
Johannes Berg | 9d30842 | 2009-04-20 18:43:46 +0200 | [diff] [blame] | 184 | err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy), |
| 185 | dev, true); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 186 | if (err) |
| 187 | return err; |
| 188 | } |
| 189 | |
| 190 | if (chan) { |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 191 | wdev->wext.ibss.channel = chan; |
| 192 | wdev->wext.ibss.channel_fixed = true; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 193 | } else { |
| 194 | /* cfg80211_ibss_wext_join will pick one if needed */ |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 195 | wdev->wext.ibss.channel_fixed = false; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev); |
| 199 | } |
| 200 | /* temporary symbol - mark GPL - in the future the handler won't be */ |
| 201 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwfreq); |
| 202 | |
| 203 | int cfg80211_ibss_wext_giwfreq(struct net_device *dev, |
| 204 | struct iw_request_info *info, |
| 205 | struct iw_freq *freq, char *extra) |
| 206 | { |
| 207 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 208 | struct ieee80211_channel *chan = NULL; |
| 209 | |
| 210 | /* call only for ibss! */ |
| 211 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 212 | return -EINVAL; |
| 213 | |
| 214 | if (wdev->current_bss) |
Johannes Berg | 19957bb | 2009-07-02 17:20:43 +0200 | [diff] [blame^] | 215 | chan = wdev->current_bss->pub.channel; |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 216 | else if (wdev->wext.ibss.channel) |
| 217 | chan = wdev->wext.ibss.channel; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 218 | |
| 219 | if (chan) { |
| 220 | freq->m = chan->center_freq; |
| 221 | freq->e = 6; |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* no channel if not joining */ |
| 226 | return -EINVAL; |
| 227 | } |
| 228 | /* temporary symbol - mark GPL - in the future the handler won't be */ |
| 229 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwfreq); |
| 230 | |
| 231 | int cfg80211_ibss_wext_siwessid(struct net_device *dev, |
| 232 | struct iw_request_info *info, |
| 233 | struct iw_point *data, char *ssid) |
| 234 | { |
| 235 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 236 | size_t len = data->length; |
| 237 | int err; |
| 238 | |
| 239 | /* call only for ibss! */ |
| 240 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss) |
| 244 | return -EOPNOTSUPP; |
| 245 | |
| 246 | if (wdev->ssid_len) { |
Johannes Berg | 9d30842 | 2009-04-20 18:43:46 +0200 | [diff] [blame] | 247 | err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy), |
| 248 | dev, true); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 249 | if (err) |
| 250 | return err; |
| 251 | } |
| 252 | |
| 253 | /* iwconfig uses nul termination in SSID.. */ |
| 254 | if (len > 0 && ssid[len - 1] == '\0') |
| 255 | len--; |
| 256 | |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 257 | wdev->wext.ibss.ssid = wdev->ssid; |
| 258 | memcpy(wdev->wext.ibss.ssid, ssid, len); |
| 259 | wdev->wext.ibss.ssid_len = len; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 260 | |
| 261 | return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev); |
| 262 | } |
| 263 | /* temporary symbol - mark GPL - in the future the handler won't be */ |
| 264 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwessid); |
| 265 | |
| 266 | int cfg80211_ibss_wext_giwessid(struct net_device *dev, |
| 267 | struct iw_request_info *info, |
| 268 | struct iw_point *data, char *ssid) |
| 269 | { |
| 270 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 271 | |
| 272 | /* call only for ibss! */ |
| 273 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 274 | return -EINVAL; |
| 275 | |
| 276 | data->flags = 0; |
| 277 | |
| 278 | if (wdev->ssid_len) { |
| 279 | data->flags = 1; |
| 280 | data->length = wdev->ssid_len; |
| 281 | memcpy(ssid, wdev->ssid, data->length); |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 282 | } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) { |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 283 | data->flags = 1; |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 284 | data->length = wdev->wext.ibss.ssid_len; |
| 285 | memcpy(ssid, wdev->wext.ibss.ssid, data->length); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | /* temporary symbol - mark GPL - in the future the handler won't be */ |
| 291 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwessid); |
| 292 | |
| 293 | int cfg80211_ibss_wext_siwap(struct net_device *dev, |
| 294 | struct iw_request_info *info, |
| 295 | struct sockaddr *ap_addr, char *extra) |
| 296 | { |
| 297 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 298 | u8 *bssid = ap_addr->sa_data; |
| 299 | int err; |
| 300 | |
| 301 | /* call only for ibss! */ |
| 302 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 303 | return -EINVAL; |
| 304 | |
| 305 | if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss) |
| 306 | return -EOPNOTSUPP; |
| 307 | |
| 308 | if (ap_addr->sa_family != ARPHRD_ETHER) |
| 309 | return -EINVAL; |
| 310 | |
| 311 | /* automatic mode */ |
| 312 | if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid)) |
| 313 | bssid = NULL; |
| 314 | |
| 315 | /* both automatic */ |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 316 | if (!bssid && !wdev->wext.ibss.bssid) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 317 | return 0; |
| 318 | |
| 319 | /* fixed already - and no change */ |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 320 | if (wdev->wext.ibss.bssid && bssid && |
| 321 | compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0) |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 322 | return 0; |
| 323 | |
| 324 | if (wdev->ssid_len) { |
Johannes Berg | 9d30842 | 2009-04-20 18:43:46 +0200 | [diff] [blame] | 325 | err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy), |
| 326 | dev, true); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 327 | if (err) |
| 328 | return err; |
| 329 | } |
| 330 | |
| 331 | if (bssid) { |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 332 | memcpy(wdev->wext.bssid, bssid, ETH_ALEN); |
| 333 | wdev->wext.ibss.bssid = wdev->wext.bssid; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 334 | } else |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 335 | wdev->wext.ibss.bssid = NULL; |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 336 | |
| 337 | return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev); |
| 338 | } |
| 339 | /* temporary symbol - mark GPL - in the future the handler won't be */ |
| 340 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwap); |
| 341 | |
| 342 | int cfg80211_ibss_wext_giwap(struct net_device *dev, |
| 343 | struct iw_request_info *info, |
| 344 | struct sockaddr *ap_addr, char *extra) |
| 345 | { |
| 346 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 347 | |
| 348 | /* call only for ibss! */ |
| 349 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 350 | return -EINVAL; |
| 351 | |
| 352 | ap_addr->sa_family = ARPHRD_ETHER; |
| 353 | |
Johannes Berg | 7ebbe6b | 2009-07-01 21:26:48 +0200 | [diff] [blame] | 354 | if (wdev->current_bss) |
Johannes Berg | 19957bb | 2009-07-02 17:20:43 +0200 | [diff] [blame^] | 355 | memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN); |
Johannes Berg | 7ebbe6b | 2009-07-01 21:26:48 +0200 | [diff] [blame] | 356 | else |
Johannes Berg | cbe8fa9 | 2009-05-09 20:09:03 +0200 | [diff] [blame] | 357 | memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN); |
Johannes Berg | 04a773a | 2009-04-19 21:24:32 +0200 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | /* temporary symbol - mark GPL - in the future the handler won't be */ |
| 361 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwap); |
| 362 | #endif |