xfrm_input.c 9.2 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 22 23
static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
static struct xfrm_input_afinfo __rcu *xfrm_input_afinfo[NPROTO];

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

27 28 29 30 31 32 33 34 35 36
int xfrm_input_register_afinfo(struct xfrm_input_afinfo *afinfo)
{
	int err = 0;

	if (unlikely(afinfo == NULL))
		return -EINVAL;
	if (unlikely(afinfo->family >= NPROTO))
		return -EAFNOSUPPORT;
	spin_lock_bh(&xfrm_input_afinfo_lock);
	if (unlikely(xfrm_input_afinfo[afinfo->family] != NULL))
37
		err = -EEXIST;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	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);

int xfrm_input_unregister_afinfo(struct xfrm_input_afinfo *afinfo)
{
	int err = 0;

	if (unlikely(afinfo == NULL))
		return -EINVAL;
	if (unlikely(afinfo->family >= NPROTO))
		return -EAFNOSUPPORT;
	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);

static struct xfrm_input_afinfo *xfrm_input_get_afinfo(unsigned int family)
{
	struct xfrm_input_afinfo *afinfo;

	if (unlikely(family >= NPROTO))
		return NULL;
	rcu_read_lock();
	afinfo = rcu_dereference(xfrm_input_afinfo[family]);
	if (unlikely(!afinfo))
		rcu_read_unlock();
	return afinfo;
}

static void xfrm_input_put_afinfo(struct xfrm_input_afinfo *afinfo)
{
	rcu_read_unlock();
}

static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
		       int err)
{
	int ret;
	struct xfrm_input_afinfo *afinfo = xfrm_input_get_afinfo(family);

	if (!afinfo)
		return -EAFNOSUPPORT;

	ret = afinfo->callback(skb, protocol, err);
	xfrm_input_put_afinfo(afinfo);

	return ret;
}

L
Linus Torvalds 已提交
99 100 101 102
void __secpath_destroy(struct sec_path *sp)
{
	int i;
	for (i = 0; i < sp->len; i++)
103
		xfrm_state_put(sp->xvec[i]);
L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111
	kmem_cache_free(secpath_cachep, sp);
}
EXPORT_SYMBOL(__secpath_destroy);

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

112
	sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121
	if (!sp)
		return NULL;

	sp->len = 0;
	if (src) {
		int i;

		memcpy(sp, src, sizeof(*sp));
		for (i = 0; i < sp->len; i++)
122
			xfrm_state_hold(sp->xvec[i]);
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130
	}
	atomic_set(&sp->refcnt, 1);
	return sp;
}
EXPORT_SYMBOL(secpath_dup);

/* Fetch spi and seq from ipsec header */

A
Al Viro 已提交
131
int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
L
Linus Torvalds 已提交
132 133
{
	int offset, offset_seq;
134
	int hlen;
L
Linus Torvalds 已提交
135 136 137

	switch (nexthdr) {
	case IPPROTO_AH:
138
		hlen = sizeof(struct ip_auth_hdr);
L
Linus Torvalds 已提交
139 140 141 142
		offset = offsetof(struct ip_auth_hdr, spi);
		offset_seq = offsetof(struct ip_auth_hdr, seq_no);
		break;
	case IPPROTO_ESP:
143
		hlen = sizeof(struct ip_esp_hdr);
L
Linus Torvalds 已提交
144 145 146 147 148 149
		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;
150
		*spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
L
Linus Torvalds 已提交
151 152 153 154 155 156
		*seq = 0;
		return 0;
	default:
		return 1;
	}

157
	if (!pskb_may_pull(skb, hlen))
L
Linus Torvalds 已提交
158 159
		return -EINVAL;

160 161
	*spi = *(__be32 *)(skb_transport_header(skb) + offset);
	*seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
L
Linus Torvalds 已提交
162 163 164
	return 0;
}

165 166
int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
{
167
	struct xfrm_mode *inner_mode = x->inner_mode;
168 169 170 171 172 173
	int err;

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

174 175 176 177 178 179 180 181
	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);
182 183 184
}
EXPORT_SYMBOL(xfrm_prepare_input);

185 186
int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
{
A
Alexey Dobriyan 已提交
187
	struct net *net = dev_net(skb->dev);
188 189
	int err;
	__be32 seq;
190
	__be32 seq_hi;
191
	struct xfrm_state *x = NULL;
192
	xfrm_address_t *daddr;
193
	struct xfrm_mode *inner_mode;
194
	u32 mark = skb->mark;
195
	unsigned int family;
196
	int decaps = 0;
197 198 199 200 201
	int async = 0;

	/* A negative encap_type indicates async resumption. */
	if (encap_type < 0) {
		async = 1;
202
		x = xfrm_input_state(skb);
203
		seq = XFRM_SKB_CB(skb)->seq.input.low;
204
		family = x->outer_mode->afinfo->family;
205 206
		goto resume;
	}
207

208 209 210 211
	daddr = (xfrm_address_t *)(skb_network_header(skb) +
				   XFRM_SPI_SKB_CB(skb)->daddroff);
	family = XFRM_SPI_SKB_CB(skb)->family;

212
	/* if tunnel is present override skb->mark value with tunnel i_key */
213 214 215
	switch (family) {
	case AF_INET:
		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
216
			mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
217 218 219
		break;
	case AF_INET6:
		if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
220
			mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
221
		break;
222 223
	}

224 225 226 227 228
	/* Allocate new secpath or COW existing one. */
	if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
		struct sec_path *sp;

		sp = secpath_dup(skb->sp);
229
		if (!sp) {
A
Alexey Dobriyan 已提交
230
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
231
			goto drop;
232
		}
233 234 235 236 237
		if (skb->sp)
			secpath_put(skb->sp);
		skb->sp = sp;
	}

238
	seq = 0;
239
	if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
A
Alexey Dobriyan 已提交
240
		XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
241
		goto drop;
242
	}
243 244

	do {
245
		if (skb->sp->len == XFRM_MAX_DEPTH) {
A
Alexey Dobriyan 已提交
246
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
247
			goto drop;
248
		}
249

250
		x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
251
		if (x == NULL) {
A
Alexey Dobriyan 已提交
252
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
P
Paul Moore 已提交
253
			xfrm_audit_state_notfound(skb, family, spi, seq);
254
			goto drop;
255
		}
256

257 258
		skb->sp->xvec[skb->sp->len++] = x;

259
		spin_lock(&x->lock);
260

261
		if (unlikely(x->km.state != XFRM_STATE_VALID)) {
L
Li RongQing 已提交
262 263 264 265 266
			if (x->km.state == XFRM_STATE_ACQ)
				XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
			else
				XFRM_INC_STATS(net,
					       LINUX_MIB_XFRMINSTATEINVALID);
267
			goto drop_unlock;
268
		}
269

270 271 272 273 274
		if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
			goto drop_unlock;
		}

275
		if (x->repl->check(x, skb, seq)) {
A
Alexey Dobriyan 已提交
276
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
277
			goto drop_unlock;
278
		}
279

280
		if (xfrm_state_check_expire(x)) {
A
Alexey Dobriyan 已提交
281
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
282
			goto drop_unlock;
283
		}
284

285 286
		spin_unlock(&x->lock);

287 288 289 290 291
		if (xfrm_tunnel_check(skb, x, family)) {
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
			goto drop;
		}

292 293
		seq_hi = htonl(xfrm_replay_seqhi(x, seq));

294
		XFRM_SKB_CB(skb)->seq.input.low = seq;
295
		XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
296

297
		skb_dst_force(skb);
298
		dev_hold(skb->dev);
299

300
		nexthdr = x->type->input(x, skb);
301

302 303 304
		if (nexthdr == -EINPROGRESS)
			return 0;
resume:
305 306
		dev_put(skb->dev);

307
		spin_lock(&x->lock);
308
		if (nexthdr <= 0) {
309 310 311
			if (nexthdr == -EBADMSG) {
				xfrm_audit_state_icvfail(x, skb,
							 x->type->proto);
312
				x->stats.integrity_failed++;
313
			}
A
Alexey Dobriyan 已提交
314
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
315
			goto drop_unlock;
316
		}
317 318 319 320

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

321
		if (async && x->repl->recheck(x, skb, seq)) {
322 323 324 325
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
			goto drop_unlock;
		}

326
		x->repl->advance(x, seq);
327 328 329 330 331 332

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

		spin_unlock(&x->lock);

333 334
		XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;

335 336 337 338
		inner_mode = x->inner_mode;

		if (x->sel.family == AF_UNSPEC) {
			inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
339 340
			if (inner_mode == NULL) {
				XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
341
				goto drop;
342
			}
343 344 345
		}

		if (inner_mode->input(x, skb)) {
A
Alexey Dobriyan 已提交
346
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
347
			goto drop;
348
		}
349 350 351 352 353 354

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

355 356 357 358 359
		/*
		 * We need the inner address.  However, we only get here for
		 * transport mode so the outer address is identical.
		 */
		daddr = &x->id.daddr;
360
		family = x->outer_mode->afinfo->family;
361

362
		err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
363
		if (err < 0) {
A
Alexey Dobriyan 已提交
364
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
365
			goto drop;
366
		}
367 368
	} while (!err);

369 370 371 372
	err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
	if (err)
		goto drop;

373 374 375
	nf_reset(skb);

	if (decaps) {
E
Eric Dumazet 已提交
376
		skb_dst_drop(skb);
377
		gro_cells_receive(&gro_cells, skb);
378 379
		return 0;
	} else {
380
		return x->inner_mode->afinfo->transport_finish(skb, async);
381 382 383 384 385
	}

drop_unlock:
	spin_unlock(&x->lock);
drop:
386
	xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
387 388 389 390 391
	kfree_skb(skb);
	return 0;
}
EXPORT_SYMBOL(xfrm_input);

392 393 394 395 396 397
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 已提交
398 399
void __init xfrm_input_init(void)
{
400 401 402 403 404 405 406
	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 已提交
407 408
	secpath_cachep = kmem_cache_create("secpath_cache",
					   sizeof(struct sec_path),
A
Alexey Dobriyan 已提交
409
					   0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
410
					   NULL);
L
Linus Torvalds 已提交
411
}