ibss.c 37 KB

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