ibss.c 27 KB

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