ibss.c 26 KB

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