ibss.c 26 KB

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