Path: blob/master/ALFA-W1F1/RTL8814AU/core/rtw_br_ext.c
1307 views
/******************************************************************************1*2* Copyright(c) 2007 - 2017 Realtek Corporation.3*4* This program is free software; you can redistribute it and/or modify it5* under the terms of version 2 of the GNU General Public License as6* published by the Free Software Foundation.7*8* This program is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for11* more details.12*13*****************************************************************************/14#define _RTW_BR_EXT_C_1516#ifdef __KERNEL__17#include <linux/if_arp.h>18#include <net/ip.h>19#include <linux/atalk.h>20#include <linux/udp.h>21#include <linux/if_pppox.h>22#endif2324#if 1 /* rtw_wifi_driver */25#include <drv_types.h>26#else /* rtw_wifi_driver */27#include "./8192cd_cfg.h"2829#ifndef __KERNEL__30#include "./sys-support.h"31#endif3233#include "./8192cd.h"34#include "./8192cd_headers.h"35#include "./8192cd_br_ext.h"36#include "./8192cd_debug.h"37#endif /* rtw_wifi_driver */3839#ifdef CL_IPV6_PASS40#ifdef __KERNEL__41#include <linux/ipv6.h>42#include <linux/icmpv6.h>43#include <net/ndisc.h>44#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24))45#include <net/ip6_checksum.h>46#else47#include <net/checksum.h>48#endif49#endif50#endif5152#ifdef CONFIG_BR_EXT5354/* #define BR_EXT_DEBUG */5556#define NAT25_IPV4 0157#define NAT25_IPV6 0258#define NAT25_IPX 0359#define NAT25_APPLE 0460#define NAT25_PPPOE 056162#define RTL_RELAY_TAG_LEN (ETH_ALEN)63#define TAG_HDR_LEN 46465#define MAGIC_CODE 0x818666#define MAGIC_CODE_LEN 267#define WAIT_TIME_PPPOE 5 /* waiting time for pppoe server in sec */6869/*-----------------------------------------------------------------70How database records network address:710 1 2 3 4 5 6 7 8 9 1072|----|----|----|----|----|----|----|----|----|----|----|73IPv4 |type| | IP addr |74IPX |type| Net addr | Node addr |75IPX |type| Net addr |Sckt addr|76Apple |type| Network |node|77PPPoE |type| SID | AC MAC |78-----------------------------------------------------------------*/798081/* Find a tag in pppoe frame and return the pointer */82static unsigned char *__nat25_find_pppoe_tag(struct pppoe_hdr *ph, unsigned short type)83{84unsigned char *cur_ptr, *start_ptr;85unsigned short tagLen, tagType;8687start_ptr = cur_ptr = (unsigned char *)ph->tag;88while ((cur_ptr - start_ptr) < ntohs(ph->length)) {89/* prevent un-alignment access */90tagType = (unsigned short)((cur_ptr[0] << 8) + cur_ptr[1]);91tagLen = (unsigned short)((cur_ptr[2] << 8) + cur_ptr[3]);92if (tagType == type)93return cur_ptr;94cur_ptr = cur_ptr + TAG_HDR_LEN + tagLen;95}96return 0;97}9899100static int __nat25_add_pppoe_tag(struct sk_buff *skb, struct pppoe_tag *tag)101{102struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);103int data_len;104105data_len = tag->tag_len + TAG_HDR_LEN;106if (skb_tailroom(skb) < data_len) {107_DEBUG_ERR("skb_tailroom() failed in add SID tag!\n");108return -1;109}110111skb_put(skb, data_len);112/* have a room for new tag */113memmove(((unsigned char *)ph->tag + data_len), (unsigned char *)ph->tag, ntohs(ph->length));114ph->length = htons(ntohs(ph->length) + data_len);115memcpy((unsigned char *)ph->tag, tag, data_len);116return data_len;117}118119static int skb_pull_and_merge(struct sk_buff *skb, unsigned char *src, int len)120{121int tail_len;122unsigned long end, tail;123124if ((src + len) > skb_tail_pointer(skb) || skb->len < len)125return -1;126127tail = (unsigned long)skb_tail_pointer(skb);128end = (unsigned long)src + len;129if (tail < end)130return -1;131132tail_len = (int)(tail - end);133if (tail_len > 0)134memmove(src, src + len, tail_len);135136skb_trim(skb, skb->len - len);137return 0;138}139140static unsigned long __nat25_timeout(_adapter *priv)141{142unsigned long timeout;143144timeout = jiffies - NAT25_AGEING_TIME * HZ;145146return timeout;147}148149150static int __nat25_has_expired(_adapter *priv,151struct nat25_network_db_entry *fdb)152{153if (time_before_eq(fdb->ageing_timer, __nat25_timeout(priv)))154return 1;155156return 0;157}158159160static void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr,161unsigned int *ipAddr)162{163memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);164165networkAddr[0] = NAT25_IPV4;166memcpy(networkAddr + 7, (unsigned char *)ipAddr, 4);167}168169170static void __nat25_generate_pppoe_network_addr(unsigned char *networkAddr,171unsigned char *ac_mac, unsigned short *sid)172{173memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);174175networkAddr[0] = NAT25_PPPOE;176memcpy(networkAddr + 1, (unsigned char *)sid, 2);177memcpy(networkAddr + 3, (unsigned char *)ac_mac, 6);178}179180181#ifdef CL_IPV6_PASS182static void __nat25_generate_ipv6_network_addr(unsigned char *networkAddr,183unsigned int *ipAddr)184{185memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);186187networkAddr[0] = NAT25_IPV6;188memcpy(networkAddr + 1, (unsigned char *)ipAddr, 16);189}190191192static unsigned char *scan_tlv(unsigned char *data, int len, unsigned char tag, unsigned char len8b)193{194while (len > 0) {195if (*data == tag && *(data + 1) == len8b && len >= len8b * 8)196return data + 2;197198len -= (*(data + 1)) * 8;199data += (*(data + 1)) * 8;200}201return NULL;202}203204205static int update_nd_link_layer_addr(unsigned char *data, int len, unsigned char *replace_mac)206{207struct icmp6hdr *icmphdr = (struct icmp6hdr *)data;208unsigned char *mac;209210if (icmphdr->icmp6_type == NDISC_ROUTER_SOLICITATION) {211if (len >= 8) {212mac = scan_tlv(&data[8], len - 8, 1, 1);213if (mac) {214RTW_INFO("Router Solicitation, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",215mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],216replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);217memcpy(mac, replace_mac, 6);218return 1;219}220}221} else if (icmphdr->icmp6_type == NDISC_ROUTER_ADVERTISEMENT) {222if (len >= 16) {223mac = scan_tlv(&data[16], len - 16, 1, 1);224if (mac) {225RTW_INFO("Router Advertisement, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",226mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],227replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);228memcpy(mac, replace_mac, 6);229return 1;230}231}232} else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {233if (len >= 24) {234mac = scan_tlv(&data[24], len - 24, 1, 1);235if (mac) {236RTW_INFO("Neighbor Solicitation, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",237mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],238replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);239memcpy(mac, replace_mac, 6);240return 1;241}242}243} else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {244if (len >= 24) {245mac = scan_tlv(&data[24], len - 24, 2, 1);246if (mac) {247RTW_INFO("Neighbor Advertisement, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",248mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],249replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);250memcpy(mac, replace_mac, 6);251return 1;252}253}254} else if (icmphdr->icmp6_type == NDISC_REDIRECT) {255if (len >= 40) {256mac = scan_tlv(&data[40], len - 40, 2, 1);257if (mac) {258RTW_INFO("Redirect, replace MAC From: %02x:%02x:%02x:%02x:%02x:%02x, To: %02x:%02x:%02x:%02x:%02x:%02x\n",259mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],260replace_mac[0], replace_mac[1], replace_mac[2], replace_mac[3], replace_mac[4], replace_mac[5]);261memcpy(mac, replace_mac, 6);262return 1;263}264}265}266return 0;267}268269#ifdef SUPPORT_RX_UNI2MCAST270static void convert_ipv6_mac_to_mc(struct sk_buff *skb)271{272struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);273unsigned char *dst_mac = skb->data;274275/* dst_mac[0] = 0xff; */276/* dst_mac[1] = 0xff; */277/*modified by qinjunjie,ipv6 multicast address ix 0x33-33-xx-xx-xx-xx*/278dst_mac[0] = 0x33;279dst_mac[1] = 0x33;280memcpy(&dst_mac[2], &iph->daddr.s6_addr32[3], 4);281#if defined(__LINUX_2_6__)282/*modified by qinjunjie,warning:should not remove next line*/283skb->pkt_type = PACKET_MULTICAST;284#endif285}286#endif /* CL_IPV6_PASS */287#endif /* SUPPORT_RX_UNI2MCAST */288289290static int __nat25_network_hash(unsigned char *networkAddr)291{292if (networkAddr[0] == NAT25_IPV4) {293unsigned long x;294295x = networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];296297return x & (NAT25_HASH_SIZE - 1);298} else if (networkAddr[0] == NAT25_IPX) {299unsigned long x;300301x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^302networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];303304return x & (NAT25_HASH_SIZE - 1);305} else if (networkAddr[0] == NAT25_APPLE) {306unsigned long x;307308x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3];309310return x & (NAT25_HASH_SIZE - 1);311} else if (networkAddr[0] == NAT25_PPPOE) {312unsigned long x;313314x = networkAddr[0] ^ networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^ networkAddr[6] ^ networkAddr[7] ^ networkAddr[8];315316return x & (NAT25_HASH_SIZE - 1);317}318#ifdef CL_IPV6_PASS319else if (networkAddr[0] == NAT25_IPV6) {320unsigned long x;321322x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^323networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10] ^324networkAddr[11] ^ networkAddr[12] ^ networkAddr[13] ^ networkAddr[14] ^ networkAddr[15] ^325networkAddr[16];326327return x & (NAT25_HASH_SIZE - 1);328}329#endif330else {331unsigned long x = 0;332int i;333334for (i = 0; i < MAX_NETWORK_ADDR_LEN; i++)335x ^= networkAddr[i];336337return x & (NAT25_HASH_SIZE - 1);338}339}340341342static void __network_hash_link(_adapter *priv,343struct nat25_network_db_entry *ent, int hash)344{345/* Caller must _enter_critical_bh already! */346/* _irqL irqL; */347/* _enter_critical_bh(&priv->br_ext_lock, &irqL); */348349ent->next_hash = priv->nethash[hash];350if (ent->next_hash != NULL)351ent->next_hash->pprev_hash = &ent->next_hash;352priv->nethash[hash] = ent;353ent->pprev_hash = &priv->nethash[hash];354355/* _exit_critical_bh(&priv->br_ext_lock, &irqL); */356}357358359static void __network_hash_unlink(struct nat25_network_db_entry *ent)360{361/* Caller must _enter_critical_bh already! */362/* _irqL irqL; */363/* _enter_critical_bh(&priv->br_ext_lock, &irqL); */364365*(ent->pprev_hash) = ent->next_hash;366if (ent->next_hash != NULL)367ent->next_hash->pprev_hash = ent->pprev_hash;368ent->next_hash = NULL;369ent->pprev_hash = NULL;370371/* _exit_critical_bh(&priv->br_ext_lock, &irqL); */372}373374375static int __nat25_db_network_lookup_and_replace(_adapter *priv,376struct sk_buff *skb, unsigned char *networkAddr)377{378struct nat25_network_db_entry *db;379_irqL irqL;380_enter_critical_bh(&priv->br_ext_lock, &irqL);381382db = priv->nethash[__nat25_network_hash(networkAddr)];383while (db != NULL) {384if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {385if (!__nat25_has_expired(priv, db)) {386/* replace the destination mac address */387memcpy(skb->data, db->macAddr, ETH_ALEN);388atomic_inc(&db->use_count);389390#ifdef CL_IPV6_PASS391RTW_INFO("NAT25: Lookup M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"392"%02x%02x%02x%02x%02x%02x\n",393db->macAddr[0],394db->macAddr[1],395db->macAddr[2],396db->macAddr[3],397db->macAddr[4],398db->macAddr[5],399db->networkAddr[0],400db->networkAddr[1],401db->networkAddr[2],402db->networkAddr[3],403db->networkAddr[4],404db->networkAddr[5],405db->networkAddr[6],406db->networkAddr[7],407db->networkAddr[8],408db->networkAddr[9],409db->networkAddr[10],410db->networkAddr[11],411db->networkAddr[12],412db->networkAddr[13],413db->networkAddr[14],414db->networkAddr[15],415db->networkAddr[16]);416#else417RTW_INFO("NAT25: Lookup M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",418db->macAddr[0],419db->macAddr[1],420db->macAddr[2],421db->macAddr[3],422db->macAddr[4],423db->macAddr[5],424db->networkAddr[0],425db->networkAddr[1],426db->networkAddr[2],427db->networkAddr[3],428db->networkAddr[4],429db->networkAddr[5],430db->networkAddr[6],431db->networkAddr[7],432db->networkAddr[8],433db->networkAddr[9],434db->networkAddr[10]);435#endif436}437_exit_critical_bh(&priv->br_ext_lock, &irqL);438return 1;439}440441db = db->next_hash;442}443444_exit_critical_bh(&priv->br_ext_lock, &irqL);445return 0;446}447448449static void __nat25_db_network_insert(_adapter *priv,450unsigned char *macAddr, unsigned char *networkAddr)451{452struct nat25_network_db_entry *db;453int hash;454_irqL irqL;455_enter_critical_bh(&priv->br_ext_lock, &irqL);456457hash = __nat25_network_hash(networkAddr);458db = priv->nethash[hash];459while (db != NULL) {460if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {461memcpy(db->macAddr, macAddr, ETH_ALEN);462db->ageing_timer = jiffies;463_exit_critical_bh(&priv->br_ext_lock, &irqL);464return;465}466467db = db->next_hash;468}469470db = (struct nat25_network_db_entry *) rtw_malloc(sizeof(*db));471if (db == NULL) {472_exit_critical_bh(&priv->br_ext_lock, &irqL);473return;474}475476memcpy(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN);477memcpy(db->macAddr, macAddr, ETH_ALEN);478atomic_set(&db->use_count, 1);479db->ageing_timer = jiffies;480481__network_hash_link(priv, db, hash);482483_exit_critical_bh(&priv->br_ext_lock, &irqL);484}485486487static void __nat25_db_print(_adapter *priv)488{489_irqL irqL;490_enter_critical_bh(&priv->br_ext_lock, &irqL);491492#ifdef BR_EXT_DEBUG493static int counter = 0;494int i, j;495struct nat25_network_db_entry *db;496497counter++;498if ((counter % 16) != 0)499return;500501for (i = 0, j = 0; i < NAT25_HASH_SIZE; i++) {502db = priv->nethash[i];503504while (db != NULL) {505#ifdef CL_IPV6_PASS506panic_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"507"%02x%02x%02x%02x%02x%02x\n",508j,509i,510atomic_read(&db->use_count),511db->macAddr[0],512db->macAddr[1],513db->macAddr[2],514db->macAddr[3],515db->macAddr[4],516db->macAddr[5],517db->networkAddr[0],518db->networkAddr[1],519db->networkAddr[2],520db->networkAddr[3],521db->networkAddr[4],522db->networkAddr[5],523db->networkAddr[6],524db->networkAddr[7],525db->networkAddr[8],526db->networkAddr[9],527db->networkAddr[10],528db->networkAddr[11],529db->networkAddr[12],530db->networkAddr[13],531db->networkAddr[14],532db->networkAddr[15],533db->networkAddr[16]);534#else535panic_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",536j,537i,538atomic_read(&db->use_count),539db->macAddr[0],540db->macAddr[1],541db->macAddr[2],542db->macAddr[3],543db->macAddr[4],544db->macAddr[5],545db->networkAddr[0],546db->networkAddr[1],547db->networkAddr[2],548db->networkAddr[3],549db->networkAddr[4],550db->networkAddr[5],551db->networkAddr[6],552db->networkAddr[7],553db->networkAddr[8],554db->networkAddr[9],555db->networkAddr[10]);556#endif557j++;558559db = db->next_hash;560}561}562#endif563564_exit_critical_bh(&priv->br_ext_lock, &irqL);565}566567568569570/*571* NAT2.5 interface572*/573574void nat25_db_cleanup(_adapter *priv)575{576int i;577_irqL irqL;578_enter_critical_bh(&priv->br_ext_lock, &irqL);579580for (i = 0; i < NAT25_HASH_SIZE; i++) {581struct nat25_network_db_entry *f;582f = priv->nethash[i];583while (f != NULL) {584struct nat25_network_db_entry *g;585586g = f->next_hash;587if (priv->scdb_entry == f) {588memset(priv->scdb_mac, 0, ETH_ALEN);589memset(priv->scdb_ip, 0, 4);590priv->scdb_entry = NULL;591}592__network_hash_unlink(f);593rtw_mfree((u8 *) f, sizeof(struct nat25_network_db_entry));594595f = g;596}597}598599_exit_critical_bh(&priv->br_ext_lock, &irqL);600}601602603void nat25_db_expire(_adapter *priv)604{605int i;606_irqL irqL;607_enter_critical_bh(&priv->br_ext_lock, &irqL);608609/* if(!priv->ethBrExtInfo.nat25_disable) */610{611for (i = 0; i < NAT25_HASH_SIZE; i++) {612struct nat25_network_db_entry *f;613f = priv->nethash[i];614615while (f != NULL) {616struct nat25_network_db_entry *g;617g = f->next_hash;618619if (__nat25_has_expired(priv, f)) {620if (atomic_dec_and_test(&f->use_count)) {621#ifdef BR_EXT_DEBUG622#ifdef CL_IPV6_PASS623panic_printk("NAT25 Expire H(%02d) M:%02x%02x%02x%02x%02x%02x N:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"624"%02x%02x%02x%02x%02x%02x\n",625i,626f->macAddr[0],627f->macAddr[1],628f->macAddr[2],629f->macAddr[3],630f->macAddr[4],631f->macAddr[5],632f->networkAddr[0],633f->networkAddr[1],634f->networkAddr[2],635f->networkAddr[3],636f->networkAddr[4],637f->networkAddr[5],638f->networkAddr[6],639f->networkAddr[7],640f->networkAddr[8],641f->networkAddr[9],642f->networkAddr[10],643f->networkAddr[11],644f->networkAddr[12],645f->networkAddr[13],646f->networkAddr[14],647f->networkAddr[15],648f->networkAddr[16]);649#else650651panic_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",652i,653f->macAddr[0],654f->macAddr[1],655f->macAddr[2],656f->macAddr[3],657f->macAddr[4],658f->macAddr[5],659f->networkAddr[0],660f->networkAddr[1],661f->networkAddr[2],662f->networkAddr[3],663f->networkAddr[4],664f->networkAddr[5],665f->networkAddr[6],666f->networkAddr[7],667f->networkAddr[8],668f->networkAddr[9],669f->networkAddr[10]);670#endif671#endif672if (priv->scdb_entry == f) {673memset(priv->scdb_mac, 0, ETH_ALEN);674memset(priv->scdb_ip, 0, 4);675priv->scdb_entry = NULL;676}677__network_hash_unlink(f);678rtw_mfree((u8 *) f, sizeof(struct nat25_network_db_entry));679}680}681682f = g;683}684}685}686687_exit_critical_bh(&priv->br_ext_lock, &irqL);688}689690691#ifdef SUPPORT_TX_MCAST2UNI692static int checkIPMcAndReplace(_adapter *priv, struct sk_buff *skb, unsigned int *dst_ip)693{694struct stat_info *pstat;695struct list_head *phead, *plist;696int i;697698phead = &priv->asoc_list;699plist = phead->next;700701while (plist != phead) {702pstat = list_entry(plist, struct stat_info, asoc_list);703plist = plist->next;704705if (pstat->ipmc_num == 0)706continue;707708for (i = 0; i < MAX_IP_MC_ENTRY; i++) {709if (pstat->ipmc[i].used && !memcmp(&pstat->ipmc[i].mcmac[3], ((unsigned char *)dst_ip) + 1, 3)) {710memcpy(skb->data, pstat->ipmc[i].mcmac, ETH_ALEN);711return 1;712}713}714}715return 0;716}717#endif718719int nat25_db_handle(_adapter *priv, struct sk_buff *skb, int method)720{721unsigned short protocol;722unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];723724if (skb == NULL)725return -1;726727if ((method <= NAT25_MIN) || (method >= NAT25_MAX))728return -1;729730protocol = *((unsigned short *)(skb->data + 2 * ETH_ALEN));731732/*---------------------------------------------------*/733/* Handle IP frame */734/*---------------------------------------------------*/735if (protocol == __constant_htons(ETH_P_IP)) {736struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);737738if (((unsigned char *)(iph) + (iph->ihl << 2)) >= (skb->data + ETH_HLEN + skb->len)) {739DEBUG_WARN("NAT25: malformed IP packet !\n");740return -1;741}742743switch (method) {744case NAT25_CHECK:745return -1;746747case NAT25_INSERT: {748/* some muticast with source IP is all zero, maybe other case is illegal */749/* in class A, B, C, host address is all zero or all one is illegal */750if (iph->saddr == 0)751return 0;752RTW_INFO("NAT25: Insert IP, SA=%08x, DA=%08x\n", iph->saddr, iph->daddr);753__nat25_generate_ipv4_network_addr(networkAddr, &iph->saddr);754/* record source IP address and , source mac address into db */755__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);756757__nat25_db_print(priv);758}759return 0;760761case NAT25_LOOKUP: {762RTW_INFO("NAT25: Lookup IP, SA=%08x, DA=%08x\n", iph->saddr, iph->daddr);763#ifdef SUPPORT_TX_MCAST2UNI764if (priv->pshare->rf_ft_var.mc2u_disable ||765((((OPMODE & (WIFI_STATION_STATE | WIFI_ASOC_STATE))766== (WIFI_STATION_STATE | WIFI_ASOC_STATE)) &&767!checkIPMcAndReplace(priv, skb, &iph->daddr)) ||768(OPMODE & WIFI_ADHOC_STATE)))769#endif770{771__nat25_generate_ipv4_network_addr(networkAddr, &iph->daddr);772773if (!__nat25_db_network_lookup_and_replace(priv, skb, networkAddr)) {774if (*((unsigned char *)&iph->daddr + 3) == 0xff) {775/* L2 is unicast but L3 is broadcast, make L2 bacome broadcast */776RTW_INFO("NAT25: Set DA as boardcast\n");777memset(skb->data, 0xff, ETH_ALEN);778} else {779/* forward unknow IP packet to upper TCP/IP */780RTW_INFO("NAT25: Replace DA with BR's MAC\n");781if ((*(u32 *)priv->br_mac) == 0 && (*(u16 *)(priv->br_mac + 4)) == 0) {782void netdev_br_init(struct net_device *netdev);783printk("Re-init netdev_br_init() due to br_mac==0!\n");784netdev_br_init(priv->pnetdev);785}786memcpy(skb->data, priv->br_mac, ETH_ALEN);787}788}789}790}791return 0;792793default:794return -1;795}796}797798/*---------------------------------------------------*/799/* Handle ARP frame */800/*---------------------------------------------------*/801else if (protocol == __constant_htons(ETH_P_ARP)) {802struct arphdr *arp = (struct arphdr *)(skb->data + ETH_HLEN);803unsigned char *arp_ptr = (unsigned char *)(arp + 1);804unsigned int *sender, *target;805806if (arp->ar_pro != __constant_htons(ETH_P_IP)) {807DEBUG_WARN("NAT25: arp protocol unknown (%4x)!\n", htons(arp->ar_pro));808return -1;809}810811switch (method) {812case NAT25_CHECK:813return 0; /* skb_copy for all ARP frame */814815case NAT25_INSERT: {816RTW_INFO("NAT25: Insert ARP, MAC=%02x%02x%02x%02x%02x%02x\n", arp_ptr[0],817arp_ptr[1], arp_ptr[2], arp_ptr[3], arp_ptr[4], arp_ptr[5]);818819/* change to ARP sender mac address to wlan STA address */820memcpy(arp_ptr, GET_MY_HWADDR(priv), ETH_ALEN);821822arp_ptr += arp->ar_hln;823sender = (unsigned int *)arp_ptr;824825__nat25_generate_ipv4_network_addr(networkAddr, sender);826827__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);828829__nat25_db_print(priv);830}831return 0;832833case NAT25_LOOKUP: {834RTW_INFO("NAT25: Lookup ARP\n");835836arp_ptr += arp->ar_hln;837sender = (unsigned int *)arp_ptr;838arp_ptr += (arp->ar_hln + arp->ar_pln);839target = (unsigned int *)arp_ptr;840841__nat25_generate_ipv4_network_addr(networkAddr, target);842843__nat25_db_network_lookup_and_replace(priv, skb, networkAddr);844845/* change to ARP target mac address to Lookup result */846arp_ptr = (unsigned char *)(arp + 1);847arp_ptr += (arp->ar_hln + arp->ar_pln);848memcpy(arp_ptr, skb->data, ETH_ALEN);849}850return 0;851852default:853return -1;854}855}856857/*---------------------------------------------------*/858/* Handle PPPoE frame */859/*---------------------------------------------------*/860else if ((protocol == __constant_htons(ETH_P_PPP_DISC)) ||861(protocol == __constant_htons(ETH_P_PPP_SES))) {862struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);863unsigned short *pMagic;864865switch (method) {866case NAT25_CHECK:867if (ph->sid == 0)868return 0;869return 1;870871case NAT25_INSERT:872if (ph->sid == 0) { /* Discovery phase according to tag */873if (ph->code == PADI_CODE || ph->code == PADR_CODE) {874if (priv->ethBrExtInfo.addPPPoETag) {875struct pppoe_tag *tag, *pOldTag;876unsigned char tag_buf[40];877int old_tag_len = 0;878879tag = (struct pppoe_tag *)tag_buf;880pOldTag = (struct pppoe_tag *)__nat25_find_pppoe_tag(ph, ntohs(PTT_RELAY_SID));881if (pOldTag) { /* if SID existed, copy old value and delete it */882old_tag_len = ntohs(pOldTag->tag_len);883if (old_tag_len + TAG_HDR_LEN + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN > sizeof(tag_buf)) {884DEBUG_ERR("SID tag length too long!\n");885return -1;886}887888memcpy(tag->tag_data + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN,889pOldTag->tag_data, old_tag_len);890891if (skb_pull_and_merge(skb, (unsigned char *)pOldTag, TAG_HDR_LEN + old_tag_len) < 0) {892DEBUG_ERR("call skb_pull_and_merge() failed in PADI/R packet!\n");893return -1;894}895ph->length = htons(ntohs(ph->length) - TAG_HDR_LEN - old_tag_len);896}897898tag->tag_type = PTT_RELAY_SID;899tag->tag_len = htons(MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN + old_tag_len);900901/* insert the magic_code+client mac in relay tag */902pMagic = (unsigned short *)tag->tag_data;903*pMagic = htons(MAGIC_CODE);904memcpy(tag->tag_data + MAGIC_CODE_LEN, skb->data + ETH_ALEN, ETH_ALEN);905906/* Add relay tag */907if (__nat25_add_pppoe_tag(skb, tag) < 0)908return -1;909910RTW_INFO("NAT25: Insert PPPoE, forward %s packet\n",911(ph->code == PADI_CODE ? "PADI" : "PADR"));912} else { /* not add relay tag */913if (priv->pppoe_connection_in_progress &&914memcmp(skb->data + ETH_ALEN, priv->pppoe_addr, ETH_ALEN)) {915DEBUG_ERR("Discard PPPoE packet due to another PPPoE connection is in progress!\n");916return -2;917}918919if (priv->pppoe_connection_in_progress == 0)920memcpy(priv->pppoe_addr, skb->data + ETH_ALEN, ETH_ALEN);921922priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;923}924} else925return -1;926} else { /* session phase */927RTW_INFO("NAT25: Insert PPPoE, insert session packet to %s\n", skb->dev->name);928929__nat25_generate_pppoe_network_addr(networkAddr, skb->data, &(ph->sid));930931__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);932933__nat25_db_print(priv);934935if (!priv->ethBrExtInfo.addPPPoETag &&936priv->pppoe_connection_in_progress &&937!memcmp(skb->data + ETH_ALEN, priv->pppoe_addr, ETH_ALEN))938priv->pppoe_connection_in_progress = 0;939}940return 0;941942case NAT25_LOOKUP:943if (ph->code == PADO_CODE || ph->code == PADS_CODE) {944if (priv->ethBrExtInfo.addPPPoETag) {945struct pppoe_tag *tag;946unsigned char *ptr;947unsigned short tagType, tagLen;948int offset = 0;949950ptr = __nat25_find_pppoe_tag(ph, ntohs(PTT_RELAY_SID));951if (ptr == 0) {952DEBUG_ERR("Fail to find PTT_RELAY_SID in FADO!\n");953return -1;954}955956tag = (struct pppoe_tag *)ptr;957tagType = (unsigned short)((ptr[0] << 8) + ptr[1]);958tagLen = (unsigned short)((ptr[2] << 8) + ptr[3]);959960if ((tagType != ntohs(PTT_RELAY_SID)) || (tagLen < (MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN))) {961DEBUG_ERR("Invalid PTT_RELAY_SID tag length [%d]!\n", tagLen);962return -1;963}964965pMagic = (unsigned short *)tag->tag_data;966if (ntohs(*pMagic) != MAGIC_CODE) {967DEBUG_ERR("Can't find MAGIC_CODE in %s packet!\n",968(ph->code == PADO_CODE ? "PADO" : "PADS"));969return -1;970}971972memcpy(skb->data, tag->tag_data + MAGIC_CODE_LEN, ETH_ALEN);973974if (tagLen > MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN)975offset = TAG_HDR_LEN;976977if (skb_pull_and_merge(skb, ptr + offset, TAG_HDR_LEN + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN - offset) < 0) {978DEBUG_ERR("call skb_pull_and_merge() failed in PADO packet!\n");979return -1;980}981ph->length = htons(ntohs(ph->length) - (TAG_HDR_LEN + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN - offset));982if (offset > 0)983tag->tag_len = htons(tagLen - MAGIC_CODE_LEN - RTL_RELAY_TAG_LEN);984985RTW_INFO("NAT25: Lookup PPPoE, forward %s Packet from %s\n",986(ph->code == PADO_CODE ? "PADO" : "PADS"), skb->dev->name);987} else { /* not add relay tag */988if (!priv->pppoe_connection_in_progress) {989DEBUG_ERR("Discard PPPoE packet due to no connection in progresss!\n");990return -1;991}992memcpy(skb->data, priv->pppoe_addr, ETH_ALEN);993priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;994}995} else {996if (ph->sid != 0) {997RTW_INFO("NAT25: Lookup PPPoE, lookup session packet from %s\n", skb->dev->name);998__nat25_generate_pppoe_network_addr(networkAddr, skb->data + ETH_ALEN, &(ph->sid));9991000__nat25_db_network_lookup_and_replace(priv, skb, networkAddr);10011002__nat25_db_print(priv);1003} else1004return -1;10051006}1007return 0;10081009default:1010return -1;1011}1012}10131014/*---------------------------------------------------*/1015/* Handle EAP frame */1016/*---------------------------------------------------*/1017else if (protocol == __constant_htons(0x888e)) {1018switch (method) {1019case NAT25_CHECK:1020return -1;10211022case NAT25_INSERT:1023return 0;10241025case NAT25_LOOKUP:1026return 0;10271028default:1029return -1;1030}1031}10321033/*---------------------------------------------------*/1034/* Handle C-Media proprietary frame */1035/*---------------------------------------------------*/1036else if ((protocol == __constant_htons(0xe2ae)) ||1037(protocol == __constant_htons(0xe2af))) {1038switch (method) {1039case NAT25_CHECK:1040return -1;10411042case NAT25_INSERT:1043return 0;10441045case NAT25_LOOKUP:1046return 0;10471048default:1049return -1;1050}1051}10521053/*---------------------------------------------------*/1054/* Handle IPV6 frame */1055/*---------------------------------------------------*/1056#ifdef CL_IPV6_PASS1057else if (protocol == __constant_htons(ETH_P_IPV6)) {1058struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);10591060if (sizeof(*iph) >= (skb->len - ETH_HLEN)) {1061DEBUG_WARN("NAT25: malformed IPv6 packet !\n");1062return -1;1063}10641065switch (method) {1066case NAT25_CHECK:1067if (skb->data[0] & 1)1068return 0;1069return -1;10701071case NAT25_INSERT: {1072RTW_INFO("NAT25: Insert IP, SA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x,"1073" DA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x\n",1074iph->saddr.s6_addr16[0], iph->saddr.s6_addr16[1], iph->saddr.s6_addr16[2], iph->saddr.s6_addr16[3],1075iph->saddr.s6_addr16[4], iph->saddr.s6_addr16[5], iph->saddr.s6_addr16[6], iph->saddr.s6_addr16[7],1076iph->daddr.s6_addr16[0], iph->daddr.s6_addr16[1], iph->daddr.s6_addr16[2], iph->daddr.s6_addr16[3],1077iph->daddr.s6_addr16[4], iph->daddr.s6_addr16[5], iph->daddr.s6_addr16[6], iph->daddr.s6_addr16[7]);10781079if (memcmp(&iph->saddr, "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 16)) {1080__nat25_generate_ipv6_network_addr(networkAddr, (unsigned int *)&iph->saddr);1081__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);1082__nat25_db_print(priv);10831084if (iph->nexthdr == IPPROTO_ICMPV6 &&1085skb->len > (ETH_HLEN + sizeof(*iph) + 4)) {1086if (update_nd_link_layer_addr(skb->data + ETH_HLEN + sizeof(*iph),1087skb->len - ETH_HLEN - sizeof(*iph), GET_MY_HWADDR(priv))) {1088struct icmp6hdr *hdr = (struct icmp6hdr *)(skb->data + ETH_HLEN + sizeof(*iph));1089hdr->icmp6_cksum = 0;1090hdr->icmp6_cksum = csum_ipv6_magic(&iph->saddr, &iph->daddr,1091iph->payload_len,1092IPPROTO_ICMPV6,1093csum_partial((__u8 *)hdr, iph->payload_len, 0));1094}1095}1096}1097}1098return 0;10991100case NAT25_LOOKUP:1101RTW_INFO("NAT25: Lookup IP, SA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x,"1102" DA=%4x:%4x:%4x:%4x:%4x:%4x:%4x:%4x\n",1103iph->saddr.s6_addr16[0], iph->saddr.s6_addr16[1], iph->saddr.s6_addr16[2], iph->saddr.s6_addr16[3],1104iph->saddr.s6_addr16[4], iph->saddr.s6_addr16[5], iph->saddr.s6_addr16[6], iph->saddr.s6_addr16[7],1105iph->daddr.s6_addr16[0], iph->daddr.s6_addr16[1], iph->daddr.s6_addr16[2], iph->daddr.s6_addr16[3],1106iph->daddr.s6_addr16[4], iph->daddr.s6_addr16[5], iph->daddr.s6_addr16[6], iph->daddr.s6_addr16[7]);110711081109__nat25_generate_ipv6_network_addr(networkAddr, (unsigned int *)&iph->daddr);1110if (!__nat25_db_network_lookup_and_replace(priv, skb, networkAddr)) {1111#ifdef SUPPORT_RX_UNI2MCAST1112if (iph->daddr.s6_addr[0] == 0xff)1113convert_ipv6_mac_to_mc(skb);1114#endif1115}1116return 0;11171118default:1119return -1;1120}1121}1122#endif /* CL_IPV6_PASS */11231124return -1;1125}112611271128int nat25_handle_frame(_adapter *priv, struct sk_buff *skb)1129{1130#ifdef BR_EXT_DEBUG1131if ((!priv->ethBrExtInfo.nat25_disable) && (!(skb->data[0] & 1))) {1132panic_printk("NAT25: Input Frame: DA=%02x%02x%02x%02x%02x%02x SA=%02x%02x%02x%02x%02x%02x\n",1133skb->data[0],1134skb->data[1],1135skb->data[2],1136skb->data[3],1137skb->data[4],1138skb->data[5],1139skb->data[6],1140skb->data[7],1141skb->data[8],1142skb->data[9],1143skb->data[10],1144skb->data[11]);1145}1146#endif11471148if (!(skb->data[0] & 1)) {1149int is_vlan_tag = 0, i, retval = 0;1150unsigned short vlan_hdr = 0;11511152if (*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_8021Q)) {1153is_vlan_tag = 1;1154vlan_hdr = *((unsigned short *)(skb->data + ETH_ALEN * 2 + 2));1155for (i = 0; i < 6; i++)1156*((unsigned short *)(skb->data + ETH_ALEN * 2 + 2 - i * 2)) = *((unsigned short *)(skb->data + ETH_ALEN * 2 - 2 - i * 2));1157skb_pull(skb, 4);1158}11591160if (!priv->ethBrExtInfo.nat25_disable) {1161_irqL irqL;1162_enter_critical_bh(&priv->br_ext_lock, &irqL);1163/*1164* This function look up the destination network address from1165* the NAT2.5 database. Return value = -1 means that the1166* corresponding network protocol is NOT support.1167*/1168if (!priv->ethBrExtInfo.nat25sc_disable &&1169(*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)) &&1170!memcmp(priv->scdb_ip, skb->data + ETH_HLEN + 16, 4)) {1171memcpy(skb->data, priv->scdb_mac, ETH_ALEN);11721173_exit_critical_bh(&priv->br_ext_lock, &irqL);1174} else {1175_exit_critical_bh(&priv->br_ext_lock, &irqL);11761177retval = nat25_db_handle(priv, skb, NAT25_LOOKUP);1178}1179} else {1180if (((*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_IP)) &&1181!memcmp(priv->br_ip, skb->data + ETH_HLEN + 16, 4)) ||1182((*((unsigned short *)(skb->data + ETH_ALEN * 2)) == __constant_htons(ETH_P_ARP)) &&1183!memcmp(priv->br_ip, skb->data + ETH_HLEN + 24, 4))) {1184/* for traffic to upper TCP/IP */1185retval = nat25_db_handle(priv, skb, NAT25_LOOKUP);1186}1187}11881189if (is_vlan_tag) {1190skb_push(skb, 4);1191for (i = 0; i < 6; i++)1192*((unsigned short *)(skb->data + i * 2)) = *((unsigned short *)(skb->data + 4 + i * 2));1193*((unsigned short *)(skb->data + ETH_ALEN * 2)) = __constant_htons(ETH_P_8021Q);1194*((unsigned short *)(skb->data + ETH_ALEN * 2 + 2)) = vlan_hdr;1195}11961197if (retval == -1) {1198/* DEBUG_ERR("NAT25: Lookup fail!\n"); */1199return -1;1200}1201}12021203return 0;1204}12051206#if 01207void mac_clone(_adapter *priv, unsigned char *addr)1208{1209struct sockaddr sa;12101211memcpy(sa.sa_data, addr, ETH_ALEN);1212RTW_INFO("MAC Clone: Addr=%02x%02x%02x%02x%02x%02x\n",1213addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);1214rtl8192cd_set_hwaddr(priv->dev, &sa);1215}121612171218int mac_clone_handle_frame(_adapter *priv, struct sk_buff *skb)1219{1220if (priv->ethBrExtInfo.macclone_enable && !priv->macclone_completed) {1221if (!(skb->data[ETH_ALEN] & 1)) { /* check any other particular MAC add */1222if (memcmp(skb->data + ETH_ALEN, GET_MY_HWADDR(priv), ETH_ALEN) &&1223((priv->dev->br_port) &&1224memcmp(skb->data + ETH_ALEN, priv->br_mac, ETH_ALEN))) {1225mac_clone(priv, skb->data + ETH_ALEN);1226priv->macclone_completed = 1;1227}1228}1229}12301231return 0;1232}1233#endif /* 0 */12341235#define SERVER_PORT 671236#define CLIENT_PORT 681237#define DHCP_MAGIC 0x638253631238#define BROADCAST_FLAG 0x800012391240struct dhcpMessage {1241u_int8_t op;1242u_int8_t htype;1243u_int8_t hlen;1244u_int8_t hops;1245u_int32_t xid;1246u_int16_t secs;1247u_int16_t flags;1248u_int32_t ciaddr;1249u_int32_t yiaddr;1250u_int32_t siaddr;1251u_int32_t giaddr;1252u_int8_t chaddr[16];1253u_int8_t sname[64];1254u_int8_t file[128];1255u_int32_t cookie;1256u_int8_t options[308]; /* 312 - cookie */1257};12581259void dhcp_flag_bcast(_adapter *priv, struct sk_buff *skb)1260{1261if (skb == NULL)1262return;12631264if (!priv->ethBrExtInfo.dhcp_bcst_disable) {1265unsigned short protocol = *((unsigned short *)(skb->data + 2 * ETH_ALEN));12661267if (protocol == __constant_htons(ETH_P_IP)) { /* IP */1268struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);12691270if (iph->protocol == IPPROTO_UDP) { /* UDP */1271struct udphdr *udph = (struct udphdr *)((SIZE_PTR)iph + (iph->ihl << 2));12721273if ((udph->source == __constant_htons(CLIENT_PORT))1274&& (udph->dest == __constant_htons(SERVER_PORT))) { /* DHCP request */1275struct dhcpMessage *dhcph =1276(struct dhcpMessage *)((SIZE_PTR)udph + sizeof(struct udphdr));12771278if (dhcph->cookie == __constant_htonl(DHCP_MAGIC)) { /* match magic word */1279if (!(dhcph->flags & htons(BROADCAST_FLAG))) { /* if not broadcast */1280register int sum = 0;12811282RTW_INFO("DHCP: change flag of DHCP request to broadcast.\n");1283/* or BROADCAST flag */1284dhcph->flags |= htons(BROADCAST_FLAG);1285/* recalculate checksum */1286sum = ~(udph->check) & 0xffff;1287sum += dhcph->flags;1288while (sum >> 16)1289sum = (sum & 0xffff) + (sum >> 16);1290udph->check = ~sum;1291}1292}1293}1294}1295}1296}1297}129812991300void *scdb_findEntry(_adapter *priv, unsigned char *macAddr,1301unsigned char *ipAddr)1302{1303unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];1304struct nat25_network_db_entry *db;1305int hash;1306/* _irqL irqL; */1307/* _enter_critical_bh(&priv->br_ext_lock, &irqL); */13081309__nat25_generate_ipv4_network_addr(networkAddr, (unsigned int *)ipAddr);1310hash = __nat25_network_hash(networkAddr);1311db = priv->nethash[hash];1312while (db != NULL) {1313if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {1314/* _exit_critical_bh(&priv->br_ext_lock, &irqL); */1315return (void *)db;1316}13171318db = db->next_hash;1319}13201321/* _exit_critical_bh(&priv->br_ext_lock, &irqL); */1322return NULL;1323}13241325#endif /* CONFIG_BR_EXT */132613271328