ibss.c 27 KB

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