ibss.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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, sdata);
  71. skb = ifibss->skb;
  72. RCU_INIT_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. netif_carrier_off(sdata->dev);
  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_INIT_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. netif_carrier_on(sdata->dev);
  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. band);
  230. else
  231. freq = rx_status->freq;
  232. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  233. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  234. return;
  235. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  236. memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
  237. rcu_read_lock();
  238. sta = sta_info_get(sdata, mgmt->sa);
  239. if (elems->supp_rates) {
  240. supp_rates = ieee80211_sta_get_rates(local, elems,
  241. band);
  242. if (sta) {
  243. u32 prev_rates;
  244. prev_rates = sta->sta.supp_rates[band];
  245. /* make sure mandatory rates are always added */
  246. sta->sta.supp_rates[band] = supp_rates |
  247. ieee80211_mandatory_rates(local, band);
  248. if (sta->sta.supp_rates[band] != prev_rates) {
  249. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  250. printk(KERN_DEBUG
  251. "%s: updated supp_rates set "
  252. "for %pM based on beacon"
  253. "/probe_resp (0x%x -> 0x%x)\n",
  254. sdata->name, sta->sta.addr,
  255. prev_rates,
  256. sta->sta.supp_rates[band]);
  257. #endif
  258. rate_control_rate_init(sta);
  259. }
  260. } else
  261. sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
  262. mgmt->sa, supp_rates,
  263. GFP_ATOMIC);
  264. }
  265. if (sta && elems->wmm_info)
  266. set_sta_flag(sta, WLAN_STA_WME);
  267. rcu_read_unlock();
  268. }
  269. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  270. channel, beacon);
  271. if (!bss)
  272. return;
  273. cbss = container_of((void *)bss, struct cfg80211_bss, priv);
  274. /* was just updated in ieee80211_bss_info_update */
  275. beacon_timestamp = cbss->tsf;
  276. /* check if we need to merge IBSS */
  277. /* we use a fixed BSSID */
  278. if (sdata->u.ibss.fixed_bssid)
  279. goto put_bss;
  280. /* not an IBSS */
  281. if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
  282. goto put_bss;
  283. /* different channel */
  284. if (cbss->channel != local->oper_channel)
  285. goto put_bss;
  286. /* different SSID */
  287. if (elems->ssid_len != sdata->u.ibss.ssid_len ||
  288. memcmp(elems->ssid, sdata->u.ibss.ssid,
  289. sdata->u.ibss.ssid_len))
  290. goto put_bss;
  291. /* same BSSID */
  292. if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
  293. goto put_bss;
  294. if (rx_status->flag & RX_FLAG_MACTIME_MPDU) {
  295. /*
  296. * For correct IBSS merging we need mactime; since mactime is
  297. * defined as the time the first data symbol of the frame hits
  298. * the PHY, and the timestamp of the beacon is defined as "the
  299. * time that the data symbol containing the first bit of the
  300. * timestamp is transmitted to the PHY plus the transmitting
  301. * STA's delays through its local PHY from the MAC-PHY
  302. * interface to its interface with the WM" (802.11 11.1.2)
  303. * - equals the time this bit arrives at the receiver - we have
  304. * to take into account the offset between the two.
  305. *
  306. * E.g. at 1 MBit that means mactime is 192 usec earlier
  307. * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
  308. */
  309. int rate;
  310. if (rx_status->flag & RX_FLAG_HT)
  311. rate = 65; /* TODO: HT rates */
  312. else
  313. rate = local->hw.wiphy->bands[band]->
  314. bitrates[rx_status->rate_idx].bitrate;
  315. rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
  316. } else {
  317. /*
  318. * second best option: get current TSF
  319. * (will return -1 if not supported)
  320. */
  321. rx_timestamp = drv_get_tsf(local, sdata);
  322. }
  323. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  324. printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
  325. "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
  326. mgmt->sa, mgmt->bssid,
  327. (unsigned long long)rx_timestamp,
  328. (unsigned long long)beacon_timestamp,
  329. (unsigned long long)(rx_timestamp - beacon_timestamp),
  330. jiffies);
  331. #endif
  332. if (beacon_timestamp > rx_timestamp) {
  333. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  334. printk(KERN_DEBUG "%s: beacon TSF higher than "
  335. "local TSF - IBSS merge with BSSID %pM\n",
  336. sdata->name, mgmt->bssid);
  337. #endif
  338. ieee80211_sta_join_ibss(sdata, bss);
  339. supp_rates = ieee80211_sta_get_rates(local, elems, band);
  340. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
  341. supp_rates, GFP_KERNEL);
  342. }
  343. put_bss:
  344. ieee80211_rx_bss_put(local, bss);
  345. }
  346. /*
  347. * Add a new IBSS station, will also be called by the RX code when,
  348. * in IBSS mode, receiving a frame from a yet-unknown station, hence
  349. * must be callable in atomic context.
  350. */
  351. struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
  352. u8 *bssid, u8 *addr, u32 supp_rates,
  353. gfp_t gfp)
  354. {
  355. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  356. struct ieee80211_local *local = sdata->local;
  357. struct sta_info *sta;
  358. int band = local->hw.conf.channel->band;
  359. /*
  360. * XXX: Consider removing the least recently used entry and
  361. * allow new one to be added.
  362. */
  363. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  364. if (net_ratelimit())
  365. printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
  366. sdata->name, addr);
  367. return NULL;
  368. }
  369. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
  370. return NULL;
  371. if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
  372. return NULL;
  373. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  374. wiphy_debug(local->hw.wiphy, "Adding new IBSS station %pM (dev=%s)\n",
  375. addr, sdata->name);
  376. #endif
  377. sta = sta_info_alloc(sdata, addr, gfp);
  378. if (!sta)
  379. return NULL;
  380. sta->last_rx = jiffies;
  381. set_sta_flag(sta, WLAN_STA_AUTHORIZED);
  382. /* make sure mandatory rates are always added */
  383. sta->sta.supp_rates[band] = supp_rates |
  384. ieee80211_mandatory_rates(local, band);
  385. rate_control_rate_init(sta);
  386. /* If it fails, maybe we raced another insertion? */
  387. if (sta_info_insert(sta))
  388. return sta_info_get(sdata, addr);
  389. return sta;
  390. }
  391. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  392. {
  393. struct ieee80211_local *local = sdata->local;
  394. int active = 0;
  395. struct sta_info *sta;
  396. lockdep_assert_held(&sdata->u.ibss.mtx);
  397. rcu_read_lock();
  398. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  399. if (sta->sdata == sdata &&
  400. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  401. jiffies)) {
  402. active++;
  403. break;
  404. }
  405. }
  406. rcu_read_unlock();
  407. return active;
  408. }
  409. /*
  410. * This function is called with state == IEEE80211_IBSS_MLME_JOINED
  411. */
  412. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
  413. {
  414. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  415. lockdep_assert_held(&ifibss->mtx);
  416. mod_timer(&ifibss->timer,
  417. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  418. ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
  419. if (time_before(jiffies, ifibss->last_scan_completed +
  420. IEEE80211_IBSS_MERGE_INTERVAL))
  421. return;
  422. if (ieee80211_sta_active_ibss(sdata))
  423. return;
  424. if (ifibss->fixed_channel)
  425. return;
  426. printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
  427. "IBSS networks with same SSID (merge)\n", sdata->name);
  428. ieee80211_request_internal_scan(sdata,
  429. ifibss->ssid, ifibss->ssid_len,
  430. ifibss->fixed_channel ? ifibss->channel : NULL);
  431. }
  432. static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
  433. {
  434. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  435. u8 bssid[ETH_ALEN];
  436. u16 capability;
  437. int i;
  438. lockdep_assert_held(&ifibss->mtx);
  439. if (ifibss->fixed_bssid) {
  440. memcpy(bssid, ifibss->bssid, ETH_ALEN);
  441. } else {
  442. /* Generate random, not broadcast, locally administered BSSID. Mix in
  443. * own MAC address to make sure that devices that do not have proper
  444. * random number generator get different BSSID. */
  445. get_random_bytes(bssid, ETH_ALEN);
  446. for (i = 0; i < ETH_ALEN; i++)
  447. bssid[i] ^= sdata->vif.addr[i];
  448. bssid[0] &= ~0x01;
  449. bssid[0] |= 0x02;
  450. }
  451. printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
  452. sdata->name, bssid);
  453. capability = WLAN_CAPABILITY_IBSS;
  454. if (ifibss->privacy)
  455. capability |= WLAN_CAPABILITY_PRIVACY;
  456. else
  457. sdata->drop_unencrypted = 0;
  458. __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
  459. ifibss->channel, ifibss->basic_rates,
  460. capability, 0);
  461. }
  462. /*
  463. * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
  464. */
  465. static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
  466. {
  467. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  468. struct ieee80211_local *local = sdata->local;
  469. struct cfg80211_bss *cbss;
  470. struct ieee80211_channel *chan = NULL;
  471. const u8 *bssid = NULL;
  472. int active_ibss;
  473. u16 capability;
  474. lockdep_assert_held(&ifibss->mtx);
  475. active_ibss = ieee80211_sta_active_ibss(sdata);
  476. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  477. printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
  478. sdata->name, active_ibss);
  479. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  480. if (active_ibss)
  481. return;
  482. capability = WLAN_CAPABILITY_IBSS;
  483. if (ifibss->privacy)
  484. capability |= WLAN_CAPABILITY_PRIVACY;
  485. if (ifibss->fixed_bssid)
  486. bssid = ifibss->bssid;
  487. if (ifibss->fixed_channel)
  488. chan = ifibss->channel;
  489. if (!is_zero_ether_addr(ifibss->bssid))
  490. bssid = ifibss->bssid;
  491. cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
  492. ifibss->ssid, ifibss->ssid_len,
  493. WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
  494. capability);
  495. if (cbss) {
  496. struct ieee80211_bss *bss;
  497. bss = (void *)cbss->priv;
  498. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  499. printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
  500. "%pM\n", cbss->bssid, ifibss->bssid);
  501. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  502. printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
  503. " based on configured SSID\n",
  504. sdata->name, cbss->bssid);
  505. ieee80211_sta_join_ibss(sdata, bss);
  506. ieee80211_rx_bss_put(local, bss);
  507. return;
  508. }
  509. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  510. printk(KERN_DEBUG " did not try to join ibss\n");
  511. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  512. /* Selected IBSS not found in current scan results - try to scan */
  513. if (time_after(jiffies, ifibss->last_scan_completed +
  514. IEEE80211_SCAN_INTERVAL)) {
  515. printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
  516. "join\n", sdata->name);
  517. ieee80211_request_internal_scan(sdata,
  518. ifibss->ssid, ifibss->ssid_len,
  519. ifibss->fixed_channel ? ifibss->channel : NULL);
  520. } else {
  521. int interval = IEEE80211_SCAN_INTERVAL;
  522. if (time_after(jiffies, ifibss->ibss_join_req +
  523. IEEE80211_IBSS_JOIN_TIMEOUT)) {
  524. if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
  525. ieee80211_sta_create_ibss(sdata);
  526. return;
  527. }
  528. printk(KERN_DEBUG "%s: IBSS not allowed on"
  529. " %d MHz\n", sdata->name,
  530. local->hw.conf.channel->center_freq);
  531. /* No IBSS found - decrease scan interval and continue
  532. * scanning. */
  533. interval = IEEE80211_SCAN_INTERVAL_SLOW;
  534. }
  535. mod_timer(&ifibss->timer,
  536. round_jiffies(jiffies + interval));
  537. }
  538. }
  539. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  540. struct sk_buff *req)
  541. {
  542. struct ieee80211_mgmt *mgmt = (void *)req->data;
  543. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  544. struct ieee80211_local *local = sdata->local;
  545. int tx_last_beacon, len = req->len;
  546. struct sk_buff *skb;
  547. struct ieee80211_mgmt *resp;
  548. struct sk_buff *presp;
  549. u8 *pos, *end;
  550. lockdep_assert_held(&ifibss->mtx);
  551. presp = rcu_dereference_protected(ifibss->presp,
  552. lockdep_is_held(&ifibss->mtx));
  553. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  554. len < 24 + 2 || !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 && is_multicast_ether_addr(mgmt->da))
  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(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. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  793. memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
  794. sdata->u.ibss.ssid_len = 0;
  795. active_ibss = ieee80211_sta_active_ibss(sdata);
  796. if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
  797. capability = WLAN_CAPABILITY_IBSS;
  798. if (ifibss->privacy)
  799. capability |= WLAN_CAPABILITY_PRIVACY;
  800. cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
  801. ifibss->bssid, ifibss->ssid,
  802. ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
  803. WLAN_CAPABILITY_PRIVACY,
  804. capability);
  805. if (cbss) {
  806. cfg80211_unlink_bss(local->hw.wiphy, cbss);
  807. cfg80211_put_bss(cbss);
  808. }
  809. }
  810. sta_info_flush(sdata->local, sdata);
  811. netif_carrier_off(sdata->dev);
  812. /* remove beacon */
  813. kfree(sdata->u.ibss.ie);
  814. skb = rcu_dereference_protected(sdata->u.ibss.presp,
  815. lockdep_is_held(&sdata->u.ibss.mtx));
  816. RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
  817. sdata->vif.bss_conf.ibss_joined = false;
  818. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
  819. BSS_CHANGED_IBSS);
  820. synchronize_rcu();
  821. kfree_skb(skb);
  822. skb_queue_purge(&sdata->skb_queue);
  823. del_timer_sync(&sdata->u.ibss.timer);
  824. mutex_unlock(&sdata->u.ibss.mtx);
  825. mutex_lock(&local->mtx);
  826. ieee80211_recalc_idle(sdata->local);
  827. mutex_unlock(&local->mtx);
  828. return 0;
  829. }