| 1 | |
| 2 | #define pr_fmt(fmt) "IPsec: " fmt
|
| 3 |
|
| 4 | #include <crypto/hash.h>
|
| 5 | #include <crypto/utils.h>
|
| 6 | #include <linux/err.h>
|
| 7 | #include <linux/module.h>
|
| 8 | #include <linux/slab.h>
|
| 9 | #include <net/ip.h>
|
| 10 | #include <net/xfrm.h>
|
| 11 | #include <net/ah.h>
|
| 12 | #include <linux/crypto.h>
|
| 13 | #include <linux/pfkeyv2.h>
|
| 14 | #include <linux/scatterlist.h>
|
| 15 | #include <net/icmp.h>
|
| 16 | #include <net/protocol.h>
|
| 17 |
|
| 18 | struct ah_skb_cb {
|
| 19 | struct xfrm_skb_cb xfrm;
|
| 20 | void *tmp;
|
| 21 | };
|
| 22 |
|
| 23 | #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
|
| 24 |
|
| 25 | static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
|
| 26 | unsigned int size)
|
| 27 | {
|
| 28 | unsigned int len;
|
| 29 |
|
| 30 | len = size + crypto_ahash_digestsize(ahash);
|
| 31 |
|
| 32 | len = ALIGN(len, crypto_tfm_ctx_alignment());
|
| 33 |
|
| 34 | len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
|
| 35 | len = ALIGN(len, __alignof__(struct scatterlist));
|
| 36 |
|
| 37 | len += sizeof(struct scatterlist) * nfrags;
|
| 38 |
|
| 39 | return kmalloc(len, GFP_ATOMIC);
|
| 40 | }
|
| 41 |
|
| 42 | static inline u8 *ah_tmp_auth(void *tmp, unsigned int offset)
|
| 43 | {
|
| 44 | return tmp + offset;
|
| 45 | }
|
| 46 |
|
| 47 | static inline u8 *ah_tmp_icv(void *tmp, unsigned int offset)
|
| 48 | {
|
| 49 | return tmp + offset;
|
| 50 | }
|
| 51 |
|
| 52 | static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
|
| 53 | u8 *icv)
|
| 54 | {
|
| 55 | struct ahash_request *req;
|
| 56 |
|
| 57 | req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
|
| 58 | crypto_tfm_ctx_alignment());
|
| 59 |
|
| 60 | ahash_request_set_tfm(req, ahash);
|
| 61 |
|
| 62 | return req;
|
| 63 | }
|
| 64 |
|
| 65 | static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
|
| 66 | struct ahash_request *req)
|
| 67 | {
|
| 68 | return (void *)ALIGN((unsigned long)(req + 1) +
|
| 69 | crypto_ahash_reqsize(ahash),
|
| 70 | __alignof__(struct scatterlist));
|
| 71 | }
|
| 72 |
|
| 73 | |
| 74 | * into IP header for icv calculation. Options are already checked
|
| 75 | * for validity, so paranoia is not required.
|
| 76 |
|
| 77 | static int ip_clear_mutable_options(const struct iphdr *iph, __be32 *daddr)
|
| 78 | {
|
| 79 | unsigned char *optptr = (unsigned char *)(iph+1);
|
| 80 | int l = iph->ihl*4 - sizeof(struct iphdr);
|
| 81 | int optlen;
|
| 82 |
|
| 83 | while (l > 0) {
|
| 84 | switch (*optptr) {
|
| 85 | case IPOPT_END:
|
| 86 | return 0;
|
| 87 | case IPOPT_NOOP:
|
| 88 | l--;
|
| 89 | optptr++;
|
| 90 | continue;
|
| 91 | }
|
| 92 | optlen = optptr[1];
|
| 93 | if (optlen<2 || optlen>l)
|
| 94 | return -EINVAL;
|
| 95 | switch (*optptr) {
|
| 96 | case IPOPT_SEC:
|
| 97 | case 0x85:
|
| 98 | case IPOPT_CIPSO:
|
| 99 | case IPOPT_RA:
|
| 100 | case 0x80|21:
|
| 101 | break;
|
| 102 | case IPOPT_LSRR:
|
| 103 | case IPOPT_SSRR:
|
| 104 | if (optlen < 6)
|
| 105 | return -EINVAL;
|
| 106 | memcpy(daddr, optptr+optlen-4, 4);
|
| 107 | fallthrough;
|
| 108 | default:
|
| 109 | memset(optptr, 0, optlen);
|
| 110 | }
|
| 111 | l -= optlen;
|
| 112 | optptr += optlen;
|
| 113 | }
|
| 114 | return 0;
|
| 115 | }
|
| 116 |
|
| 117 | static void ah_output_done(void *data, int err)
|
| 118 | {
|
| 119 | u8 *icv;
|
| 120 | struct iphdr *iph;
|
| 121 | struct sk_buff *skb = data;
|
| 122 | struct xfrm_state *x = skb_dst(skb)->xfrm;
|
| 123 | struct ah_data *ahp = x->data;
|
| 124 | struct iphdr *top_iph = ip_hdr(skb);
|
| 125 | struct ip_auth_hdr *ah = ip_auth_hdr(skb);
|
| 126 | int ihl = ip_hdrlen(skb);
|
| 127 | int seqhi_len = 0;
|
| 128 | __be32 *seqhi;
|
| 129 |
|
| 130 | if (x->props.flags & XFRM_STATE_ESN)
|
| 131 | seqhi_len = sizeof(*seqhi);
|
| 132 | iph = AH_SKB_CB(skb)->tmp;
|
| 133 | seqhi = (__be32 *)((char *)iph + ihl);
|
| 134 | icv = ah_tmp_icv(seqhi, seqhi_len);
|
| 135 | memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
|
| 136 |
|
| 137 | top_iph->tos = iph->tos;
|
| 138 | top_iph->ttl = iph->ttl;
|
| 139 | top_iph->frag_off = iph->frag_off;
|
| 140 | if (top_iph->ihl != 5) {
|
| 141 | top_iph->daddr = iph->daddr;
|
| 142 | memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
|
| 143 | }
|
| 144 |
|
| 145 | kfree(AH_SKB_CB(skb)->tmp);
|
| 146 | xfrm_output_resume(skb_to_full_sk(skb), skb, err);
|
| 147 | }
|
| 148 |
|
| 149 | static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
|
| 150 | {
|
| 151 | int err;
|
| 152 | int nfrags;
|
| 153 | int ihl;
|
| 154 | u8 *icv;
|
| 155 | struct sk_buff *trailer;
|
| 156 | struct crypto_ahash *ahash;
|
| 157 | struct ahash_request *req;
|
| 158 | struct scatterlist *sg;
|
| 159 | struct iphdr *iph, *top_iph;
|
| 160 | struct ip_auth_hdr *ah;
|
| 161 | struct ah_data *ahp;
|
| 162 | int seqhi_len = 0;
|
| 163 | __be32 *seqhi;
|
| 164 | int sglists = 0;
|
| 165 | struct scatterlist *seqhisg;
|
| 166 |
|
| 167 | ahp = x->data;
|
| 168 | ahash = ahp->ahash;
|
| 169 |
|
| 170 | if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
|
| 171 | goto out;
|
| 172 | nfrags = err;
|
| 173 |
|
| 174 | skb_push(skb, -skb_network_offset(skb));
|
| 175 | ah = ip_auth_hdr(skb);
|
| 176 | ihl = ip_hdrlen(skb);
|
| 177 |
|
| 178 | if (x->props.flags & XFRM_STATE_ESN) {
|
| 179 | sglists = 1;
|
| 180 | seqhi_len = sizeof(*seqhi);
|
| 181 | }
|
| 182 | err = -ENOMEM;
|
| 183 | iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl + seqhi_len);
|
| 184 | if (!iph)
|
| 185 | goto out;
|
| 186 | seqhi = (__be32 *)((char *)iph + ihl);
|
| 187 | icv = ah_tmp_icv(seqhi, seqhi_len);
|
| 188 | req = ah_tmp_req(ahash, icv);
|
| 189 | sg = ah_req_sg(ahash, req);
|
| 190 | seqhisg = sg + nfrags;
|
| 191 |
|
| 192 | memset(ah->auth_data, 0, ahp->icv_trunc_len);
|
| 193 |
|
| 194 | top_iph = ip_hdr(skb);
|
| 195 |
|
| 196 | iph->tos = top_iph->tos;
|
| 197 | iph->ttl = top_iph->ttl;
|
| 198 | iph->frag_off = top_iph->frag_off;
|
| 199 |
|
| 200 | if (top_iph->ihl != 5) {
|
| 201 | iph->daddr = top_iph->daddr;
|
| 202 | memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
|
| 203 | err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
|
| 204 | if (err)
|
| 205 | goto out_free;
|
| 206 | }
|
| 207 |
|
| 208 | ah->nexthdr = *skb_mac_header(skb);
|
| 209 | *skb_mac_header(skb) = IPPROTO_AH;
|
| 210 |
|
| 211 | top_iph->tos = 0;
|
| 212 | top_iph->tot_len = htons(skb->len);
|
| 213 | top_iph->frag_off = 0;
|
| 214 | top_iph->ttl = 0;
|
| 215 | top_iph->check = 0;
|
| 216 |
|
| 217 | if (x->props.flags & XFRM_STATE_ALIGN4)
|
| 218 | ah->hdrlen = (XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
|
| 219 | else
|
| 220 | ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
|
| 221 |
|
| 222 | ah->reserved = 0;
|
| 223 | ah->spi = x->id.spi;
|
| 224 | ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
|
| 225 |
|
| 226 | sg_init_table(sg, nfrags + sglists);
|
| 227 | err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
|
| 228 | if (unlikely(err < 0))
|
| 229 | goto out_free;
|
| 230 |
|
| 231 | if (x->props.flags & XFRM_STATE_ESN) {
|
| 232 |
|
| 233 | *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
|
| 234 | sg_set_buf(seqhisg, seqhi, seqhi_len);
|
| 235 | }
|
| 236 | ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
|
| 237 | ahash_request_set_callback(req, 0, ah_output_done, skb);
|
| 238 |
|
| 239 | AH_SKB_CB(skb)->tmp = iph;
|
| 240 |
|
| 241 | err = crypto_ahash_digest(req);
|
| 242 | if (err) {
|
| 243 | if (err == -EINPROGRESS)
|
| 244 | goto out;
|
| 245 |
|
| 246 | if (err == -ENOSPC)
|
| 247 | err = NET_XMIT_DROP;
|
| 248 | goto out_free;
|
| 249 | }
|
| 250 |
|
| 251 | memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
|
| 252 |
|
| 253 | top_iph->tos = iph->tos;
|
| 254 | top_iph->ttl = iph->ttl;
|
| 255 | top_iph->frag_off = iph->frag_off;
|
| 256 | if (top_iph->ihl != 5) {
|
| 257 | top_iph->daddr = iph->daddr;
|
| 258 | memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
|
| 259 | }
|
| 260 |
|
| 261 | out_free:
|
| 262 | kfree(iph);
|
| 263 | out:
|
| 264 | return err;
|
| 265 | }
|
| 266 |
|
| 267 | static void ah_input_done(void *data, int err)
|
| 268 | {
|
| 269 | u8 *auth_data;
|
| 270 | u8 *icv;
|
| 271 | struct iphdr *work_iph;
|
| 272 | struct sk_buff *skb = data;
|
| 273 | struct xfrm_state *x = xfrm_input_state(skb);
|
| 274 | struct ah_data *ahp = x->data;
|
| 275 | struct ip_auth_hdr *ah = ip_auth_hdr(skb);
|
| 276 | int ihl = ip_hdrlen(skb);
|
| 277 | int ah_hlen = (ah->hdrlen + 2) << 2;
|
| 278 | int seqhi_len = 0;
|
| 279 | __be32 *seqhi;
|
| 280 |
|
| 281 | if (err)
|
| 282 | goto out;
|
| 283 |
|
| 284 | if (x->props.flags & XFRM_STATE_ESN)
|
| 285 | seqhi_len = sizeof(*seqhi);
|
| 286 | work_iph = AH_SKB_CB(skb)->tmp;
|
| 287 | seqhi = (__be32 *)((char *)work_iph + ihl);
|
| 288 | auth_data = ah_tmp_auth(seqhi, seqhi_len);
|
| 289 | icv = ah_tmp_icv(auth_data, ahp->icv_trunc_len);
|
| 290 |
|
| 291 | err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
|
| 292 | if (err)
|
| 293 | goto out;
|
| 294 |
|
| 295 | err = ah->nexthdr;
|
| 296 |
|
| 297 | skb->network_header += ah_hlen;
|
| 298 | memcpy(skb_network_header(skb), work_iph, ihl);
|
| 299 | __skb_pull(skb, ah_hlen + ihl);
|
| 300 |
|
| 301 | if (x->props.mode == XFRM_MODE_TUNNEL)
|
| 302 | skb_reset_transport_header(skb);
|
| 303 | else
|
| 304 | skb_set_transport_header(skb, -ihl);
|
| 305 | out:
|
| 306 | kfree(AH_SKB_CB(skb)->tmp);
|
| 307 | xfrm_input_resume(skb, err);
|
| 308 | }
|
| 309 |
|
| 310 | static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
|
| 311 | {
|
| 312 | int ah_hlen;
|
| 313 | int ihl;
|
| 314 | int nexthdr;
|
| 315 | int nfrags;
|
| 316 | u8 *auth_data;
|
| 317 | u8 *icv;
|
| 318 | struct sk_buff *trailer;
|
| 319 | struct crypto_ahash *ahash;
|
| 320 | struct ahash_request *req;
|
| 321 | struct scatterlist *sg;
|
| 322 | struct iphdr *iph, *work_iph;
|
| 323 | struct ip_auth_hdr *ah;
|
| 324 | struct ah_data *ahp;
|
| 325 | int err = -ENOMEM;
|
| 326 | int seqhi_len = 0;
|
| 327 | __be32 *seqhi;
|
| 328 | int sglists = 0;
|
| 329 | struct scatterlist *seqhisg;
|
| 330 |
|
| 331 | if (!pskb_may_pull(skb, sizeof(*ah)))
|
| 332 | goto out;
|
| 333 |
|
| 334 | ah = (struct ip_auth_hdr *)skb->data;
|
| 335 | ahp = x->data;
|
| 336 | ahash = ahp->ahash;
|
| 337 |
|
| 338 | nexthdr = ah->nexthdr;
|
| 339 | ah_hlen = (ah->hdrlen + 2) << 2;
|
| 340 |
|
| 341 | if (x->props.flags & XFRM_STATE_ALIGN4) {
|
| 342 | if (ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_full_len) &&
|
| 343 | ah_hlen != XFRM_ALIGN4(sizeof(*ah) + ahp->icv_trunc_len))
|
| 344 | goto out;
|
| 345 | } else {
|
| 346 | if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
|
| 347 | ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
|
| 348 | goto out;
|
| 349 | }
|
| 350 |
|
| 351 | if (!pskb_may_pull(skb, ah_hlen))
|
| 352 | goto out;
|
| 353 |
|
| 354 | |
| 355 | * so... Later this can change.
|
| 356 | if (skb_unclone(skb, GFP_ATOMIC))
|
| 357 | goto out;
|
| 358 |
|
| 359 | skb->ip_summed = CHECKSUM_NONE;
|
| 360 |
|
| 361 |
|
| 362 | if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
|
| 363 | goto out;
|
| 364 | nfrags = err;
|
| 365 |
|
| 366 | ah = (struct ip_auth_hdr *)skb->data;
|
| 367 | iph = ip_hdr(skb);
|
| 368 | ihl = ip_hdrlen(skb);
|
| 369 |
|
| 370 | if (x->props.flags & XFRM_STATE_ESN) {
|
| 371 | sglists = 1;
|
| 372 | seqhi_len = sizeof(*seqhi);
|
| 373 | }
|
| 374 |
|
| 375 | work_iph = ah_alloc_tmp(ahash, nfrags + sglists, ihl +
|
| 376 | ahp->icv_trunc_len + seqhi_len);
|
| 377 | if (!work_iph) {
|
| 378 | err = -ENOMEM;
|
| 379 | goto out;
|
| 380 | }
|
| 381 |
|
| 382 | seqhi = (__be32 *)((char *)work_iph + ihl);
|
| 383 | auth_data = ah_tmp_auth(seqhi, seqhi_len);
|
| 384 | icv = ah_tmp_icv(auth_data, ahp->icv_trunc_len);
|
| 385 | req = ah_tmp_req(ahash, icv);
|
| 386 | sg = ah_req_sg(ahash, req);
|
| 387 | seqhisg = sg + nfrags;
|
| 388 |
|
| 389 | memcpy(work_iph, iph, ihl);
|
| 390 | memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
|
| 391 | memset(ah->auth_data, 0, ahp->icv_trunc_len);
|
| 392 |
|
| 393 | iph->ttl = 0;
|
| 394 | iph->tos = 0;
|
| 395 | iph->frag_off = 0;
|
| 396 | iph->check = 0;
|
| 397 | if (ihl > sizeof(*iph)) {
|
| 398 | __be32 dummy;
|
| 399 | err = ip_clear_mutable_options(iph, &dummy);
|
| 400 | if (err)
|
| 401 | goto out_free;
|
| 402 | }
|
| 403 |
|
| 404 | skb_push(skb, ihl);
|
| 405 |
|
| 406 | sg_init_table(sg, nfrags + sglists);
|
| 407 | err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
|
| 408 | if (unlikely(err < 0))
|
| 409 | goto out_free;
|
| 410 |
|
| 411 | if (x->props.flags & XFRM_STATE_ESN) {
|
| 412 |
|
| 413 | *seqhi = XFRM_SKB_CB(skb)->seq.input.hi;
|
| 414 | sg_set_buf(seqhisg, seqhi, seqhi_len);
|
| 415 | }
|
| 416 | ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
|
| 417 | ahash_request_set_callback(req, 0, ah_input_done, skb);
|
| 418 |
|
| 419 | AH_SKB_CB(skb)->tmp = work_iph;
|
| 420 |
|
| 421 | err = crypto_ahash_digest(req);
|
| 422 | if (err) {
|
| 423 | if (err == -EINPROGRESS)
|
| 424 | goto out;
|
| 425 |
|
| 426 | goto out_free;
|
| 427 | }
|
| 428 |
|
| 429 | err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
|
| 430 | if (err)
|
| 431 | goto out_free;
|
| 432 |
|
| 433 | skb->network_header += ah_hlen;
|
| 434 | memcpy(skb_network_header(skb), work_iph, ihl);
|
| 435 | __skb_pull(skb, ah_hlen + ihl);
|
| 436 | if (x->props.mode == XFRM_MODE_TUNNEL)
|
| 437 | skb_reset_transport_header(skb);
|
| 438 | else
|
| 439 | skb_set_transport_header(skb, -ihl);
|
| 440 |
|
| 441 | err = nexthdr;
|
| 442 |
|
| 443 | out_free:
|
| 444 | kfree (work_iph);
|
| 445 | out:
|
| 446 | return err;
|
| 447 | }
|
| 448 |
|
| 449 | static int ah4_err(struct sk_buff *skb, u32 info)
|
| 450 | {
|
| 451 | struct net *net = dev_net(skb->dev);
|
| 452 | const struct iphdr *iph = (const struct iphdr *)skb->data;
|
| 453 | struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
|
| 454 | struct xfrm_state *x;
|
| 455 |
|
| 456 | switch (icmp_hdr(skb)->type) {
|
| 457 | case ICMP_DEST_UNREACH:
|
| 458 | if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
|
| 459 | return 0;
|
| 460 | break;
|
| 461 | case ICMP_REDIRECT:
|
| 462 | break;
|
| 463 | default:
|
| 464 | return 0;
|
| 465 | }
|
| 466 |
|
| 467 | x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
|
| 468 | ah->spi, IPPROTO_AH, AF_INET);
|
| 469 | if (!x)
|
| 470 | return 0;
|
| 471 |
|
| 472 | if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
|
| 473 | ipv4_update_pmtu(skb, net, info, 0, IPPROTO_AH);
|
| 474 | else
|
| 475 | ipv4_redirect(skb, net, 0, IPPROTO_AH);
|
| 476 | xfrm_state_put(x);
|
| 477 |
|
| 478 | return 0;
|
| 479 | }
|
| 480 |
|
| 481 | static int ah_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
|
| 482 | {
|
| 483 | struct ah_data *ahp = NULL;
|
| 484 | struct xfrm_algo_desc *aalg_desc;
|
| 485 | struct crypto_ahash *ahash;
|
| 486 |
|
| 487 | if (!x->aalg) {
|
| 488 | NL_SET_ERR_MSG(extack, "AH requires a state with an AUTH algorithm");
|
| 489 | goto error;
|
| 490 | }
|
| 491 |
|
| 492 | if (x->encap) {
|
| 493 | NL_SET_ERR_MSG(extack, "AH is not compatible with encapsulation");
|
| 494 | goto error;
|
| 495 | }
|
| 496 |
|
| 497 | ahp = kzalloc_obj(*ahp);
|
| 498 | if (!ahp)
|
| 499 | return -ENOMEM;
|
| 500 |
|
| 501 | ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
|
| 502 | if (IS_ERR(ahash)) {
|
| 503 | NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
|
| 504 | goto error;
|
| 505 | }
|
| 506 |
|
| 507 | ahp->ahash = ahash;
|
| 508 | if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
|
| 509 | (x->aalg->alg_key_len + 7) / 8)) {
|
| 510 | NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
|
| 511 | goto error;
|
| 512 | }
|
| 513 |
|
| 514 | |
| 515 | * Lookup the algorithm description maintained by xfrm_algo,
|
| 516 | * verify crypto transform properties, and store information
|
| 517 | * we need for AH processing. This lookup cannot fail here
|
| 518 | * after a successful crypto_alloc_ahash().
|
| 519 |
|
| 520 | aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
|
| 521 | BUG_ON(!aalg_desc);
|
| 522 |
|
| 523 | if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
|
| 524 | crypto_ahash_digestsize(ahash)) {
|
| 525 | NL_SET_ERR_MSG(extack, "Kernel was unable to initialize cryptographic operations");
|
| 526 | goto error;
|
| 527 | }
|
| 528 |
|
| 529 | ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
|
| 530 | ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
|
| 531 |
|
| 532 | if (x->props.flags & XFRM_STATE_ALIGN4)
|
| 533 | x->props.header_len = XFRM_ALIGN4(sizeof(struct ip_auth_hdr) +
|
| 534 | ahp->icv_trunc_len);
|
| 535 | else
|
| 536 | x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
|
| 537 | ahp->icv_trunc_len);
|
| 538 | if (x->props.mode == XFRM_MODE_TUNNEL)
|
| 539 | x->props.header_len += sizeof(struct iphdr);
|
| 540 | x->data = ahp;
|
| 541 |
|
| 542 | return 0;
|
| 543 |
|
| 544 | error:
|
| 545 | if (ahp) {
|
| 546 | crypto_free_ahash(ahp->ahash);
|
| 547 | kfree(ahp);
|
| 548 | }
|
| 549 | return -EINVAL;
|
| 550 | }
|
| 551 |
|
| 552 | static void ah_destroy(struct xfrm_state *x)
|
| 553 | {
|
| 554 | struct ah_data *ahp = x->data;
|
| 555 |
|
| 556 | if (!ahp)
|
| 557 | return;
|
| 558 |
|
| 559 | crypto_free_ahash(ahp->ahash);
|
| 560 | kfree(ahp);
|
| 561 | }
|
| 562 |
|
| 563 | static int ah4_rcv_cb(struct sk_buff *skb, int err)
|
| 564 | {
|
| 565 | return 0;
|
| 566 | }
|
| 567 |
|
| 568 | static const struct xfrm_type ah_type =
|
| 569 | {
|
| 570 | .owner = THIS_MODULE,
|
| 571 | .proto = IPPROTO_AH,
|
| 572 | .flags = XFRM_TYPE_REPLAY_PROT,
|
| 573 | .init_state = ah_init_state,
|
| 574 | .destructor = ah_destroy,
|
| 575 | .input = ah_input,
|
| 576 | .output = ah_output
|
| 577 | };
|
| 578 |
|
| 579 | static struct xfrm4_protocol ah4_protocol = {
|
| 580 | .handler = xfrm4_rcv,
|
| 581 | .input_handler = xfrm_input,
|
| 582 | .cb_handler = ah4_rcv_cb,
|
| 583 | .err_handler = ah4_err,
|
| 584 | .priority = 0,
|
| 585 | };
|
| 586 |
|
| 587 | static int __init ah4_init(void)
|
| 588 | {
|
| 589 | if (xfrm_register_type(&ah_type, AF_INET) < 0) {
|
| 590 | pr_info("%s: can't add xfrm type\n", __func__);
|
| 591 | return -EAGAIN;
|
| 592 | }
|
| 593 | if (xfrm4_protocol_register(&ah4_protocol, IPPROTO_AH) < 0) {
|
| 594 | pr_info("%s: can't add protocol\n", __func__);
|
| 595 | xfrm_unregister_type(&ah_type, AF_INET);
|
| 596 | return -EAGAIN;
|
| 597 | }
|
| 598 | return 0;
|
| 599 | }
|
| 600 |
|
| 601 | static void __exit ah4_fini(void)
|
| 602 | {
|
| 603 | if (xfrm4_protocol_deregister(&ah4_protocol, IPPROTO_AH) < 0)
|
| 604 | pr_info("%s: can't remove protocol\n", __func__);
|
| 605 | xfrm_unregister_type(&ah_type, AF_INET);
|
| 606 | }
|
| 607 |
|
| 608 | module_init(ah4_init);
|
| 609 | module_exit(ah4_fini);
|
| 610 | MODULE_DESCRIPTION("IPv4 AH transformation library");
|
| 611 | MODULE_LICENSE("GPL");
|
| 612 | MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);
|