xfrm_input.c 9.63 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6
/*
 * xfrm_input.c
 *
 * Changes:
 * 	YOSHIFUJI Hideaki @USAGI
 * 		Split up af-specific portion
7
 *
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10 11
 */

#include <linux/slab.h>
#include <linux/module.h>
12 13
#include <linux/netdevice.h>
#include <net/dst.h>
Linus Torvalds's avatar
Linus Torvalds committed
14 15
#include <net/ip.h>
#include <net/xfrm.h>
16 17
#include <net/ip_tunnels.h>
#include <net/ip6_tunnel.h>
Linus Torvalds's avatar
Linus Torvalds committed
18

19
static struct kmem_cache *secpath_cachep __read_mostly;
Linus Torvalds's avatar
Linus Torvalds committed
20

21
static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
22
static struct xfrm_input_afinfo const __rcu *xfrm_input_afinfo[AF_INET6 + 1];
23

24 25 26
static struct gro_cells gro_cells;
static struct net_device xfrm_napi_dev;

27
int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo)
28 29 30
{
	int err = 0;

31
	if (WARN_ON(afinfo->family >= ARRAY_SIZE(xfrm_input_afinfo)))
32
		return -EAFNOSUPPORT;
33

34 35
	spin_lock_bh(&xfrm_input_afinfo_lock);
	if (unlikely(xfrm_input_afinfo[afinfo->family] != NULL))
36
		err = -EEXIST;
37 38 39 40 41 42 43
	else
		rcu_assign_pointer(xfrm_input_afinfo[afinfo->family], afinfo);
	spin_unlock_bh(&xfrm_input_afinfo_lock);
	return err;
}
EXPORT_SYMBOL(xfrm_input_register_afinfo);

44
int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
{
	int err = 0;

	spin_lock_bh(&xfrm_input_afinfo_lock);
	if (likely(xfrm_input_afinfo[afinfo->family] != NULL)) {
		if (unlikely(xfrm_input_afinfo[afinfo->family] != afinfo))
			err = -EINVAL;
		else
			RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->family], NULL);
	}
	spin_unlock_bh(&xfrm_input_afinfo_lock);
	synchronize_rcu();
	return err;
}
EXPORT_SYMBOL(xfrm_input_unregister_afinfo);

61
static const struct xfrm_input_afinfo *xfrm_input_get_afinfo(unsigned int family)
62
{
63
	const struct xfrm_input_afinfo *afinfo;
64

65
	if (WARN_ON_ONCE(family >= ARRAY_SIZE(xfrm_input_afinfo)))
66
		return NULL;
67

68 69 70 71 72 73 74 75 76 77 78
	rcu_read_lock();
	afinfo = rcu_dereference(xfrm_input_afinfo[family]);
	if (unlikely(!afinfo))
		rcu_read_unlock();
	return afinfo;
}

static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
		       int err)
{
	int ret;
79
	const struct xfrm_input_afinfo *afinfo = xfrm_input_get_afinfo(family);
80 81 82 83 84

	if (!afinfo)
		return -EAFNOSUPPORT;

	ret = afinfo->callback(skb, protocol, err);
85
	rcu_read_unlock();
86 87 88 89

	return ret;
}

Linus Torvalds's avatar
Linus Torvalds committed
90 91 92 93
void __secpath_destroy(struct sec_path *sp)
{
	int i;
	for (i = 0; i < sp->len; i++)
94
		xfrm_state_put(sp->xvec[i]);
Linus Torvalds's avatar
Linus Torvalds committed
95 96 97 98 99 100 101 102
	kmem_cache_free(secpath_cachep, sp);
}
EXPORT_SYMBOL(__secpath_destroy);

struct sec_path *secpath_dup(struct sec_path *src)
{
	struct sec_path *sp;

103
	sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
104 105 106 107
	if (!sp)
		return NULL;

	sp->len = 0;
108 109
	sp->olen = 0;

Linus Torvalds's avatar
Linus Torvalds committed
110 111 112 113 114
	if (src) {
		int i;

		memcpy(sp, src, sizeof(*sp));
		for (i = 0; i < sp->len; i++)
115
			xfrm_state_hold(sp->xvec[i]);
Linus Torvalds's avatar
Linus Torvalds committed
116 117 118 119 120 121
	}
	atomic_set(&sp->refcnt, 1);
	return sp;
}
EXPORT_SYMBOL(secpath_dup);

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
int secpath_set(struct sk_buff *skb)
{
	struct sec_path *sp;

	/* Allocate new secpath or COW existing one. */
	if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
		sp = secpath_dup(skb->sp);
		if (!sp)
			return -ENOMEM;

		if (skb->sp)
			secpath_put(skb->sp);
		skb->sp = sp;
	}
	return 0;
}
EXPORT_SYMBOL(secpath_set);

Linus Torvalds's avatar
Linus Torvalds committed
140 141
/* Fetch spi and seq from ipsec header */

Al Viro's avatar
Al Viro committed
142
int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
Linus Torvalds's avatar
Linus Torvalds committed
143 144
{
	int offset, offset_seq;
145
	int hlen;
Linus Torvalds's avatar
Linus Torvalds committed
146 147 148

	switch (nexthdr) {
	case IPPROTO_AH:
149
		hlen = sizeof(struct ip_auth_hdr);
Linus Torvalds's avatar
Linus Torvalds committed
150 151 152 153
		offset = offsetof(struct ip_auth_hdr, spi);
		offset_seq = offsetof(struct ip_auth_hdr, seq_no);
		break;
	case IPPROTO_ESP:
154
		hlen = sizeof(struct ip_esp_hdr);
Linus Torvalds's avatar
Linus Torvalds committed
155 156 157 158 159 160
		offset = offsetof(struct ip_esp_hdr, spi);
		offset_seq = offsetof(struct ip_esp_hdr, seq_no);
		break;
	case IPPROTO_COMP:
		if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
			return -EINVAL;
161
		*spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
Linus Torvalds's avatar
Linus Torvalds committed
162 163 164 165 166 167
		*seq = 0;
		return 0;
	default:
		return 1;
	}

168
	if (!pskb_may_pull(skb, hlen))
Linus Torvalds's avatar
Linus Torvalds committed
169 170
		return -EINVAL;

171 172
	*spi = *(__be32 *)(skb_transport_header(skb) + offset);
	*seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
Linus Torvalds's avatar
Linus Torvalds committed
173 174
	return 0;
}
175
EXPORT_SYMBOL(xfrm_parse_spi);
Linus Torvalds's avatar
Linus Torvalds committed
176

177 178
int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
{
179
	struct xfrm_mode *inner_mode = x->inner_mode;
180 181 182 183 184 185
	int err;

	err = x->outer_mode->afinfo->extract_input(x, skb);
	if (err)
		return err;

186 187 188 189 190 191 192 193
	if (x->sel.family == AF_UNSPEC) {
		inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
		if (inner_mode == NULL)
			return -EAFNOSUPPORT;
	}

	skb->protocol = inner_mode->afinfo->eth_proto;
	return inner_mode->input2(x, skb);
194 195 196
}
EXPORT_SYMBOL(xfrm_prepare_input);

197 198
int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
{
199
	struct net *net = dev_net(skb->dev);
200 201
	int err;
	__be32 seq;
202
	__be32 seq_hi;
203
	struct xfrm_state *x = NULL;
204
	xfrm_address_t *daddr;
205
	struct xfrm_mode *inner_mode;
206
	u32 mark = skb->mark;
207
	unsigned int family;
208
	int decaps = 0;
209
	int async = 0;
210 211
	struct xfrm_offload *xo;
	bool xfrm_gro = false;
212 213

	if (encap_type < 0) {
214
		x = xfrm_input_state(skb);
215
		family = x->outer_mode->afinfo->family;
216 217 218 219 220 221 222 223 224 225 226

		/* An encap_type of -1 indicates async resumption. */
		if (encap_type == -1) {
			async = 1;
			seq = XFRM_SKB_CB(skb)->seq.input.low;
			goto resume;
		}
		/* encap_type < -1 indicates a GRO call. */
		encap_type = 0;
		seq = XFRM_SPI_SKB_CB(skb)->seq;
		goto lock;
227
	}
228

229 230 231 232
	daddr = (xfrm_address_t *)(skb_network_header(skb) +
				   XFRM_SPI_SKB_CB(skb)->daddroff);
	family = XFRM_SPI_SKB_CB(skb)->family;

233
	/* if tunnel is present override skb->mark value with tunnel i_key */
234 235 236
	switch (family) {
	case AF_INET:
		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
237
			mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
238 239 240
		break;
	case AF_INET6:
		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
241
			mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
242
		break;
243 244
	}

245 246 247 248
	err = secpath_set(skb);
	if (err) {
		XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
		goto drop;
249 250
	}

251
	seq = 0;
252
	if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
Alexey Dobriyan's avatar
Alexey Dobriyan committed
253
		XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
254
		goto drop;
255
	}
256 257

	do {
258
		if (skb->sp->len == XFRM_MAX_DEPTH) {
Alexey Dobriyan's avatar
Alexey Dobriyan committed
259
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
260
			goto drop;
261
		}
262

263
		x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
264
		if (x == NULL) {
Alexey Dobriyan's avatar
Alexey Dobriyan committed
265
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
266
			xfrm_audit_state_notfound(skb, family, spi, seq);
267
			goto drop;
268
		}
269

270 271
		skb->sp->xvec[skb->sp->len++] = x;

272
lock:
273
		spin_lock(&x->lock);
274

275
		if (unlikely(x->km.state != XFRM_STATE_VALID)) {
276 277 278 279 280
			if (x->km.state == XFRM_STATE_ACQ)
				XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
			else
				XFRM_INC_STATS(net,
					       LINUX_MIB_XFRMINSTATEINVALID);
281
			goto drop_unlock;
282
		}
283

284 285 286 287 288
		if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
			goto drop_unlock;
		}

289
		if (x->repl->check(x, skb, seq)) {
Alexey Dobriyan's avatar
Alexey Dobriyan committed
290
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
291
			goto drop_unlock;
292
		}
293

294
		if (xfrm_state_check_expire(x)) {
Alexey Dobriyan's avatar
Alexey Dobriyan committed
295
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
296
			goto drop_unlock;
297
		}
298

299 300
		spin_unlock(&x->lock);

301 302 303 304 305
		if (xfrm_tunnel_check(skb, x, family)) {
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
			goto drop;
		}

306 307
		seq_hi = htonl(xfrm_replay_seqhi(x, seq));

308
		XFRM_SKB_CB(skb)->seq.input.low = seq;
309
		XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
310

311
		skb_dst_force(skb);
312
		dev_hold(skb->dev);
313

314
		nexthdr = x->type->input(x, skb);
315

316 317 318
		if (nexthdr == -EINPROGRESS)
			return 0;
resume:
319 320
		dev_put(skb->dev);

321
		spin_lock(&x->lock);
322
		if (nexthdr <= 0) {
323 324 325
			if (nexthdr == -EBADMSG) {
				xfrm_audit_state_icvfail(x, skb,
							 x->type->proto);
326
				x->stats.integrity_failed++;
327
			}
Alexey Dobriyan's avatar
Alexey Dobriyan committed
328
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
329
			goto drop_unlock;
330
		}
331 332 333 334

		/* only the first xfrm gets the encap type */
		encap_type = 0;

335
		if (async && x->repl->recheck(x, skb, seq)) {
336 337 338 339
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
			goto drop_unlock;
		}

340
		x->repl->advance(x, seq);
341 342 343 344 345 346

		x->curlft.bytes += skb->len;
		x->curlft.packets++;

		spin_unlock(&x->lock);

347 348
		XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;

349 350 351 352
		inner_mode = x->inner_mode;

		if (x->sel.family == AF_UNSPEC) {
			inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
353 354
			if (inner_mode == NULL) {
				XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
355
				goto drop;
356
			}
357 358 359
		}

		if (inner_mode->input(x, skb)) {
Alexey Dobriyan's avatar
Alexey Dobriyan committed
360
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
361
			goto drop;
362
		}
363 364 365 366 367 368

		if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
			decaps = 1;
			break;
		}

369 370 371 372 373
		/*
		 * We need the inner address.  However, we only get here for
		 * transport mode so the outer address is identical.
		 */
		daddr = &x->id.daddr;
374
		family = x->outer_mode->afinfo->family;
375

376
		err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
377
		if (err < 0) {
Alexey Dobriyan's avatar
Alexey Dobriyan committed
378
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
379
			goto drop;
380
		}
381 382
	} while (!err);

383 384 385 386
	err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
	if (err)
		goto drop;

387 388 389
	nf_reset(skb);

	if (decaps) {
Eric Dumazet's avatar
Eric Dumazet committed
390
		skb_dst_drop(skb);
391
		gro_cells_receive(&gro_cells, skb);
392 393
		return 0;
	} else {
394 395 396 397
		xo = xfrm_offload(skb);
		if (xo)
			xfrm_gro = xo->flags & XFRM_GRO;

398
		err = x->inner_mode->afinfo->transport_finish(skb, xfrm_gro || async);
399 400 401 402 403 404 405
		if (xfrm_gro) {
			skb_dst_drop(skb);
			gro_cells_receive(&gro_cells, skb);
			return err;
		}

		return err;
406 407 408 409 410
	}

drop_unlock:
	spin_unlock(&x->lock);
drop:
411
	xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
412 413 414 415 416
	kfree_skb(skb);
	return 0;
}
EXPORT_SYMBOL(xfrm_input);

417 418 419 420 421 422
int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
{
	return xfrm_input(skb, nexthdr, 0, -1);
}
EXPORT_SYMBOL(xfrm_input_resume);

Linus Torvalds's avatar
Linus Torvalds committed
423 424
void __init xfrm_input_init(void)
{
425 426 427 428 429 430 431
	int err;

	init_dummy_netdev(&xfrm_napi_dev);
	err = gro_cells_init(&gro_cells, &xfrm_napi_dev);
	if (err)
		gro_cells.cells = NULL;

Linus Torvalds's avatar
Linus Torvalds committed
432 433
	secpath_cachep = kmem_cache_create("secpath_cache",
					   sizeof(struct sec_path),
Alexey Dobriyan's avatar
Alexey Dobriyan committed
434
					   0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
435
					   NULL);
Linus Torvalds's avatar
Linus Torvalds committed
436
}