2 * xfrm_device.c - IPsec device offloading code.
4 * Copyright (c) 2015 secunet Security Networks AG
7 * Steffen Klassert <steffen.klassert@secunet.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
23 #include <linux/notifier.h>
25 #ifdef CONFIG_XFRM_OFFLOAD
26 struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again)
32 struct softnet_data *sd;
33 netdev_features_t esp_features = features;
34 struct xfrm_offload *xo = xfrm_offload(skb);
40 if (!(features & NETIF_F_HW_ESP))
41 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
43 sp = skb_sec_path(skb);
44 x = sp->xvec[sp->len - 1];
45 if (xo->flags & XFRM_GRO || x->xso.flags & XFRM_OFFLOAD_INBOUND)
48 local_irq_save(flags);
49 sd = this_cpu_ptr(&softnet_data);
50 err = !skb_queue_empty(&sd->xfrm_backlog);
51 local_irq_restore(flags);
58 if (skb_is_gso(skb)) {
59 struct net_device *dev = skb->dev;
61 if (unlikely(x->xso.dev != dev)) {
64 /* Packet got rerouted, fixup features and segment it. */
65 esp_features = esp_features & ~(NETIF_F_HW_ESP
68 segs = skb_gso_segment(skb, esp_features);
71 atomic_long_inc(&dev->tx_dropped);
81 x->outer_mode->xmit(x, skb);
83 xo->flags |= XFRM_DEV_RESUME;
85 err = x->type_offload->xmit(x, skb, esp_features);
87 if (err == -EINPROGRESS)
90 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
95 skb_push(skb, skb->data - skb_mac_header(skb));
103 struct sk_buff *nskb = skb2->next;
104 skb_mark_not_on_list(skb2);
106 xo = xfrm_offload(skb2);
107 xo->flags |= XFRM_DEV_RESUME;
109 x->outer_mode->xmit(x, skb2);
111 err = x->type_offload->xmit(x, skb2, esp_features);
114 } else if (err != -EINPROGRESS) {
115 XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
117 kfree_skb_list(skb2);
129 skb_push(skb2, skb2->data - skb_mac_header(skb2));
137 EXPORT_SYMBOL_GPL(validate_xmit_xfrm);
139 int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
140 struct xfrm_user_offload *xuo)
143 struct dst_entry *dst;
144 struct net_device *dev;
145 struct xfrm_state_offload *xso = &x->xso;
146 xfrm_address_t *saddr;
147 xfrm_address_t *daddr;
149 if (!x->type_offload)
152 /* We don't yet support UDP encapsulation and TFC padding. */
153 if (x->encap || x->tfcpad)
156 dev = dev_get_by_index(net, xuo->ifindex);
158 if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) {
159 saddr = &x->props.saddr;
160 daddr = &x->id.daddr;
162 saddr = &x->id.daddr;
163 daddr = &x->props.saddr;
166 dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr,
168 xfrm_smark_get(0, x));
178 if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) {
184 if (x->props.flags & XFRM_STATE_ESN &&
185 !dev->xfrmdev_ops->xdo_dev_state_advance_esn) {
192 xso->num_exthdrs = 1;
193 xso->flags = xuo->flags;
195 err = dev->xfrmdev_ops->xdo_dev_state_add(x);
197 xso->num_exthdrs = 0;
202 if (err != -EOPNOTSUPP)
208 EXPORT_SYMBOL_GPL(xfrm_dev_state_add);
210 bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
213 struct dst_entry *dst = skb_dst(skb);
214 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
215 struct net_device *dev = x->xso.dev;
217 if (!x->type_offload || x->encap)
220 if ((!dev || (dev == xfrm_dst_path(dst)->dev)) &&
221 (!xdst->child->xfrm && x->type->get_mtu)) {
222 mtu = x->type->get_mtu(x, xdst->child_mtu_cached);
227 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
234 if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok)
235 return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x);
239 EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok);
241 void xfrm_dev_resume(struct sk_buff *skb)
243 struct net_device *dev = skb->dev;
244 int ret = NETDEV_TX_BUSY;
245 struct netdev_queue *txq;
246 struct softnet_data *sd;
250 txq = netdev_pick_tx(dev, skb, NULL);
252 HARD_TX_LOCK(dev, txq, smp_processor_id());
253 if (!netif_xmit_frozen_or_stopped(txq))
254 skb = dev_hard_start_xmit(skb, dev, txq, &ret);
255 HARD_TX_UNLOCK(dev, txq);
257 if (!dev_xmit_complete(ret)) {
258 local_irq_save(flags);
259 sd = this_cpu_ptr(&softnet_data);
260 skb_queue_tail(&sd->xfrm_backlog, skb);
261 raise_softirq_irqoff(NET_TX_SOFTIRQ);
262 local_irq_restore(flags);
266 EXPORT_SYMBOL_GPL(xfrm_dev_resume);
268 void xfrm_dev_backlog(struct softnet_data *sd)
270 struct sk_buff_head *xfrm_backlog = &sd->xfrm_backlog;
271 struct sk_buff_head list;
274 if (skb_queue_empty(xfrm_backlog))
277 __skb_queue_head_init(&list);
279 spin_lock(&xfrm_backlog->lock);
280 skb_queue_splice_init(xfrm_backlog, &list);
281 spin_unlock(&xfrm_backlog->lock);
283 while (!skb_queue_empty(&list)) {
284 skb = __skb_dequeue(&list);
285 xfrm_dev_resume(skb);
291 static int xfrm_api_check(struct net_device *dev)
293 #ifdef CONFIG_XFRM_OFFLOAD
294 if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
295 !(dev->features & NETIF_F_HW_ESP))
298 if ((dev->features & NETIF_F_HW_ESP) &&
299 (!(dev->xfrmdev_ops &&
300 dev->xfrmdev_ops->xdo_dev_state_add &&
301 dev->xfrmdev_ops->xdo_dev_state_delete)))
304 if (dev->features & (NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM))
311 static int xfrm_dev_register(struct net_device *dev)
313 return xfrm_api_check(dev);
316 static int xfrm_dev_feat_change(struct net_device *dev)
318 return xfrm_api_check(dev);
321 static int xfrm_dev_down(struct net_device *dev)
323 if (dev->features & NETIF_F_HW_ESP)
324 xfrm_dev_state_flush(dev_net(dev), dev, true);
329 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
331 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
334 case NETDEV_REGISTER:
335 return xfrm_dev_register(dev);
337 case NETDEV_FEAT_CHANGE:
338 return xfrm_dev_feat_change(dev);
341 return xfrm_dev_down(dev);
346 static struct notifier_block xfrm_dev_notifier = {
347 .notifier_call = xfrm_dev_event,
350 void __init xfrm_dev_init(void)
352 register_netdevice_notifier(&xfrm_dev_notifier);