xfrm_input.c 5.4 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 85
	return 0;
}
EXPORT_SYMBOL(xfrm_parse_spi);

86 87 88 89 90 91 92 93 94 95 96 97 98
int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
{
	int err;

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

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

99 100 101 102 103
int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
{
	int err;
	__be32 seq;
	struct xfrm_state *x;
104
	xfrm_address_t *daddr;
105
	unsigned int family;
106
	int decaps = 0;
107 108 109 110 111
	int async = 0;

	/* A negative encap_type indicates async resumption. */
	if (encap_type < 0) {
		async = 1;
112
		x = xfrm_input_state(skb);
113 114 115
		seq = XFRM_SKB_CB(skb)->seq;
		goto resume;
	}
116

117 118 119 120 121
	/* 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);
122 123
		if (!sp) {
			XFRM_INC_STATS(LINUX_MIB_XFRMINERROR);
124
			goto drop;
125
		}
126 127 128 129 130
		if (skb->sp)
			secpath_put(skb->sp);
		skb->sp = sp;
	}

131 132
	daddr = (xfrm_address_t *)(skb_network_header(skb) +
				   XFRM_SPI_SKB_CB(skb)->daddroff);
133
	family = XFRM_SPI_SKB_CB(skb)->family;
134

135
	seq = 0;
136 137
	if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
		XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
138
		goto drop;
139
	}
140 141

	do {
142 143
		if (skb->sp->len == XFRM_MAX_DEPTH) {
			XFRM_INC_STATS(LINUX_MIB_XFRMINBUFFERERROR);
144
			goto drop;
145
		}
146

147
		x = xfrm_state_lookup(daddr, spi, nexthdr, family);
148 149
		if (x == NULL) {
			XFRM_INC_STATS(LINUX_MIB_XFRMINNOSTATES);
150
			goto drop;
151
		}
152

153 154
		skb->sp->xvec[skb->sp->len++] = x;

155
		spin_lock(&x->lock);
156 157
		if (unlikely(x->km.state != XFRM_STATE_VALID)) {
			XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEINVALID);
158
			goto drop_unlock;
159
		}
160

161 162
		if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
			XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEINVALID);
163
			goto drop_unlock;
164
		}
165

166 167
		if (x->props.replay_window && xfrm_replay_check(x, seq)) {
			XFRM_INC_STATS(LINUX_MIB_XFRMINSEQOUTOFWINDOW);
168
			goto drop_unlock;
169
		}
170

171 172
		if (xfrm_state_check_expire(x)) {
			XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEEXPIRED);
173
			goto drop_unlock;
174
		}
175

176 177
		spin_unlock(&x->lock);

178 179
		XFRM_SKB_CB(skb)->seq = seq;

180
		nexthdr = x->type->input(x, skb);
181

182 183 184 185
		if (nexthdr == -EINPROGRESS)
			return 0;

resume:
186
		spin_lock(&x->lock);
187 188 189
		if (nexthdr <= 0) {
			if (nexthdr == -EBADMSG)
				x->stats.integrity_failed++;
190
			XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEPROTOERROR);
191
			goto drop_unlock;
192
		}
193 194 195 196 197 198 199 200 201 202 203 204

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

		if (x->props.replay_window)
			xfrm_replay_advance(x, seq);

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

		spin_unlock(&x->lock);

205 206
		XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;

207 208
		if (x->inner_mode->input(x, skb)) {
			XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEMODEERROR);
209
			goto drop;
210
		}
211 212 213 214 215 216

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

217 218 219 220 221
		/*
		 * We need the inner address.  However, we only get here for
		 * transport mode so the outer address is identical.
		 */
		daddr = &x->id.daddr;
222
		family = x->outer_mode->afinfo->family;
223

224
		err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
225 226
		if (err < 0) {
			XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
227
			goto drop;
228
		}
229 230 231 232 233 234 235 236 237 238
	} while (!err);

	nf_reset(skb);

	if (decaps) {
		dst_release(skb->dst);
		skb->dst = NULL;
		netif_rx(skb);
		return 0;
	} else {
239
		return x->inner_mode->afinfo->transport_finish(skb, async);
240 241 242 243 244 245 246 247 248 249
	}

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

250 251 252 253 254 255
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 已提交
256 257 258 259
void __init xfrm_input_init(void)
{
	secpath_cachep = kmem_cache_create("secpath_cache",
					   sizeof(struct sec_path),
A
Alexey Dobriyan 已提交
260
					   0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
261
					   NULL);
L
Linus Torvalds 已提交
262
}