ibss.c 27 KB

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