blob: 5b2409746ae0a14c491cc8dfd316facc5cd67347 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * xfrm_input.c
4 *
5 * Changes:
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Herbert Xuacf568e2017-12-15 16:40:44 +110011#include <linux/bottom_half.h>
12#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
14#include <linux/module.h>
Herbert Xu716062f2007-11-13 21:44:23 -080015#include <linux/netdevice.h>
Herbert Xuacf568e2017-12-15 16:40:44 +110016#include <linux/percpu.h>
Herbert Xu716062f2007-11-13 21:44:23 -080017#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <net/ip.h>
19#include <net/xfrm.h>
Alexander Duyck049f8e22015-05-27 07:16:49 -070020#include <net/ip_tunnels.h>
21#include <net/ip6_tunnel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Herbert Xuacf568e2017-12-15 16:40:44 +110023struct xfrm_trans_tasklet {
24 struct tasklet_struct tasklet;
25 struct sk_buff_head queue;
26};
27
28struct xfrm_trans_cb {
29 int (*finish)(struct net *net, struct sock *sk, struct sk_buff *skb);
30};
31
32#define XFRM_TRANS_SKB_CB(__skb) ((struct xfrm_trans_cb *)&((__skb)->cb[0]))
33
Christoph Lametere18b8902006-12-06 20:33:20 -080034static struct kmem_cache *secpath_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Steffen Klassert2f32b512014-03-14 07:28:07 +010036static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
Florian Westphal960fdfd2017-02-07 14:52:30 +010037static struct xfrm_input_afinfo const __rcu *xfrm_input_afinfo[AF_INET6 + 1];
Steffen Klassert2f32b512014-03-14 07:28:07 +010038
Steffen Klassert19958762017-01-30 06:45:43 +010039static struct gro_cells gro_cells;
40static struct net_device xfrm_napi_dev;
41
Herbert Xuacf568e2017-12-15 16:40:44 +110042static DEFINE_PER_CPU(struct xfrm_trans_tasklet, xfrm_trans_tasklet);
43
Florian Westphal960fdfd2017-02-07 14:52:30 +010044int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo)
Steffen Klassert2f32b512014-03-14 07:28:07 +010045{
46 int err = 0;
47
Florian Westphal960fdfd2017-02-07 14:52:30 +010048 if (WARN_ON(afinfo->family >= ARRAY_SIZE(xfrm_input_afinfo)))
Steffen Klassert2f32b512014-03-14 07:28:07 +010049 return -EAFNOSUPPORT;
Florian Westphal960fdfd2017-02-07 14:52:30 +010050
Steffen Klassert2f32b512014-03-14 07:28:07 +010051 spin_lock_bh(&xfrm_input_afinfo_lock);
52 if (unlikely(xfrm_input_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +080053 err = -EEXIST;
Steffen Klassert2f32b512014-03-14 07:28:07 +010054 else
55 rcu_assign_pointer(xfrm_input_afinfo[afinfo->family], afinfo);
56 spin_unlock_bh(&xfrm_input_afinfo_lock);
57 return err;
58}
59EXPORT_SYMBOL(xfrm_input_register_afinfo);
60
Florian Westphal960fdfd2017-02-07 14:52:30 +010061int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo)
Steffen Klassert2f32b512014-03-14 07:28:07 +010062{
63 int err = 0;
64
Steffen Klassert2f32b512014-03-14 07:28:07 +010065 spin_lock_bh(&xfrm_input_afinfo_lock);
66 if (likely(xfrm_input_afinfo[afinfo->family] != NULL)) {
67 if (unlikely(xfrm_input_afinfo[afinfo->family] != afinfo))
68 err = -EINVAL;
69 else
70 RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->family], NULL);
71 }
72 spin_unlock_bh(&xfrm_input_afinfo_lock);
73 synchronize_rcu();
74 return err;
75}
76EXPORT_SYMBOL(xfrm_input_unregister_afinfo);
77
Florian Westphal960fdfd2017-02-07 14:52:30 +010078static const struct xfrm_input_afinfo *xfrm_input_get_afinfo(unsigned int family)
Steffen Klassert2f32b512014-03-14 07:28:07 +010079{
Florian Westphal960fdfd2017-02-07 14:52:30 +010080 const struct xfrm_input_afinfo *afinfo;
Steffen Klassert2f32b512014-03-14 07:28:07 +010081
Florian Westphal960fdfd2017-02-07 14:52:30 +010082 if (WARN_ON_ONCE(family >= ARRAY_SIZE(xfrm_input_afinfo)))
Steffen Klassert2f32b512014-03-14 07:28:07 +010083 return NULL;
Florian Westphal960fdfd2017-02-07 14:52:30 +010084
Steffen Klassert2f32b512014-03-14 07:28:07 +010085 rcu_read_lock();
86 afinfo = rcu_dereference(xfrm_input_afinfo[family]);
87 if (unlikely(!afinfo))
88 rcu_read_unlock();
89 return afinfo;
90}
91
Steffen Klassert2f32b512014-03-14 07:28:07 +010092static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
93 int err)
94{
95 int ret;
Florian Westphal960fdfd2017-02-07 14:52:30 +010096 const struct xfrm_input_afinfo *afinfo = xfrm_input_get_afinfo(family);
Steffen Klassert2f32b512014-03-14 07:28:07 +010097
98 if (!afinfo)
99 return -EAFNOSUPPORT;
100
101 ret = afinfo->callback(skb, protocol, err);
Florian Westphal960fdfd2017-02-07 14:52:30 +0100102 rcu_read_unlock();
Steffen Klassert2f32b512014-03-14 07:28:07 +0100103
104 return ret;
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107void __secpath_destroy(struct sec_path *sp)
108{
109 int i;
110 for (i = 0; i < sp->len; i++)
Herbert Xudbe5b4a2006-04-01 00:54:16 -0800111 xfrm_state_put(sp->xvec[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 kmem_cache_free(secpath_cachep, sp);
113}
114EXPORT_SYMBOL(__secpath_destroy);
115
116struct sec_path *secpath_dup(struct sec_path *src)
117{
118 struct sec_path *sp;
119
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800120 sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (!sp)
122 return NULL;
123
124 sp->len = 0;
Steffen Klassert54ef2072017-02-15 09:39:54 +0100125 sp->olen = 0;
126
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200127 memset(sp->ovec, 0, sizeof(sp->ovec[XFRM_MAX_OFFLOAD_DEPTH]));
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (src) {
130 int i;
131
132 memcpy(sp, src, sizeof(*sp));
133 for (i = 0; i < sp->len; i++)
Herbert Xudbe5b4a2006-04-01 00:54:16 -0800134 xfrm_state_hold(sp->xvec[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Reshetova, Elena55eabed2017-07-04 15:53:23 +0300136 refcount_set(&sp->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return sp;
138}
139EXPORT_SYMBOL(secpath_dup);
140
Steffen Klassertb0fcee82017-02-15 09:39:24 +0100141int secpath_set(struct sk_buff *skb)
142{
143 struct sec_path *sp;
144
145 /* Allocate new secpath or COW existing one. */
Reshetova, Elena55eabed2017-07-04 15:53:23 +0300146 if (!skb->sp || refcount_read(&skb->sp->refcnt) != 1) {
Steffen Klassertb0fcee82017-02-15 09:39:24 +0100147 sp = secpath_dup(skb->sp);
148 if (!sp)
149 return -ENOMEM;
150
151 if (skb->sp)
152 secpath_put(skb->sp);
153 skb->sp = sp;
154 }
155 return 0;
156}
157EXPORT_SYMBOL(secpath_set);
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* Fetch spi and seq from ipsec header */
160
Al Viro6067b2b2006-09-27 18:47:59 -0700161int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 int offset, offset_seq;
Herbert Xu44072502007-10-17 21:30:34 -0700164 int hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 switch (nexthdr) {
167 case IPPROTO_AH:
Herbert Xu44072502007-10-17 21:30:34 -0700168 hlen = sizeof(struct ip_auth_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 offset = offsetof(struct ip_auth_hdr, spi);
170 offset_seq = offsetof(struct ip_auth_hdr, seq_no);
171 break;
172 case IPPROTO_ESP:
Herbert Xu44072502007-10-17 21:30:34 -0700173 hlen = sizeof(struct ip_esp_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 offset = offsetof(struct ip_esp_hdr, spi);
175 offset_seq = offsetof(struct ip_esp_hdr, seq_no);
176 break;
177 case IPPROTO_COMP:
178 if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
179 return -EINVAL;
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800180 *spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 *seq = 0;
182 return 0;
183 default:
184 return 1;
185 }
186
Herbert Xu44072502007-10-17 21:30:34 -0700187 if (!pskb_may_pull(skb, hlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
189
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800190 *spi = *(__be32 *)(skb_transport_header(skb) + offset);
191 *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 0;
193}
Steffen Klassert1e295372017-02-15 09:39:49 +0100194EXPORT_SYMBOL(xfrm_parse_spi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Herbert Xu227620e2007-11-13 21:41:28 -0800196int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
197{
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700198 struct xfrm_mode *inner_mode = x->inner_mode;
Herbert Xu227620e2007-11-13 21:41:28 -0800199 int err;
200
201 err = x->outer_mode->afinfo->extract_input(x, skb);
202 if (err)
203 return err;
204
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700205 if (x->sel.family == AF_UNSPEC) {
206 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
207 if (inner_mode == NULL)
208 return -EAFNOSUPPORT;
209 }
210
211 skb->protocol = inner_mode->afinfo->eth_proto;
212 return inner_mode->input2(x, skb);
Herbert Xu227620e2007-11-13 21:41:28 -0800213}
214EXPORT_SYMBOL(xfrm_prepare_input);
215
Herbert Xu716062f2007-11-13 21:44:23 -0800216int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
217{
Alexey Dobriyanbd235e32008-11-25 17:37:56 -0800218 struct net *net = dev_net(skb->dev);
Herbert Xu716062f2007-11-13 21:44:23 -0800219 int err;
220 __be32 seq;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000221 __be32 seq_hi;
Steffen Klassert33287152014-02-21 08:41:08 +0100222 struct xfrm_state *x = NULL;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800223 xfrm_address_t *daddr;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700224 struct xfrm_mode *inner_mode;
Alexander Duyck049f8e22015-05-27 07:16:49 -0700225 u32 mark = skb->mark;
Aviv Heller4ce3dbe2017-11-28 19:55:40 +0200226 unsigned int family = AF_UNSPEC;
Herbert Xu716062f2007-11-13 21:44:23 -0800227 int decaps = 0;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800228 int async = 0;
Steffen Klassert7785bba2017-02-15 09:40:00 +0100229 bool xfrm_gro = false;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200230 bool crypto_done = false;
231 struct xfrm_offload *xo = xfrm_offload(skb);
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800232
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800233 if (encap_type < 0) {
Herbert Xu00501122007-12-11 01:53:43 -0800234 x = xfrm_input_state(skb);
Aviv Heller4ce3dbe2017-11-28 19:55:40 +0200235
236 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
237 if (x->km.state == XFRM_STATE_ACQ)
238 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
239 else
240 XFRM_INC_STATS(net,
241 LINUX_MIB_XFRMINSTATEINVALID);
242 goto drop;
243 }
244
Steffen Klassert33287152014-02-21 08:41:08 +0100245 family = x->outer_mode->afinfo->family;
Steffen Klassert7785bba2017-02-15 09:40:00 +0100246
247 /* An encap_type of -1 indicates async resumption. */
248 if (encap_type == -1) {
249 async = 1;
250 seq = XFRM_SKB_CB(skb)->seq.input.low;
251 goto resume;
252 }
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200253
Steffen Klassert7785bba2017-02-15 09:40:00 +0100254 /* encap_type < -1 indicates a GRO call. */
255 encap_type = 0;
256 seq = XFRM_SPI_SKB_CB(skb)->seq;
Herbert Xu716062f2007-11-13 21:44:23 -0800257
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200258 if (xo && (xo->flags & CRYPTO_DONE)) {
259 crypto_done = true;
260 x = xfrm_input_state(skb);
261 family = XFRM_SPI_SKB_CB(skb)->family;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200262
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200263 if (!(xo->status & CRYPTO_SUCCESS)) {
264 if (xo->status &
265 (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
266 CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
267 CRYPTO_TUNNEL_AH_AUTH_FAILED |
268 CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200269
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200270 xfrm_audit_state_icvfail(x, skb,
271 x->type->proto);
272 x->stats.integrity_failed++;
273 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
274 goto drop;
275 }
276
Yossi Kuperman47ebcc02017-08-30 11:30:39 +0300277 if (xo->status & CRYPTO_INVALID_PROTOCOL) {
278 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
279 goto drop;
280 }
281
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200282 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200283 goto drop;
284 }
285
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200286 if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
287 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
288 goto drop;
289 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200290 }
291
Herbert Xu716062f2007-11-13 21:44:23 -0800292 goto lock;
293 }
294
Steffen Klassert33287152014-02-21 08:41:08 +0100295 family = XFRM_SPI_SKB_CB(skb)->family;
296
Alexander Duyck049f8e22015-05-27 07:16:49 -0700297 /* if tunnel is present override skb->mark value with tunnel i_key */
Alexey Kodanev1625f452016-08-10 13:54:57 +0300298 switch (family) {
299 case AF_INET:
300 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
Alexander Duyck049f8e22015-05-27 07:16:49 -0700301 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
Alexey Kodanev1625f452016-08-10 13:54:57 +0300302 break;
303 case AF_INET6:
304 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
Alexander Duyck049f8e22015-05-27 07:16:49 -0700305 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
Alexey Kodanev1625f452016-08-10 13:54:57 +0300306 break;
Alexander Duyck049f8e22015-05-27 07:16:49 -0700307 }
308
Steffen Klassertb0fcee82017-02-15 09:39:24 +0100309 err = secpath_set(skb);
310 if (err) {
311 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
312 goto drop;
Herbert Xub2aa5e92007-11-13 21:44:55 -0800313 }
314
Herbert Xu716062f2007-11-13 21:44:23 -0800315 seq = 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800316 if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800317 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800318 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800319 }
Herbert Xu716062f2007-11-13 21:44:23 -0800320
Florian Westphalcb79a182017-11-01 20:30:49 +0100321 daddr = (xfrm_address_t *)(skb_network_header(skb) +
322 XFRM_SPI_SKB_CB(skb)->daddroff);
Herbert Xu716062f2007-11-13 21:44:23 -0800323 do {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800324 if (skb->sp->len == XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800325 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800326 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800327 }
Herbert Xu716062f2007-11-13 21:44:23 -0800328
Alexander Duyck049f8e22015-05-27 07:16:49 -0700329 x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800330 if (x == NULL) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800331 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
Paul Mooreafeb14b2007-12-21 14:58:11 -0800332 xfrm_audit_state_notfound(skb, family, spi, seq);
Herbert Xu716062f2007-11-13 21:44:23 -0800333 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800334 }
Herbert Xu716062f2007-11-13 21:44:23 -0800335
Herbert Xub2aa5e92007-11-13 21:44:55 -0800336 skb->sp->xvec[skb->sp->len++] = x;
337
Steffen Klassert7785bba2017-02-15 09:40:00 +0100338lock:
Herbert Xu716062f2007-11-13 21:44:23 -0800339 spin_lock(&x->lock);
Fan Du4c4d41f2013-06-06 10:15:54 +0800340
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800341 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
Li RongQingdc0565c2015-04-24 16:49:31 +0800342 if (x->km.state == XFRM_STATE_ACQ)
343 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
344 else
345 XFRM_INC_STATS(net,
346 LINUX_MIB_XFRMINSTATEINVALID);
Herbert Xu716062f2007-11-13 21:44:23 -0800347 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800348 }
Herbert Xu716062f2007-11-13 21:44:23 -0800349
David S. Miller3de77cf2008-12-18 19:23:56 -0800350 if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
351 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
352 goto drop_unlock;
353 }
354
Steffen Klassert36ae0142011-03-28 19:45:52 +0000355 if (x->repl->check(x, skb, seq)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800356 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800357 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800358 }
Herbert Xu716062f2007-11-13 21:44:23 -0800359
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800360 if (xfrm_state_check_expire(x)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800361 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
Herbert Xu716062f2007-11-13 21:44:23 -0800362 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800363 }
Herbert Xu716062f2007-11-13 21:44:23 -0800364
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800365 spin_unlock(&x->lock);
366
Alexey Dobriyan68c11e92015-04-02 10:58:24 +0300367 if (xfrm_tunnel_check(skb, x, family)) {
368 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
369 goto drop;
370 }
371
Steffen Klassert2cd08462011-03-08 00:09:51 +0000372 seq_hi = htonl(xfrm_replay_seqhi(x, seq));
373
Steffen Klassert1ce36442011-03-08 00:06:31 +0000374 XFRM_SKB_CB(skb)->seq.input.low = seq;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000375 XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800376
Steffen Klassert3bc07322011-03-15 21:08:28 +0000377 skb_dst_force(skb);
subashab@codeaurora.org071d36b2016-03-23 22:39:50 -0600378 dev_hold(skb->dev);
Steffen Klassert3bc07322011-03-15 21:08:28 +0000379
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200380 if (crypto_done)
381 nexthdr = x->type_offload->input_tail(x, skb);
382 else
383 nexthdr = x->type->input(x, skb);
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800384
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800385 if (nexthdr == -EINPROGRESS)
386 return 0;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800387resume:
subashab@codeaurora.org071d36b2016-03-23 22:39:50 -0600388 dev_put(skb->dev);
389
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800390 spin_lock(&x->lock);
Herbert Xu668dc8a2007-12-16 15:55:02 -0800391 if (nexthdr <= 0) {
Herbert Xu9dd32452007-12-30 21:10:30 -0800392 if (nexthdr == -EBADMSG) {
393 xfrm_audit_state_icvfail(x, skb,
394 x->type->proto);
Herbert Xu668dc8a2007-12-16 15:55:02 -0800395 x->stats.integrity_failed++;
Herbert Xu9dd32452007-12-30 21:10:30 -0800396 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800397 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800398 goto drop_unlock;
Herbert Xu668dc8a2007-12-16 15:55:02 -0800399 }
Herbert Xu716062f2007-11-13 21:44:23 -0800400
Herbert Xu716062f2007-11-13 21:44:23 -0800401 /* only the first xfrm gets the encap type */
402 encap_type = 0;
403
Steffen Klassert3b59df42012-09-04 00:03:29 +0000404 if (async && x->repl->recheck(x, skb, seq)) {
Steffen Klassertbcf66bf2011-09-20 23:38:58 +0000405 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
406 goto drop_unlock;
407 }
408
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000409 x->repl->advance(x, seq);
Herbert Xu716062f2007-11-13 21:44:23 -0800410
411 x->curlft.bytes += skb->len;
412 x->curlft.packets++;
413
414 spin_unlock(&x->lock);
415
Herbert Xu60d5fcf2007-11-19 18:47:58 -0800416 XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
417
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700418 inner_mode = x->inner_mode;
419
420 if (x->sel.family == AF_UNSPEC) {
421 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
Steffen Klassertcb866e32015-10-23 07:52:58 +0200422 if (inner_mode == NULL) {
423 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700424 goto drop;
Steffen Klassertcb866e32015-10-23 07:52:58 +0200425 }
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700426 }
427
428 if (inner_mode->input(x, skb)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800429 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800430 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800431 }
Herbert Xu716062f2007-11-13 21:44:23 -0800432
433 if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
434 decaps = 1;
435 break;
436 }
437
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800438 /*
439 * We need the inner address. However, we only get here for
440 * transport mode so the outer address is identical.
441 */
442 daddr = &x->id.daddr;
Herbert Xu2fcb45b2007-12-03 22:54:12 -0800443 family = x->outer_mode->afinfo->family;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800444
Herbert Xu716062f2007-11-13 21:44:23 -0800445 err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800446 if (err < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800447 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800448 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800449 }
Herbert Xu716062f2007-11-13 21:44:23 -0800450 } while (!err);
451
Steffen Klassert33287152014-02-21 08:41:08 +0100452 err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
453 if (err)
454 goto drop;
455
Herbert Xu716062f2007-11-13 21:44:23 -0800456 nf_reset(skb);
457
458 if (decaps) {
Alexey Kodanev23e9fcf2017-09-12 14:53:46 +0300459 if (skb->sp)
460 skb->sp->olen = 0;
Eric Dumazetadf30902009-06-02 05:19:30 +0000461 skb_dst_drop(skb);
Steffen Klassert19958762017-01-30 06:45:43 +0100462 gro_cells_receive(&gro_cells, skb);
Herbert Xu716062f2007-11-13 21:44:23 -0800463 return 0;
464 } else {
Steffen Klassert7785bba2017-02-15 09:40:00 +0100465 xo = xfrm_offload(skb);
466 if (xo)
467 xfrm_gro = xo->flags & XFRM_GRO;
468
Sabrina Dubrocacfcf99f2017-04-27 12:03:37 +0200469 err = x->inner_mode->afinfo->transport_finish(skb, xfrm_gro || async);
Steffen Klassert7785bba2017-02-15 09:40:00 +0100470 if (xfrm_gro) {
Alexey Kodanev23e9fcf2017-09-12 14:53:46 +0300471 if (skb->sp)
472 skb->sp->olen = 0;
Steffen Klassert7785bba2017-02-15 09:40:00 +0100473 skb_dst_drop(skb);
474 gro_cells_receive(&gro_cells, skb);
475 return err;
476 }
477
478 return err;
Herbert Xu716062f2007-11-13 21:44:23 -0800479 }
480
481drop_unlock:
482 spin_unlock(&x->lock);
Herbert Xu716062f2007-11-13 21:44:23 -0800483drop:
Steffen Klassert33287152014-02-21 08:41:08 +0100484 xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
Herbert Xu716062f2007-11-13 21:44:23 -0800485 kfree_skb(skb);
486 return 0;
487}
488EXPORT_SYMBOL(xfrm_input);
489
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800490int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
491{
492 return xfrm_input(skb, nexthdr, 0, -1);
493}
494EXPORT_SYMBOL(xfrm_input_resume);
495
Herbert Xuacf568e2017-12-15 16:40:44 +1100496static void xfrm_trans_reinject(unsigned long data)
497{
498 struct xfrm_trans_tasklet *trans = (void *)data;
499 struct sk_buff_head queue;
500 struct sk_buff *skb;
501
502 __skb_queue_head_init(&queue);
503 skb_queue_splice_init(&trans->queue, &queue);
504
505 while ((skb = __skb_dequeue(&queue)))
506 XFRM_TRANS_SKB_CB(skb)->finish(dev_net(skb->dev), NULL, skb);
507}
508
509int xfrm_trans_queue(struct sk_buff *skb,
510 int (*finish)(struct net *, struct sock *,
511 struct sk_buff *))
512{
513 struct xfrm_trans_tasklet *trans;
514
515 trans = this_cpu_ptr(&xfrm_trans_tasklet);
516
517 if (skb_queue_len(&trans->queue) >= netdev_max_backlog)
518 return -ENOBUFS;
519
520 XFRM_TRANS_SKB_CB(skb)->finish = finish;
Herbert Xud16b46e2018-01-04 22:25:07 +1100521 __skb_queue_tail(&trans->queue, skb);
Herbert Xuacf568e2017-12-15 16:40:44 +1100522 tasklet_schedule(&trans->tasklet);
523 return 0;
524}
525EXPORT_SYMBOL(xfrm_trans_queue);
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527void __init xfrm_input_init(void)
528{
Steffen Klassert19958762017-01-30 06:45:43 +0100529 int err;
Herbert Xuacf568e2017-12-15 16:40:44 +1100530 int i;
Steffen Klassert19958762017-01-30 06:45:43 +0100531
532 init_dummy_netdev(&xfrm_napi_dev);
533 err = gro_cells_init(&gro_cells, &xfrm_napi_dev);
534 if (err)
535 gro_cells.cells = NULL;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 secpath_cachep = kmem_cache_create("secpath_cache",
538 sizeof(struct sec_path),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -0700539 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +0900540 NULL);
Herbert Xuacf568e2017-12-15 16:40:44 +1100541
542 for_each_possible_cpu(i) {
543 struct xfrm_trans_tasklet *trans;
544
545 trans = &per_cpu(xfrm_trans_tasklet, i);
546 __skb_queue_head_init(&trans->queue);
547 tasklet_init(&trans->tasklet, xfrm_trans_reinject,
548 (unsigned long)trans);
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}