OSDN Git Service

Remove another set of unecessary typedefs
[android-x86/external-modules-rtl8723au.git] / core / rtw_br_ext.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 _RTW_BR_EXT_C_
21
22 #ifdef __KERNEL__
23 #include <linux/if_arp.h>
24 #include <net/ip.h>
25 #include <net/ipx.h>
26 #include <linux/atalk.h>
27 #include <linux/udp.h>
28 #include <linux/if_pppox.h>
29 #endif
30
31 #include <drv_conf.h>
32 #include <drv_types.h>
33 #include "rtw_br_ext.h"
34
35 #ifdef CL_IPV6_PASS
36 #ifdef __KERNEL__
37 #include <linux/ipv6.h>
38 #include <linux/icmpv6.h>
39 #include <net/ndisc.h>
40 #include <net/checksum.h>
41 #endif
42 #endif
43
44 #ifdef CONFIG_BR_EXT
45
46 /* define BR_EXT_DEBUG */
47
48 #define NAT25_IPV4              01
49 #define NAT25_IPV6              02
50 #define NAT25_IPX               03
51 #define NAT25_APPLE             04
52 #define NAT25_PPPOE             05
53
54 #define RTL_RELAY_TAG_LEN (ETH_ALEN)
55 #define TAG_HDR_LEN             4
56
57 #define MAGIC_CODE              0x8186
58 #define MAGIC_CODE_LEN  2
59 #define WAIT_TIME_PPPOE 5       /*  waiting time for pppoe server in sec */
60
61 /*-----------------------------------------------------------------
62   How database records network address:
63            0    1    2    3    4    5    6    7    8    9   10
64         |----|----|----|----|----|----|----|----|----|----|----|
65   IPv4  |type|                             |      IP addr      |
66   IPX   |type|      Net addr     |          Node addr          |
67   IPX   |type|      Net addr     |Sckt addr|
68   Apple |type| Network |node|
69   PPPoE |type|   SID   |           AC MAC            |
70 -----------------------------------------------------------------*/
71
72 /* Find a tag in pppoe frame and return the pointer */
73 static __inline__ unsigned char *__nat25_find_pppoe_tag(struct pppoe_hdr *ph, unsigned short type)
74 {
75         unsigned char *cur_ptr, *start_ptr;
76         unsigned short tagLen, tagType;
77
78         start_ptr = cur_ptr = (unsigned char *)ph->tag;
79         while((cur_ptr - start_ptr) < ntohs(ph->length)) {
80                 /*  prevent un-alignment access */
81                 tagType = (unsigned short)((cur_ptr[0] << 8) + cur_ptr[1]);
82                 tagLen  = (unsigned short)((cur_ptr[2] << 8) + cur_ptr[3]);
83                 if(tagType == type)
84                         return cur_ptr;
85                 cur_ptr = cur_ptr + TAG_HDR_LEN + tagLen;
86         }
87         return 0;
88 }
89
90 static __inline__ int __nat25_add_pppoe_tag(struct sk_buff *skb, struct pppoe_tag *tag)
91 {
92         struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
93         int data_len;
94
95         data_len = tag->tag_len + TAG_HDR_LEN;
96         if (skb_tailroom(skb) < data_len) {
97                 _DEBUG_ERR("skb_tailroom() failed in add SID tag!\n");
98                 return -1;
99         }
100
101         skb_put(skb, data_len);
102         /*  have a room for new tag */
103         memmove(((unsigned char *)ph->tag + data_len), (unsigned char *)ph->tag, ntohs(ph->length));
104         ph->length = htons(ntohs(ph->length) + data_len);
105         memcpy((unsigned char *)ph->tag, tag, data_len);
106         return data_len;
107 }
108
109 static int skb_pull_and_merge(struct sk_buff *skb, unsigned char *src, int len)
110 {
111         int tail_len;
112         unsigned long end, tail;
113
114         if ((src+len) > skb_tail_pointer(skb) || skb->len < len)
115                 return -1;
116
117         tail = (unsigned long)skb_tail_pointer(skb);
118         end = (unsigned long)src+len;
119         if (tail < end)
120                 return -1;
121
122         tail_len = (int)(tail-end);
123         if (tail_len > 0)
124                 memmove(src, src+len, tail_len);
125
126         skb_trim(skb, skb->len-len);
127         return 0;
128 }
129
130 static __inline__ unsigned long __nat25_timeout(_adapter *priv)
131 {
132         unsigned long timeout;
133
134         timeout = jiffies - NAT25_AGEING_TIME*HZ;
135
136         return timeout;
137 }
138
139 static __inline__ int  __nat25_has_expired(_adapter *priv,
140                                 struct nat25_network_db_entry *fdb)
141 {
142         if(time_before_eq(fdb->ageing_timer, __nat25_timeout(priv)))
143                 return 1;
144
145         return 0;
146 }
147
148 static __inline__ void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr,
149                                 unsigned int *ipAddr)
150 {
151         memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
152
153         networkAddr[0] = NAT25_IPV4;
154         memcpy(networkAddr+7, (unsigned char *)ipAddr, 4);
155 }
156
157 static __inline__ void __nat25_generate_ipx_network_addr_with_node(unsigned char *networkAddr,
158                                 unsigned int *ipxNetAddr, unsigned char *ipxNodeAddr)
159 {
160         memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
161
162         networkAddr[0] = NAT25_IPX;
163         memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
164         memcpy(networkAddr+5, ipxNodeAddr, 6);
165 }
166
167 static __inline__ void __nat25_generate_ipx_network_addr_with_socket(unsigned char *networkAddr,
168                                 unsigned int *ipxNetAddr, unsigned short *ipxSocketAddr)
169 {
170         memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
171
172         networkAddr[0] = NAT25_IPX;
173         memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
174         memcpy(networkAddr+5, (unsigned char *)ipxSocketAddr, 2);
175 }
176
177 static __inline__ void __nat25_generate_apple_network_addr(unsigned char *networkAddr,
178                                 unsigned short *network, unsigned char *node)
179 {
180         memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
181
182         networkAddr[0] = NAT25_APPLE;
183         memcpy(networkAddr+1, (unsigned char *)network, 2);
184         networkAddr[3] = *node;
185 }
186
187 static __inline__ void __nat25_generate_pppoe_network_addr(unsigned char *networkAddr,
188                                 unsigned char *ac_mac, unsigned short *sid)
189 {
190         memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
191
192         networkAddr[0] = NAT25_PPPOE;
193         memcpy(networkAddr+1, (unsigned char *)sid, 2);
194         memcpy(networkAddr+3, (unsigned char *)ac_mac, 6);
195 }
196
197 #ifdef CL_IPV6_PASS
198 static  void __nat25_generate_ipv6_network_addr(unsigned char *networkAddr,
199                                 unsigned int *ipAddr)
200 {
201         memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
202
203         networkAddr[0] = NAT25_IPV6;
204         memcpy(networkAddr+1, (unsigned char *)ipAddr, 16);
205 }
206
207 static unsigned char *scan_tlv(unsigned char *data, int len, unsigned char tag, unsigned char len8b)
208 {
209         while (len > 0) {
210                 if (*data == tag && *(data+1) == len8b && len >= len8b*8)
211                         return data+2;
212
213                 len -= (*(data+1))*8;
214                 data += (*(data+1))*8;
215         }
216         return NULL;
217 }
218
219 static int update_nd_link_layer_addr(unsigned char *data, int len, unsigned char *replace_mac)
220 {
221         struct icmp6hdr *icmphdr = (struct icmp6hdr *)data;
222         unsigned char *mac;
223
224         if (icmphdr->icmp6_type == NDISC_ROUTER_SOLICITATION) {
225                 if (len >= 8) {
226                         mac = scan_tlv(&data[8], len-8, 1, 1);
227                         if (mac) {
228                                 _DEBUG_INFO("Router Solicitation, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
229                                         mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],
230                                         replace_mac[0],replace_mac[1],replace_mac[2],replace_mac[3],replace_mac[4],replace_mac[5]);
231                                 memcpy(mac, replace_mac, 6);
232                                 return 1;
233                         }
234                 }
235         }
236         else if (icmphdr->icmp6_type == NDISC_ROUTER_ADVERTISEMENT) {
237                 if (len >= 16) {
238                         mac = scan_tlv(&data[16], len-16, 1, 1);
239                         if (mac) {
240                                 _DEBUG_INFO("Router Advertisement, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
241                                         mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],
242                                         replace_mac[0],replace_mac[1],replace_mac[2],replace_mac[3],replace_mac[4],replace_mac[5]);
243                                 memcpy(mac, replace_mac, 6);
244                                 return 1;
245                         }
246                 }
247         }
248         else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
249                 if (len >= 24) {
250                         mac = scan_tlv(&data[24], len-24, 1, 1);
251                         if (mac) {
252                                 _DEBUG_INFO("Neighbor Solicitation, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
253                                         mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],
254                                         replace_mac[0],replace_mac[1],replace_mac[2],replace_mac[3],replace_mac[4],replace_mac[5]);
255                                 memcpy(mac, replace_mac, 6);
256                                 return 1;
257                         }
258                 }
259         }
260         else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
261                 if (len >= 24) {
262                         mac = scan_tlv(&data[24], len-24, 2, 1);
263                         if (mac) {
264                                 _DEBUG_INFO("Neighbor Advertisement, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
265                                         mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],
266                                         replace_mac[0],replace_mac[1],replace_mac[2],replace_mac[3],replace_mac[4],replace_mac[5]);
267                                 memcpy(mac, replace_mac, 6);
268                                 return 1;
269                         }
270                 }
271         }
272         else if (icmphdr->icmp6_type == NDISC_REDIRECT) {
273                 if (len >= 40) {
274                         mac = scan_tlv(&data[40], len-40, 2, 1);
275                         if (mac) {
276                                 _DEBUG_INFO("Redirect,  replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",
277                                         mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],
278                                         replace_mac[0],replace_mac[1],replace_mac[2],replace_mac[3],replace_mac[4],replace_mac[5]);
279                                 memcpy(mac, replace_mac, 6);
280                                 return 1;
281                         }
282                 }
283         }
284         return 0;
285 }
286
287 static void convert_ipv6_mac_to_mc(struct sk_buff *skb)
288 {
289         struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
290         unsigned char *dst_mac = skb->data;
291
292         /* dst_mac[0] = 0xff; */
293         /* dst_mac[1] = 0xff; */
294         /*modified by qinjunjie,ipv6 multicast address ix 0x33-33-xx-xx-xx-xx*/
295         dst_mac[0] = 0x33;
296         dst_mac[1] = 0x33;
297         memcpy(&dst_mac[2], &iph->daddr.s6_addr32[3], 4);
298         #if defined(__LINUX_2_6__)
299         /*modified by qinjunjie,warning:should not remove next line*/
300         skb->pkt_type = PACKET_MULTICAST;
301         #endif
302 }
303 #endif /* CL_IPV6_PASS */
304
305 static __inline__ int __nat25_network_hash(unsigned char *networkAddr)
306 {
307         if(networkAddr[0] == NAT25_IPV4)
308         {
309                 unsigned long x;
310
311                 x = networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
312
313                 return x & (NAT25_HASH_SIZE - 1);
314         }
315         else if(networkAddr[0] == NAT25_IPX)
316         {
317                 unsigned long x;
318
319                 x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
320                         networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
321
322                 return x & (NAT25_HASH_SIZE - 1);
323         }
324         else if(networkAddr[0] == NAT25_APPLE)
325         {
326                 unsigned long x;
327
328                 x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3];
329
330                 return x & (NAT25_HASH_SIZE - 1);
331         }
332         else if(networkAddr[0] == NAT25_PPPOE)
333         {
334                 unsigned long x;
335
336                 x = networkAddr[0] ^ networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^ networkAddr[6] ^ networkAddr[7] ^ networkAddr[8];
337
338                 return x & (NAT25_HASH_SIZE - 1);
339         }
340 #ifdef CL_IPV6_PASS
341         else if(networkAddr[0] == NAT25_IPV6)
342         {
343                 unsigned long x;
344
345                 x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
346                         networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10] ^
347                         networkAddr[11] ^ networkAddr[12] ^ networkAddr[13] ^ networkAddr[14] ^ networkAddr[15] ^
348                         networkAddr[16];
349
350                 return x & (NAT25_HASH_SIZE - 1);
351         }
352 #endif
353         else
354         {
355                 unsigned long x = 0;
356                 int i;
357
358                 for (i=0; i<MAX_NETWORK_ADDR_LEN; i++)
359                         x ^= networkAddr[i];
360
361                 return x & (NAT25_HASH_SIZE - 1);
362         }
363 }
364
365 static __inline__ void __network_hash_link(_adapter *priv,
366                                 struct nat25_network_db_entry *ent, int hash)
367 {
368         /*  Caller must _enter_critical_bh already! */
369         /* _irqL irqL; */
370         /* spin_lock_bh(&priv->br_ext_lock); */
371
372         ent->next_hash = priv->nethash[hash];
373         if(ent->next_hash != NULL)
374                 ent->next_hash->pprev_hash = &ent->next_hash;
375         priv->nethash[hash] = ent;
376         ent->pprev_hash = &priv->nethash[hash];
377
378         /* spin_unlock_bh(&priv->br_ext_lock); */
379 }
380
381 static __inline__ void __network_hash_unlink(struct nat25_network_db_entry *ent)
382 {
383         /*  Caller must _enter_critical_bh already! */
384         /* _irqL irqL; */
385         /* spin_lock_bh(&priv->br_ext_lock); */
386
387         *(ent->pprev_hash) = ent->next_hash;
388         if(ent->next_hash != NULL)
389                 ent->next_hash->pprev_hash = ent->pprev_hash;
390         ent->next_hash = NULL;
391         ent->pprev_hash = NULL;
392
393         /* spin_unlock_bh(&priv->br_ext_lock); */
394 }
395
396 static int __nat25_db_network_lookup_and_replace(_adapter *priv,
397                                 struct sk_buff *skb, unsigned char *networkAddr)
398 {
399         struct nat25_network_db_entry *db;
400         spin_lock_bh(&priv->br_ext_lock);
401
402         db = priv->nethash[__nat25_network_hash(networkAddr)];
403         while (db != NULL)
404         {
405                 if(!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN))
406                 {
407                         if(!__nat25_has_expired(priv, db))
408                         {
409                                 /*  replace the destination mac address */
410                                 memcpy(skb->data, db->macAddr, ETH_ALEN);
411                                 atomic_inc(&db->use_count);
412
413 #ifdef CL_IPV6_PASS
414                                 DEBUG_INFO("NAT25: Lookup M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
415                                                         "%02x%02x%02x%02x%02x%02x\n",
416                                         db->macAddr[0],
417                                         db->macAddr[1],
418                                         db->macAddr[2],
419                                         db->macAddr[3],
420                                         db->macAddr[4],
421                                         db->macAddr[5],
422                                         db->networkAddr[0],
423                                         db->networkAddr[1],
424                                         db->networkAddr[2],
425                                         db->networkAddr[3],
426                                         db->networkAddr[4],
427                                         db->networkAddr[5],
428                                         db->networkAddr[6],
429                                         db->networkAddr[7],
430                                         db->networkAddr[8],
431                                         db->networkAddr[9],
432                                         db->networkAddr[10],
433                                         db->networkAddr[11],
434                                         db->networkAddr[12],
435                                         db->networkAddr[13],
436                                         db->networkAddr[14],
437                                         db->networkAddr[15],
438                                         db->networkAddr[16]);
439 #else
440                                 DEBUG_INFO("NAT25: Lookup M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
441                                         db->macAddr[0],
442                                         db->macAddr[1],
443                                         db->macAddr[2],
444                                         db->macAddr[3],
445                                         db->macAddr[4],
446                                         db->macAddr[5],
447                                         db->networkAddr[0],
448                                         db->networkAddr[1],
449                                         db->networkAddr[2],
450                                         db->networkAddr[3],
451                                         db->networkAddr[4],
452                                         db->networkAddr[5],
453                                         db->networkAddr[6],
454                                         db->networkAddr[7],
455                                         db->networkAddr[8],
456                                         db->networkAddr[9],
457                                         db->networkAddr[10]);
458 #endif
459                         }
460                         spin_unlock_bh(&priv->br_ext_lock);
461                         return 1;
462                 }
463
464                 db = db->next_hash;
465         }
466
467         spin_unlock_bh(&priv->br_ext_lock);
468         return 0;
469 }
470
471 static void __nat25_db_network_insert(_adapter *priv,
472                                 unsigned char *macAddr, unsigned char *networkAddr)
473 {
474         struct nat25_network_db_entry *db;
475         int hash;
476         spin_lock_bh(&priv->br_ext_lock);
477
478         hash = __nat25_network_hash(networkAddr);
479         db = priv->nethash[hash];
480         while (db != NULL)
481         {
482                 if(!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN))
483                 {
484                         memcpy(db->macAddr, macAddr, ETH_ALEN);
485                         db->ageing_timer = jiffies;
486                         spin_unlock_bh(&priv->br_ext_lock);
487                         return;
488                 }
489
490                 db = db->next_hash;
491         }
492
493         db = (struct nat25_network_db_entry *) rtw_malloc(sizeof(*db));
494         if(db == NULL) {
495                 spin_unlock_bh(&priv->br_ext_lock);
496                 return;
497         }
498
499         memcpy(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN);
500         memcpy(db->macAddr, macAddr, ETH_ALEN);
501         atomic_set(&db->use_count, 1);
502         db->ageing_timer = jiffies;
503
504         __network_hash_link(priv, db, hash);
505
506         spin_unlock_bh(&priv->br_ext_lock);
507 }
508
509 static void __nat25_db_print(_adapter *priv)
510 {
511         spin_lock_bh(&priv->br_ext_lock);
512
513 #ifdef BR_EXT_DEBUG
514         static int counter = 0;
515         int i, j;
516         struct nat25_network_db_entry *db;
517
518         counter++;
519         if((counter % 16) != 0)
520                 return;
521
522         for(i=0, j=0; i<NAT25_HASH_SIZE; i++)
523         {
524                 db = priv->nethash[i];
525
526                 while (db != NULL)
527                 {
528 #ifdef CL_IPV6_PASS
529                         panic_printk("NAT25: DB(%d) H(%02d) C(%d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
530                                         "%02x%02x%02x%02x%02x%02x\n",
531                                 j,
532                                 i,
533                                 atomic_read(&db->use_count),
534                                 db->macAddr[0],
535                                 db->macAddr[1],
536                                 db->macAddr[2],
537                                 db->macAddr[3],
538                                 db->macAddr[4],
539                                 db->macAddr[5],
540                                 db->networkAddr[0],
541                                 db->networkAddr[1],
542                                 db->networkAddr[2],
543                                 db->networkAddr[3],
544                                 db->networkAddr[4],
545                                 db->networkAddr[5],
546                                 db->networkAddr[6],
547                                 db->networkAddr[7],
548                                 db->networkAddr[8],
549                                 db->networkAddr[9],
550                                 db->networkAddr[10],
551                                 db->networkAddr[11],
552                                 db->networkAddr[12],
553                                 db->networkAddr[13],
554                                 db->networkAddr[14],
555                                 db->networkAddr[15],
556                                 db->networkAddr[16]);
557 #else
558                         panic_printk("NAT25: DB(%d) H(%02d) C(%d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
559                                 j,
560                                 i,
561                                 atomic_read(&db->use_count),
562                                 db->macAddr[0],
563                                 db->macAddr[1],
564                                 db->macAddr[2],
565                                 db->macAddr[3],
566                                 db->macAddr[4],
567                                 db->macAddr[5],
568                                 db->networkAddr[0],
569                                 db->networkAddr[1],
570                                 db->networkAddr[2],
571                                 db->networkAddr[3],
572                                 db->networkAddr[4],
573                                 db->networkAddr[5],
574                                 db->networkAddr[6],
575                                 db->networkAddr[7],
576                                 db->networkAddr[8],
577                                 db->networkAddr[9],
578                                 db->networkAddr[10]);
579 #endif
580                         j++;
581
582                         db = db->next_hash;
583                 }
584         }
585 #endif
586
587         spin_unlock_bh(&priv->br_ext_lock);
588 }
589
590
591
592 /*
593  *      NAT2.5 interface
594  */
595
596 void nat25_db_cleanup(_adapter *priv)
597 {
598         int i;
599         spin_lock_bh(&priv->br_ext_lock);
600
601         for(i=0; i<NAT25_HASH_SIZE; i++)
602         {
603                 struct nat25_network_db_entry *f;
604                 f = priv->nethash[i];
605                 while (f != NULL) {
606                         struct nat25_network_db_entry *g;
607
608                         g = f->next_hash;
609                         if(priv->scdb_entry == f)
610                         {
611                                 memset(priv->scdb_mac, 0, ETH_ALEN);
612                                 memset(priv->scdb_ip, 0, 4);
613                                 priv->scdb_entry = NULL;
614                         }
615                         __network_hash_unlink(f);
616                         rtw_mfree((u8 *) f, sizeof(struct nat25_network_db_entry));
617
618                         f = g;
619                 }
620         }
621
622         spin_unlock_bh(&priv->br_ext_lock);
623 }
624
625 void nat25_db_expire(_adapter *priv)
626 {
627         int i;
628         spin_lock_bh(&priv->br_ext_lock);
629
630         /* if(!priv->ethBrExtInfo.nat25_disable) */
631         {
632                 for (i=0; i<NAT25_HASH_SIZE; i++)
633                 {
634                         struct nat25_network_db_entry *f;
635                         f = priv->nethash[i];
636
637                         while (f != NULL)
638                         {
639                                 struct nat25_network_db_entry *g;
640                                 g = f->next_hash;
641
642                                 if(__nat25_has_expired(priv, f))
643                                 {
644                                         if(atomic_dec_and_test(&f->use_count))
645                                         {
646 #ifdef BR_EXT_DEBUG
647 #ifdef CL_IPV6_PASS
648                                                 panic_printk("NAT25 Expire H(%02d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
649                                                                 "%02x%02x%02x%02x%02x%02x\n",
650                                                         i,
651                                                         f->macAddr[0],
652                                                         f->macAddr[1],
653                                                         f->macAddr[2],
654                                                         f->macAddr[3],
655                                                         f->macAddr[4],
656                                                         f->macAddr[5],
657                                                         f->networkAddr[0],
658                                                         f->networkAddr[1],
659                                                         f->networkAddr[2],
660                                                         f->networkAddr[3],
661                                                         f->networkAddr[4],
662                                                         f->networkAddr[5],
663                                                         f->networkAddr[6],
664                                                         f->networkAddr[7],
665                                                         f->networkAddr[8],
666                                                         f->networkAddr[9],
667                                                         f->networkAddr[10],
668                                                         f->networkAddr[11],
669                                                         f->networkAddr[12],
670                                                         f->networkAddr[13],
671                                                         f->networkAddr[14],
672                                                         f->networkAddr[15],
673                                                         f->networkAddr[16]);
674 #else
675
676                                                 panic_printk("NAT25 Expire H(%02d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
677                                                         i,
678                                                         f->macAddr[0],
679                                                         f->macAddr[1],
680                                                         f->macAddr[2],
681                                                         f->macAddr[3],
682                                                         f->macAddr[4],
683                                                         f->macAddr[5],
684                                                         f->networkAddr[0],
685                                                         f->networkAddr[1],
686                                                         f->networkAddr[2],
687                                                         f->networkAddr[3],
688                                                         f->networkAddr[4],
689                                                         f->networkAddr[5],
690                                                         f->networkAddr[6],
691                                                         f->networkAddr[7],
692                                                         f->networkAddr[8],
693                                                         f->networkAddr[9],
694                                                         f->networkAddr[10]);
695 #endif
696 #endif
697                                                 if(priv->scdb_entry == f)
698                                                 {
699                                                         memset(priv->scdb_mac, 0, ETH_ALEN);
700                                                         memset(priv->scdb_ip, 0, 4);
701                                                         priv->scdb_entry = NULL;
702                                                 }
703                                                 __network_hash_unlink(f);
704                                                 rtw_mfree((u8 *) f, sizeof(struct nat25_network_db_entry));
705                                         }
706                                 }
707
708                                 f = g;
709                         }
710                 }
711         }
712
713         spin_unlock_bh(&priv->br_ext_lock);
714 }
715
716 #ifdef SUPPORT_TX_MCAST2UNI
717 static int checkIPMcAndReplace(_adapter *priv, struct sk_buff *skb, unsigned int *dst_ip)
718 {
719         struct stat_info        *pstat;
720         struct list_head        *phead, *plist;
721         int i;
722
723         phead = &priv->asoc_list;
724         plist = phead->next;
725
726         while (plist != phead) {
727                 pstat = list_entry(plist, struct stat_info, asoc_list);
728                 plist = plist->next;
729
730                 if (pstat->ipmc_num == 0)
731                         continue;
732
733                 for (i=0; i<MAX_IP_MC_ENTRY; i++) {
734                         if (pstat->ipmc[i].used && !memcmp(&pstat->ipmc[i].mcmac[3], ((unsigned char *)dst_ip)+1, 3)) {
735                                 memcpy(skb->data, pstat->ipmc[i].mcmac, ETH_ALEN);
736                                 return 1;
737                         }
738                 }
739         }
740         return 0;
741 }
742 #endif
743
744 int nat25_db_handle(_adapter *priv, struct sk_buff *skb, int method)
745 {
746         unsigned short protocol;
747         unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
748
749         if(skb == NULL)
750                 return -1;
751
752         if((method <= NAT25_MIN) || (method >= NAT25_MAX))
753                 return -1;
754
755         protocol = *((unsigned short *)(skb->data + 2 * ETH_ALEN));
756
757         /*---------------------------------------------------*/
758         /*                 Handle IP frame                   */
759         /*---------------------------------------------------*/
760         if(protocol == __constant_htons(ETH_P_IP))
761         {
762                 struct iphdr* iph = (struct iphdr *)(skb->data + ETH_HLEN);
763
764                 if(((unsigned char*)(iph) + (iph->ihl<<2)) >= (skb->data + ETH_HLEN + skb->len))
765                 {
766                         DEBUG_WARN("NAT25: malformed IP packet !\n");
767                         return -1;
768                 }
769
770                 switch(method)
771                 {
772                         case NAT25_CHECK:
773                                 return -1;
774
775                         case NAT25_INSERT:
776                                 {
777                                         /* some muticast with source IP is all zero, maybe other case is illegal */
778                                         /* in class A, B, C, host address is all zero or all one is illegal */
779                                         if (iph->saddr == 0)
780                                                 return 0;
781                                         DEBUG_INFO("NAT25: Insert IP, SA=%08x, DA=%08x\n", iph->saddr, iph->daddr);
782                                         __nat25_generate_ipv4_network_addr(networkAddr, &iph->saddr);
783                                         /* record source IP address and , source mac address into db */
784                                         __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
785
786                                         __nat25_db_print(priv);
787                                 }
788                                 return 0;
789
790                         case NAT25_LOOKUP:
791                                 {
792                                         DEBUG_INFO("NAT25: Lookup IP, SA=%08x, DA=%08x\n", iph->saddr, iph->daddr);
793 #ifdef SUPPORT_TX_MCAST2UNI
794                                         if (priv->pshare->rf_ft_var.mc2u_disable ||
795                                                         ((((OPMODE & (WIFI_STATION_STATE|WIFI_ASOC_STATE))
796                                                         == (WIFI_STATION_STATE|WIFI_ASOC_STATE)) &&
797                                                         !checkIPMcAndReplace(priv, skb, &iph->daddr)) ||
798                                                         (OPMODE & WIFI_ADHOC_STATE)))
799 #endif
800                                         {
801                                                 __nat25_generate_ipv4_network_addr(networkAddr, &iph->daddr);
802
803                                                 if (!__nat25_db_network_lookup_and_replace(priv, skb, networkAddr)) {
804                                                         if (*((unsigned char *)&iph->daddr + 3) == 0xff) {
805                                                                 /*  L2 is unicast but L3 is broadcast, make L2 bacome broadcast */
806                                                                 DEBUG_INFO("NAT25: Set DA as boardcast\n");
807                                                                 memset(skb->data, 0xff, ETH_ALEN);
808                                                         }
809                                                         else {
810                                                                 /*  forward unknow IP packet to upper TCP/IP */
811                                                                 DEBUG_INFO("NAT25: Replace DA with BR's MAC\n");
812                                                                 if ( (*(u32 *)priv->br_mac) == 0 && (*(u16 *)(priv->br_mac+4)) == 0 ) {
813                                                                         void netdev_br_init(struct net_device *netdev);
814                                                                         printk("Re-init netdev_br_init() due to br_mac==0!\n");
815                                                                         netdev_br_init(priv->pnetdev);
816                                                                 }
817                                                                 memcpy(skb->data, priv->br_mac, ETH_ALEN);
818                                                         }
819                                                 }
820                                         }
821                                 }
822                                 return 0;
823
824                         default:
825                                 return -1;
826                 }
827         }
828
829         /*---------------------------------------------------*/
830         /*                 Handle ARP frame                  */
831         /*---------------------------------------------------*/
832         else if(protocol == __constant_htons(ETH_P_ARP))
833         {
834                 struct arphdr *arp = (struct arphdr *)(skb->data + ETH_HLEN);
835                 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
836                 unsigned int *sender, *target;
837
838                 if(arp->ar_pro != __constant_htons(ETH_P_IP))
839                 {
840                         DEBUG_WARN("NAT25: arp protocol unknown (%4x)!\n", htons(arp->ar_pro));
841                         return -1;
842                 }
843
844                 switch(method)
845                 {
846                         case NAT25_CHECK:
847                                 return 0;       /*  skb_copy for all ARP frame */
848
849                         case NAT25_INSERT:
850                                 {
851                                         DEBUG_INFO("NAT25: Insert ARP, MAC=%02x%02x%02x%02x%02x%02x\n", arp_ptr[0],
852                                                 arp_ptr[1], arp_ptr[2], arp_ptr[3], arp_ptr[4], arp_ptr[5]);
853
854                                         /*  change to ARP sender mac address to wlan STA address */
855                                         memcpy(arp_ptr, GET_MY_HWADDR(priv), ETH_ALEN);
856
857                                         arp_ptr += arp->ar_hln;
858                                         sender = (unsigned int *)arp_ptr;
859
860                                         __nat25_generate_ipv4_network_addr(networkAddr, sender);
861
862                                         __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
863
864                                         __nat25_db_print(priv);
865                                 }
866                                 return 0;
867
868                         case NAT25_LOOKUP:
869                                 {
870                                         DEBUG_INFO("NAT25: Lookup ARP\n");
871
872                                         arp_ptr += arp->ar_hln;
873                                         sender = (unsigned int *)arp_ptr;
874                                         arp_ptr += (arp->ar_hln + arp->ar_pln);
875                                         target = (unsigned int *)arp_ptr;
876
877                                         __nat25_generate_ipv4_network_addr(networkAddr, target);
878
879                                         __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
880
881                                         /*  change to ARP target mac address to Lookup result */
882                                         arp_ptr = (unsigned char *)(arp + 1);
883                                         arp_ptr += (arp->ar_hln + arp->ar_pln);
884                                         memcpy(arp_ptr, skb->data, ETH_ALEN);
885                                 }
886                                 return 0;
887
888                         default:
889                                 return -1;
890                 }
891         }
892
893         /*---------------------------------------------------*/
894         /*         Handle IPX and Apple Talk frame           */
895         /*---------------------------------------------------*/
896         else if((protocol == __constant_htons(ETH_P_IPX)) ||
897                 (protocol <= __constant_htons(ETH_FRAME_LEN)))
898         {
899                 unsigned char ipx_header[2] = {0xFF, 0xFF};
900                 struct ipxhdr   *ipx = NULL;
901                 struct elapaarp *ea = NULL;
902                 struct ddpehdr  *ddp = NULL;
903                 unsigned char *framePtr = skb->data + ETH_HLEN;
904
905                 if(protocol == __constant_htons(ETH_P_IPX))
906                 {
907                         DEBUG_INFO("NAT25: Protocol=IPX (Ethernet II)\n");
908                         ipx = (struct ipxhdr *)framePtr;
909                 }
910                 else if(protocol <= __constant_htons(ETH_FRAME_LEN))
911                 {
912                         if(!memcmp(ipx_header, framePtr, 2))
913                         {
914                                 DEBUG_INFO("NAT25: Protocol=IPX (Ethernet 802.3)\n");
915                                 ipx = (struct ipxhdr *)framePtr;
916                         }
917                         else
918                         {
919                                 unsigned char ipx_8022_type =  0xE0;
920                                 unsigned char snap_8022_type = 0xAA;
921
922                                 if(*framePtr == snap_8022_type)
923                                 {
924                                         unsigned char ipx_snap_id[5] = {0x0, 0x0, 0x0, 0x81, 0x37};             /*  IPX SNAP ID */
925                                         unsigned char aarp_snap_id[5] = {0x00, 0x00, 0x00, 0x80, 0xF3}; /*  Apple Talk AARP SNAP ID */
926                                         unsigned char ddp_snap_id[5] = {0x08, 0x00, 0x07, 0x80, 0x9B};  /*  Apple Talk DDP SNAP ID */
927
928                                         framePtr += 3;  /*  eliminate the 802.2 header */
929
930                                         if(!memcmp(ipx_snap_id, framePtr, 5))
931                                         {
932                                                 framePtr += 5;  /*  eliminate the SNAP header */
933
934                                                 DEBUG_INFO("NAT25: Protocol=IPX (Ethernet SNAP)\n");
935                                                 ipx = (struct ipxhdr *)framePtr;
936                                         }
937                                         else if(!memcmp(aarp_snap_id, framePtr, 5))
938                                         {
939                                                 framePtr += 5;  /*  eliminate the SNAP header */
940
941                                                 ea = (struct elapaarp *)framePtr;
942                                         }
943                                         else if(!memcmp(ddp_snap_id, framePtr, 5))
944                                         {
945                                                 framePtr += 5;  /*  eliminate the SNAP header */
946
947                                                 ddp = (struct ddpehdr *)framePtr;
948                                         }
949                                         else
950                                         {
951                                                 DEBUG_WARN("NAT25: Protocol=Ethernet SNAP %02x%02x%02x%02x%02x\n", framePtr[0],
952                                                         framePtr[1], framePtr[2], framePtr[3], framePtr[4]);
953                                                 return -1;
954                                         }
955                                 }
956                                 else if(*framePtr == ipx_8022_type)
957                                 {
958                                         framePtr += 3;  /*  eliminate the 802.2 header */
959
960                                         if(!memcmp(ipx_header, framePtr, 2))
961                                         {
962                                                 DEBUG_INFO("NAT25: Protocol=IPX (Ethernet 802.2)\n");
963                                                 ipx = (struct ipxhdr *)framePtr;
964                                         }
965                                         else
966                                                 return -1;
967                                 }
968                                 else
969                                         return -1;
970                         }
971                 }
972                 else
973                         return -1;
974
975                 /*   IPX   */
976                 if(ipx != NULL)
977                 {
978                         switch(method)
979                         {
980                                 case NAT25_CHECK:
981                                         if(!memcmp(skb->data+ETH_ALEN, ipx->ipx_source.node, ETH_ALEN))
982                                         {
983                                                 DEBUG_INFO("NAT25: Check IPX skb_copy\n");
984                                                 return 0;
985                                         }
986                                         return -1;
987
988                                 case NAT25_INSERT:
989                                         {
990                                                 DEBUG_INFO("NAT25: Insert IPX, Dest=%08x,%02x%02x%02x%02x%02x%02x,%04x Source=%08x,%02x%02x%02x%02x%02x%02x,%04x\n",
991                                                         ipx->ipx_dest.net,
992                                                         ipx->ipx_dest.node[0],
993                                                         ipx->ipx_dest.node[1],
994                                                         ipx->ipx_dest.node[2],
995                                                         ipx->ipx_dest.node[3],
996                                                         ipx->ipx_dest.node[4],
997                                                         ipx->ipx_dest.node[5],
998                                                         ipx->ipx_dest.sock,
999                                                         ipx->ipx_source.net,
1000                                                         ipx->ipx_source.node[0],
1001                                                         ipx->ipx_source.node[1],
1002                                                         ipx->ipx_source.node[2],
1003                                                         ipx->ipx_source.node[3],
1004                                                         ipx->ipx_source.node[4],
1005                                                         ipx->ipx_source.node[5],
1006                                                         ipx->ipx_source.sock);
1007
1008                                                 if(!memcmp(skb->data+ETH_ALEN, ipx->ipx_source.node, ETH_ALEN))
1009                                                 {
1010                                                         DEBUG_INFO("NAT25: Use IPX Net, and Socket as network addr\n");
1011
1012                                                         __nat25_generate_ipx_network_addr_with_socket(networkAddr, &ipx->ipx_source.net, &ipx->ipx_source.sock);
1013
1014                                                         /*  change IPX source node addr to wlan STA address */
1015                                                         memcpy(ipx->ipx_source.node, GET_MY_HWADDR(priv), ETH_ALEN);
1016                                                 }
1017                                                 else
1018                                                 {
1019                                                         __nat25_generate_ipx_network_addr_with_node(networkAddr, &ipx->ipx_source.net, ipx->ipx_source.node);
1020                                                 }
1021
1022                                                 __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
1023
1024                                                 __nat25_db_print(priv);
1025                                         }
1026                                         return 0;
1027
1028                                 case NAT25_LOOKUP:
1029                                         {
1030                                                 if(!memcmp(GET_MY_HWADDR(priv), ipx->ipx_dest.node, ETH_ALEN))
1031                                                 {
1032                                                         DEBUG_INFO("NAT25: Lookup IPX, Modify Destination IPX Node addr\n");
1033
1034                                                         __nat25_generate_ipx_network_addr_with_socket(networkAddr, &ipx->ipx_dest.net, &ipx->ipx_dest.sock);
1035
1036                                                         __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
1037
1038                                                         /*  replace IPX destination node addr with Lookup destination MAC addr */
1039                                                         memcpy(ipx->ipx_dest.node, skb->data, ETH_ALEN);
1040                                                 }
1041                                                 else
1042                                                 {
1043                                                         __nat25_generate_ipx_network_addr_with_node(networkAddr, &ipx->ipx_dest.net, ipx->ipx_dest.node);
1044
1045                                                         __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
1046                                                 }
1047                                         }
1048                                         return 0;
1049
1050                                 default:
1051                                         return -1;
1052                         }
1053                 }
1054
1055                 /*   AARP   */
1056                 else if(ea != NULL)
1057                 {
1058                         /* Sanity check fields. */
1059                         if(ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN)
1060                         {
1061                                 DEBUG_WARN("NAT25: Appletalk AARP Sanity check fail!\n");
1062                                 return -1;
1063                         }
1064
1065                         switch(method)
1066                         {
1067                                 case NAT25_CHECK:
1068                                         return 0;
1069
1070                                 case NAT25_INSERT:
1071                                         {
1072                                                 /*  change to AARP source mac address to wlan STA address */
1073                                                 memcpy(ea->hw_src, GET_MY_HWADDR(priv), ETH_ALEN);
1074
1075                                                 DEBUG_INFO("NAT25: Insert AARP, Source=%d,%d Destination=%d,%d\n",
1076                                                         ea->pa_src_net,
1077                                                         ea->pa_src_node,
1078                                                         ea->pa_dst_net,
1079                                                         ea->pa_dst_node);
1080
1081                                                 __nat25_generate_apple_network_addr(networkAddr, &ea->pa_src_net, &ea->pa_src_node);
1082
1083                                                 __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
1084
1085                                                 __nat25_db_print(priv);
1086                                         }
1087                                         return 0;
1088
1089                                 case NAT25_LOOKUP:
1090                                         {
1091                                                 DEBUG_INFO("NAT25: Lookup AARP, Source=%d,%d Destination=%d,%d\n",
1092                                                         ea->pa_src_net,
1093                                                         ea->pa_src_node,
1094                                                         ea->pa_dst_net,
1095                                                         ea->pa_dst_node);
1096
1097                                                 __nat25_generate_apple_network_addr(networkAddr, &ea->pa_dst_net, &ea->pa_dst_node);
1098
1099                                                 __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
1100
1101                                                 /*  change to AARP destination mac address to Lookup result */
1102                                                 memcpy(ea->hw_dst, skb->data, ETH_ALEN);
1103                                         }
1104                                         return 0;
1105
1106                                 default:
1107                                         return -1;
1108                         }
1109                 }
1110
1111                 /*   DDP   */
1112                 else if(ddp != NULL)
1113                 {
1114                         switch(method)
1115                         {
1116                                 case NAT25_CHECK:
1117                                         return -1;
1118
1119                                 case NAT25_INSERT:
1120                                         {
1121                                                 DEBUG_INFO("NAT25: Insert DDP, Source=%d,%d Destination=%d,%d\n",
1122                                                         ddp->deh_snet,
1123                                                         ddp->deh_snode,
1124                                                         ddp->deh_dnet,
1125                                                         ddp->deh_dnode);
1126
1127                                                 __nat25_generate_apple_network_addr(networkAddr, &ddp->deh_snet, &ddp->deh_snode);
1128
1129                                                 __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
1130
1131                                                 __nat25_db_print(priv);
1132                                         }
1133                                         return 0;
1134
1135                                 case NAT25_LOOKUP:
1136                                         {
1137                                                 DEBUG_INFO("NAT25: Lookup DDP, Source=%d,%d Destination=%d,%d\n",
1138                                                         ddp->deh_snet,
1139                                                         ddp->deh_snode,
1140                                                         ddp->deh_dnet,
1141                                                         ddp->deh_dnode);
1142
1143                                                 __nat25_generate_apple_network_addr(networkAddr, &ddp->deh_dnet, &ddp->deh_dnode);
1144
1145                                                 __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
1146                                         }
1147                                         return 0;
1148
1149                                 default:
1150                                         return -1;
1151                         }
1152                 }
1153
1154                 return -1;
1155         }
1156
1157         /*---------------------------------------------------*/
1158         /*                Handle PPPoE frame                 */
1159         /*---------------------------------------------------*/
1160         else if((protocol == __constant_htons(ETH_P_PPP_DISC)) ||
1161                 (protocol == __constant_htons(ETH_P_PPP_SES)))
1162         {
1163                 struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
1164                 unsigned short *pMagic;
1165
1166                 switch(method)
1167                 {
1168                         case NAT25_CHECK:
1169                                 if (ph->sid == 0)
1170                                         return 0;
1171                                 return 1;
1172
1173                         case NAT25_INSERT:
1174                                 if(ph->sid == 0)        /*  Discovery phase according to tag */
1175                                 {
1176                                         if(ph->code == PADI_CODE || ph->code == PADR_CODE)
1177                                         {
1178                                                 if (priv->ethBrExtInfo.addPPPoETag) {
1179                                                         struct pppoe_tag *tag, *pOldTag;
1180                                                         unsigned char tag_buf[40];
1181                                                         int old_tag_len=0;
1182
1183                                                         tag = (struct pppoe_tag *)tag_buf;
1184                                                         pOldTag = (struct pppoe_tag *)__nat25_find_pppoe_tag(ph, ntohs(PTT_RELAY_SID));
1185                                                         if (pOldTag) { /*  if SID existed, copy old value and delete it */
1186                                                                 old_tag_len = ntohs(pOldTag->tag_len);
1187                                                                 if (old_tag_len+TAG_HDR_LEN+MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN > sizeof(tag_buf)) {
1188                                                                         DEBUG_ERR("SID tag length too long!\n");
1189                                                                         return -1;
1190                                                                 }
1191
1192                                                                 memcpy(tag->tag_data+MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN,
1193                                                                         pOldTag->tag_data, old_tag_len);
1194
1195                                                                 if (skb_pull_and_merge(skb, (unsigned char *)pOldTag, TAG_HDR_LEN+old_tag_len) < 0) {
1196                                                                         DEBUG_ERR("call skb_pull_and_merge() failed in PADI/R packet!\n");
1197                                                                         return -1;
1198                                                                 }
1199                                                                 ph->length = htons(ntohs(ph->length)-TAG_HDR_LEN-old_tag_len);
1200                                                         }
1201
1202                                                         tag->tag_type = PTT_RELAY_SID;
1203                                                         tag->tag_len = htons(MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN+old_tag_len);
1204
1205                                                         /*  insert the magic_code+client mac in relay tag */
1206                                                         pMagic = (unsigned short *)tag->tag_data;
1207                                                         *pMagic = htons(MAGIC_CODE);
1208                                                         memcpy(tag->tag_data+MAGIC_CODE_LEN, skb->data+ETH_ALEN, ETH_ALEN);
1209
1210                                                         /* Add relay tag */
1211                                                         if(__nat25_add_pppoe_tag(skb, tag) < 0)
1212                                                                 return -1;
1213
1214                                                         DEBUG_INFO("NAT25: Insert PPPoE, forward %s packet\n",
1215                                                                                         (ph->code == PADI_CODE ? "PADI" : "PADR"));
1216                                                 }
1217                                                 else { /*  not add relay tag */
1218                                                         if (priv->pppoe_connection_in_progress &&
1219                                                                         memcmp(skb->data+ETH_ALEN, priv->pppoe_addr, ETH_ALEN))  {
1220                                                                 DEBUG_ERR("Discard PPPoE packet due to another PPPoE connection is in progress!\n");
1221                                                                 return -2;
1222                                                         }
1223
1224                                                         if (priv->pppoe_connection_in_progress == 0)
1225                                                                 memcpy(priv->pppoe_addr, skb->data+ETH_ALEN, ETH_ALEN);
1226
1227                                                         priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;
1228                                                 }
1229                                         }
1230                                         else
1231                                                 return -1;
1232                                 }
1233                                 else    /*  session phase */
1234                                 {
1235                                                 DEBUG_INFO("NAT25: Insert PPPoE, insert session packet to %s\n", skb->dev->name);
1236
1237                                                 __nat25_generate_pppoe_network_addr(networkAddr, skb->data, &(ph->sid));
1238
1239                                                 __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
1240
1241                                                 __nat25_db_print(priv);
1242
1243                                                 if (!priv->ethBrExtInfo.addPPPoETag &&
1244                                                                 priv->pppoe_connection_in_progress &&
1245                                                                         !memcmp(skb->data+ETH_ALEN, priv->pppoe_addr, ETH_ALEN))
1246                                                         priv->pppoe_connection_in_progress = 0;
1247                                 }
1248                                 return 0;
1249
1250                         case NAT25_LOOKUP:
1251                                 if(ph->code == PADO_CODE || ph->code == PADS_CODE)
1252                                 {
1253                                         if (priv->ethBrExtInfo.addPPPoETag) {
1254                                                 struct pppoe_tag *tag;
1255                                                 unsigned char *ptr;
1256                                                 unsigned short tagType, tagLen;
1257                                                 int offset=0;
1258
1259                                                 if((ptr = __nat25_find_pppoe_tag(ph, ntohs(PTT_RELAY_SID))) == 0) {
1260                                                         DEBUG_ERR("Fail to find PTT_RELAY_SID in FADO!\n");
1261                                                         return -1;
1262                                                 }
1263
1264                                                 tag = (struct pppoe_tag *)ptr;
1265                                                 tagType = (unsigned short)((ptr[0] << 8) + ptr[1]);
1266                                                 tagLen = (unsigned short)((ptr[2] << 8) + ptr[3]);
1267
1268                                                 if((tagType != ntohs(PTT_RELAY_SID)) || (tagLen < (MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN))) {
1269                                                         DEBUG_ERR("Invalid PTT_RELAY_SID tag length [%d]!\n", tagLen);
1270                                                         return -1;
1271                                                 }
1272
1273                                                 pMagic = (unsigned short *)tag->tag_data;
1274                                                 if (ntohs(*pMagic) != MAGIC_CODE) {
1275                                                         DEBUG_ERR("Can't find MAGIC_CODE in %s packet!\n",
1276                                                                 (ph->code == PADO_CODE ? "PADO" : "PADS"));
1277                                                         return -1;
1278                                                 }
1279
1280                                                 memcpy(skb->data, tag->tag_data+MAGIC_CODE_LEN, ETH_ALEN);
1281
1282                                                 if (tagLen > MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN)
1283                                                         offset = TAG_HDR_LEN;
1284
1285                                                 if (skb_pull_and_merge(skb, ptr+offset, TAG_HDR_LEN+MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN-offset) < 0) {
1286                                                         DEBUG_ERR("call skb_pull_and_merge() failed in PADO packet!\n");
1287                                                         return -1;
1288                                                 }
1289                                                 ph->length = htons(ntohs(ph->length)-(TAG_HDR_LEN+MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN-offset));
1290                                                 if (offset > 0)
1291                                                         tag->tag_len = htons(tagLen-MAGIC_CODE_LEN-RTL_RELAY_TAG_LEN);
1292
1293                                                 DEBUG_INFO("NAT25: Lookup PPPoE, forward %s Packet from %s\n",
1294                                                         (ph->code == PADO_CODE ? "PADO" : "PADS"),      skb->dev->name);
1295                                         }
1296                                         else { /*  not add relay tag */
1297                                                 if (!priv->pppoe_connection_in_progress) {
1298                                                         DEBUG_ERR("Discard PPPoE packet due to no connection in progresss!\n");
1299                                                         return -1;
1300                                                 }
1301                                                 memcpy(skb->data, priv->pppoe_addr, ETH_ALEN);
1302                                                 priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;
1303                                         }
1304                                 }
1305                                 else {
1306                                         if(ph->sid != 0)
1307                                         {
1308                                                 DEBUG_INFO("NAT25: Lookup PPPoE, lookup session packet from %s\n", skb->dev->name);
1309                                                 __nat25_generate_pppoe_network_addr(networkAddr, skb->data+ETH_ALEN, &(ph->sid));
1310
1311                                                 __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
1312
1313                                                 __nat25_db_print(priv);
1314                                         }
1315                                         else
1316                                                 return -1;
1317
1318                                 }
1319                                 return 0;
1320
1321                         default:
1322                                 return -1;
1323                 }
1324         }
1325
1326         /*---------------------------------------------------*/
1327         /*                 Handle EAP frame                  */
1328         /*---------------------------------------------------*/
1329         else if(protocol == __constant_htons(0x888e))
1330         {
1331                 switch(method)
1332                 {
1333                         case NAT25_CHECK:
1334                                 return -1;
1335
1336                         case NAT25_INSERT:
1337                                 return 0;
1338
1339                         case NAT25_LOOKUP:
1340                                 return 0;
1341
1342                         default:
1343                                 return -1;
1344                 }
1345         }
1346
1347         /*---------------------------------------------------*/
1348         /*         Handle C-Media proprietary frame          */
1349         /*---------------------------------------------------*/
1350         else if((protocol == __constant_htons(0xe2ae)) ||
1351                 (protocol == __constant_htons(0xe2af)))
1352         {
1353                 switch(method)
1354                 {
1355                         case NAT25_CHECK:
1356                                 return -1;
1357
1358                         case NAT25_INSERT:
1359                                 return 0;
1360
1361                         case NAT25_LOOKUP:
1362                                 return 0;
1363
1364                         default:
1365                                 return -1;
1366                 }
1367         }
1368
1369         /*---------------------------------------------------*/
1370         /*         Handle IPV6 frame                                                              */
1371         /*---------------------------------------------------*/
1372 #ifdef CL_IPV6_PASS
1373         else if(protocol == __constant_htons(ETH_P_IPV6))
1374         {
1375                 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
1376
1377                 if (sizeof(*iph) >= (skb->len - ETH_HLEN))
1378                 {
1379                         DEBUG_WARN("NAT25: malformed IPv6 packet !\n");
1380                         return -1;
1381                 }
1382
1383                 switch(method)
1384                 {
1385                         case NAT25_CHECK:
1386                                 if (skb->data[0] & 1)
1387                                         return 0;
1388                                 return -1;
1389
1390                         case NAT25_INSERT:
1391                                 {
1392                                         DEBUG_INFO("NAT25: Insert IP, SA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x,"
1393                                                                         " DA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x\n",
1394                                                 iph->saddr.s6_addr16[0],iph->saddr.s6_addr16[1],iph->saddr.s6_addr16[2],iph->saddr.s6_addr16[3],
1395                                                 iph->saddr.s6_addr16[4],iph->saddr.s6_addr16[5],iph->saddr.s6_addr16[6],iph->saddr.s6_addr16[7],
1396                                                 iph->daddr.s6_addr16[0],iph->daddr.s6_addr16[1],iph->daddr.s6_addr16[2],iph->daddr.s6_addr16[3],
1397                                                 iph->daddr.s6_addr16[4],iph->daddr.s6_addr16[5],iph->daddr.s6_addr16[6],iph->daddr.s6_addr16[7]);
1398
1399                                         if (memcmp(&iph->saddr, "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 16)) {
1400                                                 __nat25_generate_ipv6_network_addr(networkAddr, (unsigned int *)&iph->saddr);
1401                                                 __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
1402                                                 __nat25_db_print(priv);
1403
1404                                                 if (iph->nexthdr == IPPROTO_ICMPV6 &&
1405                                                                 skb->len > (ETH_HLEN +  sizeof(*iph) + 4)) {
1406                                                         if (update_nd_link_layer_addr(skb->data + ETH_HLEN + sizeof(*iph),
1407                                                                 skb->len - ETH_HLEN - sizeof(*iph), GET_MY_HWADDR(priv))) {
1408                                                                 struct icmp6hdr  *hdr = (struct icmp6hdr *)(skb->data + ETH_HLEN + sizeof(*iph));
1409                                                                 hdr->icmp6_cksum = 0;
1410                                                                 hdr->icmp6_cksum = csum_ipv6_magic(&iph->saddr, &iph->daddr,
1411                                                                                                 iph->payload_len,
1412                                                                                                 IPPROTO_ICMPV6,
1413                                                                                                 csum_partial((__u8 *)hdr, iph->payload_len, 0));
1414                                                         }
1415                                                 }
1416                                         }
1417                                 }
1418                                 return 0;
1419
1420                         case NAT25_LOOKUP:
1421                                 DEBUG_INFO("NAT25: Lookup IP, SA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x,"
1422                                                                 " DA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x\n",
1423                                                 iph->saddr.s6_addr16[0],iph->saddr.s6_addr16[1],iph->saddr.s6_addr16[2],iph->saddr.s6_addr16[3],
1424                                                 iph->saddr.s6_addr16[4],iph->saddr.s6_addr16[5],iph->saddr.s6_addr16[6],iph->saddr.s6_addr16[7],
1425                                                 iph->daddr.s6_addr16[0],iph->daddr.s6_addr16[1],iph->daddr.s6_addr16[2],iph->daddr.s6_addr16[3],
1426                                                 iph->daddr.s6_addr16[4],iph->daddr.s6_addr16[5],iph->daddr.s6_addr16[6],iph->daddr.s6_addr16[7]);
1427
1428                                 __nat25_generate_ipv6_network_addr(networkAddr, (unsigned int *)&iph->daddr);
1429                                 if (!__nat25_db_network_lookup_and_replace(priv, skb, networkAddr)) {
1430 #ifdef SUPPORT_RX_UNI2MCAST
1431                                         if (iph->daddr.s6_addr[0] == 0xff)
1432                                                 convert_ipv6_mac_to_mc(skb);
1433 #endif
1434                                 }
1435                                 return 0;
1436
1437                         default:
1438                                 return -1;
1439                 }
1440         }
1441 #endif  /*  CL_IPV6_PASS */
1442
1443         return -1;
1444 }
1445
1446 int nat25_handle_frame(_adapter *priv, struct sk_buff *skb)
1447 {
1448 #ifdef BR_EXT_DEBUG
1449         if((!priv->ethBrExtInfo.nat25_disable) && (!(skb->data[0] & 1)))
1450         {
1451                 panic_printk("NAT25: Input Frame: DA=%02x%02x%02x%02x%02x%02x SA=%02x%02x%02x%02x%02x%02x\n",
1452                         skb->data[0],
1453                         skb->data[1],
1454                         skb->data[2],
1455                         skb->data[3],
1456                         skb->data[4],
1457                         skb->data[5],
1458                         skb->data[6],
1459                         skb->data[7],
1460                         skb->data[8],
1461                         skb->data[9],
1462                         skb->data[10],
1463                         skb->data[11]);
1464         }
1465 #endif
1466
1467         if(!(skb->data[0] & 1))
1468         {
1469                 int is_vlan_tag=0, i, retval=0;
1470                 unsigned short vlan_hdr=0;
1471
1472                 if (*((unsigned short *)(skb->data+ETH_ALEN*2)) == __constant_htons(ETH_P_8021Q)) {
1473                         is_vlan_tag = 1;
1474                         vlan_hdr = *((unsigned short *)(skb->data+ETH_ALEN*2+2));
1475                         for (i=0; i<6; i++)
1476                                 *((unsigned short *)(skb->data+ETH_ALEN*2+2-i*2)) = *((unsigned short *)(skb->data+ETH_ALEN*2-2-i*2));
1477                         skb_pull(skb, 4);
1478                 }
1479
1480                 if (!priv->ethBrExtInfo.nat25_disable)
1481                 {
1482                         spin_lock_bh(&priv->br_ext_lock);
1483                         /*
1484                          *      This function look up the destination network address from
1485                          *      the NAT2.5 database. Return value = -1 means that the
1486                          *      corresponding network protocol is NOT support.
1487                          */
1488                         if (!priv->ethBrExtInfo.nat25sc_disable &&
1489                                 (*((unsigned short *)(skb->data+ETH_ALEN*2)) == __constant_htons(ETH_P_IP)) &&
1490                                 !memcmp(priv->scdb_ip, skb->data+ETH_HLEN+16, 4)) {
1491                                 memcpy(skb->data, priv->scdb_mac, ETH_ALEN);
1492
1493                                 spin_unlock_bh(&priv->br_ext_lock);
1494                         }
1495                         else {
1496                                 spin_unlock_bh(&priv->br_ext_lock);
1497
1498                                 retval = nat25_db_handle(priv, skb, NAT25_LOOKUP);
1499                         }
1500                 }
1501                 else {
1502                         if (((*((unsigned short *)(skb->data+ETH_ALEN*2)) == __constant_htons(ETH_P_IP)) &&
1503                                         !memcmp(priv->br_ip, skb->data+ETH_HLEN+16, 4)) ||
1504                                 ((*((unsigned short *)(skb->data+ETH_ALEN*2)) == __constant_htons(ETH_P_ARP)) &&
1505                                         !memcmp(priv->br_ip, skb->data+ETH_HLEN+24, 4))) {
1506                                 /*  for traffic to upper TCP/IP */
1507                                 retval = nat25_db_handle(priv, skb, NAT25_LOOKUP);
1508                         }
1509                 }
1510
1511                 if (is_vlan_tag) {
1512                         skb_push(skb, 4);
1513                         for (i=0; i<6; i++)
1514                                 *((unsigned short *)(skb->data+i*2)) = *((unsigned short *)(skb->data+4+i*2));
1515                         *((unsigned short *)(skb->data+ETH_ALEN*2)) = __constant_htons(ETH_P_8021Q);
1516                         *((unsigned short *)(skb->data+ETH_ALEN*2+2)) = vlan_hdr;
1517                 }
1518
1519                 if(retval == -1) {
1520                         /* DEBUG_ERR("NAT25: Lookup fail!\n"); */
1521                         return -1;
1522                 }
1523         }
1524
1525         return 0;
1526 }
1527
1528 #define SERVER_PORT                     67
1529 #define CLIENT_PORT                     68
1530 #define DHCP_MAGIC                      0x63825363
1531 #define BROADCAST_FLAG          0x8000
1532
1533 struct dhcpMessage {
1534         u_int8_t op;
1535         u_int8_t htype;
1536         u_int8_t hlen;
1537         u_int8_t hops;
1538         u_int32_t xid;
1539         u_int16_t secs;
1540         u_int16_t flags;
1541         u_int32_t ciaddr;
1542         u_int32_t yiaddr;
1543         u_int32_t siaddr;
1544         u_int32_t giaddr;
1545         u_int8_t chaddr[16];
1546         u_int8_t sname[64];
1547         u_int8_t file[128];
1548         u_int32_t cookie;
1549         u_int8_t options[308]; /* 312 - cookie */
1550 };
1551
1552 void dhcp_flag_bcast(_adapter *priv, struct sk_buff *skb)
1553 {
1554         if(skb == NULL)
1555                 return;
1556
1557         if(!priv->ethBrExtInfo.dhcp_bcst_disable)
1558         {
1559                 unsigned short protocol = *((unsigned short *)(skb->data + 2 * ETH_ALEN));
1560
1561                 if(protocol == __constant_htons(ETH_P_IP)) /*  IP */
1562                 {
1563                         struct iphdr* iph = (struct iphdr *)(skb->data + ETH_HLEN);
1564
1565                         if(iph->protocol == IPPROTO_UDP) /*  UDP */
1566                         {
1567                                 struct udphdr *udph = (struct udphdr *)((unsigned long)iph + (iph->ihl << 2));
1568
1569                                 if((udph->source == __constant_htons(CLIENT_PORT))
1570                                         && (udph->dest == __constant_htons(SERVER_PORT))) /*  DHCP request */
1571                                 {
1572                                         struct dhcpMessage *dhcph =
1573                                                 (struct dhcpMessage *)((unsigned long)udph + sizeof(struct udphdr));
1574
1575                                         if(dhcph->cookie == __constant_htonl(DHCP_MAGIC)) /*  match magic word */
1576                                         {
1577                                                 if(!(dhcph->flags & htons(BROADCAST_FLAG))) /*  if not broadcast */
1578                                                 {
1579                                                         register int sum = 0;
1580
1581                                                         DEBUG_INFO("DHCP: change flag of DHCP request to broadcast.\n");
1582                                                         /*  or BROADCAST flag */
1583                                                         dhcph->flags |= htons(BROADCAST_FLAG);
1584                                                         /*  recalculate checksum */
1585                                                         sum = ~(udph->check) & 0xffff;
1586                                                         sum += dhcph->flags;
1587                                                         while(sum >> 16)
1588                                                                 sum = (sum & 0xffff) + (sum >> 16);
1589                                                         udph->check = ~sum;
1590                                                 }
1591                                         }
1592                                 }
1593                         }
1594                 }
1595         }
1596 }
1597
1598 void *scdb_findEntry(_adapter *priv, unsigned char *macAddr,
1599                                 unsigned char *ipAddr)
1600 {
1601         unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
1602         struct nat25_network_db_entry *db;
1603         int hash;
1604         /* spin_lock_bh(&priv->br_ext_lock); */
1605
1606         __nat25_generate_ipv4_network_addr(networkAddr, (unsigned int *)ipAddr);
1607         hash = __nat25_network_hash(networkAddr);
1608         db = priv->nethash[hash];
1609         while (db != NULL)
1610         {
1611                 if(!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
1612                         /* spin_unlock_bh(&priv->br_ext_lock); */
1613                         return (void *)db;
1614                 }
1615
1616                 db = db->next_hash;
1617         }
1618
1619         /* spin_unlock_bh(&priv->br_ext_lock); */
1620         return NULL;
1621 }
1622
1623 #endif  /*  CONFIG_BR_EXT */