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 Signed-off-by: Omar Ramadan --- drivers/net/amt.c | 134 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 94 insertions(+), 40 deletions(-) diff --git a/drivers/net/amt.c b/drivers/net/amt.c index 1f4914a63..1780d3dca 100644 --- a/drivers/net/amt.c +++ b/drivers/net/amt.c @@ -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);