PageRenderTime 262ms CodeModel.GetById 18ms RepoModel.GetById 2ms app.codeStats 0ms

/net/mac80211/ibss.c

https://bitbucket.org/abioy/linux
C | 965 lines | 706 code | 176 blank | 83 comment | 110 complexity | 4ea87814a8888720b19741a53fe9b6a6 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  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_MERGE_DELAY 0x400000
  31. #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
  32. #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
  33. static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
  34. struct ieee80211_mgmt *mgmt,
  35. size_t len)
  36. {
  37. u16 auth_alg, auth_transaction, status_code;
  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. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  43. /*
  44. * IEEE 802.11 standard does not require authentication in IBSS
  45. * networks and most implementations do not seem to use it.
  46. * However, try to reply to authentication attempts if someone
  47. * has actually implemented this.
  48. */
  49. if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1)
  50. ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
  51. sdata->u.ibss.bssid, NULL, 0, 0);
  52. }
  53. static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  54. const u8 *bssid, const int beacon_int,
  55. struct ieee80211_channel *chan,
  56. const u32 basic_rates,
  57. const u16 capability, u64 tsf)
  58. {
  59. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  60. struct ieee80211_local *local = sdata->local;
  61. int rates, i;
  62. struct sk_buff *skb;
  63. struct ieee80211_mgmt *mgmt;
  64. u8 *pos;
  65. struct ieee80211_supported_band *sband;
  66. struct cfg80211_bss *bss;
  67. u32 bss_change;
  68. u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
  69. /* Reset own TSF to allow time synchronization work. */
  70. drv_reset_tsf(local);
  71. skb = ifibss->skb;
  72. rcu_assign_pointer(ifibss->presp, NULL);
  73. synchronize_rcu();
  74. skb->data = skb->head;
  75. skb->len = 0;
  76. skb_reset_tail_pointer(skb);
  77. skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
  78. if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
  79. sta_info_flush(sdata->local, sdata);
  80. memcpy(ifibss->bssid, bssid, ETH_ALEN);
  81. sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
  82. local->oper_channel = chan;
  83. local->oper_channel_type = NL80211_CHAN_NO_HT;
  84. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  85. sband = local->hw.wiphy->bands[chan->band];
  86. /* build supported rates array */
  87. pos = supp_rates;
  88. for (i = 0; i < sband->n_bitrates; i++) {
  89. int rate = sband->bitrates[i].bitrate;
  90. u8 basic = 0;
  91. if (basic_rates & BIT(i))
  92. basic = 0x80;
  93. *pos++ = basic | (u8) (rate / 5);
  94. }
  95. /* Build IBSS probe response */
  96. mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
  97. memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  98. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  99. IEEE80211_STYPE_PROBE_RESP);
  100. memset(mgmt->da, 0xff, ETH_ALEN);
  101. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  102. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  103. mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
  104. mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
  105. mgmt->u.beacon.capab_info = cpu_to_le16(capability);
  106. pos = skb_put(skb, 2 + ifibss->ssid_len);
  107. *pos++ = WLAN_EID_SSID;
  108. *pos++ = ifibss->ssid_len;
  109. memcpy(pos, ifibss->ssid, ifibss->ssid_len);
  110. rates = sband->n_bitrates;
  111. if (rates > 8)
  112. rates = 8;
  113. pos = skb_put(skb, 2 + rates);
  114. *pos++ = WLAN_EID_SUPP_RATES;
  115. *pos++ = rates;
  116. memcpy(pos, supp_rates, rates);
  117. if (sband->band == IEEE80211_BAND_2GHZ) {
  118. pos = skb_put(skb, 2 + 1);
  119. *pos++ = WLAN_EID_DS_PARAMS;
  120. *pos++ = 1;
  121. *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
  122. }
  123. pos = skb_put(skb, 2 + 2);
  124. *pos++ = WLAN_EID_IBSS_PARAMS;
  125. *pos++ = 2;
  126. /* FIX: set ATIM window based on scan results */
  127. *pos++ = 0;
  128. *pos++ = 0;
  129. if (sband->n_bitrates > 8) {
  130. rates = sband->n_bitrates - 8;
  131. pos = skb_put(skb, 2 + rates);
  132. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  133. *pos++ = rates;
  134. memcpy(pos, &supp_rates[8], rates);
  135. }
  136. if (ifibss->ie_len)
  137. memcpy(skb_put(skb, ifibss->ie_len),
  138. ifibss->ie, ifibss->ie_len);
  139. rcu_assign_pointer(ifibss->presp, skb);
  140. sdata->vif.bss_conf.beacon_int = beacon_int;
  141. bss_change = BSS_CHANGED_BEACON_INT;
  142. bss_change |= ieee80211_reset_erp_info(sdata);
  143. bss_change |= BSS_CHANGED_BSSID;
  144. bss_change |= BSS_CHANGED_BEACON;
  145. bss_change |= BSS_CHANGED_BEACON_ENABLED;
  146. ieee80211_bss_info_change_notify(sdata, bss_change);
  147. ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
  148. ifibss->state = IEEE80211_IBSS_MLME_JOINED;
  149. mod_timer(&ifibss->timer,
  150. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  151. bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
  152. mgmt, skb->len, 0, GFP_KERNEL);
  153. cfg80211_put_bss(bss);
  154. cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
  155. }
  156. static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  157. struct ieee80211_bss *bss)
  158. {
  159. struct cfg80211_bss *cbss =
  160. container_of((void *)bss, struct cfg80211_bss, priv);
  161. struct ieee80211_supported_band *sband;
  162. u32 basic_rates;
  163. int i, j;
  164. u16 beacon_int = cbss->beacon_interval;
  165. if (beacon_int < 10)
  166. beacon_int = 10;
  167. sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
  168. basic_rates = 0;
  169. for (i = 0; i < bss->supp_rates_len; i++) {
  170. int rate = (bss->supp_rates[i] & 0x7f) * 5;
  171. bool is_basic = !!(bss->supp_rates[i] & 0x80);
  172. for (j = 0; j < sband->n_bitrates; j++) {
  173. if (sband->bitrates[j].bitrate == rate) {
  174. if (is_basic)
  175. basic_rates |= BIT(j);
  176. break;
  177. }
  178. }
  179. }
  180. __ieee80211_sta_join_ibss(sdata, cbss->bssid,
  181. beacon_int,
  182. cbss->channel,
  183. basic_rates,
  184. cbss->capability,
  185. cbss->tsf);
  186. }
  187. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  188. struct ieee80211_mgmt *mgmt,
  189. size_t len,
  190. struct ieee80211_rx_status *rx_status,
  191. struct ieee802_11_elems *elems,
  192. bool beacon)
  193. {
  194. struct ieee80211_local *local = sdata->local;
  195. int freq;
  196. struct cfg80211_bss *cbss;
  197. struct ieee80211_bss *bss;
  198. struct sta_info *sta;
  199. struct ieee80211_channel *channel;
  200. u64 beacon_timestamp, rx_timestamp;
  201. u32 supp_rates = 0;
  202. enum ieee80211_band band = rx_status->band;
  203. if (elems->ds_params && elems->ds_params_len == 1)
  204. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  205. else
  206. freq = rx_status->freq;
  207. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  208. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  209. return;
  210. if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
  211. memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
  212. supp_rates = ieee80211_sta_get_rates(local, elems, band);
  213. rcu_read_lock();
  214. sta = sta_info_get(sdata, mgmt->sa);
  215. if (sta) {
  216. u32 prev_rates;
  217. prev_rates = sta->sta.supp_rates[band];
  218. /* make sure mandatory rates are always added */
  219. sta->sta.supp_rates[band] = supp_rates |
  220. ieee80211_mandatory_rates(local, band);
  221. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  222. if (sta->sta.supp_rates[band] != prev_rates)
  223. printk(KERN_DEBUG "%s: updated supp_rates set "
  224. "for %pM based on beacon info (0x%llx | "
  225. "0x%llx -> 0x%llx)\n",
  226. sdata->name,
  227. sta->sta.addr,
  228. (unsigned long long) prev_rates,
  229. (unsigned long long) supp_rates,
  230. (unsigned long long) sta->sta.supp_rates[band]);
  231. #endif
  232. rcu_read_unlock();
  233. } else {
  234. rcu_read_unlock();
  235. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
  236. supp_rates, GFP_KERNEL);
  237. }
  238. }
  239. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  240. channel, beacon);
  241. if (!bss)
  242. return;
  243. cbss = container_of((void *)bss, struct cfg80211_bss, priv);
  244. /* was just updated in ieee80211_bss_info_update */
  245. beacon_timestamp = cbss->tsf;
  246. /* check if we need to merge IBSS */
  247. /* we use a fixed BSSID */
  248. if (sdata->u.ibss.fixed_bssid)
  249. goto put_bss;
  250. /* not an IBSS */
  251. if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
  252. goto put_bss;
  253. /* different channel */
  254. if (cbss->channel != local->oper_channel)
  255. goto put_bss;
  256. /* different SSID */
  257. if (elems->ssid_len != sdata->u.ibss.ssid_len ||
  258. memcmp(elems->ssid, sdata->u.ibss.ssid,
  259. sdata->u.ibss.ssid_len))
  260. goto put_bss;
  261. /* same BSSID */
  262. if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
  263. goto put_bss;
  264. if (rx_status->flag & RX_FLAG_TSFT) {
  265. /*
  266. * For correct IBSS merging we need mactime; since mactime is
  267. * defined as the time the first data symbol of the frame hits
  268. * the PHY, and the timestamp of the beacon is defined as "the
  269. * time that the data symbol containing the first bit of the
  270. * timestamp is transmitted to the PHY plus the transmitting
  271. * STA's delays through its local PHY from the MAC-PHY
  272. * interface to its interface with the WM" (802.11 11.1.2)
  273. * - equals the time this bit arrives at the receiver - we have
  274. * to take into account the offset between the two.
  275. *
  276. * E.g. at 1 MBit that means mactime is 192 usec earlier
  277. * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
  278. */
  279. int rate;
  280. if (rx_status->flag & RX_FLAG_HT)
  281. rate = 65; /* TODO: HT rates */
  282. else
  283. rate = local->hw.wiphy->bands[band]->
  284. bitrates[rx_status->rate_idx].bitrate;
  285. rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
  286. } else {
  287. /*
  288. * second best option: get current TSF
  289. * (will return -1 if not supported)
  290. */
  291. rx_timestamp = drv_get_tsf(local);
  292. }
  293. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  294. printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
  295. "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  296. mgmt->sa, mgmt->bssid,
  297. (unsigned long long)rx_timestamp,
  298. (unsigned long long)beacon_timestamp,
  299. (unsigned long long)(rx_timestamp - beacon_timestamp),
  300. jiffies);
  301. #endif
  302. /* give slow hardware some time to do the TSF sync */
  303. if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
  304. goto put_bss;
  305. if (beacon_timestamp > rx_timestamp) {
  306. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  307. printk(KERN_DEBUG "%s: beacon TSF higher than "
  308. "local TSF - IBSS merge with BSSID %pM\n",
  309. sdata->name, mgmt->bssid);
  310. #endif
  311. ieee80211_sta_join_ibss(sdata, bss);
  312. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
  313. supp_rates, GFP_KERNEL);
  314. }
  315. put_bss:
  316. ieee80211_rx_bss_put(local, bss);
  317. }
  318. /*
  319. * Add a new IBSS station, will also be called by the RX code when,
  320. * in IBSS mode, receiving a frame from a yet-unknown station, hence
  321. * must be callable in atomic context.
  322. */
  323. struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
  324. u8 *bssid,u8 *addr, u32 supp_rates,
  325. gfp_t gfp)
  326. {
  327. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  328. struct ieee80211_local *local = sdata->local;
  329. struct sta_info *sta;
  330. int band = local->hw.conf.channel->band;
  331. /*
  332. * XXX: Consider removing the least recently used entry and
  333. * allow new one to be added.
  334. */
  335. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  336. if (net_ratelimit())
  337. printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
  338. sdata->name, addr);
  339. return NULL;
  340. }
  341. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
  342. return NULL;
  343. if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
  344. return NULL;
  345. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  346. printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n",
  347. wiphy_name(local->hw.wiphy), addr, sdata->name);
  348. #endif
  349. sta = sta_info_alloc(sdata, addr, gfp);
  350. if (!sta)
  351. return NULL;
  352. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  353. /* make sure mandatory rates are always added */
  354. sta->sta.supp_rates[band] = supp_rates |
  355. ieee80211_mandatory_rates(local, band);
  356. rate_control_rate_init(sta);
  357. /* If it fails, maybe we raced another insertion? */
  358. if (sta_info_insert(sta))
  359. return sta_info_get(sdata, addr);
  360. return sta;
  361. }
  362. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  363. {
  364. struct ieee80211_local *local = sdata->local;
  365. int active = 0;
  366. struct sta_info *sta;
  367. rcu_read_lock();
  368. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  369. if (sta->sdata == sdata &&
  370. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  371. jiffies)) {
  372. active++;
  373. break;
  374. }
  375. }
  376. rcu_read_unlock();
  377. return active;
  378. }
  379. /*
  380. * This function is called with state == IEEE80211_IBSS_MLME_JOINED
  381. */
  382. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
  383. {
  384. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  385. mod_timer(&ifibss->timer,
  386. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  387. ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
  388. if (time_before(jiffies, ifibss->last_scan_completed +
  389. IEEE80211_IBSS_MERGE_INTERVAL))
  390. return;
  391. if (ieee80211_sta_active_ibss(sdata))
  392. return;
  393. if (ifibss->fixed_channel)
  394. return;
  395. printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
  396. "IBSS networks with same SSID (merge)\n", sdata->name);
  397. ieee80211_request_internal_scan(sdata, ifibss->ssid, ifibss->ssid_len);
  398. }
  399. static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
  400. {
  401. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  402. struct ieee80211_local *local = sdata->local;
  403. struct ieee80211_supported_band *sband;
  404. u8 bssid[ETH_ALEN];
  405. u16 capability;
  406. int i;
  407. if (ifibss->fixed_bssid) {
  408. memcpy(bssid, ifibss->bssid, ETH_ALEN);
  409. } else {
  410. /* Generate random, not broadcast, locally administered BSSID. Mix in
  411. * own MAC address to make sure that devices that do not have proper
  412. * random number generator get different BSSID. */
  413. get_random_bytes(bssid, ETH_ALEN);
  414. for (i = 0; i < ETH_ALEN; i++)
  415. bssid[i] ^= sdata->vif.addr[i];
  416. bssid[0] &= ~0x01;
  417. bssid[0] |= 0x02;
  418. }
  419. printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
  420. sdata->name, bssid);
  421. sband = local->hw.wiphy->bands[ifibss->channel->band];
  422. capability = WLAN_CAPABILITY_IBSS;
  423. if (ifibss->privacy)
  424. capability |= WLAN_CAPABILITY_PRIVACY;
  425. else
  426. sdata->drop_unencrypted = 0;
  427. __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
  428. ifibss->channel, 3, /* first two are basic */
  429. capability, 0);
  430. }
  431. /*
  432. * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
  433. */
  434. static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
  435. {
  436. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  437. struct ieee80211_local *local = sdata->local;
  438. struct cfg80211_bss *cbss;
  439. struct ieee80211_channel *chan = NULL;
  440. const u8 *bssid = NULL;
  441. int active_ibss;
  442. u16 capability;
  443. active_ibss = ieee80211_sta_active_ibss(sdata);
  444. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  445. printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
  446. sdata->name, active_ibss);
  447. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  448. if (active_ibss)
  449. return;
  450. capability = WLAN_CAPABILITY_IBSS;
  451. if (ifibss->privacy)
  452. capability |= WLAN_CAPABILITY_PRIVACY;
  453. if (ifibss->fixed_bssid)
  454. bssid = ifibss->bssid;
  455. if (ifibss->fixed_channel)
  456. chan = ifibss->channel;
  457. if (!is_zero_ether_addr(ifibss->bssid))
  458. bssid = ifibss->bssid;
  459. cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
  460. ifibss->ssid, ifibss->ssid_len,
  461. WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
  462. capability);
  463. if (cbss) {
  464. struct ieee80211_bss *bss;
  465. bss = (void *)cbss->priv;
  466. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  467. printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
  468. "%pM\n", cbss->bssid, ifibss->bssid);
  469. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  470. printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
  471. " based on configured SSID\n",
  472. sdata->name, cbss->bssid);
  473. ieee80211_sta_join_ibss(sdata, bss);
  474. ieee80211_rx_bss_put(local, bss);
  475. return;
  476. }
  477. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  478. printk(KERN_DEBUG " did not try to join ibss\n");
  479. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  480. /* Selected IBSS not found in current scan results - try to scan */
  481. if (time_after(jiffies, ifibss->last_scan_completed +
  482. IEEE80211_SCAN_INTERVAL)) {
  483. printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
  484. "join\n", sdata->name);
  485. ieee80211_request_internal_scan(sdata, ifibss->ssid,
  486. ifibss->ssid_len);
  487. } else {
  488. int interval = IEEE80211_SCAN_INTERVAL;
  489. if (time_after(jiffies, ifibss->ibss_join_req +
  490. IEEE80211_IBSS_JOIN_TIMEOUT)) {
  491. if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
  492. ieee80211_sta_create_ibss(sdata);
  493. return;
  494. }
  495. printk(KERN_DEBUG "%s: IBSS not allowed on"
  496. " %d MHz\n", sdata->name,
  497. local->hw.conf.channel->center_freq);
  498. /* No IBSS found - decrease scan interval and continue
  499. * scanning. */
  500. interval = IEEE80211_SCAN_INTERVAL_SLOW;
  501. }
  502. mod_timer(&ifibss->timer,
  503. round_jiffies(jiffies + interval));
  504. }
  505. }
  506. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  507. struct ieee80211_mgmt *mgmt,
  508. size_t len)
  509. {
  510. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  511. struct ieee80211_local *local = sdata->local;
  512. int tx_last_beacon;
  513. struct sk_buff *skb;
  514. struct ieee80211_mgmt *resp;
  515. u8 *pos, *end;
  516. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  517. len < 24 + 2 || !ifibss->presp)
  518. return;
  519. tx_last_beacon = drv_tx_last_beacon(local);
  520. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  521. printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
  522. " (tx_last_beacon=%d)\n",
  523. sdata->name, mgmt->sa, mgmt->da,
  524. mgmt->bssid, tx_last_beacon);
  525. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  526. if (!tx_last_beacon)
  527. return;
  528. if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
  529. memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  530. return;
  531. end = ((u8 *) mgmt) + len;
  532. pos = mgmt->u.probe_req.variable;
  533. if (pos[0] != WLAN_EID_SSID ||
  534. pos + 2 + pos[1] > end) {
  535. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  536. printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
  537. "from %pM\n",
  538. sdata->name, mgmt->sa);
  539. #endif
  540. return;
  541. }
  542. if (pos[1] != 0 &&
  543. (pos[1] != ifibss->ssid_len ||
  544. memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
  545. /* Ignore ProbeReq for foreign SSID */
  546. return;
  547. }
  548. /* Reply with ProbeResp */
  549. skb = skb_copy(ifibss->presp, GFP_KERNEL);
  550. if (!skb)
  551. return;
  552. resp = (struct ieee80211_mgmt *) skb->data;
  553. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  554. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  555. printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
  556. sdata->name, resp->da);
  557. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  558. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  559. ieee80211_tx_skb(sdata, skb);
  560. }
  561. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  562. struct ieee80211_mgmt *mgmt,
  563. size_t len,
  564. struct ieee80211_rx_status *rx_status)
  565. {
  566. size_t baselen;
  567. struct ieee802_11_elems elems;
  568. if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
  569. return; /* ignore ProbeResp to foreign address */
  570. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  571. if (baselen > len)
  572. return;
  573. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  574. &elems);
  575. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  576. }
  577. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  578. struct ieee80211_mgmt *mgmt,
  579. size_t len,
  580. struct ieee80211_rx_status *rx_status)
  581. {
  582. size_t baselen;
  583. struct ieee802_11_elems elems;
  584. /* Process beacon from the current BSS */
  585. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  586. if (baselen > len)
  587. return;
  588. ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
  589. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
  590. }
  591. static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  592. struct sk_buff *skb)
  593. {
  594. struct ieee80211_rx_status *rx_status;
  595. struct ieee80211_mgmt *mgmt;
  596. u16 fc;
  597. rx_status = IEEE80211_SKB_RXCB(skb);
  598. mgmt = (struct ieee80211_mgmt *) skb->data;
  599. fc = le16_to_cpu(mgmt->frame_control);
  600. switch (fc & IEEE80211_FCTL_STYPE) {
  601. case IEEE80211_STYPE_PROBE_REQ:
  602. ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
  603. break;
  604. case IEEE80211_STYPE_PROBE_RESP:
  605. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
  606. rx_status);
  607. break;
  608. case IEEE80211_STYPE_BEACON:
  609. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  610. rx_status);
  611. break;
  612. case IEEE80211_STYPE_AUTH:
  613. ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
  614. break;
  615. }
  616. kfree_skb(skb);
  617. }
  618. static void ieee80211_ibss_work(struct work_struct *work)
  619. {
  620. struct ieee80211_sub_if_data *sdata =
  621. container_of(work, struct ieee80211_sub_if_data, u.ibss.work);
  622. struct ieee80211_local *local = sdata->local;
  623. struct ieee80211_if_ibss *ifibss;
  624. struct sk_buff *skb;
  625. if (WARN_ON(local->suspended))
  626. return;
  627. if (!ieee80211_sdata_running(sdata))
  628. return;
  629. if (local->scanning)
  630. return;
  631. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_ADHOC))
  632. return;
  633. ifibss = &sdata->u.ibss;
  634. while ((skb = skb_dequeue(&ifibss->skb_queue)))
  635. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  636. if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request))
  637. return;
  638. switch (ifibss->state) {
  639. case IEEE80211_IBSS_MLME_SEARCH:
  640. ieee80211_sta_find_ibss(sdata);
  641. break;
  642. case IEEE80211_IBSS_MLME_JOINED:
  643. ieee80211_sta_merge_ibss(sdata);
  644. break;
  645. default:
  646. WARN_ON(1);
  647. break;
  648. }
  649. }
  650. static void ieee80211_ibss_timer(unsigned long data)
  651. {
  652. struct ieee80211_sub_if_data *sdata =
  653. (struct ieee80211_sub_if_data *) data;
  654. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  655. struct ieee80211_local *local = sdata->local;
  656. if (local->quiescing) {
  657. ifibss->timer_running = true;
  658. return;
  659. }
  660. set_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request);
  661. ieee80211_queue_work(&local->hw, &ifibss->work);
  662. }
  663. #ifdef CONFIG_PM
  664. void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
  665. {
  666. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  667. cancel_work_sync(&ifibss->work);
  668. if (del_timer_sync(&ifibss->timer))
  669. ifibss->timer_running = true;
  670. }
  671. void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
  672. {
  673. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  674. if (ifibss->timer_running) {
  675. add_timer(&ifibss->timer);
  676. ifibss->timer_running = false;
  677. }
  678. }
  679. #endif
  680. void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
  681. {
  682. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  683. INIT_WORK(&ifibss->work, ieee80211_ibss_work);
  684. setup_timer(&ifibss->timer, ieee80211_ibss_timer,
  685. (unsigned long) sdata);
  686. skb_queue_head_init(&ifibss->skb_queue);
  687. }
  688. /* scan finished notification */
  689. void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
  690. {
  691. struct ieee80211_sub_if_data *sdata;
  692. mutex_lock(&local->iflist_mtx);
  693. list_for_each_entry(sdata, &local->interfaces, list) {
  694. if (!ieee80211_sdata_running(sdata))
  695. continue;
  696. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  697. continue;
  698. if (!sdata->u.ibss.ssid_len)
  699. continue;
  700. sdata->u.ibss.last_scan_completed = jiffies;
  701. mod_timer(&sdata->u.ibss.timer, 0);
  702. }
  703. mutex_unlock(&local->iflist_mtx);
  704. }
  705. ieee80211_rx_result
  706. ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  707. {
  708. struct ieee80211_local *local = sdata->local;
  709. struct ieee80211_mgmt *mgmt;
  710. u16 fc;
  711. if (skb->len < 24)
  712. return RX_DROP_MONITOR;
  713. mgmt = (struct ieee80211_mgmt *) skb->data;
  714. fc = le16_to_cpu(mgmt->frame_control);
  715. switch (fc & IEEE80211_FCTL_STYPE) {
  716. case IEEE80211_STYPE_PROBE_RESP:
  717. case IEEE80211_STYPE_BEACON:
  718. case IEEE80211_STYPE_PROBE_REQ:
  719. case IEEE80211_STYPE_AUTH:
  720. skb_queue_tail(&sdata->u.ibss.skb_queue, skb);
  721. ieee80211_queue_work(&local->hw, &sdata->u.ibss.work);
  722. return RX_QUEUED;
  723. }
  724. return RX_DROP_MONITOR;
  725. }
  726. int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
  727. struct cfg80211_ibss_params *params)
  728. {
  729. struct sk_buff *skb;
  730. if (params->bssid) {
  731. memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
  732. sdata->u.ibss.fixed_bssid = true;
  733. } else
  734. sdata->u.ibss.fixed_bssid = false;
  735. sdata->u.ibss.privacy = params->privacy;
  736. sdata->vif.bss_conf.beacon_int = params->beacon_interval;
  737. sdata->u.ibss.channel = params->channel;
  738. sdata->u.ibss.fixed_channel = params->channel_fixed;
  739. if (params->ie) {
  740. sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
  741. GFP_KERNEL);
  742. if (sdata->u.ibss.ie)
  743. sdata->u.ibss.ie_len = params->ie_len;
  744. }
  745. skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
  746. 36 /* bitrates */ +
  747. 34 /* SSID */ +
  748. 3 /* DS params */ +
  749. 4 /* IBSS params */ +
  750. params->ie_len);
  751. if (!skb)
  752. return -ENOMEM;
  753. sdata->u.ibss.skb = skb;
  754. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  755. sdata->u.ibss.ibss_join_req = jiffies;
  756. memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
  757. /*
  758. * The ssid_len setting below is used to see whether
  759. * we are active, and we need all other settings
  760. * before that may get visible.
  761. */
  762. mb();
  763. sdata->u.ibss.ssid_len = params->ssid_len;
  764. ieee80211_recalc_idle(sdata->local);
  765. set_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
  766. ieee80211_queue_work(&sdata->local->hw, &sdata->u.ibss.work);
  767. return 0;
  768. }
  769. int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
  770. {
  771. struct sk_buff *skb;
  772. del_timer_sync(&sdata->u.ibss.timer);
  773. clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
  774. cancel_work_sync(&sdata->u.ibss.work);
  775. clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
  776. sta_info_flush(sdata->local, sdata);
  777. /* remove beacon */
  778. kfree(sdata->u.ibss.ie);
  779. skb = sdata->u.ibss.presp;
  780. rcu_assign_pointer(sdata->u.ibss.presp, NULL);
  781. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
  782. synchronize_rcu();
  783. kfree_skb(skb);
  784. skb_queue_purge(&sdata->u.ibss.skb_queue);
  785. memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
  786. sdata->u.ibss.ssid_len = 0;
  787. ieee80211_recalc_idle(sdata->local);
  788. return 0;
  789. }