[PATCH net-next v6 02/12] amt: send the Relay Advertisement over IPv6
From: Omar Ramadan
Date: Tue Dec 01 2026 - 23:32:28 EST
A relay on an IPv6 outer transport must answer a Discovery with the
24-byte IPv6 form of the Relay Advertisement: a gateway tells the two
forms apart by the datagram length (RFC 7450 s5.1.2.5), and the relay
must use the family of the Discovery (s5.3.3.2).
Add struct amt_header_advertisement_v6 and amt_send_advertisement_v6(),
which sends it with a new amt_send_ctrl_v6(). The helper routes with
amt_route6(), which the later IPv6 senders share, and sends with
udp_tunnel6_xmit_skb(), which fills in the UDP checksum RFC 8200 s8.1
makes mandatory. Like the IPv4 senders it uses TC_PRIO_CONTROL and
AMT_TOS and passes no netdev, so control traffic stays out of the tunnel
stats. A drop after the hand-off to IPv6 output is therefore not counted
in tx_errors, which the IPv4 senders do for an ip_local_out() error.
The Advertisement is sent to the Discovery's source from its destination
(s5.1.2, s5.3.3.2), which may be an anycast or secondary address: a
gateway accepts only an Advertisement from the address it sent the
Discovery to. The relay address in the message stays local_ipv6. A
Discovery sent to a multicast group the socket receives, such as
ff02::1, or from ::, is not answered, as a multicast address cannot be a
source (RFC 4291 s2.7).
The IPv4 Advertisement is still sent from local_ip; changing that is a
fix of its own for net.
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 | 102 ++++++++++++++++++++++++++++++++++++++++++++++
include/net/amt.h | 20 +++++++++
2 files changed, 122 insertions(+)
diff --git a/drivers/net/amt.c b/drivers/net/amt.c
index 2f1b9cd04..e3c69ebf7 100644
--- a/drivers/net/amt.c
+++ b/drivers/net/amt.c
@@ -706,6 +706,74 @@ static void __amt_update_relay_status(struct amt_tunnel_list *tunnel,
tunnel->status = status;
}
+static struct dst_entry *amt_route6(struct amt_dev *amt, struct sock *sk,
+ const struct in6_addr *saddr,
+ const struct in6_addr *daddr,
+ __be16 sport, __be16 dport)
+{
+ struct flowi6 fl6;
+
+ memset(&fl6, 0, sizeof(fl6));
+ fl6.flowi6_oif = amt->stream_dev->ifindex;
+ fl6.flowi6_proto = IPPROTO_UDP;
+ fl6.daddr = *daddr;
+ fl6.saddr = *saddr;
+ fl6.fl6_dport = dport;
+ fl6.fl6_sport = sport;
+ /* Route on AMT_TOS too, for a policy rule that matches the DSCP. */
+ fl6.flowlabel = ip6_make_flowinfo(AMT_TOS, 0);
+
+ return ip6_dst_lookup_flow(amt->net, sk, &fl6, NULL);
+}
+
+/* Send a control message from @saddr; returns 0 once handed to IPv6. */
+static int amt_send_ctrl_v6(struct amt_dev *amt, const struct in6_addr *saddr,
+ const struct in6_addr *daddr,
+ __be16 sport, __be16 dport,
+ const void *msg, unsigned int len)
+{
+ struct dst_entry *dst;
+ struct sk_buff *skb;
+ struct sock *sk;
+ int hlen, err;
+
+ rcu_read_lock();
+ sk = rcu_dereference(amt->sk);
+ if (!sk || !netif_running(amt->stream_dev) ||
+ !netif_running(amt->dev)) {
+ err = -ENETDOWN;
+ goto out;
+ }
+
+ dst = amt_route6(amt, sk, saddr, daddr, sport, dport);
+ if (IS_ERR(dst)) {
+ DEV_STATS_INC(amt->dev, tx_errors);
+ err = PTR_ERR(dst);
+ goto out;
+ }
+
+ hlen = LL_RESERVED_SPACE(amt->dev) + sizeof(struct ipv6hdr) +
+ sizeof(struct udphdr);
+ skb = netdev_alloc_skb_ip_align(amt->dev, hlen + len +
+ amt->dev->needed_tailroom);
+ if (!skb) {
+ dst_release(dst);
+ DEV_STATS_INC(amt->dev, tx_errors);
+ err = -ENOMEM;
+ goto out;
+ }
+
+ skb_reserve(skb, hlen);
+ skb_put_data(skb, msg, len);
+ skb->priority = TC_PRIO_CONTROL;
+ udp_tunnel6_xmit_skb(dst, sk, skb, NULL, saddr, daddr, AMT_TOS,
+ ip6_dst_hoplimit(dst), 0, sport, dport, false, 0);
+ err = 0;
+out:
+ rcu_read_unlock();
+ return err;
+}
+
static void amt_send_discovery(struct amt_dev *amt)
{
struct amt_header_discovery *amtd;
@@ -3008,6 +3076,21 @@ out:
rcu_read_unlock();
}
+static void amt_send_advertisement_v6(struct amt_dev *amt, __be32 nonce,
+ const struct in6_addr *saddr,
+ const struct in6_addr *daddr,
+ __be16 dport)
+{
+ struct amt_header_advertisement_v6 amta = {
+ .type = AMT_MSG_ADVERTISEMENT,
+ .nonce = nonce,
+ .ip6 = amt->local_ipv6,
+ };
+
+ amt_send_ctrl_v6(amt, saddr, daddr, amt->relay_port, dport,
+ &amta, sizeof(amta));
+}
+
static bool amt_discovery_handler(struct amt_dev *amt, struct sk_buff *skb)
{
struct amt_header_discovery *amtd;
@@ -3024,6 +3107,25 @@ static bool amt_discovery_handler(struct amt_dev *amt, struct sk_buff *skb)
if (amtd->reserved || amtd->version)
return true;
+ /* The form follows the Discovery's family (RFC 7450 s5.3.3.2), and the
+ * source is the address it was sent to, perhaps an anycast one.
+ */
+ if (amt_v6(amt)) {
+ struct in6_addr saddr = ipv6_hdr(skb)->saddr;
+ struct in6_addr daddr = ipv6_hdr(skb)->daddr;
+
+ /* No answer to a Discovery sent to a group, as a multicast
+ * address cannot be a source (RFC 4291 s2.7), or from ::.
+ */
+ if (ipv6_addr_is_multicast(&daddr) ||
+ ipv6_addr_any(&saddr))
+ return true;
+
+ amt_send_advertisement_v6(amt, amtd->nonce, &daddr, &saddr,
+ udph->source);
+ return false;
+ }
+
amt_send_advertisement(amt, amtd->nonce, iph->saddr, udph->source);
return false;
diff --git a/include/net/amt.h b/include/net/amt.h
index ca35782f8..4c096590d 100644
--- a/include/net/amt.h
+++ b/include/net/amt.h
@@ -133,6 +133,26 @@ struct amt_header_advertisement {
__be32 ip4;
} __packed;
+/* The IPv6 form of the Relay Advertisement: the same header and nonce, then
+ * a 16-byte relay address. A gateway tells the forms apart by the datagram
+ * length (RFC 7450 s5.1.2.5).
+ */
+struct amt_header_advertisement_v6 {
+#if defined(__LITTLE_ENDIAN_BITFIELD)
+ u32 type:4,
+ version:4,
+ reserved:24;
+#elif defined(__BIG_ENDIAN_BITFIELD)
+ u32 version:4,
+ type:4,
+ reserved:24;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+ __be32 nonce;
+ struct in6_addr ip6;
+} __packed;
+
struct amt_header_request {
#if defined(__LITTLE_ENDIAN_BITFIELD)
u32 type:4,