ibss.c 36 KB

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