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