blob: 8fa1c27bdfe379c4b780941bd5537e146c0a0f17 [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)
33
34#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
35
36
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020037static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
38 const u8 *bssid, const int beacon_int,
39 struct ieee80211_channel *chan,
Johannes Bergb59066a2009-05-12 21:18:38 +020040 const u32 basic_rates,
Sujith Manoharanc13a7652012-10-12 17:35:45 +053041 const u16 capability, u64 tsf,
42 bool creator)
Johannes Berg46900292009-02-15 12:44:28 +010043{
44 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
45 struct ieee80211_local *local = sdata->local;
Simon Wunderlich2103dec2013-07-08 16:55:53 +020046 int rates_n = 0, i, ri;
Johannes Berg46900292009-02-15 12:44:28 +010047 struct ieee80211_mgmt *mgmt;
48 u8 *pos;
49 struct ieee80211_supported_band *sband;
Johannes Bergf446d102009-10-28 15:12:32 +010050 struct cfg80211_bss *bss;
Simon Wunderlich2103dec2013-07-08 16:55:53 +020051 u32 bss_change, rate_flags, rates = 0, rates_added = 0;
Johannes Bergb59066a2009-05-12 21:18:38 +020052 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
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;
Johannes Bergc3ffeab2013-03-07 20:54:29 +010055 struct beacon_data *presp;
56 int frame_len;
Simon Wunderlich2103dec2013-07-08 16:55:53 +020057 int shift;
Johannes Berg24487982009-04-23 18:52:52 +020058
Johannes Berg8d61ffa2013-05-10 12:32:47 +020059 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +020060
Johannes Berg24487982009-04-23 18:52:52 +020061 /* Reset own TSF to allow time synchronization work. */
Eliad Peller37a41b42011-09-21 14:06:11 +030062 drv_reset_tsf(local, sdata);
Johannes Berg46900292009-02-15 12:44:28 +010063
Joe Perchesb203ca32012-05-08 18:56:52 +000064 if (!ether_addr_equal(ifibss->bssid, bssid))
Johannes Bergb998e8b2012-12-13 23:07:46 +010065 sta_info_flush(sdata);
Johannes Berg46900292009-02-15 12:44:28 +010066
Johannes Berg49b5c7f2010-04-29 21:34:01 +020067 /* if merging, indicate to driver that we leave the old IBSS */
68 if (sdata->vif.bss_conf.ibss_joined) {
69 sdata->vif.bss_conf.ibss_joined = false;
Sujith Manoharanc13a7652012-10-12 17:35:45 +053070 sdata->vif.bss_conf.ibss_creator = false;
Johannes Berg1852d402013-03-07 20:22:28 +010071 sdata->vif.bss_conf.enable_beacon = false;
Eliad Peller86a2ea42011-11-08 15:36:59 +020072 netif_carrier_off(sdata->dev);
Johannes Berg1852d402013-03-07 20:22:28 +010073 ieee80211_bss_info_change_notify(sdata,
74 BSS_CHANGED_IBSS |
75 BSS_CHANGED_BEACON_ENABLED);
Johannes Berg49b5c7f2010-04-29 21:34:01 +020076 }
77
Johannes Bergc3ffeab2013-03-07 20:54:29 +010078 presp = rcu_dereference_protected(ifibss->presp,
Johannes Berg8d61ffa2013-05-10 12:32:47 +020079 lockdep_is_held(&sdata->wdev.mtx));
Johannes Bergc3ffeab2013-03-07 20:54:29 +010080 rcu_assign_pointer(ifibss->presp, NULL);
81 if (presp)
82 kfree_rcu(presp, rcu_head);
Johannes Berg1852d402013-03-07 20:22:28 +010083
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020084 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
Johannes Berg46900292009-02-15 12:44:28 +010085
Simon Wunderlich3aede782013-05-16 13:00:36 +020086 chandef = ifibss->chandef;
Johannes Berg4bf88532012-11-09 11:39:59 +010087 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef)) {
88 chandef.width = NL80211_CHAN_WIDTH_20;
89 chandef.center_freq1 = chan->center_freq;
90 }
Johannes Berg55de9082012-07-26 17:24:39 +020091
92 ieee80211_vif_release_channel(sdata);
Johannes Berg4bf88532012-11-09 11:39:59 +010093 if (ieee80211_vif_use_channel(sdata, &chandef,
Johannes Berg55de9082012-07-26 17:24:39 +020094 ifibss->fixed_channel ?
95 IEEE80211_CHANCTX_SHARED :
96 IEEE80211_CHANCTX_EXCLUSIVE)) {
97 sdata_info(sdata, "Failed to join IBSS, no channel context\n");
98 return;
Alexander Simon13c40c52011-11-30 16:56:34 +010099 }
Johannes Berg55de9082012-07-26 17:24:39 +0200100
101 memcpy(ifibss->bssid, bssid, ETH_ALEN);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200102
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200103 sband = local->hw.wiphy->bands[chan->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200104 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg46900292009-02-15 12:44:28 +0100105
106 /* Build IBSS probe response */
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100107 frame_len = sizeof(struct ieee80211_hdr_3addr) +
108 12 /* struct ieee80211_mgmt.u.beacon */ +
109 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
110 2 + 8 /* max Supported Rates */ +
111 3 /* max DS params */ +
112 4 /* IBSS params */ +
113 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
114 2 + sizeof(struct ieee80211_ht_cap) +
115 2 + sizeof(struct ieee80211_ht_operation) +
116 ifibss->ie_len;
117 presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
118 if (!presp)
119 return;
120
121 presp->head = (void *)(presp + 1);
122
123 mgmt = (void *) presp->head;
Johannes Berg46900292009-02-15 12:44:28 +0100124 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
125 IEEE80211_STYPE_PROBE_RESP);
Johannes Berge83e6542012-07-13 16:23:07 +0200126 eth_broadcast_addr(mgmt->da);
Johannes Berg47846c92009-11-25 17:46:19 +0100127 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100128 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200129 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
Sujith707c1b42009-03-03 10:15:10 +0530130 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100131 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
132
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100133 pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
134
Johannes Berg46900292009-02-15 12:44:28 +0100135 *pos++ = WLAN_EID_SSID;
136 *pos++ = ifibss->ssid_len;
137 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100138 pos += ifibss->ssid_len;
Johannes Berg46900292009-02-15 12:44:28 +0100139
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200140 rate_flags = ieee80211_chandef_rate_flags(&chandef);
141 for (i = 0; i < sband->n_bitrates; i++) {
142 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
143 continue;
144
145 rates |= BIT(i);
146 rates_n++;
147 }
148
Johannes Berg46900292009-02-15 12:44:28 +0100149 *pos++ = WLAN_EID_SUPP_RATES;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200150 *pos++ = min_t(int, 8, rates_n);
151 for (ri = 0; ri < sband->n_bitrates; ri++) {
152 int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
153 5 * (1 << shift));
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100154 u8 basic = 0;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200155 if (!(rates & BIT(ri)))
156 continue;
157
158 if (basic_rates & BIT(ri))
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100159 basic = 0x80;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200160 *pos++ = basic | (u8) rate;
161 if (++rates_added == 8)
162 break;
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100163 }
Johannes Berg46900292009-02-15 12:44:28 +0100164
165 if (sband->band == IEEE80211_BAND_2GHZ) {
Johannes Berg46900292009-02-15 12:44:28 +0100166 *pos++ = WLAN_EID_DS_PARAMS;
167 *pos++ = 1;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200168 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
Johannes Berg46900292009-02-15 12:44:28 +0100169 }
170
Johannes Berg46900292009-02-15 12:44:28 +0100171 *pos++ = WLAN_EID_IBSS_PARAMS;
172 *pos++ = 2;
173 /* FIX: set ATIM window based on scan results */
174 *pos++ = 0;
175 *pos++ = 0;
176
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200177 /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
178 if (rates_n > 8) {
Johannes Berg46900292009-02-15 12:44:28 +0100179 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200180 *pos++ = rates_n - 8;
181 for (; ri < sband->n_bitrates; ri++) {
182 int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
183 5 * (1 << shift));
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100184 u8 basic = 0;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200185 if (!(rates & BIT(ri)))
186 continue;
187
188 if (basic_rates & BIT(ri))
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100189 basic = 0x80;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200190 *pos++ = basic | (u8) rate;
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100191 }
Johannes Berg46900292009-02-15 12:44:28 +0100192 }
193
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100194 if (ifibss->ie_len) {
195 memcpy(pos, ifibss->ie, ifibss->ie_len);
196 pos += ifibss->ie_len;
197 }
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200198
Alexander Simon13c40c52011-11-30 16:56:34 +0100199 /* add HT capability and information IEs */
Johannes Berg4bf88532012-11-09 11:39:59 +0100200 if (chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
Simon Wunderlich0418a442013-05-16 13:00:31 +0200201 chandef.width != NL80211_CHAN_WIDTH_5 &&
202 chandef.width != NL80211_CHAN_WIDTH_10 &&
Johannes Berg683b6d32012-11-08 21:25:48 +0100203 sband->ht_cap.ht_supported) {
Simon Wunderlich822854b2013-06-28 10:39:59 +0200204 struct ieee80211_sta_ht_cap ht_cap;
205
206 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
207 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
208
209 pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
Ashok Nagarajan0d894ec2012-05-07 21:00:29 -0700210 /*
211 * Note: According to 802.11n-2009 9.13.3.1, HT Protection
212 * field and RIFS Mode are reserved in IBSS mode, therefore
213 * keep them at 0
214 */
Johannes Berg074d46d2012-03-15 19:45:16 +0100215 pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
Johannes Berg4bf88532012-11-09 11:39:59 +0100216 &chandef, 0);
Alexander Simon13c40c52011-11-30 16:56:34 +0100217 }
218
Johannes Berg32c50572012-03-28 11:04:29 +0200219 if (local->hw.queues >= IEEE80211_NUM_ACS) {
Bruno Randolf9eba6122010-10-04 11:17:30 +0900220 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
221 *pos++ = 7; /* len */
222 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
223 *pos++ = 0x50;
224 *pos++ = 0xf2;
225 *pos++ = 2; /* WME */
226 *pos++ = 0; /* WME info */
227 *pos++ = 1; /* WME ver */
228 *pos++ = 0; /* U-APSD no in use */
229 }
230
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100231 presp->head_len = pos - presp->head;
232 if (WARN_ON(presp->head_len > frame_len))
233 return;
234
235 rcu_assign_pointer(ifibss->presp, presp);
Johannes Berg46900292009-02-15 12:44:28 +0100236
Johannes Bergd6a83222012-12-14 14:06:28 +0100237 sdata->vif.bss_conf.enable_beacon = true;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200238 sdata->vif.bss_conf.beacon_int = beacon_int;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300239 sdata->vif.bss_conf.basic_rates = basic_rates;
Marek Puzyniak0ca54f62013-04-10 13:19:13 +0200240 sdata->vif.bss_conf.ssid_len = ifibss->ssid_len;
241 memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len);
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200242 bss_change = BSS_CHANGED_BEACON_INT;
243 bss_change |= ieee80211_reset_erp_info(sdata);
244 bss_change |= BSS_CHANGED_BSSID;
245 bss_change |= BSS_CHANGED_BEACON;
246 bss_change |= BSS_CHANGED_BEACON_ENABLED;
Teemu Paasikivi392cfdb2010-06-14 12:55:32 +0300247 bss_change |= BSS_CHANGED_BASIC_RATES;
Alexander Simon13c40c52011-11-30 16:56:34 +0100248 bss_change |= BSS_CHANGED_HT;
Johannes Berg8fc214b2010-04-28 17:40:43 +0200249 bss_change |= BSS_CHANGED_IBSS;
Marek Puzyniak0ca54f62013-04-10 13:19:13 +0200250 bss_change |= BSS_CHANGED_SSID;
Simon Wunderlich2f91a962012-12-03 22:21:30 +0100251
252 /*
253 * In 5 GHz/802.11a, we can always use short slot time.
254 * (IEEE 802.11-2012 18.3.8.7)
255 *
256 * In 2.4GHz, we must always use long slots in IBSS for compatibility
257 * reasons.
258 * (IEEE 802.11-2012 19.4.5)
259 *
260 * HT follows these specifications (IEEE 802.11-2012 20.3.18)
261 */
262 sdata->vif.bss_conf.use_short_slot = chan->band == IEEE80211_BAND_5GHZ;
263 bss_change |= BSS_CHANGED_ERP_SLOT;
264
Johannes Berg8fc214b2010-04-28 17:40:43 +0200265 sdata->vif.bss_conf.ibss_joined = true;
Sujith Manoharanc13a7652012-10-12 17:35:45 +0530266 sdata->vif.bss_conf.ibss_creator = creator;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200267 ieee80211_bss_info_change_notify(sdata, bss_change);
Johannes Berg46900292009-02-15 12:44:28 +0100268
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200269 ieee80211_sta_def_wmm_params(sdata, rates, supp_rates);
Johannes Berg46900292009-02-15 12:44:28 +0100270
Johannes Berg46900292009-02-15 12:44:28 +0100271 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200272 mod_timer(&ifibss->timer,
273 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100274
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200275 scan_width = cfg80211_chandef_to_scan_width(&chandef);
276 bss = cfg80211_inform_bss_width_frame(local->hw.wiphy, chan,
277 scan_width, mgmt,
278 presp->head_len, 0, GFP_KERNEL);
Johannes Berg5b112d32013-02-01 01:49:58 +0100279 cfg80211_put_bss(local->hw.wiphy, bss);
Eliad Peller86a2ea42011-11-08 15:36:59 +0200280 netif_carrier_on(sdata->dev);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200281 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100282}
283
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200284static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
285 struct ieee80211_bss *bss)
Johannes Berg46900292009-02-15 12:44:28 +0100286{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100287 struct cfg80211_bss *cbss =
288 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergb59066a2009-05-12 21:18:38 +0200289 struct ieee80211_supported_band *sband;
290 u32 basic_rates;
291 int i, j;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100292 u16 beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +0100293 const struct cfg80211_bss_ies *ies;
294 u64 tsf;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200295 u32 rate_flags;
296 int shift;
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200297
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200298 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200299
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200300 if (beacon_int < 10)
301 beacon_int = 10;
302
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100303 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200304 rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
305 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Bergb59066a2009-05-12 21:18:38 +0200306
307 basic_rates = 0;
308
309 for (i = 0; i < bss->supp_rates_len; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200310 int rate = bss->supp_rates[i] & 0x7f;
Johannes Bergb59066a2009-05-12 21:18:38 +0200311 bool is_basic = !!(bss->supp_rates[i] & 0x80);
312
313 for (j = 0; j < sband->n_bitrates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200314 int brate;
315 if ((rate_flags & sband->bitrates[j].flags)
316 != rate_flags)
317 continue;
318
319 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
320 5 * (1 << shift));
321 if (brate == rate) {
Johannes Bergb59066a2009-05-12 21:18:38 +0200322 if (is_basic)
323 basic_rates |= BIT(j);
324 break;
325 }
326 }
327 }
328
Johannes Berg8cef2c92013-02-05 16:54:31 +0100329 rcu_read_lock();
330 ies = rcu_dereference(cbss->ies);
331 tsf = ies->tsf;
332 rcu_read_unlock();
333
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100334 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200335 beacon_int,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100336 cbss->channel,
Johannes Bergb59066a2009-05-12 21:18:38 +0200337 basic_rates,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100338 cbss->capability,
Johannes Berg8cef2c92013-02-05 16:54:31 +0100339 tsf, false);
Johannes Berg46900292009-02-15 12:44:28 +0100340}
341
Antonio Quartulli52874a52013-06-18 14:20:40 +0200342static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100343 __acquires(RCU)
344{
345 struct ieee80211_sub_if_data *sdata = sta->sdata;
346 u8 addr[ETH_ALEN];
347
348 memcpy(addr, sta->sta.addr, ETH_ALEN);
349
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200350 ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100351
Johannes Berg83d5cc02012-01-12 09:31:10 +0100352 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
353 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
Antonio Quartulli267335d2012-01-31 20:25:47 +0100354 /* authorize the station only if the network is not RSN protected. If
355 * not wait for the userspace to authorize it */
356 if (!sta->sdata->u.ibss.control_port)
357 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100358
359 rate_control_rate_init(sta);
360
361 /* If it fails, maybe we raced another insertion? */
362 if (sta_info_insert_rcu(sta))
363 return sta_info_get(sdata, addr);
364 return sta;
365}
366
367static struct sta_info *
Antonio Quartulli52874a52013-06-18 14:20:40 +0200368ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
369 const u8 *addr, u32 supp_rates)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100370 __acquires(RCU)
371{
372 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
373 struct ieee80211_local *local = sdata->local;
374 struct sta_info *sta;
Johannes Berg55de9082012-07-26 17:24:39 +0200375 struct ieee80211_chanctx_conf *chanctx_conf;
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -0700376 struct ieee80211_supported_band *sband;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200377 enum nl80211_bss_scan_width scan_width;
Johannes Berg55de9082012-07-26 17:24:39 +0200378 int band;
Johannes Berg8bf11d82011-12-15 11:17:37 +0100379
380 /*
381 * XXX: Consider removing the least recently used entry and
382 * allow new one to be added.
383 */
384 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200385 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
Joe Perchese87cc472012-05-13 21:56:26 +0000386 sdata->name, addr);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100387 rcu_read_lock();
388 return NULL;
389 }
390
391 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
392 rcu_read_lock();
393 return NULL;
394 }
395
Joe Perchesb203ca32012-05-08 18:56:52 +0000396 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
Johannes Berg8bf11d82011-12-15 11:17:37 +0100397 rcu_read_lock();
398 return NULL;
399 }
400
Johannes Berg55de9082012-07-26 17:24:39 +0200401 rcu_read_lock();
402 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
403 if (WARN_ON_ONCE(!chanctx_conf))
404 return NULL;
Johannes Berg4bf88532012-11-09 11:39:59 +0100405 band = chanctx_conf->def.chan->band;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200406 scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
Johannes Berg55de9082012-07-26 17:24:39 +0200407 rcu_read_unlock();
408
Johannes Berg8bf11d82011-12-15 11:17:37 +0100409 sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
410 if (!sta) {
411 rcu_read_lock();
412 return NULL;
413 }
414
415 sta->last_rx = jiffies;
416
417 /* make sure mandatory rates are always added */
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -0700418 sband = local->hw.wiphy->bands[band];
Johannes Berg8bf11d82011-12-15 11:17:37 +0100419 sta->sta.supp_rates[band] = supp_rates |
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200420 ieee80211_mandatory_rates(sband, scan_width);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100421
Antonio Quartulli52874a52013-06-18 14:20:40 +0200422 return ieee80211_ibss_finish_sta(sta);
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100423}
424
Antonio Quartulli2cc59e72012-09-07 13:28:53 +0200425static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
426 struct ieee80211_mgmt *mgmt,
427 size_t len)
428{
429 u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
430
431 if (len < IEEE80211_DEAUTH_FRAME_LEN)
432 return;
433
434 ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM BSSID=%pM (reason: %d)\n",
435 mgmt->sa, mgmt->da, mgmt->bssid, reason);
436 sta_info_destroy_addr(sdata, mgmt->sa);
437}
438
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100439static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
440 struct ieee80211_mgmt *mgmt,
441 size_t len)
442{
443 u16 auth_alg, auth_transaction;
444
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200445 sdata_assert_lock(sdata);
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100446
447 if (len < 24 + 6)
448 return;
449
450 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
451 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
452
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200453 ibss_dbg(sdata,
454 "RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
455 mgmt->sa, mgmt->da, mgmt->bssid, auth_transaction);
Antonio Quartulli7bed2052012-11-25 23:24:27 +0100456
457 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
458 return;
459
Antonio Quartulli2cc59e72012-09-07 13:28:53 +0200460 /*
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100461 * IEEE 802.11 standard does not require authentication in IBSS
462 * networks and most implementations do not seem to use it.
463 * However, try to reply to authentication attempts if someone
464 * has actually implemented this.
465 */
Jouni Malinen700e8ea2012-09-30 19:29:37 +0300466 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
Johannes Berg1672c0e32013-01-29 15:02:27 +0100467 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100468}
469
Johannes Berg46900292009-02-15 12:44:28 +0100470static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200471 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg46900292009-02-15 12:44:28 +0100472 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200473 struct ieee802_11_elems *elems)
Johannes Berg46900292009-02-15 12:44:28 +0100474{
475 struct ieee80211_local *local = sdata->local;
476 int freq;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100477 struct cfg80211_bss *cbss;
Johannes Berg46900292009-02-15 12:44:28 +0100478 struct ieee80211_bss *bss;
479 struct sta_info *sta;
480 struct ieee80211_channel *channel;
481 u64 beacon_timestamp, rx_timestamp;
482 u32 supp_rates = 0;
483 enum ieee80211_band band = rx_status->band;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200484 enum nl80211_bss_scan_width scan_width;
Alexander Simon13c40c52011-11-30 16:56:34 +0100485 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
486 bool rates_updated = false;
Johannes Berg46900292009-02-15 12:44:28 +0100487
Johannes Berg1cd8e882013-03-27 14:30:12 +0100488 if (elems->ds_params)
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900489 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
490 band);
Johannes Berg46900292009-02-15 12:44:28 +0100491 else
492 freq = rx_status->freq;
493
494 channel = ieee80211_get_channel(local->hw.wiphy, freq);
495
496 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
497 return;
498
Bruno Randolf9eba6122010-10-04 11:17:30 +0900499 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Joe Perchesb203ca32012-05-08 18:56:52 +0000500 ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid)) {
Johannes Berg46900292009-02-15 12:44:28 +0100501
502 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100503 sta = sta_info_get(sdata, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100504
Bruno Randolf9eba6122010-10-04 11:17:30 +0900505 if (elems->supp_rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200506 supp_rates = ieee80211_sta_get_rates(sdata, elems,
Ashok Nagarajan9ebb61a2012-04-02 21:21:21 -0700507 band, NULL);
Bruno Randolf9eba6122010-10-04 11:17:30 +0900508 if (sta) {
509 u32 prev_rates;
Johannes Berg46900292009-02-15 12:44:28 +0100510
Bruno Randolf9eba6122010-10-04 11:17:30 +0900511 prev_rates = sta->sta.supp_rates[band];
512 /* make sure mandatory rates are always added */
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200513 scan_width = NL80211_BSS_CHAN_WIDTH_20;
514 if (rx_status->flag & RX_FLAG_5MHZ)
515 scan_width = NL80211_BSS_CHAN_WIDTH_5;
516 if (rx_status->flag & RX_FLAG_10MHZ)
517 scan_width = NL80211_BSS_CHAN_WIDTH_10;
Bruno Randolf9eba6122010-10-04 11:17:30 +0900518
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200519 sta->sta.supp_rates[band] = supp_rates |
520 ieee80211_mandatory_rates(sband,
521 scan_width);
Bruno Randolf9eba6122010-10-04 11:17:30 +0900522 if (sta->sta.supp_rates[band] != prev_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200523 ibss_dbg(sdata,
524 "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
525 sta->sta.addr, prev_rates,
526 sta->sta.supp_rates[band]);
Alexander Simon13c40c52011-11-30 16:56:34 +0100527 rates_updated = true;
Bruno Randolf9eba6122010-10-04 11:17:30 +0900528 }
Johannes Berg8bf11d82011-12-15 11:17:37 +0100529 } else {
530 rcu_read_unlock();
Bruno Randolf9eba6122010-10-04 11:17:30 +0900531 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
Antonio Quartulli52874a52013-06-18 14:20:40 +0200532 mgmt->sa, supp_rates);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100533 }
Johannes Berg34e89502010-02-03 13:59:58 +0100534 }
Bruno Randolf9eba6122010-10-04 11:17:30 +0900535
536 if (sta && elems->wmm_info)
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200537 set_sta_flag(sta, WLAN_STA_WME);
Bruno Randolf9eba6122010-10-04 11:17:30 +0900538
Johannes Berg074d46d2012-03-15 19:45:16 +0100539 if (sta && elems->ht_operation && elems->ht_cap_elem &&
Simon Wunderlich3aede782013-05-16 13:00:36 +0200540 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
541 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
542 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
Alexander Simon13c40c52011-11-30 16:56:34 +0100543 /* we both use HT */
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100544 struct ieee80211_ht_cap htcap_ie;
Johannes Berg4bf88532012-11-09 11:39:59 +0100545 struct cfg80211_chan_def chandef;
546
547 ieee80211_ht_oper_to_chandef(channel,
548 elems->ht_operation,
549 &chandef);
Alexander Simon13c40c52011-11-30 16:56:34 +0100550
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100551 memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
Alexander Simon13c40c52011-11-30 16:56:34 +0100552
553 /*
554 * fall back to HT20 if we don't use or use
555 * the other extension channel
556 */
Simon Wunderlich3aede782013-05-16 13:00:36 +0200557 if (chandef.center_freq1 !=
558 sdata->u.ibss.chandef.center_freq1)
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100559 htcap_ie.cap_info &=
560 cpu_to_le16(~IEEE80211_HT_CAP_SUP_WIDTH_20_40);
Alexander Simon13c40c52011-11-30 16:56:34 +0100561
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100562 rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(
563 sdata, sband, &htcap_ie, sta);
Alexander Simon13c40c52011-11-30 16:56:34 +0100564 }
565
Antonio Quartullie687f612012-08-12 18:24:55 +0200566 if (sta && rates_updated) {
567 drv_sta_rc_update(local, sdata, &sta->sta,
568 IEEE80211_RC_SUPP_RATES_CHANGED);
Alexander Simon13c40c52011-11-30 16:56:34 +0100569 rate_control_rate_init(sta);
Antonio Quartullie687f612012-08-12 18:24:55 +0200570 }
Alexander Simon13c40c52011-11-30 16:56:34 +0100571
Bruno Randolf9eba6122010-10-04 11:17:30 +0900572 rcu_read_unlock();
Johannes Berg46900292009-02-15 12:44:28 +0100573 }
574
575 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200576 channel);
Johannes Berg46900292009-02-15 12:44:28 +0100577 if (!bss)
578 return;
579
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100580 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
581
Johannes Berg8cef2c92013-02-05 16:54:31 +0100582 /* same for beacon and probe response */
583 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
Johannes Berg46900292009-02-15 12:44:28 +0100584
585 /* check if we need to merge IBSS */
586
Johannes Berg46900292009-02-15 12:44:28 +0100587 /* we use a fixed BSSID */
Benoit Papillaulta98bfec2010-01-17 22:45:24 +0100588 if (sdata->u.ibss.fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100589 goto put_bss;
590
591 /* not an IBSS */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100592 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
Johannes Berg46900292009-02-15 12:44:28 +0100593 goto put_bss;
594
595 /* different channel */
Johannes Berg55de9082012-07-26 17:24:39 +0200596 if (sdata->u.ibss.fixed_channel &&
Simon Wunderlich3aede782013-05-16 13:00:36 +0200597 sdata->u.ibss.chandef.chan != cbss->channel)
Johannes Berg46900292009-02-15 12:44:28 +0100598 goto put_bss;
599
600 /* different SSID */
601 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
602 memcmp(elems->ssid, sdata->u.ibss.ssid,
603 sdata->u.ibss.ssid_len))
604 goto put_bss;
605
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100606 /* same BSSID */
Joe Perchesb203ca32012-05-08 18:56:52 +0000607 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100608 goto put_bss;
609
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800610 if (ieee80211_have_rx_timestamp(rx_status)) {
611 /* time when timestamp field was received */
612 rx_timestamp =
613 ieee80211_calculate_rx_timestamp(local, rx_status,
614 len + FCS_LEN, 24);
Johannes Berg24487982009-04-23 18:52:52 +0200615 } else {
616 /*
617 * second best option: get current TSF
618 * (will return -1 if not supported)
619 */
Eliad Peller37a41b42011-09-21 14:06:11 +0300620 rx_timestamp = drv_get_tsf(local, sdata);
Johannes Berg24487982009-04-23 18:52:52 +0200621 }
Johannes Berg46900292009-02-15 12:44:28 +0100622
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200623 ibss_dbg(sdata,
624 "RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
625 mgmt->sa, mgmt->bssid,
626 (unsigned long long)rx_timestamp,
627 (unsigned long long)beacon_timestamp,
628 (unsigned long long)(rx_timestamp - beacon_timestamp),
629 jiffies);
Johannes Berg46900292009-02-15 12:44:28 +0100630
631 if (beacon_timestamp > rx_timestamp) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200632 ibss_dbg(sdata,
633 "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
634 mgmt->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100635 ieee80211_sta_join_ibss(sdata, bss);
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200636 supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
Johannes Berg34e89502010-02-03 13:59:58 +0100637 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
Antonio Quartulli52874a52013-06-18 14:20:40 +0200638 supp_rates);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100639 rcu_read_unlock();
Johannes Berg46900292009-02-15 12:44:28 +0100640 }
641
642 put_bss:
643 ieee80211_rx_bss_put(local, bss);
644}
645
Johannes Berg8bf11d82011-12-15 11:17:37 +0100646void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
647 const u8 *bssid, const u8 *addr,
648 u32 supp_rates)
Johannes Berg46900292009-02-15 12:44:28 +0100649{
Felix Fietkau2e10d332009-12-20 19:07:09 +0100650 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100651 struct ieee80211_local *local = sdata->local;
652 struct sta_info *sta;
Johannes Berg55de9082012-07-26 17:24:39 +0200653 struct ieee80211_chanctx_conf *chanctx_conf;
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -0700654 struct ieee80211_supported_band *sband;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200655 enum nl80211_bss_scan_width scan_width;
Johannes Berg55de9082012-07-26 17:24:39 +0200656 int band;
Johannes Berg46900292009-02-15 12:44:28 +0100657
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200658 /*
659 * XXX: Consider removing the least recently used entry and
660 * allow new one to be added.
661 */
Johannes Berg46900292009-02-15 12:44:28 +0100662 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200663 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
Joe Perchese87cc472012-05-13 21:56:26 +0000664 sdata->name, addr);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100665 return;
Johannes Berg46900292009-02-15 12:44:28 +0100666 }
667
Felix Fietkau2e10d332009-12-20 19:07:09 +0100668 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100669 return;
Felix Fietkau2e10d332009-12-20 19:07:09 +0100670
Joe Perchesb203ca32012-05-08 18:56:52 +0000671 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
Johannes Berg8bf11d82011-12-15 11:17:37 +0100672 return;
Johannes Berg46900292009-02-15 12:44:28 +0100673
Johannes Berg55de9082012-07-26 17:24:39 +0200674 rcu_read_lock();
675 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
676 if (WARN_ON_ONCE(!chanctx_conf)) {
677 rcu_read_unlock();
678 return;
679 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100680 band = chanctx_conf->def.chan->band;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200681 scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
Johannes Berg55de9082012-07-26 17:24:39 +0200682 rcu_read_unlock();
683
Johannes Berg8bf11d82011-12-15 11:17:37 +0100684 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
Johannes Berg46900292009-02-15 12:44:28 +0100685 if (!sta)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100686 return;
Johannes Berg46900292009-02-15 12:44:28 +0100687
Rajkumar Manoharanc8716d92010-10-23 10:59:57 +0530688 sta->last_rx = jiffies;
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100689
Johannes Berg46900292009-02-15 12:44:28 +0100690 /* make sure mandatory rates are always added */
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -0700691 sband = local->hw.wiphy->bands[band];
Johannes Berg46900292009-02-15 12:44:28 +0100692 sta->sta.supp_rates[band] = supp_rates |
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200693 ieee80211_mandatory_rates(sband, scan_width);
Johannes Berg46900292009-02-15 12:44:28 +0100694
Johannes Berg8bf11d82011-12-15 11:17:37 +0100695 spin_lock(&ifibss->incomplete_lock);
696 list_add(&sta->list, &ifibss->incomplete_stations);
697 spin_unlock(&ifibss->incomplete_lock);
698 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +0100699}
700
701static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
702{
703 struct ieee80211_local *local = sdata->local;
704 int active = 0;
705 struct sta_info *sta;
706
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200707 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200708
Johannes Berg46900292009-02-15 12:44:28 +0100709 rcu_read_lock();
710
711 list_for_each_entry_rcu(sta, &local->sta_list, list) {
712 if (sta->sdata == sdata &&
713 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
714 jiffies)) {
715 active++;
716 break;
717 }
718 }
719
720 rcu_read_unlock();
721
722 return active;
723}
724
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100725/*
726 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
727 */
Johannes Berg46900292009-02-15 12:44:28 +0100728
729static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
730{
731 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200732 enum nl80211_bss_scan_width scan_width;
Johannes Berg46900292009-02-15 12:44:28 +0100733
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200734 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200735
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200736 mod_timer(&ifibss->timer,
737 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100738
739 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200740
Sujith450aae32009-11-02 12:33:23 +0530741 if (time_before(jiffies, ifibss->last_scan_completed +
742 IEEE80211_IBSS_MERGE_INTERVAL))
743 return;
744
Johannes Berg46900292009-02-15 12:44:28 +0100745 if (ieee80211_sta_active_ibss(sdata))
746 return;
747
John W. Linvillec037b832012-01-30 15:28:11 -0500748 if (ifibss->fixed_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100749 return;
750
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200751 sdata_info(sdata,
752 "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
Johannes Berg46900292009-02-15 12:44:28 +0100753
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200754 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +0100755 ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200756 NULL, scan_width);
Johannes Berg46900292009-02-15 12:44:28 +0100757}
758
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200759static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100760{
761 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100762 u8 bssid[ETH_ALEN];
Johannes Berg46900292009-02-15 12:44:28 +0100763 u16 capability;
764 int i;
765
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200766 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200767
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200768 if (ifibss->fixed_bssid) {
Johannes Berg46900292009-02-15 12:44:28 +0100769 memcpy(bssid, ifibss->bssid, ETH_ALEN);
770 } else {
771 /* Generate random, not broadcast, locally administered BSSID. Mix in
772 * own MAC address to make sure that devices that do not have proper
773 * random number generator get different BSSID. */
774 get_random_bytes(bssid, ETH_ALEN);
775 for (i = 0; i < ETH_ALEN; i++)
Johannes Berg47846c92009-11-25 17:46:19 +0100776 bssid[i] ^= sdata->vif.addr[i];
Johannes Berg46900292009-02-15 12:44:28 +0100777 bssid[0] &= ~0x01;
778 bssid[0] |= 0x02;
779 }
780
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200781 sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100782
Johannes Berg46900292009-02-15 12:44:28 +0100783 capability = WLAN_CAPABILITY_IBSS;
784
Johannes Bergfffd0932009-07-08 14:22:54 +0200785 if (ifibss->privacy)
Johannes Berg46900292009-02-15 12:44:28 +0100786 capability |= WLAN_CAPABILITY_PRIVACY;
787 else
788 sdata->drop_unencrypted = 0;
789
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200790 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
Simon Wunderlich3aede782013-05-16 13:00:36 +0200791 ifibss->chandef.chan, ifibss->basic_rates,
Sujith Manoharanc13a7652012-10-12 17:35:45 +0530792 capability, 0, true);
Johannes Berg46900292009-02-15 12:44:28 +0100793}
794
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100795/*
796 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
797 */
798
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200799static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100800{
801 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
802 struct ieee80211_local *local = sdata->local;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100803 struct cfg80211_bss *cbss;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200804 struct ieee80211_channel *chan = NULL;
Johannes Berg46900292009-02-15 12:44:28 +0100805 const u8 *bssid = NULL;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200806 enum nl80211_bss_scan_width scan_width;
Johannes Berg46900292009-02-15 12:44:28 +0100807 int active_ibss;
Johannes Berge0d61882009-05-12 20:47:32 +0200808 u16 capability;
Johannes Berg46900292009-02-15 12:44:28 +0100809
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200810 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200811
Johannes Berg46900292009-02-15 12:44:28 +0100812 active_ibss = ieee80211_sta_active_ibss(sdata);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200813 ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
Johannes Berg46900292009-02-15 12:44:28 +0100814
815 if (active_ibss)
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200816 return;
Johannes Berg46900292009-02-15 12:44:28 +0100817
Johannes Berge0d61882009-05-12 20:47:32 +0200818 capability = WLAN_CAPABILITY_IBSS;
Johannes Bergfffd0932009-07-08 14:22:54 +0200819 if (ifibss->privacy)
Johannes Berge0d61882009-05-12 20:47:32 +0200820 capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200821 if (ifibss->fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100822 bssid = ifibss->bssid;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200823 if (ifibss->fixed_channel)
Simon Wunderlich3aede782013-05-16 13:00:36 +0200824 chan = ifibss->chandef.chan;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200825 if (!is_zero_ether_addr(ifibss->bssid))
826 bssid = ifibss->bssid;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100827 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
828 ifibss->ssid, ifibss->ssid_len,
829 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
830 capability);
Johannes Berg46900292009-02-15 12:44:28 +0100831
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100832 if (cbss) {
833 struct ieee80211_bss *bss;
834
835 bss = (void *)cbss->priv;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200836 ibss_dbg(sdata,
837 "sta_find_ibss: selected %pM current %pM\n",
838 cbss->bssid, ifibss->bssid);
839 sdata_info(sdata,
840 "Selected IBSS BSSID %pM based on configured SSID\n",
841 cbss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100842
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200843 ieee80211_sta_join_ibss(sdata, bss);
Johannes Berg46900292009-02-15 12:44:28 +0100844 ieee80211_rx_bss_put(local, bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200845 return;
Reinette Chatred419b9f2009-10-19 14:55:37 -0700846 }
Johannes Berg46900292009-02-15 12:44:28 +0100847
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200848 ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
Johannes Berg46900292009-02-15 12:44:28 +0100849
850 /* Selected IBSS not found in current scan results - try to scan */
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100851 if (time_after(jiffies, ifibss->last_scan_completed +
Johannes Berg46900292009-02-15 12:44:28 +0100852 IEEE80211_SCAN_INTERVAL)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200853 sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
Johannes Berg46900292009-02-15 12:44:28 +0100854
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200855 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +0100856 ieee80211_request_ibss_scan(sdata, ifibss->ssid,
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200857 ifibss->ssid_len, chan,
858 scan_width);
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100859 } else {
Johannes Berg46900292009-02-15 12:44:28 +0100860 int interval = IEEE80211_SCAN_INTERVAL;
861
862 if (time_after(jiffies, ifibss->ibss_join_req +
Johannes Berg55de9082012-07-26 17:24:39 +0200863 IEEE80211_IBSS_JOIN_TIMEOUT))
864 ieee80211_sta_create_ibss(sdata);
Johannes Berg46900292009-02-15 12:44:28 +0100865
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200866 mod_timer(&ifibss->timer,
867 round_jiffies(jiffies + interval));
Johannes Berg46900292009-02-15 12:44:28 +0100868 }
Johannes Berg46900292009-02-15 12:44:28 +0100869}
870
871static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
Johannes Bergc269a202011-02-14 12:20:22 +0100872 struct sk_buff *req)
Johannes Berg46900292009-02-15 12:44:28 +0100873{
Johannes Bergc269a202011-02-14 12:20:22 +0100874 struct ieee80211_mgmt *mgmt = (void *)req->data;
Johannes Berg46900292009-02-15 12:44:28 +0100875 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
876 struct ieee80211_local *local = sdata->local;
Johannes Bergc269a202011-02-14 12:20:22 +0100877 int tx_last_beacon, len = req->len;
Johannes Berg46900292009-02-15 12:44:28 +0100878 struct sk_buff *skb;
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100879 struct beacon_data *presp;
Johannes Berg46900292009-02-15 12:44:28 +0100880 u8 *pos, *end;
881
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200882 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200883
Johannes Berg40b275b2011-05-13 14:15:49 +0200884 presp = rcu_dereference_protected(ifibss->presp,
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200885 lockdep_is_held(&sdata->wdev.mtx));
Johannes Berg40b275b2011-05-13 14:15:49 +0200886
Johannes Berg46900292009-02-15 12:44:28 +0100887 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
Johannes Berg40b275b2011-05-13 14:15:49 +0200888 len < 24 + 2 || !presp)
Johannes Berg46900292009-02-15 12:44:28 +0100889 return;
890
Johannes Berg24487982009-04-23 18:52:52 +0200891 tx_last_beacon = drv_tx_last_beacon(local);
Johannes Berg46900292009-02-15 12:44:28 +0100892
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200893 ibss_dbg(sdata,
894 "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
895 mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
Johannes Berg46900292009-02-15 12:44:28 +0100896
Felix Fietkau1ed76482011-03-24 19:46:18 +0100897 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
Johannes Berg46900292009-02-15 12:44:28 +0100898 return;
899
Joe Perchesb203ca32012-05-08 18:56:52 +0000900 if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
Felix Fietkau888d04d2012-03-01 15:22:09 +0100901 !is_broadcast_ether_addr(mgmt->bssid))
Johannes Berg46900292009-02-15 12:44:28 +0100902 return;
903
904 end = ((u8 *) mgmt) + len;
905 pos = mgmt->u.probe_req.variable;
906 if (pos[0] != WLAN_EID_SSID ||
907 pos + 2 + pos[1] > end) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200908 ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
909 mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100910 return;
911 }
912 if (pos[1] != 0 &&
913 (pos[1] != ifibss->ssid_len ||
Benoit Papillault0da780c2010-02-05 01:21:03 +0100914 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
Johannes Berg46900292009-02-15 12:44:28 +0100915 /* Ignore ProbeReq for foreign SSID */
916 return;
917 }
918
919 /* Reply with ProbeResp */
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100920 skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
Johannes Berg46900292009-02-15 12:44:28 +0100921 if (!skb)
922 return;
923
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100924 skb_reserve(skb, local->tx_headroom);
925 memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
926
927 memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
928 ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
Johannes Berg62ae67b2009-11-18 18:42:05 +0100929 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
930 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100931}
932
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200933static
934void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
935 struct ieee80211_mgmt *mgmt, size_t len,
936 struct ieee80211_rx_status *rx_status)
Johannes Berg46900292009-02-15 12:44:28 +0100937{
938 size_t baselen;
939 struct ieee802_11_elems elems;
940
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200941 BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
942 offsetof(typeof(mgmt->u.beacon), variable));
943
944 /*
945 * either beacon or probe_resp but the variable field is at the
946 * same offset
947 */
Johannes Berg46900292009-02-15 12:44:28 +0100948 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
949 if (baselen > len)
950 return;
951
952 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
Johannes Bergb2e506b2013-03-26 14:54:16 +0100953 false, &elems);
Johannes Berg46900292009-02-15 12:44:28 +0100954
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200955 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg46900292009-02-15 12:44:28 +0100956}
957
Johannes Berg1fa57d02010-06-10 10:21:32 +0200958void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
959 struct sk_buff *skb)
Johannes Berg46900292009-02-15 12:44:28 +0100960{
961 struct ieee80211_rx_status *rx_status;
962 struct ieee80211_mgmt *mgmt;
963 u16 fc;
964
Johannes Bergf1d58c22009-06-17 13:13:00 +0200965 rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Berg46900292009-02-15 12:44:28 +0100966 mgmt = (struct ieee80211_mgmt *) skb->data;
967 fc = le16_to_cpu(mgmt->frame_control);
968
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200969 sdata_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200970
Tim Harveyc926d002010-12-09 10:43:13 -0800971 if (!sdata->u.ibss.ssid_len)
972 goto mgmt_out; /* not ready to merge yet */
973
Johannes Berg46900292009-02-15 12:44:28 +0100974 switch (fc & IEEE80211_FCTL_STYPE) {
975 case IEEE80211_STYPE_PROBE_REQ:
Johannes Bergc269a202011-02-14 12:20:22 +0100976 ieee80211_rx_mgmt_probe_req(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100977 break;
978 case IEEE80211_STYPE_PROBE_RESP:
Johannes Berg46900292009-02-15 12:44:28 +0100979 case IEEE80211_STYPE_BEACON:
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200980 ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
981 rx_status);
Johannes Berg46900292009-02-15 12:44:28 +0100982 break;
983 case IEEE80211_STYPE_AUTH:
984 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
985 break;
Antonio Quartulli2cc59e72012-09-07 13:28:53 +0200986 case IEEE80211_STYPE_DEAUTH:
987 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
988 break;
Johannes Berg46900292009-02-15 12:44:28 +0100989 }
Johannes Berg7a17a332010-07-21 11:30:27 +0200990
Tim Harveyc926d002010-12-09 10:43:13 -0800991 mgmt_out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200992 sdata_unlock(sdata);
Johannes Berg46900292009-02-15 12:44:28 +0100993}
994
Johannes Berg1fa57d02010-06-10 10:21:32 +0200995void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100996{
Johannes Berg1fa57d02010-06-10 10:21:32 +0200997 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg8bf11d82011-12-15 11:17:37 +0100998 struct sta_info *sta;
Johannes Berg46900292009-02-15 12:44:28 +0100999
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001000 sdata_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +02001001
1002 /*
1003 * Work could be scheduled after scan or similar
1004 * when we aren't even joined (or trying) with a
1005 * network.
1006 */
1007 if (!ifibss->ssid_len)
1008 goto out;
Johannes Berg46900292009-02-15 12:44:28 +01001009
Johannes Berg8bf11d82011-12-15 11:17:37 +01001010 spin_lock_bh(&ifibss->incomplete_lock);
1011 while (!list_empty(&ifibss->incomplete_stations)) {
1012 sta = list_first_entry(&ifibss->incomplete_stations,
1013 struct sta_info, list);
1014 list_del(&sta->list);
1015 spin_unlock_bh(&ifibss->incomplete_lock);
1016
Antonio Quartulli52874a52013-06-18 14:20:40 +02001017 ieee80211_ibss_finish_sta(sta);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001018 rcu_read_unlock();
1019 spin_lock_bh(&ifibss->incomplete_lock);
1020 }
1021 spin_unlock_bh(&ifibss->incomplete_lock);
1022
Johannes Berg46900292009-02-15 12:44:28 +01001023 switch (ifibss->state) {
1024 case IEEE80211_IBSS_MLME_SEARCH:
1025 ieee80211_sta_find_ibss(sdata);
1026 break;
1027 case IEEE80211_IBSS_MLME_JOINED:
1028 ieee80211_sta_merge_ibss(sdata);
1029 break;
1030 default:
1031 WARN_ON(1);
1032 break;
1033 }
Johannes Berg46900292009-02-15 12:44:28 +01001034
Johannes Berg7a17a332010-07-21 11:30:27 +02001035 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001036 sdata_unlock(sdata);
Johannes Berg3a4d4aa2010-05-26 16:41:40 +02001037}
1038
Johannes Berg46900292009-02-15 12:44:28 +01001039static void ieee80211_ibss_timer(unsigned long data)
1040{
1041 struct ieee80211_sub_if_data *sdata =
1042 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01001043
Stanislaw Gruszkaa6182942013-02-28 10:55:28 +01001044 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +01001045}
1046
1047void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1048{
1049 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1050
Johannes Berg46900292009-02-15 12:44:28 +01001051 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
1052 (unsigned long) sdata);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001053 INIT_LIST_HEAD(&ifibss->incomplete_stations);
1054 spin_lock_init(&ifibss->incomplete_lock);
Johannes Berg46900292009-02-15 12:44:28 +01001055}
1056
1057/* scan finished notification */
1058void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1059{
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001060 struct ieee80211_sub_if_data *sdata;
Johannes Berg46900292009-02-15 12:44:28 +01001061
Johannes Berg29b4a4f2009-04-21 00:30:49 +02001062 mutex_lock(&local->iflist_mtx);
1063 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001064 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e41f712009-04-23 11:48:56 +02001065 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001066 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1067 continue;
1068 sdata->u.ibss.last_scan_completed = jiffies;
Johannes Berg7a17a332010-07-21 11:30:27 +02001069 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +01001070 }
Johannes Berg29b4a4f2009-04-21 00:30:49 +02001071 mutex_unlock(&local->iflist_mtx);
Johannes Berg46900292009-02-15 12:44:28 +01001072}
1073
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001074int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1075 struct cfg80211_ibss_params *params)
1076{
Simon Wunderlichff3cc5f2011-11-30 16:56:33 +01001077 u32 changed = 0;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001078 u32 rate_flags;
1079 struct ieee80211_supported_band *sband;
1080 int i;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001081
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001082 if (params->bssid) {
1083 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1084 sdata->u.ibss.fixed_bssid = true;
1085 } else
1086 sdata->u.ibss.fixed_bssid = false;
1087
Johannes Bergfffd0932009-07-08 14:22:54 +02001088 sdata->u.ibss.privacy = params->privacy;
Antonio Quartulli267335d2012-01-31 20:25:47 +01001089 sdata->u.ibss.control_port = params->control_port;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03001090 sdata->u.ibss.basic_rates = params->basic_rates;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001091
1092 /* fix basic_rates if channel does not support these rates */
1093 rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
1094 sband = sdata->local->hw.wiphy->bands[params->chandef.chan->band];
1095 for (i = 0; i < sband->n_bitrates; i++) {
1096 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1097 sdata->u.ibss.basic_rates &= ~BIT(i);
1098 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01001099 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1100 sizeof(params->mcast_rate));
Johannes Bergfffd0932009-07-08 14:22:54 +02001101
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001102 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1103
Simon Wunderlich3aede782013-05-16 13:00:36 +02001104 sdata->u.ibss.chandef = params->chandef;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001105 sdata->u.ibss.fixed_channel = params->channel_fixed;
1106
1107 if (params->ie) {
1108 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1109 GFP_KERNEL);
1110 if (sdata->u.ibss.ie)
1111 sdata->u.ibss.ie_len = params->ie_len;
1112 }
1113
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001114 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1115 sdata->u.ibss.ibss_join_req = jiffies;
1116
Antonio Quartullibadecb02012-10-26 18:54:25 +02001117 memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
Johannes Berg0e41f712009-04-23 11:48:56 +02001118 sdata->u.ibss.ssid_len = params->ssid_len;
1119
Simon Wunderlich822854b2013-06-28 10:39:59 +02001120 memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
1121 sizeof(sdata->u.ibss.ht_capa));
1122 memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
1123 sizeof(sdata->u.ibss.ht_capa_mask));
1124
Simon Wunderlichff3cc5f2011-11-30 16:56:33 +01001125 /*
1126 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1127 * reserved, but an HT STA shall protect HT transmissions as though
1128 * the HT Protection field were set to non-HT mixed mode.
1129 *
1130 * In an IBSS, the RIFS Mode field of the HT Operation element is
1131 * also reserved, but an HT STA shall operate as though this field
1132 * were set to 1.
1133 */
1134
1135 sdata->vif.bss_conf.ht_operation_mode |=
1136 IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1137 | IEEE80211_HT_PARAM_RIFS_MODE;
1138
1139 changed |= BSS_CHANGED_HT;
1140 ieee80211_bss_info_change_notify(sdata, changed);
1141
Johannes Berg04ecd252012-09-11 14:34:12 +02001142 sdata->smps_mode = IEEE80211_SMPS_OFF;
1143 sdata->needed_rx_chains = sdata->local->rx_chains;
1144
Johannes Berg64592c82010-06-10 10:21:31 +02001145 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001146
1147 return 0;
1148}
1149
1150int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1151{
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001152 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1153 struct ieee80211_local *local = sdata->local;
1154 struct cfg80211_bss *cbss;
1155 u16 capability;
Johannes Berg7a17a332010-07-21 11:30:27 +02001156 int active_ibss;
Johannes Berg8bf11d82011-12-15 11:17:37 +01001157 struct sta_info *sta;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01001158 struct beacon_data *presp;
Johannes Berg7a17a332010-07-21 11:30:27 +02001159
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001160 active_ibss = ieee80211_sta_active_ibss(sdata);
1161
1162 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
1163 capability = WLAN_CAPABILITY_IBSS;
1164
1165 if (ifibss->privacy)
1166 capability |= WLAN_CAPABILITY_PRIVACY;
1167
Simon Wunderlich3aede782013-05-16 13:00:36 +02001168 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001169 ifibss->bssid, ifibss->ssid,
1170 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
1171 WLAN_CAPABILITY_PRIVACY,
1172 capability);
1173
1174 if (cbss) {
1175 cfg80211_unlink_bss(local->hw.wiphy, cbss);
Johannes Berg5b112d32013-02-01 01:49:58 +01001176 cfg80211_put_bss(local->hw.wiphy, cbss);
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001177 }
1178 }
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001179
Simon Wunderlichb78a4932012-11-13 18:43:03 +01001180 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
1181 memset(ifibss->bssid, 0, ETH_ALEN);
1182 ifibss->ssid_len = 0;
1183
Johannes Bergb998e8b2012-12-13 23:07:46 +01001184 sta_info_flush(sdata);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001185
1186 spin_lock_bh(&ifibss->incomplete_lock);
1187 while (!list_empty(&ifibss->incomplete_stations)) {
1188 sta = list_first_entry(&ifibss->incomplete_stations,
1189 struct sta_info, list);
1190 list_del(&sta->list);
1191 spin_unlock_bh(&ifibss->incomplete_lock);
1192
1193 sta_info_free(local, sta);
1194 spin_lock_bh(&ifibss->incomplete_lock);
1195 }
1196 spin_unlock_bh(&ifibss->incomplete_lock);
1197
Eliad Peller86a2ea42011-11-08 15:36:59 +02001198 netif_carrier_off(sdata->dev);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001199
1200 /* remove beacon */
1201 kfree(sdata->u.ibss.ie);
Johannes Bergc3ffeab2013-03-07 20:54:29 +01001202 presp = rcu_dereference_protected(ifibss->presp,
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001203 lockdep_is_held(&sdata->wdev.mtx));
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001204 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
Simon Wunderlich822854b2013-06-28 10:39:59 +02001205
1206 /* on the next join, re-program HT parameters */
1207 memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
1208 memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
1209
Johannes Berg8fc214b2010-04-28 17:40:43 +02001210 sdata->vif.bss_conf.ibss_joined = false;
Sujith Manoharanc13a7652012-10-12 17:35:45 +05301211 sdata->vif.bss_conf.ibss_creator = false;
Johannes Bergd6a83222012-12-14 14:06:28 +01001212 sdata->vif.bss_conf.enable_beacon = false;
Marek Puzyniak0ca54f62013-04-10 13:19:13 +02001213 sdata->vif.bss_conf.ssid_len = 0;
Johannes Bergd6a83222012-12-14 14:06:28 +01001214 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
Johannes Berg8fc214b2010-04-28 17:40:43 +02001215 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1216 BSS_CHANGED_IBSS);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001217 synchronize_rcu();
Johannes Bergc3ffeab2013-03-07 20:54:29 +01001218 kfree(presp);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001219
Johannes Berg35f20c12010-06-10 10:21:30 +02001220 skb_queue_purge(&sdata->skb_queue);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001221
Johannes Bergbc05d192010-07-21 10:52:40 +02001222 del_timer_sync(&sdata->u.ibss.timer);
Johannes Berg7a17a332010-07-21 11:30:27 +02001223
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001224 return 0;
1225}