With CHECKSUM_COMPLETE, ipv6_rpl_srh_rcv() does not update skb->csum correctly. It decrements Segments Left and writes the next segment into the IPv6 destination address before skb_postpull_rcsum(), which then removes the modified bytes instead of the received ones. The same happens on the loopback path, where skb->csum is not updated for the Hop Limit decrement. If the packet is then delivered on the same node and skb->csum was computed in software, for instance by conntrack, UDP can drop the packet as a checksum error. With skb->csum computed by the device, TCP can instead log a "hw csum failure", as if the device had computed a wrong skb->csum. Fix this by updating the decompressed copy of the SRH instead, and by writing the new destination address into the packet after skb_postpull_rcsum(), which now covers all the bytes between the network header and skb->data. On the loopback path, update skb->csum for the Hop Limit decrement with csum_replace(). Since the SRH is updated in the copy, Segments Left and the destination address are written into the packet after the pskb_expand_head() later in the function, as the following commit needs. For the same reason, when a loop is detected, the ICMP Parameter Problem now carries the packet without the Segments Left decrement for the segment being processed, as the other Parameter Problems of the routing headers do. Found by manual code inspection, and reproduced in a VM without kernel changes or error injection. Kuniyuki Iwashima also pointed out that skb_postpull_rcsum() is applied to the modified header. With skb->csum computed by an nftables ct rule, UDP can drop the datagrams sent through RPL segments of the same node. With this patch, the datagrams sent by the reproducer are delivered. Fixes: 8610c7c6e3bd ("net: ipv6: add support for rpl sr exthdr") Suggested-by: Kuniyuki Iwashima Signed-off-by: Andrea Mayer --- net/ipv6/exthdrs.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/net/ipv6/exthdrs.c b/net/ipv6/exthdrs.c index ccc549de3c8a..2df32872bced 100644 --- a/net/ipv6/exthdrs.c +++ b/net/ipv6/exthdrs.c @@ -485,6 +485,7 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev) struct net *net = dev_net(skb->dev); struct ipv6hdr *oldhdr; unsigned int chdr_len; + struct in6_addr addr; unsigned char *buf; int accept_rpl_seg; int i, err; @@ -548,9 +549,6 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev) return -1; } - hdr->segments_left--; - i = n - hdr->segments_left; - buf = kcalloc(struct_size(hdr, segments.addr, n + 2), 2, GFP_ATOMIC); if (unlikely(!buf)) { kfree_skb(skb); @@ -559,6 +557,8 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev) ohdr = (struct ipv6_rpl_sr_hdr *)buf; ipv6_rpl_srh_decompress(ohdr, hdr, &ipv6_hdr(skb)->daddr, n); + ohdr->segments_left--; + i = n - ohdr->segments_left; chdr = (struct ipv6_rpl_sr_hdr *)(buf + ((ohdr->hdrlen + 1) << 3)); if (ipv6_addr_is_multicast(&ohdr->rpl_segaddr[i])) { @@ -575,17 +575,16 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev) return -1; } - swap(ipv6_hdr(skb)->daddr, ohdr->rpl_segaddr[i]); + addr = ohdr->rpl_segaddr[i]; + ohdr->rpl_segaddr[i] = ipv6_hdr(skb)->daddr; - ipv6_rpl_srh_compress(chdr, ohdr, &ipv6_hdr(skb)->daddr, n); - - oldhdr = ipv6_hdr(skb); + ipv6_rpl_srh_compress(chdr, ohdr, &addr, n); skb_pull(skb, ((hdr->hdrlen + 1) << 3)); - skb_postpull_rcsum(skb, oldhdr, - sizeof(struct ipv6hdr) + ((hdr->hdrlen + 1) << 3)); + skb_postpull_rcsum(skb, skb_network_header(skb), + -skb_network_offset(skb)); chdr_len = sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3); - if (unlikely(!hdr->segments_left || + if (unlikely(!ohdr->segments_left || skb_headroom(skb) < chdr_len + skb->mac_len)) { if (pskb_expand_head(skb, chdr_len + skb->mac_len, 0, GFP_ATOMIC)) { @@ -594,9 +593,10 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev) kfree(buf); return -1; } - - oldhdr = ipv6_hdr(skb); } + + oldhdr = ipv6_hdr(skb); + skb_push(skb, chdr_len); skb_reset_network_header(skb); skb_mac_header_rebuild(skb); @@ -605,6 +605,7 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev) memmove(ipv6_hdr(skb), oldhdr, sizeof(struct ipv6hdr)); memcpy(skb_transport_header(skb), chdr, (chdr->hdrlen + 1) << 3); + ipv6_hdr(skb)->daddr = addr; ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); skb_postpush_rcsum(skb, ipv6_hdr(skb), sizeof(struct ipv6hdr) + ((chdr->hdrlen + 1) << 3)); @@ -619,6 +620,10 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev) } if (skb_dst_dev(skb)->flags & IFF_LOOPBACK) { + /* payload_len, nexthdr and hop_limit */ + __be32 *hlim_word = (__be32 *)ipv6_hdr(skb) + 1; + __be32 old; + if (ipv6_hdr(skb)->hop_limit <= 1) { __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS); icmpv6_send(skb, ICMPV6_TIME_EXCEED, @@ -626,8 +631,13 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb, struct inet6_dev *idev) kfree_skb_reason(skb, SKB_DROP_REASON_IP_TTL_EXCEEDED); return -1; } + old = *hlim_word; ipv6_hdr(skb)->hop_limit--; + if (skb->ip_summed == CHECKSUM_COMPLETE) + csum_replace(&skb->csum, (__force __wsum)old, + (__force __wsum)*hlim_word); + skb_pull(skb, sizeof(struct ipv6hdr)); goto looped_back; } -- 2.43.0