1 // SPDX-License-Identifier: GPL-2.0
2 /* IEEE 802.11 SoftMAC layer
3  * Copyright (c) 2005 Andrea Merello <andrea.merello@gmail.com>
4  *
5  * Mostly extracted from the rtl8180-sa2400 driver for the
6  * in-kernel generic ieee802.11 stack.
7  *
8  * Few lines might be stolen from other part of the rtllib
9  * stack. Copyright who own it's copyright
10  *
11  * WPA code stolen from the ipw2200 driver.
12  * Copyright who own it's copyright.
13  */
14 #include "rtllib.h"
15 
16 #include <linux/random.h>
17 #include <linux/delay.h>
18 #include <linux/uaccess.h>
19 #include <linux/etherdevice.h>
20 #include <linux/ieee80211.h>
21 #include "dot11d.h"
22 
23 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl);
24 
25 
26 static short rtllib_is_54g(struct rtllib_network *net)
27 {
28 	return (net->rates_ex_len > 0) || (net->rates_len > 4);
29 }
30 
31 /* returns the total length needed for placing the RATE MFIE
32  * tag and the EXTENDED RATE MFIE tag if needed.
33  * It encludes two bytes per tag for the tag itself and its len
34  */
35 static unsigned int rtllib_MFIE_rate_len(struct rtllib_device *ieee)
36 {
37 	unsigned int rate_len = 0;
38 
39 	if (ieee->modulation & RTLLIB_CCK_MODULATION)
40 		rate_len = RTLLIB_CCK_RATE_LEN + 2;
41 
42 	if (ieee->modulation & RTLLIB_OFDM_MODULATION)
43 
44 		rate_len += RTLLIB_OFDM_RATE_LEN + 2;
45 
46 	return rate_len;
47 }
48 
49 /* place the MFIE rate, tag to the memory (double) pointed.
50  * Then it updates the pointer so that
51  * it points after the new MFIE tag added.
52  */
53 static void rtllib_MFIE_Brate(struct rtllib_device *ieee, u8 **tag_p)
54 {
55 	u8 *tag = *tag_p;
56 
57 	if (ieee->modulation & RTLLIB_CCK_MODULATION) {
58 		*tag++ = MFIE_TYPE_RATES;
59 		*tag++ = 4;
60 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
61 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
62 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
63 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
64 	}
65 
66 	/* We may add an option for custom rates that specific HW
67 	 * might support
68 	 */
69 	*tag_p = tag;
70 }
71 
72 static void rtllib_MFIE_Grate(struct rtllib_device *ieee, u8 **tag_p)
73 {
74 	u8 *tag = *tag_p;
75 
76 	if (ieee->modulation & RTLLIB_OFDM_MODULATION) {
77 		*tag++ = MFIE_TYPE_RATES_EX;
78 		*tag++ = 8;
79 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_6MB;
80 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_9MB;
81 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_12MB;
82 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_18MB;
83 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_24MB;
84 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_36MB;
85 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_48MB;
86 		*tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_54MB;
87 	}
88 	/* We may add an option for custom rates that specific HW might
89 	 * support
90 	 */
91 	*tag_p = tag;
92 }
93 
94 static void rtllib_WMM_Info(struct rtllib_device *ieee, u8 **tag_p)
95 {
96 	u8 *tag = *tag_p;
97 
98 	*tag++ = MFIE_TYPE_GENERIC;
99 	*tag++ = 7;
100 	*tag++ = 0x00;
101 	*tag++ = 0x50;
102 	*tag++ = 0xf2;
103 	*tag++ = 0x02;
104 	*tag++ = 0x00;
105 	*tag++ = 0x01;
106 	*tag++ = MAX_SP_Len;
107 	*tag_p = tag;
108 }
109 
110 static void rtllib_TURBO_Info(struct rtllib_device *ieee, u8 **tag_p)
111 {
112 	u8 *tag = *tag_p;
113 
114 	*tag++ = MFIE_TYPE_GENERIC;
115 	*tag++ = 7;
116 	*tag++ = 0x00;
117 	*tag++ = 0xe0;
118 	*tag++ = 0x4c;
119 	*tag++ = 0x01;
120 	*tag++ = 0x02;
121 	*tag++ = 0x11;
122 	*tag++ = 0x00;
123 
124 	*tag_p = tag;
125 	netdev_alert(ieee->dev, "This is enable turbo mode IE process\n");
126 }
127 
128 static void enqueue_mgmt(struct rtllib_device *ieee, struct sk_buff *skb)
129 {
130 	int nh;
131 
132 	nh = (ieee->mgmt_queue_head + 1) % MGMT_QUEUE_NUM;
133 
134 /* if the queue is full but we have newer frames then
135  * just overwrites the oldest.
136  *
137  * if (nh == ieee->mgmt_queue_tail)
138  *		return -1;
139  */
140 	ieee->mgmt_queue_head = nh;
141 	ieee->mgmt_queue_ring[nh] = skb;
142 
143 }
144 
145 static void init_mgmt_queue(struct rtllib_device *ieee)
146 {
147 	ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
148 }
149 
150 
151 u8
152 MgntQuery_TxRateExcludeCCKRates(struct rtllib_device *ieee)
153 {
154 	u16	i;
155 	u8	QueryRate = 0;
156 	u8	BasicRate;
157 
158 
159 	for (i = 0; i < ieee->current_network.rates_len; i++) {
160 		BasicRate = ieee->current_network.rates[i]&0x7F;
161 		if (!rtllib_is_cck_rate(BasicRate)) {
162 			if (QueryRate == 0) {
163 				QueryRate = BasicRate;
164 			} else {
165 				if (BasicRate < QueryRate)
166 					QueryRate = BasicRate;
167 			}
168 		}
169 	}
170 
171 	if (QueryRate == 0) {
172 		QueryRate = 12;
173 		netdev_info(ieee->dev, "No BasicRate found!!\n");
174 	}
175 	return QueryRate;
176 }
177 
178 static u8 MgntQuery_MgntFrameTxRate(struct rtllib_device *ieee)
179 {
180 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
181 	u8 rate;
182 
183 	if (pHTInfo->IOTAction & HT_IOT_ACT_MGNT_USE_CCK_6M)
184 		rate = 0x0c;
185 	else
186 		rate = ieee->basic_rate & 0x7f;
187 
188 	if (rate == 0) {
189 		if (ieee->mode == IEEE_A ||
190 		   ieee->mode == IEEE_N_5G ||
191 		   (ieee->mode == IEEE_N_24G && !pHTInfo->bCurSuppCCK))
192 			rate = 0x0c;
193 		else
194 			rate = 0x02;
195 	}
196 
197 	return rate;
198 }
199 
200 inline void softmac_mgmt_xmit(struct sk_buff *skb, struct rtllib_device *ieee)
201 {
202 	unsigned long flags;
203 	short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
204 	struct rtllib_hdr_3addr  *header =
205 		(struct rtllib_hdr_3addr  *) skb->data;
206 
207 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
208 
209 	spin_lock_irqsave(&ieee->lock, flags);
210 
211 	/* called with 2nd param 0, no mgmt lock required */
212 	rtllib_sta_wakeup(ieee, 0);
213 
214 	if (le16_to_cpu(header->frame_ctl) == RTLLIB_STYPE_BEACON)
215 		tcb_desc->queue_index = BEACON_QUEUE;
216 	else
217 		tcb_desc->queue_index = MGNT_QUEUE;
218 
219 	if (ieee->disable_mgnt_queue)
220 		tcb_desc->queue_index = HIGH_QUEUE;
221 
222 	tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
223 	tcb_desc->RATRIndex = 7;
224 	tcb_desc->bTxDisableRateFallBack = 1;
225 	tcb_desc->bTxUseDriverAssingedRate = 1;
226 	if (single) {
227 		if (ieee->queue_stop) {
228 			enqueue_mgmt(ieee, skb);
229 		} else {
230 			header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0]<<4);
231 
232 			if (ieee->seq_ctrl[0] == 0xFFF)
233 				ieee->seq_ctrl[0] = 0;
234 			else
235 				ieee->seq_ctrl[0]++;
236 
237 			/* avoid watchdog triggers */
238 			ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
239 							   ieee->basic_rate);
240 		}
241 
242 		spin_unlock_irqrestore(&ieee->lock, flags);
243 	} else {
244 		spin_unlock_irqrestore(&ieee->lock, flags);
245 		spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
246 
247 		header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
248 
249 		if (ieee->seq_ctrl[0] == 0xFFF)
250 			ieee->seq_ctrl[0] = 0;
251 		else
252 			ieee->seq_ctrl[0]++;
253 
254 		/* check whether the managed packet queued greater than 5 */
255 		if (!ieee->check_nic_enough_desc(ieee->dev,
256 						 tcb_desc->queue_index) ||
257 		    skb_queue_len(&ieee->skb_waitQ[tcb_desc->queue_index]) ||
258 		    ieee->queue_stop) {
259 			/* insert the skb packet to the management queue
260 			 *
261 			 * as for the completion function, it does not need
262 			 * to check it any more.
263 			 */
264 			netdev_info(ieee->dev,
265 			       "%s():insert to waitqueue, queue_index:%d!\n",
266 			       __func__, tcb_desc->queue_index);
267 			skb_queue_tail(&ieee->skb_waitQ[tcb_desc->queue_index],
268 				       skb);
269 		} else {
270 			ieee->softmac_hard_start_xmit(skb, ieee->dev);
271 		}
272 		spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
273 	}
274 }
275 
276 static inline void
277 softmac_ps_mgmt_xmit(struct sk_buff *skb,
278 		     struct rtllib_device *ieee)
279 {
280 	short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
281 	struct rtllib_hdr_3addr  *header =
282 		(struct rtllib_hdr_3addr  *) skb->data;
283 	u16 fc, type, stype;
284 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
285 
286 	fc = le16_to_cpu(header->frame_ctl);
287 	type = WLAN_FC_GET_TYPE(fc);
288 	stype = WLAN_FC_GET_STYPE(fc);
289 
290 
291 	if (stype != RTLLIB_STYPE_PSPOLL)
292 		tcb_desc->queue_index = MGNT_QUEUE;
293 	else
294 		tcb_desc->queue_index = HIGH_QUEUE;
295 
296 	if (ieee->disable_mgnt_queue)
297 		tcb_desc->queue_index = HIGH_QUEUE;
298 
299 
300 	tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
301 	tcb_desc->RATRIndex = 7;
302 	tcb_desc->bTxDisableRateFallBack = 1;
303 	tcb_desc->bTxUseDriverAssingedRate = 1;
304 	if (single) {
305 		if (type != RTLLIB_FTYPE_CTL) {
306 			header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
307 
308 			if (ieee->seq_ctrl[0] == 0xFFF)
309 				ieee->seq_ctrl[0] = 0;
310 			else
311 				ieee->seq_ctrl[0]++;
312 
313 		}
314 		/* avoid watchdog triggers */
315 		ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
316 						   ieee->basic_rate);
317 
318 	} else {
319 		if (type != RTLLIB_FTYPE_CTL) {
320 			header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
321 
322 			if (ieee->seq_ctrl[0] == 0xFFF)
323 				ieee->seq_ctrl[0] = 0;
324 			else
325 				ieee->seq_ctrl[0]++;
326 		}
327 		ieee->softmac_hard_start_xmit(skb, ieee->dev);
328 
329 	}
330 }
331 
332 static inline struct sk_buff *rtllib_probe_req(struct rtllib_device *ieee)
333 {
334 	unsigned int len, rate_len;
335 	u8 *tag;
336 	struct sk_buff *skb;
337 	struct rtllib_probe_request *req;
338 
339 	len = ieee->current_network.ssid_len;
340 
341 	rate_len = rtllib_MFIE_rate_len(ieee);
342 
343 	skb = dev_alloc_skb(sizeof(struct rtllib_probe_request) +
344 			    2 + len + rate_len + ieee->tx_headroom);
345 
346 	if (!skb)
347 		return NULL;
348 
349 	skb_reserve(skb, ieee->tx_headroom);
350 
351 	req = skb_put(skb, sizeof(struct rtllib_probe_request));
352 	req->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_PROBE_REQ);
353 	req->header.duration_id = 0;
354 
355 	eth_broadcast_addr(req->header.addr1);
356 	ether_addr_copy(req->header.addr2, ieee->dev->dev_addr);
357 	eth_broadcast_addr(req->header.addr3);
358 
359 	tag = skb_put(skb, len + 2 + rate_len);
360 
361 	*tag++ = MFIE_TYPE_SSID;
362 	*tag++ = len;
363 	memcpy(tag, ieee->current_network.ssid, len);
364 	tag += len;
365 
366 	rtllib_MFIE_Brate(ieee, &tag);
367 	rtllib_MFIE_Grate(ieee, &tag);
368 
369 	return skb;
370 }
371 
372 static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee);
373 
374 static void rtllib_send_beacon(struct rtllib_device *ieee)
375 {
376 	struct sk_buff *skb;
377 
378 	if (!ieee->ieee_up)
379 		return;
380 	skb = rtllib_get_beacon_(ieee);
381 
382 	if (skb) {
383 		softmac_mgmt_xmit(skb, ieee);
384 		ieee->softmac_stats.tx_beacons++;
385 	}
386 
387 	if (ieee->beacon_txing && ieee->ieee_up)
388 		mod_timer(&ieee->beacon_timer, jiffies +
389 			  (msecs_to_jiffies(ieee->current_network.beacon_interval - 5)));
390 }
391 
392 
393 static void rtllib_send_beacon_cb(struct timer_list *t)
394 {
395 	struct rtllib_device *ieee =
396 		from_timer(ieee, t, beacon_timer);
397 	unsigned long flags;
398 
399 	spin_lock_irqsave(&ieee->beacon_lock, flags);
400 	rtllib_send_beacon(ieee);
401 	spin_unlock_irqrestore(&ieee->beacon_lock, flags);
402 }
403 
404 /* Enables network monitor mode, all rx packets will be received. */
405 void rtllib_EnableNetMonitorMode(struct net_device *dev,
406 		bool bInitState)
407 {
408 	struct rtllib_device *ieee = netdev_priv_rsl(dev);
409 
410 	netdev_info(dev, "========>Enter Monitor Mode\n");
411 
412 	ieee->AllowAllDestAddrHandler(dev, true, !bInitState);
413 }
414 
415 
416 /* Disables network monitor mode. Only packets destinated to
417  * us will be received.
418  */
419 void rtllib_DisableNetMonitorMode(struct net_device *dev,
420 		bool bInitState)
421 {
422 	struct rtllib_device *ieee = netdev_priv_rsl(dev);
423 
424 	netdev_info(dev, "========>Exit Monitor Mode\n");
425 
426 	ieee->AllowAllDestAddrHandler(dev, false, !bInitState);
427 }
428 
429 
430 /* Enables the specialized promiscuous mode required by Intel.
431  * In this mode, Intel intends to hear traffics from/to other STAs in the
432  * same BSS. Therefore we don't have to disable checking BSSID and we only need
433  * to allow all dest. BUT: if we enable checking BSSID then we can't recv
434  * packets from other STA.
435  */
436 void rtllib_EnableIntelPromiscuousMode(struct net_device *dev,
437 		bool bInitState)
438 {
439 	bool bFilterOutNonAssociatedBSSID = false;
440 
441 	struct rtllib_device *ieee = netdev_priv_rsl(dev);
442 
443 	netdev_info(dev, "========>Enter Intel Promiscuous Mode\n");
444 
445 	ieee->AllowAllDestAddrHandler(dev, true, !bInitState);
446 	ieee->SetHwRegHandler(dev, HW_VAR_CECHK_BSSID,
447 			     (u8 *)&bFilterOutNonAssociatedBSSID);
448 
449 	ieee->bNetPromiscuousMode = true;
450 }
451 EXPORT_SYMBOL(rtllib_EnableIntelPromiscuousMode);
452 
453 
454 /* Disables the specialized promiscuous mode required by Intel.
455  * See MgntEnableIntelPromiscuousMode for detail.
456  */
457 void rtllib_DisableIntelPromiscuousMode(struct net_device *dev,
458 		bool bInitState)
459 {
460 	bool bFilterOutNonAssociatedBSSID = true;
461 
462 	struct rtllib_device *ieee = netdev_priv_rsl(dev);
463 
464 	netdev_info(dev, "========>Exit Intel Promiscuous Mode\n");
465 
466 	ieee->AllowAllDestAddrHandler(dev, false, !bInitState);
467 	ieee->SetHwRegHandler(dev, HW_VAR_CECHK_BSSID,
468 			     (u8 *)&bFilterOutNonAssociatedBSSID);
469 
470 	ieee->bNetPromiscuousMode = false;
471 }
472 EXPORT_SYMBOL(rtllib_DisableIntelPromiscuousMode);
473 
474 static void rtllib_send_probe(struct rtllib_device *ieee, u8 is_mesh)
475 {
476 	struct sk_buff *skb;
477 
478 	skb = rtllib_probe_req(ieee);
479 	if (skb) {
480 		softmac_mgmt_xmit(skb, ieee);
481 		ieee->softmac_stats.tx_probe_rq++;
482 	}
483 }
484 
485 
486 static void rtllib_send_probe_requests(struct rtllib_device *ieee, u8 is_mesh)
487 {
488 	if (ieee->active_scan && (ieee->softmac_features &
489 	    IEEE_SOFTMAC_PROBERQ)) {
490 		rtllib_send_probe(ieee, 0);
491 		rtllib_send_probe(ieee, 0);
492 	}
493 }
494 
495 static void rtllib_update_active_chan_map(struct rtllib_device *ieee)
496 {
497 	memcpy(ieee->active_channel_map, GET_DOT11D_INFO(ieee)->channel_map,
498 	       MAX_CHANNEL_NUMBER+1);
499 }
500 
501 /* this performs syncro scan blocking the caller until all channels
502  * in the allowed channel map has been checked.
503  */
504 static void rtllib_softmac_scan_syncro(struct rtllib_device *ieee, u8 is_mesh)
505 {
506 	union iwreq_data wrqu;
507 	short ch = 0;
508 
509 	rtllib_update_active_chan_map(ieee);
510 
511 	ieee->be_scan_inprogress = true;
512 
513 	mutex_lock(&ieee->scan_mutex);
514 
515 	while (1) {
516 		do {
517 			ch++;
518 			if (ch > MAX_CHANNEL_NUMBER)
519 				goto out; /* scan completed */
520 		} while (!ieee->active_channel_map[ch]);
521 
522 		/* this function can be called in two situations
523 		 * 1- We have switched to ad-hoc mode and we are
524 		 *    performing a complete syncro scan before conclude
525 		 *    there are no interesting cell and to create a
526 		 *    new one. In this case the link state is
527 		 *    RTLLIB_NOLINK until we found an interesting cell.
528 		 *    If so the ieee8021_new_net, called by the RX path
529 		 *    will set the state to RTLLIB_LINKED, so we stop
530 		 *    scanning
531 		 * 2- We are linked and the root uses run iwlist scan.
532 		 *    So we switch to RTLLIB_LINKED_SCANNING to remember
533 		 *    that we are still logically linked (not interested in
534 		 *    new network events, despite for updating the net list,
535 		 *    but we are temporarly 'unlinked' as the driver shall
536 		 *    not filter RX frames and the channel is changing.
537 		 * So the only situation in which are interested is to check
538 		 * if the state become LINKED because of the #1 situation
539 		 */
540 
541 		if (ieee->state == RTLLIB_LINKED)
542 			goto out;
543 		if (ieee->sync_scan_hurryup) {
544 			netdev_info(ieee->dev,
545 				    "============>sync_scan_hurryup out\n");
546 			goto out;
547 		}
548 
549 		ieee->set_chan(ieee->dev, ch);
550 		if (ieee->active_channel_map[ch] == 1)
551 			rtllib_send_probe_requests(ieee, 0);
552 
553 		/* this prevent excessive time wait when we
554 		 * need to wait for a syncro scan to end..
555 		 */
556 		msleep_interruptible_rsl(RTLLIB_SOFTMAC_SCAN_TIME);
557 	}
558 out:
559 	ieee->actscanning = false;
560 	ieee->sync_scan_hurryup = 0;
561 
562 	if (ieee->state >= RTLLIB_LINKED) {
563 		if (IS_DOT11D_ENABLE(ieee))
564 			dot11d_scan_complete(ieee);
565 	}
566 	mutex_unlock(&ieee->scan_mutex);
567 
568 	ieee->be_scan_inprogress = false;
569 
570 	memset(&wrqu, 0, sizeof(wrqu));
571 	wireless_send_event(ieee->dev, SIOCGIWSCAN, &wrqu, NULL);
572 }
573 
574 static void rtllib_softmac_scan_wq(void *data)
575 {
576 	struct rtllib_device *ieee = container_of_dwork_rsl(data,
577 				     struct rtllib_device, softmac_scan_wq);
578 	u8 last_channel = ieee->current_network.channel;
579 
580 	rtllib_update_active_chan_map(ieee);
581 
582 	if (!ieee->ieee_up)
583 		return;
584 	if (rtllib_act_scanning(ieee, true))
585 		return;
586 
587 	mutex_lock(&ieee->scan_mutex);
588 
589 	if (ieee->eRFPowerState == eRfOff) {
590 		netdev_info(ieee->dev,
591 			    "======>%s():rf state is eRfOff, return\n",
592 			    __func__);
593 		goto out1;
594 	}
595 
596 	do {
597 		ieee->current_network.channel =
598 			(ieee->current_network.channel + 1) %
599 			MAX_CHANNEL_NUMBER;
600 		if (ieee->scan_watch_dog++ > MAX_CHANNEL_NUMBER) {
601 			if (!ieee->active_channel_map[ieee->current_network.channel])
602 				ieee->current_network.channel = 6;
603 			goto out; /* no good chans */
604 		}
605 	} while (!ieee->active_channel_map[ieee->current_network.channel]);
606 
607 	if (ieee->scanning_continue == 0)
608 		goto out;
609 
610 	ieee->set_chan(ieee->dev, ieee->current_network.channel);
611 
612 	if (ieee->active_channel_map[ieee->current_network.channel] == 1)
613 		rtllib_send_probe_requests(ieee, 0);
614 
615 	schedule_delayed_work(&ieee->softmac_scan_wq,
616 			      msecs_to_jiffies(RTLLIB_SOFTMAC_SCAN_TIME));
617 
618 	mutex_unlock(&ieee->scan_mutex);
619 	return;
620 
621 out:
622 	if (IS_DOT11D_ENABLE(ieee))
623 		dot11d_scan_complete(ieee);
624 	ieee->current_network.channel = last_channel;
625 
626 out1:
627 	ieee->actscanning = false;
628 	ieee->scan_watch_dog = 0;
629 	ieee->scanning_continue = 0;
630 	mutex_unlock(&ieee->scan_mutex);
631 }
632 
633 
634 
635 static void rtllib_beacons_start(struct rtllib_device *ieee)
636 {
637 	unsigned long flags;
638 
639 	spin_lock_irqsave(&ieee->beacon_lock, flags);
640 
641 	ieee->beacon_txing = 1;
642 	rtllib_send_beacon(ieee);
643 
644 	spin_unlock_irqrestore(&ieee->beacon_lock, flags);
645 }
646 
647 static void rtllib_beacons_stop(struct rtllib_device *ieee)
648 {
649 	unsigned long flags;
650 
651 	spin_lock_irqsave(&ieee->beacon_lock, flags);
652 
653 	ieee->beacon_txing = 0;
654 	del_timer_sync(&ieee->beacon_timer);
655 
656 	spin_unlock_irqrestore(&ieee->beacon_lock, flags);
657 
658 }
659 
660 
661 void rtllib_stop_send_beacons(struct rtllib_device *ieee)
662 {
663 	if (ieee->stop_send_beacons)
664 		ieee->stop_send_beacons(ieee->dev);
665 	if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
666 		rtllib_beacons_stop(ieee);
667 }
668 EXPORT_SYMBOL(rtllib_stop_send_beacons);
669 
670 
671 void rtllib_start_send_beacons(struct rtllib_device *ieee)
672 {
673 	if (ieee->start_send_beacons)
674 		ieee->start_send_beacons(ieee->dev);
675 	if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
676 		rtllib_beacons_start(ieee);
677 }
678 EXPORT_SYMBOL(rtllib_start_send_beacons);
679 
680 
681 static void rtllib_softmac_stop_scan(struct rtllib_device *ieee)
682 {
683 	mutex_lock(&ieee->scan_mutex);
684 	ieee->scan_watch_dog = 0;
685 	if (ieee->scanning_continue == 1) {
686 		ieee->scanning_continue = 0;
687 		ieee->actscanning = false;
688 
689 		cancel_delayed_work_sync(&ieee->softmac_scan_wq);
690 	}
691 
692 	mutex_unlock(&ieee->scan_mutex);
693 }
694 
695 void rtllib_stop_scan(struct rtllib_device *ieee)
696 {
697 	if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
698 		rtllib_softmac_stop_scan(ieee);
699 	} else {
700 		if (ieee->rtllib_stop_hw_scan)
701 			ieee->rtllib_stop_hw_scan(ieee->dev);
702 	}
703 }
704 EXPORT_SYMBOL(rtllib_stop_scan);
705 
706 void rtllib_stop_scan_syncro(struct rtllib_device *ieee)
707 {
708 	if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
709 		ieee->sync_scan_hurryup = 1;
710 	} else {
711 		if (ieee->rtllib_stop_hw_scan)
712 			ieee->rtllib_stop_hw_scan(ieee->dev);
713 	}
714 }
715 EXPORT_SYMBOL(rtllib_stop_scan_syncro);
716 
717 bool rtllib_act_scanning(struct rtllib_device *ieee, bool sync_scan)
718 {
719 	if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
720 		if (sync_scan)
721 			return ieee->be_scan_inprogress;
722 		else
723 			return ieee->actscanning || ieee->be_scan_inprogress;
724 	} else {
725 		return test_bit(STATUS_SCANNING, &ieee->status);
726 	}
727 }
728 EXPORT_SYMBOL(rtllib_act_scanning);
729 
730 /* called with ieee->lock held */
731 static void rtllib_start_scan(struct rtllib_device *ieee)
732 {
733 	RT_TRACE(COMP_DBG, "===>%s()\n", __func__);
734 	if (ieee->rtllib_ips_leave_wq != NULL)
735 		ieee->rtllib_ips_leave_wq(ieee->dev);
736 
737 	if (IS_DOT11D_ENABLE(ieee)) {
738 		if (IS_COUNTRY_IE_VALID(ieee))
739 			RESET_CIE_WATCHDOG(ieee);
740 	}
741 	if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
742 		if (ieee->scanning_continue == 0) {
743 			ieee->actscanning = true;
744 			ieee->scanning_continue = 1;
745 			schedule_delayed_work(&ieee->softmac_scan_wq, 0);
746 		}
747 	} else {
748 		if (ieee->rtllib_start_hw_scan)
749 			ieee->rtllib_start_hw_scan(ieee->dev);
750 	}
751 }
752 
753 /* called with wx_mutex held */
754 void rtllib_start_scan_syncro(struct rtllib_device *ieee, u8 is_mesh)
755 {
756 	if (IS_DOT11D_ENABLE(ieee)) {
757 		if (IS_COUNTRY_IE_VALID(ieee))
758 			RESET_CIE_WATCHDOG(ieee);
759 	}
760 	ieee->sync_scan_hurryup = 0;
761 	if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
762 		rtllib_softmac_scan_syncro(ieee, is_mesh);
763 	} else {
764 		if (ieee->rtllib_start_hw_scan)
765 			ieee->rtllib_start_hw_scan(ieee->dev);
766 	}
767 }
768 EXPORT_SYMBOL(rtllib_start_scan_syncro);
769 
770 static inline struct sk_buff *
771 rtllib_authentication_req(struct rtllib_network *beacon,
772 			  struct rtllib_device *ieee,
773 			  int challengelen, u8 *daddr)
774 {
775 	struct sk_buff *skb;
776 	struct rtllib_authentication *auth;
777 	int  len;
778 
779 	len = sizeof(struct rtllib_authentication) + challengelen +
780 		     ieee->tx_headroom + 4;
781 	skb = dev_alloc_skb(len);
782 
783 	if (!skb)
784 		return NULL;
785 
786 	skb_reserve(skb, ieee->tx_headroom);
787 
788 	auth = skb_put(skb, sizeof(struct rtllib_authentication));
789 
790 	auth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_AUTH);
791 	if (challengelen)
792 		auth->header.frame_ctl |= cpu_to_le16(RTLLIB_FCTL_WEP);
793 
794 	auth->header.duration_id = cpu_to_le16(0x013a);
795 	ether_addr_copy(auth->header.addr1, beacon->bssid);
796 	ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
797 	ether_addr_copy(auth->header.addr3, beacon->bssid);
798 	if (ieee->auth_mode == 0)
799 		auth->algorithm = WLAN_AUTH_OPEN;
800 	else if (ieee->auth_mode == 1)
801 		auth->algorithm = cpu_to_le16(WLAN_AUTH_SHARED_KEY);
802 	else if (ieee->auth_mode == 2)
803 		auth->algorithm = WLAN_AUTH_OPEN;
804 	auth->transaction = cpu_to_le16(ieee->associate_seq);
805 	ieee->associate_seq++;
806 
807 	auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
808 
809 	return skb;
810 }
811 
812 static struct sk_buff *rtllib_probe_resp(struct rtllib_device *ieee,
813 					 const u8 *dest)
814 {
815 	u8 *tag;
816 	int beacon_size;
817 	struct rtllib_probe_response *beacon_buf;
818 	struct sk_buff *skb = NULL;
819 	int encrypt;
820 	int atim_len, erp_len;
821 	struct lib80211_crypt_data *crypt;
822 
823 	char *ssid = ieee->current_network.ssid;
824 	int ssid_len = ieee->current_network.ssid_len;
825 	int rate_len = ieee->current_network.rates_len+2;
826 	int rate_ex_len = ieee->current_network.rates_ex_len;
827 	int wpa_ie_len = ieee->wpa_ie_len;
828 	u8 erpinfo_content = 0;
829 
830 	u8 *tmp_ht_cap_buf = NULL;
831 	u8 tmp_ht_cap_len = 0;
832 	u8 *tmp_ht_info_buf = NULL;
833 	u8 tmp_ht_info_len = 0;
834 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
835 	u8 *tmp_generic_ie_buf = NULL;
836 	u8 tmp_generic_ie_len = 0;
837 
838 	if (rate_ex_len > 0)
839 		rate_ex_len += 2;
840 
841 	if (ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
842 		atim_len = 4;
843 	else
844 		atim_len = 0;
845 
846 	if ((ieee->current_network.mode == IEEE_G) ||
847 	   (ieee->current_network.mode == IEEE_N_24G &&
848 	   ieee->pHTInfo->bCurSuppCCK)) {
849 		erp_len = 3;
850 		erpinfo_content = 0;
851 		if (ieee->current_network.buseprotection)
852 			erpinfo_content |= ERP_UseProtection;
853 	} else
854 		erp_len = 0;
855 
856 	crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
857 	encrypt = ieee->host_encrypt && crypt && crypt->ops &&
858 		((strcmp(crypt->ops->name, "R-WEP") == 0 || wpa_ie_len));
859 	if (ieee->pHTInfo->bCurrentHTSupport) {
860 		tmp_ht_cap_buf = (u8 *) &(ieee->pHTInfo->SelfHTCap);
861 		tmp_ht_cap_len = sizeof(ieee->pHTInfo->SelfHTCap);
862 		tmp_ht_info_buf = (u8 *) &(ieee->pHTInfo->SelfHTInfo);
863 		tmp_ht_info_len = sizeof(ieee->pHTInfo->SelfHTInfo);
864 		HTConstructCapabilityElement(ieee, tmp_ht_cap_buf,
865 					     &tmp_ht_cap_len, encrypt, false);
866 		HTConstructInfoElement(ieee, tmp_ht_info_buf, &tmp_ht_info_len,
867 				       encrypt);
868 
869 		if (pHTInfo->bRegRT2RTAggregation) {
870 			tmp_generic_ie_buf = ieee->pHTInfo->szRT2RTAggBuffer;
871 			tmp_generic_ie_len =
872 				 sizeof(ieee->pHTInfo->szRT2RTAggBuffer);
873 			HTConstructRT2RTAggElement(ieee, tmp_generic_ie_buf,
874 						   &tmp_generic_ie_len);
875 		}
876 	}
877 
878 	beacon_size = sizeof(struct rtllib_probe_response)+2+
879 		ssid_len + 3 + rate_len + rate_ex_len + atim_len + erp_len
880 		+ wpa_ie_len + ieee->tx_headroom;
881 	skb = dev_alloc_skb(beacon_size);
882 	if (!skb)
883 		return NULL;
884 
885 	skb_reserve(skb, ieee->tx_headroom);
886 
887 	beacon_buf = skb_put(skb, (beacon_size - ieee->tx_headroom));
888 	ether_addr_copy(beacon_buf->header.addr1, dest);
889 	ether_addr_copy(beacon_buf->header.addr2, ieee->dev->dev_addr);
890 	ether_addr_copy(beacon_buf->header.addr3, ieee->current_network.bssid);
891 
892 	beacon_buf->header.duration_id = 0;
893 	beacon_buf->beacon_interval =
894 		cpu_to_le16(ieee->current_network.beacon_interval);
895 	beacon_buf->capability =
896 		cpu_to_le16(ieee->current_network.capability &
897 		WLAN_CAPABILITY_IBSS);
898 	beacon_buf->capability |=
899 		cpu_to_le16(ieee->current_network.capability &
900 		WLAN_CAPABILITY_SHORT_PREAMBLE);
901 
902 	if (ieee->short_slot && (ieee->current_network.capability &
903 	    WLAN_CAPABILITY_SHORT_SLOT_TIME))
904 		beacon_buf->capability |=
905 			cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
906 
907 	crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
908 	if (encrypt)
909 		beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
910 
911 
912 	beacon_buf->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_PROBE_RESP);
913 	beacon_buf->info_element[0].id = MFIE_TYPE_SSID;
914 	beacon_buf->info_element[0].len = ssid_len;
915 
916 	tag = (u8 *) beacon_buf->info_element[0].data;
917 
918 	memcpy(tag, ssid, ssid_len);
919 
920 	tag += ssid_len;
921 
922 	*(tag++) = MFIE_TYPE_RATES;
923 	*(tag++) = rate_len-2;
924 	memcpy(tag, ieee->current_network.rates, rate_len-2);
925 	tag += rate_len-2;
926 
927 	*(tag++) = MFIE_TYPE_DS_SET;
928 	*(tag++) = 1;
929 	*(tag++) = ieee->current_network.channel;
930 
931 	if (atim_len) {
932 		u16 val16;
933 		*(tag++) = MFIE_TYPE_IBSS_SET;
934 		*(tag++) = 2;
935 		val16 = ieee->current_network.atim_window;
936 		memcpy((u8 *)tag, (u8 *)&val16, 2);
937 		tag += 2;
938 	}
939 
940 	if (erp_len) {
941 		*(tag++) = MFIE_TYPE_ERP;
942 		*(tag++) = 1;
943 		*(tag++) = erpinfo_content;
944 	}
945 	if (rate_ex_len) {
946 		*(tag++) = MFIE_TYPE_RATES_EX;
947 		*(tag++) = rate_ex_len-2;
948 		memcpy(tag, ieee->current_network.rates_ex, rate_ex_len-2);
949 		tag += rate_ex_len-2;
950 	}
951 
952 	if (wpa_ie_len) {
953 		if (ieee->iw_mode == IW_MODE_ADHOC)
954 			memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
955 		memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
956 		tag += ieee->wpa_ie_len;
957 	}
958 	return skb;
959 }
960 
961 static struct sk_buff *rtllib_assoc_resp(struct rtllib_device *ieee, u8 *dest)
962 {
963 	struct sk_buff *skb;
964 	u8 *tag;
965 
966 	struct lib80211_crypt_data *crypt;
967 	struct rtllib_assoc_response_frame *assoc;
968 	short encrypt;
969 
970 	unsigned int rate_len = rtllib_MFIE_rate_len(ieee);
971 	int len = sizeof(struct rtllib_assoc_response_frame) + rate_len +
972 		  ieee->tx_headroom;
973 
974 	skb = dev_alloc_skb(len);
975 
976 	if (!skb)
977 		return NULL;
978 
979 	skb_reserve(skb, ieee->tx_headroom);
980 
981 	assoc = skb_put(skb, sizeof(struct rtllib_assoc_response_frame));
982 
983 	assoc->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_ASSOC_RESP);
984 	ether_addr_copy(assoc->header.addr1, dest);
985 	ether_addr_copy(assoc->header.addr3, ieee->dev->dev_addr);
986 	ether_addr_copy(assoc->header.addr2, ieee->dev->dev_addr);
987 	assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
988 		WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS);
989 
990 
991 	if (ieee->short_slot)
992 		assoc->capability |=
993 				 cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
994 
995 	if (ieee->host_encrypt)
996 		crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
997 	else
998 		crypt = NULL;
999 
1000 	encrypt = (crypt && crypt->ops);
1001 
1002 	if (encrypt)
1003 		assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1004 
1005 	assoc->status = 0;
1006 	assoc->aid = cpu_to_le16(ieee->assoc_id);
1007 	if (ieee->assoc_id == 0x2007)
1008 		ieee->assoc_id = 0;
1009 	else
1010 		ieee->assoc_id++;
1011 
1012 	tag = skb_put(skb, rate_len);
1013 	rtllib_MFIE_Brate(ieee, &tag);
1014 	rtllib_MFIE_Grate(ieee, &tag);
1015 
1016 	return skb;
1017 }
1018 
1019 static struct sk_buff *rtllib_auth_resp(struct rtllib_device *ieee, int status,
1020 				 u8 *dest)
1021 {
1022 	struct sk_buff *skb = NULL;
1023 	struct rtllib_authentication *auth;
1024 	int len = ieee->tx_headroom + sizeof(struct rtllib_authentication) + 1;
1025 
1026 	skb = dev_alloc_skb(len);
1027 	if (!skb)
1028 		return NULL;
1029 
1030 	skb->len = sizeof(struct rtllib_authentication);
1031 
1032 	skb_reserve(skb, ieee->tx_headroom);
1033 
1034 	auth = skb_put(skb, sizeof(struct rtllib_authentication));
1035 
1036 	auth->status = cpu_to_le16(status);
1037 	auth->transaction = cpu_to_le16(2);
1038 	auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
1039 
1040 	ether_addr_copy(auth->header.addr3, ieee->dev->dev_addr);
1041 	ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
1042 	ether_addr_copy(auth->header.addr1, dest);
1043 	auth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_AUTH);
1044 	return skb;
1045 
1046 
1047 }
1048 
1049 static struct sk_buff *rtllib_null_func(struct rtllib_device *ieee, short pwr)
1050 {
1051 	struct sk_buff *skb;
1052 	struct rtllib_hdr_3addr *hdr;
1053 
1054 	skb = dev_alloc_skb(sizeof(struct rtllib_hdr_3addr)+ieee->tx_headroom);
1055 	if (!skb)
1056 		return NULL;
1057 
1058 	skb_reserve(skb, ieee->tx_headroom);
1059 
1060 	hdr = skb_put(skb, sizeof(struct rtllib_hdr_3addr));
1061 
1062 	ether_addr_copy(hdr->addr1, ieee->current_network.bssid);
1063 	ether_addr_copy(hdr->addr2, ieee->dev->dev_addr);
1064 	ether_addr_copy(hdr->addr3, ieee->current_network.bssid);
1065 
1066 	hdr->frame_ctl = cpu_to_le16(RTLLIB_FTYPE_DATA |
1067 		RTLLIB_STYPE_NULLFUNC | RTLLIB_FCTL_TODS |
1068 		(pwr ? RTLLIB_FCTL_PM : 0));
1069 
1070 	return skb;
1071 
1072 
1073 }
1074 
1075 static struct sk_buff *rtllib_pspoll_func(struct rtllib_device *ieee)
1076 {
1077 	struct sk_buff *skb;
1078 	struct rtllib_pspoll_hdr *hdr;
1079 
1080 	skb = dev_alloc_skb(sizeof(struct rtllib_pspoll_hdr)+ieee->tx_headroom);
1081 	if (!skb)
1082 		return NULL;
1083 
1084 	skb_reserve(skb, ieee->tx_headroom);
1085 
1086 	hdr = skb_put(skb, sizeof(struct rtllib_pspoll_hdr));
1087 
1088 	ether_addr_copy(hdr->bssid, ieee->current_network.bssid);
1089 	ether_addr_copy(hdr->ta, ieee->dev->dev_addr);
1090 
1091 	hdr->aid = cpu_to_le16(ieee->assoc_id | 0xc000);
1092 	hdr->frame_ctl = cpu_to_le16(RTLLIB_FTYPE_CTL | RTLLIB_STYPE_PSPOLL |
1093 			 RTLLIB_FCTL_PM);
1094 
1095 	return skb;
1096 
1097 }
1098 
1099 static void rtllib_resp_to_assoc_rq(struct rtllib_device *ieee, u8 *dest)
1100 {
1101 	struct sk_buff *buf = rtllib_assoc_resp(ieee, dest);
1102 
1103 	if (buf)
1104 		softmac_mgmt_xmit(buf, ieee);
1105 }
1106 
1107 
1108 static void rtllib_resp_to_auth(struct rtllib_device *ieee, int s, u8 *dest)
1109 {
1110 	struct sk_buff *buf = rtllib_auth_resp(ieee, s, dest);
1111 
1112 	if (buf)
1113 		softmac_mgmt_xmit(buf, ieee);
1114 }
1115 
1116 
1117 static void rtllib_resp_to_probe(struct rtllib_device *ieee, u8 *dest)
1118 {
1119 	struct sk_buff *buf = rtllib_probe_resp(ieee, dest);
1120 
1121 	if (buf)
1122 		softmac_mgmt_xmit(buf, ieee);
1123 }
1124 
1125 
1126 static inline int SecIsInPMKIDList(struct rtllib_device *ieee, u8 *bssid)
1127 {
1128 	int i = 0;
1129 
1130 	do {
1131 		if ((ieee->PMKIDList[i].bUsed) &&
1132 		   (memcmp(ieee->PMKIDList[i].Bssid, bssid, ETH_ALEN) == 0))
1133 			break;
1134 		i++;
1135 	} while (i < NUM_PMKID_CACHE);
1136 
1137 	if (i == NUM_PMKID_CACHE)
1138 		i = -1;
1139 	return i;
1140 }
1141 
1142 static inline struct sk_buff *
1143 rtllib_association_req(struct rtllib_network *beacon,
1144 		       struct rtllib_device *ieee)
1145 {
1146 	struct sk_buff *skb;
1147 	struct rtllib_assoc_request_frame *hdr;
1148 	u8 *tag, *ies;
1149 	int i;
1150 	u8 *ht_cap_buf = NULL;
1151 	u8 ht_cap_len = 0;
1152 	u8 *realtek_ie_buf = NULL;
1153 	u8 realtek_ie_len = 0;
1154 	int wpa_ie_len = ieee->wpa_ie_len;
1155 	int wps_ie_len = ieee->wps_ie_len;
1156 	unsigned int ckip_ie_len = 0;
1157 	unsigned int ccxrm_ie_len = 0;
1158 	unsigned int cxvernum_ie_len = 0;
1159 	struct lib80211_crypt_data *crypt;
1160 	int encrypt;
1161 	int	PMKCacheIdx;
1162 
1163 	unsigned int rate_len = (beacon->rates_len ?
1164 				(beacon->rates_len + 2) : 0) +
1165 				(beacon->rates_ex_len ? (beacon->rates_ex_len) +
1166 				2 : 0);
1167 
1168 	unsigned int wmm_info_len = beacon->qos_data.supported ? 9 : 0;
1169 	unsigned int turbo_info_len = beacon->Turbo_Enable ? 9 : 0;
1170 
1171 	int len = 0;
1172 
1173 	crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
1174 	if (crypt != NULL)
1175 		encrypt = ieee->host_encrypt && crypt && crypt->ops &&
1176 			  ((strcmp(crypt->ops->name, "R-WEP") == 0 ||
1177 			  wpa_ie_len));
1178 	else
1179 		encrypt = 0;
1180 
1181 	if ((ieee->rtllib_ap_sec_type &&
1182 	    (ieee->rtllib_ap_sec_type(ieee) & SEC_ALG_TKIP)) ||
1183 	    ieee->bForcedBgMode) {
1184 		ieee->pHTInfo->bEnableHT = 0;
1185 		ieee->mode = WIRELESS_MODE_G;
1186 	}
1187 
1188 	if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1189 		ht_cap_buf = (u8 *)&(ieee->pHTInfo->SelfHTCap);
1190 		ht_cap_len = sizeof(ieee->pHTInfo->SelfHTCap);
1191 		HTConstructCapabilityElement(ieee, ht_cap_buf, &ht_cap_len,
1192 					     encrypt, true);
1193 		if (ieee->pHTInfo->bCurrentRT2RTAggregation) {
1194 			realtek_ie_buf = ieee->pHTInfo->szRT2RTAggBuffer;
1195 			realtek_ie_len =
1196 				 sizeof(ieee->pHTInfo->szRT2RTAggBuffer);
1197 			HTConstructRT2RTAggElement(ieee, realtek_ie_buf,
1198 						   &realtek_ie_len);
1199 		}
1200 	}
1201 
1202 	if (beacon->bCkipSupported)
1203 		ckip_ie_len = 30+2;
1204 	if (beacon->bCcxRmEnable)
1205 		ccxrm_ie_len = 6+2;
1206 	if (beacon->BssCcxVerNumber >= 2)
1207 		cxvernum_ie_len = 5+2;
1208 
1209 	PMKCacheIdx = SecIsInPMKIDList(ieee, ieee->current_network.bssid);
1210 	if (PMKCacheIdx >= 0) {
1211 		wpa_ie_len += 18;
1212 		netdev_info(ieee->dev, "[PMK cache]: WPA2 IE length: %x\n",
1213 			    wpa_ie_len);
1214 	}
1215 	len = sizeof(struct rtllib_assoc_request_frame) + 2
1216 		+ beacon->ssid_len
1217 		+ rate_len
1218 		+ wpa_ie_len
1219 		+ wps_ie_len
1220 		+ wmm_info_len
1221 		+ turbo_info_len
1222 		+ ht_cap_len
1223 		+ realtek_ie_len
1224 		+ ckip_ie_len
1225 		+ ccxrm_ie_len
1226 		+ cxvernum_ie_len
1227 		+ ieee->tx_headroom;
1228 
1229 	skb = dev_alloc_skb(len);
1230 
1231 	if (!skb)
1232 		return NULL;
1233 
1234 	skb_reserve(skb, ieee->tx_headroom);
1235 
1236 	hdr = skb_put(skb, sizeof(struct rtllib_assoc_request_frame) + 2);
1237 
1238 
1239 	hdr->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_ASSOC_REQ);
1240 	hdr->header.duration_id = cpu_to_le16(37);
1241 	ether_addr_copy(hdr->header.addr1, beacon->bssid);
1242 	ether_addr_copy(hdr->header.addr2, ieee->dev->dev_addr);
1243 	ether_addr_copy(hdr->header.addr3, beacon->bssid);
1244 
1245 	ether_addr_copy(ieee->ap_mac_addr, beacon->bssid);
1246 
1247 	hdr->capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
1248 	if (beacon->capability & WLAN_CAPABILITY_PRIVACY)
1249 		hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1250 
1251 	if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1252 		hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
1253 
1254 	if (ieee->short_slot &&
1255 	   (beacon->capability&WLAN_CAPABILITY_SHORT_SLOT_TIME))
1256 		hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
1257 
1258 
1259 	hdr->listen_interval = cpu_to_le16(beacon->listen_interval);
1260 
1261 	hdr->info_element[0].id = MFIE_TYPE_SSID;
1262 
1263 	hdr->info_element[0].len = beacon->ssid_len;
1264 	skb_put_data(skb, beacon->ssid, beacon->ssid_len);
1265 
1266 	tag = skb_put(skb, rate_len);
1267 
1268 	if (beacon->rates_len) {
1269 		*tag++ = MFIE_TYPE_RATES;
1270 		*tag++ = beacon->rates_len;
1271 		for (i = 0; i < beacon->rates_len; i++)
1272 			*tag++ = beacon->rates[i];
1273 	}
1274 
1275 	if (beacon->rates_ex_len) {
1276 		*tag++ = MFIE_TYPE_RATES_EX;
1277 		*tag++ = beacon->rates_ex_len;
1278 		for (i = 0; i < beacon->rates_ex_len; i++)
1279 			*tag++ = beacon->rates_ex[i];
1280 	}
1281 
1282 	if (beacon->bCkipSupported) {
1283 		static const u8 AironetIeOui[] = {0x00, 0x01, 0x66};
1284 		u8	CcxAironetBuf[30];
1285 		struct octet_string osCcxAironetIE;
1286 
1287 		memset(CcxAironetBuf, 0, 30);
1288 		osCcxAironetIE.Octet = CcxAironetBuf;
1289 		osCcxAironetIE.Length = sizeof(CcxAironetBuf);
1290 		memcpy(osCcxAironetIE.Octet, AironetIeOui,
1291 		       sizeof(AironetIeOui));
1292 
1293 		osCcxAironetIE.Octet[IE_CISCO_FLAG_POSITION] |=
1294 					 (SUPPORT_CKIP_PK|SUPPORT_CKIP_MIC);
1295 		tag = skb_put(skb, ckip_ie_len);
1296 		*tag++ = MFIE_TYPE_AIRONET;
1297 		*tag++ = osCcxAironetIE.Length;
1298 		memcpy(tag, osCcxAironetIE.Octet, osCcxAironetIE.Length);
1299 		tag += osCcxAironetIE.Length;
1300 	}
1301 
1302 	if (beacon->bCcxRmEnable) {
1303 		static const u8 CcxRmCapBuf[] = {0x00, 0x40, 0x96, 0x01, 0x01,
1304 			0x00};
1305 		struct octet_string osCcxRmCap;
1306 
1307 		osCcxRmCap.Octet = (u8 *) CcxRmCapBuf;
1308 		osCcxRmCap.Length = sizeof(CcxRmCapBuf);
1309 		tag = skb_put(skb, ccxrm_ie_len);
1310 		*tag++ = MFIE_TYPE_GENERIC;
1311 		*tag++ = osCcxRmCap.Length;
1312 		memcpy(tag, osCcxRmCap.Octet, osCcxRmCap.Length);
1313 		tag += osCcxRmCap.Length;
1314 	}
1315 
1316 	if (beacon->BssCcxVerNumber >= 2) {
1317 		u8 CcxVerNumBuf[] = {0x00, 0x40, 0x96, 0x03, 0x00};
1318 		struct octet_string osCcxVerNum;
1319 
1320 		CcxVerNumBuf[4] = beacon->BssCcxVerNumber;
1321 		osCcxVerNum.Octet = CcxVerNumBuf;
1322 		osCcxVerNum.Length = sizeof(CcxVerNumBuf);
1323 		tag = skb_put(skb, cxvernum_ie_len);
1324 		*tag++ = MFIE_TYPE_GENERIC;
1325 		*tag++ = osCcxVerNum.Length;
1326 		memcpy(tag, osCcxVerNum.Octet, osCcxVerNum.Length);
1327 		tag += osCcxVerNum.Length;
1328 	}
1329 	if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1330 		if (ieee->pHTInfo->ePeerHTSpecVer != HT_SPEC_VER_EWC) {
1331 			tag = skb_put(skb, ht_cap_len);
1332 			*tag++ = MFIE_TYPE_HT_CAP;
1333 			*tag++ = ht_cap_len - 2;
1334 			memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1335 			tag += ht_cap_len - 2;
1336 		}
1337 	}
1338 
1339 	if (wpa_ie_len) {
1340 		skb_put_data(skb, ieee->wpa_ie, ieee->wpa_ie_len);
1341 
1342 		if (PMKCacheIdx >= 0) {
1343 			tag = skb_put(skb, 18);
1344 			*tag = 1;
1345 			*(tag + 1) = 0;
1346 			memcpy((tag + 2), &ieee->PMKIDList[PMKCacheIdx].PMKID,
1347 			       16);
1348 		}
1349 	}
1350 	if (wmm_info_len) {
1351 		tag = skb_put(skb, wmm_info_len);
1352 		rtllib_WMM_Info(ieee, &tag);
1353 	}
1354 
1355 	if (wps_ie_len && ieee->wps_ie) {
1356 		skb_put_data(skb, ieee->wps_ie, wps_ie_len);
1357 	}
1358 
1359 	if (turbo_info_len) {
1360 		tag = skb_put(skb, turbo_info_len);
1361 		rtllib_TURBO_Info(ieee, &tag);
1362 	}
1363 
1364 	if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1365 		if (ieee->pHTInfo->ePeerHTSpecVer == HT_SPEC_VER_EWC) {
1366 			tag = skb_put(skb, ht_cap_len);
1367 			*tag++ = MFIE_TYPE_GENERIC;
1368 			*tag++ = ht_cap_len - 2;
1369 			memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1370 			tag += ht_cap_len - 2;
1371 		}
1372 
1373 		if (ieee->pHTInfo->bCurrentRT2RTAggregation) {
1374 			tag = skb_put(skb, realtek_ie_len);
1375 			*tag++ = MFIE_TYPE_GENERIC;
1376 			*tag++ = realtek_ie_len - 2;
1377 			memcpy(tag, realtek_ie_buf, realtek_ie_len - 2);
1378 		}
1379 	}
1380 
1381 	kfree(ieee->assocreq_ies);
1382 	ieee->assocreq_ies = NULL;
1383 	ies = &(hdr->info_element[0].id);
1384 	ieee->assocreq_ies_len = (skb->data + skb->len) - ies;
1385 	ieee->assocreq_ies = kmemdup(ies, ieee->assocreq_ies_len, GFP_ATOMIC);
1386 	if (!ieee->assocreq_ies)
1387 		ieee->assocreq_ies_len = 0;
1388 
1389 	return skb;
1390 }
1391 
1392 static void rtllib_associate_abort(struct rtllib_device *ieee)
1393 {
1394 	unsigned long flags;
1395 
1396 	spin_lock_irqsave(&ieee->lock, flags);
1397 
1398 	ieee->associate_seq++;
1399 
1400 	/* don't scan, and avoid to have the RX path possibily
1401 	 * try again to associate. Even do not react to AUTH or
1402 	 * ASSOC response. Just wait for the retry wq to be scheduled.
1403 	 * Here we will check if there are good nets to associate
1404 	 * with, so we retry or just get back to NO_LINK and scanning
1405 	 */
1406 	if (ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATING) {
1407 		netdev_dbg(ieee->dev, "Authentication failed\n");
1408 		ieee->softmac_stats.no_auth_rs++;
1409 	} else {
1410 		netdev_dbg(ieee->dev, "Association failed\n");
1411 		ieee->softmac_stats.no_ass_rs++;
1412 	}
1413 
1414 	ieee->state = RTLLIB_ASSOCIATING_RETRY;
1415 
1416 	schedule_delayed_work(&ieee->associate_retry_wq,
1417 			      RTLLIB_SOFTMAC_ASSOC_RETRY_TIME);
1418 
1419 	spin_unlock_irqrestore(&ieee->lock, flags);
1420 }
1421 
1422 static void rtllib_associate_abort_cb(struct timer_list *t)
1423 {
1424 	struct rtllib_device *dev = from_timer(dev, t, associate_timer);
1425 
1426 	rtllib_associate_abort(dev);
1427 }
1428 
1429 static void rtllib_associate_step1(struct rtllib_device *ieee, u8 *daddr)
1430 {
1431 	struct rtllib_network *beacon = &ieee->current_network;
1432 	struct sk_buff *skb;
1433 
1434 	netdev_dbg(ieee->dev, "Stopping scan\n");
1435 
1436 	ieee->softmac_stats.tx_auth_rq++;
1437 
1438 	skb = rtllib_authentication_req(beacon, ieee, 0, daddr);
1439 
1440 	if (!skb)
1441 		rtllib_associate_abort(ieee);
1442 	else {
1443 		ieee->state = RTLLIB_ASSOCIATING_AUTHENTICATING;
1444 		netdev_dbg(ieee->dev, "Sending authentication request\n");
1445 		softmac_mgmt_xmit(skb, ieee);
1446 		if (!timer_pending(&ieee->associate_timer)) {
1447 			ieee->associate_timer.expires = jiffies + (HZ / 2);
1448 			add_timer(&ieee->associate_timer);
1449 		}
1450 	}
1451 }
1452 
1453 static void rtllib_auth_challenge(struct rtllib_device *ieee, u8 *challenge,
1454 				  int chlen)
1455 {
1456 	u8 *c;
1457 	struct sk_buff *skb;
1458 	struct rtllib_network *beacon = &ieee->current_network;
1459 
1460 	ieee->associate_seq++;
1461 	ieee->softmac_stats.tx_auth_rq++;
1462 
1463 	skb = rtllib_authentication_req(beacon, ieee, chlen + 2, beacon->bssid);
1464 
1465 	if (!skb)
1466 		rtllib_associate_abort(ieee);
1467 	else {
1468 		c = skb_put(skb, chlen+2);
1469 		*(c++) = MFIE_TYPE_CHALLENGE;
1470 		*(c++) = chlen;
1471 		memcpy(c, challenge, chlen);
1472 
1473 		netdev_dbg(ieee->dev,
1474 			   "Sending authentication challenge response\n");
1475 
1476 		rtllib_encrypt_fragment(ieee, skb,
1477 					sizeof(struct rtllib_hdr_3addr));
1478 
1479 		softmac_mgmt_xmit(skb, ieee);
1480 		mod_timer(&ieee->associate_timer, jiffies + (HZ/2));
1481 	}
1482 	kfree(challenge);
1483 }
1484 
1485 static void rtllib_associate_step2(struct rtllib_device *ieee)
1486 {
1487 	struct sk_buff *skb;
1488 	struct rtllib_network *beacon = &ieee->current_network;
1489 
1490 	del_timer_sync(&ieee->associate_timer);
1491 
1492 	netdev_dbg(ieee->dev, "Sending association request\n");
1493 
1494 	ieee->softmac_stats.tx_ass_rq++;
1495 	skb = rtllib_association_req(beacon, ieee);
1496 	if (!skb)
1497 		rtllib_associate_abort(ieee);
1498 	else {
1499 		softmac_mgmt_xmit(skb, ieee);
1500 		mod_timer(&ieee->associate_timer, jiffies + (HZ/2));
1501 	}
1502 }
1503 
1504 static void rtllib_associate_complete_wq(void *data)
1505 {
1506 	struct rtllib_device *ieee = (struct rtllib_device *)
1507 				     container_of_work_rsl(data,
1508 				     struct rtllib_device,
1509 				     associate_complete_wq);
1510 	struct rt_pwr_save_ctrl *pPSC = &(ieee->PowerSaveControl);
1511 
1512 	netdev_info(ieee->dev, "Associated successfully with %pM\n",
1513 		    ieee->current_network.bssid);
1514 	if (!ieee->is_silent_reset) {
1515 		netdev_info(ieee->dev, "normal associate\n");
1516 		notify_wx_assoc_event(ieee);
1517 	}
1518 
1519 	netif_carrier_on(ieee->dev);
1520 	ieee->is_roaming = false;
1521 	if (rtllib_is_54g(&ieee->current_network) &&
1522 	   (ieee->modulation & RTLLIB_OFDM_MODULATION)) {
1523 		ieee->rate = 108;
1524 		netdev_info(ieee->dev, "Using G rates:%d\n", ieee->rate);
1525 	} else {
1526 		ieee->rate = 22;
1527 		ieee->SetWirelessMode(ieee->dev, IEEE_B);
1528 		netdev_info(ieee->dev, "Using B rates:%d\n", ieee->rate);
1529 	}
1530 	if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1531 		netdev_info(ieee->dev, "Successfully associated, ht enabled\n");
1532 		HTOnAssocRsp(ieee);
1533 	} else {
1534 		netdev_info(ieee->dev,
1535 			    "Successfully associated, ht not enabled(%d, %d)\n",
1536 			    ieee->pHTInfo->bCurrentHTSupport,
1537 			    ieee->pHTInfo->bEnableHT);
1538 		memset(ieee->dot11HTOperationalRateSet, 0, 16);
1539 	}
1540 	ieee->LinkDetectInfo.SlotNum = 2 * (1 +
1541 				       ieee->current_network.beacon_interval /
1542 				       500);
1543 	if (ieee->LinkDetectInfo.NumRecvBcnInPeriod == 0 ||
1544 	    ieee->LinkDetectInfo.NumRecvDataInPeriod == 0) {
1545 		ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
1546 		ieee->LinkDetectInfo.NumRecvDataInPeriod = 1;
1547 	}
1548 	pPSC->LpsIdleCount = 0;
1549 	ieee->link_change(ieee->dev);
1550 
1551 	if (ieee->is_silent_reset) {
1552 		netdev_info(ieee->dev, "silent reset associate\n");
1553 		ieee->is_silent_reset = false;
1554 	}
1555 
1556 	if (ieee->data_hard_resume)
1557 		ieee->data_hard_resume(ieee->dev);
1558 
1559 }
1560 
1561 static void rtllib_sta_send_associnfo(struct rtllib_device *ieee)
1562 {
1563 }
1564 
1565 static void rtllib_associate_complete(struct rtllib_device *ieee)
1566 {
1567 	del_timer_sync(&ieee->associate_timer);
1568 
1569 	ieee->state = RTLLIB_LINKED;
1570 	rtllib_sta_send_associnfo(ieee);
1571 
1572 	schedule_work(&ieee->associate_complete_wq);
1573 }
1574 
1575 static void rtllib_associate_procedure_wq(void *data)
1576 {
1577 	struct rtllib_device *ieee = container_of_dwork_rsl(data,
1578 				     struct rtllib_device,
1579 				     associate_procedure_wq);
1580 	rtllib_stop_scan_syncro(ieee);
1581 	if (ieee->rtllib_ips_leave != NULL)
1582 		ieee->rtllib_ips_leave(ieee->dev);
1583 	mutex_lock(&ieee->wx_mutex);
1584 
1585 	if (ieee->data_hard_stop)
1586 		ieee->data_hard_stop(ieee->dev);
1587 
1588 	rtllib_stop_scan(ieee);
1589 	RT_TRACE(COMP_DBG, "===>%s(), chan:%d\n", __func__,
1590 		 ieee->current_network.channel);
1591 	HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
1592 	if (ieee->eRFPowerState == eRfOff) {
1593 		RT_TRACE(COMP_DBG,
1594 			 "=============>%s():Rf state is eRfOff, schedule ipsleave wq again,return\n",
1595 			 __func__);
1596 		if (ieee->rtllib_ips_leave_wq != NULL)
1597 			ieee->rtllib_ips_leave_wq(ieee->dev);
1598 		mutex_unlock(&ieee->wx_mutex);
1599 		return;
1600 	}
1601 	ieee->associate_seq = 1;
1602 
1603 	rtllib_associate_step1(ieee, ieee->current_network.bssid);
1604 
1605 	mutex_unlock(&ieee->wx_mutex);
1606 }
1607 
1608 inline void rtllib_softmac_new_net(struct rtllib_device *ieee,
1609 				   struct rtllib_network *net)
1610 {
1611 	u8 tmp_ssid[IW_ESSID_MAX_SIZE + 1];
1612 	int tmp_ssid_len = 0;
1613 
1614 	short apset, ssidset, ssidbroad, apmatch, ssidmatch;
1615 
1616 	/* we are interested in new new only if we are not associated
1617 	 * and we are not associating / authenticating
1618 	 */
1619 	if (ieee->state != RTLLIB_NOLINK)
1620 		return;
1621 
1622 	if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability &
1623 	    WLAN_CAPABILITY_ESS))
1624 		return;
1625 
1626 	if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability &
1627 	     WLAN_CAPABILITY_IBSS))
1628 		return;
1629 
1630 	if ((ieee->iw_mode == IW_MODE_ADHOC) &&
1631 	    (net->channel > ieee->ibss_maxjoin_chal))
1632 		return;
1633 	if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC) {
1634 		/* if the user specified the AP MAC, we need also the essid
1635 		 * This could be obtained by beacons or, if the network does not
1636 		 * broadcast it, it can be put manually.
1637 		 */
1638 		apset = ieee->wap_set;
1639 		ssidset = ieee->ssid_set;
1640 		ssidbroad =  !(net->ssid_len == 0 || net->ssid[0] == '\0');
1641 		apmatch = (memcmp(ieee->current_network.bssid, net->bssid,
1642 				  ETH_ALEN) == 0);
1643 		if (!ssidbroad) {
1644 			ssidmatch = (ieee->current_network.ssid_len ==
1645 				    net->hidden_ssid_len) &&
1646 				    (!strncmp(ieee->current_network.ssid,
1647 				    net->hidden_ssid, net->hidden_ssid_len));
1648 			if (net->hidden_ssid_len > 0) {
1649 				strncpy(net->ssid, net->hidden_ssid,
1650 					net->hidden_ssid_len);
1651 				net->ssid_len = net->hidden_ssid_len;
1652 				ssidbroad = 1;
1653 			}
1654 		} else
1655 			ssidmatch =
1656 			   (ieee->current_network.ssid_len == net->ssid_len) &&
1657 			   (!strncmp(ieee->current_network.ssid, net->ssid,
1658 			   net->ssid_len));
1659 
1660 		/* if the user set the AP check if match.
1661 		 * if the network does not broadcast essid we check the
1662 		 *	 user supplied ANY essid
1663 		 * if the network does broadcast and the user does not set
1664 		 *	 essid it is OK
1665 		 * if the network does broadcast and the user did set essid
1666 		 * check if essid match
1667 		 * if the ap is not set, check that the user set the bssid
1668 		 * and the network does broadcast and that those two bssid match
1669 		 */
1670 		if ((apset && apmatch &&
1671 		   ((ssidset && ssidbroad && ssidmatch) ||
1672 		   (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1673 		   (!apset && ssidset && ssidbroad && ssidmatch) ||
1674 		   (ieee->is_roaming && ssidset && ssidbroad && ssidmatch)) {
1675 			/* Save the essid so that if it is hidden, it is
1676 			 * replaced with the essid provided by the user.
1677 			 */
1678 			if (!ssidbroad) {
1679 				memcpy(tmp_ssid, ieee->current_network.ssid,
1680 				       ieee->current_network.ssid_len);
1681 				tmp_ssid_len = ieee->current_network.ssid_len;
1682 			}
1683 			memcpy(&ieee->current_network, net,
1684 				sizeof(ieee->current_network));
1685 			if (!ssidbroad) {
1686 				memcpy(ieee->current_network.ssid, tmp_ssid,
1687 				       tmp_ssid_len);
1688 				ieee->current_network.ssid_len = tmp_ssid_len;
1689 			}
1690 			netdev_info(ieee->dev,
1691 				    "Linking with %s,channel:%d, qos:%d, myHT:%d, networkHT:%d, mode:%x cur_net.flags:0x%x\n",
1692 				    ieee->current_network.ssid,
1693 				    ieee->current_network.channel,
1694 				    ieee->current_network.qos_data.supported,
1695 				    ieee->pHTInfo->bEnableHT,
1696 				    ieee->current_network.bssht.bdSupportHT,
1697 				    ieee->current_network.mode,
1698 				    ieee->current_network.flags);
1699 
1700 			if ((rtllib_act_scanning(ieee, false)) &&
1701 			   !(ieee->softmac_features & IEEE_SOFTMAC_SCAN))
1702 				rtllib_stop_scan_syncro(ieee);
1703 
1704 			HTResetIOTSetting(ieee->pHTInfo);
1705 			ieee->wmm_acm = 0;
1706 			if (ieee->iw_mode == IW_MODE_INFRA) {
1707 				/* Join the network for the first time */
1708 				ieee->AsocRetryCount = 0;
1709 				if ((ieee->current_network.qos_data.supported == 1) &&
1710 				    ieee->current_network.bssht.bdSupportHT)
1711 					HTResetSelfAndSavePeerSetting(ieee,
1712 						 &(ieee->current_network));
1713 				else
1714 					ieee->pHTInfo->bCurrentHTSupport =
1715 								 false;
1716 
1717 				ieee->state = RTLLIB_ASSOCIATING;
1718 				if (ieee->LedControlHandler != NULL)
1719 					ieee->LedControlHandler(ieee->dev,
1720 							 LED_CTL_START_TO_LINK);
1721 				schedule_delayed_work(
1722 					   &ieee->associate_procedure_wq, 0);
1723 			} else {
1724 				if (rtllib_is_54g(&ieee->current_network) &&
1725 				    (ieee->modulation &
1726 				     RTLLIB_OFDM_MODULATION)) {
1727 					ieee->rate = 108;
1728 					ieee->SetWirelessMode(ieee->dev,
1729 							      IEEE_G);
1730 					netdev_info(ieee->dev,
1731 						    "Using G rates\n");
1732 				} else {
1733 					ieee->rate = 22;
1734 					ieee->SetWirelessMode(ieee->dev,
1735 							      IEEE_B);
1736 					netdev_info(ieee->dev,
1737 						    "Using B rates\n");
1738 				}
1739 				memset(ieee->dot11HTOperationalRateSet, 0, 16);
1740 				ieee->state = RTLLIB_LINKED;
1741 			}
1742 		}
1743 	}
1744 }
1745 
1746 static void rtllib_softmac_check_all_nets(struct rtllib_device *ieee)
1747 {
1748 	unsigned long flags;
1749 	struct rtllib_network *target;
1750 
1751 	spin_lock_irqsave(&ieee->lock, flags);
1752 
1753 	list_for_each_entry(target, &ieee->network_list, list) {
1754 
1755 		/* if the state become different that NOLINK means
1756 		 * we had found what we are searching for
1757 		 */
1758 
1759 		if (ieee->state != RTLLIB_NOLINK)
1760 			break;
1761 
1762 		if (ieee->scan_age == 0 || time_after(target->last_scanned +
1763 		    ieee->scan_age, jiffies))
1764 			rtllib_softmac_new_net(ieee, target);
1765 	}
1766 	spin_unlock_irqrestore(&ieee->lock, flags);
1767 }
1768 
1769 static inline u16 auth_parse(struct net_device *dev, struct sk_buff *skb,
1770 			     u8 **challenge, int *chlen)
1771 {
1772 	struct rtllib_authentication *a;
1773 	u8 *t;
1774 
1775 	if (skb->len <  (sizeof(struct rtllib_authentication) -
1776 	    sizeof(struct rtllib_info_element))) {
1777 		netdev_dbg(dev, "invalid len in auth resp: %d\n", skb->len);
1778 		return 0xcafe;
1779 	}
1780 	*challenge = NULL;
1781 	a = (struct rtllib_authentication *) skb->data;
1782 	if (skb->len > (sizeof(struct rtllib_authentication) + 3)) {
1783 		t = skb->data + sizeof(struct rtllib_authentication);
1784 
1785 		if (*(t++) == MFIE_TYPE_CHALLENGE) {
1786 			*chlen = *(t++);
1787 			*challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1788 			if (!*challenge)
1789 				return -ENOMEM;
1790 		}
1791 	}
1792 	return le16_to_cpu(a->status);
1793 }
1794 
1795 static int auth_rq_parse(struct net_device *dev, struct sk_buff *skb, u8 *dest)
1796 {
1797 	struct rtllib_authentication *a;
1798 
1799 	if (skb->len <  (sizeof(struct rtllib_authentication) -
1800 	    sizeof(struct rtllib_info_element))) {
1801 		netdev_dbg(dev, "invalid len in auth request: %d\n", skb->len);
1802 		return -1;
1803 	}
1804 	a = (struct rtllib_authentication *) skb->data;
1805 
1806 	ether_addr_copy(dest, a->header.addr2);
1807 
1808 	if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
1809 		return  WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1810 
1811 	return WLAN_STATUS_SUCCESS;
1812 }
1813 
1814 static short probe_rq_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1815 			    u8 *src)
1816 {
1817 	u8 *tag;
1818 	u8 *skbend;
1819 	u8 *ssid = NULL;
1820 	u8 ssidlen = 0;
1821 	struct rtllib_hdr_3addr   *header =
1822 		(struct rtllib_hdr_3addr   *) skb->data;
1823 	bool bssid_match;
1824 
1825 	if (skb->len < sizeof(struct rtllib_hdr_3addr))
1826 		return -1; /* corrupted */
1827 
1828 	bssid_match =
1829 	  (!ether_addr_equal(header->addr3, ieee->current_network.bssid)) &&
1830 	  (!is_broadcast_ether_addr(header->addr3));
1831 	if (bssid_match)
1832 		return -1;
1833 
1834 	ether_addr_copy(src, header->addr2);
1835 
1836 	skbend = (u8 *)skb->data + skb->len;
1837 
1838 	tag = skb->data + sizeof(struct rtllib_hdr_3addr);
1839 
1840 	while (tag + 1 < skbend) {
1841 		if (*tag == 0) {
1842 			ssid = tag + 2;
1843 			ssidlen = *(tag + 1);
1844 			break;
1845 		}
1846 		tag++; /* point to the len field */
1847 		tag = tag + *(tag); /* point to the last data byte of the tag */
1848 		tag++; /* point to the next tag */
1849 	}
1850 
1851 	if (ssidlen == 0)
1852 		return 1;
1853 
1854 	if (!ssid)
1855 		return 1; /* ssid not found in tagged param */
1856 
1857 	return !strncmp(ssid, ieee->current_network.ssid, ssidlen);
1858 }
1859 
1860 static int assoc_rq_parse(struct net_device *dev, struct sk_buff *skb, u8 *dest)
1861 {
1862 	struct rtllib_assoc_request_frame *a;
1863 
1864 	if (skb->len < (sizeof(struct rtllib_assoc_request_frame) -
1865 		sizeof(struct rtllib_info_element))) {
1866 		netdev_dbg(dev, "invalid len in auth request:%d\n", skb->len);
1867 		return -1;
1868 	}
1869 
1870 	a = (struct rtllib_assoc_request_frame *) skb->data;
1871 
1872 	ether_addr_copy(dest, a->header.addr2);
1873 
1874 	return 0;
1875 }
1876 
1877 static inline u16 assoc_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1878 			      int *aid)
1879 {
1880 	struct rtllib_assoc_response_frame *response_head;
1881 	u16 status_code;
1882 
1883 	if (skb->len <  sizeof(struct rtllib_assoc_response_frame)) {
1884 		netdev_dbg(ieee->dev, "Invalid len in auth resp: %d\n",
1885 			   skb->len);
1886 		return 0xcafe;
1887 	}
1888 
1889 	response_head = (struct rtllib_assoc_response_frame *) skb->data;
1890 	*aid = le16_to_cpu(response_head->aid) & 0x3fff;
1891 
1892 	status_code = le16_to_cpu(response_head->status);
1893 	if ((status_code == WLAN_STATUS_ASSOC_DENIED_RATES ||
1894 	   status_code == WLAN_STATUS_CAPS_UNSUPPORTED) &&
1895 	   ((ieee->mode == IEEE_G) &&
1896 	   (ieee->current_network.mode == IEEE_N_24G) &&
1897 	   (ieee->AsocRetryCount++ < (RT_ASOC_RETRY_LIMIT-1)))) {
1898 		ieee->pHTInfo->IOTAction |= HT_IOT_ACT_PURE_N_MODE;
1899 	} else {
1900 		ieee->AsocRetryCount = 0;
1901 	}
1902 
1903 	return le16_to_cpu(response_head->status);
1904 }
1905 
1906 void rtllib_rx_probe_rq(struct rtllib_device *ieee, struct sk_buff *skb)
1907 {
1908 	u8 dest[ETH_ALEN];
1909 
1910 	ieee->softmac_stats.rx_probe_rq++;
1911 	if (probe_rq_parse(ieee, skb, dest) > 0) {
1912 		ieee->softmac_stats.tx_probe_rs++;
1913 		rtllib_resp_to_probe(ieee, dest);
1914 	}
1915 }
1916 
1917 static inline void rtllib_rx_auth_rq(struct rtllib_device *ieee,
1918 				     struct sk_buff *skb)
1919 {
1920 	u8 dest[ETH_ALEN];
1921 	int status;
1922 
1923 	ieee->softmac_stats.rx_auth_rq++;
1924 
1925 	status = auth_rq_parse(ieee->dev, skb, dest);
1926 	if (status != -1)
1927 		rtllib_resp_to_auth(ieee, status, dest);
1928 }
1929 
1930 static inline void rtllib_rx_assoc_rq(struct rtllib_device *ieee,
1931 				      struct sk_buff *skb)
1932 {
1933 	u8 dest[ETH_ALEN];
1934 
1935 
1936 	ieee->softmac_stats.rx_ass_rq++;
1937 	if (assoc_rq_parse(ieee->dev, skb, dest) != -1)
1938 		rtllib_resp_to_assoc_rq(ieee, dest);
1939 
1940 	netdev_info(ieee->dev, "New client associated: %pM\n", dest);
1941 }
1942 
1943 void rtllib_sta_ps_send_null_frame(struct rtllib_device *ieee, short pwr)
1944 {
1945 
1946 	struct sk_buff *buf = rtllib_null_func(ieee, pwr);
1947 
1948 	if (buf)
1949 		softmac_ps_mgmt_xmit(buf, ieee);
1950 }
1951 EXPORT_SYMBOL(rtllib_sta_ps_send_null_frame);
1952 
1953 void rtllib_sta_ps_send_pspoll_frame(struct rtllib_device *ieee)
1954 {
1955 	struct sk_buff *buf = rtllib_pspoll_func(ieee);
1956 
1957 	if (buf)
1958 		softmac_ps_mgmt_xmit(buf, ieee);
1959 }
1960 
1961 static short rtllib_sta_ps_sleep(struct rtllib_device *ieee, u64 *time)
1962 {
1963 	int timeout;
1964 	u8 dtim;
1965 	struct rt_pwr_save_ctrl *pPSC = &(ieee->PowerSaveControl);
1966 
1967 	if (ieee->LPSDelayCnt) {
1968 		ieee->LPSDelayCnt--;
1969 		return 0;
1970 	}
1971 
1972 	dtim = ieee->current_network.dtim_data;
1973 	if (!(dtim & RTLLIB_DTIM_VALID))
1974 		return 0;
1975 	timeout = ieee->current_network.beacon_interval;
1976 	ieee->current_network.dtim_data = RTLLIB_DTIM_INVALID;
1977 	/* there's no need to nofity AP that I find you buffered
1978 	 * with broadcast packet
1979 	 */
1980 	if (dtim & (RTLLIB_DTIM_UCAST & ieee->ps))
1981 		return 2;
1982 
1983 	if (!time_after(jiffies,
1984 			dev_trans_start(ieee->dev) + msecs_to_jiffies(timeout)))
1985 		return 0;
1986 	if (!time_after(jiffies,
1987 			ieee->last_rx_ps_time + msecs_to_jiffies(timeout)))
1988 		return 0;
1989 	if ((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) &&
1990 	    (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1991 		return 0;
1992 
1993 	if (time) {
1994 		if (ieee->bAwakePktSent) {
1995 			pPSC->LPSAwakeIntvl = 1;
1996 		} else {
1997 			u8 MaxPeriod = 1;
1998 
1999 			if (pPSC->LPSAwakeIntvl == 0)
2000 				pPSC->LPSAwakeIntvl = 1;
2001 			if (pPSC->RegMaxLPSAwakeIntvl == 0)
2002 				MaxPeriod = 1;
2003 			else if (pPSC->RegMaxLPSAwakeIntvl == 0xFF)
2004 				MaxPeriod = ieee->current_network.dtim_period;
2005 			else
2006 				MaxPeriod = pPSC->RegMaxLPSAwakeIntvl;
2007 			pPSC->LPSAwakeIntvl = (pPSC->LPSAwakeIntvl >=
2008 					       MaxPeriod) ? MaxPeriod :
2009 					       (pPSC->LPSAwakeIntvl + 1);
2010 		}
2011 		{
2012 			u8 LPSAwakeIntvl_tmp = 0;
2013 			u8 period = ieee->current_network.dtim_period;
2014 			u8 count = ieee->current_network.tim.tim_count;
2015 
2016 			if (count == 0) {
2017 				if (pPSC->LPSAwakeIntvl > period)
2018 					LPSAwakeIntvl_tmp = period +
2019 						 (pPSC->LPSAwakeIntvl -
2020 						 period) -
2021 						 ((pPSC->LPSAwakeIntvl-period) %
2022 						 period);
2023 				else
2024 					LPSAwakeIntvl_tmp = pPSC->LPSAwakeIntvl;
2025 
2026 			} else {
2027 				if (pPSC->LPSAwakeIntvl >
2028 				    ieee->current_network.tim.tim_count)
2029 					LPSAwakeIntvl_tmp = count +
2030 					(pPSC->LPSAwakeIntvl - count) -
2031 					((pPSC->LPSAwakeIntvl-count)%period);
2032 				else
2033 					LPSAwakeIntvl_tmp = pPSC->LPSAwakeIntvl;
2034 			}
2035 
2036 		*time = ieee->current_network.last_dtim_sta_time
2037 			+ msecs_to_jiffies(ieee->current_network.beacon_interval *
2038 			LPSAwakeIntvl_tmp);
2039 	}
2040 	}
2041 
2042 	return 1;
2043 
2044 
2045 }
2046 
2047 static inline void rtllib_sta_ps(unsigned long data)
2048 {
2049 	struct rtllib_device *ieee = (struct rtllib_device *)data;
2050 	u64 time;
2051 	short sleep;
2052 	unsigned long flags, flags2;
2053 
2054 	spin_lock_irqsave(&ieee->lock, flags);
2055 
2056 	if ((ieee->ps == RTLLIB_PS_DISABLED ||
2057 	     ieee->iw_mode != IW_MODE_INFRA ||
2058 	     ieee->state != RTLLIB_LINKED)) {
2059 		RT_TRACE(COMP_DBG,
2060 			 "=====>%s(): no need to ps,wake up!! ieee->ps is %d, ieee->iw_mode is %d, ieee->state is %d\n",
2061 			 __func__, ieee->ps, ieee->iw_mode, ieee->state);
2062 		spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2063 		rtllib_sta_wakeup(ieee, 1);
2064 
2065 		spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2066 	}
2067 	sleep = rtllib_sta_ps_sleep(ieee, &time);
2068 	/* 2 wake, 1 sleep, 0 do nothing */
2069 	if (sleep == 0)
2070 		goto out;
2071 	if (sleep == 1) {
2072 		if (ieee->sta_sleep == LPS_IS_SLEEP) {
2073 			ieee->enter_sleep_state(ieee->dev, time);
2074 		} else if (ieee->sta_sleep == LPS_IS_WAKE) {
2075 			spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2076 
2077 			if (ieee->ps_is_queue_empty(ieee->dev)) {
2078 				ieee->sta_sleep = LPS_WAIT_NULL_DATA_SEND;
2079 				ieee->ack_tx_to_ieee = 1;
2080 				rtllib_sta_ps_send_null_frame(ieee, 1);
2081 				ieee->ps_time = time;
2082 			}
2083 			spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2084 
2085 		}
2086 
2087 		ieee->bAwakePktSent = false;
2088 
2089 	} else if (sleep == 2) {
2090 		spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2091 
2092 		rtllib_sta_wakeup(ieee, 1);
2093 
2094 		spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2095 	}
2096 
2097 out:
2098 	spin_unlock_irqrestore(&ieee->lock, flags);
2099 
2100 }
2101 
2102 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl)
2103 {
2104 	if (ieee->sta_sleep == LPS_IS_WAKE) {
2105 		if (nl) {
2106 			if (ieee->pHTInfo->IOTAction &
2107 			    HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
2108 				ieee->ack_tx_to_ieee = 1;
2109 				rtllib_sta_ps_send_null_frame(ieee, 0);
2110 			} else {
2111 				ieee->ack_tx_to_ieee = 1;
2112 				rtllib_sta_ps_send_pspoll_frame(ieee);
2113 			}
2114 		}
2115 		return;
2116 
2117 	}
2118 
2119 	if (ieee->sta_sleep == LPS_IS_SLEEP)
2120 		ieee->sta_wake_up(ieee->dev);
2121 	if (nl) {
2122 		if (ieee->pHTInfo->IOTAction &
2123 		    HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
2124 			ieee->ack_tx_to_ieee = 1;
2125 			rtllib_sta_ps_send_null_frame(ieee, 0);
2126 		} else {
2127 			ieee->ack_tx_to_ieee = 1;
2128 			ieee->polling = true;
2129 			rtllib_sta_ps_send_pspoll_frame(ieee);
2130 		}
2131 
2132 	} else {
2133 		ieee->sta_sleep = LPS_IS_WAKE;
2134 		ieee->polling = false;
2135 	}
2136 }
2137 
2138 void rtllib_ps_tx_ack(struct rtllib_device *ieee, short success)
2139 {
2140 	unsigned long flags, flags2;
2141 
2142 	spin_lock_irqsave(&ieee->lock, flags);
2143 
2144 	if (ieee->sta_sleep == LPS_WAIT_NULL_DATA_SEND) {
2145 		/* Null frame with PS bit set */
2146 		if (success) {
2147 			ieee->sta_sleep = LPS_IS_SLEEP;
2148 			ieee->enter_sleep_state(ieee->dev, ieee->ps_time);
2149 		}
2150 		/* if the card report not success we can't be sure the AP
2151 		 * has not RXed so we can't assume the AP believe us awake
2152 		 */
2153 	} else {/* 21112005 - tx again null without PS bit if lost */
2154 
2155 		if ((ieee->sta_sleep == LPS_IS_WAKE) && !success) {
2156 			spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2157 			if (ieee->pHTInfo->IOTAction &
2158 			    HT_IOT_ACT_NULL_DATA_POWER_SAVING)
2159 				rtllib_sta_ps_send_null_frame(ieee, 0);
2160 			else
2161 				rtllib_sta_ps_send_pspoll_frame(ieee);
2162 			spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2163 		}
2164 	}
2165 	spin_unlock_irqrestore(&ieee->lock, flags);
2166 }
2167 EXPORT_SYMBOL(rtllib_ps_tx_ack);
2168 
2169 static void rtllib_process_action(struct rtllib_device *ieee,
2170 				  struct sk_buff *skb)
2171 {
2172 	struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2173 	u8 *act = rtllib_get_payload((struct rtllib_hdr *)header);
2174 	u8 category = 0;
2175 
2176 	if (act == NULL) {
2177 		netdev_warn(ieee->dev,
2178 			    "Error getting payload of action frame\n");
2179 		return;
2180 	}
2181 
2182 	category = *act;
2183 	act++;
2184 	switch (category) {
2185 	case ACT_CAT_BA:
2186 		switch (*act) {
2187 		case ACT_ADDBAREQ:
2188 			rtllib_rx_ADDBAReq(ieee, skb);
2189 			break;
2190 		case ACT_ADDBARSP:
2191 			rtllib_rx_ADDBARsp(ieee, skb);
2192 			break;
2193 		case ACT_DELBA:
2194 			rtllib_rx_DELBA(ieee, skb);
2195 			break;
2196 		}
2197 		break;
2198 	default:
2199 		break;
2200 	}
2201 }
2202 
2203 static inline int
2204 rtllib_rx_assoc_resp(struct rtllib_device *ieee, struct sk_buff *skb,
2205 		     struct rtllib_rx_stats *rx_stats)
2206 {
2207 	u16 errcode;
2208 	int aid;
2209 	u8 *ies;
2210 	struct rtllib_assoc_response_frame *assoc_resp;
2211 	struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2212 	u16 frame_ctl = le16_to_cpu(header->frame_ctl);
2213 
2214 	netdev_dbg(ieee->dev, "received [RE]ASSOCIATION RESPONSE (%d)\n",
2215 		   WLAN_FC_GET_STYPE(frame_ctl));
2216 
2217 	if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2218 	     ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATED &&
2219 	     (ieee->iw_mode == IW_MODE_INFRA)) {
2220 		errcode = assoc_parse(ieee, skb, &aid);
2221 		if (!errcode) {
2222 			struct rtllib_network *network =
2223 				 kzalloc(sizeof(struct rtllib_network),
2224 				 GFP_ATOMIC);
2225 
2226 			if (!network)
2227 				return 1;
2228 			ieee->state = RTLLIB_LINKED;
2229 			ieee->assoc_id = aid;
2230 			ieee->softmac_stats.rx_ass_ok++;
2231 			/* station support qos */
2232 			/* Let the register setting default with Legacy station */
2233 			assoc_resp = (struct rtllib_assoc_response_frame *)skb->data;
2234 			if (ieee->current_network.qos_data.supported == 1) {
2235 				if (rtllib_parse_info_param(ieee, assoc_resp->info_element,
2236 							rx_stats->len - sizeof(*assoc_resp),
2237 							network, rx_stats)) {
2238 					kfree(network);
2239 					return 1;
2240 				}
2241 				memcpy(ieee->pHTInfo->PeerHTCapBuf,
2242 				       network->bssht.bdHTCapBuf,
2243 				       network->bssht.bdHTCapLen);
2244 				memcpy(ieee->pHTInfo->PeerHTInfoBuf,
2245 				       network->bssht.bdHTInfoBuf,
2246 				       network->bssht.bdHTInfoLen);
2247 				if (ieee->handle_assoc_response != NULL)
2248 					ieee->handle_assoc_response(ieee->dev,
2249 						 (struct rtllib_assoc_response_frame *)header,
2250 						 network);
2251 			}
2252 			kfree(network);
2253 
2254 			kfree(ieee->assocresp_ies);
2255 			ieee->assocresp_ies = NULL;
2256 			ies = &(assoc_resp->info_element[0].id);
2257 			ieee->assocresp_ies_len = (skb->data + skb->len) - ies;
2258 			ieee->assocresp_ies = kmemdup(ies,
2259 						      ieee->assocresp_ies_len,
2260 						      GFP_ATOMIC);
2261 			if (!ieee->assocresp_ies)
2262 				ieee->assocresp_ies_len = 0;
2263 
2264 			rtllib_associate_complete(ieee);
2265 		} else {
2266 			/* aid could not been allocated */
2267 			ieee->softmac_stats.rx_ass_err++;
2268 			netdev_info(ieee->dev,
2269 				    "Association response status code 0x%x\n",
2270 				    errcode);
2271 			if (ieee->AsocRetryCount < RT_ASOC_RETRY_LIMIT)
2272 				schedule_delayed_work(
2273 					 &ieee->associate_procedure_wq, 0);
2274 			else
2275 				rtllib_associate_abort(ieee);
2276 		}
2277 	}
2278 	return 0;
2279 }
2280 
2281 static void rtllib_rx_auth_resp(struct rtllib_device *ieee, struct sk_buff *skb)
2282 {
2283 	u16 errcode;
2284 	u8 *challenge;
2285 	int chlen = 0;
2286 	bool bSupportNmode = true, bHalfSupportNmode = false;
2287 
2288 	errcode = auth_parse(ieee->dev, skb, &challenge, &chlen);
2289 
2290 	if (errcode) {
2291 		ieee->softmac_stats.rx_auth_rs_err++;
2292 		netdev_info(ieee->dev,
2293 			    "Authentication response status code 0x%x",
2294 			    errcode);
2295 		rtllib_associate_abort(ieee);
2296 		return;
2297 	}
2298 
2299 	if (ieee->open_wep || !challenge) {
2300 		ieee->state = RTLLIB_ASSOCIATING_AUTHENTICATED;
2301 		ieee->softmac_stats.rx_auth_rs_ok++;
2302 		if (!(ieee->pHTInfo->IOTAction & HT_IOT_ACT_PURE_N_MODE)) {
2303 			if (!ieee->GetNmodeSupportBySecCfg(ieee->dev)) {
2304 				if (IsHTHalfNmodeAPs(ieee)) {
2305 					bSupportNmode = true;
2306 					bHalfSupportNmode = true;
2307 				} else {
2308 					bSupportNmode = false;
2309 					bHalfSupportNmode = false;
2310 				}
2311 			}
2312 		}
2313 		/* Dummy wirless mode setting to avoid encryption issue */
2314 		if (bSupportNmode) {
2315 			ieee->SetWirelessMode(ieee->dev,
2316 					      ieee->current_network.mode);
2317 		} else {
2318 			/*TODO*/
2319 			ieee->SetWirelessMode(ieee->dev, IEEE_G);
2320 		}
2321 
2322 		if ((ieee->current_network.mode == IEEE_N_24G) &&
2323 		    bHalfSupportNmode) {
2324 			netdev_info(ieee->dev, "======>enter half N mode\n");
2325 			ieee->bHalfWirelessN24GMode = true;
2326 		} else {
2327 			ieee->bHalfWirelessN24GMode = false;
2328 		}
2329 		rtllib_associate_step2(ieee);
2330 	} else {
2331 		rtllib_auth_challenge(ieee, challenge,  chlen);
2332 	}
2333 }
2334 
2335 static inline int
2336 rtllib_rx_auth(struct rtllib_device *ieee, struct sk_buff *skb,
2337 	       struct rtllib_rx_stats *rx_stats)
2338 {
2339 
2340 	if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) {
2341 		if (ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATING &&
2342 		    (ieee->iw_mode == IW_MODE_INFRA)) {
2343 			netdev_dbg(ieee->dev,
2344 				   "Received authentication response");
2345 			rtllib_rx_auth_resp(ieee, skb);
2346 		} else if (ieee->iw_mode == IW_MODE_MASTER) {
2347 			rtllib_rx_auth_rq(ieee, skb);
2348 		}
2349 	}
2350 	return 0;
2351 }
2352 
2353 static inline int
2354 rtllib_rx_deauth(struct rtllib_device *ieee, struct sk_buff *skb)
2355 {
2356 	struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2357 	u16 frame_ctl;
2358 
2359 	if (memcmp(header->addr3, ieee->current_network.bssid, ETH_ALEN) != 0)
2360 		return 0;
2361 
2362 	/* FIXME for now repeat all the association procedure
2363 	 * both for disassociation and deauthentication
2364 	 */
2365 	if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2366 	    ieee->state == RTLLIB_LINKED &&
2367 	    (ieee->iw_mode == IW_MODE_INFRA)) {
2368 		frame_ctl = le16_to_cpu(header->frame_ctl);
2369 		netdev_info(ieee->dev,
2370 			    "==========>received disassoc/deauth(%x) frame, reason code:%x\n",
2371 			    WLAN_FC_GET_STYPE(frame_ctl),
2372 			    ((struct rtllib_disassoc *)skb->data)->reason);
2373 		ieee->state = RTLLIB_ASSOCIATING;
2374 		ieee->softmac_stats.reassoc++;
2375 		ieee->is_roaming = true;
2376 		ieee->LinkDetectInfo.bBusyTraffic = false;
2377 		rtllib_disassociate(ieee);
2378 		RemovePeerTS(ieee, header->addr2);
2379 		if (ieee->LedControlHandler != NULL)
2380 			ieee->LedControlHandler(ieee->dev,
2381 						LED_CTL_START_TO_LINK);
2382 
2383 		if (!(ieee->rtllib_ap_sec_type(ieee) &
2384 		    (SEC_ALG_CCMP|SEC_ALG_TKIP)))
2385 			schedule_delayed_work(
2386 				       &ieee->associate_procedure_wq, 5);
2387 	}
2388 	return 0;
2389 }
2390 
2391 inline int rtllib_rx_frame_softmac(struct rtllib_device *ieee,
2392 				   struct sk_buff *skb,
2393 				   struct rtllib_rx_stats *rx_stats, u16 type,
2394 				   u16 stype)
2395 {
2396 	struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2397 	u16 frame_ctl;
2398 
2399 	if (!ieee->proto_started)
2400 		return 0;
2401 
2402 	frame_ctl = le16_to_cpu(header->frame_ctl);
2403 	switch (WLAN_FC_GET_STYPE(frame_ctl)) {
2404 	case RTLLIB_STYPE_ASSOC_RESP:
2405 	case RTLLIB_STYPE_REASSOC_RESP:
2406 		if (rtllib_rx_assoc_resp(ieee, skb, rx_stats) == 1)
2407 			return 1;
2408 		break;
2409 	case RTLLIB_STYPE_ASSOC_REQ:
2410 	case RTLLIB_STYPE_REASSOC_REQ:
2411 		if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2412 		     ieee->iw_mode == IW_MODE_MASTER)
2413 			rtllib_rx_assoc_rq(ieee, skb);
2414 		break;
2415 	case RTLLIB_STYPE_AUTH:
2416 		rtllib_rx_auth(ieee, skb, rx_stats);
2417 		break;
2418 	case RTLLIB_STYPE_DISASSOC:
2419 	case RTLLIB_STYPE_DEAUTH:
2420 		rtllib_rx_deauth(ieee, skb);
2421 		break;
2422 	case RTLLIB_STYPE_MANAGE_ACT:
2423 		rtllib_process_action(ieee, skb);
2424 		break;
2425 	default:
2426 		return -1;
2427 	}
2428 	return 0;
2429 }
2430 
2431 /* following are for a simpler TX queue management.
2432  * Instead of using netif_[stop/wake]_queue the driver
2433  * will use these two functions (plus a reset one), that
2434  * will internally use the kernel netif_* and takes
2435  * care of the ieee802.11 fragmentation.
2436  * So the driver receives a fragment per time and might
2437  * call the stop function when it wants to not
2438  * have enough room to TX an entire packet.
2439  * This might be useful if each fragment needs it's own
2440  * descriptor, thus just keep a total free memory > than
2441  * the max fragmentation threshold is not enough.. If the
2442  * ieee802.11 stack passed a TXB struct then you need
2443  * to keep N free descriptors where
2444  * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
2445  * In this way you need just one and the 802.11 stack
2446  * will take care of buffering fragments and pass them to
2447  * to the driver later, when it wakes the queue.
2448  */
2449 void rtllib_softmac_xmit(struct rtllib_txb *txb, struct rtllib_device *ieee)
2450 {
2451 
2452 	unsigned int queue_index = txb->queue_index;
2453 	unsigned long flags;
2454 	int  i;
2455 	struct cb_desc *tcb_desc = NULL;
2456 	unsigned long queue_len = 0;
2457 
2458 	spin_lock_irqsave(&ieee->lock, flags);
2459 
2460 	/* called with 2nd parm 0, no tx mgmt lock required */
2461 	rtllib_sta_wakeup(ieee, 0);
2462 
2463 	/* update the tx status */
2464 	tcb_desc = (struct cb_desc *)(txb->fragments[0]->cb +
2465 		   MAX_DEV_ADDR_SIZE);
2466 	if (tcb_desc->bMulticast)
2467 		ieee->stats.multicast++;
2468 
2469 	/* if xmit available, just xmit it immediately, else just insert it to
2470 	 * the wait queue
2471 	 */
2472 	for (i = 0; i < txb->nr_frags; i++) {
2473 		queue_len = skb_queue_len(&ieee->skb_waitQ[queue_index]);
2474 		if ((queue_len  != 0) ||
2475 		    (!ieee->check_nic_enough_desc(ieee->dev, queue_index)) ||
2476 		    (ieee->queue_stop)) {
2477 			/* insert the skb packet to the wait queue
2478 			 * as for the completion function, it does not need
2479 			 * to check it any more.
2480 			 */
2481 			if (queue_len < 200)
2482 				skb_queue_tail(&ieee->skb_waitQ[queue_index],
2483 					       txb->fragments[i]);
2484 			else
2485 				kfree_skb(txb->fragments[i]);
2486 		} else {
2487 			ieee->softmac_data_hard_start_xmit(
2488 					txb->fragments[i],
2489 					ieee->dev, ieee->rate);
2490 		}
2491 	}
2492 
2493 	rtllib_txb_free(txb);
2494 
2495 	spin_unlock_irqrestore(&ieee->lock, flags);
2496 
2497 }
2498 
2499 void rtllib_reset_queue(struct rtllib_device *ieee)
2500 {
2501 	unsigned long flags;
2502 
2503 	spin_lock_irqsave(&ieee->lock, flags);
2504 	init_mgmt_queue(ieee);
2505 	if (ieee->tx_pending.txb) {
2506 		rtllib_txb_free(ieee->tx_pending.txb);
2507 		ieee->tx_pending.txb = NULL;
2508 	}
2509 	ieee->queue_stop = 0;
2510 	spin_unlock_irqrestore(&ieee->lock, flags);
2511 
2512 }
2513 EXPORT_SYMBOL(rtllib_reset_queue);
2514 
2515 void rtllib_stop_all_queues(struct rtllib_device *ieee)
2516 {
2517 	unsigned int i;
2518 
2519 	for (i = 0; i < ieee->dev->num_tx_queues; i++)
2520 		netdev_get_tx_queue(ieee->dev, i)->trans_start = jiffies;
2521 
2522 	netif_tx_stop_all_queues(ieee->dev);
2523 }
2524 
2525 void rtllib_wake_all_queues(struct rtllib_device *ieee)
2526 {
2527 	netif_tx_wake_all_queues(ieee->dev);
2528 }
2529 
2530 /* called in user context only */
2531 static void rtllib_start_master_bss(struct rtllib_device *ieee)
2532 {
2533 	ieee->assoc_id = 1;
2534 
2535 	if (ieee->current_network.ssid_len == 0) {
2536 		strncpy(ieee->current_network.ssid,
2537 			RTLLIB_DEFAULT_TX_ESSID,
2538 			IW_ESSID_MAX_SIZE);
2539 
2540 		ieee->current_network.ssid_len =
2541 				 strlen(RTLLIB_DEFAULT_TX_ESSID);
2542 		ieee->ssid_set = 1;
2543 	}
2544 
2545 	ether_addr_copy(ieee->current_network.bssid, ieee->dev->dev_addr);
2546 
2547 	ieee->set_chan(ieee->dev, ieee->current_network.channel);
2548 	ieee->state = RTLLIB_LINKED;
2549 	ieee->link_change(ieee->dev);
2550 	notify_wx_assoc_event(ieee);
2551 
2552 	if (ieee->data_hard_resume)
2553 		ieee->data_hard_resume(ieee->dev);
2554 
2555 	netif_carrier_on(ieee->dev);
2556 }
2557 
2558 static void rtllib_start_monitor_mode(struct rtllib_device *ieee)
2559 {
2560 	/* reset hardware status */
2561 	if (ieee->raw_tx) {
2562 		if (ieee->data_hard_resume)
2563 			ieee->data_hard_resume(ieee->dev);
2564 
2565 		netif_carrier_on(ieee->dev);
2566 	}
2567 }
2568 
2569 static void rtllib_start_ibss_wq(void *data)
2570 {
2571 	struct rtllib_device *ieee = container_of_dwork_rsl(data,
2572 				     struct rtllib_device, start_ibss_wq);
2573 	/* iwconfig mode ad-hoc will schedule this and return
2574 	 * on the other hand this will block further iwconfig SET
2575 	 * operations because of the wx_mutex hold.
2576 	 * Anyway some most set operations set a flag to speed-up
2577 	 * (abort) this wq (when syncro scanning) before sleeping
2578 	 * on the mutex
2579 	 */
2580 	if (!ieee->proto_started) {
2581 		netdev_info(ieee->dev, "==========oh driver down return\n");
2582 		return;
2583 	}
2584 	mutex_lock(&ieee->wx_mutex);
2585 
2586 	if (ieee->current_network.ssid_len == 0) {
2587 		strcpy(ieee->current_network.ssid, RTLLIB_DEFAULT_TX_ESSID);
2588 		ieee->current_network.ssid_len = strlen(RTLLIB_DEFAULT_TX_ESSID);
2589 		ieee->ssid_set = 1;
2590 	}
2591 
2592 	ieee->state = RTLLIB_NOLINK;
2593 	ieee->mode = IEEE_G;
2594 	/* check if we have this cell in our network list */
2595 	rtllib_softmac_check_all_nets(ieee);
2596 
2597 
2598 	/* if not then the state is not linked. Maybe the user switched to
2599 	 * ad-hoc mode just after being in monitor mode, or just after
2600 	 * being very few time in managed mode (so the card have had no
2601 	 * time to scan all the chans..) or we have just run up the iface
2602 	 * after setting ad-hoc mode. So we have to give another try..
2603 	 * Here, in ibss mode, should be safe to do this without extra care
2604 	 * (in bss mode we had to make sure no-one tried to associate when
2605 	 * we had just checked the ieee->state and we was going to start the
2606 	 * scan) because in ibss mode the rtllib_new_net function, when
2607 	 * finds a good net, just set the ieee->state to RTLLIB_LINKED,
2608 	 * so, at worst, we waste a bit of time to initiate an unneeded syncro
2609 	 * scan, that will stop at the first round because it sees the state
2610 	 * associated.
2611 	 */
2612 	if (ieee->state == RTLLIB_NOLINK)
2613 		rtllib_start_scan_syncro(ieee, 0);
2614 
2615 	/* the network definitively is not here.. create a new cell */
2616 	if (ieee->state == RTLLIB_NOLINK) {
2617 		netdev_info(ieee->dev, "creating new IBSS cell\n");
2618 		ieee->current_network.channel = ieee->bss_start_channel;
2619 		if (!ieee->wap_set)
2620 			eth_random_addr(ieee->current_network.bssid);
2621 
2622 		if (ieee->modulation & RTLLIB_CCK_MODULATION) {
2623 
2624 			ieee->current_network.rates_len = 4;
2625 
2626 			ieee->current_network.rates[0] =
2627 				 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
2628 			ieee->current_network.rates[1] =
2629 				 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
2630 			ieee->current_network.rates[2] =
2631 				 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
2632 			ieee->current_network.rates[3] =
2633 				 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
2634 
2635 		} else
2636 			ieee->current_network.rates_len = 0;
2637 
2638 		if (ieee->modulation & RTLLIB_OFDM_MODULATION) {
2639 			ieee->current_network.rates_ex_len = 8;
2640 
2641 			ieee->current_network.rates_ex[0] =
2642 						 RTLLIB_OFDM_RATE_6MB;
2643 			ieee->current_network.rates_ex[1] =
2644 						 RTLLIB_OFDM_RATE_9MB;
2645 			ieee->current_network.rates_ex[2] =
2646 						 RTLLIB_OFDM_RATE_12MB;
2647 			ieee->current_network.rates_ex[3] =
2648 						 RTLLIB_OFDM_RATE_18MB;
2649 			ieee->current_network.rates_ex[4] =
2650 						 RTLLIB_OFDM_RATE_24MB;
2651 			ieee->current_network.rates_ex[5] =
2652 						 RTLLIB_OFDM_RATE_36MB;
2653 			ieee->current_network.rates_ex[6] =
2654 						 RTLLIB_OFDM_RATE_48MB;
2655 			ieee->current_network.rates_ex[7] =
2656 						 RTLLIB_OFDM_RATE_54MB;
2657 
2658 			ieee->rate = 108;
2659 		} else {
2660 			ieee->current_network.rates_ex_len = 0;
2661 			ieee->rate = 22;
2662 		}
2663 
2664 		ieee->current_network.qos_data.supported = 0;
2665 		ieee->SetWirelessMode(ieee->dev, IEEE_G);
2666 		ieee->current_network.mode = ieee->mode;
2667 		ieee->current_network.atim_window = 0;
2668 		ieee->current_network.capability = WLAN_CAPABILITY_IBSS;
2669 	}
2670 
2671 	netdev_info(ieee->dev, "%s(): ieee->mode = %d\n", __func__, ieee->mode);
2672 	if ((ieee->mode == IEEE_N_24G) || (ieee->mode == IEEE_N_5G))
2673 		HTUseDefaultSetting(ieee);
2674 	else
2675 		ieee->pHTInfo->bCurrentHTSupport = false;
2676 
2677 	ieee->SetHwRegHandler(ieee->dev, HW_VAR_MEDIA_STATUS,
2678 			      (u8 *)(&ieee->state));
2679 
2680 	ieee->state = RTLLIB_LINKED;
2681 	ieee->link_change(ieee->dev);
2682 
2683 	HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
2684 	if (ieee->LedControlHandler != NULL)
2685 		ieee->LedControlHandler(ieee->dev, LED_CTL_LINK);
2686 
2687 	rtllib_start_send_beacons(ieee);
2688 
2689 	notify_wx_assoc_event(ieee);
2690 
2691 	if (ieee->data_hard_resume)
2692 		ieee->data_hard_resume(ieee->dev);
2693 
2694 	netif_carrier_on(ieee->dev);
2695 
2696 	mutex_unlock(&ieee->wx_mutex);
2697 }
2698 
2699 inline void rtllib_start_ibss(struct rtllib_device *ieee)
2700 {
2701 	schedule_delayed_work(&ieee->start_ibss_wq, msecs_to_jiffies(150));
2702 }
2703 
2704 /* this is called only in user context, with wx_mutex held */
2705 static void rtllib_start_bss(struct rtllib_device *ieee)
2706 {
2707 	unsigned long flags;
2708 
2709 	if (IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee)) {
2710 		if (!ieee->global_domain)
2711 			return;
2712 	}
2713 	/* check if we have already found the net we
2714 	 * are interested in (if any).
2715 	 * if not (we are disassociated and we are not
2716 	 * in associating / authenticating phase) start the background scanning.
2717 	 */
2718 	rtllib_softmac_check_all_nets(ieee);
2719 
2720 	/* ensure no-one start an associating process (thus setting
2721 	 * the ieee->state to rtllib_ASSOCIATING) while we
2722 	 * have just checked it and we are going to enable scan.
2723 	 * The rtllib_new_net function is always called with
2724 	 * lock held (from both rtllib_softmac_check_all_nets and
2725 	 * the rx path), so we cannot be in the middle of such function
2726 	 */
2727 	spin_lock_irqsave(&ieee->lock, flags);
2728 
2729 	if (ieee->state == RTLLIB_NOLINK)
2730 		rtllib_start_scan(ieee);
2731 	spin_unlock_irqrestore(&ieee->lock, flags);
2732 }
2733 
2734 static void rtllib_link_change_wq(void *data)
2735 {
2736 	struct rtllib_device *ieee = container_of_dwork_rsl(data,
2737 				     struct rtllib_device, link_change_wq);
2738 	ieee->link_change(ieee->dev);
2739 }
2740 /* called only in userspace context */
2741 void rtllib_disassociate(struct rtllib_device *ieee)
2742 {
2743 	netif_carrier_off(ieee->dev);
2744 	if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2745 		rtllib_reset_queue(ieee);
2746 
2747 	if (ieee->data_hard_stop)
2748 		ieee->data_hard_stop(ieee->dev);
2749 	if (IS_DOT11D_ENABLE(ieee))
2750 		dot11d_reset(ieee);
2751 	ieee->state = RTLLIB_NOLINK;
2752 	ieee->is_set_key = false;
2753 	ieee->wap_set = 0;
2754 
2755 	schedule_delayed_work(&ieee->link_change_wq, 0);
2756 
2757 	notify_wx_assoc_event(ieee);
2758 }
2759 
2760 static void rtllib_associate_retry_wq(void *data)
2761 {
2762 	struct rtllib_device *ieee = container_of_dwork_rsl(data,
2763 				     struct rtllib_device, associate_retry_wq);
2764 	unsigned long flags;
2765 
2766 	mutex_lock(&ieee->wx_mutex);
2767 	if (!ieee->proto_started)
2768 		goto exit;
2769 
2770 	if (ieee->state != RTLLIB_ASSOCIATING_RETRY)
2771 		goto exit;
2772 
2773 	/* until we do not set the state to RTLLIB_NOLINK
2774 	 * there are no possibility to have someone else trying
2775 	 * to start an association procedure (we get here with
2776 	 * ieee->state = RTLLIB_ASSOCIATING).
2777 	 * When we set the state to RTLLIB_NOLINK it is possible
2778 	 * that the RX path run an attempt to associate, but
2779 	 * both rtllib_softmac_check_all_nets and the
2780 	 * RX path works with ieee->lock held so there are no
2781 	 * problems. If we are still disassociated then start a scan.
2782 	 * the lock here is necessary to ensure no one try to start
2783 	 * an association procedure when we have just checked the
2784 	 * state and we are going to start the scan.
2785 	 */
2786 	ieee->beinretry = true;
2787 	ieee->state = RTLLIB_NOLINK;
2788 
2789 	rtllib_softmac_check_all_nets(ieee);
2790 
2791 	spin_lock_irqsave(&ieee->lock, flags);
2792 
2793 	if (ieee->state == RTLLIB_NOLINK)
2794 		rtllib_start_scan(ieee);
2795 	spin_unlock_irqrestore(&ieee->lock, flags);
2796 
2797 	ieee->beinretry = false;
2798 exit:
2799 	mutex_unlock(&ieee->wx_mutex);
2800 }
2801 
2802 static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee)
2803 {
2804 	static const u8 broadcast_addr[] = {
2805 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2806 	};
2807 	struct sk_buff *skb;
2808 	struct rtllib_probe_response *b;
2809 
2810 	skb = rtllib_probe_resp(ieee, broadcast_addr);
2811 
2812 	if (!skb)
2813 		return NULL;
2814 
2815 	b = (struct rtllib_probe_response *) skb->data;
2816 	b->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_BEACON);
2817 
2818 	return skb;
2819 
2820 }
2821 
2822 struct sk_buff *rtllib_get_beacon(struct rtllib_device *ieee)
2823 {
2824 	struct sk_buff *skb;
2825 	struct rtllib_probe_response *b;
2826 
2827 	skb = rtllib_get_beacon_(ieee);
2828 	if (!skb)
2829 		return NULL;
2830 
2831 	b = (struct rtllib_probe_response *) skb->data;
2832 	b->header.seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2833 
2834 	if (ieee->seq_ctrl[0] == 0xFFF)
2835 		ieee->seq_ctrl[0] = 0;
2836 	else
2837 		ieee->seq_ctrl[0]++;
2838 
2839 	return skb;
2840 }
2841 EXPORT_SYMBOL(rtllib_get_beacon);
2842 
2843 void rtllib_softmac_stop_protocol(struct rtllib_device *ieee, u8 mesh_flag,
2844 				  u8 shutdown)
2845 {
2846 	rtllib_stop_scan_syncro(ieee);
2847 	mutex_lock(&ieee->wx_mutex);
2848 	rtllib_stop_protocol(ieee, shutdown);
2849 	mutex_unlock(&ieee->wx_mutex);
2850 }
2851 EXPORT_SYMBOL(rtllib_softmac_stop_protocol);
2852 
2853 
2854 void rtllib_stop_protocol(struct rtllib_device *ieee, u8 shutdown)
2855 {
2856 	if (!ieee->proto_started)
2857 		return;
2858 
2859 	if (shutdown) {
2860 		ieee->proto_started = 0;
2861 		ieee->proto_stoppping = 1;
2862 		if (ieee->rtllib_ips_leave != NULL)
2863 			ieee->rtllib_ips_leave(ieee->dev);
2864 	}
2865 
2866 	rtllib_stop_send_beacons(ieee);
2867 	del_timer_sync(&ieee->associate_timer);
2868 	cancel_delayed_work_sync(&ieee->associate_retry_wq);
2869 	cancel_delayed_work_sync(&ieee->start_ibss_wq);
2870 	cancel_delayed_work_sync(&ieee->link_change_wq);
2871 	rtllib_stop_scan(ieee);
2872 
2873 	if (ieee->state <= RTLLIB_ASSOCIATING_AUTHENTICATED)
2874 		ieee->state = RTLLIB_NOLINK;
2875 
2876 	if (ieee->state == RTLLIB_LINKED) {
2877 		if (ieee->iw_mode == IW_MODE_INFRA)
2878 			SendDisassociation(ieee, 1, WLAN_REASON_DEAUTH_LEAVING);
2879 		rtllib_disassociate(ieee);
2880 	}
2881 
2882 	if (shutdown) {
2883 		RemoveAllTS(ieee);
2884 		ieee->proto_stoppping = 0;
2885 	}
2886 	kfree(ieee->assocreq_ies);
2887 	ieee->assocreq_ies = NULL;
2888 	ieee->assocreq_ies_len = 0;
2889 	kfree(ieee->assocresp_ies);
2890 	ieee->assocresp_ies = NULL;
2891 	ieee->assocresp_ies_len = 0;
2892 }
2893 
2894 void rtllib_softmac_start_protocol(struct rtllib_device *ieee, u8 mesh_flag)
2895 {
2896 	mutex_lock(&ieee->wx_mutex);
2897 	rtllib_start_protocol(ieee);
2898 	mutex_unlock(&ieee->wx_mutex);
2899 }
2900 EXPORT_SYMBOL(rtllib_softmac_start_protocol);
2901 
2902 void rtllib_start_protocol(struct rtllib_device *ieee)
2903 {
2904 	short ch = 0;
2905 	int i = 0;
2906 
2907 	rtllib_update_active_chan_map(ieee);
2908 
2909 	if (ieee->proto_started)
2910 		return;
2911 
2912 	ieee->proto_started = 1;
2913 
2914 	if (ieee->current_network.channel == 0) {
2915 		do {
2916 			ch++;
2917 			if (ch > MAX_CHANNEL_NUMBER)
2918 				return; /* no channel found */
2919 		} while (!ieee->active_channel_map[ch]);
2920 		ieee->current_network.channel = ch;
2921 	}
2922 
2923 	if (ieee->current_network.beacon_interval == 0)
2924 		ieee->current_network.beacon_interval = 100;
2925 
2926 	for (i = 0; i < 17; i++) {
2927 		ieee->last_rxseq_num[i] = -1;
2928 		ieee->last_rxfrag_num[i] = -1;
2929 		ieee->last_packet_time[i] = 0;
2930 	}
2931 
2932 	if (ieee->UpdateBeaconInterruptHandler)
2933 		ieee->UpdateBeaconInterruptHandler(ieee->dev, false);
2934 
2935 	ieee->wmm_acm = 0;
2936 	/* if the user set the MAC of the ad-hoc cell and then
2937 	 * switch to managed mode, shall we  make sure that association
2938 	 * attempts does not fail just because the user provide the essid
2939 	 * and the nic is still checking for the AP MAC ??
2940 	 */
2941 	if (ieee->iw_mode == IW_MODE_INFRA) {
2942 		rtllib_start_bss(ieee);
2943 	} else if (ieee->iw_mode == IW_MODE_ADHOC) {
2944 		if (ieee->UpdateBeaconInterruptHandler)
2945 			ieee->UpdateBeaconInterruptHandler(ieee->dev, true);
2946 
2947 		rtllib_start_ibss(ieee);
2948 
2949 	} else if (ieee->iw_mode == IW_MODE_MASTER) {
2950 		rtllib_start_master_bss(ieee);
2951 	} else if (ieee->iw_mode == IW_MODE_MONITOR) {
2952 		rtllib_start_monitor_mode(ieee);
2953 	}
2954 }
2955 
2956 void rtllib_softmac_init(struct rtllib_device *ieee)
2957 {
2958 	int i;
2959 
2960 	memset(&ieee->current_network, 0, sizeof(struct rtllib_network));
2961 
2962 	ieee->state = RTLLIB_NOLINK;
2963 	for (i = 0; i < 5; i++)
2964 		ieee->seq_ctrl[i] = 0;
2965 	ieee->dot11d_info = kzalloc(sizeof(struct rt_dot11d_info), GFP_ATOMIC);
2966 	if (!ieee->dot11d_info)
2967 		netdev_err(ieee->dev, "Can't alloc memory for DOT11D\n");
2968 	ieee->LinkDetectInfo.SlotIndex = 0;
2969 	ieee->LinkDetectInfo.SlotNum = 2;
2970 	ieee->LinkDetectInfo.NumRecvBcnInPeriod = 0;
2971 	ieee->LinkDetectInfo.NumRecvDataInPeriod = 0;
2972 	ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
2973 	ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
2974 	ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
2975 	ieee->bIsAggregateFrame = false;
2976 	ieee->assoc_id = 0;
2977 	ieee->queue_stop = 0;
2978 	ieee->scanning_continue = 0;
2979 	ieee->softmac_features = 0;
2980 	ieee->wap_set = 0;
2981 	ieee->ssid_set = 0;
2982 	ieee->proto_started = 0;
2983 	ieee->proto_stoppping = 0;
2984 	ieee->basic_rate = RTLLIB_DEFAULT_BASIC_RATE;
2985 	ieee->rate = 22;
2986 	ieee->ps = RTLLIB_PS_DISABLED;
2987 	ieee->sta_sleep = LPS_IS_WAKE;
2988 
2989 	ieee->Regdot11HTOperationalRateSet[0] = 0xff;
2990 	ieee->Regdot11HTOperationalRateSet[1] = 0xff;
2991 	ieee->Regdot11HTOperationalRateSet[4] = 0x01;
2992 
2993 	ieee->Regdot11TxHTOperationalRateSet[0] = 0xff;
2994 	ieee->Regdot11TxHTOperationalRateSet[1] = 0xff;
2995 	ieee->Regdot11TxHTOperationalRateSet[4] = 0x01;
2996 
2997 	ieee->FirstIe_InScan = false;
2998 	ieee->actscanning = false;
2999 	ieee->beinretry = false;
3000 	ieee->is_set_key = false;
3001 	init_mgmt_queue(ieee);
3002 
3003 	ieee->tx_pending.txb = NULL;
3004 
3005 	timer_setup(&ieee->associate_timer, rtllib_associate_abort_cb, 0);
3006 
3007 	timer_setup(&ieee->beacon_timer, rtllib_send_beacon_cb, 0);
3008 
3009 	INIT_DELAYED_WORK_RSL(&ieee->link_change_wq,
3010 			      (void *)rtllib_link_change_wq, ieee);
3011 	INIT_DELAYED_WORK_RSL(&ieee->start_ibss_wq,
3012 			      (void *)rtllib_start_ibss_wq, ieee);
3013 	INIT_WORK_RSL(&ieee->associate_complete_wq,
3014 		      (void *)rtllib_associate_complete_wq, ieee);
3015 	INIT_DELAYED_WORK_RSL(&ieee->associate_procedure_wq,
3016 			      (void *)rtllib_associate_procedure_wq, ieee);
3017 	INIT_DELAYED_WORK_RSL(&ieee->softmac_scan_wq,
3018 			      (void *)rtllib_softmac_scan_wq, ieee);
3019 	INIT_DELAYED_WORK_RSL(&ieee->associate_retry_wq,
3020 			      (void *)rtllib_associate_retry_wq, ieee);
3021 	INIT_WORK_RSL(&ieee->wx_sync_scan_wq, (void *)rtllib_wx_sync_scan_wq,
3022 		      ieee);
3023 
3024 	mutex_init(&ieee->wx_mutex);
3025 	mutex_init(&ieee->scan_mutex);
3026 	mutex_init(&ieee->ips_mutex);
3027 
3028 	spin_lock_init(&ieee->mgmt_tx_lock);
3029 	spin_lock_init(&ieee->beacon_lock);
3030 
3031 	tasklet_init(&ieee->ps_task, rtllib_sta_ps, (unsigned long)ieee);
3032 
3033 }
3034 
3035 void rtllib_softmac_free(struct rtllib_device *ieee)
3036 {
3037 	mutex_lock(&ieee->wx_mutex);
3038 	kfree(ieee->dot11d_info);
3039 	ieee->dot11d_info = NULL;
3040 	del_timer_sync(&ieee->associate_timer);
3041 
3042 	cancel_delayed_work_sync(&ieee->associate_retry_wq);
3043 	cancel_delayed_work_sync(&ieee->associate_procedure_wq);
3044 	cancel_delayed_work_sync(&ieee->softmac_scan_wq);
3045 	cancel_delayed_work_sync(&ieee->start_ibss_wq);
3046 	cancel_delayed_work_sync(&ieee->hw_wakeup_wq);
3047 	cancel_delayed_work_sync(&ieee->hw_sleep_wq);
3048 	cancel_delayed_work_sync(&ieee->link_change_wq);
3049 	cancel_work_sync(&ieee->associate_complete_wq);
3050 	cancel_work_sync(&ieee->ips_leave_wq);
3051 	cancel_work_sync(&ieee->wx_sync_scan_wq);
3052 	mutex_unlock(&ieee->wx_mutex);
3053 	tasklet_kill(&ieee->ps_task);
3054 }
3055 
3056 static inline struct sk_buff *
3057 rtllib_disauth_skb(struct rtllib_network *beacon,
3058 		   struct rtllib_device *ieee, u16 asRsn)
3059 {
3060 	struct sk_buff *skb;
3061 	struct rtllib_disauth *disauth;
3062 	int len = sizeof(struct rtllib_disauth) + ieee->tx_headroom;
3063 
3064 	skb = dev_alloc_skb(len);
3065 	if (!skb)
3066 		return NULL;
3067 
3068 	skb_reserve(skb, ieee->tx_headroom);
3069 
3070 	disauth = skb_put(skb, sizeof(struct rtllib_disauth));
3071 	disauth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_DEAUTH);
3072 	disauth->header.duration_id = 0;
3073 
3074 	ether_addr_copy(disauth->header.addr1, beacon->bssid);
3075 	ether_addr_copy(disauth->header.addr2, ieee->dev->dev_addr);
3076 	ether_addr_copy(disauth->header.addr3, beacon->bssid);
3077 
3078 	disauth->reason = cpu_to_le16(asRsn);
3079 	return skb;
3080 }
3081 
3082 static inline struct sk_buff *
3083 rtllib_disassociate_skb(struct rtllib_network *beacon,
3084 			struct rtllib_device *ieee, u16 asRsn)
3085 {
3086 	struct sk_buff *skb;
3087 	struct rtllib_disassoc *disass;
3088 	int len = sizeof(struct rtllib_disassoc) + ieee->tx_headroom;
3089 
3090 	skb = dev_alloc_skb(len);
3091 
3092 	if (!skb)
3093 		return NULL;
3094 
3095 	skb_reserve(skb, ieee->tx_headroom);
3096 
3097 	disass = skb_put(skb, sizeof(struct rtllib_disassoc));
3098 	disass->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_DISASSOC);
3099 	disass->header.duration_id = 0;
3100 
3101 	ether_addr_copy(disass->header.addr1, beacon->bssid);
3102 	ether_addr_copy(disass->header.addr2, ieee->dev->dev_addr);
3103 	ether_addr_copy(disass->header.addr3, beacon->bssid);
3104 
3105 	disass->reason = cpu_to_le16(asRsn);
3106 	return skb;
3107 }
3108 
3109 void SendDisassociation(struct rtllib_device *ieee, bool deauth, u16 asRsn)
3110 {
3111 	struct rtllib_network *beacon = &ieee->current_network;
3112 	struct sk_buff *skb;
3113 
3114 	if (deauth)
3115 		skb = rtllib_disauth_skb(beacon, ieee, asRsn);
3116 	else
3117 		skb = rtllib_disassociate_skb(beacon, ieee, asRsn);
3118 
3119 	if (skb)
3120 		softmac_mgmt_xmit(skb, ieee);
3121 }
3122 
3123 u8 rtllib_ap_sec_type(struct rtllib_device *ieee)
3124 {
3125 	static u8 ccmp_ie[4] = {0x00, 0x50, 0xf2, 0x04};
3126 	static u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
3127 	int wpa_ie_len = ieee->wpa_ie_len;
3128 	struct lib80211_crypt_data *crypt;
3129 	int encrypt;
3130 
3131 	crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
3132 	encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY)
3133 		  || (ieee->host_encrypt && crypt && crypt->ops &&
3134 		  (strcmp(crypt->ops->name, "R-WEP") == 0));
3135 
3136 	/* simply judge  */
3137 	if (encrypt && (wpa_ie_len == 0)) {
3138 		return SEC_ALG_WEP;
3139 	} else if ((wpa_ie_len != 0)) {
3140 		if (((ieee->wpa_ie[0] == 0xdd) &&
3141 		    (!memcmp(&(ieee->wpa_ie[14]), ccmp_ie, 4))) ||
3142 		    ((ieee->wpa_ie[0] == 0x30) &&
3143 		    (!memcmp(&ieee->wpa_ie[10], ccmp_rsn_ie, 4))))
3144 			return SEC_ALG_CCMP;
3145 		else
3146 			return SEC_ALG_TKIP;
3147 	} else {
3148 		return SEC_ALG_NONE;
3149 	}
3150 }
3151 
3152 static void rtllib_MgntDisconnectIBSS(struct rtllib_device *rtllib)
3153 {
3154 	u8	OpMode;
3155 	u8	i;
3156 	bool	bFilterOutNonAssociatedBSSID = false;
3157 
3158 	rtllib->state = RTLLIB_NOLINK;
3159 
3160 	for (i = 0; i < 6; i++)
3161 		rtllib->current_network.bssid[i] = 0x55;
3162 
3163 	rtllib->OpMode = RT_OP_MODE_NO_LINK;
3164 	rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
3165 				rtllib->current_network.bssid);
3166 	OpMode = RT_OP_MODE_NO_LINK;
3167 	rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS, &OpMode);
3168 	rtllib_stop_send_beacons(rtllib);
3169 
3170 	bFilterOutNonAssociatedBSSID = false;
3171 	rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
3172 				(u8 *)(&bFilterOutNonAssociatedBSSID));
3173 	notify_wx_assoc_event(rtllib);
3174 
3175 }
3176 
3177 static void rtllib_MlmeDisassociateRequest(struct rtllib_device *rtllib,
3178 					   u8 *asSta, u8 asRsn)
3179 {
3180 	u8 i;
3181 	u8	OpMode;
3182 
3183 	RemovePeerTS(rtllib, asSta);
3184 
3185 	if (memcmp(rtllib->current_network.bssid, asSta, 6) == 0) {
3186 		rtllib->state = RTLLIB_NOLINK;
3187 
3188 		for (i = 0; i < 6; i++)
3189 			rtllib->current_network.bssid[i] = 0x22;
3190 		OpMode = RT_OP_MODE_NO_LINK;
3191 		rtllib->OpMode = RT_OP_MODE_NO_LINK;
3192 		rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS,
3193 					(u8 *)(&OpMode));
3194 		rtllib_disassociate(rtllib);
3195 
3196 		rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
3197 					rtllib->current_network.bssid);
3198 
3199 	}
3200 
3201 }
3202 
3203 static void
3204 rtllib_MgntDisconnectAP(
3205 	struct rtllib_device *rtllib,
3206 	u8 asRsn
3207 )
3208 {
3209 	bool bFilterOutNonAssociatedBSSID = false;
3210 
3211 	bFilterOutNonAssociatedBSSID = false;
3212 	rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
3213 				(u8 *)(&bFilterOutNonAssociatedBSSID));
3214 	rtllib_MlmeDisassociateRequest(rtllib, rtllib->current_network.bssid,
3215 				       asRsn);
3216 
3217 	rtllib->state = RTLLIB_NOLINK;
3218 }
3219 
3220 bool rtllib_MgntDisconnect(struct rtllib_device *rtllib, u8 asRsn)
3221 {
3222 	if (rtllib->ps != RTLLIB_PS_DISABLED)
3223 		rtllib->sta_wake_up(rtllib->dev);
3224 
3225 	if (rtllib->state == RTLLIB_LINKED) {
3226 		if (rtllib->iw_mode == IW_MODE_ADHOC)
3227 			rtllib_MgntDisconnectIBSS(rtllib);
3228 		if (rtllib->iw_mode == IW_MODE_INFRA)
3229 			rtllib_MgntDisconnectAP(rtllib, asRsn);
3230 
3231 	}
3232 
3233 	return true;
3234 }
3235 EXPORT_SYMBOL(rtllib_MgntDisconnect);
3236 
3237 void notify_wx_assoc_event(struct rtllib_device *ieee)
3238 {
3239 	union iwreq_data wrqu;
3240 
3241 	if (ieee->cannot_notify)
3242 		return;
3243 
3244 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3245 	if (ieee->state == RTLLIB_LINKED)
3246 		memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid,
3247 		       ETH_ALEN);
3248 	else {
3249 
3250 		netdev_info(ieee->dev, "%s(): Tell user space disconnected\n",
3251 			    __func__);
3252 		eth_zero_addr(wrqu.ap_addr.sa_data);
3253 	}
3254 	wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
3255 }
3256 EXPORT_SYMBOL(notify_wx_assoc_event);
3257