ibss.c 33 KB

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