Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * xfrm_input.c |
| 4 | * |
| 5 | * Changes: |
| 6 | * YOSHIFUJI Hideaki @USAGI |
| 7 | * Split up af-specific portion |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/module.h> |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 13 | #include <linux/netdevice.h> |
| 14 | #include <net/dst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <net/ip.h> |
| 16 | #include <net/xfrm.h> |
Alexander Duyck | 049f8e2 | 2015-05-27 07:16:49 -0700 | [diff] [blame] | 17 | #include <net/ip_tunnels.h> |
| 18 | #include <net/ip6_tunnel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 20 | static struct kmem_cache *secpath_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 22 | static DEFINE_SPINLOCK(xfrm_input_afinfo_lock); |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 23 | static struct xfrm_input_afinfo const __rcu *xfrm_input_afinfo[AF_INET6 + 1]; |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 24 | |
Steffen Klassert | 1995876 | 2017-01-30 06:45:43 +0100 | [diff] [blame] | 25 | static struct gro_cells gro_cells; |
| 26 | static struct net_device xfrm_napi_dev; |
| 27 | |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 28 | int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo) |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 29 | { |
| 30 | int err = 0; |
| 31 | |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 32 | if (WARN_ON(afinfo->family >= ARRAY_SIZE(xfrm_input_afinfo))) |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 33 | return -EAFNOSUPPORT; |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 34 | |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 35 | spin_lock_bh(&xfrm_input_afinfo_lock); |
| 36 | if (unlikely(xfrm_input_afinfo[afinfo->family] != NULL)) |
Li RongQing | f31e8d4f | 2015-04-23 11:06:53 +0800 | [diff] [blame] | 37 | err = -EEXIST; |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 38 | else |
| 39 | rcu_assign_pointer(xfrm_input_afinfo[afinfo->family], afinfo); |
| 40 | spin_unlock_bh(&xfrm_input_afinfo_lock); |
| 41 | return err; |
| 42 | } |
| 43 | EXPORT_SYMBOL(xfrm_input_register_afinfo); |
| 44 | |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 45 | int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo) |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 46 | { |
| 47 | int err = 0; |
| 48 | |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 49 | 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 | } |
| 60 | EXPORT_SYMBOL(xfrm_input_unregister_afinfo); |
| 61 | |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 62 | static const struct xfrm_input_afinfo *xfrm_input_get_afinfo(unsigned int family) |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 63 | { |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 64 | const struct xfrm_input_afinfo *afinfo; |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 65 | |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 66 | if (WARN_ON_ONCE(family >= ARRAY_SIZE(xfrm_input_afinfo))) |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 67 | return NULL; |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 68 | |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 69 | 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 Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 76 | static int xfrm_rcv_cb(struct sk_buff *skb, unsigned int family, u8 protocol, |
| 77 | int err) |
| 78 | { |
| 79 | int ret; |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 80 | const struct xfrm_input_afinfo *afinfo = xfrm_input_get_afinfo(family); |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 81 | |
| 82 | if (!afinfo) |
| 83 | return -EAFNOSUPPORT; |
| 84 | |
| 85 | ret = afinfo->callback(skb, protocol, err); |
Florian Westphal | 960fdfd | 2017-02-07 14:52:30 +0100 | [diff] [blame] | 86 | rcu_read_unlock(); |
Steffen Klassert | 2f32b51 | 2014-03-14 07:28:07 +0100 | [diff] [blame] | 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | void __secpath_destroy(struct sec_path *sp) |
| 92 | { |
| 93 | int i; |
| 94 | for (i = 0; i < sp->len; i++) |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 95 | xfrm_state_put(sp->xvec[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | kmem_cache_free(secpath_cachep, sp); |
| 97 | } |
| 98 | EXPORT_SYMBOL(__secpath_destroy); |
| 99 | |
| 100 | struct sec_path *secpath_dup(struct sec_path *src) |
| 101 | { |
| 102 | struct sec_path *sp; |
| 103 | |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 104 | sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | if (!sp) |
| 106 | return NULL; |
| 107 | |
| 108 | sp->len = 0; |
Steffen Klassert | 54ef207 | 2017-02-15 09:39:54 +0100 | [diff] [blame] | 109 | sp->olen = 0; |
| 110 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 111 | memset(sp->ovec, 0, sizeof(sp->ovec[XFRM_MAX_OFFLOAD_DEPTH])); |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | if (src) { |
| 114 | int i; |
| 115 | |
| 116 | memcpy(sp, src, sizeof(*sp)); |
| 117 | for (i = 0; i < sp->len; i++) |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 118 | xfrm_state_hold(sp->xvec[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
Reshetova, Elena | 55eabed | 2017-07-04 15:53:23 +0300 | [diff] [blame] | 120 | refcount_set(&sp->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return sp; |
| 122 | } |
| 123 | EXPORT_SYMBOL(secpath_dup); |
| 124 | |
Steffen Klassert | b0fcee8 | 2017-02-15 09:39:24 +0100 | [diff] [blame] | 125 | int secpath_set(struct sk_buff *skb) |
| 126 | { |
| 127 | struct sec_path *sp; |
| 128 | |
| 129 | /* Allocate new secpath or COW existing one. */ |
Reshetova, Elena | 55eabed | 2017-07-04 15:53:23 +0300 | [diff] [blame] | 130 | if (!skb->sp || refcount_read(&skb->sp->refcnt) != 1) { |
Steffen Klassert | b0fcee8 | 2017-02-15 09:39:24 +0100 | [diff] [blame] | 131 | 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 | } |
| 141 | EXPORT_SYMBOL(secpath_set); |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | /* Fetch spi and seq from ipsec header */ |
| 144 | |
Al Viro | 6067b2b | 2006-09-27 18:47:59 -0700 | [diff] [blame] | 145 | int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | int offset, offset_seq; |
Herbert Xu | 4407250 | 2007-10-17 21:30:34 -0700 | [diff] [blame] | 148 | int hlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | switch (nexthdr) { |
| 151 | case IPPROTO_AH: |
Herbert Xu | 4407250 | 2007-10-17 21:30:34 -0700 | [diff] [blame] | 152 | hlen = sizeof(struct ip_auth_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | offset = offsetof(struct ip_auth_hdr, spi); |
| 154 | offset_seq = offsetof(struct ip_auth_hdr, seq_no); |
| 155 | break; |
| 156 | case IPPROTO_ESP: |
Herbert Xu | 4407250 | 2007-10-17 21:30:34 -0700 | [diff] [blame] | 157 | hlen = sizeof(struct ip_esp_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | 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 Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 164 | *spi = htonl(ntohs(*(__be16 *)(skb_transport_header(skb) + 2))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | *seq = 0; |
| 166 | return 0; |
| 167 | default: |
| 168 | return 1; |
| 169 | } |
| 170 | |
Herbert Xu | 4407250 | 2007-10-17 21:30:34 -0700 | [diff] [blame] | 171 | if (!pskb_may_pull(skb, hlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return -EINVAL; |
| 173 | |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 174 | *spi = *(__be32 *)(skb_transport_header(skb) + offset); |
| 175 | *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return 0; |
| 177 | } |
Steffen Klassert | 1e29537 | 2017-02-15 09:39:49 +0100 | [diff] [blame] | 178 | EXPORT_SYMBOL(xfrm_parse_spi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 180 | int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb) |
| 181 | { |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 182 | struct xfrm_mode *inner_mode = x->inner_mode; |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 183 | int err; |
| 184 | |
| 185 | err = x->outer_mode->afinfo->extract_input(x, skb); |
| 186 | if (err) |
| 187 | return err; |
| 188 | |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 189 | 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 Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 197 | } |
| 198 | EXPORT_SYMBOL(xfrm_prepare_input); |
| 199 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 200 | int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) |
| 201 | { |
Alexey Dobriyan | bd235e3 | 2008-11-25 17:37:56 -0800 | [diff] [blame] | 202 | struct net *net = dev_net(skb->dev); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 203 | int err; |
| 204 | __be32 seq; |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 205 | __be32 seq_hi; |
Steffen Klassert | 3328715 | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 206 | struct xfrm_state *x = NULL; |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame] | 207 | xfrm_address_t *daddr; |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 208 | struct xfrm_mode *inner_mode; |
Alexander Duyck | 049f8e2 | 2015-05-27 07:16:49 -0700 | [diff] [blame] | 209 | u32 mark = skb->mark; |
Aviv Heller | 4ce3dbe | 2017-11-28 19:55:40 +0200 | [diff] [blame^] | 210 | unsigned int family = AF_UNSPEC; |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 211 | int decaps = 0; |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame] | 212 | int async = 0; |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 213 | bool xfrm_gro = false; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 214 | bool crypto_done = false; |
| 215 | struct xfrm_offload *xo = xfrm_offload(skb); |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame] | 216 | |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame] | 217 | if (encap_type < 0) { |
Herbert Xu | 0050112 | 2007-12-11 01:53:43 -0800 | [diff] [blame] | 218 | x = xfrm_input_state(skb); |
Aviv Heller | 4ce3dbe | 2017-11-28 19:55:40 +0200 | [diff] [blame^] | 219 | |
| 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 Klassert | 3328715 | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 229 | family = x->outer_mode->afinfo->family; |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 230 | |
| 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 Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 237 | |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 238 | /* encap_type < -1 indicates a GRO call. */ |
| 239 | encap_type = 0; |
| 240 | seq = XFRM_SPI_SKB_CB(skb)->seq; |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 241 | |
Steffen Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 242 | 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 Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 246 | |
Steffen Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 247 | 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 Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 253 | |
Steffen Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 254 | 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 Kuperman | 47ebcc0 | 2017-08-30 11:30:39 +0300 | [diff] [blame] | 261 | if (xo->status & CRYPTO_INVALID_PROTOCOL) { |
| 262 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR); |
| 263 | goto drop; |
| 264 | } |
| 265 | |
Steffen Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 266 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 267 | goto drop; |
| 268 | } |
| 269 | |
Steffen Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 270 | if ((err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) { |
| 271 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); |
| 272 | goto drop; |
| 273 | } |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 276 | goto lock; |
| 277 | } |
| 278 | |
Steffen Klassert | 3328715 | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 279 | family = XFRM_SPI_SKB_CB(skb)->family; |
| 280 | |
Alexander Duyck | 049f8e2 | 2015-05-27 07:16:49 -0700 | [diff] [blame] | 281 | /* if tunnel is present override skb->mark value with tunnel i_key */ |
Alexey Kodanev | 1625f45 | 2016-08-10 13:54:57 +0300 | [diff] [blame] | 282 | switch (family) { |
| 283 | case AF_INET: |
| 284 | if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4) |
Alexander Duyck | 049f8e2 | 2015-05-27 07:16:49 -0700 | [diff] [blame] | 285 | mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4->parms.i_key); |
Alexey Kodanev | 1625f45 | 2016-08-10 13:54:57 +0300 | [diff] [blame] | 286 | break; |
| 287 | case AF_INET6: |
| 288 | if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6) |
Alexander Duyck | 049f8e2 | 2015-05-27 07:16:49 -0700 | [diff] [blame] | 289 | mark = be32_to_cpu(XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6->parms.i_key); |
Alexey Kodanev | 1625f45 | 2016-08-10 13:54:57 +0300 | [diff] [blame] | 290 | break; |
Alexander Duyck | 049f8e2 | 2015-05-27 07:16:49 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Steffen Klassert | b0fcee8 | 2017-02-15 09:39:24 +0100 | [diff] [blame] | 293 | err = secpath_set(skb); |
| 294 | if (err) { |
| 295 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR); |
| 296 | goto drop; |
Herbert Xu | b2aa5e9 | 2007-11-13 21:44:55 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 299 | seq = 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 300 | if (!spi && (err = xfrm_parse_spi(skb, nexthdr, &spi, &seq)) != 0) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 301 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 302 | goto drop; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 303 | } |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 304 | |
Florian Westphal | cb79a18 | 2017-11-01 20:30:49 +0100 | [diff] [blame] | 305 | daddr = (xfrm_address_t *)(skb_network_header(skb) + |
| 306 | XFRM_SPI_SKB_CB(skb)->daddroff); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 307 | do { |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 308 | if (skb->sp->len == XFRM_MAX_DEPTH) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 309 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 310 | goto drop; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 311 | } |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 312 | |
Alexander Duyck | 049f8e2 | 2015-05-27 07:16:49 -0700 | [diff] [blame] | 313 | x = xfrm_state_lookup(net, mark, daddr, spi, nexthdr, family); |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 314 | if (x == NULL) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 315 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 316 | xfrm_audit_state_notfound(skb, family, spi, seq); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 317 | goto drop; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 318 | } |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 319 | |
Herbert Xu | b2aa5e9 | 2007-11-13 21:44:55 -0800 | [diff] [blame] | 320 | skb->sp->xvec[skb->sp->len++] = x; |
| 321 | |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 322 | lock: |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 323 | spin_lock(&x->lock); |
Fan Du | 4c4d41f | 2013-06-06 10:15:54 +0800 | [diff] [blame] | 324 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 325 | if (unlikely(x->km.state != XFRM_STATE_VALID)) { |
Li RongQing | dc0565c | 2015-04-24 16:49:31 +0800 | [diff] [blame] | 326 | 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 Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 331 | goto drop_unlock; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 332 | } |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 333 | |
David S. Miller | 3de77cf | 2008-12-18 19:23:56 -0800 | [diff] [blame] | 334 | 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 Klassert | 36ae014 | 2011-03-28 19:45:52 +0000 | [diff] [blame] | 339 | if (x->repl->check(x, skb, seq)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 340 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 341 | goto drop_unlock; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 342 | } |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 343 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 344 | if (xfrm_state_check_expire(x)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 345 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEEXPIRED); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 346 | goto drop_unlock; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 347 | } |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 348 | |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 349 | spin_unlock(&x->lock); |
| 350 | |
Alexey Dobriyan | 68c11e9 | 2015-04-02 10:58:24 +0300 | [diff] [blame] | 351 | if (xfrm_tunnel_check(skb, x, family)) { |
| 352 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR); |
| 353 | goto drop; |
| 354 | } |
| 355 | |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 356 | seq_hi = htonl(xfrm_replay_seqhi(x, seq)); |
| 357 | |
Steffen Klassert | 1ce3644 | 2011-03-08 00:06:31 +0000 | [diff] [blame] | 358 | XFRM_SKB_CB(skb)->seq.input.low = seq; |
Steffen Klassert | 2cd0846 | 2011-03-08 00:09:51 +0000 | [diff] [blame] | 359 | XFRM_SKB_CB(skb)->seq.input.hi = seq_hi; |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame] | 360 | |
Steffen Klassert | 3bc0732 | 2011-03-15 21:08:28 +0000 | [diff] [blame] | 361 | skb_dst_force(skb); |
subashab@codeaurora.org | 071d36b | 2016-03-23 22:39:50 -0600 | [diff] [blame] | 362 | dev_hold(skb->dev); |
Steffen Klassert | 3bc0732 | 2011-03-15 21:08:28 +0000 | [diff] [blame] | 363 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 364 | if (crypto_done) |
| 365 | nexthdr = x->type_offload->input_tail(x, skb); |
| 366 | else |
| 367 | nexthdr = x->type->input(x, skb); |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 368 | |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame] | 369 | if (nexthdr == -EINPROGRESS) |
| 370 | return 0; |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame] | 371 | resume: |
subashab@codeaurora.org | 071d36b | 2016-03-23 22:39:50 -0600 | [diff] [blame] | 372 | dev_put(skb->dev); |
| 373 | |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 374 | spin_lock(&x->lock); |
Herbert Xu | 668dc8a | 2007-12-16 15:55:02 -0800 | [diff] [blame] | 375 | if (nexthdr <= 0) { |
Herbert Xu | 9dd3245 | 2007-12-30 21:10:30 -0800 | [diff] [blame] | 376 | if (nexthdr == -EBADMSG) { |
| 377 | xfrm_audit_state_icvfail(x, skb, |
| 378 | x->type->proto); |
Herbert Xu | 668dc8a | 2007-12-16 15:55:02 -0800 | [diff] [blame] | 379 | x->stats.integrity_failed++; |
Herbert Xu | 9dd3245 | 2007-12-30 21:10:30 -0800 | [diff] [blame] | 380 | } |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 381 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEPROTOERROR); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 382 | goto drop_unlock; |
Herbert Xu | 668dc8a | 2007-12-16 15:55:02 -0800 | [diff] [blame] | 383 | } |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 384 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 385 | /* only the first xfrm gets the encap type */ |
| 386 | encap_type = 0; |
| 387 | |
Steffen Klassert | 3b59df4 | 2012-09-04 00:03:29 +0000 | [diff] [blame] | 388 | if (async && x->repl->recheck(x, skb, seq)) { |
Steffen Klassert | bcf66bf | 2011-09-20 23:38:58 +0000 | [diff] [blame] | 389 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATESEQERROR); |
| 390 | goto drop_unlock; |
| 391 | } |
| 392 | |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 393 | x->repl->advance(x, seq); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 394 | |
| 395 | x->curlft.bytes += skb->len; |
| 396 | x->curlft.packets++; |
| 397 | |
| 398 | spin_unlock(&x->lock); |
| 399 | |
Herbert Xu | 60d5fcf | 2007-11-19 18:47:58 -0800 | [diff] [blame] | 400 | XFRM_MODE_SKB_CB(skb)->protocol = nexthdr; |
| 401 | |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 402 | 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 Klassert | cb866e3 | 2015-10-23 07:52:58 +0200 | [diff] [blame] | 406 | if (inner_mode == NULL) { |
| 407 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR); |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 408 | goto drop; |
Steffen Klassert | cb866e3 | 2015-10-23 07:52:58 +0200 | [diff] [blame] | 409 | } |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | if (inner_mode->input(x, skb)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 413 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 414 | goto drop; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 415 | } |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 416 | |
| 417 | if (x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) { |
| 418 | decaps = 1; |
| 419 | break; |
| 420 | } |
| 421 | |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame] | 422 | /* |
| 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 Xu | 2fcb45b | 2007-12-03 22:54:12 -0800 | [diff] [blame] | 427 | family = x->outer_mode->afinfo->family; |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame] | 428 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 429 | err = xfrm_parse_spi(skb, nexthdr, &spi, &seq); |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 430 | if (err < 0) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 431 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 432 | goto drop; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 433 | } |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 434 | } while (!err); |
| 435 | |
Steffen Klassert | 3328715 | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 436 | err = xfrm_rcv_cb(skb, family, x->type->proto, 0); |
| 437 | if (err) |
| 438 | goto drop; |
| 439 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 440 | nf_reset(skb); |
| 441 | |
| 442 | if (decaps) { |
Alexey Kodanev | 23e9fcf | 2017-09-12 14:53:46 +0300 | [diff] [blame] | 443 | if (skb->sp) |
| 444 | skb->sp->olen = 0; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 445 | skb_dst_drop(skb); |
Steffen Klassert | 1995876 | 2017-01-30 06:45:43 +0100 | [diff] [blame] | 446 | gro_cells_receive(&gro_cells, skb); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 447 | return 0; |
| 448 | } else { |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 449 | xo = xfrm_offload(skb); |
| 450 | if (xo) |
| 451 | xfrm_gro = xo->flags & XFRM_GRO; |
| 452 | |
Sabrina Dubroca | cfcf99f | 2017-04-27 12:03:37 +0200 | [diff] [blame] | 453 | err = x->inner_mode->afinfo->transport_finish(skb, xfrm_gro || async); |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 454 | if (xfrm_gro) { |
Alexey Kodanev | 23e9fcf | 2017-09-12 14:53:46 +0300 | [diff] [blame] | 455 | if (skb->sp) |
| 456 | skb->sp->olen = 0; |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 457 | skb_dst_drop(skb); |
| 458 | gro_cells_receive(&gro_cells, skb); |
| 459 | return err; |
| 460 | } |
| 461 | |
| 462 | return err; |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | drop_unlock: |
| 466 | spin_unlock(&x->lock); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 467 | drop: |
Steffen Klassert | 3328715 | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 468 | xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 469 | kfree_skb(skb); |
| 470 | return 0; |
| 471 | } |
| 472 | EXPORT_SYMBOL(xfrm_input); |
| 473 | |
Herbert Xu | 1bf06cd | 2007-11-19 18:50:17 -0800 | [diff] [blame] | 474 | int xfrm_input_resume(struct sk_buff *skb, int nexthdr) |
| 475 | { |
| 476 | return xfrm_input(skb, nexthdr, 0, -1); |
| 477 | } |
| 478 | EXPORT_SYMBOL(xfrm_input_resume); |
| 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | void __init xfrm_input_init(void) |
| 481 | { |
Steffen Klassert | 1995876 | 2017-01-30 06:45:43 +0100 | [diff] [blame] | 482 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | secpath_cachep = kmem_cache_create("secpath_cache", |
| 490 | sizeof(struct sec_path), |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 491 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 492 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |