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