[PATCH net-next v6 03/12] amt: key relay tunnels on a union amt_addr endpoint
From: Omar Ramadan
Date: Tue Dec 01 2026 - 23:32:35 EST
A relay finds a tunnel by the gateway's IPv4 address, a __be32 taken
from ip_hdr(skb)->saddr, and its UDP port. On an IPv6 outer transport
that would read four bytes from the middle of the source address.
Store the endpoint as a union amt_addr, moved above struct
amt_tunnel_list. Its ip6 member is not under CONFIG_IPV6, so code that
only copies or compares an endpoint needs no #if, and the tunnel needs
no family of its own, as the socket fixes it. amt_outer_saddr() copies
the outer source by value before a pull can move the header, and
amt_update_handler() and amt_request_handler() use it. The Update MAC
becomes amt_relay_mac(), one siphash() over {address, port, nonce}; only
the relay checks it, so the IPv4 value is not visible on the wire.
The per-source tunnel cap counts an IPv6 /64, not an address
(amt_same_source()): one host normally owns a /64 and could otherwise
hold the cap from each of its addresses. Gateways in one /64 share the
cap but keep their own tunnel, found by full endpoint, so a full table
refuses new gateways and evicts none. A party delegated a shorter prefix
still gets one source per /64 it routes: with the default max_tunnels
the cap is 16, so eight /64s fill the table, as eight addresses do for
IPv4. The relay cannot see the delegation size; the cap bounds one
source's share, not the number of sources.
amt_v6() is still false for every device, so there is no functional
change.
Assisted-by: LLM
Signed-off-by: Omar Ramadan <omar@xxxxxxxxxxxxx>
---
drivers/net/amt.c | 113 +++++++++++++++++++++++++++++++---------------
include/net/amt.h | 18 ++++----
2 files changed, 86 insertions(+), 45 deletions(-)
diff --git a/drivers/net/amt.c b/drivers/net/amt.c
index e3c69ebf7..bfdce6897 100644
--- a/drivers/net/amt.c
+++ b/drivers/net/amt.c
@@ -96,6 +96,29 @@ static bool amt_v6(const struct amt_dev *amt)
return IS_ENABLED(CONFIG_IPV6) && !ipv6_addr_any(&amt->local_ipv6);
}
+static void amt_outer_saddr(const struct amt_dev *amt,
+ const struct sk_buff *skb, union amt_addr *addr)
+{
+ memset(addr, 0, sizeof(*addr));
+ if (amt_v6(amt))
+ addr->ip6 = ipv6_hdr(skb)->saddr;
+ else
+ addr->ip4 = ip_hdr(skb)->saddr;
+}
+
+/* One siphash() over the padded endpoint serves both families. */
+static u64 amt_relay_mac(const union amt_addr *addr, __be16 port, __be32 nonce,
+ const siphash_key_t *key)
+{
+ struct {
+ union amt_addr addr;
+ __be16 port;
+ __be32 nonce;
+ } __packed in = { .addr = *addr, .port = port, .nonce = nonce };
+
+ return siphash(&in, sizeof(in), key) >> 16;
+}
+
static void __amt_source_gc_work(void)
{
struct amt_source_node *snode;
@@ -187,6 +210,19 @@ static bool amt_addr_equal(union amt_addr *a, union amt_addr *b)
return !memcmp(a, b, sizeof(union amt_addr));
}
+#define AMT_SRC_PREFIX6_LEN 8
+
+/* One source for the per-source tunnel cap: an IPv4 address or an IPv6
+ * /64, so that one host cannot reach the cap from each of its addresses.
+ */
+static bool amt_same_source(const struct amt_dev *amt,
+ const union amt_addr *a, const union amt_addr *b)
+{
+ if (amt_v6(amt))
+ return !memcmp(&a->ip6, &b->ip6, AMT_SRC_PREFIX6_LEN);
+ return a->ip4 == b->ip4;
+}
+
static u32 amt_source_hash(struct amt_tunnel_list *tunnel, union amt_addr *src)
{
u32 hash = jhash(src, sizeof(*src), tunnel->amt->hash_seed);
@@ -699,10 +735,16 @@ static void __amt_update_relay_status(struct amt_tunnel_list *tunnel,
{
if (validate && tunnel->status >= status)
return;
- netdev_dbg(tunnel->amt->dev,
- "Update Tunnel(IP = %pI4, PORT = %u) status %s -> %s",
- &tunnel->ip4, ntohs(tunnel->source_port),
- status_str[tunnel->status], status_str[status]);
+ if (amt_v6(tunnel->amt))
+ netdev_dbg(tunnel->amt->dev,
+ "Update Tunnel(IP = %pI6c, PORT = %u) status %s -> %s",
+ &tunnel->addr.ip6, ntohs(tunnel->source_port),
+ status_str[tunnel->status], status_str[status]);
+ else
+ netdev_dbg(tunnel->amt->dev,
+ "Update Tunnel(IP = %pI4, PORT = %u) status %s -> %s",
+ &tunnel->addr.ip4, ntohs(tunnel->source_port),
+ status_str[tunnel->status], status_str[status]);
tunnel->status = status;
}
@@ -1284,12 +1326,12 @@ static void amt_send_multicast_data(struct amt_dev *amt,
memset(&fl4, 0, sizeof(struct flowi4));
fl4.flowi4_oif = amt->stream_dev->ifindex;
- fl4.daddr = tunnel->ip4;
+ fl4.daddr = tunnel->addr.ip4;
fl4.saddr = amt->local_ip;
fl4.flowi4_proto = IPPROTO_UDP;
rt = ip_route_output_key(amt->net, &fl4);
if (IS_ERR(rt)) {
- netdev_dbg(amt->dev, "no route to %pI4\n", &tunnel->ip4);
+ netdev_dbg(amt->dev, "no route to %pI4\n", &tunnel->addr.ip4);
kfree_skb(skb);
return;
}
@@ -2720,21 +2762,21 @@ static bool amt_membership_query_handler(struct amt_dev *amt,
* race with amt_tunnel_expire() creates the replacement.
*/
static struct amt_tunnel_list *amt_tunnel_lookup(struct amt_dev *amt,
- __be32 saddr, __be16 sport)
+ union amt_addr *saddr,
+ __be16 sport)
{
struct amt_tunnel_list *tunnel;
list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) {
- if (tunnel->ip4 == saddr && tunnel->source_port == sport &&
+ if (amt_addr_equal(&tunnel->addr, saddr) &&
+ tunnel->source_port == sport &&
!READ_ONCE(tunnel->dead))
return tunnel;
}
return NULL;
}
-/* Most tunnels one source address may hold: an eighth of the table, at
- * least one and at most AMT_MAX_TUNNELS_PER_SRC.
- */
+/* An eighth of the table per source, within 1..AMT_MAX_TUNNELS_PER_SRC. */
static u32 amt_max_tunnels_per_src(const struct amt_dev *amt)
{
return clamp_t(u32, amt->max_tunnels / 8, 1,
@@ -2748,10 +2790,10 @@ static u32 amt_max_tunnels_per_src(const struct amt_dev *amt)
* bounds the number of *verified* gateways rather than the number of
* unverified Requests anyone can send. A per-source cap derived from the
* table, amt_max_tunnels_per_src(), additionally limits the tunnels one
- * verified source address may hold.
+ * verified source (an IPv4 address or an IPv6 /64) may hold.
*/
static struct amt_tunnel_list *amt_tunnel_get_or_create(struct amt_dev *amt,
- __be32 saddr,
+ union amt_addr *saddr,
__be16 sport)
{
struct amt_tunnel_list *tunnel;
@@ -2768,12 +2810,14 @@ static struct amt_tunnel_list *amt_tunnel_get_or_create(struct amt_dev *amt,
* count how many tunnels this source already holds on the same walk.
* Skip dead tunnels, as amt_tunnel_lookup() and the Request path do, so
* a tunnel being freed neither stands in for its endpoint nor counts
- * against its address.
+ * against its source.
*/
list_for_each_entry(tunnel, &amt->tunnel_list, list) {
- if (tunnel->ip4 != saddr || READ_ONCE(tunnel->dead))
+ if (!amt_same_source(amt, &tunnel->addr, saddr) ||
+ READ_ONCE(tunnel->dead))
continue;
- if (tunnel->source_port == sport) {
+ if (amt_addr_equal(&tunnel->addr, saddr) &&
+ tunnel->source_port == sport) {
spin_unlock_bh(&amt->lock);
return tunnel;
}
@@ -2800,7 +2844,7 @@ static struct amt_tunnel_list *amt_tunnel_get_or_create(struct amt_dev *amt,
}
tunnel->source_port = sport;
- tunnel->ip4 = saddr;
+ tunnel->addr = *saddr;
tunnel->amt = amt;
spin_lock_init(&tunnel->lock);
for (i = 0; i < amt->hash_buckets; i++)
@@ -2830,7 +2874,7 @@ static struct amt_tunnel_list *amt_tunnel_get_or_create(struct amt_dev *amt,
* second amt_tunnel_get_or_create() call, which creates its replacement.
*/
static bool amt_update_tunnel(struct amt_dev *amt, struct sk_buff *skb,
- __be32 saddr, __be16 sport, bool v6)
+ union amt_addr *saddr, __be16 sport, bool v6)
{
struct amt_tunnel_list *tunnel;
int i;
@@ -2870,12 +2914,13 @@ static bool amt_update_handler(struct amt_dev *amt, struct sk_buff *skb)
struct ethhdr *eth;
struct iphdr *iph;
unsigned int seq;
+ union amt_addr saddr;
u64 response_mac;
- __be32 saddr;
__be32 nonce;
__be16 sport;
- saddr = ip_hdr(skb)->saddr;
+ /* Snapshot the outer source before any pull can move the header. */
+ amt_outer_saddr(amt, skb, &saddr);
hdr_size = sizeof(*amtmu) + sizeof(struct udphdr);
if (!pskb_may_pull(skb, hdr_size))
@@ -2902,10 +2947,7 @@ static bool amt_update_handler(struct amt_dev *amt, struct sk_buff *skb)
} while (read_seqcount_retry(&amt->key_seq, seq));
for (i = 0; i < ARRAY_SIZE(key); i++) {
- u64 mac = siphash_3u32((__force u32)saddr,
- (__force u32)sport,
- (__force u32)nonce,
- &key[i]) >> 16;
+ u64 mac = amt_relay_mac(&saddr, sport, nonce, &key[i]);
if (response_mac == mac) {
verified = true;
@@ -2939,7 +2981,7 @@ static bool amt_update_handler(struct amt_dev *amt, struct sk_buff *skb)
return true;
}
- if (!amt_update_tunnel(amt, skb, saddr, sport, false))
+ if (!amt_update_tunnel(amt, skb, &saddr, sport, false))
return true;
skb_push(skb, sizeof(struct ethhdr));
@@ -2958,7 +3000,7 @@ static bool amt_update_handler(struct amt_dev *amt, struct sk_buff *skb)
return true;
}
- if (!amt_update_tunnel(amt, skb, saddr, sport, true))
+ if (!amt_update_tunnel(amt, skb, &saddr, sport, true))
return true;
skb_push(skb, sizeof(struct ethhdr));
@@ -3150,8 +3192,8 @@ static bool amt_request_handler(struct amt_dev *amt, struct sk_buff *skb)
struct amt_header_request *amtrh;
struct amt_tunnel_list *tunnel;
bool full, have = false;
+ union amt_addr saddr;
struct udphdr *udph;
- struct iphdr *iph;
siphash_key_t key;
u32 per_src = 0;
u64 mac;
@@ -3159,7 +3201,7 @@ static bool amt_request_handler(struct amt_dev *amt, struct sk_buff *skb)
if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtrh)))
return true;
- iph = ip_hdr(skb);
+ amt_outer_saddr(amt, skb, &saddr);
udph = udp_hdr(skb);
amtrh = (struct amt_header_request *)(udp_hdr(skb) + 1);
@@ -3183,10 +3225,12 @@ static bool amt_request_handler(struct amt_dev *amt, struct sk_buff *skb)
*/
rcu_read_lock();
list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) {
- if (tunnel->ip4 != iph->saddr || READ_ONCE(tunnel->dead))
+ if (!amt_same_source(amt, &tunnel->addr, &saddr) ||
+ READ_ONCE(tunnel->dead))
continue;
per_src++;
- if (tunnel->source_port == udph->source)
+ if (amt_addr_equal(&tunnel->addr, &saddr) &&
+ tunnel->source_port == udph->source)
have = true;
}
rcu_read_unlock();
@@ -3195,15 +3239,12 @@ static bool amt_request_handler(struct amt_dev *amt, struct sk_buff *skb)
if (!have && (full || per_src >= amt_max_tunnels_per_src(amt)))
return true;
- mac = siphash_3u32((__force u32)iph->saddr,
- (__force u32)udph->source,
- (__force u32)amtrh->nonce,
- &key) >> 16;
+ mac = amt_relay_mac(&saddr, udph->source, amtrh->nonce, &key);
if (!amtrh->p)
- amt_send_igmp_gq(amt, iph->saddr, udph->source, amtrh->nonce,
+ amt_send_igmp_gq(amt, saddr.ip4, udph->source, amtrh->nonce,
mac);
else
- amt_send_mld_gq(amt, iph->saddr, udph->source, amtrh->nonce,
+ amt_send_mld_gq(amt, saddr.ip4, udph->source, amtrh->nonce,
mac);
return false;
diff --git a/include/net/amt.h b/include/net/amt.h
index 4c096590d..82ab63876 100644
--- a/include/net/amt.h
+++ b/include/net/amt.h
@@ -251,6 +251,12 @@ struct amt_relay_headers {
};
} __packed;
+/* ip6 exists without CONFIG_IPV6, so copy and compare code needs no #if. */
+union amt_addr {
+ __be32 ip4;
+ struct in6_addr ip6;
+};
+
struct amt_tunnel_list {
struct list_head list;
/* Protect All resources under an amt_tunne_list */
@@ -272,18 +278,12 @@ struct amt_tunnel_list {
*/
bool dead;
__be16 source_port;
- __be32 ip4;
+ /* Outer address of the gateway endpoint, in the outer family */
+ union amt_addr addr;
struct rcu_head rcu;
struct hlist_head groups[];
};
-union amt_addr {
- __be32 ip4;
-#if IS_ENABLED(CONFIG_IPV6)
- struct in6_addr ip6;
-#endif
-};
-
/* RFC 3810
*
* When the router is in EXCLUDE mode, the router state is represented
@@ -423,7 +423,7 @@ struct amt_dev {
#define AMT_SECRET_TIMEOUT 60000
#define IANA_AMT_UDP_PORT 2268
#define AMT_MAX_TUNNELS 128
-/* Upper bound of amt_max_tunnels_per_src(), the per-address tunnel cap */
+/* Upper bound of amt_max_tunnels_per_src(), the per-source tunnel cap */
#define AMT_MAX_TUNNELS_PER_SRC 16
#define AMT_MAX_REQS 128
#define AMT_GW_HLEN (sizeof(struct iphdr) + \