diff options
author | Tom Herbert <therbert@google.com> | 2014-06-04 20:19:48 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2014-06-05 01:46:38 -0400 |
commit | af5fcba7f38f3166392f4087ab734433c84f160b (patch) | |
tree | 8a6deba10cd458a72e159112ab6381bcaa352b3e | |
parent | 6579867c8b02606e101a6c511c2511b027ed3f4a (diff) |
udp: Generic functions to set checksum
Added udp_set_csum and udp6_set_csum functions to set UDP checksums
in packets. These are for simple UDP packets such as those that might
be created in UDP tunnels.
Signed-off-by: Tom Herbert <therbert@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r-- | include/net/ip6_checksum.h | 12 | ||||
-rw-r--r-- | include/net/udp.h | 9 | ||||
-rw-r--r-- | net/ipv4/udp.c | 37 | ||||
-rw-r--r-- | net/ipv6/ip6_checksum.c | 38 |
4 files changed, 96 insertions, 0 deletions
diff --git a/include/net/ip6_checksum.h b/include/net/ip6_checksum.h index 8ac5c21f8456..55236cb71174 100644 --- a/include/net/ip6_checksum.h +++ b/include/net/ip6_checksum.h | |||
@@ -82,5 +82,17 @@ static inline void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb) | |||
82 | } | 82 | } |
83 | #endif | 83 | #endif |
84 | 84 | ||
85 | static inline __sum16 udp_v6_check(int len, | ||
86 | const struct in6_addr *saddr, | ||
87 | const struct in6_addr *daddr, | ||
88 | __wsum base) | ||
89 | { | ||
90 | return csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, base); | ||
91 | } | ||
92 | |||
93 | void udp6_set_csum(bool nocheck, struct sk_buff *skb, | ||
94 | const struct in6_addr *saddr, | ||
95 | const struct in6_addr *daddr, int len); | ||
96 | |||
85 | int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, int proto); | 97 | int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, int proto); |
86 | #endif | 98 | #endif |
diff --git a/include/net/udp.h b/include/net/udp.h index 5eb86874bcd6..2ecfc6e15609 100644 --- a/include/net/udp.h +++ b/include/net/udp.h | |||
@@ -147,6 +147,15 @@ static inline __wsum udp_csum(struct sk_buff *skb) | |||
147 | return csum; | 147 | return csum; |
148 | } | 148 | } |
149 | 149 | ||
150 | static inline __sum16 udp_v4_check(int len, __be32 saddr, | ||
151 | __be32 daddr, __wsum base) | ||
152 | { | ||
153 | return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base); | ||
154 | } | ||
155 | |||
156 | void udp_set_csum(bool nocheck, struct sk_buff *skb, | ||
157 | __be32 saddr, __be32 daddr, int len); | ||
158 | |||
150 | /* hash routines shared between UDPv4/6 and UDP-Litev4/6 */ | 159 | /* hash routines shared between UDPv4/6 and UDP-Litev4/6 */ |
151 | static inline void udp_lib_hash(struct sock *sk) | 160 | static inline void udp_lib_hash(struct sock *sk) |
152 | { | 161 | { |
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index e07d52b8617a..a84f6762ea9e 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c | |||
@@ -762,6 +762,43 @@ void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) | |||
762 | } | 762 | } |
763 | EXPORT_SYMBOL_GPL(udp4_hwcsum); | 763 | EXPORT_SYMBOL_GPL(udp4_hwcsum); |
764 | 764 | ||
765 | /* Function to set UDP checksum for an IPv4 UDP packet. This is intended | ||
766 | * for the simple case like when setting the checksum for a UDP tunnel. | ||
767 | */ | ||
768 | void udp_set_csum(bool nocheck, struct sk_buff *skb, | ||
769 | __be32 saddr, __be32 daddr, int len) | ||
770 | { | ||
771 | struct udphdr *uh = udp_hdr(skb); | ||
772 | |||
773 | if (nocheck) | ||
774 | uh->check = 0; | ||
775 | else if (skb_is_gso(skb)) | ||
776 | uh->check = ~udp_v4_check(len, saddr, daddr, 0); | ||
777 | else if (skb_dst(skb) && skb_dst(skb)->dev && | ||
778 | (skb_dst(skb)->dev->features & NETIF_F_V4_CSUM)) { | ||
779 | |||
780 | BUG_ON(skb->ip_summed == CHECKSUM_PARTIAL); | ||
781 | |||
782 | skb->ip_summed = CHECKSUM_PARTIAL; | ||
783 | skb->csum_start = skb_transport_header(skb) - skb->head; | ||
784 | skb->csum_offset = offsetof(struct udphdr, check); | ||
785 | uh->check = ~udp_v4_check(len, saddr, daddr, 0); | ||
786 | } else { | ||
787 | __wsum csum; | ||
788 | |||
789 | BUG_ON(skb->ip_summed == CHECKSUM_PARTIAL); | ||
790 | |||
791 | uh->check = 0; | ||
792 | csum = skb_checksum(skb, 0, len, 0); | ||
793 | uh->check = udp_v4_check(len, saddr, daddr, csum); | ||
794 | if (uh->check == 0) | ||
795 | uh->check = CSUM_MANGLED_0; | ||
796 | |||
797 | skb->ip_summed = CHECKSUM_UNNECESSARY; | ||
798 | } | ||
799 | } | ||
800 | EXPORT_SYMBOL(udp_set_csum); | ||
801 | |||
765 | static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4) | 802 | static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4) |
766 | { | 803 | { |
767 | struct sock *sk = skb->sk; | 804 | struct sock *sk = skb->sk; |
diff --git a/net/ipv6/ip6_checksum.c b/net/ipv6/ip6_checksum.c index da26224a5993..9a4d7322fb22 100644 --- a/net/ipv6/ip6_checksum.c +++ b/net/ipv6/ip6_checksum.c | |||
@@ -84,3 +84,41 @@ int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, int proto) | |||
84 | ip6_compute_pseudo); | 84 | ip6_compute_pseudo); |
85 | } | 85 | } |
86 | EXPORT_SYMBOL(udp6_csum_init); | 86 | EXPORT_SYMBOL(udp6_csum_init); |
87 | |||
88 | /* Function to set UDP checksum for an IPv6 UDP packet. This is intended | ||
89 | * for the simple case like when setting the checksum for a UDP tunnel. | ||
90 | */ | ||
91 | void udp6_set_csum(bool nocheck, struct sk_buff *skb, | ||
92 | const struct in6_addr *saddr, | ||
93 | const struct in6_addr *daddr, int len) | ||
94 | { | ||
95 | struct udphdr *uh = udp_hdr(skb); | ||
96 | |||
97 | if (nocheck) | ||
98 | uh->check = 0; | ||
99 | else if (skb_is_gso(skb)) | ||
100 | uh->check = ~udp_v6_check(len, saddr, daddr, 0); | ||
101 | else if (skb_dst(skb) && skb_dst(skb)->dev && | ||
102 | (skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) { | ||
103 | |||
104 | BUG_ON(skb->ip_summed == CHECKSUM_PARTIAL); | ||
105 | |||
106 | skb->ip_summed = CHECKSUM_PARTIAL; | ||
107 | skb->csum_start = skb_transport_header(skb) - skb->head; | ||
108 | skb->csum_offset = offsetof(struct udphdr, check); | ||
109 | uh->check = ~udp_v6_check(len, saddr, daddr, 0); | ||
110 | } else { | ||
111 | __wsum csum; | ||
112 | |||
113 | BUG_ON(skb->ip_summed == CHECKSUM_PARTIAL); | ||
114 | |||
115 | uh->check = 0; | ||
116 | csum = skb_checksum(skb, 0, len, 0); | ||
117 | uh->check = udp_v6_check(len, saddr, daddr, csum); | ||
118 | if (uh->check == 0) | ||
119 | uh->check = CSUM_MANGLED_0; | ||
120 | |||
121 | skb->ip_summed = CHECKSUM_UNNECESSARY; | ||
122 | } | ||
123 | } | ||
124 | EXPORT_SYMBOL(udp6_set_csum); | ||