ibss.c 33 KB

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