2022-06-08 23:46:35 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright(c) 2007 - 2011 Realtek Corporation. i*/
|
|
|
|
|
2013-05-08 21:45:39 +00:00
|
|
|
#define _RTW_BR_EXT_C_
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
#include "../include/linux/if_arp.h"
|
|
|
|
#include "../include/net/ip.h"
|
|
|
|
#include "../include/linux/atalk.h"
|
|
|
|
#include "../include/linux/udp.h"
|
|
|
|
#include "../include/linux/if_pppox.h"
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
#include "../include/drv_types.h"
|
|
|
|
#include "../include/rtw_br_ext.h"
|
|
|
|
#include "../include/usb_osintf.h"
|
|
|
|
#include "../include/recv_osdep.h"
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-05-15 18:31:06 +00:00
|
|
|
#ifndef csum_ipv6_magic
|
2022-06-08 23:46:35 +00:00
|
|
|
#include "../include/net/ip6_checksum.h"
|
2013-05-15 18:31:06 +00:00
|
|
|
#endif
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
#include "../include/linux/ipv6.h"
|
|
|
|
#include "../include/linux/icmpv6.h"
|
|
|
|
#include "../include/net/ndisc.h"
|
|
|
|
#include "../include/net/checksum.h"
|
2013-05-08 21:45:39 +00:00
|
|
|
|
|
|
|
#define NAT25_IPV4 01
|
|
|
|
#define NAT25_IPV6 02
|
|
|
|
#define NAT25_IPX 03
|
|
|
|
#define NAT25_APPLE 04
|
|
|
|
#define NAT25_PPPOE 05
|
|
|
|
|
|
|
|
#define RTL_RELAY_TAG_LEN (ETH_ALEN)
|
|
|
|
#define TAG_HDR_LEN 4
|
|
|
|
|
|
|
|
#define MAGIC_CODE 0x8186
|
|
|
|
#define MAGIC_CODE_LEN 2
|
2013-07-09 22:38:46 +00:00
|
|
|
#define WAIT_TIME_PPPOE 5 /* waiting time for pppoe server in sec */
|
2013-05-08 21:45:39 +00:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------
|
|
|
|
How database records network address:
|
2013-07-27 18:27:48 +00:00
|
|
|
0 1 2 3 4 5 6 7 8 9 10
|
|
|
|
|----|----|----|----|----|----|----|----|----|----|----|
|
2013-05-08 21:45:39 +00:00
|
|
|
IPv4 |type| | IP addr |
|
|
|
|
IPX |type| Net addr | Node addr |
|
|
|
|
IPX |type| Net addr |Sckt addr|
|
|
|
|
Apple |type| Network |node|
|
|
|
|
PPPoE |type| SID | AC MAC |
|
|
|
|
-----------------------------------------------------------------*/
|
|
|
|
|
2013-07-09 22:38:46 +00:00
|
|
|
/* Find a tag in pppoe frame and return the pointer */
|
2022-06-08 23:46:35 +00:00
|
|
|
static unsigned char *__nat25_find_pppoe_tag(struct pppoe_hdr *ph, unsigned short type)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
|
|
|
unsigned char *cur_ptr, *start_ptr;
|
|
|
|
unsigned short tagLen, tagType;
|
|
|
|
|
|
|
|
start_ptr = cur_ptr = (unsigned char *)ph->tag;
|
2013-05-09 04:04:25 +00:00
|
|
|
while ((cur_ptr - start_ptr) < ntohs(ph->length)) {
|
2013-07-09 22:38:46 +00:00
|
|
|
/* prevent un-alignment access */
|
2013-05-08 21:45:39 +00:00
|
|
|
tagType = (unsigned short)((cur_ptr[0] << 8) + cur_ptr[1]);
|
|
|
|
tagLen = (unsigned short)((cur_ptr[2] << 8) + cur_ptr[3]);
|
2013-05-09 04:04:25 +00:00
|
|
|
if (tagType == type)
|
2013-05-08 21:45:39 +00:00
|
|
|
return cur_ptr;
|
|
|
|
cur_ptr = cur_ptr + TAG_HDR_LEN + tagLen;
|
|
|
|
}
|
2013-06-03 19:52:18 +00:00
|
|
|
return NULL;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
static int __nat25_add_pppoe_tag(struct sk_buff *skb, struct pppoe_tag *tag)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
|
|
|
struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
|
|
|
|
int data_len;
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
data_len = be16_to_cpu(tag->tag_len) + TAG_HDR_LEN;
|
|
|
|
if (skb_tailroom(skb) < data_len)
|
2013-05-08 21:45:39 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
skb_put(skb, data_len);
|
2013-07-09 22:38:46 +00:00
|
|
|
/* have a room for new tag */
|
2013-05-08 21:45:39 +00:00
|
|
|
memmove(((unsigned char *)ph->tag + data_len), (unsigned char *)ph->tag, ntohs(ph->length));
|
|
|
|
ph->length = htons(ntohs(ph->length) + data_len);
|
|
|
|
memcpy((unsigned char *)ph->tag, tag, data_len);
|
|
|
|
return data_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int skb_pull_and_merge(struct sk_buff *skb, unsigned char *src, int len)
|
|
|
|
{
|
|
|
|
int tail_len;
|
|
|
|
unsigned long end, tail;
|
|
|
|
|
|
|
|
if ((src+len) > skb_tail_pointer(skb) || skb->len < len)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tail = (unsigned long)skb_tail_pointer(skb);
|
|
|
|
end = (unsigned long)src+len;
|
|
|
|
if (tail < end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tail_len = (int)(tail-end);
|
|
|
|
if (tail_len > 0)
|
|
|
|
memmove(src, src+len, tail_len);
|
|
|
|
|
|
|
|
skb_trim(skb, skb->len-len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
static int __nat25_has_expired(struct nat25_network_db_entry *fdb)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
2022-06-08 23:46:35 +00:00
|
|
|
if (time_before_eq(fdb->ageing_timer, jiffies - NAT25_AGEING_TIME * HZ))
|
2013-05-08 21:45:39 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
static void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr,
|
2013-05-08 21:45:39 +00:00
|
|
|
unsigned int *ipAddr)
|
|
|
|
{
|
|
|
|
memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
|
|
|
|
|
|
|
|
networkAddr[0] = NAT25_IPV4;
|
|
|
|
memcpy(networkAddr+7, (unsigned char *)ipAddr, 4);
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
static void __nat25_generate_pppoe_network_addr(unsigned char *networkAddr,
|
|
|
|
unsigned char *ac_mac, __be16 *sid)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
|
|
|
memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
|
|
|
|
|
|
|
|
networkAddr[0] = NAT25_PPPOE;
|
2013-11-29 22:10:20 +00:00
|
|
|
memcpy(networkAddr+1, (unsigned char *)sid, 2);
|
2013-05-08 21:45:39 +00:00
|
|
|
memcpy(networkAddr+3, (unsigned char *)ac_mac, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __nat25_generate_ipv6_network_addr(unsigned char *networkAddr,
|
|
|
|
unsigned int *ipAddr)
|
|
|
|
{
|
|
|
|
memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
|
|
|
|
|
|
|
|
networkAddr[0] = NAT25_IPV6;
|
|
|
|
memcpy(networkAddr+1, (unsigned char *)ipAddr, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char *scan_tlv(unsigned char *data, int len, unsigned char tag, unsigned char len8b)
|
|
|
|
{
|
|
|
|
while (len > 0) {
|
|
|
|
if (*data == tag && *(data+1) == len8b && len >= len8b*8)
|
2013-05-19 04:28:07 +00:00
|
|
|
return data+2;
|
|
|
|
|
|
|
|
len -= (*(data+1))*8;
|
|
|
|
data += (*(data+1))*8;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update_nd_link_layer_addr(unsigned char *data, int len, unsigned char *replace_mac)
|
|
|
|
{
|
|
|
|
struct icmp6hdr *icmphdr = (struct icmp6hdr *)data;
|
|
|
|
unsigned char *mac;
|
2013-05-19 04:28:07 +00:00
|
|
|
|
|
|
|
if (icmphdr->icmp6_type == NDISC_ROUTER_SOLICITATION) {
|
2013-05-08 21:45:39 +00:00
|
|
|
if (len >= 8) {
|
|
|
|
mac = scan_tlv(&data[8], len-8, 1, 1);
|
|
|
|
if (mac) {
|
2013-05-19 04:28:07 +00:00
|
|
|
memcpy(mac, replace_mac, 6);
|
|
|
|
return 1;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-21 15:31:26 +00:00
|
|
|
} else if (icmphdr->icmp6_type == NDISC_ROUTER_ADVERTISEMENT) {
|
2013-05-08 21:45:39 +00:00
|
|
|
if (len >= 16) {
|
|
|
|
mac = scan_tlv(&data[16], len-16, 1, 1);
|
|
|
|
if (mac) {
|
2013-05-19 04:28:07 +00:00
|
|
|
memcpy(mac, replace_mac, 6);
|
|
|
|
return 1;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
2013-05-19 04:28:07 +00:00
|
|
|
}
|
2013-07-21 15:31:26 +00:00
|
|
|
} else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
|
2013-05-08 21:45:39 +00:00
|
|
|
if (len >= 24) {
|
|
|
|
mac = scan_tlv(&data[24], len-24, 1, 1);
|
2013-05-19 04:28:07 +00:00
|
|
|
if (mac) {
|
|
|
|
memcpy(mac, replace_mac, 6);
|
|
|
|
return 1;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
2013-05-19 04:28:07 +00:00
|
|
|
}
|
2013-07-21 15:31:26 +00:00
|
|
|
} else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
|
2013-05-08 21:45:39 +00:00
|
|
|
if (len >= 24) {
|
|
|
|
mac = scan_tlv(&data[24], len-24, 2, 1);
|
|
|
|
if (mac) {
|
2013-05-19 04:28:07 +00:00
|
|
|
memcpy(mac, replace_mac, 6);
|
|
|
|
return 1;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
2013-05-19 04:28:07 +00:00
|
|
|
}
|
2013-07-21 15:31:26 +00:00
|
|
|
} else if (icmphdr->icmp6_type == NDISC_REDIRECT) {
|
2013-05-08 21:45:39 +00:00
|
|
|
if (len >= 40) {
|
|
|
|
mac = scan_tlv(&data[40], len-40, 2, 1);
|
2013-05-19 04:28:07 +00:00
|
|
|
if (mac) {
|
|
|
|
memcpy(mac, replace_mac, 6);
|
|
|
|
return 1;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
2013-05-19 04:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-08 21:45:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
static int __nat25_network_hash(unsigned char *networkAddr)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
2013-07-27 18:27:48 +00:00
|
|
|
if (networkAddr[0] == NAT25_IPV4) {
|
2013-05-08 21:45:39 +00:00
|
|
|
unsigned long x;
|
|
|
|
|
|
|
|
x = networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
|
|
|
|
|
|
|
|
return x & (NAT25_HASH_SIZE - 1);
|
2013-07-27 18:27:48 +00:00
|
|
|
} else if (networkAddr[0] == NAT25_IPX) {
|
2013-05-08 21:45:39 +00:00
|
|
|
unsigned long x;
|
|
|
|
|
|
|
|
x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
|
|
|
|
networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
|
|
|
|
|
|
|
|
return x & (NAT25_HASH_SIZE - 1);
|
2013-07-27 18:27:48 +00:00
|
|
|
} else if (networkAddr[0] == NAT25_APPLE) {
|
2013-05-08 21:45:39 +00:00
|
|
|
unsigned long x;
|
|
|
|
|
|
|
|
x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3];
|
|
|
|
|
|
|
|
return x & (NAT25_HASH_SIZE - 1);
|
2013-07-27 18:27:48 +00:00
|
|
|
} else if (networkAddr[0] == NAT25_PPPOE) {
|
2013-05-08 21:45:39 +00:00
|
|
|
unsigned long x;
|
|
|
|
|
|
|
|
x = networkAddr[0] ^ networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^ networkAddr[6] ^ networkAddr[7] ^ networkAddr[8];
|
|
|
|
|
|
|
|
return x & (NAT25_HASH_SIZE - 1);
|
2013-07-21 15:31:26 +00:00
|
|
|
} else if (networkAddr[0] == NAT25_IPV6) {
|
2013-05-08 21:45:39 +00:00
|
|
|
unsigned long x;
|
|
|
|
|
|
|
|
x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
|
|
|
|
networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10] ^
|
|
|
|
networkAddr[11] ^ networkAddr[12] ^ networkAddr[13] ^ networkAddr[14] ^ networkAddr[15] ^
|
|
|
|
networkAddr[16];
|
2013-05-19 04:28:07 +00:00
|
|
|
|
2013-05-08 21:45:39 +00:00
|
|
|
return x & (NAT25_HASH_SIZE - 1);
|
2013-07-21 15:31:26 +00:00
|
|
|
} else {
|
2013-05-08 21:45:39 +00:00
|
|
|
unsigned long x = 0;
|
|
|
|
int i;
|
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
for (i = 0; i < MAX_NETWORK_ADDR_LEN; i++)
|
2013-05-08 21:45:39 +00:00
|
|
|
x ^= networkAddr[i];
|
|
|
|
|
|
|
|
return x & (NAT25_HASH_SIZE - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
static void __network_hash_link(struct adapter *priv,
|
2013-05-08 21:45:39 +00:00
|
|
|
struct nat25_network_db_entry *ent, int hash)
|
|
|
|
{
|
2014-11-28 23:37:57 +00:00
|
|
|
/* Caller must spin_lock already! */
|
2013-05-08 21:45:39 +00:00
|
|
|
ent->next_hash = priv->nethash[hash];
|
2022-06-08 23:46:35 +00:00
|
|
|
if (ent->next_hash)
|
2013-05-08 21:45:39 +00:00
|
|
|
ent->next_hash->pprev_hash = &ent->next_hash;
|
|
|
|
priv->nethash[hash] = ent;
|
|
|
|
ent->pprev_hash = &priv->nethash[hash];
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
static void __network_hash_unlink(struct nat25_network_db_entry *ent)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
2014-11-28 23:37:57 +00:00
|
|
|
/* Caller must spin_lock already! */
|
2022-06-08 23:46:35 +00:00
|
|
|
*ent->pprev_hash = ent->next_hash;
|
|
|
|
if (ent->next_hash)
|
2013-05-08 21:45:39 +00:00
|
|
|
ent->next_hash->pprev_hash = ent->pprev_hash;
|
|
|
|
ent->next_hash = NULL;
|
|
|
|
ent->pprev_hash = NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-27 01:08:39 +00:00
|
|
|
static void __nat25_db_network_insert(struct adapter *priv,
|
2013-05-08 21:45:39 +00:00
|
|
|
unsigned char *macAddr, unsigned char *networkAddr)
|
|
|
|
{
|
|
|
|
struct nat25_network_db_entry *db;
|
|
|
|
int hash;
|
|
|
|
|
2014-11-29 00:42:41 +00:00
|
|
|
spin_lock_bh(&priv->br_ext_lock);
|
2013-05-08 21:45:39 +00:00
|
|
|
hash = __nat25_network_hash(networkAddr);
|
|
|
|
db = priv->nethash[hash];
|
2022-06-08 23:46:35 +00:00
|
|
|
while (db) {
|
2013-07-27 18:27:48 +00:00
|
|
|
if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
|
2013-05-08 21:45:39 +00:00
|
|
|
memcpy(db->macAddr, macAddr, ETH_ALEN);
|
|
|
|
db->ageing_timer = jiffies;
|
2014-11-29 00:42:41 +00:00
|
|
|
spin_unlock_bh(&priv->br_ext_lock);
|
2013-05-08 21:45:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
db = db->next_hash;
|
|
|
|
}
|
2022-06-08 23:46:35 +00:00
|
|
|
db = kmalloc(sizeof(*db), GFP_ATOMIC);
|
|
|
|
if (!db) {
|
2014-11-29 00:42:41 +00:00
|
|
|
spin_unlock_bh(&priv->br_ext_lock);
|
2013-05-08 21:45:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN);
|
|
|
|
memcpy(db->macAddr, macAddr, ETH_ALEN);
|
|
|
|
atomic_set(&db->use_count, 1);
|
|
|
|
db->ageing_timer = jiffies;
|
|
|
|
|
|
|
|
__network_hash_link(priv, db, hash);
|
|
|
|
|
2014-11-29 00:42:41 +00:00
|
|
|
spin_unlock_bh(&priv->br_ext_lock);
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NAT2.5 interface
|
|
|
|
*/
|
|
|
|
|
2013-07-27 01:08:39 +00:00
|
|
|
void nat25_db_cleanup(struct adapter *priv)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
|
|
|
int i;
|
2014-11-28 23:37:57 +00:00
|
|
|
|
2014-11-29 00:42:41 +00:00
|
|
|
spin_lock_bh(&priv->br_ext_lock);
|
2013-05-19 04:28:07 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
for (i = 0; i < NAT25_HASH_SIZE; i++) {
|
2013-05-08 21:45:39 +00:00
|
|
|
struct nat25_network_db_entry *f;
|
|
|
|
f = priv->nethash[i];
|
2022-06-08 23:46:35 +00:00
|
|
|
while (f) {
|
2013-05-08 21:45:39 +00:00
|
|
|
struct nat25_network_db_entry *g;
|
|
|
|
|
|
|
|
g = f->next_hash;
|
2013-07-27 18:27:48 +00:00
|
|
|
if (priv->scdb_entry == f) {
|
2013-05-08 21:45:39 +00:00
|
|
|
memset(priv->scdb_mac, 0, ETH_ALEN);
|
|
|
|
memset(priv->scdb_ip, 0, 4);
|
|
|
|
priv->scdb_entry = NULL;
|
|
|
|
}
|
|
|
|
__network_hash_unlink(f);
|
2013-10-19 17:45:47 +00:00
|
|
|
kfree(f);
|
2013-05-08 21:45:39 +00:00
|
|
|
f = g;
|
|
|
|
}
|
|
|
|
}
|
2014-11-29 00:42:41 +00:00
|
|
|
spin_unlock_bh(&priv->br_ext_lock);
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
|
2013-07-27 01:08:39 +00:00
|
|
|
void nat25_db_expire(struct adapter *priv)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
|
|
|
int i;
|
2014-11-28 23:37:57 +00:00
|
|
|
|
2014-11-29 00:42:41 +00:00
|
|
|
spin_lock_bh(&priv->br_ext_lock);
|
2013-05-19 04:28:07 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
for (i = 0; i < NAT25_HASH_SIZE; i++) {
|
|
|
|
struct nat25_network_db_entry *f;
|
|
|
|
f = priv->nethash[i];
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
while (f) {
|
2013-07-27 18:27:48 +00:00
|
|
|
struct nat25_network_db_entry *g;
|
|
|
|
g = f->next_hash;
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
if (__nat25_has_expired(f)) {
|
2013-07-27 18:27:48 +00:00
|
|
|
if (atomic_dec_and_test(&f->use_count)) {
|
|
|
|
if (priv->scdb_entry == f) {
|
|
|
|
memset(priv->scdb_mac, 0, ETH_ALEN);
|
|
|
|
memset(priv->scdb_ip, 0, 4);
|
|
|
|
priv->scdb_entry = NULL;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
2013-07-27 18:27:48 +00:00
|
|
|
__network_hash_unlink(f);
|
2013-10-19 17:45:47 +00:00
|
|
|
kfree(f);
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-27 18:27:48 +00:00
|
|
|
f = g;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-29 00:42:41 +00:00
|
|
|
spin_unlock_bh(&priv->br_ext_lock);
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
|
2013-07-27 01:08:39 +00:00
|
|
|
int nat25_db_handle(struct adapter *priv, struct sk_buff *skb, int method)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
|
|
|
unsigned short protocol;
|
|
|
|
unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
|
2013-07-09 22:38:46 +00:00
|
|
|
unsigned int tmp;
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
if (!skb)
|
2013-05-08 21:45:39 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-05-09 04:04:25 +00:00
|
|
|
if ((method <= NAT25_MIN) || (method >= NAT25_MAX))
|
2013-05-08 21:45:39 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-07-09 22:38:46 +00:00
|
|
|
protocol = be16_to_cpu(*((__be16 *)(skb->data + 2 * ETH_ALEN)));
|
2013-05-08 21:45:39 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
/* Handle IP frame */
|
|
|
|
/*---------------------------------------------------*/
|
2013-07-27 18:27:48 +00:00
|
|
|
if (protocol == ETH_P_IP) {
|
|
|
|
struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
if (((unsigned char *)(iph) + (iph->ihl << 2)) >= (skb->data + ETH_HLEN + skb->len))
|
2013-05-08 21:45:39 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
switch (method) {
|
|
|
|
case NAT25_CHECK:
|
|
|
|
return -1;
|
|
|
|
case NAT25_INSERT:
|
2013-11-29 22:10:20 +00:00
|
|
|
/* some multicast with source IP is all zero, maybe other case is illegal */
|
2013-07-27 18:27:48 +00:00
|
|
|
/* in class A, B, C, host address is all zero or all one is illegal */
|
|
|
|
if (iph->saddr == 0)
|
2013-05-08 21:45:39 +00:00
|
|
|
return 0;
|
2013-07-27 18:27:48 +00:00
|
|
|
tmp = be32_to_cpu(iph->saddr);
|
|
|
|
__nat25_generate_ipv4_network_addr(networkAddr, &tmp);
|
|
|
|
/* record source IP address and , source mac address into db */
|
|
|
|
__nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
2013-07-27 18:27:48 +00:00
|
|
|
} else if (protocol == ETH_P_ARP) {
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
/* Handle ARP frame */
|
|
|
|
/*---------------------------------------------------*/
|
2013-05-08 21:45:39 +00:00
|
|
|
struct arphdr *arp = (struct arphdr *)(skb->data + ETH_HLEN);
|
|
|
|
unsigned char *arp_ptr = (unsigned char *)(arp + 1);
|
2022-06-08 23:46:35 +00:00
|
|
|
unsigned int *sender;
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
if (arp->ar_pro != htons(ETH_P_IP))
|
2013-05-08 21:45:39 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
switch (method) {
|
|
|
|
case NAT25_CHECK:
|
|
|
|
return 0; /* skb_copy for all ARP frame */
|
|
|
|
case NAT25_INSERT:
|
|
|
|
/* change to ARP sender mac address to wlan STA address */
|
|
|
|
memcpy(arp_ptr, GET_MY_HWADDR(priv), ETH_ALEN);
|
|
|
|
arp_ptr += arp->ar_hln;
|
|
|
|
sender = (unsigned int *)arp_ptr;
|
|
|
|
__nat25_generate_ipv4_network_addr(networkAddr, sender);
|
|
|
|
__nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
2013-07-27 18:27:48 +00:00
|
|
|
} else if ((protocol == ETH_P_PPP_DISC) ||
|
|
|
|
(protocol == ETH_P_PPP_SES)) {
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
/* Handle PPPoE frame */
|
|
|
|
/*---------------------------------------------------*/
|
2013-05-08 21:45:39 +00:00
|
|
|
struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
|
2013-11-29 22:10:20 +00:00
|
|
|
unsigned short *pMagic;
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
switch (method) {
|
|
|
|
case NAT25_CHECK:
|
|
|
|
if (ph->sid == 0)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
case NAT25_INSERT:
|
|
|
|
if (ph->sid == 0) { /* Discovery phase according to tag */
|
|
|
|
if (ph->code == PADI_CODE || ph->code == PADR_CODE) {
|
|
|
|
if (priv->ethBrExtInfo.addPPPoETag) {
|
|
|
|
struct pppoe_tag *tag, *pOldTag;
|
|
|
|
unsigned char tag_buf[40];
|
|
|
|
int old_tag_len = 0;
|
|
|
|
|
|
|
|
tag = (struct pppoe_tag *)tag_buf;
|
|
|
|
pOldTag = (struct pppoe_tag *)__nat25_find_pppoe_tag(ph, ntohs(PTT_RELAY_SID));
|
|
|
|
if (pOldTag) { /* if SID existed, copy old value and delete it */
|
|
|
|
old_tag_len = ntohs(pOldTag->tag_len);
|
2022-06-08 23:46:35 +00:00
|
|
|
if (old_tag_len +
|
|
|
|
TAG_HDR_LEN +
|
|
|
|
MAGIC_CODE_LEN +
|
|
|
|
RTL_RELAY_TAG_LEN >
|
|
|
|
sizeof(tag_buf))
|
2013-07-27 18:27:48 +00:00
|
|
|
return -1;
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
memcpy(tag->tag_data+MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN,
|
|
|
|
pOldTag->tag_data, old_tag_len);
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
if (skb_pull_and_merge(skb, (unsigned char *)pOldTag, TAG_HDR_LEN+old_tag_len) < 0)
|
2013-05-08 21:45:39 +00:00
|
|
|
return -1;
|
2022-06-08 23:46:35 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
ph->length = htons(ntohs(ph->length)-TAG_HDR_LEN-old_tag_len);
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
tag->tag_type = PTT_RELAY_SID;
|
|
|
|
tag->tag_len = htons(MAGIC_CODE_LEN+RTL_RELAY_TAG_LEN+old_tag_len);
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
/* insert the magic_code+client mac in relay tag */
|
2013-11-29 22:10:20 +00:00
|
|
|
pMagic = (unsigned short *)tag->tag_data;
|
2013-07-27 18:27:48 +00:00
|
|
|
*pMagic = htons(MAGIC_CODE);
|
|
|
|
memcpy(tag->tag_data+MAGIC_CODE_LEN, skb->data+ETH_ALEN, ETH_ALEN);
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
/* Add relay tag */
|
|
|
|
if (__nat25_add_pppoe_tag(skb, tag) < 0)
|
2013-05-08 21:45:39 +00:00
|
|
|
return -1;
|
2013-07-27 18:27:48 +00:00
|
|
|
} else { /* not add relay tag */
|
|
|
|
if (priv->pppoe_connection_in_progress &&
|
2022-06-08 23:46:35 +00:00
|
|
|
memcmp(skb->data + ETH_ALEN,
|
|
|
|
priv->pppoe_addr,
|
|
|
|
ETH_ALEN))
|
2013-07-27 18:27:48 +00:00
|
|
|
return -2;
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
if (priv->pppoe_connection_in_progress == 0)
|
|
|
|
memcpy(priv->pppoe_addr, skb->data+ETH_ALEN, ETH_ALEN);
|
2013-05-08 21:45:39 +00:00
|
|
|
|
|
|
|
priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;
|
|
|
|
}
|
2013-07-27 18:27:48 +00:00
|
|
|
} else {
|
|
|
|
return -1;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
2013-07-27 18:27:48 +00:00
|
|
|
} else { /* session phase */
|
2022-06-08 23:46:35 +00:00
|
|
|
__nat25_generate_pppoe_network_addr(networkAddr, skb->data, &ph->sid);
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
__nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
if (!priv->ethBrExtInfo.addPPPoETag &&
|
|
|
|
priv->pppoe_connection_in_progress &&
|
|
|
|
!memcmp(skb->data+ETH_ALEN, priv->pppoe_addr, ETH_ALEN))
|
|
|
|
priv->pppoe_connection_in_progress = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
2013-07-27 18:27:48 +00:00
|
|
|
} else if (protocol == 0x888e) {
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
/* Handle EAP frame */
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
switch (method) {
|
|
|
|
case NAT25_CHECK:
|
|
|
|
return -1;
|
|
|
|
case NAT25_INSERT:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if ((protocol == 0xe2ae) || (protocol == 0xe2af)) {
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
/* Handle C-Media proprietary frame */
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
switch (method) {
|
|
|
|
case NAT25_CHECK:
|
|
|
|
return -1;
|
|
|
|
case NAT25_INSERT:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (protocol == ETH_P_IPV6) {
|
|
|
|
/*------------------------------------------------*/
|
|
|
|
/* Handle IPV6 frame */
|
|
|
|
/*------------------------------------------------*/
|
2013-05-08 21:45:39 +00:00
|
|
|
struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
if (sizeof(*iph) >= (skb->len - ETH_HLEN))
|
2013-05-08 21:45:39 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
switch (method) {
|
|
|
|
case NAT25_CHECK:
|
|
|
|
if (skb->data[0] & 1)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
case NAT25_INSERT:
|
|
|
|
if (memcmp(&iph->saddr, "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 16)) {
|
|
|
|
__nat25_generate_ipv6_network_addr(networkAddr, (unsigned int *)&iph->saddr);
|
|
|
|
__nat25_db_network_insert(priv, skb->data+ETH_ALEN, networkAddr);
|
|
|
|
|
|
|
|
if (iph->nexthdr == IPPROTO_ICMPV6 &&
|
|
|
|
skb->len > (ETH_HLEN + sizeof(*iph) + 4)) {
|
|
|
|
if (update_nd_link_layer_addr(skb->data + ETH_HLEN + sizeof(*iph),
|
|
|
|
skb->len - ETH_HLEN - sizeof(*iph), GET_MY_HWADDR(priv))) {
|
|
|
|
struct icmp6hdr *hdr = (struct icmp6hdr *)(skb->data + ETH_HLEN + sizeof(*iph));
|
|
|
|
hdr->icmp6_cksum = 0;
|
|
|
|
hdr->icmp6_cksum = csum_ipv6_magic(&iph->saddr, &iph->daddr,
|
2013-11-29 22:10:20 +00:00
|
|
|
iph->payload_len,
|
2013-07-27 18:27:48 +00:00
|
|
|
IPPROTO_ICMPV6,
|
2013-11-29 22:10:20 +00:00
|
|
|
csum_partial((__u8 *)hdr, iph->payload_len, 0));
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-27 18:27:48 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SERVER_PORT 67
|
|
|
|
#define CLIENT_PORT 68
|
|
|
|
#define DHCP_MAGIC 0x63825363
|
|
|
|
#define BROADCAST_FLAG 0x8000
|
|
|
|
|
|
|
|
struct dhcpMessage {
|
|
|
|
u_int8_t op;
|
|
|
|
u_int8_t htype;
|
|
|
|
u_int8_t hlen;
|
|
|
|
u_int8_t hops;
|
|
|
|
u_int32_t xid;
|
2022-06-08 23:46:35 +00:00
|
|
|
__be16 secs;
|
|
|
|
__be16 flags;
|
|
|
|
__be32 ciaddr;
|
|
|
|
__be32 yiaddr;
|
|
|
|
__be32 siaddr;
|
|
|
|
__be32 giaddr;
|
2013-05-08 21:45:39 +00:00
|
|
|
u_int8_t chaddr[16];
|
|
|
|
u_int8_t sname[64];
|
|
|
|
u_int8_t file[128];
|
2022-06-08 23:46:35 +00:00
|
|
|
__be32 cookie;
|
2013-05-08 21:45:39 +00:00
|
|
|
u_int8_t options[308]; /* 312 - cookie */
|
|
|
|
};
|
|
|
|
|
2013-07-27 01:08:39 +00:00
|
|
|
void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
2022-06-08 23:46:35 +00:00
|
|
|
if (!skb)
|
2013-05-08 21:45:39 +00:00
|
|
|
return;
|
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
if (!priv->ethBrExtInfo.dhcp_bcst_disable) {
|
2013-07-09 22:38:46 +00:00
|
|
|
__be16 protocol = *((__be16 *)(skb->data + 2 * ETH_ALEN));
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
if (protocol == __constant_htons(ETH_P_IP)) { /* IP */
|
|
|
|
struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-27 18:27:48 +00:00
|
|
|
if (iph->protocol == IPPROTO_UDP) { /* UDP */
|
2013-07-24 17:19:14 +00:00
|
|
|
struct udphdr *udph = (struct udphdr *)((size_t)iph + (iph->ihl << 2));
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-09 22:38:46 +00:00
|
|
|
if ((udph->source == __constant_htons(CLIENT_PORT)) &&
|
|
|
|
(udph->dest == __constant_htons(SERVER_PORT))) { /* DHCP request */
|
2013-05-08 21:45:39 +00:00
|
|
|
struct dhcpMessage *dhcph =
|
2013-07-24 17:19:14 +00:00
|
|
|
(struct dhcpMessage *)((size_t)udph + sizeof(struct udphdr));
|
2013-07-09 22:38:46 +00:00
|
|
|
u32 cookie = be32_to_cpu((__be32)dhcph->cookie);
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-09 22:38:46 +00:00
|
|
|
if (cookie == DHCP_MAGIC) { /* match magic word */
|
2013-07-27 18:27:48 +00:00
|
|
|
if (!(dhcph->flags & htons(BROADCAST_FLAG))) {
|
|
|
|
/* if not broadcast */
|
2013-11-29 22:10:20 +00:00
|
|
|
register int sum = 0;
|
2013-05-08 21:45:39 +00:00
|
|
|
|
2013-07-09 22:38:46 +00:00
|
|
|
/* or BROADCAST flag */
|
2013-05-08 21:45:39 +00:00
|
|
|
dhcph->flags |= htons(BROADCAST_FLAG);
|
2013-07-09 22:38:46 +00:00
|
|
|
/* recalculate checksum */
|
2013-11-29 22:10:20 +00:00
|
|
|
sum = ~(udph->check) & 0xffff;
|
2013-07-09 22:38:46 +00:00
|
|
|
sum += be16_to_cpu(dhcph->flags);
|
2013-05-09 04:04:25 +00:00
|
|
|
while (sum >> 16)
|
2013-05-08 21:45:39 +00:00
|
|
|
sum = (sum & 0xffff) + (sum >> 16);
|
2013-11-29 22:10:20 +00:00
|
|
|
udph->check = ~sum;
|
2013-05-08 21:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:46:35 +00:00
|
|
|
void *scdb_findEntry(struct adapter *priv, unsigned char *ipAddr)
|
2013-05-08 21:45:39 +00:00
|
|
|
{
|
|
|
|
unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
|
|
|
|
struct nat25_network_db_entry *db;
|
|
|
|
int hash;
|
|
|
|
|
|
|
|
__nat25_generate_ipv4_network_addr(networkAddr, (unsigned int *)ipAddr);
|
|
|
|
hash = __nat25_network_hash(networkAddr);
|
|
|
|
db = priv->nethash[hash];
|
2022-06-08 23:46:35 +00:00
|
|
|
while (db) {
|
2013-05-09 04:04:25 +00:00
|
|
|
if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
|
2013-05-08 21:45:39 +00:00
|
|
|
return (void *)db;
|
|
|
|
}
|
|
|
|
|
|
|
|
db = db->next_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|