Skip to content

Commit

Permalink
nat46: Bump to version 2024-08-12
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaY-shi committed Oct 20, 2024
1 parent 208a792 commit abbecb4
Show file tree
Hide file tree
Showing 18 changed files with 2,520 additions and 4 deletions.
16 changes: 12 additions & 4 deletions package/kernel/nat46/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=nat46

PKG_MIRROR_HASH:=09b93f31d10030d3b4f326066b544b70b1f60236d0482f27c384ed93b298c0a6
PKG_SOURCE_URL:=https://github.com/ayourtch/nat46.git
PKG_SOURCE_DATE:=2022-09-19
PKG_SOURCE_DATE:=2024-08-12
PKG_SOURCE_PROTO:=git
PKG_SOURCE_VERSION:=4c5beee236841724219598fabb1edc93d4f08ce5
PKG_SOURCE_VERSION:=b42c37d5c6dee4593df0fc60b37bc7ec4ee243a4
PKG_MIRROR_HASH:=2d7bbb6cc22808f7e81d716e6bd7a27d9978fb802496304851946627e21534a5

PKG_MAINTAINER:=Hans Dedecker <dedeckeh@gmail.com>
PKG_LICENSE:=GPL-2.0

PKG_BUILD_PARALLEL:=1

include $(INCLUDE_DIR)/package.mk

define KernelPackage/nat46
Expand All @@ -25,11 +27,17 @@ endef

include $(INCLUDE_DIR)/kernel-defaults.mk

define Build/InstallDev
$(INSTALL_DIR) $(STAGING_DIR)/usr/include/nat46
$(INSTALL_DATA) $(PKG_BUILD_DIR)/nat46/modules/*.h $(STAGING_DIR)/usr/include/nat46/
endef

define Build/Compile
$(KERNEL_MAKE) M="$(PKG_BUILD_DIR)/nat46/modules" \
+$(KERNEL_MAKE) M="$(PKG_BUILD_DIR)/nat46/modules" \
MODFLAGS="-DMODULE -mlong-calls" \
EXTRA_CFLAGS="-DNAT46_VERSION=\\\"$(PKG_SOURCE_VERSION)\\\"" \
modules
$(INSTALL_DATA) $(PKG_BUILD_DIR)/nat46/modules/Module.symvers $(PKG_BUILD_DIR)/Module.symvers
endef

$(eval $(call KernelPackage,nat46))
30 changes: 30 additions & 0 deletions package/kernel/nat46/patches/101-skb-reset.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Author: Pavithra R <pavir@codeaurora.org>
Date: Sun Sep 20 13:33:42 2020 +0530

nat46: Add skb_ext_reset to reset skb extensions

This patch adds support to reset the skb extensions before
resetting the netfilter. Without the change, conntrack
is in invalid state and traffic gets dropped.

Change-Id: I24ee6fe8a9a9dec09d61d8e716fff587f65e4e4f
Signed-off-by: Pavithra R <pavir@codeaurora.org>

--- a/nat46/modules/nat46-core.c
+++ b/nat46/modules/nat46-core.c
@@ -1710,6 +1710,7 @@ int nat46_ipv6_input(struct sk_buff *old
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,4,0)
nf_reset(new_skb);
#else
+ skb_ext_reset(new_skb);
nf_reset_ct(new_skb);
#endif

@@ -1936,6 +1937,7 @@ int nat46_ipv4_input(struct sk_buff *old
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,4,0)
nf_reset(new_skb);
#else
+ skb_ext_reset(new_skb);
nf_reset_ct(new_skb);
#endif

209 changes: 209 additions & 0 deletions package/kernel/nat46/patches/102-mapt.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
Author: Pavithra R <pavir@codeaurora.org>
Date: Sat Aug 1 13:27:20 2020 +0530

nat46: Export APIs for acceleration engine support in nat46 for kernel 5.4

This patch is propagated from kernel 4.4 commit
861e64a607fd22d5af089cf56539f42a2e31d581

The patch defines and exports APIs in nat46 to be used for accelaration.

Change-Id: I7934b15544953f870d3595b8b359433b4fff7c30
Signed-off-by: Pavithra R <pavir@codeaurora.org>

--- a/nat46/modules/nat46-core.c
+++ b/nat46/modules/nat46-core.c
@@ -1497,7 +1497,6 @@ static uint16_t nat46_fixup_icmp_dest_un
return 0;
}

-
/* Fixup ICMP->ICMP6 before IP header translation, according to http://tools.ietf.org/html/rfc6145 */

static uint16_t nat46_fixup_icmp(nat46_instance_t *nat46, struct iphdr *iph, struct sk_buff *old_skb) {
@@ -1579,6 +1578,10 @@ int pairs_xlate_v6_to_v4_outer(nat46_ins
return ( (xlate_src >= 0) && (xlate_dst >= 0) );
}

+int xlate_6_to_4(struct net_device *dev, struct ipv6hdr *ip6h, uint16_t proto, __u32 *pv4saddr, __u32 *pv4daddr) {
+ return pairs_xlate_v6_to_v4_outer(netdev_nat46_instance(dev), ip6h, proto, pv4saddr, pv4daddr);
+}
+EXPORT_SYMBOL(xlate_6_to_4);

int nat46_ipv6_input(struct sk_buff *old_skb) {
struct ipv6hdr *ip6h = ipv6_hdr(old_skb);
@@ -1733,6 +1736,10 @@ int nat46_ipv6_input(struct sk_buff *old

nat46debug(5, "about to send v4 packet, flags: %02x", IPCB(new_skb)->flags);
nat46_netdev_count_xmit(new_skb, old_skb->dev);
+
+ /* set skb->iif */
+ new_skb->skb_iif = old_skb->skb_iif;
+
netif_rx(new_skb);

/* TBD: should copy be released here? */
@@ -1841,6 +1848,10 @@ int pairs_xlate_v4_to_v6_outer(nat46_ins
return 0;
}

+int xlate_4_to_6(struct net_device *dev, struct iphdr *hdr4, uint16_t sport, uint16_t dport, void *v6saddr, void *v6daddr) {
+ return pairs_xlate_v4_to_v6_outer(netdev_nat46_instance(dev), hdr4, &sport, &dport, v6saddr, v6daddr);
+}
+EXPORT_SYMBOL(xlate_4_to_6);

int nat46_ipv4_input(struct sk_buff *old_skb) {
nat46_instance_t *nat46 = get_nat46_instance(old_skb);
@@ -1981,6 +1992,10 @@ int nat46_ipv4_input(struct sk_buff *old

nat46debug(5, "about to send v6 packet, flags: %02x", IP6CB(new_skb)->flags);
nat46_netdev_count_xmit(new_skb, old_skb->dev);
+
+ /* set skb->iif */
+ new_skb->skb_iif = old_skb->skb_iif;
+
netif_rx(new_skb);

done:
@@ -1988,4 +2003,22 @@ done:
return err;
}

+int nat46_get_npairs(struct net_device *dev) {
+ nat46_instance_t *nat46 = netdev_nat46_instance(dev);
+ return nat46->npairs;
+}
+EXPORT_SYMBOL(nat46_get_npairs);

+bool nat46_get_rule_config(struct net_device *dev, nat46_xlate_rulepair_t **nat46_rule_pair, int *count) {
+ nat46_instance_t *nat46 = netdev_nat46_instance(dev);
+ if (nat46->npairs < 1) {
+ /*
+ * no rules ?
+ */
+ return false;
+ }
+ *count = nat46->npairs;
+ *nat46_rule_pair = nat46->pairs;
+ return true;
+}
+EXPORT_SYMBOL(nat46_get_rule_config);
--- a/nat46/modules/nat46-core.h
+++ b/nat46/modules/nat46-core.h
@@ -42,18 +42,18 @@ typedef enum {
#define NAT46_SIGNATURE 0x544e3634
#define FREED_NAT46_SIGNATURE 0xdead544e

-typedef struct {
+typedef struct nat46_xlate_rule {
nat46_xlate_style_t style;
struct in6_addr v6_pref;
- int v6_pref_len;
- u32 v4_pref;
- int v4_pref_len;
- int ea_len;
- int psid_offset;
- int fmr_flag;
+ int v6_pref_len;
+ u32 v4_pref;
+ int v4_pref_len;
+ int ea_len;
+ int psid_offset;
+ int fmr_flag;
} nat46_xlate_rule_t;

-typedef struct {
+typedef struct nat46_xlate_rulepair {
nat46_xlate_rule_t local;
nat46_xlate_rule_t remote;
} nat46_xlate_rulepair_t;
@@ -82,4 +82,9 @@ nat46_instance_t *get_nat46_instance(str
nat46_instance_t *alloc_nat46_instance(int npairs, nat46_instance_t *old, int from_ipair, int to_ipair, int remove_ipair);
void release_nat46_instance(nat46_instance_t *nat46);

+int xlate_6_to_4(struct net_device *dev, struct ipv6hdr *ip6h, uint16_t proto, __u32 *pv4saddr, __u32 *pv4daddr);
+int xlate_4_to_6(struct net_device *dev, struct iphdr *hdr4, uint16_t sport, uint16_t dport, void *v6saddr, void *v6daddr);
+bool nat46_get_rule_config(struct net_device *dev, nat46_xlate_rulepair_t **nat46_rule_pair, int *count);
+int nat46_get_npairs(struct net_device *dev);
+
#endif
--- a/nat46/modules/nat46-netdev.c
+++ b/nat46/modules/nat46-netdev.c
@@ -24,10 +24,12 @@
#include <net/ip6_route.h>
#include <net/ipv6.h>
#include <linux/version.h>
+#include <linux/radix-tree.h>
#include "nat46-core.h"
#include "nat46-module.h"

#define NETDEV_DEFAULT_NAME "nat46."
+static RADIX_TREE(netdev_tree, GFP_ATOMIC);

typedef struct {
u32 sig;
@@ -83,6 +85,18 @@ void nat46_netdev_count_xmit(struct sk_b
dev->stats.tx_bytes += skb->len;
}

+void nat46_update_stats(struct net_device *dev, uint32_t rx_packets, uint32_t rx_bytes,
+ uint32_t tx_packets, uint32_t tx_bytes, uint32_t rx_dropped, uint32_t tx_dropped)
+{
+ dev->stats.rx_packets += rx_packets;
+ dev->stats.rx_bytes += rx_bytes;
+ dev->stats.tx_packets += tx_packets;
+ dev->stats.tx_bytes += tx_bytes;
+ dev->stats.rx_dropped += rx_dropped;
+ dev->stats.tx_dropped += tx_dropped;
+}
+EXPORT_SYMBOL(nat46_update_stats);
+
void *netdev_nat46_instance(struct net_device *dev) {
nat46_netdev_priv_t *priv = netdev_priv(dev);
return priv->nat46;
@@ -160,6 +174,11 @@ int nat46_netdev_create(struct net *net,
printk("nat46: netdevice nat46 '%s' created successfully.\n", devname);
kfree(devname);

+ /*
+ * add this netdevice to list
+ */
+ radix_tree_insert(&netdev_tree, (*dev)->ifindex, (void *)*dev);
+
return 0;

err_register_dev:
@@ -176,10 +195,24 @@ void nat46_netdev_destroy(struct net_dev
netif_stop_queue(dev);
netdev_nat46_set_instance(dev, NULL);
unregister_netdev(dev);
+ radix_tree_delete(&netdev_tree, dev->ifindex);
free_netdev(dev);
printk("nat46: Destroying nat46 device.\n");
}

+bool is_map_t_dev(struct net_device *dev)
+{
+ if(!dev) {
+ return false;
+ }
+
+ if(radix_tree_lookup(&netdev_tree, dev->ifindex)) {
+ return true;
+ }
+ return false;
+}
+EXPORT_SYMBOL(is_map_t_dev);
+
static int is_nat46(struct net_device *dev) {
nat46_netdev_priv_t *priv = netdev_priv(dev);
return (priv && (NAT46_DEVICE_SIGNATURE == priv->sig));
--- a/nat46/modules/nat46-netdev.h
+++ b/nat46/modules/nat46-netdev.h
@@ -26,3 +26,6 @@ void nat64_show_all_configs(struct net *
void nat46_netdev_count_xmit(struct sk_buff *skb, struct net_device *dev);
void *netdev_nat46_instance(struct net_device *dev);

+void nat46_update_stats(struct net_device *dev, uint32_t rx_packets, uint32_t rx_bytes, uint32_t tx_packets, uint32_t tx_bytes,
+ uint32_t rx_dropped, uint32_t tx_dropped);
+bool is_map_t_dev(struct net_device *dev);
39 changes: 39 additions & 0 deletions package/kernel/nat46/patches/103-tos.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Author: Pavithra R <pavir@codeaurora.org>
Date: Sat Aug 1 13:55:33 2020 +0530

nat46: Set IPv6 traffic class from IPv4 ToS value

Set IPv6 traffic class from IPv4 ToS value during
IPv4 to IPv6 translation and vice-versa.

This patch is propagated from kernel 4.4 commit
1cd3b55b059d4513649bb73bc69da931ed3beb7b

Change-Id: Ia14e53447e829c8648c01656237ac902ad8674ec
Signed-off-by: Pavithra R <pavir@codeaurora.org>

--- a/nat46/modules/nat46-core.c
+++ b/nat46/modules/nat46-core.c
@@ -886,11 +886,12 @@ void *get_next_header_ptr6(void *pv6, in
}

void fill_v4hdr_from_v6hdr(struct iphdr * iph, struct ipv6hdr *ip6h, __u32 v4saddr, __u32 v4daddr, __u16 id, __u16 frag_off, __u16 proto, int l3_payload_len) {
+ uint32_t ver_class_flow = ntohl(*(__be32 *)ip6h);
iph->ttl = ip6h->hop_limit;
iph->saddr = v4saddr;
iph->daddr = v4daddr;
iph->protocol = proto;
- *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (0x00/*tos*/ & 0xff));
+ *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | ((ver_class_flow >> 20) & 0xff));
iph->frag_off = frag_off;
iph->id = id;
iph->tot_len = htons( l3_payload_len + IPV4HDRSIZE );
@@ -1859,7 +1860,7 @@ int nat46_ipv4_input(struct sk_buff *old
uint16_t sport = 0, dport = 0;

int err = 0;
- int tclass = 0;
+ uint8_t tclass = 0;
int flowlabel = 0;
int check_for_l4 = 0;
int having_l4 = 0;
Loading

0 comments on commit abbecb4

Please sign in to comment.