xfrm_input.c 6.1 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 16
#include <net/ip.h>
#include <net/xfrm.h>

17
static struct kmem_cache *secpath_cachep __read_mostly;
L
Linus Torvalds 已提交
18 19 20 21 22

void __secpath_destroy(struct sec_path *sp)
{
	int i;
	for (i = 0; i < sp->len; i++)
23
		xfrm_state_put(sp->xvec[i]);
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31
	kmem_cache_free(secpath_cachep, sp);
}
EXPORT_SYMBOL(__secpath_destroy);

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

32
	sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41
	if (!sp)
		return NULL;

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

		memcpy(sp, src, sizeof(*sp));
		for (i = 0; i < sp->len; i++)
42
			xfrm_state_hold(sp->xvec[i]);
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50
	}
	atomic_set(&sp->refcnt, 1);
	return sp;
}
EXPORT_SYMBOL(secpath_dup);

/* Fetch spi and seq from ipsec header */

A
Al Viro 已提交
51
int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
L
Linus Torvalds 已提交
52 53
{
	int offset, offset_seq;
54
	int hlen;
L
Linus Torvalds 已提交
55 56 57

	switch (nexthdr) {
	case IPPROTO_AH:
58
		hlen = sizeof(struct ip_auth_hdr);
L
Linus Torvalds 已提交
59 60 61 62
		offset = offsetof(struct ip_auth_hdr, spi);
		offset_seq = offsetof(struct ip_auth_hdr, seq_no);
		break;
	case IPPROTO_ESP:
63
		hlen = sizeof(struct ip_esp_hdr);
L
Linus Torvalds 已提交
64 65 66 67 68 69
		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;
70
		*spi = htonl(ntohs(*(__be16*)(skb_transport_header(skb) + 2)));
L
Linus Torvalds 已提交
71 72 73 74 75 76
		*seq = 0;
		return 0;
	default:
		return 1;
	}

77
	if (!pskb_may_pull(skb, hlen))
L
Linus Torvalds 已提交
78 79
		return -EINVAL;

80 81
	*spi = *(__be32*)(skb_transport_header(skb) + offset);
	*seq = *(__be32*)(skb_transport_header(skb) + offset_seq);
L
Linus Torvalds 已提交
82 83 84
	return 0;
}

85 86
int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
{
87
	struct xfrm_mode *inner_mode = x->inner_mode;
88 89 90 91 92 93
	int err;

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

94 95 96 97 98 99 100 101
	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);
102 103 104
}
EXPORT_SYMBOL(xfrm_prepare_input);

105 106
int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
{
A
Alexey Dobriyan 已提交
107
	struct net *net = dev_net(skb->dev);
108 109
	int err;
	__be32 seq;
110
	__be32 seq_hi;
111
	struct xfrm_state *x;
112
	xfrm_address_t *daddr;
113
	struct xfrm_mode *inner_mode;
114
	unsigned int family;
115
	int decaps = 0;
116 117 118 119 120
	int async = 0;

	/* A negative encap_type indicates async resumption. */
	if (encap_type < 0) {
		async = 1;
121
		x = xfrm_input_state(skb);
122
		seq = XFRM_SKB_CB(skb)->seq.input.low;
123 124
		goto resume;
	}
125

126 127 128 129 130
	/* 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);
131
		if (!sp) {
A
Alexey Dobriyan 已提交
132
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
133
			goto drop;
134
		}
135 136 137 138 139
		if (skb->sp)
			secpath_put(skb->sp);
		skb->sp = sp;
	}

140 141
	daddr = (xfrm_address_t *)(skb_network_header(skb) +
				   XFRM_SPI_SKB_CB(skb)->daddroff);
142
	family = XFRM_SPI_SKB_CB(skb)->family;
143

144
	seq = 0;
145
	if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
A
Alexey Dobriyan 已提交
146
		XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
147
		goto drop;
148
	}
149 150

	do {
151
		if (skb->sp->len == XFRM_MAX_DEPTH) {
A
Alexey Dobriyan 已提交
152
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
153
			goto drop;
154
		}
155

156
		x = xfrm_state_lookup(net, skb->mark, daddr, spi, nexthdr, family);
157
		if (x == NULL) {
A
Alexey Dobriyan 已提交
158
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
P
Paul Moore 已提交
159
			xfrm_audit_state_notfound(skb, family, spi, seq);
160
			goto drop;
161
		}
162

163 164
		skb->sp->xvec[skb->sp->len++] = x;

165
		spin_lock(&x->lock);
166
		if (unlikely(x->km.state != XFRM_STATE_VALID)) {
A
Alexey Dobriyan 已提交
167
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEINVALID);
168
			goto drop_unlock;
169
		}
170

171 172 173 174 175
		if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
			goto drop_unlock;
		}

176
		if (x->props.replay_window && x->repl->check(x, skb, seq)) {
A
Alexey Dobriyan 已提交
177
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
178
			goto drop_unlock;
179
		}
180

181
		if (xfrm_state_check_expire(x)) {
A
Alexey Dobriyan 已提交
182
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
183
			goto drop_unlock;
184
		}
185

186 187
		spin_unlock(&x->lock);

188 189
		seq_hi = htonl(xfrm_replay_seqhi(x, seq));

190
		XFRM_SKB_CB(skb)->seq.input.low = seq;
191
		XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
192

193
		nexthdr = x->type->input(x, skb);
194

195 196 197 198
		if (nexthdr == -EINPROGRESS)
			return 0;

resume:
199
		spin_lock(&x->lock);
200
		if (nexthdr <= 0) {
201 202 203
			if (nexthdr == -EBADMSG) {
				xfrm_audit_state_icvfail(x, skb,
							 x->type->proto);
204
				x->stats.integrity_failed++;
205
			}
A
Alexey Dobriyan 已提交
206
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
207
			goto drop_unlock;
208
		}
209 210 211 212

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

213
		x->repl->advance(x, seq);
214 215 216 217 218 219

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

		spin_unlock(&x->lock);

220 221
		XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;

222 223 224 225 226 227 228 229 230
		inner_mode = x->inner_mode;

		if (x->sel.family == AF_UNSPEC) {
			inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
			if (inner_mode == NULL)
				goto drop;
		}

		if (inner_mode->input(x, skb)) {
A
Alexey Dobriyan 已提交
231
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
232
			goto drop;
233
		}
234 235 236 237 238 239

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

240 241 242 243 244
		/*
		 * We need the inner address.  However, we only get here for
		 * transport mode so the outer address is identical.
		 */
		daddr = &x->id.daddr;
245
		family = x->outer_mode->afinfo->family;
246

247
		err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
248
		if (err < 0) {
A
Alexey Dobriyan 已提交
249
			XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
250
			goto drop;
251
		}
252 253 254 255 256
	} while (!err);

	nf_reset(skb);

	if (decaps) {
E
Eric Dumazet 已提交
257
		skb_dst_drop(skb);
258 259 260
		netif_rx(skb);
		return 0;
	} else {
261
		return x->inner_mode->afinfo->transport_finish(skb, async);
262 263 264 265 266 267 268 269 270 271
	}

drop_unlock:
	spin_unlock(&x->lock);
drop:
	kfree_skb(skb);
	return 0;
}
EXPORT_SYMBOL(xfrm_input);

272 273 274 275 276 277
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 已提交
278 279 280 281
void __init xfrm_input_init(void)
{
	secpath_cachep = kmem_cache_create("secpath_cache",
					   sizeof(struct sec_path),
A
Alexey Dobriyan 已提交
282
					   0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
283
					   NULL);
L
Linus Torvalds 已提交
284
}