xfrm_input.c 10.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 * xfrm_input.c
 *
 * Changes:
 * 	YOSHIFUJI Hideaki @USAGI
 * 		Split up af-specific portion
7
 *
L
Linus Torvalds 已提交
8 9 10 11
 */

#include <linux/slab.h>
#include <linux/module.h>
12 13
#include <linux/netdevice.h>
#include <net/dst.h>
L
Linus Torvalds 已提交
14 15
#include <net/ip.h>
#include <net/xfrm.h>
16 17
#include <net/ip_tunnels.h>
#include <net/ip6_tunnel.h>
L
Linus Torvalds 已提交
18

19
static struct kmem_cache *secpath_cachep __read_mostly;
L
Linus Torvalds 已提交
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;
}

L
Linus Torvalds 已提交
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]);
L
Linus Torvalds 已提交
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);
L
Linus Torvalds 已提交
104 105 106 107
	if (!sp)
		return NULL;

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

110 111
	memset(sp->ovec, 0, sizeof(sp->ovec[XFRM_MAX_OFFLOAD_DEPTH]));

L
Linus Torvalds 已提交
112 113 114 115 116
	if (src) {
		int i;

		memcpy(sp, src, sizeof(*sp));
		for (i = 0; i < sp->len; i++)
117
			xfrm_state_hold(sp->xvec[i]);
L
Linus Torvalds 已提交
118
	}
119
	refcount_set(&sp->refcnt, 1);
L
Linus Torvalds 已提交
120 121 122 123
	return sp;
}
EXPORT_SYMBOL(secpath_dup);

124 125 126 127 128
int secpath_set(struct sk_buff *skb)
{
	struct sec_path *sp;

	/* Allocate new secpath or COW existing one. */
129
	if (!skb->sp || refcount_read(&skb->sp->refcnt) != 1) {
130 131 132 133 134 135 136 137 138 139 140 141
		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);

L
Linus Torvalds 已提交
142 143
/* Fetch spi and seq from ipsec header */

A
Al Viro 已提交
144
int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
L
Linus Torvalds 已提交
145 146
{
	int offset, offset_seq;
147
	int hlen;
L
Linus Torvalds 已提交
148 149 150

	switch (nexthdr) {
	case IPPROTO_AH:
151
		hlen = sizeof(struct ip_auth_hdr);
L
Linus Torvalds 已提交
152 153 154 155
		offset = offsetof(struct ip_auth_hdr, spi);
		offset_seq = offsetof(struct ip_auth_hdr, seq_no);
		break;
	case IPPROTO_ESP:
156
		hlen = sizeof(struct ip_esp_hdr);
L
Linus Torvalds 已提交
157 158 159 160 161 162
		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;
163
		*spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
L
Linus Torvalds 已提交
164 165 166 167 168 169
		*seq = 0;
		return 0;
	default:
		return 1;
	}

170
	if (!pskb_may_pull(skb, hlen))
L
Linus Torvalds 已提交
171 172
		return -EINVAL;

173 174
	*spi = *(__be32 *)(skb_transport_header(skb) + offset);
	*seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
L
Linus Torvalds 已提交
175 176
	return 0;
}
S
Steffen Klassert 已提交
177
EXPORT_SYMBOL(xfrm_parse_spi);
L
Linus Torvalds 已提交
178

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

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

188 189 190 191 192 193 194 195
	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);
196 197 198
}
EXPORT_SYMBOL(xfrm_prepare_input);

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

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

		/* An encap_type of -1 indicates async resumption. */
		if (encap_type == -1) {
			async = 1;
			seq = XFRM_SKB_CB(skb)->seq.input.low;
			goto resume;
		}
226

227 228 229
		/* encap_type < -1 indicates a GRO call. */
		encap_type = 0;
		seq = XFRM_SPI_SKB_CB(skb)->seq;
230

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		if (xo && (xo->flags & CRYPTO_DONE)) {
			crypto_done = true;
			x = xfrm_input_state(skb);
			family = XFRM_SPI_SKB_CB(skb)->family;

			if (!(xo->status & CRYPTO_SUCCESS)) {
				if (xo->status &
				    (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
				     CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
				     CRYPTO_TUNNEL_AH_AUTH_FAILED |
				     CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {

					xfrm_audit_state_icvfail(x, skb,
								 x->type->proto);
					x->stats.integrity_failed++;
					XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
					goto drop;
				}

				XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
251 252 253
				goto drop;
			}

254 255 256 257
			if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
				XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
				goto drop;
			}
258 259
		}

260
		goto lock;
261
	}
262

263 264 265 266
	daddr = (xfrm_address_t *)(skb_network_header(skb) +
				   XFRM_SPI_SKB_CB(skb)->daddroff);
	family = XFRM_SPI_SKB_CB(skb)->family;

267
	/* if tunnel is present override skb->mark value with tunnel i_key */
268 269 270
	switch (family) {
	case AF_INET:
		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
271
			mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
272 273 274
		break;
	case AF_INET6:
		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
275
			mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
276
		break;
277 278
	}

279 280 281 282
	err = secpath_set(skb);
	if (err) {
		XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
		goto drop;
283 284
	}

285
	seq = 0;
286
	if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
A
Alexey Dobriyan 已提交
287
		XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
288
		goto drop;
289
	}
290 291

	do {
292
		if (skb->sp->len == XFRM_MAX_DEPTH) {
A
Alexey Dobriyan 已提交
293
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
294
			goto drop;
295
		}
296

297
		x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
298
		if (x == NULL) {
A
Alexey Dobriyan 已提交
299
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
P
Paul Moore 已提交
300
			xfrm_audit_state_notfound(skb, family, spi, seq);
301
			goto drop;
302
		}
303

304 305
		skb->sp->xvec[skb->sp->len++] = x;

306
lock:
307
		spin_lock(&x->lock);
308

309
		if (unlikely(x->km.state != XFRM_STATE_VALID)) {
L
Li RongQing 已提交
310 311 312 313 314
			if (x->km.state == XFRM_STATE_ACQ)
				XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
			else
				XFRM_INC_STATS(net,
					       LINUX_MIB_XFRMINSTATEINVALID);
315
			goto drop_unlock;
316
		}
317

318 319 320 321 322
		if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
			goto drop_unlock;
		}

323
		if (x->repl->check(x, skb, seq)) {
A
Alexey Dobriyan 已提交
324
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
325
			goto drop_unlock;
326
		}
327

328
		if (xfrm_state_check_expire(x)) {
A
Alexey Dobriyan 已提交
329
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
330
			goto drop_unlock;
331
		}
332

333 334
		spin_unlock(&x->lock);

335 336 337 338 339
		if (xfrm_tunnel_check(skb, x, family)) {
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
			goto drop;
		}

340 341
		seq_hi = htonl(xfrm_replay_seqhi(x, seq));

342
		XFRM_SKB_CB(skb)->seq.input.low = seq;
343
		XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
344

345
		skb_dst_force(skb);
346
		dev_hold(skb->dev);
347

348 349 350 351
		if (crypto_done)
			nexthdr = x->type_offload->input_tail(x, skb);
		else
			nexthdr = x->type->input(x, skb);
352

353 354 355
		if (nexthdr == -EINPROGRESS)
			return 0;
resume:
356 357
		dev_put(skb->dev);

358
		spin_lock(&x->lock);
359
		if (nexthdr <= 0) {
360 361 362
			if (nexthdr == -EBADMSG) {
				xfrm_audit_state_icvfail(x, skb,
							 x->type->proto);
363
				x->stats.integrity_failed++;
364
			}
A
Alexey Dobriyan 已提交
365
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
366
			goto drop_unlock;
367
		}
368 369 370 371

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

372
		if (async && x->repl->recheck(x, skb, seq)) {
373 374 375 376
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
			goto drop_unlock;
		}

377
		x->repl->advance(x, seq);
378 379 380 381 382 383

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

		spin_unlock(&x->lock);

384 385
		XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;

386 387 388 389
		inner_mode = x->inner_mode;

		if (x->sel.family == AF_UNSPEC) {
			inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
390 391
			if (inner_mode == NULL) {
				XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
392
				goto drop;
393
			}
394 395 396
		}

		if (inner_mode->input(x, skb)) {
A
Alexey Dobriyan 已提交
397
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
398
			goto drop;
399
		}
400 401 402 403 404 405

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

406 407 408 409 410
		/*
		 * We need the inner address.  However, we only get here for
		 * transport mode so the outer address is identical.
		 */
		daddr = &x->id.daddr;
411
		family = x->outer_mode->afinfo->family;
412

413
		err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
414
		if (err < 0) {
A
Alexey Dobriyan 已提交
415
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
416
			goto drop;
417
		}
418 419
	} while (!err);

420 421 422 423
	err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
	if (err)
		goto drop;

424 425 426
	nf_reset(skb);

	if (decaps) {
E
Eric Dumazet 已提交
427
		skb_dst_drop(skb);
428
		gro_cells_receive(&gro_cells, skb);
429 430
		return 0;
	} else {
431 432 433 434
		xo = xfrm_offload(skb);
		if (xo)
			xfrm_gro = xo->flags & XFRM_GRO;

435
		err = x->inner_mode->afinfo->transport_finish(skb, xfrm_gro || async);
436 437 438 439 440 441 442
		if (xfrm_gro) {
			skb_dst_drop(skb);
			gro_cells_receive(&gro_cells, skb);
			return err;
		}

		return err;
443 444 445 446 447
	}

drop_unlock:
	spin_unlock(&x->lock);
drop:
448
	xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
449 450 451 452 453
	kfree_skb(skb);
	return 0;
}
EXPORT_SYMBOL(xfrm_input);

454 455 456 457 458 459
int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
{
	return xfrm_input(skb, nexthdr, 0, -1);
}
EXPORT_SYMBOL(xfrm_input_resume);

L
Linus Torvalds 已提交
460 461
void __init xfrm_input_init(void)
{
462 463 464 465 466 467 468
	int err;

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

L
Linus Torvalds 已提交
469 470
	secpath_cachep = kmem_cache_create("secpath_cache",
					   sizeof(struct sec_path),
A
Alexey Dobriyan 已提交
471
					   0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
472
					   NULL);
L
Linus Torvalds 已提交
473
}