1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2015        Intel Deutschland GmbH
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called COPYING.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  * BSD LICENSE
29  *
30  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
31  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
32  * Copyright(c) 2015        Intel Deutschland GmbH
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  *
39  *  * Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  *  * Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in
43  *    the documentation and/or other materials provided with the
44  *    distribution.
45  *  * Neither the name Intel Corporation nor the names of its
46  *    contributors may be used to endorse or promote products derived
47  *    from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  *****************************************************************************/
61 #include <linux/etherdevice.h>
62 #include <linux/skbuff.h>
63 #include "iwl-trans.h"
64 #include "mvm.h"
65 #include "fw-api.h"
66 #include "fw-dbg.h"
67 
68 void iwl_mvm_rx_phy_cmd_mq(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
69 {
70 	mvm->ampdu_ref++;
71 
72 #ifdef CONFIG_IWLWIFI_DEBUGFS
73 	if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
74 		spin_lock(&mvm->drv_stats_lock);
75 		mvm->drv_rx_stats.ampdu_count++;
76 		spin_unlock(&mvm->drv_stats_lock);
77 	}
78 #endif
79 }
80 
81 static inline int iwl_mvm_check_pn(struct iwl_mvm *mvm, struct sk_buff *skb,
82 				   int queue, struct ieee80211_sta *sta)
83 {
84 	struct iwl_mvm_sta *mvmsta;
85 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
86 	struct ieee80211_rx_status *stats = IEEE80211_SKB_RXCB(skb);
87 	struct iwl_mvm_key_pn *ptk_pn;
88 	u8 tid, keyidx;
89 	u8 pn[IEEE80211_CCMP_PN_LEN];
90 	u8 *extiv;
91 
92 	/* do PN checking */
93 
94 	/* multicast and non-data only arrives on default queue */
95 	if (!ieee80211_is_data(hdr->frame_control) ||
96 	    is_multicast_ether_addr(hdr->addr1))
97 		return 0;
98 
99 	/* do not check PN for open AP */
100 	if (!(stats->flag & RX_FLAG_DECRYPTED))
101 		return 0;
102 
103 	/*
104 	 * avoid checking for default queue - we don't want to replicate
105 	 * all the logic that's necessary for checking the PN on fragmented
106 	 * frames, leave that to mac80211
107 	 */
108 	if (queue == 0)
109 		return 0;
110 
111 	/* if we are here - this for sure is either CCMP or GCMP */
112 	if (IS_ERR_OR_NULL(sta)) {
113 		IWL_ERR(mvm,
114 			"expected hw-decrypted unicast frame for station\n");
115 		return -1;
116 	}
117 
118 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
119 
120 	extiv = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control);
121 	keyidx = extiv[3] >> 6;
122 
123 	ptk_pn = rcu_dereference(mvmsta->ptk_pn[keyidx]);
124 	if (!ptk_pn)
125 		return -1;
126 
127 	if (ieee80211_is_data_qos(hdr->frame_control))
128 		tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
129 	else
130 		tid = 0;
131 
132 	/* we don't use HCCA/802.11 QoS TSPECs, so drop such frames */
133 	if (tid >= IWL_MAX_TID_COUNT)
134 		return -1;
135 
136 	/* load pn */
137 	pn[0] = extiv[7];
138 	pn[1] = extiv[6];
139 	pn[2] = extiv[5];
140 	pn[3] = extiv[4];
141 	pn[4] = extiv[1];
142 	pn[5] = extiv[0];
143 
144 	if (memcmp(pn, ptk_pn->q[queue].pn[tid],
145 		   IEEE80211_CCMP_PN_LEN) <= 0)
146 		return -1;
147 
148 	memcpy(ptk_pn->q[queue].pn[tid], pn, IEEE80211_CCMP_PN_LEN);
149 	stats->flag |= RX_FLAG_PN_VALIDATED;
150 
151 	return 0;
152 }
153 
154 /* iwl_mvm_create_skb Adds the rxb to a new skb */
155 static void iwl_mvm_create_skb(struct sk_buff *skb, struct ieee80211_hdr *hdr,
156 			       u16 len, u8 crypt_len,
157 			       struct iwl_rx_cmd_buffer *rxb)
158 {
159 	unsigned int hdrlen, fraglen;
160 
161 	/* If frame is small enough to fit in skb->head, pull it completely.
162 	 * If not, only pull ieee80211_hdr (including crypto if present, and
163 	 * an additional 8 bytes for SNAP/ethertype, see below) so that
164 	 * splice() or TCP coalesce are more efficient.
165 	 *
166 	 * Since, in addition, ieee80211_data_to_8023() always pull in at
167 	 * least 8 bytes (possibly more for mesh) we can do the same here
168 	 * to save the cost of doing it later. That still doesn't pull in
169 	 * the actual IP header since the typical case has a SNAP header.
170 	 * If the latter changes (there are efforts in the standards group
171 	 * to do so) we should revisit this and ieee80211_data_to_8023().
172 	 */
173 	hdrlen = (len <= skb_tailroom(skb)) ? len :
174 					      sizeof(*hdr) + crypt_len + 8;
175 
176 	memcpy(skb_put(skb, hdrlen), hdr, hdrlen);
177 	fraglen = len - hdrlen;
178 
179 	if (fraglen) {
180 		int offset = (void *)hdr + hdrlen -
181 			     rxb_addr(rxb) + rxb_offset(rxb);
182 
183 		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
184 				fraglen, rxb->truesize);
185 	}
186 }
187 
188 /* iwl_mvm_pass_packet_to_mac80211 - passes the packet for mac80211 */
189 static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
190 					    struct napi_struct *napi,
191 					    struct sk_buff *skb, int queue,
192 					    struct ieee80211_sta *sta)
193 {
194 	if (iwl_mvm_check_pn(mvm, skb, queue, sta))
195 		kfree_skb(skb);
196 	else
197 		ieee80211_rx_napi(mvm->hw, skb, napi);
198 }
199 
200 static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
201 					struct iwl_rx_mpdu_desc *desc,
202 					struct ieee80211_rx_status *rx_status)
203 {
204 	int energy_a, energy_b, energy_c, max_energy;
205 
206 	energy_a = desc->energy_a;
207 	energy_a = energy_a ? -energy_a : S8_MIN;
208 	energy_b = desc->energy_b;
209 	energy_b = energy_b ? -energy_b : S8_MIN;
210 	energy_c = desc->energy_c;
211 	energy_c = energy_c ? -energy_c : S8_MIN;
212 	max_energy = max(energy_a, energy_b);
213 	max_energy = max(max_energy, energy_c);
214 
215 	IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n",
216 			energy_a, energy_b, energy_c, max_energy);
217 
218 	rx_status->signal = max_energy;
219 	rx_status->chains = 0; /* TODO: phy info */
220 	rx_status->chain_signal[0] = energy_a;
221 	rx_status->chain_signal[1] = energy_b;
222 	rx_status->chain_signal[2] = energy_c;
223 }
224 
225 static int iwl_mvm_rx_crypto(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
226 			     struct ieee80211_rx_status *stats,
227 			     struct iwl_rx_mpdu_desc *desc, int queue,
228 			     u8 *crypt_len)
229 {
230 	u16 status = le16_to_cpu(desc->status);
231 
232 	if (!ieee80211_has_protected(hdr->frame_control) ||
233 	    (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
234 	    IWL_RX_MPDU_STATUS_SEC_NONE)
235 		return 0;
236 
237 	/* TODO: handle packets encrypted with unknown alg */
238 
239 	switch (status & IWL_RX_MPDU_STATUS_SEC_MASK) {
240 	case IWL_RX_MPDU_STATUS_SEC_CCM:
241 	case IWL_RX_MPDU_STATUS_SEC_GCM:
242 		BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN != IEEE80211_GCMP_PN_LEN);
243 		/* alg is CCM: check MIC only */
244 		if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
245 			return -1;
246 
247 		stats->flag |= RX_FLAG_DECRYPTED;
248 		*crypt_len = IEEE80211_CCMP_HDR_LEN;
249 		return 0;
250 	case IWL_RX_MPDU_STATUS_SEC_TKIP:
251 		/* Don't drop the frame and decrypt it in SW */
252 		if (!(status & IWL_RX_MPDU_RES_STATUS_TTAK_OK))
253 			return 0;
254 
255 		*crypt_len = IEEE80211_TKIP_IV_LEN;
256 		/* fall through if TTAK OK */
257 	case IWL_RX_MPDU_STATUS_SEC_WEP:
258 		if (!(status & IWL_RX_MPDU_STATUS_ICV_OK))
259 			return -1;
260 
261 		stats->flag |= RX_FLAG_DECRYPTED;
262 		if ((status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
263 				IWL_RX_MPDU_STATUS_SEC_WEP)
264 			*crypt_len = IEEE80211_WEP_IV_LEN;
265 		return 0;
266 	case IWL_RX_MPDU_STATUS_SEC_EXT_ENC:
267 		if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
268 			return -1;
269 		stats->flag |= RX_FLAG_DECRYPTED;
270 		return 0;
271 	default:
272 		IWL_ERR(mvm, "Unhandled alg: 0x%x\n", status);
273 	}
274 
275 	return 0;
276 }
277 
278 static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
279 			    struct sk_buff *skb,
280 			    struct iwl_rx_mpdu_desc *desc)
281 {
282 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
283 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
284 
285 	if (mvmvif->features & NETIF_F_RXCSUM &&
286 	    desc->l3l4_flags & cpu_to_le16(IWL_RX_L3L4_IP_HDR_CSUM_OK) &&
287 	    desc->l3l4_flags & cpu_to_le16(IWL_RX_L3L4_TCP_UDP_CSUM_OK))
288 		skb->ip_summed = CHECKSUM_UNNECESSARY;
289 }
290 
291 void iwl_mvm_rx_mpdu_mq(struct iwl_mvm *mvm, struct napi_struct *napi,
292 			struct iwl_rx_cmd_buffer *rxb, int queue)
293 {
294 	struct ieee80211_rx_status *rx_status;
295 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
296 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
297 	struct ieee80211_hdr *hdr = (void *)(desc + 1);
298 	u32 len = le16_to_cpu(desc->mpdu_len);
299 	u32 rate_n_flags = le32_to_cpu(desc->rate_n_flags);
300 	struct ieee80211_sta *sta = NULL;
301 	struct sk_buff *skb;
302 	u8 crypt_len = 0;
303 
304 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
305 	 * ieee80211_hdr pulled.
306 	 */
307 	skb = alloc_skb(128, GFP_ATOMIC);
308 	if (!skb) {
309 		IWL_ERR(mvm, "alloc_skb failed\n");
310 		return;
311 	}
312 
313 	rx_status = IEEE80211_SKB_RXCB(skb);
314 
315 	if (iwl_mvm_rx_crypto(mvm, hdr, rx_status, desc, queue, &crypt_len)) {
316 		kfree_skb(skb);
317 		return;
318 	}
319 
320 	/*
321 	 * Keep packets with CRC errors (and with overrun) for monitor mode
322 	 * (otherwise the firmware discards them) but mark them as bad.
323 	 */
324 	if (!(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_CRC_OK)) ||
325 	    !(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_OVERRUN_OK))) {
326 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n",
327 			     le16_to_cpu(desc->status));
328 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
329 	}
330 
331 	rx_status->mactime = le64_to_cpu(desc->tsf_on_air_rise);
332 	rx_status->device_timestamp = le32_to_cpu(desc->gp2_on_air_rise);
333 	rx_status->band = desc->channel > 14 ? IEEE80211_BAND_5GHZ :
334 					       IEEE80211_BAND_2GHZ;
335 	rx_status->freq = ieee80211_channel_to_frequency(desc->channel,
336 							 rx_status->band);
337 	iwl_mvm_get_signal_strength(mvm, desc, rx_status);
338 
339 	rcu_read_lock();
340 
341 	if (le16_to_cpu(desc->status) & IWL_RX_MPDU_STATUS_SRC_STA_FOUND) {
342 		u8 id = desc->sta_id_flags & IWL_RX_MPDU_SIF_STA_ID_MASK;
343 
344 		if (!WARN_ON_ONCE(id >= IWL_MVM_STATION_COUNT)) {
345 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
346 			if (IS_ERR(sta))
347 				sta = NULL;
348 		}
349 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
350 		/*
351 		 * This is fine since we prevent two stations with the same
352 		 * address from being added.
353 		 */
354 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
355 	}
356 
357 	if (sta) {
358 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
359 
360 		/*
361 		 * We have tx blocked stations (with CS bit). If we heard
362 		 * frames from a blocked station on a new channel we can
363 		 * TX to it again.
364 		 */
365 		if (unlikely(mvm->csa_tx_block_bcn_timeout))
366 			iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, false);
367 
368 		rs_update_last_rssi(mvm, &mvmsta->lq_sta, rx_status);
369 
370 		if (iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_RSSI) &&
371 		    ieee80211_is_beacon(hdr->frame_control)) {
372 			struct iwl_fw_dbg_trigger_tlv *trig;
373 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
374 			bool trig_check;
375 			s32 rssi;
376 
377 			trig = iwl_fw_dbg_get_trigger(mvm->fw,
378 						      FW_DBG_TRIGGER_RSSI);
379 			rssi_trig = (void *)trig->data;
380 			rssi = le32_to_cpu(rssi_trig->rssi);
381 
382 			trig_check =
383 				iwl_fw_dbg_trigger_check_stop(mvm, mvmsta->vif,
384 							      trig);
385 			if (trig_check && rx_status->signal < rssi)
386 				iwl_mvm_fw_dbg_collect_trig(mvm, trig, NULL);
387 		}
388 
389 		/* TODO: multi queue TCM */
390 
391 		if (ieee80211_is_data(hdr->frame_control))
392 			iwl_mvm_rx_csum(sta, skb, desc);
393 	}
394 
395 	/*
396 	 * TODO: PHY info.
397 	 * Verify we don't have the information in the MPDU descriptor and
398 	 * that it is not needed.
399 	 * Make sure for monitor mode that we are on default queue, update
400 	 * ampdu_ref and the rest of phy info then
401 	 */
402 
403 	/* Set up the HT phy flags */
404 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
405 	case RATE_MCS_CHAN_WIDTH_20:
406 		break;
407 	case RATE_MCS_CHAN_WIDTH_40:
408 		rx_status->flag |= RX_FLAG_40MHZ;
409 		break;
410 	case RATE_MCS_CHAN_WIDTH_80:
411 		rx_status->vht_flag |= RX_VHT_FLAG_80MHZ;
412 		break;
413 	case RATE_MCS_CHAN_WIDTH_160:
414 		rx_status->vht_flag |= RX_VHT_FLAG_160MHZ;
415 		break;
416 	}
417 	if (rate_n_flags & RATE_MCS_SGI_MSK)
418 		rx_status->flag |= RX_FLAG_SHORT_GI;
419 	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
420 		rx_status->flag |= RX_FLAG_HT_GF;
421 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
422 		rx_status->flag |= RX_FLAG_LDPC;
423 	if (rate_n_flags & RATE_MCS_HT_MSK) {
424 		u8 stbc = (rate_n_flags & RATE_MCS_HT_STBC_MSK) >>
425 				RATE_MCS_STBC_POS;
426 		rx_status->flag |= RX_FLAG_HT;
427 		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
428 		rx_status->flag |= stbc << RX_FLAG_STBC_SHIFT;
429 	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
430 		u8 stbc = (rate_n_flags & RATE_MCS_VHT_STBC_MSK) >>
431 				RATE_MCS_STBC_POS;
432 		rx_status->vht_nss =
433 			((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
434 						RATE_VHT_MCS_NSS_POS) + 1;
435 		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
436 		rx_status->flag |= RX_FLAG_VHT;
437 		rx_status->flag |= stbc << RX_FLAG_STBC_SHIFT;
438 		if (rate_n_flags & RATE_MCS_BF_MSK)
439 			rx_status->vht_flag |= RX_VHT_FLAG_BF;
440 	} else {
441 		rx_status->rate_idx =
442 			iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
443 							    rx_status->band);
444 	}
445 
446 	/* TODO: PHY info - update ampdu queue statistics (for debugfs) */
447 	/* TODO: PHY info - gscan */
448 
449 	iwl_mvm_create_skb(skb, hdr, len, crypt_len, rxb);
450 	iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, queue, sta);
451 	rcu_read_unlock();
452 }
453 
454 void iwl_mvm_rx_frame_release(struct iwl_mvm *mvm,
455 			      struct iwl_rx_cmd_buffer *rxb, int queue)
456 {
457 	/* TODO */
458 }
459