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 pskb_pull(skb
, skb
->mac_len
+ hsize
+ x
->props
.header_len
);
30 if (xo
->flags
& XFRM_GSO_SEGMENT
) {
31 skb_reset_transport_header(skb
);
32 skb
->transport_header
-= x
->props
.header_len
;
36 static void __xfrm_mode_tunnel_prep(struct xfrm_state
*x
, struct sk_buff
*skb
,
40 struct xfrm_offload
*xo
= xfrm_offload(skb
);
42 if (xo
->flags
& XFRM_GSO_SEGMENT
)
43 skb
->transport_header
= skb
->network_header
+ hsize
;
45 skb_reset_mac_len(skb
);
46 pskb_pull(skb
, skb
->mac_len
+ x
->props
.header_len
);
49 /* Adjust pointers into the packet when IPsec is done at layer2 */
50 static void xfrm_outer_mode_prep(struct xfrm_state
*x
, struct sk_buff
*skb
)
52 switch (x
->outer_mode
.encap
) {
53 case XFRM_MODE_TUNNEL
:
54 if (x
->outer_mode
.family
== AF_INET
)
55 return __xfrm_mode_tunnel_prep(x
, skb
,
56 sizeof(struct iphdr
));
57 if (x
->outer_mode
.family
== AF_INET6
)
58 return __xfrm_mode_tunnel_prep(x
, skb
,
59 sizeof(struct ipv6hdr
));
61 case XFRM_MODE_TRANSPORT
:
62 if (x
->outer_mode
.family
== AF_INET
)
63 return __xfrm_transport_prep(x
, skb
,
64 sizeof(struct iphdr
));
65 if (x
->outer_mode
.family
== AF_INET6
)
66 return __xfrm_transport_prep(x
, skb
,
67 sizeof(struct ipv6hdr
));
69 case XFRM_MODE_ROUTEOPTIMIZATION
:
70 case XFRM_MODE_IN_TRIGGER
:
76 struct sk_buff
*validate_xmit_xfrm(struct sk_buff
*skb
, netdev_features_t features
, bool *again
)
81 struct sk_buff
*skb2
, *nskb
;
82 struct softnet_data
*sd
;
83 netdev_features_t esp_features
= features
;
84 struct xfrm_offload
*xo
= xfrm_offload(skb
);
90 if (!(features
& NETIF_F_HW_ESP
))
91 esp_features
= features
& ~(NETIF_F_SG
| NETIF_F_CSUM_MASK
);
93 sp
= skb_sec_path(skb
);
94 x
= sp
->xvec
[sp
->len
- 1];
95 if (xo
->flags
& XFRM_GRO
|| x
->xso
.flags
& XFRM_OFFLOAD_INBOUND
)
98 local_irq_save(flags
);
99 sd
= this_cpu_ptr(&softnet_data
);
100 err
= !skb_queue_empty(&sd
->xfrm_backlog
);
101 local_irq_restore(flags
);
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
));
151 skb_list_walk_safe(skb
, skb2
, nskb
) {
152 esp_features
|= skb
->dev
->gso_partial_features
;
153 skb_mark_not_on_list(skb2
);
155 xo
= xfrm_offload(skb2
);
156 xo
->flags
|= XFRM_DEV_RESUME
;
158 xfrm_outer_mode_prep(x
, skb2
);
160 err
= x
->type_offload
->xmit(x
, skb2
, esp_features
);
163 } else if (err
!= -EINPROGRESS
) {
164 XFRM_INC_STATS(xs_net(x
), LINUX_MIB_XFRMOUTSTATEPROTOERROR
);
166 kfree_skb_list(skb2
);
178 skb_push(skb2
, skb2
->data
- skb_mac_header(skb2
));
183 EXPORT_SYMBOL_GPL(validate_xmit_xfrm
);
185 int xfrm_dev_state_add(struct net
*net
, struct xfrm_state
*x
,
186 struct xfrm_user_offload
*xuo
)
189 struct dst_entry
*dst
;
190 struct net_device
*dev
;
191 struct xfrm_state_offload
*xso
= &x
->xso
;
192 xfrm_address_t
*saddr
;
193 xfrm_address_t
*daddr
;
195 if (!x
->type_offload
)
198 /* We don't yet support UDP encapsulation and TFC padding. */
199 if (x
->encap
|| x
->tfcpad
)
202 dev
= dev_get_by_index(net
, xuo
->ifindex
);
204 if (!(xuo
->flags
& XFRM_OFFLOAD_INBOUND
)) {
205 saddr
= &x
->props
.saddr
;
206 daddr
= &x
->id
.daddr
;
208 saddr
= &x
->id
.daddr
;
209 daddr
= &x
->props
.saddr
;
212 dst
= __xfrm_dst_lookup(net
, 0, 0, saddr
, daddr
,
214 xfrm_smark_get(0, x
));
224 if (!dev
->xfrmdev_ops
|| !dev
->xfrmdev_ops
->xdo_dev_state_add
) {
230 if (x
->props
.flags
& XFRM_STATE_ESN
&&
231 !dev
->xfrmdev_ops
->xdo_dev_state_advance_esn
) {
238 xso
->num_exthdrs
= 1;
239 xso
->flags
= xuo
->flags
;
241 err
= dev
->xfrmdev_ops
->xdo_dev_state_add(x
);
243 xso
->num_exthdrs
= 0;
248 if (err
!= -EOPNOTSUPP
)
254 EXPORT_SYMBOL_GPL(xfrm_dev_state_add
);
256 bool xfrm_dev_offload_ok(struct sk_buff
*skb
, struct xfrm_state
*x
)
259 struct dst_entry
*dst
= skb_dst(skb
);
260 struct xfrm_dst
*xdst
= (struct xfrm_dst
*)dst
;
261 struct net_device
*dev
= x
->xso
.dev
;
263 if (!x
->type_offload
|| x
->encap
)
266 if ((!dev
|| (dev
== xfrm_dst_path(dst
)->dev
)) &&
267 (!xdst
->child
->xfrm
)) {
268 mtu
= xfrm_state_mtu(x
, xdst
->child_mtu_cached
);
272 if (skb_is_gso(skb
) && skb_gso_validate_network_len(skb
, mtu
))
279 if (dev
&& dev
->xfrmdev_ops
&& dev
->xfrmdev_ops
->xdo_dev_offload_ok
)
280 return x
->xso
.dev
->xfrmdev_ops
->xdo_dev_offload_ok(skb
, x
);
284 EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok
);
286 void xfrm_dev_resume(struct sk_buff
*skb
)
288 struct net_device
*dev
= skb
->dev
;
289 int ret
= NETDEV_TX_BUSY
;
290 struct netdev_queue
*txq
;
291 struct softnet_data
*sd
;
295 txq
= netdev_core_pick_tx(dev
, skb
, NULL
);
297 HARD_TX_LOCK(dev
, txq
, smp_processor_id());
298 if (!netif_xmit_frozen_or_stopped(txq
))
299 skb
= dev_hard_start_xmit(skb
, dev
, txq
, &ret
);
300 HARD_TX_UNLOCK(dev
, txq
);
302 if (!dev_xmit_complete(ret
)) {
303 local_irq_save(flags
);
304 sd
= this_cpu_ptr(&softnet_data
);
305 skb_queue_tail(&sd
->xfrm_backlog
, skb
);
306 raise_softirq_irqoff(NET_TX_SOFTIRQ
);
307 local_irq_restore(flags
);
311 EXPORT_SYMBOL_GPL(xfrm_dev_resume
);
313 void xfrm_dev_backlog(struct softnet_data
*sd
)
315 struct sk_buff_head
*xfrm_backlog
= &sd
->xfrm_backlog
;
316 struct sk_buff_head list
;
319 if (skb_queue_empty(xfrm_backlog
))
322 __skb_queue_head_init(&list
);
324 spin_lock(&xfrm_backlog
->lock
);
325 skb_queue_splice_init(xfrm_backlog
, &list
);
326 spin_unlock(&xfrm_backlog
->lock
);
328 while (!skb_queue_empty(&list
)) {
329 skb
= __skb_dequeue(&list
);
330 xfrm_dev_resume(skb
);
336 static int xfrm_api_check(struct net_device
*dev
)
338 #ifdef CONFIG_XFRM_OFFLOAD
339 if ((dev
->features
& NETIF_F_HW_ESP_TX_CSUM
) &&
340 !(dev
->features
& NETIF_F_HW_ESP
))
343 if ((dev
->features
& NETIF_F_HW_ESP
) &&
344 (!(dev
->xfrmdev_ops
&&
345 dev
->xfrmdev_ops
->xdo_dev_state_add
&&
346 dev
->xfrmdev_ops
->xdo_dev_state_delete
)))
349 if (dev
->features
& (NETIF_F_HW_ESP
| NETIF_F_HW_ESP_TX_CSUM
))
356 static int xfrm_dev_register(struct net_device
*dev
)
358 return xfrm_api_check(dev
);
361 static int xfrm_dev_feat_change(struct net_device
*dev
)
363 return xfrm_api_check(dev
);
366 static int xfrm_dev_down(struct net_device
*dev
)
368 if (dev
->features
& NETIF_F_HW_ESP
)
369 xfrm_dev_state_flush(dev_net(dev
), dev
, true);
374 static int xfrm_dev_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
376 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
379 case NETDEV_REGISTER
:
380 return xfrm_dev_register(dev
);
382 case NETDEV_FEAT_CHANGE
:
383 return xfrm_dev_feat_change(dev
);
386 return xfrm_dev_down(dev
);
391 static struct notifier_block xfrm_dev_notifier
= {
392 .notifier_call
= xfrm_dev_event
,
395 void __init
xfrm_dev_init(void)
397 register_netdevice_notifier(&xfrm_dev_notifier
);