ibss.c 25 KB

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