1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(c) 2003 - 2004 Intel Corporation. All rights reserved.
4  *
5  * Contact Information:
6  * James P. Ketrenos <ipw2100-admin@linux.intel.com>
7  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
8  *
9  * Few modifications for Realtek's Wi-Fi drivers by
10  * Andrea Merello <andrea.merello@gmail.com>
11  *
12  * A special thanks goes to Realtek for their support !
13  */
14 #include <linux/compiler.h>
15 #include <linux/errno.h>
16 #include <linux/if_arp.h>
17 #include <linux/in6.h>
18 #include <linux/in.h>
19 #include <linux/ip.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/netdevice.h>
23 #include <linux/pci.h>
24 #include <linux/proc_fs.h>
25 #include <linux/skbuff.h>
26 #include <linux/slab.h>
27 #include <linux/tcp.h>
28 #include <linux/types.h>
29 #include <linux/wireless.h>
30 #include <linux/etherdevice.h>
31 #include <linux/uaccess.h>
32 #include <linux/if_vlan.h>
33 
34 #include "rtllib.h"
35 
36 /* 802.11 Data Frame
37  *
38  *
39  * 802.11 frame_control for data frames - 2 bytes
40  *      ,--------------------------------------------------------------------.
41  * bits | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |  9 |  a |  b  |  c  |  d  | e  |
42  *      |---|---|---|---|---|---|---|---|---|----|----|-----|-----|-----|----|
43  * val  | 0 | 0 | 0 | 1 | x | 0 | 0 | 0 | 1 |  0 |  x |  x  |  x  |  x  | x  |
44  *      |---|---|---|---|---|---|---|---|---|----|----|-----|-----|-----|----|
45  * desc |  ver  | type  |  ^-subtype-^  |to |from|more|retry| pwr |more |wep |
46  *      |       |       | x=0 data      |DS | DS |frag|     | mgm |data |    |
47  *      |       |       | x=1 data+ack  |   |    |    |     |     |     |    |
48  *      '--------------------------------------------------------------------'
49  *                                           /\
50  *                                           |
51  * 802.11 Data Frame                         |
52  *          ,--------- 'ctrl' expands to >---'
53  *          |
54  *       ,--'---,-------------------------------------------------------------.
55  * Bytes |  2   |  2   |    6    |    6    |    6    |  2   | 0..2312 |   4  |
56  *       |------|------|---------|---------|---------|------|---------|------|
57  * Desc. | ctrl | dura |  DA/RA  |   TA    |    SA   | Sequ |  Frame  |  fcs |
58  *       |      | tion | (BSSID) |         |         | ence |  data   |      |
59  *       `--------------------------------------------------|         |------'
60  * Total: 28 non-data bytes                                 `----.----'
61  *                                                               |
62  *        .- 'Frame data' expands to <---------------------------'
63  *        |
64  *        V
65  *       ,---------------------------------------------------.
66  * Bytes |  1   |  1   |    1    |    3     |  2   |  0-2304 |
67  *       |------|------|---------|----------|------|---------|
68  * Desc. | SNAP | SNAP | Control |Eth Tunnel| Type | IP      |
69  *       | DSAP | SSAP |         |          |      | Packet  |
70  *       | 0xAA | 0xAA |0x03 (UI)|0x00-00-F8|      |         |
71  *       `-----------------------------------------|         |
72  * Total: 8 non-data bytes                         `----.----'
73  *                                                      |
74  *        .- 'IP Packet' expands, if WEP enabled, to <--'
75  *        |
76  *        V
77  *       ,-----------------------.
78  * Bytes |  4  |   0-2296  |  4  |
79  *       |-----|-----------|-----|
80  * Desc. | IV  | Encrypted | ICV |
81  *       |     | IP Packet |     |
82  *       `-----------------------'
83  * Total: 8 non-data bytes
84  *
85  *
86  * 802.3 Ethernet Data Frame
87  *
88  *       ,-----------------------------------------.
89  * Bytes |   6   |   6   |  2   |  Variable |   4  |
90  *       |-------|-------|------|-----------|------|
91  * Desc. | Dest. | Source| Type | IP Packet |  fcs |
92  *       |  MAC  |  MAC  |      |	   |      |
93  *       `-----------------------------------------'
94  * Total: 18 non-data bytes
95  *
96  * In the event that fragmentation is required, the incoming payload is split
97  * into N parts of size ieee->fts.  The first fragment contains the SNAP header
98  * and the remaining packets are just data.
99  *
100  * If encryption is enabled, each fragment payload size is reduced by enough
101  * space to add the prefix and postfix (IV and ICV totalling 8 bytes in
102  * the case of WEP) So if you have 1500 bytes of payload with ieee->fts set to
103  * 500 without encryption it will take 3 frames.  With WEP it will take 4 frames
104  * as the payload of each frame is reduced to 492 bytes.
105  *
106  * SKB visualization
107  *
108  * ,- skb->data
109  * |
110  * |    ETHERNET HEADER        ,-<-- PAYLOAD
111  * |                           |     14 bytes from skb->data
112  * |  2 bytes for Type --> ,T. |     (sizeof ethhdr)
113  * |                       | | |
114  * |,-Dest.--. ,--Src.---. | | |
115  * |  6 bytes| | 6 bytes | | | |
116  * v         | |         | | | |
117  * 0         | v       1 | v | v           2
118  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
119  *     ^     | ^         | ^ |
120  *     |     | |         | | |
121  *     |     | |         | `T' <---- 2 bytes for Type
122  *     |     | |         |
123  *     |     | '---SNAP--' <-------- 6 bytes for SNAP
124  *     |     |
125  *     `-IV--' <-------------------- 4 bytes for IV (WEP)
126  *
127  *      SNAP HEADER
128  *
129  */
130 
131 static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
132 static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
133 
rtllib_put_snap(u8 * data,u16 h_proto)134 static int rtllib_put_snap(u8 *data, u16 h_proto)
135 {
136 	struct rtllib_snap_hdr *snap;
137 	u8 *oui;
138 
139 	snap = (struct rtllib_snap_hdr *)data;
140 	snap->dsap = 0xaa;
141 	snap->ssap = 0xaa;
142 	snap->ctrl = 0x03;
143 
144 	if (h_proto == 0x8137 || h_proto == 0x80f3)
145 		oui = P802_1H_OUI;
146 	else
147 		oui = RFC1042_OUI;
148 	snap->oui[0] = oui[0];
149 	snap->oui[1] = oui[1];
150 	snap->oui[2] = oui[2];
151 
152 	*(__be16 *)(data + SNAP_SIZE) = htons(h_proto);
153 
154 	return SNAP_SIZE + sizeof(u16);
155 }
156 
rtllib_encrypt_fragment(struct rtllib_device * ieee,struct sk_buff * frag,int hdr_len)157 int rtllib_encrypt_fragment(struct rtllib_device *ieee, struct sk_buff *frag,
158 			    int hdr_len)
159 {
160 	struct lib80211_crypt_data *crypt = NULL;
161 	int res;
162 
163 	crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
164 
165 	if (!(crypt && crypt->ops)) {
166 		netdev_info(ieee->dev, "=========>%s(), crypt is null\n",
167 			    __func__);
168 		return -1;
169 	}
170 	/* To encrypt, frame format is:
171 	 * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes)
172 	 */
173 
174 	/* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
175 	 * call both MSDU and MPDU encryption functions from here.
176 	 */
177 	atomic_inc(&crypt->refcnt);
178 	res = 0;
179 	if (crypt->ops->encrypt_msdu)
180 		res = crypt->ops->encrypt_msdu(frag, hdr_len, crypt->priv);
181 	if (res == 0 && crypt->ops->encrypt_mpdu)
182 		res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
183 
184 	atomic_dec(&crypt->refcnt);
185 	if (res < 0) {
186 		netdev_info(ieee->dev, "%s: Encryption failed: len=%d.\n",
187 			    ieee->dev->name, frag->len);
188 		return -1;
189 	}
190 
191 	return 0;
192 }
193 
rtllib_txb_free(struct rtllib_txb * txb)194 void rtllib_txb_free(struct rtllib_txb *txb)
195 {
196 	if (unlikely(!txb))
197 		return;
198 	kfree(txb);
199 }
200 
rtllib_alloc_txb(int nr_frags,int txb_size,gfp_t gfp_mask)201 static struct rtllib_txb *rtllib_alloc_txb(int nr_frags, int txb_size,
202 					   gfp_t gfp_mask)
203 {
204 	struct rtllib_txb *txb;
205 	int i;
206 
207 	txb = kzalloc(struct_size(txb, fragments, nr_frags), gfp_mask);
208 	if (!txb)
209 		return NULL;
210 
211 	txb->nr_frags = nr_frags;
212 	txb->frag_size = cpu_to_le16(txb_size);
213 
214 	for (i = 0; i < nr_frags; i++) {
215 		txb->fragments[i] = dev_alloc_skb(txb_size);
216 		if (unlikely(!txb->fragments[i]))
217 			goto err_free;
218 		memset(txb->fragments[i]->cb, 0, sizeof(txb->fragments[i]->cb));
219 	}
220 
221 	return txb;
222 
223 err_free:
224 	while (--i >= 0)
225 		dev_kfree_skb_any(txb->fragments[i]);
226 	kfree(txb);
227 
228 	return NULL;
229 }
230 
rtllib_classify(struct sk_buff * skb,u8 bIsAmsdu)231 static int rtllib_classify(struct sk_buff *skb, u8 bIsAmsdu)
232 {
233 	struct ethhdr *eth;
234 	struct iphdr *ip;
235 
236 	eth = (struct ethhdr *)skb->data;
237 	if (eth->h_proto != htons(ETH_P_IP))
238 		return 0;
239 
240 #ifdef VERBOSE_DEBUG
241 	print_hex_dump_bytes("%s: ", __func__, DUMP_PREFIX_NONE, skb->data,
242 			     skb->len);
243 #endif
244 	ip = ip_hdr(skb);
245 	switch (ip->tos & 0xfc) {
246 	case 0x20:
247 		return 2;
248 	case 0x40:
249 		return 1;
250 	case 0x60:
251 		return 3;
252 	case 0x80:
253 		return 4;
254 	case 0xa0:
255 		return 5;
256 	case 0xc0:
257 		return 6;
258 	case 0xe0:
259 		return 7;
260 	default:
261 		return 0;
262 	}
263 }
264 
rtllib_tx_query_agg_cap(struct rtllib_device * ieee,struct sk_buff * skb,struct cb_desc * tcb_desc)265 static void rtllib_tx_query_agg_cap(struct rtllib_device *ieee,
266 				    struct sk_buff *skb,
267 				    struct cb_desc *tcb_desc)
268 {
269 	struct rt_hi_throughput *ht_info = ieee->ht_info;
270 	struct tx_ts_record *pTxTs = NULL;
271 	struct rtllib_hdr_1addr *hdr = (struct rtllib_hdr_1addr *)skb->data;
272 
273 	if (rtllib_act_scanning(ieee, false))
274 		return;
275 
276 	if (!ht_info->bCurrentHTSupport || !ht_info->enable_ht)
277 		return;
278 	if (!IsQoSDataFrame(skb->data))
279 		return;
280 	if (is_multicast_ether_addr(hdr->addr1))
281 		return;
282 
283 	if (tcb_desc->bdhcp || ieee->CntAfterLink < 2)
284 		return;
285 
286 	if (ht_info->iot_action & HT_IOT_ACT_TX_NO_AGGREGATION)
287 		return;
288 
289 	if (!ieee->GetNmodeSupportBySecCfg(ieee->dev))
290 		return;
291 	if (ht_info->bCurrentAMPDUEnable) {
292 		if (!GetTs(ieee, (struct ts_common_info **)(&pTxTs), hdr->addr1,
293 			   skb->priority, TX_DIR, true)) {
294 			netdev_info(ieee->dev, "%s: can't get TS\n", __func__);
295 			return;
296 		}
297 		if (!pTxTs->TxAdmittedBARecord.b_valid) {
298 			if (ieee->wpa_ie_len && (ieee->pairwise_key_type ==
299 			    KEY_TYPE_NA)) {
300 				;
301 			} else if (tcb_desc->bdhcp == 1) {
302 				;
303 			} else if (!pTxTs->bDisable_AddBa) {
304 				TsStartAddBaProcess(ieee, pTxTs);
305 			}
306 			goto FORCED_AGG_SETTING;
307 		} else if (!pTxTs->bUsingBa) {
308 			if (SN_LESS(pTxTs->TxAdmittedBARecord.ba_start_seq_ctrl.field.seq_num,
309 				    (pTxTs->TxCurSeq + 1) % 4096))
310 				pTxTs->bUsingBa = true;
311 			else
312 				goto FORCED_AGG_SETTING;
313 		}
314 		if (ieee->iw_mode == IW_MODE_INFRA) {
315 			tcb_desc->bAMPDUEnable = true;
316 			tcb_desc->ampdu_factor = ht_info->CurrentAMPDUFactor;
317 			tcb_desc->ampdu_density = ht_info->current_mpdu_density;
318 		}
319 	}
320 FORCED_AGG_SETTING:
321 	switch (ht_info->ForcedAMPDUMode) {
322 	case HT_AGG_AUTO:
323 		break;
324 
325 	case HT_AGG_FORCE_ENABLE:
326 		tcb_desc->bAMPDUEnable = true;
327 		tcb_desc->ampdu_density = ht_info->forced_mpdu_density;
328 		tcb_desc->ampdu_factor = ht_info->forced_ampdu_factor;
329 		break;
330 
331 	case HT_AGG_FORCE_DISABLE:
332 		tcb_desc->bAMPDUEnable = false;
333 		tcb_desc->ampdu_density = 0;
334 		tcb_desc->ampdu_factor = 0;
335 		break;
336 	}
337 }
338 
rtllib_query_ShortPreambleMode(struct rtllib_device * ieee,struct cb_desc * tcb_desc)339 static void rtllib_query_ShortPreambleMode(struct rtllib_device *ieee,
340 					   struct cb_desc *tcb_desc)
341 {
342 	tcb_desc->bUseShortPreamble = false;
343 	if (tcb_desc->data_rate == 2)
344 		return;
345 	else if (ieee->current_network.capability &
346 		 WLAN_CAPABILITY_SHORT_PREAMBLE)
347 		tcb_desc->bUseShortPreamble = true;
348 }
349 
rtllib_query_HTCapShortGI(struct rtllib_device * ieee,struct cb_desc * tcb_desc)350 static void rtllib_query_HTCapShortGI(struct rtllib_device *ieee,
351 				      struct cb_desc *tcb_desc)
352 {
353 	struct rt_hi_throughput *ht_info = ieee->ht_info;
354 
355 	tcb_desc->bUseShortGI		= false;
356 
357 	if (!ht_info->bCurrentHTSupport || !ht_info->enable_ht)
358 		return;
359 
360 	if (ht_info->forced_short_gi) {
361 		tcb_desc->bUseShortGI = true;
362 		return;
363 	}
364 
365 	if (ht_info->bCurBW40MHz && ht_info->bCurShortGI40MHz)
366 		tcb_desc->bUseShortGI = true;
367 	else if (!ht_info->bCurBW40MHz && ht_info->bCurShortGI20MHz)
368 		tcb_desc->bUseShortGI = true;
369 }
370 
rtllib_query_BandwidthMode(struct rtllib_device * ieee,struct cb_desc * tcb_desc)371 static void rtllib_query_BandwidthMode(struct rtllib_device *ieee,
372 				       struct cb_desc *tcb_desc)
373 {
374 	struct rt_hi_throughput *ht_info = ieee->ht_info;
375 
376 	tcb_desc->bPacketBW = false;
377 
378 	if (!ht_info->bCurrentHTSupport || !ht_info->enable_ht)
379 		return;
380 
381 	if (tcb_desc->bMulticast || tcb_desc->bBroadcast)
382 		return;
383 
384 	if ((tcb_desc->data_rate & 0x80) == 0)
385 		return;
386 	if (ht_info->bCurBW40MHz && ht_info->cur_tx_bw40mhz &&
387 	    !ieee->bandwidth_auto_switch.bforced_tx20Mhz)
388 		tcb_desc->bPacketBW = true;
389 }
390 
rtllib_query_protectionmode(struct rtllib_device * ieee,struct cb_desc * tcb_desc,struct sk_buff * skb)391 static void rtllib_query_protectionmode(struct rtllib_device *ieee,
392 					struct cb_desc *tcb_desc,
393 					struct sk_buff *skb)
394 {
395 	struct rt_hi_throughput *ht_info;
396 
397 	tcb_desc->bRTSSTBC			= false;
398 	tcb_desc->bRTSUseShortGI		= false;
399 	tcb_desc->bCTSEnable			= false;
400 	tcb_desc->RTSSC				= 0;
401 	tcb_desc->bRTSBW			= false;
402 
403 	if (tcb_desc->bBroadcast || tcb_desc->bMulticast)
404 		return;
405 
406 	if (is_broadcast_ether_addr(skb->data + 16))
407 		return;
408 
409 	if (ieee->mode < WIRELESS_MODE_N_24G) {
410 		if (skb->len > ieee->rts) {
411 			tcb_desc->bRTSEnable = true;
412 			tcb_desc->rts_rate = MGN_24M;
413 		} else if (ieee->current_network.buseprotection) {
414 			tcb_desc->bRTSEnable = true;
415 			tcb_desc->bCTSEnable = true;
416 			tcb_desc->rts_rate = MGN_24M;
417 		}
418 		return;
419 	}
420 
421 	ht_info = ieee->ht_info;
422 
423 	while (true) {
424 		if (ht_info->iot_action & HT_IOT_ACT_FORCED_CTS2SELF) {
425 			tcb_desc->bCTSEnable	= true;
426 			tcb_desc->rts_rate  =	MGN_24M;
427 			tcb_desc->bRTSEnable = true;
428 			break;
429 		} else if (ht_info->iot_action & (HT_IOT_ACT_FORCED_RTS |
430 			   HT_IOT_ACT_PURE_N_MODE)) {
431 			tcb_desc->bRTSEnable = true;
432 			tcb_desc->rts_rate  =	MGN_24M;
433 			break;
434 		}
435 		if (ieee->current_network.buseprotection) {
436 			tcb_desc->bRTSEnable = true;
437 			tcb_desc->bCTSEnable = true;
438 			tcb_desc->rts_rate = MGN_24M;
439 			break;
440 		}
441 		if (ht_info->bCurrentHTSupport  && ht_info->enable_ht) {
442 			u8 HTOpMode = ht_info->current_op_mode;
443 
444 			if ((ht_info->bCurBW40MHz && (HTOpMode == 2 ||
445 						      HTOpMode == 3)) ||
446 			     (!ht_info->bCurBW40MHz && HTOpMode == 3)) {
447 				tcb_desc->rts_rate = MGN_24M;
448 				tcb_desc->bRTSEnable = true;
449 				break;
450 			}
451 		}
452 		if (skb->len > ieee->rts) {
453 			tcb_desc->rts_rate = MGN_24M;
454 			tcb_desc->bRTSEnable = true;
455 			break;
456 		}
457 		if (tcb_desc->bAMPDUEnable) {
458 			tcb_desc->rts_rate = MGN_24M;
459 			tcb_desc->bRTSEnable = false;
460 			break;
461 		}
462 		goto NO_PROTECTION;
463 	}
464 	if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
465 		tcb_desc->bUseShortPreamble = true;
466 	return;
467 NO_PROTECTION:
468 	tcb_desc->bRTSEnable	= false;
469 	tcb_desc->bCTSEnable	= false;
470 	tcb_desc->rts_rate	= 0;
471 	tcb_desc->RTSSC		= 0;
472 	tcb_desc->bRTSBW	= false;
473 }
474 
rtllib_txrate_selectmode(struct rtllib_device * ieee,struct cb_desc * tcb_desc)475 static void rtllib_txrate_selectmode(struct rtllib_device *ieee,
476 				     struct cb_desc *tcb_desc)
477 {
478 	if (ieee->tx_dis_rate_fallback)
479 		tcb_desc->tx_dis_rate_fallback = true;
480 
481 	if (ieee->tx_use_drv_assinged_rate)
482 		tcb_desc->tx_use_drv_assinged_rate = true;
483 	if (!tcb_desc->tx_dis_rate_fallback ||
484 	    !tcb_desc->tx_use_drv_assinged_rate) {
485 		if (ieee->iw_mode == IW_MODE_INFRA ||
486 		    ieee->iw_mode == IW_MODE_ADHOC)
487 			tcb_desc->ratr_index = 0;
488 	}
489 }
490 
rtllib_query_seqnum(struct rtllib_device * ieee,struct sk_buff * skb,u8 * dst)491 static u16 rtllib_query_seqnum(struct rtllib_device *ieee, struct sk_buff *skb,
492 			       u8 *dst)
493 {
494 	u16 seqnum = 0;
495 
496 	if (is_multicast_ether_addr(dst))
497 		return 0;
498 	if (IsQoSDataFrame(skb->data)) {
499 		struct tx_ts_record *pTS = NULL;
500 
501 		if (!GetTs(ieee, (struct ts_common_info **)(&pTS), dst,
502 			   skb->priority, TX_DIR, true))
503 			return 0;
504 		seqnum = pTS->TxCurSeq;
505 		pTS->TxCurSeq = (pTS->TxCurSeq + 1) % 4096;
506 		return seqnum;
507 	}
508 	return 0;
509 }
510 
wme_downgrade_ac(struct sk_buff * skb)511 static int wme_downgrade_ac(struct sk_buff *skb)
512 {
513 	switch (skb->priority) {
514 	case 6:
515 	case 7:
516 		skb->priority = 5; /* VO -> VI */
517 		return 0;
518 	case 4:
519 	case 5:
520 		skb->priority = 3; /* VI -> BE */
521 		return 0;
522 	case 0:
523 	case 3:
524 		skb->priority = 1; /* BE -> BK */
525 		return 0;
526 	default:
527 		return -1;
528 	}
529 }
530 
rtllib_current_rate(struct rtllib_device * ieee)531 static u8 rtllib_current_rate(struct rtllib_device *ieee)
532 {
533 	if (ieee->mode & IEEE_MODE_MASK)
534 		return ieee->rate;
535 
536 	if (ieee->HTCurrentOperaRate)
537 		return ieee->HTCurrentOperaRate;
538 	else
539 		return ieee->rate & 0x7F;
540 }
541 
rtllib_xmit_inter(struct sk_buff * skb,struct net_device * dev)542 static int rtllib_xmit_inter(struct sk_buff *skb, struct net_device *dev)
543 {
544 	struct rtllib_device *ieee = (struct rtllib_device *)
545 				     netdev_priv_rsl(dev);
546 	struct rtllib_txb *txb = NULL;
547 	struct rtllib_hdr_3addrqos *frag_hdr;
548 	int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size;
549 	unsigned long flags;
550 	struct net_device_stats *stats = &ieee->stats;
551 	int ether_type = 0, encrypt;
552 	int bytes, fc, qos_ctl = 0, hdr_len;
553 	struct sk_buff *skb_frag;
554 	struct rtllib_hdr_3addrqos header = { /* Ensure zero initialized */
555 		.duration_id = 0,
556 		.seq_ctl = 0,
557 		.qos_ctl = 0
558 	};
559 	int qos_activated = ieee->current_network.qos_data.active;
560 	u8 dest[ETH_ALEN];
561 	u8 src[ETH_ALEN];
562 	struct lib80211_crypt_data *crypt = NULL;
563 	struct cb_desc *tcb_desc;
564 	u8 bIsMulticast = false;
565 	u8 IsAmsdu = false;
566 	bool	bdhcp = false;
567 
568 	spin_lock_irqsave(&ieee->lock, flags);
569 
570 	/* If there is no driver handler to take the TXB, don't bother
571 	 * creating it...
572 	 */
573 	if (!(ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE) ||
574 	   ((!ieee->softmac_data_hard_start_xmit &&
575 	   (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)))) {
576 		netdev_warn(ieee->dev, "No xmit handler.\n");
577 		goto success;
578 	}
579 
580 	if (likely(ieee->raw_tx == 0)) {
581 		if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
582 			netdev_warn(ieee->dev, "skb too small (%d).\n",
583 				    skb->len);
584 			goto success;
585 		}
586 		/* Save source and destination addresses */
587 		ether_addr_copy(dest, skb->data);
588 		ether_addr_copy(src, skb->data + ETH_ALEN);
589 
590 		memset(skb->cb, 0, sizeof(skb->cb));
591 		ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
592 
593 		if (ieee->iw_mode == IW_MODE_MONITOR) {
594 			txb = rtllib_alloc_txb(1, skb->len, GFP_ATOMIC);
595 			if (unlikely(!txb)) {
596 				netdev_warn(ieee->dev,
597 					    "Could not allocate TXB\n");
598 				goto failed;
599 			}
600 
601 			txb->encrypted = 0;
602 			txb->payload_size = cpu_to_le16(skb->len);
603 			skb_put_data(txb->fragments[0], skb->data, skb->len);
604 
605 			goto success;
606 		}
607 
608 		if (skb->len > 282) {
609 			if (ether_type == ETH_P_IP) {
610 				const struct iphdr *ip = (struct iphdr *)
611 					((u8 *)skb->data + 14);
612 				if (ip->protocol == IPPROTO_UDP) {
613 					struct udphdr *udp;
614 
615 					udp = (struct udphdr *)((u8 *)ip +
616 					      (ip->ihl << 2));
617 					if (((((u8 *)udp)[1] == 68) &&
618 					     (((u8 *)udp)[3] == 67)) ||
619 					   ((((u8 *)udp)[1] == 67) &&
620 					   (((u8 *)udp)[3] == 68))) {
621 						bdhcp = true;
622 						ieee->LPSDelayCnt = 200;
623 					}
624 				}
625 			} else if (ether_type == ETH_P_ARP) {
626 				netdev_info(ieee->dev,
627 					    "=================>DHCP Protocol start tx ARP pkt!!\n");
628 				bdhcp = true;
629 				ieee->LPSDelayCnt =
630 					 ieee->current_network.tim.tim_count;
631 			}
632 		}
633 
634 		skb->priority = rtllib_classify(skb, IsAmsdu);
635 		crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
636 		encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) && crypt && crypt->ops;
637 		if (!encrypt && ieee->ieee802_1x &&
638 		    ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
639 			stats->tx_dropped++;
640 			goto success;
641 		}
642 		if (crypt && !encrypt && ether_type == ETH_P_PAE) {
643 			struct eapol *eap = (struct eapol *)(skb->data +
644 				sizeof(struct ethhdr) - SNAP_SIZE -
645 				sizeof(u16));
646 			netdev_dbg(ieee->dev,
647 				   "TX: IEEE 802.11 EAPOL frame: %s\n",
648 				   eap_get_type(eap->type));
649 		}
650 
651 		/* Advance the SKB to the start of the payload */
652 		skb_pull(skb, sizeof(struct ethhdr));
653 
654 		/* Determine total amount of storage required for TXB packets */
655 		bytes = skb->len + SNAP_SIZE + sizeof(u16);
656 
657 		if (encrypt)
658 			fc = RTLLIB_FTYPE_DATA | RTLLIB_FCTL_WEP;
659 		else
660 			fc = RTLLIB_FTYPE_DATA;
661 
662 		if (qos_activated)
663 			fc |= RTLLIB_STYPE_QOS_DATA;
664 		else
665 			fc |= RTLLIB_STYPE_DATA;
666 
667 		if (ieee->iw_mode == IW_MODE_INFRA) {
668 			fc |= RTLLIB_FCTL_TODS;
669 			/* To DS: Addr1 = BSSID, Addr2 = SA,
670 			 * Addr3 = DA
671 			 */
672 			ether_addr_copy(header.addr1,
673 					ieee->current_network.bssid);
674 			ether_addr_copy(header.addr2, src);
675 			if (IsAmsdu)
676 				ether_addr_copy(header.addr3,
677 						ieee->current_network.bssid);
678 			else
679 				ether_addr_copy(header.addr3, dest);
680 		} else if (ieee->iw_mode == IW_MODE_ADHOC) {
681 			/* not From/To DS: Addr1 = DA, Addr2 = SA,
682 			 * Addr3 = BSSID
683 			 */
684 			ether_addr_copy(header.addr1, dest);
685 			ether_addr_copy(header.addr2, src);
686 			ether_addr_copy(header.addr3,
687 					ieee->current_network.bssid);
688 		}
689 
690 		bIsMulticast = is_multicast_ether_addr(header.addr1);
691 
692 		header.frame_ctl = cpu_to_le16(fc);
693 
694 		/* Determine fragmentation size based on destination (multicast
695 		 * and broadcast are not fragmented)
696 		 */
697 		if (bIsMulticast) {
698 			frag_size = MAX_FRAG_THRESHOLD;
699 			qos_ctl |= QOS_CTL_NOTCONTAIN_ACK;
700 		} else {
701 			frag_size = ieee->fts;
702 			qos_ctl = 0;
703 		}
704 
705 		if (qos_activated) {
706 			hdr_len = RTLLIB_3ADDR_LEN + 2;
707 
708 			/* in case we are a client verify acm is not set for this ac */
709 			while (unlikely(ieee->wmm_acm & (0x01 << skb->priority))) {
710 				netdev_info(ieee->dev, "skb->priority = %x\n",
711 					    skb->priority);
712 				if (wme_downgrade_ac(skb))
713 					break;
714 				netdev_info(ieee->dev, "converted skb->priority = %x\n",
715 					    skb->priority);
716 			}
717 
718 			qos_ctl |= skb->priority;
719 			header.qos_ctl = cpu_to_le16(qos_ctl & RTLLIB_QOS_TID);
720 
721 		} else {
722 			hdr_len = RTLLIB_3ADDR_LEN;
723 		}
724 		/* Determine amount of payload per fragment.  Regardless of if
725 		 * this stack is providing the full 802.11 header, one will
726 		 * eventually be affixed to this fragment -- so we must account
727 		 * for it when determining the amount of payload space.
728 		 */
729 		bytes_per_frag = frag_size - hdr_len;
730 		if (ieee->config &
731 		   (CFG_RTLLIB_COMPUTE_FCS | CFG_RTLLIB_RESERVE_FCS))
732 			bytes_per_frag -= RTLLIB_FCS_LEN;
733 
734 		/* Each fragment may need to have room for encrypting
735 		 * pre/postfix
736 		 */
737 		if (encrypt) {
738 			bytes_per_frag -= crypt->ops->extra_mpdu_prefix_len +
739 				crypt->ops->extra_mpdu_postfix_len +
740 				crypt->ops->extra_msdu_prefix_len +
741 				crypt->ops->extra_msdu_postfix_len;
742 		}
743 		/* Number of fragments is the total bytes_per_frag /
744 		 * payload_per_fragment
745 		 */
746 		nr_frags = bytes / bytes_per_frag;
747 		bytes_last_frag = bytes % bytes_per_frag;
748 		if (bytes_last_frag)
749 			nr_frags++;
750 		else
751 			bytes_last_frag = bytes_per_frag;
752 
753 		/* When we allocate the TXB we allocate enough space for the
754 		 * reserve and full fragment bytes (bytes_per_frag doesn't
755 		 * include prefix, postfix, header, FCS, etc.)
756 		 */
757 		txb = rtllib_alloc_txb(nr_frags, frag_size +
758 				       ieee->tx_headroom, GFP_ATOMIC);
759 		if (unlikely(!txb)) {
760 			netdev_warn(ieee->dev, "Could not allocate TXB\n");
761 			goto failed;
762 		}
763 		txb->encrypted = encrypt;
764 		txb->payload_size = cpu_to_le16(bytes);
765 
766 		if (qos_activated)
767 			txb->queue_index = UP2AC(skb->priority);
768 		else
769 			txb->queue_index = WME_AC_BE;
770 
771 		for (i = 0; i < nr_frags; i++) {
772 			skb_frag = txb->fragments[i];
773 			tcb_desc = (struct cb_desc *)(skb_frag->cb +
774 				    MAX_DEV_ADDR_SIZE);
775 			if (qos_activated) {
776 				skb_frag->priority = skb->priority;
777 				tcb_desc->queue_index =  UP2AC(skb->priority);
778 			} else {
779 				skb_frag->priority = WME_AC_BE;
780 				tcb_desc->queue_index = WME_AC_BE;
781 			}
782 			skb_reserve(skb_frag, ieee->tx_headroom);
783 
784 			if (encrypt) {
785 				if (ieee->hwsec_active)
786 					tcb_desc->bHwSec = 1;
787 				else
788 					tcb_desc->bHwSec = 0;
789 				skb_reserve(skb_frag,
790 					    crypt->ops->extra_mpdu_prefix_len +
791 					    crypt->ops->extra_msdu_prefix_len);
792 			} else {
793 				tcb_desc->bHwSec = 0;
794 			}
795 			frag_hdr = skb_put_data(skb_frag, &header, hdr_len);
796 
797 			/* If this is not the last fragment, then add the
798 			 * MOREFRAGS bit to the frame control
799 			 */
800 			if (i != nr_frags - 1) {
801 				frag_hdr->frame_ctl = cpu_to_le16(fc |
802 								  RTLLIB_FCTL_MOREFRAGS);
803 				bytes = bytes_per_frag;
804 
805 			} else {
806 				/* The last fragment has the remaining length */
807 				bytes = bytes_last_frag;
808 			}
809 			if ((qos_activated) && (!bIsMulticast)) {
810 				frag_hdr->seq_ctl =
811 					 cpu_to_le16(rtllib_query_seqnum(ieee, skb_frag,
812 									 header.addr1));
813 				frag_hdr->seq_ctl =
814 					 cpu_to_le16(le16_to_cpu(frag_hdr->seq_ctl) << 4 | i);
815 			} else {
816 				frag_hdr->seq_ctl =
817 					 cpu_to_le16(ieee->seq_ctrl[0] << 4 | i);
818 			}
819 			/* Put a SNAP header on the first fragment */
820 			if (i == 0) {
821 				rtllib_put_snap(skb_put(skb_frag,
822 							SNAP_SIZE +
823 							sizeof(u16)), ether_type);
824 				bytes -= SNAP_SIZE + sizeof(u16);
825 			}
826 
827 			skb_put_data(skb_frag, skb->data, bytes);
828 
829 			/* Advance the SKB... */
830 			skb_pull(skb, bytes);
831 
832 			/* Encryption routine will move the header forward in
833 			 * order to insert the IV between the header and the
834 			 * payload
835 			 */
836 			if (encrypt)
837 				rtllib_encrypt_fragment(ieee, skb_frag,
838 							hdr_len);
839 			if (ieee->config &
840 			   (CFG_RTLLIB_COMPUTE_FCS | CFG_RTLLIB_RESERVE_FCS))
841 				skb_put(skb_frag, 4);
842 		}
843 
844 		if ((qos_activated) && (!bIsMulticast)) {
845 			if (ieee->seq_ctrl[UP2AC(skb->priority) + 1] == 0xFFF)
846 				ieee->seq_ctrl[UP2AC(skb->priority) + 1] = 0;
847 			else
848 				ieee->seq_ctrl[UP2AC(skb->priority) + 1]++;
849 		} else {
850 			if (ieee->seq_ctrl[0] == 0xFFF)
851 				ieee->seq_ctrl[0] = 0;
852 			else
853 				ieee->seq_ctrl[0]++;
854 		}
855 	} else {
856 		if (unlikely(skb->len < sizeof(struct rtllib_hdr_3addr))) {
857 			netdev_warn(ieee->dev, "skb too small (%d).\n",
858 				    skb->len);
859 			goto success;
860 		}
861 
862 		txb = rtllib_alloc_txb(1, skb->len, GFP_ATOMIC);
863 		if (!txb) {
864 			netdev_warn(ieee->dev, "Could not allocate TXB\n");
865 			goto failed;
866 		}
867 
868 		txb->encrypted = 0;
869 		txb->payload_size = cpu_to_le16(skb->len);
870 		skb_put_data(txb->fragments[0], skb->data, skb->len);
871 	}
872 
873  success:
874 	if (txb) {
875 		tcb_desc = (struct cb_desc *)
876 				(txb->fragments[0]->cb + MAX_DEV_ADDR_SIZE);
877 		tcb_desc->bTxEnableFwCalcDur = 1;
878 		tcb_desc->priority = skb->priority;
879 
880 		if (ether_type == ETH_P_PAE) {
881 			if (ieee->ht_info->iot_action &
882 			    HT_IOT_ACT_WA_IOT_Broadcom) {
883 				tcb_desc->data_rate =
884 					 MgntQuery_TxRateExcludeCCKRates(ieee);
885 				tcb_desc->tx_dis_rate_fallback = false;
886 			} else {
887 				tcb_desc->data_rate = ieee->basic_rate;
888 				tcb_desc->tx_dis_rate_fallback = 1;
889 			}
890 
891 			tcb_desc->ratr_index = 7;
892 			tcb_desc->tx_use_drv_assinged_rate = 1;
893 		} else {
894 			if (is_multicast_ether_addr(header.addr1))
895 				tcb_desc->bMulticast = 1;
896 			if (is_broadcast_ether_addr(header.addr1))
897 				tcb_desc->bBroadcast = 1;
898 			rtllib_txrate_selectmode(ieee, tcb_desc);
899 			if (tcb_desc->bMulticast ||  tcb_desc->bBroadcast)
900 				tcb_desc->data_rate = ieee->basic_rate;
901 			else
902 				tcb_desc->data_rate = rtllib_current_rate(ieee);
903 
904 			if (bdhcp) {
905 				if (ieee->ht_info->iot_action &
906 				    HT_IOT_ACT_WA_IOT_Broadcom) {
907 					tcb_desc->data_rate =
908 					   MgntQuery_TxRateExcludeCCKRates(ieee);
909 					tcb_desc->tx_dis_rate_fallback = false;
910 				} else {
911 					tcb_desc->data_rate = MGN_1M;
912 					tcb_desc->tx_dis_rate_fallback = 1;
913 				}
914 
915 				tcb_desc->ratr_index = 7;
916 				tcb_desc->tx_use_drv_assinged_rate = 1;
917 				tcb_desc->bdhcp = 1;
918 			}
919 
920 			rtllib_query_ShortPreambleMode(ieee, tcb_desc);
921 			rtllib_tx_query_agg_cap(ieee, txb->fragments[0],
922 						tcb_desc);
923 			rtllib_query_HTCapShortGI(ieee, tcb_desc);
924 			rtllib_query_BandwidthMode(ieee, tcb_desc);
925 			rtllib_query_protectionmode(ieee, tcb_desc,
926 						    txb->fragments[0]);
927 		}
928 	}
929 	spin_unlock_irqrestore(&ieee->lock, flags);
930 	dev_kfree_skb_any(skb);
931 	if (txb) {
932 		if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE) {
933 			dev->stats.tx_packets++;
934 			dev->stats.tx_bytes += le16_to_cpu(txb->payload_size);
935 			rtllib_softmac_xmit(txb, ieee);
936 		} else {
937 			rtllib_txb_free(txb);
938 		}
939 	}
940 
941 	return 0;
942 
943  failed:
944 	spin_unlock_irqrestore(&ieee->lock, flags);
945 	netif_stop_queue(dev);
946 	stats->tx_errors++;
947 	return 1;
948 }
949 
rtllib_xmit(struct sk_buff * skb,struct net_device * dev)950 netdev_tx_t rtllib_xmit(struct sk_buff *skb, struct net_device *dev)
951 {
952 	memset(skb->cb, 0, sizeof(skb->cb));
953 	return rtllib_xmit_inter(skb, dev) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
954 }
955 EXPORT_SYMBOL(rtllib_xmit);
956