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 
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 
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 
194 
195 void rtllib_txb_free(struct rtllib_txb *txb)
196 {
197 	if (unlikely(!txb))
198 		return;
199 	kfree(txb);
200 }
201 
202 static struct rtllib_txb *rtllib_alloc_txb(int nr_frags, int txb_size,
203 					   gfp_t gfp_mask)
204 {
205 	struct rtllib_txb *txb;
206 	int i;
207 
208 	txb = kzalloc(struct_size(txb, fragments, nr_frags), gfp_mask);
209 	if (!txb)
210 		return NULL;
211 
212 	txb->nr_frags = nr_frags;
213 	txb->frag_size = cpu_to_le16(txb_size);
214 
215 	for (i = 0; i < nr_frags; i++) {
216 		txb->fragments[i] = dev_alloc_skb(txb_size);
217 		if (unlikely(!txb->fragments[i]))
218 			goto err_free;
219 		memset(txb->fragments[i]->cb, 0, sizeof(txb->fragments[i]->cb));
220 	}
221 
222 	return txb;
223 
224 err_free:
225 	while (--i >= 0)
226 		dev_kfree_skb_any(txb->fragments[i]);
227 	kfree(txb);
228 
229 	return NULL;
230 }
231 
232 static int rtllib_classify(struct sk_buff *skb, u8 bIsAmsdu)
233 {
234 	struct ethhdr *eth;
235 	struct iphdr *ip;
236 
237 	eth = (struct ethhdr *)skb->data;
238 	if (eth->h_proto != htons(ETH_P_IP))
239 		return 0;
240 
241 #ifdef VERBOSE_DEBUG
242 	print_hex_dump_bytes("%s: ", __func__, DUMP_PREFIX_NONE, skb->data,
243 			     skb->len);
244 #endif
245 	ip = ip_hdr(skb);
246 	switch (ip->tos & 0xfc) {
247 	case 0x20:
248 		return 2;
249 	case 0x40:
250 		return 1;
251 	case 0x60:
252 		return 3;
253 	case 0x80:
254 		return 4;
255 	case 0xa0:
256 		return 5;
257 	case 0xc0:
258 		return 6;
259 	case 0xe0:
260 		return 7;
261 	default:
262 		return 0;
263 	}
264 }
265 
266 static void rtllib_tx_query_agg_cap(struct rtllib_device *ieee,
267 				    struct sk_buff *skb,
268 				    struct cb_desc *tcb_desc)
269 {
270 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
271 	struct tx_ts_record *pTxTs = NULL;
272 	struct rtllib_hdr_1addr *hdr = (struct rtllib_hdr_1addr *)skb->data;
273 
274 	if (rtllib_act_scanning(ieee, false))
275 		return;
276 
277 	if (!pHTInfo->bCurrentHTSupport || !pHTInfo->bEnableHT)
278 		return;
279 	if (!IsQoSDataFrame(skb->data))
280 		return;
281 	if (is_multicast_ether_addr(hdr->addr1))
282 		return;
283 
284 	if (tcb_desc->bdhcp || ieee->CntAfterLink < 2)
285 		return;
286 
287 	if (pHTInfo->iot_action & HT_IOT_ACT_TX_NO_AGGREGATION)
288 		return;
289 
290 	if (!ieee->GetNmodeSupportBySecCfg(ieee->dev))
291 		return;
292 	if (pHTInfo->bCurrentAMPDUEnable) {
293 		if (!GetTs(ieee, (struct ts_common_info **)(&pTxTs), hdr->addr1,
294 		    skb->priority, TX_DIR, true)) {
295 			netdev_info(ieee->dev, "%s: can't get TS\n", __func__);
296 			return;
297 		}
298 		if (!pTxTs->TxAdmittedBARecord.b_valid) {
299 			if (ieee->wpa_ie_len && (ieee->pairwise_key_type ==
300 			    KEY_TYPE_NA)) {
301 				;
302 			} else if (tcb_desc->bdhcp == 1) {
303 				;
304 			} else if (!pTxTs->bDisable_AddBa) {
305 				TsStartAddBaProcess(ieee, pTxTs);
306 			}
307 			goto FORCED_AGG_SETTING;
308 		} else if (!pTxTs->bUsingBa) {
309 			if (SN_LESS(pTxTs->TxAdmittedBARecord.ba_start_seq_ctrl.field.seq_num,
310 			   (pTxTs->TxCurSeq+1)%4096))
311 				pTxTs->bUsingBa = true;
312 			else
313 				goto FORCED_AGG_SETTING;
314 		}
315 		if (ieee->iw_mode == IW_MODE_INFRA) {
316 			tcb_desc->bAMPDUEnable = true;
317 			tcb_desc->ampdu_factor = pHTInfo->CurrentAMPDUFactor;
318 			tcb_desc->ampdu_density = pHTInfo->current_mpdu_density;
319 		}
320 	}
321 FORCED_AGG_SETTING:
322 	switch (pHTInfo->ForcedAMPDUMode) {
323 	case HT_AGG_AUTO:
324 		break;
325 
326 	case HT_AGG_FORCE_ENABLE:
327 		tcb_desc->bAMPDUEnable = true;
328 		tcb_desc->ampdu_density = pHTInfo->forced_mpdu_density;
329 		tcb_desc->ampdu_factor = pHTInfo->forced_ampdu_factor;
330 		break;
331 
332 	case HT_AGG_FORCE_DISABLE:
333 		tcb_desc->bAMPDUEnable = false;
334 		tcb_desc->ampdu_density = 0;
335 		tcb_desc->ampdu_factor = 0;
336 		break;
337 	}
338 }
339 
340 static void rtllib_query_ShortPreambleMode(struct rtllib_device *ieee,
341 					   struct cb_desc *tcb_desc)
342 {
343 	tcb_desc->bUseShortPreamble = false;
344 	if (tcb_desc->data_rate == 2)
345 		return;
346 	else if (ieee->current_network.capability &
347 		 WLAN_CAPABILITY_SHORT_PREAMBLE)
348 		tcb_desc->bUseShortPreamble = true;
349 }
350 
351 static void rtllib_query_HTCapShortGI(struct rtllib_device *ieee,
352 				      struct cb_desc *tcb_desc)
353 {
354 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
355 
356 	tcb_desc->bUseShortGI		= false;
357 
358 	if (!pHTInfo->bCurrentHTSupport || !pHTInfo->bEnableHT)
359 		return;
360 
361 	if (pHTInfo->forced_short_gi) {
362 		tcb_desc->bUseShortGI = true;
363 		return;
364 	}
365 
366 	if (pHTInfo->bCurBW40MHz && pHTInfo->bCurShortGI40MHz)
367 		tcb_desc->bUseShortGI = true;
368 	else if (!pHTInfo->bCurBW40MHz && pHTInfo->bCurShortGI20MHz)
369 		tcb_desc->bUseShortGI = true;
370 }
371 
372 static void rtllib_query_BandwidthMode(struct rtllib_device *ieee,
373 				       struct cb_desc *tcb_desc)
374 {
375 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
376 
377 	tcb_desc->bPacketBW = false;
378 
379 	if (!pHTInfo->bCurrentHTSupport || !pHTInfo->bEnableHT)
380 		return;
381 
382 	if (tcb_desc->bMulticast || tcb_desc->bBroadcast)
383 		return;
384 
385 	if ((tcb_desc->data_rate & 0x80) == 0)
386 		return;
387 	if (pHTInfo->bCurBW40MHz && pHTInfo->cur_tx_bw40mhz &&
388 	    !ieee->bandwidth_auto_switch.bforced_tx20Mhz)
389 		tcb_desc->bPacketBW = true;
390 }
391 
392 static void rtllib_query_protectionmode(struct rtllib_device *ieee,
393 					struct cb_desc *tcb_desc,
394 					struct sk_buff *skb)
395 {
396 	struct rt_hi_throughput *pHTInfo;
397 
398 	tcb_desc->bRTSSTBC			= false;
399 	tcb_desc->bRTSUseShortGI		= false;
400 	tcb_desc->bCTSEnable			= false;
401 	tcb_desc->RTSSC				= 0;
402 	tcb_desc->bRTSBW			= false;
403 
404 	if (tcb_desc->bBroadcast || tcb_desc->bMulticast)
405 		return;
406 
407 	if (is_broadcast_ether_addr(skb->data+16))
408 		return;
409 
410 	if (ieee->mode < IEEE_N_24G) {
411 		if (skb->len > ieee->rts) {
412 			tcb_desc->bRTSEnable = true;
413 			tcb_desc->rts_rate = MGN_24M;
414 		} else if (ieee->current_network.buseprotection) {
415 			tcb_desc->bRTSEnable = true;
416 			tcb_desc->bCTSEnable = true;
417 			tcb_desc->rts_rate = MGN_24M;
418 		}
419 		return;
420 	}
421 
422 	pHTInfo = ieee->pHTInfo;
423 
424 	while (true) {
425 		if (pHTInfo->iot_action & HT_IOT_ACT_FORCED_CTS2SELF) {
426 			tcb_desc->bCTSEnable	= true;
427 			tcb_desc->rts_rate  =	MGN_24M;
428 			tcb_desc->bRTSEnable = true;
429 			break;
430 		} else if (pHTInfo->iot_action & (HT_IOT_ACT_FORCED_RTS |
431 			   HT_IOT_ACT_PURE_N_MODE)) {
432 			tcb_desc->bRTSEnable = true;
433 			tcb_desc->rts_rate  =	MGN_24M;
434 			break;
435 		}
436 		if (ieee->current_network.buseprotection) {
437 			tcb_desc->bRTSEnable = true;
438 			tcb_desc->bCTSEnable = true;
439 			tcb_desc->rts_rate = MGN_24M;
440 			break;
441 		}
442 		if (pHTInfo->bCurrentHTSupport  && pHTInfo->bEnableHT) {
443 			u8 HTOpMode = pHTInfo->current_op_mode;
444 
445 			if ((pHTInfo->bCurBW40MHz && (HTOpMode == 2 ||
446 			     HTOpMode == 3)) ||
447 			     (!pHTInfo->bCurBW40MHz && HTOpMode == 3)) {
448 				tcb_desc->rts_rate = MGN_24M;
449 				tcb_desc->bRTSEnable = true;
450 				break;
451 			}
452 		}
453 		if (skb->len > ieee->rts) {
454 			tcb_desc->rts_rate = MGN_24M;
455 			tcb_desc->bRTSEnable = true;
456 			break;
457 		}
458 		if (tcb_desc->bAMPDUEnable) {
459 			tcb_desc->rts_rate = MGN_24M;
460 			tcb_desc->bRTSEnable = false;
461 			break;
462 		}
463 		goto NO_PROTECTION;
464 	}
465 	if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
466 		tcb_desc->bUseShortPreamble = true;
467 	if (ieee->iw_mode == IW_MODE_MASTER)
468 		goto NO_PROTECTION;
469 	return;
470 NO_PROTECTION:
471 	tcb_desc->bRTSEnable	= false;
472 	tcb_desc->bCTSEnable	= false;
473 	tcb_desc->rts_rate	= 0;
474 	tcb_desc->RTSSC		= 0;
475 	tcb_desc->bRTSBW	= false;
476 }
477 
478 
479 static void rtllib_txrate_selectmode(struct rtllib_device *ieee,
480 				     struct cb_desc *tcb_desc)
481 {
482 	if (ieee->bTxDisableRateFallBack)
483 		tcb_desc->bTxDisableRateFallBack = true;
484 
485 	if (ieee->bTxUseDriverAssingedRate)
486 		tcb_desc->bTxUseDriverAssingedRate = true;
487 	if (!tcb_desc->bTxDisableRateFallBack ||
488 	    !tcb_desc->bTxUseDriverAssingedRate) {
489 		if (ieee->iw_mode == IW_MODE_INFRA ||
490 		    ieee->iw_mode == IW_MODE_ADHOC)
491 			tcb_desc->RATRIndex = 0;
492 	}
493 }
494 
495 static u16 rtllib_query_seqnum(struct rtllib_device *ieee, struct sk_buff *skb,
496 			       u8 *dst)
497 {
498 	u16 seqnum = 0;
499 
500 	if (is_multicast_ether_addr(dst))
501 		return 0;
502 	if (IsQoSDataFrame(skb->data)) {
503 		struct tx_ts_record *pTS = NULL;
504 
505 		if (!GetTs(ieee, (struct ts_common_info **)(&pTS), dst,
506 		    skb->priority, TX_DIR, true))
507 			return 0;
508 		seqnum = pTS->TxCurSeq;
509 		pTS->TxCurSeq = (pTS->TxCurSeq+1)%4096;
510 		return seqnum;
511 	}
512 	return 0;
513 }
514 
515 static int wme_downgrade_ac(struct sk_buff *skb)
516 {
517 	switch (skb->priority) {
518 	case 6:
519 	case 7:
520 		skb->priority = 5; /* VO -> VI */
521 		return 0;
522 	case 4:
523 	case 5:
524 		skb->priority = 3; /* VI -> BE */
525 		return 0;
526 	case 0:
527 	case 3:
528 		skb->priority = 1; /* BE -> BK */
529 		return 0;
530 	default:
531 		return -1;
532 	}
533 }
534 
535 static u8 rtllib_current_rate(struct rtllib_device *ieee)
536 {
537 	if (ieee->mode & IEEE_MODE_MASK)
538 		return ieee->rate;
539 
540 	if (ieee->HTCurrentOperaRate)
541 		return ieee->HTCurrentOperaRate;
542 	else
543 		return ieee->rate & 0x7F;
544 }
545 
546 static int rtllib_xmit_inter(struct sk_buff *skb, struct net_device *dev)
547 {
548 	struct rtllib_device *ieee = (struct rtllib_device *)
549 				     netdev_priv_rsl(dev);
550 	struct rtllib_txb *txb = NULL;
551 	struct rtllib_hdr_3addrqos *frag_hdr;
552 	int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size;
553 	unsigned long flags;
554 	struct net_device_stats *stats = &ieee->stats;
555 	int ether_type = 0, encrypt;
556 	int bytes, fc, qos_ctl = 0, hdr_len;
557 	struct sk_buff *skb_frag;
558 	struct rtllib_hdr_3addrqos header = { /* Ensure zero initialized */
559 		.duration_id = 0,
560 		.seq_ctl = 0,
561 		.qos_ctl = 0
562 	};
563 	int qos_activated = ieee->current_network.qos_data.active;
564 	u8 dest[ETH_ALEN];
565 	u8 src[ETH_ALEN];
566 	struct lib80211_crypt_data *crypt = NULL;
567 	struct cb_desc *tcb_desc;
568 	u8 bIsMulticast = false;
569 	u8 IsAmsdu = false;
570 	bool	bdhcp = false;
571 
572 	spin_lock_irqsave(&ieee->lock, flags);
573 
574 	/* If there is no driver handler to take the TXB, don't bother
575 	 * creating it...
576 	 */
577 	if ((!ieee->hard_start_xmit && !(ieee->softmac_features &
578 	   IEEE_SOFTMAC_TX_QUEUE)) ||
579 	   ((!ieee->softmac_data_hard_start_xmit &&
580 	   (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)))) {
581 		netdev_warn(ieee->dev, "No xmit handler.\n");
582 		goto success;
583 	}
584 
585 
586 	if (likely(ieee->raw_tx == 0)) {
587 		if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
588 			netdev_warn(ieee->dev, "skb too small (%d).\n",
589 				    skb->len);
590 			goto success;
591 		}
592 		/* Save source and destination addresses */
593 		ether_addr_copy(dest, skb->data);
594 		ether_addr_copy(src, skb->data + ETH_ALEN);
595 
596 		memset(skb->cb, 0, sizeof(skb->cb));
597 		ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
598 
599 		if (ieee->iw_mode == IW_MODE_MONITOR) {
600 			txb = rtllib_alloc_txb(1, skb->len, GFP_ATOMIC);
601 			if (unlikely(!txb)) {
602 				netdev_warn(ieee->dev,
603 					    "Could not allocate TXB\n");
604 				goto failed;
605 			}
606 
607 			txb->encrypted = 0;
608 			txb->payload_size = cpu_to_le16(skb->len);
609 			skb_put_data(txb->fragments[0], skb->data, skb->len);
610 
611 			goto success;
612 		}
613 
614 		if (skb->len > 282) {
615 			if (ether_type == ETH_P_IP) {
616 				const struct iphdr *ip = (struct iphdr *)
617 					((u8 *)skb->data+14);
618 				if (ip->protocol == IPPROTO_UDP) {
619 					struct udphdr *udp;
620 
621 					udp = (struct udphdr *)((u8 *)ip +
622 					      (ip->ihl << 2));
623 					if (((((u8 *)udp)[1] == 68) &&
624 					   (((u8 *)udp)[3] == 67)) ||
625 					   ((((u8 *)udp)[1] == 67) &&
626 					   (((u8 *)udp)[3] == 68))) {
627 						bdhcp = true;
628 						ieee->LPSDelayCnt = 200;
629 					}
630 				}
631 			} else if (ether_type == ETH_P_ARP) {
632 				netdev_info(ieee->dev,
633 					    "=================>DHCP Protocol start tx ARP pkt!!\n");
634 				bdhcp = true;
635 				ieee->LPSDelayCnt =
636 					 ieee->current_network.tim.tim_count;
637 			}
638 		}
639 
640 		skb->priority = rtllib_classify(skb, IsAmsdu);
641 		crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
642 		encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) &&
643 			ieee->host_encrypt && crypt && crypt->ops;
644 		if (!encrypt && ieee->ieee802_1x &&
645 		    ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
646 			stats->tx_dropped++;
647 			goto success;
648 		}
649 		if (crypt && !encrypt && ether_type == ETH_P_PAE) {
650 			struct eapol *eap = (struct eapol *)(skb->data +
651 				sizeof(struct ethhdr) - SNAP_SIZE -
652 				sizeof(u16));
653 			netdev_dbg(ieee->dev,
654 				   "TX: IEEE 802.11 EAPOL frame: %s\n",
655 				   eap_get_type(eap->type));
656 		}
657 
658 		/* Advance the SKB to the start of the payload */
659 		skb_pull(skb, sizeof(struct ethhdr));
660 
661 		/* Determine total amount of storage required for TXB packets */
662 		bytes = skb->len + SNAP_SIZE + sizeof(u16);
663 
664 		if (encrypt)
665 			fc = RTLLIB_FTYPE_DATA | RTLLIB_FCTL_WEP;
666 		else
667 			fc = RTLLIB_FTYPE_DATA;
668 
669 		if (qos_activated)
670 			fc |= RTLLIB_STYPE_QOS_DATA;
671 		else
672 			fc |= RTLLIB_STYPE_DATA;
673 
674 		if (ieee->iw_mode == IW_MODE_INFRA) {
675 			fc |= RTLLIB_FCTL_TODS;
676 			/* To DS: Addr1 = BSSID, Addr2 = SA,
677 			 * Addr3 = DA
678 			 */
679 			ether_addr_copy(header.addr1,
680 					ieee->current_network.bssid);
681 			ether_addr_copy(header.addr2, src);
682 			if (IsAmsdu)
683 				ether_addr_copy(header.addr3,
684 						ieee->current_network.bssid);
685 			else
686 				ether_addr_copy(header.addr3, dest);
687 		} else if (ieee->iw_mode == IW_MODE_ADHOC) {
688 			/* not From/To DS: Addr1 = DA, Addr2 = SA,
689 			 * Addr3 = BSSID
690 			 */
691 			ether_addr_copy(header.addr1, dest);
692 			ether_addr_copy(header.addr2, src);
693 			ether_addr_copy(header.addr3,
694 					ieee->current_network.bssid);
695 		}
696 
697 		bIsMulticast = is_multicast_ether_addr(header.addr1);
698 
699 		header.frame_ctl = cpu_to_le16(fc);
700 
701 		/* Determine fragmentation size based on destination (multicast
702 		 * and broadcast are not fragmented)
703 		 */
704 		if (bIsMulticast) {
705 			frag_size = MAX_FRAG_THRESHOLD;
706 			qos_ctl |= QOS_CTL_NOTCONTAIN_ACK;
707 		} else {
708 			frag_size = ieee->fts;
709 			qos_ctl = 0;
710 		}
711 
712 		if (qos_activated) {
713 			hdr_len = RTLLIB_3ADDR_LEN + 2;
714 
715 			/* in case we are a client verify acm is not set for this ac */
716 			while (unlikely(ieee->wmm_acm & (0x01 << skb->priority))) {
717 				netdev_info(ieee->dev, "skb->priority = %x\n",
718 						skb->priority);
719 				if (wme_downgrade_ac(skb))
720 					break;
721 				netdev_info(ieee->dev, "converted skb->priority = %x\n",
722 					   skb->priority);
723 			}
724 
725 			qos_ctl |= skb->priority;
726 			header.qos_ctl = cpu_to_le16(qos_ctl & RTLLIB_QOS_TID);
727 
728 		} else {
729 			hdr_len = RTLLIB_3ADDR_LEN;
730 		}
731 		/* Determine amount of payload per fragment.  Regardless of if
732 		 * this stack is providing the full 802.11 header, one will
733 		 * eventually be affixed to this fragment -- so we must account
734 		 * for it when determining the amount of payload space.
735 		 */
736 		bytes_per_frag = frag_size - hdr_len;
737 		if (ieee->config &
738 		   (CFG_RTLLIB_COMPUTE_FCS | CFG_RTLLIB_RESERVE_FCS))
739 			bytes_per_frag -= RTLLIB_FCS_LEN;
740 
741 		/* Each fragment may need to have room for encrypting
742 		 * pre/postfix
743 		 */
744 		if (encrypt) {
745 			bytes_per_frag -= crypt->ops->extra_mpdu_prefix_len +
746 				crypt->ops->extra_mpdu_postfix_len +
747 				crypt->ops->extra_msdu_prefix_len +
748 				crypt->ops->extra_msdu_postfix_len;
749 		}
750 		/* Number of fragments is the total bytes_per_frag /
751 		 * payload_per_fragment
752 		 */
753 		nr_frags = bytes / bytes_per_frag;
754 		bytes_last_frag = bytes % bytes_per_frag;
755 		if (bytes_last_frag)
756 			nr_frags++;
757 		else
758 			bytes_last_frag = bytes_per_frag;
759 
760 		/* When we allocate the TXB we allocate enough space for the
761 		 * reserve and full fragment bytes (bytes_per_frag doesn't
762 		 * include prefix, postfix, header, FCS, etc.)
763 		 */
764 		txb = rtllib_alloc_txb(nr_frags, frag_size +
765 				       ieee->tx_headroom, GFP_ATOMIC);
766 		if (unlikely(!txb)) {
767 			netdev_warn(ieee->dev, "Could not allocate TXB\n");
768 			goto failed;
769 		}
770 		txb->encrypted = encrypt;
771 		txb->payload_size = cpu_to_le16(bytes);
772 
773 		if (qos_activated)
774 			txb->queue_index = UP2AC(skb->priority);
775 		else
776 			txb->queue_index = WME_AC_BE;
777 
778 		for (i = 0; i < nr_frags; i++) {
779 			skb_frag = txb->fragments[i];
780 			tcb_desc = (struct cb_desc *)(skb_frag->cb +
781 				    MAX_DEV_ADDR_SIZE);
782 			if (qos_activated) {
783 				skb_frag->priority = skb->priority;
784 				tcb_desc->queue_index =  UP2AC(skb->priority);
785 			} else {
786 				skb_frag->priority = WME_AC_BE;
787 				tcb_desc->queue_index = WME_AC_BE;
788 			}
789 			skb_reserve(skb_frag, ieee->tx_headroom);
790 
791 			if (encrypt) {
792 				if (ieee->hwsec_active)
793 					tcb_desc->bHwSec = 1;
794 				else
795 					tcb_desc->bHwSec = 0;
796 				skb_reserve(skb_frag,
797 					    crypt->ops->extra_mpdu_prefix_len +
798 					    crypt->ops->extra_msdu_prefix_len);
799 			} else {
800 				tcb_desc->bHwSec = 0;
801 			}
802 			frag_hdr = skb_put_data(skb_frag, &header, hdr_len);
803 
804 			/* If this is not the last fragment, then add the
805 			 * MOREFRAGS bit to the frame control
806 			 */
807 			if (i != nr_frags - 1) {
808 				frag_hdr->frame_ctl = cpu_to_le16(
809 					fc | RTLLIB_FCTL_MOREFRAGS);
810 				bytes = bytes_per_frag;
811 
812 			} else {
813 				/* The last fragment has the remaining length */
814 				bytes = bytes_last_frag;
815 			}
816 			if ((qos_activated) && (!bIsMulticast)) {
817 				frag_hdr->seq_ctl =
818 					 cpu_to_le16(rtllib_query_seqnum(ieee, skb_frag,
819 							     header.addr1));
820 				frag_hdr->seq_ctl =
821 					 cpu_to_le16(le16_to_cpu(frag_hdr->seq_ctl)<<4 | i);
822 			} else {
823 				frag_hdr->seq_ctl =
824 					 cpu_to_le16(ieee->seq_ctrl[0]<<4 | i);
825 			}
826 			/* Put a SNAP header on the first fragment */
827 			if (i == 0) {
828 				rtllib_put_snap(
829 					skb_put(skb_frag, SNAP_SIZE +
830 					sizeof(u16)), ether_type);
831 				bytes -= SNAP_SIZE + sizeof(u16);
832 			}
833 
834 			skb_put_data(skb_frag, skb->data, bytes);
835 
836 			/* Advance the SKB... */
837 			skb_pull(skb, bytes);
838 
839 			/* Encryption routine will move the header forward in
840 			 * order to insert the IV between the header and the
841 			 * payload
842 			 */
843 			if (encrypt)
844 				rtllib_encrypt_fragment(ieee, skb_frag,
845 							hdr_len);
846 			if (ieee->config &
847 			   (CFG_RTLLIB_COMPUTE_FCS | CFG_RTLLIB_RESERVE_FCS))
848 				skb_put(skb_frag, 4);
849 		}
850 
851 		if ((qos_activated) && (!bIsMulticast)) {
852 			if (ieee->seq_ctrl[UP2AC(skb->priority) + 1] == 0xFFF)
853 				ieee->seq_ctrl[UP2AC(skb->priority) + 1] = 0;
854 			else
855 				ieee->seq_ctrl[UP2AC(skb->priority) + 1]++;
856 		} else {
857 			if (ieee->seq_ctrl[0] == 0xFFF)
858 				ieee->seq_ctrl[0] = 0;
859 			else
860 				ieee->seq_ctrl[0]++;
861 		}
862 	} else {
863 		if (unlikely(skb->len < sizeof(struct rtllib_hdr_3addr))) {
864 			netdev_warn(ieee->dev, "skb too small (%d).\n",
865 				    skb->len);
866 			goto success;
867 		}
868 
869 		txb = rtllib_alloc_txb(1, skb->len, GFP_ATOMIC);
870 		if (!txb) {
871 			netdev_warn(ieee->dev, "Could not allocate TXB\n");
872 			goto failed;
873 		}
874 
875 		txb->encrypted = 0;
876 		txb->payload_size = cpu_to_le16(skb->len);
877 		skb_put_data(txb->fragments[0], skb->data, skb->len);
878 	}
879 
880  success:
881 	if (txb) {
882 		tcb_desc = (struct cb_desc *)
883 				(txb->fragments[0]->cb + MAX_DEV_ADDR_SIZE);
884 		tcb_desc->bTxEnableFwCalcDur = 1;
885 		tcb_desc->priority = skb->priority;
886 
887 		if (ether_type == ETH_P_PAE) {
888 			if (ieee->pHTInfo->iot_action &
889 			    HT_IOT_ACT_WA_IOT_Broadcom) {
890 				tcb_desc->data_rate =
891 					 MgntQuery_TxRateExcludeCCKRates(ieee);
892 				tcb_desc->bTxDisableRateFallBack = false;
893 			} else {
894 				tcb_desc->data_rate = ieee->basic_rate;
895 				tcb_desc->bTxDisableRateFallBack = 1;
896 			}
897 
898 
899 			tcb_desc->RATRIndex = 7;
900 			tcb_desc->bTxUseDriverAssingedRate = 1;
901 		} else {
902 			if (is_multicast_ether_addr(header.addr1))
903 				tcb_desc->bMulticast = 1;
904 			if (is_broadcast_ether_addr(header.addr1))
905 				tcb_desc->bBroadcast = 1;
906 			rtllib_txrate_selectmode(ieee, tcb_desc);
907 			if (tcb_desc->bMulticast ||  tcb_desc->bBroadcast)
908 				tcb_desc->data_rate = ieee->basic_rate;
909 			else
910 				tcb_desc->data_rate = rtllib_current_rate(ieee);
911 
912 			if (bdhcp) {
913 				if (ieee->pHTInfo->iot_action &
914 				    HT_IOT_ACT_WA_IOT_Broadcom) {
915 					tcb_desc->data_rate =
916 					   MgntQuery_TxRateExcludeCCKRates(ieee);
917 					tcb_desc->bTxDisableRateFallBack = false;
918 				} else {
919 					tcb_desc->data_rate = MGN_1M;
920 					tcb_desc->bTxDisableRateFallBack = 1;
921 				}
922 
923 
924 				tcb_desc->RATRIndex = 7;
925 				tcb_desc->bTxUseDriverAssingedRate = 1;
926 				tcb_desc->bdhcp = 1;
927 			}
928 
929 			rtllib_query_ShortPreambleMode(ieee, tcb_desc);
930 			rtllib_tx_query_agg_cap(ieee, txb->fragments[0],
931 						tcb_desc);
932 			rtllib_query_HTCapShortGI(ieee, tcb_desc);
933 			rtllib_query_BandwidthMode(ieee, tcb_desc);
934 			rtllib_query_protectionmode(ieee, tcb_desc,
935 						    txb->fragments[0]);
936 		}
937 	}
938 	spin_unlock_irqrestore(&ieee->lock, flags);
939 	dev_kfree_skb_any(skb);
940 	if (txb) {
941 		if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE) {
942 			dev->stats.tx_packets++;
943 			dev->stats.tx_bytes += le16_to_cpu(txb->payload_size);
944 			rtllib_softmac_xmit(txb, ieee);
945 		} else {
946 			if ((*ieee->hard_start_xmit)(txb, dev) == 0) {
947 				stats->tx_packets++;
948 				stats->tx_bytes += le16_to_cpu(txb->payload_size);
949 				return 0;
950 			}
951 			rtllib_txb_free(txb);
952 		}
953 	}
954 
955 	return 0;
956 
957  failed:
958 	spin_unlock_irqrestore(&ieee->lock, flags);
959 	netif_stop_queue(dev);
960 	stats->tx_errors++;
961 	return 1;
962 
963 }
964 
965 netdev_tx_t rtllib_xmit(struct sk_buff *skb, struct net_device *dev)
966 {
967 	memset(skb->cb, 0, sizeof(skb->cb));
968 	return rtllib_xmit_inter(skb, dev) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
969 }
970 EXPORT_SYMBOL(rtllib_xmit);
971