ibss.c 25 KB

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