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->iot_action & 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->rf_power_state == rf_off) {
590 		netdev_info(ieee->dev,
591 			    "======>%s():rf state is rf_off, 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 
655 	spin_unlock_irqrestore(&ieee->beacon_lock, flags);
656 	del_timer_sync(&ieee->beacon_timer);
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 	if (ieee->rtllib_ips_leave_wq != NULL)
734 		ieee->rtllib_ips_leave_wq(ieee->dev);
735 
736 	if (IS_DOT11D_ENABLE(ieee)) {
737 		if (IS_COUNTRY_IE_VALID(ieee))
738 			RESET_CIE_WATCHDOG(ieee);
739 	}
740 	if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
741 		if (ieee->scanning_continue == 0) {
742 			ieee->actscanning = true;
743 			ieee->scanning_continue = 1;
744 			schedule_delayed_work(&ieee->softmac_scan_wq, 0);
745 		}
746 	} else {
747 		if (ieee->rtllib_start_hw_scan)
748 			ieee->rtllib_start_hw_scan(ieee->dev);
749 	}
750 }
751 
752 /* called with wx_mutex held */
753 void rtllib_start_scan_syncro(struct rtllib_device *ieee, u8 is_mesh)
754 {
755 	if (IS_DOT11D_ENABLE(ieee)) {
756 		if (IS_COUNTRY_IE_VALID(ieee))
757 			RESET_CIE_WATCHDOG(ieee);
758 	}
759 	ieee->sync_scan_hurryup = 0;
760 	if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
761 		rtllib_softmac_scan_syncro(ieee, is_mesh);
762 	} else {
763 		if (ieee->rtllib_start_hw_scan)
764 			ieee->rtllib_start_hw_scan(ieee->dev);
765 	}
766 }
767 EXPORT_SYMBOL(rtllib_start_scan_syncro);
768 
769 static inline struct sk_buff *
770 rtllib_authentication_req(struct rtllib_network *beacon,
771 			  struct rtllib_device *ieee,
772 			  int challengelen, u8 *daddr)
773 {
774 	struct sk_buff *skb;
775 	struct rtllib_authentication *auth;
776 	int  len;
777 
778 	len = sizeof(struct rtllib_authentication) + challengelen +
779 		     ieee->tx_headroom + 4;
780 	skb = dev_alloc_skb(len);
781 
782 	if (!skb)
783 		return NULL;
784 
785 	skb_reserve(skb, ieee->tx_headroom);
786 
787 	auth = skb_put(skb, sizeof(struct rtllib_authentication));
788 
789 	auth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_AUTH);
790 	if (challengelen)
791 		auth->header.frame_ctl |= cpu_to_le16(RTLLIB_FCTL_WEP);
792 
793 	auth->header.duration_id = cpu_to_le16(0x013a);
794 	ether_addr_copy(auth->header.addr1, beacon->bssid);
795 	ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
796 	ether_addr_copy(auth->header.addr3, beacon->bssid);
797 	if (ieee->auth_mode == 0)
798 		auth->algorithm = WLAN_AUTH_OPEN;
799 	else if (ieee->auth_mode == 1)
800 		auth->algorithm = cpu_to_le16(WLAN_AUTH_SHARED_KEY);
801 	else if (ieee->auth_mode == 2)
802 		auth->algorithm = WLAN_AUTH_OPEN;
803 	auth->transaction = cpu_to_le16(ieee->associate_seq);
804 	ieee->associate_seq++;
805 
806 	auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
807 
808 	return skb;
809 }
810 
811 static struct sk_buff *rtllib_probe_resp(struct rtllib_device *ieee,
812 					 const u8 *dest)
813 {
814 	u8 *tag;
815 	int beacon_size;
816 	struct rtllib_probe_response *beacon_buf;
817 	struct sk_buff *skb = NULL;
818 	int encrypt;
819 	int atim_len, erp_len;
820 	struct lib80211_crypt_data *crypt;
821 
822 	char *ssid = ieee->current_network.ssid;
823 	int ssid_len = ieee->current_network.ssid_len;
824 	int rate_len = ieee->current_network.rates_len+2;
825 	int rate_ex_len = ieee->current_network.rates_ex_len;
826 	int wpa_ie_len = ieee->wpa_ie_len;
827 	u8 erpinfo_content = 0;
828 
829 	u8 *tmp_ht_cap_buf = NULL;
830 	u8 tmp_ht_cap_len = 0;
831 	u8 *tmp_ht_info_buf = NULL;
832 	u8 tmp_ht_info_len = 0;
833 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
834 	u8 *tmp_generic_ie_buf = NULL;
835 	u8 tmp_generic_ie_len = 0;
836 
837 	if (rate_ex_len > 0)
838 		rate_ex_len += 2;
839 
840 	if (ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
841 		atim_len = 4;
842 	else
843 		atim_len = 0;
844 
845 	if ((ieee->current_network.mode == IEEE_G) ||
846 	   (ieee->current_network.mode == IEEE_N_24G &&
847 	   ieee->pHTInfo->bCurSuppCCK)) {
848 		erp_len = 3;
849 		erpinfo_content = 0;
850 		if (ieee->current_network.buseprotection)
851 			erpinfo_content |= ERP_UseProtection;
852 	} else
853 		erp_len = 0;
854 
855 	crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
856 	encrypt = ieee->host_encrypt && crypt && crypt->ops &&
857 		((strcmp(crypt->ops->name, "R-WEP") == 0 || wpa_ie_len));
858 	if (ieee->pHTInfo->bCurrentHTSupport) {
859 		tmp_ht_cap_buf = (u8 *)&(ieee->pHTInfo->SelfHTCap);
860 		tmp_ht_cap_len = sizeof(ieee->pHTInfo->SelfHTCap);
861 		tmp_ht_info_buf = (u8 *)&(ieee->pHTInfo->SelfHTInfo);
862 		tmp_ht_info_len = sizeof(ieee->pHTInfo->SelfHTInfo);
863 		HTConstructCapabilityElement(ieee, tmp_ht_cap_buf,
864 					     &tmp_ht_cap_len, encrypt, false);
865 		HTConstructInfoElement(ieee, tmp_ht_info_buf, &tmp_ht_info_len,
866 				       encrypt);
867 
868 		if (pHTInfo->reg_rt2rt_aggregation) {
869 			tmp_generic_ie_buf = ieee->pHTInfo->sz_rt2rt_agg_buf;
870 			tmp_generic_ie_len =
871 				 sizeof(ieee->pHTInfo->sz_rt2rt_agg_buf);
872 			HTConstructRT2RTAggElement(ieee, tmp_generic_ie_buf,
873 						   &tmp_generic_ie_len);
874 		}
875 	}
876 
877 	beacon_size = sizeof(struct rtllib_probe_response)+2+
878 		ssid_len + 3 + rate_len + rate_ex_len + atim_len + erp_len
879 		+ wpa_ie_len + ieee->tx_headroom;
880 	skb = dev_alloc_skb(beacon_size);
881 	if (!skb)
882 		return NULL;
883 
884 	skb_reserve(skb, ieee->tx_headroom);
885 
886 	beacon_buf = skb_put(skb, (beacon_size - ieee->tx_headroom));
887 	ether_addr_copy(beacon_buf->header.addr1, dest);
888 	ether_addr_copy(beacon_buf->header.addr2, ieee->dev->dev_addr);
889 	ether_addr_copy(beacon_buf->header.addr3, ieee->current_network.bssid);
890 
891 	beacon_buf->header.duration_id = 0;
892 	beacon_buf->beacon_interval =
893 		cpu_to_le16(ieee->current_network.beacon_interval);
894 	beacon_buf->capability =
895 		cpu_to_le16(ieee->current_network.capability &
896 		WLAN_CAPABILITY_IBSS);
897 	beacon_buf->capability |=
898 		cpu_to_le16(ieee->current_network.capability &
899 		WLAN_CAPABILITY_SHORT_PREAMBLE);
900 
901 	if (ieee->short_slot && (ieee->current_network.capability &
902 	    WLAN_CAPABILITY_SHORT_SLOT_TIME))
903 		beacon_buf->capability |=
904 			cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
905 
906 	crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
907 	if (encrypt)
908 		beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
909 
910 
911 	beacon_buf->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_PROBE_RESP);
912 	beacon_buf->info_element[0].id = MFIE_TYPE_SSID;
913 	beacon_buf->info_element[0].len = ssid_len;
914 
915 	tag = (u8 *)beacon_buf->info_element[0].data;
916 
917 	memcpy(tag, ssid, ssid_len);
918 
919 	tag += ssid_len;
920 
921 	*(tag++) = MFIE_TYPE_RATES;
922 	*(tag++) = rate_len-2;
923 	memcpy(tag, ieee->current_network.rates, rate_len-2);
924 	tag += rate_len-2;
925 
926 	*(tag++) = MFIE_TYPE_DS_SET;
927 	*(tag++) = 1;
928 	*(tag++) = ieee->current_network.channel;
929 
930 	if (atim_len) {
931 		u16 val16;
932 		*(tag++) = MFIE_TYPE_IBSS_SET;
933 		*(tag++) = 2;
934 		val16 = ieee->current_network.atim_window;
935 		memcpy((u8 *)tag, (u8 *)&val16, 2);
936 		tag += 2;
937 	}
938 
939 	if (erp_len) {
940 		*(tag++) = MFIE_TYPE_ERP;
941 		*(tag++) = 1;
942 		*(tag++) = erpinfo_content;
943 	}
944 	if (rate_ex_len) {
945 		*(tag++) = MFIE_TYPE_RATES_EX;
946 		*(tag++) = rate_ex_len-2;
947 		memcpy(tag, ieee->current_network.rates_ex, rate_ex_len-2);
948 		tag += rate_ex_len-2;
949 	}
950 
951 	if (wpa_ie_len) {
952 		if (ieee->iw_mode == IW_MODE_ADHOC)
953 			memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
954 		memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
955 		tag += ieee->wpa_ie_len;
956 	}
957 	return skb;
958 }
959 
960 static struct sk_buff *rtllib_assoc_resp(struct rtllib_device *ieee, u8 *dest)
961 {
962 	struct sk_buff *skb;
963 	u8 *tag;
964 
965 	struct lib80211_crypt_data *crypt;
966 	struct rtllib_assoc_response_frame *assoc;
967 	short encrypt;
968 
969 	unsigned int rate_len = rtllib_MFIE_rate_len(ieee);
970 	int len = sizeof(struct rtllib_assoc_response_frame) + rate_len +
971 		  ieee->tx_headroom;
972 
973 	skb = dev_alloc_skb(len);
974 
975 	if (!skb)
976 		return NULL;
977 
978 	skb_reserve(skb, ieee->tx_headroom);
979 
980 	assoc = skb_put(skb, sizeof(struct rtllib_assoc_response_frame));
981 
982 	assoc->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_ASSOC_RESP);
983 	ether_addr_copy(assoc->header.addr1, dest);
984 	ether_addr_copy(assoc->header.addr3, ieee->dev->dev_addr);
985 	ether_addr_copy(assoc->header.addr2, ieee->dev->dev_addr);
986 	assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
987 		WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS);
988 
989 
990 	if (ieee->short_slot)
991 		assoc->capability |=
992 				 cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
993 
994 	if (ieee->host_encrypt)
995 		crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
996 	else
997 		crypt = NULL;
998 
999 	encrypt = (crypt && crypt->ops);
1000 
1001 	if (encrypt)
1002 		assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1003 
1004 	assoc->status = 0;
1005 	assoc->aid = cpu_to_le16(ieee->assoc_id);
1006 	if (ieee->assoc_id == 0x2007)
1007 		ieee->assoc_id = 0;
1008 	else
1009 		ieee->assoc_id++;
1010 
1011 	tag = skb_put(skb, rate_len);
1012 	rtllib_MFIE_Brate(ieee, &tag);
1013 	rtllib_MFIE_Grate(ieee, &tag);
1014 
1015 	return skb;
1016 }
1017 
1018 static struct sk_buff *rtllib_auth_resp(struct rtllib_device *ieee, int status,
1019 				 u8 *dest)
1020 {
1021 	struct sk_buff *skb = NULL;
1022 	struct rtllib_authentication *auth;
1023 	int len = ieee->tx_headroom + sizeof(struct rtllib_authentication) + 1;
1024 
1025 	skb = dev_alloc_skb(len);
1026 	if (!skb)
1027 		return NULL;
1028 
1029 	skb->len = sizeof(struct rtllib_authentication);
1030 
1031 	skb_reserve(skb, ieee->tx_headroom);
1032 
1033 	auth = skb_put(skb, sizeof(struct rtllib_authentication));
1034 
1035 	auth->status = cpu_to_le16(status);
1036 	auth->transaction = cpu_to_le16(2);
1037 	auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
1038 
1039 	ether_addr_copy(auth->header.addr3, ieee->dev->dev_addr);
1040 	ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
1041 	ether_addr_copy(auth->header.addr1, dest);
1042 	auth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_AUTH);
1043 	return skb;
1044 
1045 
1046 }
1047 
1048 static struct sk_buff *rtllib_null_func(struct rtllib_device *ieee, short pwr)
1049 {
1050 	struct sk_buff *skb;
1051 	struct rtllib_hdr_3addr *hdr;
1052 
1053 	skb = dev_alloc_skb(sizeof(struct rtllib_hdr_3addr)+ieee->tx_headroom);
1054 	if (!skb)
1055 		return NULL;
1056 
1057 	skb_reserve(skb, ieee->tx_headroom);
1058 
1059 	hdr = skb_put(skb, sizeof(struct rtllib_hdr_3addr));
1060 
1061 	ether_addr_copy(hdr->addr1, ieee->current_network.bssid);
1062 	ether_addr_copy(hdr->addr2, ieee->dev->dev_addr);
1063 	ether_addr_copy(hdr->addr3, ieee->current_network.bssid);
1064 
1065 	hdr->frame_ctl = cpu_to_le16(RTLLIB_FTYPE_DATA |
1066 		RTLLIB_STYPE_NULLFUNC | RTLLIB_FCTL_TODS |
1067 		(pwr ? RTLLIB_FCTL_PM : 0));
1068 
1069 	return skb;
1070 
1071 
1072 }
1073 
1074 static struct sk_buff *rtllib_pspoll_func(struct rtllib_device *ieee)
1075 {
1076 	struct sk_buff *skb;
1077 	struct rtllib_pspoll_hdr *hdr;
1078 
1079 	skb = dev_alloc_skb(sizeof(struct rtllib_pspoll_hdr)+ieee->tx_headroom);
1080 	if (!skb)
1081 		return NULL;
1082 
1083 	skb_reserve(skb, ieee->tx_headroom);
1084 
1085 	hdr = skb_put(skb, sizeof(struct rtllib_pspoll_hdr));
1086 
1087 	ether_addr_copy(hdr->bssid, ieee->current_network.bssid);
1088 	ether_addr_copy(hdr->ta, ieee->dev->dev_addr);
1089 
1090 	hdr->aid = cpu_to_le16(ieee->assoc_id | 0xc000);
1091 	hdr->frame_ctl = cpu_to_le16(RTLLIB_FTYPE_CTL | RTLLIB_STYPE_PSPOLL |
1092 			 RTLLIB_FCTL_PM);
1093 
1094 	return skb;
1095 
1096 }
1097 
1098 static void rtllib_resp_to_assoc_rq(struct rtllib_device *ieee, u8 *dest)
1099 {
1100 	struct sk_buff *buf = rtllib_assoc_resp(ieee, dest);
1101 
1102 	if (buf)
1103 		softmac_mgmt_xmit(buf, ieee);
1104 }
1105 
1106 
1107 static void rtllib_resp_to_auth(struct rtllib_device *ieee, int s, u8 *dest)
1108 {
1109 	struct sk_buff *buf = rtllib_auth_resp(ieee, s, dest);
1110 
1111 	if (buf)
1112 		softmac_mgmt_xmit(buf, ieee);
1113 }
1114 
1115 
1116 static void rtllib_resp_to_probe(struct rtllib_device *ieee, u8 *dest)
1117 {
1118 	struct sk_buff *buf = rtllib_probe_resp(ieee, dest);
1119 
1120 	if (buf)
1121 		softmac_mgmt_xmit(buf, ieee);
1122 }
1123 
1124 
1125 static inline int SecIsInPMKIDList(struct rtllib_device *ieee, u8 *bssid)
1126 {
1127 	int i = 0;
1128 
1129 	do {
1130 		if ((ieee->PMKIDList[i].bUsed) &&
1131 		   (memcmp(ieee->PMKIDList[i].Bssid, bssid, ETH_ALEN) == 0))
1132 			break;
1133 		i++;
1134 	} while (i < NUM_PMKID_CACHE);
1135 
1136 	if (i == NUM_PMKID_CACHE)
1137 		i = -1;
1138 	return i;
1139 }
1140 
1141 static inline struct sk_buff *
1142 rtllib_association_req(struct rtllib_network *beacon,
1143 		       struct rtllib_device *ieee)
1144 {
1145 	struct sk_buff *skb;
1146 	struct rtllib_assoc_request_frame *hdr;
1147 	u8 *tag, *ies;
1148 	int i;
1149 	u8 *ht_cap_buf = NULL;
1150 	u8 ht_cap_len = 0;
1151 	u8 *realtek_ie_buf = NULL;
1152 	u8 realtek_ie_len = 0;
1153 	int wpa_ie_len = ieee->wpa_ie_len;
1154 	int wps_ie_len = ieee->wps_ie_len;
1155 	unsigned int ckip_ie_len = 0;
1156 	unsigned int ccxrm_ie_len = 0;
1157 	unsigned int cxvernum_ie_len = 0;
1158 	struct lib80211_crypt_data *crypt;
1159 	int encrypt;
1160 	int	PMKCacheIdx;
1161 
1162 	unsigned int rate_len = (beacon->rates_len ?
1163 				(beacon->rates_len + 2) : 0) +
1164 				(beacon->rates_ex_len ? (beacon->rates_ex_len) +
1165 				2 : 0);
1166 
1167 	unsigned int wmm_info_len = beacon->qos_data.supported ? 9 : 0;
1168 	unsigned int turbo_info_len = beacon->Turbo_Enable ? 9 : 0;
1169 
1170 	int len = 0;
1171 
1172 	crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
1173 	if (crypt != NULL)
1174 		encrypt = ieee->host_encrypt && crypt && crypt->ops &&
1175 			  ((strcmp(crypt->ops->name, "R-WEP") == 0 ||
1176 			  wpa_ie_len));
1177 	else
1178 		encrypt = 0;
1179 
1180 	if ((ieee->rtllib_ap_sec_type &&
1181 	    (ieee->rtllib_ap_sec_type(ieee) & SEC_ALG_TKIP)) ||
1182 	    ieee->bForcedBgMode) {
1183 		ieee->pHTInfo->bEnableHT = 0;
1184 		ieee->mode = WIRELESS_MODE_G;
1185 	}
1186 
1187 	if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1188 		ht_cap_buf = (u8 *)&(ieee->pHTInfo->SelfHTCap);
1189 		ht_cap_len = sizeof(ieee->pHTInfo->SelfHTCap);
1190 		HTConstructCapabilityElement(ieee, ht_cap_buf, &ht_cap_len,
1191 					     encrypt, true);
1192 		if (ieee->pHTInfo->current_rt2rt_aggregation) {
1193 			realtek_ie_buf = ieee->pHTInfo->sz_rt2rt_agg_buf;
1194 			realtek_ie_len =
1195 				 sizeof(ieee->pHTInfo->sz_rt2rt_agg_buf);
1196 			HTConstructRT2RTAggElement(ieee, realtek_ie_buf,
1197 						   &realtek_ie_len);
1198 		}
1199 	}
1200 
1201 	if (beacon->bCkipSupported)
1202 		ckip_ie_len = 30+2;
1203 	if (beacon->bCcxRmEnable)
1204 		ccxrm_ie_len = 6+2;
1205 	if (beacon->BssCcxVerNumber >= 2)
1206 		cxvernum_ie_len = 5+2;
1207 
1208 	PMKCacheIdx = SecIsInPMKIDList(ieee, ieee->current_network.bssid);
1209 	if (PMKCacheIdx >= 0) {
1210 		wpa_ie_len += 18;
1211 		netdev_info(ieee->dev, "[PMK cache]: WPA2 IE length: %x\n",
1212 			    wpa_ie_len);
1213 	}
1214 	len = sizeof(struct rtllib_assoc_request_frame) + 2
1215 		+ beacon->ssid_len
1216 		+ rate_len
1217 		+ wpa_ie_len
1218 		+ wps_ie_len
1219 		+ wmm_info_len
1220 		+ turbo_info_len
1221 		+ ht_cap_len
1222 		+ realtek_ie_len
1223 		+ ckip_ie_len
1224 		+ ccxrm_ie_len
1225 		+ cxvernum_ie_len
1226 		+ ieee->tx_headroom;
1227 
1228 	skb = dev_alloc_skb(len);
1229 
1230 	if (!skb)
1231 		return NULL;
1232 
1233 	skb_reserve(skb, ieee->tx_headroom);
1234 
1235 	hdr = skb_put(skb, sizeof(struct rtllib_assoc_request_frame) + 2);
1236 
1237 
1238 	hdr->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_ASSOC_REQ);
1239 	hdr->header.duration_id = cpu_to_le16(37);
1240 	ether_addr_copy(hdr->header.addr1, beacon->bssid);
1241 	ether_addr_copy(hdr->header.addr2, ieee->dev->dev_addr);
1242 	ether_addr_copy(hdr->header.addr3, beacon->bssid);
1243 
1244 	ether_addr_copy(ieee->ap_mac_addr, beacon->bssid);
1245 
1246 	hdr->capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
1247 	if (beacon->capability & WLAN_CAPABILITY_PRIVACY)
1248 		hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1249 
1250 	if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1251 		hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
1252 
1253 	if (ieee->short_slot &&
1254 	   (beacon->capability&WLAN_CAPABILITY_SHORT_SLOT_TIME))
1255 		hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
1256 
1257 
1258 	hdr->listen_interval = cpu_to_le16(beacon->listen_interval);
1259 
1260 	hdr->info_element[0].id = MFIE_TYPE_SSID;
1261 
1262 	hdr->info_element[0].len = beacon->ssid_len;
1263 	skb_put_data(skb, beacon->ssid, beacon->ssid_len);
1264 
1265 	tag = skb_put(skb, rate_len);
1266 
1267 	if (beacon->rates_len) {
1268 		*tag++ = MFIE_TYPE_RATES;
1269 		*tag++ = beacon->rates_len;
1270 		for (i = 0; i < beacon->rates_len; i++)
1271 			*tag++ = beacon->rates[i];
1272 	}
1273 
1274 	if (beacon->rates_ex_len) {
1275 		*tag++ = MFIE_TYPE_RATES_EX;
1276 		*tag++ = beacon->rates_ex_len;
1277 		for (i = 0; i < beacon->rates_ex_len; i++)
1278 			*tag++ = beacon->rates_ex[i];
1279 	}
1280 
1281 	if (beacon->bCkipSupported) {
1282 		static const u8 AironetIeOui[] = {0x00, 0x01, 0x66};
1283 		u8	CcxAironetBuf[30];
1284 		struct octet_string osCcxAironetIE;
1285 
1286 		memset(CcxAironetBuf, 0, 30);
1287 		osCcxAironetIE.Octet = CcxAironetBuf;
1288 		osCcxAironetIE.Length = sizeof(CcxAironetBuf);
1289 		memcpy(osCcxAironetIE.Octet, AironetIeOui,
1290 		       sizeof(AironetIeOui));
1291 
1292 		osCcxAironetIE.Octet[IE_CISCO_FLAG_POSITION] |=
1293 					 (SUPPORT_CKIP_PK|SUPPORT_CKIP_MIC);
1294 		tag = skb_put(skb, ckip_ie_len);
1295 		*tag++ = MFIE_TYPE_AIRONET;
1296 		*tag++ = osCcxAironetIE.Length;
1297 		memcpy(tag, osCcxAironetIE.Octet, osCcxAironetIE.Length);
1298 		tag += osCcxAironetIE.Length;
1299 	}
1300 
1301 	if (beacon->bCcxRmEnable) {
1302 		static const u8 CcxRmCapBuf[] = {0x00, 0x40, 0x96, 0x01, 0x01,
1303 			0x00};
1304 		struct octet_string osCcxRmCap;
1305 
1306 		osCcxRmCap.Octet = (u8 *)CcxRmCapBuf;
1307 		osCcxRmCap.Length = sizeof(CcxRmCapBuf);
1308 		tag = skb_put(skb, ccxrm_ie_len);
1309 		*tag++ = MFIE_TYPE_GENERIC;
1310 		*tag++ = osCcxRmCap.Length;
1311 		memcpy(tag, osCcxRmCap.Octet, osCcxRmCap.Length);
1312 		tag += osCcxRmCap.Length;
1313 	}
1314 
1315 	if (beacon->BssCcxVerNumber >= 2) {
1316 		u8 CcxVerNumBuf[] = {0x00, 0x40, 0x96, 0x03, 0x00};
1317 		struct octet_string osCcxVerNum;
1318 
1319 		CcxVerNumBuf[4] = beacon->BssCcxVerNumber;
1320 		osCcxVerNum.Octet = CcxVerNumBuf;
1321 		osCcxVerNum.Length = sizeof(CcxVerNumBuf);
1322 		tag = skb_put(skb, cxvernum_ie_len);
1323 		*tag++ = MFIE_TYPE_GENERIC;
1324 		*tag++ = osCcxVerNum.Length;
1325 		memcpy(tag, osCcxVerNum.Octet, osCcxVerNum.Length);
1326 		tag += osCcxVerNum.Length;
1327 	}
1328 	if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1329 		if (ieee->pHTInfo->ePeerHTSpecVer != HT_SPEC_VER_EWC) {
1330 			tag = skb_put(skb, ht_cap_len);
1331 			*tag++ = MFIE_TYPE_HT_CAP;
1332 			*tag++ = ht_cap_len - 2;
1333 			memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1334 			tag += ht_cap_len - 2;
1335 		}
1336 	}
1337 
1338 	if (wpa_ie_len) {
1339 		skb_put_data(skb, ieee->wpa_ie, ieee->wpa_ie_len);
1340 
1341 		if (PMKCacheIdx >= 0) {
1342 			tag = skb_put(skb, 18);
1343 			*tag = 1;
1344 			*(tag + 1) = 0;
1345 			memcpy((tag + 2), &ieee->PMKIDList[PMKCacheIdx].PMKID,
1346 			       16);
1347 		}
1348 	}
1349 	if (wmm_info_len) {
1350 		tag = skb_put(skb, wmm_info_len);
1351 		rtllib_WMM_Info(ieee, &tag);
1352 	}
1353 
1354 	if (wps_ie_len && ieee->wps_ie)
1355 		skb_put_data(skb, ieee->wps_ie, wps_ie_len);
1356 
1357 	if (turbo_info_len) {
1358 		tag = skb_put(skb, turbo_info_len);
1359 		rtllib_TURBO_Info(ieee, &tag);
1360 	}
1361 
1362 	if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1363 		if (ieee->pHTInfo->ePeerHTSpecVer == HT_SPEC_VER_EWC) {
1364 			tag = skb_put(skb, ht_cap_len);
1365 			*tag++ = MFIE_TYPE_GENERIC;
1366 			*tag++ = ht_cap_len - 2;
1367 			memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1368 			tag += ht_cap_len - 2;
1369 		}
1370 
1371 		if (ieee->pHTInfo->current_rt2rt_aggregation) {
1372 			tag = skb_put(skb, realtek_ie_len);
1373 			*tag++ = MFIE_TYPE_GENERIC;
1374 			*tag++ = realtek_ie_len - 2;
1375 			memcpy(tag, realtek_ie_buf, realtek_ie_len - 2);
1376 		}
1377 	}
1378 
1379 	kfree(ieee->assocreq_ies);
1380 	ieee->assocreq_ies = NULL;
1381 	ies = &(hdr->info_element[0].id);
1382 	ieee->assocreq_ies_len = (skb->data + skb->len) - ies;
1383 	ieee->assocreq_ies = kmemdup(ies, ieee->assocreq_ies_len, GFP_ATOMIC);
1384 	if (!ieee->assocreq_ies)
1385 		ieee->assocreq_ies_len = 0;
1386 
1387 	return skb;
1388 }
1389 
1390 static void rtllib_associate_abort(struct rtllib_device *ieee)
1391 {
1392 	unsigned long flags;
1393 
1394 	spin_lock_irqsave(&ieee->lock, flags);
1395 
1396 	ieee->associate_seq++;
1397 
1398 	/* don't scan, and avoid to have the RX path possibily
1399 	 * try again to associate. Even do not react to AUTH or
1400 	 * ASSOC response. Just wait for the retry wq to be scheduled.
1401 	 * Here we will check if there are good nets to associate
1402 	 * with, so we retry or just get back to NO_LINK and scanning
1403 	 */
1404 	if (ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATING) {
1405 		netdev_dbg(ieee->dev, "Authentication failed\n");
1406 		ieee->softmac_stats.no_auth_rs++;
1407 	} else {
1408 		netdev_dbg(ieee->dev, "Association failed\n");
1409 		ieee->softmac_stats.no_ass_rs++;
1410 	}
1411 
1412 	ieee->state = RTLLIB_ASSOCIATING_RETRY;
1413 
1414 	schedule_delayed_work(&ieee->associate_retry_wq,
1415 			      RTLLIB_SOFTMAC_ASSOC_RETRY_TIME);
1416 
1417 	spin_unlock_irqrestore(&ieee->lock, flags);
1418 }
1419 
1420 static void rtllib_associate_abort_cb(struct timer_list *t)
1421 {
1422 	struct rtllib_device *dev = from_timer(dev, t, associate_timer);
1423 
1424 	rtllib_associate_abort(dev);
1425 }
1426 
1427 static void rtllib_associate_step1(struct rtllib_device *ieee, u8 *daddr)
1428 {
1429 	struct rtllib_network *beacon = &ieee->current_network;
1430 	struct sk_buff *skb;
1431 
1432 	netdev_dbg(ieee->dev, "Stopping scan\n");
1433 
1434 	ieee->softmac_stats.tx_auth_rq++;
1435 
1436 	skb = rtllib_authentication_req(beacon, ieee, 0, daddr);
1437 
1438 	if (!skb)
1439 		rtllib_associate_abort(ieee);
1440 	else {
1441 		ieee->state = RTLLIB_ASSOCIATING_AUTHENTICATING;
1442 		netdev_dbg(ieee->dev, "Sending authentication request\n");
1443 		softmac_mgmt_xmit(skb, ieee);
1444 		if (!timer_pending(&ieee->associate_timer)) {
1445 			ieee->associate_timer.expires = jiffies + (HZ / 2);
1446 			add_timer(&ieee->associate_timer);
1447 		}
1448 	}
1449 }
1450 
1451 static void rtllib_auth_challenge(struct rtllib_device *ieee, u8 *challenge,
1452 				  int chlen)
1453 {
1454 	u8 *c;
1455 	struct sk_buff *skb;
1456 	struct rtllib_network *beacon = &ieee->current_network;
1457 
1458 	ieee->associate_seq++;
1459 	ieee->softmac_stats.tx_auth_rq++;
1460 
1461 	skb = rtllib_authentication_req(beacon, ieee, chlen + 2, beacon->bssid);
1462 
1463 	if (!skb)
1464 		rtllib_associate_abort(ieee);
1465 	else {
1466 		c = skb_put(skb, chlen+2);
1467 		*(c++) = MFIE_TYPE_CHALLENGE;
1468 		*(c++) = chlen;
1469 		memcpy(c, challenge, chlen);
1470 
1471 		netdev_dbg(ieee->dev,
1472 			   "Sending authentication challenge response\n");
1473 
1474 		rtllib_encrypt_fragment(ieee, skb,
1475 					sizeof(struct rtllib_hdr_3addr));
1476 
1477 		softmac_mgmt_xmit(skb, ieee);
1478 		mod_timer(&ieee->associate_timer, jiffies + (HZ/2));
1479 	}
1480 	kfree(challenge);
1481 }
1482 
1483 static void rtllib_associate_step2(struct rtllib_device *ieee)
1484 {
1485 	struct sk_buff *skb;
1486 	struct rtllib_network *beacon = &ieee->current_network;
1487 
1488 	del_timer_sync(&ieee->associate_timer);
1489 
1490 	netdev_dbg(ieee->dev, "Sending association request\n");
1491 
1492 	ieee->softmac_stats.tx_ass_rq++;
1493 	skb = rtllib_association_req(beacon, ieee);
1494 	if (!skb)
1495 		rtllib_associate_abort(ieee);
1496 	else {
1497 		softmac_mgmt_xmit(skb, ieee);
1498 		mod_timer(&ieee->associate_timer, jiffies + (HZ/2));
1499 	}
1500 }
1501 
1502 static void rtllib_associate_complete_wq(void *data)
1503 {
1504 	struct rtllib_device *ieee = (struct rtllib_device *)
1505 				     container_of_work_rsl(data,
1506 				     struct rtllib_device,
1507 				     associate_complete_wq);
1508 	struct rt_pwr_save_ctrl *pPSC = &(ieee->PowerSaveControl);
1509 
1510 	netdev_info(ieee->dev, "Associated successfully with %pM\n",
1511 		    ieee->current_network.bssid);
1512 	if (!ieee->is_silent_reset) {
1513 		netdev_info(ieee->dev, "normal associate\n");
1514 		notify_wx_assoc_event(ieee);
1515 	}
1516 
1517 	netif_carrier_on(ieee->dev);
1518 	ieee->is_roaming = false;
1519 	if (rtllib_is_54g(&ieee->current_network) &&
1520 	   (ieee->modulation & RTLLIB_OFDM_MODULATION)) {
1521 		ieee->rate = 108;
1522 		netdev_info(ieee->dev, "Using G rates:%d\n", ieee->rate);
1523 	} else {
1524 		ieee->rate = 22;
1525 		ieee->SetWirelessMode(ieee->dev, IEEE_B);
1526 		netdev_info(ieee->dev, "Using B rates:%d\n", ieee->rate);
1527 	}
1528 	if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1529 		netdev_info(ieee->dev, "Successfully associated, ht enabled\n");
1530 		HTOnAssocRsp(ieee);
1531 	} else {
1532 		netdev_info(ieee->dev,
1533 			    "Successfully associated, ht not enabled(%d, %d)\n",
1534 			    ieee->pHTInfo->bCurrentHTSupport,
1535 			    ieee->pHTInfo->bEnableHT);
1536 		memset(ieee->dot11HTOperationalRateSet, 0, 16);
1537 	}
1538 	ieee->LinkDetectInfo.SlotNum = 2 * (1 +
1539 				       ieee->current_network.beacon_interval /
1540 				       500);
1541 	if (ieee->LinkDetectInfo.NumRecvBcnInPeriod == 0 ||
1542 	    ieee->LinkDetectInfo.NumRecvDataInPeriod == 0) {
1543 		ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
1544 		ieee->LinkDetectInfo.NumRecvDataInPeriod = 1;
1545 	}
1546 	pPSC->LpsIdleCount = 0;
1547 	ieee->link_change(ieee->dev);
1548 
1549 	if (ieee->is_silent_reset) {
1550 		netdev_info(ieee->dev, "silent reset associate\n");
1551 		ieee->is_silent_reset = false;
1552 	}
1553 
1554 	if (ieee->data_hard_resume)
1555 		ieee->data_hard_resume(ieee->dev);
1556 
1557 }
1558 
1559 static void rtllib_sta_send_associnfo(struct rtllib_device *ieee)
1560 {
1561 }
1562 
1563 static void rtllib_associate_complete(struct rtllib_device *ieee)
1564 {
1565 	del_timer_sync(&ieee->associate_timer);
1566 
1567 	ieee->state = RTLLIB_LINKED;
1568 	rtllib_sta_send_associnfo(ieee);
1569 
1570 	schedule_work(&ieee->associate_complete_wq);
1571 }
1572 
1573 static void rtllib_associate_procedure_wq(void *data)
1574 {
1575 	struct rtllib_device *ieee = container_of_dwork_rsl(data,
1576 				     struct rtllib_device,
1577 				     associate_procedure_wq);
1578 	rtllib_stop_scan_syncro(ieee);
1579 	if (ieee->rtllib_ips_leave != NULL)
1580 		ieee->rtllib_ips_leave(ieee->dev);
1581 	mutex_lock(&ieee->wx_mutex);
1582 
1583 	if (ieee->data_hard_stop)
1584 		ieee->data_hard_stop(ieee->dev);
1585 
1586 	rtllib_stop_scan(ieee);
1587 	HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
1588 	if (ieee->rf_power_state == rf_off) {
1589 		if (ieee->rtllib_ips_leave_wq != NULL)
1590 			ieee->rtllib_ips_leave_wq(ieee->dev);
1591 		mutex_unlock(&ieee->wx_mutex);
1592 		return;
1593 	}
1594 	ieee->associate_seq = 1;
1595 
1596 	rtllib_associate_step1(ieee, ieee->current_network.bssid);
1597 
1598 	mutex_unlock(&ieee->wx_mutex);
1599 }
1600 
1601 inline void rtllib_softmac_new_net(struct rtllib_device *ieee,
1602 				   struct rtllib_network *net)
1603 {
1604 	u8 tmp_ssid[IW_ESSID_MAX_SIZE + 1];
1605 	int tmp_ssid_len = 0;
1606 
1607 	short apset, ssidset, ssidbroad, apmatch, ssidmatch;
1608 
1609 	/* we are interested in new only if we are not associated
1610 	 * and we are not associating / authenticating
1611 	 */
1612 	if (ieee->state != RTLLIB_NOLINK)
1613 		return;
1614 
1615 	if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability &
1616 	    WLAN_CAPABILITY_ESS))
1617 		return;
1618 
1619 	if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability &
1620 	     WLAN_CAPABILITY_IBSS))
1621 		return;
1622 
1623 	if ((ieee->iw_mode == IW_MODE_ADHOC) &&
1624 	    (net->channel > ieee->ibss_maxjoin_chal))
1625 		return;
1626 	if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC) {
1627 		/* if the user specified the AP MAC, we need also the essid
1628 		 * This could be obtained by beacons or, if the network does not
1629 		 * broadcast it, it can be put manually.
1630 		 */
1631 		apset = ieee->wap_set;
1632 		ssidset = ieee->ssid_set;
1633 		ssidbroad =  !(net->ssid_len == 0 || net->ssid[0] == '\0');
1634 		apmatch = (memcmp(ieee->current_network.bssid, net->bssid,
1635 				  ETH_ALEN) == 0);
1636 		if (!ssidbroad) {
1637 			ssidmatch = (ieee->current_network.ssid_len ==
1638 				    net->hidden_ssid_len) &&
1639 				    (!strncmp(ieee->current_network.ssid,
1640 				    net->hidden_ssid, net->hidden_ssid_len));
1641 			if (net->hidden_ssid_len > 0) {
1642 				strncpy(net->ssid, net->hidden_ssid,
1643 					net->hidden_ssid_len);
1644 				net->ssid_len = net->hidden_ssid_len;
1645 				ssidbroad = 1;
1646 			}
1647 		} else
1648 			ssidmatch =
1649 			   (ieee->current_network.ssid_len == net->ssid_len) &&
1650 			   (!strncmp(ieee->current_network.ssid, net->ssid,
1651 			   net->ssid_len));
1652 
1653 		/* if the user set the AP check if match.
1654 		 * if the network does not broadcast essid we check the
1655 		 *	 user supplied ANY essid
1656 		 * if the network does broadcast and the user does not set
1657 		 *	 essid it is OK
1658 		 * if the network does broadcast and the user did set essid
1659 		 * check if essid match
1660 		 * if the ap is not set, check that the user set the bssid
1661 		 * and the network does broadcast and that those two bssid match
1662 		 */
1663 		if ((apset && apmatch &&
1664 		   ((ssidset && ssidbroad && ssidmatch) ||
1665 		   (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1666 		   (!apset && ssidset && ssidbroad && ssidmatch) ||
1667 		   (ieee->is_roaming && ssidset && ssidbroad && ssidmatch)) {
1668 			/* Save the essid so that if it is hidden, it is
1669 			 * replaced with the essid provided by the user.
1670 			 */
1671 			if (!ssidbroad) {
1672 				memcpy(tmp_ssid, ieee->current_network.ssid,
1673 				       ieee->current_network.ssid_len);
1674 				tmp_ssid_len = ieee->current_network.ssid_len;
1675 			}
1676 			memcpy(&ieee->current_network, net,
1677 				sizeof(ieee->current_network));
1678 			if (!ssidbroad) {
1679 				memcpy(ieee->current_network.ssid, tmp_ssid,
1680 				       tmp_ssid_len);
1681 				ieee->current_network.ssid_len = tmp_ssid_len;
1682 			}
1683 			netdev_info(ieee->dev,
1684 				    "Linking with %s,channel:%d, qos:%d, myHT:%d, networkHT:%d, mode:%x cur_net.flags:0x%x\n",
1685 				    ieee->current_network.ssid,
1686 				    ieee->current_network.channel,
1687 				    ieee->current_network.qos_data.supported,
1688 				    ieee->pHTInfo->bEnableHT,
1689 				    ieee->current_network.bssht.bd_support_ht,
1690 				    ieee->current_network.mode,
1691 				    ieee->current_network.flags);
1692 
1693 			if ((rtllib_act_scanning(ieee, false)) &&
1694 			   !(ieee->softmac_features & IEEE_SOFTMAC_SCAN))
1695 				rtllib_stop_scan_syncro(ieee);
1696 
1697 			HTResetIOTSetting(ieee->pHTInfo);
1698 			ieee->wmm_acm = 0;
1699 			if (ieee->iw_mode == IW_MODE_INFRA) {
1700 				/* Join the network for the first time */
1701 				ieee->AsocRetryCount = 0;
1702 				if ((ieee->current_network.qos_data.supported == 1) &&
1703 				    ieee->current_network.bssht.bd_support_ht)
1704 					HTResetSelfAndSavePeerSetting(ieee,
1705 						 &(ieee->current_network));
1706 				else
1707 					ieee->pHTInfo->bCurrentHTSupport =
1708 								 false;
1709 
1710 				ieee->state = RTLLIB_ASSOCIATING;
1711 				if (ieee->LedControlHandler != NULL)
1712 					ieee->LedControlHandler(ieee->dev,
1713 							 LED_CTL_START_TO_LINK);
1714 				schedule_delayed_work(
1715 					   &ieee->associate_procedure_wq, 0);
1716 			} else {
1717 				if (rtllib_is_54g(&ieee->current_network) &&
1718 				    (ieee->modulation &
1719 				     RTLLIB_OFDM_MODULATION)) {
1720 					ieee->rate = 108;
1721 					ieee->SetWirelessMode(ieee->dev,
1722 							      IEEE_G);
1723 					netdev_info(ieee->dev,
1724 						    "Using G rates\n");
1725 				} else {
1726 					ieee->rate = 22;
1727 					ieee->SetWirelessMode(ieee->dev,
1728 							      IEEE_B);
1729 					netdev_info(ieee->dev,
1730 						    "Using B rates\n");
1731 				}
1732 				memset(ieee->dot11HTOperationalRateSet, 0, 16);
1733 				ieee->state = RTLLIB_LINKED;
1734 			}
1735 		}
1736 	}
1737 }
1738 
1739 static void rtllib_softmac_check_all_nets(struct rtllib_device *ieee)
1740 {
1741 	unsigned long flags;
1742 	struct rtllib_network *target;
1743 
1744 	spin_lock_irqsave(&ieee->lock, flags);
1745 
1746 	list_for_each_entry(target, &ieee->network_list, list) {
1747 
1748 		/* if the state become different that NOLINK means
1749 		 * we had found what we are searching for
1750 		 */
1751 
1752 		if (ieee->state != RTLLIB_NOLINK)
1753 			break;
1754 
1755 		if (ieee->scan_age == 0 || time_after(target->last_scanned +
1756 		    ieee->scan_age, jiffies))
1757 			rtllib_softmac_new_net(ieee, target);
1758 	}
1759 	spin_unlock_irqrestore(&ieee->lock, flags);
1760 }
1761 
1762 static inline int auth_parse(struct net_device *dev, struct sk_buff *skb,
1763 			     u8 **challenge, int *chlen)
1764 {
1765 	struct rtllib_authentication *a;
1766 	u8 *t;
1767 
1768 	if (skb->len <  (sizeof(struct rtllib_authentication) -
1769 	    sizeof(struct rtllib_info_element))) {
1770 		netdev_dbg(dev, "invalid len in auth resp: %d\n", skb->len);
1771 		return -EINVAL;
1772 	}
1773 	*challenge = NULL;
1774 	a = (struct rtllib_authentication *)skb->data;
1775 	if (skb->len > (sizeof(struct rtllib_authentication) + 3)) {
1776 		t = skb->data + sizeof(struct rtllib_authentication);
1777 
1778 		if (*(t++) == MFIE_TYPE_CHALLENGE) {
1779 			*chlen = *(t++);
1780 			*challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1781 			if (!*challenge)
1782 				return -ENOMEM;
1783 		}
1784 	}
1785 
1786 	if (a->status) {
1787 		netdev_dbg(dev, "auth_parse() failed\n");
1788 		return -EINVAL;
1789 	}
1790 
1791 	return 0;
1792 }
1793 
1794 static int auth_rq_parse(struct net_device *dev, struct sk_buff *skb, u8 *dest)
1795 {
1796 	struct rtllib_authentication *a;
1797 
1798 	if (skb->len <  (sizeof(struct rtllib_authentication) -
1799 	    sizeof(struct rtllib_info_element))) {
1800 		netdev_dbg(dev, "invalid len in auth request: %d\n", skb->len);
1801 		return -1;
1802 	}
1803 	a = (struct rtllib_authentication *)skb->data;
1804 
1805 	ether_addr_copy(dest, a->header.addr2);
1806 
1807 	if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
1808 		return  WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1809 
1810 	return WLAN_STATUS_SUCCESS;
1811 }
1812 
1813 static short probe_rq_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1814 			    u8 *src)
1815 {
1816 	u8 *tag;
1817 	u8 *skbend;
1818 	u8 *ssid = NULL;
1819 	u8 ssidlen = 0;
1820 	struct rtllib_hdr_3addr   *header =
1821 		(struct rtllib_hdr_3addr   *)skb->data;
1822 	bool bssid_match;
1823 
1824 	if (skb->len < sizeof(struct rtllib_hdr_3addr))
1825 		return -1; /* corrupted */
1826 
1827 	bssid_match =
1828 	  (!ether_addr_equal(header->addr3, ieee->current_network.bssid)) &&
1829 	  (!is_broadcast_ether_addr(header->addr3));
1830 	if (bssid_match)
1831 		return -1;
1832 
1833 	ether_addr_copy(src, header->addr2);
1834 
1835 	skbend = (u8 *)skb->data + skb->len;
1836 
1837 	tag = skb->data + sizeof(struct rtllib_hdr_3addr);
1838 
1839 	while (tag + 1 < skbend) {
1840 		if (*tag == 0) {
1841 			ssid = tag + 2;
1842 			ssidlen = *(tag + 1);
1843 			break;
1844 		}
1845 		tag++; /* point to the len field */
1846 		tag = tag + *(tag); /* point to the last data byte of the tag */
1847 		tag++; /* point to the next tag */
1848 	}
1849 
1850 	if (ssidlen == 0)
1851 		return 1;
1852 
1853 	if (!ssid)
1854 		return 1; /* ssid not found in tagged param */
1855 
1856 	return !strncmp(ssid, ieee->current_network.ssid, ssidlen);
1857 }
1858 
1859 static int assoc_rq_parse(struct net_device *dev, struct sk_buff *skb, u8 *dest)
1860 {
1861 	struct rtllib_assoc_request_frame *a;
1862 
1863 	if (skb->len < (sizeof(struct rtllib_assoc_request_frame) -
1864 		sizeof(struct rtllib_info_element))) {
1865 		netdev_dbg(dev, "invalid len in auth request:%d\n", skb->len);
1866 		return -1;
1867 	}
1868 
1869 	a = (struct rtllib_assoc_request_frame *)skb->data;
1870 
1871 	ether_addr_copy(dest, a->header.addr2);
1872 
1873 	return 0;
1874 }
1875 
1876 static inline u16 assoc_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1877 			      int *aid)
1878 {
1879 	struct rtllib_assoc_response_frame *response_head;
1880 	u16 status_code;
1881 
1882 	if (skb->len <  sizeof(struct rtllib_assoc_response_frame)) {
1883 		netdev_dbg(ieee->dev, "Invalid len in auth resp: %d\n",
1884 			   skb->len);
1885 		return 0xcafe;
1886 	}
1887 
1888 	response_head = (struct rtllib_assoc_response_frame *)skb->data;
1889 	*aid = le16_to_cpu(response_head->aid) & 0x3fff;
1890 
1891 	status_code = le16_to_cpu(response_head->status);
1892 	if ((status_code == WLAN_STATUS_ASSOC_DENIED_RATES ||
1893 	   status_code == WLAN_STATUS_CAPS_UNSUPPORTED) &&
1894 	   ((ieee->mode == IEEE_G) &&
1895 	   (ieee->current_network.mode == IEEE_N_24G) &&
1896 	   (ieee->AsocRetryCount++ < (RT_ASOC_RETRY_LIMIT-1)))) {
1897 		ieee->pHTInfo->iot_action |= HT_IOT_ACT_PURE_N_MODE;
1898 	} else {
1899 		ieee->AsocRetryCount = 0;
1900 	}
1901 
1902 	return le16_to_cpu(response_head->status);
1903 }
1904 
1905 void rtllib_rx_probe_rq(struct rtllib_device *ieee, struct sk_buff *skb)
1906 {
1907 	u8 dest[ETH_ALEN];
1908 
1909 	ieee->softmac_stats.rx_probe_rq++;
1910 	if (probe_rq_parse(ieee, skb, dest) > 0) {
1911 		ieee->softmac_stats.tx_probe_rs++;
1912 		rtllib_resp_to_probe(ieee, dest);
1913 	}
1914 }
1915 
1916 static inline void rtllib_rx_auth_rq(struct rtllib_device *ieee,
1917 				     struct sk_buff *skb)
1918 {
1919 	u8 dest[ETH_ALEN];
1920 	int status;
1921 
1922 	ieee->softmac_stats.rx_auth_rq++;
1923 
1924 	status = auth_rq_parse(ieee->dev, skb, dest);
1925 	if (status != -1)
1926 		rtllib_resp_to_auth(ieee, status, dest);
1927 }
1928 
1929 static inline void rtllib_rx_assoc_rq(struct rtllib_device *ieee,
1930 				      struct sk_buff *skb)
1931 {
1932 	u8 dest[ETH_ALEN];
1933 
1934 
1935 	ieee->softmac_stats.rx_ass_rq++;
1936 	if (assoc_rq_parse(ieee->dev, skb, dest) != -1)
1937 		rtllib_resp_to_assoc_rq(ieee, dest);
1938 
1939 	netdev_info(ieee->dev, "New client associated: %pM\n", dest);
1940 }
1941 
1942 void rtllib_sta_ps_send_null_frame(struct rtllib_device *ieee, short pwr)
1943 {
1944 
1945 	struct sk_buff *buf = rtllib_null_func(ieee, pwr);
1946 
1947 	if (buf)
1948 		softmac_ps_mgmt_xmit(buf, ieee);
1949 }
1950 EXPORT_SYMBOL(rtllib_sta_ps_send_null_frame);
1951 
1952 void rtllib_sta_ps_send_pspoll_frame(struct rtllib_device *ieee)
1953 {
1954 	struct sk_buff *buf = rtllib_pspoll_func(ieee);
1955 
1956 	if (buf)
1957 		softmac_ps_mgmt_xmit(buf, ieee);
1958 }
1959 
1960 static short rtllib_sta_ps_sleep(struct rtllib_device *ieee, u64 *time)
1961 {
1962 	int timeout;
1963 	u8 dtim;
1964 	struct rt_pwr_save_ctrl *pPSC = &(ieee->PowerSaveControl);
1965 
1966 	if (ieee->LPSDelayCnt) {
1967 		ieee->LPSDelayCnt--;
1968 		return 0;
1969 	}
1970 
1971 	dtim = ieee->current_network.dtim_data;
1972 	if (!(dtim & RTLLIB_DTIM_VALID))
1973 		return 0;
1974 	timeout = ieee->current_network.beacon_interval;
1975 	ieee->current_network.dtim_data = RTLLIB_DTIM_INVALID;
1976 	/* there's no need to nofity AP that I find you buffered
1977 	 * with broadcast packet
1978 	 */
1979 	if (dtim & (RTLLIB_DTIM_UCAST & ieee->ps))
1980 		return 2;
1981 
1982 	if (!time_after(jiffies,
1983 			dev_trans_start(ieee->dev) + msecs_to_jiffies(timeout)))
1984 		return 0;
1985 	if (!time_after(jiffies,
1986 			ieee->last_rx_ps_time + msecs_to_jiffies(timeout)))
1987 		return 0;
1988 	if ((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) &&
1989 	    (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1990 		return 0;
1991 
1992 	if (time) {
1993 		if (ieee->bAwakePktSent) {
1994 			pPSC->LPSAwakeIntvl = 1;
1995 		} else {
1996 			u8 MaxPeriod = 1;
1997 
1998 			if (pPSC->LPSAwakeIntvl == 0)
1999 				pPSC->LPSAwakeIntvl = 1;
2000 			if (pPSC->RegMaxLPSAwakeIntvl == 0)
2001 				MaxPeriod = 1;
2002 			else if (pPSC->RegMaxLPSAwakeIntvl == 0xFF)
2003 				MaxPeriod = ieee->current_network.dtim_period;
2004 			else
2005 				MaxPeriod = pPSC->RegMaxLPSAwakeIntvl;
2006 			pPSC->LPSAwakeIntvl = (pPSC->LPSAwakeIntvl >=
2007 					       MaxPeriod) ? MaxPeriod :
2008 					       (pPSC->LPSAwakeIntvl + 1);
2009 		}
2010 		{
2011 			u8 LPSAwakeIntvl_tmp = 0;
2012 			u8 period = ieee->current_network.dtim_period;
2013 			u8 count = ieee->current_network.tim.tim_count;
2014 
2015 			if (count == 0) {
2016 				if (pPSC->LPSAwakeIntvl > period)
2017 					LPSAwakeIntvl_tmp = period +
2018 						 (pPSC->LPSAwakeIntvl -
2019 						 period) -
2020 						 ((pPSC->LPSAwakeIntvl-period) %
2021 						 period);
2022 				else
2023 					LPSAwakeIntvl_tmp = pPSC->LPSAwakeIntvl;
2024 
2025 			} else {
2026 				if (pPSC->LPSAwakeIntvl >
2027 				    ieee->current_network.tim.tim_count)
2028 					LPSAwakeIntvl_tmp = count +
2029 					(pPSC->LPSAwakeIntvl - count) -
2030 					((pPSC->LPSAwakeIntvl-count)%period);
2031 				else
2032 					LPSAwakeIntvl_tmp = pPSC->LPSAwakeIntvl;
2033 			}
2034 
2035 		*time = ieee->current_network.last_dtim_sta_time
2036 			+ msecs_to_jiffies(ieee->current_network.beacon_interval *
2037 			LPSAwakeIntvl_tmp);
2038 	}
2039 	}
2040 
2041 	return 1;
2042 
2043 
2044 }
2045 
2046 static inline void rtllib_sta_ps(struct work_struct *work)
2047 {
2048 	struct rtllib_device *ieee;
2049 	u64 time;
2050 	short sleep;
2051 	unsigned long flags, flags2;
2052 
2053 	ieee = container_of(work, struct rtllib_device, ps_task);
2054 
2055 	spin_lock_irqsave(&ieee->lock, flags);
2056 
2057 	if ((ieee->ps == RTLLIB_PS_DISABLED ||
2058 	     ieee->iw_mode != IW_MODE_INFRA ||
2059 	     ieee->state != RTLLIB_LINKED)) {
2060 		spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2061 		rtllib_sta_wakeup(ieee, 1);
2062 
2063 		spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2064 	}
2065 	sleep = rtllib_sta_ps_sleep(ieee, &time);
2066 	/* 2 wake, 1 sleep, 0 do nothing */
2067 	if (sleep == 0)
2068 		goto out;
2069 	if (sleep == 1) {
2070 		if (ieee->sta_sleep == LPS_IS_SLEEP) {
2071 			ieee->enter_sleep_state(ieee->dev, time);
2072 		} else if (ieee->sta_sleep == LPS_IS_WAKE) {
2073 			spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2074 
2075 			if (ieee->ps_is_queue_empty(ieee->dev)) {
2076 				ieee->sta_sleep = LPS_WAIT_NULL_DATA_SEND;
2077 				ieee->ack_tx_to_ieee = 1;
2078 				rtllib_sta_ps_send_null_frame(ieee, 1);
2079 				ieee->ps_time = time;
2080 			}
2081 			spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2082 
2083 		}
2084 
2085 		ieee->bAwakePktSent = false;
2086 
2087 	} else if (sleep == 2) {
2088 		spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2089 
2090 		rtllib_sta_wakeup(ieee, 1);
2091 
2092 		spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2093 	}
2094 
2095 out:
2096 	spin_unlock_irqrestore(&ieee->lock, flags);
2097 
2098 }
2099 
2100 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl)
2101 {
2102 	if (ieee->sta_sleep == LPS_IS_WAKE) {
2103 		if (nl) {
2104 			if (ieee->pHTInfo->iot_action &
2105 			    HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
2106 				ieee->ack_tx_to_ieee = 1;
2107 				rtllib_sta_ps_send_null_frame(ieee, 0);
2108 			} else {
2109 				ieee->ack_tx_to_ieee = 1;
2110 				rtllib_sta_ps_send_pspoll_frame(ieee);
2111 			}
2112 		}
2113 		return;
2114 
2115 	}
2116 
2117 	if (ieee->sta_sleep == LPS_IS_SLEEP)
2118 		ieee->sta_wake_up(ieee->dev);
2119 	if (nl) {
2120 		if (ieee->pHTInfo->iot_action &
2121 		    HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
2122 			ieee->ack_tx_to_ieee = 1;
2123 			rtllib_sta_ps_send_null_frame(ieee, 0);
2124 		} else {
2125 			ieee->ack_tx_to_ieee = 1;
2126 			ieee->polling = true;
2127 			rtllib_sta_ps_send_pspoll_frame(ieee);
2128 		}
2129 
2130 	} else {
2131 		ieee->sta_sleep = LPS_IS_WAKE;
2132 		ieee->polling = false;
2133 	}
2134 }
2135 
2136 void rtllib_ps_tx_ack(struct rtllib_device *ieee, short success)
2137 {
2138 	unsigned long flags, flags2;
2139 
2140 	spin_lock_irqsave(&ieee->lock, flags);
2141 
2142 	if (ieee->sta_sleep == LPS_WAIT_NULL_DATA_SEND) {
2143 		/* Null frame with PS bit set */
2144 		if (success) {
2145 			ieee->sta_sleep = LPS_IS_SLEEP;
2146 			ieee->enter_sleep_state(ieee->dev, ieee->ps_time);
2147 		}
2148 		/* if the card report not success we can't be sure the AP
2149 		 * has not RXed so we can't assume the AP believe us awake
2150 		 */
2151 	} else {/* 21112005 - tx again null without PS bit if lost */
2152 
2153 		if ((ieee->sta_sleep == LPS_IS_WAKE) && !success) {
2154 			spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2155 			if (ieee->pHTInfo->iot_action &
2156 			    HT_IOT_ACT_NULL_DATA_POWER_SAVING)
2157 				rtllib_sta_ps_send_null_frame(ieee, 0);
2158 			else
2159 				rtllib_sta_ps_send_pspoll_frame(ieee);
2160 			spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2161 		}
2162 	}
2163 	spin_unlock_irqrestore(&ieee->lock, flags);
2164 }
2165 EXPORT_SYMBOL(rtllib_ps_tx_ack);
2166 
2167 static void rtllib_process_action(struct rtllib_device *ieee,
2168 				  struct sk_buff *skb)
2169 {
2170 	struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *)skb->data;
2171 	u8 *act = rtllib_get_payload((struct rtllib_hdr *)header);
2172 	u8 category = 0;
2173 
2174 	if (act == NULL) {
2175 		netdev_warn(ieee->dev,
2176 			    "Error getting payload of action frame\n");
2177 		return;
2178 	}
2179 
2180 	category = *act;
2181 	act++;
2182 	switch (category) {
2183 	case ACT_CAT_BA:
2184 		switch (*act) {
2185 		case ACT_ADDBAREQ:
2186 			rtllib_rx_ADDBAReq(ieee, skb);
2187 			break;
2188 		case ACT_ADDBARSP:
2189 			rtllib_rx_ADDBARsp(ieee, skb);
2190 			break;
2191 		case ACT_DELBA:
2192 			rtllib_rx_DELBA(ieee, skb);
2193 			break;
2194 		}
2195 		break;
2196 	default:
2197 		break;
2198 	}
2199 }
2200 
2201 static inline int
2202 rtllib_rx_assoc_resp(struct rtllib_device *ieee, struct sk_buff *skb,
2203 		     struct rtllib_rx_stats *rx_stats)
2204 {
2205 	u16 errcode;
2206 	int aid;
2207 	u8 *ies;
2208 	struct rtllib_assoc_response_frame *assoc_resp;
2209 	struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *)skb->data;
2210 	u16 frame_ctl = le16_to_cpu(header->frame_ctl);
2211 
2212 	netdev_dbg(ieee->dev, "received [RE]ASSOCIATION RESPONSE (%d)\n",
2213 		   WLAN_FC_GET_STYPE(frame_ctl));
2214 
2215 	if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2216 	     ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATED &&
2217 	     (ieee->iw_mode == IW_MODE_INFRA)) {
2218 		errcode = assoc_parse(ieee, skb, &aid);
2219 		if (!errcode) {
2220 			struct rtllib_network *network =
2221 				 kzalloc(sizeof(struct rtllib_network),
2222 				 GFP_ATOMIC);
2223 
2224 			if (!network)
2225 				return 1;
2226 			ieee->state = RTLLIB_LINKED;
2227 			ieee->assoc_id = aid;
2228 			ieee->softmac_stats.rx_ass_ok++;
2229 			/* station support qos */
2230 			/* Let the register setting default with Legacy station */
2231 			assoc_resp = (struct rtllib_assoc_response_frame *)skb->data;
2232 			if (ieee->current_network.qos_data.supported == 1) {
2233 				if (rtllib_parse_info_param(ieee, assoc_resp->info_element,
2234 							rx_stats->len - sizeof(*assoc_resp),
2235 							network, rx_stats)) {
2236 					kfree(network);
2237 					return 1;
2238 				}
2239 				memcpy(ieee->pHTInfo->PeerHTCapBuf,
2240 				       network->bssht.bd_ht_cap_buf,
2241 				       network->bssht.bd_ht_cap_len);
2242 				memcpy(ieee->pHTInfo->PeerHTInfoBuf,
2243 				       network->bssht.bd_ht_info_buf,
2244 				       network->bssht.bd_ht_info_len);
2245 				if (ieee->handle_assoc_response != NULL)
2246 					ieee->handle_assoc_response(ieee->dev,
2247 						 (struct rtllib_assoc_response_frame *)header,
2248 						 network);
2249 			}
2250 			kfree(network);
2251 
2252 			kfree(ieee->assocresp_ies);
2253 			ieee->assocresp_ies = NULL;
2254 			ies = &(assoc_resp->info_element[0].id);
2255 			ieee->assocresp_ies_len = (skb->data + skb->len) - ies;
2256 			ieee->assocresp_ies = kmemdup(ies,
2257 						      ieee->assocresp_ies_len,
2258 						      GFP_ATOMIC);
2259 			if (!ieee->assocresp_ies)
2260 				ieee->assocresp_ies_len = 0;
2261 
2262 			rtllib_associate_complete(ieee);
2263 		} else {
2264 			/* aid could not been allocated */
2265 			ieee->softmac_stats.rx_ass_err++;
2266 			netdev_info(ieee->dev,
2267 				    "Association response status code 0x%x\n",
2268 				    errcode);
2269 			if (ieee->AsocRetryCount < RT_ASOC_RETRY_LIMIT)
2270 				schedule_delayed_work(
2271 					 &ieee->associate_procedure_wq, 0);
2272 			else
2273 				rtllib_associate_abort(ieee);
2274 		}
2275 	}
2276 	return 0;
2277 }
2278 
2279 static void rtllib_rx_auth_resp(struct rtllib_device *ieee, struct sk_buff *skb)
2280 {
2281 	int errcode;
2282 	u8 *challenge;
2283 	int chlen = 0;
2284 	bool bSupportNmode = true, bHalfSupportNmode = false;
2285 
2286 	errcode = auth_parse(ieee->dev, skb, &challenge, &chlen);
2287 
2288 	if (errcode) {
2289 		ieee->softmac_stats.rx_auth_rs_err++;
2290 		netdev_info(ieee->dev,
2291 			    "Authentication response status code %d", errcode);
2292 		rtllib_associate_abort(ieee);
2293 		return;
2294 	}
2295 
2296 	if (ieee->open_wep || !challenge) {
2297 		ieee->state = RTLLIB_ASSOCIATING_AUTHENTICATED;
2298 		ieee->softmac_stats.rx_auth_rs_ok++;
2299 		if (!(ieee->pHTInfo->iot_action & HT_IOT_ACT_PURE_N_MODE)) {
2300 			if (!ieee->GetNmodeSupportBySecCfg(ieee->dev)) {
2301 				if (IsHTHalfNmodeAPs(ieee)) {
2302 					bSupportNmode = true;
2303 					bHalfSupportNmode = true;
2304 				} else {
2305 					bSupportNmode = false;
2306 					bHalfSupportNmode = false;
2307 				}
2308 			}
2309 		}
2310 		/* Dummy wirless mode setting to avoid encryption issue */
2311 		if (bSupportNmode) {
2312 			ieee->SetWirelessMode(ieee->dev,
2313 					      ieee->current_network.mode);
2314 		} else {
2315 			/*TODO*/
2316 			ieee->SetWirelessMode(ieee->dev, IEEE_G);
2317 		}
2318 
2319 		if ((ieee->current_network.mode == IEEE_N_24G) &&
2320 		    bHalfSupportNmode) {
2321 			netdev_info(ieee->dev, "======>enter half N mode\n");
2322 			ieee->bHalfWirelessN24GMode = true;
2323 		} else {
2324 			ieee->bHalfWirelessN24GMode = false;
2325 		}
2326 		rtllib_associate_step2(ieee);
2327 	} else {
2328 		rtllib_auth_challenge(ieee, challenge,  chlen);
2329 	}
2330 }
2331 
2332 static inline int
2333 rtllib_rx_auth(struct rtllib_device *ieee, struct sk_buff *skb,
2334 	       struct rtllib_rx_stats *rx_stats)
2335 {
2336 
2337 	if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) {
2338 		if (ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATING &&
2339 		    (ieee->iw_mode == IW_MODE_INFRA)) {
2340 			netdev_dbg(ieee->dev,
2341 				   "Received authentication response");
2342 			rtllib_rx_auth_resp(ieee, skb);
2343 		} else if (ieee->iw_mode == IW_MODE_MASTER) {
2344 			rtllib_rx_auth_rq(ieee, skb);
2345 		}
2346 	}
2347 	return 0;
2348 }
2349 
2350 static inline int
2351 rtllib_rx_deauth(struct rtllib_device *ieee, struct sk_buff *skb)
2352 {
2353 	struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *)skb->data;
2354 	u16 frame_ctl;
2355 
2356 	if (memcmp(header->addr3, ieee->current_network.bssid, ETH_ALEN) != 0)
2357 		return 0;
2358 
2359 	/* FIXME for now repeat all the association procedure
2360 	 * both for disassociation and deauthentication
2361 	 */
2362 	if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2363 	    ieee->state == RTLLIB_LINKED &&
2364 	    (ieee->iw_mode == IW_MODE_INFRA)) {
2365 		frame_ctl = le16_to_cpu(header->frame_ctl);
2366 		netdev_info(ieee->dev,
2367 			    "==========>received disassoc/deauth(%x) frame, reason code:%x\n",
2368 			    WLAN_FC_GET_STYPE(frame_ctl),
2369 			    ((struct rtllib_disassoc *)skb->data)->reason);
2370 		ieee->state = RTLLIB_ASSOCIATING;
2371 		ieee->softmac_stats.reassoc++;
2372 		ieee->is_roaming = true;
2373 		ieee->LinkDetectInfo.bBusyTraffic = false;
2374 		rtllib_disassociate(ieee);
2375 		RemovePeerTS(ieee, header->addr2);
2376 		if (ieee->LedControlHandler != NULL)
2377 			ieee->LedControlHandler(ieee->dev,
2378 						LED_CTL_START_TO_LINK);
2379 
2380 		if (!(ieee->rtllib_ap_sec_type(ieee) &
2381 		    (SEC_ALG_CCMP|SEC_ALG_TKIP)))
2382 			schedule_delayed_work(
2383 				       &ieee->associate_procedure_wq, 5);
2384 	}
2385 	return 0;
2386 }
2387 
2388 inline int rtllib_rx_frame_softmac(struct rtllib_device *ieee,
2389 				   struct sk_buff *skb,
2390 				   struct rtllib_rx_stats *rx_stats, u16 type,
2391 				   u16 stype)
2392 {
2393 	struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *)skb->data;
2394 	u16 frame_ctl;
2395 
2396 	if (!ieee->proto_started)
2397 		return 0;
2398 
2399 	frame_ctl = le16_to_cpu(header->frame_ctl);
2400 	switch (WLAN_FC_GET_STYPE(frame_ctl)) {
2401 	case RTLLIB_STYPE_ASSOC_RESP:
2402 	case RTLLIB_STYPE_REASSOC_RESP:
2403 		if (rtllib_rx_assoc_resp(ieee, skb, rx_stats) == 1)
2404 			return 1;
2405 		break;
2406 	case RTLLIB_STYPE_ASSOC_REQ:
2407 	case RTLLIB_STYPE_REASSOC_REQ:
2408 		if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2409 		     ieee->iw_mode == IW_MODE_MASTER)
2410 			rtllib_rx_assoc_rq(ieee, skb);
2411 		break;
2412 	case RTLLIB_STYPE_AUTH:
2413 		rtllib_rx_auth(ieee, skb, rx_stats);
2414 		break;
2415 	case RTLLIB_STYPE_DISASSOC:
2416 	case RTLLIB_STYPE_DEAUTH:
2417 		rtllib_rx_deauth(ieee, skb);
2418 		break;
2419 	case RTLLIB_STYPE_MANAGE_ACT:
2420 		rtllib_process_action(ieee, skb);
2421 		break;
2422 	default:
2423 		return -1;
2424 	}
2425 	return 0;
2426 }
2427 
2428 /* following are for a simpler TX queue management.
2429  * Instead of using netif_[stop/wake]_queue the driver
2430  * will use these two functions (plus a reset one), that
2431  * will internally use the kernel netif_* and takes
2432  * care of the ieee802.11 fragmentation.
2433  * So the driver receives a fragment per time and might
2434  * call the stop function when it wants to not
2435  * have enough room to TX an entire packet.
2436  * This might be useful if each fragment needs it's own
2437  * descriptor, thus just keep a total free memory > than
2438  * the max fragmentation threshold is not enough.. If the
2439  * ieee802.11 stack passed a TXB struct then you need
2440  * to keep N free descriptors where
2441  * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
2442  * In this way you need just one and the 802.11 stack
2443  * will take care of buffering fragments and pass them to
2444  * the driver later, when it wakes the queue.
2445  */
2446 void rtllib_softmac_xmit(struct rtllib_txb *txb, struct rtllib_device *ieee)
2447 {
2448 
2449 	unsigned int queue_index = txb->queue_index;
2450 	unsigned long flags;
2451 	int  i;
2452 	struct cb_desc *tcb_desc = NULL;
2453 	unsigned long queue_len = 0;
2454 
2455 	spin_lock_irqsave(&ieee->lock, flags);
2456 
2457 	/* called with 2nd parm 0, no tx mgmt lock required */
2458 	rtllib_sta_wakeup(ieee, 0);
2459 
2460 	/* update the tx status */
2461 	tcb_desc = (struct cb_desc *)(txb->fragments[0]->cb +
2462 		   MAX_DEV_ADDR_SIZE);
2463 	if (tcb_desc->bMulticast)
2464 		ieee->stats.multicast++;
2465 
2466 	/* if xmit available, just xmit it immediately, else just insert it to
2467 	 * the wait queue
2468 	 */
2469 	for (i = 0; i < txb->nr_frags; i++) {
2470 		queue_len = skb_queue_len(&ieee->skb_waitQ[queue_index]);
2471 		if ((queue_len  != 0) ||
2472 		    (!ieee->check_nic_enough_desc(ieee->dev, queue_index)) ||
2473 		    (ieee->queue_stop)) {
2474 			/* insert the skb packet to the wait queue
2475 			 * as for the completion function, it does not need
2476 			 * to check it any more.
2477 			 */
2478 			if (queue_len < 200)
2479 				skb_queue_tail(&ieee->skb_waitQ[queue_index],
2480 					       txb->fragments[i]);
2481 			else
2482 				kfree_skb(txb->fragments[i]);
2483 		} else {
2484 			ieee->softmac_data_hard_start_xmit(
2485 					txb->fragments[i],
2486 					ieee->dev, ieee->rate);
2487 		}
2488 	}
2489 
2490 	rtllib_txb_free(txb);
2491 
2492 	spin_unlock_irqrestore(&ieee->lock, flags);
2493 
2494 }
2495 
2496 void rtllib_reset_queue(struct rtllib_device *ieee)
2497 {
2498 	unsigned long flags;
2499 
2500 	spin_lock_irqsave(&ieee->lock, flags);
2501 	init_mgmt_queue(ieee);
2502 	if (ieee->tx_pending.txb) {
2503 		rtllib_txb_free(ieee->tx_pending.txb);
2504 		ieee->tx_pending.txb = NULL;
2505 	}
2506 	ieee->queue_stop = 0;
2507 	spin_unlock_irqrestore(&ieee->lock, flags);
2508 
2509 }
2510 EXPORT_SYMBOL(rtllib_reset_queue);
2511 
2512 void rtllib_stop_all_queues(struct rtllib_device *ieee)
2513 {
2514 	unsigned int i;
2515 
2516 	for (i = 0; i < ieee->dev->num_tx_queues; i++)
2517 		txq_trans_cond_update(netdev_get_tx_queue(ieee->dev, i));
2518 
2519 	netif_tx_stop_all_queues(ieee->dev);
2520 }
2521 
2522 void rtllib_wake_all_queues(struct rtllib_device *ieee)
2523 {
2524 	netif_tx_wake_all_queues(ieee->dev);
2525 }
2526 
2527 /* called in user context only */
2528 static void rtllib_start_master_bss(struct rtllib_device *ieee)
2529 {
2530 	ieee->assoc_id = 1;
2531 
2532 	if (ieee->current_network.ssid_len == 0) {
2533 		strncpy(ieee->current_network.ssid,
2534 			RTLLIB_DEFAULT_TX_ESSID,
2535 			IW_ESSID_MAX_SIZE);
2536 
2537 		ieee->current_network.ssid_len =
2538 				 strlen(RTLLIB_DEFAULT_TX_ESSID);
2539 		ieee->ssid_set = 1;
2540 	}
2541 
2542 	ether_addr_copy(ieee->current_network.bssid, ieee->dev->dev_addr);
2543 
2544 	ieee->set_chan(ieee->dev, ieee->current_network.channel);
2545 	ieee->state = RTLLIB_LINKED;
2546 	ieee->link_change(ieee->dev);
2547 	notify_wx_assoc_event(ieee);
2548 
2549 	if (ieee->data_hard_resume)
2550 		ieee->data_hard_resume(ieee->dev);
2551 
2552 	netif_carrier_on(ieee->dev);
2553 }
2554 
2555 static void rtllib_start_monitor_mode(struct rtllib_device *ieee)
2556 {
2557 	/* reset hardware status */
2558 	if (ieee->raw_tx) {
2559 		if (ieee->data_hard_resume)
2560 			ieee->data_hard_resume(ieee->dev);
2561 
2562 		netif_carrier_on(ieee->dev);
2563 	}
2564 }
2565 
2566 static void rtllib_start_ibss_wq(void *data)
2567 {
2568 	struct rtllib_device *ieee = container_of_dwork_rsl(data,
2569 				     struct rtllib_device, start_ibss_wq);
2570 	/* iwconfig mode ad-hoc will schedule this and return
2571 	 * on the other hand this will block further iwconfig SET
2572 	 * operations because of the wx_mutex hold.
2573 	 * Anyway some most set operations set a flag to speed-up
2574 	 * (abort) this wq (when syncro scanning) before sleeping
2575 	 * on the mutex
2576 	 */
2577 	if (!ieee->proto_started) {
2578 		netdev_info(ieee->dev, "==========oh driver down return\n");
2579 		return;
2580 	}
2581 	mutex_lock(&ieee->wx_mutex);
2582 
2583 	if (ieee->current_network.ssid_len == 0) {
2584 		strscpy(ieee->current_network.ssid, RTLLIB_DEFAULT_TX_ESSID,
2585 			sizeof(ieee->current_network.ssid));
2586 		ieee->current_network.ssid_len = strlen(RTLLIB_DEFAULT_TX_ESSID);
2587 		ieee->ssid_set = 1;
2588 	}
2589 
2590 	ieee->state = RTLLIB_NOLINK;
2591 	ieee->mode = IEEE_G;
2592 	/* check if we have this cell in our network list */
2593 	rtllib_softmac_check_all_nets(ieee);
2594 
2595 
2596 	/* if not then the state is not linked. Maybe the user switched to
2597 	 * ad-hoc mode just after being in monitor mode, or just after
2598 	 * being very few time in managed mode (so the card have had no
2599 	 * time to scan all the chans..) or we have just run up the iface
2600 	 * after setting ad-hoc mode. So we have to give another try..
2601 	 * Here, in ibss mode, should be safe to do this without extra care
2602 	 * (in bss mode we had to make sure no-one tried to associate when
2603 	 * we had just checked the ieee->state and we was going to start the
2604 	 * scan) because in ibss mode the rtllib_new_net function, when
2605 	 * finds a good net, just set the ieee->state to RTLLIB_LINKED,
2606 	 * so, at worst, we waste a bit of time to initiate an unneeded syncro
2607 	 * scan, that will stop at the first round because it sees the state
2608 	 * associated.
2609 	 */
2610 	if (ieee->state == RTLLIB_NOLINK)
2611 		rtllib_start_scan_syncro(ieee, 0);
2612 
2613 	/* the network definitively is not here.. create a new cell */
2614 	if (ieee->state == RTLLIB_NOLINK) {
2615 		netdev_info(ieee->dev, "creating new IBSS cell\n");
2616 		ieee->current_network.channel = ieee->bss_start_channel;
2617 		if (!ieee->wap_set)
2618 			eth_random_addr(ieee->current_network.bssid);
2619 
2620 		if (ieee->modulation & RTLLIB_CCK_MODULATION) {
2621 
2622 			ieee->current_network.rates_len = 4;
2623 
2624 			ieee->current_network.rates[0] =
2625 				 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
2626 			ieee->current_network.rates[1] =
2627 				 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
2628 			ieee->current_network.rates[2] =
2629 				 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
2630 			ieee->current_network.rates[3] =
2631 				 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
2632 
2633 		} else
2634 			ieee->current_network.rates_len = 0;
2635 
2636 		if (ieee->modulation & RTLLIB_OFDM_MODULATION) {
2637 			ieee->current_network.rates_ex_len = 8;
2638 
2639 			ieee->current_network.rates_ex[0] =
2640 						 RTLLIB_OFDM_RATE_6MB;
2641 			ieee->current_network.rates_ex[1] =
2642 						 RTLLIB_OFDM_RATE_9MB;
2643 			ieee->current_network.rates_ex[2] =
2644 						 RTLLIB_OFDM_RATE_12MB;
2645 			ieee->current_network.rates_ex[3] =
2646 						 RTLLIB_OFDM_RATE_18MB;
2647 			ieee->current_network.rates_ex[4] =
2648 						 RTLLIB_OFDM_RATE_24MB;
2649 			ieee->current_network.rates_ex[5] =
2650 						 RTLLIB_OFDM_RATE_36MB;
2651 			ieee->current_network.rates_ex[6] =
2652 						 RTLLIB_OFDM_RATE_48MB;
2653 			ieee->current_network.rates_ex[7] =
2654 						 RTLLIB_OFDM_RATE_54MB;
2655 
2656 			ieee->rate = 108;
2657 		} else {
2658 			ieee->current_network.rates_ex_len = 0;
2659 			ieee->rate = 22;
2660 		}
2661 
2662 		ieee->current_network.qos_data.supported = 0;
2663 		ieee->SetWirelessMode(ieee->dev, IEEE_G);
2664 		ieee->current_network.mode = ieee->mode;
2665 		ieee->current_network.atim_window = 0;
2666 		ieee->current_network.capability = WLAN_CAPABILITY_IBSS;
2667 	}
2668 
2669 	netdev_info(ieee->dev, "%s(): ieee->mode = %d\n", __func__, ieee->mode);
2670 	if ((ieee->mode == IEEE_N_24G) || (ieee->mode == IEEE_N_5G))
2671 		HTUseDefaultSetting(ieee);
2672 	else
2673 		ieee->pHTInfo->bCurrentHTSupport = false;
2674 
2675 	ieee->SetHwRegHandler(ieee->dev, HW_VAR_MEDIA_STATUS,
2676 			      (u8 *)(&ieee->state));
2677 
2678 	ieee->state = RTLLIB_LINKED;
2679 	ieee->link_change(ieee->dev);
2680 
2681 	HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
2682 	if (ieee->LedControlHandler != NULL)
2683 		ieee->LedControlHandler(ieee->dev, LED_CTL_LINK);
2684 
2685 	rtllib_start_send_beacons(ieee);
2686 
2687 	notify_wx_assoc_event(ieee);
2688 
2689 	if (ieee->data_hard_resume)
2690 		ieee->data_hard_resume(ieee->dev);
2691 
2692 	netif_carrier_on(ieee->dev);
2693 
2694 	mutex_unlock(&ieee->wx_mutex);
2695 }
2696 
2697 inline void rtllib_start_ibss(struct rtllib_device *ieee)
2698 {
2699 	schedule_delayed_work(&ieee->start_ibss_wq, msecs_to_jiffies(150));
2700 }
2701 
2702 /* this is called only in user context, with wx_mutex held */
2703 static void rtllib_start_bss(struct rtllib_device *ieee)
2704 {
2705 	unsigned long flags;
2706 
2707 	if (IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee)) {
2708 		if (!ieee->global_domain)
2709 			return;
2710 	}
2711 	/* check if we have already found the net we
2712 	 * are interested in (if any).
2713 	 * if not (we are disassociated and we are not
2714 	 * in associating / authenticating phase) start the background scanning.
2715 	 */
2716 	rtllib_softmac_check_all_nets(ieee);
2717 
2718 	/* ensure no-one start an associating process (thus setting
2719 	 * the ieee->state to rtllib_ASSOCIATING) while we
2720 	 * have just checked it and we are going to enable scan.
2721 	 * The rtllib_new_net function is always called with
2722 	 * lock held (from both rtllib_softmac_check_all_nets and
2723 	 * the rx path), so we cannot be in the middle of such function
2724 	 */
2725 	spin_lock_irqsave(&ieee->lock, flags);
2726 
2727 	if (ieee->state == RTLLIB_NOLINK)
2728 		rtllib_start_scan(ieee);
2729 	spin_unlock_irqrestore(&ieee->lock, flags);
2730 }
2731 
2732 static void rtllib_link_change_wq(void *data)
2733 {
2734 	struct rtllib_device *ieee = container_of_dwork_rsl(data,
2735 				     struct rtllib_device, link_change_wq);
2736 	ieee->link_change(ieee->dev);
2737 }
2738 /* called only in userspace context */
2739 void rtllib_disassociate(struct rtllib_device *ieee)
2740 {
2741 	netif_carrier_off(ieee->dev);
2742 	if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2743 		rtllib_reset_queue(ieee);
2744 
2745 	if (ieee->data_hard_stop)
2746 		ieee->data_hard_stop(ieee->dev);
2747 	if (IS_DOT11D_ENABLE(ieee))
2748 		dot11d_reset(ieee);
2749 	ieee->state = RTLLIB_NOLINK;
2750 	ieee->is_set_key = false;
2751 	ieee->wap_set = 0;
2752 
2753 	schedule_delayed_work(&ieee->link_change_wq, 0);
2754 
2755 	notify_wx_assoc_event(ieee);
2756 }
2757 
2758 static void rtllib_associate_retry_wq(void *data)
2759 {
2760 	struct rtllib_device *ieee = container_of_dwork_rsl(data,
2761 				     struct rtllib_device, associate_retry_wq);
2762 	unsigned long flags;
2763 
2764 	mutex_lock(&ieee->wx_mutex);
2765 	if (!ieee->proto_started)
2766 		goto exit;
2767 
2768 	if (ieee->state != RTLLIB_ASSOCIATING_RETRY)
2769 		goto exit;
2770 
2771 	/* until we do not set the state to RTLLIB_NOLINK
2772 	 * there are no possibility to have someone else trying
2773 	 * to start an association procedure (we get here with
2774 	 * ieee->state = RTLLIB_ASSOCIATING).
2775 	 * When we set the state to RTLLIB_NOLINK it is possible
2776 	 * that the RX path run an attempt to associate, but
2777 	 * both rtllib_softmac_check_all_nets and the
2778 	 * RX path works with ieee->lock held so there are no
2779 	 * problems. If we are still disassociated then start a scan.
2780 	 * the lock here is necessary to ensure no one try to start
2781 	 * an association procedure when we have just checked the
2782 	 * state and we are going to start the scan.
2783 	 */
2784 	ieee->beinretry = true;
2785 	ieee->state = RTLLIB_NOLINK;
2786 
2787 	rtllib_softmac_check_all_nets(ieee);
2788 
2789 	spin_lock_irqsave(&ieee->lock, flags);
2790 
2791 	if (ieee->state == RTLLIB_NOLINK)
2792 		rtllib_start_scan(ieee);
2793 	spin_unlock_irqrestore(&ieee->lock, flags);
2794 
2795 	ieee->beinretry = false;
2796 exit:
2797 	mutex_unlock(&ieee->wx_mutex);
2798 }
2799 
2800 static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee)
2801 {
2802 	static const u8 broadcast_addr[] = {
2803 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2804 	};
2805 	struct sk_buff *skb;
2806 	struct rtllib_probe_response *b;
2807 
2808 	skb = rtllib_probe_resp(ieee, broadcast_addr);
2809 
2810 	if (!skb)
2811 		return NULL;
2812 
2813 	b = (struct rtllib_probe_response *)skb->data;
2814 	b->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_BEACON);
2815 
2816 	return skb;
2817 
2818 }
2819 
2820 struct sk_buff *rtllib_get_beacon(struct rtllib_device *ieee)
2821 {
2822 	struct sk_buff *skb;
2823 	struct rtllib_probe_response *b;
2824 
2825 	skb = rtllib_get_beacon_(ieee);
2826 	if (!skb)
2827 		return NULL;
2828 
2829 	b = (struct rtllib_probe_response *)skb->data;
2830 	b->header.seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2831 
2832 	if (ieee->seq_ctrl[0] == 0xFFF)
2833 		ieee->seq_ctrl[0] = 0;
2834 	else
2835 		ieee->seq_ctrl[0]++;
2836 
2837 	return skb;
2838 }
2839 EXPORT_SYMBOL(rtllib_get_beacon);
2840 
2841 void rtllib_softmac_stop_protocol(struct rtllib_device *ieee, u8 mesh_flag,
2842 				  u8 shutdown)
2843 {
2844 	rtllib_stop_scan_syncro(ieee);
2845 	mutex_lock(&ieee->wx_mutex);
2846 	rtllib_stop_protocol(ieee, shutdown);
2847 	mutex_unlock(&ieee->wx_mutex);
2848 }
2849 EXPORT_SYMBOL(rtllib_softmac_stop_protocol);
2850 
2851 
2852 void rtllib_stop_protocol(struct rtllib_device *ieee, u8 shutdown)
2853 {
2854 	if (!ieee->proto_started)
2855 		return;
2856 
2857 	if (shutdown) {
2858 		ieee->proto_started = 0;
2859 		ieee->proto_stoppping = 1;
2860 		if (ieee->rtllib_ips_leave != NULL)
2861 			ieee->rtllib_ips_leave(ieee->dev);
2862 	}
2863 
2864 	rtllib_stop_send_beacons(ieee);
2865 	del_timer_sync(&ieee->associate_timer);
2866 	cancel_delayed_work_sync(&ieee->associate_retry_wq);
2867 	cancel_delayed_work_sync(&ieee->start_ibss_wq);
2868 	cancel_delayed_work_sync(&ieee->link_change_wq);
2869 	rtllib_stop_scan(ieee);
2870 
2871 	if (ieee->state <= RTLLIB_ASSOCIATING_AUTHENTICATED)
2872 		ieee->state = RTLLIB_NOLINK;
2873 
2874 	if (ieee->state == RTLLIB_LINKED) {
2875 		if (ieee->iw_mode == IW_MODE_INFRA)
2876 			SendDisassociation(ieee, 1, WLAN_REASON_DEAUTH_LEAVING);
2877 		rtllib_disassociate(ieee);
2878 	}
2879 
2880 	if (shutdown) {
2881 		RemoveAllTS(ieee);
2882 		ieee->proto_stoppping = 0;
2883 	}
2884 	kfree(ieee->assocreq_ies);
2885 	ieee->assocreq_ies = NULL;
2886 	ieee->assocreq_ies_len = 0;
2887 	kfree(ieee->assocresp_ies);
2888 	ieee->assocresp_ies = NULL;
2889 	ieee->assocresp_ies_len = 0;
2890 }
2891 
2892 void rtllib_softmac_start_protocol(struct rtllib_device *ieee, u8 mesh_flag)
2893 {
2894 	mutex_lock(&ieee->wx_mutex);
2895 	rtllib_start_protocol(ieee);
2896 	mutex_unlock(&ieee->wx_mutex);
2897 }
2898 EXPORT_SYMBOL(rtllib_softmac_start_protocol);
2899 
2900 void rtllib_start_protocol(struct rtllib_device *ieee)
2901 {
2902 	short ch = 0;
2903 	int i = 0;
2904 
2905 	rtllib_update_active_chan_map(ieee);
2906 
2907 	if (ieee->proto_started)
2908 		return;
2909 
2910 	ieee->proto_started = 1;
2911 
2912 	if (ieee->current_network.channel == 0) {
2913 		do {
2914 			ch++;
2915 			if (ch > MAX_CHANNEL_NUMBER)
2916 				return; /* no channel found */
2917 		} while (!ieee->active_channel_map[ch]);
2918 		ieee->current_network.channel = ch;
2919 	}
2920 
2921 	if (ieee->current_network.beacon_interval == 0)
2922 		ieee->current_network.beacon_interval = 100;
2923 
2924 	for (i = 0; i < 17; i++) {
2925 		ieee->last_rxseq_num[i] = -1;
2926 		ieee->last_rxfrag_num[i] = -1;
2927 		ieee->last_packet_time[i] = 0;
2928 	}
2929 
2930 	if (ieee->UpdateBeaconInterruptHandler)
2931 		ieee->UpdateBeaconInterruptHandler(ieee->dev, false);
2932 
2933 	ieee->wmm_acm = 0;
2934 	/* if the user set the MAC of the ad-hoc cell and then
2935 	 * switch to managed mode, shall we  make sure that association
2936 	 * attempts does not fail just because the user provide the essid
2937 	 * and the nic is still checking for the AP MAC ??
2938 	 */
2939 	if (ieee->iw_mode == IW_MODE_INFRA) {
2940 		rtllib_start_bss(ieee);
2941 	} else if (ieee->iw_mode == IW_MODE_ADHOC) {
2942 		if (ieee->UpdateBeaconInterruptHandler)
2943 			ieee->UpdateBeaconInterruptHandler(ieee->dev, true);
2944 
2945 		rtllib_start_ibss(ieee);
2946 
2947 	} else if (ieee->iw_mode == IW_MODE_MASTER) {
2948 		rtllib_start_master_bss(ieee);
2949 	} else if (ieee->iw_mode == IW_MODE_MONITOR) {
2950 		rtllib_start_monitor_mode(ieee);
2951 	}
2952 }
2953 
2954 int rtllib_softmac_init(struct rtllib_device *ieee)
2955 {
2956 	int i;
2957 
2958 	memset(&ieee->current_network, 0, sizeof(struct rtllib_network));
2959 
2960 	ieee->state = RTLLIB_NOLINK;
2961 	for (i = 0; i < 5; i++)
2962 		ieee->seq_ctrl[i] = 0;
2963 	ieee->dot11d_info = kzalloc(sizeof(struct rt_dot11d_info), GFP_ATOMIC);
2964 	if (!ieee->dot11d_info)
2965 		return -ENOMEM;
2966 
2967 	ieee->LinkDetectInfo.SlotIndex = 0;
2968 	ieee->LinkDetectInfo.SlotNum = 2;
2969 	ieee->LinkDetectInfo.NumRecvBcnInPeriod = 0;
2970 	ieee->LinkDetectInfo.NumRecvDataInPeriod = 0;
2971 	ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
2972 	ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
2973 	ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
2974 	ieee->bIsAggregateFrame = false;
2975 	ieee->assoc_id = 0;
2976 	ieee->queue_stop = 0;
2977 	ieee->scanning_continue = 0;
2978 	ieee->softmac_features = 0;
2979 	ieee->wap_set = 0;
2980 	ieee->ssid_set = 0;
2981 	ieee->proto_started = 0;
2982 	ieee->proto_stoppping = 0;
2983 	ieee->basic_rate = RTLLIB_DEFAULT_BASIC_RATE;
2984 	ieee->rate = 22;
2985 	ieee->ps = RTLLIB_PS_DISABLED;
2986 	ieee->sta_sleep = LPS_IS_WAKE;
2987 
2988 	ieee->Regdot11HTOperationalRateSet[0] = 0xff;
2989 	ieee->Regdot11HTOperationalRateSet[1] = 0xff;
2990 	ieee->Regdot11HTOperationalRateSet[4] = 0x01;
2991 
2992 	ieee->Regdot11TxHTOperationalRateSet[0] = 0xff;
2993 	ieee->Regdot11TxHTOperationalRateSet[1] = 0xff;
2994 	ieee->Regdot11TxHTOperationalRateSet[4] = 0x01;
2995 
2996 	ieee->FirstIe_InScan = false;
2997 	ieee->actscanning = false;
2998 	ieee->beinretry = false;
2999 	ieee->is_set_key = false;
3000 	init_mgmt_queue(ieee);
3001 
3002 	ieee->tx_pending.txb = NULL;
3003 
3004 	timer_setup(&ieee->associate_timer, rtllib_associate_abort_cb, 0);
3005 
3006 	timer_setup(&ieee->beacon_timer, rtllib_send_beacon_cb, 0);
3007 
3008 	INIT_DELAYED_WORK_RSL(&ieee->link_change_wq,
3009 			      (void *)rtllib_link_change_wq, ieee);
3010 	INIT_DELAYED_WORK_RSL(&ieee->start_ibss_wq,
3011 			      (void *)rtllib_start_ibss_wq, ieee);
3012 	INIT_WORK_RSL(&ieee->associate_complete_wq,
3013 		      (void *)rtllib_associate_complete_wq, ieee);
3014 	INIT_DELAYED_WORK_RSL(&ieee->associate_procedure_wq,
3015 			      (void *)rtllib_associate_procedure_wq, ieee);
3016 	INIT_DELAYED_WORK_RSL(&ieee->softmac_scan_wq,
3017 			      (void *)rtllib_softmac_scan_wq, ieee);
3018 	INIT_DELAYED_WORK_RSL(&ieee->associate_retry_wq,
3019 			      (void *)rtllib_associate_retry_wq, ieee);
3020 	INIT_WORK_RSL(&ieee->wx_sync_scan_wq, (void *)rtllib_wx_sync_scan_wq,
3021 		      ieee);
3022 
3023 	mutex_init(&ieee->wx_mutex);
3024 	mutex_init(&ieee->scan_mutex);
3025 	mutex_init(&ieee->ips_mutex);
3026 
3027 	spin_lock_init(&ieee->mgmt_tx_lock);
3028 	spin_lock_init(&ieee->beacon_lock);
3029 
3030 	INIT_WORK(&ieee->ps_task, rtllib_sta_ps);
3031 
3032 	return 0;
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 	cancel_work_sync(&ieee->ps_task);
3053 	mutex_unlock(&ieee->wx_mutex);
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