blob: 1472c08579756ec8588e7f5537c8ac162fa28af0 [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;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200260 family = XFRM_SPI_SKB_CB(skb)->family;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200261
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200262 if (!(xo->status & CRYPTO_SUCCESS)) {
263 if (xo->status &
264 (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
265 CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
266 CRYPTO_TUNNEL_AH_AUTH_FAILED |
267 CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200268
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200269 xfrm_audit_state_icvfail(x, skb,
270 x->type->proto);
271 x->stats.integrity_failed++;
272 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
273 goto drop;
274 }
275
Yossi Kuperman47ebcc02017-08-30 11:30:39 +0300276 if (xo->status & CRYPTO_INVALID_PROTOCOL) {
277 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
278 goto drop;
279 }
280
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200281 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200282 goto drop;
283 }
284
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200285 if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
286 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
287 goto drop;
288 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200289 }
290
Herbert Xu716062f2007-11-13 21:44:23 -0800291 goto lock;
292 }
293
Steffen Klassert33287152014-02-21 08:41:08 +0100294 family = XFRM_SPI_SKB_CB(skb)->family;
295
Alexander Duyck049f8e22015-05-27 07:16:49 -0700296 /* if tunnel is present override skb->mark value with tunnel i_key */
Alexey Kodanev1625f452016-08-10 13:54:57 +0300297 switch (family) {
298 case AF_INET:
299 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
Alexander Duyck049f8e22015-05-27 07:16:49 -0700300 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
Alexey Kodanev1625f452016-08-10 13:54:57 +0300301 break;
302 case AF_INET6:
303 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
Alexander Duyck049f8e22015-05-27 07:16:49 -0700304 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
Alexey Kodanev1625f452016-08-10 13:54:57 +0300305 break;
Alexander Duyck049f8e22015-05-27 07:16:49 -0700306 }
307
Steffen Klassertb0fcee82017-02-15 09:39:24 +0100308 err = secpath_set(skb);
309 if (err) {
310 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
311 goto drop;
Herbert Xub2aa5e92007-11-13 21:44:55 -0800312 }
313
Herbert Xu716062f2007-11-13 21:44:23 -0800314 seq = 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800315 if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800316 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800317 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800318 }
Herbert Xu716062f2007-11-13 21:44:23 -0800319
Florian Westphalcb79a182017-11-01 20:30:49 +0100320 daddr = (xfrm_address_t *)(skb_network_header(skb) +
321 XFRM_SPI_SKB_CB(skb)->daddroff);
Herbert Xu716062f2007-11-13 21:44:23 -0800322 do {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800323 if (skb->sp->len == XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800324 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800325 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800326 }
Herbert Xu716062f2007-11-13 21:44:23 -0800327
Alexander Duyck049f8e22015-05-27 07:16:49 -0700328 x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800329 if (x == NULL) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800330 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
Paul Mooreafeb14b2007-12-21 14:58:11 -0800331 xfrm_audit_state_notfound(skb, family, spi, seq);
Herbert Xu716062f2007-11-13 21:44:23 -0800332 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800333 }
Herbert Xu716062f2007-11-13 21:44:23 -0800334
Herbert Xub2aa5e92007-11-13 21:44:55 -0800335 skb->sp->xvec[skb->sp->len++] = x;
336
Steffen Klassert7785bba2017-02-15 09:40:00 +0100337lock:
Herbert Xu716062f2007-11-13 21:44:23 -0800338 spin_lock(&x->lock);
Fan Du4c4d41f2013-06-06 10:15:54 +0800339
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800340 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
Li RongQingdc0565c2015-04-24 16:49:31 +0800341 if (x->km.state == XFRM_STATE_ACQ)
342 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
343 else
344 XFRM_INC_STATS(net,
345 LINUX_MIB_XFRMINSTATEINVALID);
Herbert Xu716062f2007-11-13 21:44:23 -0800346 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800347 }
Herbert Xu716062f2007-11-13 21:44:23 -0800348
David S. Miller3de77cf2008-12-18 19:23:56 -0800349 if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
350 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
351 goto drop_unlock;
352 }
353
Steffen Klassert36ae0142011-03-28 19:45:52 +0000354 if (x->repl->check(x, skb, seq)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800355 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800356 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800357 }
Herbert Xu716062f2007-11-13 21:44:23 -0800358
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800359 if (xfrm_state_check_expire(x)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800360 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
Herbert Xu716062f2007-11-13 21:44:23 -0800361 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800362 }
Herbert Xu716062f2007-11-13 21:44:23 -0800363
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800364 spin_unlock(&x->lock);
365
Alexey Dobriyan68c11e92015-04-02 10:58:24 +0300366 if (xfrm_tunnel_check(skb, x, family)) {
367 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
368 goto drop;
369 }
370
Steffen Klassert2cd08462011-03-08 00:09:51 +0000371 seq_hi = htonl(xfrm_replay_seqhi(x, seq));
372
Steffen Klassert1ce36442011-03-08 00:06:31 +0000373 XFRM_SKB_CB(skb)->seq.input.low = seq;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000374 XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800375
Steffen Klassert3bc07322011-03-15 21:08:28 +0000376 skb_dst_force(skb);
subashab@codeaurora.org071d36b2016-03-23 22:39:50 -0600377 dev_hold(skb->dev);
Steffen Klassert3bc07322011-03-15 21:08:28 +0000378
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200379 if (crypto_done)
380 nexthdr = x->type_offload->input_tail(x, skb);
381 else
382 nexthdr = x->type->input(x, skb);
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800383
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800384 if (nexthdr == -EINPROGRESS)
385 return 0;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800386resume:
subashab@codeaurora.org071d36b2016-03-23 22:39:50 -0600387 dev_put(skb->dev);
388
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800389 spin_lock(&x->lock);
Herbert Xu668dc8a2007-12-16 15:55:02 -0800390 if (nexthdr <= 0) {
Herbert Xu9dd32452007-12-30 21:10:30 -0800391 if (nexthdr == -EBADMSG) {
392 xfrm_audit_state_icvfail(x, skb,
393 x->type->proto);
Herbert Xu668dc8a2007-12-16 15:55:02 -0800394 x->stats.integrity_failed++;
Herbert Xu9dd32452007-12-30 21:10:30 -0800395 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800396 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800397 goto drop_unlock;
Herbert Xu668dc8a2007-12-16 15:55:02 -0800398 }
Herbert Xu716062f2007-11-13 21:44:23 -0800399
Herbert Xu716062f2007-11-13 21:44:23 -0800400 /* only the first xfrm gets the encap type */
401 encap_type = 0;
402
Steffen Klassert3b59df42012-09-04 00:03:29 +0000403 if (async && x->repl->recheck(x, skb, seq)) {
Steffen Klassertbcf66bf2011-09-20 23:38:58 +0000404 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
405 goto drop_unlock;
406 }
407
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000408 x->repl->advance(x, seq);
Herbert Xu716062f2007-11-13 21:44:23 -0800409
410 x->curlft.bytes += skb->len;
411 x->curlft.packets++;
412
413 spin_unlock(&x->lock);
414
Herbert Xu60d5fcf2007-11-19 18:47:58 -0800415 XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
416
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700417 inner_mode = x->inner_mode;
418
419 if (x->sel.family == AF_UNSPEC) {
420 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
Steffen Klassertcb866e32015-10-23 07:52:58 +0200421 if (inner_mode == NULL) {
422 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700423 goto drop;
Steffen Klassertcb866e32015-10-23 07:52:58 +0200424 }
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700425 }
426
427 if (inner_mode->input(x, skb)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800428 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800429 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800430 }
Herbert Xu716062f2007-11-13 21:44:23 -0800431
432 if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
433 decaps = 1;
434 break;
435 }
436
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800437 /*
438 * We need the inner address. However, we only get here for
439 * transport mode so the outer address is identical.
440 */
441 daddr = &x->id.daddr;
Herbert Xu2fcb45b2007-12-03 22:54:12 -0800442 family = x->outer_mode->afinfo->family;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800443
Herbert Xu716062f2007-11-13 21:44:23 -0800444 err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800445 if (err < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800446 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800447 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800448 }
Herbert Xu716062f2007-11-13 21:44:23 -0800449 } while (!err);
450
Steffen Klassert33287152014-02-21 08:41:08 +0100451 err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
452 if (err)
453 goto drop;
454
Herbert Xu716062f2007-11-13 21:44:23 -0800455 nf_reset(skb);
456
457 if (decaps) {
Alexey Kodanev23e9fcf2017-09-12 14:53:46 +0300458 if (skb->sp)
459 skb->sp->olen = 0;
Eric Dumazetadf30902009-06-02 05:19:30 +0000460 skb_dst_drop(skb);
Steffen Klassert19958762017-01-30 06:45:43 +0100461 gro_cells_receive(&gro_cells, skb);
Herbert Xu716062f2007-11-13 21:44:23 -0800462 return 0;
463 } else {
Steffen Klassert7785bba2017-02-15 09:40:00 +0100464 xo = xfrm_offload(skb);
465 if (xo)
466 xfrm_gro = xo->flags & XFRM_GRO;
467
Sabrina Dubrocacfcf99f2017-04-27 12:03:37 +0200468 err = x->inner_mode->afinfo->transport_finish(skb, xfrm_gro || async);
Steffen Klassert7785bba2017-02-15 09:40:00 +0100469 if (xfrm_gro) {
Alexey Kodanev23e9fcf2017-09-12 14:53:46 +0300470 if (skb->sp)
471 skb->sp->olen = 0;
Steffen Klassert7785bba2017-02-15 09:40:00 +0100472 skb_dst_drop(skb);
473 gro_cells_receive(&gro_cells, skb);
474 return err;
475 }
476
477 return err;
Herbert Xu716062f2007-11-13 21:44:23 -0800478 }
479
480drop_unlock:
481 spin_unlock(&x->lock);
Herbert Xu716062f2007-11-13 21:44:23 -0800482drop:
Steffen Klassert33287152014-02-21 08:41:08 +0100483 xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
Herbert Xu716062f2007-11-13 21:44:23 -0800484 kfree_skb(skb);
485 return 0;
486}
487EXPORT_SYMBOL(xfrm_input);
488
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800489int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
490{
491 return xfrm_input(skb, nexthdr, 0, -1);
492}
493EXPORT_SYMBOL(xfrm_input_resume);
494
Herbert Xuacf568e2017-12-15 16:40:44 +1100495static void xfrm_trans_reinject(unsigned long data)
496{
497 struct xfrm_trans_tasklet *trans = (void *)data;
498 struct sk_buff_head queue;
499 struct sk_buff *skb;
500
501 __skb_queue_head_init(&queue);
502 skb_queue_splice_init(&trans->queue, &queue);
503
504 while ((skb = __skb_dequeue(&queue)))
505 XFRM_TRANS_SKB_CB(skb)->finish(dev_net(skb->dev), NULL, skb);
506}
507
508int xfrm_trans_queue(struct sk_buff *skb,
509 int (*finish)(struct net *, struct sock *,
510 struct sk_buff *))
511{
512 struct xfrm_trans_tasklet *trans;
513
514 trans = this_cpu_ptr(&xfrm_trans_tasklet);
515
516 if (skb_queue_len(&trans->queue) >= netdev_max_backlog)
517 return -ENOBUFS;
518
519 XFRM_TRANS_SKB_CB(skb)->finish = finish;
Herbert Xud16b46e2018-01-04 22:25:07 +1100520 __skb_queue_tail(&trans->queue, skb);
Herbert Xuacf568e2017-12-15 16:40:44 +1100521 tasklet_schedule(&trans->tasklet);
522 return 0;
523}
524EXPORT_SYMBOL(xfrm_trans_queue);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526void __init xfrm_input_init(void)
527{
Steffen Klassert19958762017-01-30 06:45:43 +0100528 int err;
Herbert Xuacf568e2017-12-15 16:40:44 +1100529 int i;
Steffen Klassert19958762017-01-30 06:45:43 +0100530
531 init_dummy_netdev(&xfrm_napi_dev);
532 err = gro_cells_init(&gro_cells, &xfrm_napi_dev);
533 if (err)
534 gro_cells.cells = NULL;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 secpath_cachep = kmem_cache_create("secpath_cache",
537 sizeof(struct sec_path),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -0700538 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +0900539 NULL);
Herbert Xuacf568e2017-12-15 16:40:44 +1100540
541 for_each_possible_cpu(i) {
542 struct xfrm_trans_tasklet *trans;
543
544 trans = &per_cpu(xfrm_trans_tasklet, i);
545 __skb_queue_head_init(&trans->queue);
546 tasklet_init(&trans->tasklet, xfrm_trans_reinject,
547 (unsigned long)trans);
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}