ibss.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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 sk_buff *req)
  547. {
  548. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(req);
  549. struct ieee80211_mgmt *mgmt = (void *)req->data;
  550. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  551. struct ieee80211_local *local = sdata->local;
  552. int tx_last_beacon, len = req->len;
  553. struct sk_buff *skb;
  554. struct ieee80211_mgmt *resp;
  555. u8 *pos, *end;
  556. lockdep_assert_held(&ifibss->mtx);
  557. if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
  558. len < 24 + 2 || !ifibss->presp)
  559. return;
  560. tx_last_beacon = drv_tx_last_beacon(local);
  561. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  562. printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
  563. " (tx_last_beacon=%d)\n",
  564. sdata->name, mgmt->sa, mgmt->da,
  565. mgmt->bssid, tx_last_beacon);
  566. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  567. if (!tx_last_beacon && !(rx_status->rx_flags & IEEE80211_RX_RA_MATCH))
  568. return;
  569. if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
  570. memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
  571. return;
  572. end = ((u8 *) mgmt) + len;
  573. pos = mgmt->u.probe_req.variable;
  574. if (pos[0] != WLAN_EID_SSID ||
  575. pos + 2 + pos[1] > end) {
  576. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  577. printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
  578. "from %pM\n",
  579. sdata->name, mgmt->sa);
  580. #endif
  581. return;
  582. }
  583. if (pos[1] != 0 &&
  584. (pos[1] != ifibss->ssid_len ||
  585. memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
  586. /* Ignore ProbeReq for foreign SSID */
  587. return;
  588. }
  589. /* Reply with ProbeResp */
  590. skb = skb_copy(ifibss->presp, GFP_KERNEL);
  591. if (!skb)
  592. return;
  593. resp = (struct ieee80211_mgmt *) skb->data;
  594. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  595. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  596. printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
  597. sdata->name, resp->da);
  598. #endif /* CONFIG_MAC80211_IBSS_DEBUG */
  599. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  600. ieee80211_tx_skb(sdata, skb);
  601. }
  602. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  603. struct ieee80211_mgmt *mgmt,
  604. size_t len,
  605. struct ieee80211_rx_status *rx_status)
  606. {
  607. size_t baselen;
  608. struct ieee802_11_elems elems;
  609. if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
  610. return; /* ignore ProbeResp to foreign address */
  611. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  612. if (baselen > len)
  613. return;
  614. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  615. &elems);
  616. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  617. }
  618. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  619. struct ieee80211_mgmt *mgmt,
  620. size_t len,
  621. struct ieee80211_rx_status *rx_status)
  622. {
  623. size_t baselen;
  624. struct ieee802_11_elems elems;
  625. /* Process beacon from the current BSS */
  626. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  627. if (baselen > len)
  628. return;
  629. ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
  630. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
  631. }
  632. void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  633. struct sk_buff *skb)
  634. {
  635. struct ieee80211_rx_status *rx_status;
  636. struct ieee80211_mgmt *mgmt;
  637. u16 fc;
  638. rx_status = IEEE80211_SKB_RXCB(skb);
  639. mgmt = (struct ieee80211_mgmt *) skb->data;
  640. fc = le16_to_cpu(mgmt->frame_control);
  641. mutex_lock(&sdata->u.ibss.mtx);
  642. if (!sdata->u.ibss.ssid_len)
  643. goto mgmt_out; /* not ready to merge yet */
  644. switch (fc & IEEE80211_FCTL_STYPE) {
  645. case IEEE80211_STYPE_PROBE_REQ:
  646. ieee80211_rx_mgmt_probe_req(sdata, skb);
  647. break;
  648. case IEEE80211_STYPE_PROBE_RESP:
  649. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
  650. rx_status);
  651. break;
  652. case IEEE80211_STYPE_BEACON:
  653. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  654. rx_status);
  655. break;
  656. case IEEE80211_STYPE_AUTH:
  657. ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
  658. break;
  659. }
  660. mgmt_out:
  661. mutex_unlock(&sdata->u.ibss.mtx);
  662. }
  663. void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
  664. {
  665. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  666. mutex_lock(&ifibss->mtx);
  667. /*
  668. * Work could be scheduled after scan or similar
  669. * when we aren't even joined (or trying) with a
  670. * network.
  671. */
  672. if (!ifibss->ssid_len)
  673. goto out;
  674. switch (ifibss->state) {
  675. case IEEE80211_IBSS_MLME_SEARCH:
  676. ieee80211_sta_find_ibss(sdata);
  677. break;
  678. case IEEE80211_IBSS_MLME_JOINED:
  679. ieee80211_sta_merge_ibss(sdata);
  680. break;
  681. default:
  682. WARN_ON(1);
  683. break;
  684. }
  685. out:
  686. mutex_unlock(&ifibss->mtx);
  687. }
  688. static void ieee80211_ibss_timer(unsigned long data)
  689. {
  690. struct ieee80211_sub_if_data *sdata =
  691. (struct ieee80211_sub_if_data *) data;
  692. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  693. struct ieee80211_local *local = sdata->local;
  694. if (local->quiescing) {
  695. ifibss->timer_running = true;
  696. return;
  697. }
  698. ieee80211_queue_work(&local->hw, &sdata->work);
  699. }
  700. #ifdef CONFIG_PM
  701. void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
  702. {
  703. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  704. if (del_timer_sync(&ifibss->timer))
  705. ifibss->timer_running = true;
  706. }
  707. void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
  708. {
  709. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  710. if (ifibss->timer_running) {
  711. add_timer(&ifibss->timer);
  712. ifibss->timer_running = false;
  713. }
  714. }
  715. #endif
  716. void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
  717. {
  718. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  719. setup_timer(&ifibss->timer, ieee80211_ibss_timer,
  720. (unsigned long) sdata);
  721. mutex_init(&ifibss->mtx);
  722. }
  723. /* scan finished notification */
  724. void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
  725. {
  726. struct ieee80211_sub_if_data *sdata;
  727. mutex_lock(&local->iflist_mtx);
  728. list_for_each_entry(sdata, &local->interfaces, list) {
  729. if (!ieee80211_sdata_running(sdata))
  730. continue;
  731. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  732. continue;
  733. sdata->u.ibss.last_scan_completed = jiffies;
  734. ieee80211_queue_work(&local->hw, &sdata->work);
  735. }
  736. mutex_unlock(&local->iflist_mtx);
  737. }
  738. int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
  739. struct cfg80211_ibss_params *params)
  740. {
  741. struct sk_buff *skb;
  742. skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
  743. 36 /* bitrates */ +
  744. 34 /* SSID */ +
  745. 3 /* DS params */ +
  746. 4 /* IBSS params */ +
  747. params->ie_len);
  748. if (!skb)
  749. return -ENOMEM;
  750. mutex_lock(&sdata->u.ibss.mtx);
  751. if (params->bssid) {
  752. memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
  753. sdata->u.ibss.fixed_bssid = true;
  754. } else
  755. sdata->u.ibss.fixed_bssid = false;
  756. sdata->u.ibss.privacy = params->privacy;
  757. sdata->u.ibss.basic_rates = params->basic_rates;
  758. memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
  759. sizeof(params->mcast_rate));
  760. sdata->vif.bss_conf.beacon_int = params->beacon_interval;
  761. sdata->u.ibss.channel = params->channel;
  762. sdata->u.ibss.fixed_channel = params->channel_fixed;
  763. /* fix ourselves to that channel now already */
  764. if (params->channel_fixed) {
  765. sdata->local->oper_channel = params->channel;
  766. WARN_ON(!ieee80211_set_channel_type(sdata->local, sdata,
  767. NL80211_CHAN_NO_HT));
  768. }
  769. if (params->ie) {
  770. sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
  771. GFP_KERNEL);
  772. if (sdata->u.ibss.ie)
  773. sdata->u.ibss.ie_len = params->ie_len;
  774. }
  775. sdata->u.ibss.skb = skb;
  776. sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
  777. sdata->u.ibss.ibss_join_req = jiffies;
  778. memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
  779. sdata->u.ibss.ssid_len = params->ssid_len;
  780. mutex_unlock(&sdata->u.ibss.mtx);
  781. mutex_lock(&sdata->local->mtx);
  782. ieee80211_recalc_idle(sdata->local);
  783. mutex_unlock(&sdata->local->mtx);
  784. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  785. return 0;
  786. }
  787. int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
  788. {
  789. struct sk_buff *skb;
  790. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  791. struct ieee80211_local *local = sdata->local;
  792. struct cfg80211_bss *cbss;
  793. u16 capability;
  794. int active_ibss;
  795. mutex_lock(&sdata->u.ibss.mtx);
  796. active_ibss = ieee80211_sta_active_ibss(sdata);
  797. if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
  798. capability = WLAN_CAPABILITY_IBSS;
  799. if (ifibss->privacy)
  800. capability |= WLAN_CAPABILITY_PRIVACY;
  801. cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
  802. ifibss->bssid, ifibss->ssid,
  803. ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
  804. WLAN_CAPABILITY_PRIVACY,
  805. capability);
  806. if (cbss) {
  807. cfg80211_unlink_bss(local->hw.wiphy, cbss);
  808. cfg80211_put_bss(cbss);
  809. }
  810. }
  811. sta_info_flush(sdata->local, sdata);
  812. /* remove beacon */
  813. kfree(sdata->u.ibss.ie);
  814. skb = sdata->u.ibss.presp;
  815. rcu_assign_pointer(sdata->u.ibss.presp, NULL);
  816. sdata->vif.bss_conf.ibss_joined = false;
  817. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
  818. BSS_CHANGED_IBSS);
  819. synchronize_rcu();
  820. kfree_skb(skb);
  821. skb_queue_purge(&sdata->skb_queue);
  822. memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
  823. sdata->u.ibss.ssid_len = 0;
  824. del_timer_sync(&sdata->u.ibss.timer);
  825. mutex_unlock(&sdata->u.ibss.mtx);
  826. mutex_lock(&local->mtx);
  827. ieee80211_recalc_idle(sdata->local);
  828. mutex_unlock(&local->mtx);
  829. return 0;
  830. }