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