ibss.c 26 KB

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