Skip to contentSkip to conversation

amt: add an IPv6 outer transport

PATCH New Omar Ramadan 2 participants 19 messages Dec 2 – Dec 3

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v6 cover amt: add an IPv6 outer transport

The amt driver runs AMT (RFC 7450) only over IPv4: the relay and the gateway bind one AF_INET UDP socket and every message is routed and sent with the IPv4 helpers. RFC 7450 defines the protocol over either IP version, with an IPv6 form of the Relay Advertisement, so a gateway on an IPv6-only access network cannot reach an amt relay today.

This series adds an IPv6 outer transport to both modes. The outer family is fixed when the link is created: an IPv6 local address (IFLA_AMT_LOCAL_IP6) selects IPv6, and the inner multicast family stays independent, so an IPv6 tunnel carries IGMP/IPv4 and MLD/IPv6 alike.

Use case, which Jakub asked about on v1: Blockcast runs amt relays and gateways to deliver multicast video over networks that do not carry multicast natively, and some of those access networks are IPv6-only. The gateway side has to reach a relay from such a network, and the relay has to answer it, so both ends need the IPv6 form.

Patches 1-5 convert the relay: the AF_INET6 socket, the IPv6 Relay Advertisement, tunnel state keyed on a union amt_addr, and the Membership Query, Membership Update and Multicast Data paths, which share one amt_udp_xmit() instead of IPv6 twins of each sender. Multicast Data over IPv6 is checked against the tunnel MTU and never fragmented, and the source of an SSM group is sent a Packet Too Big. Patch 6 sizes headroom and MTU for the 40-byte outer header. Patches 7-8 convert the gateway; the learned IPv6 relay address is published under a seqlock because, unlike the IPv4 one, it cannot be stored in one access. Patch 9 adds the netlink attributes, last, so that no intermediate commit can create a half-working IPv6 device. Patch 10 adds the AMT headers and selftests to MAINTAINERS, and patches 11-12 add selftests. amt_v6.sh needs the same tools as amt.sh (smcroute, socat, nc, jq, tc), and tcpdump for the Discovery checks, which skip without it.

The series is based on net-next ccb81d5e317b, which carries the six-patch "amt: relay tunnel lifetime, validation and request-path hardening" series (net v7, merged in 24630ff92eff). It depends on that
series: the tunnel creation, MAC and per-source cap that patch 3 re-keys are the ones it introduced.

The IPv6 Relay Advertisement is sent from the address the Discovery was sent to, as RFC 7450 s5.1.2 requires, so a gateway can discover the relay through an anycast or secondary address. A Discovery sent to a multicast group, or from ::, is not answered, since neither can be used as an address. A relay also drops a packet from the wire whose outer source is one of its own addresses, unicast or anycast, as IPv4 input does with a martian: it would answer it to itself over lo. 8/12 exports ipv6_chk_acast_addr() from net/ipv6/anycast.c for the anycast half, a one-line change outside amt. The IPv4 Advertisement has always been sent from local_ip, which breaks the same case for an IPv4 gateway. This series leaves that alone: it changes the behaviour of existing IPv4 relays and is not needed by the IPv6 code. The IPv4 control senders also look the route up with a zero TOS while the packet carries AMT_TOS, which is the mismatch that patch 2 avoids for IPv6; that is a possible separate fix for net.

The IPv6 socket is bound to the underlying link. That makes the IPv6 route lookups strict about the output interface, as the IPv4 ones already are, and keeps link-local peers on other links out of the tunnel table. An IPv6 gateway accepts a zero UDP checksum on Multicast Data only, as RFC 7450 s5.2.3.3 requires; a relay requires the checksum on everything.

The IPv6 tunnel MTU follows the route's path MTU, as on every IPv6 UDP tunnel in the tree. RFC 7450 s5.3.3.6.1 and s6.1 want a relay to offer a switch that turns dynamic tunnel MTU adjustment off. The IPv6 stack gives a tunnel no way to do that: it records every Packet Too Big in a route exception before it looks up the socket, and "mtu lock" does not apply to IPv6. So this series does not offer the switch. Patch 5 explains this in detail, and sends the Packet Too Big only for an SSM group, as RFC 7450 s5.3.3.6.2.2 asks.

Received headers are handled as commit 3656a79f94c4 ("amt: re-read skb header pointers after every pull") requires. amt_rcv(), amt_request_handler() and amt_update_handler() copy the outer source address by value before any pull, or after the last one. The discovery and advertisement handlers take header pointers only after their last pskb_may_pull(), and the send paths never pull the received skb.

The iproute2 support is a separate iproute2-next patch, now at v2:
https://lore.kernel.org/netdev/[email protected]/
The selftests skip when iproute2 or the kernel lacks the attributes.

IFLA_AMT_LOCAL_IP6, IFLA_AMT_DISCOVERY_IP6 and IFLA_AMT_REMOTE_IP6 are the next three values after IFLA_AMT_MAX_TUNNELS in this series. The net-next amt limits series appends IFLA_AMT_MAX_GROUPS and IFLA_AMT_MAX_SOURCES at the same position, so whichever of the two lands second renumbers its attributes after the other's.

Changes in v6 (netdev's Sashiko review of v5):
- 8/12: drop a packet from one of the relay's own unicast or anycast addresses with one helper, amt_saddr_is_local(), scoped to the stream device's L3 domain; export ipv6_chk_acast_addr()
- 5/12: send the Packet Too Big only for an SSM group (RFC 7450 s5.3.3.6.2.2)
- 1/12, 2/12: cite RFC 7450 s5.1.2.5 and s5.3.3.2, not s5.2
- 5/12, 8/12: say that amt_v6() is still false; list every writer of the learned relay address
- 3/12: say that a /64 is not a bound per subscriber
- 11/12: Packet Too Big cases for an SSM and an ASM group, an anycast own-source case, capture failures say why, SPOOF without DAD, amtg starts once its bridge port forwards, paced sends, amtg4 deleted, br_gw's MAC set
- 12/12: do not skip on "Operation not supported"
- Messages and comments shortened
- Not changed: the /64 unit of the per-source cap (3/12)

Changes in v5 (netdev's Sashiko review of v4):
- 2/12 (Medium): a relay's amt_rcv() drops a packet whose outer source is an address that ipv6_chk_addr() finds on a device of the namespace, before it parses the message. IPv4 input refuses such a source as a martian when it comes in from the wire; IPv6 input delivers it, and a forged Discovery or Request would have made the relay answer itself over lo. 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 reaches it. The check is in 8/12, which holds the receive path, and covers the Request and the Update too. 11/12 sends such a Discovery in from the wire, from a namespace that holds the address on its lo only, and checks that it is dropped and that nothing answers on lo; the same Discovery over lo is answered.
- 5/12 (Medium): not changed in design. The message and the comment say that the transport header is put back to the offset it had on entry, which for a forwarded packet is past a Hop-by-Hop header only; ip6_forward() and ip6_finish_output_gso(), where ip6mr forwarding ends, validate a GSO skb from the same offset, so another extension header is not counted there either.
- 11/12 (Medium, Low, Low): disc_answer() fails the check, with status 2, when tcpdump is still not listening after five seconds, instead of sending anyway; the comment that amtr is NOARP is removed, as ether_setup() clears the flag; no change for the ERR trap in slowwait, which does not fire for the polled command on bash 5.2.
- 12/12 (Low): the header no longer lists a 16-byte attribute as reachable only from a raw netlink client.

Changes in v4 (netdev's Sashiko review of v3):
- 5/12 (Medium): the message says what a forged Packet Too Big does: it lowers the tunnel MTU to 1230, so every multicast payload above that, IPv4 or IPv6, is dropped toward that gateway and its source is told nothing, until the route exception expires. The design is unchanged, as that is what path MTU discovery does to any IPv6 UDP tunnel that must not fragment. Two drops that were not counted are counted in tx_dropped, and two header comments say when no Packet Too Big is sent.
- 12/12 (Medium): the probe skips only on the readback an iproute2 that truncates the literal gives, 32.1.13.184; any other readback without a colon is a FAIL. The nodad comment and the header's list of what is not covered are corrected.
- 11/12: a listener that must get nothing has to be alive at the end, the Discovery capture waits until tcpdump is listening, and the comments on DAD and on the IGMP egress counter are corrected.
- 3/12, 4/12: the messages describe the old read of the source address, the Request handler and the first patch of the series correctly, and the comments that still said per address say source.
- 8/12: the comment and the message no longer say IPv4 input never delivers a 0.0.0.0 source, and amt_route6() refuses ::, which a transmit can read just after a gateway forgot its relay.
- 5/12, 8/12: the messages cite RFC 7450 s5.3.3.6.2.2 with its SSM condition and s5.1.6 as it reads, and describe the old read of the outer source on an IPv6 header correctly.

Changes in v3 (netdev's Sashiko review of v2):
- The base is net-next ccb81d5e317b. Only MAINTAINERS and the net selftests Makefile changed under the series, so 10/12 to 12/12 are rebased and nothing else is.
- 3/12 (Medium): the per-source tunnel cap counts the tunnels of one IPv6 /64, not of one address, as one host usually owns a whole /64 and could otherwise open the cap's worth of tunnels from each of its addresses and keep the table full. A tunnel is still found by its full endpoint, so gateways in one /64 each keep their own tunnel and share the cap. The message no longer says that amt_request_handler() snapshots the source before a pull (Low).
- 5/12 (Medium, Low): amt_dev_xmit() puts the transport header of a data packet back after ipv6_mc_check_mld(), which leaves it just past the fixed IPv6 header, so the per-segment length of an inner IPv6 GSO skb counts any extension headers. amt_tmtu() returns zero, not a wrapped value, for a route MTU below the outer headers.
- 2/12 (Low): the message says that a drop after the hand-off to IPv6 output is not counted in tx_errors, as the sender passes no netdev.
- 4/12 (Low): the comment of amt_send_membership_query() says that the endpoint is passed by pointer.
- 6/12 (Low): the message names the right drop site, the tunnel MTU check, and no longer claims that ip6_fragment() drops the packet.
- 8/12 (Low): the exact-length check of an Advertisement applies to an IPv6 gateway only, so an IPv4 gateway behaves as it always did. The message says so.
- 11/12 (Medium, Medium, Low): the probe skips on "binary attribute size out of range", which is what this kernel says to an iproute2 that packs an IPv6 literal into the IPv4 attribute, and on an iproute2 or kernel without amt, instead of failing; the egress check of amt.sh is ported, and a third gateway in the first gateways' /64 is checked to get no tunnel while one in another /64 does. The message no longer says "plus jq" and names the checks that are not carried over.
- 12/12 (Medium, Medium, Low): the same probe fixes; a readback that is not an IPv6 address is a skip, as four truncated bytes cannot be told from an IPv4 address.
- Not changed: the IPv4 Advertisement is still sent from local_ip, and the IPv6 tunnel MTU still follows the route's path MTU.

Changes in v2 (netdev's Sashiko review of v1, and net v7):
- The base is net-next 24630ff92eff. The three net prerequisites of v1 are now in net, as v7 of the hardening series. v1's "amt: send the relay General Query directly" is not needed any more, and the GSO fix was done differently: the device still offers software GSO, so the tunnel MTU check in 5/12 is made per segment with skb_gso_validate_network_len() instead of assuming a single packet.
- v1 3/13 and 5/13 are one patch now (3/12). v7 made the Request handler stateless and moved tunnel creation to the authenticated Membership Update, so the endpoint key is carried through amt_tunnel_lookup(), amt_tunnel_get_or_create(), amt_update_tunnel() and the new amt_relay_mac(), which both handlers use for the MAC. The ICMPv6 Destination Unreachable for a full tunnel table is gone, as v7 drops such a Request silently.
- 2/12: route on AMT_TOS through the flow label (Medium on 4/13, Low on 2/13); count route and allocation failures with DEV_STATS_INC(); drop a Discovery from ::. A failure of ip6_local_out() is still not counted, as udp_tunnel6_xmit_skb() returns void and passing a device would add control messages to the tunnel's byte counters.
- 4/12: the Membership Query and the relay's General Query take the gateway endpoint as a union amt_addr, for v7's stateless Request.
- 5/12: at most one Packet Too Big per multicast packet (High on 6/13), not one per tunnel; the inner family comes from the parsed version, not skb->protocol (Low); a tunnel MTU below IPV6_MIN_MTU, which a source would ignore, only drops the payload.
- 7/12 (8/13): the message says that none of the new sender code can run yet and that the amt_send_membership_update() conversion changes no IPv4 behaviour (Low).
- 8/12 (9/13): an Advertisement is accepted only if its UDP length is that of the device's family (Low); the deferred Query check in amt_membership_query_handler() uses amt_from_relay(); the IPv4 half of the 0.0.0.0 fix is net v7 3/6 (Medium).
- 9/12 (10/13): the policy of the existing IPv4 attributes is net v7 4/6, so the hunk that refused a 16-byte value is dropped (Low).
- 11/12 (12/13): disc_answer() reports a harness failure separately, so the ff02::1 check cannot pass because the capture never ran (Medium); the script skips without CONFIG_IPV6 before creating any link (Low). CONFIG_IP_MROUTE is already in the net selftests config.
- Fixed by net v7 and inherited: the Update handler use-after-free (High, 5/13), the duplicate-tunnel race and the zero-key MAC (Medium, 3/13).

Assisted-by LLM

Omar Ramadan (12):
amt: create an AF_INET6 encapsulation socket for an IPv6 outer address
amt: send the Relay Advertisement over IPv6
amt: key relay tunnels on a union amt_addr endpoint
amt: send the Membership Query over IPv6
amt: forward multicast data over IPv6
amt: size the encapsulation headroom by the outer IP version
amt: send the AMT gateway control plane over IPv6
amt: receive the AMT gateway control plane over IPv6
amt: add netlink attributes for an IPv6 outer transport
MAINTAINERS: amt: cover the amt headers and selftests
selftests: net: add amt_v6.sh for an IPv6 outer transport
selftests: net: add amt_gw_v6.sh for the IPv6 netlink attributes

 MAINTAINERS                              |   3 +
 drivers/net/amt.c                        | 885 +++++++++++++++++------
 include/net/amt.h                        |  52 +-
 include/uapi/linux/amt.h                 |  13 +
 net/ipv6/anycast.c                       |   1 +
 tools/testing/selftests/net/Makefile     |   2 +
 tools/testing/selftests/net/amt_gw_v6.sh | 205 ++++++
 tools/testing/selftests/net/amt_v6.sh    | 716 ++++++++++++++++++
 8 files changed, 1665 insertions(+), 212 deletions(-)
 create mode 100755 tools/testing/selftests/net/amt_gw_v6.sh
 create mode 100755 tools/testing/selftests/net/amt_v6.sh

base-commit: ccb81d5e317b0ae2cc66ca1c628c0ca907928f94

#1 lore raw

PATCH v6 1/12 amt: create an AF_INET6 encapsulation socket for an IPv6 outer address

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
---
 drivers/net/amt.c | 34 ++++++++++++++++++++++++++++------
 include/net/amt.h |  2 ++
 2 files changed, 30 insertions(+), 6 deletions(-)
drivers/net/amt.c +28 -6
index b145ed3ca..2f1b9cd04 100644
@@ -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);
 
include/net/amt.h +2 -0
index 9ba097127..ca35782f8 100644
@@ -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 */
#2 lore raw

PATCH v6 2/12 amt: send the Relay Advertisement over IPv6

In reply to Omar Ramadan

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
---
 drivers/net/amt.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++
 include/net/amt.h |  20 +++++++++
 2 files changed, 122 insertions(+)
drivers/net/amt.c +102 -0
index 2f1b9cd04..e3c69ebf7 100644
@@ -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;
include/net/amt.h +20 -0
index ca35782f8..4c096590d 100644
@@ -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,
#3 lore raw

PATCH v6 3/12 amt: key relay tunnels on a union amt_addr endpoint

In reply to Omar Ramadan

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
---
 drivers/net/amt.c | 113 +++++++++++++++++++++++++++++++---------------
 include/net/amt.h |  18 ++++----
 2 files changed, 86 insertions(+), 45 deletions(-)
drivers/net/amt.c +77 -36
index e3c69ebf7..bfdce6897 100644
@@ -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;
include/net/amt.h +9 -9
index 4c096590d..82ab63876 100644
@@ -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) + \
#4 lore raw

Re: [PATCH net-next v6 03/12] amt: key relay tunnels on a union amt_addr endpoint

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message's reason for the IPv6 per-source tunnel cap is wrong, so the trade-off it describes is misjudged.

--

amt: key relay tunnels on a union amt_addr endpoint

This commit changes the relay tunnel key from a __be32 IPv4 address to a union amt_addr, so that an IPv6 outer source can be stored and compared in full. It replaces siphash_3u32() for the Update MAC with amt_relay_mac(). It also makes the per-source tunnel cap count an IPv6 /64 instead of a single address, via amt_same_source().

Quoted 4 lines
drivers/net/amt.c +0 -0
index e3c69ebf78f20..bfdce6897f7cd 100644

[ ... ]

Quoted 6 lines
drivers/net/amt.c +2 -0
@@ -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
+

drivers/net/amt.c static bool amt_addr_equal(union amt_addr *a, union amt_addr *b) · line 224

Quoted 10 lines
drivers/net/amt.c +10 -0
+/* 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;
+}

[Severity: Low]
Is the premise behind this grouping accurate? The commit message says:

"one host normally owns a /64 and could otherwise hold the cap from each of its addresses"

A /64 is usually the size of one link, not one host. SLAAC gives every host on a LAN an address from the same /64. Many cloud subnets and VPS providers also put many hosts or customers in one /64.

On an IPv6 relay, all gateways in such a /64 would share a single cap from amt_max_tunnels_per_src():

	return clamp_t(u32, amt->max_tunnels / 8, 1,
		       AMT_MAX_TUNNELS_PER_SRC);

That cap can never go above 16, whatever max_tunnels is set to.

Once that many tunnels exist in the /64, amt_request_handler() stops sending the Membership Query:

	if (!have && (full || per_src >= amt_max_tunnels_per_src(amt)))
		return true;

and amt_tunnel_get_or_create() refuses to create a tunnel:

	if (amt->nr_tunnels >= amt->max_tunnels ||
	    per_src >= amt_max_tunnels_per_src(amt)) {

Would every later gateway on that link then get no service? It also looks like one party in a shared /64 could use up the cap and lock out its /64 neighbours. That can't happen for IPv4 hosts that have separate public addresses.

The commit message does say "Gateways in one /64 share the cap", but it treats this as the unusual case when it is probably the common one.

This patch can't reach the path yet, because amt_v6() is still false. The behaviour stays the same once the later patch "amt: add netlink attributes for an IPv6 outer transport" makes amt_v6() reachable.

Could the commit message rationale be corrected, or the /64 grouping be reconsidered?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202043200.286260-1-omar%40blockcast.net
#5 lore raw

PATCH v6 4/12 amt: send the Membership Query over IPv6

In reply to Omar Ramadan

amt_send_membership_query() routes and sends only over IPv4. Move that step into amt_udp_xmit(), which takes an skb that carries the AMT message, a union amt_addr destination and the ports, and sends over the device's family: ip_route_output_key() and udp_tunnel_xmit_skb() for IPv4, amt_route6() and udp_tunnel6_xmit_skb() for IPv6, both with AMT_TOS.

iptunnel_xmit() scrubs the skb but udp_tunnel6_xmit_skb() does not, so the IPv6 path calls skb_scrub_packet(); otherwise the inner packet's conntrack entry would stay on the outer one. The Query has none, the Multicast Data and Membership Update sent through this helper later do.

amt_send_membership_query() sizes its headroom with amt_ip_hlen(), and the IPv4 route lookup now runs after the header push; on a route failure the caller still frees the skb. amt_send_igmp_gq() and amt_send_mld_gq() take the gateway's endpoint as a union amt_addr. The v6 argument still selects the inner protocol, independent of the outer family.

amt_v6() is still false for every device, so the only effect is the order of the IPv4 route lookup.

Assisted-by LLM
---
 drivers/net/amt.c | 122 ++++++++++++++++++++++++++++++----------------
 1 file changed, 81 insertions(+), 41 deletions(-)
drivers/net/amt.c +81 -41
index bfdce6897..1f4914a63 100644
@@ -96,6 +96,11 @@ static bool amt_v6(const struct amt_dev *amt)
 	return IS_ENABLED(CONFIG_IPV6) && !ipv6_addr_any(&amt->local_ipv6);
 }
 
+static unsigned int amt_ip_hlen(const struct amt_dev *amt)
+{
+	return amt_v6(amt) ? sizeof(struct ipv6hdr) : sizeof(struct iphdr);
+}
+
 static void amt_outer_saddr(const struct amt_dev *amt,
 			    const struct sk_buff *skb, union amt_addr *addr)
 {
@@ -999,16 +1004,16 @@ out:
 
 static bool amt_send_membership_query(struct amt_dev *amt,
 				      struct sk_buff *skb,
-				      __be32 daddr, __be16 dport,
+				      const union amt_addr *daddr,
+				      __be16 dport,
 				      __be32 nonce, u64 mac,
 				      bool v6);
 
-/* The destination is passed by value rather than as a tunnel: a Request is
- * unauthenticated, so no tunnel state is created or changed for it - see
- * amt_request_handler().
+/* A Request is unauthenticated, so @daddr is the caller's copy of the
+ * endpoint and no tunnel state is touched; see amt_request_handler().
  */
-static void amt_send_igmp_gq(struct amt_dev *amt, __be32 daddr, __be16 dport,
-			     __be32 nonce, u64 mac)
+static void amt_send_igmp_gq(struct amt_dev *amt, const union amt_addr *daddr,
+			     __be16 dport, __be32 nonce, u64 mac)
 {
 	struct sk_buff *skb;
 
@@ -1097,8 +1102,8 @@ static struct sk_buff *amt_build_mld_gq(struct amt_dev *amt)
 	return skb;
 }
 
-static void amt_send_mld_gq(struct amt_dev *amt, __be32 daddr, __be16 dport,
-			    __be32 nonce, u64 mac)
+static void amt_send_mld_gq(struct amt_dev *amt, const union amt_addr *daddr,
+			    __be16 dport, __be32 nonce, u64 mac)
 {
 	struct sk_buff *skb;
 
@@ -1114,8 +1119,8 @@ static void amt_send_mld_gq(struct amt_dev *amt, __be32 daddr, __be16 dport,
 	}
 }
 #else
-static void amt_send_mld_gq(struct amt_dev *amt, __be32 daddr, __be16 dport,
-			    __be32 nonce, u64 mac)
+static void amt_send_mld_gq(struct amt_dev *amt, const union amt_addr *daddr,
+			    __be16 dport, __be32 nonce, u64 mac)
 {
 }
 #endif
@@ -1232,6 +1237,63 @@ static void amt_req_work(struct work_struct *work)
 				 msecs_to_jiffies(100));
 }
 
+/* Route and send an AMT message to @daddr over the device's family. Over
+ * IPv6 @dst may hold the route already looked up; it is consumed.
+ */
+static int amt_udp_xmit(struct amt_dev *amt, struct sock *sk,
+			struct sk_buff *skb, const union amt_addr *daddr,
+			__be16 sport, __be16 dport, dscp_t dscp)
+{
+	struct rtable *rt;
+	struct flowi4 fl4;
+
+	if (amt_v6(amt)) {
+		struct dst_entry *dst;
+
+		dst = amt_route6(amt, sk, &amt->local_ipv6, &daddr->ip6,
+				 sport, dport);
+		if (IS_ERR(dst)) {
+			netdev_dbg(amt->dev, "no route to %pI6c\n",
+				   &daddr->ip6);
+			return PTR_ERR(dst);
+		}
+		/* Unlike iptunnel_xmit(), udp_tunnel6_xmit_skb() does not scrub
+		 * the skb.
+		 */
+		skb_scrub_packet(skb, false);
+		udp_tunnel6_xmit_skb(dst, sk, skb, amt->dev, &amt->local_ipv6,
+				     &daddr->ip6, AMT_TOS,
+				     ip6_dst_hoplimit(dst), 0, sport, dport,
+				     false, 0);
+		return 0;
+	}
+
+	memset(&fl4, 0, sizeof(struct flowi4));
+	fl4.flowi4_oif         = amt->stream_dev->ifindex;
+	fl4.daddr              = daddr->ip4;
+	fl4.saddr              = amt->local_ip;
+	fl4.flowi4_dscp        = dscp;
+	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", &daddr->ip4);
+		return PTR_ERR(rt);
+	}
+
+	udp_tunnel_xmit_skb(rt, sk, skb,
+			    fl4.saddr,
+			    fl4.daddr,
+			    AMT_TOS,
+			    ip4_dst_hoplimit(&rt->dst),
+			    0,
+			    sport,
+			    dport,
+			    false,
+			    false,
+			    0);
+	return 0;
+}
+
 static bool amt_send_membership_update(struct amt_dev *amt,
 				       struct sk_buff *skb,
 				       bool v6)
@@ -1362,17 +1424,17 @@ static void amt_send_multicast_data(struct amt_dev *amt,
  *
  * No per-source state is read or created: the gateway echoes the nonce and
  * the keyed MAC in its Membership Update, where the MAC is recomputed, so
- * everything this needs is passed by value.
+ * everything this needs is passed in: the nonce and the MAC by value, the
+ * endpoint as a pointer to the caller's copy.
  */
 static bool amt_send_membership_query(struct amt_dev *amt,
 				      struct sk_buff *skb,
-				      __be32 daddr, __be16 dport,
+				      const union amt_addr *daddr,
+				      __be16 dport,
 				      __be32 nonce, u64 mac,
 				      bool v6)
 {
 	struct amt_header_membership_query *amtmq;
-	struct rtable *rt;
-	struct flowi4 fl4;
 	struct sock *sk;
 	int err;
 
@@ -1381,23 +1443,11 @@ static bool amt_send_membership_query(struct amt_dev *amt,
 		return true;
 
 	err = skb_cow_head(skb, LL_RESERVED_SPACE(amt->dev) + sizeof(*amtmq) +
-			   sizeof(struct iphdr) + 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              = daddr;
-	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", &daddr);
-		return true;
-	}
-
 	amtmq		= skb_push(skb, sizeof(*amtmq));
 	amtmq->version	= 0;
 	amtmq->type	= AMT_MSG_MEMBERSHIP_QUERY;
@@ -1411,18 +1461,8 @@ static bool amt_send_membership_query(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->relay_port,
-			    dport,
-			    false,
-			    false,
-			    0);
-	return false;
+	return amt_udp_xmit(amt, sk, skb, daddr, amt->relay_port, dport,
+			    inet_dsfield_to_dscp(AMT_TOS));
 }
 
 static netdev_tx_t amt_dev_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -3241,10 +3281,10 @@ static bool amt_request_handler(struct amt_dev *amt, struct sk_buff *skb)
 
 	mac = amt_relay_mac(&saddr, udph->source, amtrh->nonce, &key);
 	if (!amtrh->p)
-		amt_send_igmp_gq(amt, saddr.ip4, udph->source, amtrh->nonce,
+		amt_send_igmp_gq(amt, &saddr, udph->source, amtrh->nonce,
 				 mac);
 	else
-		amt_send_mld_gq(amt, saddr.ip4, udph->source, amtrh->nonce,
+		amt_send_mld_gq(amt, &saddr, udph->source, amtrh->nonce,
 				mac);
 
 	return false;
#6 lore raw

Re: [PATCH net-next v6 04/12] amt: send the Membership Query over IPv6

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] amt_udp_xmit() in drivers/net/amt.c takes a dscp_t dscp argument, but only the IPv4 branch uses it (fl4.flowi4_dscp = dscp;,…

--

amt: send the Membership Query over IPv6

This commit moves the route lookup and transmit step of amt_send_membership_query() into a new helper, amt_udp_xmit(). The helper sends over IPv4 or IPv6 depending on the device's outer family. amt_send_igmp_gq() and amt_send_mld_gq() now take the gateway endpoint as a union amt_addr.

Quoted 4 lines
drivers/net/amt.c +0 -0
index bfdce6897f7cd..1f4914a635f48 100644

[ ... ]

Quoted 19 lines
drivers/net/amt.c +15 -0
@@ -1232,6 +1237,63 @@ static void amt_req_work(struct work_struct *work)
 				 msecs_to_jiffies(100));
 }
 
+/* Route and send an AMT message to @daddr over the device's family. Over
+ * IPv6 @dst may hold the route already looked up; it is consumed.
+ */
+static int amt_udp_xmit(struct amt_dev *amt, struct sock *sk,
+			struct sk_buff *skb, const union amt_addr *daddr,
+			__be16 sport, __be16 dport, dscp_t dscp)
+{
+	struct rtable *rt;
+	struct flowi4 fl4;
+
+	if (amt_v6(amt)) {
+		struct dst_entry *dst;
+
+		dst = amt_route6(amt, sk, &amt->local_ipv6, &daddr->ip6,
+				 sport, dport);

[ ... ]

drivers/net/amt.c static void amt_req_work(struct work_struct *work) · line 1275

Quoted 5 lines
drivers/net/amt.c +5 -0
+	memset(&fl4, 0, sizeof(struct flowi4));
+	fl4.flowi4_oif         = amt->stream_dev->ifindex;
+	fl4.daddr              = daddr->ip4;
+	fl4.saddr              = amt->local_ip;
+	fl4.flowi4_dscp        = dscp;

[Severity: Low]
Is it intended that amt_udp_xmit() ignores its dscp argument over IPv6?

Only the IPv4 branch uses dscp. The IPv6 branch calls amt_route6() without it, and amt_route6() always routes on AMT_TOS:

drivers/net/amt.c:amt_route6() {
    ...
	/* Route on AMT_TOS too, for a policy rule that matches the DSCP. */
	fl6.flowlabel		= ip6_make_flowinfo(AMT_TOS, 0);
    ...
}

In this commit the only caller is amt_send_membership_query(). It passes inet_dsfield_to_dscp(AMT_TOS), so both families route the same way for now.

The commit message says the Multicast Data and Membership Update will go through this helper later. Later in the series, amt_send_multicast_data() calls:

    amt_udp_xmit(..., tunnel->source_port, 0, dst)

Over IPv4, Multicast Data is routed with DSCP 0, as it was before the series. Over IPv6, the dst comes from amt_route6() with AMT_TOS, and the 0 is ignored.

A DSCP-keyed policy rule (ip rule ... dsfield) would then match IPv6 Multicast Data but not IPv4 Multicast Data, even though both carry AMT_TOS on the wire.

Could the IPv6 path pass dscp through to amt_route6(), or could the parameter be dropped so that both families route the same way?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202043200.286260-1-omar%40blockcast.net
#7 lore raw

PATCH v6 5/12 amt: forward multicast data over IPv6

In reply to Omar Ramadan

amt_send_multicast_data() routes and sends the relay's copy of a multicast packet only over IPv4. Size its headroom for the outer family and send it through amt_udp_xmit(). A copy that cannot be sent is now counted in tx_dropped, and one with no route is freed with SKB_DROP_REASON_IP_OUTNOROUTES. The inner protocol stays independent of the outer family.

RFC 7450 s5.3.3.6.3.2 forbids a Fragment header on Multicast Data over IPv6, and s5.3.3.6.2.2 has a payload above the tunnel MTU dropped and, if it came from an SSM source, a Packet Too Big sent to it. The scrub in amt_udp_xmit() clears ignore_df, so ip6_fragment() would drop such a packet and send the Packet Too Big to the relay itself. amt_send_multicast_data() therefore routes first and compares the inner packet with the route's MTU less the outer headers and the AMT header, per segment with skb_gso_validate_network_len() for a GSO skb. A route MTU below the outer headers gives a tunnel MTU of zero, not a wrapped one.

A packet can match every tunnel, and an attacker can create tunnels, so amt_dev_xmit() sends one Packet Too Big through icmpv6_ndo_send() after walking all of them, with the smallest failing tunnel MTU. It is sent only for an SSM group, ff3x::/32 (RFC 4607 s1); the source of an ASM group is not told. A tunnel MTU below IPV6_MIN_MTU is left out, as a smaller Packet Too Big is ignored. An IPv4 payload is dropped without an ICMP error, as icmp_send() does not answer multicast, and is not fragmented first (s5.3.3.6.2.1), as the IPv4 tunnel does not either.

ipv6_mc_check_mld() leaves the transport header just past the fixed IPv6 header, also for a packet it rejects as not MLD. amt_dev_xmit() puts it back to the offset it had on entry, or the per-segment length of a GSO skb would miss extension headers; the forwarding path counts them no better, as in ip6_forward() and ip6_finish_output_gso().

The tunnel MTU follows the route's path MTU, as on every IPv6 UDP tunnel. RFC 7450 s5.3.3.6.1 and s6.1 ask for a way to turn that off. The IPv6 stack gives a tunnel none: udpv6_err() records the lowered MTU before it looks up a socket, and "mtu lock" does not stop it. This series adds none. A forged Packet Too Big, which anyone who can send ICMPv6 to the relay can aim at a gateway's address, lowers the tunnel MTU toward that gateway, down to 1230 (1280 less the 50 bytes of IPv6, UDP and AMT headers), until the route exception expires, 10 minutes by default, and every payload above it is dropped toward that gateway. A forged value M with 1330 <= M < 1490 on a 1500 byte link, where the device MTU is 1440, gives a tunnel MTU of M - 50, which the source of an SSM group is sent if its payload exceeds it, and may apply to the group for every receiver. Sending the Packet Too Big only for SSM narrows who learns of it; it does not authenticate it.

amt_v6() is still false for every device, so on an IPv4 relay only the tx_dropped accounting, the drop reason and the transport header restore take effect.

Assisted-by LLM
---
 drivers/net/amt.c | 134 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 94 insertions(+), 40 deletions(-)
drivers/net/amt.c +94 -40
index 1f4914a63..1780d3dca 100644
@@ -1237,25 +1237,53 @@ static void amt_req_work(struct work_struct *work)
 				 msecs_to_jiffies(100));
 }
 
+/* A payload above the tunnel MTU is dropped, as RFC 7450 s5.3.3.6 forbids a
+ * Fragment header. Its source is sent a Packet Too Big only for an SSM group
+ * (s5.3.3.6.2.2) and not below IPV6_MIN_MTU, which a source would ignore.
+ * The tunnel MTU follows the route's path MTU, as on any IPv6 UDP tunnel.
+ */
+static u32 amt_tmtu(const struct dst_entry *dst)
+{
+	u32 hlen = sizeof(struct ipv6hdr) + sizeof(struct udphdr) +
+		   sizeof(struct amt_header_mcast_data);
+	u32 mtu = dst_mtu(dst);
+
+	return mtu > hlen ? mtu - hlen : 0;
+}
+
+/* FF3x::/32 is SSM for any scope x (RFC 4607 s1). */
+static bool amt_group6_is_ssm(const struct in6_addr *group)
+{
+	return (group->s6_addr32[0] & htonl(0xfff0ffff)) == htonl(0xff300000);
+}
+
+static bool amt_tmtu_exceeded(const struct sk_buff *skb, u32 tmtu)
+{
+	if (skb_is_gso(skb))
+		return !skb_gso_validate_network_len(skb, tmtu);
+	return skb->len > tmtu;
+}
+
 /* Route and send an AMT message to @daddr over the device's family. Over
  * IPv6 @dst may hold the route already looked up; it is consumed.
  */
 static int amt_udp_xmit(struct amt_dev *amt, struct sock *sk,
 			struct sk_buff *skb, const union amt_addr *daddr,
-			__be16 sport, __be16 dport, dscp_t dscp)
+			__be16 sport, __be16 dport, dscp_t dscp,
+			struct dst_entry *dst)
 {
 	struct rtable *rt;
 	struct flowi4 fl4;
 
 	if (amt_v6(amt)) {
-		struct dst_entry *dst;
-
-		dst = amt_route6(amt, sk, &amt->local_ipv6, &daddr->ip6,
-				 sport, dport);
-		if (IS_ERR(dst)) {
-			netdev_dbg(amt->dev, "no route to %pI6c\n",
-				   &daddr->ip6);
-			return PTR_ERR(dst);
+		if (!dst) {
+			dst = amt_route6(amt, sk, &amt->local_ipv6,
+					 &daddr->ip6, sport, dport);
+			if (IS_ERR(dst)) {
+				netdev_dbg(amt->dev, "no route to %pI6c\n",
+					   &daddr->ip6);
+				return PTR_ERR(dst);
+			}
 		}
 		/* Unlike iptunnel_xmit(), udp_tunnel6_xmit_skb() does not scrub
 		 * the skb.
@@ -1354,26 +1382,55 @@ static bool amt_send_membership_update(struct amt_dev *amt,
 	return false;
 }
 
+/* Send the relay's copy to one gateway. Over IPv6 a payload that does not
+ * fit the tunnel MTU is dropped and *@ptb lowered to that MTU, for
+ * amt_dev_xmit() to send one Packet Too Big after walking every tunnel.
+ */
 static void amt_send_multicast_data(struct amt_dev *amt,
 				    const struct sk_buff *oskb,
 				    struct amt_tunnel_list *tunnel,
-				    bool v6)
+				    bool v6, u32 *ptb)
 {
 	struct amt_header_mcast_data *amtmd;
+	struct dst_entry *dst = NULL;
 	struct sk_buff *skb;
-	struct iphdr *iph;
-	struct flowi4 fl4;
-	struct rtable *rt;
 	struct sock *sk;
+	int err;
 
 	sk = rcu_dereference_bh(amt->sk);
 	if (!sk)
 		return;
 
-	skb = skb_copy_expand(oskb, sizeof(*amtmd) + sizeof(*iph) +
+	if (amt_v6(amt)) {
+		u32 tmtu;
+
+		dst = amt_route6(amt, sk, &amt->local_ipv6, &tunnel->addr.ip6,
+				 amt->relay_port, tunnel->source_port);
+		if (IS_ERR(dst)) {
+			netdev_dbg(amt->dev, "no route to %pI6c\n",
+				   &tunnel->addr.ip6);
+			DEV_STATS_INC(amt->dev, tx_dropped);
+			return;
+		}
+
+		tmtu = amt_tmtu(dst);
+		if (amt_tmtu_exceeded(oskb, tmtu)) {
+			dst_release(dst);
+			/* A Packet Too Big below IPV6_MIN_MTU is ignored. */
+			if (tmtu >= IPV6_MIN_MTU && (!*ptb || tmtu < *ptb))
+				*ptb = tmtu;
+			DEV_STATS_INC(amt->dev, tx_dropped);
+			return;
+		}
+	}
+
+	skb = skb_copy_expand(oskb, sizeof(*amtmd) + amt_ip_hlen(amt) +
 			      sizeof(struct udphdr), 0, GFP_ATOMIC);
-	if (!skb)
+	if (!skb) {
+		dst_release(dst);
+		DEV_STATS_INC(amt->dev, tx_dropped);
 		return;
+	}
 
 	/* amt_dev_xmit() pulled the Ethernet header without moving the mac
 	 * header. The tunnelled payload has no link-layer header, so the
@@ -1382,18 +1439,8 @@ static void amt_send_multicast_data(struct amt_dev *amt,
 	skb_reset_mac_header(skb);
 	skb_reset_inner_headers(skb);
 	if (udp_tunnel_handle_offloads(skb, true)) {
-		kfree_skb(skb);
-		return;
-	}
-
-	memset(&fl4, 0, sizeof(struct flowi4));
-	fl4.flowi4_oif         = amt->stream_dev->ifindex;
-	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->addr.ip4);
+		dst_release(dst);
+		DEV_STATS_INC(amt->dev, tx_dropped);
 		kfree_skb(skb);
 		return;
 	}
@@ -1407,17 +1454,12 @@ static void amt_send_multicast_data(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->relay_port,
-			    tunnel->source_port,
-			    false,
-			    false,
-			    0);
+	err = amt_udp_xmit(amt, sk, skb, &tunnel->addr, amt->relay_port,
+			   tunnel->source_port, 0, dst);
+	if (err) {
+		DEV_STATS_INC(amt->dev, tx_dropped);
+		kfree_skb_reason(skb, SKB_DROP_REASON_IP_OUTNOROUTES);
+	}
 }
 
 /* Send a Membership Query in reply to an unauthenticated Request.
@@ -1462,7 +1504,7 @@ static bool amt_send_membership_query(struct amt_dev *amt,
 	else
 		skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
 	return amt_udp_xmit(amt, sk, skb, daddr, amt->relay_port, dport,
-			    inet_dsfield_to_dscp(AMT_TOS));
+			    inet_dsfield_to_dscp(AMT_TOS), NULL);
 }
 
 static netdev_tx_t amt_dev_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -1480,6 +1522,7 @@ static netdev_tx_t amt_dev_xmit(struct sk_buff *skb, struct net_device *dev)
 	struct iphdr *iph;
 	bool data = false;
 	bool v6 = false;
+	u32 ptb = 0;
 	u32 hash;
 
 	iph = ip_hdr(skb);
@@ -1504,6 +1547,9 @@ static netdev_tx_t amt_dev_xmit(struct sk_buff *skb, struct net_device *dev)
 		group.ip4 = ip_hdr(skb)->daddr;
 #if IS_ENABLED(CONFIG_IPV6)
 	} else if (iph->version == 6) {
+		bool th_set = skb_transport_header_was_set(skb);
+		int th_off = th_set ? skb_transport_offset(skb) : 0;
+
 		ip6h = ipv6_hdr(skb);
 		if (!ipv6_addr_is_multicast(&ip6h->daddr))
 			goto free;
@@ -1520,6 +1566,12 @@ static netdev_tx_t amt_dev_xmit(struct sk_buff *skb, struct net_device *dev)
 			}
 		} else {
 			data = true;
+			/* ipv6_mc_check_mld() moved the transport header just
+			 * past the fixed header; restore it, or a GSO skb's
+			 * segments look shorter than they are.
+			 */
+			if (th_set)
+				skb_set_transport_header(skb, th_off);
 		}
 		v6 = true;
 		group.ip6 = ipv6_hdr(skb)->daddr;
@@ -1573,8 +1625,10 @@ static netdev_tx_t amt_dev_xmit(struct sk_buff *skb, struct net_device *dev)
 			}
 			continue;
 found:
-			amt_send_multicast_data(amt, skb, tunnel, v6);
+			amt_send_multicast_data(amt, skb, tunnel, v6, &ptb);
 		}
+		if (ptb && v6 && amt_group6_is_ssm(&group.ip6))
+			icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, ptb);
 	}
 
 	dev_kfree_skb(skb);
#8 lore raw

Re: [PATCH net-next v6 05/12] amt: forward multicast data over IPv6

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] On an IPv6 outer transport, amt_send_multicast_data() (drivers/net/amt.c) silently drops an IPv4 multicast payload that…
- [Low] Drops on the new IPv6 outer data path record no drop reason, so the kfree_skb tracepoint and drop monitors (dropwatch, perf…

--

amt: forward multicast data over IPv6

This commit makes amt_send_multicast_data() send the relay's copies through amt_udp_xmit() over the outer family. Over IPv6, a payload above the tunnel MTU is dropped rather than fragmented. After walking every tunnel, amt_dev_xmit() sends one Packet Too Big for SSM groups.

Quoted 4 lines
drivers/net/amt.c +0 -0
index 1f4914a635f48..1780d3dca1014 100644

[ ... ]

Quoted 14 lines
drivers/net/amt.c +5 -1
@@ -1354,26 +1382,55 @@ static bool amt_send_membership_update(struct amt_dev *amt,
 	return false;
 }
 
+/* Send the relay's copy to one gateway. Over IPv6 a payload that does not
+ * fit the tunnel MTU is dropped and *@ptb lowered to that MTU, for
+ * amt_dev_xmit() to send one Packet Too Big after walking every tunnel.
+ */
 static void amt_send_multicast_data(struct amt_dev *amt,
 				    const struct sk_buff *oskb,
 				    struct amt_tunnel_list *tunnel,
-				    bool v6)
+				    bool v6, u32 *ptb)
 {

[ ... ]

Quoted 12 lines
drivers/net/amt.c +12 -0
+	if (amt_v6(amt)) {
+		u32 tmtu;
+
+		dst = amt_route6(amt, sk, &amt->local_ipv6, &tunnel->addr.ip6,
+				 amt->relay_port, tunnel->source_port);
+		if (IS_ERR(dst)) {
+			netdev_dbg(amt->dev, "no route to %pI6c\n",
+				   &tunnel->addr.ip6);
+			DEV_STATS_INC(amt->dev, tx_dropped);
+			return;
+		}
+

drivers/net/amt.c static bool amt_send_membership_update(struct amt_dev *amt, · line 1425

Quoted 10 lines
drivers/net/amt.c +10 -0
+		tmtu = amt_tmtu(dst);
+		if (amt_tmtu_exceeded(oskb, tmtu)) {
+			dst_release(dst);
+			/* A Packet Too Big below IPV6_MIN_MTU is ignored. */
+			if (tmtu >= IPV6_MIN_MTU && (!*ptb || tmtu < *ptb))
+				*ptb = tmtu;
+			DEV_STATS_INC(amt->dev, tx_dropped);
+			return;
+		}
+	}

[Severity: Medium]
On an IPv6 outer transport, what happens here to an IPv4 inner payload (v6 == false) that exceeds tmtu? It is dropped and only tx_dropped is incremented. *ptb may be updated, but amt_dev_xmit() only uses it for IPv6 SSM groups:

    if (ptb && v6 && amt_group6_is_ssm(&group.ip6))
        icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, ptb);

The IPv4 source therefore gets no signal at all.

The commit message justifies this with:

An IPv4 payload is dropped without an ICMP error, as icmp_send() does not answer multicast, and is not fragmented first (s5.3.3.6.2.1), as the IPv4 tunnel does not either.

Is the comparison with the IPv4 tunnel accurate? The IPv4 outer path in amt_udp_xmit() sends with df = 0 and has no tunnel MTU check:

    udp_tunnel_xmit_skb(rt, sk, skb, fl4.saddr, fl4.daddr, AMT_TOS,
                        ip4_dst_hoplimit(&rt->dst), 0, sport, dport,
                        false, false, 0);

ip_fragment() fragments an oversized packet at the outer layer, and the packet is still delivered.

An IPv4 payload sized between the tunnel MTU and the device MTU then reaches a gateway through an IPv4 relay, but is silently lost through an IPv6 relay.

For example, take a 1500 byte stream device, where the device MTU is 1440 at the end of the series, and a 1480 byte path MTU toward a gateway. tmtu is then 1430, so IPv4 payloads of 1431 to 1440 bytes are dropped for that gateway.

amt_v6() is still false at this commit. This becomes reachable with "amt: add netlink attributes for an IPv6 outer transport", and this code is unchanged at the end of the series.

s5.3.3.6.2.1 asks for the inner IPv4 payload to be fragmented before encapsulation. Should the IPv6 transport do that for IPv4 inner packets, since outer fragmentation is not available there?

[ ... ]

Quoted 12 lines
drivers/net/amt.c +0 -8
@@ -1407,17 +1454,12 @@ static void amt_send_multicast_data(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->relay_port,
-			    tunnel->source_port,

drivers/net/amt.c static void amt_send_multicast_data(struct amt_dev *amt, · line 1463

Quoted 10 lines
drivers/net/amt.c +6 -3
-			    false,
-			    false,
-			    0);
+	err = amt_udp_xmit(amt, sk, skb, &tunnel->addr, amt->relay_port,
+			   tunnel->source_port, 0, dst);
+	if (err) {
+		DEV_STATS_INC(amt->dev, tx_dropped);
+		kfree_skb_reason(skb, SKB_DROP_REASON_IP_OUTNOROUTES);
+	}
 }

[Severity: Low]
The commit message says "one with no route is freed with SKB_DROP_REASON_IP_OUTNOROUTES". Does that hold on the IPv6 path?

When amt_v6() is true, the amt_route6() failure and the amt_tmtu_exceeded() drop both return before skb_copy_expand(). No skb is freed with a reason in either case.

amt_udp_xmit() also never returns an error over IPv6 when it is given a dst. This kfree_skb_reason() can therefore only be reached when the IPv4 route lookup fails.

amt_dev_xmit() then frees the original skb with dev_kfree_skb(), which is consume_skb():

    dev_kfree_skb(skb);
    return NETDEV_TX_OK;

So even when every tunnel copy was dropped, the packet is traced as consumed. Users of the kfree_skb tracepoint, such as dropwatch or perf skb:kfree_skb, would not see these drops. That includes the MTU drops that the commit message says a forged Packet Too Big can cause.

Could these IPv6 drops record a drop reason? For example, SKB_DROP_REASON_PKT_TOO_BIG would fit the tunnel MTU case.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202043200.286260-1-omar%40blockcast.net
#9 lore raw

PATCH v6 6/12 amt: size the encapsulation headroom by the outer IP version

In reply to Omar Ramadan

AMT_RELAY_HLEN and AMT_GW_HLEN, which size the device's headroom and its MTU below the stream device's, assume a 20-byte IPv4 outer header, 20 bytes short of an IPv6 one. A payload near the device MTU would then exceed the tunnel MTU that amt_send_multicast_data() computes from the route and be dropped there.

Add AMT_RELAY_HLEN6 and AMT_GW_HLEN6, pick between them in amt_hlen(), and use amt_hlen() at newlink and on NETDEV_CHANGEMTU of the stream device, so that the stack refuses such a payload first.

amt_v6() is still false for every device, so there is no functional change.

Assisted-by LLM
---
 drivers/net/amt.c | 25 ++++++++++++++-----------
 include/net/amt.h |  6 ++++++
 2 files changed, 20 insertions(+), 11 deletions(-)
drivers/net/amt.c +14 -11
index 1780d3dca..79702b002 100644
@@ -101,6 +101,13 @@ static unsigned int amt_ip_hlen(const struct amt_dev *amt)
 	return amt_v6(amt) ? sizeof(struct ipv6hdr) : sizeof(struct iphdr);
 }
 
+static unsigned int amt_hlen(const struct amt_dev *amt)
+{
+	if (amt->mode == AMT_MODE_RELAY)
+		return amt_v6(amt) ? AMT_RELAY_HLEN6 : AMT_RELAY_HLEN;
+	return amt_v6(amt) ? AMT_GW_HLEN6 : AMT_GW_HLEN;
+}
+
 static void amt_outer_saddr(const struct amt_dev *amt,
 			    const struct sk_buff *skb, union amt_addr *addr)
 {
@@ -3904,10 +3911,10 @@ static int amt_newlink(struct net_device *dev,
 		amt->qrv = READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv);
 		amt->qri = 10;
 		dev->needed_headroom = amt->stream_dev->needed_headroom +
-				       AMT_RELAY_HLEN;
-		dev->mtu = amt->stream_dev->mtu - AMT_RELAY_HLEN;
+				       amt_hlen(amt);
+		dev->mtu = amt->stream_dev->mtu - amt_hlen(amt);
 		dev->max_mtu = dev->mtu;
-		dev->min_mtu = ETH_MIN_MTU + AMT_RELAY_HLEN;
+		dev->min_mtu = ETH_MIN_MTU + amt_hlen(amt);
 	} else {
 		if (!data[IFLA_AMT_DISCOVERY_IP]) {
 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
@@ -3930,10 +3937,10 @@ static int amt_newlink(struct net_device *dev,
 		}
 
 		dev->needed_headroom = amt->stream_dev->needed_headroom +
-				       AMT_GW_HLEN;
-		dev->mtu = amt->stream_dev->mtu - AMT_GW_HLEN;
+				       amt_hlen(amt);
+		dev->mtu = amt->stream_dev->mtu - amt_hlen(amt);
 		dev->max_mtu = dev->mtu;
-		dev->min_mtu = ETH_MIN_MTU + AMT_GW_HLEN;
+		dev->min_mtu = ETH_MIN_MTU + amt_hlen(amt);
 	}
 	amt->qi = AMT_INIT_QUERY_INTERVAL;
 
@@ -4054,11 +4061,7 @@ static int amt_device_event(struct notifier_block *unused,
 			amt_dellink(amt->dev, &list);
 			break;
 		case NETDEV_CHANGEMTU:
-			if (amt->mode == AMT_MODE_RELAY)
-				new_mtu = dev->mtu - AMT_RELAY_HLEN;
-			else
-				new_mtu = dev->mtu - AMT_GW_HLEN;
-
+			new_mtu = dev->mtu - amt_hlen(amt);
 			dev_set_mtu(amt->dev, new_mtu);
 			break;
 		}
include/net/amt.h +6 -0
index 82ab63876..726026259 100644
@@ -429,9 +429,15 @@ struct amt_dev {
 #define AMT_GW_HLEN (sizeof(struct iphdr) + \
 		     sizeof(struct udphdr) + \
 		     sizeof(struct amt_gw_headers))
+#define AMT_GW_HLEN6 (sizeof(struct ipv6hdr) + \
+		      sizeof(struct udphdr) + \
+		      sizeof(struct amt_gw_headers))
 #define AMT_RELAY_HLEN (sizeof(struct iphdr) + \
 		     sizeof(struct udphdr) + \
 		     sizeof(struct amt_relay_headers))
+#define AMT_RELAY_HLEN6 (sizeof(struct ipv6hdr) + \
+			 sizeof(struct udphdr) + \
+			 sizeof(struct amt_relay_headers))
 
 static inline bool netif_is_amt(const struct net_device *dev)
 {
#10 lore raw

PATCH v6 7/12 amt: send the AMT gateway control plane over IPv6

In reply to Omar Ramadan

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
---
 drivers/net/amt.c | 79 +++++++++++++++++++++++++++++------------------
 include/net/amt.h |  4 +++
 2 files changed, 53 insertions(+), 30 deletions(-)
drivers/net/amt.c +49 -30
index 79702b002..40481b0f9 100644
@@ -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;
 }
include/net/amt.h +4 -0
index 726026259..967acaf38 100644
@@ -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 */
#11 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The new gateway IPv6 control path never counts transmit failures that happen after routing.

--

amt: send the AMT gateway control plane over IPv6

This commit lets an AMT gateway send its Discovery, Request and Membership Update messages with an IPv6 outer header. Discovery and Request are built on the stack and sent via amt_send_ctrl_v6(). The Membership Update goes through amt_udp_xmit().

Quoted 4 lines
drivers/net/amt.c +0 -0
index 79702b002d14f..40481b0f9d5ef 100644

[ ... ]

Quoted 24 lines
drivers/net/amt.c +16 -1
@@ -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)

drivers/net/amt.c static int amt_send_ctrl_v6(struct amt_dev *amt, const struct in6_addr *saddr, · line 856

Quoted 10 lines
drivers/net/amt.c +10 -0
+{
+	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));
+}

[Severity: Medium]
Does the IPv6 Discovery and Request path stop counting tx_errors on amt->dev when a send fails after routing?

amt_send_ctrl_v6() passes a NULL net_device to udp_tunnel6_xmit_skb() and then sets err = 0 unconditionally:

	udp_tunnel6_xmit_skb(dst, sk, skb, NULL, saddr, daddr, AMT_TOS,
			     ip6_dst_hoplimit(dst), 0, sport, dport, false, 0);
	err = 0;

In ip6tunnel_xmit(), all of the error accounting sits behind a dev check. That covers both the recursion-limit drop and the ip6_local_out() failure:

include/net/ip6_tunnel.h:ip6tunnel_xmit() {
    ...
	if (unlikely(dev_recursion_level() > IP_TUNNEL_RECURSION_LIMIT)) {
		if (dev) {
			...
			DEV_STATS_INC(dev, tx_errors);
		}
    ...
	err = ip6_local_out(skb_dst_dev_net(skb), sk, skb);

	if (dev) {
		if (unlikely(net_xmit_eval(err)))
			pkt_len = -1;
		iptunnel_xmit_stats(dev, pkt_len);
	}
    ...
}

So a Discovery or Request that netfilter drops, that fails in the neighbour or xmit path, or that hits the recursion limit will not be counted in amt->dev tx_errors.

The IPv4 code paths that these functions replace, amt_send_discovery() and amt_send_request(), do count these failures:

	err = ip_local_out(amt->net, sk, skb);
	if (unlikely(net_xmit_eval(err)))
		DEV_STATS_INC(amt->dev, tx_errors);

The IPv6 Membership Update path in amt_udp_xmit() passes amt->dev to the same udp_tunnel6_xmit_skb(). On one device, IPv6 Update failures are counted but IPv6 Discovery/Request failures are not.

amt_v6() is always false at this commit. Later in the series, "amt: add netlink attributes for an IPv6 outer transport" sets local_ipv6 from IFLA_AMT_LOCAL_IP6, which makes this path reachable. The final state of the series still passes NULL here.

Passing amt->dev directly would also add tx_packets and tx_bytes for control messages, which the IPv4 control path does not count. Would it make sense to count tx_errors in amt_send_ctrl_v6() itself, so that it matches the IPv4 behaviour?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202043200.286260-1-omar%40blockcast.net
#12 lore raw

PATCH v6 8/12 amt: receive the AMT gateway control plane over IPv6

In reply to Omar Ramadan

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
---
 drivers/net/amt.c  | 161 ++++++++++++++++++++++++++++++++++++++-------
 include/net/amt.h  |   2 +
 net/ipv6/anycast.c |   1 +
 3 files changed, 140 insertions(+), 24 deletions(-)
drivers/net/amt.c +137 -24
index 40481b0f9..c2126c7ba 100644
@@ -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;
include/net/amt.h +2 -0
index 967acaf38..823623b99 100644
@@ -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) */
net/ipv6/anycast.c +1 -0
index be6dac8a8..4adc6f316 100644
@@ -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
#13 lore raw

Re: [PATCH net-next v6 08/12] amt: receive the AMT gateway control plane over IPv6

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] This patch changes the transmit path in a way the commit message never mentions.

--

amt: receive the AMT gateway control plane over IPv6

This patch lets amt_rcv() and amt_advertisement_handler() handle the replies a gateway gets over an IPv6 outer transport. The learned remote_ipv6 is now published under a new seqlock, and a relay drops packets whose IPv6 source is one of the host's own addresses. The UDP zero-checksum policy for an IPv6 gateway is also adjusted.

drivers/net/amt.c static struct dst_entry *amt_route6(struct amt_dev *amt, struct sock *sk, · line 773

Quoted 12 lines
drivers/net/amt.c +4 -0
index 40481b0f9d5ef..c2126c7ba625a 100644
@@ -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);
+

[Severity: Low]
This isn't a bug, but should the commit message mention this transmit side change?

amt_route6() is shared by every IPv6 sender: amt_send_ctrl_v6(), amt_udp_xmit() and amt_send_multicast_data(). With this check, each of them now gets -EINVAL instead of a route lookup when the destination is ::. amt_send_ctrl_v6() also counts it in tx_errors:

	dst = amt_route6(amt, sk, saddr, daddr, sport, dport);
	if (IS_ERR(dst)) {
		DEV_STATS_INC(amt->dev, tx_errors);
		err = PTR_ERR(dst);

The check follows from this patch's own amt_clear_remote(), which writes in6addr_any into remote_ipv6. amt_send_request_v6() and amt_send_membership_update() then read that value back through amt_get_remote_ipv6().

The commit message describes the other hunks in detail, and the subject says the patch is about receiving. It never says that sends to :: are now refused or that they show up in tx_errors.

Could this be described in the commit message? Alternatively, it could go into the earlier "amt: send the AMT gateway control plane over IPv6" patch, which added the IPv6 senders.

Quoted 3 lines
drivers/net/amt.c +0 -0
 	memset(&fl6, 0, sizeof(fl6));
 	fl6.flowi6_oif		= amt->stream_dev->ifindex;
 	fl6.flowi6_proto	= IPPROTO_UDP;

[ ... ]

Quoted 5 lines
drivers/net/amt.c +5 -0
+static void amt_clear_remote(struct amt_dev *amt)
+{
+	WRITE_ONCE(amt->remote_ip, 0);
+	amt_set_remote_ipv6(amt, &in6addr_any);
+}

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202043200.286260-1-omar%40blockcast.net
#14 lore raw

PATCH v6 9/12 amt: add netlink attributes for an IPv6 outer transport

In reply to Omar Ramadan

Every path of the relay and the gateway can now run over an IPv6 outer transport, but nothing can create such a device. Add the attributes that do:

- IFLA_AMT_LOCAL_IP6, the local IPv6 address. It selects IPv6 as the outer transport and excludes IFLA_AMT_LOCAL_IP.
- IFLA_AMT_DISCOVERY_IP6, the address a gateway sends its Relay Discovery to, in the family of its local address, as both ends use one outer transport.
- IFLA_AMT_REMOTE_IP6, the relay address the gateway learned. Like IFLA_AMT_REMOTE_IP it is only reported; the policy rejects it on input.

amt_fill_info() reports the addresses of the device's outer family. IFLA_AMT_LOCAL_IP6 is refused with -EAFNOSUPPORT without CONFIG_IPV6, rather than creating an IPv4 device with no local address. An IPv6 local or discovery address is refused when unspecified, loopback, multicast or IPv4-mapped, the last because the V6ONLY socket could never send from or to it.

IFLA_AMT_LOCAL_IP and IFLA_AMT_DISCOVERY_IP cannot carry 16 bytes: net requires exactly four ("amt: require an exact IPv4-sized IFLA_AMT_{LOCAL,DISCOVERY}_IP"), so the IPv6 addresses get attributes of their own, as in vxlan and geneve. A kernel that does not know them ignores them and fails for lack of a local address, so userspace can tell. They are appended to the enum, so existing values do not change; strict_start_type makes the policy validate them and later attributes strictly, with an exact 16-byte length.

A relay has always ignored IFLA_AMT_DISCOVERY_IP, so an IPv4 relay still accepts it. An IPv6 relay has no existing users and rejects a discovery address of either family, and an IPv4 relay rejects IFLA_AMT_DISCOVERY_IP6.

Assisted-by LLM
---
 drivers/net/amt.c        | 157 ++++++++++++++++++++++++++++++---------
 include/uapi/linux/amt.h |  13 ++++
 2 files changed, 134 insertions(+), 36 deletions(-)
drivers/net/amt.c +121 -36
index c2126c7ba..14c544d80 100644
@@ -3925,6 +3925,7 @@ static void amt_link_setup(struct net_device *dev)
 }
 
 static const struct nla_policy amt_policy[IFLA_AMT_MAX + 1] = {
+	[IFLA_AMT_UNSPEC]	= { .strict_start_type = IFLA_AMT_LOCAL_IP6 },
 	[IFLA_AMT_MODE]		= { .type = NLA_U32 },
 	[IFLA_AMT_RELAY_PORT]	= { .type = NLA_U16 },
 	[IFLA_AMT_GATEWAY_PORT]	= { .type = NLA_U16 },
@@ -3933,6 +3934,10 @@ static const struct nla_policy amt_policy[IFLA_AMT_MAX + 1] = {
 	[IFLA_AMT_REMOTE_IP]	= { .len = sizeof_field(struct iphdr, daddr) },
 	[IFLA_AMT_DISCOVERY_IP]	= NLA_POLICY_EXACT_LEN(sizeof(__be32)),
 	[IFLA_AMT_MAX_TUNNELS]	= { .type = NLA_U32 },
+	[IFLA_AMT_LOCAL_IP6]	= NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
+	[IFLA_AMT_DISCOVERY_IP6] =
+		NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
+	[IFLA_AMT_REMOTE_IP6]	= { .type = NLA_REJECT },
 };
 
 static int amt_validate(struct nlattr *tb[], struct nlattr *data[],
@@ -3959,16 +3964,49 @@ static int amt_validate(struct nlattr *tb[], struct nlattr *data[],
 		return -EINVAL;
 	}
 
-	if (!data[IFLA_AMT_LOCAL_IP]) {
-		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_DISCOVERY_IP],
-				    "Local attribute is required");
+	if (!data[IFLA_AMT_LOCAL_IP] && !data[IFLA_AMT_LOCAL_IP6]) {
+		NL_SET_ERR_MSG_MOD(extack, "Local attribute is required");
 		return -EINVAL;
 	}
 
-	if (!data[IFLA_AMT_DISCOVERY_IP] &&
-	    nla_get_u32(data[IFLA_AMT_MODE]) == AMT_MODE_GATEWAY) {
-		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LOCAL_IP],
-				    "Discovery attribute is required");
+	if (data[IFLA_AMT_LOCAL_IP] && data[IFLA_AMT_LOCAL_IP6]) {
+		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LOCAL_IP6],
+				    "Local IPv4 and IPv6 are mutually exclusive");
+		return -EINVAL;
+	}
+
+	if (data[IFLA_AMT_LOCAL_IP6] && !IS_ENABLED(CONFIG_IPV6)) {
+		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LOCAL_IP6],
+				    "IPv6 support is disabled");
+		return -EAFNOSUPPORT;
+	}
+
+	if (nla_get_u32(data[IFLA_AMT_MODE]) != AMT_MODE_GATEWAY) {
+		struct nlattr *disc = data[IFLA_AMT_DISCOVERY_IP6];
+
+		/* An IPv4 relay has always ignored IFLA_AMT_DISCOVERY_IP; an
+		 * IPv6 one has no such users and rejects both families.
+		 */
+		if (!disc && data[IFLA_AMT_LOCAL_IP6])
+			disc = data[IFLA_AMT_DISCOVERY_IP];
+		if (disc) {
+			NL_SET_ERR_MSG_ATTR(extack, disc,
+					    "Discovery is only valid in gateway mode");
+			return -EINVAL;
+		}
+		return 0;
+	}
+
+	if (data[IFLA_AMT_LOCAL_IP6] ? !data[IFLA_AMT_DISCOVERY_IP6] :
+				       !data[IFLA_AMT_DISCOVERY_IP]) {
+		NL_SET_ERR_MSG_MOD(extack,
+				   "Discovery attribute of the local family is required");
+		return -EINVAL;
+	}
+
+	if (data[IFLA_AMT_DISCOVERY_IP] && data[IFLA_AMT_DISCOVERY_IP6]) {
+		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_DISCOVERY_IP6],
+				    "Discovery IPv4 and IPv6 are mutually exclusive");
 		return -EINVAL;
 	}
 
@@ -4019,13 +4057,25 @@ static int amt_newlink(struct net_device *dev,
 		goto err;
 	}
 
-	amt->local_ip = nla_get_in_addr(data[IFLA_AMT_LOCAL_IP]);
-	if (ipv4_is_loopback(amt->local_ip) ||
-	    ipv4_is_zeronet(amt->local_ip) ||
-	    ipv4_is_multicast(amt->local_ip)) {
-		NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LOCAL_IP],
-				    "Invalid Local address");
-		goto err;
+	if (data[IFLA_AMT_LOCAL_IP6]) {
+		amt->local_ipv6 = nla_get_in6_addr(data[IFLA_AMT_LOCAL_IP6]);
+		if (ipv6_addr_loopback(&amt->local_ipv6) ||
+		    ipv6_addr_any(&amt->local_ipv6) ||
+		    ipv6_addr_is_multicast(&amt->local_ipv6) ||
+		    ipv6_addr_v4mapped(&amt->local_ipv6)) {
+			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LOCAL_IP6],
+					    "Invalid Local IPv6 address");
+			goto err;
+		}
+	} else {
+		amt->local_ip = nla_get_in_addr(data[IFLA_AMT_LOCAL_IP]);
+		if (ipv4_is_loopback(amt->local_ip) ||
+		    ipv4_is_zeronet(amt->local_ip) ||
+		    ipv4_is_multicast(amt->local_ip)) {
+			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LOCAL_IP],
+					    "Invalid Local address");
+			goto err;
+		}
 	}
 
 	amt->relay_port = nla_get_be16_default(data[IFLA_AMT_RELAY_PORT],
@@ -4048,24 +4098,35 @@ static int amt_newlink(struct net_device *dev,
 		dev->max_mtu = dev->mtu;
 		dev->min_mtu = ETH_MIN_MTU + amt_hlen(amt);
 	} else {
-		if (!data[IFLA_AMT_DISCOVERY_IP]) {
-			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
-					    "discovery must be set in gateway mode");
-			goto err;
-		}
 		if (!amt->gw_port) {
 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
 					    "gateway port must not be 0");
 			goto err;
 		}
-		WRITE_ONCE(amt->remote_ip, 0);
-		amt->discovery_ip = nla_get_in_addr(data[IFLA_AMT_DISCOVERY_IP]);
-		if (ipv4_is_loopback(amt->discovery_ip) ||
-		    ipv4_is_zeronet(amt->discovery_ip) ||
-		    ipv4_is_multicast(amt->discovery_ip)) {
-			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
-					    "discovery must be unicast");
-			goto err;
+		if (data[IFLA_AMT_DISCOVERY_IP6]) {
+			struct nlattr *attr = data[IFLA_AMT_DISCOVERY_IP6];
+
+			amt->discovery_ipv6 = nla_get_in6_addr(attr);
+			if (ipv6_addr_loopback(&amt->discovery_ipv6) ||
+			    ipv6_addr_any(&amt->discovery_ipv6) ||
+			    ipv6_addr_is_multicast(&amt->discovery_ipv6) ||
+			    ipv6_addr_v4mapped(&amt->discovery_ipv6)) {
+				NL_SET_ERR_MSG_ATTR(extack, attr,
+						    "discovery must be unicast");
+				goto err;
+			}
+		} else {
+			WRITE_ONCE(amt->remote_ip, 0);
+			amt->discovery_ip =
+				nla_get_in_addr(data[IFLA_AMT_DISCOVERY_IP]);
+			if (ipv4_is_loopback(amt->discovery_ip) ||
+			    ipv4_is_zeronet(amt->discovery_ip) ||
+			    ipv4_is_multicast(amt->discovery_ip)) {
+				NL_SET_ERR_MSG_ATTR(extack,
+						    tb[IFLA_AMT_DISCOVERY_IP],
+						    "discovery must be unicast");
+				goto err;
+			}
 		}
 
 		dev->needed_headroom = amt->stream_dev->needed_headroom +
@@ -4119,7 +4180,25 @@ static size_t amt_get_size(const struct net_device *dev)
 	       nla_total_size(sizeof(__u32)) + /* IFLA_MAX_TUNNELS */
 	       nla_total_size(sizeof(__be32)) + /* IFLA_AMT_DISCOVERY_IP */
 	       nla_total_size(sizeof(__be32)) + /* IFLA_AMT_REMOTE_IP */
-	       nla_total_size(sizeof(__be32)); /* IFLA_AMT_LOCAL_IP */
+	       nla_total_size(sizeof(__be32)) + /* IFLA_AMT_LOCAL_IP */
+	       /* {LOCAL,DISCOVERY,REMOTE}_IP6: sized for both families. */
+	       3 * nla_total_size(sizeof(struct in6_addr));
+}
+
+static int amt_fill_addr6(struct sk_buff *skb, const struct amt_dev *amt)
+{
+	struct in6_addr remote6;
+
+	if (nla_put_in6_addr(skb, IFLA_AMT_LOCAL_IP6, &amt->local_ipv6))
+		return -EMSGSIZE;
+	if (amt->mode == AMT_MODE_GATEWAY &&
+	    nla_put_in6_addr(skb, IFLA_AMT_DISCOVERY_IP6, &amt->discovery_ipv6))
+		return -EMSGSIZE;
+	amt_get_remote_ipv6(amt, &remote6);
+	if (!ipv6_addr_any(&remote6) &&
+	    nla_put_in6_addr(skb, IFLA_AMT_REMOTE_IP6, &remote6))
+		return -EMSGSIZE;
+	return 0;
 }
 
 static int amt_fill_info(struct sk_buff *skb, const struct net_device *dev)
@@ -4136,15 +4215,21 @@ static int amt_fill_info(struct sk_buff *skb, const struct net_device *dev)
 		goto nla_put_failure;
 	if (nla_put_u32(skb, IFLA_AMT_LINK, amt->stream_dev->ifindex))
 		goto nla_put_failure;
-	if (nla_put_in_addr(skb, IFLA_AMT_LOCAL_IP, amt->local_ip))
-		goto nla_put_failure;
-	if (nla_put_in_addr(skb, IFLA_AMT_DISCOVERY_IP, amt->discovery_ip))
-		goto nla_put_failure;
-
-	remote_ip = READ_ONCE(amt->remote_ip);
-	if (remote_ip)
-		if (nla_put_in_addr(skb, IFLA_AMT_REMOTE_IP, remote_ip))
+	if (amt_v6(amt)) {
+		if (amt_fill_addr6(skb, amt))
 			goto nla_put_failure;
+	} else {
+		if (nla_put_in_addr(skb, IFLA_AMT_LOCAL_IP, amt->local_ip))
+			goto nla_put_failure;
+		if (nla_put_in_addr(skb, IFLA_AMT_DISCOVERY_IP,
+				    amt->discovery_ip))
+			goto nla_put_failure;
+
+		remote_ip = READ_ONCE(amt->remote_ip);
+		if (remote_ip)
+			if (nla_put_in_addr(skb, IFLA_AMT_REMOTE_IP, remote_ip))
+				goto nla_put_failure;
+	}
 	if (nla_put_u32(skb, IFLA_AMT_MAX_TUNNELS, amt->max_tunnels))
 		goto nla_put_failure;
 
include/uapi/linux/amt.h +13 -0
index 2dccff417..5290ece75 100644
@@ -54,6 +54,19 @@ enum {
 	IFLA_AMT_DISCOVERY_IP,
 	/* This attribute specify number of maximum tunnel. */
 	IFLA_AMT_MAX_TUNNELS,
+	/* This attribute specifies the local IPv6 address. It selects IPv6
+	 * as the outer transport and excludes IFLA_AMT_LOCAL_IP.
+	 */
+	IFLA_AMT_LOCAL_IP6,
+	/* This attribute specifies the IPv6 address of the relay a gateway
+	 * sends its Discovery to. It is the IPv6 form of
+	 * IFLA_AMT_DISCOVERY_IP and needs IFLA_AMT_LOCAL_IP6.
+	 */
+	IFLA_AMT_DISCOVERY_IP6,
+	/* This attribute reports the IPv6 relay address a gateway learned
+	 * from the Relay Advertisement. It is read-only.
+	 */
+	IFLA_AMT_REMOTE_IP6,
 	__IFLA_AMT_MAX,
 };
 
#15 lore raw

PATCH v6 10/12 MAINTAINERS: amt: cover the amt headers and selftests

In reply to Omar Ramadan

The AMT entry lists only drivers/net/amt.c, so get_maintainer.pl finds no AMT maintainer for include/net/amt.h, include/uapi/linux/amt.h or tools/testing/selftests/net/amt.sh. Add the headers, and a glob for amt.sh and the new amt_*.sh scripts.

Assisted-by LLM
---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)
MAINTAINERS +3 -0
index 05b2d9e36..067271943 100644
@@ -1506,6 +1506,9 @@ S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git
 F:	drivers/net/amt.c
+F:	include/net/amt.h
+F:	include/uapi/linux/amt.h
+F:	tools/testing/selftests/net/amt*.sh
 
 ANALOG DEVICES INC AD3530R DRIVER
 M:	Kim Seer Paller <[email protected]>
#16 lore raw

PATCH v6 11/12 selftests: net: add amt_v6.sh for an IPv6 outer transport

In reply to Omar Ramadan

Nothing covers the IPv6 outer transport. amt_v6.sh is amt.sh with the gateway<->relay link moved to IPv6, and a second gateway on it:

amtg: mode gateway local 2001:db8:a::1 discovery 2001:db8:a::3
amtg2: mode gateway local 2001:db8:a::4 discovery 2001:db8:a::3
amtr: mode relay local 2001:db8:a::2

The namespaces, inner addresses, smcroute routes and tools are those of amt.sh, plus tcpdump for the Discovery checks, which skip without it. The forwarding checks cover the whole handshake, as the relay forwards only after the gateway's Update. It checks that:

- amtg learns 2001:db8:a::2 from an Advertisement requested through the relay's second address, so the Advertisement comes from the address the Discovery was sent to;
- IPv4 and IPv6 multicast reach the listener through the tunnel, and IPv4 multicast reaches a listener on the second gateway, which shares the first one's /64 and port;
- with max_tunnels 16 the relay allows two tunnels per /64 source: a third gateway in it gets no tunnel and no multicast, one in another /64 does;
- the relay's General Queries do not leave through amtr;
- with the route MTU to the gateway lowered, an IPv6 payload above the tunnel MTU is dropped and counted in tx_dropped, neither fragmented nor sent to IPv6 output, where it would count in Ip6FragFails;
- its source is sent a Packet Too Big if the group is SSM, ff3e::5:6, and none if it is ASM, ff0e::5:6 (RFC 7450 s5.3.3.6.2.2);
- a Discovery to ff02::1 draws no reply, while one to the discovery address, the positive control, does;
- a Discovery from the wire with the relay's own address, unicast or the subnet-router anycast one, as its source is dropped, counted in rx_dropped and not answered on lo, while the same Discovery over lo, as from a gateway on the relay's host, is answered;
- taking amtg down clears the learned relay address, and bringing it up discovers the relay again.

A probe creates a throwaway IPv6 relay first and skips when iproute2 does not know amt, when the kernel ignores IFLA_AMT_LOCAL_IP6 ("Local attribute is required") or has no amt link type ("Unknown device type"), and when iproute2 cannot pass the address: it rejects the literal ("expected rather than"), or packs it into the 4-byte IFLA_AMT_LOCAL_IP, which this kernel refuses with "binary attribute size out of range" and an older one creates as an IPv4 relay, read back as an address without a colon. Any other failure is a FAIL. The script skips without CONFIG_IPV6 before it creates a link.

A listener that must get nothing has to be alive at the end, so one that never started cannot pass. disc_answer() and local_src_answers() fail with a reason, and tcpdump's own text, when tcpdump does not start or ends early, or a send fails, so a harness failure is not read as "no reply". gw_l is up before it joins the gateway's bridge, and amtg comes up only once its bridge port forwards, so that the relay's first Query reaches the listener. br_gw's MAC is set, as a bridge takes its lowest port's and a port added later would leave the gateway a stale neighbour entry for the relay.

The iproute2 side is posted to iproute2-next separately.

Assisted-by LLM
---
 tools/testing/selftests/net/Makefile  |   1 +
 tools/testing/selftests/net/amt_v6.sh | 716 ++++++++++++++++++++++++++
 2 files changed, 717 insertions(+)
 create mode 100755 tools/testing/selftests/net/amt_v6.sh
tools/testing/selftests/net/Makefile +1 -0
index fb0a64662..a31f19e9f 100644
@@ -9,6 +9,7 @@ CFLAGS += -I../
 TEST_PROGS := \
 	altnames.sh \
 	amt.sh \
+	amt_v6.sh \
 	arp_ndisc_evict_nocarrier.sh \
 	arp_ndisc_untracked_subnets.sh \
 	bareudp.sh \
tools/testing/selftests/net/amt_v6.sh new file +716 -0

716 lines not on this page · raw message

#17 lore raw

Re: [PATCH net-next v6 11/12] selftests: net: add amt_v6.sh for an IPv6 outer transport

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] In tools/testing/selftests/net/amt_v6.sh, the header comment of test_tmtu() reads "With the route MTU to the gateway below the…
- [Low] In tools/testing/selftests/net/amt_v6.sh, test_forward() starts the listener with ip netns exec "$ns" timeout 20 socat -u ..., so it…

--

selftests: net: add amt_v6.sh for an IPv6 outer transport

This adds amt_v6.sh, a variant of amt.sh in which the gateway<->relay link runs over IPv6, with a second gateway on that link. It covers discovery, IPv4/IPv6 multicast forwarding, the per-/64 tunnel cap and tunnel MTU drops. It also covers Packet Too Big for SSM groups, multicast and local-source Discovery handling, and gateway down/up.

Quoted 6 lines
tools/testing/selftests/net/amt_v6.sh new file +0 -0
new file mode 100755
index 0000000000000..c5447ffeaa4f7
@@ -0,0 +1,716 @@

[ ... ]

Quoted 8 lines
tools/testing/selftests/net/amt_v6.sh new file +8 -0
+test_forward()
+{
+	local ns=$1 addr=$2 port=$3 grp=$4 src=$5 desc=$6 want=${7:-data}
+	local out pid i rc
+
+	RET=0
+	out=$(mktemp)
+	ip netns exec "$ns" timeout 20 \

tools/testing/selftests/net/amt_v6.sh line 235

Quoted 10 lines
tools/testing/selftests/net/amt_v6.sh new file +10 -0
+		socat -u "$addr,readbytes=128" - > "$out" &
+	pid=$!
+	wait_local_port_listen "$ns" "$port" udp
+	# Paced to outlast a Request/Query refresh of the gateway.
+	for i in $(seq 40); do
+		ip netns exec "$SOURCE" bash -c \
+			"printf '%s %128s' $src | nc -w 1 -u $grp $port"
+		[ "$want" = data ] && grep -q "$src" "$out" && break
+		sleep 0.25
+	done

[Severity: Low]
Can this loop keep sending well after the listener is gone?

The listener is limited to 20 seconds by timeout 20. The loop only exits early in the data case once the source shows up in $out. Nothing checks whether the listener is still running, for example with kill -0 "$pid".

With an OpenBSD or traditional netcat, nc -w 1 waits up to a second after stdin EOF. Each iteration then takes about 1.25s, so the 40 iterations take about 50s.

In the "none" case ("a third gateway in a /64 at its cap gets no tunnel") and in any failing data case, the loop always runs all 40 iterations. That means roughly 30s of datagrams go out after the listener has exited, and nothing observes them.

So the window in which the refused gateway is shown to get nothing is the listener's 20s, not the longer sending window that the "Paced to outlast" comment suggests. Each such run also adds about 30s.

Would it make sense to break out of the loop once the listener exits? The other option is to match the listener timeout to the length of the loop.

Quoted 2 lines
tools/testing/selftests/net/amt_v6.sh new file +2 -0
+	wait "$pid"
+	rc=$?

[ ... ]

Quoted 11 lines
tools/testing/selftests/net/amt_v6.sh new file +11 -0
+# With the route MTU to the gateway below the payload, the relay must drop the
+# payload without fragmenting it, and send its source a Packet Too Big only
+# for an SSM group (RFC 7450 s5.3.3.6.2.2).
+# test_tmtu <group> <ptb|none> <description>
+test_tmtu()
+{
+	local grp=$1 want=$2 desc=$3
+	local ptb frag fragfail drop i
+
+	RET=0
+	ip -n "$RELAY" -6 route add "$GW6/128" dev br_gw mtu 1400

[ ... ]

tools/testing/selftests/net/amt_v6.sh line 276

Quoted 4 lines
tools/testing/selftests/net/amt_v6.sh new file +4 -0
+	for i in 1 2 3; do
+		ip netns exec "$SOURCE" bash -c \
+			"printf '%1352s' x | nc -w 1 -u $grp 6000"
+	done

[Severity: Low]
Does the header comment of test_tmtu() describe the right condition?

It says "With the route MTU to the gateway below the payload". The route MTU is 1400, and the inner IPv6 packet is 40 + 8 + 1352 = 1400 bytes. The route MTU is therefore equal to the payload, not below it.

The drop happens because the payload is above the tunnel MTU. In drivers/net/amt.c:

drivers/net/amt.c:amt_tmtu() {
	u32 hlen = sizeof(struct ipv6hdr) + sizeof(struct udphdr) +
		   sizeof(struct amt_header_mcast_data);
	u32 mtu = dst_mtu(dst);

	return mtu > hlen ? mtu - hlen : 0;
}

That gives 1400 - 50 = 1350. amt_send_multicast_data() then drops the 1400-byte skb through amt_tmtu_exceeded().

The kernel comment above amt_tmtu(), the test descriptions and the commit message all describe the threshold as the tunnel MTU. Should this comment say "payload above the tunnel MTU" as well?

Someone who sets the route MTU from this comment, for example to payload + 1, would no longer exercise the drop path.

Quoted 3 lines
tools/testing/selftests/net/amt_v6.sh new file +3 -0
+	# The drops show that the payload reached the tunnel, so that "none" is
+	# not a send that never ran. -gt: the first Packet Too Big caches a path
+	# MTU in the source, so later datagrams may be fragmented, not dropped.

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202043200.286260-1-omar%40blockcast.net
#18 lore raw

PATCH v6 12/12 selftests: net: add amt_gw_v6.sh for the IPv6 netlink attributes

In reply to Omar Ramadan

amt_v6.sh checks the data plane with valid attributes only. The link-creation rules for IFLA_AMT_LOCAL_IP6 and IFLA_AMT_DISCOVERY_IP6 are not exercised.

This adds a companion without a data plane: one netns and one dummy device. It checks that the IPv6 local and discovery addresses of a gateway, and the local address of a relay, read back through amt_fill_info(), that a relay reports no discovery address, that an IPv4 gateway still creates and reports its IPv4 discovery address, and that an IPv4 relay still accepts the IPv4 discovery address it has always ignored. It then checks each rejection: a gateway without a discovery address, a gateway whose local and discovery addresses differ in family, an IPv6 relay given a discovery address of either family, an IPv4 relay given an IPv6 one, and an unspecified, loopback, multicast or IPv4-mapped IPv6 local or discovery address. Each is matched on its extack message, so a rule that fails for the wrong reason is not a pass. The unspecified and multicast cases need iproute2 to pass those addresses on as given, which it does: ip/iplink_amt.c tests is_addrtype_inet(), true for "::" and multicast, and the IPv6 branch of the iproute2 patch adds its attribute without a test.

Not covered, as iproute2 cannot express them: both discovery <v4> and discovery <v6>, or both local forms, in one command, and a 16-byte IFLA_AMT_LOCAL_IP or IFLA_AMT_DISCOVERY_IP.

The probe skips on an iproute2 without IPv6 AMT support: one that rejects the literal or does not know amt, or that packs the 16-byte literal into the IPv4 attribute. This kernel refuses that with "binary attribute size out of range", logged as a passing test before the skip; a kernel without that rule truncates it to an IPv4 address (discovery 2001:db8:a::2 reads back as 32.1.13.184), and the probe skips on that readback only. It also skips on a kernel that ignores the attributes ("Local attribute is required"), has no amt ("Unknown device type") or no IPv6. Any other failure to create the probe link, and any other readback without a colon, is a FAIL.

Assisted-by LLM
---
 tools/testing/selftests/net/Makefile     |   1 +
 tools/testing/selftests/net/amt_gw_v6.sh | 205 +++++++++++++++++++++++
 2 files changed, 206 insertions(+)
 create mode 100755 tools/testing/selftests/net/amt_gw_v6.sh
tools/testing/selftests/net/Makefile +1 -0
index a31f19e9f..bd6c08d11 100644
@@ -9,6 +9,7 @@ CFLAGS += -I../
 TEST_PROGS := \
 	altnames.sh \
 	amt.sh \
+	amt_gw_v6.sh \
 	amt_v6.sh \
 	arp_ndisc_evict_nocarrier.sh \
 	arp_ndisc_untracked_subnets.sh \
tools/testing/selftests/net/amt_gw_v6.sh new file +205 -0

205 lines not on this page · raw message