blob: 9d9a16e219d6facf2ae12749d17535dfabc7e7da [file] [log] [blame]
Cong Wangacb3e042013-01-07 20:52:40 +00001#include <net/ip.h>
2#include <net/udp.h>
3#include <net/udplite.h>
4#include <asm/checksum.h>
5
6#ifndef _HAVE_ARCH_IPV6_CSUM
7__sum16 csum_ipv6_magic(const struct in6_addr *saddr,
8 const struct in6_addr *daddr,
Alexander Duyck1e940822016-03-11 14:05:41 -08009 __u32 len, __u8 proto, __wsum csum)
Cong Wangacb3e042013-01-07 20:52:40 +000010{
11
12 int carry;
13 __u32 ulen;
14 __u32 uproto;
15 __u32 sum = (__force u32)csum;
16
17 sum += (__force u32)saddr->s6_addr32[0];
18 carry = (sum < (__force u32)saddr->s6_addr32[0]);
19 sum += carry;
20
21 sum += (__force u32)saddr->s6_addr32[1];
22 carry = (sum < (__force u32)saddr->s6_addr32[1]);
23 sum += carry;
24
25 sum += (__force u32)saddr->s6_addr32[2];
26 carry = (sum < (__force u32)saddr->s6_addr32[2]);
27 sum += carry;
28
29 sum += (__force u32)saddr->s6_addr32[3];
30 carry = (sum < (__force u32)saddr->s6_addr32[3]);
31 sum += carry;
32
33 sum += (__force u32)daddr->s6_addr32[0];
34 carry = (sum < (__force u32)daddr->s6_addr32[0]);
35 sum += carry;
36
37 sum += (__force u32)daddr->s6_addr32[1];
38 carry = (sum < (__force u32)daddr->s6_addr32[1]);
39 sum += carry;
40
41 sum += (__force u32)daddr->s6_addr32[2];
42 carry = (sum < (__force u32)daddr->s6_addr32[2]);
43 sum += carry;
44
45 sum += (__force u32)daddr->s6_addr32[3];
46 carry = (sum < (__force u32)daddr->s6_addr32[3]);
47 sum += carry;
48
49 ulen = (__force u32)htonl((__u32) len);
50 sum += ulen;
51 carry = (sum < ulen);
52 sum += carry;
53
54 uproto = (__force u32)htonl(proto);
55 sum += uproto;
56 carry = (sum < uproto);
57 sum += carry;
58
59 return csum_fold((__force __wsum)sum);
60}
61EXPORT_SYMBOL(csum_ipv6_magic);
62#endif
63
64int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, int proto)
65{
66 int err;
67
68 UDP_SKB_CB(skb)->partial_cov = 0;
69 UDP_SKB_CB(skb)->cscov = skb->len;
70
71 if (proto == IPPROTO_UDPLITE) {
72 err = udplite_checksum_init(skb, uh);
73 if (err)
74 return err;
Alexey Kodanev59849012018-02-15 20:18:43 +030075
76 if (UDP_SKB_CB(skb)->partial_cov) {
77 skb->csum = ip6_compute_pseudo(skb, proto);
78 return 0;
79 }
Cong Wangacb3e042013-01-07 20:52:40 +000080 }
81
Tom Herbert40685792014-05-02 16:29:58 -070082 /* To support RFC 6936 (allow zero checksum in UDP/IPV6 for tunnels)
83 * we accept a checksum of zero here. When we find the socket
84 * for the UDP packet we'll check if that socket allows zero checksum
85 * for IPv6 (set by socket option).
Hannes Frederic Sowac148d162016-06-11 21:15:37 +020086 *
87 * Note, we are only interested in != 0 or == 0, thus the
88 * force to int.
Tom Herbert40685792014-05-02 16:29:58 -070089 */
Sean Tranchetti33424e72018-10-23 16:04:31 -060090 err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check,
91 ip6_compute_pseudo);
92 if (err)
93 return err;
94
95 if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) {
96 /* If SW calculated the value, we know it's bad */
97 if (skb->csum_complete_sw)
98 return 1;
99
100 /* HW says the value is bad. Let's validate that.
101 * skb->csum is no longer the full packet checksum,
102 * so don't treat is as such.
103 */
104 skb_checksum_complete_unset(skb);
105 }
106
107 return 0;
Cong Wangacb3e042013-01-07 20:52:40 +0000108}
109EXPORT_SYMBOL(udp6_csum_init);
Tom Herbertaf5fcba2014-06-04 17:19:48 -0700110
111/* Function to set UDP checksum for an IPv6 UDP packet. This is intended
112 * for the simple case like when setting the checksum for a UDP tunnel.
113 */
114void udp6_set_csum(bool nocheck, struct sk_buff *skb,
115 const struct in6_addr *saddr,
116 const struct in6_addr *daddr, int len)
117{
118 struct udphdr *uh = udp_hdr(skb);
119
120 if (nocheck)
121 uh->check = 0;
122 else if (skb_is_gso(skb))
123 uh->check = ~udp_v6_check(len, saddr, daddr, 0);
Edward Cree179bc672016-02-11 20:48:04 +0000124 else if (skb->ip_summed == CHECKSUM_PARTIAL) {
125 uh->check = 0;
126 uh->check = udp_v6_check(len, saddr, daddr, lco_csum(skb));
127 if (uh->check == 0)
128 uh->check = CSUM_MANGLED_0;
Edward Creed75f1302016-02-11 20:49:40 +0000129 } else {
Tom Herbertaf5fcba2014-06-04 17:19:48 -0700130 skb->ip_summed = CHECKSUM_PARTIAL;
131 skb->csum_start = skb_transport_header(skb) - skb->head;
132 skb->csum_offset = offsetof(struct udphdr, check);
133 uh->check = ~udp_v6_check(len, saddr, daddr, 0);
Tom Herbertaf5fcba2014-06-04 17:19:48 -0700134 }
135}
136EXPORT_SYMBOL(udp6_set_csum);