ALSA: usb-audio: Fix an out-of-bound read in create_composite_quirks
[linux/fpc-iii.git] / net / ipv6 / ip6_checksum.c
blob391a8fedb27ed7941cdc098663a0e05dda8d2a39
1 #include <net/ip.h>
2 #include <net/udp.h>
3 #include <net/udplite.h>
4 #include <asm/checksum.h>
6 #ifndef _HAVE_ARCH_IPV6_CSUM
7 __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
8 const struct in6_addr *daddr,
9 __u32 len, unsigned short proto,
10 __wsum csum)
13 int carry;
14 __u32 ulen;
15 __u32 uproto;
16 __u32 sum = (__force u32)csum;
18 sum += (__force u32)saddr->s6_addr32[0];
19 carry = (sum < (__force u32)saddr->s6_addr32[0]);
20 sum += carry;
22 sum += (__force u32)saddr->s6_addr32[1];
23 carry = (sum < (__force u32)saddr->s6_addr32[1]);
24 sum += carry;
26 sum += (__force u32)saddr->s6_addr32[2];
27 carry = (sum < (__force u32)saddr->s6_addr32[2]);
28 sum += carry;
30 sum += (__force u32)saddr->s6_addr32[3];
31 carry = (sum < (__force u32)saddr->s6_addr32[3]);
32 sum += carry;
34 sum += (__force u32)daddr->s6_addr32[0];
35 carry = (sum < (__force u32)daddr->s6_addr32[0]);
36 sum += carry;
38 sum += (__force u32)daddr->s6_addr32[1];
39 carry = (sum < (__force u32)daddr->s6_addr32[1]);
40 sum += carry;
42 sum += (__force u32)daddr->s6_addr32[2];
43 carry = (sum < (__force u32)daddr->s6_addr32[2]);
44 sum += carry;
46 sum += (__force u32)daddr->s6_addr32[3];
47 carry = (sum < (__force u32)daddr->s6_addr32[3]);
48 sum += carry;
50 ulen = (__force u32)htonl((__u32) len);
51 sum += ulen;
52 carry = (sum < ulen);
53 sum += carry;
55 uproto = (__force u32)htonl(proto);
56 sum += uproto;
57 carry = (sum < uproto);
58 sum += carry;
60 return csum_fold((__force __wsum)sum);
62 EXPORT_SYMBOL(csum_ipv6_magic);
63 #endif
65 int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, int proto)
67 int err;
69 UDP_SKB_CB(skb)->partial_cov = 0;
70 UDP_SKB_CB(skb)->cscov = skb->len;
72 if (proto == IPPROTO_UDPLITE) {
73 err = udplite_checksum_init(skb, uh);
74 if (err)
75 return err;
77 if (UDP_SKB_CB(skb)->partial_cov) {
78 skb->csum = ip6_compute_pseudo(skb, proto);
79 return 0;
83 /* To support RFC 6936 (allow zero checksum in UDP/IPV6 for tunnels)
84 * we accept a checksum of zero here. When we find the socket
85 * for the UDP packet we'll check if that socket allows zero checksum
86 * for IPv6 (set by socket option).
88 return skb_checksum_init_zero_check(skb, proto, uh->check,
89 ip6_compute_pseudo);
91 EXPORT_SYMBOL(udp6_csum_init);
93 /* Function to set UDP checksum for an IPv6 UDP packet. This is intended
94 * for the simple case like when setting the checksum for a UDP tunnel.
96 void udp6_set_csum(bool nocheck, struct sk_buff *skb,
97 const struct in6_addr *saddr,
98 const struct in6_addr *daddr, int len)
100 struct udphdr *uh = udp_hdr(skb);
102 if (nocheck)
103 uh->check = 0;
104 else if (skb_is_gso(skb))
105 uh->check = ~udp_v6_check(len, saddr, daddr, 0);
106 else if (skb_dst(skb) && skb_dst(skb)->dev &&
107 (skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
109 BUG_ON(skb->ip_summed == CHECKSUM_PARTIAL);
111 skb->ip_summed = CHECKSUM_PARTIAL;
112 skb->csum_start = skb_transport_header(skb) - skb->head;
113 skb->csum_offset = offsetof(struct udphdr, check);
114 uh->check = ~udp_v6_check(len, saddr, daddr, 0);
115 } else {
116 __wsum csum;
118 BUG_ON(skb->ip_summed == CHECKSUM_PARTIAL);
120 uh->check = 0;
121 csum = skb_checksum(skb, 0, len, 0);
122 uh->check = udp_v6_check(len, saddr, daddr, csum);
123 if (uh->check == 0)
124 uh->check = CSUM_MANGLED_0;
126 skb->ip_summed = CHECKSUM_UNNECESSARY;
129 EXPORT_SYMBOL(udp6_set_csum);