xref: /linux/include/net/gso.h (revision 7e170da2edec9fb0717543571cc14b0aabee2b46)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #ifndef _NET_GSO_H
4 #define _NET_GSO_H
5 
6 #include <linux/skbuff.h>
7 
8 /* Keeps track of mac header offset relative to skb->head.
9  * It is useful for TSO of Tunneling protocol. e.g. GRE.
10  * For non-tunnel skb it points to skb_mac_header() and for
11  * tunnel skb it points to outer mac header.
12  * Keeps track of level of encapsulation of network headers.
13  */
14 struct skb_gso_cb {
15 	union {
16 		int	mac_offset;
17 		int	data_offset;
18 	};
19 	int	encap_level;
20 	__wsum	csum;
21 	__u16	csum_start;
22 	/* Number of IPv4/IPv6 GSO handler entries for this packet. */
23 	u8	recursion_counter;
24 };
25 #define SKB_GSO_CB_OFFSET	32
26 #define SKB_GSO_CB(skb) ((struct skb_gso_cb *)((skb)->cb + SKB_GSO_CB_OFFSET))
27 
28 static inline bool gso_recursion_inc_test(struct sk_buff *skb,
29 					  unsigned int limit)
30 {
31 	return ++SKB_GSO_CB(skb)->recursion_counter > limit;
32 }
33 
34 static inline int skb_tnl_header_len(const struct sk_buff *inner_skb)
35 {
36 	return (skb_mac_header(inner_skb) - inner_skb->head) -
37 		SKB_GSO_CB(inner_skb)->mac_offset;
38 }
39 
40 static inline int gso_pskb_expand_head(struct sk_buff *skb, int extra)
41 {
42 	int new_headroom, headroom;
43 	int ret;
44 
45 	headroom = skb_headroom(skb);
46 	ret = pskb_expand_head(skb, extra, 0, GFP_ATOMIC);
47 	if (ret)
48 		return ret;
49 
50 	new_headroom = skb_headroom(skb);
51 	SKB_GSO_CB(skb)->mac_offset += (new_headroom - headroom);
52 	return 0;
53 }
54 
55 static inline void gso_reset_checksum(struct sk_buff *skb, __wsum res)
56 {
57 	/* Do not update partial checksums if remote checksum is enabled. */
58 	if (skb->remcsum_offload)
59 		return;
60 
61 	SKB_GSO_CB(skb)->csum = res;
62 	SKB_GSO_CB(skb)->csum_start = skb_checksum_start(skb) - skb->head;
63 }
64 
65 /* Compute the checksum for a gso segment. First compute the checksum value
66  * from the start of transport header to SKB_GSO_CB(skb)->csum_start, and
67  * then add in skb->csum (checksum from csum_start to end of packet).
68  * skb->csum and csum_start are then updated to reflect the checksum of the
69  * resultant packet starting from the transport header-- the resultant checksum
70  * is in the res argument (i.e. normally zero or ~ of checksum of a pseudo
71  * header.
72  */
73 static inline __sum16 gso_make_checksum(struct sk_buff *skb, __wsum res)
74 {
75 	unsigned char *csum_start = skb_transport_header(skb);
76 	int plen = (skb->head + SKB_GSO_CB(skb)->csum_start) - csum_start;
77 	__wsum partial = SKB_GSO_CB(skb)->csum;
78 
79 	SKB_GSO_CB(skb)->csum = res;
80 	SKB_GSO_CB(skb)->csum_start = csum_start - skb->head;
81 
82 	return csum_fold(csum_partial(csum_start, plen, partial));
83 }
84 
85 struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
86 				  netdev_features_t features, bool tx_path);
87 
88 static inline struct sk_buff *skb_gso_segment(struct sk_buff *skb,
89 					      netdev_features_t features)
90 {
91 	return __skb_gso_segment(skb, features, true);
92 }
93 
94 struct sk_buff *skb_eth_gso_segment(struct sk_buff *skb,
95 				    netdev_features_t features, __be16 type);
96 
97 struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
98 				    netdev_features_t features);
99 
100 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu);
101 
102 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len);
103 
104 static inline void skb_gso_error_unwind(struct sk_buff *skb, __be16 protocol,
105 					int pulled_hlen, u16 mac_offset,
106 					int mac_len)
107 {
108 	skb->protocol = protocol;
109 	skb->encapsulation = 1;
110 	skb_push(skb, pulled_hlen);
111 	skb_reset_transport_header(skb);
112 	skb->mac_header = mac_offset;
113 	skb->network_header = skb->mac_header + mac_len;
114 	skb->mac_len = mac_len;
115 }
116 
117 #endif /* _NET_GSO_H */
118