ibss.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  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. struct cfg80211_csa_settings *csa_settings)
  38. {
  39. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  40. struct ieee80211_local *local = sdata->local;
  41. int rates_n = 0, i, ri;
  42. struct ieee80211_mgmt *mgmt;
  43. u8 *pos;
  44. struct ieee80211_supported_band *sband;
  45. u32 rate_flags, rates = 0, rates_added = 0;
  46. struct beacon_data *presp;
  47. int frame_len;
  48. int shift;
  49. /* Build IBSS probe response */
  50. frame_len = sizeof(struct ieee80211_hdr_3addr) +
  51. 12 /* struct ieee80211_mgmt.u.beacon */ +
  52. 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
  53. 2 + 8 /* max Supported Rates */ +
  54. 3 /* max DS params */ +
  55. 4 /* IBSS params */ +
  56. 5 /* Channel Switch Announcement */ +
  57. 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  58. 2 + sizeof(struct ieee80211_ht_cap) +
  59. 2 + sizeof(struct ieee80211_ht_operation) +
  60. ifibss->ie_len;
  61. presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
  62. if (!presp)
  63. return NULL;
  64. presp->head = (void *)(presp + 1);
  65. mgmt = (void *) presp->head;
  66. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  67. IEEE80211_STYPE_PROBE_RESP);
  68. eth_broadcast_addr(mgmt->da);
  69. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  70. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  71. mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
  72. mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
  73. mgmt->u.beacon.capab_info = cpu_to_le16(capability);
  74. pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
  75. *pos++ = WLAN_EID_SSID;
  76. *pos++ = ifibss->ssid_len;
  77. memcpy(pos, ifibss->ssid, ifibss->ssid_len);
  78. pos += ifibss->ssid_len;
  79. sband = local->hw.wiphy->bands[chandef->chan->band];
  80. rate_flags = ieee80211_chandef_rate_flags(chandef);
  81. shift = ieee80211_chandef_get_shift(chandef);
  82. rates_n = 0;
  83. if (have_higher_than_11mbit)
  84. *have_higher_than_11mbit = false;
  85. for (i = 0; i < sband->n_bitrates; i++) {
  86. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  87. continue;
  88. if (sband->bitrates[i].bitrate > 110 &&
  89. have_higher_than_11mbit)
  90. *have_higher_than_11mbit = true;
  91. rates |= BIT(i);
  92. rates_n++;
  93. }
  94. *pos++ = WLAN_EID_SUPP_RATES;
  95. *pos++ = min_t(int, 8, rates_n);
  96. for (ri = 0; ri < sband->n_bitrates; ri++) {
  97. int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
  98. 5 * (1 << shift));
  99. u8 basic = 0;
  100. if (!(rates & BIT(ri)))
  101. continue;
  102. if (basic_rates & BIT(ri))
  103. basic = 0x80;
  104. *pos++ = basic | (u8) rate;
  105. if (++rates_added == 8) {
  106. ri++; /* continue at next rate for EXT_SUPP_RATES */
  107. break;
  108. }
  109. }
  110. if (sband->band == IEEE80211_BAND_2GHZ) {
  111. *pos++ = WLAN_EID_DS_PARAMS;
  112. *pos++ = 1;
  113. *pos++ = ieee80211_frequency_to_channel(
  114. chandef->chan->center_freq);
  115. }
  116. *pos++ = WLAN_EID_IBSS_PARAMS;
  117. *pos++ = 2;
  118. /* FIX: set ATIM window based on scan results */
  119. *pos++ = 0;
  120. *pos++ = 0;
  121. if (csa_settings) {
  122. *pos++ = WLAN_EID_CHANNEL_SWITCH;
  123. *pos++ = 3;
  124. *pos++ = csa_settings->block_tx ? 1 : 0;
  125. *pos++ = ieee80211_frequency_to_channel(
  126. csa_settings->chandef.chan->center_freq);
  127. presp->csa_counter_offsets[0] = (pos - presp->head);
  128. *pos++ = csa_settings->count;
  129. }
  130. /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
  131. if (rates_n > 8) {
  132. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  133. *pos++ = rates_n - 8;
  134. for (; ri < sband->n_bitrates; ri++) {
  135. int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
  136. 5 * (1 << shift));
  137. u8 basic = 0;
  138. if (!(rates & BIT(ri)))
  139. continue;
  140. if (basic_rates & BIT(ri))
  141. basic = 0x80;
  142. *pos++ = basic | (u8) rate;
  143. }
  144. }
  145. if (ifibss->ie_len) {
  146. memcpy(pos, ifibss->ie, ifibss->ie_len);
  147. pos += ifibss->ie_len;
  148. }
  149. /* add HT capability and information IEs */
  150. if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  151. chandef->width != NL80211_CHAN_WIDTH_5 &&
  152. chandef->width != NL80211_CHAN_WIDTH_10 &&
  153. sband->ht_cap.ht_supported) {
  154. struct ieee80211_sta_ht_cap ht_cap;
  155. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  156. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  157. pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  158. /*
  159. * Note: According to 802.11n-2009 9.13.3.1, HT Protection
  160. * field and RIFS Mode are reserved in IBSS mode, therefore
  161. * keep them at 0
  162. */
  163. pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
  164. chandef, 0);
  165. }
  166. if (local->hw.queues >= IEEE80211_NUM_ACS)
  167. pos = ieee80211_add_wmm_info_ie(pos, 0); /* U-APSD not in use */
  168. presp->head_len = pos - presp->head;
  169. if (WARN_ON(presp->head_len > frame_len))
  170. goto error;
  171. return presp;
  172. error:
  173. kfree(presp);
  174. return NULL;
  175. }
  176. static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  177. const u8 *bssid, const int beacon_int,
  178. struct cfg80211_chan_def *req_chandef,
  179. const u32 basic_rates,
  180. const u16 capability, u64 tsf,
  181. bool creator)
  182. {
  183. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  184. struct ieee80211_local *local = sdata->local;
  185. struct ieee80211_mgmt *mgmt;
  186. struct cfg80211_bss *bss;
  187. u32 bss_change;
  188. struct cfg80211_chan_def chandef;
  189. struct ieee80211_channel *chan;
  190. struct beacon_data *presp;
  191. enum nl80211_bss_scan_width scan_width;
  192. bool have_higher_than_11mbit;
  193. bool radar_required;
  194. int err;
  195. sdata_assert_lock(sdata);
  196. /* Reset own TSF to allow time synchronization work. */
  197. drv_reset_tsf(local, sdata);
  198. if (!ether_addr_equal(ifibss->bssid, bssid))
  199. sta_info_flush(sdata);
  200. /* if merging, indicate to driver that we leave the old IBSS */
  201. if (sdata->vif.bss_conf.ibss_joined) {
  202. sdata->vif.bss_conf.ibss_joined = false;
  203. sdata->vif.bss_conf.ibss_creator = false;
  204. sdata->vif.bss_conf.enable_beacon = false;
  205. netif_carrier_off(sdata->dev);
  206. ieee80211_bss_info_change_notify(sdata,
  207. BSS_CHANGED_IBSS |
  208. BSS_CHANGED_BEACON_ENABLED);
  209. drv_leave_ibss(local, sdata);
  210. }
  211. presp = rcu_dereference_protected(ifibss->presp,
  212. lockdep_is_held(&sdata->wdev.mtx));
  213. RCU_INIT_POINTER(ifibss->presp, NULL);
  214. if (presp)
  215. kfree_rcu(presp, rcu_head);
  216. sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
  217. /* make a copy of the chandef, it could be modified below. */
  218. chandef = *req_chandef;
  219. chan = chandef.chan;
  220. if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
  221. NL80211_IFTYPE_ADHOC)) {
  222. if (chandef.width == NL80211_CHAN_WIDTH_5 ||
  223. chandef.width == NL80211_CHAN_WIDTH_10 ||
  224. chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
  225. chandef.width == NL80211_CHAN_WIDTH_20) {
  226. sdata_info(sdata,
  227. "Failed to join IBSS, beacons forbidden\n");
  228. return;
  229. }
  230. chandef.width = NL80211_CHAN_WIDTH_20;
  231. chandef.center_freq1 = chan->center_freq;
  232. /* check again for downgraded chandef */
  233. if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
  234. NL80211_IFTYPE_ADHOC)) {
  235. sdata_info(sdata,
  236. "Failed to join IBSS, beacons forbidden\n");
  237. return;
  238. }
  239. }
  240. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  241. &chandef, NL80211_IFTYPE_ADHOC);
  242. if (err < 0) {
  243. sdata_info(sdata,
  244. "Failed to join IBSS, invalid chandef\n");
  245. return;
  246. }
  247. if (err > 0 && !ifibss->userspace_handles_dfs) {
  248. sdata_info(sdata,
  249. "Failed to join IBSS, DFS channel without control program\n");
  250. return;
  251. }
  252. radar_required = err;
  253. mutex_lock(&local->mtx);
  254. if (ieee80211_vif_use_channel(sdata, &chandef,
  255. ifibss->fixed_channel ?
  256. IEEE80211_CHANCTX_SHARED :
  257. IEEE80211_CHANCTX_EXCLUSIVE)) {
  258. sdata_info(sdata, "Failed to join IBSS, no channel context\n");
  259. mutex_unlock(&local->mtx);
  260. return;
  261. }
  262. sdata->radar_required = radar_required;
  263. mutex_unlock(&local->mtx);
  264. memcpy(ifibss->bssid, bssid, ETH_ALEN);
  265. presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates,
  266. capability, tsf, &chandef,
  267. &have_higher_than_11mbit, NULL);
  268. if (!presp)
  269. return;
  270. rcu_assign_pointer(ifibss->presp, presp);
  271. mgmt = (void *)presp->head;
  272. sdata->vif.bss_conf.enable_beacon = true;
  273. sdata->vif.bss_conf.beacon_int = beacon_int;
  274. sdata->vif.bss_conf.basic_rates = basic_rates;
  275. sdata->vif.bss_conf.ssid_len = ifibss->ssid_len;
  276. memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len);
  277. bss_change = BSS_CHANGED_BEACON_INT;
  278. bss_change |= ieee80211_reset_erp_info(sdata);
  279. bss_change |= BSS_CHANGED_BSSID;
  280. bss_change |= BSS_CHANGED_BEACON;
  281. bss_change |= BSS_CHANGED_BEACON_ENABLED;
  282. bss_change |= BSS_CHANGED_BASIC_RATES;
  283. bss_change |= BSS_CHANGED_HT;
  284. bss_change |= BSS_CHANGED_IBSS;
  285. bss_change |= BSS_CHANGED_SSID;
  286. /*
  287. * In 5 GHz/802.11a, we can always use short slot time.
  288. * (IEEE 802.11-2012 18.3.8.7)
  289. *
  290. * In 2.4GHz, we must always use long slots in IBSS for compatibility
  291. * reasons.
  292. * (IEEE 802.11-2012 19.4.5)
  293. *
  294. * HT follows these specifications (IEEE 802.11-2012 20.3.18)
  295. */
  296. sdata->vif.bss_conf.use_short_slot = chan->band == IEEE80211_BAND_5GHZ;
  297. bss_change |= BSS_CHANGED_ERP_SLOT;
  298. /* cf. IEEE 802.11 9.2.12 */
  299. if (chan->band == IEEE80211_BAND_2GHZ && have_higher_than_11mbit)
  300. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  301. else
  302. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  303. ieee80211_set_wmm_default(sdata, true);
  304. sdata->vif.bss_conf.ibss_joined = true;
  305. sdata->vif.bss_conf.ibss_creator = creator;
  306. err = drv_join_ibss(local, sdata);
  307. if (err) {
  308. sdata->vif.bss_conf.ibss_joined = false;
  309. sdata->vif.bss_conf.ibss_creator = false;
  310. sdata->vif.bss_conf.enable_beacon = false;
  311. sdata->vif.bss_conf.ssid_len = 0;
  312. RCU_INIT_POINTER(ifibss->presp, NULL);
  313. kfree_rcu(presp, rcu_head);
  314. mutex_lock(&local->mtx);
  315. ieee80211_vif_release_channel(sdata);
  316. mutex_unlock(&local->mtx);
  317. sdata_info(sdata, "Failed to join IBSS, driver failure: %d\n",
  318. err);
  319. return;
  320. }
  321. ieee80211_bss_info_change_notify(sdata, bss_change);
  322. ifibss->state = IEEE80211_IBSS_MLME_JOINED;
  323. mod_timer(&ifibss->timer,
  324. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  325. scan_width = cfg80211_chandef_to_scan_width(&chandef);
  326. bss = cfg80211_inform_bss_width_frame(local->hw.wiphy, chan,
  327. scan_width, mgmt,
  328. presp->head_len, 0, GFP_KERNEL);
  329. cfg80211_put_bss(local->hw.wiphy, bss);
  330. netif_carrier_on(sdata->dev);
  331. cfg80211_ibss_joined(sdata->dev, ifibss->bssid, chan, GFP_KERNEL);
  332. }
  333. static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  334. struct ieee80211_bss *bss)
  335. {
  336. struct cfg80211_bss *cbss =
  337. container_of((void *)bss, struct cfg80211_bss, priv);
  338. struct ieee80211_supported_band *sband;
  339. struct cfg80211_chan_def chandef;
  340. u32 basic_rates;
  341. int i, j;
  342. u16 beacon_int = cbss->beacon_interval;
  343. const struct cfg80211_bss_ies *ies;
  344. enum nl80211_channel_type chan_type;
  345. u64 tsf;
  346. u32 rate_flags;
  347. int shift;
  348. sdata_assert_lock(sdata);
  349. if (beacon_int < 10)
  350. beacon_int = 10;
  351. switch (sdata->u.ibss.chandef.width) {
  352. case NL80211_CHAN_WIDTH_20_NOHT:
  353. case NL80211_CHAN_WIDTH_20:
  354. case NL80211_CHAN_WIDTH_40:
  355. chan_type = cfg80211_get_chandef_type(&sdata->u.ibss.chandef);
  356. cfg80211_chandef_create(&chandef, cbss->channel, chan_type);
  357. break;
  358. case NL80211_CHAN_WIDTH_5:
  359. case NL80211_CHAN_WIDTH_10:
  360. cfg80211_chandef_create(&chandef, cbss->channel,
  361. NL80211_CHAN_WIDTH_20_NOHT);
  362. chandef.width = sdata->u.ibss.chandef.width;
  363. break;
  364. default:
  365. /* fall back to 20 MHz for unsupported modes */
  366. cfg80211_chandef_create(&chandef, cbss->channel,
  367. NL80211_CHAN_WIDTH_20_NOHT);
  368. break;
  369. }
  370. sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
  371. rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
  372. shift = ieee80211_vif_get_shift(&sdata->vif);
  373. basic_rates = 0;
  374. for (i = 0; i < bss->supp_rates_len; i++) {
  375. int rate = bss->supp_rates[i] & 0x7f;
  376. bool is_basic = !!(bss->supp_rates[i] & 0x80);
  377. for (j = 0; j < sband->n_bitrates; j++) {
  378. int brate;
  379. if ((rate_flags & sband->bitrates[j].flags)
  380. != rate_flags)
  381. continue;
  382. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  383. 5 * (1 << shift));
  384. if (brate == rate) {
  385. if (is_basic)
  386. basic_rates |= BIT(j);
  387. break;
  388. }
  389. }
  390. }
  391. rcu_read_lock();
  392. ies = rcu_dereference(cbss->ies);
  393. tsf = ies->tsf;
  394. rcu_read_unlock();
  395. __ieee80211_sta_join_ibss(sdata, cbss->bssid,
  396. beacon_int,
  397. &chandef,
  398. basic_rates,
  399. cbss->capability,
  400. tsf, false);
  401. }
  402. int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
  403. struct cfg80211_csa_settings *csa_settings)
  404. {
  405. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  406. struct beacon_data *presp, *old_presp;
  407. struct cfg80211_bss *cbss;
  408. const struct cfg80211_bss_ies *ies;
  409. u16 capability;
  410. u64 tsf;
  411. int ret = 0;
  412. sdata_assert_lock(sdata);
  413. capability = WLAN_CAPABILITY_IBSS;
  414. if (ifibss->privacy)
  415. capability |= WLAN_CAPABILITY_PRIVACY;
  416. cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
  417. ifibss->bssid, ifibss->ssid,
  418. ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
  419. WLAN_CAPABILITY_PRIVACY,
  420. capability);
  421. if (WARN_ON(!cbss)) {
  422. ret = -EINVAL;
  423. goto out;
  424. }
  425. rcu_read_lock();
  426. ies = rcu_dereference(cbss->ies);
  427. tsf = ies->tsf;
  428. rcu_read_unlock();
  429. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  430. old_presp = rcu_dereference_protected(ifibss->presp,
  431. lockdep_is_held(&sdata->wdev.mtx));
  432. presp = ieee80211_ibss_build_presp(sdata,
  433. sdata->vif.bss_conf.beacon_int,
  434. sdata->vif.bss_conf.basic_rates,
  435. capability, tsf, &ifibss->chandef,
  436. NULL, csa_settings);
  437. if (!presp) {
  438. ret = -ENOMEM;
  439. goto out;
  440. }
  441. rcu_assign_pointer(ifibss->presp, presp);
  442. if (old_presp)
  443. kfree_rcu(old_presp, rcu_head);
  444. return BSS_CHANGED_BEACON;
  445. out:
  446. return ret;
  447. }
  448. int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata)
  449. {
  450. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  451. struct cfg80211_bss *cbss;
  452. int err, changed = 0;
  453. u16 capability;
  454. sdata_assert_lock(sdata);
  455. /* update cfg80211 bss information with the new channel */
  456. if (!is_zero_ether_addr(ifibss->bssid)) {
  457. capability = WLAN_CAPABILITY_IBSS;
  458. if (ifibss->privacy)
  459. capability |= WLAN_CAPABILITY_PRIVACY;
  460. cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
  461. ifibss->chandef.chan,
  462. ifibss->bssid, ifibss->ssid,
  463. ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
  464. WLAN_CAPABILITY_PRIVACY,
  465. capability);
  466. /* XXX: should not really modify cfg80211 data */
  467. if (cbss) {
  468. cbss->channel = sdata->csa_chandef.chan;
  469. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  470. }
  471. }
  472. ifibss->chandef = sdata->csa_chandef;
  473. /* generate the beacon */
  474. err = ieee80211_ibss_csa_beacon(sdata, NULL);
  475. if (err < 0)
  476. return err;
  477. changed |= err;
  478. return changed;
  479. }
  480. void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
  481. {
  482. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  483. cancel_work_sync(&ifibss->csa_connection_drop_work);
  484. }
  485. static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
  486. __acquires(RCU)
  487. {
  488. struct ieee80211_sub_if_data *sdata = sta->sdata;
  489. u8 addr[ETH_ALEN];
  490. memcpy(addr, sta->sta.addr, ETH_ALEN);
  491. ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
  492. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  493. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  494. /* authorize the station only if the network is not RSN protected. If
  495. * not wait for the userspace to authorize it */
  496. if (!sta->sdata->u.ibss.control_port)
  497. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  498. rate_control_rate_init(sta);
  499. /* If it fails, maybe we raced another insertion? */
  500. if (sta_info_insert_rcu(sta))
  501. return sta_info_get(sdata, addr);
  502. return sta;
  503. }
  504. static struct sta_info *
  505. ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
  506. const u8 *addr, u32 supp_rates)
  507. __acquires(RCU)
  508. {
  509. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  510. struct ieee80211_local *local = sdata->local;
  511. struct sta_info *sta;
  512. struct ieee80211_chanctx_conf *chanctx_conf;
  513. struct ieee80211_supported_band *sband;
  514. enum nl80211_bss_scan_width scan_width;
  515. int band;
  516. /*
  517. * XXX: Consider removing the least recently used entry and
  518. * allow new one to be added.
  519. */
  520. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  521. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  522. sdata->name, addr);
  523. rcu_read_lock();
  524. return NULL;
  525. }
  526. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
  527. rcu_read_lock();
  528. return NULL;
  529. }
  530. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
  531. rcu_read_lock();
  532. return NULL;
  533. }
  534. rcu_read_lock();
  535. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  536. if (WARN_ON_ONCE(!chanctx_conf))
  537. return NULL;
  538. band = chanctx_conf->def.chan->band;
  539. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  540. rcu_read_unlock();
  541. sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
  542. if (!sta) {
  543. rcu_read_lock();
  544. return NULL;
  545. }
  546. sta->last_rx = jiffies;
  547. /* make sure mandatory rates are always added */
  548. sband = local->hw.wiphy->bands[band];
  549. sta->sta.supp_rates[band] = supp_rates |
  550. ieee80211_mandatory_rates(sband, scan_width);
  551. return ieee80211_ibss_finish_sta(sta);
  552. }
  553. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  554. {
  555. struct ieee80211_local *local = sdata->local;
  556. int active = 0;
  557. struct sta_info *sta;
  558. sdata_assert_lock(sdata);
  559. rcu_read_lock();
  560. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  561. if (sta->sdata == sdata &&
  562. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  563. jiffies)) {
  564. active++;
  565. break;
  566. }
  567. }
  568. rcu_read_unlock();
  569. return active;
  570. }
  571. static void ieee80211_ibss_disconnect(struct ieee80211_sub_if_data *sdata)
  572. {
  573. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  574. struct ieee80211_local *local = sdata->local;
  575. struct cfg80211_bss *cbss;
  576. struct beacon_data *presp;
  577. struct sta_info *sta;
  578. u16 capability;
  579. if (!is_zero_ether_addr(ifibss->bssid)) {
  580. capability = WLAN_CAPABILITY_IBSS;
  581. if (ifibss->privacy)
  582. capability |= WLAN_CAPABILITY_PRIVACY;
  583. cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
  584. ifibss->bssid, ifibss->ssid,
  585. ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
  586. WLAN_CAPABILITY_PRIVACY,
  587. capability);
  588. if (cbss) {
  589. cfg80211_unlink_bss(local->hw.wiphy, cbss);
  590. cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
  591. }
  592. }
  593. ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
  594. sta_info_flush(sdata);
  595. spin_lock_bh(&ifibss->incomplete_lock);
  596. while (!list_empty(&ifibss->incomplete_stations)) {
  597. sta = list_first_entry(&ifibss->incomplete_stations,
  598. struct sta_info, list);
  599. list_del(&sta->list);
  600. spin_unlock_bh(&ifibss->incomplete_lock);
  601. sta_info_free(local, sta);
  602. spin_lock_bh(&ifibss->incomplete_lock);
  603. }
  604. spin_unlock_bh(&ifibss->incomplete_lock);
  605. netif_carrier_off(sdata->dev);
  606. sdata->vif.bss_conf.ibss_joined = false;
  607. sdata->vif.bss_conf.ibss_creator = false;
  608. sdata->vif.bss_conf.enable_beacon = false;
  609. sdata->vif.bss_conf.ssid_len = 0;
  610. /* remove beacon */
  611. presp = rcu_dereference_protected(ifibss->presp,
  612. lockdep_is_held(&sdata->wdev.mtx));
  613. RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
  614. if (presp)
  615. kfree_rcu(presp, rcu_head);
  616. clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
  617. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
  618. BSS_CHANGED_IBSS);
  619. drv_leave_ibss(local, sdata);
  620. mutex_lock(&local->mtx);
  621. ieee80211_vif_release_channel(sdata);
  622. mutex_unlock(&local->mtx);
  623. }
  624. static void ieee80211_csa_connection_drop_work(struct work_struct *work)
  625. {
  626. struct ieee80211_sub_if_data *sdata =
  627. container_of(work, struct ieee80211_sub_if_data,
  628. u.ibss.csa_connection_drop_work);
  629. sdata_lock(sdata);
  630. ieee80211_ibss_disconnect(sdata);
  631. synchronize_rcu();
  632. skb_queue_purge(&sdata->skb_queue);
  633. /* trigger a scan to find another IBSS network to join */
  634. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  635. sdata_unlock(sdata);
  636. }
  637. static void ieee80211_ibss_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
  638. {
  639. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  640. int err;
  641. /* if the current channel is a DFS channel, mark the channel as
  642. * unavailable.
  643. */
  644. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  645. &ifibss->chandef,
  646. NL80211_IFTYPE_ADHOC);
  647. if (err > 0)
  648. cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
  649. GFP_ATOMIC);
  650. }
  651. static bool
  652. ieee80211_ibss_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  653. struct ieee802_11_elems *elems,
  654. bool beacon)
  655. {
  656. struct cfg80211_csa_settings params;
  657. struct ieee80211_csa_ie csa_ie;
  658. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  659. enum nl80211_channel_type ch_type;
  660. int err;
  661. u32 sta_flags;
  662. sdata_assert_lock(sdata);
  663. sta_flags = IEEE80211_STA_DISABLE_VHT;
  664. switch (ifibss->chandef.width) {
  665. case NL80211_CHAN_WIDTH_5:
  666. case NL80211_CHAN_WIDTH_10:
  667. case NL80211_CHAN_WIDTH_20_NOHT:
  668. sta_flags |= IEEE80211_STA_DISABLE_HT;
  669. /* fall through */
  670. case NL80211_CHAN_WIDTH_20:
  671. sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
  672. break;
  673. default:
  674. break;
  675. }
  676. memset(&params, 0, sizeof(params));
  677. memset(&csa_ie, 0, sizeof(csa_ie));
  678. err = ieee80211_parse_ch_switch_ie(sdata, elems, beacon,
  679. ifibss->chandef.chan->band,
  680. sta_flags, ifibss->bssid, &csa_ie);
  681. /* can't switch to destination channel, fail */
  682. if (err < 0)
  683. goto disconnect;
  684. /* did not contain a CSA */
  685. if (err)
  686. return false;
  687. /* channel switch is not supported, disconnect */
  688. if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
  689. goto disconnect;
  690. params.count = csa_ie.count;
  691. params.chandef = csa_ie.chandef;
  692. switch (ifibss->chandef.width) {
  693. case NL80211_CHAN_WIDTH_20_NOHT:
  694. case NL80211_CHAN_WIDTH_20:
  695. case NL80211_CHAN_WIDTH_40:
  696. /* keep our current HT mode (HT20/HT40+/HT40-), even if
  697. * another mode has been announced. The mode is not adopted
  698. * within the beacon while doing CSA and we should therefore
  699. * keep the mode which we announce.
  700. */
  701. ch_type = cfg80211_get_chandef_type(&ifibss->chandef);
  702. cfg80211_chandef_create(&params.chandef, params.chandef.chan,
  703. ch_type);
  704. break;
  705. case NL80211_CHAN_WIDTH_5:
  706. case NL80211_CHAN_WIDTH_10:
  707. if (params.chandef.width != ifibss->chandef.width) {
  708. sdata_info(sdata,
  709. "IBSS %pM received channel switch from incompatible channel width (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
  710. ifibss->bssid,
  711. params.chandef.chan->center_freq,
  712. params.chandef.width,
  713. params.chandef.center_freq1,
  714. params.chandef.center_freq2);
  715. goto disconnect;
  716. }
  717. break;
  718. default:
  719. /* should not happen, sta_flags should prevent VHT modes. */
  720. WARN_ON(1);
  721. goto disconnect;
  722. }
  723. if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
  724. NL80211_IFTYPE_ADHOC)) {
  725. sdata_info(sdata,
  726. "IBSS %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
  727. ifibss->bssid,
  728. params.chandef.chan->center_freq,
  729. params.chandef.width,
  730. params.chandef.center_freq1,
  731. params.chandef.center_freq2);
  732. goto disconnect;
  733. }
  734. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  735. &params.chandef,
  736. NL80211_IFTYPE_ADHOC);
  737. if (err < 0)
  738. goto disconnect;
  739. if (err > 0 && !ifibss->userspace_handles_dfs) {
  740. /* IBSS-DFS only allowed with a control program */
  741. goto disconnect;
  742. }
  743. params.radar_required = err;
  744. if (cfg80211_chandef_identical(&params.chandef,
  745. &sdata->vif.bss_conf.chandef)) {
  746. ibss_dbg(sdata,
  747. "received csa with an identical chandef, ignoring\n");
  748. return true;
  749. }
  750. /* all checks done, now perform the channel switch. */
  751. ibss_dbg(sdata,
  752. "received channel switch announcement to go to channel %d MHz\n",
  753. params.chandef.chan->center_freq);
  754. params.block_tx = !!csa_ie.mode;
  755. if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
  756. &params))
  757. goto disconnect;
  758. ieee80211_ibss_csa_mark_radar(sdata);
  759. return true;
  760. disconnect:
  761. ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
  762. ieee80211_queue_work(&sdata->local->hw,
  763. &ifibss->csa_connection_drop_work);
  764. ieee80211_ibss_csa_mark_radar(sdata);
  765. return true;
  766. }
  767. static void
  768. ieee80211_rx_mgmt_spectrum_mgmt(struct ieee80211_sub_if_data *sdata,
  769. struct ieee80211_mgmt *mgmt, size_t len,
  770. struct ieee80211_rx_status *rx_status,
  771. struct ieee802_11_elems *elems)
  772. {
  773. int required_len;
  774. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  775. return;
  776. /* CSA is the only action we handle for now */
  777. if (mgmt->u.action.u.measurement.action_code !=
  778. WLAN_ACTION_SPCT_CHL_SWITCH)
  779. return;
  780. required_len = IEEE80211_MIN_ACTION_SIZE +
  781. sizeof(mgmt->u.action.u.chan_switch);
  782. if (len < required_len)
  783. return;
  784. if (!sdata->vif.csa_active)
  785. ieee80211_ibss_process_chanswitch(sdata, elems, false);
  786. }
  787. static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
  788. struct ieee80211_mgmt *mgmt,
  789. size_t len)
  790. {
  791. u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
  792. if (len < IEEE80211_DEAUTH_FRAME_LEN)
  793. return;
  794. ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM BSSID=%pM (reason: %d)\n",
  795. mgmt->sa, mgmt->da, mgmt->bssid, reason);
  796. sta_info_destroy_addr(sdata, mgmt->sa);
  797. }
  798. static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
  799. struct ieee80211_mgmt *mgmt,
  800. size_t len)
  801. {
  802. u16 auth_alg, auth_transaction;
  803. sdata_assert_lock(sdata);
  804. if (len < 24 + 6)
  805. return;
  806. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  807. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  808. ibss_dbg(sdata,
  809. "RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
  810. mgmt->sa, mgmt->da, mgmt->bssid, auth_transaction);
  811. if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
  812. return;
  813. /*
  814. * IEEE 802.11 standard does not require authentication in IBSS
  815. * networks and most implementations do not seem to use it.
  816. * However, try to reply to authentication attempts if someone
  817. * has actually implemented this.
  818. */
  819. ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
  820. mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
  821. }
  822. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  823. struct ieee80211_mgmt *mgmt, size_t len,
  824. struct ieee80211_rx_status *rx_status,
  825. struct ieee802_11_elems *elems)
  826. {
  827. struct ieee80211_local *local = sdata->local;
  828. struct cfg80211_bss *cbss;
  829. struct ieee80211_bss *bss;
  830. struct sta_info *sta;
  831. struct ieee80211_channel *channel;
  832. u64 beacon_timestamp, rx_timestamp;
  833. u32 supp_rates = 0;
  834. enum ieee80211_band band = rx_status->band;
  835. enum nl80211_bss_scan_width scan_width;
  836. struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
  837. bool rates_updated = false;
  838. channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
  839. if (!channel)
  840. return;
  841. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  842. ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid)) {
  843. rcu_read_lock();
  844. sta = sta_info_get(sdata, mgmt->sa);
  845. if (elems->supp_rates) {
  846. supp_rates = ieee80211_sta_get_rates(sdata, elems,
  847. band, NULL);
  848. if (sta) {
  849. u32 prev_rates;
  850. prev_rates = sta->sta.supp_rates[band];
  851. /* make sure mandatory rates are always added */
  852. scan_width = NL80211_BSS_CHAN_WIDTH_20;
  853. if (rx_status->flag & RX_FLAG_5MHZ)
  854. scan_width = NL80211_BSS_CHAN_WIDTH_5;
  855. if (rx_status->flag & RX_FLAG_10MHZ)
  856. scan_width = NL80211_BSS_CHAN_WIDTH_10;
  857. sta->sta.supp_rates[band] = supp_rates |
  858. ieee80211_mandatory_rates(sband,
  859. scan_width);
  860. if (sta->sta.supp_rates[band] != prev_rates) {
  861. ibss_dbg(sdata,
  862. "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
  863. sta->sta.addr, prev_rates,
  864. sta->sta.supp_rates[band]);
  865. rates_updated = true;
  866. }
  867. } else {
  868. rcu_read_unlock();
  869. sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
  870. mgmt->sa, supp_rates);
  871. }
  872. }
  873. if (sta && elems->wmm_info)
  874. set_sta_flag(sta, WLAN_STA_WME);
  875. if (sta && elems->ht_operation && elems->ht_cap_elem &&
  876. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
  877. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
  878. sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
  879. /* we both use HT */
  880. struct ieee80211_ht_cap htcap_ie;
  881. struct cfg80211_chan_def chandef;
  882. ieee80211_ht_oper_to_chandef(channel,
  883. elems->ht_operation,
  884. &chandef);
  885. memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
  886. /*
  887. * fall back to HT20 if we don't use or use
  888. * the other extension channel
  889. */
  890. if (chandef.center_freq1 !=
  891. sdata->u.ibss.chandef.center_freq1)
  892. htcap_ie.cap_info &=
  893. cpu_to_le16(~IEEE80211_HT_CAP_SUP_WIDTH_20_40);
  894. rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(
  895. sdata, sband, &htcap_ie, sta);
  896. }
  897. if (sta && rates_updated) {
  898. drv_sta_rc_update(local, sdata, &sta->sta,
  899. IEEE80211_RC_SUPP_RATES_CHANGED);
  900. rate_control_rate_init(sta);
  901. }
  902. rcu_read_unlock();
  903. }
  904. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  905. channel);
  906. if (!bss)
  907. return;
  908. cbss = container_of((void *)bss, struct cfg80211_bss, priv);
  909. /* same for beacon and probe response */
  910. beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
  911. /* check if we need to merge IBSS */
  912. /* not an IBSS */
  913. if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
  914. goto put_bss;
  915. /* different channel */
  916. if (sdata->u.ibss.fixed_channel &&
  917. sdata->u.ibss.chandef.chan != cbss->channel)
  918. goto put_bss;
  919. /* different SSID */
  920. if (elems->ssid_len != sdata->u.ibss.ssid_len ||
  921. memcmp(elems->ssid, sdata->u.ibss.ssid,
  922. sdata->u.ibss.ssid_len))
  923. goto put_bss;
  924. /* process channel switch */
  925. if (sdata->vif.csa_active ||
  926. ieee80211_ibss_process_chanswitch(sdata, elems, true))
  927. goto put_bss;
  928. /* same BSSID */
  929. if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
  930. goto put_bss;
  931. /* we use a fixed BSSID */
  932. if (sdata->u.ibss.fixed_bssid)
  933. goto put_bss;
  934. if (ieee80211_have_rx_timestamp(rx_status)) {
  935. /* time when timestamp field was received */
  936. rx_timestamp =
  937. ieee80211_calculate_rx_timestamp(local, rx_status,
  938. len + FCS_LEN, 24);
  939. } else {
  940. /*
  941. * second best option: get current TSF
  942. * (will return -1 if not supported)
  943. */
  944. rx_timestamp = drv_get_tsf(local, sdata);
  945. }
  946. ibss_dbg(sdata,
  947. "RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  948. mgmt->sa, mgmt->bssid,
  949. (unsigned long long)rx_timestamp,
  950. (unsigned long long)beacon_timestamp,
  951. (unsigned long long)(rx_timestamp - beacon_timestamp),
  952. jiffies);
  953. if (beacon_timestamp > rx_timestamp) {
  954. ibss_dbg(sdata,
  955. "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
  956. mgmt->bssid);
  957. ieee80211_sta_join_ibss(sdata, bss);
  958. supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
  959. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
  960. supp_rates);
  961. rcu_read_unlock();
  962. }
  963. put_bss:
  964. ieee80211_rx_bss_put(local, bss);
  965. }
  966. void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
  967. const u8 *bssid, const u8 *addr,
  968. u32 supp_rates)
  969. {
  970. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  971. struct ieee80211_local *local = sdata->local;
  972. struct sta_info *sta;
  973. struct ieee80211_chanctx_conf *chanctx_conf;
  974. struct ieee80211_supported_band *sband;
  975. enum nl80211_bss_scan_width scan_width;
  976. int band;
  977. /*
  978. * XXX: Consider removing the least recently used entry and
  979. * allow new one to be added.
  980. */
  981. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  982. net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
  983. sdata->name, addr);
  984. return;
  985. }
  986. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
  987. return;
  988. if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
  989. return;
  990. rcu_read_lock();
  991. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  992. if (WARN_ON_ONCE(!chanctx_conf)) {
  993. rcu_read_unlock();
  994. return;
  995. }
  996. band = chanctx_conf->def.chan->band;
  997. scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
  998. rcu_read_unlock();
  999. sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
  1000. if (!sta)
  1001. return;
  1002. sta->last_rx = jiffies;
  1003. /* make sure mandatory rates are always added */
  1004. sband = local->hw.wiphy->bands[band];
  1005. sta->sta.supp_rates[band] = supp_rates |
  1006. ieee80211_mandatory_rates(sband, scan_width);
  1007. spin_lock(&ifibss->incomplete_lock);
  1008. list_add(&sta->list, &ifibss->incomplete_stations);
  1009. spin_unlock(&ifibss->incomplete_lock);
  1010. ieee80211_queue_work(&local->hw, &sdata->work);
  1011. }
  1012. static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
  1013. {
  1014. struct ieee80211_local *local = sdata->local;
  1015. struct sta_info *sta, *tmp;
  1016. unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
  1017. unsigned long exp_rsn_time = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
  1018. mutex_lock(&local->sta_mtx);
  1019. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  1020. if (sdata != sta->sdata)
  1021. continue;
  1022. if (time_after(jiffies, sta->last_rx + exp_time) ||
  1023. (time_after(jiffies, sta->last_rx + exp_rsn_time) &&
  1024. sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
  1025. sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
  1026. sta->sta_state != IEEE80211_STA_AUTHORIZED ?
  1027. "not authorized " : "", sta->sta.addr);
  1028. WARN_ON(__sta_info_destroy(sta));
  1029. }
  1030. }
  1031. mutex_unlock(&local->sta_mtx);
  1032. }
  1033. /*
  1034. * This function is called with state == IEEE80211_IBSS_MLME_JOINED
  1035. */
  1036. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
  1037. {
  1038. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1039. enum nl80211_bss_scan_width scan_width;
  1040. sdata_assert_lock(sdata);
  1041. mod_timer(&ifibss->timer,
  1042. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  1043. ieee80211_ibss_sta_expire(sdata);
  1044. if (time_before(jiffies, ifibss->last_scan_completed +
  1045. IEEE80211_IBSS_MERGE_INTERVAL))
  1046. return;
  1047. if (ieee80211_sta_active_ibss(sdata))
  1048. return;
  1049. if (ifibss->fixed_channel)
  1050. return;
  1051. sdata_info(sdata,
  1052. "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
  1053. scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
  1054. ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
  1055. NULL, scan_width);
  1056. }
  1057. static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
  1058. {
  1059. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1060. u8 bssid[ETH_ALEN];
  1061. u16 capability;
  1062. int i;
  1063. sdata_assert_lock(sdata);
  1064. if (ifibss->fixed_bssid) {
  1065. memcpy(bssid, ifibss->bssid, ETH_ALEN);
  1066. } else {
  1067. /* Generate random, not broadcast, locally administered BSSID. Mix in
  1068. * own MAC address to make sure that devices that do not have proper
  1069. * random number generator get different BSSID. */
  1070. get_random_bytes(bssid, ETH_ALEN);
  1071. for (i = 0; i < ETH_ALEN; i++)
  1072. bssid[i] ^= sdata->vif.addr[i];
  1073. bssid[0] &= ~0x01;
  1074. bssid[0] |= 0x02;
  1075. }
  1076. sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
  1077. capability = WLAN_CAPABILITY_IBSS;
  1078. if (ifibss->privacy)
  1079. capability |= WLAN_CAPABILITY_PRIVACY;
  1080. else
  1081. sdata->drop_unencrypted = 0;
  1082. __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
  1083. &ifibss->chandef, ifibss->basic_rates,
  1084. capability, 0, true);
  1085. }
  1086. /*
  1087. * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
  1088. */
  1089. static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
  1090. {
  1091. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1092. struct ieee80211_local *local = sdata->local;
  1093. struct cfg80211_bss *cbss;
  1094. struct ieee80211_channel *chan = NULL;
  1095. const u8 *bssid = NULL;
  1096. enum nl80211_bss_scan_width scan_width;
  1097. int active_ibss;
  1098. u16 capability;
  1099. sdata_assert_lock(sdata);
  1100. active_ibss = ieee80211_sta_active_ibss(sdata);
  1101. ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
  1102. if (active_ibss)
  1103. return;
  1104. capability = WLAN_CAPABILITY_IBSS;
  1105. if (ifibss->privacy)
  1106. capability |= WLAN_CAPABILITY_PRIVACY;
  1107. if (ifibss->fixed_bssid)
  1108. bssid = ifibss->bssid;
  1109. if (ifibss->fixed_channel)
  1110. chan = ifibss->chandef.chan;
  1111. if (!is_zero_ether_addr(ifibss->bssid))
  1112. bssid = ifibss->bssid;
  1113. cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
  1114. ifibss->ssid, ifibss->ssid_len,
  1115. WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
  1116. capability);
  1117. if (cbss) {
  1118. struct ieee80211_bss *bss;
  1119. bss = (void *)cbss->priv;
  1120. ibss_dbg(sdata,
  1121. "sta_find_ibss: selected %pM current %pM\n",
  1122. cbss->bssid, ifibss->bssid);
  1123. sdata_info(sdata,
  1124. "Selected IBSS BSSID %pM based on configured SSID\n",
  1125. cbss->bssid);
  1126. ieee80211_sta_join_ibss(sdata, bss);
  1127. ieee80211_rx_bss_put(local, bss);
  1128. return;
  1129. }
  1130. /* if a fixed bssid and a fixed freq have been provided create the IBSS
  1131. * directly and do not waste time scanning
  1132. */
  1133. if (ifibss->fixed_bssid && ifibss->fixed_channel) {
  1134. sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
  1135. bssid);
  1136. ieee80211_sta_create_ibss(sdata);
  1137. return;
  1138. }
  1139. ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
  1140. /* Selected IBSS not found in current scan results - try to scan */
  1141. if (time_after(jiffies, ifibss->last_scan_completed +
  1142. IEEE80211_SCAN_INTERVAL)) {
  1143. sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
  1144. scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
  1145. ieee80211_request_ibss_scan(sdata, ifibss->ssid,
  1146. ifibss->ssid_len, chan,
  1147. scan_width);
  1148. } else {
  1149. int interval = IEEE80211_SCAN_INTERVAL;
  1150. if (time_after(jiffies, ifibss->ibss_join_req +
  1151. IEEE80211_IBSS_JOIN_TIMEOUT))
  1152. ieee80211_sta_create_ibss(sdata);
  1153. mod_timer(&ifibss->timer,
  1154. round_jiffies(jiffies + interval));
  1155. }
  1156. }
  1157. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  1158. struct sk_buff *req)
  1159. {
  1160. struct ieee80211_mgmt *mgmt = (void *)req->data;
  1161. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1162. struct ieee80211_local *local = sdata->local;
  1163. int tx_last_beacon, len = req->len;
  1164. struct sk_buff *skb;
  1165. struct beacon_data *presp;
  1166. u8 *pos, *end;
  1167. sdata_assert_lock(sdata);
  1168. presp = rcu_dereference_protected(ifibss->presp,
  1169. lockdep_is_held(&sdata->wdev.mtx));
  1170. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  1171. len < 24 + 2 || !presp)
  1172. return;
  1173. tx_last_beacon = drv_tx_last_beacon(local);
  1174. ibss_dbg(sdata,
  1175. "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
  1176. mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
  1177. if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
  1178. return;
  1179. if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
  1180. !is_broadcast_ether_addr(mgmt->bssid))
  1181. return;
  1182. end = ((u8 *) mgmt) + len;
  1183. pos = mgmt->u.probe_req.variable;
  1184. if (pos[0] != WLAN_EID_SSID ||
  1185. pos + 2 + pos[1] > end) {
  1186. ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
  1187. mgmt->sa);
  1188. return;
  1189. }
  1190. if (pos[1] != 0 &&
  1191. (pos[1] != ifibss->ssid_len ||
  1192. memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
  1193. /* Ignore ProbeReq for foreign SSID */
  1194. return;
  1195. }
  1196. /* Reply with ProbeResp */
  1197. skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
  1198. if (!skb)
  1199. return;
  1200. skb_reserve(skb, local->tx_headroom);
  1201. memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
  1202. memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
  1203. ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
  1204. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1205. /* avoid excessive retries for probe request to wildcard SSIDs */
  1206. if (pos[1] == 0)
  1207. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
  1208. ieee80211_tx_skb(sdata, skb);
  1209. }
  1210. static
  1211. void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
  1212. struct ieee80211_mgmt *mgmt, size_t len,
  1213. struct ieee80211_rx_status *rx_status)
  1214. {
  1215. size_t baselen;
  1216. struct ieee802_11_elems elems;
  1217. BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
  1218. offsetof(typeof(mgmt->u.beacon), variable));
  1219. /*
  1220. * either beacon or probe_resp but the variable field is at the
  1221. * same offset
  1222. */
  1223. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1224. if (baselen > len)
  1225. return;
  1226. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1227. false, &elems);
  1228. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  1229. }
  1230. void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1231. struct sk_buff *skb)
  1232. {
  1233. struct ieee80211_rx_status *rx_status;
  1234. struct ieee80211_mgmt *mgmt;
  1235. u16 fc;
  1236. struct ieee802_11_elems elems;
  1237. int ies_len;
  1238. rx_status = IEEE80211_SKB_RXCB(skb);
  1239. mgmt = (struct ieee80211_mgmt *) skb->data;
  1240. fc = le16_to_cpu(mgmt->frame_control);
  1241. sdata_lock(sdata);
  1242. if (!sdata->u.ibss.ssid_len)
  1243. goto mgmt_out; /* not ready to merge yet */
  1244. switch (fc & IEEE80211_FCTL_STYPE) {
  1245. case IEEE80211_STYPE_PROBE_REQ:
  1246. ieee80211_rx_mgmt_probe_req(sdata, skb);
  1247. break;
  1248. case IEEE80211_STYPE_PROBE_RESP:
  1249. case IEEE80211_STYPE_BEACON:
  1250. ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
  1251. rx_status);
  1252. break;
  1253. case IEEE80211_STYPE_AUTH:
  1254. ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
  1255. break;
  1256. case IEEE80211_STYPE_DEAUTH:
  1257. ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
  1258. break;
  1259. case IEEE80211_STYPE_ACTION:
  1260. switch (mgmt->u.action.category) {
  1261. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1262. ies_len = skb->len -
  1263. offsetof(struct ieee80211_mgmt,
  1264. u.action.u.chan_switch.variable);
  1265. if (ies_len < 0)
  1266. break;
  1267. ieee802_11_parse_elems(
  1268. mgmt->u.action.u.chan_switch.variable,
  1269. ies_len, true, &elems);
  1270. if (elems.parse_error)
  1271. break;
  1272. ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt, skb->len,
  1273. rx_status, &elems);
  1274. break;
  1275. }
  1276. }
  1277. mgmt_out:
  1278. sdata_unlock(sdata);
  1279. }
  1280. void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
  1281. {
  1282. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1283. struct sta_info *sta;
  1284. sdata_lock(sdata);
  1285. /*
  1286. * Work could be scheduled after scan or similar
  1287. * when we aren't even joined (or trying) with a
  1288. * network.
  1289. */
  1290. if (!ifibss->ssid_len)
  1291. goto out;
  1292. spin_lock_bh(&ifibss->incomplete_lock);
  1293. while (!list_empty(&ifibss->incomplete_stations)) {
  1294. sta = list_first_entry(&ifibss->incomplete_stations,
  1295. struct sta_info, list);
  1296. list_del(&sta->list);
  1297. spin_unlock_bh(&ifibss->incomplete_lock);
  1298. ieee80211_ibss_finish_sta(sta);
  1299. rcu_read_unlock();
  1300. spin_lock_bh(&ifibss->incomplete_lock);
  1301. }
  1302. spin_unlock_bh(&ifibss->incomplete_lock);
  1303. switch (ifibss->state) {
  1304. case IEEE80211_IBSS_MLME_SEARCH:
  1305. ieee80211_sta_find_ibss(sdata);
  1306. break;
  1307. case IEEE80211_IBSS_MLME_JOINED:
  1308. ieee80211_sta_merge_ibss(sdata);
  1309. break;
  1310. default:
  1311. WARN_ON(1);
  1312. break;
  1313. }
  1314. out:
  1315. sdata_unlock(sdata);
  1316. }
  1317. static void ieee80211_ibss_timer(unsigned long data)
  1318. {
  1319. struct ieee80211_sub_if_data *sdata =
  1320. (struct ieee80211_sub_if_data *) data;
  1321. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1322. }
  1323. void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1324. {
  1325. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1326. setup_timer(&ifibss->timer, ieee80211_ibss_timer,
  1327. (unsigned long) sdata);
  1328. INIT_LIST_HEAD(&ifibss->incomplete_stations);
  1329. spin_lock_init(&ifibss->incomplete_lock);
  1330. INIT_WORK(&ifibss->csa_connection_drop_work,
  1331. ieee80211_csa_connection_drop_work);
  1332. }
  1333. /* scan finished notification */
  1334. void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
  1335. {
  1336. struct ieee80211_sub_if_data *sdata;
  1337. mutex_lock(&local->iflist_mtx);
  1338. list_for_each_entry(sdata, &local->interfaces, list) {
  1339. if (!ieee80211_sdata_running(sdata))
  1340. continue;
  1341. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  1342. continue;
  1343. sdata->u.ibss.last_scan_completed = jiffies;
  1344. ieee80211_queue_work(&local->hw, &sdata->work);
  1345. }
  1346. mutex_unlock(&local->iflist_mtx);
  1347. }
  1348. int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
  1349. struct cfg80211_ibss_params *params)
  1350. {
  1351. u32 changed = 0;
  1352. u32 rate_flags;
  1353. struct ieee80211_supported_band *sband;
  1354. enum ieee80211_chanctx_mode chanmode;
  1355. struct ieee80211_local *local = sdata->local;
  1356. int radar_detect_width = 0;
  1357. int i;
  1358. int ret;
  1359. ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
  1360. &params->chandef,
  1361. sdata->wdev.iftype);
  1362. if (ret < 0)
  1363. return ret;
  1364. if (ret > 0) {
  1365. if (!params->userspace_handles_dfs)
  1366. return -EINVAL;
  1367. radar_detect_width = BIT(params->chandef.width);
  1368. }
  1369. chanmode = (params->channel_fixed && !ret) ?
  1370. IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
  1371. mutex_lock(&local->chanctx_mtx);
  1372. ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
  1373. radar_detect_width);
  1374. mutex_unlock(&local->chanctx_mtx);
  1375. if (ret < 0)
  1376. return ret;
  1377. if (params->bssid) {
  1378. memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
  1379. sdata->u.ibss.fixed_bssid = true;
  1380. } else
  1381. sdata->u.ibss.fixed_bssid = false;
  1382. sdata->u.ibss.privacy = params->privacy;
  1383. sdata->u.ibss.control_port = params->control_port;
  1384. sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
  1385. sdata->u.ibss.basic_rates = params->basic_rates;
  1386. sdata->u.ibss.last_scan_completed = jiffies;
  1387. /* fix basic_rates if channel does not support these rates */
  1388. rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
  1389. sband = local->hw.wiphy->bands[params->chandef.chan->band];
  1390. for (i = 0; i < sband->n_bitrates; i++) {
  1391. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1392. sdata->u.ibss.basic_rates &= ~BIT(i);
  1393. }
  1394. memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
  1395. sizeof(params->mcast_rate));
  1396. sdata->vif.bss_conf.beacon_int = params->beacon_interval;
  1397. sdata->u.ibss.chandef = params->chandef;
  1398. sdata->u.ibss.fixed_channel = params->channel_fixed;
  1399. if (params->ie) {
  1400. sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
  1401. GFP_KERNEL);
  1402. if (sdata->u.ibss.ie)
  1403. sdata->u.ibss.ie_len = params->ie_len;
  1404. }
  1405. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  1406. sdata->u.ibss.ibss_join_req = jiffies;
  1407. memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
  1408. sdata->u.ibss.ssid_len = params->ssid_len;
  1409. memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
  1410. sizeof(sdata->u.ibss.ht_capa));
  1411. memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
  1412. sizeof(sdata->u.ibss.ht_capa_mask));
  1413. /*
  1414. * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
  1415. * reserved, but an HT STA shall protect HT transmissions as though
  1416. * the HT Protection field were set to non-HT mixed mode.
  1417. *
  1418. * In an IBSS, the RIFS Mode field of the HT Operation element is
  1419. * also reserved, but an HT STA shall operate as though this field
  1420. * were set to 1.
  1421. */
  1422. sdata->vif.bss_conf.ht_operation_mode |=
  1423. IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
  1424. | IEEE80211_HT_PARAM_RIFS_MODE;
  1425. changed |= BSS_CHANGED_HT;
  1426. ieee80211_bss_info_change_notify(sdata, changed);
  1427. sdata->smps_mode = IEEE80211_SMPS_OFF;
  1428. sdata->needed_rx_chains = local->rx_chains;
  1429. ieee80211_queue_work(&local->hw, &sdata->work);
  1430. return 0;
  1431. }
  1432. int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
  1433. {
  1434. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  1435. ieee80211_ibss_disconnect(sdata);
  1436. ifibss->ssid_len = 0;
  1437. memset(ifibss->bssid, 0, ETH_ALEN);
  1438. /* remove beacon */
  1439. kfree(sdata->u.ibss.ie);
  1440. /* on the next join, re-program HT parameters */
  1441. memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
  1442. memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
  1443. synchronize_rcu();
  1444. skb_queue_purge(&sdata->skb_queue);
  1445. del_timer_sync(&sdata->u.ibss.timer);
  1446. return 0;
  1447. }