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