Johannes Berg | 4690029 | 2009-02-15 12:44:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * IBSS mode implementation |
| 3 | * Copyright 2003-2008, Jouni Malinen <j@w1.fi> |
| 4 | * Copyright 2004, Instant802 Networks, Inc. |
| 5 | * Copyright 2005, Devicescape Software, Inc. |
| 6 | * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> |
| 7 | * Copyright 2007, Michael Wu <flamingice@sourmilk.net> |
| 8 | * Copyright 2009, Johannes Berg <johannes@sipsolutions.net> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/if_ether.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/if_arp.h> |
| 19 | #include <linux/etherdevice.h> |
| 20 | #include <linux/rtnetlink.h> |
| 21 | #include <net/mac80211.h> |
| 22 | #include <asm/unaligned.h> |
| 23 | |
| 24 | #include "ieee80211_i.h" |
| 25 | #include "rate.h" |
| 26 | |
| 27 | #define IEEE80211_SCAN_INTERVAL (2 * HZ) |
| 28 | #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ) |
| 29 | #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ) |
| 30 | |
| 31 | #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ) |
| 32 | #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ) |
| 33 | |
| 34 | #define IEEE80211_IBSS_MAX_STA_ENTRIES 128 |
| 35 | |
| 36 | |
| 37 | static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata, |
| 38 | struct ieee80211_mgmt *mgmt, |
| 39 | size_t len) |
| 40 | { |
| 41 | u16 auth_alg, auth_transaction, status_code; |
| 42 | |
| 43 | if (len < 24 + 6) |
| 44 | return; |
| 45 | |
| 46 | auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg); |
| 47 | auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction); |
| 48 | status_code = le16_to_cpu(mgmt->u.auth.status_code); |
| 49 | |
| 50 | /* |
| 51 | * IEEE 802.11 standard does not require authentication in IBSS |
| 52 | * networks and most implementations do not seem to use it. |
| 53 | * However, try to reply to authentication attempts if someone |
| 54 | * has actually implemented this. |
| 55 | */ |
| 56 | if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1) |
| 57 | ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0, |
| 58 | sdata->u.ibss.bssid, 0); |
| 59 | } |
| 60 | |
| 61 | static int __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata, |
| 62 | const u8 *bssid, const int beacon_int, |
| 63 | const int freq, |
| 64 | const size_t supp_rates_len, |
| 65 | const u8 *supp_rates, |
| 66 | const u16 capability) |
| 67 | { |
| 68 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 69 | struct ieee80211_local *local = sdata->local; |
| 70 | int res = 0, rates, i, j; |
| 71 | struct sk_buff *skb; |
| 72 | struct ieee80211_mgmt *mgmt; |
| 73 | u8 *pos; |
| 74 | struct ieee80211_supported_band *sband; |
| 75 | union iwreq_data wrqu; |
| 76 | |
| 77 | if (local->ops->reset_tsf) { |
| 78 | /* Reset own TSF to allow time synchronization work. */ |
| 79 | local->ops->reset_tsf(local_to_hw(local)); |
| 80 | } |
| 81 | |
| 82 | if ((ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET) && |
| 83 | memcmp(ifibss->bssid, bssid, ETH_ALEN) == 0) |
| 84 | return res; |
| 85 | |
| 86 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400); |
| 87 | if (!skb) { |
| 88 | printk(KERN_DEBUG "%s: failed to allocate buffer for probe " |
| 89 | "response\n", sdata->dev->name); |
| 90 | return -ENOMEM; |
| 91 | } |
| 92 | |
| 93 | if (!(ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET)) { |
| 94 | /* Remove possible STA entries from other IBSS networks. */ |
| 95 | sta_info_flush_delayed(sdata); |
| 96 | } |
| 97 | |
| 98 | memcpy(ifibss->bssid, bssid, ETH_ALEN); |
| 99 | res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID); |
| 100 | if (res) |
| 101 | return res; |
| 102 | |
| 103 | local->hw.conf.beacon_int = beacon_int >= 10 ? beacon_int : 10; |
| 104 | |
| 105 | sdata->drop_unencrypted = capability & |
| 106 | WLAN_CAPABILITY_PRIVACY ? 1 : 0; |
| 107 | |
| 108 | res = ieee80211_set_freq(sdata, freq); |
| 109 | |
| 110 | if (res) |
| 111 | return res; |
| 112 | |
| 113 | sband = local->hw.wiphy->bands[local->hw.conf.channel->band]; |
| 114 | |
| 115 | /* Build IBSS probe response */ |
| 116 | |
| 117 | skb_reserve(skb, local->hw.extra_tx_headroom); |
| 118 | |
| 119 | mgmt = (struct ieee80211_mgmt *) |
| 120 | skb_put(skb, 24 + sizeof(mgmt->u.beacon)); |
| 121 | memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon)); |
| 122 | mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | |
| 123 | IEEE80211_STYPE_PROBE_RESP); |
| 124 | memset(mgmt->da, 0xff, ETH_ALEN); |
| 125 | memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN); |
| 126 | memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN); |
| 127 | mgmt->u.beacon.beacon_int = |
| 128 | cpu_to_le16(local->hw.conf.beacon_int); |
| 129 | mgmt->u.beacon.capab_info = cpu_to_le16(capability); |
| 130 | |
| 131 | pos = skb_put(skb, 2 + ifibss->ssid_len); |
| 132 | *pos++ = WLAN_EID_SSID; |
| 133 | *pos++ = ifibss->ssid_len; |
| 134 | memcpy(pos, ifibss->ssid, ifibss->ssid_len); |
| 135 | |
| 136 | rates = supp_rates_len; |
| 137 | if (rates > 8) |
| 138 | rates = 8; |
| 139 | pos = skb_put(skb, 2 + rates); |
| 140 | *pos++ = WLAN_EID_SUPP_RATES; |
| 141 | *pos++ = rates; |
| 142 | memcpy(pos, supp_rates, rates); |
| 143 | |
| 144 | if (sband->band == IEEE80211_BAND_2GHZ) { |
| 145 | pos = skb_put(skb, 2 + 1); |
| 146 | *pos++ = WLAN_EID_DS_PARAMS; |
| 147 | *pos++ = 1; |
| 148 | *pos++ = ieee80211_frequency_to_channel(freq); |
| 149 | } |
| 150 | |
| 151 | pos = skb_put(skb, 2 + 2); |
| 152 | *pos++ = WLAN_EID_IBSS_PARAMS; |
| 153 | *pos++ = 2; |
| 154 | /* FIX: set ATIM window based on scan results */ |
| 155 | *pos++ = 0; |
| 156 | *pos++ = 0; |
| 157 | |
| 158 | if (supp_rates_len > 8) { |
| 159 | rates = supp_rates_len - 8; |
| 160 | pos = skb_put(skb, 2 + rates); |
| 161 | *pos++ = WLAN_EID_EXT_SUPP_RATES; |
| 162 | *pos++ = rates; |
| 163 | memcpy(pos, &supp_rates[8], rates); |
| 164 | } |
| 165 | |
| 166 | ifibss->probe_resp = skb; |
| 167 | |
| 168 | ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON | |
| 169 | IEEE80211_IFCC_BEACON_ENABLED); |
| 170 | |
| 171 | |
| 172 | rates = 0; |
| 173 | for (i = 0; i < supp_rates_len; i++) { |
| 174 | int bitrate = (supp_rates[i] & 0x7f) * 5; |
| 175 | for (j = 0; j < sband->n_bitrates; j++) |
| 176 | if (sband->bitrates[j].bitrate == bitrate) |
| 177 | rates |= BIT(j); |
| 178 | } |
| 179 | |
| 180 | ieee80211_sta_def_wmm_params(sdata, supp_rates_len, supp_rates); |
| 181 | |
| 182 | ifibss->flags |= IEEE80211_IBSS_PREV_BSSID_SET; |
| 183 | ifibss->state = IEEE80211_IBSS_MLME_JOINED; |
| 184 | mod_timer(&ifibss->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL); |
| 185 | |
| 186 | memset(&wrqu, 0, sizeof(wrqu)); |
| 187 | memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN); |
| 188 | wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL); |
| 189 | |
| 190 | return res; |
| 191 | } |
| 192 | |
| 193 | static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata, |
| 194 | struct ieee80211_bss *bss) |
| 195 | { |
| 196 | return __ieee80211_sta_join_ibss(sdata, |
| 197 | bss->cbss.bssid, |
| 198 | bss->cbss.beacon_interval, |
| 199 | bss->cbss.channel->center_freq, |
| 200 | bss->supp_rates_len, bss->supp_rates, |
| 201 | bss->cbss.capability); |
| 202 | } |
| 203 | |
| 204 | static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata, |
| 205 | struct ieee80211_mgmt *mgmt, |
| 206 | size_t len, |
| 207 | struct ieee80211_rx_status *rx_status, |
| 208 | struct ieee802_11_elems *elems, |
| 209 | bool beacon) |
| 210 | { |
| 211 | struct ieee80211_local *local = sdata->local; |
| 212 | int freq; |
| 213 | struct ieee80211_bss *bss; |
| 214 | struct sta_info *sta; |
| 215 | struct ieee80211_channel *channel; |
| 216 | u64 beacon_timestamp, rx_timestamp; |
| 217 | u32 supp_rates = 0; |
| 218 | enum ieee80211_band band = rx_status->band; |
| 219 | |
| 220 | if (elems->ds_params && elems->ds_params_len == 1) |
| 221 | freq = ieee80211_channel_to_frequency(elems->ds_params[0]); |
| 222 | else |
| 223 | freq = rx_status->freq; |
| 224 | |
| 225 | channel = ieee80211_get_channel(local->hw.wiphy, freq); |
| 226 | |
| 227 | if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) |
| 228 | return; |
| 229 | |
| 230 | if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates && |
| 231 | memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) { |
| 232 | supp_rates = ieee80211_sta_get_rates(local, elems, band); |
| 233 | |
| 234 | rcu_read_lock(); |
| 235 | |
| 236 | sta = sta_info_get(local, mgmt->sa); |
| 237 | if (sta) { |
| 238 | u32 prev_rates; |
| 239 | |
| 240 | prev_rates = sta->sta.supp_rates[band]; |
| 241 | /* make sure mandatory rates are always added */ |
| 242 | sta->sta.supp_rates[band] = supp_rates | |
| 243 | ieee80211_mandatory_rates(local, band); |
| 244 | |
| 245 | #ifdef CONFIG_MAC80211_IBSS_DEBUG |
| 246 | if (sta->sta.supp_rates[band] != prev_rates) |
| 247 | printk(KERN_DEBUG "%s: updated supp_rates set " |
| 248 | "for %pM based on beacon info (0x%llx | " |
| 249 | "0x%llx -> 0x%llx)\n", |
| 250 | sdata->dev->name, |
| 251 | sta->sta.addr, |
| 252 | (unsigned long long) prev_rates, |
| 253 | (unsigned long long) supp_rates, |
| 254 | (unsigned long long) sta->sta.supp_rates[band]); |
| 255 | #endif |
| 256 | } else |
| 257 | ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates); |
| 258 | |
| 259 | rcu_read_unlock(); |
| 260 | } |
| 261 | |
| 262 | bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems, |
| 263 | channel, beacon); |
| 264 | if (!bss) |
| 265 | return; |
| 266 | |
| 267 | /* was just updated in ieee80211_bss_info_update */ |
| 268 | beacon_timestamp = bss->cbss.tsf; |
| 269 | |
| 270 | /* check if we need to merge IBSS */ |
| 271 | |
| 272 | /* merge only on beacons (???) */ |
| 273 | if (!beacon) |
| 274 | goto put_bss; |
| 275 | |
| 276 | /* we use a fixed BSSID */ |
| 277 | if (sdata->u.ibss.flags & IEEE80211_IBSS_BSSID_SET) |
| 278 | goto put_bss; |
| 279 | |
| 280 | /* not an IBSS */ |
| 281 | if (!(bss->cbss.capability & WLAN_CAPABILITY_IBSS)) |
| 282 | goto put_bss; |
| 283 | |
| 284 | /* different channel */ |
| 285 | if (bss->cbss.channel != local->oper_channel) |
| 286 | goto put_bss; |
| 287 | |
| 288 | /* different SSID */ |
| 289 | if (elems->ssid_len != sdata->u.ibss.ssid_len || |
| 290 | memcmp(elems->ssid, sdata->u.ibss.ssid, |
| 291 | sdata->u.ibss.ssid_len)) |
| 292 | goto put_bss; |
| 293 | |
Alina Friedrichsen | 34e8f08 | 2009-02-22 00:07:28 +0100 | [diff] [blame^] | 294 | /* same BSSID */ |
| 295 | if (memcmp(bss->cbss.bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) |
| 296 | goto put_bss; |
| 297 | |
Johannes Berg | 4690029 | 2009-02-15 12:44:28 +0100 | [diff] [blame] | 298 | if (rx_status->flag & RX_FLAG_TSFT) { |
| 299 | /* |
| 300 | * For correct IBSS merging we need mactime; since mactime is |
| 301 | * defined as the time the first data symbol of the frame hits |
| 302 | * the PHY, and the timestamp of the beacon is defined as "the |
| 303 | * time that the data symbol containing the first bit of the |
| 304 | * timestamp is transmitted to the PHY plus the transmitting |
| 305 | * STA's delays through its local PHY from the MAC-PHY |
| 306 | * interface to its interface with the WM" (802.11 11.1.2) |
| 307 | * - equals the time this bit arrives at the receiver - we have |
| 308 | * to take into account the offset between the two. |
| 309 | * |
| 310 | * E.g. at 1 MBit that means mactime is 192 usec earlier |
| 311 | * (=24 bytes * 8 usecs/byte) than the beacon timestamp. |
| 312 | */ |
| 313 | int rate; |
| 314 | |
| 315 | if (rx_status->flag & RX_FLAG_HT) |
| 316 | rate = 65; /* TODO: HT rates */ |
| 317 | else |
| 318 | rate = local->hw.wiphy->bands[band]-> |
| 319 | bitrates[rx_status->rate_idx].bitrate; |
| 320 | |
| 321 | rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate); |
| 322 | } else if (local && local->ops && local->ops->get_tsf) |
| 323 | /* second best option: get current TSF */ |
| 324 | rx_timestamp = local->ops->get_tsf(local_to_hw(local)); |
| 325 | else |
| 326 | /* can't merge without knowing the TSF */ |
| 327 | rx_timestamp = -1LLU; |
| 328 | |
| 329 | #ifdef CONFIG_MAC80211_IBSS_DEBUG |
| 330 | printk(KERN_DEBUG "RX beacon SA=%pM BSSID=" |
| 331 | "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n", |
| 332 | mgmt->sa, mgmt->bssid, |
| 333 | (unsigned long long)rx_timestamp, |
| 334 | (unsigned long long)beacon_timestamp, |
| 335 | (unsigned long long)(rx_timestamp - beacon_timestamp), |
| 336 | jiffies); |
| 337 | #endif |
| 338 | |
| 339 | if (beacon_timestamp > rx_timestamp) { |
| 340 | #ifdef CONFIG_MAC80211_IBSS_DEBUG |
| 341 | printk(KERN_DEBUG "%s: beacon TSF higher than " |
| 342 | "local TSF - IBSS merge with BSSID %pM\n", |
| 343 | sdata->dev->name, mgmt->bssid); |
| 344 | #endif |
| 345 | ieee80211_sta_join_ibss(sdata, bss); |
| 346 | ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates); |
| 347 | } |
| 348 | |
| 349 | put_bss: |
| 350 | ieee80211_rx_bss_put(local, bss); |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Add a new IBSS station, will also be called by the RX code when, |
| 355 | * in IBSS mode, receiving a frame from a yet-unknown station, hence |
| 356 | * must be callable in atomic context. |
| 357 | */ |
| 358 | struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, |
| 359 | u8 *bssid,u8 *addr, u32 supp_rates) |
| 360 | { |
| 361 | struct ieee80211_local *local = sdata->local; |
| 362 | struct sta_info *sta; |
| 363 | int band = local->hw.conf.channel->band; |
| 364 | |
| 365 | /* TODO: Could consider removing the least recently used entry and |
| 366 | * allow new one to be added. */ |
| 367 | if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) { |
| 368 | if (net_ratelimit()) { |
| 369 | printk(KERN_DEBUG "%s: No room for a new IBSS STA " |
| 370 | "entry %pM\n", sdata->dev->name, addr); |
| 371 | } |
| 372 | return NULL; |
| 373 | } |
| 374 | |
| 375 | if (compare_ether_addr(bssid, sdata->u.ibss.bssid)) |
| 376 | return NULL; |
| 377 | |
| 378 | #ifdef CONFIG_MAC80211_VERBOSE_DEBUG |
| 379 | printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n", |
| 380 | wiphy_name(local->hw.wiphy), addr, sdata->dev->name); |
| 381 | #endif |
| 382 | |
| 383 | sta = sta_info_alloc(sdata, addr, GFP_ATOMIC); |
| 384 | if (!sta) |
| 385 | return NULL; |
| 386 | |
| 387 | set_sta_flags(sta, WLAN_STA_AUTHORIZED); |
| 388 | |
| 389 | /* make sure mandatory rates are always added */ |
| 390 | sta->sta.supp_rates[band] = supp_rates | |
| 391 | ieee80211_mandatory_rates(local, band); |
| 392 | |
| 393 | rate_control_rate_init(sta); |
| 394 | |
| 395 | if (sta_info_insert(sta)) |
| 396 | return NULL; |
| 397 | |
| 398 | return sta; |
| 399 | } |
| 400 | |
| 401 | static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata) |
| 402 | { |
| 403 | struct ieee80211_local *local = sdata->local; |
| 404 | int active = 0; |
| 405 | struct sta_info *sta; |
| 406 | |
| 407 | rcu_read_lock(); |
| 408 | |
| 409 | list_for_each_entry_rcu(sta, &local->sta_list, list) { |
| 410 | if (sta->sdata == sdata && |
| 411 | time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL, |
| 412 | jiffies)) { |
| 413 | active++; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | rcu_read_unlock(); |
| 419 | |
| 420 | return active; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata) |
| 425 | { |
| 426 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 427 | |
| 428 | mod_timer(&ifibss->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL); |
| 429 | |
| 430 | ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT); |
| 431 | if (ieee80211_sta_active_ibss(sdata)) |
| 432 | return; |
| 433 | |
| 434 | if ((ifibss->flags & IEEE80211_IBSS_BSSID_SET) && |
| 435 | (!(ifibss->flags & IEEE80211_IBSS_AUTO_CHANNEL_SEL))) |
| 436 | return; |
| 437 | |
| 438 | printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other " |
| 439 | "IBSS networks with same SSID (merge)\n", sdata->dev->name); |
| 440 | |
| 441 | /* XXX maybe racy? */ |
| 442 | if (sdata->local->scan_req) |
| 443 | return; |
| 444 | |
| 445 | memcpy(sdata->local->int_scan_req.ssids[0].ssid, |
| 446 | ifibss->ssid, IEEE80211_MAX_SSID_LEN); |
| 447 | sdata->local->int_scan_req.ssids[0].ssid_len = ifibss->ssid_len; |
| 448 | ieee80211_request_scan(sdata, &sdata->local->int_scan_req); |
| 449 | } |
| 450 | |
| 451 | static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata) |
| 452 | { |
| 453 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 454 | struct ieee80211_local *local = sdata->local; |
| 455 | struct ieee80211_supported_band *sband; |
| 456 | u8 *pos; |
| 457 | u8 bssid[ETH_ALEN]; |
| 458 | u8 supp_rates[IEEE80211_MAX_SUPP_RATES]; |
| 459 | u16 capability; |
| 460 | int i; |
| 461 | |
| 462 | if (ifibss->flags & IEEE80211_IBSS_BSSID_SET) { |
| 463 | memcpy(bssid, ifibss->bssid, ETH_ALEN); |
| 464 | } else { |
| 465 | /* Generate random, not broadcast, locally administered BSSID. Mix in |
| 466 | * own MAC address to make sure that devices that do not have proper |
| 467 | * random number generator get different BSSID. */ |
| 468 | get_random_bytes(bssid, ETH_ALEN); |
| 469 | for (i = 0; i < ETH_ALEN; i++) |
| 470 | bssid[i] ^= sdata->dev->dev_addr[i]; |
| 471 | bssid[0] &= ~0x01; |
| 472 | bssid[0] |= 0x02; |
| 473 | } |
| 474 | |
| 475 | printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n", |
| 476 | sdata->dev->name, bssid); |
| 477 | |
| 478 | sband = local->hw.wiphy->bands[local->hw.conf.channel->band]; |
| 479 | |
| 480 | if (local->hw.conf.beacon_int == 0) |
| 481 | local->hw.conf.beacon_int = 100; |
| 482 | |
| 483 | capability = WLAN_CAPABILITY_IBSS; |
| 484 | |
| 485 | if (sdata->default_key) |
| 486 | capability |= WLAN_CAPABILITY_PRIVACY; |
| 487 | else |
| 488 | sdata->drop_unencrypted = 0; |
| 489 | |
| 490 | pos = supp_rates; |
| 491 | for (i = 0; i < sband->n_bitrates; i++) { |
| 492 | int rate = sband->bitrates[i].bitrate; |
| 493 | *pos++ = (u8) (rate / 5); |
| 494 | } |
| 495 | |
| 496 | return __ieee80211_sta_join_ibss(sdata, |
| 497 | bssid, local->hw.conf.beacon_int, |
| 498 | local->hw.conf.channel->center_freq, |
| 499 | sband->n_bitrates, supp_rates, |
| 500 | capability); |
| 501 | } |
| 502 | |
| 503 | static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata) |
| 504 | { |
| 505 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 506 | struct ieee80211_local *local = sdata->local; |
| 507 | struct ieee80211_bss *bss; |
| 508 | const u8 *bssid = NULL; |
| 509 | int active_ibss; |
| 510 | |
| 511 | if (ifibss->ssid_len == 0) |
| 512 | return -EINVAL; |
| 513 | |
| 514 | active_ibss = ieee80211_sta_active_ibss(sdata); |
| 515 | #ifdef CONFIG_MAC80211_IBSS_DEBUG |
| 516 | printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n", |
| 517 | sdata->dev->name, active_ibss); |
| 518 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ |
| 519 | |
| 520 | if (active_ibss) |
| 521 | return 0; |
| 522 | |
| 523 | if (ifibss->flags & IEEE80211_IBSS_BSSID_SET) |
| 524 | bssid = ifibss->bssid; |
| 525 | bss = (void *)cfg80211_get_bss(local->hw.wiphy, NULL, bssid, |
| 526 | ifibss->ssid, ifibss->ssid_len, |
| 527 | WLAN_CAPABILITY_IBSS, |
| 528 | WLAN_CAPABILITY_IBSS); |
| 529 | |
| 530 | #ifdef CONFIG_MAC80211_IBSS_DEBUG |
| 531 | if (bss) |
| 532 | printk(KERN_DEBUG " sta_find_ibss: selected %pM current " |
| 533 | "%pM\n", bss->cbss.bssid, ifibss->bssid); |
| 534 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ |
| 535 | |
| 536 | if (bss && |
| 537 | (!(ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET) || |
| 538 | memcmp(ifibss->bssid, bss->cbss.bssid, ETH_ALEN))) { |
| 539 | int ret; |
| 540 | |
| 541 | printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM" |
| 542 | " based on configured SSID\n", |
| 543 | sdata->dev->name, bss->cbss.bssid); |
| 544 | |
| 545 | ret = ieee80211_sta_join_ibss(sdata, bss); |
| 546 | ieee80211_rx_bss_put(local, bss); |
| 547 | return ret; |
| 548 | } else if (bss) |
| 549 | ieee80211_rx_bss_put(local, bss); |
| 550 | |
| 551 | #ifdef CONFIG_MAC80211_IBSS_DEBUG |
| 552 | printk(KERN_DEBUG " did not try to join ibss\n"); |
| 553 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ |
| 554 | |
| 555 | /* Selected IBSS not found in current scan results - try to scan */ |
| 556 | if (ifibss->state == IEEE80211_IBSS_MLME_JOINED && |
| 557 | !ieee80211_sta_active_ibss(sdata)) { |
| 558 | mod_timer(&ifibss->timer, jiffies + |
| 559 | IEEE80211_IBSS_MERGE_INTERVAL); |
| 560 | } else if (time_after(jiffies, local->last_scan_completed + |
| 561 | IEEE80211_SCAN_INTERVAL)) { |
| 562 | printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to " |
| 563 | "join\n", sdata->dev->name); |
| 564 | |
| 565 | /* XXX maybe racy? */ |
| 566 | if (local->scan_req) |
| 567 | return -EBUSY; |
| 568 | |
| 569 | memcpy(local->int_scan_req.ssids[0].ssid, |
| 570 | ifibss->ssid, IEEE80211_MAX_SSID_LEN); |
| 571 | local->int_scan_req.ssids[0].ssid_len = ifibss->ssid_len; |
| 572 | return ieee80211_request_scan(sdata, &local->int_scan_req); |
| 573 | } else if (ifibss->state != IEEE80211_IBSS_MLME_JOINED) { |
| 574 | int interval = IEEE80211_SCAN_INTERVAL; |
| 575 | |
| 576 | if (time_after(jiffies, ifibss->ibss_join_req + |
| 577 | IEEE80211_IBSS_JOIN_TIMEOUT)) { |
| 578 | if (!(local->oper_channel->flags & |
| 579 | IEEE80211_CHAN_NO_IBSS)) |
| 580 | return ieee80211_sta_create_ibss(sdata); |
| 581 | printk(KERN_DEBUG "%s: IBSS not allowed on" |
| 582 | " %d MHz\n", sdata->dev->name, |
| 583 | local->hw.conf.channel->center_freq); |
| 584 | |
| 585 | /* No IBSS found - decrease scan interval and continue |
| 586 | * scanning. */ |
| 587 | interval = IEEE80211_SCAN_INTERVAL_SLOW; |
| 588 | } |
| 589 | |
| 590 | ifibss->state = IEEE80211_IBSS_MLME_SEARCH; |
| 591 | mod_timer(&ifibss->timer, jiffies + interval); |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata, |
| 599 | struct ieee80211_mgmt *mgmt, |
| 600 | size_t len) |
| 601 | { |
| 602 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 603 | struct ieee80211_local *local = sdata->local; |
| 604 | int tx_last_beacon; |
| 605 | struct sk_buff *skb; |
| 606 | struct ieee80211_mgmt *resp; |
| 607 | u8 *pos, *end; |
| 608 | |
| 609 | if (ifibss->state != IEEE80211_IBSS_MLME_JOINED || |
| 610 | len < 24 + 2 || !ifibss->probe_resp) |
| 611 | return; |
| 612 | |
| 613 | if (local->ops->tx_last_beacon) |
| 614 | tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local)); |
| 615 | else |
| 616 | tx_last_beacon = 1; |
| 617 | |
| 618 | #ifdef CONFIG_MAC80211_IBSS_DEBUG |
| 619 | printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM" |
| 620 | " (tx_last_beacon=%d)\n", |
| 621 | sdata->dev->name, mgmt->sa, mgmt->da, |
| 622 | mgmt->bssid, tx_last_beacon); |
| 623 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ |
| 624 | |
| 625 | if (!tx_last_beacon) |
| 626 | return; |
| 627 | |
| 628 | if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 && |
| 629 | memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0) |
| 630 | return; |
| 631 | |
| 632 | end = ((u8 *) mgmt) + len; |
| 633 | pos = mgmt->u.probe_req.variable; |
| 634 | if (pos[0] != WLAN_EID_SSID || |
| 635 | pos + 2 + pos[1] > end) { |
| 636 | #ifdef CONFIG_MAC80211_IBSS_DEBUG |
| 637 | printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq " |
| 638 | "from %pM\n", |
| 639 | sdata->dev->name, mgmt->sa); |
| 640 | #endif |
| 641 | return; |
| 642 | } |
| 643 | if (pos[1] != 0 && |
| 644 | (pos[1] != ifibss->ssid_len || |
| 645 | memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len) != 0)) { |
| 646 | /* Ignore ProbeReq for foreign SSID */ |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | /* Reply with ProbeResp */ |
| 651 | skb = skb_copy(ifibss->probe_resp, GFP_KERNEL); |
| 652 | if (!skb) |
| 653 | return; |
| 654 | |
| 655 | resp = (struct ieee80211_mgmt *) skb->data; |
| 656 | memcpy(resp->da, mgmt->sa, ETH_ALEN); |
| 657 | #ifdef CONFIG_MAC80211_IBSS_DEBUG |
| 658 | printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n", |
| 659 | sdata->dev->name, resp->da); |
| 660 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ |
| 661 | ieee80211_tx_skb(sdata, skb, 0); |
| 662 | } |
| 663 | |
| 664 | static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata, |
| 665 | struct ieee80211_mgmt *mgmt, |
| 666 | size_t len, |
| 667 | struct ieee80211_rx_status *rx_status) |
| 668 | { |
| 669 | size_t baselen; |
| 670 | struct ieee802_11_elems elems; |
| 671 | |
| 672 | if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN)) |
| 673 | return; /* ignore ProbeResp to foreign address */ |
| 674 | |
| 675 | baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt; |
| 676 | if (baselen > len) |
| 677 | return; |
| 678 | |
| 679 | ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen, |
| 680 | &elems); |
| 681 | |
| 682 | ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false); |
| 683 | } |
| 684 | |
| 685 | static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata, |
| 686 | struct ieee80211_mgmt *mgmt, |
| 687 | size_t len, |
| 688 | struct ieee80211_rx_status *rx_status) |
| 689 | { |
| 690 | size_t baselen; |
| 691 | struct ieee802_11_elems elems; |
| 692 | |
| 693 | /* Process beacon from the current BSS */ |
| 694 | baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt; |
| 695 | if (baselen > len) |
| 696 | return; |
| 697 | |
| 698 | ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems); |
| 699 | |
| 700 | ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true); |
| 701 | } |
| 702 | |
| 703 | static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata, |
| 704 | struct sk_buff *skb) |
| 705 | { |
| 706 | struct ieee80211_rx_status *rx_status; |
| 707 | struct ieee80211_mgmt *mgmt; |
| 708 | u16 fc; |
| 709 | |
| 710 | rx_status = (struct ieee80211_rx_status *) skb->cb; |
| 711 | mgmt = (struct ieee80211_mgmt *) skb->data; |
| 712 | fc = le16_to_cpu(mgmt->frame_control); |
| 713 | |
| 714 | switch (fc & IEEE80211_FCTL_STYPE) { |
| 715 | case IEEE80211_STYPE_PROBE_REQ: |
| 716 | ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len); |
| 717 | break; |
| 718 | case IEEE80211_STYPE_PROBE_RESP: |
| 719 | ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, |
| 720 | rx_status); |
| 721 | break; |
| 722 | case IEEE80211_STYPE_BEACON: |
| 723 | ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, |
| 724 | rx_status); |
| 725 | break; |
| 726 | case IEEE80211_STYPE_AUTH: |
| 727 | ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len); |
| 728 | break; |
| 729 | } |
| 730 | |
| 731 | kfree_skb(skb); |
| 732 | } |
| 733 | |
| 734 | static void ieee80211_ibss_work(struct work_struct *work) |
| 735 | { |
| 736 | struct ieee80211_sub_if_data *sdata = |
| 737 | container_of(work, struct ieee80211_sub_if_data, u.ibss.work); |
| 738 | struct ieee80211_local *local = sdata->local; |
| 739 | struct ieee80211_if_ibss *ifibss; |
| 740 | struct sk_buff *skb; |
| 741 | |
| 742 | if (!netif_running(sdata->dev)) |
| 743 | return; |
| 744 | |
| 745 | if (local->sw_scanning || local->hw_scanning) |
| 746 | return; |
| 747 | |
| 748 | if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_ADHOC)) |
| 749 | return; |
| 750 | ifibss = &sdata->u.ibss; |
| 751 | |
| 752 | while ((skb = skb_dequeue(&ifibss->skb_queue))) |
| 753 | ieee80211_ibss_rx_queued_mgmt(sdata, skb); |
| 754 | |
| 755 | if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request)) |
| 756 | return; |
| 757 | |
| 758 | switch (ifibss->state) { |
| 759 | case IEEE80211_IBSS_MLME_SEARCH: |
| 760 | ieee80211_sta_find_ibss(sdata); |
| 761 | break; |
| 762 | case IEEE80211_IBSS_MLME_JOINED: |
| 763 | ieee80211_sta_merge_ibss(sdata); |
| 764 | break; |
| 765 | default: |
| 766 | WARN_ON(1); |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | static void ieee80211_ibss_timer(unsigned long data) |
| 772 | { |
| 773 | struct ieee80211_sub_if_data *sdata = |
| 774 | (struct ieee80211_sub_if_data *) data; |
| 775 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 776 | struct ieee80211_local *local = sdata->local; |
| 777 | |
| 778 | set_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request); |
| 779 | queue_work(local->hw.workqueue, &ifibss->work); |
| 780 | } |
| 781 | |
| 782 | void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata) |
| 783 | { |
| 784 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 785 | |
| 786 | INIT_WORK(&ifibss->work, ieee80211_ibss_work); |
| 787 | setup_timer(&ifibss->timer, ieee80211_ibss_timer, |
| 788 | (unsigned long) sdata); |
| 789 | skb_queue_head_init(&ifibss->skb_queue); |
| 790 | |
| 791 | ifibss->flags |= IEEE80211_IBSS_AUTO_BSSID_SEL | |
| 792 | IEEE80211_IBSS_AUTO_CHANNEL_SEL; |
| 793 | } |
| 794 | |
Alina Friedrichsen | 79f6440 | 2009-02-21 01:27:29 +0100 | [diff] [blame] | 795 | int ieee80211_ibss_commit(struct ieee80211_sub_if_data *sdata) |
| 796 | { |
| 797 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 798 | |
| 799 | ifibss->flags &= ~IEEE80211_IBSS_PREV_BSSID_SET; |
| 800 | |
| 801 | if (ifibss->ssid_len) |
| 802 | ifibss->flags |= IEEE80211_IBSS_SSID_SET; |
| 803 | else |
| 804 | ifibss->flags &= ~IEEE80211_IBSS_SSID_SET; |
| 805 | |
| 806 | ifibss->ibss_join_req = jiffies; |
| 807 | ifibss->state = IEEE80211_IBSS_MLME_SEARCH; |
| 808 | |
| 809 | return ieee80211_sta_find_ibss(sdata); |
| 810 | } |
| 811 | |
Johannes Berg | 4690029 | 2009-02-15 12:44:28 +0100 | [diff] [blame] | 812 | int ieee80211_ibss_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len) |
| 813 | { |
| 814 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 815 | |
| 816 | if (len > IEEE80211_MAX_SSID_LEN) |
| 817 | return -EINVAL; |
| 818 | |
| 819 | if (ifibss->ssid_len != len || memcmp(ifibss->ssid, ssid, len) != 0) { |
| 820 | memset(ifibss->ssid, 0, sizeof(ifibss->ssid)); |
| 821 | memcpy(ifibss->ssid, ssid, len); |
| 822 | ifibss->ssid_len = len; |
| 823 | } |
| 824 | |
Alina Friedrichsen | 79f6440 | 2009-02-21 01:27:29 +0100 | [diff] [blame] | 825 | return ieee80211_ibss_commit(sdata); |
Johannes Berg | 4690029 | 2009-02-15 12:44:28 +0100 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | int ieee80211_ibss_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len) |
| 829 | { |
| 830 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 831 | |
| 832 | memcpy(ssid, ifibss->ssid, ifibss->ssid_len); |
| 833 | *len = ifibss->ssid_len; |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | int ieee80211_ibss_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid) |
| 839 | { |
| 840 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
| 841 | |
| 842 | if (is_valid_ether_addr(bssid)) { |
| 843 | memcpy(ifibss->bssid, bssid, ETH_ALEN); |
| 844 | ifibss->flags |= IEEE80211_IBSS_BSSID_SET; |
| 845 | } else { |
| 846 | memset(ifibss->bssid, 0, ETH_ALEN); |
| 847 | ifibss->flags &= ~IEEE80211_IBSS_BSSID_SET; |
| 848 | } |
| 849 | |
| 850 | if (netif_running(sdata->dev)) { |
| 851 | if (ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID)) { |
| 852 | printk(KERN_DEBUG "%s: Failed to config new BSSID to " |
| 853 | "the low-level driver\n", sdata->dev->name); |
| 854 | } |
| 855 | } |
| 856 | |
Alina Friedrichsen | 79f6440 | 2009-02-21 01:27:29 +0100 | [diff] [blame] | 857 | return ieee80211_ibss_commit(sdata); |
Johannes Berg | 4690029 | 2009-02-15 12:44:28 +0100 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /* scan finished notification */ |
| 861 | void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local) |
| 862 | { |
| 863 | struct ieee80211_sub_if_data *sdata = local->scan_sdata; |
| 864 | struct ieee80211_if_ibss *ifibss; |
| 865 | |
| 866 | if (sdata && sdata->vif.type == NL80211_IFTYPE_ADHOC) { |
| 867 | ifibss = &sdata->u.ibss; |
| 868 | if ((!(ifibss->flags & IEEE80211_IBSS_PREV_BSSID_SET)) || |
| 869 | !ieee80211_sta_active_ibss(sdata)) |
| 870 | ieee80211_sta_find_ibss(sdata); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | ieee80211_rx_result |
| 875 | ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb, |
| 876 | struct ieee80211_rx_status *rx_status) |
| 877 | { |
| 878 | struct ieee80211_local *local = sdata->local; |
| 879 | struct ieee80211_mgmt *mgmt; |
| 880 | u16 fc; |
| 881 | |
| 882 | if (skb->len < 24) |
| 883 | return RX_DROP_MONITOR; |
| 884 | |
| 885 | mgmt = (struct ieee80211_mgmt *) skb->data; |
| 886 | fc = le16_to_cpu(mgmt->frame_control); |
| 887 | |
| 888 | switch (fc & IEEE80211_FCTL_STYPE) { |
| 889 | case IEEE80211_STYPE_PROBE_RESP: |
| 890 | case IEEE80211_STYPE_BEACON: |
| 891 | memcpy(skb->cb, rx_status, sizeof(*rx_status)); |
| 892 | case IEEE80211_STYPE_PROBE_REQ: |
| 893 | case IEEE80211_STYPE_AUTH: |
| 894 | skb_queue_tail(&sdata->u.ibss.skb_queue, skb); |
| 895 | queue_work(local->hw.workqueue, &sdata->u.ibss.work); |
| 896 | return RX_QUEUED; |
| 897 | } |
| 898 | |
| 899 | return RX_DROP_MONITOR; |
| 900 | } |