ibss.c 25 KB

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