ibss.c 30 KB

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