ibss.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /*
  2. * IBSS mode implementation
  3. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright 2004, Instant802 Networks, Inc.
  5. * Copyright 2005, Devicescape Software, Inc.
  6. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  8. * Copyright 2009, Johannes Berg <johannes@sipsolutions.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/rtnetlink.h>
  21. #include <net/mac80211.h>
  22. #include "ieee80211_i.h"
  23. #include "driver-ops.h"
  24. #include "rate.h"
  25. #define IEEE80211_SCAN_INTERVAL (2 * HZ)
  26. #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
  27. #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
  28. #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
  29. #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
  30. static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  31. const u8 *bssid, const int beacon_int,
  32. struct ieee80211_channel *chan,
  33. const u32 basic_rates,
  34. const u16 capability, u64 tsf,
  35. bool creator)
  36. {
  37. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  38. struct ieee80211_local *local = sdata->local;
  39. int rates_n = 0, i, ri;
  40. struct ieee80211_mgmt *mgmt;
  41. u8 *pos;
  42. struct ieee80211_supported_band *sband;
  43. struct cfg80211_bss *bss;
  44. u32 bss_change, rate_flags, rates = 0, rates_added = 0;
  45. u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
  46. struct cfg80211_chan_def chandef;
  47. struct beacon_data *presp;
  48. int frame_len;
  49. int shift;
  50. sdata_assert_lock(sdata);
  51. /* Reset own TSF to allow time synchronization work. */
  52. drv_reset_tsf(local, sdata);
  53. if (!ether_addr_equal(ifibss->bssid, bssid))
  54. sta_info_flush(sdata);
  55. /* if merging, indicate to driver that we leave the old IBSS */
  56. if (sdata->vif.bss_conf.ibss_joined) {
  57. sdata->vif.bss_conf.ibss_joined = false;
  58. sdata->vif.bss_conf.ibss_creator = false;
  59. sdata->vif.bss_conf.enable_beacon = false;
  60. netif_carrier_off(sdata->dev);
  61. ieee80211_bss_info_change_notify(sdata,
  62. BSS_CHANGED_IBSS |
  63. BSS_CHANGED_BEACON_ENABLED);
  64. }
  65. presp = rcu_dereference_protected(ifibss->presp,
  66. lockdep_is_held(&sdata->wdev.mtx));
  67. rcu_assign_pointer(ifibss->presp, NULL);
  68. if (presp)
  69. kfree_rcu(presp, rcu_head);
  70. sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
  71. chandef = ifibss->chandef;
  72. if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef)) {
  73. chandef.width = NL80211_CHAN_WIDTH_20;
  74. chandef.center_freq1 = chan->center_freq;
  75. }
  76. ieee80211_vif_release_channel(sdata);
  77. if (ieee80211_vif_use_channel(sdata, &chandef,
  78. ifibss->fixed_channel ?
  79. IEEE80211_CHANCTX_SHARED :
  80. IEEE80211_CHANCTX_EXCLUSIVE)) {
  81. sdata_info(sdata, "Failed to join IBSS, no channel context\n");
  82. return;
  83. }
  84. memcpy(ifibss->bssid, bssid, ETH_ALEN);
  85. sband = local->hw.wiphy->bands[chan->band];
  86. shift = ieee80211_vif_get_shift(&sdata->vif);
  87. /* Build IBSS probe response */
  88. frame_len = sizeof(struct ieee80211_hdr_3addr) +
  89. 12 /* struct ieee80211_mgmt.u.beacon */ +
  90. 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
  91. 2 + 8 /* max Supported Rates */ +
  92. 3 /* max DS params */ +
  93. 4 /* IBSS params */ +
  94. 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  95. 2 + sizeof(struct ieee80211_ht_cap) +
  96. 2 + sizeof(struct ieee80211_ht_operation) +
  97. ifibss->ie_len;
  98. presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
  99. if (!presp)
  100. return;
  101. presp->head = (void *)(presp + 1);
  102. mgmt = (void *) presp->head;
  103. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  104. IEEE80211_STYPE_PROBE_RESP);
  105. eth_broadcast_addr(mgmt->da);
  106. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  107. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  108. mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
  109. mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
  110. mgmt->u.beacon.capab_info = cpu_to_le16(capability);
  111. pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
  112. *pos++ = WLAN_EID_SSID;
  113. *pos++ = ifibss->ssid_len;
  114. memcpy(pos, ifibss->ssid, ifibss->ssid_len);
  115. pos += ifibss->ssid_len;
  116. rate_flags = ieee80211_chandef_rate_flags(&chandef);
  117. for (i = 0; i < sband->n_bitrates; i++) {
  118. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  119. continue;
  120. rates |= BIT(i);
  121. rates_n++;
  122. }
  123. *pos++ = WLAN_EID_SUPP_RATES;
  124. *pos++ = min_t(int, 8, rates_n);
  125. for (ri = 0; ri < sband->n_bitrates; ri++) {
  126. int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
  127. 5 * (1 << shift));
  128. u8 basic = 0;
  129. if (!(rates & BIT(ri)))
  130. continue;
  131. if (basic_rates & BIT(ri))
  132. basic = 0x80;
  133. *pos++ = basic | (u8) rate;
  134. if (++rates_added == 8)
  135. break;
  136. }
  137. if (sband->band == IEEE80211_BAND_2GHZ) {
  138. *pos++ = WLAN_EID_DS_PARAMS;
  139. *pos++ = 1;
  140. *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
  141. }
  142. *pos++ = WLAN_EID_IBSS_PARAMS;
  143. *pos++ = 2;
  144. /* FIX: set ATIM window based on scan results */
  145. *pos++ = 0;
  146. *pos++ = 0;
  147. /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
  148. if (rates_n > 8) {
  149. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  150. *pos++ = rates_n - 8;
  151. for (; ri < sband->n_bitrates; ri++) {
  152. int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
  153. 5 * (1 << shift));
  154. u8 basic = 0;
  155. if (!(rates & BIT(ri)))
  156. continue;
  157. if (basic_rates & BIT(ri))
  158. basic = 0x80;
  159. *pos++ = basic | (u8) rate;
  160. }
  161. }
  162. if (ifibss->ie_len) {
  163. memcpy(pos, ifibss->ie, ifibss->ie_len);
  164. pos += ifibss->ie_len;
  165. }
  166. /* add HT capability and information IEs */
  167. if (chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
  168. chandef.width != NL80211_CHAN_WIDTH_5 &&
  169. chandef.width != NL80211_CHAN_WIDTH_10 &&
  170. sband->ht_cap.ht_supported) {
  171. struct ieee80211_sta_ht_cap ht_cap;
  172. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  173. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  174. pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  175. /*
  176. * Note: According to 802.11n-2009 9.13.3.1, HT Protection
  177. * field and RIFS Mode are reserved in IBSS mode, therefore
  178. * keep them at 0
  179. */
  180. pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
  181. &chandef, 0);
  182. }
  183. if (local->hw.queues >= IEEE80211_NUM_ACS) {
  184. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  185. *pos++ = 7; /* len */
  186. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  187. *pos++ = 0x50;
  188. *pos++ = 0xf2;
  189. *pos++ = 2; /* WME */
  190. *pos++ = 0; /* WME info */
  191. *pos++ = 1; /* WME ver */
  192. *pos++ = 0; /* U-APSD no in use */
  193. }
  194. presp->head_len = pos - presp->head;
  195. if (WARN_ON(presp->head_len > frame_len))
  196. return;
  197. rcu_assign_pointer(ifibss->presp, presp);
  198. sdata->vif.bss_conf.enable_beacon = true;
  199. sdata->vif.bss_conf.beacon_int = beacon_int;
  200. sdata->vif.bss_conf.basic_rates = basic_rates;
  201. sdata->vif.bss_conf.ssid_len = ifibss->ssid_len;
  202. memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len);
  203. bss_change = BSS_CHANGED_BEACON_INT;
  204. bss_change |= ieee80211_reset_erp_info(sdata);
  205. bss_change |= BSS_CHANGED_BSSID;
  206. bss_change |= BSS_CHANGED_BEACON;
  207. bss_change |= BSS_CHANGED_BEACON_ENABLED;
  208. bss_change |= BSS_CHANGED_BASIC_RATES;
  209. bss_change |= BSS_CHANGED_HT;
  210. bss_change |= BSS_CHANGED_IBSS;
  211. bss_change |= BSS_CHANGED_SSID;
  212. /*
  213. * In 5 GHz/802.11a, we can always use short slot time.
  214. * (IEEE 802.11-2012 18.3.8.7)
  215. *
  216. * In 2.4GHz, we must always use long slots in IBSS for compatibility
  217. * reasons.
  218. * (IEEE 802.11-2012 19.4.5)
  219. *
  220. * HT follows these specifications (IEEE 802.11-2012 20.3.18)
  221. */
  222. sdata->vif.bss_conf.use_short_slot = chan->band == IEEE80211_BAND_5GHZ;
  223. bss_change |= BSS_CHANGED_ERP_SLOT;
  224. sdata->vif.bss_conf.ibss_joined = true;
  225. sdata->vif.bss_conf.ibss_creator = creator;
  226. ieee80211_bss_info_change_notify(sdata, bss_change);
  227. ieee80211_sta_def_wmm_params(sdata, rates, supp_rates);
  228. ifibss->state = IEEE80211_IBSS_MLME_JOINED;
  229. mod_timer(&ifibss->timer,
  230. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  231. bss = cfg80211_inform_bss_frame(local->hw.wiphy, chan,
  232. mgmt, presp->head_len, 0, GFP_KERNEL);
  233. cfg80211_put_bss(local->hw.wiphy, bss);
  234. netif_carrier_on(sdata->dev);
  235. cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
  236. }
  237. static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  238. struct ieee80211_bss *bss)
  239. {
  240. struct cfg80211_bss *cbss =
  241. container_of((void *)bss, struct cfg80211_bss, priv);
  242. struct ieee80211_supported_band *sband;
  243. u32 basic_rates;
  244. int i, j;
  245. u16 beacon_int = cbss->beacon_interval;
  246. const struct cfg80211_bss_ies *ies;
  247. u64 tsf;
  248. u32 rate_flags;
  249. int shift;
  250. sdata_assert_lock(sdata);
  251. if (beacon_int < 10)
  252. beacon_int = 10;
  253. sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
  254. rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
  255. shift = ieee80211_vif_get_shift(&sdata->vif);
  256. basic_rates = 0;
  257. for (i = 0; i < bss->supp_rates_len; i++) {
  258. int rate = bss->supp_rates[i] & 0x7f;
  259. bool is_basic = !!(bss->supp_rates[i] & 0x80);
  260. for (j = 0; j < sband->n_bitrates; j++) {
  261. int brate;
  262. if ((rate_flags & sband->bitrates[j].flags)
  263. != rate_flags)
  264. continue;
  265. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  266. 5 * (1 << shift));
  267. if (brate == rate) {
  268. if (is_basic)
  269. basic_rates |= BIT(j);
  270. break;
  271. }
  272. }
  273. }
  274. rcu_read_lock();
  275. ies = rcu_dereference(cbss->ies);
  276. tsf = ies->tsf;
  277. rcu_read_unlock();
  278. __ieee80211_sta_join_ibss(sdata, cbss->bssid,
  279. beacon_int,
  280. cbss->channel,
  281. basic_rates,
  282. cbss->capability,
  283. tsf, false);
  284. }
  285. static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
  286. __acquires(RCU)
  287. {
  288. struct ieee80211_sub_if_data *sdata = sta->sdata;
  289. u8 addr[ETH_ALEN];
  290. memcpy(addr, sta->sta.addr, ETH_ALEN);
  291. ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
  292. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  293. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  294. /* authorize the station only if the network is not RSN protected. If
  295. * not wait for the userspace to authorize it */
  296. if (!sta->sdata->u.ibss.control_port)
  297. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  298. rate_control_rate_init(sta);
  299. /* If it fails, maybe we raced another insertion? */
  300. if (sta_info_insert_rcu(sta))
  301. return sta_info_get(sdata, addr);
  302. return sta;
  303. }
  304. static struct sta_info *
  305. ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
  306. const u8 *addr, u32 supp_rates)
  307. __acquires(RCU)
  308. {
  309. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  310. struct ieee80211_local *local = sdata->local;
  311. struct sta_info *sta;
  312. struct ieee80211_chanctx_conf *chanctx_conf;
  313. struct ieee80211_supported_band *sband;
  314. enum nl80211_bss_scan_width scan_width;
  315. int band;
  316. /*
  317. * XXX: Consider removing the least recently used entry and
  318. * allow new one to be added.
  319. */
  320. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  321. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  322. sdata->name, addr);
  323. rcu_read_lock();
  324. return NULL;
  325. }
  326. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
  327. rcu_read_lock();
  328. return NULL;
  329. }
  330. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
  331. rcu_read_lock();
  332. return NULL;
  333. }
  334. rcu_read_lock();
  335. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  336. if (WARN_ON_ONCE(!chanctx_conf))
  337. return NULL;
  338. band = chanctx_conf->def.chan->band;
  339. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  340. rcu_read_unlock();
  341. sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
  342. if (!sta) {
  343. rcu_read_lock();
  344. return NULL;
  345. }
  346. sta->last_rx = jiffies;
  347. /* make sure mandatory rates are always added */
  348. sband = local->hw.wiphy->bands[band];
  349. sta->sta.supp_rates[band] = supp_rates |
  350. ieee80211_mandatory_rates(sband, scan_width);
  351. return ieee80211_ibss_finish_sta(sta);
  352. }
  353. static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
  354. struct ieee80211_mgmt *mgmt,
  355. size_t len)
  356. {
  357. u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
  358. if (len < IEEE80211_DEAUTH_FRAME_LEN)
  359. return;
  360. ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM BSSID=%pM (reason: %d)\n",
  361. mgmt->sa, mgmt->da, mgmt->bssid, reason);
  362. sta_info_destroy_addr(sdata, mgmt->sa);
  363. }
  364. static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
  365. struct ieee80211_mgmt *mgmt,
  366. size_t len)
  367. {
  368. u16 auth_alg, auth_transaction;
  369. sdata_assert_lock(sdata);
  370. if (len < 24 + 6)
  371. return;
  372. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  373. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  374. ibss_dbg(sdata,
  375. "RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
  376. mgmt->sa, mgmt->da, mgmt->bssid, auth_transaction);
  377. if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
  378. return;
  379. /*
  380. * IEEE 802.11 standard does not require authentication in IBSS
  381. * networks and most implementations do not seem to use it.
  382. * However, try to reply to authentication attempts if someone
  383. * has actually implemented this.
  384. */
  385. ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
  386. mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
  387. }
  388. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  389. struct ieee80211_mgmt *mgmt, size_t len,
  390. struct ieee80211_rx_status *rx_status,
  391. struct ieee802_11_elems *elems)
  392. {
  393. struct ieee80211_local *local = sdata->local;
  394. int freq;
  395. struct cfg80211_bss *cbss;
  396. struct ieee80211_bss *bss;
  397. struct sta_info *sta;
  398. struct ieee80211_channel *channel;
  399. u64 beacon_timestamp, rx_timestamp;
  400. u32 supp_rates = 0;
  401. enum ieee80211_band band = rx_status->band;
  402. enum nl80211_bss_scan_width scan_width;
  403. struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
  404. bool rates_updated = false;
  405. if (elems->ds_params)
  406. freq = ieee80211_channel_to_frequency(elems->ds_params[0],
  407. band);
  408. else
  409. freq = rx_status->freq;
  410. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  411. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  412. return;
  413. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  414. ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid)) {
  415. rcu_read_lock();
  416. sta = sta_info_get(sdata, mgmt->sa);
  417. if (elems->supp_rates) {
  418. supp_rates = ieee80211_sta_get_rates(sdata, elems,
  419. band, NULL);
  420. if (sta) {
  421. u32 prev_rates;
  422. prev_rates = sta->sta.supp_rates[band];
  423. /* make sure mandatory rates are always added */
  424. scan_width = NL80211_BSS_CHAN_WIDTH_20;
  425. if (rx_status->flag & RX_FLAG_5MHZ)
  426. scan_width = NL80211_BSS_CHAN_WIDTH_5;
  427. if (rx_status->flag & RX_FLAG_10MHZ)
  428. scan_width = NL80211_BSS_CHAN_WIDTH_10;
  429. sta->sta.supp_rates[band] = supp_rates |
  430. ieee80211_mandatory_rates(sband,
  431. scan_width);
  432. if (sta->sta.supp_rates[band] != prev_rates) {
  433. ibss_dbg(sdata,
  434. "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
  435. sta->sta.addr, prev_rates,
  436. sta->sta.supp_rates[band]);
  437. rates_updated = true;
  438. }
  439. } else {
  440. rcu_read_unlock();
  441. sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
  442. mgmt->sa, supp_rates);
  443. }
  444. }
  445. if (sta && elems->wmm_info)
  446. set_sta_flag(sta, WLAN_STA_WME);
  447. if (sta && elems->ht_operation && elems->ht_cap_elem &&
  448. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
  449. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
  450. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
  451. /* we both use HT */
  452. struct ieee80211_ht_cap htcap_ie;
  453. struct cfg80211_chan_def chandef;
  454. ieee80211_ht_oper_to_chandef(channel,
  455. elems->ht_operation,
  456. &chandef);
  457. memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
  458. /*
  459. * fall back to HT20 if we don't use or use
  460. * the other extension channel
  461. */
  462. if (chandef.center_freq1 !=
  463. sdata->u.ibss.chandef.center_freq1)
  464. htcap_ie.cap_info &=
  465. cpu_to_le16(~IEEE80211_HT_CAP_SUP_WIDTH_20_40);
  466. rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(
  467. sdata, sband, &htcap_ie, sta);
  468. }
  469. if (sta && rates_updated) {
  470. drv_sta_rc_update(local, sdata, &sta->sta,
  471. IEEE80211_RC_SUPP_RATES_CHANGED);
  472. rate_control_rate_init(sta);
  473. }
  474. rcu_read_unlock();
  475. }
  476. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  477. channel);
  478. if (!bss)
  479. return;
  480. cbss = container_of((void *)bss, struct cfg80211_bss, priv);
  481. /* same for beacon and probe response */
  482. beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
  483. /* check if we need to merge IBSS */
  484. /* we use a fixed BSSID */
  485. if (sdata->u.ibss.fixed_bssid)
  486. goto put_bss;
  487. /* not an IBSS */
  488. if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
  489. goto put_bss;
  490. /* different channel */
  491. if (sdata->u.ibss.fixed_channel &&
  492. sdata->u.ibss.chandef.chan != cbss->channel)
  493. goto put_bss;
  494. /* different SSID */
  495. if (elems->ssid_len != sdata->u.ibss.ssid_len ||
  496. memcmp(elems->ssid, sdata->u.ibss.ssid,
  497. sdata->u.ibss.ssid_len))
  498. goto put_bss;
  499. /* same BSSID */
  500. if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
  501. goto put_bss;
  502. if (ieee80211_have_rx_timestamp(rx_status)) {
  503. /* time when timestamp field was received */
  504. rx_timestamp =
  505. ieee80211_calculate_rx_timestamp(local, rx_status,
  506. len + FCS_LEN, 24);
  507. } else {
  508. /*
  509. * second best option: get current TSF
  510. * (will return -1 if not supported)
  511. */
  512. rx_timestamp = drv_get_tsf(local, sdata);
  513. }
  514. ibss_dbg(sdata,
  515. "RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  516. mgmt->sa, mgmt->bssid,
  517. (unsigned long long)rx_timestamp,
  518. (unsigned long long)beacon_timestamp,
  519. (unsigned long long)(rx_timestamp - beacon_timestamp),
  520. jiffies);
  521. if (beacon_timestamp > rx_timestamp) {
  522. ibss_dbg(sdata,
  523. "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
  524. mgmt->bssid);
  525. ieee80211_sta_join_ibss(sdata, bss);
  526. supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
  527. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
  528. supp_rates);
  529. rcu_read_unlock();
  530. }
  531. put_bss:
  532. ieee80211_rx_bss_put(local, bss);
  533. }
  534. void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
  535. const u8 *bssid, const u8 *addr,
  536. u32 supp_rates)
  537. {
  538. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  539. struct ieee80211_local *local = sdata->local;
  540. struct sta_info *sta;
  541. struct ieee80211_chanctx_conf *chanctx_conf;
  542. struct ieee80211_supported_band *sband;
  543. enum nl80211_bss_scan_width scan_width;
  544. int band;
  545. /*
  546. * XXX: Consider removing the least recently used entry and
  547. * allow new one to be added.
  548. */
  549. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  550. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  551. sdata->name, addr);
  552. return;
  553. }
  554. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
  555. return;
  556. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
  557. return;
  558. rcu_read_lock();
  559. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  560. if (WARN_ON_ONCE(!chanctx_conf)) {
  561. rcu_read_unlock();
  562. return;
  563. }
  564. band = chanctx_conf->def.chan->band;
  565. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  566. rcu_read_unlock();
  567. sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
  568. if (!sta)
  569. return;
  570. sta->last_rx = jiffies;
  571. /* make sure mandatory rates are always added */
  572. sband = local->hw.wiphy->bands[band];
  573. sta->sta.supp_rates[band] = supp_rates |
  574. ieee80211_mandatory_rates(sband, scan_width);
  575. spin_lock(&ifibss->incomplete_lock);
  576. list_add(&sta->list, &ifibss->incomplete_stations);
  577. spin_unlock(&ifibss->incomplete_lock);
  578. ieee80211_queue_work(&local->hw, &sdata->work);
  579. }
  580. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  581. {
  582. struct ieee80211_local *local = sdata->local;
  583. int active = 0;
  584. struct sta_info *sta;
  585. sdata_assert_lock(sdata);
  586. rcu_read_lock();
  587. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  588. if (sta->sdata == sdata &&
  589. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  590. jiffies)) {
  591. active++;
  592. break;
  593. }
  594. }
  595. rcu_read_unlock();
  596. return active;
  597. }
  598. /*
  599. * This function is called with state == IEEE80211_IBSS_MLME_JOINED
  600. */
  601. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
  602. {
  603. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  604. sdata_assert_lock(sdata);
  605. mod_timer(&ifibss->timer,
  606. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  607. ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
  608. if (time_before(jiffies, ifibss->last_scan_completed +
  609. IEEE80211_IBSS_MERGE_INTERVAL))
  610. return;
  611. if (ieee80211_sta_active_ibss(sdata))
  612. return;
  613. if (ifibss->fixed_channel)
  614. return;
  615. sdata_info(sdata,
  616. "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
  617. ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
  618. NULL);
  619. }
  620. static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
  621. {
  622. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  623. u8 bssid[ETH_ALEN];
  624. u16 capability;
  625. int i;
  626. sdata_assert_lock(sdata);
  627. if (ifibss->fixed_bssid) {
  628. memcpy(bssid, ifibss->bssid, ETH_ALEN);
  629. } else {
  630. /* Generate random, not broadcast, locally administered BSSID. Mix in
  631. * own MAC address to make sure that devices that do not have proper
  632. * random number generator get different BSSID. */
  633. get_random_bytes(bssid, ETH_ALEN);
  634. for (i = 0; i < ETH_ALEN; i++)
  635. bssid[i] ^= sdata->vif.addr[i];
  636. bssid[0] &= ~0x01;
  637. bssid[0] |= 0x02;
  638. }
  639. sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
  640. capability = WLAN_CAPABILITY_IBSS;
  641. if (ifibss->privacy)
  642. capability |= WLAN_CAPABILITY_PRIVACY;
  643. else
  644. sdata->drop_unencrypted = 0;
  645. __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
  646. ifibss->chandef.chan, ifibss->basic_rates,
  647. capability, 0, true);
  648. }
  649. /*
  650. * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
  651. */
  652. static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
  653. {
  654. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  655. struct ieee80211_local *local = sdata->local;
  656. struct cfg80211_bss *cbss;
  657. struct ieee80211_channel *chan = NULL;
  658. const u8 *bssid = NULL;
  659. int active_ibss;
  660. u16 capability;
  661. sdata_assert_lock(sdata);
  662. active_ibss = ieee80211_sta_active_ibss(sdata);
  663. ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
  664. if (active_ibss)
  665. return;
  666. capability = WLAN_CAPABILITY_IBSS;
  667. if (ifibss->privacy)
  668. capability |= WLAN_CAPABILITY_PRIVACY;
  669. if (ifibss->fixed_bssid)
  670. bssid = ifibss->bssid;
  671. if (ifibss->fixed_channel)
  672. chan = ifibss->chandef.chan;
  673. if (!is_zero_ether_addr(ifibss->bssid))
  674. bssid = ifibss->bssid;
  675. cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
  676. ifibss->ssid, ifibss->ssid_len,
  677. WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
  678. capability);
  679. if (cbss) {
  680. struct ieee80211_bss *bss;
  681. bss = (void *)cbss->priv;
  682. ibss_dbg(sdata,
  683. "sta_find_ibss: selected %pM current %pM\n",
  684. cbss->bssid, ifibss->bssid);
  685. sdata_info(sdata,
  686. "Selected IBSS BSSID %pM based on configured SSID\n",
  687. cbss->bssid);
  688. ieee80211_sta_join_ibss(sdata, bss);
  689. ieee80211_rx_bss_put(local, bss);
  690. return;
  691. }
  692. ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
  693. /* Selected IBSS not found in current scan results - try to scan */
  694. if (time_after(jiffies, ifibss->last_scan_completed +
  695. IEEE80211_SCAN_INTERVAL)) {
  696. sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
  697. ieee80211_request_ibss_scan(sdata, ifibss->ssid,
  698. ifibss->ssid_len, chan);
  699. } else {
  700. int interval = IEEE80211_SCAN_INTERVAL;
  701. if (time_after(jiffies, ifibss->ibss_join_req +
  702. IEEE80211_IBSS_JOIN_TIMEOUT))
  703. ieee80211_sta_create_ibss(sdata);
  704. mod_timer(&ifibss->timer,
  705. round_jiffies(jiffies + interval));
  706. }
  707. }
  708. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  709. struct sk_buff *req)
  710. {
  711. struct ieee80211_mgmt *mgmt = (void *)req->data;
  712. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  713. struct ieee80211_local *local = sdata->local;
  714. int tx_last_beacon, len = req->len;
  715. struct sk_buff *skb;
  716. struct beacon_data *presp;
  717. u8 *pos, *end;
  718. sdata_assert_lock(sdata);
  719. presp = rcu_dereference_protected(ifibss->presp,
  720. lockdep_is_held(&sdata->wdev.mtx));
  721. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  722. len < 24 + 2 || !presp)
  723. return;
  724. tx_last_beacon = drv_tx_last_beacon(local);
  725. ibss_dbg(sdata,
  726. "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
  727. mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
  728. if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
  729. return;
  730. if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
  731. !is_broadcast_ether_addr(mgmt->bssid))
  732. return;
  733. end = ((u8 *) mgmt) + len;
  734. pos = mgmt->u.probe_req.variable;
  735. if (pos[0] != WLAN_EID_SSID ||
  736. pos + 2 + pos[1] > end) {
  737. ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
  738. mgmt->sa);
  739. return;
  740. }
  741. if (pos[1] != 0 &&
  742. (pos[1] != ifibss->ssid_len ||
  743. memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
  744. /* Ignore ProbeReq for foreign SSID */
  745. return;
  746. }
  747. /* Reply with ProbeResp */
  748. skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
  749. if (!skb)
  750. return;
  751. skb_reserve(skb, local->tx_headroom);
  752. memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
  753. memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
  754. ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
  755. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  756. ieee80211_tx_skb(sdata, skb);
  757. }
  758. static
  759. void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
  760. struct ieee80211_mgmt *mgmt, size_t len,
  761. struct ieee80211_rx_status *rx_status)
  762. {
  763. size_t baselen;
  764. struct ieee802_11_elems elems;
  765. BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
  766. offsetof(typeof(mgmt->u.beacon), variable));
  767. /*
  768. * either beacon or probe_resp but the variable field is at the
  769. * same offset
  770. */
  771. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  772. if (baselen > len)
  773. return;
  774. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  775. false, &elems);
  776. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  777. }
  778. void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  779. struct sk_buff *skb)
  780. {
  781. struct ieee80211_rx_status *rx_status;
  782. struct ieee80211_mgmt *mgmt;
  783. u16 fc;
  784. rx_status = IEEE80211_SKB_RXCB(skb);
  785. mgmt = (struct ieee80211_mgmt *) skb->data;
  786. fc = le16_to_cpu(mgmt->frame_control);
  787. sdata_lock(sdata);
  788. if (!sdata->u.ibss.ssid_len)
  789. goto mgmt_out; /* not ready to merge yet */
  790. switch (fc & IEEE80211_FCTL_STYPE) {
  791. case IEEE80211_STYPE_PROBE_REQ:
  792. ieee80211_rx_mgmt_probe_req(sdata, skb);
  793. break;
  794. case IEEE80211_STYPE_PROBE_RESP:
  795. case IEEE80211_STYPE_BEACON:
  796. ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
  797. rx_status);
  798. break;
  799. case IEEE80211_STYPE_AUTH:
  800. ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
  801. break;
  802. case IEEE80211_STYPE_DEAUTH:
  803. ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
  804. break;
  805. }
  806. mgmt_out:
  807. sdata_unlock(sdata);
  808. }
  809. void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
  810. {
  811. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  812. struct sta_info *sta;
  813. sdata_lock(sdata);
  814. /*
  815. * Work could be scheduled after scan or similar
  816. * when we aren't even joined (or trying) with a
  817. * network.
  818. */
  819. if (!ifibss->ssid_len)
  820. goto out;
  821. spin_lock_bh(&ifibss->incomplete_lock);
  822. while (!list_empty(&ifibss->incomplete_stations)) {
  823. sta = list_first_entry(&ifibss->incomplete_stations,
  824. struct sta_info, list);
  825. list_del(&sta->list);
  826. spin_unlock_bh(&ifibss->incomplete_lock);
  827. ieee80211_ibss_finish_sta(sta);
  828. rcu_read_unlock();
  829. spin_lock_bh(&ifibss->incomplete_lock);
  830. }
  831. spin_unlock_bh(&ifibss->incomplete_lock);
  832. switch (ifibss->state) {
  833. case IEEE80211_IBSS_MLME_SEARCH:
  834. ieee80211_sta_find_ibss(sdata);
  835. break;
  836. case IEEE80211_IBSS_MLME_JOINED:
  837. ieee80211_sta_merge_ibss(sdata);
  838. break;
  839. default:
  840. WARN_ON(1);
  841. break;
  842. }
  843. out:
  844. sdata_unlock(sdata);
  845. }
  846. static void ieee80211_ibss_timer(unsigned long data)
  847. {
  848. struct ieee80211_sub_if_data *sdata =
  849. (struct ieee80211_sub_if_data *) data;
  850. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  851. }
  852. void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
  853. {
  854. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  855. setup_timer(&ifibss->timer, ieee80211_ibss_timer,
  856. (unsigned long) sdata);
  857. INIT_LIST_HEAD(&ifibss->incomplete_stations);
  858. spin_lock_init(&ifibss->incomplete_lock);
  859. }
  860. /* scan finished notification */
  861. void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
  862. {
  863. struct ieee80211_sub_if_data *sdata;
  864. mutex_lock(&local->iflist_mtx);
  865. list_for_each_entry(sdata, &local->interfaces, list) {
  866. if (!ieee80211_sdata_running(sdata))
  867. continue;
  868. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  869. continue;
  870. sdata->u.ibss.last_scan_completed = jiffies;
  871. ieee80211_queue_work(&local->hw, &sdata->work);
  872. }
  873. mutex_unlock(&local->iflist_mtx);
  874. }
  875. int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
  876. struct cfg80211_ibss_params *params)
  877. {
  878. u32 changed = 0;
  879. u32 rate_flags;
  880. struct ieee80211_supported_band *sband;
  881. int i;
  882. if (params->bssid) {
  883. memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
  884. sdata->u.ibss.fixed_bssid = true;
  885. } else
  886. sdata->u.ibss.fixed_bssid = false;
  887. sdata->u.ibss.privacy = params->privacy;
  888. sdata->u.ibss.control_port = params->control_port;
  889. sdata->u.ibss.basic_rates = params->basic_rates;
  890. /* fix basic_rates if channel does not support these rates */
  891. rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
  892. sband = sdata->local->hw.wiphy->bands[params->chandef.chan->band];
  893. for (i = 0; i < sband->n_bitrates; i++) {
  894. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  895. sdata->u.ibss.basic_rates &= ~BIT(i);
  896. }
  897. memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
  898. sizeof(params->mcast_rate));
  899. sdata->vif.bss_conf.beacon_int = params->beacon_interval;
  900. sdata->u.ibss.chandef = params->chandef;
  901. sdata->u.ibss.fixed_channel = params->channel_fixed;
  902. if (params->ie) {
  903. sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
  904. GFP_KERNEL);
  905. if (sdata->u.ibss.ie)
  906. sdata->u.ibss.ie_len = params->ie_len;
  907. }
  908. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  909. sdata->u.ibss.ibss_join_req = jiffies;
  910. memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
  911. sdata->u.ibss.ssid_len = params->ssid_len;
  912. memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
  913. sizeof(sdata->u.ibss.ht_capa));
  914. memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
  915. sizeof(sdata->u.ibss.ht_capa_mask));
  916. /*
  917. * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
  918. * reserved, but an HT STA shall protect HT transmissions as though
  919. * the HT Protection field were set to non-HT mixed mode.
  920. *
  921. * In an IBSS, the RIFS Mode field of the HT Operation element is
  922. * also reserved, but an HT STA shall operate as though this field
  923. * were set to 1.
  924. */
  925. sdata->vif.bss_conf.ht_operation_mode |=
  926. IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
  927. | IEEE80211_HT_PARAM_RIFS_MODE;
  928. changed |= BSS_CHANGED_HT;
  929. ieee80211_bss_info_change_notify(sdata, changed);
  930. sdata->smps_mode = IEEE80211_SMPS_OFF;
  931. sdata->needed_rx_chains = sdata->local->rx_chains;
  932. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  933. return 0;
  934. }
  935. int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
  936. {
  937. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  938. struct ieee80211_local *local = sdata->local;
  939. struct cfg80211_bss *cbss;
  940. u16 capability;
  941. int active_ibss;
  942. struct sta_info *sta;
  943. struct beacon_data *presp;
  944. active_ibss = ieee80211_sta_active_ibss(sdata);
  945. if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
  946. capability = WLAN_CAPABILITY_IBSS;
  947. if (ifibss->privacy)
  948. capability |= WLAN_CAPABILITY_PRIVACY;
  949. cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
  950. ifibss->bssid, ifibss->ssid,
  951. ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
  952. WLAN_CAPABILITY_PRIVACY,
  953. capability);
  954. if (cbss) {
  955. cfg80211_unlink_bss(local->hw.wiphy, cbss);
  956. cfg80211_put_bss(local->hw.wiphy, cbss);
  957. }
  958. }
  959. ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
  960. memset(ifibss->bssid, 0, ETH_ALEN);
  961. ifibss->ssid_len = 0;
  962. sta_info_flush(sdata);
  963. spin_lock_bh(&ifibss->incomplete_lock);
  964. while (!list_empty(&ifibss->incomplete_stations)) {
  965. sta = list_first_entry(&ifibss->incomplete_stations,
  966. struct sta_info, list);
  967. list_del(&sta->list);
  968. spin_unlock_bh(&ifibss->incomplete_lock);
  969. sta_info_free(local, sta);
  970. spin_lock_bh(&ifibss->incomplete_lock);
  971. }
  972. spin_unlock_bh(&ifibss->incomplete_lock);
  973. netif_carrier_off(sdata->dev);
  974. /* remove beacon */
  975. kfree(sdata->u.ibss.ie);
  976. presp = rcu_dereference_protected(ifibss->presp,
  977. lockdep_is_held(&sdata->wdev.mtx));
  978. RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
  979. /* on the next join, re-program HT parameters */
  980. memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
  981. memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
  982. sdata->vif.bss_conf.ibss_joined = false;
  983. sdata->vif.bss_conf.ibss_creator = false;
  984. sdata->vif.bss_conf.enable_beacon = false;
  985. sdata->vif.bss_conf.ssid_len = 0;
  986. clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
  987. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
  988. BSS_CHANGED_IBSS);
  989. synchronize_rcu();
  990. kfree(presp);
  991. skb_queue_purge(&sdata->skb_queue);
  992. del_timer_sync(&sdata->u.ibss.timer);
  993. return 0;
  994. }