Netdev List
 help / color / mirror / Atom feed
From: Omar Ramadan <omar@blockcast.net>
To: Taehee Yoo <ap420073@gmail.com>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Shuah Khan <shuah@kernel.org>
Cc: Simon Horman <horms@kernel.org>,
	netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Willem de Bruijn <willemb@google.com>,
	David Ahern <dsahern@kernel.org>,
	Ido Schimmel <idosch@nvidia.com>
Subject: [PATCH net-next v6 08/12] amt: receive the AMT gateway control plane over IPv6
Date: Wed,  2 Dec 2026 04:31:56 +0000	[thread overview]
Message-ID: <20261202043200.286260-9-omar@blockcast.net> (raw)
In-Reply-To: <20261202043200.286260-1-omar@blockcast.net>

The previous patch added the IPv6 senders of a gateway, but amt_rcv()
could not take the replies: it checks the outer source with
ip_hdr(skb)->saddr, which on an IPv6 header is four bytes from the
middle of the address, and amt_advertisement_handler() parses only the
IPv4 form.

amt_rcv() takes the outer source by value with amt_outer_saddr() before
amt_parse_type() pulls, and checks it with amt_from_relay(): the
discovery address for an Advertisement, the learned relay address
otherwise, in the device's family, and nothing matches while the relay
is not known. amt_membership_query_handler() repeats the check for a
queued Query. The IPv4 half is "amt: reject a gateway Query or Data from
0.0.0.0" in net. IPv6 input delivers a packet from ::, which would match
a cleared relay address and, with a zero nonce, inject a Query or have a
relay create a tunnel, so amt_rcv() drops it in either mode.

amt_advertisement_handler() pulls the 24-byte form on an IPv6 gateway
and refuses an unspecified, loopback, multicast or IPv4-mapped relay
address; the V6ONLY socket could not reach the last. An IPv6 gateway
takes only the exact 24-byte form, which is this driver's policy:
s5.1.2.5 only has a gateway tell the forms apart by length. An IPv4
gateway keeps its minimum-length check.

The learned relay address is written from the event worker (the
Advertisement handler, and amt_clear_remote() when called from
amt_event_send_request()) and in process context (amt_dev_open() and
amt_dev_stop() call amt_clear_remote()), and read in softirq and, from
the next patch on, by amt_fill_info(), which no longer relies on RTNL. A
struct in6_addr is not copied in one access, so a reader could see half
of an old and half of a new address. It is published with
amt_set_remote_ipv6() under a seqlock, remote_ipv6_lock, and read with
amt_get_remote_ipv6(), one snapshot per packet, without a lock.
amt_clear_remote() forgets the relay in both families in the three
places that reset it, so a gateway taken down and up neither accepts
traffic from its previous relay nor, once amt_fill_info() reports it,
keeps reporting it.

IPv4 input refuses a source that is one of the host's own addresses as a
martian when it comes from the wire; IPv6 input delivers it, an anycast
address too. A relay answers a Discovery and a Request to their source,
so a forged one from the relay's own address would make it send to
itself over lo, where a rule such as "-i lo -j ACCEPT" lets it in. A
relay's amt_rcv() therefore drops such a packet. amt_saddr_is_local()
looks a unicast address up with ipv6_chk_addr() in the L3 domain of the
stream device, so an address on a slave of its VRF is found, a
link-local one only on the stream device itself and a tentative one not,
and an anycast address with ipv6_chk_acast_addr() in the whole
namespace. That function is not exported, so this patch exports it from
net/ipv6/anycast.c, as ipv6_chk_addr() is. A packet that arrives over lo
comes from the host itself and is not dropped, as for IPv4, so a gateway
on the relay's host still works. In a VRF every packet arrives on the
VRF device, so that exception cannot apply there.

A gateway must accept a zero UDP checksum on Multicast Data (RFC 7450
s5.2.3.3; a relay may use the RFC 6936 exception to RFC 8200 s8.1), so
an IPv6 gateway's socket does, and still verifies a non-zero one.
amt_rcv() drops a zero checksum on every other message (RFC 6936 s5). A
relay's socket keeps requiring it.

The Query and Multicast Data handlers dispatch on the inner family and
need no change. amt_v6() is still false for every device, because
nothing sets local_ipv6 until the netlink attributes of the next patch,
so none of this runs yet and the receive path of an IPv4 gateway is
unchanged.

Assisted-by: LLM
Signed-off-by: Omar Ramadan <omar@blockcast.net>
---
 drivers/net/amt.c  | 161 ++++++++++++++++++++++++++++++++++++++-------
 include/net/amt.h  |   2 +
 net/ipv6/anycast.c |   1 +
 3 files changed, 140 insertions(+), 24 deletions(-)

diff --git a/drivers/net/amt.c b/drivers/net/amt.c
index 40481b0f9..c2126c7ba 100644
--- a/drivers/net/amt.c
+++ b/drivers/net/amt.c
@@ -767,6 +767,10 @@ static struct dst_entry *amt_route6(struct amt_dev *amt, struct sock *sk,
 {
 	struct flowi6 fl6;
 
+	/* A gateway that just forgot its relay publishes ::. */
+	if (ipv6_addr_any(daddr))
+		return ERR_PTR(-EINVAL);
+
 	memset(&fl6, 0, sizeof(fl6));
 	fl6.flowi6_oif		= amt->stream_dev->ifindex;
 	fl6.flowi6_proto	= IPPROTO_UDP;
@@ -829,6 +833,69 @@ out:
 	return err;
 }
 
+/* Written from the event worker and process context, read in softirq. A
+ * struct in6_addr is not stored in one access, so it goes under a seqlock
+ * and each consumer takes one snapshot per packet.
+ */
+static void amt_set_remote_ipv6(struct amt_dev *amt,
+				const struct in6_addr *addr)
+{
+	write_seqlock_bh(&amt->remote_ipv6_lock);
+	amt->remote_ipv6 = *addr;
+	write_sequnlock_bh(&amt->remote_ipv6_lock);
+}
+
+static void amt_get_remote_ipv6(const struct amt_dev *amt,
+				struct in6_addr *addr)
+{
+	unsigned int seq;
+
+	do {
+		seq = read_seqbegin(&amt->remote_ipv6_lock);
+		*addr = amt->remote_ipv6;
+	} while (read_seqretry(&amt->remote_ipv6_lock, seq));
+}
+
+static void amt_clear_remote(struct amt_dev *amt)
+{
+	WRITE_ONCE(amt->remote_ip, 0);
+	amt_set_remote_ipv6(amt, &in6addr_any);
+}
+
+/* Whether a gateway's message came from its relay: the discovery address
+ * for an Advertisement, the learned relay address otherwise. An unknown
+ * relay (::) matches nothing.
+ */
+static bool amt_from_relay(const struct amt_dev *amt,
+			   const union amt_addr *saddr, bool discovery)
+{
+	__be32 relay4;
+
+	if (amt_v6(amt)) {
+		struct in6_addr relay;
+
+		if (discovery)
+			relay = amt->discovery_ipv6;
+		else
+			amt_get_remote_ipv6(amt, &relay);
+		return !ipv6_addr_any(&relay) &&
+		       ipv6_addr_equal(&saddr->ip6, &relay);
+	}
+	relay4 = discovery ? amt->discovery_ip : READ_ONCE(amt->remote_ip);
+	return relay4 && saddr->ip4 == relay4;
+}
+
+/* A unicast address is looked up in the stream device's L3 domain (a
+ * link-local one only on the device itself), an anycast address in the
+ * whole namespace. A gateway has no reason to send from either.
+ */
+static bool amt_saddr_is_local(const struct amt_dev *amt,
+			       const struct in6_addr *a)
+{
+	return ipv6_chk_addr(amt->net, a, amt->stream_dev, 0) ||
+	       ipv6_chk_acast_addr(amt->net, NULL, a);
+}
+
 static void amt_send_discovery_v6(struct amt_dev *amt)
 {
 	struct amt_header_discovery amtd = {
@@ -850,9 +917,11 @@ static void amt_send_request_v6(struct amt_dev *amt, bool v6)
 		.p	= v6,
 		.nonce	= amt->nonce,
 	};
+	struct in6_addr remote6;
 
-	amt_send_ctrl_v6(amt, &amt->local_ipv6, &amt->remote_ipv6,
-			 amt->gw_port, amt->relay_port, &amtrh, sizeof(amtrh));
+	amt_get_remote_ipv6(amt, &remote6);
+	amt_send_ctrl_v6(amt, &amt->local_ipv6, &remote6, amt->gw_port,
+			 amt->relay_port, &amtrh, sizeof(amtrh));
 }
 
 static void amt_send_discovery(struct amt_dev *amt)
@@ -1248,7 +1317,7 @@ static void amt_event_send_request(struct amt_dev *amt)
 		amt->qi = AMT_INIT_REQ_TIMEOUT;
 		WRITE_ONCE(amt->ready4, false);
 		WRITE_ONCE(amt->ready6, false);
-		WRITE_ONCE(amt->remote_ip, 0);
+		amt_clear_remote(amt);
 		amt_update_gw_status(amt, AMT_STATUS_INIT, false);
 		amt->req_cnt = 0;
 		amt->nonce = 0;
@@ -1398,7 +1467,7 @@ static bool amt_send_membership_update(struct amt_dev *amt,
 	else
 		skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
 	if (amt_v6(amt))
-		remote.ip6 = amt->remote_ipv6;
+		amt_get_remote_ipv6(amt, &remote.ip6);
 	else
 		remote.ip4 = remote_ip;
 	if (amt_udp_xmit(amt, sk, skb, &remote, amt->gw_port,
@@ -2664,27 +2733,45 @@ static bool amt_advertisement_handler(struct amt_dev *amt, struct sk_buff *skb)
 	struct amt_header_advertisement *amta;
 	int hdr_size;
 
-	hdr_size = sizeof(*amta) + sizeof(struct udphdr);
+	hdr_size = sizeof(struct udphdr) +
+		   (amt_v6(amt) ? sizeof(struct amt_header_advertisement_v6) :
+				  sizeof(*amta));
 	if (!pskb_may_pull(skb, hdr_size))
 		return true;
 
-	amta = (struct amt_header_advertisement *)(udp_hdr(skb) + 1);
-	if (!amta->ip4)
+	/* The forms differ only in length. An IPv6 gateway accepts only its
+	 * own: driver policy, not the RFC's.
+	 */
+	if (amt_v6(amt) && ntohs(udp_hdr(skb)->len) != hdr_size)
 		return true;
 
+	amta = (struct amt_header_advertisement *)(udp_hdr(skb) + 1);
 	if (amta->reserved || amta->version)
 		return true;
 
-	if (ipv4_is_loopback(amta->ip4) || ipv4_is_multicast(amta->ip4) ||
-	    ipv4_is_zeronet(amta->ip4))
-		return true;
-
 	if (amt->status != AMT_STATUS_SENT_DISCOVERY ||
 	    amt->nonce != amta->nonce)
 		return true;
 
-	WRITE_ONCE(amt->remote_ip, amta->ip4);
-	netdev_dbg(amt->dev, "advertised remote ip = %pI4\n", &amta->ip4);
+	if (amt_v6(amt)) {
+		const struct in6_addr *ip6;
+
+		ip6 = &((struct amt_header_advertisement_v6 *)amta)->ip6;
+		if (ipv6_addr_any(ip6) || ipv6_addr_loopback(ip6) ||
+		    ipv6_addr_is_multicast(ip6) || ipv6_addr_v4mapped(ip6))
+			return true;
+
+		amt_set_remote_ipv6(amt, ip6);
+		netdev_dbg(amt->dev, "advertised remote ipv6 = %pI6c\n", ip6);
+	} else {
+		if (!amta->ip4 || ipv4_is_loopback(amta->ip4) ||
+		    ipv4_is_multicast(amta->ip4) || ipv4_is_zeronet(amta->ip4))
+			return true;
+
+		WRITE_ONCE(amt->remote_ip, amta->ip4);
+		netdev_dbg(amt->dev, "advertised remote ip = %pI4\n",
+			   &amta->ip4);
+	}
 	mod_delayed_work(amt_wq, &amt->req_wq, 0);
 
 	amt_update_gw_status(amt, AMT_STATUS_RECEIVED_ADVERTISEMENT, true);
@@ -2769,6 +2856,7 @@ static bool amt_membership_query_handler(struct amt_dev *amt,
 	struct ethhdr *eth, *oeth;
 	struct igmpv3_query *ihv3;
 	u8 h_source[ETH_ALEN];
+	union amt_addr saddr;
 	struct iphdr *iph;
 	int hdr_size, len;
 	u64 response_mac;
@@ -2782,7 +2870,8 @@ static bool amt_membership_query_handler(struct amt_dev *amt,
 		return true;
 
 	/* A deferred query may outlive a relay address change. */
-	if (ip_hdr(skb)->saddr != READ_ONCE(amt->remote_ip))
+	amt_outer_saddr(amt, skb, &saddr);
+	if (!amt_from_relay(amt, &saddr, false))
 		return true;
 
 	if (amtmq->nonce != amt->nonce)
@@ -3404,9 +3493,8 @@ drop:
 
 static int amt_rcv(struct sock *sk, struct sk_buff *skb)
 {
+	union amt_addr saddr;
 	struct amt_dev *amt;
-	__be32 remote_ip;
-	__be32 saddr;
 	int type;
 	bool err;
 
@@ -3417,10 +3505,28 @@ static int amt_rcv(struct sock *sk, struct sk_buff *skb)
 		kfree_skb(skb);
 		goto out;
 	}
-	remote_ip = READ_ONCE(amt->remote_ip);
 
 	skb->dev = amt->dev;
-	saddr = ip_hdr(skb)->saddr;
+	/* Snapshot the outer source before amt_parse_type() pulls. */
+	amt_outer_saddr(amt, skb, &saddr);
+	/* IPv6 input delivers a packet from ::. */
+	if (amt_v6(amt) && ipv6_addr_any(&saddr.ip6)) {
+		err = true;
+		goto drop;
+	}
+	/* IPv4 input refuses a source that is one of this host's own addresses
+	 * as a martian, IPv6 input does not. Answering a forged one would send
+	 * to ourselves over lo. A packet over lo is from this host, as for
+	 * IPv4, so a gateway on the relay's host still works; in a VRF the
+	 * incoming interface is the VRF device, so that exception cannot apply
+	 * there.
+	 */
+	if (amt_v6(amt) && amt->mode == AMT_MODE_RELAY &&
+	    skb->skb_iif != amt->net->loopback_dev->ifindex &&
+	    amt_saddr_is_local(amt, &saddr.ip6)) {
+		err = true;
+		goto drop;
+	}
 	type = amt_parse_type(skb);
 	if (type == -1) {
 		err = true;
@@ -3428,9 +3534,15 @@ static int amt_rcv(struct sock *sk, struct sk_buff *skb)
 	}
 
 	if (amt->mode == AMT_MODE_GATEWAY) {
+		/* RFC 6936 s5: a zero checksum only on Multicast Data. */
+		if (amt_v6(amt) && !udp_hdr(skb)->check &&
+		    type != AMT_MSG_MULTICAST_DATA) {
+			err = true;
+			goto drop;
+		}
 		switch (type) {
 		case AMT_MSG_ADVERTISEMENT:
-			if (saddr != amt->discovery_ip) {
+			if (!amt_from_relay(amt, &saddr, true)) {
 				netdev_dbg(amt->dev, "Invalid Relay IP\n");
 				err = true;
 				goto drop;
@@ -3442,7 +3554,7 @@ static int amt_rcv(struct sock *sk, struct sk_buff *skb)
 			}
 			goto out;
 		case AMT_MSG_MULTICAST_DATA:
-			if (!remote_ip || saddr != remote_ip) {
+			if (!amt_from_relay(amt, &saddr, false)) {
 				netdev_dbg(amt->dev, "Invalid Relay IP\n");
 				err = true;
 				goto drop;
@@ -3453,7 +3565,7 @@ static int amt_rcv(struct sock *sk, struct sk_buff *skb)
 			else
 				goto out;
 		case AMT_MSG_MEMBERSHIP_QUERY:
-			if (!remote_ip || saddr != remote_ip) {
+			if (!amt_from_relay(amt, &saddr, false)) {
 				netdev_dbg(amt->dev, "Invalid Relay IP\n");
 				err = true;
 				goto drop;
@@ -3598,7 +3710,7 @@ static struct sock *amt_create_sock(const struct amt_dev *amt)
 		udp_conf.family = AF_INET6;
 		udp_conf.bind_ifindex = amt->stream_dev->ifindex;
 		udp_conf.use_udp6_tx_checksums = true;
-		udp_conf.use_udp6_rx_checksums = true;
+		udp_conf.use_udp6_rx_checksums = amt->mode == AMT_MODE_RELAY;
 		udp_conf.ipv6_v6only = true;
 	} else {
 		udp_conf.family = AF_INET;
@@ -3667,7 +3779,7 @@ static int amt_dev_open(struct net_device *dev)
 	}
 
 	amt->req_cnt = 0;
-	WRITE_ONCE(amt->remote_ip, 0);
+	amt_clear_remote(amt);
 	amt->nonce = 0;
 
 	amt->status = AMT_STATUS_INIT;
@@ -3716,7 +3828,7 @@ static int amt_dev_stop(struct net_device *dev)
 	amt->ready4 = false;
 	amt->ready6 = false;
 	amt->req_cnt = 0;
-	WRITE_ONCE(amt->remote_ip, 0);
+	amt_clear_remote(amt);
 
 	/* Mark every tunnel dead first, so a concurrent amt_tunnel_expire()
 	 * sees ->dead and bails instead of freeing a tunnel in parallel; this
@@ -3886,6 +3998,7 @@ static int amt_newlink(struct net_device *dev,
 		amt->max_tunnels = AMT_MAX_TUNNELS;
 
 	spin_lock_init(&amt->lock);
+	seqlock_init(&amt->remote_ipv6_lock);
 	seqcount_spinlock_init(&amt->key_seq, &amt->lock);
 	amt->max_groups = AMT_MAX_GROUP;
 	amt->max_sources = AMT_MAX_SOURCE;
diff --git a/include/net/amt.h b/include/net/amt.h
index 967acaf38..823623b99 100644
--- a/include/net/amt.h
+++ b/include/net/amt.h
@@ -387,6 +387,8 @@ struct amt_dev {
 	__be32			remote_ip;
 	/* Outer remote ipv6 (learned from the Advertisement; :: in v4 mode) */
 	struct in6_addr		remote_ipv6;
+	/* Publishes remote_ipv6, which is too wide for a single access */
+	seqlock_t		remote_ipv6_lock;
 	/* Outer discovery ip */
 	__be32			discovery_ip;
 	/* Outer discovery ipv6 (v6 gateway only; :: in v4 mode) */
diff --git a/net/ipv6/anycast.c b/net/ipv6/anycast.c
index be6dac8a8..4adc6f316 100644
--- a/net/ipv6/anycast.c
+++ b/net/ipv6/anycast.c
@@ -513,6 +513,7 @@ bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev,
 	rcu_read_unlock();
 	return found;
 }
+EXPORT_SYMBOL(ipv6_chk_acast_addr);
 
 /*	check if this anycast address is link-local on given interface or
  *	is global

  parent reply	other threads:[~2026-12-02  4:32 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  4:31 [PATCH net-next v6 00/12] amt: add an IPv6 outer transport Omar Ramadan
2026-12-02  4:31 ` [PATCH net-next v6 01/12] amt: create an AF_INET6 encapsulation socket for an IPv6 outer address Omar Ramadan
2026-12-02  4:31 ` [PATCH net-next v6 02/12] amt: send the Relay Advertisement over IPv6 Omar Ramadan
2026-12-02  4:31 ` [PATCH net-next v6 03/12] amt: key relay tunnels on a union amt_addr endpoint Omar Ramadan
2026-12-02  4:31 ` [PATCH net-next v6 04/12] amt: send the Membership Query over IPv6 Omar Ramadan
2026-12-02  4:31 ` [PATCH net-next v6 05/12] amt: forward multicast data " Omar Ramadan
2026-12-02  4:31 ` [PATCH net-next v6 06/12] amt: size the encapsulation headroom by the outer IP version Omar Ramadan
2026-12-02  4:31 ` [PATCH net-next v6 07/12] amt: send the AMT gateway control plane over IPv6 Omar Ramadan
2026-12-02  4:31 ` Omar Ramadan [this message]
2026-12-02  4:31 ` [PATCH net-next v6 09/12] amt: add netlink attributes for an IPv6 outer transport Omar Ramadan
2026-12-02  4:31 ` [PATCH net-next v6 10/12] MAINTAINERS: amt: cover the amt headers and selftests Omar Ramadan
2026-12-02  4:31 ` [PATCH net-next v6 11/12] selftests: net: add amt_v6.sh for an IPv6 outer transport Omar Ramadan
2026-12-02  4:32 ` [PATCH net-next v6 12/12] selftests: net: add amt_gw_v6.sh for the IPv6 netlink attributes Omar Ramadan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202043200.286260-9-omar@blockcast.net \
    --to=omar@blockcast.net \
    --cc=andrew+netdev@lunn.ch \
    --cc=ap420073@gmail.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox