ibss.c 34 KB

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