From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796185929; b=rE+A14NGfX98keTUwfRHaLDVnbsMo/Se0iPEtu2DPgBlsMRcMf8aA7DhhB6F0c7OynsS WQSIrCOZOY7uPIKtcaBdsp6ehWjR4Iu/Otxr53PGsfJPU5itq5GnVEJZgyaLAdjA4/Aee kmgzvWcW4zCyJlDw32cTDJqFVRqJDx9Xeyatms6OwDDLpl1qep7Xyse9LAZtpDTmo0p18 V7wA6+sUphMKGWu+iopM/cMitGGtGR2yvObzE0Xe0RLd4hDsQJfIa9myChSRHn4SQh5ux yG2Nz/uj9YXlvKlWtQAw7B2+yyz4wHx0EiG56ESDD4fbakcJ/dCLIOt7uAdyVArwSXA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796185929; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=pPaRdrmUZa+vFPOCFiQ8n4zL80cnOC0w3sT1VpJj704=; b=IvMY6fu0LCVza9bNqT8uECiSG8U1h2TdoKtpBDd/+VtSyDUYck8sTW1aaHnPkWirZAj/ 8SFiw69tRm1NBEzzaWLaOUGX2llPrHjEQei2Hx8MNeyGGhwfoWEHMyAtMrb7L9+dWnKQi 1YyktrJ/8dQ3wFzbWs543Sn00AWns/dSY/LzTS3P3eraDJGwW7x8t5/lN5ATfKLQjOq5J rjWl/O5uePHwSY0ffSnAp5U3rrV7s2NA2AqqnYHvNQ72sKyh+ho5+ZHoQF1nRvjewC6qR tN9j00LXL4+7njMFkPWKaURkEKrMXPZ8F7u+rDbCLE4l4Jk5L1BwxP5OJsWRvtJsRxQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=blockcast.net; spf=pass smtp.mailfrom=blockcast.net; dkim=pass header.d=blockcast.net header.i=@blockcast.net header.a=rsa-sha256 header.s=google header.b=dRLHaWIB; arc=none smtp.remote-ip=74.125.82.182 Received: from mail-dy1-f182.google.com (mail-dy1-f182.google.com [74.125.82.182]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A179F606A684 for ; Wed, 2 Dec 2026 04:32:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A179F606A684 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=blockcast.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=blockcast.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=blockcast.net header.i=@blockcast.net header.a=rsa-sha256 header.s=google header.b=dRLHaWIB Received: by mail-dy1-f182.google.com with SMTP id 5a478bee46e88-386bd881d39so7161784eec.1 for ; Tue, 01 Dec 2026 20:32:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blockcast.net; s=google; t=1796185928; x=1796790728; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=pPaRdrmUZa+vFPOCFiQ8n4zL80cnOC0w3sT1VpJj704=; b=dRLHaWIBM5Ww9unOlcAfWaWX8mPIEDcwFhVsjPmwwEuq/+TtzLxKG4loJRah/74DFV 7Kqy3YMZ5l/WYW+2y9EKOnRiwJOQJrAbFsRQUUxbLkaUTSow1oraUUeDPbNO7Ima+HA4 UoGQ3GDHvc0CnSfYKNIbQVQzcdS4F5ni0wy0/Y/BsEREmdSqUYriHsTlLuv+ay6DQ+me jvQj1eecxwgixn+4OHQVYaoSv9s/bKPDBUr1f/BAjPdJQvyXnJCBzYABfrdF3wISIb6G nhcF0U3PN5EVA4fC5J+VHnI9Z3pIk2CoGy4jkNKEnIRLjg3AUBsrqxy3kLYzXzgIekpL 7kwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796185928; x=1796790728; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=pPaRdrmUZa+vFPOCFiQ8n4zL80cnOC0w3sT1VpJj704=; b=B26QMItHoGlqZCMerHBCacHGnn+kPu2KkFhdjitSqPnV2lMZbWsYK6WJRRPTDdIaV4 XkvisQvbns8wI68m3uYT4eaFJEQyi+PAVYYbAmNL1AJYNoR6wEFaxSqP+fRc73/bc8yt x5RysFNVa8JRjh/1AHJ/tIyJYypUeU5YOks2My4clOQ88O9u3XfAtO/Ng95FaiVM/JNl 9ZN6x8VFwheOy41WC2JXmUZUCYNNqdIXDY9HGoBcauw+hvza6bi8UfgkrkeMnT5QL0ie +2ng4EKV1TGlUpSDT2/5WGLyYN6S1rk5TA+vRTsjVr8BnkV9dgjFmYOGkBVFee9o5kYy avXw== X-Forwarded-Encrypted: i=1; AL+hErGkUvQ+AM0thoDGA2Eh9XDMQ8VbsMRQjPenFOCvIYz4tqILtPbzq1D0SUBwlGvE+g/nU76BYEDWcrn46FY=@vger.kernel.org X-Gm-Message-State: APuU4Z/ffChsK6Y3OzqmMndm9yh9aSum2IqNsR/nFW5N5Cygw+ObgRTj f47FnH4x/Wqm+cZECHPtTeIyqPYshB4SBn7VjEFnXcXyyWMX8mt/Y2R8pPW+Hq3WUKI= X-Gm-Gg: AZFdTZe06Np9IRt4iV7lfJQLnb19ubP7ZETB3G10m1umS+uXxQugxeUiBPWIp+9rr0j oqsPL4tXXYF1LR9dhJkOj1tga6HXa6m6GpoLH9upi+kE0ltAbxLFAk4DOjGSYkTtFm7xSz14tqP /Qp0Odpxb3qn1gkm5JAxUaoDIIUe7UnIuiwbiUyaCeupPthCYPhEXgrcbm8IRXR+Ctl/zcffl1D vPwOKpoGeBOR0ehcKf75FlCFHW5cTv4yRmS5Qvx1awOu9egHoUODhGD4Wi0+HNF6CfHRHO61v1O MBi5JEYPIdYCWtlilRy/SXylStIgh6whpkIG2WwXolRXsZcUQwURXc+0e6of+vNl1NEkDA6V20J Hl8PeL/4dqP+yYhaWmdtSrzBfWfm7bXab0jpC0u7CdwiKqmPGdEod23OskMwN5CJD2x3YCq5xck 8puo3BynkUJhuFedBfIFhcI0l98JGoC2Ka3HQDVQge9H0dY1SN/+Ub9AHafiO+elUXmUYqXIsfT v0NjQIxfJPjmOWr2DEf1ppBC755oVpgceqJqQb601yUL27zy2cD9WgWagD75hsBB5ifa7quKnrx TwQvDkt0K+KnSe+ukIIJrlYz1ObuE26bxBxpk+e7P2CN+/CM1QQEKAxwZigy6png29/yZOmr/io Vzoja17zzp4IOGsJFjbTwsAHCdCc= X-Received: by 2002:a05:7301:4284:b0:382:1aea:c21d with SMTP id 5a478bee46e88-3857191bcf7mr39251079eec.41.1796185927642; Tue, 01 Dec 2026 20:32:07 -0800 (PST) Received: from devbox.ts.blockcast.net ([2602:f74d:1::32]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-386d605c894sm54632028eec.2.2026.12.01.20.32.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 20:32:06 -0800 (PST) From: Omar Ramadan To: Taehee Yoo , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan Cc: Simon Horman , netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Willem de Bruijn , David Ahern , Ido Schimmel Subject: [PATCH net-next v6 03/12] amt: key relay tunnels on a union amt_addr endpoint Date: Wed, 2 Dec 2026 04:31:51 +0000 Message-ID: <20261202043200.286260-4-omar@blockcast.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261202043200.286260-1-omar@blockcast.net> References: <20261202043200.286260-1-omar@blockcast.net> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Omar Ramadan --- drivers/net/amt.c | 113 +++++++++++++++++++++++++++++++--------------- include/net/amt.h | 18 ++++---- 2 files changed, 86 insertions(+), 45 deletions(-) diff --git a/drivers/net/amt.c b/drivers/net/amt.c index e3c69ebf7..bfdce6897 100644 --- a/drivers/net/amt.c +++ b/drivers/net/amt.c @@ -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; diff --git a/include/net/amt.h b/include/net/amt.h index 4c096590d..82ab63876 100644 --- a/include/net/amt.h +++ b/include/net/amt.h @@ -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) + \