ibss.c 27 KB

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