ibss.c
Go to the documentation of this file.
00001 /*
00002  * IBSS mode implementation
00003  * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
00004  * Copyright 2004, Instant802 Networks, Inc.
00005  * Copyright 2005, Devicescape Software, Inc.
00006  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
00007  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
00008  * Copyright 2009, Johannes Berg <johannes@sipsolutions.net>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License version 2 as
00012  * published by the Free Software Foundation.
00013  */
00014 
00015 #include <linux/delay.h>
00016 #include <linux/slab.h>
00017 #include <linux/if_ether.h>
00018 #include <linux/skbuff.h>
00019 #include <linux/if_arp.h>
00020 #include <linux/etherdevice.h>
00021 #include <linux/rtnetlink.h>
00022 #include <net/mac80211.h>
00023 #include <asm/unaligned.h>
00024 
00025 #include "ieee80211_i.h"
00026 #include "driver-ops.h"
00027 #include "rate.h"
00028 #include "rt_wmp.h"
00029 
00030 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
00031 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
00032 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
00033 
00034 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
00035 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
00036 
00037 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
00038 
00039 
00040 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
00041                                         struct ieee80211_mgmt *mgmt,
00042                                         size_t len)
00043 {
00044         u16 auth_alg, auth_transaction;
00045 
00046         lockdep_assert_held(&sdata->u.ibss.mtx);
00047 
00048         if (len < 24 + 6)
00049                 return;
00050 
00051         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
00052         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
00053 
00054         /*
00055          * IEEE 802.11 standard does not require authentication in IBSS
00056          * networks and most implementations do not seem to use it.
00057          * However, try to reply to authentication attempts if someone
00058          * has actually implemented this.
00059          */
00060         if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1)
00061                 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
00062                                     sdata->u.ibss.bssid, NULL, 0, 0);
00063 }
00064 
00065 static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
00066                                       const u8 *bssid, const int beacon_int,
00067                                       struct ieee80211_channel *chan,
00068                                       const u32 basic_rates,
00069                                       const u16 capability, u64 tsf)
00070 {
00071         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00072         struct ieee80211_local *local = sdata->local;
00073         int rates, i;
00074         struct sk_buff *skb;
00075         struct ieee80211_mgmt *mgmt;
00076         u8 *pos;
00077         struct ieee80211_supported_band *sband;
00078         struct cfg80211_bss *bss;
00079         u32 bss_change;
00080         u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
00081 
00082         lockdep_assert_held(&ifibss->mtx);
00083 
00084         /* Reset own TSF to allow time synchronization work. */
00085         drv_reset_tsf(local, sdata);
00086 
00087         skb = ifibss->skb;
00088         RCU_INIT_POINTER(ifibss->presp, NULL);
00089         synchronize_rcu();
00090         skb->data = skb->head;
00091         skb->len = 0;
00092         skb_reset_tail_pointer(skb);
00093         skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
00094 
00095         if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
00096                 sta_info_flush(sdata->local, sdata);
00097 
00098         /* if merging, indicate to driver that we leave the old IBSS */
00099         if (sdata->vif.bss_conf.ibss_joined) {
00100                 sdata->vif.bss_conf.ibss_joined = false;
00101                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
00102         }
00103 
00104         memcpy(ifibss->bssid, bssid, ETH_ALEN);
00105 
00106         sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
00107 
00108         local->oper_channel = chan;
00109         WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
00110         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
00111 
00112         sband = local->hw.wiphy->bands[chan->band];
00113 
00114         /* build supported rates array */
00115         pos = supp_rates;
00116         for (i = 0; i < sband->n_bitrates; i++) {
00117                 int rate = sband->bitrates[i].bitrate;
00118                 u8 basic = 0;
00119                 if (basic_rates & BIT(i))
00120                         basic = 0x80;
00121                 *pos++ = basic | (u8) (rate / 5);
00122         }
00123 
00124         /* Build IBSS probe response */
00125         mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
00126         memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
00127         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
00128                                           IEEE80211_STYPE_PROBE_RESP);
00129         memset(mgmt->da, 0xff, ETH_ALEN);
00130         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
00131         memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
00132         mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
00133         mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
00134         mgmt->u.beacon.capab_info = cpu_to_le16(capability);
00135 
00136         pos = skb_put(skb, 2 + ifibss->ssid_len);
00137         *pos++ = WLAN_EID_SSID;
00138         *pos++ = ifibss->ssid_len;
00139         memcpy(pos, ifibss->ssid, ifibss->ssid_len);
00140 
00141         rates = sband->n_bitrates;
00142         if (rates > 8)
00143                 rates = 8;
00144         pos = skb_put(skb, 2 + rates);
00145         *pos++ = WLAN_EID_SUPP_RATES;
00146         *pos++ = rates;
00147         memcpy(pos, supp_rates, rates);
00148 
00149         if (sband->band == IEEE80211_BAND_2GHZ) {
00150                 pos = skb_put(skb, 2 + 1);
00151                 *pos++ = WLAN_EID_DS_PARAMS;
00152                 *pos++ = 1;
00153                 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
00154         }
00155 
00156         pos = skb_put(skb, 2 + 2);
00157         *pos++ = WLAN_EID_IBSS_PARAMS;
00158         *pos++ = 2;
00159         /* FIX: set ATIM window based on scan results */
00160         *pos++ = 0;
00161         *pos++ = 0;
00162 
00163         if (sband->n_bitrates > 8) {
00164                 rates = sband->n_bitrates - 8;
00165                 pos = skb_put(skb, 2 + rates);
00166                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
00167                 *pos++ = rates;
00168                 memcpy(pos, &supp_rates[8], rates);
00169         }
00170 
00171         if (ifibss->ie_len)
00172                 memcpy(skb_put(skb, ifibss->ie_len),
00173                        ifibss->ie, ifibss->ie_len);
00174 
00175         if (local->hw.queues >= 4) {
00176                 pos = skb_put(skb, 9);
00177                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
00178                 *pos++ = 7; /* len */
00179                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
00180                 *pos++ = 0x50;
00181                 *pos++ = 0xf2;
00182                 *pos++ = 2; /* WME */
00183                 *pos++ = 0; /* WME info */
00184                 *pos++ = 1; /* WME ver */
00185                 *pos++ = 0; /* U-APSD no in use */
00186         }
00187 
00188         rcu_assign_pointer(ifibss->presp, skb);
00189 
00190         sdata->vif.bss_conf.beacon_int = beacon_int;
00191         sdata->vif.bss_conf.basic_rates = basic_rates;
00192         bss_change = BSS_CHANGED_BEACON_INT;
00193         bss_change |= ieee80211_reset_erp_info(sdata);
00194         bss_change |= BSS_CHANGED_BSSID;
00195         bss_change |= BSS_CHANGED_BEACON;
00196         bss_change |= BSS_CHANGED_BEACON_ENABLED;
00197         bss_change |= BSS_CHANGED_BASIC_RATES;
00198         bss_change |= BSS_CHANGED_IBSS;
00199         sdata->vif.bss_conf.ibss_joined = true;
00200         ieee80211_bss_info_change_notify(sdata, bss_change);
00201 
00202         ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
00203 
00204         ifibss->state = IEEE80211_IBSS_MLME_JOINED;
00205         mod_timer(&ifibss->timer,
00206                   round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
00207 
00208         bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
00209                                         mgmt, skb->len, 0, GFP_KERNEL);
00210         cfg80211_put_bss(bss);
00211         cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
00212 }
00213 
00214 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
00215                                     struct ieee80211_bss *bss)
00216 {
00217         struct cfg80211_bss *cbss =
00218                 container_of((void *)bss, struct cfg80211_bss, priv);
00219         struct ieee80211_supported_band *sband;
00220         u32 basic_rates;
00221         int i, j;
00222         u16 beacon_int = cbss->beacon_interval;
00223 
00224         lockdep_assert_held(&sdata->u.ibss.mtx);
00225 
00226         if (beacon_int < 10)
00227                 beacon_int = 10;
00228 
00229         sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
00230 
00231         basic_rates = 0;
00232 
00233         for (i = 0; i < bss->supp_rates_len; i++) {
00234                 int rate = (bss->supp_rates[i] & 0x7f) * 5;
00235                 bool is_basic = !!(bss->supp_rates[i] & 0x80);
00236 
00237                 for (j = 0; j < sband->n_bitrates; j++) {
00238                         if (sband->bitrates[j].bitrate == rate) {
00239                                 if (is_basic)
00240                                         basic_rates |= BIT(j);
00241                                 break;
00242                         }
00243                 }
00244         }
00245 
00246         __ieee80211_sta_join_ibss(sdata, cbss->bssid,
00247                                   beacon_int,
00248                                   cbss->channel,
00249                                   basic_rates,
00250                                   cbss->capability,
00251                                   cbss->tsf);
00252 }
00253 
00254 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
00255                                   struct ieee80211_mgmt *mgmt,
00256                                   size_t len,
00257                                   struct ieee80211_rx_status *rx_status,
00258                                   struct ieee802_11_elems *elems,
00259                                   bool beacon)
00260 {
00261         struct ieee80211_local *local = sdata->local;
00262         int freq;
00263         struct cfg80211_bss *cbss;
00264         struct ieee80211_bss *bss;
00265         struct sta_info *sta;
00266         struct ieee80211_channel *channel;
00267         u64 beacon_timestamp, rx_timestamp;
00268         u32 supp_rates = 0;
00269         enum ieee80211_band band = rx_status->band;
00270 
00271         if (elems->ds_params && elems->ds_params_len == 1)
00272                 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
00273                                                       band);
00274         else
00275                 freq = rx_status->freq;
00276 
00277         channel = ieee80211_get_channel(local->hw.wiphy, freq);
00278 
00279         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
00280                 return;
00281 
00282         if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
00283             memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
00284 
00285                 rcu_read_lock();
00286                 sta = sta_info_get(sdata, mgmt->sa);
00287 
00288                 if (elems->supp_rates) {
00289                         supp_rates = ieee80211_sta_get_rates(local, elems,
00290                                                              band);
00291                         if (sta) {
00292                                 u32 prev_rates;
00293 
00294                                 prev_rates = sta->sta.supp_rates[band];
00295                                 /* make sure mandatory rates are always added */
00296                                 sta->sta.supp_rates[band] = supp_rates |
00297                                         ieee80211_mandatory_rates(local, band);
00298 
00299                                 if (sta->sta.supp_rates[band] != prev_rates) {
00300 #ifdef CONFIG_MAC80211_IBSS_DEBUG
00301                                         printk(KERN_DEBUG
00302                                                 "%s: updated supp_rates set "
00303                                                 "for %pM based on beacon"
00304                                                 "/probe_resp (0x%x -> 0x%x)\n",
00305                                                 sdata->name, sta->sta.addr,
00306                                                 prev_rates,
00307                                                 sta->sta.supp_rates[band]);
00308 #endif
00309                                         rate_control_rate_init(sta);
00310                                 }
00311                         } else
00312                                 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
00313                                                 mgmt->sa, supp_rates,
00314                                                 GFP_ATOMIC);
00315                 }
00316 
00317                 if (sta && elems->wmm_info)
00318                         set_sta_flag(sta, WLAN_STA_WME);
00319 
00320                 rcu_read_unlock();
00321         }
00322 
00323         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
00324                                         channel, beacon);
00325         if (!bss)
00326                 return;
00327 
00328         cbss = container_of((void *)bss, struct cfg80211_bss, priv);
00329 
00330         /* was just updated in ieee80211_bss_info_update */
00331         beacon_timestamp = cbss->tsf;
00332 
00333         /* check if we need to merge IBSS */
00334 
00335         /* we use a fixed BSSID */
00336         if (sdata->u.ibss.fixed_bssid)
00337                 goto put_bss;
00338 
00339         /* not an IBSS */
00340         if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
00341                 goto put_bss;
00342 
00343         /* different channel */
00344         if (cbss->channel != local->oper_channel)
00345                 goto put_bss;
00346 
00347         /* different SSID */
00348         if (elems->ssid_len != sdata->u.ibss.ssid_len ||
00349             memcmp(elems->ssid, sdata->u.ibss.ssid,
00350                                 sdata->u.ibss.ssid_len))
00351                 goto put_bss;
00352 
00353         /* same BSSID */
00354         if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
00355                 goto put_bss;
00356 
00357         if (rx_status->flag & RX_FLAG_MACTIME_MPDU) {
00358                 /*
00359                  * For correct IBSS merging we need mactime; since mactime is
00360                  * defined as the time the first data symbol of the frame hits
00361                  * the PHY, and the timestamp of the beacon is defined as "the
00362                  * time that the data symbol containing the first bit of the
00363                  * timestamp is transmitted to the PHY plus the transmitting
00364                  * STA's delays through its local PHY from the MAC-PHY
00365                  * interface to its interface with the WM" (802.11 11.1.2)
00366                  * - equals the time this bit arrives at the receiver - we have
00367                  * to take into account the offset between the two.
00368                  *
00369                  * E.g. at 1 MBit that means mactime is 192 usec earlier
00370                  * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
00371                  */
00372                 int rate;
00373 
00374                 if (rx_status->flag & RX_FLAG_HT)
00375                         rate = 65; /* TODO: HT rates */
00376                 else
00377                         rate = local->hw.wiphy->bands[band]->
00378                                 bitrates[rx_status->rate_idx].bitrate;
00379 
00380                 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
00381         } else {
00382                 /*
00383                  * second best option: get current TSF
00384                  * (will return -1 if not supported)
00385                  */
00386                 rx_timestamp = drv_get_tsf(local, sdata);
00387         }
00388 
00389 #ifdef CONFIG_MAC80211_IBSS_DEBUG
00390         printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
00391                "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
00392                mgmt->sa, mgmt->bssid,
00393                (unsigned long long)rx_timestamp,
00394                (unsigned long long)beacon_timestamp,
00395                (unsigned long long)(rx_timestamp - beacon_timestamp),
00396                jiffies);
00397 #endif
00398 
00399         if (beacon_timestamp > rx_timestamp) {
00400 #ifdef CONFIG_MAC80211_IBSS_DEBUG
00401                 printk(KERN_DEBUG "%s: beacon TSF higher than "
00402                        "local TSF - IBSS merge with BSSID %pM\n",
00403                        sdata->name, mgmt->bssid);
00404 #endif
00405                 ieee80211_sta_join_ibss(sdata, bss);
00406                 supp_rates = ieee80211_sta_get_rates(local, elems, band);
00407                 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
00408                                        supp_rates, GFP_KERNEL);
00409         }
00410 
00411  put_bss:
00412         ieee80211_rx_bss_put(local, bss);
00413 }
00414 
00415 /*
00416  * Add a new IBSS station, will also be called by the RX code when,
00417  * in IBSS mode, receiving a frame from a yet-unknown station, hence
00418  * must be callable in atomic context.
00419  */
00420 struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
00421                                         u8 *bssid, u8 *addr, u32 supp_rates,
00422                                         gfp_t gfp)
00423 {
00424         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00425         struct ieee80211_local *local = sdata->local;
00426         struct sta_info *sta;
00427         int band = local->hw.conf.channel->band;
00428 
00429         /*
00430          * XXX: Consider removing the least recently used entry and
00431          *      allow new one to be added.
00432          */
00433         if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
00434                 if (net_ratelimit())
00435                         printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
00436                                sdata->name, addr);
00437                 return NULL;
00438         }
00439 
00440         if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
00441                 return NULL;
00442 
00443         if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
00444                 return NULL;
00445 
00446 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
00447         wiphy_debug(local->hw.wiphy, "Adding new IBSS station %pM (dev=%s)\n",
00448                     addr, sdata->name);
00449 #endif
00450 
00451         sta = sta_info_alloc(sdata, addr, gfp);
00452         if (!sta)
00453                 return NULL;
00454 
00455         sta->last_rx = jiffies;
00456         set_sta_flag(sta, WLAN_STA_AUTHORIZED);
00457 
00458         /* make sure mandatory rates are always added */
00459         sta->sta.supp_rates[band] = supp_rates |
00460                         ieee80211_mandatory_rates(local, band);
00461 
00462         rate_control_rate_init(sta);
00463 
00464         /* If it fails, maybe we raced another insertion? */
00465         if (sta_info_insert(sta))
00466                 return sta_info_get(sdata, addr);
00467         return sta;
00468 }
00469 
00470 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
00471 {
00472         struct ieee80211_local *local = sdata->local;
00473         int active = 0;
00474         struct sta_info *sta;
00475 
00476         lockdep_assert_held(&sdata->u.ibss.mtx);
00477 
00478         rcu_read_lock();
00479 
00480         list_for_each_entry_rcu(sta, &local->sta_list, list) {
00481                 if (sta->sdata == sdata &&
00482                     time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
00483                                jiffies)) {
00484                         active++;
00485                         break;
00486                 }
00487         }
00488 
00489         rcu_read_unlock();
00490 
00491         return active;
00492 }
00493 
00494 /*
00495  * This function is called with state == IEEE80211_IBSS_MLME_JOINED
00496  */
00497 
00498 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
00499 {
00500         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00501 
00502         lockdep_assert_held(&ifibss->mtx);
00503 
00504         mod_timer(&ifibss->timer,
00505                   round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
00506 
00507         ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
00508 
00509         if (time_before(jiffies, ifibss->last_scan_completed +
00510                        IEEE80211_IBSS_MERGE_INTERVAL))
00511                 return;
00512 
00513         if (ieee80211_sta_active_ibss(sdata))
00514                 return;
00515 
00516         if (ifibss->fixed_channel)
00517                 return;
00518 
00519         printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
00520                "IBSS networks with same SSID (merge)\n", sdata->name);
00521 
00522         ieee80211_request_internal_scan(sdata,
00523                         ifibss->ssid, ifibss->ssid_len,
00524                         ifibss->fixed_channel ? ifibss->channel : NULL);
00525 }
00526 
00527 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
00528 {
00529         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00530         u8 bssid[ETH_ALEN];
00531         u16 capability;
00532         int i;
00533 
00534         lockdep_assert_held(&ifibss->mtx);
00535 
00536         if (ifibss->fixed_bssid) {
00537                 memcpy(bssid, ifibss->bssid, ETH_ALEN);
00538         } else {
00539                 /* Generate random, not broadcast, locally administered BSSID. Mix in
00540                  * own MAC address to make sure that devices that do not have proper
00541                  * random number generator get different BSSID. */
00542                 get_random_bytes(bssid, ETH_ALEN);
00543                 for (i = 0; i < ETH_ALEN; i++)
00544                         bssid[i] ^= sdata->vif.addr[i];
00545                 bssid[0] &= ~0x01;
00546                 bssid[0] |= 0x02;
00547         }
00548 
00549         printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
00550                sdata->name, bssid);
00551 
00552         capability = WLAN_CAPABILITY_IBSS;
00553 
00554         if (ifibss->privacy)
00555                 capability |= WLAN_CAPABILITY_PRIVACY;
00556         else
00557                 sdata->drop_unencrypted = 0;
00558 
00559         __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
00560                                   ifibss->channel, ifibss->basic_rates,
00561                                   capability, 0);
00562 }
00563 
00564 /*
00565  * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
00566  */
00567 
00568 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
00569 {
00570         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00571         struct ieee80211_local *local = sdata->local;
00572         struct cfg80211_bss *cbss;
00573         struct ieee80211_channel *chan = NULL;
00574         const u8 *bssid = NULL;
00575         int active_ibss;
00576         u16 capability;
00577 
00578         lockdep_assert_held(&ifibss->mtx);
00579 
00580         active_ibss = ieee80211_sta_active_ibss(sdata);
00581 #ifdef CONFIG_MAC80211_IBSS_DEBUG
00582         printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
00583                sdata->name, active_ibss);
00584 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
00585 
00586         if (active_ibss)
00587                 return;
00588 
00589         capability = WLAN_CAPABILITY_IBSS;
00590         if (ifibss->privacy)
00591                 capability |= WLAN_CAPABILITY_PRIVACY;
00592         if (ifibss->fixed_bssid)
00593                 bssid = ifibss->bssid;
00594         if (ifibss->fixed_channel)
00595                 chan = ifibss->channel;
00596         if (!is_zero_ether_addr(ifibss->bssid))
00597                 bssid = ifibss->bssid;
00598         cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
00599                                 ifibss->ssid, ifibss->ssid_len,
00600                                 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
00601                                 capability);
00602 
00603         if (cbss) {
00604                 struct ieee80211_bss *bss;
00605 
00606                 bss = (void *)cbss->priv;
00607 #ifdef CONFIG_MAC80211_IBSS_DEBUG
00608                 printk(KERN_DEBUG "   sta_find_ibss: selected %pM current "
00609                        "%pM\n", cbss->bssid, ifibss->bssid);
00610 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
00611 
00612                 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
00613                        " based on configured SSID\n",
00614                        sdata->name, cbss->bssid);
00615 
00616                 ieee80211_sta_join_ibss(sdata, bss);
00617                 ieee80211_rx_bss_put(local, bss);
00618                 return;
00619         }
00620 
00621 #ifdef CONFIG_MAC80211_IBSS_DEBUG
00622         printk(KERN_DEBUG "   did not try to join ibss\n");
00623 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
00624 
00625         /* Selected IBSS not found in current scan results - try to scan */
00626         if (time_after(jiffies, ifibss->last_scan_completed +
00627                                         IEEE80211_SCAN_INTERVAL)) {
00628                 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
00629                        "join\n", sdata->name);
00630 
00631                 ieee80211_request_internal_scan(sdata,
00632                                 ifibss->ssid, ifibss->ssid_len,
00633                                 ifibss->fixed_channel ? ifibss->channel : NULL);
00634         } else {
00635                 int interval = IEEE80211_SCAN_INTERVAL;
00636 
00637                 if (time_after(jiffies, ifibss->ibss_join_req +
00638                                IEEE80211_IBSS_JOIN_TIMEOUT)) {
00639                         if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
00640                                 ieee80211_sta_create_ibss(sdata);
00641                                 return;
00642                         }
00643                         printk(KERN_DEBUG "%s: IBSS not allowed on"
00644                                " %d MHz\n", sdata->name,
00645                                local->hw.conf.channel->center_freq);
00646 
00647                         /* No IBSS found - decrease scan interval and continue
00648                          * scanning. */
00649                         interval = IEEE80211_SCAN_INTERVAL_SLOW;
00650                 }
00651 
00652                 mod_timer(&ifibss->timer,
00653                           round_jiffies(jiffies + interval));
00654         }
00655 }
00656 
00657 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
00658                                         struct sk_buff *req)
00659 {
00660         struct ieee80211_mgmt *mgmt = (void *)req->data;
00661         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00662         struct ieee80211_local *local = sdata->local;
00663         int tx_last_beacon, len = req->len;
00664         struct sk_buff *skb;
00665         struct ieee80211_mgmt *resp;
00666         struct sk_buff *presp;
00667         u8 *pos, *end;
00668 
00669         lockdep_assert_held(&ifibss->mtx);
00670 
00671         presp = rcu_dereference_protected(ifibss->presp,
00672                                           lockdep_is_held(&ifibss->mtx));
00673 
00674         if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
00675             len < 24 + 2 || !presp)
00676                 return;
00677 
00678         tx_last_beacon = drv_tx_last_beacon(local);
00679 
00680 #ifdef CONFIG_MAC80211_IBSS_DEBUG
00681         printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
00682                " (tx_last_beacon=%d)\n",
00683                sdata->name, mgmt->sa, mgmt->da,
00684                mgmt->bssid, tx_last_beacon);
00685 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
00686 
00687         if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
00688                 return;
00689 
00690         if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
00691             memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
00692                 return;
00693 
00694         end = ((u8 *) mgmt) + len;
00695         pos = mgmt->u.probe_req.variable;
00696         if (pos[0] != WLAN_EID_SSID ||
00697             pos + 2 + pos[1] > end) {
00698 #ifdef CONFIG_MAC80211_IBSS_DEBUG
00699                 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
00700                        "from %pM\n",
00701                        sdata->name, mgmt->sa);
00702 #endif
00703                 return;
00704         }
00705         if (pos[1] != 0 &&
00706             (pos[1] != ifibss->ssid_len ||
00707              memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
00708                 /* Ignore ProbeReq for foreign SSID */
00709                 return;
00710         }
00711 
00712         /* Reply with ProbeResp */
00713         skb = skb_copy(presp, GFP_KERNEL);
00714         if (!skb)
00715                 return;
00716 
00717         resp = (struct ieee80211_mgmt *) skb->data;
00718         memcpy(resp->da, mgmt->sa, ETH_ALEN);
00719 #ifdef CONFIG_MAC80211_IBSS_DEBUG
00720         printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
00721                sdata->name, resp->da);
00722 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
00723         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
00724         ieee80211_tx_skb(sdata, skb);
00725 }
00726 
00727 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
00728                                          struct ieee80211_mgmt *mgmt,
00729                                          size_t len,
00730                                          struct ieee80211_rx_status *rx_status)
00731 {
00732         size_t baselen;
00733         struct ieee802_11_elems elems;
00734 
00735         if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
00736                 return; /* ignore ProbeResp to foreign address */
00737 
00738         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
00739         if (baselen > len)
00740                 return;
00741 
00742         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
00743                                 &elems);
00744 
00745         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
00746 }
00747 
00748 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
00749                                      struct ieee80211_mgmt *mgmt,
00750                                      size_t len,
00751                                      struct ieee80211_rx_status *rx_status)
00752 {
00753         size_t baselen;
00754         struct ieee802_11_elems elems;
00755 
00756         /* Process beacon from the current BSS */
00757         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
00758         if (baselen > len)
00759                 return;
00760 
00761         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
00762 
00763         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
00764 }
00765 
00766 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
00767                                    struct sk_buff *skb)
00768 {
00769         struct ieee80211_rx_status *rx_status;
00770         struct ieee80211_mgmt *mgmt;
00771         u16 fc;
00772 
00773         rx_status = IEEE80211_SKB_RXCB(skb);
00774         mgmt = (struct ieee80211_mgmt *) skb->data;
00775         fc = le16_to_cpu(mgmt->frame_control);
00776 
00777         mutex_lock(&sdata->u.ibss.mtx);
00778 
00779         if (!sdata->u.ibss.ssid_len)
00780                 goto mgmt_out; /* not ready to merge yet */
00781 
00782         switch (fc & IEEE80211_FCTL_STYPE) {
00783         case IEEE80211_STYPE_PROBE_REQ:
00784                 ieee80211_rx_mgmt_probe_req(sdata, skb);
00785                 break;
00786         case IEEE80211_STYPE_PROBE_RESP:
00787                 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
00788                                              rx_status);
00789                 break;
00790         case IEEE80211_STYPE_BEACON:
00791                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
00792                                          rx_status);
00793                 break;
00794         case IEEE80211_STYPE_AUTH:
00795                 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
00796                 break;
00797         }
00798 
00799  mgmt_out:
00800         mutex_unlock(&sdata->u.ibss.mtx);
00801 }
00802 
00803 void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
00804 {
00805         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00806 
00807         mutex_lock(&ifibss->mtx);
00808 
00809         /*
00810          * Work could be scheduled after scan or similar
00811          * when we aren't even joined (or trying) with a
00812          * network.
00813          */
00814         if (!ifibss->ssid_len)
00815                 goto out;
00816 
00817         switch (ifibss->state) {
00818         case IEEE80211_IBSS_MLME_SEARCH:
00819                 ieee80211_sta_find_ibss(sdata);
00820                 break;
00821         case IEEE80211_IBSS_MLME_JOINED:
00822                 ieee80211_sta_merge_ibss(sdata);
00823                 break;
00824         default:
00825                 WARN_ON(1);
00826                 break;
00827         }
00828 
00829  out:
00830         mutex_unlock(&ifibss->mtx);
00831 }
00832 
00833 static void ieee80211_ibss_timer(unsigned long data)
00834 {
00835         struct ieee80211_sub_if_data *sdata =
00836                 (struct ieee80211_sub_if_data *) data;
00837         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00838         struct ieee80211_local *local = sdata->local;
00839 
00840         if (local->quiescing) {
00841                 ifibss->timer_running = true;
00842                 return;
00843         }
00844 
00845         ieee80211_queue_work(&local->hw, &sdata->work);
00846 }
00847 
00848 #ifdef CONFIG_PM
00849 void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
00850 {
00851         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00852 
00853         if (del_timer_sync(&ifibss->timer))
00854                 ifibss->timer_running = true;
00855 }
00856 
00857 void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
00858 {
00859         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00860 
00861         if (ifibss->timer_running) {
00862                 add_timer(&ifibss->timer);
00863                 ifibss->timer_running = false;
00864         }
00865 }
00866 #endif
00867 
00868 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
00869 {
00870         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00871 
00872         setup_timer(&ifibss->timer, ieee80211_ibss_timer,
00873                     (unsigned long) sdata);
00874         mutex_init(&ifibss->mtx);
00875 }
00876 
00877 /* scan finished notification */
00878 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
00879 {
00880         struct ieee80211_sub_if_data *sdata;
00881 
00882         mutex_lock(&local->iflist_mtx);
00883         list_for_each_entry(sdata, &local->interfaces, list) {
00884                 if (!ieee80211_sdata_running(sdata))
00885                         continue;
00886                 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
00887                         continue;
00888                 sdata->u.ibss.last_scan_completed = jiffies;
00889                 ieee80211_queue_work(&local->hw, &sdata->work);
00890         }
00891         mutex_unlock(&local->iflist_mtx);
00892 }
00893 
00894 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
00895                         struct cfg80211_ibss_params *params)
00896 {
00897         struct sk_buff *skb;
00898 
00899         skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
00900                             36 /* bitrates */ +
00901                             34 /* SSID */ +
00902                             3  /* DS params */ +
00903                             4  /* IBSS params */ +
00904                             params->ie_len);
00905         if (!skb)
00906                 return -ENOMEM;
00907 
00908         mutex_lock(&sdata->u.ibss.mtx);
00909 
00910         if (params->bssid) {
00911                 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
00912                 sdata->u.ibss.fixed_bssid = true;
00913         } else
00914                 sdata->u.ibss.fixed_bssid = false;
00915 
00916         sdata->u.ibss.privacy = params->privacy;
00917         sdata->u.ibss.basic_rates = params->basic_rates;
00918 //DANI
00919 //      printk(KERN_ERR "Joining: basic rates: %d\n",params->basic_rates);
00920 //      params->mcast_rate[0]=4;
00921 //      params->mcast_rate[1]=4;
00922         params->beacon_interval=500;
00923 
00924         memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
00925                sizeof(params->mcast_rate));
00926 
00927         sdata->vif.bss_conf.beacon_int = params->beacon_interval;
00928 
00929         sdata->u.ibss.channel = params->channel;
00930         sdata->u.ibss.fixed_channel = params->channel_fixed;
00931 
00932         /* fix ourselves to that channel now already */
00933         if (params->channel_fixed) {
00934                 sdata->local->oper_channel = params->channel;
00935                 WARN_ON(!ieee80211_set_channel_type(sdata->local, sdata,
00936                                                     NL80211_CHAN_NO_HT));
00937         }
00938 
00939         if (params->ie) {
00940                 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
00941                                            GFP_KERNEL);
00942                 if (sdata->u.ibss.ie)
00943                         sdata->u.ibss.ie_len = params->ie_len;
00944         }
00945 
00946         sdata->u.ibss.skb = skb;
00947         sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
00948         sdata->u.ibss.ibss_join_req = jiffies;
00949 
00950         memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
00951         sdata->u.ibss.ssid_len = params->ssid_len;
00952 
00953         mutex_unlock(&sdata->u.ibss.mtx);
00954 
00955         mutex_lock(&sdata->local->mtx);
00956         ieee80211_recalc_idle(sdata->local);
00957         mutex_unlock(&sdata->local->mtx);
00958 
00959         ieee80211_queue_work(&sdata->local->hw, &sdata->work);
00960 
00961         return 0;
00962 }
00963 
00964 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
00965 {
00966         struct sk_buff *skb;
00967         struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
00968         struct ieee80211_local *local = sdata->local;
00969         struct cfg80211_bss *cbss;
00970         u16 capability;
00971         int active_ibss;
00972 
00973         mutex_lock(&sdata->u.ibss.mtx);
00974 
00975         sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
00976         memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
00977         sdata->u.ibss.ssid_len = 0;
00978 
00979         active_ibss = ieee80211_sta_active_ibss(sdata);
00980 
00981         if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
00982                 capability = WLAN_CAPABILITY_IBSS;
00983 
00984                 if (ifibss->privacy)
00985                         capability |= WLAN_CAPABILITY_PRIVACY;
00986 
00987                 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
00988                                         ifibss->bssid, ifibss->ssid,
00989                                         ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
00990                                         WLAN_CAPABILITY_PRIVACY,
00991                                         capability);
00992 
00993                 if (cbss) {
00994                         cfg80211_unlink_bss(local->hw.wiphy, cbss);
00995                         cfg80211_put_bss(cbss);
00996                 }
00997         }
00998 
00999         sta_info_flush(sdata->local, sdata);
01000 
01001         /* remove beacon */
01002         kfree(sdata->u.ibss.ie);
01003         skb = rcu_dereference_protected(sdata->u.ibss.presp,
01004                                         lockdep_is_held(&sdata->u.ibss.mtx));
01005         RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
01006         sdata->vif.bss_conf.ibss_joined = false;
01007         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
01008                                                 BSS_CHANGED_IBSS);
01009         synchronize_rcu();
01010         kfree_skb(skb);
01011 
01012         skb_queue_purge(&sdata->skb_queue);
01013 
01014         del_timer_sync(&sdata->u.ibss.timer);
01015 
01016         mutex_unlock(&sdata->u.ibss.mtx);
01017 
01018         mutex_lock(&local->mtx);
01019         ieee80211_recalc_idle(sdata->local);
01020         mutex_unlock(&local->mtx);
01021 
01022         return 0;
01023 }


ros_rt_wmp
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Fri Jan 3 2014 12:07:54