Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 1 | #include <crypto/hash.h> |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 2 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | #include <linux/module.h> |
| 4 | #include <net/ip.h> |
| 5 | #include <net/xfrm.h> |
| 6 | #include <net/ah.h> |
| 7 | #include <linux/crypto.h> |
| 8 | #include <linux/pfkeyv2.h> |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 9 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <net/icmp.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 11 | #include <net/protocol.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 13 | struct ah_skb_cb { |
| 14 | struct xfrm_skb_cb xfrm; |
| 15 | void *tmp; |
| 16 | }; |
| 17 | |
| 18 | #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0])) |
| 19 | |
| 20 | static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags, |
| 21 | unsigned int size) |
| 22 | { |
| 23 | unsigned int len; |
| 24 | |
| 25 | len = size + crypto_ahash_digestsize(ahash) + |
| 26 | (crypto_ahash_alignmask(ahash) & |
| 27 | ~(crypto_tfm_ctx_alignment() - 1)); |
| 28 | |
| 29 | len = ALIGN(len, crypto_tfm_ctx_alignment()); |
| 30 | |
| 31 | len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash); |
| 32 | len = ALIGN(len, __alignof__(struct scatterlist)); |
| 33 | |
| 34 | len += sizeof(struct scatterlist) * nfrags; |
| 35 | |
| 36 | return kmalloc(len, GFP_ATOMIC); |
| 37 | } |
| 38 | |
| 39 | static inline u8 *ah_tmp_auth(void *tmp, unsigned int offset) |
| 40 | { |
| 41 | return tmp + offset; |
| 42 | } |
| 43 | |
| 44 | static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp, |
| 45 | unsigned int offset) |
| 46 | { |
| 47 | return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1); |
| 48 | } |
| 49 | |
| 50 | static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash, |
| 51 | u8 *icv) |
| 52 | { |
| 53 | struct ahash_request *req; |
| 54 | |
| 55 | req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash), |
| 56 | crypto_tfm_ctx_alignment()); |
| 57 | |
| 58 | ahash_request_set_tfm(req, ahash); |
| 59 | |
| 60 | return req; |
| 61 | } |
| 62 | |
| 63 | static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash, |
| 64 | struct ahash_request *req) |
| 65 | { |
| 66 | return (void *)ALIGN((unsigned long)(req + 1) + |
| 67 | crypto_ahash_reqsize(ahash), |
| 68 | __alignof__(struct scatterlist)); |
| 69 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | /* Clear mutable options and find final destination to substitute |
| 72 | * into IP header for icv calculation. Options are already checked |
| 73 | * for validity, so paranoia is not required. */ |
| 74 | |
Al Viro | d5a0a1e | 2006-11-08 00:23:14 -0800 | [diff] [blame] | 75 | static int ip_clear_mutable_options(struct iphdr *iph, __be32 *daddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | unsigned char * optptr = (unsigned char*)(iph+1); |
| 78 | int l = iph->ihl*4 - sizeof(struct iphdr); |
| 79 | int optlen; |
| 80 | |
| 81 | while (l > 0) { |
| 82 | switch (*optptr) { |
| 83 | case IPOPT_END: |
| 84 | return 0; |
| 85 | case IPOPT_NOOP: |
| 86 | l--; |
| 87 | optptr++; |
| 88 | continue; |
| 89 | } |
| 90 | optlen = optptr[1]; |
| 91 | if (optlen<2 || optlen>l) |
| 92 | return -EINVAL; |
| 93 | switch (*optptr) { |
| 94 | case IPOPT_SEC: |
| 95 | case 0x85: /* Some "Extended Security" crap. */ |
Paul Moore | 11a03f7 | 2006-08-03 16:46:20 -0700 | [diff] [blame] | 96 | case IPOPT_CIPSO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | case IPOPT_RA: |
| 98 | case 0x80|21: /* RFC1770 */ |
| 99 | break; |
| 100 | case IPOPT_LSRR: |
| 101 | case IPOPT_SSRR: |
| 102 | if (optlen < 6) |
| 103 | return -EINVAL; |
| 104 | memcpy(daddr, optptr+optlen-4, 4); |
| 105 | /* Fall through */ |
| 106 | default: |
Nick Bowler | 96fe1c0 | 2007-08-22 12:33:51 -0700 | [diff] [blame] | 107 | memset(optptr, 0, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | l -= optlen; |
| 110 | optptr += optlen; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 115 | static void ah_output_done(struct crypto_async_request *base, int err) |
| 116 | { |
| 117 | u8 *icv; |
| 118 | struct iphdr *iph; |
| 119 | struct sk_buff *skb = base->data; |
| 120 | struct xfrm_state *x = skb_dst(skb)->xfrm; |
| 121 | struct ah_data *ahp = x->data; |
| 122 | struct iphdr *top_iph = ip_hdr(skb); |
| 123 | struct ip_auth_hdr *ah = ip_auth_hdr(skb); |
| 124 | int ihl = ip_hdrlen(skb); |
| 125 | |
| 126 | iph = AH_SKB_CB(skb)->tmp; |
| 127 | icv = ah_tmp_icv(ahp->ahash, iph, ihl); |
| 128 | memcpy(ah->auth_data, icv, ahp->icv_trunc_len); |
| 129 | |
| 130 | top_iph->tos = iph->tos; |
| 131 | top_iph->ttl = iph->ttl; |
| 132 | top_iph->frag_off = iph->frag_off; |
| 133 | if (top_iph->ihl != 5) { |
| 134 | top_iph->daddr = iph->daddr; |
| 135 | memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); |
| 136 | } |
| 137 | |
| 138 | err = ah->nexthdr; |
| 139 | |
| 140 | kfree(AH_SKB_CB(skb)->tmp); |
| 141 | xfrm_output_resume(skb, err); |
| 142 | } |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | static int ah_output(struct xfrm_state *x, struct sk_buff *skb) |
| 145 | { |
| 146 | int err; |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 147 | int nfrags; |
| 148 | int ihl; |
| 149 | u8 *icv; |
| 150 | struct sk_buff *trailer; |
| 151 | struct crypto_ahash *ahash; |
| 152 | struct ahash_request *req; |
| 153 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | struct iphdr *iph, *top_iph; |
| 155 | struct ip_auth_hdr *ah; |
| 156 | struct ah_data *ahp; |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 157 | |
| 158 | ahp = x->data; |
| 159 | ahash = ahp->ahash; |
| 160 | |
| 161 | if ((err = skb_cow_data(skb, 0, &trailer)) < 0) |
| 162 | goto out; |
| 163 | nfrags = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Herbert Xu | 7b277b1 | 2007-10-10 15:44:06 -0700 | [diff] [blame] | 165 | skb_push(skb, -skb_network_offset(skb)); |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 166 | ah = ip_auth_hdr(skb); |
| 167 | ihl = ip_hdrlen(skb); |
| 168 | |
| 169 | err = -ENOMEM; |
| 170 | iph = ah_alloc_tmp(ahash, nfrags, ihl); |
| 171 | if (!iph) |
| 172 | goto out; |
| 173 | |
| 174 | icv = ah_tmp_icv(ahash, iph, ihl); |
| 175 | req = ah_tmp_req(ahash, icv); |
| 176 | sg = ah_req_sg(ahash, req); |
| 177 | |
| 178 | memset(ah->auth_data, 0, ahp->icv_trunc_len); |
| 179 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 180 | top_iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | iph->tos = top_iph->tos; |
| 183 | iph->ttl = top_iph->ttl; |
| 184 | iph->frag_off = top_iph->frag_off; |
| 185 | |
| 186 | if (top_iph->ihl != 5) { |
| 187 | iph->daddr = top_iph->daddr; |
| 188 | memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); |
| 189 | err = ip_clear_mutable_options(top_iph, &top_iph->daddr); |
| 190 | if (err) |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 191 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Herbert Xu | 37fedd3 | 2007-10-10 15:44:44 -0700 | [diff] [blame] | 194 | ah->nexthdr = *skb_mac_header(skb); |
| 195 | *skb_mac_header(skb) = IPPROTO_AH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | top_iph->tos = 0; |
| 198 | top_iph->tot_len = htons(skb->len); |
| 199 | top_iph->frag_off = 0; |
| 200 | top_iph->ttl = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | top_iph->check = 0; |
| 202 | |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 203 | ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | ah->reserved = 0; |
| 206 | ah->spi = x->id.spi; |
Herbert Xu | b318e0e | 2008-02-12 22:50:35 -0800 | [diff] [blame] | 207 | ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output); |
Herbert Xu | b7c6538 | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 208 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 209 | sg_init_table(sg, nfrags); |
| 210 | skb_to_sgvec(skb, sg, 0, skb->len); |
Herbert Xu | b7c6538 | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 211 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 212 | ahash_request_set_crypt(req, sg, icv, skb->len); |
| 213 | ahash_request_set_callback(req, 0, ah_output_done, skb); |
| 214 | |
| 215 | AH_SKB_CB(skb)->tmp = iph; |
| 216 | |
| 217 | err = crypto_ahash_digest(req); |
| 218 | if (err) { |
| 219 | if (err == -EINPROGRESS) |
| 220 | goto out; |
| 221 | |
| 222 | if (err == -EBUSY) |
| 223 | err = NET_XMIT_DROP; |
| 224 | goto out_free; |
| 225 | } |
| 226 | |
| 227 | memcpy(ah->auth_data, icv, ahp->icv_trunc_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | top_iph->tos = iph->tos; |
| 230 | top_iph->ttl = iph->ttl; |
| 231 | top_iph->frag_off = iph->frag_off; |
| 232 | if (top_iph->ihl != 5) { |
| 233 | top_iph->daddr = iph->daddr; |
| 234 | memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr)); |
| 235 | } |
| 236 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 237 | out_free: |
| 238 | kfree(iph); |
| 239 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return err; |
| 241 | } |
| 242 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 243 | static void ah_input_done(struct crypto_async_request *base, int err) |
| 244 | { |
| 245 | u8 *auth_data; |
| 246 | u8 *icv; |
| 247 | struct iphdr *work_iph; |
| 248 | struct sk_buff *skb = base->data; |
| 249 | struct xfrm_state *x = xfrm_input_state(skb); |
| 250 | struct ah_data *ahp = x->data; |
| 251 | struct ip_auth_hdr *ah = ip_auth_hdr(skb); |
| 252 | int ihl = ip_hdrlen(skb); |
| 253 | int ah_hlen = (ah->hdrlen + 2) << 2; |
| 254 | |
| 255 | work_iph = AH_SKB_CB(skb)->tmp; |
| 256 | auth_data = ah_tmp_auth(work_iph, ihl); |
| 257 | icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len); |
| 258 | |
| 259 | err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0; |
| 260 | if (err) |
| 261 | goto out; |
| 262 | |
| 263 | skb->network_header += ah_hlen; |
| 264 | memcpy(skb_network_header(skb), work_iph, ihl); |
| 265 | __skb_pull(skb, ah_hlen + ihl); |
| 266 | skb_set_transport_header(skb, -ihl); |
| 267 | |
| 268 | err = ah->nexthdr; |
| 269 | out: |
| 270 | kfree(AH_SKB_CB(skb)->tmp); |
| 271 | xfrm_input_resume(skb, err); |
| 272 | } |
| 273 | |
Herbert Xu | e695633 | 2006-04-01 00:52:46 -0800 | [diff] [blame] | 274 | static int ah_input(struct xfrm_state *x, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
| 276 | int ah_hlen; |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 277 | int ihl; |
Herbert Xu | 631a6698 | 2007-10-10 15:46:21 -0700 | [diff] [blame] | 278 | int nexthdr; |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 279 | int nfrags; |
| 280 | u8 *auth_data; |
| 281 | u8 *icv; |
| 282 | struct sk_buff *trailer; |
| 283 | struct crypto_ahash *ahash; |
| 284 | struct ahash_request *req; |
| 285 | struct scatterlist *sg; |
| 286 | struct iphdr *iph, *work_iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | struct ip_auth_hdr *ah; |
| 288 | struct ah_data *ahp; |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 289 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 291 | if (!pskb_may_pull(skb, sizeof(*ah))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | goto out; |
| 293 | |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 294 | ah = (struct ip_auth_hdr *)skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | ahp = x->data; |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 296 | ahash = ahp->ahash; |
| 297 | |
Herbert Xu | 631a6698 | 2007-10-10 15:46:21 -0700 | [diff] [blame] | 298 | nexthdr = ah->nexthdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | ah_hlen = (ah->hdrlen + 2) << 2; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 300 | |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 301 | if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) && |
| 302 | ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | goto out; |
| 304 | |
| 305 | if (!pskb_may_pull(skb, ah_hlen)) |
| 306 | goto out; |
| 307 | |
| 308 | /* We are going to _remove_ AH header to keep sockets happy, |
| 309 | * so... Later this can change. */ |
| 310 | if (skb_cloned(skb) && |
| 311 | pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) |
| 312 | goto out; |
| 313 | |
| 314 | skb->ip_summed = CHECKSUM_NONE; |
| 315 | |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 316 | ah = (struct ip_auth_hdr *)skb->data; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 317 | iph = ip_hdr(skb); |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 318 | ihl = ip_hdrlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 320 | if ((err = skb_cow_data(skb, 0, &trailer)) < 0) |
| 321 | goto out; |
| 322 | nfrags = err; |
| 323 | |
| 324 | work_iph = ah_alloc_tmp(ahash, nfrags, ihl + ahp->icv_trunc_len); |
| 325 | if (!work_iph) |
| 326 | goto out; |
| 327 | |
| 328 | auth_data = ah_tmp_auth(work_iph, ihl); |
| 329 | icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len); |
| 330 | req = ah_tmp_req(ahash, icv); |
| 331 | sg = ah_req_sg(ahash, req); |
| 332 | |
| 333 | memcpy(work_iph, iph, ihl); |
| 334 | memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len); |
| 335 | memset(ah->auth_data, 0, ahp->icv_trunc_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | iph->ttl = 0; |
| 338 | iph->tos = 0; |
| 339 | iph->frag_off = 0; |
| 340 | iph->check = 0; |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 341 | if (ihl > sizeof(*iph)) { |
Al Viro | d5a0a1e | 2006-11-08 00:23:14 -0800 | [diff] [blame] | 342 | __be32 dummy; |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 343 | err = ip_clear_mutable_options(iph, &dummy); |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 344 | if (err) |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 345 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 347 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 348 | skb_push(skb, ihl); |
| 349 | |
| 350 | sg_init_table(sg, nfrags); |
| 351 | skb_to_sgvec(skb, sg, 0, skb->len); |
| 352 | |
| 353 | ahash_request_set_crypt(req, sg, icv, skb->len); |
| 354 | ahash_request_set_callback(req, 0, ah_input_done, skb); |
| 355 | |
| 356 | AH_SKB_CB(skb)->tmp = work_iph; |
| 357 | |
| 358 | err = crypto_ahash_digest(req); |
| 359 | if (err) { |
| 360 | if (err == -EINPROGRESS) |
| 361 | goto out; |
| 362 | |
| 363 | if (err == -EBUSY) |
| 364 | err = NET_XMIT_DROP; |
| 365 | goto out_free; |
| 366 | } |
| 367 | |
| 368 | err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0; |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 369 | if (err) |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 370 | goto out_free; |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 371 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 372 | skb->network_header += ah_hlen; |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 373 | memcpy(skb_network_header(skb), work_iph, ihl); |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 374 | __skb_pull(skb, ah_hlen + ihl); |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 375 | skb_set_transport_header(skb, -ihl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 377 | err = nexthdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 379 | out_free: |
| 380 | kfree (work_iph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | out: |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 382 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static void ah4_err(struct sk_buff *skb, u32 info) |
| 386 | { |
Alexey Dobriyan | 4fb236b | 2008-11-25 17:59:27 -0800 | [diff] [blame] | 387 | struct net *net = dev_net(skb->dev); |
Jianjun Kong | d9319100 | 2008-11-03 00:23:42 -0800 | [diff] [blame] | 388 | struct iphdr *iph = (struct iphdr *)skb->data; |
| 389 | struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | struct xfrm_state *x; |
| 391 | |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 392 | if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH || |
| 393 | icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | return; |
| 395 | |
Alexey Dobriyan | 4fb236b | 2008-11-25 17:59:27 -0800 | [diff] [blame] | 396 | x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (!x) |
| 398 | return; |
| 399 | printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n", |
| 400 | ntohl(ah->spi), ntohl(iph->daddr)); |
| 401 | xfrm_state_put(x); |
| 402 | } |
| 403 | |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 404 | static int ah_init_state(struct xfrm_state *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
| 406 | struct ah_data *ahp = NULL; |
| 407 | struct xfrm_algo_desc *aalg_desc; |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 408 | struct crypto_ahash *ahash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
| 410 | if (!x->aalg) |
| 411 | goto error; |
| 412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | if (x->encap) |
| 414 | goto error; |
| 415 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 416 | ahp = kzalloc(sizeof(*ahp), GFP_KERNEL); |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 417 | if (!ahp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return -ENOMEM; |
| 419 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 420 | ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0); |
| 421 | if (IS_ERR(ahash)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | goto error; |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 423 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 424 | ahp->ahash = ahash; |
| 425 | if (crypto_ahash_setkey(ahash, x->aalg->alg_key, |
| 426 | (x->aalg->alg_key_len + 7) / 8)) |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 427 | goto error; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | /* |
| 430 | * Lookup the algorithm description maintained by xfrm_algo, |
| 431 | * verify crypto transform properties, and store information |
| 432 | * we need for AH processing. This lookup cannot fail here |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 433 | * after a successful crypto_alloc_ahash(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | */ |
| 435 | aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); |
| 436 | BUG_ON(!aalg_desc); |
| 437 | |
| 438 | if (aalg_desc->uinfo.auth.icv_fullbits/8 != |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 439 | crypto_ahash_digestsize(ahash)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | printk(KERN_INFO "AH: %s digestsize %u != %hu\n", |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 441 | x->aalg->alg_name, crypto_ahash_digestsize(ahash), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | aalg_desc->uinfo.auth.icv_fullbits/8); |
| 443 | goto error; |
| 444 | } |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; |
Martin Willi | 8f8a088 | 2009-11-25 00:29:53 +0000 | [diff] [blame] | 447 | ahp->icv_trunc_len = x->aalg->alg_trunc_len/8; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 450 | |
Herbert Xu | 87bdc48 | 2007-10-10 15:45:25 -0700 | [diff] [blame] | 451 | x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) + |
| 452 | ahp->icv_trunc_len); |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 453 | if (x->props.mode == XFRM_MODE_TUNNEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | x->props.header_len += sizeof(struct iphdr); |
| 455 | x->data = ahp; |
| 456 | |
| 457 | return 0; |
| 458 | |
| 459 | error: |
| 460 | if (ahp) { |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 461 | crypto_free_ahash(ahp->ahash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | kfree(ahp); |
| 463 | } |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | |
| 467 | static void ah_destroy(struct xfrm_state *x) |
| 468 | { |
| 469 | struct ah_data *ahp = x->data; |
| 470 | |
| 471 | if (!ahp) |
| 472 | return; |
| 473 | |
Steffen Klassert | dff3bb0 | 2009-10-07 22:48:17 +0000 | [diff] [blame] | 474 | crypto_free_ahash(ahp->ahash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | kfree(ahp); |
| 476 | } |
| 477 | |
| 478 | |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 479 | static const struct xfrm_type ah_type = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
| 481 | .description = "AH4", |
| 482 | .owner = THIS_MODULE, |
| 483 | .proto = IPPROTO_AH, |
Herbert Xu | 436a0a4 | 2007-10-08 17:25:53 -0700 | [diff] [blame] | 484 | .flags = XFRM_TYPE_REPLAY_PROT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | .init_state = ah_init_state, |
| 486 | .destructor = ah_destroy, |
| 487 | .input = ah_input, |
| 488 | .output = ah_output |
| 489 | }; |
| 490 | |
Alexey Dobriyan | 3261309 | 2009-09-14 12:21:47 +0000 | [diff] [blame] | 491 | static const struct net_protocol ah4_protocol = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | .handler = xfrm4_rcv, |
| 493 | .err_handler = ah4_err, |
| 494 | .no_policy = 1, |
Alexey Dobriyan | 4fb236b | 2008-11-25 17:59:27 -0800 | [diff] [blame] | 495 | .netns_ok = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | static int __init ah4_init(void) |
| 499 | { |
| 500 | if (xfrm_register_type(&ah_type, AF_INET) < 0) { |
| 501 | printk(KERN_INFO "ip ah init: can't add xfrm type\n"); |
| 502 | return -EAGAIN; |
| 503 | } |
| 504 | if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) { |
| 505 | printk(KERN_INFO "ip ah init: can't add protocol\n"); |
| 506 | xfrm_unregister_type(&ah_type, AF_INET); |
| 507 | return -EAGAIN; |
| 508 | } |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static void __exit ah4_fini(void) |
| 513 | { |
| 514 | if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0) |
| 515 | printk(KERN_INFO "ip ah close: can't remove protocol\n"); |
| 516 | if (xfrm_unregister_type(&ah_type, AF_INET) < 0) |
| 517 | printk(KERN_INFO "ip ah close: can't remove xfrm type\n"); |
| 518 | } |
| 519 | |
| 520 | module_init(ah4_init); |
| 521 | module_exit(ah4_fini); |
| 522 | MODULE_LICENSE("GPL"); |
Masahide NAKAMURA | d3d6dd3 | 2007-06-26 23:57:49 -0700 | [diff] [blame] | 523 | MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH); |