blob: da6447389ffb9aac4e32af4c580a45f08b34c1bd [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
11#include <linux/slab.h>
12#include <linux/module.h>
Herbert Xu716062f2007-11-13 21:44:23 -080013#include <linux/netdevice.h>
14#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <net/ip.h>
16#include <net/xfrm.h>
Alexander Duyck049f8e22015-05-27 07:16:49 -070017#include <net/ip_tunnels.h>
18#include <net/ip6_tunnel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Christoph Lametere18b8902006-12-06 20:33:20 -080020static struct kmem_cache *secpath_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Steffen Klassert2f32b512014-03-14 07:28:07 +010022static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
Florian Westphal960fdfd2017-02-07 14:52:30 +010023static struct xfrm_input_afinfo const __rcu *xfrm_input_afinfo[AF_INET6 + 1];
Steffen Klassert2f32b512014-03-14 07:28:07 +010024
Steffen Klassert19958762017-01-30 06:45:43 +010025static struct gro_cells gro_cells;
26static struct net_device xfrm_napi_dev;
27
Florian Westphal960fdfd2017-02-07 14:52:30 +010028int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo)
Steffen Klassert2f32b512014-03-14 07:28:07 +010029{
30 int err = 0;
31
Florian Westphal960fdfd2017-02-07 14:52:30 +010032 if (WARN_ON(afinfo->family >= ARRAY_SIZE(xfrm_input_afinfo)))
Steffen Klassert2f32b512014-03-14 07:28:07 +010033 return -EAFNOSUPPORT;
Florian Westphal960fdfd2017-02-07 14:52:30 +010034
Steffen Klassert2f32b512014-03-14 07:28:07 +010035 spin_lock_bh(&xfrm_input_afinfo_lock);
36 if (unlikely(xfrm_input_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +080037 err = -EEXIST;
Steffen Klassert2f32b512014-03-14 07:28:07 +010038 else
39 rcu_assign_pointer(xfrm_input_afinfo[afinfo->family], afinfo);
40 spin_unlock_bh(&xfrm_input_afinfo_lock);
41 return err;
42}
43EXPORT_SYMBOL(xfrm_input_register_afinfo);
44
Florian Westphal960fdfd2017-02-07 14:52:30 +010045int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo)
Steffen Klassert2f32b512014-03-14 07:28:07 +010046{
47 int err = 0;
48
Steffen Klassert2f32b512014-03-14 07:28:07 +010049 spin_lock_bh(&xfrm_input_afinfo_lock);
50 if (likely(xfrm_input_afinfo[afinfo->family] != NULL)) {
51 if (unlikely(xfrm_input_afinfo[afinfo->family] != afinfo))
52 err = -EINVAL;
53 else
54 RCU_INIT_POINTER(xfrm_input_afinfo[afinfo->family], NULL);
55 }
56 spin_unlock_bh(&xfrm_input_afinfo_lock);
57 synchronize_rcu();
58 return err;
59}
60EXPORT_SYMBOL(xfrm_input_unregister_afinfo);
61
Florian Westphal960fdfd2017-02-07 14:52:30 +010062static const struct xfrm_input_afinfo *xfrm_input_get_afinfo(unsigned int family)
Steffen Klassert2f32b512014-03-14 07:28:07 +010063{
Florian Westphal960fdfd2017-02-07 14:52:30 +010064 const struct xfrm_input_afinfo *afinfo;
Steffen Klassert2f32b512014-03-14 07:28:07 +010065
Florian Westphal960fdfd2017-02-07 14:52:30 +010066 if (WARN_ON_ONCE(family >= ARRAY_SIZE(xfrm_input_afinfo)))
Steffen Klassert2f32b512014-03-14 07:28:07 +010067 return NULL;
Florian Westphal960fdfd2017-02-07 14:52:30 +010068
Steffen Klassert2f32b512014-03-14 07:28:07 +010069 rcu_read_lock();
70 afinfo = rcu_dereference(xfrm_input_afinfo[family]);
71 if (unlikely(!afinfo))
72 rcu_read_unlock();
73 return afinfo;
74}
75
Steffen Klassert2f32b512014-03-14 07:28:07 +010076static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol,
77 int err)
78{
79 int ret;
Florian Westphal960fdfd2017-02-07 14:52:30 +010080 const struct xfrm_input_afinfo *afinfo = xfrm_input_get_afinfo(family);
Steffen Klassert2f32b512014-03-14 07:28:07 +010081
82 if (!afinfo)
83 return -EAFNOSUPPORT;
84
85 ret = afinfo->callback(skb, protocol, err);
Florian Westphal960fdfd2017-02-07 14:52:30 +010086 rcu_read_unlock();
Steffen Klassert2f32b512014-03-14 07:28:07 +010087
88 return ret;
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091void __secpath_destroy(struct sec_path *sp)
92{
93 int i;
94 for (i = 0; i < sp->len; i++)
Herbert Xudbe5b4a2006-04-01 00:54:16 -080095 xfrm_state_put(sp->xvec[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 kmem_cache_free(secpath_cachep, sp);
97}
98EXPORT_SYMBOL(__secpath_destroy);
99
100struct sec_path *secpath_dup(struct sec_path *src)
101{
102 struct sec_path *sp;
103
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800104 sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (!sp)
106 return NULL;
107
108 sp->len = 0;
Steffen Klassert54ef2072017-02-15 09:39:54 +0100109 sp->olen = 0;
110
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200111 memset(sp->ovec, 0, sizeof(sp->ovec[XFRM_MAX_OFFLOAD_DEPTH]));
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (src) {
114 int i;
115
116 memcpy(sp, src, sizeof(*sp));
117 for (i = 0; i < sp->len; i++)
Herbert Xudbe5b4a2006-04-01 00:54:16 -0800118 xfrm_state_hold(sp->xvec[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Reshetova, Elena55eabed2017-07-04 15:53:23 +0300120 refcount_set(&sp->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return sp;
122}
123EXPORT_SYMBOL(secpath_dup);
124
Steffen Klassertb0fcee82017-02-15 09:39:24 +0100125int secpath_set(struct sk_buff *skb)
126{
127 struct sec_path *sp;
128
129 /* Allocate new secpath or COW existing one. */
Reshetova, Elena55eabed2017-07-04 15:53:23 +0300130 if (!skb->sp || refcount_read(&skb->sp->refcnt) != 1) {
Steffen Klassertb0fcee82017-02-15 09:39:24 +0100131 sp = secpath_dup(skb->sp);
132 if (!sp)
133 return -ENOMEM;
134
135 if (skb->sp)
136 secpath_put(skb->sp);
137 skb->sp = sp;
138 }
139 return 0;
140}
141EXPORT_SYMBOL(secpath_set);
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* Fetch spi and seq from ipsec header */
144
Al Viro6067b2b2006-09-27 18:47:59 -0700145int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 int offset, offset_seq;
Herbert Xu44072502007-10-17 21:30:34 -0700148 int hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 switch (nexthdr) {
151 case IPPROTO_AH:
Herbert Xu44072502007-10-17 21:30:34 -0700152 hlen = sizeof(struct ip_auth_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 offset = offsetof(struct ip_auth_hdr, spi);
154 offset_seq = offsetof(struct ip_auth_hdr, seq_no);
155 break;
156 case IPPROTO_ESP:
Herbert Xu44072502007-10-17 21:30:34 -0700157 hlen = sizeof(struct ip_esp_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 offset = offsetof(struct ip_esp_hdr, spi);
159 offset_seq = offsetof(struct ip_esp_hdr, seq_no);
160 break;
161 case IPPROTO_COMP:
162 if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
163 return -EINVAL;
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800164 *spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *seq = 0;
166 return 0;
167 default:
168 return 1;
169 }
170
Herbert Xu44072502007-10-17 21:30:34 -0700171 if (!pskb_may_pull(skb, hlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -EINVAL;
173
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800174 *spi = *(__be32 *)(skb_transport_header(skb) + offset);
175 *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
Steffen Klassert1e295372017-02-15 09:39:49 +0100178EXPORT_SYMBOL(xfrm_parse_spi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Herbert Xu227620e2007-11-13 21:41:28 -0800180int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
181{
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700182 struct xfrm_mode *inner_mode = x->inner_mode;
Herbert Xu227620e2007-11-13 21:41:28 -0800183 int err;
184
185 err = x->outer_mode->afinfo->extract_input(x, skb);
186 if (err)
187 return err;
188
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700189 if (x->sel.family == AF_UNSPEC) {
190 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
191 if (inner_mode == NULL)
192 return -EAFNOSUPPORT;
193 }
194
195 skb->protocol = inner_mode->afinfo->eth_proto;
196 return inner_mode->input2(x, skb);
Herbert Xu227620e2007-11-13 21:41:28 -0800197}
198EXPORT_SYMBOL(xfrm_prepare_input);
199
Herbert Xu716062f2007-11-13 21:44:23 -0800200int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
201{
Alexey Dobriyanbd235e32008-11-25 17:37:56 -0800202 struct net *net = dev_net(skb->dev);
Herbert Xu716062f2007-11-13 21:44:23 -0800203 int err;
204 __be32 seq;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000205 __be32 seq_hi;
Steffen Klassert33287152014-02-21 08:41:08 +0100206 struct xfrm_state *x = NULL;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800207 xfrm_address_t *daddr;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700208 struct xfrm_mode *inner_mode;
Alexander Duyck049f8e22015-05-27 07:16:49 -0700209 u32 mark = skb->mark;
Aviv Heller4ce3dbe2017-11-28 19:55:40 +0200210 unsigned int family = AF_UNSPEC;
Herbert Xu716062f2007-11-13 21:44:23 -0800211 int decaps = 0;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800212 int async = 0;
Steffen Klassert7785bba2017-02-15 09:40:00 +0100213 bool xfrm_gro = false;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200214 bool crypto_done = false;
215 struct xfrm_offload *xo = xfrm_offload(skb);
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800216
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800217 if (encap_type < 0) {
Herbert Xu00501122007-12-11 01:53:43 -0800218 x = xfrm_input_state(skb);
Aviv Heller4ce3dbe2017-11-28 19:55:40 +0200219
220 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
221 if (x->km.state == XFRM_STATE_ACQ)
222 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
223 else
224 XFRM_INC_STATS(net,
225 LINUX_MIB_XFRMINSTATEINVALID);
226 goto drop;
227 }
228
Steffen Klassert33287152014-02-21 08:41:08 +0100229 family = x->outer_mode->afinfo->family;
Steffen Klassert7785bba2017-02-15 09:40:00 +0100230
231 /* An encap_type of -1 indicates async resumption. */
232 if (encap_type == -1) {
233 async = 1;
234 seq = XFRM_SKB_CB(skb)->seq.input.low;
235 goto resume;
236 }
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200237
Steffen Klassert7785bba2017-02-15 09:40:00 +0100238 /* encap_type < -1 indicates a GRO call. */
239 encap_type = 0;
240 seq = XFRM_SPI_SKB_CB(skb)->seq;
Herbert Xu716062f2007-11-13 21:44:23 -0800241
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200242 if (xo && (xo->flags & CRYPTO_DONE)) {
243 crypto_done = true;
244 x = xfrm_input_state(skb);
245 family = XFRM_SPI_SKB_CB(skb)->family;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200246
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200247 if (!(xo->status & CRYPTO_SUCCESS)) {
248 if (xo->status &
249 (CRYPTO_TRANSPORT_AH_AUTH_FAILED |
250 CRYPTO_TRANSPORT_ESP_AUTH_FAILED |
251 CRYPTO_TUNNEL_AH_AUTH_FAILED |
252 CRYPTO_TUNNEL_ESP_AUTH_FAILED)) {
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200253
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200254 xfrm_audit_state_icvfail(x, skb,
255 x->type->proto);
256 x->stats.integrity_failed++;
257 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
258 goto drop;
259 }
260
Yossi Kuperman47ebcc02017-08-30 11:30:39 +0300261 if (xo->status & CRYPTO_INVALID_PROTOCOL) {
262 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
263 goto drop;
264 }
265
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200266 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200267 goto drop;
268 }
269
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +0200270 if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
271 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
272 goto drop;
273 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200274 }
275
Herbert Xu716062f2007-11-13 21:44:23 -0800276 goto lock;
277 }
278
Steffen Klassert33287152014-02-21 08:41:08 +0100279 family = XFRM_SPI_SKB_CB(skb)->family;
280
Alexander Duyck049f8e22015-05-27 07:16:49 -0700281 /* if tunnel is present override skb->mark value with tunnel i_key */
Alexey Kodanev1625f452016-08-10 13:54:57 +0300282 switch (family) {
283 case AF_INET:
284 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
Alexander Duyck049f8e22015-05-27 07:16:49 -0700285 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key);
Alexey Kodanev1625f452016-08-10 13:54:57 +0300286 break;
287 case AF_INET6:
288 if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
Alexander Duyck049f8e22015-05-27 07:16:49 -0700289 mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key);
Alexey Kodanev1625f452016-08-10 13:54:57 +0300290 break;
Alexander Duyck049f8e22015-05-27 07:16:49 -0700291 }
292
Steffen Klassertb0fcee82017-02-15 09:39:24 +0100293 err = secpath_set(skb);
294 if (err) {
295 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
296 goto drop;
Herbert Xub2aa5e92007-11-13 21:44:55 -0800297 }
298
Herbert Xu716062f2007-11-13 21:44:23 -0800299 seq = 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800300 if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800301 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800302 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800303 }
Herbert Xu716062f2007-11-13 21:44:23 -0800304
Florian Westphalcb79a182017-11-01 20:30:49 +0100305 daddr = (xfrm_address_t *)(skb_network_header(skb) +
306 XFRM_SPI_SKB_CB(skb)->daddroff);
Herbert Xu716062f2007-11-13 21:44:23 -0800307 do {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800308 if (skb->sp->len == XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800309 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800310 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800311 }
Herbert Xu716062f2007-11-13 21:44:23 -0800312
Alexander Duyck049f8e22015-05-27 07:16:49 -0700313 x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800314 if (x == NULL) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800315 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
Paul Mooreafeb14b2007-12-21 14:58:11 -0800316 xfrm_audit_state_notfound(skb, family, spi, seq);
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
Herbert Xub2aa5e92007-11-13 21:44:55 -0800320 skb->sp->xvec[skb->sp->len++] = x;
321
Steffen Klassert7785bba2017-02-15 09:40:00 +0100322lock:
Herbert Xu716062f2007-11-13 21:44:23 -0800323 spin_lock(&x->lock);
Fan Du4c4d41f2013-06-06 10:15:54 +0800324
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800325 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
Li RongQingdc0565c2015-04-24 16:49:31 +0800326 if (x->km.state == XFRM_STATE_ACQ)
327 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
328 else
329 XFRM_INC_STATS(net,
330 LINUX_MIB_XFRMINSTATEINVALID);
Herbert Xu716062f2007-11-13 21:44:23 -0800331 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800332 }
Herbert Xu716062f2007-11-13 21:44:23 -0800333
David S. Miller3de77cf2008-12-18 19:23:56 -0800334 if ((x->encap ? x->encap->encap_type : 0) != encap_type) {
335 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
336 goto drop_unlock;
337 }
338
Steffen Klassert36ae0142011-03-28 19:45:52 +0000339 if (x->repl->check(x, skb, seq)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800340 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800341 goto drop_unlock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800342 }
Herbert Xu716062f2007-11-13 21:44:23 -0800343
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800344 if (xfrm_state_check_expire(x)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800345 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED);
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
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800349 spin_unlock(&x->lock);
350
Alexey Dobriyan68c11e92015-04-02 10:58:24 +0300351 if (xfrm_tunnel_check(skb, x, family)) {
352 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
353 goto drop;
354 }
355
Steffen Klassert2cd08462011-03-08 00:09:51 +0000356 seq_hi = htonl(xfrm_replay_seqhi(x, seq));
357
Steffen Klassert1ce36442011-03-08 00:06:31 +0000358 XFRM_SKB_CB(skb)->seq.input.low = seq;
Steffen Klassert2cd08462011-03-08 00:09:51 +0000359 XFRM_SKB_CB(skb)->seq.input.hi = seq_hi;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800360
Steffen Klassert3bc07322011-03-15 21:08:28 +0000361 skb_dst_force(skb);
subashab@codeaurora.org071d36b2016-03-23 22:39:50 -0600362 dev_hold(skb->dev);
Steffen Klassert3bc07322011-03-15 21:08:28 +0000363
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200364 if (crypto_done)
365 nexthdr = x->type_offload->input_tail(x, skb);
366 else
367 nexthdr = x->type->input(x, skb);
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800368
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800369 if (nexthdr == -EINPROGRESS)
370 return 0;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800371resume:
subashab@codeaurora.org071d36b2016-03-23 22:39:50 -0600372 dev_put(skb->dev);
373
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800374 spin_lock(&x->lock);
Herbert Xu668dc8a2007-12-16 15:55:02 -0800375 if (nexthdr <= 0) {
Herbert Xu9dd32452007-12-30 21:10:30 -0800376 if (nexthdr == -EBADMSG) {
377 xfrm_audit_state_icvfail(x, skb,
378 x->type->proto);
Herbert Xu668dc8a2007-12-16 15:55:02 -0800379 x->stats.integrity_failed++;
Herbert Xu9dd32452007-12-30 21:10:30 -0800380 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800381 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800382 goto drop_unlock;
Herbert Xu668dc8a2007-12-16 15:55:02 -0800383 }
Herbert Xu716062f2007-11-13 21:44:23 -0800384
Herbert Xu716062f2007-11-13 21:44:23 -0800385 /* only the first xfrm gets the encap type */
386 encap_type = 0;
387
Steffen Klassert3b59df42012-09-04 00:03:29 +0000388 if (async && x->repl->recheck(x, skb, seq)) {
Steffen Klassertbcf66bf2011-09-20 23:38:58 +0000389 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR);
390 goto drop_unlock;
391 }
392
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000393 x->repl->advance(x, seq);
Herbert Xu716062f2007-11-13 21:44:23 -0800394
395 x->curlft.bytes += skb->len;
396 x->curlft.packets++;
397
398 spin_unlock(&x->lock);
399
Herbert Xu60d5fcf2007-11-19 18:47:58 -0800400 XFRM_MODE_SKB_CB(skb)->protocol = nexthdr;
401
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700402 inner_mode = x->inner_mode;
403
404 if (x->sel.family == AF_UNSPEC) {
405 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
Steffen Klassertcb866e32015-10-23 07:52:58 +0200406 if (inner_mode == NULL) {
407 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700408 goto drop;
Steffen Klassertcb866e32015-10-23 07:52:58 +0200409 }
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700410 }
411
412 if (inner_mode->input(x, skb)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800413 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800414 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800415 }
Herbert Xu716062f2007-11-13 21:44:23 -0800416
417 if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) {
418 decaps = 1;
419 break;
420 }
421
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800422 /*
423 * We need the inner address. However, we only get here for
424 * transport mode so the outer address is identical.
425 */
426 daddr = &x->id.daddr;
Herbert Xu2fcb45b2007-12-03 22:54:12 -0800427 family = x->outer_mode->afinfo->family;
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800428
Herbert Xu716062f2007-11-13 21:44:23 -0800429 err = xfrm_parse_spi(skb, nexthdr, &spi, &seq);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800430 if (err < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800431 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Herbert Xu716062f2007-11-13 21:44:23 -0800432 goto drop;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800433 }
Herbert Xu716062f2007-11-13 21:44:23 -0800434 } while (!err);
435
Steffen Klassert33287152014-02-21 08:41:08 +0100436 err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
437 if (err)
438 goto drop;
439
Herbert Xu716062f2007-11-13 21:44:23 -0800440 nf_reset(skb);
441
442 if (decaps) {
Alexey Kodanev23e9fcf2017-09-12 14:53:46 +0300443 if (skb->sp)
444 skb->sp->olen = 0;
Eric Dumazetadf30902009-06-02 05:19:30 +0000445 skb_dst_drop(skb);
Steffen Klassert19958762017-01-30 06:45:43 +0100446 gro_cells_receive(&gro_cells, skb);
Herbert Xu716062f2007-11-13 21:44:23 -0800447 return 0;
448 } else {
Steffen Klassert7785bba2017-02-15 09:40:00 +0100449 xo = xfrm_offload(skb);
450 if (xo)
451 xfrm_gro = xo->flags & XFRM_GRO;
452
Sabrina Dubrocacfcf99f2017-04-27 12:03:37 +0200453 err = x->inner_mode->afinfo->transport_finish(skb, xfrm_gro || async);
Steffen Klassert7785bba2017-02-15 09:40:00 +0100454 if (xfrm_gro) {
Alexey Kodanev23e9fcf2017-09-12 14:53:46 +0300455 if (skb->sp)
456 skb->sp->olen = 0;
Steffen Klassert7785bba2017-02-15 09:40:00 +0100457 skb_dst_drop(skb);
458 gro_cells_receive(&gro_cells, skb);
459 return err;
460 }
461
462 return err;
Herbert Xu716062f2007-11-13 21:44:23 -0800463 }
464
465drop_unlock:
466 spin_unlock(&x->lock);
Herbert Xu716062f2007-11-13 21:44:23 -0800467drop:
Steffen Klassert33287152014-02-21 08:41:08 +0100468 xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1);
Herbert Xu716062f2007-11-13 21:44:23 -0800469 kfree_skb(skb);
470 return 0;
471}
472EXPORT_SYMBOL(xfrm_input);
473
Herbert Xu1bf06cd2007-11-19 18:50:17 -0800474int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
475{
476 return xfrm_input(skb, nexthdr, 0, -1);
477}
478EXPORT_SYMBOL(xfrm_input_resume);
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480void __init xfrm_input_init(void)
481{
Steffen Klassert19958762017-01-30 06:45:43 +0100482 int err;
483
484 init_dummy_netdev(&xfrm_napi_dev);
485 err = gro_cells_init(&gro_cells, &xfrm_napi_dev);
486 if (err)
487 gro_cells.cells = NULL;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 secpath_cachep = kmem_cache_create("secpath_cache",
490 sizeof(struct sec_path),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -0700491 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +0900492 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}