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