blob: 85d1d476461257b3e248bd870cd9f7217fa21657 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm_input.c
3 *
4 * Changes:
5 * YOSHIFUJI Hideaki @USAGI
6 * Split up af-specific portion
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/slab.h>
11#include <linux/module.h>
Herbert Xu716062f2007-11-13 21:44:23 -080012#include <linux/netdevice.h>
13#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <net/ip.h>
15#include <net/xfrm.h>
16
Christoph Lametere18b8902006-12-06 20:33:20 -080017static struct kmem_cache *secpath_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Steffen Klassert2f32b512014-03-14 07:28:07 +010019static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
20static struct xfrm_input_afinfo __rcu *xfrm_input_afinfo[NPROTO];
21
22int xfrm_input_register_afinfo(struct xfrm_input_afinfo *afinfo)
23{
24 int err = 0;
25
26 if (unlikely(afinfo == NULL))
27 return -EINVAL;
28 if (unlikely(afinfo->family >= NPROTO))
29 return -EAFNOSUPPORT;
30 spin_lock_bh(&xfrm_input_afinfo_lock);
31 if (unlikely(xfrm_input_afinfo[afinfo->family] != NULL))
32 err = -ENOBUFS;
33 else
34 rcu_assign_pointer(xfrm_input_afinfo[afinfo->family], afinfo);
35 spin_unlock_bh(&xfrm_input_afinfo_lock);
36 return err;
37}
38EXPORT_SYMBOL(xfrm_input_register_afinfo);
39
40int xfrm_input_unregister_afinfo(struct xfrm_input_afinfo *afinfo)
41{
42 int err = 0;
43
44 if (unlikely(afinfo == NULL))
45 return -EINVAL;
46 if (unlikely(afinfo->family >= NPROTO))
47 return -EAFNOSUPPORT;
48 spin_lock_bh(&xfrm_input_afinfo_lock);
49 if (likely(xfrm_input_afinfo[afinfo->family] != NULL)) {
50 if (unlikely(xfrm_input_afinfo[afinfo->family] != afinfo))
51 err = -EINVAL;
52 else
53 RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->family], NULL);
54 }
55 spin_unlock_bh(&xfrm_input_afinfo_lock);
56 synchronize_rcu();
57 return err;
58}
59EXPORT_SYMBOL(xfrm_input_unregister_afinfo);
60
61static struct xfrm_input_afinfo *xfrm_input_get_afinfo(unsigned int family)
62{
63 struct xfrm_input_afinfo *afinfo;
64
65 if (unlikely(family >= NPROTO))
66 return NULL;
67 rcu_read_lock();
68 afinfo = rcu_dereference(xfrm_input_afinfo[family]);
69 if (unlikely(!afinfo))
70 rcu_read_unlock();
71 return afinfo;
72}
73
74static void xfrm_input_put_afinfo(struct xfrm_input_afinfo *afinfo)
75{
76 rcu_read_unlock();
77}
78
79static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
80 int err)
81{
82 int ret;
83 struct xfrm_input_afinfo *afinfo = xfrm_input_get_afinfo(family);
84
85 if (!afinfo)
86 return -EAFNOSUPPORT;
87
88 ret = afinfo->callback(skb, protocol, err);
89 xfrm_input_put_afinfo(afinfo);
90
91 return ret;
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094void __secpath_destroy(struct sec_path *sp)
95{
96 int i;
97 for (i = 0; i < sp->len; i++)
Herbert Xudbe5b4a2006-04-01 00:54:16 -080098 xfrm_state_put(sp->xvec[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 kmem_cache_free(secpath_cachep, sp);
100}
101EXPORT_SYMBOL(__secpath_destroy);
102
103struct sec_path *secpath_dup(struct sec_path *src)
104{
105 struct sec_path *sp;
106
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800107 sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (!sp)
109 return NULL;
110
111 sp->len = 0;
112 if (src) {
113 int i;
114
115 memcpy(sp, src, sizeof(*sp));
116 for (i = 0; i < sp->len; i++)
Herbert Xudbe5b4a2006-04-01 00:54:16 -0800117 xfrm_state_hold(sp->xvec[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 atomic_set(&sp->refcnt, 1);
120 return sp;
121}
122EXPORT_SYMBOL(secpath_dup);
123
124/* Fetch spi and seq from ipsec header */
125
Al Viro6067b2b2006-09-27 18:47:59 -0700126int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 int offset, offset_seq;
Herbert Xu44072502007-10-17 21:30:34 -0700129 int hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 switch (nexthdr) {
132 case IPPROTO_AH:
Herbert Xu44072502007-10-17 21:30:34 -0700133 hlen = sizeof(struct ip_auth_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 offset = offsetof(struct ip_auth_hdr, spi);
135 offset_seq = offsetof(struct ip_auth_hdr, seq_no);
136 break;
137 case IPPROTO_ESP:
Herbert Xu44072502007-10-17 21:30:34 -0700138 hlen = sizeof(struct ip_esp_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 offset = offsetof(struct ip_esp_hdr, spi);
140 offset_seq = offsetof(struct ip_esp_hdr, seq_no);
141 break;
142 case IPPROTO_COMP:
143 if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
144 return -EINVAL;
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800145 *spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 *seq = 0;
147 return 0;
148 default:
149 return 1;
150 }
151
Herbert Xu44072502007-10-17 21:30:34 -0700152 if (!pskb_may_pull(skb, hlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return -EINVAL;
154
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800155 *spi = *(__be32 *)(skb_transport_header(skb) + offset);
156 *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return 0;
158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Herbert Xu227620e2007-11-13 21:41:28 -0800160int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
161{
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700162 struct xfrm_mode *inner_mode = x->inner_mode;
Herbert Xu227620e2007-11-13 21:41:28 -0800163 int err;
164
165 err = x->outer_mode->afinfo->extract_input(x, skb);
166 if (err)
167 return err;
168
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700169 if (x->sel.family == AF_UNSPEC) {
170 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
171 if (inner_mode == NULL)
172 return -EAFNOSUPPORT;
173 }
174
175 skb->protocol = inner_mode->afinfo->eth_proto;
176 return inner_mode->input2(x, skb);
Herbert Xu227620e2007-11-13 21:41:28 -0800177}
178EXPORT_SYMBOL(xfrm_prepare_input);
179
Herbert Xu716062f2007-11-13 21:44:23 -0800180int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
181{
Alexey Dobriyanbd235e32008-11-25 17:37:56 -0800182 struct net *net = dev_net(skb->dev);
Herbert Xu716062f2007-11-13 21:44:23 -0800183 int err;
184 __be32 seq;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000185 __be32 seq_hi;
Steffen Klassert33287152014-02-21 08:41:08 +0100186 struct xfrm_state *x = NULL;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800187 xfrm_address_t *daddr;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700188 struct xfrm_mode *inner_mode;
Herbert Xu2fcb45b2007-12-03 22:54:12 -0800189 unsigned int family;
Herbert Xu716062f2007-11-13 21:44:23 -0800190 int decaps = 0;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800191 int async = 0;
192
193 /* A negative encap_type indicates async resumption. */
194 if (encap_type < 0) {
195 async = 1;
Herbert Xu00501122007-12-11 01:53:43 -0800196 x = xfrm_input_state(skb);
Steffen Klassert1ce36442011-03-08 00:06:31 +0000197 seq = XFRM_SKB_CB(skb)->seq.input.low;
Steffen Klassert33287152014-02-21 08:41:08 +0100198 family = x->outer_mode->afinfo->family;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800199 goto resume;
200 }
Herbert Xu716062f2007-11-13 21:44:23 -0800201
Steffen Klassert33287152014-02-21 08:41:08 +0100202 daddr = (xfrm_address_t *)(skb_network_header(skb) +
203 XFRM_SPI_SKB_CB(skb)->daddroff);
204 family = XFRM_SPI_SKB_CB(skb)->family;
205
Herbert Xub2aa5e92007-11-13 21:44:55 -0800206 /* Allocate new secpath or COW existing one. */
207 if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
208 struct sec_path *sp;
209
210 sp = secpath_dup(skb->sp);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800211 if (!sp) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800212 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
Herbert Xub2aa5e92007-11-13 21:44:55 -0800213 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800214 }
Herbert Xub2aa5e92007-11-13 21:44:55 -0800215 if (skb->sp)
216 secpath_put(skb->sp);
217 skb->sp = sp;
218 }
219
Herbert Xu716062f2007-11-13 21:44:23 -0800220 seq = 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800221 if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800222 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800223 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800224 }
Herbert Xu716062f2007-11-13 21:44:23 -0800225
226 do {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800227 if (skb->sp->len == XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800228 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800229 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800230 }
Herbert Xu716062f2007-11-13 21:44:23 -0800231
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800232 x = xfrm_state_lookup(net, skb->mark, daddr, spi, nexthdr, family);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800233 if (x == NULL) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800234 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
Paul Mooreafeb14b2007-12-21 14:58:11 -0800235 xfrm_audit_state_notfound(skb, family, spi, seq);
Herbert Xu716062f2007-11-13 21:44:23 -0800236 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800237 }
Herbert Xu716062f2007-11-13 21:44:23 -0800238
Herbert Xub2aa5e92007-11-13 21:44:55 -0800239 skb->sp->xvec[skb->sp->len++] = x;
240
Steffen Klassert70be6c92014-02-21 08:41:09 +0100241 if (xfrm_tunnel_check(skb, x, family)) {
242 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
243 goto drop;
244 }
245
Herbert Xu716062f2007-11-13 21:44:23 -0800246 spin_lock(&x->lock);
Fan Du4c4d41f2013-06-06 10:15:54 +0800247 if (unlikely(x->km.state == XFRM_STATE_ACQ)) {
248 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
249 goto drop_unlock;
250 }
251
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800252 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800253 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEINVALID);
Herbert Xu716062f2007-11-13 21:44:23 -0800254 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800255 }
Herbert Xu716062f2007-11-13 21:44:23 -0800256
David S. Miller3de77cf2008-12-18 19:23:56 -0800257 if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
258 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
259 goto drop_unlock;
260 }
261
Steffen Klassert36ae0142011-03-28 19:45:52 +0000262 if (x->repl->check(x, skb, seq)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800263 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800264 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800265 }
Herbert Xu716062f2007-11-13 21:44:23 -0800266
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800267 if (xfrm_state_check_expire(x)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800268 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
Herbert Xu716062f2007-11-13 21:44:23 -0800269 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800270 }
Herbert Xu716062f2007-11-13 21:44:23 -0800271
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800272 spin_unlock(&x->lock);
273
Steffen Klassert2cd08462011-03-08 00:09:51 +0000274 seq_hi = htonl(xfrm_replay_seqhi(x, seq));
275
Steffen Klassert1ce36442011-03-08 00:06:31 +0000276 XFRM_SKB_CB(skb)->seq.input.low = seq;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000277 XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800278
Steffen Klassert3bc07322011-03-15 21:08:28 +0000279 skb_dst_force(skb);
280
Herbert Xu716062f2007-11-13 21:44:23 -0800281 nexthdr = x->type->input(x, skb);
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800282
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800283 if (nexthdr == -EINPROGRESS)
284 return 0;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800285resume:
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800286 spin_lock(&x->lock);
Herbert Xu668dc8a2007-12-16 15:55:02 -0800287 if (nexthdr <= 0) {
Herbert Xu9dd32452007-12-30 21:10:30 -0800288 if (nexthdr == -EBADMSG) {
289 xfrm_audit_state_icvfail(x, skb,
290 x->type->proto);
Herbert Xu668dc8a2007-12-16 15:55:02 -0800291 x->stats.integrity_failed++;
Herbert Xu9dd32452007-12-30 21:10:30 -0800292 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800293 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800294 goto drop_unlock;
Herbert Xu668dc8a2007-12-16 15:55:02 -0800295 }
Herbert Xu716062f2007-11-13 21:44:23 -0800296
Herbert Xu716062f2007-11-13 21:44:23 -0800297 /* only the first xfrm gets the encap type */
298 encap_type = 0;
299
Steffen Klassert3b59df42012-09-04 00:03:29 +0000300 if (async && x->repl->recheck(x, skb, seq)) {
Steffen Klassertbcf66bf2011-09-20 23:38:58 +0000301 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
302 goto drop_unlock;
303 }
304
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000305 x->repl->advance(x, seq);
Herbert Xu716062f2007-11-13 21:44:23 -0800306
307 x->curlft.bytes += skb->len;
308 x->curlft.packets++;
309
310 spin_unlock(&x->lock);
311
Herbert Xu60d5fcf2007-11-19 18:47:58 -0800312 XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
313
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700314 inner_mode = x->inner_mode;
315
316 if (x->sel.family == AF_UNSPEC) {
317 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
318 if (inner_mode == NULL)
319 goto drop;
320 }
321
322 if (inner_mode->input(x, skb)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800323 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800324 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800325 }
Herbert Xu716062f2007-11-13 21:44:23 -0800326
327 if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
328 decaps = 1;
329 break;
330 }
331
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800332 /*
333 * We need the inner address. However, we only get here for
334 * transport mode so the outer address is identical.
335 */
336 daddr = &x->id.daddr;
Herbert Xu2fcb45b2007-12-03 22:54:12 -0800337 family = x->outer_mode->afinfo->family;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800338
Herbert Xu716062f2007-11-13 21:44:23 -0800339 err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800340 if (err < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800341 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800342 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800343 }
Herbert Xu716062f2007-11-13 21:44:23 -0800344 } while (!err);
345
Steffen Klassert33287152014-02-21 08:41:08 +0100346 err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
347 if (err)
348 goto drop;
349
Herbert Xu716062f2007-11-13 21:44:23 -0800350 nf_reset(skb);
351
352 if (decaps) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000353 skb_dst_drop(skb);
Herbert Xu716062f2007-11-13 21:44:23 -0800354 netif_rx(skb);
355 return 0;
356 } else {
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800357 return x->inner_mode->afinfo->transport_finish(skb, async);
Herbert Xu716062f2007-11-13 21:44:23 -0800358 }
359
360drop_unlock:
361 spin_unlock(&x->lock);
Herbert Xu716062f2007-11-13 21:44:23 -0800362drop:
Steffen Klassert33287152014-02-21 08:41:08 +0100363 xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
Herbert Xu716062f2007-11-13 21:44:23 -0800364 kfree_skb(skb);
365 return 0;
366}
367EXPORT_SYMBOL(xfrm_input);
368
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800369int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
370{
371 return xfrm_input(skb, nexthdr, 0, -1);
372}
373EXPORT_SYMBOL(xfrm_input_resume);
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375void __init xfrm_input_init(void)
376{
377 secpath_cachep = kmem_cache_create("secpath_cache",
378 sizeof(struct sec_path),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -0700379 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +0900380 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}