[PATCH net-next v6 01/12] amt: create an AF_INET6 encapsulation socket for an IPv6 outer address
From: Omar Ramadan
Date: Tue Dec 01 2026 - 23:32:14 EST
The amt device binds one AF_INET UDP socket, so a relay and its gateways
can only talk AMT over IPv4. RFC 7450 allows either: the Relay
Advertisement carries an IPv4 or an IPv6 relay address (s5.1.2.5), and a
relay must answer a Discovery received over IPv6 with the IPv6 form
(s5.3.3.2).
Add the outer IPv6 local address to struct amt_dev, and amt_v6(), which
reports the outer family from it. For an IPv6 device amt_create_sock()
binds an AF_INET6 socket that is V6ONLY, so that it does not claim the
IPv4 wildcard port, and that sends and verifies the UDP checksum (RFC
8200 s8.1).
The socket is also bound to IFLA_AMT_LINK. An IPv6 route lookup with a
source address treats the output interface only as a preference, so
without the binding the outer traffic could leave through another link,
and two gateways with the same link-local address on different links
would share a tunnel.
ICMPv6 errors go to amt_err_lookup(), which relies on amt_parse_type()
pulling the AMT header behind the transport header, as "amt: pull the
AMT header behind the transport header in amt_parse_type()" in net does.
Nothing sets local_ipv6 yet, so there is no functional change.
Assisted-by: LLM
Signed-off-by: Omar Ramadan <omar@xxxxxxxxxxxxx>
---
drivers/net/amt.c | 34 ++++++++++++++++++++++++++++------
include/net/amt.h | 2 ++
2 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/drivers/net/amt.c b/drivers/net/amt.c
index b145ed3ca..2f1b9cd04 100644
--- a/drivers/net/amt.c
+++ b/drivers/net/amt.c
@@ -90,6 +90,12 @@ static struct in6_addr mld2_all_node = MLD2_ALL_NODE_INIT;
static struct mld2_grec mldv2_zero_grec;
#endif
+/* An IPv6 local address selects an IPv6 outer transport. */
+static bool amt_v6(const struct amt_dev *amt)
+{
+ return IS_ENABLED(CONFIG_IPV6) && !ipv6_addr_any(&amt->local_ipv6);
+}
+
static void __amt_source_gc_work(void)
{
struct amt_source_node *snode;
@@ -3310,19 +3316,35 @@ drop:
return 0;
}
-static struct sock *amt_create_sock(struct net *net, __be16 port)
+static struct sock *amt_create_sock(const struct amt_dev *amt)
{
struct udp_port_cfg udp_conf;
struct socket *sock;
int err;
memset(&udp_conf, 0, sizeof(udp_conf));
- udp_conf.family = AF_INET;
- udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
+ if (amt_v6(amt)) {
+ /* Bound to :: and to the underlying link: that makes route
+ * lookups strict about the output interface and receives from
+ * that link only, where a link-local peer is unique. V6ONLY
+ * leaves the IPv4 wildcard port to an IPv4 amt device. The UDP
+ * checksum is always sent (RFC 8200 s8.1); a gateway also
+ * accepts a zero one on Multicast Data (RFC 7450 s5.2.3.3), and
+ * amt_rcv() drops it on other messages.
+ */
+ 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.ipv6_v6only = true;
+ } else {
+ udp_conf.family = AF_INET;
+ udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
+ }
- udp_conf.local_udp_port = port;
+ udp_conf.local_udp_port = amt->relay_port;
- err = udp_sock_create(net, &udp_conf, &sock);
+ err = udp_sock_create(amt->net, &udp_conf, &sock);
if (err < 0)
return ERR_PTR(err);
@@ -3334,7 +3356,7 @@ static int amt_socket_create(struct amt_dev *amt)
struct udp_tunnel_sock_cfg tunnel_cfg;
struct sock *sk;
- sk = amt_create_sock(amt->net, amt->relay_port);
+ sk = amt_create_sock(amt);
if (IS_ERR(sk))
return PTR_ERR(sk);
diff --git a/include/net/amt.h b/include/net/amt.h
index 9ba097127..ca35782f8 100644
--- a/include/net/amt.h
+++ b/include/net/amt.h
@@ -361,6 +361,8 @@ struct amt_dev {
__be16 gw_port;
/* Outer local ip */
__be32 local_ip;
+ /* Outer local IPv6 address, :: unless the outer transport is IPv6 */
+ struct in6_addr local_ipv6;
/* Outer remote ip */
__be32 remote_ip;
/* Outer discovery ip */