[PATCH net-next v6 07/12] amt: send the AMT gateway control plane over IPv6

From: Omar Ramadan

Date: Tue Dec 01 2026 - 23:32:28 EST


A gateway builds its Discovery, Request and Membership Updates for an
IPv4 outer header only, so on an IPv6-only access network it cannot
reach a relay.

Send the Discovery and the Request from amt_send_discovery_v6() and
amt_send_request_v6(). They are a few bytes, so they are built on the
stack and sent with amt_send_ctrl_v6(). The Membership Update carries
the IGMP or MLD report, so amt_send_membership_update() keeps its skb,
sizes the headroom with amt_ip_hlen() and sends through amt_udp_xmit().
struct amt_dev gains the IPv6 discovery address and the IPv6 relay
address a gateway learns; nothing writes the latter until the next
patch.

amt_send_ctrl_v6() holds rcu_read_lock_bh(): a gateway sends from its
event worker, and udp_tunnel6_xmit_skb() reaches ip6tunnel_xmit(), which
increments and decrements a per-CPU xmit recursion counter that a
migration in between would unbalance. IPv4 never touches it, and the
relay sends from softirq.

The P bit of the Request and the inner protocol of the Update follow the
inner family, as for IPv4. amt_v6() is still false for every device, so
none of the IPv6 code runs yet and IPv4 behaviour is unchanged.

Assisted-by: LLM
Signed-off-by: Omar Ramadan <omar@xxxxxxxxxxxxx>
---
drivers/net/amt.c | 79 +++++++++++++++++++++++++++++------------------
include/net/amt.h | 4 +++
2 files changed, 53 insertions(+), 30 deletions(-)

diff --git a/drivers/net/amt.c b/drivers/net/amt.c
index 79702b002..40481b0f9 100644
--- a/drivers/net/amt.c
+++ b/drivers/net/amt.c
@@ -791,8 +791,9 @@ static int amt_send_ctrl_v6(struct amt_dev *amt, const struct in6_addr *saddr,
struct sock *sk;
int hlen, err;

- rcu_read_lock();
- sk = rcu_dereference(amt->sk);
+ /* ip6tunnel_xmit() counts xmit recursion per CPU: BH must be off. */
+ rcu_read_lock_bh();
+ sk = rcu_dereference_bh(amt->sk);
if (!sk || !netif_running(amt->stream_dev) ||
!netif_running(amt->dev)) {
err = -ENETDOWN;
@@ -824,10 +825,36 @@ static int amt_send_ctrl_v6(struct amt_dev *amt, const struct in6_addr *saddr,
ip6_dst_hoplimit(dst), 0, sport, dport, false, 0);
err = 0;
out:
- rcu_read_unlock();
+ rcu_read_unlock_bh();
return err;
}

+static void amt_send_discovery_v6(struct amt_dev *amt)
+{
+ struct amt_header_discovery amtd = {
+ .type = AMT_MSG_DISCOVERY,
+ .nonce = amt->nonce,
+ };
+
+ if (!amt_send_ctrl_v6(amt, &amt->local_ipv6, &amt->discovery_ipv6,
+ amt->gw_port, amt->relay_port,
+ &amtd, sizeof(amtd)))
+ amt_update_gw_status(amt, AMT_STATUS_SENT_DISCOVERY, true);
+}
+
+/* The inner family @v6 sets the P bit, independent of the outer one. */
+static void amt_send_request_v6(struct amt_dev *amt, bool v6)
+{
+ struct amt_header_request amtrh = {
+ .type = AMT_MSG_REQUEST,
+ .p = v6,
+ .nonce = amt->nonce,
+ };
+
+ amt_send_ctrl_v6(amt, &amt->local_ipv6, &amt->remote_ipv6,
+ amt->gw_port, amt->relay_port, &amtrh, sizeof(amtrh));
+}
+
static void amt_send_discovery(struct amt_dev *amt)
{
struct amt_header_discovery *amtd;
@@ -841,6 +868,11 @@ static void amt_send_discovery(struct amt_dev *amt)
u32 len;
int err;

+ if (amt_v6(amt)) {
+ amt_send_discovery_v6(amt);
+ return;
+ }
+
rcu_read_lock();
sk = rcu_dereference(amt->sk);
if (!sk)
@@ -931,6 +963,11 @@ static void amt_send_request(struct amt_dev *amt, bool v6)
u32 len;
int err;

+ if (amt_v6(amt)) {
+ amt_send_request_v6(amt, v6);
+ return;
+ }
+
rcu_read_lock();
remote_ip = READ_ONCE(amt->remote_ip);
sk = rcu_dereference(amt->sk);
@@ -1335,9 +1372,7 @@ static bool amt_send_membership_update(struct amt_dev *amt,
{
__be32 remote_ip = READ_ONCE(amt->remote_ip);
struct amt_header_membership_update *amtmu;
- struct iphdr *iph;
- struct flowi4 fl4;
- struct rtable *rt;
+ union amt_addr remote = {};
struct sock *sk;
int err;

@@ -1346,23 +1381,11 @@ static bool amt_send_membership_update(struct amt_dev *amt,
return true;

err = skb_cow_head(skb, LL_RESERVED_SPACE(amt->dev) + sizeof(*amtmu) +
- sizeof(*iph) + sizeof(struct udphdr));
+ amt_ip_hlen(amt) + sizeof(struct udphdr));
if (err)
return true;

skb_reset_inner_headers(skb);
- memset(&fl4, 0, sizeof(struct flowi4));
- fl4.flowi4_oif = amt->stream_dev->ifindex;
- fl4.daddr = remote_ip;
- fl4.saddr = amt->local_ip;
- fl4.flowi4_dscp = inet_dsfield_to_dscp(AMT_TOS);
- 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", &remote_ip);
- return true;
- }
-
amtmu = skb_push(skb, sizeof(*amtmu));
amtmu->version = 0;
amtmu->type = AMT_MSG_MEMBERSHIP_UPDATE;
@@ -1374,17 +1397,13 @@ static bool amt_send_membership_update(struct amt_dev *amt,
skb_set_inner_protocol(skb, htons(ETH_P_IP));
else
skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
- udp_tunnel_xmit_skb(rt, sk, skb,
- fl4.saddr,
- fl4.daddr,
- AMT_TOS,
- ip4_dst_hoplimit(&rt->dst),
- 0,
- amt->gw_port,
- amt->relay_port,
- false,
- false,
- 0);
+ if (amt_v6(amt))
+ remote.ip6 = amt->remote_ipv6;
+ else
+ remote.ip4 = remote_ip;
+ if (amt_udp_xmit(amt, sk, skb, &remote, amt->gw_port,
+ amt->relay_port, inet_dsfield_to_dscp(AMT_TOS), NULL))
+ return true;
amt_update_gw_status(amt, AMT_STATUS_SENT_UPDATE, true);
return false;
}
diff --git a/include/net/amt.h b/include/net/amt.h
index 726026259..967acaf38 100644
--- a/include/net/amt.h
+++ b/include/net/amt.h
@@ -385,8 +385,12 @@ struct amt_dev {
struct in6_addr local_ipv6;
/* Outer remote ip */
__be32 remote_ip;
+ /* Outer remote ipv6 (learned from the Advertisement; :: in v4 mode) */
+ struct in6_addr remote_ipv6;
/* Outer discovery ip */
__be32 discovery_ip;
+ /* Outer discovery ipv6 (v6 gateway only; :: in v4 mode) */
+ struct in6_addr discovery_ipv6;
/* Only used in gateway mode */
__be32 nonce;
/* Gateway sent request and received query */