1 From 043efc0e619e04661be2b1889382db2fdd378145 Mon Sep 17 00:00:00 2001
2 From: John Crispin <john@phrozen.org>
3 Date: Thu, 10 Aug 2017 16:34:36 +0200
4 Subject: [PATCH 56/57] net: mediatek: add hw nat support
6 Signed-off-by: John Crispin <john@phrozen.org>
8 drivers/net/ethernet/mediatek/Kconfig | 7 +++++++
9 drivers/net/ethernet/mediatek/Makefile | 1 +
10 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 13 +++++++++++++
11 net/netfilter/nf_conntrack_proto_tcp.c | 19 +++++++++++++++++++
12 4 files changed, 40 insertions(+)
14 --- a/drivers/net/ethernet/mediatek/Kconfig
15 +++ b/drivers/net/ethernet/mediatek/Kconfig
16 @@ -14,4 +14,11 @@ config NET_MEDIATEK_SOC
17 This driver supports the gigabit ethernet MACs in the
18 MediaTek MT2701/MT7623 chipset family.
20 +config NET_MEDIATEK_HNAT
21 + tristate "MediaTek MT7623 hardware NAT support"
22 + depends on NET_MEDIATEK_SOC && NF_CONNTRACK && NF_CONNTRACK_IPV4 && IP_NF_NAT && IP_NF_TARGET_MASQUERADE
24 + This driver supports the hardwaer NAT in the
25 + MediaTek MT2701/MT7623 chipset family.
27 endif #NET_VENDOR_MEDIATEK
28 --- a/drivers/net/ethernet/mediatek/Makefile
29 +++ b/drivers/net/ethernet/mediatek/Makefile
33 obj-$(CONFIG_NET_MEDIATEK_SOC) += mtk_eth_soc.o
34 +obj-$(CONFIG_NET_MEDIATEK_HNAT) += mtk_hnat/
35 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
36 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
38 #include <linux/reset.h>
39 #include <linux/tcp.h>
41 +#if defined(CONFIG_NET_MEDIATEK_HNAT) || defined(CONFIG_NET_MEDIATEK_HNAT_MODULE)
42 +#include "mtk_hnat/nf_hnat_mtk.h"
45 #include "mtk_eth_soc.h"
47 static int mtk_msg_level = -1;
48 @@ -649,6 +653,11 @@ static int mtk_tx_map(struct sk_buff *sk
51 /* set the forward port */
52 +#if defined(CONFIG_NET_MEDIATEK_HNAT) || defined(CONFIG_NET_MEDIATEK_HNAT_MODULE)
53 + if (HNAT_SKB_CB2(skb)->magic == 0x78681415)
54 + fport |= 0x4 << TX_DMA_FPORT_SHIFT;
57 fport = (mac->id + 1) << TX_DMA_FPORT_SHIFT;
60 @@ -1013,6 +1022,10 @@ static int mtk_poll_rx(struct napi_struc
61 RX_DMA_VID(trxd.rxd3))
62 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
63 RX_DMA_VID(trxd.rxd3));
64 +#if defined(CONFIG_NET_MEDIATEK_HNAT) || defined(CONFIG_NET_MEDIATEK_HNAT_MODULE)
65 + *(u32 *)(skb->head) = trxd.rxd4;
66 + skb_hnat_alg(skb) = 0;
68 skb_record_rx_queue(skb, 0);
69 napi_gro_receive(napi, skb);
71 --- a/net/netfilter/nf_conntrack_proto_tcp.c
72 +++ b/net/netfilter/nf_conntrack_proto_tcp.c
74 #include <linux/types.h>
75 #include <linux/timer.h>
76 #include <linux/module.h>
77 +#include <linux/inetdevice.h>
79 #include <linux/tcp.h>
80 #include <linux/spinlock.h>
82 #include <net/ip6_checksum.h>
83 #include <asm/unaligned.h>
88 #include <linux/netfilter.h>
89 @@ -53,6 +55,11 @@ static int nf_ct_tcp_max_retrans __read_
90 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
91 closely. They're more complex. --RR */
93 +#ifndef IPV4_DEVCONF_DFLT
94 + #define IPV4_DEVCONF_DFLT(net, attr) \
95 + IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr)
98 static const char *const tcp_conntrack_names[] = {
101 @@ -519,6 +526,18 @@ static bool tcp_in_window(const struct n
102 if (nf_ct_tcp_no_window_check)
106 + if ((net->ipv4.devconf_all && net->ipv4.devconf_dflt && net->ipv6.devconf_all) &&
107 + net->ipv6.devconf_dflt) {
108 + if ((IPV4_DEVCONF_DFLT(net, FORWARDING) ||
109 + IPV4_DEVCONF_ALL(net, FORWARDING)) ||
110 + (net->ipv6.devconf_all->forwarding ||
111 + net->ipv6.devconf_dflt->forwarding)) {
118 * Get the required data from the packet.