xref: /openbmc/linux/include/net/ip6_checksum.h (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		Checksumming functions for IPv6
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	Jorge Cwik, <jorge@laser.satlink.net>
101da177e4SLinus Torvalds  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
111da177e4SLinus Torvalds  *		Borrows very liberally from tcp.c and ip.c, see those
121da177e4SLinus Torvalds  *		files for more names.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds  *	Fixes:
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	Ralf Baechle			:	generic ipv6 checksum
191da177e4SLinus Torvalds  *	<ralf@waldorf-gmbh.de>
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #ifndef _CHECKSUM_IPV6_H
231da177e4SLinus Torvalds #define _CHECKSUM_IPV6_H
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include <asm/types.h>
261da177e4SLinus Torvalds #include <asm/byteorder.h>
271da177e4SLinus Torvalds #include <net/ip.h>
281da177e4SLinus Torvalds #include <asm/checksum.h>
291da177e4SLinus Torvalds #include <linux/in6.h>
308663e02aSVlad Yasevich #include <linux/tcp.h>
318663e02aSVlad Yasevich #include <linux/ipv6.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #ifndef _HAVE_ARCH_IPV6_CSUM
34acb3e041SCong Wang __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
3556649d5dSAl Viro 			const struct in6_addr *daddr,
361e940829SAlexander Duyck 			__u32 len, __u8 proto, __wsum csum);
371da177e4SLinus Torvalds #endif
388663e02aSVlad Yasevich 
ip6_compute_pseudo(struct sk_buff * skb,int proto)39e4f45b7fSTom Herbert static inline __wsum ip6_compute_pseudo(struct sk_buff *skb, int proto)
40e4f45b7fSTom Herbert {
41e4f45b7fSTom Herbert 	return ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
42e4f45b7fSTom Herbert 					    &ipv6_hdr(skb)->daddr,
43e4f45b7fSTom Herbert 					    skb->len, proto, 0));
44e4f45b7fSTom Herbert }
45e4f45b7fSTom Herbert 
tcp_v6_check(int len,const struct in6_addr * saddr,const struct in6_addr * daddr,__wsum base)468663e02aSVlad Yasevich static __inline__ __sum16 tcp_v6_check(int len,
478663e02aSVlad Yasevich 				   const struct in6_addr *saddr,
488663e02aSVlad Yasevich 				   const struct in6_addr *daddr,
498663e02aSVlad Yasevich 				   __wsum base)
508663e02aSVlad Yasevich {
518663e02aSVlad Yasevich 	return csum_ipv6_magic(saddr, daddr, len, IPPROTO_TCP, base);
528663e02aSVlad Yasevich }
538663e02aSVlad Yasevich 
__tcp_v6_send_check(struct sk_buff * skb,const struct in6_addr * saddr,const struct in6_addr * daddr)548663e02aSVlad Yasevich static inline void __tcp_v6_send_check(struct sk_buff *skb,
558663e02aSVlad Yasevich 				       const struct in6_addr *saddr,
568663e02aSVlad Yasevich 				       const struct in6_addr *daddr)
578663e02aSVlad Yasevich {
588663e02aSVlad Yasevich 	struct tcphdr *th = tcp_hdr(skb);
598663e02aSVlad Yasevich 
608663e02aSVlad Yasevich 	th->check = ~tcp_v6_check(skb->len, saddr, daddr, 0);
618663e02aSVlad Yasevich 	skb->csum_start = skb_transport_header(skb) - skb->head;
628663e02aSVlad Yasevich 	skb->csum_offset = offsetof(struct tcphdr, check);
638663e02aSVlad Yasevich }
648663e02aSVlad Yasevich 
tcp_v6_gso_csum_prep(struct sk_buff * skb)65*76eeb12bSHeiner Kallweit static inline void tcp_v6_gso_csum_prep(struct sk_buff *skb)
66*76eeb12bSHeiner Kallweit {
67*76eeb12bSHeiner Kallweit 	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
68*76eeb12bSHeiner Kallweit 	struct tcphdr *th = tcp_hdr(skb);
69*76eeb12bSHeiner Kallweit 
70*76eeb12bSHeiner Kallweit 	ipv6h->payload_len = 0;
71*76eeb12bSHeiner Kallweit 	th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
72*76eeb12bSHeiner Kallweit }
73*76eeb12bSHeiner Kallweit 
udp_v6_check(int len,const struct in6_addr * saddr,const struct in6_addr * daddr,__wsum base)74af5fcba7STom Herbert static inline __sum16 udp_v6_check(int len,
75af5fcba7STom Herbert 				   const struct in6_addr *saddr,
76af5fcba7STom Herbert 				   const struct in6_addr *daddr,
77af5fcba7STom Herbert 				   __wsum base)
78af5fcba7STom Herbert {
79af5fcba7STom Herbert 	return csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, base);
80af5fcba7STom Herbert }
81af5fcba7STom Herbert 
82af5fcba7STom Herbert void udp6_set_csum(bool nocheck, struct sk_buff *skb,
83af5fcba7STom Herbert 		   const struct in6_addr *saddr,
84af5fcba7STom Herbert 		   const struct in6_addr *daddr, int len);
85af5fcba7STom Herbert 
86acb3e041SCong Wang int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, int proto);
871da177e4SLinus Torvalds #endif
88