xfrm_input.c 9.0 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 24 25 26 27 28 29 30 31 32 33
static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
static struct xfrm_input_afinfo __rcu *xfrm_input_afinfo[NPROTO];

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))
34
		err = -EEXIST;
35 36 37 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
	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 已提交
96 97 98 99
void __secpath_destroy(struct sec_path *sp)
{
	int i;
	for (i = 0; i < sp->len; i++)
100
		xfrm_state_put(sp->xvec[i]);
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108
	kmem_cache_free(secpath_cachep, sp);
}
EXPORT_SYMBOL(__secpath_destroy);

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

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

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

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

/* Fetch spi and seq from ipsec header */

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

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

154
	if (!pskb_may_pull(skb, hlen))
L
Linus Torvalds 已提交
155 156
		return -EINVAL;

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

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

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

171 172 173 174 175 176 177 178
	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);
179 180 181
}
EXPORT_SYMBOL(xfrm_prepare_input);

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

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

205 206 207 208
	daddr = (xfrm_address_t *)(skb_network_header(skb) +
				   XFRM_SPI_SKB_CB(skb)->daddroff);
	family = XFRM_SPI_SKB_CB(skb)->family;

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

221 222 223 224 225
	/* 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);
226
		if (!sp) {
A
Alexey Dobriyan 已提交
227
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
228
			goto drop;
229
		}
230 231 232 233 234
		if (skb->sp)
			secpath_put(skb->sp);
		skb->sp = sp;
	}

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

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

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

254 255
		skb->sp->xvec[skb->sp->len++] = x;

256
		spin_lock(&x->lock);
257

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

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

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

277
		if (xfrm_state_check_expire(x)) {
A
Alexey Dobriyan 已提交
278
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
279
			goto drop_unlock;
280
		}
281

282 283
		spin_unlock(&x->lock);

284 285 286 287 288
		if (xfrm_tunnel_check(skb, x, family)) {
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
			goto drop;
		}

289 290
		seq_hi = htonl(xfrm_replay_seqhi(x, seq));

291
		XFRM_SKB_CB(skb)->seq.input.low = seq;
292
		XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
293

294
		skb_dst_force(skb);
295
		dev_hold(skb->dev);
296

297
		nexthdr = x->type->input(x, skb);
298

299 300 301
		if (nexthdr == -EINPROGRESS)
			return 0;
resume:
302 303
		dev_put(skb->dev);

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

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

318
		if (async && x->repl->recheck(x, skb, seq)) {
319 320 321 322
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
			goto drop_unlock;
		}

323
		x->repl->advance(x, seq);
324 325 326 327 328 329

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

		spin_unlock(&x->lock);

330 331
		XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;

332 333 334 335
		inner_mode = x->inner_mode;

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

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

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

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

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

366 367 368 369
	err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
	if (err)
		goto drop;

370 371 372
	nf_reset(skb);

	if (decaps) {
E
Eric Dumazet 已提交
373
		skb_dst_drop(skb);
374 375 376
		netif_rx(skb);
		return 0;
	} else {
377
		return x->inner_mode->afinfo->transport_finish(skb, async);
378 379 380 381 382
	}

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

389 390 391 392 393 394
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 已提交
395 396 397 398
void __init xfrm_input_init(void)
{
	secpath_cachep = kmem_cache_create("secpath_cache",
					   sizeof(struct sec_path),
A
Alexey Dobriyan 已提交
399
					   0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
400
					   NULL);
L
Linus Torvalds 已提交
401
}