blob: 79e294e9b5cc279f3bc4efa5eb4260a9c5b6660d [file] [log] [blame]
Johannes Berg46900292009-02-15 12:44:28 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Johannes Berg46900292009-02-15 12:44:28 +010017#include <linux/if_ether.h>
18#include <linux/skbuff.h>
19#include <linux/if_arp.h>
20#include <linux/etherdevice.h>
21#include <linux/rtnetlink.h>
22#include <net/mac80211.h>
Johannes Berg46900292009-02-15 12:44:28 +010023
24#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020025#include "driver-ops.h"
Johannes Berg46900292009-02-15 12:44:28 +010026#include "rate.h"
27
28#define IEEE80211_SCAN_INTERVAL (2 * HZ)
Johannes Berg46900292009-02-15 12:44:28 +010029#define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
30
31#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
32#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
Antonio Quartullidad9def2013-07-26 21:15:02 +020033#define IEEE80211_IBSS_RSN_INACTIVITY_LIMIT (10 * HZ)
Johannes Berg46900292009-02-15 12:44:28 +010034
35#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
36
37
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020038static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
39 const u8 *bssid, const int beacon_int,
40 struct ieee80211_channel *chan,
Johannes Bergb59066a2009-05-12 21:18:38 +020041 const u32 basic_rates,
Sujith Manoharanc13a7652012-10-12 17:35:45 +053042 const u16 capability, u64 tsf,
43 bool creator)
Johannes Berg46900292009-02-15 12:44:28 +010044{
45 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46 struct ieee80211_local *local = sdata->local;
Simon Wunderlich2103dec2013-07-08 16:55:53 +020047 int rates_n = 0, i, ri;
Johannes Berg46900292009-02-15 12:44:28 +010048 struct ieee80211_mgmt *mgmt;
49 u8 *pos;
50 struct ieee80211_supported_band *sband;
Johannes Bergf446d102009-10-28 15:12:32 +010051 struct cfg80211_bss *bss;
Simon Wunderlich2103dec2013-07-08 16:55:53 +020052 u32 bss_change, rate_flags, rates = 0, rates_added = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +010053 struct cfg80211_chan_def chandef;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +020054 enum nl80211_bss_scan_width scan_width;
Simon Wunderlich2ec9c1f2013-07-11 18:07:46 +020055 bool have_higher_than_11mbit = false;
Johannes Bergc3ffeab2013-03-07 20:54:29 +010056 struct beacon_data *presp;
57 int frame_len;
Simon Wunderlich2103dec2013-07-08 16:55:53 +020058 int shift;
Johannes Berg24487982009-04-23 18:52:52 +020059
Johannes Berg8d61ffa2013-05-10 12:32:47 +020060 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +020061
Johannes Berg24487982009-04-23 18:52:52 +020062 /* Reset own TSF to allow time synchronization work. */
Eliad Peller37a41b42011-09-21 14:06:11 +030063 drv_reset_tsf(local, sdata);
Johannes Berg46900292009-02-15 12:44:28 +010064
Joe Perchesb203ca32012-05-08 18:56:52 +000065 if (!ether_addr_equal(ifibss->bssid, bssid))
Johannes Bergb998e8b2012-12-13 23:07:46 +010066 sta_info_flush(sdata);
Johannes Berg46900292009-02-15 12:44:28 +010067
Johannes Berg49b5c7f2010-04-29 21:34:01 +020068 /* if merging, indicate to driver that we leave the old IBSS */
69 if (sdata->vif.bss_conf.ibss_joined) {
70 sdata->vif.bss_conf.ibss_joined = false;
Sujith Manoharanc13a7652012-10-12 17:35:45 +053071 sdata->vif.bss_conf.ibss_creator = false;
Johannes Berg1852d402013-03-07 20:22:28 +010072 sdata->vif.bss_conf.enable_beacon = false;
Eliad Peller86a2ea42011-11-08 15:36:59 +020073 netif_carrier_off(sdata->dev);
Johannes Berg1852d402013-03-07 20:22:28 +010074 ieee80211_bss_info_change_notify(sdata,
75 BSS_CHANGED_IBSS |
76 BSS_CHANGED_BEACON_ENABLED);
Johannes Berg49b5c7f2010-04-29 21:34:01 +020077 }
78
Johannes Bergc3ffeab2013-03-07 20:54:29 +010079 presp = rcu_dereference_protected(ifibss->presp,
Johannes Berg8d61ffa2013-05-10 12:32:47 +020080 lockdep_is_held(&sdata->wdev.mtx));
Johannes Bergc3ffeab2013-03-07 20:54:29 +010081 rcu_assign_pointer(ifibss->presp, NULL);
82 if (presp)
83 kfree_rcu(presp, rcu_head);
Johannes Berg1852d402013-03-07 20:22:28 +010084
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020085 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
Johannes Berg46900292009-02-15 12:44:28 +010086
Simon Wunderlich3aede782013-05-16 13:00:36 +020087 chandef = ifibss->chandef;
Johannes Berg4bf88532012-11-09 11:39:59 +010088 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef)) {
Simon Wunderlich4b42aab2013-07-08 16:55:57 +020089 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
90 chandef.width == NL80211_CHAN_WIDTH_10 ||
91 chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
92 chandef.width == NL80211_CHAN_WIDTH_20) {
93 sdata_info(sdata,
94 "Failed to join IBSS, beacons forbidden\n");
95 return;
96 }
Johannes Berg4bf88532012-11-09 11:39:59 +010097 chandef.width = NL80211_CHAN_WIDTH_20;
98 chandef.center_freq1 = chan->center_freq;
99 }
Johannes Berg55de9082012-07-26 17:24:39 +0200100
101 ieee80211_vif_release_channel(sdata);
Johannes Berg4bf88532012-11-09 11:39:59 +0100102 if (ieee80211_vif_use_channel(sdata, &chandef,
Johannes Berg55de9082012-07-26 17:24:39 +0200103 ifibss->fixed_channel ?
104 IEEE80211_CHANCTX_SHARED :
105 IEEE80211_CHANCTX_EXCLUSIVE)) {
106 sdata_info(sdata, "Failed to join IBSS, no channel context\n");
107 return;
Alexander Simon13c40c52011-11-30 16:56:34 +0100108 }
Johannes Berg55de9082012-07-26 17:24:39 +0200109
110 memcpy(ifibss->bssid, bssid, ETH_ALEN);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200111
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200112 sband = local->hw.wiphy->bands[chan->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200113 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg46900292009-02-15 12:44:28 +0100114
115 /* Build IBSS probe response */
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100116 frame_len = sizeof(struct ieee80211_hdr_3addr) +
117 12 /* struct ieee80211_mgmt.u.beacon */ +
118 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
119 2 + 8 /* max Supported Rates */ +
120 3 /* max DS params */ +
121 4 /* IBSS params */ +
122 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
123 2 + sizeof(struct ieee80211_ht_cap) +
124 2 + sizeof(struct ieee80211_ht_operation) +
125 ifibss->ie_len;
126 presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
127 if (!presp)
128 return;
129
130 presp->head = (void *)(presp + 1);
131
132 mgmt = (void *) presp->head;
Johannes Berg46900292009-02-15 12:44:28 +0100133 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
134 IEEE80211_STYPE_PROBE_RESP);
Johannes Berge83e6542012-07-13 16:23:07 +0200135 eth_broadcast_addr(mgmt->da);
Johannes Berg47846c92009-11-25 17:46:19 +0100136 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100137 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200138 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
Sujith707c1b42009-03-03 10:15:10 +0530139 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100140 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
141
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100142 pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
143
Johannes Berg46900292009-02-15 12:44:28 +0100144 *pos++ = WLAN_EID_SSID;
145 *pos++ = ifibss->ssid_len;
146 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100147 pos += ifibss->ssid_len;
Johannes Berg46900292009-02-15 12:44:28 +0100148
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200149 rate_flags = ieee80211_chandef_rate_flags(&chandef);
150 for (i = 0; i < sband->n_bitrates; i++) {
151 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
152 continue;
Simon Wunderlich2ec9c1f2013-07-11 18:07:46 +0200153 if (sband->bitrates[i].bitrate > 110)
154 have_higher_than_11mbit = true;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200155
156 rates |= BIT(i);
157 rates_n++;
158 }
159
Johannes Berg46900292009-02-15 12:44:28 +0100160 *pos++ = WLAN_EID_SUPP_RATES;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200161 *pos++ = min_t(int, 8, rates_n);
162 for (ri = 0; ri < sband->n_bitrates; ri++) {
163 int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
164 5 * (1 << shift));
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100165 u8 basic = 0;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200166 if (!(rates & BIT(ri)))
167 continue;
168
169 if (basic_rates & BIT(ri))
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100170 basic = 0x80;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200171 *pos++ = basic | (u8) rate;
Simon Wunderlich93c75782013-07-11 20:29:49 +0200172 if (++rates_added == 8) {
173 ri++; /* continue at next rate for EXT_SUPP_RATES */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200174 break;
Simon Wunderlich93c75782013-07-11 20:29:49 +0200175 }
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100176 }
Johannes Berg46900292009-02-15 12:44:28 +0100177
178 if (sband->band == IEEE80211_BAND_2GHZ) {
Johannes Berg46900292009-02-15 12:44:28 +0100179 *pos++ = WLAN_EID_DS_PARAMS;
180 *pos++ = 1;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200181 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
Johannes Berg46900292009-02-15 12:44:28 +0100182 }
183
Johannes Berg46900292009-02-15 12:44:28 +0100184 *pos++ = WLAN_EID_IBSS_PARAMS;
185 *pos++ = 2;
186 /* FIX: set ATIM window based on scan results */
187 *pos++ = 0;
188 *pos++ = 0;
189
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200190 /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
191 if (rates_n > 8) {
Johannes Berg46900292009-02-15 12:44:28 +0100192 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200193 *pos++ = rates_n - 8;
194 for (; ri < sband->n_bitrates; ri++) {
195 int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
196 5 * (1 << shift));
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100197 u8 basic = 0;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200198 if (!(rates & BIT(ri)))
199 continue;
200
201 if (basic_rates & BIT(ri))
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100202 basic = 0x80;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200203 *pos++ = basic | (u8) rate;
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100204 }
Johannes Berg46900292009-02-15 12:44:28 +0100205 }
206
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100207 if (ifibss->ie_len) {
208 memcpy(pos, ifibss->ie, ifibss->ie_len);
209 pos += ifibss->ie_len;
210 }
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200211
Alexander Simon13c40c52011-11-30 16:56:34 +0100212 /* add HT capability and information IEs */
Johannes Berg4bf88532012-11-09 11:39:59 +0100213 if (chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
Simon Wunderlich0418a442013-05-16 13:00:31 +0200214 chandef.width != NL80211_CHAN_WIDTH_5 &&
215 chandef.width != NL80211_CHAN_WIDTH_10 &&
Johannes Berg683b6d32012-11-08 21:25:48 +0100216 sband->ht_cap.ht_supported) {
Simon Wunderlich822854b2013-06-28 10:39:59 +0200217 struct ieee80211_sta_ht_cap ht_cap;
218
219 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
220 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
221
222 pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
Ashok Nagarajan0d894ec2012-05-07 21:00:29 -0700223 /*
224 * Note: According to 802.11n-2009 9.13.3.1, HT Protection
225 * field and RIFS Mode are reserved in IBSS mode, therefore
226 * keep them at 0
227 */
Johannes Berg074d46d2012-03-15 19:45:16 +0100228 pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
Johannes Berg4bf88532012-11-09 11:39:59 +0100229 &chandef, 0);
Alexander Simon13c40c52011-11-30 16:56:34 +0100230 }
231
Johannes Berg32c50572012-03-28 11:04:29 +0200232 if (local->hw.queues >= IEEE80211_NUM_ACS) {
Bruno Randolf9eba6122010-10-04 11:17:30 +0900233 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
234 *pos++ = 7; /* len */
235 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
236 *pos++ = 0x50;
237 *pos++ = 0xf2;
238 *pos++ = 2; /* WME */
239 *pos++ = 0; /* WME info */
240 *pos++ = 1; /* WME ver */
241 *pos++ = 0; /* U-APSD no in use */
242 }
243
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100244 presp->head_len = pos - presp->head;
245 if (WARN_ON(presp->head_len > frame_len))
246 return;
247
248 rcu_assign_pointer(ifibss->presp, presp);
Johannes Berg46900292009-02-15 12:44:28 +0100249
Johannes Bergd6a83222012-12-14 14:06:28 +0100250 sdata->vif.bss_conf.enable_beacon = true;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200251 sdata->vif.bss_conf.beacon_int = beacon_int;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300252 sdata->vif.bss_conf.basic_rates = basic_rates;
Marek Puzyniak0ca54f62013-04-10 13:19:13 +0200253 sdata->vif.bss_conf.ssid_len = ifibss->ssid_len;
254 memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len);
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200255 bss_change = BSS_CHANGED_BEACON_INT;
256 bss_change |= ieee80211_reset_erp_info(sdata);
257 bss_change |= BSS_CHANGED_BSSID;
258 bss_change |= BSS_CHANGED_BEACON;
259 bss_change |= BSS_CHANGED_BEACON_ENABLED;
Teemu Paasikivi392cfdb2010-06-14 12:55:32 +0300260 bss_change |= BSS_CHANGED_BASIC_RATES;
Alexander Simon13c40c52011-11-30 16:56:34 +0100261 bss_change |= BSS_CHANGED_HT;
Johannes Berg8fc214b2010-04-28 17:40:43 +0200262 bss_change |= BSS_CHANGED_IBSS;
Marek Puzyniak0ca54f62013-04-10 13:19:13 +0200263 bss_change |= BSS_CHANGED_SSID;
Simon Wunderlich2f91a962012-12-03 22:21:30 +0100264
265 /*
266 * In 5 GHz/802.11a, we can always use short slot time.
267 * (IEEE 802.11-2012 18.3.8.7)
268 *
269 * In 2.4GHz, we must always use long slots in IBSS for compatibility
270 * reasons.
271 * (IEEE 802.11-2012 19.4.5)
272 *
273 * HT follows these specifications (IEEE 802.11-2012 20.3.18)
274 */
275 sdata->vif.bss_conf.use_short_slot = chan->band == IEEE80211_BAND_5GHZ;
276 bss_change |= BSS_CHANGED_ERP_SLOT;
277
Simon Wunderlich2ec9c1f2013-07-11 18:07:46 +0200278 /* cf. IEEE 802.11 9.2.12 */
279 if (chan->band == IEEE80211_BAND_2GHZ && have_higher_than_11mbit)
280 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
281 else
282 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
283
Johannes Berg8fc214b2010-04-28 17:40:43 +0200284 sdata->vif.bss_conf.ibss_joined = true;
Sujith Manoharanc13a7652012-10-12 17:35:45 +0530285 sdata->vif.bss_conf.ibss_creator = creator;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200286 ieee80211_bss_info_change_notify(sdata, bss_change);
Johannes Berg46900292009-02-15 12:44:28 +0100287
Simon Wunderlich2ec9c1f2013-07-11 18:07:46 +0200288 ieee80211_set_wmm_default(sdata, true);
Johannes Berg46900292009-02-15 12:44:28 +0100289
Johannes Berg46900292009-02-15 12:44:28 +0100290 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200291 mod_timer(&ifibss->timer,
292 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100293
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200294 scan_width = cfg80211_chandef_to_scan_width(&chandef);
295 bss = cfg80211_inform_bss_width_frame(local->hw.wiphy, chan,
296 scan_width, mgmt,
297 presp->head_len, 0, GFP_KERNEL);
Johannes Berg5b112d32013-02-01 01:49:58 +0100298 cfg80211_put_bss(local->hw.wiphy, bss);
Eliad Peller86a2ea42011-11-08 15:36:59 +0200299 netif_carrier_on(sdata->dev);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200300 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100301}
302
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200303static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
304 struct ieee80211_bss *bss)
Johannes Berg46900292009-02-15 12:44:28 +0100305{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100306 struct cfg80211_bss *cbss =
307 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergb59066a2009-05-12 21:18:38 +0200308 struct ieee80211_supported_band *sband;
309 u32 basic_rates;
310 int i, j;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100311 u16 beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +0100312 const struct cfg80211_bss_ies *ies;
313 u64 tsf;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200314 u32 rate_flags;
315 int shift;
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200316
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200317 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200318
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200319 if (beacon_int < 10)
320 beacon_int = 10;
321
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100322 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200323 rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
324 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Bergb59066a2009-05-12 21:18:38 +0200325
326 basic_rates = 0;
327
328 for (i = 0; i < bss->supp_rates_len; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200329 int rate = bss->supp_rates[i] & 0x7f;
Johannes Bergb59066a2009-05-12 21:18:38 +0200330 bool is_basic = !!(bss->supp_rates[i] & 0x80);
331
332 for (j = 0; j < sband->n_bitrates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200333 int brate;
334 if ((rate_flags & sband->bitrates[j].flags)
335 != rate_flags)
336 continue;
337
338 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
339 5 * (1 << shift));
340 if (brate == rate) {
Johannes Bergb59066a2009-05-12 21:18:38 +0200341 if (is_basic)
342 basic_rates |= BIT(j);
343 break;
344 }
345 }
346 }
347
Johannes Berg8cef2c92013-02-05 16:54:31 +0100348 rcu_read_lock();
349 ies = rcu_dereference(cbss->ies);
350 tsf = ies->tsf;
351 rcu_read_unlock();
352
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100353 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200354 beacon_int,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100355 cbss->channel,
Johannes Bergb59066a2009-05-12 21:18:38 +0200356 basic_rates,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100357 cbss->capability,
Johannes Berg8cef2c92013-02-05 16:54:31 +0100358 tsf, false);
Johannes Berg46900292009-02-15 12:44:28 +0100359}
360
Antonio Quartulli52874a52013-06-18 14:20:40 +0200361static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100362 __acquires(RCU)
363{
364 struct ieee80211_sub_if_data *sdata = sta->sdata;
365 u8 addr[ETH_ALEN];
366
367 memcpy(addr, sta->sta.addr, ETH_ALEN);
368
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200369 ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100370
Johannes Berg83d5cc02012-01-12 09:31:10 +0100371 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
372 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
Antonio Quartulli267335d2012-01-31 20:25:47 +0100373 /* authorize the station only if the network is not RSN protected. If
374 * not wait for the userspace to authorize it */
375 if (!sta->sdata->u.ibss.control_port)
376 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100377
378 rate_control_rate_init(sta);
379
380 /* If it fails, maybe we raced another insertion? */
381 if (sta_info_insert_rcu(sta))
382 return sta_info_get(sdata, addr);
383 return sta;
384}
385
386static struct sta_info *
Antonio Quartulli52874a52013-06-18 14:20:40 +0200387ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
388 const u8 *addr, u32 supp_rates)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100389 __acquires(RCU)
390{
391 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
392 struct ieee80211_local *local = sdata->local;
393 struct sta_info *sta;
Johannes Berg55de9082012-07-26 17:24:39 +0200394 struct ieee80211_chanctx_conf *chanctx_conf;
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -0700395 struct ieee80211_supported_band *sband;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200396 enum nl80211_bss_scan_width scan_width;
Johannes Berg55de9082012-07-26 17:24:39 +0200397 int band;
Johannes Berg8bf11d82011-12-15 11:17:37 +0100398
399 /*
400 * XXX: Consider removing the least recently used entry and
401 * allow new one to be added.
402 */
403 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200404 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
Joe Perchese87cc472012-05-13 21:56:26 +0000405 sdata->name, addr);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100406 rcu_read_lock();
407 return NULL;
408 }
409
410 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
411 rcu_read_lock();
412 return NULL;
413 }
414
Joe Perchesb203ca32012-05-08 18:56:52 +0000415 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
Johannes Berg8bf11d82011-12-15 11:17:37 +0100416 rcu_read_lock();
417 return NULL;
418 }
419
Johannes Berg55de9082012-07-26 17:24:39 +0200420 rcu_read_lock();
421 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
422 if (WARN_ON_ONCE(!chanctx_conf))
423 return NULL;
Johannes Berg4bf88532012-11-09 11:39:59 +0100424 band = chanctx_conf->def.chan->band;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200425 scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
Johannes Berg55de9082012-07-26 17:24:39 +0200426 rcu_read_unlock();
427
Johannes Berg8bf11d82011-12-15 11:17:37 +0100428 sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
429 if (!sta) {
430 rcu_read_lock();
431 return NULL;
432 }
433
434 sta->last_rx = jiffies;
435
436 /* make sure mandatory rates are always added */
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -0700437 sband = local->hw.wiphy->bands[band];
Johannes Berg8bf11d82011-12-15 11:17:37 +0100438 sta->sta.supp_rates[band] = supp_rates |
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200439 ieee80211_mandatory_rates(sband, scan_width);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100440
Antonio Quartulli52874a52013-06-18 14:20:40 +0200441 return ieee80211_ibss_finish_sta(sta);
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100442}
443
Antonio Quartulli2cc59e72012-09-07 13:28:53 +0200444static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
445 struct ieee80211_mgmt *mgmt,
446 size_t len)
447{
448 u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
449
450 if (len < IEEE80211_DEAUTH_FRAME_LEN)
451 return;
452
453 ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM BSSID=%pM (reason: %d)\n",
454 mgmt->sa, mgmt->da, mgmt->bssid, reason);
455 sta_info_destroy_addr(sdata, mgmt->sa);
456}
457
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100458static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
459 struct ieee80211_mgmt *mgmt,
460 size_t len)
461{
462 u16 auth_alg, auth_transaction;
463
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200464 sdata_assert_lock(sdata);
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100465
466 if (len < 24 + 6)
467 return;
468
469 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
470 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
471
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200472 ibss_dbg(sdata,
473 "RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
474 mgmt->sa, mgmt->da, mgmt->bssid, auth_transaction);
Antonio Quartulli7bed2052012-11-25 23:24:27 +0100475
476 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
477 return;
478
Antonio Quartulli2cc59e72012-09-07 13:28:53 +0200479 /*
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100480 * IEEE 802.11 standard does not require authentication in IBSS
481 * networks and most implementations do not seem to use it.
482 * However, try to reply to authentication attempts if someone
483 * has actually implemented this.
484 */
Jouni Malinen700e8ea2012-09-30 19:29:37 +0300485 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
Johannes Berg1672c0e32013-01-29 15:02:27 +0100486 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100487}
488
Johannes Berg46900292009-02-15 12:44:28 +0100489static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200490 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg46900292009-02-15 12:44:28 +0100491 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200492 struct ieee802_11_elems *elems)
Johannes Berg46900292009-02-15 12:44:28 +0100493{
494 struct ieee80211_local *local = sdata->local;
495 int freq;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100496 struct cfg80211_bss *cbss;
Johannes Berg46900292009-02-15 12:44:28 +0100497 struct ieee80211_bss *bss;
498 struct sta_info *sta;
499 struct ieee80211_channel *channel;
500 u64 beacon_timestamp, rx_timestamp;
501 u32 supp_rates = 0;
502 enum ieee80211_band band = rx_status->band;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200503 enum nl80211_bss_scan_width scan_width;
Alexander Simon13c40c52011-11-30 16:56:34 +0100504 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
505 bool rates_updated = false;
Johannes Berg46900292009-02-15 12:44:28 +0100506
Johannes Berg1cd8e882013-03-27 14:30:12 +0100507 if (elems->ds_params)
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900508 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
509 band);
Johannes Berg46900292009-02-15 12:44:28 +0100510 else
511 freq = rx_status->freq;
512
513 channel = ieee80211_get_channel(local->hw.wiphy, freq);
514
515 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
516 return;
517
Bruno Randolf9eba6122010-10-04 11:17:30 +0900518 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Joe Perchesb203ca32012-05-08 18:56:52 +0000519 ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid)) {
Johannes Berg46900292009-02-15 12:44:28 +0100520
521 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100522 sta = sta_info_get(sdata, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100523
Bruno Randolf9eba6122010-10-04 11:17:30 +0900524 if (elems->supp_rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200525 supp_rates = ieee80211_sta_get_rates(sdata, elems,
Ashok Nagarajan9ebb61a2012-04-02 21:21:21 -0700526 band, NULL);
Bruno Randolf9eba6122010-10-04 11:17:30 +0900527 if (sta) {
528 u32 prev_rates;
Johannes Berg46900292009-02-15 12:44:28 +0100529
Bruno Randolf9eba6122010-10-04 11:17:30 +0900530 prev_rates = sta->sta.supp_rates[band];
531 /* make sure mandatory rates are always added */
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200532 scan_width = NL80211_BSS_CHAN_WIDTH_20;
533 if (rx_status->flag & RX_FLAG_5MHZ)
534 scan_width = NL80211_BSS_CHAN_WIDTH_5;
535 if (rx_status->flag & RX_FLAG_10MHZ)
536 scan_width = NL80211_BSS_CHAN_WIDTH_10;
Bruno Randolf9eba6122010-10-04 11:17:30 +0900537
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200538 sta->sta.supp_rates[band] = supp_rates |
539 ieee80211_mandatory_rates(sband,
540 scan_width);
Bruno Randolf9eba6122010-10-04 11:17:30 +0900541 if (sta->sta.supp_rates[band] != prev_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200542 ibss_dbg(sdata,
543 "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
544 sta->sta.addr, prev_rates,
545 sta->sta.supp_rates[band]);
Alexander Simon13c40c52011-11-30 16:56:34 +0100546 rates_updated = true;
Bruno Randolf9eba6122010-10-04 11:17:30 +0900547 }
Johannes Berg8bf11d82011-12-15 11:17:37 +0100548 } else {
549 rcu_read_unlock();
Bruno Randolf9eba6122010-10-04 11:17:30 +0900550 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
Antonio Quartulli52874a52013-06-18 14:20:40 +0200551 mgmt->sa, supp_rates);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100552 }
Johannes Berg34e89502010-02-03 13:59:58 +0100553 }
Bruno Randolf9eba6122010-10-04 11:17:30 +0900554
555 if (sta && elems->wmm_info)
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200556 set_sta_flag(sta, WLAN_STA_WME);
Bruno Randolf9eba6122010-10-04 11:17:30 +0900557
Johannes Berg074d46d2012-03-15 19:45:16 +0100558 if (sta && elems->ht_operation && elems->ht_cap_elem &&
Simon Wunderlich3aede782013-05-16 13:00:36 +0200559 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
560 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
561 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
Alexander Simon13c40c52011-11-30 16:56:34 +0100562 /* we both use HT */
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100563 struct ieee80211_ht_cap htcap_ie;
Johannes Berg4bf88532012-11-09 11:39:59 +0100564 struct cfg80211_chan_def chandef;
565
566 ieee80211_ht_oper_to_chandef(channel,
567 elems->ht_operation,
568 &chandef);
Alexander Simon13c40c52011-11-30 16:56:34 +0100569
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100570 memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
Alexander Simon13c40c52011-11-30 16:56:34 +0100571
572 /*
573 * fall back to HT20 if we don't use or use
574 * the other extension channel
575 */
Simon Wunderlich3aede782013-05-16 13:00:36 +0200576 if (chandef.center_freq1 !=
577 sdata->u.ibss.chandef.center_freq1)
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100578 htcap_ie.cap_info &=
579 cpu_to_le16(~IEEE80211_HT_CAP_SUP_WIDTH_20_40);
Alexander Simon13c40c52011-11-30 16:56:34 +0100580
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100581 rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(
582 sdata, sband, &htcap_ie, sta);
Alexander Simon13c40c52011-11-30 16:56:34 +0100583 }
584
Antonio Quartullie687f612012-08-12 18:24:55 +0200585 if (sta && rates_updated) {
586 drv_sta_rc_update(local, sdata, &sta->sta,
587 IEEE80211_RC_SUPP_RATES_CHANGED);
Alexander Simon13c40c52011-11-30 16:56:34 +0100588 rate_control_rate_init(sta);
Antonio Quartullie687f612012-08-12 18:24:55 +0200589 }
Alexander Simon13c40c52011-11-30 16:56:34 +0100590
Bruno Randolf9eba6122010-10-04 11:17:30 +0900591 rcu_read_unlock();
Johannes Berg46900292009-02-15 12:44:28 +0100592 }
593
594 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200595 channel);
Johannes Berg46900292009-02-15 12:44:28 +0100596 if (!bss)
597 return;
598
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100599 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
600
Johannes Berg8cef2c92013-02-05 16:54:31 +0100601 /* same for beacon and probe response */
602 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
Johannes Berg46900292009-02-15 12:44:28 +0100603
604 /* check if we need to merge IBSS */
605
Johannes Berg46900292009-02-15 12:44:28 +0100606 /* we use a fixed BSSID */
Benoit Papillaulta98bfec2010-01-17 22:45:24 +0100607 if (sdata->u.ibss.fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100608 goto put_bss;
609
610 /* not an IBSS */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100611 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
Johannes Berg46900292009-02-15 12:44:28 +0100612 goto put_bss;
613
614 /* different channel */
Johannes Berg55de9082012-07-26 17:24:39 +0200615 if (sdata->u.ibss.fixed_channel &&
Simon Wunderlich3aede782013-05-16 13:00:36 +0200616 sdata->u.ibss.chandef.chan != cbss->channel)
Johannes Berg46900292009-02-15 12:44:28 +0100617 goto put_bss;
618
619 /* different SSID */
620 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
621 memcmp(elems->ssid, sdata->u.ibss.ssid,
622 sdata->u.ibss.ssid_len))
623 goto put_bss;
624
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100625 /* same BSSID */
Joe Perchesb203ca32012-05-08 18:56:52 +0000626 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100627 goto put_bss;
628
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800629 if (ieee80211_have_rx_timestamp(rx_status)) {
630 /* time when timestamp field was received */
631 rx_timestamp =
632 ieee80211_calculate_rx_timestamp(local, rx_status,
633 len + FCS_LEN, 24);
Johannes Berg24487982009-04-23 18:52:52 +0200634 } else {
635 /*
636 * second best option: get current TSF
637 * (will return -1 if not supported)
638 */
Eliad Peller37a41b42011-09-21 14:06:11 +0300639 rx_timestamp = drv_get_tsf(local, sdata);
Johannes Berg24487982009-04-23 18:52:52 +0200640 }
Johannes Berg46900292009-02-15 12:44:28 +0100641
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200642 ibss_dbg(sdata,
643 "RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
644 mgmt->sa, mgmt->bssid,
645 (unsigned long long)rx_timestamp,
646 (unsigned long long)beacon_timestamp,
647 (unsigned long long)(rx_timestamp - beacon_timestamp),
648 jiffies);
Johannes Berg46900292009-02-15 12:44:28 +0100649
650 if (beacon_timestamp > rx_timestamp) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200651 ibss_dbg(sdata,
652 "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
653 mgmt->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100654 ieee80211_sta_join_ibss(sdata, bss);
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200655 supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
Johannes Berg34e89502010-02-03 13:59:58 +0100656 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
Antonio Quartulli52874a52013-06-18 14:20:40 +0200657 supp_rates);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100658 rcu_read_unlock();
Johannes Berg46900292009-02-15 12:44:28 +0100659 }
660
661 put_bss:
662 ieee80211_rx_bss_put(local, bss);
663}
664
Johannes Berg8bf11d82011-12-15 11:17:37 +0100665void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
666 const u8 *bssid, const u8 *addr,
667 u32 supp_rates)
Johannes Berg46900292009-02-15 12:44:28 +0100668{
Felix Fietkau2e10d332009-12-20 19:07:09 +0100669 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100670 struct ieee80211_local *local = sdata->local;
671 struct sta_info *sta;
Johannes Berg55de9082012-07-26 17:24:39 +0200672 struct ieee80211_chanctx_conf *chanctx_conf;
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -0700673 struct ieee80211_supported_band *sband;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200674 enum nl80211_bss_scan_width scan_width;
Johannes Berg55de9082012-07-26 17:24:39 +0200675 int band;
Johannes Berg46900292009-02-15 12:44:28 +0100676
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200677 /*
678 * XXX: Consider removing the least recently used entry and
679 * allow new one to be added.
680 */
Johannes Berg46900292009-02-15 12:44:28 +0100681 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200682 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
Joe Perchese87cc472012-05-13 21:56:26 +0000683 sdata->name, addr);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100684 return;
Johannes Berg46900292009-02-15 12:44:28 +0100685 }
686
Felix Fietkau2e10d332009-12-20 19:07:09 +0100687 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100688 return;
Felix Fietkau2e10d332009-12-20 19:07:09 +0100689
Joe Perchesb203ca32012-05-08 18:56:52 +0000690 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
Johannes Berg8bf11d82011-12-15 11:17:37 +0100691 return;
Johannes Berg46900292009-02-15 12:44:28 +0100692
Johannes Berg55de9082012-07-26 17:24:39 +0200693 rcu_read_lock();
694 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
695 if (WARN_ON_ONCE(!chanctx_conf)) {
696 rcu_read_unlock();
697 return;
698 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100699 band = chanctx_conf->def.chan->band;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200700 scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
Johannes Berg55de9082012-07-26 17:24:39 +0200701 rcu_read_unlock();
702
Johannes Berg8bf11d82011-12-15 11:17:37 +0100703 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
Johannes Berg46900292009-02-15 12:44:28 +0100704 if (!sta)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100705 return;
Johannes Berg46900292009-02-15 12:44:28 +0100706
Rajkumar Manoharanc8716d92010-10-23 10:59:57 +0530707 sta->last_rx = jiffies;
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100708
Johannes Berg46900292009-02-15 12:44:28 +0100709 /* make sure mandatory rates are always added */
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -0700710 sband = local->hw.wiphy->bands[band];
Johannes Berg46900292009-02-15 12:44:28 +0100711 sta->sta.supp_rates[band] = supp_rates |
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200712 ieee80211_mandatory_rates(sband, scan_width);
Johannes Berg46900292009-02-15 12:44:28 +0100713
Johannes Berg8bf11d82011-12-15 11:17:37 +0100714 spin_lock(&ifibss->incomplete_lock);
715 list_add(&sta->list, &ifibss->incomplete_stations);
716 spin_unlock(&ifibss->incomplete_lock);
717 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +0100718}
719
720static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
721{
722 struct ieee80211_local *local = sdata->local;
723 int active = 0;
724 struct sta_info *sta;
725
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200726 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200727
Johannes Berg46900292009-02-15 12:44:28 +0100728 rcu_read_lock();
729
730 list_for_each_entry_rcu(sta, &local->sta_list, list) {
731 if (sta->sdata == sdata &&
732 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
733 jiffies)) {
734 active++;
735 break;
736 }
737 }
738
739 rcu_read_unlock();
740
741 return active;
742}
743
Antonio Quartullidad9def2013-07-26 21:15:02 +0200744static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
745{
746 struct ieee80211_local *local = sdata->local;
747 struct sta_info *sta, *tmp;
748 unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
749 unsigned long exp_rsn_time = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
750
751 mutex_lock(&local->sta_mtx);
752
753 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
754 if (sdata != sta->sdata)
755 continue;
756
757 if (time_after(jiffies, sta->last_rx + exp_time) ||
758 (time_after(jiffies, sta->last_rx + exp_rsn_time) &&
759 sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
760 sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
761 sta->sta_state != IEEE80211_STA_AUTHORIZED ?
762 "not authorized " : "", sta->sta.addr);
763
764 WARN_ON(__sta_info_destroy(sta));
765 }
766 }
767
768 mutex_unlock(&local->sta_mtx);
769}
770
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100771/*
772 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
773 */
Johannes Berg46900292009-02-15 12:44:28 +0100774
775static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
776{
777 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200778 enum nl80211_bss_scan_width scan_width;
Johannes Berg46900292009-02-15 12:44:28 +0100779
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200780 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200781
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200782 mod_timer(&ifibss->timer,
783 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100784
Antonio Quartullidad9def2013-07-26 21:15:02 +0200785 ieee80211_ibss_sta_expire(sdata);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200786
Sujith450aae32009-11-02 12:33:23 +0530787 if (time_before(jiffies, ifibss->last_scan_completed +
788 IEEE80211_IBSS_MERGE_INTERVAL))
789 return;
790
Johannes Berg46900292009-02-15 12:44:28 +0100791 if (ieee80211_sta_active_ibss(sdata))
792 return;
793
John W. Linvillec037b832012-01-30 15:28:11 -0500794 if (ifibss->fixed_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100795 return;
796
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200797 sdata_info(sdata,
798 "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
Johannes Berg46900292009-02-15 12:44:28 +0100799
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200800 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +0100801 ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200802 NULL, scan_width);
Johannes Berg46900292009-02-15 12:44:28 +0100803}
804
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200805static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100806{
807 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100808 u8 bssid[ETH_ALEN];
Johannes Berg46900292009-02-15 12:44:28 +0100809 u16 capability;
810 int i;
811
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200812 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200813
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200814 if (ifibss->fixed_bssid) {
Johannes Berg46900292009-02-15 12:44:28 +0100815 memcpy(bssid, ifibss->bssid, ETH_ALEN);
816 } else {
817 /* Generate random, not broadcast, locally administered BSSID. Mix in
818 * own MAC address to make sure that devices that do not have proper
819 * random number generator get different BSSID. */
820 get_random_bytes(bssid, ETH_ALEN);
821 for (i = 0; i < ETH_ALEN; i++)
Johannes Berg47846c92009-11-25 17:46:19 +0100822 bssid[i] ^= sdata->vif.addr[i];
Johannes Berg46900292009-02-15 12:44:28 +0100823 bssid[0] &= ~0x01;
824 bssid[0] |= 0x02;
825 }
826
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200827 sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100828
Johannes Berg46900292009-02-15 12:44:28 +0100829 capability = WLAN_CAPABILITY_IBSS;
830
Johannes Bergfffd0932009-07-08 14:22:54 +0200831 if (ifibss->privacy)
Johannes Berg46900292009-02-15 12:44:28 +0100832 capability |= WLAN_CAPABILITY_PRIVACY;
833 else
834 sdata->drop_unencrypted = 0;
835
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200836 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
Simon Wunderlich3aede782013-05-16 13:00:36 +0200837 ifibss->chandef.chan, ifibss->basic_rates,
Sujith Manoharanc13a7652012-10-12 17:35:45 +0530838 capability, 0, true);
Johannes Berg46900292009-02-15 12:44:28 +0100839}
840
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100841/*
842 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
843 */
844
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200845static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100846{
847 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
848 struct ieee80211_local *local = sdata->local;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100849 struct cfg80211_bss *cbss;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200850 struct ieee80211_channel *chan = NULL;
Johannes Berg46900292009-02-15 12:44:28 +0100851 const u8 *bssid = NULL;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200852 enum nl80211_bss_scan_width scan_width;
Johannes Berg46900292009-02-15 12:44:28 +0100853 int active_ibss;
Johannes Berge0d61882009-05-12 20:47:32 +0200854 u16 capability;
Johannes Berg46900292009-02-15 12:44:28 +0100855
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200856 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200857
Johannes Berg46900292009-02-15 12:44:28 +0100858 active_ibss = ieee80211_sta_active_ibss(sdata);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200859 ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
Johannes Berg46900292009-02-15 12:44:28 +0100860
861 if (active_ibss)
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200862 return;
Johannes Berg46900292009-02-15 12:44:28 +0100863
Johannes Berge0d61882009-05-12 20:47:32 +0200864 capability = WLAN_CAPABILITY_IBSS;
Johannes Bergfffd0932009-07-08 14:22:54 +0200865 if (ifibss->privacy)
Johannes Berge0d61882009-05-12 20:47:32 +0200866 capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200867 if (ifibss->fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100868 bssid = ifibss->bssid;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200869 if (ifibss->fixed_channel)
Simon Wunderlich3aede782013-05-16 13:00:36 +0200870 chan = ifibss->chandef.chan;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200871 if (!is_zero_ether_addr(ifibss->bssid))
872 bssid = ifibss->bssid;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100873 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
874 ifibss->ssid, ifibss->ssid_len,
875 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
876 capability);
Johannes Berg46900292009-02-15 12:44:28 +0100877
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100878 if (cbss) {
879 struct ieee80211_bss *bss;
880
881 bss = (void *)cbss->priv;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200882 ibss_dbg(sdata,
883 "sta_find_ibss: selected %pM current %pM\n",
884 cbss->bssid, ifibss->bssid);
885 sdata_info(sdata,
886 "Selected IBSS BSSID %pM based on configured SSID\n",
887 cbss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100888
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200889 ieee80211_sta_join_ibss(sdata, bss);
Johannes Berg46900292009-02-15 12:44:28 +0100890 ieee80211_rx_bss_put(local, bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200891 return;
Reinette Chatred419b9f2009-10-19 14:55:37 -0700892 }
Johannes Berg46900292009-02-15 12:44:28 +0100893
Antonio Quartullid8eb7412013-08-09 18:58:32 +0200894 /* if a fixed bssid and a fixed freq have been provided create the IBSS
895 * directly and do not waste time scanning
896 */
897 if (ifibss->fixed_bssid && ifibss->fixed_channel) {
898 sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
899 bssid);
900 ieee80211_sta_create_ibss(sdata);
901 return;
902 }
903
904
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200905 ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
Johannes Berg46900292009-02-15 12:44:28 +0100906
907 /* Selected IBSS not found in current scan results - try to scan */
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100908 if (time_after(jiffies, ifibss->last_scan_completed +
Johannes Berg46900292009-02-15 12:44:28 +0100909 IEEE80211_SCAN_INTERVAL)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200910 sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
Johannes Berg46900292009-02-15 12:44:28 +0100911
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200912 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +0100913 ieee80211_request_ibss_scan(sdata, ifibss->ssid,
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200914 ifibss->ssid_len, chan,
915 scan_width);
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100916 } else {
Johannes Berg46900292009-02-15 12:44:28 +0100917 int interval = IEEE80211_SCAN_INTERVAL;
918
919 if (time_after(jiffies, ifibss->ibss_join_req +
Johannes Berg55de9082012-07-26 17:24:39 +0200920 IEEE80211_IBSS_JOIN_TIMEOUT))
921 ieee80211_sta_create_ibss(sdata);
Johannes Berg46900292009-02-15 12:44:28 +0100922
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200923 mod_timer(&ifibss->timer,
924 round_jiffies(jiffies + interval));
Johannes Berg46900292009-02-15 12:44:28 +0100925 }
Johannes Berg46900292009-02-15 12:44:28 +0100926}
927
928static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
Johannes Bergc269a202011-02-14 12:20:22 +0100929 struct sk_buff *req)
Johannes Berg46900292009-02-15 12:44:28 +0100930{
Johannes Bergc269a202011-02-14 12:20:22 +0100931 struct ieee80211_mgmt *mgmt = (void *)req->data;
Johannes Berg46900292009-02-15 12:44:28 +0100932 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
933 struct ieee80211_local *local = sdata->local;
Johannes Bergc269a202011-02-14 12:20:22 +0100934 int tx_last_beacon, len = req->len;
Johannes Berg46900292009-02-15 12:44:28 +0100935 struct sk_buff *skb;
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100936 struct beacon_data *presp;
Johannes Berg46900292009-02-15 12:44:28 +0100937 u8 *pos, *end;
938
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200939 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200940
Johannes Berg40b275b2011-05-13 14:15:49 +0200941 presp = rcu_dereference_protected(ifibss->presp,
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200942 lockdep_is_held(&sdata->wdev.mtx));
Johannes Berg40b275b2011-05-13 14:15:49 +0200943
Johannes Berg46900292009-02-15 12:44:28 +0100944 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
Johannes Berg40b275b2011-05-13 14:15:49 +0200945 len < 24 + 2 || !presp)
Johannes Berg46900292009-02-15 12:44:28 +0100946 return;
947
Johannes Berg24487982009-04-23 18:52:52 +0200948 tx_last_beacon = drv_tx_last_beacon(local);
Johannes Berg46900292009-02-15 12:44:28 +0100949
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200950 ibss_dbg(sdata,
951 "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
952 mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
Johannes Berg46900292009-02-15 12:44:28 +0100953
Felix Fietkau1ed76482011-03-24 19:46:18 +0100954 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
Johannes Berg46900292009-02-15 12:44:28 +0100955 return;
956
Joe Perchesb203ca32012-05-08 18:56:52 +0000957 if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
Felix Fietkau888d04d2012-03-01 15:22:09 +0100958 !is_broadcast_ether_addr(mgmt->bssid))
Johannes Berg46900292009-02-15 12:44:28 +0100959 return;
960
961 end = ((u8 *) mgmt) + len;
962 pos = mgmt->u.probe_req.variable;
963 if (pos[0] != WLAN_EID_SSID ||
964 pos + 2 + pos[1] > end) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200965 ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
966 mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100967 return;
968 }
969 if (pos[1] != 0 &&
970 (pos[1] != ifibss->ssid_len ||
Benoit Papillault0da780c2010-02-05 01:21:03 +0100971 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
Johannes Berg46900292009-02-15 12:44:28 +0100972 /* Ignore ProbeReq for foreign SSID */
973 return;
974 }
975
976 /* Reply with ProbeResp */
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100977 skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
Johannes Berg46900292009-02-15 12:44:28 +0100978 if (!skb)
979 return;
980
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100981 skb_reserve(skb, local->tx_headroom);
982 memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
983
984 memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
985 ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
Johannes Berg62ae67b2009-11-18 18:42:05 +0100986 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
987 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100988}
989
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200990static
991void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
992 struct ieee80211_mgmt *mgmt, size_t len,
993 struct ieee80211_rx_status *rx_status)
Johannes Berg46900292009-02-15 12:44:28 +0100994{
995 size_t baselen;
996 struct ieee802_11_elems elems;
997
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200998 BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
999 offsetof(typeof(mgmt->u.beacon), variable));
1000
1001 /*
1002 * either beacon or probe_resp but the variable field is at the
1003 * same offset
1004 */
Johannes Berg46900292009-02-15 12:44:28 +01001005 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1006 if (baselen > len)
1007 return;
1008
1009 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
Johannes Bergb2e506b2013-03-26 14:54:16 +01001010 false, &elems);
Johannes Berg46900292009-02-15 12:44:28 +01001011
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02001012 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg46900292009-02-15 12:44:28 +01001013}
1014
Johannes Berg1fa57d02010-06-10 10:21:32 +02001015void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1016 struct sk_buff *skb)
Johannes Berg46900292009-02-15 12:44:28 +01001017{
1018 struct ieee80211_rx_status *rx_status;
1019 struct ieee80211_mgmt *mgmt;
1020 u16 fc;
1021
Johannes Bergf1d58c22009-06-17 13:13:00 +02001022 rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Berg46900292009-02-15 12:44:28 +01001023 mgmt = (struct ieee80211_mgmt *) skb->data;
1024 fc = le16_to_cpu(mgmt->frame_control);
1025
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001026 sdata_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +02001027
Tim Harveyc926d002010-12-09 10:43:13 -08001028 if (!sdata->u.ibss.ssid_len)
1029 goto mgmt_out; /* not ready to merge yet */
1030
Johannes Berg46900292009-02-15 12:44:28 +01001031 switch (fc & IEEE80211_FCTL_STYPE) {
1032 case IEEE80211_STYPE_PROBE_REQ:
Johannes Bergc269a202011-02-14 12:20:22 +01001033 ieee80211_rx_mgmt_probe_req(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +01001034 break;
1035 case IEEE80211_STYPE_PROBE_RESP:
Johannes Berg46900292009-02-15 12:44:28 +01001036 case IEEE80211_STYPE_BEACON:
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02001037 ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
1038 rx_status);
Johannes Berg46900292009-02-15 12:44:28 +01001039 break;
1040 case IEEE80211_STYPE_AUTH:
1041 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
1042 break;
Antonio Quartulli2cc59e72012-09-07 13:28:53 +02001043 case IEEE80211_STYPE_DEAUTH:
1044 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
1045 break;
Johannes Berg46900292009-02-15 12:44:28 +01001046 }
Johannes Berg7a17a332010-07-21 11:30:27 +02001047
Tim Harveyc926d002010-12-09 10:43:13 -08001048 mgmt_out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001049 sdata_unlock(sdata);
Johannes Berg46900292009-02-15 12:44:28 +01001050}
1051
Johannes Berg1fa57d02010-06-10 10:21:32 +02001052void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +01001053{
Johannes Berg1fa57d02010-06-10 10:21:32 +02001054 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg8bf11d82011-12-15 11:17:37 +01001055 struct sta_info *sta;
Johannes Berg46900292009-02-15 12:44:28 +01001056
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001057 sdata_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +02001058
1059 /*
1060 * Work could be scheduled after scan or similar
1061 * when we aren't even joined (or trying) with a
1062 * network.
1063 */
1064 if (!ifibss->ssid_len)
1065 goto out;
Johannes Berg46900292009-02-15 12:44:28 +01001066
Johannes Berg8bf11d82011-12-15 11:17:37 +01001067 spin_lock_bh(&ifibss->incomplete_lock);
1068 while (!list_empty(&ifibss->incomplete_stations)) {
1069 sta = list_first_entry(&ifibss->incomplete_stations,
1070 struct sta_info, list);
1071 list_del(&sta->list);
1072 spin_unlock_bh(&ifibss->incomplete_lock);
1073
Antonio Quartulli52874a52013-06-18 14:20:40 +02001074 ieee80211_ibss_finish_sta(sta);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001075 rcu_read_unlock();
1076 spin_lock_bh(&ifibss->incomplete_lock);
1077 }
1078 spin_unlock_bh(&ifibss->incomplete_lock);
1079
Johannes Berg46900292009-02-15 12:44:28 +01001080 switch (ifibss->state) {
1081 case IEEE80211_IBSS_MLME_SEARCH:
1082 ieee80211_sta_find_ibss(sdata);
1083 break;
1084 case IEEE80211_IBSS_MLME_JOINED:
1085 ieee80211_sta_merge_ibss(sdata);
1086 break;
1087 default:
1088 WARN_ON(1);
1089 break;
1090 }
Johannes Berg46900292009-02-15 12:44:28 +01001091
Johannes Berg7a17a332010-07-21 11:30:27 +02001092 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001093 sdata_unlock(sdata);
Johannes Berg3a4d4aa2010-05-26 16:41:40 +02001094}
1095
Johannes Berg46900292009-02-15 12:44:28 +01001096static void ieee80211_ibss_timer(unsigned long data)
1097{
1098 struct ieee80211_sub_if_data *sdata =
1099 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01001100
Stanislaw Gruszkaa6182942013-02-28 10:55:28 +01001101 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +01001102}
1103
1104void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1105{
1106 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1107
Johannes Berg46900292009-02-15 12:44:28 +01001108 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
1109 (unsigned long) sdata);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001110 INIT_LIST_HEAD(&ifibss->incomplete_stations);
1111 spin_lock_init(&ifibss->incomplete_lock);
Johannes Berg46900292009-02-15 12:44:28 +01001112}
1113
1114/* scan finished notification */
1115void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1116{
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001117 struct ieee80211_sub_if_data *sdata;
Johannes Berg46900292009-02-15 12:44:28 +01001118
Johannes Berg29b4a4f2009-04-21 00:30:49 +02001119 mutex_lock(&local->iflist_mtx);
1120 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001121 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e41f712009-04-23 11:48:56 +02001122 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001123 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1124 continue;
1125 sdata->u.ibss.last_scan_completed = jiffies;
Johannes Berg7a17a332010-07-21 11:30:27 +02001126 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +01001127 }
Johannes Berg29b4a4f2009-04-21 00:30:49 +02001128 mutex_unlock(&local->iflist_mtx);
Johannes Berg46900292009-02-15 12:44:28 +01001129}
1130
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001131int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1132 struct cfg80211_ibss_params *params)
1133{
Simon Wunderlichff3cc5f2011-11-30 16:56:33 +01001134 u32 changed = 0;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001135 u32 rate_flags;
1136 struct ieee80211_supported_band *sband;
1137 int i;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001138
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001139 if (params->bssid) {
1140 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1141 sdata->u.ibss.fixed_bssid = true;
1142 } else
1143 sdata->u.ibss.fixed_bssid = false;
1144
Johannes Bergfffd0932009-07-08 14:22:54 +02001145 sdata->u.ibss.privacy = params->privacy;
Antonio Quartulli267335d2012-01-31 20:25:47 +01001146 sdata->u.ibss.control_port = params->control_port;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03001147 sdata->u.ibss.basic_rates = params->basic_rates;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001148
1149 /* fix basic_rates if channel does not support these rates */
1150 rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
1151 sband = sdata->local->hw.wiphy->bands[params->chandef.chan->band];
1152 for (i = 0; i < sband->n_bitrates; i++) {
1153 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1154 sdata->u.ibss.basic_rates &= ~BIT(i);
1155 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01001156 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1157 sizeof(params->mcast_rate));
Johannes Bergfffd0932009-07-08 14:22:54 +02001158
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001159 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1160
Simon Wunderlich3aede782013-05-16 13:00:36 +02001161 sdata->u.ibss.chandef = params->chandef;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001162 sdata->u.ibss.fixed_channel = params->channel_fixed;
1163
1164 if (params->ie) {
1165 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1166 GFP_KERNEL);
1167 if (sdata->u.ibss.ie)
1168 sdata->u.ibss.ie_len = params->ie_len;
1169 }
1170
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001171 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1172 sdata->u.ibss.ibss_join_req = jiffies;
1173
Antonio Quartullibadecb02012-10-26 18:54:25 +02001174 memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
Johannes Berg0e41f712009-04-23 11:48:56 +02001175 sdata->u.ibss.ssid_len = params->ssid_len;
1176
Simon Wunderlich822854b2013-06-28 10:39:59 +02001177 memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
1178 sizeof(sdata->u.ibss.ht_capa));
1179 memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
1180 sizeof(sdata->u.ibss.ht_capa_mask));
1181
Simon Wunderlichff3cc5f2011-11-30 16:56:33 +01001182 /*
1183 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1184 * reserved, but an HT STA shall protect HT transmissions as though
1185 * the HT Protection field were set to non-HT mixed mode.
1186 *
1187 * In an IBSS, the RIFS Mode field of the HT Operation element is
1188 * also reserved, but an HT STA shall operate as though this field
1189 * were set to 1.
1190 */
1191
1192 sdata->vif.bss_conf.ht_operation_mode |=
1193 IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1194 | IEEE80211_HT_PARAM_RIFS_MODE;
1195
1196 changed |= BSS_CHANGED_HT;
1197 ieee80211_bss_info_change_notify(sdata, changed);
1198
Johannes Berg04ecd252012-09-11 14:34:12 +02001199 sdata->smps_mode = IEEE80211_SMPS_OFF;
1200 sdata->needed_rx_chains = sdata->local->rx_chains;
1201
Johannes Berg64592c82010-06-10 10:21:31 +02001202 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001203
1204 return 0;
1205}
1206
1207int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1208{
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001209 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1210 struct ieee80211_local *local = sdata->local;
1211 struct cfg80211_bss *cbss;
1212 u16 capability;
Johannes Berg7a17a332010-07-21 11:30:27 +02001213 int active_ibss;
Johannes Berg8bf11d82011-12-15 11:17:37 +01001214 struct sta_info *sta;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01001215 struct beacon_data *presp;
Johannes Berg7a17a332010-07-21 11:30:27 +02001216
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001217 active_ibss = ieee80211_sta_active_ibss(sdata);
1218
1219 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
1220 capability = WLAN_CAPABILITY_IBSS;
1221
1222 if (ifibss->privacy)
1223 capability |= WLAN_CAPABILITY_PRIVACY;
1224
Simon Wunderlich3aede782013-05-16 13:00:36 +02001225 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001226 ifibss->bssid, ifibss->ssid,
1227 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
1228 WLAN_CAPABILITY_PRIVACY,
1229 capability);
1230
1231 if (cbss) {
1232 cfg80211_unlink_bss(local->hw.wiphy, cbss);
Johannes Berg5b112d32013-02-01 01:49:58 +01001233 cfg80211_put_bss(local->hw.wiphy, cbss);
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001234 }
1235 }
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001236
Simon Wunderlichb78a4932012-11-13 18:43:03 +01001237 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
1238 memset(ifibss->bssid, 0, ETH_ALEN);
1239 ifibss->ssid_len = 0;
1240
Johannes Bergb998e8b2012-12-13 23:07:46 +01001241 sta_info_flush(sdata);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001242
1243 spin_lock_bh(&ifibss->incomplete_lock);
1244 while (!list_empty(&ifibss->incomplete_stations)) {
1245 sta = list_first_entry(&ifibss->incomplete_stations,
1246 struct sta_info, list);
1247 list_del(&sta->list);
1248 spin_unlock_bh(&ifibss->incomplete_lock);
1249
1250 sta_info_free(local, sta);
1251 spin_lock_bh(&ifibss->incomplete_lock);
1252 }
1253 spin_unlock_bh(&ifibss->incomplete_lock);
1254
Eliad Peller86a2ea42011-11-08 15:36:59 +02001255 netif_carrier_off(sdata->dev);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001256
1257 /* remove beacon */
1258 kfree(sdata->u.ibss.ie);
Johannes Bergc3ffeab2013-03-07 20:54:29 +01001259 presp = rcu_dereference_protected(ifibss->presp,
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001260 lockdep_is_held(&sdata->wdev.mtx));
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001261 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
Simon Wunderlich822854b2013-06-28 10:39:59 +02001262
1263 /* on the next join, re-program HT parameters */
1264 memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
1265 memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
1266
Johannes Berg8fc214b2010-04-28 17:40:43 +02001267 sdata->vif.bss_conf.ibss_joined = false;
Sujith Manoharanc13a7652012-10-12 17:35:45 +05301268 sdata->vif.bss_conf.ibss_creator = false;
Johannes Bergd6a83222012-12-14 14:06:28 +01001269 sdata->vif.bss_conf.enable_beacon = false;
Marek Puzyniak0ca54f62013-04-10 13:19:13 +02001270 sdata->vif.bss_conf.ssid_len = 0;
Johannes Bergd6a83222012-12-14 14:06:28 +01001271 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
Johannes Berg8fc214b2010-04-28 17:40:43 +02001272 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1273 BSS_CHANGED_IBSS);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001274 synchronize_rcu();
Johannes Bergc3ffeab2013-03-07 20:54:29 +01001275 kfree(presp);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001276
Johannes Berg35f20c12010-06-10 10:21:30 +02001277 skb_queue_purge(&sdata->skb_queue);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001278
Johannes Bergbc05d192010-07-21 10:52:40 +02001279 del_timer_sync(&sdata->u.ibss.timer);
Johannes Berg7a17a332010-07-21 11:30:27 +02001280
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001281 return 0;
1282}