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