Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2002 USAGI/WIDE Project |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 13 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Authors |
| 19 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 20 | * Mitsuru KANDA @USAGI : IPv6 Support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * Kazunori MIYAZAWA @USAGI : |
| 22 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * This file is derived from net/ipv4/ah.c. |
| 25 | */ |
| 26 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 27 | #include <crypto/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <net/ip.h> |
| 30 | #include <net/ah.h> |
| 31 | #include <linux/crypto.h> |
| 32 | #include <linux/pfkeyv2.h> |
| 33 | #include <linux/string.h> |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 34 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <net/icmp.h> |
| 36 | #include <net/ipv6.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 37 | #include <net/protocol.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <net/xfrm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 40 | #define IPV6HDR_BASELEN 8 |
| 41 | |
| 42 | struct tmp_ext { |
| 43 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
| 44 | struct in6_addr saddr; |
| 45 | #endif |
| 46 | struct in6_addr daddr; |
| 47 | char hdrs[0]; |
| 48 | }; |
| 49 | |
| 50 | struct ah_skb_cb { |
| 51 | struct xfrm_skb_cb xfrm; |
| 52 | void *tmp; |
| 53 | }; |
| 54 | |
| 55 | #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0])) |
| 56 | |
| 57 | static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags, |
| 58 | unsigned int size) |
| 59 | { |
| 60 | unsigned int len; |
| 61 | |
| 62 | len = size + crypto_ahash_digestsize(ahash) + |
| 63 | (crypto_ahash_alignmask(ahash) & |
| 64 | ~(crypto_tfm_ctx_alignment() - 1)); |
| 65 | |
| 66 | len = ALIGN(len, crypto_tfm_ctx_alignment()); |
| 67 | |
| 68 | len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash); |
| 69 | len = ALIGN(len, __alignof__(struct scatterlist)); |
| 70 | |
| 71 | len += sizeof(struct scatterlist) * nfrags; |
| 72 | |
| 73 | return kmalloc(len, GFP_ATOMIC); |
| 74 | } |
| 75 | |
| 76 | static inline struct tmp_ext *ah_tmp_ext(void *base) |
| 77 | { |
| 78 | return base + IPV6HDR_BASELEN; |
| 79 | } |
| 80 | |
| 81 | static inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset) |
| 82 | { |
| 83 | return tmp + offset; |
| 84 | } |
| 85 | |
| 86 | static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp, |
| 87 | unsigned int offset) |
| 88 | { |
| 89 | return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1); |
| 90 | } |
| 91 | |
| 92 | static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash, |
| 93 | u8 *icv) |
| 94 | { |
| 95 | struct ahash_request *req; |
| 96 | |
| 97 | req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash), |
| 98 | crypto_tfm_ctx_alignment()); |
| 99 | |
| 100 | ahash_request_set_tfm(req, ahash); |
| 101 | |
| 102 | return req; |
| 103 | } |
| 104 | |
| 105 | static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash, |
| 106 | struct ahash_request *req) |
| 107 | { |
| 108 | return (void *)ALIGN((unsigned long)(req + 1) + |
| 109 | crypto_ahash_reqsize(ahash), |
| 110 | __alignof__(struct scatterlist)); |
| 111 | } |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr) |
| 114 | { |
| 115 | u8 *opt = (u8 *)opthdr; |
| 116 | int len = ipv6_optlen(opthdr); |
| 117 | int off = 0; |
| 118 | int optlen = 0; |
| 119 | |
| 120 | off += 2; |
| 121 | len -= 2; |
| 122 | |
| 123 | while (len > 0) { |
| 124 | |
| 125 | switch (opt[off]) { |
| 126 | |
| 127 | case IPV6_TLV_PAD0: |
| 128 | optlen = 1; |
| 129 | break; |
| 130 | default: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 131 | if (len < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | goto bad; |
| 133 | optlen = opt[off+1]+2; |
| 134 | if (len < optlen) |
| 135 | goto bad; |
| 136 | if (opt[off] & 0x20) |
| 137 | memset(&opt[off+2], 0, opt[off+1]); |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | off += optlen; |
| 142 | len -= optlen; |
| 143 | } |
| 144 | if (len == 0) |
| 145 | return 1; |
| 146 | |
| 147 | bad: |
| 148 | return 0; |
| 149 | } |
| 150 | |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 151 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
Masahide NAKAMURA | 27637df | 2006-08-23 19:29:47 -0700 | [diff] [blame] | 152 | /** |
| 153 | * ipv6_rearrange_destopt - rearrange IPv6 destination options header |
| 154 | * @iph: IPv6 header |
| 155 | * @destopt: destionation options header |
| 156 | */ |
| 157 | static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) |
| 158 | { |
| 159 | u8 *opt = (u8 *)destopt; |
| 160 | int len = ipv6_optlen(destopt); |
| 161 | int off = 0; |
| 162 | int optlen = 0; |
| 163 | |
| 164 | off += 2; |
| 165 | len -= 2; |
| 166 | |
| 167 | while (len > 0) { |
| 168 | |
| 169 | switch (opt[off]) { |
| 170 | |
| 171 | case IPV6_TLV_PAD0: |
| 172 | optlen = 1; |
| 173 | break; |
| 174 | default: |
| 175 | if (len < 2) |
| 176 | goto bad; |
| 177 | optlen = opt[off+1]+2; |
| 178 | if (len < optlen) |
| 179 | goto bad; |
| 180 | |
| 181 | /* Rearrange the source address in @iph and the |
| 182 | * addresses in home address option for final source. |
| 183 | * See 11.3.2 of RFC 3775 for details. |
| 184 | */ |
| 185 | if (opt[off] == IPV6_TLV_HAO) { |
| 186 | struct in6_addr final_addr; |
| 187 | struct ipv6_destopt_hao *hao; |
| 188 | |
| 189 | hao = (struct ipv6_destopt_hao *)&opt[off]; |
| 190 | if (hao->length != sizeof(hao->addr)) { |
| 191 | if (net_ratelimit()) |
| 192 | printk(KERN_WARNING "destopt hao: invalid header length: %u\n", hao->length); |
| 193 | goto bad; |
| 194 | } |
| 195 | ipv6_addr_copy(&final_addr, &hao->addr); |
| 196 | ipv6_addr_copy(&hao->addr, &iph->saddr); |
| 197 | ipv6_addr_copy(&iph->saddr, &final_addr); |
| 198 | } |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | off += optlen; |
| 203 | len -= optlen; |
| 204 | } |
YOSHIFUJI Hideaki | e731c24 | 2006-08-24 23:18:12 +0900 | [diff] [blame] | 205 | /* Note: ok if len == 0 */ |
Masahide NAKAMURA | 27637df | 2006-08-23 19:29:47 -0700 | [diff] [blame] | 206 | bad: |
| 207 | return; |
| 208 | } |
Masahide NAKAMURA | 136ebf0 | 2007-06-26 23:51:41 -0700 | [diff] [blame] | 209 | #else |
| 210 | static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {} |
Masahide NAKAMURA | 27637df | 2006-08-23 19:29:47 -0700 | [diff] [blame] | 211 | #endif |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | /** |
| 214 | * ipv6_rearrange_rthdr - rearrange IPv6 routing header |
| 215 | * @iph: IPv6 header |
| 216 | * @rthdr: routing header |
| 217 | * |
| 218 | * Rearrange the destination address in @iph and the addresses in @rthdr |
| 219 | * so that they appear in the order they will at the final destination. |
| 220 | * See Appendix A2 of RFC 2402 for details. |
| 221 | */ |
| 222 | static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr) |
| 223 | { |
| 224 | int segments, segments_left; |
| 225 | struct in6_addr *addrs; |
| 226 | struct in6_addr final_addr; |
| 227 | |
| 228 | segments_left = rthdr->segments_left; |
| 229 | if (segments_left == 0) |
| 230 | return; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 231 | rthdr->segments_left = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | /* The value of rthdr->hdrlen has been verified either by the system |
| 234 | * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming |
| 235 | * packets. So we can assume that it is even and that segments is |
| 236 | * greater than or equal to segments_left. |
| 237 | * |
| 238 | * For the same reason we can assume that this option is of type 0. |
| 239 | */ |
| 240 | segments = rthdr->hdrlen >> 1; |
| 241 | |
| 242 | addrs = ((struct rt0_hdr *)rthdr)->addr; |
| 243 | ipv6_addr_copy(&final_addr, addrs + segments - 1); |
| 244 | |
| 245 | addrs += segments - segments_left; |
| 246 | memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs)); |
| 247 | |
| 248 | ipv6_addr_copy(addrs, &iph->daddr); |
| 249 | ipv6_addr_copy(&iph->daddr, &final_addr); |
| 250 | } |
| 251 | |
Masahide NAKAMURA | 27637df | 2006-08-23 19:29:47 -0700 | [diff] [blame] | 252 | static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
| 254 | union { |
| 255 | struct ipv6hdr *iph; |
| 256 | struct ipv6_opt_hdr *opth; |
| 257 | struct ipv6_rt_hdr *rth; |
| 258 | char *raw; |
| 259 | } exthdr = { .iph = iph }; |
| 260 | char *end = exthdr.raw + len; |
| 261 | int nexthdr = iph->nexthdr; |
| 262 | |
| 263 | exthdr.iph++; |
| 264 | |
| 265 | while (exthdr.raw < end) { |
| 266 | switch (nexthdr) { |
Masahide NAKAMURA | 27637df | 2006-08-23 19:29:47 -0700 | [diff] [blame] | 267 | case NEXTHDR_DEST: |
| 268 | if (dir == XFRM_POLICY_OUT) |
| 269 | ipv6_rearrange_destopt(iph, exthdr.opth); |
YOSHIFUJI Hideaki | e731c24 | 2006-08-24 23:18:12 +0900 | [diff] [blame] | 270 | case NEXTHDR_HOP: |
| 271 | if (!zero_out_mutable_opts(exthdr.opth)) { |
| 272 | LIMIT_NETDEBUG( |
| 273 | KERN_WARNING "overrun %sopts\n", |
| 274 | nexthdr == NEXTHDR_HOP ? |
| 275 | "hop" : "dest"); |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | case NEXTHDR_ROUTING: |
| 281 | ipv6_rearrange_rthdr(iph, exthdr.rth); |
| 282 | break; |
| 283 | |
| 284 | default : |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | nexthdr = exthdr.opth->nexthdr; |
| 289 | exthdr.raw += ipv6_optlen(exthdr.opth); |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 295 | static void ah6_output_done(struct crypto_async_request *base, int err) |
| 296 | { |
| 297 | int extlen; |
| 298 | u8 *iph_base; |
| 299 | u8 *icv; |
| 300 | struct sk_buff *skb = base->data; |
| 301 | struct xfrm_state *x = skb_dst(skb)->xfrm; |
| 302 | struct ah_data *ahp = x->data; |
| 303 | struct ipv6hdr *top_iph = ipv6_hdr(skb); |
| 304 | struct ip_auth_hdr *ah = ip_auth_hdr(skb); |
| 305 | struct tmp_ext *iph_ext; |
| 306 | |
| 307 | extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr); |
| 308 | if (extlen) |
| 309 | extlen += sizeof(*iph_ext); |
| 310 | |
| 311 | iph_base = AH_SKB_CB(skb)->tmp; |
| 312 | iph_ext = ah_tmp_ext(iph_base); |
| 313 | icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen); |
| 314 | |
| 315 | memcpy(ah->auth_data, icv, ahp->icv_trunc_len); |
| 316 | memcpy(top_iph, iph_base, IPV6HDR_BASELEN); |
| 317 | |
| 318 | if (extlen) { |
| 319 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
| 320 | memcpy(&top_iph->saddr, iph_ext, extlen); |
| 321 | #else |
| 322 | memcpy(&top_iph->daddr, iph_ext, extlen); |
| 323 | #endif |
| 324 | } |
| 325 | |
| 326 | err = ah->nexthdr; |
| 327 | |
| 328 | kfree(AH_SKB_CB(skb)->tmp); |
| 329 | xfrm_output_resume(skb, err); |
| 330 | } |
| 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | static int ah6_output(struct xfrm_state *x, struct sk_buff *skb) |
| 333 | { |
| 334 | int err; |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 335 | int nfrags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | int extlen; |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 337 | u8 *iph_base; |
| 338 | u8 *icv; |
| 339 | u8 nexthdr; |
| 340 | struct sk_buff *trailer; |
| 341 | struct crypto_ahash *ahash; |
| 342 | struct ahash_request *req; |
| 343 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | struct ipv6hdr *top_iph; |
| 345 | struct ip_auth_hdr *ah; |
| 346 | struct ah_data *ahp; |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 347 | struct tmp_ext *iph_ext; |
| 348 | |
| 349 | ahp = x->data; |
| 350 | ahash = ahp->ahash; |
| 351 | |
| 352 | if ((err = skb_cow_data(skb, 0, &trailer)) < 0) |
| 353 | goto out; |
| 354 | nfrags = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Herbert Xu | 7b277b1 | 2007-10-10 15:44:06 -0700 | [diff] [blame] | 356 | skb_push(skb, -skb_network_offset(skb)); |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 357 | extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr); |
| 358 | if (extlen) |
| 359 | extlen += sizeof(*iph_ext); |
| 360 | |
| 361 | err = -ENOMEM; |
| 362 | iph_base = ah_alloc_tmp(ahash, nfrags, IPV6HDR_BASELEN + extlen); |
| 363 | if (!iph_base) |
| 364 | goto out; |
| 365 | |
| 366 | iph_ext = ah_tmp_ext(iph_base); |
| 367 | icv = ah_tmp_icv(ahash, iph_ext, extlen); |
| 368 | req = ah_tmp_req(ahash, icv); |
| 369 | sg = ah_req_sg(ahash, req); |
| 370 | |
| 371 | ah = ip_auth_hdr(skb); |
| 372 | memset(ah->auth_data, 0, ahp->icv_trunc_len); |
| 373 | |
Herbert Xu | 007f021 | 2007-10-09 13:25:59 -0700 | [diff] [blame] | 374 | top_iph = ipv6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | top_iph->payload_len = htons(skb->len - sizeof(*top_iph)); |
| 376 | |
Herbert Xu | 007f021 | 2007-10-09 13:25:59 -0700 | [diff] [blame] | 377 | nexthdr = *skb_mac_header(skb); |
| 378 | *skb_mac_header(skb) = IPPROTO_AH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | /* When there are no extension headers, we only need to save the first |
| 381 | * 8 bytes of the base IP header. |
| 382 | */ |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 383 | memcpy(iph_base, top_iph, IPV6HDR_BASELEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (extlen) { |
Masahide NAKAMURA | 59fbb3a | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 386 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 387 | memcpy(iph_ext, &top_iph->saddr, extlen); |
YOSHIFUJI Hideaki | e731c24 | 2006-08-24 23:18:12 +0900 | [diff] [blame] | 388 | #else |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 389 | memcpy(iph_ext, &top_iph->daddr, extlen); |
YOSHIFUJI Hideaki | e731c24 | 2006-08-24 23:18:12 +0900 | [diff] [blame] | 390 | #endif |
Masahide NAKAMURA | 27637df | 2006-08-23 19:29:47 -0700 | [diff] [blame] | 391 | err = ipv6_clear_mutable_options(top_iph, |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 392 | extlen - sizeof(*iph_ext) + |
Masahide NAKAMURA | 27637df | 2006-08-23 19:29:47 -0700 | [diff] [blame] | 393 | sizeof(*top_iph), |
| 394 | XFRM_POLICY_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (err) |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 396 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | ah->nexthdr = nexthdr; |
| 400 | |
| 401 | top_iph->priority = 0; |
| 402 | top_iph->flow_lbl[0] = 0; |
| 403 | top_iph->flow_lbl[1] = 0; |
| 404 | top_iph->flow_lbl[2] = 0; |
| 405 | top_iph->hop_limit = 0; |
| 406 | |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 407 | ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
| 409 | ah->reserved = 0; |
| 410 | ah->spi = x->id.spi; |
Herbert Xu | b318e0e | 2008-02-12 22:50:35 -0800 | [diff] [blame] | 411 | ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output); |
Herbert Xu | b7c6538 | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 412 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 413 | sg_init_table(sg, nfrags); |
| 414 | skb_to_sgvec(skb, sg, 0, skb->len); |
Herbert Xu | b7c6538 | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 415 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 416 | ahash_request_set_crypt(req, sg, icv, skb->len); |
| 417 | ahash_request_set_callback(req, 0, ah6_output_done, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 419 | AH_SKB_CB(skb)->tmp = iph_base; |
| 420 | |
| 421 | err = crypto_ahash_digest(req); |
| 422 | if (err) { |
| 423 | if (err == -EINPROGRESS) |
| 424 | goto out; |
| 425 | |
| 426 | if (err == -EBUSY) |
| 427 | err = NET_XMIT_DROP; |
| 428 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 431 | memcpy(ah->auth_data, icv, ahp->icv_trunc_len); |
| 432 | memcpy(top_iph, iph_base, IPV6HDR_BASELEN); |
| 433 | |
| 434 | if (extlen) { |
| 435 | #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) |
| 436 | memcpy(&top_iph->saddr, iph_ext, extlen); |
| 437 | #else |
| 438 | memcpy(&top_iph->daddr, iph_ext, extlen); |
| 439 | #endif |
| 440 | } |
| 441 | |
| 442 | out_free: |
| 443 | kfree(iph_base); |
| 444 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return err; |
| 446 | } |
| 447 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 448 | static void ah6_input_done(struct crypto_async_request *base, int err) |
| 449 | { |
| 450 | u8 *auth_data; |
| 451 | u8 *icv; |
| 452 | u8 *work_iph; |
| 453 | struct sk_buff *skb = base->data; |
| 454 | struct xfrm_state *x = xfrm_input_state(skb); |
| 455 | struct ah_data *ahp = x->data; |
| 456 | struct ip_auth_hdr *ah = ip_auth_hdr(skb); |
| 457 | int hdr_len = skb_network_header_len(skb); |
| 458 | int ah_hlen = (ah->hdrlen + 2) << 2; |
| 459 | |
| 460 | work_iph = AH_SKB_CB(skb)->tmp; |
| 461 | auth_data = ah_tmp_auth(work_iph, hdr_len); |
| 462 | icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len); |
| 463 | |
| 464 | err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0; |
| 465 | if (err) |
| 466 | goto out; |
| 467 | |
| 468 | skb->network_header += ah_hlen; |
| 469 | memcpy(skb_network_header(skb), work_iph, hdr_len); |
| 470 | __skb_pull(skb, ah_hlen + hdr_len); |
| 471 | skb_set_transport_header(skb, -hdr_len); |
| 472 | |
| 473 | err = ah->nexthdr; |
| 474 | out: |
| 475 | kfree(AH_SKB_CB(skb)->tmp); |
| 476 | xfrm_input_resume(skb, err); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | |
Herbert Xu | e695633 | 2006-04-01 00:52:46 -0800 | [diff] [blame] | 481 | static int ah6_input(struct xfrm_state *x, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
| 483 | /* |
| 484 | * Before process AH |
| 485 | * [IPv6][Ext1][Ext2][AH][Dest][Payload] |
| 486 | * |<-------------->| hdr_len |
| 487 | * |
| 488 | * To erase AH: |
| 489 | * Keeping copy of cleared headers. After AH processing, |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 490 | * Moving the pointer of skb->network_header by using skb_pull as long |
| 491 | * as AH header length. Then copy back the copy as long as hdr_len |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | * If destination header following AH exists, copy it into after [Ext2]. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 493 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | * |<>|[IPv6][Ext1][Ext2][Dest][Payload] |
| 495 | * There is offset of AH before IPv6 header after the process. |
| 496 | */ |
| 497 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 498 | u8 *auth_data; |
| 499 | u8 *icv; |
| 500 | u8 *work_iph; |
| 501 | struct sk_buff *trailer; |
| 502 | struct crypto_ahash *ahash; |
| 503 | struct ahash_request *req; |
| 504 | struct scatterlist *sg; |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 505 | struct ip_auth_hdr *ah; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 506 | struct ipv6hdr *ip6h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | struct ah_data *ahp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | u16 hdr_len; |
| 509 | u16 ah_hlen; |
| 510 | int nexthdr; |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 511 | int nfrags; |
| 512 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr))) |
| 515 | goto out; |
| 516 | |
| 517 | /* We are going to _remove_ AH header to keep sockets happy, |
| 518 | * so... Later this can change. */ |
| 519 | if (skb_cloned(skb) && |
| 520 | pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) |
| 521 | goto out; |
| 522 | |
Herbert Xu | 7aa68cb | 2007-10-17 21:30:07 -0700 | [diff] [blame] | 523 | skb->ip_summed = CHECKSUM_NONE; |
| 524 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 525 | hdr_len = skb_network_header_len(skb); |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 526 | ah = (struct ip_auth_hdr *)skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | ahp = x->data; |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 528 | ahash = ahp->ahash; |
| 529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | nexthdr = ah->nexthdr; |
| 531 | ah_hlen = (ah->hdrlen + 2) << 2; |
| 532 | |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 533 | if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) && |
| 534 | ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len)) |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 535 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | if (!pskb_may_pull(skb, ah_hlen)) |
| 538 | goto out; |
| 539 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 540 | ip6h = ipv6_hdr(skb); |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 541 | |
| 542 | skb_push(skb, hdr_len); |
| 543 | |
| 544 | if ((err = skb_cow_data(skb, 0, &trailer)) < 0) |
| 545 | goto out; |
| 546 | nfrags = err; |
| 547 | |
| 548 | work_iph = ah_alloc_tmp(ahash, nfrags, hdr_len + ahp->icv_trunc_len); |
| 549 | if (!work_iph) |
| 550 | goto out; |
| 551 | |
| 552 | auth_data = ah_tmp_auth(work_iph, hdr_len); |
| 553 | icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len); |
| 554 | req = ah_tmp_req(ahash, icv); |
| 555 | sg = ah_req_sg(ahash, req); |
| 556 | |
| 557 | memcpy(work_iph, ip6h, hdr_len); |
| 558 | memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len); |
| 559 | memset(ah->auth_data, 0, ahp->icv_trunc_len); |
| 560 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 561 | if (ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN)) |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 562 | goto out_free; |
| 563 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 564 | ip6h->priority = 0; |
| 565 | ip6h->flow_lbl[0] = 0; |
| 566 | ip6h->flow_lbl[1] = 0; |
| 567 | ip6h->flow_lbl[2] = 0; |
| 568 | ip6h->hop_limit = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 570 | sg_init_table(sg, nfrags); |
| 571 | skb_to_sgvec(skb, sg, 0, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 573 | ahash_request_set_crypt(req, sg, icv, skb->len); |
| 574 | ahash_request_set_callback(req, 0, ah6_input_done, skb); |
| 575 | |
| 576 | AH_SKB_CB(skb)->tmp = work_iph; |
| 577 | |
| 578 | err = crypto_ahash_digest(req); |
| 579 | if (err) { |
| 580 | if (err == -EINPROGRESS) |
| 581 | goto out; |
| 582 | |
| 583 | if (err == -EBUSY) |
| 584 | err = NET_XMIT_DROP; |
| 585 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 587 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 588 | err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0; |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 589 | if (err) |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 590 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 592 | skb->network_header += ah_hlen; |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 593 | memcpy(skb_network_header(skb), work_iph, hdr_len); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 594 | skb->transport_header = skb->network_header; |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 595 | __skb_pull(skb, ah_hlen + hdr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 597 | err = nexthdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 599 | out_free: |
| 600 | kfree(work_iph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | out: |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 602 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 605 | static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 606 | u8 type, u8 code, int offset, __be32 info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
Alexey Dobriyan | 4fb236b | 2008-11-25 17:59:27 -0800 | [diff] [blame] | 608 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | struct ipv6hdr *iph = (struct ipv6hdr*)skb->data; |
| 610 | struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset); |
| 611 | struct xfrm_state *x; |
| 612 | |
| 613 | if (type != ICMPV6_DEST_UNREACH && |
| 614 | type != ICMPV6_PKT_TOOBIG) |
| 615 | return; |
| 616 | |
Alexey Dobriyan | 4fb236b | 2008-11-25 17:59:27 -0800 | [diff] [blame] | 617 | x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | if (!x) |
| 619 | return; |
| 620 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 621 | NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/%pI6\n", |
Harvey Harrison | 0c6ce78 | 2008-10-28 16:09:23 -0700 | [diff] [blame] | 622 | ntohl(ah->spi), &iph->daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | xfrm_state_put(x); |
| 625 | } |
| 626 | |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 627 | static int ah6_init_state(struct xfrm_state *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
| 629 | struct ah_data *ahp = NULL; |
| 630 | struct xfrm_algo_desc *aalg_desc; |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 631 | struct crypto_ahash *ahash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
| 633 | if (!x->aalg) |
| 634 | goto error; |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | if (x->encap) |
| 637 | goto error; |
| 638 | |
Ingo Oeser | 0c600ed | 2006-03-20 23:01:32 -0800 | [diff] [blame] | 639 | ahp = kzalloc(sizeof(*ahp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | if (ahp == NULL) |
| 641 | return -ENOMEM; |
| 642 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 643 | ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0); |
| 644 | if (IS_ERR(ahash)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | goto error; |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 646 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 647 | ahp->ahash = ahash; |
| 648 | if (crypto_ahash_setkey(ahash, x->aalg->alg_key, |
Herbert Xu | bc31d3b | 2007-10-08 17:14:34 -0700 | [diff] [blame] | 649 | (x->aalg->alg_key_len + 7) / 8)) |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 650 | goto error; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | /* |
| 653 | * Lookup the algorithm description maintained by xfrm_algo, |
| 654 | * verify crypto transform properties, and store information |
| 655 | * we need for AH processing. This lookup cannot fail here |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 656 | * after a successful crypto_alloc_hash(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | */ |
| 658 | aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); |
| 659 | BUG_ON(!aalg_desc); |
| 660 | |
| 661 | if (aalg_desc->uinfo.auth.icv_fullbits/8 != |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 662 | crypto_ahash_digestsize(ahash)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | printk(KERN_INFO "AH: %s digestsize %u != %hu\n", |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 664 | x->aalg->alg_name, crypto_ahash_digestsize(ahash), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | aalg_desc->uinfo.auth.icv_fullbits/8); |
| 666 | goto error; |
| 667 | } |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; |
| 670 | ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 673 | |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 674 | x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + |
| 675 | ahp->icv_trunc_len); |
Herbert Xu | ca68145 | 2007-10-17 21:35:15 -0700 | [diff] [blame] | 676 | switch (x->props.mode) { |
| 677 | case XFRM_MODE_BEET: |
| 678 | case XFRM_MODE_TRANSPORT: |
| 679 | break; |
| 680 | case XFRM_MODE_TUNNEL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | x->props.header_len += sizeof(struct ipv6hdr); |
Masahide NAKAMURA | ea2c47b | 2007-10-22 02:30:15 -0700 | [diff] [blame] | 682 | break; |
Herbert Xu | ca68145 | 2007-10-17 21:35:15 -0700 | [diff] [blame] | 683 | default: |
| 684 | goto error; |
| 685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | x->data = ahp; |
| 687 | |
| 688 | return 0; |
| 689 | |
| 690 | error: |
| 691 | if (ahp) { |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 692 | crypto_free_ahash(ahp->ahash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | kfree(ahp); |
| 694 | } |
| 695 | return -EINVAL; |
| 696 | } |
| 697 | |
| 698 | static void ah6_destroy(struct xfrm_state *x) |
| 699 | { |
| 700 | struct ah_data *ahp = x->data; |
| 701 | |
| 702 | if (!ahp) |
| 703 | return; |
| 704 | |
Steffen Klassert | 8631e9b | 2009-10-07 22:49:01 +0000 | [diff] [blame] | 705 | crypto_free_ahash(ahp->ahash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | kfree(ahp); |
| 707 | } |
| 708 | |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 709 | static const struct xfrm_type ah6_type = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
| 711 | .description = "AH6", |
| 712 | .owner = THIS_MODULE, |
| 713 | .proto = IPPROTO_AH, |
Herbert Xu | 436a0a4 | 2007-10-08 17:25:53 -0700 | [diff] [blame] | 714 | .flags = XFRM_TYPE_REPLAY_PROT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | .init_state = ah6_init_state, |
| 716 | .destructor = ah6_destroy, |
| 717 | .input = ah6_input, |
Masahide NAKAMURA | aee5adb | 2006-08-23 17:57:28 -0700 | [diff] [blame] | 718 | .output = ah6_output, |
| 719 | .hdr_offset = xfrm6_find_1stfragopt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | }; |
| 721 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 722 | static const struct inet6_protocol ah6_protocol = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | .handler = xfrm6_rcv, |
| 724 | .err_handler = ah6_err, |
| 725 | .flags = INET6_PROTO_NOPOLICY, |
| 726 | }; |
| 727 | |
| 728 | static int __init ah6_init(void) |
| 729 | { |
| 730 | if (xfrm_register_type(&ah6_type, AF_INET6) < 0) { |
| 731 | printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n"); |
| 732 | return -EAGAIN; |
| 733 | } |
| 734 | |
| 735 | if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) { |
| 736 | printk(KERN_INFO "ipv6 ah init: can't add protocol\n"); |
| 737 | xfrm_unregister_type(&ah6_type, AF_INET6); |
| 738 | return -EAGAIN; |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static void __exit ah6_fini(void) |
| 745 | { |
| 746 | if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0) |
| 747 | printk(KERN_INFO "ipv6 ah close: can't remove protocol\n"); |
| 748 | |
| 749 | if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0) |
| 750 | printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n"); |
| 751 | |
| 752 | } |
| 753 | |
| 754 | module_init(ah6_init); |
| 755 | module_exit(ah6_fini); |
| 756 | |
| 757 | MODULE_LICENSE("GPL"); |
Masahide NAKAMURA | d3d6dd3 | 2007-06-26 23:57:49 -0700 | [diff] [blame] | 758 | MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH); |