xfrm_device.c 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * xfrm_device.c - IPsec device offloading code.
 *
 * Copyright (c) 2015 secunet Security Networks AG
 *
 * Author:
 * Steffen Klassert <steffen.klassert@secunet.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/errno.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <net/dst.h>
#include <net/xfrm.h>
#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)
27 28
{
	int err;
29
	unsigned long flags;
30
	struct xfrm_state *x;
31
	struct sk_buff *skb2;
32
	struct softnet_data *sd;
33
	netdev_features_t esp_features = features;
34
	struct xfrm_offload *xo = xfrm_offload(skb);
35
	struct sec_path *sp;
36

37 38
	if (!xo)
		return skb;
39

40 41
	if (!(features & NETIF_F_HW_ESP))
		esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
42

43 44
	sp = skb_sec_path(skb);
	x = sp->xvec[sp->len - 1];
45 46 47
	if (xo->flags & XFRM_GRO || x->xso.flags & XFRM_OFFLOAD_INBOUND)
		return skb;

48 49 50 51 52 53 54 55 56 57
	local_irq_save(flags);
	sd = this_cpu_ptr(&softnet_data);
	err = !skb_queue_empty(&sd->xfrm_backlog);
	local_irq_restore(flags);

	if (err) {
		*again = true;
		return skb;
	}

58 59 60
	if (skb_is_gso(skb)) {
		struct net_device *dev = skb->dev;

61
		if (unlikely(x->xso.dev != dev)) {
62 63 64 65 66 67 68 69 70
			struct sk_buff *segs;

			/* Packet got rerouted, fixup features and segment it. */
			esp_features = esp_features & ~(NETIF_F_HW_ESP
							| NETIF_F_GSO_ESP);

			segs = skb_gso_segment(skb, esp_features);
			if (IS_ERR(segs)) {
				kfree_skb(skb);
71
				atomic_long_inc(&dev->tx_dropped);
72 73 74 75 76 77 78 79 80
				return NULL;
			} else {
				consume_skb(skb);
				skb = segs;
			}
		}
	}

	if (!skb->next) {
81 82
		x->outer_mode->xmit(x, skb);

83 84
		xo->flags |= XFRM_DEV_RESUME;

85
		err = x->type_offload->xmit(x, skb, esp_features);
86
		if (err) {
87 88 89
			if (err == -EINPROGRESS)
				return NULL;

90
			XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
91 92
			kfree_skb(skb);
			return NULL;
93 94 95
		}

		skb_push(skb, skb->data - skb_mac_header(skb));
96 97

		return skb;
98 99
	}

100 101 102 103
	skb2 = skb;

	do {
		struct sk_buff *nskb = skb2->next;
104
		skb_mark_not_on_list(skb2);
105 106

		xo = xfrm_offload(skb2);
107
		xo->flags |= XFRM_DEV_RESUME;
108 109 110 111

		x->outer_mode->xmit(x, skb2);

		err = x->type_offload->xmit(x, skb2, esp_features);
112 113 114
		if (!err) {
			skb2->next = nskb;
		} else if (err != -EINPROGRESS) {
115 116 117 118
			XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
			skb2->next = nskb;
			kfree_skb_list(skb2);
			return NULL;
119 120 121 122 123 124
		} else {
			if (skb == skb2)
				skb = nskb;

			if (!skb)
				return NULL;
125

126 127
			goto skip_push;
		}
128 129 130

		skb_push(skb2, skb2->data - skb_mac_header(skb2));

131
skip_push:
132 133 134 135
		skb2 = nskb;
	} while (skb2);

	return skb;
136 137 138
}
EXPORT_SYMBOL_GPL(validate_xmit_xfrm);

139 140 141 142 143 144 145 146 147 148 149
int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
		       struct xfrm_user_offload *xuo)
{
	int err;
	struct dst_entry *dst;
	struct net_device *dev;
	struct xfrm_state_offload *xso = &x->xso;
	xfrm_address_t *saddr;
	xfrm_address_t *daddr;

	if (!x->type_offload)
150
		return -EINVAL;
151

152 153
	/* We don't yet support UDP encapsulation and TFC padding. */
	if (x->encap || x->tfcpad)
154
		return -EINVAL;
155 156 157 158 159 160 161 162 163 164 165

	dev = dev_get_by_index(net, xuo->ifindex);
	if (!dev) {
		if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) {
			saddr = &x->props.saddr;
			daddr = &x->id.daddr;
		} else {
			saddr = &x->id.daddr;
			daddr = &x->props.saddr;
		}

166
		dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr,
167 168
					x->props.family,
					xfrm_smark_get(0, x));
169 170 171 172 173 174 175 176 177 178
		if (IS_ERR(dst))
			return 0;

		dev = dst->dev;

		dev_hold(dev);
		dst_release(dst);
	}

	if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) {
179
		xso->dev = NULL;
180 181 182 183
		dev_put(dev);
		return 0;
	}

184 185 186 187 188 189 190
	if (x->props.flags & XFRM_STATE_ESN &&
	    !dev->xfrmdev_ops->xdo_dev_state_advance_esn) {
		xso->dev = NULL;
		dev_put(dev);
		return -EINVAL;
	}

191 192 193 194 195 196
	xso->dev = dev;
	xso->num_exthdrs = 1;
	xso->flags = xuo->flags;

	err = dev->xfrmdev_ops->xdo_dev_state_add(x);
	if (err) {
197 198
		xso->num_exthdrs = 0;
		xso->flags = 0;
199
		xso->dev = NULL;
200
		dev_put(dev);
201 202 203

		if (err != -EOPNOTSUPP)
			return err;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	}

	return 0;
}
EXPORT_SYMBOL_GPL(xfrm_dev_state_add);

bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
{
	int mtu;
	struct dst_entry *dst = skb_dst(skb);
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
	struct net_device *dev = x->xso.dev;

	if (!x->type_offload || x->encap)
		return false;

220 221
	if ((!dev || (dev == xfrm_dst_path(dst)->dev)) &&
	    (!xdst->child->xfrm && x->type->get_mtu)) {
222 223 224 225 226
		mtu = x->type->get_mtu(x, xdst->child_mtu_cached);

		if (skb->len <= mtu)
			goto ok;

227
		if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
228 229 230 231 232 233 234 235 236 237 238 239
			goto ok;
	}

	return false;

ok:
	if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok)
		return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x);

	return true;
}
EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

void xfrm_dev_resume(struct sk_buff *skb)
{
	struct net_device *dev = skb->dev;
	int ret = NETDEV_TX_BUSY;
	struct netdev_queue *txq;
	struct softnet_data *sd;
	unsigned long flags;

	rcu_read_lock();
	txq = netdev_pick_tx(dev, skb, NULL);

	HARD_TX_LOCK(dev, txq, smp_processor_id());
	if (!netif_xmit_frozen_or_stopped(txq))
		skb = dev_hard_start_xmit(skb, dev, txq, &ret);
	HARD_TX_UNLOCK(dev, txq);

	if (!dev_xmit_complete(ret)) {
		local_irq_save(flags);
		sd = this_cpu_ptr(&softnet_data);
		skb_queue_tail(&sd->xfrm_backlog, skb);
		raise_softirq_irqoff(NET_TX_SOFTIRQ);
		local_irq_restore(flags);
	}
	rcu_read_unlock();
}
EXPORT_SYMBOL_GPL(xfrm_dev_resume);

void xfrm_dev_backlog(struct softnet_data *sd)
{
	struct sk_buff_head *xfrm_backlog = &sd->xfrm_backlog;
	struct sk_buff_head list;
	struct sk_buff *skb;

	if (skb_queue_empty(xfrm_backlog))
		return;

	__skb_queue_head_init(&list);

	spin_lock(&xfrm_backlog->lock);
	skb_queue_splice_init(xfrm_backlog, &list);
	spin_unlock(&xfrm_backlog->lock);

	while (!skb_queue_empty(&list)) {
		skb = __skb_dequeue(&list);
		xfrm_dev_resume(skb);
	}

}
289
#endif
290

291
static int xfrm_api_check(struct net_device *dev)
292
{
293
#ifdef CONFIG_XFRM_OFFLOAD
294 295 296 297
	if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
	    !(dev->features & NETIF_F_HW_ESP))
		return NOTIFY_BAD;

298 299 300 301 302 303 304 305 306 307
	if ((dev->features & NETIF_F_HW_ESP) &&
	    (!(dev->xfrmdev_ops &&
	       dev->xfrmdev_ops->xdo_dev_state_add &&
	       dev->xfrmdev_ops->xdo_dev_state_delete)))
		return NOTIFY_BAD;
#else
	if (dev->features & (NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM))
		return NOTIFY_BAD;
#endif

308 309 310
	return NOTIFY_DONE;
}

311 312 313 314 315
static int xfrm_dev_register(struct net_device *dev)
{
	return xfrm_api_check(dev);
}

316 317
static int xfrm_dev_feat_change(struct net_device *dev)
{
318
	return xfrm_api_check(dev);
319 320 321 322
}

static int xfrm_dev_down(struct net_device *dev)
{
323
	if (dev->features & NETIF_F_HW_ESP)
324 325 326 327 328
		xfrm_dev_state_flush(dev_net(dev), dev, true);

	return NOTIFY_DONE;
}

329 330 331 332 333
static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
{
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);

	switch (event) {
334 335 336 337 338 339
	case NETDEV_REGISTER:
		return xfrm_dev_register(dev);

	case NETDEV_FEAT_CHANGE:
		return xfrm_dev_feat_change(dev);

340
	case NETDEV_DOWN:
341
		return xfrm_dev_down(dev);
342 343 344 345 346 347 348 349
	}
	return NOTIFY_DONE;
}

static struct notifier_block xfrm_dev_notifier = {
	.notifier_call	= xfrm_dev_event,
};

350
void __init xfrm_dev_init(void)
351 352 353
{
	register_netdevice_notifier(&xfrm_dev_notifier);
}