1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * xfrm_device.c - IPsec device offloading code.
5 * Copyright (c) 2015 secunet Security Networks AG
8 * Steffen Klassert <steffen.klassert@secunet.com>
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>
19 #include <linux/notifier.h>
21 #ifdef CONFIG_XFRM_OFFLOAD
22 static void __xfrm_transport_prep(struct xfrm_state
*x
, struct sk_buff
*skb
,
25 struct xfrm_offload
*xo
= xfrm_offload(skb
);
27 skb_reset_mac_len(skb
);
28 if (xo
->flags
& XFRM_GSO_SEGMENT
)
29 skb
->transport_header
-= x
->props
.header_len
;
31 pskb_pull(skb
, skb_transport_offset(skb
) + x
->props
.header_len
);
34 static void __xfrm_mode_tunnel_prep(struct xfrm_state
*x
, struct sk_buff
*skb
,
38 struct xfrm_offload
*xo
= xfrm_offload(skb
);
40 if (xo
->flags
& XFRM_GSO_SEGMENT
)
41 skb
->transport_header
= skb
->network_header
+ hsize
;
43 skb_reset_mac_len(skb
);
44 pskb_pull(skb
, skb
->mac_len
+ x
->props
.header_len
);
47 /* Adjust pointers into the packet when IPsec is done at layer2 */
48 static void xfrm_outer_mode_prep(struct xfrm_state
*x
, struct sk_buff
*skb
)
50 switch (x
->outer_mode
.encap
) {
51 case XFRM_MODE_TUNNEL
:
52 if (x
->outer_mode
.family
== AF_INET
)
53 return __xfrm_mode_tunnel_prep(x
, skb
,
54 sizeof(struct iphdr
));
55 if (x
->outer_mode
.family
== AF_INET6
)
56 return __xfrm_mode_tunnel_prep(x
, skb
,
57 sizeof(struct ipv6hdr
));
59 case XFRM_MODE_TRANSPORT
:
60 if (x
->outer_mode
.family
== AF_INET
)
61 return __xfrm_transport_prep(x
, skb
,
62 sizeof(struct iphdr
));
63 if (x
->outer_mode
.family
== AF_INET6
)
64 return __xfrm_transport_prep(x
, skb
,
65 sizeof(struct ipv6hdr
));
67 case XFRM_MODE_ROUTEOPTIMIZATION
:
68 case XFRM_MODE_IN_TRIGGER
:
74 struct sk_buff
*validate_xmit_xfrm(struct sk_buff
*skb
, netdev_features_t features
, bool *again
)
80 struct softnet_data
*sd
;
81 netdev_features_t esp_features
= features
;
82 struct xfrm_offload
*xo
= xfrm_offload(skb
);
85 if (!xo
|| (xo
->flags
& XFRM_XMIT
))
88 if (!(features
& NETIF_F_HW_ESP
))
89 esp_features
= features
& ~(NETIF_F_SG
| NETIF_F_CSUM_MASK
);
91 sp
= skb_sec_path(skb
);
92 x
= sp
->xvec
[sp
->len
- 1];
93 if (xo
->flags
& XFRM_GRO
|| x
->xso
.flags
& XFRM_OFFLOAD_INBOUND
)
96 local_irq_save(flags
);
97 sd
= this_cpu_ptr(&softnet_data
);
98 err
= !skb_queue_empty(&sd
->xfrm_backlog
);
99 local_irq_restore(flags
);
106 xo
->flags
|= XFRM_XMIT
;
108 if (skb_is_gso(skb
)) {
109 struct net_device
*dev
= skb
->dev
;
111 if (unlikely(x
->xso
.dev
!= dev
)) {
112 struct sk_buff
*segs
;
114 /* Packet got rerouted, fixup features and segment it. */
115 esp_features
= esp_features
& ~(NETIF_F_HW_ESP
118 segs
= skb_gso_segment(skb
, esp_features
);
121 atomic_long_inc(&dev
->tx_dropped
);
131 esp_features
|= skb
->dev
->gso_partial_features
;
132 xfrm_outer_mode_prep(x
, skb
);
134 xo
->flags
|= XFRM_DEV_RESUME
;
136 err
= x
->type_offload
->xmit(x
, skb
, esp_features
);
138 if (err
== -EINPROGRESS
)
141 XFRM_INC_STATS(xs_net(x
), LINUX_MIB_XFRMOUTSTATEPROTOERROR
);
146 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
154 struct sk_buff
*nskb
= skb2
->next
;
156 esp_features
|= skb
->dev
->gso_partial_features
;
157 skb_mark_not_on_list(skb2
);
159 xo
= xfrm_offload(skb2
);
160 xo
->flags
|= XFRM_DEV_RESUME
;
162 xfrm_outer_mode_prep(x
, skb2
);
164 err
= x
->type_offload
->xmit(x
, skb2
, esp_features
);
167 } else if (err
!= -EINPROGRESS
) {
168 XFRM_INC_STATS(xs_net(x
), LINUX_MIB_XFRMOUTSTATEPROTOERROR
);
170 kfree_skb_list(skb2
);
182 skb_push(skb2
, skb2
->data
- skb_mac_header(skb2
));
190 EXPORT_SYMBOL_GPL(validate_xmit_xfrm
);
192 int xfrm_dev_state_add(struct net
*net
, struct xfrm_state
*x
,
193 struct xfrm_user_offload
*xuo
)
196 struct dst_entry
*dst
;
197 struct net_device
*dev
;
198 struct xfrm_state_offload
*xso
= &x
->xso
;
199 xfrm_address_t
*saddr
;
200 xfrm_address_t
*daddr
;
202 if (!x
->type_offload
)
205 /* We don't yet support UDP encapsulation and TFC padding. */
206 if (x
->encap
|| x
->tfcpad
)
209 dev
= dev_get_by_index(net
, xuo
->ifindex
);
211 if (!(xuo
->flags
& XFRM_OFFLOAD_INBOUND
)) {
212 saddr
= &x
->props
.saddr
;
213 daddr
= &x
->id
.daddr
;
215 saddr
= &x
->id
.daddr
;
216 daddr
= &x
->props
.saddr
;
219 dst
= __xfrm_dst_lookup(net
, 0, 0, saddr
, daddr
,
221 xfrm_smark_get(0, x
));
231 if (!dev
->xfrmdev_ops
|| !dev
->xfrmdev_ops
->xdo_dev_state_add
) {
237 if (x
->props
.flags
& XFRM_STATE_ESN
&&
238 !dev
->xfrmdev_ops
->xdo_dev_state_advance_esn
) {
245 xso
->num_exthdrs
= 1;
246 xso
->flags
= xuo
->flags
;
248 err
= dev
->xfrmdev_ops
->xdo_dev_state_add(x
);
250 xso
->num_exthdrs
= 0;
255 if (err
!= -EOPNOTSUPP
)
261 EXPORT_SYMBOL_GPL(xfrm_dev_state_add
);
263 bool xfrm_dev_offload_ok(struct sk_buff
*skb
, struct xfrm_state
*x
)
266 struct dst_entry
*dst
= skb_dst(skb
);
267 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
268 struct net_device
*dev
= x
->xso
.dev
;
270 if (!x
->type_offload
|| x
->encap
)
273 if ((!dev
|| (dev
== xfrm_dst_path(dst
)->dev
)) &&
274 (!xdst
->child
->xfrm
)) {
275 mtu
= xfrm_state_mtu(x
, xdst
->child_mtu_cached
);
279 if (skb_is_gso(skb
) && skb_gso_validate_network_len(skb
, mtu
))
286 if (dev
&& dev
->xfrmdev_ops
&& dev
->xfrmdev_ops
->xdo_dev_offload_ok
)
287 return x
->xso
.dev
->xfrmdev_ops
->xdo_dev_offload_ok(skb
, x
);
291 EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok
);
293 void xfrm_dev_resume(struct sk_buff
*skb
)
295 struct net_device
*dev
= skb
->dev
;
296 int ret
= NETDEV_TX_BUSY
;
297 struct netdev_queue
*txq
;
298 struct softnet_data
*sd
;
302 txq
= netdev_core_pick_tx(dev
, skb
, NULL
);
304 HARD_TX_LOCK(dev
, txq
, smp_processor_id());
305 if (!netif_xmit_frozen_or_stopped(txq
))
306 skb
= dev_hard_start_xmit(skb
, dev
, txq
, &ret
);
307 HARD_TX_UNLOCK(dev
, txq
);
309 if (!dev_xmit_complete(ret
)) {
310 local_irq_save(flags
);
311 sd
= this_cpu_ptr(&softnet_data
);
312 skb_queue_tail(&sd
->xfrm_backlog
, skb
);
313 raise_softirq_irqoff(NET_TX_SOFTIRQ
);
314 local_irq_restore(flags
);
318 EXPORT_SYMBOL_GPL(xfrm_dev_resume
);
320 void xfrm_dev_backlog(struct softnet_data
*sd
)
322 struct sk_buff_head
*xfrm_backlog
= &sd
->xfrm_backlog
;
323 struct sk_buff_head list
;
326 if (skb_queue_empty(xfrm_backlog
))
329 __skb_queue_head_init(&list
);
331 spin_lock(&xfrm_backlog
->lock
);
332 skb_queue_splice_init(xfrm_backlog
, &list
);
333 spin_unlock(&xfrm_backlog
->lock
);
335 while (!skb_queue_empty(&list
)) {
336 skb
= __skb_dequeue(&list
);
337 xfrm_dev_resume(skb
);
343 static int xfrm_api_check(struct net_device
*dev
)
345 #ifdef CONFIG_XFRM_OFFLOAD
346 if ((dev
->features
& NETIF_F_HW_ESP_TX_CSUM
) &&
347 !(dev
->features
& NETIF_F_HW_ESP
))
350 if ((dev
->features
& NETIF_F_HW_ESP
) &&
351 (!(dev
->xfrmdev_ops
&&
352 dev
->xfrmdev_ops
->xdo_dev_state_add
&&
353 dev
->xfrmdev_ops
->xdo_dev_state_delete
)))
356 if (dev
->features
& (NETIF_F_HW_ESP
| NETIF_F_HW_ESP_TX_CSUM
))
363 static int xfrm_dev_register(struct net_device
*dev
)
365 return xfrm_api_check(dev
);
368 static int xfrm_dev_feat_change(struct net_device
*dev
)
370 return xfrm_api_check(dev
);
373 static int xfrm_dev_down(struct net_device
*dev
)
375 if (dev
->features
& NETIF_F_HW_ESP
)
376 xfrm_dev_state_flush(dev_net(dev
), dev
, true);
381 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
383 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
386 case NETDEV_REGISTER
:
387 return xfrm_dev_register(dev
);
389 case NETDEV_FEAT_CHANGE
:
390 return xfrm_dev_feat_change(dev
);
393 case NETDEV_UNREGISTER
:
394 return xfrm_dev_down(dev
);
399 static struct notifier_block xfrm_dev_notifier
= {
400 .notifier_call
= xfrm_dev_event
,
403 void __init
xfrm_dev_init(void)
405 register_netdevice_notifier(&xfrm_dev_notifier
);