xref: /linux/include/net/gso.h (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
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/NSH GSO handler entries for this packet. */
23 	u8	recursion_counter;
24 	/* Max MSS segs per output skb, 0 = no limit */
25 	u8	max_segs;
26 };
27 #define SKB_GSO_CB_OFFSET	32
28 #define SKB_GSO_CB(skb) ((struct skb_gso_cb *)((skb)->cb + SKB_GSO_CB_OFFSET))
29 
30 static inline bool gso_recursion_inc_test(struct sk_buff *skb,
31 					  unsigned int limit)
32 {
33 	return ++SKB_GSO_CB(skb)->recursion_counter > limit;
34 }
35 
36 static inline int skb_tnl_header_len(const struct sk_buff *inner_skb)
37 {
38 	return (skb_mac_header(inner_skb) - inner_skb->head) -
39 		SKB_GSO_CB(inner_skb)->mac_offset;
40 }
41 
42 static inline int gso_pskb_expand_head(struct sk_buff *skb, int extra)
43 {
44 	int new_headroom, headroom;
45 	int ret;
46 
47 	headroom = skb_headroom(skb);
48 	ret = pskb_expand_head(skb, extra, 0, GFP_ATOMIC);
49 	if (ret)
50 		return ret;
51 
52 	new_headroom = skb_headroom(skb);
53 	SKB_GSO_CB(skb)->mac_offset += (new_headroom - headroom);
54 	return 0;
55 }
56 
57 static inline void gso_reset_checksum(struct sk_buff *skb, __wsum res)
58 {
59 	/* Do not update partial checksums if remote checksum is enabled. */
60 	if (skb->remcsum_offload)
61 		return;
62 
63 	SKB_GSO_CB(skb)->csum = res;
64 	SKB_GSO_CB(skb)->csum_start = skb_checksum_start(skb) - skb->head;
65 }
66 
67 /* Compute the checksum for a gso segment. First compute the checksum value
68  * from the start of transport header to SKB_GSO_CB(skb)->csum_start, and
69  * then add in skb->csum (checksum from csum_start to end of packet).
70  * skb->csum and csum_start are then updated to reflect the checksum of the
71  * resultant packet starting from the transport header-- the resultant checksum
72  * is in the res argument (i.e. normally zero or ~ of checksum of a pseudo
73  * header.
74  */
75 static inline __sum16 gso_make_checksum(struct sk_buff *skb, __wsum res)
76 {
77 	unsigned char *csum_start = skb_transport_header(skb);
78 	int plen = (skb->head + SKB_GSO_CB(skb)->csum_start) - csum_start;
79 	__wsum partial = SKB_GSO_CB(skb)->csum;
80 
81 	SKB_GSO_CB(skb)->csum = res;
82 	SKB_GSO_CB(skb)->csum_start = csum_start - skb->head;
83 
84 	return csum_fold(csum_partial(csum_start, plen, partial));
85 }
86 
87 struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
88 				  netdev_features_t features, bool tx_path,
89 				  unsigned int max_segs);
90 
91 static inline struct sk_buff *skb_gso_segment(struct sk_buff *skb,
92 					      netdev_features_t features)
93 {
94 	return __skb_gso_segment(skb, features, true, 0);
95 }
96 
97 struct sk_buff *skb_eth_gso_segment(struct sk_buff *skb,
98 				    netdev_features_t features, __be16 type);
99 
100 struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
101 				    netdev_features_t features);
102 
103 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu);
104 
105 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len);
106 
107 static inline void skb_gso_error_unwind(struct sk_buff *skb, __be16 protocol,
108 					int pulled_hlen, u16 mac_offset,
109 					int mac_len)
110 {
111 	skb->protocol = protocol;
112 	skb->encapsulation = 1;
113 	skb_push(skb, pulled_hlen);
114 	skb_reset_transport_header(skb);
115 	skb->mac_header = mac_offset;
116 	skb->network_header = skb->mac_header + mac_len;
117 	skb->mac_len = mac_len;
118 }
119 
120 #endif /* _NET_GSO_H */
121