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_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. 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_flags(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_TSFT) {
  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);
  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. /* give slow hardware some time to do the TSF sync */
  333. if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
  334. goto put_bss;
  335. if (beacon_timestamp > rx_timestamp) {
  336. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  337. printk(KERN_DEBUG "%s: beacon TSF higher than "
  338. "local TSF - IBSS merge with BSSID %pM\n",
  339. sdata->name, mgmt->bssid);
  340. #endif
  341. ieee80211_sta_join_ibss(sdata, bss);
  342. supp_rates = ieee80211_sta_get_rates(local, elems, band);
  343. ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
  344. supp_rates, GFP_KERNEL);
  345. }
  346. put_bss:
  347. ieee80211_rx_bss_put(local, bss);
  348. }
  349. /*
  350. * Add a new IBSS station, will also be called by the RX code when,
  351. * in IBSS mode, receiving a frame from a yet-unknown station, hence
  352. * must be callable in atomic context.
  353. */
  354. struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
  355. u8 *bssid,u8 *addr, u32 supp_rates,
  356. gfp_t gfp)
  357. {
  358. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  359. struct ieee80211_local *local = sdata->local;
  360. struct sta_info *sta;
  361. int band = local->hw.conf.channel->band;
  362. /*
  363. * XXX: Consider removing the least recently used entry and
  364. * allow new one to be added.
  365. */
  366. if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
  367. if (net_ratelimit())
  368. printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
  369. sdata->name, addr);
  370. return NULL;
  371. }
  372. if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
  373. return NULL;
  374. if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
  375. return NULL;
  376. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  377. wiphy_debug(local->hw.wiphy, "Adding new IBSS station %pM (dev=%s)\n",
  378. addr, sdata->name);
  379. #endif
  380. sta = sta_info_alloc(sdata, addr, gfp);
  381. if (!sta)
  382. return NULL;
  383. sta->last_rx = jiffies;
  384. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  385. /* make sure mandatory rates are always added */
  386. sta->sta.supp_rates[band] = supp_rates |
  387. ieee80211_mandatory_rates(local, band);
  388. rate_control_rate_init(sta);
  389. /* If it fails, maybe we raced another insertion? */
  390. if (sta_info_insert(sta))
  391. return sta_info_get(sdata, addr);
  392. return sta;
  393. }
  394. static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
  395. {
  396. struct ieee80211_local *local = sdata->local;
  397. int active = 0;
  398. struct sta_info *sta;
  399. lockdep_assert_held(&sdata->u.ibss.mtx);
  400. rcu_read_lock();
  401. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  402. if (sta->sdata == sdata &&
  403. time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
  404. jiffies)) {
  405. active++;
  406. break;
  407. }
  408. }
  409. rcu_read_unlock();
  410. return active;
  411. }
  412. /*
  413. * This function is called with state == IEEE80211_IBSS_MLME_JOINED
  414. */
  415. static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
  416. {
  417. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  418. lockdep_assert_held(&ifibss->mtx);
  419. mod_timer(&ifibss->timer,
  420. round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
  421. ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
  422. if (time_before(jiffies, ifibss->last_scan_completed +
  423. IEEE80211_IBSS_MERGE_INTERVAL))
  424. return;
  425. if (ieee80211_sta_active_ibss(sdata))
  426. return;
  427. if (ifibss->fixed_channel)
  428. return;
  429. printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
  430. "IBSS networks with same SSID (merge)\n", sdata->name);
  431. ieee80211_request_internal_scan(sdata,
  432. ifibss->ssid, ifibss->ssid_len,
  433. ifibss->fixed_channel ? ifibss->channel : NULL);
  434. }
  435. static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
  436. {
  437. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  438. struct ieee80211_local *local = sdata->local;
  439. struct ieee80211_supported_band *sband;
  440. u8 bssid[ETH_ALEN];
  441. u16 capability;
  442. int i;
  443. lockdep_assert_held(&ifibss->mtx);
  444. if (ifibss->fixed_bssid) {
  445. memcpy(bssid, ifibss->bssid, ETH_ALEN);
  446. } else {
  447. /* Generate random, not broadcast, locally administered BSSID. Mix in
  448. * own MAC address to make sure that devices that do not have proper
  449. * random number generator get different BSSID. */
  450. get_random_bytes(bssid, ETH_ALEN);
  451. for (i = 0; i < ETH_ALEN; i++)
  452. bssid[i] ^= sdata->vif.addr[i];
  453. bssid[0] &= ~0x01;
  454. bssid[0] |= 0x02;
  455. }
  456. printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
  457. sdata->name, bssid);
  458. sband = local->hw.wiphy->bands[ifibss->channel->band];
  459. capability = WLAN_CAPABILITY_IBSS;
  460. if (ifibss->privacy)
  461. capability |= WLAN_CAPABILITY_PRIVACY;
  462. else
  463. sdata->drop_unencrypted = 0;
  464. __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
  465. ifibss->channel, ifibss->basic_rates,
  466. capability, 0);
  467. }
  468. /*
  469. * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
  470. */
  471. static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
  472. {
  473. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  474. struct ieee80211_local *local = sdata->local;
  475. struct cfg80211_bss *cbss;
  476. struct ieee80211_channel *chan = NULL;
  477. const u8 *bssid = NULL;
  478. int active_ibss;
  479. u16 capability;
  480. lockdep_assert_held(&ifibss->mtx);
  481. active_ibss = ieee80211_sta_active_ibss(sdata);
  482. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  483. printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
  484. sdata->name, active_ibss);
  485. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  486. if (active_ibss)
  487. return;
  488. capability = WLAN_CAPABILITY_IBSS;
  489. if (ifibss->privacy)
  490. capability |= WLAN_CAPABILITY_PRIVACY;
  491. if (ifibss->fixed_bssid)
  492. bssid = ifibss->bssid;
  493. if (ifibss->fixed_channel)
  494. chan = ifibss->channel;
  495. if (!is_zero_ether_addr(ifibss->bssid))
  496. bssid = ifibss->bssid;
  497. cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
  498. ifibss->ssid, ifibss->ssid_len,
  499. WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
  500. capability);
  501. if (cbss) {
  502. struct ieee80211_bss *bss;
  503. bss = (void *)cbss->priv;
  504. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  505. printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
  506. "%pM\n", cbss->bssid, ifibss->bssid);
  507. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  508. printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
  509. " based on configured SSID\n",
  510. sdata->name, cbss->bssid);
  511. ieee80211_sta_join_ibss(sdata, bss);
  512. ieee80211_rx_bss_put(local, bss);
  513. return;
  514. }
  515. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  516. printk(KERN_DEBUG " did not try to join ibss\n");
  517. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  518. /* Selected IBSS not found in current scan results - try to scan */
  519. if (time_after(jiffies, ifibss->last_scan_completed +
  520. IEEE80211_SCAN_INTERVAL)) {
  521. printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
  522. "join\n", sdata->name);
  523. ieee80211_request_internal_scan(sdata,
  524. ifibss->ssid, ifibss->ssid_len,
  525. ifibss->fixed_channel ? ifibss->channel : NULL);
  526. } else {
  527. int interval = IEEE80211_SCAN_INTERVAL;
  528. if (time_after(jiffies, ifibss->ibss_join_req +
  529. IEEE80211_IBSS_JOIN_TIMEOUT)) {
  530. if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
  531. ieee80211_sta_create_ibss(sdata);
  532. return;
  533. }
  534. printk(KERN_DEBUG "%s: IBSS not allowed on"
  535. " %d MHz\n", sdata->name,
  536. local->hw.conf.channel->center_freq);
  537. /* No IBSS found - decrease scan interval and continue
  538. * scanning. */
  539. interval = IEEE80211_SCAN_INTERVAL_SLOW;
  540. }
  541. mod_timer(&ifibss->timer,
  542. round_jiffies(jiffies + interval));
  543. }
  544. }
  545. static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
  546. struct ieee80211_mgmt *mgmt,
  547. size_t len)
  548. {
  549. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  550. struct ieee80211_local *local = sdata->local;
  551. int tx_last_beacon;
  552. struct sk_buff *skb;
  553. struct ieee80211_mgmt *resp;
  554. u8 *pos, *end;
  555. lockdep_assert_held(&ifibss->mtx);
  556. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  557. len < 24 + 2 || !ifibss->presp)
  558. return;
  559. tx_last_beacon = drv_tx_last_beacon(local);
  560. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  561. printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
  562. " (tx_last_beacon=%d)\n",
  563. sdata->name, mgmt->sa, mgmt->da,
  564. mgmt->bssid, tx_last_beacon);
  565. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  566. if (!tx_last_beacon)
  567. return;
  568. if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
  569. memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  570. return;
  571. end = ((u8 *) mgmt) + len;
  572. pos = mgmt->u.probe_req.variable;
  573. if (pos[0] != WLAN_EID_SSID ||
  574. pos + 2 + pos[1] > end) {
  575. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  576. printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
  577. "from %pM\n",
  578. sdata->name, mgmt->sa);
  579. #endif
  580. return;
  581. }
  582. if (pos[1] != 0 &&
  583. (pos[1] != ifibss->ssid_len ||
  584. memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
  585. /* Ignore ProbeReq for foreign SSID */
  586. return;
  587. }
  588. /* Reply with ProbeResp */
  589. skb = skb_copy(ifibss->presp, GFP_KERNEL);
  590. if (!skb)
  591. return;
  592. resp = (struct ieee80211_mgmt *) skb->data;
  593. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  594. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  595. printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
  596. sdata->name, resp->da);
  597. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  598. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  599. ieee80211_tx_skb(sdata, skb);
  600. }
  601. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  602. struct ieee80211_mgmt *mgmt,
  603. size_t len,
  604. struct ieee80211_rx_status *rx_status)
  605. {
  606. size_t baselen;
  607. struct ieee802_11_elems elems;
  608. if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
  609. return; /* ignore ProbeResp to foreign address */
  610. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  611. if (baselen > len)
  612. return;
  613. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  614. &elems);
  615. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  616. }
  617. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  618. struct ieee80211_mgmt *mgmt,
  619. size_t len,
  620. struct ieee80211_rx_status *rx_status)
  621. {
  622. size_t baselen;
  623. struct ieee802_11_elems elems;
  624. /* Process beacon from the current BSS */
  625. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  626. if (baselen > len)
  627. return;
  628. ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
  629. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
  630. }
  631. void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  632. struct sk_buff *skb)
  633. {
  634. struct ieee80211_rx_status *rx_status;
  635. struct ieee80211_mgmt *mgmt;
  636. u16 fc;
  637. rx_status = IEEE80211_SKB_RXCB(skb);
  638. mgmt = (struct ieee80211_mgmt *) skb->data;
  639. fc = le16_to_cpu(mgmt->frame_control);
  640. mutex_lock(&sdata->u.ibss.mtx);
  641. if (!sdata->u.ibss.ssid_len)
  642. goto mgmt_out; /* not ready to merge yet */
  643. switch (fc & IEEE80211_FCTL_STYPE) {
  644. case IEEE80211_STYPE_PROBE_REQ:
  645. ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
  646. break;
  647. case IEEE80211_STYPE_PROBE_RESP:
  648. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
  649. rx_status);
  650. break;
  651. case IEEE80211_STYPE_BEACON:
  652. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  653. rx_status);
  654. break;
  655. case IEEE80211_STYPE_AUTH:
  656. ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
  657. break;
  658. }
  659. mgmt_out:
  660. mutex_unlock(&sdata->u.ibss.mtx);
  661. }
  662. void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
  663. {
  664. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  665. mutex_lock(&ifibss->mtx);
  666. /*
  667. * Work could be scheduled after scan or similar
  668. * when we aren't even joined (or trying) with a
  669. * network.
  670. */
  671. if (!ifibss->ssid_len)
  672. goto out;
  673. switch (ifibss->state) {
  674. case IEEE80211_IBSS_MLME_SEARCH:
  675. ieee80211_sta_find_ibss(sdata);
  676. break;
  677. case IEEE80211_IBSS_MLME_JOINED:
  678. ieee80211_sta_merge_ibss(sdata);
  679. break;
  680. default:
  681. WARN_ON(1);
  682. break;
  683. }
  684. out:
  685. mutex_unlock(&ifibss->mtx);
  686. }
  687. static void ieee80211_ibss_timer(unsigned long data)
  688. {
  689. struct ieee80211_sub_if_data *sdata =
  690. (struct ieee80211_sub_if_data *) data;
  691. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  692. struct ieee80211_local *local = sdata->local;
  693. if (local->quiescing) {
  694. ifibss->timer_running = true;
  695. return;
  696. }
  697. ieee80211_queue_work(&local->hw, &sdata->work);
  698. }
  699. #ifdef CONFIG_PM
  700. void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
  701. {
  702. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  703. if (del_timer_sync(&ifibss->timer))
  704. ifibss->timer_running = true;
  705. }
  706. void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
  707. {
  708. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  709. if (ifibss->timer_running) {
  710. add_timer(&ifibss->timer);
  711. ifibss->timer_running = false;
  712. }
  713. }
  714. #endif
  715. void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
  716. {
  717. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  718. setup_timer(&ifibss->timer, ieee80211_ibss_timer,
  719. (unsigned long) sdata);
  720. mutex_init(&ifibss->mtx);
  721. }
  722. /* scan finished notification */
  723. void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
  724. {
  725. struct ieee80211_sub_if_data *sdata;
  726. mutex_lock(&local->iflist_mtx);
  727. list_for_each_entry(sdata, &local->interfaces, list) {
  728. if (!ieee80211_sdata_running(sdata))
  729. continue;
  730. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  731. continue;
  732. sdata->u.ibss.last_scan_completed = jiffies;
  733. ieee80211_queue_work(&local->hw, &sdata->work);
  734. }
  735. mutex_unlock(&local->iflist_mtx);
  736. }
  737. int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
  738. struct cfg80211_ibss_params *params)
  739. {
  740. struct sk_buff *skb;
  741. skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
  742. 36 /* bitrates */ +
  743. 34 /* SSID */ +
  744. 3 /* DS params */ +
  745. 4 /* IBSS params */ +
  746. params->ie_len);
  747. if (!skb)
  748. return -ENOMEM;
  749. mutex_lock(&sdata->u.ibss.mtx);
  750. if (params->bssid) {
  751. memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
  752. sdata->u.ibss.fixed_bssid = true;
  753. } else
  754. sdata->u.ibss.fixed_bssid = false;
  755. sdata->u.ibss.privacy = params->privacy;
  756. sdata->u.ibss.basic_rates = params->basic_rates;
  757. memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
  758. sizeof(params->mcast_rate));
  759. sdata->vif.bss_conf.beacon_int = params->beacon_interval;
  760. sdata->u.ibss.channel = params->channel;
  761. sdata->u.ibss.fixed_channel = params->channel_fixed;
  762. /* fix ourselves to that channel now already */
  763. if (params->channel_fixed) {
  764. sdata->local->oper_channel = params->channel;
  765. WARN_ON(!ieee80211_set_channel_type(sdata->local, sdata,
  766. NL80211_CHAN_NO_HT));
  767. }
  768. if (params->ie) {
  769. sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
  770. GFP_KERNEL);
  771. if (sdata->u.ibss.ie)
  772. sdata->u.ibss.ie_len = params->ie_len;
  773. }
  774. sdata->u.ibss.skb = skb;
  775. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  776. sdata->u.ibss.ibss_join_req = jiffies;
  777. memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
  778. sdata->u.ibss.ssid_len = params->ssid_len;
  779. mutex_unlock(&sdata->u.ibss.mtx);
  780. mutex_lock(&sdata->local->mtx);
  781. ieee80211_recalc_idle(sdata->local);
  782. mutex_unlock(&sdata->local->mtx);
  783. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  784. return 0;
  785. }
  786. int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
  787. {
  788. struct sk_buff *skb;
  789. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  790. struct ieee80211_local *local = sdata->local;
  791. struct cfg80211_bss *cbss;
  792. u16 capability;
  793. int active_ibss;
  794. mutex_lock(&sdata->u.ibss.mtx);
  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. /* remove beacon */
  812. kfree(sdata->u.ibss.ie);
  813. skb = sdata->u.ibss.presp;
  814. rcu_assign_pointer(sdata->u.ibss.presp, NULL);
  815. sdata->vif.bss_conf.ibss_joined = false;
  816. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
  817. BSS_CHANGED_IBSS);
  818. synchronize_rcu();
  819. kfree_skb(skb);
  820. skb_queue_purge(&sdata->skb_queue);
  821. memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
  822. sdata->u.ibss.ssid_len = 0;
  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. }