ibss.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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 void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  52. const u8 *bssid, const int beacon_int,
  53. struct ieee80211_channel *chan,
  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 rates, i, j;
  61. struct sk_buff *skb;
  62. struct ieee80211_mgmt *mgmt;
  63. u8 *pos;
  64. struct ieee80211_supported_band *sband;
  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. skb = ifibss->skb;
  70. rcu_assign_pointer(ifibss->presp, NULL);
  71. synchronize_rcu();
  72. skb->data = skb->head;
  73. skb->len = 0;
  74. skb_reset_tail_pointer(skb);
  75. skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
  76. if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
  77. sta_info_flush(sdata->local, sdata);
  78. memcpy(ifibss->bssid, bssid, ETH_ALEN);
  79. local->hw.conf.beacon_int = beacon_int >= 10 ? beacon_int : 10;
  80. sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
  81. ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
  82. local->oper_channel = chan;
  83. local->oper_channel_type = NL80211_CHAN_NO_HT;
  84. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  85. sband = local->hw.wiphy->bands[chan->band];
  86. /* Build IBSS probe response */
  87. mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
  88. memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
  89. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  90. IEEE80211_STYPE_PROBE_RESP);
  91. memset(mgmt->da, 0xff, ETH_ALEN);
  92. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  93. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  94. mgmt->u.beacon.beacon_int = cpu_to_le16(local->hw.conf.beacon_int);
  95. mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
  96. mgmt->u.beacon.capab_info = cpu_to_le16(capability);
  97. pos = skb_put(skb, 2 + ifibss->ssid_len);
  98. *pos++ = WLAN_EID_SSID;
  99. *pos++ = ifibss->ssid_len;
  100. memcpy(pos, ifibss->ssid, ifibss->ssid_len);
  101. rates = supp_rates_len;
  102. if (rates > 8)
  103. rates = 8;
  104. pos = skb_put(skb, 2 + rates);
  105. *pos++ = WLAN_EID_SUPP_RATES;
  106. *pos++ = rates;
  107. memcpy(pos, supp_rates, rates);
  108. if (sband->band == IEEE80211_BAND_2GHZ) {
  109. pos = skb_put(skb, 2 + 1);
  110. *pos++ = WLAN_EID_DS_PARAMS;
  111. *pos++ = 1;
  112. *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
  113. }
  114. pos = skb_put(skb, 2 + 2);
  115. *pos++ = WLAN_EID_IBSS_PARAMS;
  116. *pos++ = 2;
  117. /* FIX: set ATIM window based on scan results */
  118. *pos++ = 0;
  119. *pos++ = 0;
  120. if (supp_rates_len > 8) {
  121. rates = supp_rates_len - 8;
  122. pos = skb_put(skb, 2 + rates);
  123. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  124. *pos++ = rates;
  125. memcpy(pos, &supp_rates[8], rates);
  126. }
  127. if (ifibss->ie_len)
  128. memcpy(skb_put(skb, ifibss->ie_len),
  129. ifibss->ie, ifibss->ie_len);
  130. rcu_assign_pointer(ifibss->presp, skb);
  131. ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON |
  132. IEEE80211_IFCC_BEACON_ENABLED);
  133. rates = 0;
  134. for (i = 0; i < supp_rates_len; i++) {
  135. int bitrate = (supp_rates[i] & 0x7f) * 5;
  136. for (j = 0; j < sband->n_bitrates; j++)
  137. if (sband->bitrates[j].bitrate == bitrate)
  138. rates |= BIT(j);
  139. }
  140. ieee80211_sta_def_wmm_params(sdata, supp_rates_len, supp_rates);
  141. ifibss->state = IEEE80211_IBSS_MLME_JOINED;
  142. mod_timer(&ifibss->timer,
  143. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  144. cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
  145. mgmt, skb->len, 0, GFP_KERNEL);
  146. cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
  147. }
  148. static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
  149. struct ieee80211_bss *bss)
  150. {
  151. __ieee80211_sta_join_ibss(sdata, bss->cbss.bssid,
  152. bss->cbss.beacon_interval,
  153. bss->cbss.channel,
  154. bss->supp_rates_len, bss->supp_rates,
  155. bss->cbss.capability,
  156. bss->cbss.tsf);
  157. }
  158. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  159. struct ieee80211_mgmt *mgmt,
  160. size_t len,
  161. struct ieee80211_rx_status *rx_status,
  162. struct ieee802_11_elems *elems,
  163. bool beacon)
  164. {
  165. struct ieee80211_local *local = sdata->local;
  166. int freq;
  167. struct ieee80211_bss *bss;
  168. struct sta_info *sta;
  169. struct ieee80211_channel *channel;
  170. u64 beacon_timestamp, rx_timestamp;
  171. u32 supp_rates = 0;
  172. enum ieee80211_band band = rx_status->band;
  173. if (elems->ds_params && elems->ds_params_len == 1)
  174. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  175. else
  176. freq = rx_status->freq;
  177. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  178. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  179. return;
  180. if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
  181. memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
  182. supp_rates = ieee80211_sta_get_rates(local, elems, band);
  183. rcu_read_lock();
  184. sta = sta_info_get(local, mgmt->sa);
  185. if (sta) {
  186. u32 prev_rates;
  187. prev_rates = sta->sta.supp_rates[band];
  188. /* make sure mandatory rates are always added */
  189. sta->sta.supp_rates[band] = supp_rates |
  190. ieee80211_mandatory_rates(local, band);
  191. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  192. if (sta->sta.supp_rates[band] != prev_rates)
  193. printk(KERN_DEBUG "%s: updated supp_rates set "
  194. "for %pM based on beacon info (0x%llx | "
  195. "0x%llx -> 0x%llx)\n",
  196. sdata->dev->name,
  197. sta->sta.addr,
  198. (unsigned long long) prev_rates,
  199. (unsigned long long) supp_rates,
  200. (unsigned long long) sta->sta.supp_rates[band]);
  201. #endif
  202. } else
  203. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
  204. rcu_read_unlock();
  205. }
  206. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  207. channel, beacon);
  208. if (!bss)
  209. return;
  210. /* was just updated in ieee80211_bss_info_update */
  211. beacon_timestamp = bss->cbss.tsf;
  212. /* check if we need to merge IBSS */
  213. /* merge only on beacons (???) */
  214. if (!beacon)
  215. goto put_bss;
  216. /* we use a fixed BSSID */
  217. if (sdata->u.ibss.bssid)
  218. goto put_bss;
  219. /* not an IBSS */
  220. if (!(bss->cbss.capability & WLAN_CAPABILITY_IBSS))
  221. goto put_bss;
  222. /* different channel */
  223. if (bss->cbss.channel != local->oper_channel)
  224. goto put_bss;
  225. /* different SSID */
  226. if (elems->ssid_len != sdata->u.ibss.ssid_len ||
  227. memcmp(elems->ssid, sdata->u.ibss.ssid,
  228. sdata->u.ibss.ssid_len))
  229. goto put_bss;
  230. /* same BSSID */
  231. if (memcmp(bss->cbss.bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
  232. goto put_bss;
  233. if (rx_status->flag & RX_FLAG_TSFT) {
  234. /*
  235. * For correct IBSS merging we need mactime; since mactime is
  236. * defined as the time the first data symbol of the frame hits
  237. * the PHY, and the timestamp of the beacon is defined as "the
  238. * time that the data symbol containing the first bit of the
  239. * timestamp is transmitted to the PHY plus the transmitting
  240. * STA's delays through its local PHY from the MAC-PHY
  241. * interface to its interface with the WM" (802.11 11.1.2)
  242. * - equals the time this bit arrives at the receiver - we have
  243. * to take into account the offset between the two.
  244. *
  245. * E.g. at 1 MBit that means mactime is 192 usec earlier
  246. * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
  247. */
  248. int rate;
  249. if (rx_status->flag & RX_FLAG_HT)
  250. rate = 65; /* TODO: HT rates */
  251. else
  252. rate = local->hw.wiphy->bands[band]->
  253. bitrates[rx_status->rate_idx].bitrate;
  254. rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
  255. } else if (local && local->ops && local->ops->get_tsf)
  256. /* second best option: get current TSF */
  257. rx_timestamp = local->ops->get_tsf(local_to_hw(local));
  258. else
  259. /* can't merge without knowing the TSF */
  260. rx_timestamp = -1LLU;
  261. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  262. printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
  263. "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  264. mgmt->sa, mgmt->bssid,
  265. (unsigned long long)rx_timestamp,
  266. (unsigned long long)beacon_timestamp,
  267. (unsigned long long)(rx_timestamp - beacon_timestamp),
  268. jiffies);
  269. #endif
  270. /* give slow hardware some time to do the TSF sync */
  271. if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
  272. goto put_bss;
  273. if (beacon_timestamp > rx_timestamp) {
  274. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  275. printk(KERN_DEBUG "%s: beacon TSF higher than "
  276. "local TSF - IBSS merge with BSSID %pM\n",
  277. sdata->dev->name, mgmt->bssid);
  278. #endif
  279. ieee80211_sta_join_ibss(sdata, bss);
  280. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, supp_rates);
  281. }
  282. put_bss:
  283. ieee80211_rx_bss_put(local, bss);
  284. }
  285. /*
  286. * Add a new IBSS station, will also be called by the RX code when,
  287. * in IBSS mode, receiving a frame from a yet-unknown station, hence
  288. * must be callable in atomic context.
  289. */
  290. struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
  291. u8 *bssid,u8 *addr, u32 supp_rates)
  292. {
  293. struct ieee80211_local *local = sdata->local;
  294. struct sta_info *sta;
  295. int band = local->hw.conf.channel->band;
  296. /*
  297. * XXX: Consider removing the least recently used entry and
  298. * allow new one to be added.
  299. */
  300. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  301. if (net_ratelimit())
  302. printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
  303. sdata->dev->name, addr);
  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,
  345. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  346. ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
  347. if (ieee80211_sta_active_ibss(sdata))
  348. return;
  349. if (ifibss->fixed_channel)
  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 void 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->fixed_bssid) {
  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[ifibss->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. __ieee80211_sta_join_ibss(sdata, bssid, local->hw.conf.beacon_int,
  399. ifibss->channel, sband->n_bitrates,
  400. supp_rates, capability, 0);
  401. }
  402. static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
  403. {
  404. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  405. struct ieee80211_local *local = sdata->local;
  406. struct ieee80211_bss *bss;
  407. struct ieee80211_channel *chan = NULL;
  408. const u8 *bssid = NULL;
  409. int active_ibss;
  410. active_ibss = ieee80211_sta_active_ibss(sdata);
  411. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  412. printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
  413. sdata->dev->name, active_ibss);
  414. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  415. if (active_ibss)
  416. return;
  417. if (ifibss->fixed_bssid)
  418. bssid = ifibss->bssid;
  419. if (ifibss->fixed_channel)
  420. chan = ifibss->channel;
  421. if (!is_zero_ether_addr(ifibss->bssid))
  422. bssid = ifibss->bssid;
  423. bss = (void *)cfg80211_get_bss(local->hw.wiphy, chan, bssid,
  424. ifibss->ssid, ifibss->ssid_len,
  425. WLAN_CAPABILITY_IBSS,
  426. WLAN_CAPABILITY_IBSS);
  427. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  428. if (bss)
  429. printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
  430. "%pM\n", bss->cbss.bssid, ifibss->bssid);
  431. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  432. if (bss && memcmp(ifibss->bssid, bss->cbss.bssid, ETH_ALEN)) {
  433. printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
  434. " based on configured SSID\n",
  435. sdata->dev->name, bss->cbss.bssid);
  436. ieee80211_sta_join_ibss(sdata, bss);
  437. ieee80211_rx_bss_put(local, bss);
  438. return;
  439. } else if (bss)
  440. ieee80211_rx_bss_put(local, bss);
  441. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  442. printk(KERN_DEBUG " did not try to join ibss\n");
  443. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  444. /* Selected IBSS not found in current scan results - try to scan */
  445. if (ifibss->state == IEEE80211_IBSS_MLME_JOINED &&
  446. !ieee80211_sta_active_ibss(sdata)) {
  447. mod_timer(&ifibss->timer,
  448. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  449. } else if (time_after(jiffies, ifibss->last_scan_completed +
  450. IEEE80211_SCAN_INTERVAL)) {
  451. printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
  452. "join\n", sdata->dev->name);
  453. /* XXX maybe racy? */
  454. if (local->scan_req)
  455. return;
  456. memcpy(local->int_scan_req.ssids[0].ssid,
  457. ifibss->ssid, IEEE80211_MAX_SSID_LEN);
  458. local->int_scan_req.ssids[0].ssid_len =
  459. ifibss->ssid_len;
  460. ieee80211_request_scan(sdata, &local->int_scan_req);
  461. } else if (ifibss->state != IEEE80211_IBSS_MLME_JOINED) {
  462. int interval = IEEE80211_SCAN_INTERVAL;
  463. if (time_after(jiffies, ifibss->ibss_join_req +
  464. IEEE80211_IBSS_JOIN_TIMEOUT)) {
  465. if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
  466. ieee80211_sta_create_ibss(sdata);
  467. return;
  468. }
  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,
  478. round_jiffies(jiffies + interval));
  479. }
  480. }
  481. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  482. struct ieee80211_mgmt *mgmt,
  483. size_t len)
  484. {
  485. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  486. struct ieee80211_local *local = sdata->local;
  487. int tx_last_beacon;
  488. struct sk_buff *skb;
  489. struct ieee80211_mgmt *resp;
  490. u8 *pos, *end;
  491. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  492. len < 24 + 2 || !ifibss->presp)
  493. return;
  494. if (local->ops->tx_last_beacon)
  495. tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
  496. else
  497. tx_last_beacon = 1;
  498. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  499. printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
  500. " (tx_last_beacon=%d)\n",
  501. sdata->dev->name, mgmt->sa, mgmt->da,
  502. mgmt->bssid, tx_last_beacon);
  503. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  504. if (!tx_last_beacon)
  505. return;
  506. if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
  507. memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  508. return;
  509. end = ((u8 *) mgmt) + len;
  510. pos = mgmt->u.probe_req.variable;
  511. if (pos[0] != WLAN_EID_SSID ||
  512. pos + 2 + pos[1] > end) {
  513. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  514. printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
  515. "from %pM\n",
  516. sdata->dev->name, mgmt->sa);
  517. #endif
  518. return;
  519. }
  520. if (pos[1] != 0 &&
  521. (pos[1] != ifibss->ssid_len ||
  522. !memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
  523. /* Ignore ProbeReq for foreign SSID */
  524. return;
  525. }
  526. /* Reply with ProbeResp */
  527. skb = skb_copy(ifibss->presp, GFP_KERNEL);
  528. if (!skb)
  529. return;
  530. resp = (struct ieee80211_mgmt *) skb->data;
  531. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  532. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  533. printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
  534. sdata->dev->name, resp->da);
  535. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  536. ieee80211_tx_skb(sdata, skb, 0);
  537. }
  538. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  539. struct ieee80211_mgmt *mgmt,
  540. size_t len,
  541. struct ieee80211_rx_status *rx_status)
  542. {
  543. size_t baselen;
  544. struct ieee802_11_elems elems;
  545. if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
  546. return; /* ignore ProbeResp to foreign address */
  547. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  548. if (baselen > len)
  549. return;
  550. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  551. &elems);
  552. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  553. }
  554. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  555. struct ieee80211_mgmt *mgmt,
  556. size_t len,
  557. struct ieee80211_rx_status *rx_status)
  558. {
  559. size_t baselen;
  560. struct ieee802_11_elems elems;
  561. /* Process beacon from the current BSS */
  562. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  563. if (baselen > len)
  564. return;
  565. ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
  566. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
  567. }
  568. static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  569. struct sk_buff *skb)
  570. {
  571. struct ieee80211_rx_status *rx_status;
  572. struct ieee80211_mgmt *mgmt;
  573. u16 fc;
  574. rx_status = (struct ieee80211_rx_status *) skb->cb;
  575. mgmt = (struct ieee80211_mgmt *) skb->data;
  576. fc = le16_to_cpu(mgmt->frame_control);
  577. switch (fc & IEEE80211_FCTL_STYPE) {
  578. case IEEE80211_STYPE_PROBE_REQ:
  579. ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
  580. break;
  581. case IEEE80211_STYPE_PROBE_RESP:
  582. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
  583. rx_status);
  584. break;
  585. case IEEE80211_STYPE_BEACON:
  586. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  587. rx_status);
  588. break;
  589. case IEEE80211_STYPE_AUTH:
  590. ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
  591. break;
  592. }
  593. kfree_skb(skb);
  594. }
  595. static void ieee80211_ibss_work(struct work_struct *work)
  596. {
  597. struct ieee80211_sub_if_data *sdata =
  598. container_of(work, struct ieee80211_sub_if_data, u.ibss.work);
  599. struct ieee80211_local *local = sdata->local;
  600. struct ieee80211_if_ibss *ifibss;
  601. struct sk_buff *skb;
  602. if (!netif_running(sdata->dev))
  603. return;
  604. if (local->sw_scanning || local->hw_scanning)
  605. return;
  606. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_ADHOC))
  607. return;
  608. ifibss = &sdata->u.ibss;
  609. while ((skb = skb_dequeue(&ifibss->skb_queue)))
  610. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  611. if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request))
  612. return;
  613. switch (ifibss->state) {
  614. case IEEE80211_IBSS_MLME_SEARCH:
  615. ieee80211_sta_find_ibss(sdata);
  616. break;
  617. case IEEE80211_IBSS_MLME_JOINED:
  618. ieee80211_sta_merge_ibss(sdata);
  619. break;
  620. default:
  621. WARN_ON(1);
  622. break;
  623. }
  624. }
  625. static void ieee80211_ibss_timer(unsigned long data)
  626. {
  627. struct ieee80211_sub_if_data *sdata =
  628. (struct ieee80211_sub_if_data *) data;
  629. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  630. struct ieee80211_local *local = sdata->local;
  631. set_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request);
  632. queue_work(local->hw.workqueue, &ifibss->work);
  633. }
  634. void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
  635. {
  636. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  637. INIT_WORK(&ifibss->work, ieee80211_ibss_work);
  638. setup_timer(&ifibss->timer, ieee80211_ibss_timer,
  639. (unsigned long) sdata);
  640. skb_queue_head_init(&ifibss->skb_queue);
  641. }
  642. /* scan finished notification */
  643. void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
  644. {
  645. struct ieee80211_sub_if_data *sdata;
  646. mutex_lock(&local->iflist_mtx);
  647. list_for_each_entry(sdata, &local->interfaces, list) {
  648. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  649. continue;
  650. sdata->u.ibss.last_scan_completed = jiffies;
  651. ieee80211_sta_find_ibss(sdata);
  652. }
  653. mutex_unlock(&local->iflist_mtx);
  654. }
  655. ieee80211_rx_result
  656. ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  657. struct ieee80211_rx_status *rx_status)
  658. {
  659. struct ieee80211_local *local = sdata->local;
  660. struct ieee80211_mgmt *mgmt;
  661. u16 fc;
  662. if (skb->len < 24)
  663. return RX_DROP_MONITOR;
  664. mgmt = (struct ieee80211_mgmt *) skb->data;
  665. fc = le16_to_cpu(mgmt->frame_control);
  666. switch (fc & IEEE80211_FCTL_STYPE) {
  667. case IEEE80211_STYPE_PROBE_RESP:
  668. case IEEE80211_STYPE_BEACON:
  669. memcpy(skb->cb, rx_status, sizeof(*rx_status));
  670. case IEEE80211_STYPE_PROBE_REQ:
  671. case IEEE80211_STYPE_AUTH:
  672. skb_queue_tail(&sdata->u.ibss.skb_queue, skb);
  673. queue_work(local->hw.workqueue, &sdata->u.ibss.work);
  674. return RX_QUEUED;
  675. }
  676. return RX_DROP_MONITOR;
  677. }
  678. int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
  679. struct cfg80211_ibss_params *params)
  680. {
  681. struct sk_buff *skb;
  682. memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
  683. sdata->u.ibss.ssid_len = params->ssid_len;
  684. if (params->bssid) {
  685. memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
  686. sdata->u.ibss.fixed_bssid = true;
  687. } else
  688. sdata->u.ibss.fixed_bssid = false;
  689. sdata->u.ibss.channel = params->channel;
  690. sdata->u.ibss.fixed_channel = params->channel_fixed;
  691. if (params->ie) {
  692. sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
  693. GFP_KERNEL);
  694. if (sdata->u.ibss.ie)
  695. sdata->u.ibss.ie_len = params->ie_len;
  696. }
  697. skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
  698. 36 /* bitrates */ +
  699. 34 /* SSID */ +
  700. 3 /* DS params */ +
  701. 4 /* IBSS params */ +
  702. params->ie_len);
  703. if (!skb)
  704. return -ENOMEM;
  705. sdata->u.ibss.skb = skb;
  706. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  707. sdata->u.ibss.ibss_join_req = jiffies;
  708. set_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
  709. queue_work(sdata->local->hw.workqueue, &sdata->u.ibss.work);
  710. return 0;
  711. }
  712. int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
  713. {
  714. struct sk_buff *skb;
  715. del_timer_sync(&sdata->u.ibss.timer);
  716. clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
  717. cancel_work_sync(&sdata->u.ibss.work);
  718. clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
  719. sta_info_flush(sdata->local, sdata);
  720. /* remove beacon */
  721. kfree(sdata->u.ibss.ie);
  722. skb = sdata->u.ibss.presp;
  723. rcu_assign_pointer(sdata->u.ibss.presp, NULL);
  724. ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON_ENABLED);
  725. synchronize_rcu();
  726. kfree_skb(skb);
  727. skb_queue_purge(&sdata->u.ibss.skb_queue);
  728. memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
  729. return 0;
  730. }