xref: /linux/net/xfrm/xfrm_device.c (revision 0c7c237b1c35011ef0b8d30c1d5c20bc6ae7b69b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * xfrm_device.c - IPsec device offloading code.
4  *
5  * Copyright (c) 2015 secunet Security Networks AG
6  *
7  * Author:
8  * Steffen Klassert <steffen.klassert@secunet.com>
9  */
10 
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <net/dst.h>
18 #include <net/gso.h>
19 #include <net/xfrm.h>
20 #include <linux/notifier.h>
21 
22 #ifdef CONFIG_XFRM_OFFLOAD
23 static void __xfrm_transport_prep(struct xfrm_state *x, struct sk_buff *skb,
24 				  unsigned int hsize)
25 {
26 	struct xfrm_offload *xo = xfrm_offload(skb);
27 
28 	skb_reset_mac_len(skb);
29 	if (xo->flags & XFRM_GSO_SEGMENT)
30 		skb->transport_header -= x->props.header_len;
31 
32 	pskb_pull(skb, skb_transport_offset(skb) + x->props.header_len);
33 }
34 
35 static void __xfrm_mode_tunnel_prep(struct xfrm_state *x, struct sk_buff *skb,
36 				    unsigned int hsize)
37 
38 {
39 	struct xfrm_offload *xo = xfrm_offload(skb);
40 
41 	if (xo->flags & XFRM_GSO_SEGMENT)
42 		skb->transport_header = skb->network_header + hsize;
43 
44 	skb_reset_mac_len(skb);
45 	pskb_pull(skb, skb->mac_len + x->props.header_len);
46 }
47 
48 static void __xfrm_mode_beet_prep(struct xfrm_state *x, struct sk_buff *skb,
49 				  unsigned int hsize)
50 {
51 	struct xfrm_offload *xo = xfrm_offload(skb);
52 	int phlen = 0;
53 
54 	if (xo->flags & XFRM_GSO_SEGMENT)
55 		skb->transport_header = skb->network_header + hsize;
56 
57 	skb_reset_mac_len(skb);
58 	if (x->sel.family != AF_INET6) {
59 		phlen = IPV4_BEET_PHMAXLEN;
60 		if (x->outer_mode.family == AF_INET6)
61 			phlen += sizeof(struct ipv6hdr) - sizeof(struct iphdr);
62 	}
63 
64 	pskb_pull(skb, skb->mac_len + hsize + (x->props.header_len - phlen));
65 }
66 
67 /* Adjust pointers into the packet when IPsec is done at layer2 */
68 static void xfrm_outer_mode_prep(struct xfrm_state *x, struct sk_buff *skb)
69 {
70 	switch (x->outer_mode.encap) {
71 	case XFRM_MODE_TUNNEL:
72 		if (x->outer_mode.family == AF_INET)
73 			return __xfrm_mode_tunnel_prep(x, skb,
74 						       sizeof(struct iphdr));
75 		if (x->outer_mode.family == AF_INET6)
76 			return __xfrm_mode_tunnel_prep(x, skb,
77 						       sizeof(struct ipv6hdr));
78 		break;
79 	case XFRM_MODE_TRANSPORT:
80 		if (x->outer_mode.family == AF_INET)
81 			return __xfrm_transport_prep(x, skb,
82 						     sizeof(struct iphdr));
83 		if (x->outer_mode.family == AF_INET6)
84 			return __xfrm_transport_prep(x, skb,
85 						     sizeof(struct ipv6hdr));
86 		break;
87 	case XFRM_MODE_BEET:
88 		if (x->outer_mode.family == AF_INET)
89 			return __xfrm_mode_beet_prep(x, skb,
90 						     sizeof(struct iphdr));
91 		if (x->outer_mode.family == AF_INET6)
92 			return __xfrm_mode_beet_prep(x, skb,
93 						     sizeof(struct ipv6hdr));
94 		break;
95 	case XFRM_MODE_ROUTEOPTIMIZATION:
96 	case XFRM_MODE_IN_TRIGGER:
97 		break;
98 	}
99 }
100 
101 static inline bool xmit_xfrm_check_overflow(struct sk_buff *skb)
102 {
103 	struct xfrm_offload *xo = xfrm_offload(skb);
104 	__u32 seq = xo->seq.low;
105 
106 	seq += skb_shinfo(skb)->gso_segs;
107 	if (unlikely(seq < xo->seq.low))
108 		return true;
109 
110 	return false;
111 }
112 
113 struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again)
114 {
115 	int err;
116 	unsigned long flags;
117 	struct xfrm_state *x;
118 	struct softnet_data *sd;
119 	struct sk_buff *skb2, *nskb, *pskb = NULL;
120 	netdev_features_t esp_features = features;
121 	struct xfrm_offload *xo = xfrm_offload(skb);
122 	struct net_device *dev = skb->dev;
123 	struct sec_path *sp;
124 
125 	if (!xo || (xo->flags & XFRM_XMIT))
126 		return skb;
127 
128 	if (!(features & NETIF_F_HW_ESP))
129 		esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
130 
131 	sp = skb_sec_path(skb);
132 	x = sp->xvec[sp->len - 1];
133 	if (xo->flags & XFRM_GRO || x->xso.dir == XFRM_DEV_OFFLOAD_IN)
134 		return skb;
135 
136 	/* The packet was sent to HW IPsec packet offload engine,
137 	 * but to wrong device. Drop the packet, so it won't skip
138 	 * XFRM stack.
139 	 */
140 	if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET && x->xso.dev != dev) {
141 		kfree_skb(skb);
142 		dev_core_stats_tx_dropped_inc(dev);
143 		return NULL;
144 	}
145 
146 	/* This skb was already validated on the upper/virtual dev */
147 	if ((x->xso.dev != dev) && (x->xso.real_dev == dev))
148 		return skb;
149 
150 	local_irq_save(flags);
151 	sd = this_cpu_ptr(&softnet_data);
152 	err = !skb_queue_empty(&sd->xfrm_backlog);
153 	local_irq_restore(flags);
154 
155 	if (err) {
156 		*again = true;
157 		return skb;
158 	}
159 
160 	if (skb_is_gso(skb) && (unlikely(x->xso.dev != dev) ||
161 				unlikely(xmit_xfrm_check_overflow(skb)))) {
162 		struct sk_buff *segs;
163 
164 		/* Packet got rerouted, fixup features and segment it. */
165 		esp_features = esp_features & ~(NETIF_F_HW_ESP | NETIF_F_GSO_ESP);
166 
167 		segs = skb_gso_segment(skb, esp_features);
168 		if (IS_ERR(segs)) {
169 			kfree_skb(skb);
170 			dev_core_stats_tx_dropped_inc(dev);
171 			return NULL;
172 		} else {
173 			consume_skb(skb);
174 			skb = segs;
175 		}
176 	}
177 
178 	if (!skb->next) {
179 		esp_features |= skb->dev->gso_partial_features;
180 		xfrm_outer_mode_prep(x, skb);
181 
182 		xo->flags |= XFRM_DEV_RESUME;
183 
184 		err = x->type_offload->xmit(x, skb, esp_features);
185 		if (err) {
186 			if (err == -EINPROGRESS)
187 				return NULL;
188 
189 			XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
190 			kfree_skb(skb);
191 			return NULL;
192 		}
193 
194 		skb_push(skb, skb->data - skb_mac_header(skb));
195 
196 		return skb;
197 	}
198 
199 	skb_list_walk_safe(skb, skb2, nskb) {
200 		esp_features |= skb->dev->gso_partial_features;
201 		skb_mark_not_on_list(skb2);
202 
203 		xo = xfrm_offload(skb2);
204 		xo->flags |= XFRM_DEV_RESUME;
205 
206 		xfrm_outer_mode_prep(x, skb2);
207 
208 		err = x->type_offload->xmit(x, skb2, esp_features);
209 		if (!err) {
210 			skb2->next = nskb;
211 		} else if (err != -EINPROGRESS) {
212 			XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
213 			skb2->next = nskb;
214 			kfree_skb_list(skb2);
215 			return NULL;
216 		} else {
217 			if (skb == skb2)
218 				skb = nskb;
219 			else
220 				pskb->next = nskb;
221 
222 			continue;
223 		}
224 
225 		skb_push(skb2, skb2->data - skb_mac_header(skb2));
226 		pskb = skb2;
227 	}
228 
229 	return skb;
230 }
231 EXPORT_SYMBOL_GPL(validate_xmit_xfrm);
232 
233 int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
234 		       struct xfrm_user_offload *xuo,
235 		       struct netlink_ext_ack *extack)
236 {
237 	int err;
238 	struct dst_entry *dst;
239 	struct net_device *dev;
240 	struct xfrm_dev_offload *xso = &x->xso;
241 	xfrm_address_t *saddr;
242 	xfrm_address_t *daddr;
243 	bool is_packet_offload;
244 
245 	if (!x->type_offload) {
246 		NL_SET_ERR_MSG(extack, "Type doesn't support offload");
247 		return -EINVAL;
248 	}
249 
250 	/* We don't yet support UDP encapsulation and TFC padding. */
251 	if (x->encap || x->tfcpad) {
252 		NL_SET_ERR_MSG(extack, "Encapsulation and TFC padding can't be offloaded");
253 		return -EINVAL;
254 	}
255 
256 	if (xuo->flags &
257 	    ~(XFRM_OFFLOAD_IPV6 | XFRM_OFFLOAD_INBOUND | XFRM_OFFLOAD_PACKET)) {
258 		NL_SET_ERR_MSG(extack, "Unrecognized flags in offload request");
259 		return -EINVAL;
260 	}
261 
262 	is_packet_offload = xuo->flags & XFRM_OFFLOAD_PACKET;
263 	dev = dev_get_by_index(net, xuo->ifindex);
264 	if (!dev) {
265 		if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) {
266 			saddr = &x->props.saddr;
267 			daddr = &x->id.daddr;
268 		} else {
269 			saddr = &x->id.daddr;
270 			daddr = &x->props.saddr;
271 		}
272 
273 		dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr,
274 					x->props.family,
275 					xfrm_smark_get(0, x));
276 		if (IS_ERR(dst))
277 			return (is_packet_offload) ? -EINVAL : 0;
278 
279 		dev = dst->dev;
280 
281 		dev_hold(dev);
282 		dst_release(dst);
283 	}
284 
285 	if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) {
286 		xso->dev = NULL;
287 		dev_put(dev);
288 		return (is_packet_offload) ? -EINVAL : 0;
289 	}
290 
291 	if (!is_packet_offload && x->props.flags & XFRM_STATE_ESN &&
292 	    !dev->xfrmdev_ops->xdo_dev_state_advance_esn) {
293 		NL_SET_ERR_MSG(extack, "Device doesn't support offload with ESN");
294 		xso->dev = NULL;
295 		dev_put(dev);
296 		return -EINVAL;
297 	}
298 
299 	xso->dev = dev;
300 	netdev_tracker_alloc(dev, &xso->dev_tracker, GFP_ATOMIC);
301 	xso->real_dev = dev;
302 
303 	if (xuo->flags & XFRM_OFFLOAD_INBOUND)
304 		xso->dir = XFRM_DEV_OFFLOAD_IN;
305 	else
306 		xso->dir = XFRM_DEV_OFFLOAD_OUT;
307 
308 	if (is_packet_offload)
309 		xso->type = XFRM_DEV_OFFLOAD_PACKET;
310 	else
311 		xso->type = XFRM_DEV_OFFLOAD_CRYPTO;
312 
313 	err = dev->xfrmdev_ops->xdo_dev_state_add(x, extack);
314 	if (err) {
315 		xso->dev = NULL;
316 		xso->dir = 0;
317 		xso->real_dev = NULL;
318 		netdev_put(dev, &xso->dev_tracker);
319 		xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
320 
321 		/* User explicitly requested packet offload mode and configured
322 		 * policy in addition to the XFRM state. So be civil to users,
323 		 * and return an error instead of taking fallback path.
324 		 *
325 		 * This WARN_ON() can be seen as a documentation for driver
326 		 * authors to do not return -EOPNOTSUPP in packet offload mode.
327 		 */
328 		WARN_ON(err == -EOPNOTSUPP && is_packet_offload);
329 		if (err != -EOPNOTSUPP || is_packet_offload) {
330 			NL_SET_ERR_MSG_WEAK(extack, "Device failed to offload this state");
331 			return err;
332 		}
333 	}
334 
335 	return 0;
336 }
337 EXPORT_SYMBOL_GPL(xfrm_dev_state_add);
338 
339 int xfrm_dev_policy_add(struct net *net, struct xfrm_policy *xp,
340 			struct xfrm_user_offload *xuo, u8 dir,
341 			struct netlink_ext_ack *extack)
342 {
343 	struct xfrm_dev_offload *xdo = &xp->xdo;
344 	struct net_device *dev;
345 	int err;
346 
347 	if (!xuo->flags || xuo->flags & ~XFRM_OFFLOAD_PACKET) {
348 		/* We support only packet offload mode and it means
349 		 * that user must set XFRM_OFFLOAD_PACKET bit.
350 		 */
351 		NL_SET_ERR_MSG(extack, "Unrecognized flags in offload request");
352 		return -EINVAL;
353 	}
354 
355 	dev = dev_get_by_index(net, xuo->ifindex);
356 	if (!dev)
357 		return -EINVAL;
358 
359 	if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_policy_add) {
360 		xdo->dev = NULL;
361 		dev_put(dev);
362 		NL_SET_ERR_MSG(extack, "Policy offload is not supported");
363 		return -EINVAL;
364 	}
365 
366 	xdo->dev = dev;
367 	netdev_tracker_alloc(dev, &xdo->dev_tracker, GFP_ATOMIC);
368 	xdo->real_dev = dev;
369 	xdo->type = XFRM_DEV_OFFLOAD_PACKET;
370 	switch (dir) {
371 	case XFRM_POLICY_IN:
372 		xdo->dir = XFRM_DEV_OFFLOAD_IN;
373 		break;
374 	case XFRM_POLICY_OUT:
375 		xdo->dir = XFRM_DEV_OFFLOAD_OUT;
376 		break;
377 	case XFRM_POLICY_FWD:
378 		xdo->dir = XFRM_DEV_OFFLOAD_FWD;
379 		break;
380 	default:
381 		xdo->dev = NULL;
382 		netdev_put(dev, &xdo->dev_tracker);
383 		NL_SET_ERR_MSG(extack, "Unrecognized offload direction");
384 		return -EINVAL;
385 	}
386 
387 	err = dev->xfrmdev_ops->xdo_dev_policy_add(xp, extack);
388 	if (err) {
389 		xdo->dev = NULL;
390 		xdo->real_dev = NULL;
391 		xdo->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
392 		xdo->dir = 0;
393 		netdev_put(dev, &xdo->dev_tracker);
394 		NL_SET_ERR_MSG_WEAK(extack, "Device failed to offload this policy");
395 		return err;
396 	}
397 
398 	return 0;
399 }
400 EXPORT_SYMBOL_GPL(xfrm_dev_policy_add);
401 
402 bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
403 {
404 	int mtu;
405 	struct dst_entry *dst = skb_dst(skb);
406 	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
407 	struct net_device *dev = x->xso.dev;
408 
409 	if (!x->type_offload || x->encap)
410 		return false;
411 
412 	if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET ||
413 	    ((!dev || (dev == xfrm_dst_path(dst)->dev)) &&
414 	     !xdst->child->xfrm)) {
415 		mtu = xfrm_state_mtu(x, xdst->child_mtu_cached);
416 		if (skb->len <= mtu)
417 			goto ok;
418 
419 		if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
420 			goto ok;
421 	}
422 
423 	return false;
424 
425 ok:
426 	if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok)
427 		return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x);
428 
429 	return true;
430 }
431 EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok);
432 
433 void xfrm_dev_resume(struct sk_buff *skb)
434 {
435 	struct net_device *dev = skb->dev;
436 	int ret = NETDEV_TX_BUSY;
437 	struct netdev_queue *txq;
438 	struct softnet_data *sd;
439 	unsigned long flags;
440 
441 	rcu_read_lock();
442 	txq = netdev_core_pick_tx(dev, skb, NULL);
443 
444 	HARD_TX_LOCK(dev, txq, smp_processor_id());
445 	if (!netif_xmit_frozen_or_stopped(txq))
446 		skb = dev_hard_start_xmit(skb, dev, txq, &ret);
447 	HARD_TX_UNLOCK(dev, txq);
448 
449 	if (!dev_xmit_complete(ret)) {
450 		local_irq_save(flags);
451 		sd = this_cpu_ptr(&softnet_data);
452 		skb_queue_tail(&sd->xfrm_backlog, skb);
453 		raise_softirq_irqoff(NET_TX_SOFTIRQ);
454 		local_irq_restore(flags);
455 	}
456 	rcu_read_unlock();
457 }
458 EXPORT_SYMBOL_GPL(xfrm_dev_resume);
459 
460 void xfrm_dev_backlog(struct softnet_data *sd)
461 {
462 	struct sk_buff_head *xfrm_backlog = &sd->xfrm_backlog;
463 	struct sk_buff_head list;
464 	struct sk_buff *skb;
465 
466 	if (skb_queue_empty(xfrm_backlog))
467 		return;
468 
469 	__skb_queue_head_init(&list);
470 
471 	spin_lock(&xfrm_backlog->lock);
472 	skb_queue_splice_init(xfrm_backlog, &list);
473 	spin_unlock(&xfrm_backlog->lock);
474 
475 	while (!skb_queue_empty(&list)) {
476 		skb = __skb_dequeue(&list);
477 		xfrm_dev_resume(skb);
478 	}
479 
480 }
481 #endif
482 
483 static int xfrm_api_check(struct net_device *dev)
484 {
485 #ifdef CONFIG_XFRM_OFFLOAD
486 	if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
487 	    !(dev->features & NETIF_F_HW_ESP))
488 		return NOTIFY_BAD;
489 
490 	if ((dev->features & NETIF_F_HW_ESP) &&
491 	    (!(dev->xfrmdev_ops &&
492 	       dev->xfrmdev_ops->xdo_dev_state_add &&
493 	       dev->xfrmdev_ops->xdo_dev_state_delete)))
494 		return NOTIFY_BAD;
495 #else
496 	if (dev->features & (NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM))
497 		return NOTIFY_BAD;
498 #endif
499 
500 	return NOTIFY_DONE;
501 }
502 
503 static int xfrm_dev_down(struct net_device *dev)
504 {
505 	if (dev->features & NETIF_F_HW_ESP) {
506 		xfrm_dev_state_flush(dev_net(dev), dev, true);
507 		xfrm_dev_policy_flush(dev_net(dev), dev, true);
508 	}
509 
510 	return NOTIFY_DONE;
511 }
512 
513 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
514 {
515 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
516 
517 	switch (event) {
518 	case NETDEV_REGISTER:
519 		return xfrm_api_check(dev);
520 
521 	case NETDEV_FEAT_CHANGE:
522 		return xfrm_api_check(dev);
523 
524 	case NETDEV_DOWN:
525 	case NETDEV_UNREGISTER:
526 		return xfrm_dev_down(dev);
527 	}
528 	return NOTIFY_DONE;
529 }
530 
531 static struct notifier_block xfrm_dev_notifier = {
532 	.notifier_call	= xfrm_dev_event,
533 };
534 
535 void __init xfrm_dev_init(void)
536 {
537 	register_netdevice_notifier(&xfrm_dev_notifier);
538 }
539