xref: /openbmc/linux/net/ipv6/ip6_checksum.c (revision 3eb66e91a25497065c5322b1268cbc3953642227)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2acb3e041SCong Wang #include <net/ip.h>
3acb3e041SCong Wang #include <net/udp.h>
4acb3e041SCong Wang #include <net/udplite.h>
5acb3e041SCong Wang #include <asm/checksum.h>
6acb3e041SCong Wang 
7acb3e041SCong Wang #ifndef _HAVE_ARCH_IPV6_CSUM
csum_ipv6_magic(const struct in6_addr * saddr,const struct in6_addr * daddr,__u32 len,__u8 proto,__wsum csum)8acb3e041SCong Wang __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
9acb3e041SCong Wang 			const struct in6_addr *daddr,
101e940829SAlexander Duyck 			__u32 len, __u8 proto, __wsum csum)
11acb3e041SCong Wang {
12acb3e041SCong Wang 
13acb3e041SCong Wang 	int carry;
14acb3e041SCong Wang 	__u32 ulen;
15acb3e041SCong Wang 	__u32 uproto;
16acb3e041SCong Wang 	__u32 sum = (__force u32)csum;
17acb3e041SCong Wang 
18acb3e041SCong Wang 	sum += (__force u32)saddr->s6_addr32[0];
19acb3e041SCong Wang 	carry = (sum < (__force u32)saddr->s6_addr32[0]);
20acb3e041SCong Wang 	sum += carry;
21acb3e041SCong Wang 
22acb3e041SCong Wang 	sum += (__force u32)saddr->s6_addr32[1];
23acb3e041SCong Wang 	carry = (sum < (__force u32)saddr->s6_addr32[1]);
24acb3e041SCong Wang 	sum += carry;
25acb3e041SCong Wang 
26acb3e041SCong Wang 	sum += (__force u32)saddr->s6_addr32[2];
27acb3e041SCong Wang 	carry = (sum < (__force u32)saddr->s6_addr32[2]);
28acb3e041SCong Wang 	sum += carry;
29acb3e041SCong Wang 
30acb3e041SCong Wang 	sum += (__force u32)saddr->s6_addr32[3];
31acb3e041SCong Wang 	carry = (sum < (__force u32)saddr->s6_addr32[3]);
32acb3e041SCong Wang 	sum += carry;
33acb3e041SCong Wang 
34acb3e041SCong Wang 	sum += (__force u32)daddr->s6_addr32[0];
35acb3e041SCong Wang 	carry = (sum < (__force u32)daddr->s6_addr32[0]);
36acb3e041SCong Wang 	sum += carry;
37acb3e041SCong Wang 
38acb3e041SCong Wang 	sum += (__force u32)daddr->s6_addr32[1];
39acb3e041SCong Wang 	carry = (sum < (__force u32)daddr->s6_addr32[1]);
40acb3e041SCong Wang 	sum += carry;
41acb3e041SCong Wang 
42acb3e041SCong Wang 	sum += (__force u32)daddr->s6_addr32[2];
43acb3e041SCong Wang 	carry = (sum < (__force u32)daddr->s6_addr32[2]);
44acb3e041SCong Wang 	sum += carry;
45acb3e041SCong Wang 
46acb3e041SCong Wang 	sum += (__force u32)daddr->s6_addr32[3];
47acb3e041SCong Wang 	carry = (sum < (__force u32)daddr->s6_addr32[3]);
48acb3e041SCong Wang 	sum += carry;
49acb3e041SCong Wang 
50acb3e041SCong Wang 	ulen = (__force u32)htonl((__u32) len);
51acb3e041SCong Wang 	sum += ulen;
52acb3e041SCong Wang 	carry = (sum < ulen);
53acb3e041SCong Wang 	sum += carry;
54acb3e041SCong Wang 
55acb3e041SCong Wang 	uproto = (__force u32)htonl(proto);
56acb3e041SCong Wang 	sum += uproto;
57acb3e041SCong Wang 	carry = (sum < uproto);
58acb3e041SCong Wang 	sum += carry;
59acb3e041SCong Wang 
60acb3e041SCong Wang 	return csum_fold((__force __wsum)sum);
61acb3e041SCong Wang }
62acb3e041SCong Wang EXPORT_SYMBOL(csum_ipv6_magic);
63acb3e041SCong Wang #endif
64acb3e041SCong Wang 
udp6_csum_init(struct sk_buff * skb,struct udphdr * uh,int proto)65acb3e041SCong Wang int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, int proto)
66acb3e041SCong Wang {
67acb3e041SCong Wang 	int err;
68acb3e041SCong Wang 
69acb3e041SCong Wang 	UDP_SKB_CB(skb)->partial_cov = 0;
70acb3e041SCong Wang 	UDP_SKB_CB(skb)->cscov = skb->len;
71acb3e041SCong Wang 
72acb3e041SCong Wang 	if (proto == IPPROTO_UDPLITE) {
73acb3e041SCong Wang 		err = udplite_checksum_init(skb, uh);
74acb3e041SCong Wang 		if (err)
75acb3e041SCong Wang 			return err;
7615f35d49SAlexey Kodanev 
7715f35d49SAlexey Kodanev 		if (UDP_SKB_CB(skb)->partial_cov) {
7815f35d49SAlexey Kodanev 			skb->csum = ip6_compute_pseudo(skb, proto);
7915f35d49SAlexey Kodanev 			return 0;
8015f35d49SAlexey Kodanev 		}
81acb3e041SCong Wang 	}
82acb3e041SCong Wang 
834068579eSTom Herbert 	/* To support RFC 6936 (allow zero checksum in UDP/IPV6 for tunnels)
844068579eSTom Herbert 	 * we accept a checksum of zero here. When we find the socket
854068579eSTom Herbert 	 * for the UDP packet we'll check if that socket allows zero checksum
864068579eSTom Herbert 	 * for IPv6 (set by socket option).
87c148d163SHannes Frederic Sowa 	 *
88c148d163SHannes Frederic Sowa 	 * Note, we are only interested in != 0 or == 0, thus the
89c148d163SHannes Frederic Sowa 	 * force to int.
90acb3e041SCong Wang 	 */
91*db4f1be3SSean Tranchetti 	err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check,
924068579eSTom Herbert 							ip6_compute_pseudo);
93*db4f1be3SSean Tranchetti 	if (err)
94*db4f1be3SSean Tranchetti 		return err;
95*db4f1be3SSean Tranchetti 
96*db4f1be3SSean Tranchetti 	if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) {
97*db4f1be3SSean Tranchetti 		/* If SW calculated the value, we know it's bad */
98*db4f1be3SSean Tranchetti 		if (skb->csum_complete_sw)
99*db4f1be3SSean Tranchetti 			return 1;
100*db4f1be3SSean Tranchetti 
101*db4f1be3SSean Tranchetti 		/* HW says the value is bad. Let's validate that.
102*db4f1be3SSean Tranchetti 		 * skb->csum is no longer the full packet checksum,
103*db4f1be3SSean Tranchetti 		 * so don't treat is as such.
104*db4f1be3SSean Tranchetti 		 */
105*db4f1be3SSean Tranchetti 		skb_checksum_complete_unset(skb);
106*db4f1be3SSean Tranchetti 	}
107*db4f1be3SSean Tranchetti 
108*db4f1be3SSean Tranchetti 	return 0;
109acb3e041SCong Wang }
110acb3e041SCong Wang EXPORT_SYMBOL(udp6_csum_init);
111af5fcba7STom Herbert 
112af5fcba7STom Herbert /* Function to set UDP checksum for an IPv6 UDP packet. This is intended
113af5fcba7STom Herbert  * for the simple case like when setting the checksum for a UDP tunnel.
114af5fcba7STom Herbert  */
udp6_set_csum(bool nocheck,struct sk_buff * skb,const struct in6_addr * saddr,const struct in6_addr * daddr,int len)115af5fcba7STom Herbert void udp6_set_csum(bool nocheck, struct sk_buff *skb,
116af5fcba7STom Herbert 		   const struct in6_addr *saddr,
117af5fcba7STom Herbert 		   const struct in6_addr *daddr, int len)
118af5fcba7STom Herbert {
119af5fcba7STom Herbert 	struct udphdr *uh = udp_hdr(skb);
120af5fcba7STom Herbert 
121af5fcba7STom Herbert 	if (nocheck)
122af5fcba7STom Herbert 		uh->check = 0;
123af5fcba7STom Herbert 	else if (skb_is_gso(skb))
124af5fcba7STom Herbert 		uh->check = ~udp_v6_check(len, saddr, daddr, 0);
125179bc67fSEdward Cree 	else if (skb->ip_summed == CHECKSUM_PARTIAL) {
126179bc67fSEdward Cree 		uh->check = 0;
127179bc67fSEdward Cree 		uh->check = udp_v6_check(len, saddr, daddr, lco_csum(skb));
128179bc67fSEdward Cree 		if (uh->check == 0)
129179bc67fSEdward Cree 			uh->check = CSUM_MANGLED_0;
130d75f1306SEdward Cree 	} else {
131af5fcba7STom Herbert 		skb->ip_summed = CHECKSUM_PARTIAL;
132af5fcba7STom Herbert 		skb->csum_start = skb_transport_header(skb) - skb->head;
133af5fcba7STom Herbert 		skb->csum_offset = offsetof(struct udphdr, check);
134af5fcba7STom Herbert 		uh->check = ~udp_v6_check(len, saddr, daddr, 0);
135af5fcba7STom Herbert 	}
136af5fcba7STom Herbert }
137af5fcba7STom Herbert EXPORT_SYMBOL(udp6_set_csum);
138