net: wireless: rockchip_wlan: add rtl8723cs support
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8723cs / os_dep / linux / recv_linux.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _RECV_OSDEP_C_
21
22 #include <drv_types.h>
23
24 int rtw_os_recvframe_duplicate_skb(_adapter *padapter, union recv_frame *pcloneframe, _pkt *pskb)
25 {
26         int res = _SUCCESS;
27         _pkt    *pkt_copy = NULL;
28         struct rx_pkt_attrib *pattrib = &pcloneframe->u.hdr.attrib;
29
30         if (pskb == NULL) {
31                 RTW_INFO("%s [WARN] skb == NULL, drop frag frame\n", __func__);
32                 return _FAIL;
33         }
34 #if 1
35         pkt_copy = rtw_skb_copy(pskb);
36
37         if (pkt_copy == NULL) {
38                 RTW_INFO("%s [WARN] rtw_skb_copy fail , drop frag frame\n", __func__);
39                 return _FAIL;
40         }
41 #else
42         pkt_copy = rtw_skb_clone(pskb);
43
44         if (pkt_copy == NULL) {
45                 RTW_INFO("%s [WARN] rtw_skb_clone fail , drop frag frame\n", __func__);
46                 return _FAIL;
47         }
48 #endif
49         pkt_copy->dev = padapter->pnetdev;
50
51         pcloneframe->u.hdr.pkt = pkt_copy;
52         pcloneframe->u.hdr.rx_head = pkt_copy->head;
53         pcloneframe->u.hdr.rx_data = pkt_copy->data;
54         pcloneframe->u.hdr.rx_end = skb_end_pointer(pkt_copy);
55         pcloneframe->u.hdr.rx_tail = skb_tail_pointer(pkt_copy);
56         pcloneframe->u.hdr.len = pkt_copy->len;
57
58         return res;
59 }
60
61 int rtw_os_alloc_recvframe(_adapter *padapter, union recv_frame *precvframe, u8 *pdata, _pkt *pskb)
62 {
63         int res = _SUCCESS;
64         u8      shift_sz = 0;
65         u32     skb_len, alloc_sz;
66         _pkt    *pkt_copy = NULL;
67         struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
68
69
70         if (pdata == NULL) {
71                 precvframe->u.hdr.pkt = NULL;
72                 res = _FAIL;
73                 return res;
74         }
75
76
77         /*      Modified by Albert 20101213 */
78         /*      For 8 bytes IP header alignment. */
79         shift_sz = pattrib->qos ? 6 : 0; /*     Qos data, wireless lan header length is 26 */
80
81         skb_len = pattrib->pkt_len;
82
83         /* for first fragment packet, driver need allocate 1536+drvinfo_sz+RXDESC_SIZE to defrag packet. */
84         /* modify alloc_sz for recvive crc error packet by thomas 2011-06-02 */
85         if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
86                 /* alloc_sz = 1664;      */ /* 1664 is 128 alignment. */
87                 alloc_sz = (skb_len <= 1650) ? 1664 : (skb_len + 14);
88         } else {
89                 alloc_sz = skb_len;
90                 /*      6 is for IP header 8 bytes alignment in QoS packet case. */
91                 /*      8 is for skb->data 4 bytes alignment. */
92                 alloc_sz += 14;
93         }
94
95         pkt_copy = rtw_skb_alloc(alloc_sz);
96
97         if (pkt_copy) {
98                 pkt_copy->dev = padapter->pnetdev;
99                 pkt_copy->len = skb_len;
100                 precvframe->u.hdr.pkt = pkt_copy;
101                 precvframe->u.hdr.rx_head = pkt_copy->head;
102                 precvframe->u.hdr.rx_end = pkt_copy->data + alloc_sz;
103                 skb_reserve(pkt_copy, 8 - ((SIZE_PTR)(pkt_copy->data) & 7));  /* force pkt_copy->data at 8-byte alignment address */
104                 skb_reserve(pkt_copy, shift_sz);/* force ip_hdr at 8-byte alignment address according to shift_sz. */
105                 _rtw_memcpy(pkt_copy->data, pdata, skb_len);
106                 precvframe->u.hdr.rx_data = precvframe->u.hdr.rx_tail = pkt_copy->data;
107         } else {
108 #if 0
109                 {
110                         rtw_free_recvframe(precvframe_if2, &precvpriv->free_recv_queue);
111                         rtw_enqueue_recvbuf_to_head(precvbuf, &precvpriv->recv_buf_pending_queue);
112
113                         /* The case of can't allocate skb is serious and may never be recovered,
114                          once bDriverStopped is enable, this task should be stopped.*/
115                         if (!rtw_is_drv_stopped(secondary_padapter))
116 #ifdef PLATFORM_LINUX
117                                 tasklet_schedule(&precvpriv->recv_tasklet);
118 #endif
119                         return ret;
120                 }
121
122 #endif
123
124 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
125                 RTW_INFO("%s:can not allocate memory for skb copy\n", __func__);
126
127                 precvframe->u.hdr.pkt = NULL;
128
129                 /* rtw_free_recvframe(precvframe, pfree_recv_queue); */
130                 /*exit_rtw_os_recv_resource_alloc;*/
131
132                 res = _FAIL;
133 #else
134                 if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
135                         RTW_INFO("%s: alloc_skb fail , drop frag frame\n", __FUNCTION__);
136                         /* rtw_free_recvframe(precvframe, pfree_recv_queue); */
137                         res = _FAIL;
138                         goto exit_rtw_os_recv_resource_alloc;
139                 }
140
141                 if (pskb == NULL) {
142                         res = _FAIL;
143                         goto exit_rtw_os_recv_resource_alloc;
144                 }
145
146                 precvframe->u.hdr.pkt = rtw_skb_clone(pskb);
147                 if (precvframe->u.hdr.pkt) {
148                         precvframe->u.hdr.pkt->dev = padapter->pnetdev;
149                         precvframe->u.hdr.rx_head = precvframe->u.hdr.rx_data = precvframe->u.hdr.rx_tail = pdata;
150                         precvframe->u.hdr.rx_end =  pdata + alloc_sz;
151                 } else {
152                         RTW_INFO("%s: rtw_skb_clone fail\n", __FUNCTION__);
153                         /* rtw_free_recvframe(precvframe, pfree_recv_queue); */
154                         /*exit_rtw_os_recv_resource_alloc;*/
155                         res = _FAIL;
156                 }
157 #endif
158         }
159
160 exit_rtw_os_recv_resource_alloc:
161
162         return res;
163
164 }
165
166 void rtw_os_free_recvframe(union recv_frame *precvframe)
167 {
168         if (precvframe->u.hdr.pkt) {
169                 rtw_skb_free(precvframe->u.hdr.pkt);/* free skb by driver */
170
171                 precvframe->u.hdr.pkt = NULL;
172         }
173 }
174
175 /* init os related resource in struct recv_priv */
176 int rtw_os_recv_resource_init(struct recv_priv *precvpriv, _adapter *padapter)
177 {
178         int     res = _SUCCESS;
179
180
181 #ifdef CONFIG_RTW_NAPI
182         skb_queue_head_init(&precvpriv->rx_napi_skb_queue);
183 #endif /* CONFIG_RTW_NAPI */
184
185         return res;
186 }
187
188 /* alloc os related resource in union recv_frame */
189 int rtw_os_recv_resource_alloc(_adapter *padapter, union recv_frame *precvframe)
190 {
191         int     res = _SUCCESS;
192
193         precvframe->u.hdr.pkt_newalloc = precvframe->u.hdr.pkt = NULL;
194
195         return res;
196 }
197
198 /* free os related resource in union recv_frame */
199 void rtw_os_recv_resource_free(struct recv_priv *precvpriv)
200 {
201         sint i;
202         union recv_frame *precvframe;
203         precvframe = (union recv_frame *) precvpriv->precv_frame_buf;
204
205
206 #ifdef CONFIG_RTW_NAPI
207         if (skb_queue_len(&precvpriv->rx_napi_skb_queue))
208                 RTW_WARN("rx_napi_skb_queue not empty\n");
209         rtw_skb_queue_purge(&precvpriv->rx_napi_skb_queue);
210 #endif /* CONFIG_RTW_NAPI */
211
212         for (i = 0; i < NR_RECVFRAME; i++) {
213                 if (precvframe->u.hdr.pkt) {
214                         rtw_skb_free(precvframe->u.hdr.pkt);/* free skb by driver */
215                         precvframe->u.hdr.pkt = NULL;
216                 }
217                 precvframe++;
218         }
219 }
220
221 /* alloc os related resource in struct recv_buf */
222 int rtw_os_recvbuf_resource_alloc(_adapter *padapter, struct recv_buf *precvbuf)
223 {
224         int res = _SUCCESS;
225
226 #ifdef CONFIG_USB_HCI
227         struct dvobj_priv       *pdvobjpriv = adapter_to_dvobj(padapter);
228         struct usb_device       *pusbd = pdvobjpriv->pusbdev;
229
230         precvbuf->irp_pending = _FALSE;
231         precvbuf->purb = usb_alloc_urb(0, GFP_KERNEL);
232         if (precvbuf->purb == NULL)
233                 res = _FAIL;
234
235         precvbuf->pskb = NULL;
236
237         precvbuf->pallocated_buf  = precvbuf->pbuf = NULL;
238
239         precvbuf->pdata = precvbuf->phead = precvbuf->ptail = precvbuf->pend = NULL;
240
241         precvbuf->transfer_len = 0;
242
243         precvbuf->len = 0;
244
245 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
246         precvbuf->pallocated_buf = rtw_usb_buffer_alloc(pusbd, (size_t)precvbuf->alloc_sz, &precvbuf->dma_transfer_addr);
247         precvbuf->pbuf = precvbuf->pallocated_buf;
248         if (precvbuf->pallocated_buf == NULL)
249                 return _FAIL;
250 #endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */
251
252 #endif /* CONFIG_USB_HCI */
253
254         return res;
255 }
256
257 /* free os related resource in struct recv_buf */
258 int rtw_os_recvbuf_resource_free(_adapter *padapter, struct recv_buf *precvbuf)
259 {
260         int ret = _SUCCESS;
261
262 #ifdef CONFIG_USB_HCI
263
264 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
265
266         struct dvobj_priv       *pdvobjpriv = adapter_to_dvobj(padapter);
267         struct usb_device       *pusbd = pdvobjpriv->pusbdev;
268
269         rtw_usb_buffer_free(pusbd, (size_t)precvbuf->alloc_sz, precvbuf->pallocated_buf, precvbuf->dma_transfer_addr);
270         precvbuf->pallocated_buf =  NULL;
271         precvbuf->dma_transfer_addr = 0;
272
273 #endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */
274
275         if (precvbuf->purb) {
276                 /* usb_kill_urb(precvbuf->purb); */
277                 usb_free_urb(precvbuf->purb);
278         }
279
280 #endif /* CONFIG_USB_HCI */
281
282
283         if (precvbuf->pskb) {
284 #ifdef CONFIG_PREALLOC_RX_SKB_BUFFER
285                 if (rtw_free_skb_premem(precvbuf->pskb) != 0)
286 #endif
287                         rtw_skb_free(precvbuf->pskb);
288         }
289         return ret;
290
291 }
292
293 _pkt *rtw_os_alloc_msdu_pkt(union recv_frame *prframe, u16 nSubframe_Length, u8 *pdata)
294 {
295         u16     eth_type;
296         u8      *data_ptr;
297         _pkt *sub_skb;
298         struct rx_pkt_attrib *pattrib;
299
300         pattrib = &prframe->u.hdr.attrib;
301
302 #ifdef CONFIG_SKB_COPY
303         sub_skb = rtw_skb_alloc(nSubframe_Length + 12);
304         if (sub_skb) {
305                 skb_reserve(sub_skb, 12);
306                 data_ptr = (u8 *)skb_put(sub_skb, nSubframe_Length);
307                 _rtw_memcpy(data_ptr, (pdata + ETH_HLEN), nSubframe_Length);
308         } else
309 #endif /* CONFIG_SKB_COPY */
310         {
311                 sub_skb = rtw_skb_clone(prframe->u.hdr.pkt);
312                 if (sub_skb) {
313                         sub_skb->data = pdata + ETH_HLEN;
314                         sub_skb->len = nSubframe_Length;
315                         skb_set_tail_pointer(sub_skb, nSubframe_Length);
316                 } else {
317                         RTW_INFO("%s(): rtw_skb_clone() Fail!!!\n", __FUNCTION__);
318                         return NULL;
319                 }
320         }
321
322         eth_type = RTW_GET_BE16(&sub_skb->data[6]);
323
324         if (sub_skb->len >= 8 &&
325             ((_rtw_memcmp(sub_skb->data, rtw_rfc1042_header, SNAP_SIZE) &&
326               eth_type != ETH_P_AARP && eth_type != ETH_P_IPX) ||
327              _rtw_memcmp(sub_skb->data, rtw_bridge_tunnel_header, SNAP_SIZE))) {
328                 /* remove RFC1042 or Bridge-Tunnel encapsulation and replace EtherType */
329                 skb_pull(sub_skb, SNAP_SIZE);
330                 _rtw_memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
331                 _rtw_memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
332         } else {
333                 u16 len;
334                 /* Leave Ethernet header part of hdr and full payload */
335                 len = htons(sub_skb->len);
336                 _rtw_memcpy(skb_push(sub_skb, 2), &len, 2);
337                 _rtw_memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->src, ETH_ALEN);
338                 _rtw_memcpy(skb_push(sub_skb, ETH_ALEN), pattrib->dst, ETH_ALEN);
339         }
340
341         return sub_skb;
342 }
343
344 #ifdef CONFIG_RTW_NAPI
345 static int napi_recv(_adapter *padapter, int budget)
346 {
347         _pkt *pskb;
348         struct recv_priv *precvpriv = &padapter->recvpriv;
349         int work_done = 0;
350         struct registry_priv *pregistrypriv = &padapter->registrypriv;
351         u8 rx_ok;
352
353
354         while ((work_done < budget) &&
355                (!skb_queue_empty(&precvpriv->rx_napi_skb_queue))) {
356                 pskb = skb_dequeue(&precvpriv->rx_napi_skb_queue);
357                 if (!pskb)
358                         break;
359
360                 rx_ok = _FALSE;
361
362 #ifdef CONFIG_RTW_GRO
363                 if (pregistrypriv->en_gro) {
364                         if (rtw_napi_gro_receive(&padapter->napi, pskb) != GRO_DROP)
365                                 rx_ok = _TRUE;
366                         goto next;
367                 }
368 #endif /* CONFIG_RTW_GRO */
369
370                 if (rtw_netif_receive_skb(padapter->pnetdev, pskb) == NET_RX_SUCCESS)
371                         rx_ok = _TRUE;
372
373 next:
374                 if (rx_ok == _TRUE) {
375                         work_done++;
376                         DBG_COUNTER(padapter->rx_logs.os_netif_ok);
377                 } else {
378                         DBG_COUNTER(padapter->rx_logs.os_netif_err);
379                 }
380         }
381
382         return work_done;
383 }
384
385 int rtw_recv_napi_poll(struct napi_struct *napi, int budget)
386 {
387         _adapter *padapter = container_of(napi, _adapter, napi);
388         int work_done = 0;
389         struct recv_priv *precvpriv = &padapter->recvpriv;
390
391
392         work_done = napi_recv(padapter, budget);
393         if (work_done < budget) {
394                 napi_complete(napi);
395                 if (!skb_queue_empty(&precvpriv->rx_napi_skb_queue))
396                         napi_schedule(napi);
397         }
398
399         return work_done;
400 }
401 #endif /* CONFIG_RTW_NAPI */
402
403 #ifdef DBG_UDP_PKT_LOSE_11AC
404         #define PAYLOAD_LEN_LOC_OF_IP_HDR 0x10 /*ethernet payload length location of ip header (DA + SA+eth_type+(version&hdr_len)) */
405 #endif
406
407 void rtw_os_recv_indicate_pkt(_adapter *padapter, _pkt *pkt, struct rx_pkt_attrib *pattrib)
408 {
409         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
410         struct recv_priv *precvpriv = &(padapter->recvpriv);
411         struct registry_priv    *pregistrypriv = &padapter->registrypriv;
412 #ifdef CONFIG_BR_EXT
413         void *br_port = NULL;
414 #endif
415         int ret;
416
417         /* Indicat the packets to upper layer */
418         if (pkt) {
419                 if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE) {
420                         _pkt *pskb2 = NULL;
421                         struct sta_info *psta = NULL;
422                         struct sta_priv *pstapriv = &padapter->stapriv;
423                         int bmcast = IS_MCAST(pattrib->dst);
424
425                         /* RTW_INFO("bmcast=%d\n", bmcast); */
426
427                         if (_rtw_memcmp(pattrib->dst, adapter_mac_addr(padapter), ETH_ALEN) == _FALSE) {
428                                 /* RTW_INFO("not ap psta=%p, addr=%pM\n", psta, pattrib->dst); */
429
430                                 if (bmcast) {
431                                         psta = rtw_get_bcmc_stainfo(padapter);
432                                         pskb2 = rtw_skb_clone(pkt);
433                                 } else
434                                         psta = rtw_get_stainfo(pstapriv, pattrib->dst);
435
436                                 if (psta) {
437                                         struct net_device *pnetdev = (struct net_device *)padapter->pnetdev;
438
439                                         /* RTW_INFO("directly forwarding to the rtw_xmit_entry\n"); */
440
441                                         /* skb->ip_summed = CHECKSUM_NONE; */
442                                         pkt->dev = pnetdev;
443 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
444                                         skb_set_queue_mapping(pkt, rtw_recv_select_queue(pkt));
445 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35) */
446
447                                         _rtw_xmit_entry(pkt, pnetdev);
448
449                                         if (bmcast && (pskb2 != NULL)) {
450                                                 pkt = pskb2;
451                                                 DBG_COUNTER(padapter->rx_logs.os_indicate_ap_mcast);
452                                         } else {
453                                                 DBG_COUNTER(padapter->rx_logs.os_indicate_ap_forward);
454                                                 return;
455                                         }
456                                 }
457                         } else { /* to APself */
458                                 /* RTW_INFO("to APSelf\n"); */
459                                 DBG_COUNTER(padapter->rx_logs.os_indicate_ap_self);
460                         }
461                 }
462
463 #ifdef CONFIG_BR_EXT
464                 /* Insert NAT2.5 RX here! */
465 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35))
466                 br_port = padapter->pnetdev->br_port;
467 #else   /* (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35)) */
468                 rcu_read_lock();
469                 br_port = rcu_dereference(padapter->pnetdev->rx_handler_data);
470                 rcu_read_unlock();
471 #endif /* (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35)) */
472
473
474                 if (br_port && (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_ADHOC_STATE) == _TRUE)) {
475                         int nat25_handle_frame(_adapter *priv, struct sk_buff *skb);
476                         if (nat25_handle_frame(padapter, pkt) == -1) {
477                                 /* priv->ext_stats.rx_data_drops++; */
478                                 /* DEBUG_ERR("RX DROP: nat25_handle_frame fail!\n"); */
479                                 /* return FAIL; */
480
481 #if 1
482                                 /* bypass this frame to upper layer!! */
483 #else
484                                 rtw_skb_free(sub_skb);
485                                 continue;
486 #endif
487                         }
488                 }
489 #endif /* CONFIG_BR_EXT */
490                 if (precvpriv->sink_udpport > 0)
491                         rtw_sink_rtp_seq_dbg(padapter, pkt);
492 #ifdef DBG_UDP_PKT_LOSE_11AC
493                 /* After eth_type_trans process , pkt->data pointer will move from ethrnet header to ip header ,
494                 *       we have to check ethernet type , so this debug must be print before eth_type_trans
495                 */
496                 if (*((unsigned short *)(pkt->data + ETH_ALEN * 2)) == htons(ETH_P_ARP)) {
497                         /* ARP Payload length will be 42bytes or 42+18(tailer)=60bytes*/
498                         if (pkt->len != 42 && pkt->len != 60)
499                                 RTW_INFO("Error !!%s,ARP Payload length %u not correct\n" , __func__ , pkt->len);
500                 } else if (*((unsigned short *)(pkt->data + ETH_ALEN * 2)) == htons(ETH_P_IP)) {
501                         if (be16_to_cpu(*((u16 *)(pkt->data + PAYLOAD_LEN_LOC_OF_IP_HDR))) != (pkt->len) - ETH_HLEN) {
502                                 RTW_INFO("Error !!%s,Payload length not correct\n" , __func__);
503                                 RTW_INFO("%s, IP header describe Total length=%u\n" , __func__ , be16_to_cpu(*((u16 *)(pkt->data + PAYLOAD_LEN_LOC_OF_IP_HDR))));
504                                 RTW_INFO("%s, Pkt real length=%u\n" , __func__ , (pkt->len) - ETH_HLEN);
505                         }
506                 }
507 #endif
508                 /* After eth_type_trans process , pkt->data pointer will move from ethrnet header to ip header */
509                 pkt->protocol = eth_type_trans(pkt, padapter->pnetdev);
510                 pkt->dev = padapter->pnetdev;
511
512 #ifdef CONFIG_TCP_CSUM_OFFLOAD_RX
513                 if ((pattrib->tcpchk_valid == 1) && (pattrib->tcp_chkrpt == 1))
514                         pkt->ip_summed = CHECKSUM_UNNECESSARY;
515                 else
516                         pkt->ip_summed = CHECKSUM_NONE;
517 #else /* !CONFIG_TCP_CSUM_OFFLOAD_RX */
518                 pkt->ip_summed = CHECKSUM_NONE;
519 #endif /* CONFIG_TCP_CSUM_OFFLOAD_RX */
520
521 #ifdef CONFIG_RTW_NAPI
522                 if (pregistrypriv->en_napi) {
523                         skb_queue_tail(&precvpriv->rx_napi_skb_queue, pkt);
524                         napi_schedule(&padapter->napi);
525                         return;
526                 }
527 #endif /* CONFIG_RTW_NAPI */
528
529                 ret = rtw_netif_rx(padapter->pnetdev, pkt);
530                 if (ret == NET_RX_SUCCESS)
531                         DBG_COUNTER(padapter->rx_logs.os_netif_ok);
532                 else
533                         DBG_COUNTER(padapter->rx_logs.os_netif_err);
534         }
535 }
536
537 void rtw_handle_tkip_mic_err(_adapter *padapter, struct sta_info *sta, u8 bgroup)
538 {
539 #ifdef CONFIG_IOCTL_CFG80211
540         enum nl80211_key_type key_type = 0;
541 #endif
542         union iwreq_data wrqu;
543         struct iw_michaelmicfailure    ev;
544         struct mlme_priv              *pmlmepriv  = &padapter->mlmepriv;
545         struct security_priv    *psecuritypriv = &padapter->securitypriv;
546         u32 cur_time = 0;
547
548         if (psecuritypriv->last_mic_err_time == 0)
549                 psecuritypriv->last_mic_err_time = rtw_get_current_time();
550         else {
551                 cur_time = rtw_get_current_time();
552
553                 if (cur_time - psecuritypriv->last_mic_err_time < 60 * HZ) {
554                         psecuritypriv->btkip_countermeasure = _TRUE;
555                         psecuritypriv->last_mic_err_time = 0;
556                         psecuritypriv->btkip_countermeasure_time = cur_time;
557                 } else
558                         psecuritypriv->last_mic_err_time = rtw_get_current_time();
559         }
560
561 #ifdef CONFIG_IOCTL_CFG80211
562         if (bgroup)
563                 key_type |= NL80211_KEYTYPE_GROUP;
564         else
565                 key_type |= NL80211_KEYTYPE_PAIRWISE;
566
567         cfg80211_michael_mic_failure(padapter->pnetdev, sta->hwaddr, key_type, -1, NULL, GFP_ATOMIC);
568 #endif
569
570         _rtw_memset(&ev, 0x00, sizeof(ev));
571         if (bgroup)
572                 ev.flags |= IW_MICFAILURE_GROUP;
573         else
574                 ev.flags |= IW_MICFAILURE_PAIRWISE;
575
576         ev.src_addr.sa_family = ARPHRD_ETHER;
577         _rtw_memcpy(ev.src_addr.sa_data, sta->hwaddr, ETH_ALEN);
578
579         _rtw_memset(&wrqu, 0x00, sizeof(wrqu));
580         wrqu.data.length = sizeof(ev);
581
582 #ifndef CONFIG_IOCTL_CFG80211
583         wireless_send_event(padapter->pnetdev, IWEVMICHAELMICFAILURE, &wrqu, (char *) &ev);
584 #endif
585 }
586
587 void rtw_hostapd_mlme_rx(_adapter *padapter, union recv_frame *precv_frame)
588 {
589 #ifdef CONFIG_HOSTAPD_MLME
590         _pkt *skb;
591         struct hostapd_priv *phostapdpriv  = padapter->phostapdpriv;
592         struct net_device *pmgnt_netdev = phostapdpriv->pmgnt_netdev;
593
594
595         skb = precv_frame->u.hdr.pkt;
596
597         if (skb == NULL)
598                 return;
599
600         skb->data = precv_frame->u.hdr.rx_data;
601         skb->tail = precv_frame->u.hdr.rx_tail;
602         skb->len = precv_frame->u.hdr.len;
603
604         /* pskb_copy = rtw_skb_copy(skb);
605         *       if(skb == NULL) goto _exit; */
606
607         skb->dev = pmgnt_netdev;
608         skb->ip_summed = CHECKSUM_NONE;
609         skb->pkt_type = PACKET_OTHERHOST;
610         /* skb->protocol = __constant_htons(0x0019); ETH_P_80211_RAW */
611         skb->protocol = __constant_htons(0x0003); /*ETH_P_80211_RAW*/
612
613         /* RTW_INFO("(1)data=0x%x, head=0x%x, tail=0x%x, mac_header=0x%x, len=%d\n", skb->data, skb->head, skb->tail, skb->mac_header, skb->len); */
614
615         /* skb->mac.raw = skb->data; */
616         skb_reset_mac_header(skb);
617
618         /* skb_pull(skb, 24); */
619         _rtw_memset(skb->cb, 0, sizeof(skb->cb));
620
621         rtw_netif_rx(pmgnt_netdev, skb);
622
623         precv_frame->u.hdr.pkt = NULL; /* set pointer to NULL before rtw_free_recvframe() if call rtw_netif_rx() */
624 #endif
625 }
626
627 #ifdef CONFIG_AUTO_AP_MODE
628 static void rtw_os_ksocket_send(_adapter *padapter, union recv_frame *precv_frame)
629 {
630         _pkt *skb = precv_frame->u.hdr.pkt;
631         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
632         struct sta_info *psta = precv_frame->u.hdr.psta;
633
634         RTW_INFO("eth rx: got eth_type=0x%x\n", pattrib->eth_type);
635
636         if (psta && psta->isrc && psta->pid > 0) {
637                 u16 rx_pid;
638
639                 rx_pid = *(u16 *)(skb->data + ETH_HLEN);
640
641                 RTW_INFO("eth rx(pid=0x%x): sta("MAC_FMT") pid=0x%x\n",
642                          rx_pid, MAC_ARG(psta->hwaddr), psta->pid);
643
644                 if (rx_pid == psta->pid) {
645                         int i;
646                         u16 len = *(u16 *)(skb->data + ETH_HLEN + 2);
647                         /* u16 ctrl_type = *(u16*)(skb->data+ETH_HLEN+4); */
648
649                         /* RTW_INFO("eth, RC: len=0x%x, ctrl_type=0x%x\n", len, ctrl_type);  */
650                         RTW_INFO("eth, RC: len=0x%x\n", len);
651
652                         for (i = 0; i < len; i++)
653                                 RTW_INFO("0x%x\n", *(skb->data + ETH_HLEN + 4 + i));
654                         /* RTW_INFO("0x%x\n", *(skb->data+ETH_HLEN+6+i)); */
655
656                         RTW_INFO("eth, RC-end\n");
657
658 #if 0
659                         /* send_sz = ksocket_send(padapter->ksock_send, &padapter->kaddr_send, (skb->data+ETH_HLEN+2), len);                             */
660                         rtw_recv_ksocket_send_cmd(padapter, (skb->data + ETH_HLEN + 2), len);
661
662                         /* RTW_INFO("ksocket_send size=%d\n", send_sz);  */
663 #endif
664                 }
665
666         }
667
668 }
669 #endif /* CONFIG_AUTO_AP_MODE */
670
671 int rtw_recv_monitor(_adapter *padapter, union recv_frame *precv_frame)
672 {
673         int ret = _FAIL;
674         struct recv_priv *precvpriv;
675         _queue  *pfree_recv_queue;
676         _pkt *skb;
677         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
678         struct rx_pkt_attrib *pattrib;
679
680         if (NULL == precv_frame)
681                 goto _recv_drop;
682
683         pattrib = &precv_frame->u.hdr.attrib;
684         precvpriv = &(padapter->recvpriv);
685         pfree_recv_queue = &(precvpriv->free_recv_queue);
686
687         skb = precv_frame->u.hdr.pkt;
688         if (skb == NULL) {
689                 RTW_INFO("%s :skb==NULL something wrong!!!!\n", __func__);
690                 goto _recv_drop;
691         }
692
693         skb->data = precv_frame->u.hdr.rx_data;
694         skb_set_tail_pointer(skb, precv_frame->u.hdr.len);
695         skb->len = precv_frame->u.hdr.len;
696         skb->ip_summed = CHECKSUM_NONE;
697         skb->pkt_type = PACKET_OTHERHOST;
698         skb->protocol = htons(0x0019); /* ETH_P_80211_RAW */
699
700         rtw_netif_rx(padapter->pnetdev, skb);
701
702         /* pointers to NULL before rtw_free_recvframe() */
703         precv_frame->u.hdr.pkt = NULL;
704
705         ret = _SUCCESS;
706
707 _recv_drop:
708
709         /* enqueue back to free_recv_queue */
710         if (precv_frame)
711                 rtw_free_recvframe(precv_frame, pfree_recv_queue);
712
713         return ret;
714
715 }
716
717 int rtw_recv_indicatepkt(_adapter *padapter, union recv_frame *precv_frame)
718 {
719         struct recv_priv *precvpriv;
720         _queue  *pfree_recv_queue;
721         _pkt *skb;
722         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
723         struct rx_pkt_attrib *pattrib;
724
725         if (NULL == precv_frame)
726                 goto _recv_indicatepkt_drop;
727
728         DBG_COUNTER(padapter->rx_logs.os_indicate);
729         pattrib = &precv_frame->u.hdr.attrib;
730         precvpriv = &(padapter->recvpriv);
731         pfree_recv_queue = &(precvpriv->free_recv_queue);
732
733 #ifdef CONFIG_DRVEXT_MODULE
734         if (drvext_rx_handler(padapter, precv_frame->u.hdr.rx_data, precv_frame->u.hdr.len) == _SUCCESS)
735                 goto _recv_indicatepkt_drop;
736 #endif
737
738 #ifdef CONFIG_WAPI_SUPPORT
739         if (rtw_wapi_check_for_drop(padapter, precv_frame)) {
740                 WAPI_TRACE(WAPI_ERR, "%s(): Rx Reorder Drop case!!\n", __FUNCTION__);
741                 goto _recv_indicatepkt_drop;
742         }
743 #endif
744
745         skb = precv_frame->u.hdr.pkt;
746         if (skb == NULL) {
747                 goto _recv_indicatepkt_drop;
748         }
749
750
751         skb->data = precv_frame->u.hdr.rx_data;
752
753         skb_set_tail_pointer(skb, precv_frame->u.hdr.len);
754
755         skb->len = precv_frame->u.hdr.len;
756
757
758         if (pattrib->eth_type == 0x888e)
759                 RTW_PRINT("recv eapol packet\n");
760
761 #ifdef CONFIG_AUTO_AP_MODE
762 #if 1 /* for testing */
763 #if 1
764         if (0x8899 == pattrib->eth_type) {
765                 rtw_os_ksocket_send(padapter, precv_frame);
766
767                 /* goto _recv_indicatepkt_drop; */
768         }
769 #else
770         if (0x8899 == pattrib->eth_type) {
771                 rtw_auto_ap_mode_rx(padapter, precv_frame);
772
773                 goto _recv_indicatepkt_end;
774         }
775 #endif
776 #endif
777 #endif /* CONFIG_AUTO_AP_MODE */
778
779         /* TODO: move to core */
780         {
781                 _pkt *pkt = skb;
782                 struct ethhdr *etherhdr = (struct ethhdr *)pkt->data;
783                 struct sta_info *sta = precv_frame->u.hdr.psta;
784
785                 if (!sta)
786                         goto bypass_session_tracker;
787
788                 if (ntohs(etherhdr->h_proto) == ETH_P_IP) {
789                         u8 *ip = pkt->data + 14;
790
791                         if (GET_IPV4_PROTOCOL(ip) == 0x06  /* TCP */
792                             && rtw_st_ctl_chk_reg_s_proto(&sta->st_ctl, 0x06) == _TRUE
793                            ) {
794                                 u8 *tcp = ip + GET_IPV4_IHL(ip) * 4;
795
796                                 if (rtw_st_ctl_chk_reg_rule(&sta->st_ctl, padapter, IPV4_DST(ip), TCP_DST(tcp), IPV4_SRC(ip), TCP_SRC(tcp)) == _TRUE) {
797                                         if (GET_TCP_SYN(tcp) && GET_TCP_ACK(tcp)) {
798                                                 session_tracker_add_cmd(padapter, sta
799                                                         , IPV4_DST(ip), TCP_DST(tcp)
800                                                         , IPV4_SRC(ip), TCP_SRC(tcp));
801                                                 if (DBG_SESSION_TRACKER)
802                                                         RTW_INFO(FUNC_ADPT_FMT" local:"IP_FMT":"PORT_FMT", remote:"IP_FMT":"PORT_FMT" SYN-ACK\n"
803                                                                 , FUNC_ADPT_ARG(padapter)
804                                                                 , IP_ARG(IPV4_DST(ip)), PORT_ARG(TCP_DST(tcp))
805                                                                 , IP_ARG(IPV4_SRC(ip)), PORT_ARG(TCP_SRC(tcp)));
806                                         }
807                                         if (GET_TCP_FIN(tcp)) {
808                                                 session_tracker_del_cmd(padapter, sta
809                                                         , IPV4_DST(ip), TCP_DST(tcp)
810                                                         , IPV4_SRC(ip), TCP_SRC(tcp));
811                                                 if (DBG_SESSION_TRACKER)
812                                                         RTW_INFO(FUNC_ADPT_FMT" local:"IP_FMT":"PORT_FMT", remote:"IP_FMT":"PORT_FMT" FIN\n"
813                                                                 , FUNC_ADPT_ARG(padapter)
814                                                                 , IP_ARG(IPV4_DST(ip)), PORT_ARG(TCP_DST(tcp))
815                                                                 , IP_ARG(IPV4_SRC(ip)), PORT_ARG(TCP_SRC(tcp)));
816                                         }
817                                 }
818
819                         }
820                 }
821 bypass_session_tracker:
822                 ;
823         }
824
825         rtw_os_recv_indicate_pkt(padapter, skb, pattrib);
826
827 _recv_indicatepkt_end:
828
829         precv_frame->u.hdr.pkt = NULL; /* pointers to NULL before rtw_free_recvframe() */
830
831         rtw_free_recvframe(precv_frame, pfree_recv_queue);
832
833
834
835         return _SUCCESS;
836
837 _recv_indicatepkt_drop:
838
839         /* enqueue back to free_recv_queue */
840         if (precv_frame)
841                 rtw_free_recvframe(precv_frame, pfree_recv_queue);
842
843         DBG_COUNTER(padapter->rx_logs.os_indicate_err);
844
845         return _FAIL;
846
847 }
848
849 void rtw_os_read_port(_adapter *padapter, struct recv_buf *precvbuf)
850 {
851         struct recv_priv *precvpriv = &padapter->recvpriv;
852
853 #ifdef CONFIG_USB_HCI
854
855         precvbuf->ref_cnt--;
856
857         /* free skb in recv_buf */
858         rtw_skb_free(precvbuf->pskb);
859
860         precvbuf->pskb = NULL;
861
862         if (precvbuf->irp_pending == _FALSE)
863                 rtw_read_port(padapter, precvpriv->ff_hwaddr, 0, (unsigned char *)precvbuf);
864
865
866 #endif
867 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
868         precvbuf->pskb = NULL;
869 #endif
870
871 }
872 void _rtw_reordering_ctrl_timeout_handler(void *FunctionContext);
873 void _rtw_reordering_ctrl_timeout_handler(void *FunctionContext)
874 {
875         struct recv_reorder_ctrl *preorder_ctrl = (struct recv_reorder_ctrl *)FunctionContext;
876         rtw_reordering_ctrl_timeout_handler(preorder_ctrl);
877 }
878
879 void rtw_init_recv_timer(struct recv_reorder_ctrl *preorder_ctrl)
880 {
881         _adapter *padapter = preorder_ctrl->padapter;
882
883         _init_timer(&(preorder_ctrl->reordering_ctrl_timer), padapter->pnetdev, _rtw_reordering_ctrl_timeout_handler, preorder_ctrl);
884
885 }