blob: 189ef15acbbc3b8b09505435bebed1f14ca90c76 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Steffen Klassert21f42cc2017-04-14 10:05:53 +02002/*
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>
Steffen Klassert21f42cc2017-04-14 10:05:53 +02009 */
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/xfrm.h>
19#include <linux/notifier.h>
20
Hangbin Liub81f884a2017-06-01 14:57:56 +080021#ifdef CONFIG_XFRM_OFFLOAD
Florian Westphal303c5fa2019-03-29 21:16:26 +010022static void __xfrm_transport_prep(struct xfrm_state *x, struct sk_buff *skb,
23 unsigned int hsize)
24{
25 struct xfrm_offload *xo = xfrm_offload(skb);
26
27 skb_reset_mac_len(skb);
28 pskb_pull(skb, skb->mac_len + hsize + x->props.header_len);
29
30 if (xo->flags & XFRM_GSO_SEGMENT) {
31 skb_reset_transport_header(skb);
32 skb->transport_header -= x->props.header_len;
33 }
34}
35
36static void __xfrm_mode_tunnel_prep(struct xfrm_state *x, struct sk_buff *skb,
37 unsigned int hsize)
38
39{
40 struct xfrm_offload *xo = xfrm_offload(skb);
41
42 if (xo->flags & XFRM_GSO_SEGMENT)
43 skb->transport_header = skb->network_header + hsize;
44
45 skb_reset_mac_len(skb);
46 pskb_pull(skb, skb->mac_len + x->props.header_len);
47}
48
49/* Adjust pointers into the packet when IPsec is done at layer2 */
50static void xfrm_outer_mode_prep(struct xfrm_state *x, struct sk_buff *skb)
51{
Florian Westphalc9500d72019-03-29 21:16:32 +010052 switch (x->outer_mode.encap) {
Florian Westphal303c5fa2019-03-29 21:16:26 +010053 case XFRM_MODE_TUNNEL:
Florian Westphalc9500d72019-03-29 21:16:32 +010054 if (x->outer_mode.family == AF_INET)
Florian Westphal303c5fa2019-03-29 21:16:26 +010055 return __xfrm_mode_tunnel_prep(x, skb,
56 sizeof(struct iphdr));
Florian Westphalc9500d72019-03-29 21:16:32 +010057 if (x->outer_mode.family == AF_INET6)
Florian Westphal303c5fa2019-03-29 21:16:26 +010058 return __xfrm_mode_tunnel_prep(x, skb,
59 sizeof(struct ipv6hdr));
60 break;
61 case XFRM_MODE_TRANSPORT:
Florian Westphalc9500d72019-03-29 21:16:32 +010062 if (x->outer_mode.family == AF_INET)
Florian Westphal303c5fa2019-03-29 21:16:26 +010063 return __xfrm_transport_prep(x, skb,
64 sizeof(struct iphdr));
Florian Westphalc9500d72019-03-29 21:16:32 +010065 if (x->outer_mode.family == AF_INET6)
Florian Westphal303c5fa2019-03-29 21:16:26 +010066 return __xfrm_transport_prep(x, skb,
67 sizeof(struct ipv6hdr));
68 break;
69 case XFRM_MODE_ROUTEOPTIMIZATION:
70 case XFRM_MODE_IN_TRIGGER:
71 case XFRM_MODE_BEET:
72 break;
73 }
74}
75
Steffen Klassertf53c7232017-12-20 10:41:36 +010076struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again)
Steffen Klassertf6e27112017-04-14 10:07:28 +020077{
78 int err;
Steffen Klassertf53c7232017-12-20 10:41:36 +010079 unsigned long flags;
Steffen Klassertf6e27112017-04-14 10:07:28 +020080 struct xfrm_state *x;
Steffen Klassert3dca3f32017-12-20 10:41:31 +010081 struct sk_buff *skb2;
Steffen Klassertf53c7232017-12-20 10:41:36 +010082 struct softnet_data *sd;
Steffen Klassert3dca3f32017-12-20 10:41:31 +010083 netdev_features_t esp_features = features;
Steffen Klassertf6e27112017-04-14 10:07:28 +020084 struct xfrm_offload *xo = xfrm_offload(skb);
Florian Westphal2294be0f2018-12-18 17:15:20 +010085 struct sec_path *sp;
Steffen Klassertf6e27112017-04-14 10:07:28 +020086
Steffen Klassert3dca3f32017-12-20 10:41:31 +010087 if (!xo)
88 return skb;
Steffen Klassertf6e27112017-04-14 10:07:28 +020089
Steffen Klassert3dca3f32017-12-20 10:41:31 +010090 if (!(features & NETIF_F_HW_ESP))
91 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
Steffen Klassertf6e27112017-04-14 10:07:28 +020092
Florian Westphal2294be0f2018-12-18 17:15:20 +010093 sp = skb_sec_path(skb);
94 x = sp->xvec[sp->len - 1];
Steffen Klassert3dca3f32017-12-20 10:41:31 +010095 if (xo->flags & XFRM_GRO || x->xso.flags & XFRM_OFFLOAD_INBOUND)
96 return skb;
97
Steffen Klassertf53c7232017-12-20 10:41:36 +010098 local_irq_save(flags);
99 sd = this_cpu_ptr(&softnet_data);
100 err = !skb_queue_empty(&sd->xfrm_backlog);
101 local_irq_restore(flags);
102
103 if (err) {
104 *again = true;
105 return skb;
106 }
107
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100108 if (skb_is_gso(skb)) {
109 struct net_device *dev = skb->dev;
110
Shannon Nelsonfcb662d2018-06-26 14:19:10 -0700111 if (unlikely(x->xso.dev != dev)) {
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100112 struct sk_buff *segs;
113
114 /* Packet got rerouted, fixup features and segment it. */
115 esp_features = esp_features & ~(NETIF_F_HW_ESP
116 | NETIF_F_GSO_ESP);
117
118 segs = skb_gso_segment(skb, esp_features);
119 if (IS_ERR(segs)) {
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100120 kfree_skb(skb);
Steffen Klassertf53c7232017-12-20 10:41:36 +0100121 atomic_long_inc(&dev->tx_dropped);
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100122 return NULL;
123 } else {
124 consume_skb(skb);
125 skb = segs;
126 }
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100127 }
128 }
129
130 if (!skb->next) {
Boris Pismenny65fd2c22019-03-21 16:41:37 +0200131 esp_features |= skb->dev->gso_partial_features;
Florian Westphal303c5fa2019-03-29 21:16:26 +0100132 xfrm_outer_mode_prep(x, skb);
Steffen Klassertf6e27112017-04-14 10:07:28 +0200133
Steffen Klassertf53c7232017-12-20 10:41:36 +0100134 xo->flags |= XFRM_DEV_RESUME;
135
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100136 err = x->type_offload->xmit(x, skb, esp_features);
Steffen Klassertf6e27112017-04-14 10:07:28 +0200137 if (err) {
Steffen Klassertf53c7232017-12-20 10:41:36 +0100138 if (err == -EINPROGRESS)
139 return NULL;
140
Steffen Klassertf6e27112017-04-14 10:07:28 +0200141 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100142 kfree_skb(skb);
143 return NULL;
Steffen Klassertf6e27112017-04-14 10:07:28 +0200144 }
145
146 skb_push(skb, skb->data - skb_mac_header(skb));
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100147
148 return skb;
Steffen Klassertf6e27112017-04-14 10:07:28 +0200149 }
150
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100151 skb2 = skb;
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100152
153 do {
154 struct sk_buff *nskb = skb2->next;
Boris Pismenny65fd2c22019-03-21 16:41:37 +0200155
156 esp_features |= skb->dev->gso_partial_features;
David S. Millera8305bf2018-07-29 20:42:53 -0700157 skb_mark_not_on_list(skb2);
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100158
159 xo = xfrm_offload(skb2);
Steffen Klassertf53c7232017-12-20 10:41:36 +0100160 xo->flags |= XFRM_DEV_RESUME;
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100161
Florian Westphal303c5fa2019-03-29 21:16:26 +0100162 xfrm_outer_mode_prep(x, skb2);
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100163
164 err = x->type_offload->xmit(x, skb2, esp_features);
Steffen Klassertf53c7232017-12-20 10:41:36 +0100165 if (!err) {
166 skb2->next = nskb;
167 } else if (err != -EINPROGRESS) {
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100168 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
169 skb2->next = nskb;
170 kfree_skb_list(skb2);
171 return NULL;
Steffen Klassertf53c7232017-12-20 10:41:36 +0100172 } else {
173 if (skb == skb2)
174 skb = nskb;
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100175
Steffen Klassertf53c7232017-12-20 10:41:36 +0100176 if (!skb)
177 return NULL;
178
179 goto skip_push;
180 }
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100181
182 skb_push(skb2, skb2->data - skb_mac_header(skb2));
183
Steffen Klassertf53c7232017-12-20 10:41:36 +0100184skip_push:
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100185 skb2 = nskb;
186 } while (skb2);
187
188 return skb;
Steffen Klassertf6e27112017-04-14 10:07:28 +0200189}
190EXPORT_SYMBOL_GPL(validate_xmit_xfrm);
191
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200192int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
193 struct xfrm_user_offload *xuo)
194{
195 int err;
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;
201
202 if (!x->type_offload)
Ilan Tayariffdb5212017-08-01 12:49:08 +0300203 return -EINVAL;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200204
Yossef Efraim50bd8702018-01-14 11:39:10 +0200205 /* We don't yet support UDP encapsulation and TFC padding. */
206 if (x->encap || x->tfcpad)
Yossef Efraim43024b92017-11-28 11:49:29 +0200207 return -EINVAL;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200208
209 dev = dev_get_by_index(net, xuo->ifindex);
210 if (!dev) {
211 if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) {
212 saddr = &x->props.saddr;
213 daddr = &x->id.daddr;
214 } else {
215 saddr = &x->id.daddr;
216 daddr = &x->props.saddr;
217 }
218
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900219 dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr,
Steffen Klassert9b42c1f2018-06-12 12:44:26 +0200220 x->props.family,
221 xfrm_smark_get(0, x));
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200222 if (IS_ERR(dst))
223 return 0;
224
225 dev = dst->dev;
226
227 dev_hold(dev);
228 dst_release(dst);
229 }
230
231 if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) {
Steffen Klassert67a63382017-09-04 10:59:55 +0200232 xso->dev = NULL;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200233 dev_put(dev);
234 return 0;
235 }
236
Yossef Efraim50bd8702018-01-14 11:39:10 +0200237 if (x->props.flags & XFRM_STATE_ESN &&
238 !dev->xfrmdev_ops->xdo_dev_state_advance_esn) {
239 xso->dev = NULL;
240 dev_put(dev);
241 return -EINVAL;
242 }
243
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200244 xso->dev = dev;
245 xso->num_exthdrs = 1;
246 xso->flags = xuo->flags;
247
248 err = dev->xfrmdev_ops->xdo_dev_state_add(x);
249 if (err) {
Shannon Nelson4a132092018-08-22 14:38:10 -0700250 xso->num_exthdrs = 0;
251 xso->flags = 0;
Aviad Yehezkelaa5dd6f2018-01-18 15:41:51 +0200252 xso->dev = NULL;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200253 dev_put(dev);
Shannon Nelson4a132092018-08-22 14:38:10 -0700254
255 if (err != -EOPNOTSUPP)
256 return err;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200257 }
258
259 return 0;
260}
261EXPORT_SYMBOL_GPL(xfrm_dev_state_add);
262
263bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
264{
265 int mtu;
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;
269
270 if (!x->type_offload || x->encap)
271 return false;
272
Shannon Nelsonfcb662d2018-06-26 14:19:10 -0700273 if ((!dev || (dev == xfrm_dst_path(dst)->dev)) &&
Florian Westphalc7b37c72019-06-24 22:04:48 +0200274 (!xdst->child->xfrm)) {
275 mtu = xfrm_state_mtu(x, xdst->child_mtu_cached);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200276 if (skb->len <= mtu)
277 goto ok;
278
Daniel Axtens779b7932018-03-01 17:13:37 +1100279 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200280 goto ok;
281 }
282
283 return false;
284
285ok:
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);
288
289 return true;
290}
291EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok);
Steffen Klassertf53c7232017-12-20 10:41:36 +0100292
293void xfrm_dev_resume(struct sk_buff *skb)
294{
295 struct net_device *dev = skb->dev;
296 int ret = NETDEV_TX_BUSY;
297 struct netdev_queue *txq;
298 struct softnet_data *sd;
299 unsigned long flags;
300
301 rcu_read_lock();
Paolo Abeni4bd97d52019-03-20 11:02:04 +0100302 txq = netdev_core_pick_tx(dev, skb, NULL);
Steffen Klassertf53c7232017-12-20 10:41:36 +0100303
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);
308
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);
315 }
316 rcu_read_unlock();
317}
318EXPORT_SYMBOL_GPL(xfrm_dev_resume);
319
320void xfrm_dev_backlog(struct softnet_data *sd)
321{
322 struct sk_buff_head *xfrm_backlog = &sd->xfrm_backlog;
323 struct sk_buff_head list;
324 struct sk_buff *skb;
325
326 if (skb_queue_empty(xfrm_backlog))
327 return;
328
329 __skb_queue_head_init(&list);
330
331 spin_lock(&xfrm_backlog->lock);
332 skb_queue_splice_init(xfrm_backlog, &list);
333 spin_unlock(&xfrm_backlog->lock);
334
335 while (!skb_queue_empty(&list)) {
336 skb = __skb_dequeue(&list);
337 xfrm_dev_resume(skb);
338 }
339
340}
Hangbin Liub81f884a2017-06-01 14:57:56 +0800341#endif
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200342
Shannon Nelson92a23202017-12-19 15:35:48 -0800343static int xfrm_api_check(struct net_device *dev)
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200344{
Shannon Nelson92a23202017-12-19 15:35:48 -0800345#ifdef CONFIG_XFRM_OFFLOAD
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200346 if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
347 !(dev->features & NETIF_F_HW_ESP))
348 return NOTIFY_BAD;
349
Shannon Nelson92a23202017-12-19 15:35:48 -0800350 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)))
354 return NOTIFY_BAD;
355#else
356 if (dev->features & (NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM))
357 return NOTIFY_BAD;
358#endif
359
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200360 return NOTIFY_DONE;
361}
362
Shannon Nelson92a23202017-12-19 15:35:48 -0800363static int xfrm_dev_register(struct net_device *dev)
364{
365 return xfrm_api_check(dev);
366}
367
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200368static int xfrm_dev_feat_change(struct net_device *dev)
369{
Shannon Nelson92a23202017-12-19 15:35:48 -0800370 return xfrm_api_check(dev);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200371}
372
373static int xfrm_dev_down(struct net_device *dev)
374{
Ilan Tayari2c1497b2017-05-08 10:30:18 +0300375 if (dev->features & NETIF_F_HW_ESP)
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200376 xfrm_dev_state_flush(dev_net(dev), dev, true);
377
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200378 return NOTIFY_DONE;
379}
380
Steffen Klassert21f42cc2017-04-14 10:05:53 +0200381static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
382{
383 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
384
385 switch (event) {
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200386 case NETDEV_REGISTER:
387 return xfrm_dev_register(dev);
388
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200389 case NETDEV_FEAT_CHANGE:
390 return xfrm_dev_feat_change(dev);
391
Steffen Klassert21f42cc2017-04-14 10:05:53 +0200392 case NETDEV_DOWN:
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200393 return xfrm_dev_down(dev);
Steffen Klassert21f42cc2017-04-14 10:05:53 +0200394 }
395 return NOTIFY_DONE;
396}
397
398static struct notifier_block xfrm_dev_notifier = {
399 .notifier_call = xfrm_dev_event,
400};
401
Kirill Tkhaie9a441b2018-03-29 17:03:25 +0300402void __init xfrm_dev_init(void)
Steffen Klassert21f42cc2017-04-14 10:05:53 +0200403{
404 register_netdevice_notifier(&xfrm_dev_notifier);
405}