ibss.c 33 KB

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