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