xref: /linux/net/core/gso.c (revision 72ebc685bca99f466ef35600b5263d1c21c790d9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <linux/skbuff.h>
3 #include <linux/sctp.h>
4 #include <net/gso.h>
5 #include <net/gro.h>
6 
7 /**
8  *	skb_eth_gso_segment - segmentation handler for ethernet protocols.
9  *	@skb: buffer to segment
10  *	@features: features for the output path (see dev->features)
11  *	@type: Ethernet Protocol ID
12  */
13 struct sk_buff *skb_eth_gso_segment(struct sk_buff *skb,
14 				    netdev_features_t features, __be16 type)
15 {
16 	struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
17 	struct packet_offload *ptype;
18 
19 	rcu_read_lock();
20 	list_for_each_entry_rcu(ptype, &net_hotdata.offload_base, list) {
21 		if (ptype->type == type && ptype->callbacks.gso_segment) {
22 			segs = ptype->callbacks.gso_segment(skb, features);
23 			break;
24 		}
25 	}
26 	rcu_read_unlock();
27 
28 	return segs;
29 }
30 EXPORT_SYMBOL(skb_eth_gso_segment);
31 
32 /**
33  *	skb_mac_gso_segment - mac layer segmentation handler.
34  *	@skb: buffer to segment
35  *	@features: features for the output path (see dev->features)
36  */
37 struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
38 				    netdev_features_t features)
39 {
40 	struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
41 	struct packet_offload *ptype;
42 	int vlan_depth = skb->mac_len;
43 	__be16 type = skb_network_protocol(skb, &vlan_depth);
44 
45 	if (unlikely(!type))
46 		return ERR_PTR(-EINVAL);
47 
48 	__skb_pull(skb, vlan_depth);
49 
50 	rcu_read_lock();
51 	list_for_each_entry_rcu(ptype, &net_hotdata.offload_base, list) {
52 		if (ptype->type == type && ptype->callbacks.gso_segment) {
53 			segs = ptype->callbacks.gso_segment(skb, features);
54 			break;
55 		}
56 	}
57 	rcu_read_unlock();
58 
59 	__skb_push(skb, skb->data - skb_mac_header(skb));
60 
61 	return segs;
62 }
63 EXPORT_SYMBOL(skb_mac_gso_segment);
64 /* openvswitch calls this on rx path, so we need a different check.
65  */
66 static bool skb_needs_check(const struct sk_buff *skb, bool tx_path)
67 {
68 	if (tx_path)
69 		return skb->ip_summed != CHECKSUM_PARTIAL &&
70 		       skb->ip_summed != CHECKSUM_UNNECESSARY;
71 
72 	return skb->ip_summed == CHECKSUM_NONE;
73 }
74 
75 /**
76  *	__skb_gso_segment - Perform segmentation on skb.
77  *	@skb: buffer to segment
78  *	@features: features for the output path (see dev->features)
79  *	@tx_path: whether it is called in TX path
80  *	@max_segs: maximum MSS segments per output GSO skb, 0 means no limit;
81  *		   set only for an unencapsulated TCP skb without a frag_list
82  *
83  *	This function segments the given skb and returns a list of segments.
84  *
85  *	It may return NULL if the skb requires no segmentation.  This is
86  *	only possible when GSO is used for verifying header integrity.
87  *
88  *	Segmentation preserves SKB_GSO_CB_OFFSET bytes of previous skb cb.
89  */
90 struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
91 				  netdev_features_t features, bool tx_path,
92 				  unsigned int max_segs)
93 {
94 	struct sk_buff *segs;
95 
96 	if (unlikely(skb_needs_check(skb, tx_path))) {
97 		int err;
98 
99 		/* We're going to init ->check field in TCP or UDP header */
100 		err = skb_cow_head(skb, 0);
101 		if (err < 0)
102 			return ERR_PTR(err);
103 	}
104 
105 	/* Only report GSO partial support if it will enable us to
106 	 * support segmentation on this frame without needing additional
107 	 * work.
108 	 */
109 	if (features & NETIF_F_GSO_PARTIAL) {
110 		netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
111 		struct net_device *dev = skb->dev;
112 
113 		partial_features |= dev->features & dev->gso_partial_features;
114 		if (!skb_gso_ok(skb, features | partial_features))
115 			features &= ~NETIF_F_GSO_PARTIAL;
116 	}
117 
118 	BUILD_BUG_ON(SKB_GSO_CB_OFFSET +
119 		     sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
120 
121 	SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
122 	SKB_GSO_CB(skb)->encap_level = 0;
123 	SKB_GSO_CB(skb)->recursion_counter = 0;
124 	SKB_GSO_CB(skb)->max_segs = min(max_segs, U8_MAX);
125 
126 	skb_reset_mac_header(skb);
127 	skb_reset_mac_len(skb);
128 
129 	segs = skb_mac_gso_segment(skb, features);
130 
131 	if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
132 		skb_warn_bad_offload(skb);
133 
134 	return segs;
135 }
136 EXPORT_SYMBOL(__skb_gso_segment);
137 
138 /**
139  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
140  *
141  * @skb: GSO skb
142  *
143  * skb_gso_transport_seglen is used to determine the real size of the
144  * individual segments, including Layer4 headers (TCP/UDP).
145  *
146  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
147  */
148 static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
149 {
150 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
151 	unsigned int thlen = 0;
152 
153 	if (skb->encapsulation) {
154 		thlen = skb_inner_transport_header(skb) -
155 			skb_transport_header(skb);
156 
157 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
158 			thlen += inner_tcp_hdrlen(skb);
159 	} else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
160 		thlen = tcp_hdrlen(skb);
161 	} else if (unlikely(skb_is_gso_sctp(skb))) {
162 		thlen = sizeof(struct sctphdr);
163 	} else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
164 		thlen = sizeof(struct udphdr);
165 	}
166 	/* UFO sets gso_size to the size of the fragmentation
167 	 * payload, i.e. the size of the L4 (UDP) header is already
168 	 * accounted for.
169 	 */
170 	return thlen + shinfo->gso_size;
171 }
172 
173 /**
174  * skb_gso_network_seglen - Return length of individual segments of a gso packet
175  *
176  * @skb: GSO skb
177  *
178  * skb_gso_network_seglen is used to determine the real size of the
179  * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
180  *
181  * The MAC/L2 header is not accounted for.
182  */
183 static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
184 {
185 	unsigned int hdr_len = skb_transport_header(skb) -
186 			       skb_network_header(skb);
187 
188 	return hdr_len + skb_gso_transport_seglen(skb);
189 }
190 
191 /**
192  * skb_gso_mac_seglen - Return length of individual segments of a gso packet
193  *
194  * @skb: GSO skb
195  *
196  * skb_gso_mac_seglen is used to determine the real size of the
197  * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
198  * headers (TCP/UDP).
199  */
200 static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
201 {
202 	unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
203 
204 	return hdr_len + skb_gso_transport_seglen(skb);
205 }
206 
207 /**
208  * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
209  *
210  * There are a couple of instances where we have a GSO skb, and we
211  * want to determine what size it would be after it is segmented.
212  *
213  * We might want to check:
214  * -    L3+L4+payload size (e.g. IP forwarding)
215  * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
216  *
217  * This is a helper to do that correctly considering GSO_BY_FRAGS.
218  *
219  * @skb: GSO skb
220  *
221  * @seg_len: The segmented length (from skb_gso_*_seglen). In the
222  *           GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
223  *
224  * @max_len: The maximum permissible length.
225  *
226  * Returns true if the segmented length <= max length.
227  */
228 static inline bool skb_gso_size_check(const struct sk_buff *skb,
229 				      unsigned int seg_len,
230 				      unsigned int max_len) {
231 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
232 	const struct sk_buff *iter;
233 
234 	if (shinfo->gso_size != GSO_BY_FRAGS)
235 		return seg_len <= max_len;
236 
237 	/* Undo this so we can re-use header sizes */
238 	seg_len -= GSO_BY_FRAGS;
239 
240 	skb_walk_frags(skb, iter) {
241 		if (seg_len + skb_headlen(iter) > max_len)
242 			return false;
243 	}
244 
245 	return true;
246 }
247 
248 /**
249  * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
250  *
251  * @skb: GSO skb
252  * @mtu: MTU to validate against
253  *
254  * skb_gso_validate_network_len validates if a given skb will fit a
255  * wanted MTU once split. It considers L3 headers, L4 headers, and the
256  * payload.
257  */
258 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
259 {
260 	return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
261 }
262 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
263 
264 /**
265  * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
266  *
267  * @skb: GSO skb
268  * @len: length to validate against
269  *
270  * skb_gso_validate_mac_len validates if a given skb will fit a wanted
271  * length once split, including L2, L3 and L4 headers and the payload.
272  */
273 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
274 {
275 	return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
276 }
277 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
278 
279