ibss.c 33 KB

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