ibss.c 25 KB

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