ibss.c 27 KB

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