ibss.c 33 KB

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