1 /******************************************************************************
3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
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.
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
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
19 ******************************************************************************/
20 #define _RTW_BR_EXT_C_
23 #include <linux/if_arp.h>
26 #include <linux/atalk.h>
27 #include <linux/udp.h>
28 #include <linux/if_pppox.h>
32 #include <drv_types.h>
33 #include "rtw_br_ext.h"
37 #include <linux/ipv6.h>
38 #include <linux/icmpv6.h>
39 #include <net/ndisc.h>
40 #include <net/checksum.h>
46 /* define BR_EXT_DEBUG */
51 #define NAT25_APPLE 04
52 #define NAT25_PPPOE 05
54 #define RTL_RELAY_TAG_LEN (ETH_ALEN)
57 #define MAGIC_CODE 0x8186
58 #define MAGIC_CODE_LEN 2
59 #define WAIT_TIME_PPPOE 5 /* waiting time for pppoe server in sec */
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 -----------------------------------------------------------------*/
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)
75 unsigned char *cur_ptr, *start_ptr;
76 unsigned short tagLen, tagType;
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]);
85 cur_ptr = cur_ptr + TAG_HDR_LEN + tagLen;
90 static __inline__ int __nat25_add_pppoe_tag(struct sk_buff *skb, struct pppoe_tag *tag)
92 struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
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");
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);
109 static int skb_pull_and_merge(struct sk_buff *skb, unsigned char *src, int len)
112 unsigned long end, tail;
114 if ((src+len) > skb_tail_pointer(skb) || skb->len < len)
117 tail = (unsigned long)skb_tail_pointer(skb);
118 end = (unsigned long)src+len;
122 tail_len = (int)(tail-end);
124 memmove(src, src+len, tail_len);
126 skb_trim(skb, skb->len-len);
130 static __inline__ unsigned long __nat25_timeout(_adapter *priv)
132 unsigned long timeout;
134 timeout = jiffies - NAT25_AGEING_TIME*HZ;
139 static __inline__ int __nat25_has_expired(_adapter *priv,
140 struct nat25_network_db_entry *fdb)
142 if(time_before_eq(fdb->ageing_timer, __nat25_timeout(priv)))
148 static __inline__ void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr,
149 unsigned int *ipAddr)
151 memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
153 networkAddr[0] = NAT25_IPV4;
154 memcpy(networkAddr+7, (unsigned char *)ipAddr, 4);
157 static __inline__ void __nat25_generate_ipx_network_addr_with_node(unsigned char *networkAddr,
158 unsigned int *ipxNetAddr, unsigned char *ipxNodeAddr)
160 memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
162 networkAddr[0] = NAT25_IPX;
163 memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
164 memcpy(networkAddr+5, ipxNodeAddr, 6);
167 static __inline__ void __nat25_generate_ipx_network_addr_with_socket(unsigned char *networkAddr,
168 unsigned int *ipxNetAddr, unsigned short *ipxSocketAddr)
170 memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
172 networkAddr[0] = NAT25_IPX;
173 memcpy(networkAddr+1, (unsigned char *)ipxNetAddr, 4);
174 memcpy(networkAddr+5, (unsigned char *)ipxSocketAddr, 2);
177 static __inline__ void __nat25_generate_apple_network_addr(unsigned char *networkAddr,
178 unsigned short *network, unsigned char *node)
180 memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
182 networkAddr[0] = NAT25_APPLE;
183 memcpy(networkAddr+1, (unsigned char *)network, 2);
184 networkAddr[3] = *node;
187 static __inline__ void __nat25_generate_pppoe_network_addr(unsigned char *networkAddr,
188 unsigned char *ac_mac, unsigned short *sid)
190 memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
192 networkAddr[0] = NAT25_PPPOE;
193 memcpy(networkAddr+1, (unsigned char *)sid, 2);
194 memcpy(networkAddr+3, (unsigned char *)ac_mac, 6);
198 static void __nat25_generate_ipv6_network_addr(unsigned char *networkAddr,
199 unsigned int *ipAddr)
201 memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
203 networkAddr[0] = NAT25_IPV6;
204 memcpy(networkAddr+1, (unsigned char *)ipAddr, 16);
207 static unsigned char *scan_tlv(unsigned char *data, int len, unsigned char tag, unsigned char len8b)
210 if (*data == tag && *(data+1) == len8b && len >= len8b*8)
213 len -= (*(data+1))*8;
214 data += (*(data+1))*8;
219 static int update_nd_link_layer_addr(unsigned char *data, int len, unsigned char *replace_mac)
221 struct icmp6hdr *icmphdr = (struct icmp6hdr *)data;
224 if (icmphdr->icmp6_type == NDISC_ROUTER_SOLICITATION) {
226 mac = scan_tlv(&data[8], len-8, 1, 1);
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);
236 else if (icmphdr->icmp6_type == NDISC_ROUTER_ADVERTISEMENT) {
238 mac = scan_tlv(&data[16], len-16, 1, 1);
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);
248 else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
250 mac = scan_tlv(&data[24], len-24, 1, 1);
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);
260 else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
262 mac = scan_tlv(&data[24], len-24, 2, 1);
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);
272 else if (icmphdr->icmp6_type == NDISC_REDIRECT) {
274 mac = scan_tlv(&data[40], len-40, 2, 1);
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);
287 static void convert_ipv6_mac_to_mc(struct sk_buff *skb)
289 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
290 unsigned char *dst_mac = skb->data;
292 /* dst_mac[0] = 0xff; */
293 /* dst_mac[1] = 0xff; */
294 /*modified by qinjunjie,ipv6 multicast address ix 0x33-33-xx-xx-xx-xx*/
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;
303 #endif /* CL_IPV6_PASS */
305 static __inline__ int __nat25_network_hash(unsigned char *networkAddr)
307 if(networkAddr[0] == NAT25_IPV4)
311 x = networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
313 return x & (NAT25_HASH_SIZE - 1);
315 else if(networkAddr[0] == NAT25_IPX)
319 x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
320 networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
322 return x & (NAT25_HASH_SIZE - 1);
324 else if(networkAddr[0] == NAT25_APPLE)
328 x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3];
330 return x & (NAT25_HASH_SIZE - 1);
332 else if(networkAddr[0] == NAT25_PPPOE)
336 x = networkAddr[0] ^ networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^ networkAddr[6] ^ networkAddr[7] ^ networkAddr[8];
338 return x & (NAT25_HASH_SIZE - 1);
341 else if(networkAddr[0] == NAT25_IPV6)
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] ^
350 return x & (NAT25_HASH_SIZE - 1);
358 for (i=0; i<MAX_NETWORK_ADDR_LEN; i++)
361 return x & (NAT25_HASH_SIZE - 1);
365 static __inline__ void __network_hash_link(_adapter *priv,
366 struct nat25_network_db_entry *ent, int hash)
368 /* Caller must _enter_critical_bh already! */
370 /* spin_lock_bh(&priv->br_ext_lock); */
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];
378 /* spin_unlock_bh(&priv->br_ext_lock); */
381 static __inline__ void __network_hash_unlink(struct nat25_network_db_entry *ent)
383 /* Caller must _enter_critical_bh already! */
385 /* spin_lock_bh(&priv->br_ext_lock); */
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;
393 /* spin_unlock_bh(&priv->br_ext_lock); */
396 static int __nat25_db_network_lookup_and_replace(_adapter *priv,
397 struct sk_buff *skb, unsigned char *networkAddr)
399 struct nat25_network_db_entry *db;
400 spin_lock_bh(&priv->br_ext_lock);
402 db = priv->nethash[__nat25_network_hash(networkAddr)];
405 if(!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN))
407 if(!__nat25_has_expired(priv, db))
409 /* replace the destination mac address */
410 memcpy(skb->data, db->macAddr, ETH_ALEN);
411 atomic_inc(&db->use_count);
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",
438 db->networkAddr[16]);
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",
457 db->networkAddr[10]);
460 spin_unlock_bh(&priv->br_ext_lock);
467 spin_unlock_bh(&priv->br_ext_lock);
471 static void __nat25_db_network_insert(_adapter *priv,
472 unsigned char *macAddr, unsigned char *networkAddr)
474 struct nat25_network_db_entry *db;
476 spin_lock_bh(&priv->br_ext_lock);
478 hash = __nat25_network_hash(networkAddr);
479 db = priv->nethash[hash];
482 if(!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN))
484 memcpy(db->macAddr, macAddr, ETH_ALEN);
485 db->ageing_timer = jiffies;
486 spin_unlock_bh(&priv->br_ext_lock);
493 db = (struct nat25_network_db_entry *) rtw_malloc(sizeof(*db));
495 spin_unlock_bh(&priv->br_ext_lock);
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;
504 __network_hash_link(priv, db, hash);
506 spin_unlock_bh(&priv->br_ext_lock);
509 static void __nat25_db_print(_adapter *priv)
511 spin_lock_bh(&priv->br_ext_lock);
514 static int counter = 0;
516 struct nat25_network_db_entry *db;
519 if((counter % 16) != 0)
522 for(i=0, j=0; i<NAT25_HASH_SIZE; i++)
524 db = priv->nethash[i];
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",
533 atomic_read(&db->use_count),
556 db->networkAddr[16]);
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",
561 atomic_read(&db->use_count),
578 db->networkAddr[10]);
587 spin_unlock_bh(&priv->br_ext_lock);
596 void nat25_db_cleanup(_adapter *priv)
599 spin_lock_bh(&priv->br_ext_lock);
601 for(i=0; i<NAT25_HASH_SIZE; i++)
603 struct nat25_network_db_entry *f;
604 f = priv->nethash[i];
606 struct nat25_network_db_entry *g;
609 if(priv->scdb_entry == f)
611 memset(priv->scdb_mac, 0, ETH_ALEN);
612 memset(priv->scdb_ip, 0, 4);
613 priv->scdb_entry = NULL;
615 __network_hash_unlink(f);
616 rtw_mfree((u8 *) f, sizeof(struct nat25_network_db_entry));
622 spin_unlock_bh(&priv->br_ext_lock);
625 void nat25_db_expire(_adapter *priv)
628 spin_lock_bh(&priv->br_ext_lock);
630 /* if(!priv->ethBrExtInfo.nat25_disable) */
632 for (i=0; i<NAT25_HASH_SIZE; i++)
634 struct nat25_network_db_entry *f;
635 f = priv->nethash[i];
639 struct nat25_network_db_entry *g;
642 if(__nat25_has_expired(priv, f))
644 if(atomic_dec_and_test(&f->use_count))
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",
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",
697 if(priv->scdb_entry == f)
699 memset(priv->scdb_mac, 0, ETH_ALEN);
700 memset(priv->scdb_ip, 0, 4);
701 priv->scdb_entry = NULL;
703 __network_hash_unlink(f);
704 rtw_mfree((u8 *) f, sizeof(struct nat25_network_db_entry));
713 spin_unlock_bh(&priv->br_ext_lock);
716 #ifdef SUPPORT_TX_MCAST2UNI
717 static int checkIPMcAndReplace(_adapter *priv, struct sk_buff *skb, unsigned int *dst_ip)
719 struct stat_info *pstat;
720 struct list_head *phead, *plist;
723 phead = &priv->asoc_list;
726 while (plist != phead) {
727 pstat = list_entry(plist, struct stat_info, asoc_list);
730 if (pstat->ipmc_num == 0)
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);
744 int nat25_db_handle(_adapter *priv, struct sk_buff *skb, int method)
746 unsigned short protocol;
747 unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
752 if((method <= NAT25_MIN) || (method >= NAT25_MAX))
755 protocol = *((unsigned short *)(skb->data + 2 * ETH_ALEN));
757 /*---------------------------------------------------*/
758 /* Handle IP frame */
759 /*---------------------------------------------------*/
760 if(protocol == __constant_htons(ETH_P_IP))
762 struct iphdr* iph = (struct iphdr *)(skb->data + ETH_HLEN);
764 if(((unsigned char*)(iph) + (iph->ihl<<2)) >= (skb->data + ETH_HLEN + skb->len))
766 DEBUG_WARN("NAT25: malformed IP packet !\n");
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 */
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);
786 __nat25_db_print(priv);
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)))
801 __nat25_generate_ipv4_network_addr(networkAddr, &iph->daddr);
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);
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);
817 memcpy(skb->data, priv->br_mac, ETH_ALEN);
829 /*---------------------------------------------------*/
830 /* Handle ARP frame */
831 /*---------------------------------------------------*/
832 else if(protocol == __constant_htons(ETH_P_ARP))
834 struct arphdr *arp = (struct arphdr *)(skb->data + ETH_HLEN);
835 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
836 unsigned int *sender, *target;
838 if(arp->ar_pro != __constant_htons(ETH_P_IP))
840 DEBUG_WARN("NAT25: arp protocol unknown (%4x)!\n", htons(arp->ar_pro));
847 return 0; /* skb_copy for all ARP frame */
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]);
854 /* change to ARP sender mac address to wlan STA address */
855 memcpy(arp_ptr, GET_MY_HWADDR(priv), ETH_ALEN);
857 arp_ptr += arp->ar_hln;
858 sender = (unsigned int *)arp_ptr;
860 __nat25_generate_ipv4_network_addr(networkAddr, sender);
862 __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
864 __nat25_db_print(priv);
870 DEBUG_INFO("NAT25: Lookup ARP\n");
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;
877 __nat25_generate_ipv4_network_addr(networkAddr, target);
879 __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
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);
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)))
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;
905 if(protocol == __constant_htons(ETH_P_IPX))
907 DEBUG_INFO("NAT25: Protocol=IPX (Ethernet II)\n");
908 ipx = (struct ipxhdr *)framePtr;
910 else if(protocol <= __constant_htons(ETH_FRAME_LEN))
912 if(!memcmp(ipx_header, framePtr, 2))
914 DEBUG_INFO("NAT25: Protocol=IPX (Ethernet 802.3)\n");
915 ipx = (struct ipxhdr *)framePtr;
919 unsigned char ipx_8022_type = 0xE0;
920 unsigned char snap_8022_type = 0xAA;
922 if(*framePtr == snap_8022_type)
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 */
928 framePtr += 3; /* eliminate the 802.2 header */
930 if(!memcmp(ipx_snap_id, framePtr, 5))
932 framePtr += 5; /* eliminate the SNAP header */
934 DEBUG_INFO("NAT25: Protocol=IPX (Ethernet SNAP)\n");
935 ipx = (struct ipxhdr *)framePtr;
937 else if(!memcmp(aarp_snap_id, framePtr, 5))
939 framePtr += 5; /* eliminate the SNAP header */
941 ea = (struct elapaarp *)framePtr;
943 else if(!memcmp(ddp_snap_id, framePtr, 5))
945 framePtr += 5; /* eliminate the SNAP header */
947 ddp = (struct ddpehdr *)framePtr;
951 DEBUG_WARN("NAT25: Protocol=Ethernet SNAP %02x%02x%02x%02x%02x\n", framePtr[0],
952 framePtr[1], framePtr[2], framePtr[3], framePtr[4]);
956 else if(*framePtr == ipx_8022_type)
958 framePtr += 3; /* eliminate the 802.2 header */
960 if(!memcmp(ipx_header, framePtr, 2))
962 DEBUG_INFO("NAT25: Protocol=IPX (Ethernet 802.2)\n");
963 ipx = (struct ipxhdr *)framePtr;
981 if(!memcmp(skb->data+ETH_ALEN, ipx->ipx_source.node, ETH_ALEN))
983 DEBUG_INFO("NAT25: Check IPX skb_copy\n");
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",
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],
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);
1008 if(!memcmp(skb->data+ETH_ALEN, ipx->ipx_source.node, ETH_ALEN))
1010 DEBUG_INFO("NAT25: Use IPX Net, and Socket as network addr\n");
1012 __nat25_generate_ipx_network_addr_with_socket(networkAddr, &ipx->ipx_source.net, &ipx->ipx_source.sock);
1014 /* change IPX source node addr to wlan STA address */
1015 memcpy(ipx->ipx_source.node, GET_MY_HWADDR(priv), ETH_ALEN);
1019 __nat25_generate_ipx_network_addr_with_node(networkAddr, &ipx->ipx_source.net, ipx->ipx_source.node);
1022 __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
1024 __nat25_db_print(priv);
1030 if(!memcmp(GET_MY_HWADDR(priv), ipx->ipx_dest.node, ETH_ALEN))
1032 DEBUG_INFO("NAT25: Lookup IPX, Modify Destination IPX Node addr\n");
1034 __nat25_generate_ipx_network_addr_with_socket(networkAddr, &ipx->ipx_dest.net, &ipx->ipx_dest.sock);
1036 __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
1038 /* replace IPX destination node addr with Lookup destination MAC addr */
1039 memcpy(ipx->ipx_dest.node, skb->data, ETH_ALEN);
1043 __nat25_generate_ipx_network_addr_with_node(networkAddr, &ipx->ipx_dest.net, ipx->ipx_dest.node);
1045 __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
1058 /* Sanity check fields. */
1059 if(ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN)
1061 DEBUG_WARN("NAT25: Appletalk AARP Sanity check fail!\n");
1072 /* change to AARP source mac address to wlan STA address */
1073 memcpy(ea->hw_src, GET_MY_HWADDR(priv), ETH_ALEN);
1075 DEBUG_INFO("NAT25: Insert AARP, Source=%d,%d Destination=%d,%d\n",
1081 __nat25_generate_apple_network_addr(networkAddr, &ea->pa_src_net, &ea->pa_src_node);
1083 __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
1085 __nat25_db_print(priv);
1091 DEBUG_INFO("NAT25: Lookup AARP, Source=%d,%d Destination=%d,%d\n",
1097 __nat25_generate_apple_network_addr(networkAddr, &ea->pa_dst_net, &ea->pa_dst_node);
1099 __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
1101 /* change to AARP destination mac address to Lookup result */
1102 memcpy(ea->hw_dst, skb->data, ETH_ALEN);
1112 else if(ddp != NULL)
1121 DEBUG_INFO("NAT25: Insert DDP, Source=%d,%d Destination=%d,%d\n",
1127 __nat25_generate_apple_network_addr(networkAddr, &ddp->deh_snet, &ddp->deh_snode);
1129 __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
1131 __nat25_db_print(priv);
1137 DEBUG_INFO("NAT25: Lookup DDP, Source=%d,%d Destination=%d,%d\n",
1143 __nat25_generate_apple_network_addr(networkAddr, &ddp->deh_dnet, &ddp->deh_dnode);
1145 __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
1157 /*---------------------------------------------------*/
1158 /* Handle PPPoE frame */
1159 /*---------------------------------------------------*/
1160 else if((protocol == __constant_htons(ETH_P_PPP_DISC)) ||
1161 (protocol == __constant_htons(ETH_P_PPP_SES)))
1163 struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
1164 unsigned short *pMagic;
1174 if(ph->sid == 0) /* Discovery phase according to tag */
1176 if(ph->code == PADI_CODE || ph->code == PADR_CODE)
1178 if (priv->ethBrExtInfo.addPPPoETag) {
1179 struct pppoe_tag *tag, *pOldTag;
1180 unsigned char tag_buf[40];
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");
1192 memcpy(tag->tag_data+MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN,
1193 pOldTag->tag_data, old_tag_len);
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");
1199 ph->length = htons(ntohs(ph->length)-TAG_HDR_LEN-old_tag_len);
1202 tag->tag_type = PTT_RELAY_SID;
1203 tag->tag_len = htons(MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN+old_tag_len);
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);
1211 if(__nat25_add_pppoe_tag(skb, tag) < 0)
1214 DEBUG_INFO("NAT25: Insert PPPoE, forward %s packet\n",
1215 (ph->code == PADI_CODE ? "PADI" : "PADR"));
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");
1224 if (priv->pppoe_connection_in_progress == 0)
1225 memcpy(priv->pppoe_addr, skb->data+ETH_ALEN, ETH_ALEN);
1227 priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;
1233 else /* session phase */
1235 DEBUG_INFO("NAT25: Insert PPPoE, insert session packet to %s\n", skb->dev->name);
1237 __nat25_generate_pppoe_network_addr(networkAddr, skb->data, &(ph->sid));
1239 __nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
1241 __nat25_db_print(priv);
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;
1251 if(ph->code == PADO_CODE || ph->code == PADS_CODE)
1253 if (priv->ethBrExtInfo.addPPPoETag) {
1254 struct pppoe_tag *tag;
1256 unsigned short tagType, tagLen;
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");
1264 tag = (struct pppoe_tag *)ptr;
1265 tagType = (unsigned short)((ptr[0] << 8) + ptr[1]);
1266 tagLen = (unsigned short)((ptr[2] << 8) + ptr[3]);
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);
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"));
1280 memcpy(skb->data, tag->tag_data+MAGIC_CODE_LEN, ETH_ALEN);
1282 if (tagLen > MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN)
1283 offset = TAG_HDR_LEN;
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");
1289 ph->length = htons(ntohs(ph->length)-(TAG_HDR_LEN+MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN-offset));
1291 tag->tag_len = htons(tagLen-MAGIC_CODE_LEN-RTL_RELAY_TAG_LEN);
1293 DEBUG_INFO("NAT25: Lookup PPPoE, forward %s Packet from %s\n",
1294 (ph->code == PADO_CODE ? "PADO" : "PADS"), skb->dev->name);
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");
1301 memcpy(skb->data, priv->pppoe_addr, ETH_ALEN);
1302 priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;
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));
1311 __nat25_db_network_lookup_and_replace(priv, skb, networkAddr);
1313 __nat25_db_print(priv);
1326 /*---------------------------------------------------*/
1327 /* Handle EAP frame */
1328 /*---------------------------------------------------*/
1329 else if(protocol == __constant_htons(0x888e))
1347 /*---------------------------------------------------*/
1348 /* Handle C-Media proprietary frame */
1349 /*---------------------------------------------------*/
1350 else if((protocol == __constant_htons(0xe2ae)) ||
1351 (protocol == __constant_htons(0xe2af)))
1369 /*---------------------------------------------------*/
1370 /* Handle IPV6 frame */
1371 /*---------------------------------------------------*/
1373 else if(protocol == __constant_htons(ETH_P_IPV6))
1375 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
1377 if (sizeof(*iph) >= (skb->len - ETH_HLEN))
1379 DEBUG_WARN("NAT25: malformed IPv6 packet !\n");
1386 if (skb->data[0] & 1)
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]);
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);
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,
1413 csum_partial((__u8 *)hdr, iph->payload_len, 0));
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]);
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);
1441 #endif /* CL_IPV6_PASS */
1446 int nat25_handle_frame(_adapter *priv, struct sk_buff *skb)
1449 if((!priv->ethBrExtInfo.nat25_disable) && (!(skb->data[0] & 1)))
1451 panic_printk("NAT25: Input Frame: DA=%02x%02x%02x%02x%02x%02x SA=%02x%02x%02x%02x%02x%02x\n",
1467 if(!(skb->data[0] & 1))
1469 int is_vlan_tag=0, i, retval=0;
1470 unsigned short vlan_hdr=0;
1472 if (*((unsigned short *)(skb->data+ETH_ALEN*2)) == __constant_htons(ETH_P_8021Q)) {
1474 vlan_hdr = *((unsigned short *)(skb->data+ETH_ALEN*2+2));
1476 *((unsigned short *)(skb->data+ETH_ALEN*2+2-i*2)) = *((unsigned short *)(skb->data+ETH_ALEN*2-2-i*2));
1480 if (!priv->ethBrExtInfo.nat25_disable)
1482 spin_lock_bh(&priv->br_ext_lock);
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.
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);
1493 spin_unlock_bh(&priv->br_ext_lock);
1496 spin_unlock_bh(&priv->br_ext_lock);
1498 retval = nat25_db_handle(priv, skb, NAT25_LOOKUP);
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);
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;
1520 /* DEBUG_ERR("NAT25: Lookup fail!\n"); */
1528 #define SERVER_PORT 67
1529 #define CLIENT_PORT 68
1530 #define DHCP_MAGIC 0x63825363
1531 #define BROADCAST_FLAG 0x8000
1533 struct dhcpMessage {
1545 u_int8_t chaddr[16];
1549 u_int8_t options[308]; /* 312 - cookie */
1552 void dhcp_flag_bcast(_adapter *priv, struct sk_buff *skb)
1557 if(!priv->ethBrExtInfo.dhcp_bcst_disable)
1559 unsigned short protocol = *((unsigned short *)(skb->data + 2 * ETH_ALEN));
1561 if(protocol == __constant_htons(ETH_P_IP)) /* IP */
1563 struct iphdr* iph = (struct iphdr *)(skb->data + ETH_HLEN);
1565 if(iph->protocol == IPPROTO_UDP) /* UDP */
1567 struct udphdr *udph = (struct udphdr *)((unsigned long)iph + (iph->ihl << 2));
1569 if((udph->source == __constant_htons(CLIENT_PORT))
1570 && (udph->dest == __constant_htons(SERVER_PORT))) /* DHCP request */
1572 struct dhcpMessage *dhcph =
1573 (struct dhcpMessage *)((unsigned long)udph + sizeof(struct udphdr));
1575 if(dhcph->cookie == __constant_htonl(DHCP_MAGIC)) /* match magic word */
1577 if(!(dhcph->flags & htons(BROADCAST_FLAG))) /* if not broadcast */
1579 register int sum = 0;
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;
1588 sum = (sum & 0xffff) + (sum >> 16);
1598 void *scdb_findEntry(_adapter *priv, unsigned char *macAddr,
1599 unsigned char *ipAddr)
1601 unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
1602 struct nat25_network_db_entry *db;
1604 /* spin_lock_bh(&priv->br_ext_lock); */
1606 __nat25_generate_ipv4_network_addr(networkAddr, (unsigned int *)ipAddr);
1607 hash = __nat25_network_hash(networkAddr);
1608 db = priv->nethash[hash];
1611 if(!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
1612 /* spin_unlock_bh(&priv->br_ext_lock); */
1619 /* spin_unlock_bh(&priv->br_ext_lock); */
1623 #endif /* CONFIG_BR_EXT */