Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Joe Perches | afd46503 | 2012-03-12 07:03:32 +0000 | [diff] [blame] | 2 | #define pr_fmt(fmt) "IPsec: " fmt |
| 3 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 4 | #include <crypto/aead.h> |
| 5 | #include <crypto/authenc.h> |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 6 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/module.h> |
| 8 | #include <net/ip.h> |
| 9 | #include <net/xfrm.h> |
| 10 | #include <net/esp.h> |
Adrian Bunk | 72998d8 | 2007-10-26 22:53:58 -0700 | [diff] [blame] | 11 | #include <linux/scatterlist.h> |
Herbert Xu | a02a642 | 2005-10-10 21:11:08 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/pfkeyv2.h> |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 14 | #include <linux/rtnetlink.h> |
| 15 | #include <linux/slab.h> |
Herbert Xu | b7c6538 | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 16 | #include <linux/spinlock.h> |
Thomas Graf | 2017a72 | 2007-12-10 16:53:05 -0800 | [diff] [blame] | 17 | #include <linux/in6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <net/icmp.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 19 | #include <net/protocol.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <net/udp.h> |
| 21 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 22 | #include <linux/highmem.h> |
| 23 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 24 | struct esp_skb_cb { |
| 25 | struct xfrm_skb_cb xfrm; |
| 26 | void *tmp; |
| 27 | }; |
| 28 | |
Herbert Xu | 962fcef | 2016-06-18 13:03:36 +0800 | [diff] [blame] | 29 | struct esp_output_extra { |
| 30 | __be32 seqhi; |
| 31 | u32 esphoff; |
| 32 | }; |
| 33 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 34 | #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0])) |
| 35 | |
Martin Willi | d979e20 | 2010-12-08 04:37:50 +0000 | [diff] [blame] | 36 | static u32 esp4_get_mtu(struct xfrm_state *x, int mtu); |
| 37 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 38 | /* |
| 39 | * Allocate an AEAD request structure with extra space for SG and IV. |
| 40 | * |
| 41 | * For alignment considerations the IV is placed at the front, followed |
| 42 | * by the request and finally the SG list. |
| 43 | * |
| 44 | * TODO: Use spare space in skb for this where possible. |
| 45 | */ |
Herbert Xu | 962fcef | 2016-06-18 13:03:36 +0800 | [diff] [blame] | 46 | static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int extralen) |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 47 | { |
| 48 | unsigned int len; |
| 49 | |
Herbert Xu | 962fcef | 2016-06-18 13:03:36 +0800 | [diff] [blame] | 50 | len = extralen; |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 51 | |
| 52 | len += crypto_aead_ivsize(aead); |
| 53 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 54 | if (len) { |
| 55 | len += crypto_aead_alignmask(aead) & |
| 56 | ~(crypto_tfm_ctx_alignment() - 1); |
| 57 | len = ALIGN(len, crypto_tfm_ctx_alignment()); |
| 58 | } |
| 59 | |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 60 | len += sizeof(struct aead_request) + crypto_aead_reqsize(aead); |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 61 | len = ALIGN(len, __alignof__(struct scatterlist)); |
| 62 | |
| 63 | len += sizeof(struct scatterlist) * nfrags; |
| 64 | |
| 65 | return kmalloc(len, GFP_ATOMIC); |
| 66 | } |
| 67 | |
Herbert Xu | 962fcef | 2016-06-18 13:03:36 +0800 | [diff] [blame] | 68 | static inline void *esp_tmp_extra(void *tmp) |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 69 | { |
Herbert Xu | 962fcef | 2016-06-18 13:03:36 +0800 | [diff] [blame] | 70 | return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra)); |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 71 | } |
Herbert Xu | 962fcef | 2016-06-18 13:03:36 +0800 | [diff] [blame] | 72 | |
| 73 | static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int extralen) |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 74 | { |
| 75 | return crypto_aead_ivsize(aead) ? |
Herbert Xu | 962fcef | 2016-06-18 13:03:36 +0800 | [diff] [blame] | 76 | PTR_ALIGN((u8 *)tmp + extralen, |
| 77 | crypto_aead_alignmask(aead) + 1) : tmp + extralen; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 80 | static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv) |
| 81 | { |
| 82 | struct aead_request *req; |
| 83 | |
| 84 | req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead), |
| 85 | crypto_tfm_ctx_alignment()); |
| 86 | aead_request_set_tfm(req, aead); |
| 87 | return req; |
| 88 | } |
| 89 | |
| 90 | static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead, |
| 91 | struct aead_request *req) |
| 92 | { |
| 93 | return (void *)ALIGN((unsigned long)(req + 1) + |
| 94 | crypto_aead_reqsize(aead), |
| 95 | __alignof__(struct scatterlist)); |
| 96 | } |
| 97 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 98 | static void esp_ssg_unref(struct xfrm_state *x, void *tmp) |
| 99 | { |
| 100 | struct esp_output_extra *extra = esp_tmp_extra(tmp); |
| 101 | struct crypto_aead *aead = x->data; |
| 102 | int extralen = 0; |
| 103 | u8 *iv; |
| 104 | struct aead_request *req; |
| 105 | struct scatterlist *sg; |
| 106 | |
| 107 | if (x->props.flags & XFRM_STATE_ESN) |
| 108 | extralen += sizeof(*extra); |
| 109 | |
| 110 | extra = esp_tmp_extra(tmp); |
| 111 | iv = esp_tmp_iv(aead, tmp, extralen); |
| 112 | req = esp_tmp_req(aead, iv); |
| 113 | |
| 114 | /* Unref skb_frag_pages in the src scatterlist if necessary. |
| 115 | * Skip the first sg which comes from skb->data. |
| 116 | */ |
| 117 | if (req->src != req->dst) |
| 118 | for (sg = sg_next(req->src); sg; sg = sg_next(sg)) |
| 119 | put_page(sg_page(sg)); |
| 120 | } |
| 121 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 122 | static void esp_output_done(struct crypto_async_request *base, int err) |
| 123 | { |
| 124 | struct sk_buff *skb = base->data; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 125 | struct xfrm_offload *xo = xfrm_offload(skb); |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 126 | void *tmp; |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 127 | struct xfrm_state *x; |
| 128 | |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 129 | if (xo && (xo->flags & XFRM_DEV_RESUME)) { |
| 130 | struct sec_path *sp = skb_sec_path(skb); |
| 131 | |
| 132 | x = sp->xvec[sp->len - 1]; |
| 133 | } else { |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 134 | x = skb_dst(skb)->xfrm; |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 135 | } |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 136 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 137 | tmp = ESP_SKB_CB(skb)->tmp; |
| 138 | esp_ssg_unref(x, tmp); |
| 139 | kfree(tmp); |
Steffen Klassert | f53c723 | 2017-12-20 10:41:36 +0100 | [diff] [blame] | 140 | |
| 141 | if (xo && (xo->flags & XFRM_DEV_RESUME)) { |
| 142 | if (err) { |
| 143 | XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR); |
| 144 | kfree_skb(skb); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | skb_push(skb, skb->data - skb_mac_header(skb)); |
| 149 | secpath_reset(skb); |
| 150 | xfrm_dev_resume(skb); |
| 151 | } else { |
| 152 | xfrm_output_resume(skb, err); |
| 153 | } |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 156 | /* Move ESP header back into place. */ |
| 157 | static void esp_restore_header(struct sk_buff *skb, unsigned int offset) |
| 158 | { |
| 159 | struct ip_esp_hdr *esph = (void *)(skb->data + offset); |
| 160 | void *tmp = ESP_SKB_CB(skb)->tmp; |
Herbert Xu | 962fcef | 2016-06-18 13:03:36 +0800 | [diff] [blame] | 161 | __be32 *seqhi = esp_tmp_extra(tmp); |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 162 | |
| 163 | esph->seq_no = esph->spi; |
| 164 | esph->spi = *seqhi; |
| 165 | } |
| 166 | |
| 167 | static void esp_output_restore_header(struct sk_buff *skb) |
| 168 | { |
Herbert Xu | 962fcef | 2016-06-18 13:03:36 +0800 | [diff] [blame] | 169 | void *tmp = ESP_SKB_CB(skb)->tmp; |
| 170 | struct esp_output_extra *extra = esp_tmp_extra(tmp); |
| 171 | |
| 172 | esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff - |
| 173 | sizeof(__be32)); |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 174 | } |
| 175 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 176 | static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb, |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 177 | struct xfrm_state *x, |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 178 | struct ip_esp_hdr *esph, |
| 179 | struct esp_output_extra *extra) |
| 180 | { |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 181 | /* For ESN we move the header forward by 4 bytes to |
| 182 | * accomodate the high bits. We will move it back after |
| 183 | * encryption. |
| 184 | */ |
| 185 | if ((x->props.flags & XFRM_STATE_ESN)) { |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 186 | __u32 seqhi; |
| 187 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 188 | |
| 189 | if (xo) |
| 190 | seqhi = xo->seq.hi; |
| 191 | else |
| 192 | seqhi = XFRM_SKB_CB(skb)->seq.output.hi; |
| 193 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 194 | extra->esphoff = (unsigned char *)esph - |
| 195 | skb_transport_header(skb); |
| 196 | esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4); |
| 197 | extra->seqhi = esph->spi; |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 198 | esph->seq_no = htonl(seqhi); |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | esph->spi = x->id.spi; |
| 202 | |
| 203 | return esph; |
| 204 | } |
| 205 | |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 206 | static void esp_output_done_esn(struct crypto_async_request *base, int err) |
| 207 | { |
| 208 | struct sk_buff *skb = base->data; |
| 209 | |
| 210 | esp_output_restore_header(skb); |
| 211 | esp_output_done(base, err); |
| 212 | } |
| 213 | |
Steffen Klassert | eb758c8 | 2017-01-17 10:23:08 +0100 | [diff] [blame] | 214 | static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto) |
| 215 | { |
| 216 | /* Fill padding... */ |
| 217 | if (tfclen) { |
| 218 | memset(tail, 0, tfclen); |
| 219 | tail += tfclen; |
| 220 | } |
| 221 | do { |
| 222 | int i; |
| 223 | for (i = 0; i < plen - 2; i++) |
| 224 | tail[i] = i + 1; |
| 225 | } while (0); |
| 226 | tail[plen - 2] = plen - 2; |
| 227 | tail[plen - 1] = proto; |
| 228 | } |
| 229 | |
Sabrina Dubroca | 8dfb4eb | 2019-03-25 14:30:00 +0100 | [diff] [blame] | 230 | static int esp_output_udp_encap(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 232 | int encap_type; |
| 233 | struct udphdr *uh; |
| 234 | __be32 *udpdata32; |
| 235 | __be16 sport, dport; |
| 236 | struct xfrm_encap_tmpl *encap = x->encap; |
| 237 | struct ip_esp_hdr *esph = esp->esph; |
Sabrina Dubroca | 8dfb4eb | 2019-03-25 14:30:00 +0100 | [diff] [blame] | 238 | unsigned int len; |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 239 | |
| 240 | spin_lock_bh(&x->lock); |
| 241 | sport = encap->encap_sport; |
| 242 | dport = encap->encap_dport; |
| 243 | encap_type = encap->encap_type; |
| 244 | spin_unlock_bh(&x->lock); |
| 245 | |
Sabrina Dubroca | 8dfb4eb | 2019-03-25 14:30:00 +0100 | [diff] [blame] | 246 | len = skb->len + esp->tailen - skb_transport_offset(skb); |
| 247 | if (len + sizeof(struct iphdr) >= IP_MAX_MTU) |
| 248 | return -EMSGSIZE; |
| 249 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 250 | uh = (struct udphdr *)esph; |
| 251 | uh->source = sport; |
| 252 | uh->dest = dport; |
Sabrina Dubroca | 8dfb4eb | 2019-03-25 14:30:00 +0100 | [diff] [blame] | 253 | uh->len = htons(len); |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 254 | uh->check = 0; |
| 255 | |
| 256 | switch (encap_type) { |
| 257 | default: |
| 258 | case UDP_ENCAP_ESPINUDP: |
| 259 | esph = (struct ip_esp_hdr *)(uh + 1); |
| 260 | break; |
| 261 | case UDP_ENCAP_ESPINUDP_NON_IKE: |
| 262 | udpdata32 = (__be32 *)(uh + 1); |
| 263 | udpdata32[0] = udpdata32[1] = 0; |
| 264 | esph = (struct ip_esp_hdr *)(udpdata32 + 2); |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | *skb_mac_header(skb) = IPPROTO_UDP; |
| 269 | esp->esph = esph; |
Sabrina Dubroca | 8dfb4eb | 2019-03-25 14:30:00 +0100 | [diff] [blame] | 270 | |
| 271 | return 0; |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp) |
| 275 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 276 | u8 *tail; |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 277 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | int nfrags; |
Steffen Klassert | 0e78a87 | 2017-05-03 08:44:27 +0200 | [diff] [blame] | 279 | int esph_offset; |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 280 | struct page *page; |
| 281 | struct sk_buff *trailer; |
| 282 | int tailen = esp->tailen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | /* this is non-NULL only with UDP Encapsulation */ |
Sabrina Dubroca | 8dfb4eb | 2019-03-25 14:30:00 +0100 | [diff] [blame] | 285 | if (x->encap) { |
| 286 | int err = esp_output_udp_encap(x, skb, esp); |
| 287 | |
| 288 | if (err < 0) |
| 289 | return err; |
| 290 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 292 | if (!skb_cloned(skb)) { |
Steffen Klassert | 54ffd79 | 2017-08-25 07:34:35 +0200 | [diff] [blame] | 293 | if (tailen <= skb_tailroom(skb)) { |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 294 | nfrags = 1; |
| 295 | trailer = skb; |
| 296 | tail = skb_tail_pointer(trailer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 298 | goto skip_cow; |
| 299 | } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS) |
| 300 | && !skb_has_frag_list(skb)) { |
| 301 | int allocsize; |
| 302 | struct sock *sk = skb->sk; |
| 303 | struct page_frag *pfrag = &x->xfrag; |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 304 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 305 | esp->inplace = false; |
| 306 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 307 | allocsize = ALIGN(tailen, L1_CACHE_BYTES); |
| 308 | |
| 309 | spin_lock_bh(&x->lock); |
| 310 | |
| 311 | if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) { |
| 312 | spin_unlock_bh(&x->lock); |
| 313 | goto cow; |
| 314 | } |
| 315 | |
| 316 | page = pfrag->page; |
| 317 | get_page(page); |
| 318 | |
| 319 | vaddr = kmap_atomic(page); |
| 320 | |
| 321 | tail = vaddr + pfrag->offset; |
| 322 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 323 | esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto); |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 324 | |
| 325 | kunmap_atomic(vaddr); |
| 326 | |
| 327 | nfrags = skb_shinfo(skb)->nr_frags; |
| 328 | |
| 329 | __skb_fill_page_desc(skb, nfrags, page, pfrag->offset, |
| 330 | tailen); |
| 331 | skb_shinfo(skb)->nr_frags = ++nfrags; |
| 332 | |
| 333 | pfrag->offset = pfrag->offset + allocsize; |
Steffen Klassert | 36ff0dd | 2017-08-25 07:16:07 +0200 | [diff] [blame] | 334 | |
| 335 | spin_unlock_bh(&x->lock); |
| 336 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 337 | nfrags++; |
| 338 | |
| 339 | skb->len += tailen; |
| 340 | skb->data_len += tailen; |
| 341 | skb->truesize += tailen; |
Martin Willi | 09db512 | 2019-01-28 09:35:35 +0100 | [diff] [blame] | 342 | if (sk && sk_fullsock(sk)) |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 343 | refcount_add(tailen, &sk->sk_wmem_alloc); |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 344 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 345 | goto out; |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 346 | } |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 347 | } |
| 348 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 349 | cow: |
Steffen Klassert | 0e78a87 | 2017-05-03 08:44:27 +0200 | [diff] [blame] | 350 | esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb); |
| 351 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 352 | nfrags = skb_cow_data(skb, tailen, &trailer); |
| 353 | if (nfrags < 0) |
| 354 | goto out; |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 355 | tail = skb_tail_pointer(trailer); |
Steffen Klassert | 0e78a87 | 2017-05-03 08:44:27 +0200 | [diff] [blame] | 356 | esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset); |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 357 | |
| 358 | skip_cow: |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 359 | esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto); |
| 360 | pskb_put(skb, trailer, tailen); |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 361 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 362 | out: |
| 363 | return nfrags; |
| 364 | } |
| 365 | EXPORT_SYMBOL_GPL(esp_output_head); |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 366 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 367 | int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp) |
| 368 | { |
| 369 | u8 *iv; |
| 370 | int alen; |
| 371 | void *tmp; |
| 372 | int ivlen; |
| 373 | int assoclen; |
| 374 | int extralen; |
| 375 | struct page *page; |
| 376 | struct ip_esp_hdr *esph; |
| 377 | struct crypto_aead *aead; |
| 378 | struct aead_request *req; |
| 379 | struct scatterlist *sg, *dsg; |
| 380 | struct esp_output_extra *extra; |
| 381 | int err = -ENOMEM; |
| 382 | |
| 383 | assoclen = sizeof(struct ip_esp_hdr); |
| 384 | extralen = 0; |
| 385 | |
| 386 | if (x->props.flags & XFRM_STATE_ESN) { |
| 387 | extralen += sizeof(*extra); |
| 388 | assoclen += sizeof(__be32); |
| 389 | } |
| 390 | |
| 391 | aead = x->data; |
| 392 | alen = crypto_aead_authsize(aead); |
| 393 | ivlen = crypto_aead_ivsize(aead); |
| 394 | |
| 395 | tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen); |
Steffen Klassert | e892d2d | 2017-04-24 07:33:56 +0200 | [diff] [blame] | 396 | if (!tmp) |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 397 | goto error; |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 398 | |
| 399 | extra = esp_tmp_extra(tmp); |
| 400 | iv = esp_tmp_iv(aead, tmp, extralen); |
| 401 | req = esp_tmp_req(aead, iv); |
| 402 | sg = esp_req_sg(aead, req); |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 403 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 404 | if (esp->inplace) |
| 405 | dsg = sg; |
| 406 | else |
| 407 | dsg = &sg[esp->nfrags]; |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 408 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 409 | esph = esp_output_set_extra(skb, x, esp->esph, extra); |
| 410 | esp->esph = esph; |
| 411 | |
| 412 | sg_init_table(sg, esp->nfrags); |
Jason A. Donenfeld | 3f29770 | 2017-06-04 04:16:23 +0200 | [diff] [blame] | 413 | err = skb_to_sgvec(skb, sg, |
| 414 | (unsigned char *)esph - skb->data, |
| 415 | assoclen + ivlen + esp->clen + alen); |
| 416 | if (unlikely(err < 0)) |
Steffen Klassert | e619492 | 2017-07-13 09:13:30 +0200 | [diff] [blame] | 417 | goto error_free; |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 418 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 419 | if (!esp->inplace) { |
| 420 | int allocsize; |
| 421 | struct page_frag *pfrag = &x->xfrag; |
| 422 | |
| 423 | allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES); |
| 424 | |
| 425 | spin_lock_bh(&x->lock); |
| 426 | if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) { |
| 427 | spin_unlock_bh(&x->lock); |
Steffen Klassert | e619492 | 2017-07-13 09:13:30 +0200 | [diff] [blame] | 428 | goto error_free; |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | skb_shinfo(skb)->nr_frags = 1; |
| 432 | |
| 433 | page = pfrag->page; |
| 434 | get_page(page); |
| 435 | /* replace page frags in skb with new page */ |
| 436 | __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len); |
| 437 | pfrag->offset = pfrag->offset + allocsize; |
| 438 | spin_unlock_bh(&x->lock); |
| 439 | |
| 440 | sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1); |
Jason A. Donenfeld | 3f29770 | 2017-06-04 04:16:23 +0200 | [diff] [blame] | 441 | err = skb_to_sgvec(skb, dsg, |
| 442 | (unsigned char *)esph - skb->data, |
| 443 | assoclen + ivlen + esp->clen + alen); |
| 444 | if (unlikely(err < 0)) |
Steffen Klassert | e619492 | 2017-07-13 09:13:30 +0200 | [diff] [blame] | 445 | goto error_free; |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 448 | if ((x->props.flags & XFRM_STATE_ESN)) |
| 449 | aead_request_set_callback(req, 0, esp_output_done_esn, skb); |
| 450 | else |
| 451 | aead_request_set_callback(req, 0, esp_output_done, skb); |
| 452 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 453 | aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv); |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 454 | aead_request_set_ad(req, assoclen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 456 | memset(iv, 0, ivlen); |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 457 | memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8), |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 458 | min(ivlen, 8)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 460 | ESP_SKB_CB(skb)->tmp = tmp; |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 461 | err = crypto_aead_encrypt(req); |
| 462 | |
| 463 | switch (err) { |
| 464 | case -EINPROGRESS: |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 465 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
Gilad Ben-Yossef | 068c2e7 | 2017-10-18 08:00:35 +0100 | [diff] [blame] | 467 | case -ENOSPC: |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 468 | err = NET_XMIT_DROP; |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 469 | break; |
| 470 | |
| 471 | case 0: |
| 472 | if ((x->props.flags & XFRM_STATE_ESN)) |
| 473 | esp_output_restore_header(skb); |
| 474 | } |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 475 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 476 | if (sg != dsg) |
| 477 | esp_ssg_unref(x, tmp); |
Herbert Xu | b7c6538 | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 478 | |
Steffen Klassert | e619492 | 2017-07-13 09:13:30 +0200 | [diff] [blame] | 479 | error_free: |
| 480 | kfree(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | error: |
| 482 | return err; |
| 483 | } |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 484 | EXPORT_SYMBOL_GPL(esp_output_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 486 | static int esp_output(struct xfrm_state *x, struct sk_buff *skb) |
| 487 | { |
| 488 | int alen; |
| 489 | int blksize; |
| 490 | struct ip_esp_hdr *esph; |
| 491 | struct crypto_aead *aead; |
| 492 | struct esp_info esp; |
| 493 | |
| 494 | esp.inplace = true; |
| 495 | |
| 496 | esp.proto = *skb_mac_header(skb); |
| 497 | *skb_mac_header(skb) = IPPROTO_ESP; |
| 498 | |
| 499 | /* skb is pure payload to encrypt */ |
| 500 | |
| 501 | aead = x->data; |
| 502 | alen = crypto_aead_authsize(aead); |
| 503 | |
| 504 | esp.tfclen = 0; |
| 505 | if (x->tfcpad) { |
| 506 | struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb); |
| 507 | u32 padto; |
| 508 | |
| 509 | padto = min(x->tfcpad, esp4_get_mtu(x, dst->child_mtu_cached)); |
| 510 | if (skb->len < padto) |
| 511 | esp.tfclen = padto - skb->len; |
| 512 | } |
| 513 | blksize = ALIGN(crypto_aead_blocksize(aead), 4); |
| 514 | esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize); |
| 515 | esp.plen = esp.clen - skb->len - esp.tfclen; |
| 516 | esp.tailen = esp.tfclen + esp.plen + alen; |
| 517 | |
| 518 | esp.esph = ip_esp_hdr(skb); |
| 519 | |
| 520 | esp.nfrags = esp_output_head(x, skb, &esp); |
| 521 | if (esp.nfrags < 0) |
| 522 | return esp.nfrags; |
| 523 | |
| 524 | esph = esp.esph; |
| 525 | esph->spi = x->id.spi; |
| 526 | |
| 527 | esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low); |
| 528 | esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low + |
| 529 | ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32)); |
| 530 | |
| 531 | skb_push(skb, -skb_network_offset(skb)); |
| 532 | |
| 533 | return esp_output_tail(x, skb, &esp); |
| 534 | } |
| 535 | |
Yossi Kuperman | 47ebcc0 | 2017-08-30 11:30:39 +0300 | [diff] [blame] | 536 | static inline int esp_remove_trailer(struct sk_buff *skb) |
| 537 | { |
| 538 | struct xfrm_state *x = xfrm_input_state(skb); |
| 539 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 540 | struct crypto_aead *aead = x->data; |
| 541 | int alen, hlen, elen; |
| 542 | int padlen, trimlen; |
| 543 | __wsum csumdiff; |
| 544 | u8 nexthdr[2]; |
| 545 | int ret; |
| 546 | |
| 547 | alen = crypto_aead_authsize(aead); |
| 548 | hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead); |
| 549 | elen = skb->len - hlen; |
| 550 | |
| 551 | if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) { |
| 552 | ret = xo->proto; |
| 553 | goto out; |
| 554 | } |
| 555 | |
| 556 | if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2)) |
| 557 | BUG(); |
| 558 | |
| 559 | ret = -EINVAL; |
| 560 | padlen = nexthdr[0]; |
| 561 | if (padlen + 2 + alen >= elen) { |
| 562 | net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n", |
| 563 | padlen + 2, elen - alen); |
| 564 | goto out; |
| 565 | } |
| 566 | |
| 567 | trimlen = alen + padlen + 2; |
| 568 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 569 | csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0); |
| 570 | skb->csum = csum_block_sub(skb->csum, csumdiff, |
| 571 | skb->len - trimlen); |
| 572 | } |
| 573 | pskb_trim(skb, skb->len - trimlen); |
| 574 | |
| 575 | ret = nexthdr[1]; |
| 576 | |
| 577 | out: |
| 578 | return ret; |
| 579 | } |
| 580 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 581 | int esp_input_done2(struct sk_buff *skb, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 583 | const struct iphdr *iph; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 584 | struct xfrm_state *x = xfrm_input_state(skb); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 585 | struct xfrm_offload *xo = xfrm_offload(skb); |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 586 | struct crypto_aead *aead = x->data; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 587 | int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead); |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 588 | int ihl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 590 | if (!xo || (xo && !(xo->flags & CRYPTO_DONE))) |
| 591 | kfree(ESP_SKB_CB(skb)->tmp); |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 592 | |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 593 | if (unlikely(err)) |
Herbert Xu | 668dc8a | 2007-12-16 15:55:02 -0800 | [diff] [blame] | 594 | goto out; |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 595 | |
Yossi Kuperman | 47ebcc0 | 2017-08-30 11:30:39 +0300 | [diff] [blame] | 596 | err = esp_remove_trailer(skb); |
| 597 | if (unlikely(err < 0)) |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 598 | goto out; |
| 599 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 600 | iph = ip_hdr(skb); |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 601 | ihl = iph->ihl * 4; |
| 602 | |
Herbert Xu | 752c1f4 | 2006-02-27 13:00:40 -0800 | [diff] [blame] | 603 | if (x->encap) { |
| 604 | struct xfrm_encap_tmpl *encap = x->encap; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 605 | struct udphdr *uh = (void *)(skb_network_header(skb) + ihl); |
Herbert Xu | 752c1f4 | 2006-02-27 13:00:40 -0800 | [diff] [blame] | 606 | |
| 607 | /* |
| 608 | * 1) if the NAT-T peer's IP or port changed then |
| 609 | * advertize the change to the keying daemon. |
| 610 | * This is an inbound SA, so just compare |
| 611 | * SRC ports. |
| 612 | */ |
| 613 | if (iph->saddr != x->props.saddr.a4 || |
| 614 | uh->source != encap->encap_sport) { |
| 615 | xfrm_address_t ipaddr; |
| 616 | |
| 617 | ipaddr.a4 = iph->saddr; |
| 618 | km_new_mapping(x, &ipaddr, uh->source); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 619 | |
Herbert Xu | 752c1f4 | 2006-02-27 13:00:40 -0800 | [diff] [blame] | 620 | /* XXX: perhaps add an extra |
| 621 | * policy check here, to see |
| 622 | * if we should allow or |
| 623 | * reject a packet from a |
| 624 | * different source |
| 625 | * address/port. |
| 626 | */ |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 627 | } |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 628 | |
Herbert Xu | 752c1f4 | 2006-02-27 13:00:40 -0800 | [diff] [blame] | 629 | /* |
| 630 | * 2) ignore UDP/TCP checksums in case |
| 631 | * of NAT-T in Transport Mode, or |
| 632 | * perform other post-processing fixes |
| 633 | * as per draft-ietf-ipsec-udp-encaps-06, |
| 634 | * section 3.1.2 |
| 635 | */ |
Herbert Xu | 8bd1707 | 2007-10-10 15:41:41 -0700 | [diff] [blame] | 636 | if (x->props.mode == XFRM_MODE_TRANSPORT) |
Herbert Xu | 752c1f4 | 2006-02-27 13:00:40 -0800 | [diff] [blame] | 637 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Ilan Tayari | ec9567a | 2017-08-01 12:49:04 +0300 | [diff] [blame] | 640 | skb_pull_rcsum(skb, hlen); |
Li RongQing | 7143dfa | 2012-12-28 16:07:16 +0800 | [diff] [blame] | 641 | if (x->props.mode == XFRM_MODE_TUNNEL) |
| 642 | skb_reset_transport_header(skb); |
| 643 | else |
| 644 | skb_set_transport_header(skb, -ihl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 646 | /* RFC4303: Drop dummy packets without any error */ |
| 647 | if (err == IPPROTO_NONE) |
| 648 | err = -EINVAL; |
| 649 | |
| 650 | out: |
| 651 | return err; |
| 652 | } |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 653 | EXPORT_SYMBOL_GPL(esp_input_done2); |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 654 | |
| 655 | static void esp_input_done(struct crypto_async_request *base, int err) |
| 656 | { |
| 657 | struct sk_buff *skb = base->data; |
| 658 | |
| 659 | xfrm_input_resume(skb, esp_input_done2(skb, err)); |
| 660 | } |
| 661 | |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 662 | static void esp_input_restore_header(struct sk_buff *skb) |
| 663 | { |
| 664 | esp_restore_header(skb, 0); |
| 665 | __skb_pull(skb, 4); |
| 666 | } |
| 667 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 668 | static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi) |
| 669 | { |
| 670 | struct xfrm_state *x = xfrm_input_state(skb); |
Colin Ian King | 60aa804 | 2018-01-30 14:53:48 +0000 | [diff] [blame] | 671 | struct ip_esp_hdr *esph; |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 672 | |
| 673 | /* For ESN we move the header forward by 4 bytes to |
| 674 | * accomodate the high bits. We will move it back after |
| 675 | * decryption. |
| 676 | */ |
| 677 | if ((x->props.flags & XFRM_STATE_ESN)) { |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 678 | esph = skb_push(skb, 4); |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 679 | *seqhi = esph->spi; |
| 680 | esph->spi = esph->seq_no; |
| 681 | esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi; |
| 682 | } |
| 683 | } |
| 684 | |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 685 | static void esp_input_done_esn(struct crypto_async_request *base, int err) |
| 686 | { |
| 687 | struct sk_buff *skb = base->data; |
| 688 | |
| 689 | esp_input_restore_header(skb); |
| 690 | esp_input_done(base, err); |
| 691 | } |
| 692 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 693 | /* |
| 694 | * Note: detecting truncated vs. non-truncated authentication data is very |
| 695 | * expensive, so we only support truncated data, which is the recommended |
| 696 | * and common case. |
| 697 | */ |
| 698 | static int esp_input(struct xfrm_state *x, struct sk_buff *skb) |
| 699 | { |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 700 | struct crypto_aead *aead = x->data; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 701 | struct aead_request *req; |
| 702 | struct sk_buff *trailer; |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 703 | int ivlen = crypto_aead_ivsize(aead); |
Haishuang Yan | 0c05f98 | 2018-08-17 15:51:00 +0800 | [diff] [blame] | 704 | int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 705 | int nfrags; |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 706 | int assoclen; |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 707 | int seqhilen; |
| 708 | __be32 *seqhi; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 709 | void *tmp; |
| 710 | u8 *iv; |
| 711 | struct scatterlist *sg; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 712 | int err = -EINVAL; |
| 713 | |
Haishuang Yan | 0c05f98 | 2018-08-17 15:51:00 +0800 | [diff] [blame] | 714 | if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen)) |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 715 | goto out; |
| 716 | |
| 717 | if (elen <= 0) |
| 718 | goto out; |
| 719 | |
Haishuang Yan | 0c05f98 | 2018-08-17 15:51:00 +0800 | [diff] [blame] | 720 | assoclen = sizeof(struct ip_esp_hdr); |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 721 | seqhilen = 0; |
| 722 | |
| 723 | if (x->props.flags & XFRM_STATE_ESN) { |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 724 | seqhilen += sizeof(__be32); |
| 725 | assoclen += seqhilen; |
| 726 | } |
| 727 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 728 | if (!skb_cloned(skb)) { |
| 729 | if (!skb_is_nonlinear(skb)) { |
| 730 | nfrags = 1; |
| 731 | |
| 732 | goto skip_cow; |
| 733 | } else if (!skb_has_frag_list(skb)) { |
| 734 | nfrags = skb_shinfo(skb)->nr_frags; |
| 735 | nfrags++; |
| 736 | |
| 737 | goto skip_cow; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | err = skb_cow_data(skb, 0, &trailer); |
| 742 | if (err < 0) |
| 743 | goto out; |
| 744 | |
| 745 | nfrags = err; |
| 746 | |
| 747 | skip_cow: |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 748 | err = -ENOMEM; |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 749 | tmp = esp_alloc_tmp(aead, nfrags, seqhilen); |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 750 | if (!tmp) |
| 751 | goto out; |
| 752 | |
| 753 | ESP_SKB_CB(skb)->tmp = tmp; |
Herbert Xu | 962fcef | 2016-06-18 13:03:36 +0800 | [diff] [blame] | 754 | seqhi = esp_tmp_extra(tmp); |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 755 | iv = esp_tmp_iv(aead, tmp, seqhilen); |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 756 | req = esp_tmp_req(aead, iv); |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 757 | sg = esp_req_sg(aead, req); |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 758 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 759 | esp_input_set_header(skb, seqhi); |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 760 | |
| 761 | sg_init_table(sg, nfrags); |
Jason A. Donenfeld | 3f29770 | 2017-06-04 04:16:23 +0200 | [diff] [blame] | 762 | err = skb_to_sgvec(skb, sg, 0, skb->len); |
Steffen Klassert | e619492 | 2017-07-13 09:13:30 +0200 | [diff] [blame] | 763 | if (unlikely(err < 0)) { |
| 764 | kfree(tmp); |
Jason A. Donenfeld | 3f29770 | 2017-06-04 04:16:23 +0200 | [diff] [blame] | 765 | goto out; |
Steffen Klassert | e619492 | 2017-07-13 09:13:30 +0200 | [diff] [blame] | 766 | } |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 767 | |
Steffen Klassert | cac2661 | 2017-01-17 10:22:57 +0100 | [diff] [blame] | 768 | skb->ip_summed = CHECKSUM_NONE; |
| 769 | |
| 770 | if ((x->props.flags & XFRM_STATE_ESN)) |
| 771 | aead_request_set_callback(req, 0, esp_input_done_esn, skb); |
| 772 | else |
| 773 | aead_request_set_callback(req, 0, esp_input_done, skb); |
| 774 | |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 775 | aead_request_set_crypt(req, sg, sg, elen + ivlen, iv); |
| 776 | aead_request_set_ad(req, assoclen); |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 777 | |
| 778 | err = crypto_aead_decrypt(req); |
| 779 | if (err == -EINPROGRESS) |
| 780 | goto out; |
| 781 | |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 782 | if ((x->props.flags & XFRM_STATE_ESN)) |
| 783 | esp_input_restore_header(skb); |
| 784 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 785 | err = esp_input_done2(skb, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
| 787 | out: |
Herbert Xu | 668dc8a | 2007-12-16 15:55:02 -0800 | [diff] [blame] | 788 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Patrick McHardy | c5c2523 | 2007-04-09 11:47:18 -0700 | [diff] [blame] | 791 | static u32 esp4_get_mtu(struct xfrm_state *x, int mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | { |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 793 | struct crypto_aead *aead = x->data; |
| 794 | u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4); |
Benjamin Poirier | 91657ea | 2012-05-24 11:32:38 +0000 | [diff] [blame] | 795 | unsigned int net_adj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 797 | switch (x->props.mode) { |
Benjamin Poirier | 91657ea | 2012-05-24 11:32:38 +0000 | [diff] [blame] | 798 | case XFRM_MODE_TRANSPORT: |
| 799 | case XFRM_MODE_BEET: |
| 800 | net_adj = sizeof(struct iphdr); |
| 801 | break; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 802 | case XFRM_MODE_TUNNEL: |
Benjamin Poirier | 91657ea | 2012-05-24 11:32:38 +0000 | [diff] [blame] | 803 | net_adj = 0; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 804 | break; |
| 805 | default: |
Benjamin Poirier | 91657ea | 2012-05-24 11:32:38 +0000 | [diff] [blame] | 806 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 808 | |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 809 | return ((mtu - x->props.header_len - crypto_aead_authsize(aead) - |
Mathias Krause | 123b0d1 | 2013-10-18 12:09:04 +0200 | [diff] [blame] | 810 | net_adj) & ~(blksize - 1)) + net_adj - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 813 | static int esp4_err(struct sk_buff *skb, u32 info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | { |
Alexey Dobriyan | 4fb236b | 2008-11-25 17:59:27 -0800 | [diff] [blame] | 815 | struct net *net = dev_net(skb->dev); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 816 | const struct iphdr *iph = (const struct iphdr *)skb->data; |
Jianjun Kong | d9319100 | 2008-11-03 00:23:42 -0800 | [diff] [blame] | 817 | struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | struct xfrm_state *x; |
| 819 | |
David S. Miller | 55be7a9 | 2012-07-11 21:27:49 -0700 | [diff] [blame] | 820 | switch (icmp_hdr(skb)->type) { |
| 821 | case ICMP_DEST_UNREACH: |
| 822 | if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 823 | return 0; |
David S. Miller | 55be7a9 | 2012-07-11 21:27:49 -0700 | [diff] [blame] | 824 | case ICMP_REDIRECT: |
| 825 | break; |
| 826 | default: |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 827 | return 0; |
David S. Miller | 55be7a9 | 2012-07-11 21:27:49 -0700 | [diff] [blame] | 828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 830 | x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, |
| 831 | esph->spi, IPPROTO_ESP, AF_INET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | if (!x) |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 833 | return 0; |
David S. Miller | 55be7a9 | 2012-07-11 21:27:49 -0700 | [diff] [blame] | 834 | |
Timo Teräs | 387aa65 | 2013-05-27 20:46:31 +0000 | [diff] [blame] | 835 | if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) |
Maciej Żenczykowski | d888f39 | 2018-09-25 20:56:26 -0700 | [diff] [blame] | 836 | ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ESP); |
Timo Teräs | 387aa65 | 2013-05-27 20:46:31 +0000 | [diff] [blame] | 837 | else |
Maciej Żenczykowski | 1042caa | 2018-09-25 20:56:27 -0700 | [diff] [blame] | 838 | ipv4_redirect(skb, net, 0, IPPROTO_ESP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | xfrm_state_put(x); |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 840 | |
| 841 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | static void esp_destroy(struct xfrm_state *x) |
| 845 | { |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 846 | struct crypto_aead *aead = x->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 848 | if (!aead) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | return; |
| 850 | |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 851 | crypto_free_aead(aead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 854 | static int esp_init_aead(struct xfrm_state *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | { |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 856 | char aead_name[CRYPTO_MAX_ALG_NAME]; |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 857 | struct crypto_aead *aead; |
| 858 | int err; |
| 859 | |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 860 | err = -ENAMETOOLONG; |
| 861 | if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", |
| 862 | x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME) |
| 863 | goto error; |
| 864 | |
Steffen Klassert | f58869c | 2017-12-20 10:41:53 +0100 | [diff] [blame] | 865 | aead = crypto_alloc_aead(aead_name, 0, 0); |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 866 | err = PTR_ERR(aead); |
| 867 | if (IS_ERR(aead)) |
| 868 | goto error; |
| 869 | |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 870 | x->data = aead; |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 871 | |
| 872 | err = crypto_aead_setkey(aead, x->aead->alg_key, |
| 873 | (x->aead->alg_key_len + 7) / 8); |
| 874 | if (err) |
| 875 | goto error; |
| 876 | |
| 877 | err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8); |
| 878 | if (err) |
| 879 | goto error; |
| 880 | |
| 881 | error: |
| 882 | return err; |
| 883 | } |
| 884 | |
| 885 | static int esp_init_authenc(struct xfrm_state *x) |
| 886 | { |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 887 | struct crypto_aead *aead; |
| 888 | struct crypto_authenc_key_param *param; |
| 889 | struct rtattr *rta; |
| 890 | char *key; |
| 891 | char *p; |
| 892 | char authenc_name[CRYPTO_MAX_ALG_NAME]; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 893 | unsigned int keylen; |
| 894 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 896 | err = -EINVAL; |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 897 | if (!x->ealg) |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 898 | goto error; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 899 | |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 900 | err = -ENAMETOOLONG; |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 901 | |
| 902 | if ((x->props.flags & XFRM_STATE_ESN)) { |
| 903 | if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 904 | "%s%sauthencesn(%s,%s)%s", |
| 905 | x->geniv ?: "", x->geniv ? "(" : "", |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 906 | x->aalg ? x->aalg->alg_name : "digest_null", |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 907 | x->ealg->alg_name, |
| 908 | x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 909 | goto error; |
| 910 | } else { |
| 911 | if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 912 | "%s%sauthenc(%s,%s)%s", |
| 913 | x->geniv ?: "", x->geniv ? "(" : "", |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 914 | x->aalg ? x->aalg->alg_name : "digest_null", |
Herbert Xu | 7021b2e | 2015-05-27 16:03:46 +0800 | [diff] [blame] | 915 | x->ealg->alg_name, |
| 916 | x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) |
Steffen Klassert | 0dc49e9 | 2011-03-08 00:07:14 +0000 | [diff] [blame] | 917 | goto error; |
| 918 | } |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 919 | |
Steffen Klassert | f58869c | 2017-12-20 10:41:53 +0100 | [diff] [blame] | 920 | aead = crypto_alloc_aead(authenc_name, 0, 0); |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 921 | err = PTR_ERR(aead); |
| 922 | if (IS_ERR(aead)) |
| 923 | goto error; |
| 924 | |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 925 | x->data = aead; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 926 | |
| 927 | keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) + |
| 928 | (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param)); |
| 929 | err = -ENOMEM; |
| 930 | key = kmalloc(keylen, GFP_KERNEL); |
| 931 | if (!key) |
| 932 | goto error; |
| 933 | |
| 934 | p = key; |
| 935 | rta = (void *)p; |
| 936 | rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM; |
| 937 | rta->rta_len = RTA_LENGTH(sizeof(*param)); |
| 938 | param = RTA_DATA(rta); |
| 939 | p += RTA_SPACE(sizeof(*param)); |
| 940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | if (x->aalg) { |
| 942 | struct xfrm_algo_desc *aalg_desc; |
| 943 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 944 | memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8); |
| 945 | p += (x->aalg->alg_key_len + 7) / 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
| 947 | aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); |
| 948 | BUG_ON(!aalg_desc); |
| 949 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 950 | err = -EINVAL; |
Joe Perches | 4508349 | 2014-11-05 15:36:08 -0800 | [diff] [blame] | 951 | if (aalg_desc->uinfo.auth.icv_fullbits / 8 != |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 952 | crypto_aead_authsize(aead)) { |
Joe Perches | 4508349 | 2014-11-05 15:36:08 -0800 | [diff] [blame] | 953 | pr_info("ESP: %s digestsize %u != %hu\n", |
| 954 | x->aalg->alg_name, |
| 955 | crypto_aead_authsize(aead), |
| 956 | aalg_desc->uinfo.auth.icv_fullbits / 8); |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 957 | goto free_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 960 | err = crypto_aead_setauthsize( |
Martin Willi | 8f8a088 | 2009-11-25 00:29:53 +0000 | [diff] [blame] | 961 | aead, x->aalg->alg_trunc_len / 8); |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 962 | if (err) |
| 963 | goto free_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
Herbert Xu | 4b7137f | 2007-10-08 17:13:44 -0700 | [diff] [blame] | 965 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 966 | param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8); |
| 967 | memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8); |
| 968 | |
| 969 | err = crypto_aead_setkey(aead, key, keylen); |
| 970 | |
| 971 | free_key: |
| 972 | kfree(key); |
| 973 | |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 974 | error: |
| 975 | return err; |
| 976 | } |
| 977 | |
| 978 | static int esp_init_state(struct xfrm_state *x) |
| 979 | { |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 980 | struct crypto_aead *aead; |
| 981 | u32 align; |
| 982 | int err; |
| 983 | |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 984 | x->data = NULL; |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 985 | |
| 986 | if (x->aead) |
| 987 | err = esp_init_aead(x); |
| 988 | else |
| 989 | err = esp_init_authenc(x); |
| 990 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 991 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | goto error; |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 993 | |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 994 | aead = x->data; |
Herbert Xu | 1a6509d | 2008-01-28 19:37:29 -0800 | [diff] [blame] | 995 | |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 996 | x->props.header_len = sizeof(struct ip_esp_hdr) + |
| 997 | crypto_aead_ivsize(aead); |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 998 | if (x->props.mode == XFRM_MODE_TUNNEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | x->props.header_len += sizeof(struct iphdr); |
Joakim Koskela | eb49e63 | 2008-08-06 02:39:30 -0700 | [diff] [blame] | 1000 | else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6) |
Patrick McHardy | ac758e3 | 2007-04-09 11:47:58 -0700 | [diff] [blame] | 1001 | x->props.header_len += IPV4_BEET_PHMAXLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | if (x->encap) { |
| 1003 | struct xfrm_encap_tmpl *encap = x->encap; |
| 1004 | |
| 1005 | switch (encap->encap_type) { |
| 1006 | default: |
Herbert Xu | bcfd09f | 2018-01-05 22:12:32 +1100 | [diff] [blame] | 1007 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | goto error; |
| 1009 | case UDP_ENCAP_ESPINUDP: |
| 1010 | x->props.header_len += sizeof(struct udphdr); |
| 1011 | break; |
| 1012 | case UDP_ENCAP_ESPINUDP_NON_IKE: |
| 1013 | x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32); |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 1017 | |
| 1018 | align = ALIGN(crypto_aead_blocksize(aead), 4); |
Mathias Krause | 1c5ad13 | 2013-10-18 12:09:05 +0200 | [diff] [blame] | 1019 | x->props.trailer_len = align + 1 + crypto_aead_authsize(aead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | error: |
Herbert Xu | 38320c7 | 2008-01-28 19:35:05 -0800 | [diff] [blame] | 1022 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 1025 | static int esp4_rcv_cb(struct sk_buff *skb, int err) |
| 1026 | { |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 1030 | static const struct xfrm_type esp_type = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | { |
| 1032 | .description = "ESP4", |
| 1033 | .owner = THIS_MODULE, |
| 1034 | .proto = IPPROTO_ESP, |
Herbert Xu | 436a0a4 | 2007-10-08 17:25:53 -0700 | [diff] [blame] | 1035 | .flags = XFRM_TYPE_REPLAY_PROT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | .init_state = esp_init_state, |
| 1037 | .destructor = esp_destroy, |
Patrick McHardy | c5c2523 | 2007-04-09 11:47:18 -0700 | [diff] [blame] | 1038 | .get_mtu = esp4_get_mtu, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | .input = esp_input, |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 1040 | .output = esp_output, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | }; |
| 1042 | |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 1043 | static struct xfrm4_protocol esp4_protocol = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | .handler = xfrm4_rcv, |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 1045 | .input_handler = xfrm_input, |
| 1046 | .cb_handler = esp4_rcv_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | .err_handler = esp4_err, |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 1048 | .priority = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | }; |
| 1050 | |
| 1051 | static int __init esp4_init(void) |
| 1052 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | if (xfrm_register_type(&esp_type, AF_INET) < 0) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 1054 | pr_info("%s: can't add xfrm type\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | return -EAGAIN; |
| 1056 | } |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 1057 | if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 1058 | pr_info("%s: can't add protocol\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | xfrm_unregister_type(&esp_type, AF_INET); |
| 1060 | return -EAGAIN; |
| 1061 | } |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static void __exit esp4_fini(void) |
| 1066 | { |
Steffen Klassert | 827789c | 2014-02-21 08:41:08 +0100 | [diff] [blame] | 1067 | if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0) |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 1068 | pr_info("%s: can't remove protocol\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | if (xfrm_unregister_type(&esp_type, AF_INET) < 0) |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 1070 | pr_info("%s: can't remove xfrm type\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | module_init(esp4_init); |
| 1074 | module_exit(esp4_fini); |
| 1075 | MODULE_LICENSE("GPL"); |
Masahide NAKAMURA | d3d6dd3 | 2007-06-26 23:57:49 -0700 | [diff] [blame] | 1076 | MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP); |