ibss.c 27 KB

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