1/* $FreeBSD: head/sys/netipsec/xform_ah.c 190909 2009-04-11 05:58:58Z zec $ */
| 1/* $FreeBSD: head/sys/netipsec/xform_ah.c 195699 2009-07-14 22:48:30Z rwatson $ */
|
2/* $OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */ 3/*- 4 * The authors of this code are John Ioannidis (ji@tla.org), 5 * Angelos D. Keromytis (kermit@csd.uch.gr) and 6 * Niels Provos (provos@physnet.uni-hamburg.de). 7 * 8 * The original version of this code was written by John Ioannidis 9 * for BSD/OS in Athens, Greece, in November 1995. 10 * 11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 12 * by Angelos D. Keromytis. 13 * 14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 15 * and Niels Provos. 16 * 17 * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist. 18 * 19 * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis, 20 * Angelos D. Keromytis and Niels Provos. 21 * Copyright (c) 1999 Niklas Hallqvist. 22 * Copyright (c) 2001 Angelos D. Keromytis. 23 * 24 * Permission to use, copy, and modify this software with or without fee 25 * is hereby granted, provided that this entire notice is included in 26 * all copies of any software which is or includes a copy or 27 * modification of this software. 28 * You may use this code under the GNU public license if you so wish. Please 29 * contribute changes back to the authors under this freer than GPL license 30 * so that we may further the use of strong encryption without limitations to 31 * all. 32 * 33 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 34 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 35 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 36 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 37 * PURPOSE. 38 */ 39#include "opt_inet.h" 40#include "opt_inet6.h" 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/mbuf.h> 45#include <sys/socket.h> 46#include <sys/syslog.h> 47#include <sys/kernel.h> 48#include <sys/sysctl.h> 49#include <sys/vimage.h> 50 51#include <net/if.h>
| 2/* $OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */ 3/*- 4 * The authors of this code are John Ioannidis (ji@tla.org), 5 * Angelos D. Keromytis (kermit@csd.uch.gr) and 6 * Niels Provos (provos@physnet.uni-hamburg.de). 7 * 8 * The original version of this code was written by John Ioannidis 9 * for BSD/OS in Athens, Greece, in November 1995. 10 * 11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 12 * by Angelos D. Keromytis. 13 * 14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 15 * and Niels Provos. 16 * 17 * Additional features in 1999 by Angelos D. Keromytis and Niklas Hallqvist. 18 * 19 * Copyright (c) 1995, 1996, 1997, 1998, 1999 by John Ioannidis, 20 * Angelos D. Keromytis and Niels Provos. 21 * Copyright (c) 1999 Niklas Hallqvist. 22 * Copyright (c) 2001 Angelos D. Keromytis. 23 * 24 * Permission to use, copy, and modify this software with or without fee 25 * is hereby granted, provided that this entire notice is included in 26 * all copies of any software which is or includes a copy or 27 * modification of this software. 28 * You may use this code under the GNU public license if you so wish. Please 29 * contribute changes back to the authors under this freer than GPL license 30 * so that we may further the use of strong encryption without limitations to 31 * all. 32 * 33 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 34 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 35 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 36 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 37 * PURPOSE. 38 */ 39#include "opt_inet.h" 40#include "opt_inet6.h" 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/mbuf.h> 45#include <sys/socket.h> 46#include <sys/syslog.h> 47#include <sys/kernel.h> 48#include <sys/sysctl.h> 49#include <sys/vimage.h> 50 51#include <net/if.h>
|
| 52#include <net/vnet.h>
|
52 53#include <netinet/in.h> 54#include <netinet/in_systm.h> 55#include <netinet/ip.h> 56#include <netinet/ip_ecn.h> 57#include <netinet/ip6.h> 58 59#include <net/route.h> 60#include <netipsec/ipsec.h> 61#include <netipsec/ah.h> 62#include <netipsec/ah_var.h> 63#include <netipsec/xform.h> 64 65#ifdef INET6 66#include <netinet6/ip6_var.h> 67#include <netipsec/ipsec6.h> 68#include <netinet6/ip6_ecn.h> 69#endif 70 71#include <netipsec/key.h> 72#include <netipsec/key_debug.h> 73 74#include <opencrypto/cryptodev.h> 75
| 53 54#include <netinet/in.h> 55#include <netinet/in_systm.h> 56#include <netinet/ip.h> 57#include <netinet/ip_ecn.h> 58#include <netinet/ip6.h> 59 60#include <net/route.h> 61#include <netipsec/ipsec.h> 62#include <netipsec/ah.h> 63#include <netipsec/ah_var.h> 64#include <netipsec/xform.h> 65 66#ifdef INET6 67#include <netinet6/ip6_var.h> 68#include <netipsec/ipsec6.h> 69#include <netinet6/ip6_ecn.h> 70#endif 71 72#include <netipsec/key.h> 73#include <netipsec/key_debug.h> 74 75#include <opencrypto/cryptodev.h> 76
|
76static int ah_iattach(const void *); 77 78#ifndef VIMAGE_GLOBALS 79static const vnet_modinfo_t vnet_ah_modinfo = { 80 .vmi_id = VNET_MOD_AH, 81 .vmi_name = "ipsec_ah", 82 .vmi_dependson = VNET_MOD_IPSEC, 83 .vmi_iattach = ah_iattach 84}; 85#endif /* !VIMAGE_GLOBALS */ 86
| |
87/* 88 * Return header size in bytes. The old protocol did not support 89 * the replay counter; the new protocol always includes the counter. 90 */ 91#define HDRSIZE(sav) \ 92 (((sav)->flags & SADB_X_EXT_OLD) ? \ 93 sizeof (struct ah) : sizeof (struct ah) + sizeof (u_int32_t)) 94/* 95 * Return authenticator size in bytes. The old protocol is known 96 * to use a fixed 16-byte authenticator. The new algorithm use 12-byte 97 * authenticator. 98 */ 99#define AUTHSIZE(sav) \ 100 ((sav->flags & SADB_X_EXT_OLD) ? 16 : AH_HMAC_HASHLEN) 101
| 77/* 78 * Return header size in bytes. The old protocol did not support 79 * the replay counter; the new protocol always includes the counter. 80 */ 81#define HDRSIZE(sav) \ 82 (((sav)->flags & SADB_X_EXT_OLD) ? \ 83 sizeof (struct ah) : sizeof (struct ah) + sizeof (u_int32_t)) 84/* 85 * Return authenticator size in bytes. The old protocol is known 86 * to use a fixed 16-byte authenticator. The new algorithm use 12-byte 87 * authenticator. 88 */ 89#define AUTHSIZE(sav) \ 90 ((sav->flags & SADB_X_EXT_OLD) ? 16 : AH_HMAC_HASHLEN) 91
|
102#ifdef VIMAGE_GLOBALS 103int ah_enable; 104int ah_cleartos; 105struct ahstat ahstat; 106#endif
| 92VNET_DEFINE(int, ah_enable) = 1; /* control flow of packets with AH */ 93VNET_DEFINE(int, ah_cleartos) = 1; /* clear ip_tos when doing AH calc */ 94VNET_DEFINE(struct ahstat, ahstat);
|
107 108SYSCTL_DECL(_net_inet_ah);
| 95 96SYSCTL_DECL(_net_inet_ah);
|
109SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ah, OID_AUTO, 110 ah_enable, CTLFLAG_RW, ah_enable, 0, ""); 111SYSCTL_V_INT(V_NET, vnet_ipsec, _net_inet_ah, OID_AUTO, 112 ah_cleartos, CTLFLAG_RW, ah_cleartos, 0, ""); 113SYSCTL_V_STRUCT(V_NET, vnet_ipsec, _net_inet_ah, IPSECCTL_STATS, 114 stats, CTLFLAG_RD, ahstat, ahstat, "");
| 97SYSCTL_VNET_INT(_net_inet_ah, OID_AUTO, 98 ah_enable, CTLFLAG_RW, &VNET_NAME(ah_enable), 0, ""); 99SYSCTL_VNET_INT(_net_inet_ah, OID_AUTO, 100 ah_cleartos, CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0, ""); 101SYSCTL_VNET_STRUCT(_net_inet_ah, IPSECCTL_STATS, 102 stats, CTLFLAG_RD, &VNET_NAME(ahstat), ahstat, "");
|
115 116static unsigned char ipseczeroes[256]; /* larger than an ip6 extension hdr */ 117 118static int ah_input_cb(struct cryptop*); 119static int ah_output_cb(struct cryptop*); 120 121/* 122 * NB: this is public for use by the PF_KEY support. 123 */ 124struct auth_hash * 125ah_algorithm_lookup(int alg) 126{ 127 if (alg > SADB_AALG_MAX) 128 return NULL; 129 switch (alg) { 130 case SADB_X_AALG_NULL: 131 return &auth_hash_null; 132 case SADB_AALG_MD5HMAC: 133 return &auth_hash_hmac_md5; 134 case SADB_AALG_SHA1HMAC: 135 return &auth_hash_hmac_sha1; 136 case SADB_X_AALG_RIPEMD160HMAC: 137 return &auth_hash_hmac_ripemd_160; 138 case SADB_X_AALG_MD5: 139 return &auth_hash_key_md5; 140 case SADB_X_AALG_SHA: 141 return &auth_hash_key_sha1; 142 case SADB_X_AALG_SHA2_256: 143 return &auth_hash_hmac_sha2_256; 144 case SADB_X_AALG_SHA2_384: 145 return &auth_hash_hmac_sha2_384; 146 case SADB_X_AALG_SHA2_512: 147 return &auth_hash_hmac_sha2_512; 148 } 149 return NULL; 150} 151 152size_t 153ah_hdrsiz(struct secasvar *sav) 154{ 155 size_t size; 156 157 if (sav != NULL) { 158 int authsize; 159 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform")); 160 /*XXX not right for null algorithm--does it matter??*/ 161 authsize = AUTHSIZE(sav); 162 size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav); 163 } else { 164 /* default guess */ 165 size = sizeof (struct ah) + sizeof (u_int32_t) + 16; 166 } 167 return size; 168} 169 170/* 171 * NB: public for use by esp_init. 172 */ 173int 174ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria) 175{
| 103 104static unsigned char ipseczeroes[256]; /* larger than an ip6 extension hdr */ 105 106static int ah_input_cb(struct cryptop*); 107static int ah_output_cb(struct cryptop*); 108 109/* 110 * NB: this is public for use by the PF_KEY support. 111 */ 112struct auth_hash * 113ah_algorithm_lookup(int alg) 114{ 115 if (alg > SADB_AALG_MAX) 116 return NULL; 117 switch (alg) { 118 case SADB_X_AALG_NULL: 119 return &auth_hash_null; 120 case SADB_AALG_MD5HMAC: 121 return &auth_hash_hmac_md5; 122 case SADB_AALG_SHA1HMAC: 123 return &auth_hash_hmac_sha1; 124 case SADB_X_AALG_RIPEMD160HMAC: 125 return &auth_hash_hmac_ripemd_160; 126 case SADB_X_AALG_MD5: 127 return &auth_hash_key_md5; 128 case SADB_X_AALG_SHA: 129 return &auth_hash_key_sha1; 130 case SADB_X_AALG_SHA2_256: 131 return &auth_hash_hmac_sha2_256; 132 case SADB_X_AALG_SHA2_384: 133 return &auth_hash_hmac_sha2_384; 134 case SADB_X_AALG_SHA2_512: 135 return &auth_hash_hmac_sha2_512; 136 } 137 return NULL; 138} 139 140size_t 141ah_hdrsiz(struct secasvar *sav) 142{ 143 size_t size; 144 145 if (sav != NULL) { 146 int authsize; 147 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform")); 148 /*XXX not right for null algorithm--does it matter??*/ 149 authsize = AUTHSIZE(sav); 150 size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav); 151 } else { 152 /* default guess */ 153 size = sizeof (struct ah) + sizeof (u_int32_t) + 16; 154 } 155 return size; 156} 157 158/* 159 * NB: public for use by esp_init. 160 */ 161int 162ah_init0(struct secasvar *sav, struct xformsw *xsp, struct cryptoini *cria) 163{
|
176 INIT_VNET_IPSEC(curvnet);
| |
177 struct auth_hash *thash; 178 int keylen; 179 180 thash = ah_algorithm_lookup(sav->alg_auth); 181 if (thash == NULL) { 182 DPRINTF(("%s: unsupported authentication algorithm %u\n", 183 __func__, sav->alg_auth)); 184 return EINVAL; 185 } 186 /* 187 * Verify the replay state block allocation is consistent with 188 * the protocol type. We check here so we can make assumptions 189 * later during protocol processing. 190 */ 191 /* NB: replay state is setup elsewhere (sigh) */ 192 if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) { 193 DPRINTF(("%s: replay state block inconsistency, " 194 "%s algorithm %s replay state\n", __func__, 195 (sav->flags & SADB_X_EXT_OLD) ? "old" : "new", 196 sav->replay == NULL ? "without" : "with")); 197 return EINVAL; 198 } 199 if (sav->key_auth == NULL) { 200 DPRINTF(("%s: no authentication key for %s algorithm\n", 201 __func__, thash->name)); 202 return EINVAL; 203 } 204 keylen = _KEYLEN(sav->key_auth); 205 if (keylen != thash->keysize && thash->keysize != 0) { 206 DPRINTF(("%s: invalid keylength %d, algorithm %s requires " 207 "keysize %d\n", __func__, 208 keylen, thash->name, thash->keysize)); 209 return EINVAL; 210 } 211 212 sav->tdb_xform = xsp; 213 sav->tdb_authalgxform = thash; 214 215 /* Initialize crypto session. */ 216 bzero(cria, sizeof (*cria)); 217 cria->cri_alg = sav->tdb_authalgxform->type; 218 cria->cri_klen = _KEYBITS(sav->key_auth); 219 cria->cri_key = sav->key_auth->key_data; 220 cria->cri_mlen = AUTHSIZE(sav); 221 222 return 0; 223} 224 225/* 226 * ah_init() is called when an SPI is being set up. 227 */ 228static int 229ah_init(struct secasvar *sav, struct xformsw *xsp) 230{
| 164 struct auth_hash *thash; 165 int keylen; 166 167 thash = ah_algorithm_lookup(sav->alg_auth); 168 if (thash == NULL) { 169 DPRINTF(("%s: unsupported authentication algorithm %u\n", 170 __func__, sav->alg_auth)); 171 return EINVAL; 172 } 173 /* 174 * Verify the replay state block allocation is consistent with 175 * the protocol type. We check here so we can make assumptions 176 * later during protocol processing. 177 */ 178 /* NB: replay state is setup elsewhere (sigh) */ 179 if (((sav->flags&SADB_X_EXT_OLD) == 0) ^ (sav->replay != NULL)) { 180 DPRINTF(("%s: replay state block inconsistency, " 181 "%s algorithm %s replay state\n", __func__, 182 (sav->flags & SADB_X_EXT_OLD) ? "old" : "new", 183 sav->replay == NULL ? "without" : "with")); 184 return EINVAL; 185 } 186 if (sav->key_auth == NULL) { 187 DPRINTF(("%s: no authentication key for %s algorithm\n", 188 __func__, thash->name)); 189 return EINVAL; 190 } 191 keylen = _KEYLEN(sav->key_auth); 192 if (keylen != thash->keysize && thash->keysize != 0) { 193 DPRINTF(("%s: invalid keylength %d, algorithm %s requires " 194 "keysize %d\n", __func__, 195 keylen, thash->name, thash->keysize)); 196 return EINVAL; 197 } 198 199 sav->tdb_xform = xsp; 200 sav->tdb_authalgxform = thash; 201 202 /* Initialize crypto session. */ 203 bzero(cria, sizeof (*cria)); 204 cria->cri_alg = sav->tdb_authalgxform->type; 205 cria->cri_klen = _KEYBITS(sav->key_auth); 206 cria->cri_key = sav->key_auth->key_data; 207 cria->cri_mlen = AUTHSIZE(sav); 208 209 return 0; 210} 211 212/* 213 * ah_init() is called when an SPI is being set up. 214 */ 215static int 216ah_init(struct secasvar *sav, struct xformsw *xsp) 217{
|
231 INIT_VNET_IPSEC(curvnet);
| |
232 struct cryptoini cria; 233 int error; 234 235 error = ah_init0(sav, xsp, &cria); 236 return error ? error : 237 crypto_newsession(&sav->tdb_cryptoid, &cria, V_crypto_support); 238} 239 240/* 241 * Paranoia. 242 * 243 * NB: public for use by esp_zeroize (XXX). 244 */ 245int 246ah_zeroize(struct secasvar *sav) 247{ 248 int err; 249 250 if (sav->key_auth) 251 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth)); 252 253 err = crypto_freesession(sav->tdb_cryptoid); 254 sav->tdb_cryptoid = 0; 255 sav->tdb_authalgxform = NULL; 256 sav->tdb_xform = NULL; 257 return err; 258} 259 260/* 261 * Massage IPv4/IPv6 headers for AH processing. 262 */ 263static int 264ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out) 265{
| 218 struct cryptoini cria; 219 int error; 220 221 error = ah_init0(sav, xsp, &cria); 222 return error ? error : 223 crypto_newsession(&sav->tdb_cryptoid, &cria, V_crypto_support); 224} 225 226/* 227 * Paranoia. 228 * 229 * NB: public for use by esp_zeroize (XXX). 230 */ 231int 232ah_zeroize(struct secasvar *sav) 233{ 234 int err; 235 236 if (sav->key_auth) 237 bzero(sav->key_auth->key_data, _KEYLEN(sav->key_auth)); 238 239 err = crypto_freesession(sav->tdb_cryptoid); 240 sav->tdb_cryptoid = 0; 241 sav->tdb_authalgxform = NULL; 242 sav->tdb_xform = NULL; 243 return err; 244} 245 246/* 247 * Massage IPv4/IPv6 headers for AH processing. 248 */ 249static int 250ah_massage_headers(struct mbuf **m0, int proto, int skip, int alg, int out) 251{
|
266 INIT_VNET_IPSEC(curvnet);
| |
267 struct mbuf *m = *m0; 268 unsigned char *ptr; 269 int off, count; 270 271#ifdef INET 272 struct ip *ip; 273#endif /* INET */ 274 275#ifdef INET6 276 struct ip6_ext *ip6e; 277 struct ip6_hdr ip6; 278 int alloc, len, ad; 279#endif /* INET6 */ 280 281 switch (proto) { 282#ifdef INET 283 case AF_INET: 284 /* 285 * This is the least painful way of dealing with IPv4 header 286 * and option processing -- just make sure they're in 287 * contiguous memory. 288 */ 289 *m0 = m = m_pullup(m, skip); 290 if (m == NULL) { 291 DPRINTF(("%s: m_pullup failed\n", __func__)); 292 return ENOBUFS; 293 } 294 295 /* Fix the IP header */ 296 ip = mtod(m, struct ip *); 297 if (V_ah_cleartos) 298 ip->ip_tos = 0; 299 ip->ip_ttl = 0; 300 ip->ip_sum = 0; 301 302 /* 303 * On input, fix ip_len which has been byte-swapped 304 * at ip_input(). 305 */ 306 if (!out) { 307 ip->ip_len = htons(ip->ip_len + skip); 308 309 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK) 310 ip->ip_off = htons(ip->ip_off & IP_DF); 311 else 312 ip->ip_off = 0; 313 } else { 314 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK) 315 ip->ip_off = htons(ntohs(ip->ip_off) & IP_DF); 316 else 317 ip->ip_off = 0; 318 } 319 320 ptr = mtod(m, unsigned char *) + sizeof(struct ip); 321 322 /* IPv4 option processing */ 323 for (off = sizeof(struct ip); off < skip;) { 324 if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP || 325 off + 1 < skip) 326 ; 327 else { 328 DPRINTF(("%s: illegal IPv4 option length for " 329 "option %d\n", __func__, ptr[off])); 330 331 m_freem(m); 332 return EINVAL; 333 } 334 335 switch (ptr[off]) { 336 case IPOPT_EOL: 337 off = skip; /* End the loop. */ 338 break; 339 340 case IPOPT_NOP: 341 off++; 342 break; 343 344 case IPOPT_SECURITY: /* 0x82 */ 345 case 0x85: /* Extended security. */ 346 case 0x86: /* Commercial security. */ 347 case 0x94: /* Router alert */ 348 case 0x95: /* RFC1770 */ 349 /* Sanity check for option length. */ 350 if (ptr[off + 1] < 2) { 351 DPRINTF(("%s: illegal IPv4 option " 352 "length for option %d\n", 353 __func__, ptr[off])); 354 355 m_freem(m); 356 return EINVAL; 357 } 358 359 off += ptr[off + 1]; 360 break; 361 362 case IPOPT_LSRR: 363 case IPOPT_SSRR: 364 /* Sanity check for option length. */ 365 if (ptr[off + 1] < 2) { 366 DPRINTF(("%s: illegal IPv4 option " 367 "length for option %d\n", 368 __func__, ptr[off])); 369 370 m_freem(m); 371 return EINVAL; 372 } 373 374 /* 375 * On output, if we have either of the 376 * source routing options, we should 377 * swap the destination address of the 378 * IP header with the last address 379 * specified in the option, as that is 380 * what the destination's IP header 381 * will look like. 382 */ 383 if (out) 384 bcopy(ptr + off + ptr[off + 1] - 385 sizeof(struct in_addr), 386 &(ip->ip_dst), sizeof(struct in_addr)); 387 388 /* Fall through */ 389 default: 390 /* Sanity check for option length. */ 391 if (ptr[off + 1] < 2) { 392 DPRINTF(("%s: illegal IPv4 option " 393 "length for option %d\n", 394 __func__, ptr[off])); 395 m_freem(m); 396 return EINVAL; 397 } 398 399 /* Zeroize all other options. */ 400 count = ptr[off + 1]; 401 bcopy(ipseczeroes, ptr, count); 402 off += count; 403 break; 404 } 405 406 /* Sanity check. */ 407 if (off > skip) { 408 DPRINTF(("%s: malformed IPv4 options header\n", 409 __func__)); 410 411 m_freem(m); 412 return EINVAL; 413 } 414 } 415 416 break; 417#endif /* INET */ 418 419#ifdef INET6 420 case AF_INET6: /* Ugly... */ 421 /* Copy and "cook" the IPv6 header. */ 422 m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6); 423 424 /* We don't do IPv6 Jumbograms. */ 425 if (ip6.ip6_plen == 0) { 426 DPRINTF(("%s: unsupported IPv6 jumbogram\n", __func__)); 427 m_freem(m); 428 return EMSGSIZE; 429 } 430 431 ip6.ip6_flow = 0; 432 ip6.ip6_hlim = 0; 433 ip6.ip6_vfc &= ~IPV6_VERSION_MASK; 434 ip6.ip6_vfc |= IPV6_VERSION; 435 436 /* Scoped address handling. */ 437 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src)) 438 ip6.ip6_src.s6_addr16[1] = 0; 439 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst)) 440 ip6.ip6_dst.s6_addr16[1] = 0; 441 442 /* Done with IPv6 header. */ 443 m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6); 444 445 /* Let's deal with the remaining headers (if any). */ 446 if (skip - sizeof(struct ip6_hdr) > 0) { 447 if (m->m_len <= skip) { 448 ptr = (unsigned char *) malloc( 449 skip - sizeof(struct ip6_hdr), 450 M_XDATA, M_NOWAIT); 451 if (ptr == NULL) { 452 DPRINTF(("%s: failed to allocate memory" 453 "for IPv6 headers\n",__func__)); 454 m_freem(m); 455 return ENOBUFS; 456 } 457 458 /* 459 * Copy all the protocol headers after 460 * the IPv6 header. 461 */ 462 m_copydata(m, sizeof(struct ip6_hdr), 463 skip - sizeof(struct ip6_hdr), ptr); 464 alloc = 1; 465 } else { 466 /* No need to allocate memory. */ 467 ptr = mtod(m, unsigned char *) + 468 sizeof(struct ip6_hdr); 469 alloc = 0; 470 } 471 } else 472 break; 473 474 off = ip6.ip6_nxt & 0xff; /* Next header type. */ 475 476 for (len = 0; len < skip - sizeof(struct ip6_hdr);) 477 switch (off) { 478 case IPPROTO_HOPOPTS: 479 case IPPROTO_DSTOPTS: 480 ip6e = (struct ip6_ext *) (ptr + len); 481 482 /* 483 * Process the mutable/immutable 484 * options -- borrows heavily from the 485 * KAME code. 486 */ 487 for (count = len + sizeof(struct ip6_ext); 488 count < len + ((ip6e->ip6e_len + 1) << 3);) { 489 if (ptr[count] == IP6OPT_PAD1) { 490 count++; 491 continue; /* Skip padding. */ 492 } 493 494 /* Sanity check. */ 495 if (count > len + 496 ((ip6e->ip6e_len + 1) << 3)) { 497 m_freem(m); 498 499 /* Free, if we allocated. */ 500 if (alloc) 501 free(ptr, M_XDATA); 502 return EINVAL; 503 } 504 505 ad = ptr[count + 1]; 506 507 /* If mutable option, zeroize. */ 508 if (ptr[count] & IP6OPT_MUTABLE) 509 bcopy(ipseczeroes, ptr + count, 510 ptr[count + 1]); 511 512 count += ad; 513 514 /* Sanity check. */ 515 if (count > 516 skip - sizeof(struct ip6_hdr)) { 517 m_freem(m); 518 519 /* Free, if we allocated. */ 520 if (alloc) 521 free(ptr, M_XDATA); 522 return EINVAL; 523 } 524 } 525 526 /* Advance. */ 527 len += ((ip6e->ip6e_len + 1) << 3); 528 off = ip6e->ip6e_nxt; 529 break; 530 531 case IPPROTO_ROUTING: 532 /* 533 * Always include routing headers in 534 * computation. 535 */ 536 ip6e = (struct ip6_ext *) (ptr + len); 537 len += ((ip6e->ip6e_len + 1) << 3); 538 off = ip6e->ip6e_nxt; 539 break; 540 541 default: 542 DPRINTF(("%s: unexpected IPv6 header type %d", 543 __func__, off)); 544 if (alloc) 545 free(ptr, M_XDATA); 546 m_freem(m); 547 return EINVAL; 548 } 549 550 /* Copyback and free, if we allocated. */ 551 if (alloc) { 552 m_copyback(m, sizeof(struct ip6_hdr), 553 skip - sizeof(struct ip6_hdr), ptr); 554 free(ptr, M_XDATA); 555 } 556 557 break; 558#endif /* INET6 */ 559 } 560 561 return 0; 562} 563 564/* 565 * ah_input() gets called to verify that an input packet 566 * passes authentication. 567 */ 568static int 569ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) 570{
| 252 struct mbuf *m = *m0; 253 unsigned char *ptr; 254 int off, count; 255 256#ifdef INET 257 struct ip *ip; 258#endif /* INET */ 259 260#ifdef INET6 261 struct ip6_ext *ip6e; 262 struct ip6_hdr ip6; 263 int alloc, len, ad; 264#endif /* INET6 */ 265 266 switch (proto) { 267#ifdef INET 268 case AF_INET: 269 /* 270 * This is the least painful way of dealing with IPv4 header 271 * and option processing -- just make sure they're in 272 * contiguous memory. 273 */ 274 *m0 = m = m_pullup(m, skip); 275 if (m == NULL) { 276 DPRINTF(("%s: m_pullup failed\n", __func__)); 277 return ENOBUFS; 278 } 279 280 /* Fix the IP header */ 281 ip = mtod(m, struct ip *); 282 if (V_ah_cleartos) 283 ip->ip_tos = 0; 284 ip->ip_ttl = 0; 285 ip->ip_sum = 0; 286 287 /* 288 * On input, fix ip_len which has been byte-swapped 289 * at ip_input(). 290 */ 291 if (!out) { 292 ip->ip_len = htons(ip->ip_len + skip); 293 294 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK) 295 ip->ip_off = htons(ip->ip_off & IP_DF); 296 else 297 ip->ip_off = 0; 298 } else { 299 if (alg == CRYPTO_MD5_KPDK || alg == CRYPTO_SHA1_KPDK) 300 ip->ip_off = htons(ntohs(ip->ip_off) & IP_DF); 301 else 302 ip->ip_off = 0; 303 } 304 305 ptr = mtod(m, unsigned char *) + sizeof(struct ip); 306 307 /* IPv4 option processing */ 308 for (off = sizeof(struct ip); off < skip;) { 309 if (ptr[off] == IPOPT_EOL || ptr[off] == IPOPT_NOP || 310 off + 1 < skip) 311 ; 312 else { 313 DPRINTF(("%s: illegal IPv4 option length for " 314 "option %d\n", __func__, ptr[off])); 315 316 m_freem(m); 317 return EINVAL; 318 } 319 320 switch (ptr[off]) { 321 case IPOPT_EOL: 322 off = skip; /* End the loop. */ 323 break; 324 325 case IPOPT_NOP: 326 off++; 327 break; 328 329 case IPOPT_SECURITY: /* 0x82 */ 330 case 0x85: /* Extended security. */ 331 case 0x86: /* Commercial security. */ 332 case 0x94: /* Router alert */ 333 case 0x95: /* RFC1770 */ 334 /* Sanity check for option length. */ 335 if (ptr[off + 1] < 2) { 336 DPRINTF(("%s: illegal IPv4 option " 337 "length for option %d\n", 338 __func__, ptr[off])); 339 340 m_freem(m); 341 return EINVAL; 342 } 343 344 off += ptr[off + 1]; 345 break; 346 347 case IPOPT_LSRR: 348 case IPOPT_SSRR: 349 /* Sanity check for option length. */ 350 if (ptr[off + 1] < 2) { 351 DPRINTF(("%s: illegal IPv4 option " 352 "length for option %d\n", 353 __func__, ptr[off])); 354 355 m_freem(m); 356 return EINVAL; 357 } 358 359 /* 360 * On output, if we have either of the 361 * source routing options, we should 362 * swap the destination address of the 363 * IP header with the last address 364 * specified in the option, as that is 365 * what the destination's IP header 366 * will look like. 367 */ 368 if (out) 369 bcopy(ptr + off + ptr[off + 1] - 370 sizeof(struct in_addr), 371 &(ip->ip_dst), sizeof(struct in_addr)); 372 373 /* Fall through */ 374 default: 375 /* Sanity check for option length. */ 376 if (ptr[off + 1] < 2) { 377 DPRINTF(("%s: illegal IPv4 option " 378 "length for option %d\n", 379 __func__, ptr[off])); 380 m_freem(m); 381 return EINVAL; 382 } 383 384 /* Zeroize all other options. */ 385 count = ptr[off + 1]; 386 bcopy(ipseczeroes, ptr, count); 387 off += count; 388 break; 389 } 390 391 /* Sanity check. */ 392 if (off > skip) { 393 DPRINTF(("%s: malformed IPv4 options header\n", 394 __func__)); 395 396 m_freem(m); 397 return EINVAL; 398 } 399 } 400 401 break; 402#endif /* INET */ 403 404#ifdef INET6 405 case AF_INET6: /* Ugly... */ 406 /* Copy and "cook" the IPv6 header. */ 407 m_copydata(m, 0, sizeof(ip6), (caddr_t) &ip6); 408 409 /* We don't do IPv6 Jumbograms. */ 410 if (ip6.ip6_plen == 0) { 411 DPRINTF(("%s: unsupported IPv6 jumbogram\n", __func__)); 412 m_freem(m); 413 return EMSGSIZE; 414 } 415 416 ip6.ip6_flow = 0; 417 ip6.ip6_hlim = 0; 418 ip6.ip6_vfc &= ~IPV6_VERSION_MASK; 419 ip6.ip6_vfc |= IPV6_VERSION; 420 421 /* Scoped address handling. */ 422 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_src)) 423 ip6.ip6_src.s6_addr16[1] = 0; 424 if (IN6_IS_SCOPE_LINKLOCAL(&ip6.ip6_dst)) 425 ip6.ip6_dst.s6_addr16[1] = 0; 426 427 /* Done with IPv6 header. */ 428 m_copyback(m, 0, sizeof(struct ip6_hdr), (caddr_t) &ip6); 429 430 /* Let's deal with the remaining headers (if any). */ 431 if (skip - sizeof(struct ip6_hdr) > 0) { 432 if (m->m_len <= skip) { 433 ptr = (unsigned char *) malloc( 434 skip - sizeof(struct ip6_hdr), 435 M_XDATA, M_NOWAIT); 436 if (ptr == NULL) { 437 DPRINTF(("%s: failed to allocate memory" 438 "for IPv6 headers\n",__func__)); 439 m_freem(m); 440 return ENOBUFS; 441 } 442 443 /* 444 * Copy all the protocol headers after 445 * the IPv6 header. 446 */ 447 m_copydata(m, sizeof(struct ip6_hdr), 448 skip - sizeof(struct ip6_hdr), ptr); 449 alloc = 1; 450 } else { 451 /* No need to allocate memory. */ 452 ptr = mtod(m, unsigned char *) + 453 sizeof(struct ip6_hdr); 454 alloc = 0; 455 } 456 } else 457 break; 458 459 off = ip6.ip6_nxt & 0xff; /* Next header type. */ 460 461 for (len = 0; len < skip - sizeof(struct ip6_hdr);) 462 switch (off) { 463 case IPPROTO_HOPOPTS: 464 case IPPROTO_DSTOPTS: 465 ip6e = (struct ip6_ext *) (ptr + len); 466 467 /* 468 * Process the mutable/immutable 469 * options -- borrows heavily from the 470 * KAME code. 471 */ 472 for (count = len + sizeof(struct ip6_ext); 473 count < len + ((ip6e->ip6e_len + 1) << 3);) { 474 if (ptr[count] == IP6OPT_PAD1) { 475 count++; 476 continue; /* Skip padding. */ 477 } 478 479 /* Sanity check. */ 480 if (count > len + 481 ((ip6e->ip6e_len + 1) << 3)) { 482 m_freem(m); 483 484 /* Free, if we allocated. */ 485 if (alloc) 486 free(ptr, M_XDATA); 487 return EINVAL; 488 } 489 490 ad = ptr[count + 1]; 491 492 /* If mutable option, zeroize. */ 493 if (ptr[count] & IP6OPT_MUTABLE) 494 bcopy(ipseczeroes, ptr + count, 495 ptr[count + 1]); 496 497 count += ad; 498 499 /* Sanity check. */ 500 if (count > 501 skip - sizeof(struct ip6_hdr)) { 502 m_freem(m); 503 504 /* Free, if we allocated. */ 505 if (alloc) 506 free(ptr, M_XDATA); 507 return EINVAL; 508 } 509 } 510 511 /* Advance. */ 512 len += ((ip6e->ip6e_len + 1) << 3); 513 off = ip6e->ip6e_nxt; 514 break; 515 516 case IPPROTO_ROUTING: 517 /* 518 * Always include routing headers in 519 * computation. 520 */ 521 ip6e = (struct ip6_ext *) (ptr + len); 522 len += ((ip6e->ip6e_len + 1) << 3); 523 off = ip6e->ip6e_nxt; 524 break; 525 526 default: 527 DPRINTF(("%s: unexpected IPv6 header type %d", 528 __func__, off)); 529 if (alloc) 530 free(ptr, M_XDATA); 531 m_freem(m); 532 return EINVAL; 533 } 534 535 /* Copyback and free, if we allocated. */ 536 if (alloc) { 537 m_copyback(m, sizeof(struct ip6_hdr), 538 skip - sizeof(struct ip6_hdr), ptr); 539 free(ptr, M_XDATA); 540 } 541 542 break; 543#endif /* INET6 */ 544 } 545 546 return 0; 547} 548 549/* 550 * ah_input() gets called to verify that an input packet 551 * passes authentication. 552 */ 553static int 554ah_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) 555{
|
571 INIT_VNET_IPSEC(curvnet);
| |
572 struct auth_hash *ahx; 573 struct tdb_ident *tdbi; 574 struct tdb_crypto *tc; 575 struct m_tag *mtag; 576 struct newah *ah; 577 int hl, rplen, authsize; 578 579 struct cryptodesc *crda; 580 struct cryptop *crp; 581 582 IPSEC_ASSERT(sav != NULL, ("null SA")); 583 IPSEC_ASSERT(sav->key_auth != NULL, ("null authentication key")); 584 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, 585 ("null authentication xform")); 586 587 /* Figure out header size. */ 588 rplen = HDRSIZE(sav); 589 590 /* XXX don't pullup, just copy header */ 591 IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen); 592 if (ah == NULL) { 593 DPRINTF(("ah_input: cannot pullup header\n")); 594 V_ahstat.ahs_hdrops++; /*XXX*/ 595 m_freem(m); 596 return ENOBUFS; 597 } 598 599 /* Check replay window, if applicable. */ 600 if (sav->replay && !ipsec_chkreplay(ntohl(ah->ah_seq), sav)) { 601 V_ahstat.ahs_replay++; 602 DPRINTF(("%s: packet replay failure: %s\n", __func__, 603 ipsec_logsastr(sav))); 604 m_freem(m); 605 return ENOBUFS; 606 } 607 608 /* Verify AH header length. */ 609 hl = ah->ah_len * sizeof (u_int32_t); 610 ahx = sav->tdb_authalgxform; 611 authsize = AUTHSIZE(sav); 612 if (hl != authsize + rplen - sizeof (struct ah)) { 613 DPRINTF(("%s: bad authenticator length %u (expecting %lu)" 614 " for packet in SA %s/%08lx\n", __func__, 615 hl, (u_long) (authsize + rplen - sizeof (struct ah)), 616 ipsec_address(&sav->sah->saidx.dst), 617 (u_long) ntohl(sav->spi))); 618 V_ahstat.ahs_badauthl++; 619 m_freem(m); 620 return EACCES; 621 } 622 V_ahstat.ahs_ibytes += m->m_pkthdr.len - skip - hl; 623 624 /* Get crypto descriptors. */ 625 crp = crypto_getreq(1); 626 if (crp == NULL) { 627 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__)); 628 V_ahstat.ahs_crypto++; 629 m_freem(m); 630 return ENOBUFS; 631 } 632 633 crda = crp->crp_desc; 634 IPSEC_ASSERT(crda != NULL, ("null crypto descriptor")); 635 636 crda->crd_skip = 0; 637 crda->crd_len = m->m_pkthdr.len; 638 crda->crd_inject = skip + rplen; 639 640 /* Authentication operation. */ 641 crda->crd_alg = ahx->type; 642 crda->crd_klen = _KEYBITS(sav->key_auth); 643 crda->crd_key = sav->key_auth->key_data; 644 645 /* Find out if we've already done crypto. */ 646 for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL); 647 mtag != NULL; 648 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) { 649 tdbi = (struct tdb_ident *) (mtag + 1); 650 if (tdbi->proto == sav->sah->saidx.proto && 651 tdbi->spi == sav->spi && 652 !bcmp(&tdbi->dst, &sav->sah->saidx.dst, 653 sizeof (union sockaddr_union))) 654 break; 655 } 656 657 /* Allocate IPsec-specific opaque crypto info. */ 658 if (mtag == NULL) { 659 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto) + 660 skip + rplen + authsize, M_XDATA, M_NOWAIT|M_ZERO); 661 } else { 662 /* Hash verification has already been done successfully. */ 663 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto), 664 M_XDATA, M_NOWAIT|M_ZERO); 665 } 666 if (tc == NULL) { 667 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__)); 668 V_ahstat.ahs_crypto++; 669 crypto_freereq(crp); 670 m_freem(m); 671 return ENOBUFS; 672 } 673 674 /* Only save information if crypto processing is needed. */ 675 if (mtag == NULL) { 676 int error; 677 678 /* 679 * Save the authenticator, the skipped portion of the packet, 680 * and the AH header. 681 */ 682 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(tc+1)); 683 684 /* Zeroize the authenticator on the packet. */ 685 m_copyback(m, skip + rplen, authsize, ipseczeroes); 686 687 /* "Massage" the packet headers for crypto processing. */ 688 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family, 689 skip, ahx->type, 0); 690 if (error != 0) { 691 /* NB: mbuf is free'd by ah_massage_headers */ 692 V_ahstat.ahs_hdrops++; 693 free(tc, M_XDATA); 694 crypto_freereq(crp); 695 return error; 696 } 697 } 698 699 /* Crypto operation descriptor. */ 700 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */ 701 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 702 crp->crp_buf = (caddr_t) m; 703 crp->crp_callback = ah_input_cb; 704 crp->crp_sid = sav->tdb_cryptoid; 705 crp->crp_opaque = (caddr_t) tc; 706 707 /* These are passed as-is to the callback. */ 708 tc->tc_spi = sav->spi; 709 tc->tc_dst = sav->sah->saidx.dst; 710 tc->tc_proto = sav->sah->saidx.proto; 711 tc->tc_nxt = ah->ah_nxt; 712 tc->tc_protoff = protoff; 713 tc->tc_skip = skip; 714 tc->tc_ptr = (caddr_t) mtag; /* Save the mtag we've identified. */ 715 716 if (mtag == NULL) 717 return crypto_dispatch(crp); 718 else 719 return ah_input_cb(crp); 720} 721 722#ifdef INET6 723#define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \ 724 if (saidx->dst.sa.sa_family == AF_INET6) { \ 725 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \ 726 } else { \ 727 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \ 728 } \ 729} while (0) 730#else 731#define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \ 732 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag)) 733#endif 734 735/* 736 * AH input callback from the crypto driver. 737 */ 738static int 739ah_input_cb(struct cryptop *crp) 740{
| 556 struct auth_hash *ahx; 557 struct tdb_ident *tdbi; 558 struct tdb_crypto *tc; 559 struct m_tag *mtag; 560 struct newah *ah; 561 int hl, rplen, authsize; 562 563 struct cryptodesc *crda; 564 struct cryptop *crp; 565 566 IPSEC_ASSERT(sav != NULL, ("null SA")); 567 IPSEC_ASSERT(sav->key_auth != NULL, ("null authentication key")); 568 IPSEC_ASSERT(sav->tdb_authalgxform != NULL, 569 ("null authentication xform")); 570 571 /* Figure out header size. */ 572 rplen = HDRSIZE(sav); 573 574 /* XXX don't pullup, just copy header */ 575 IP6_EXTHDR_GET(ah, struct newah *, m, skip, rplen); 576 if (ah == NULL) { 577 DPRINTF(("ah_input: cannot pullup header\n")); 578 V_ahstat.ahs_hdrops++; /*XXX*/ 579 m_freem(m); 580 return ENOBUFS; 581 } 582 583 /* Check replay window, if applicable. */ 584 if (sav->replay && !ipsec_chkreplay(ntohl(ah->ah_seq), sav)) { 585 V_ahstat.ahs_replay++; 586 DPRINTF(("%s: packet replay failure: %s\n", __func__, 587 ipsec_logsastr(sav))); 588 m_freem(m); 589 return ENOBUFS; 590 } 591 592 /* Verify AH header length. */ 593 hl = ah->ah_len * sizeof (u_int32_t); 594 ahx = sav->tdb_authalgxform; 595 authsize = AUTHSIZE(sav); 596 if (hl != authsize + rplen - sizeof (struct ah)) { 597 DPRINTF(("%s: bad authenticator length %u (expecting %lu)" 598 " for packet in SA %s/%08lx\n", __func__, 599 hl, (u_long) (authsize + rplen - sizeof (struct ah)), 600 ipsec_address(&sav->sah->saidx.dst), 601 (u_long) ntohl(sav->spi))); 602 V_ahstat.ahs_badauthl++; 603 m_freem(m); 604 return EACCES; 605 } 606 V_ahstat.ahs_ibytes += m->m_pkthdr.len - skip - hl; 607 608 /* Get crypto descriptors. */ 609 crp = crypto_getreq(1); 610 if (crp == NULL) { 611 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__)); 612 V_ahstat.ahs_crypto++; 613 m_freem(m); 614 return ENOBUFS; 615 } 616 617 crda = crp->crp_desc; 618 IPSEC_ASSERT(crda != NULL, ("null crypto descriptor")); 619 620 crda->crd_skip = 0; 621 crda->crd_len = m->m_pkthdr.len; 622 crda->crd_inject = skip + rplen; 623 624 /* Authentication operation. */ 625 crda->crd_alg = ahx->type; 626 crda->crd_klen = _KEYBITS(sav->key_auth); 627 crda->crd_key = sav->key_auth->key_data; 628 629 /* Find out if we've already done crypto. */ 630 for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL); 631 mtag != NULL; 632 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) { 633 tdbi = (struct tdb_ident *) (mtag + 1); 634 if (tdbi->proto == sav->sah->saidx.proto && 635 tdbi->spi == sav->spi && 636 !bcmp(&tdbi->dst, &sav->sah->saidx.dst, 637 sizeof (union sockaddr_union))) 638 break; 639 } 640 641 /* Allocate IPsec-specific opaque crypto info. */ 642 if (mtag == NULL) { 643 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto) + 644 skip + rplen + authsize, M_XDATA, M_NOWAIT|M_ZERO); 645 } else { 646 /* Hash verification has already been done successfully. */ 647 tc = (struct tdb_crypto *) malloc(sizeof (struct tdb_crypto), 648 M_XDATA, M_NOWAIT|M_ZERO); 649 } 650 if (tc == NULL) { 651 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__)); 652 V_ahstat.ahs_crypto++; 653 crypto_freereq(crp); 654 m_freem(m); 655 return ENOBUFS; 656 } 657 658 /* Only save information if crypto processing is needed. */ 659 if (mtag == NULL) { 660 int error; 661 662 /* 663 * Save the authenticator, the skipped portion of the packet, 664 * and the AH header. 665 */ 666 m_copydata(m, 0, skip + rplen + authsize, (caddr_t)(tc+1)); 667 668 /* Zeroize the authenticator on the packet. */ 669 m_copyback(m, skip + rplen, authsize, ipseczeroes); 670 671 /* "Massage" the packet headers for crypto processing. */ 672 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family, 673 skip, ahx->type, 0); 674 if (error != 0) { 675 /* NB: mbuf is free'd by ah_massage_headers */ 676 V_ahstat.ahs_hdrops++; 677 free(tc, M_XDATA); 678 crypto_freereq(crp); 679 return error; 680 } 681 } 682 683 /* Crypto operation descriptor. */ 684 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */ 685 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 686 crp->crp_buf = (caddr_t) m; 687 crp->crp_callback = ah_input_cb; 688 crp->crp_sid = sav->tdb_cryptoid; 689 crp->crp_opaque = (caddr_t) tc; 690 691 /* These are passed as-is to the callback. */ 692 tc->tc_spi = sav->spi; 693 tc->tc_dst = sav->sah->saidx.dst; 694 tc->tc_proto = sav->sah->saidx.proto; 695 tc->tc_nxt = ah->ah_nxt; 696 tc->tc_protoff = protoff; 697 tc->tc_skip = skip; 698 tc->tc_ptr = (caddr_t) mtag; /* Save the mtag we've identified. */ 699 700 if (mtag == NULL) 701 return crypto_dispatch(crp); 702 else 703 return ah_input_cb(crp); 704} 705 706#ifdef INET6 707#define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \ 708 if (saidx->dst.sa.sa_family == AF_INET6) { \ 709 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \ 710 } else { \ 711 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \ 712 } \ 713} while (0) 714#else 715#define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \ 716 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag)) 717#endif 718 719/* 720 * AH input callback from the crypto driver. 721 */ 722static int 723ah_input_cb(struct cryptop *crp) 724{
|
741 INIT_VNET_IPSEC(curvnet);
| |
742 int rplen, error, skip, protoff; 743 unsigned char calc[AH_ALEN_MAX]; 744 struct mbuf *m; 745 struct cryptodesc *crd; 746 struct auth_hash *ahx; 747 struct tdb_crypto *tc; 748 struct m_tag *mtag; 749 struct secasvar *sav; 750 struct secasindex *saidx; 751 u_int8_t nxt; 752 caddr_t ptr; 753 int authsize; 754 755 crd = crp->crp_desc; 756 757 tc = (struct tdb_crypto *) crp->crp_opaque; 758 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!")); 759 skip = tc->tc_skip; 760 nxt = tc->tc_nxt; 761 protoff = tc->tc_protoff; 762 mtag = (struct m_tag *) tc->tc_ptr; 763 m = (struct mbuf *) crp->crp_buf; 764 765 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi); 766 if (sav == NULL) { 767 V_ahstat.ahs_notdb++; 768 DPRINTF(("%s: SA expired while in crypto\n", __func__)); 769 error = ENOBUFS; /*XXX*/ 770 goto bad; 771 } 772 773 saidx = &sav->sah->saidx; 774 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET || 775 saidx->dst.sa.sa_family == AF_INET6, 776 ("unexpected protocol family %u", saidx->dst.sa.sa_family)); 777 778 ahx = (struct auth_hash *) sav->tdb_authalgxform; 779 780 /* Check for crypto errors. */ 781 if (crp->crp_etype) { 782 if (sav->tdb_cryptoid != 0) 783 sav->tdb_cryptoid = crp->crp_sid; 784 785 if (crp->crp_etype == EAGAIN) { 786 error = crypto_dispatch(crp); 787 return error; 788 } 789 790 V_ahstat.ahs_noxform++; 791 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 792 error = crp->crp_etype; 793 goto bad; 794 } else { 795 V_ahstat.ahs_hist[sav->alg_auth]++; 796 crypto_freereq(crp); /* No longer needed. */ 797 crp = NULL; 798 } 799 800 /* Shouldn't happen... */ 801 if (m == NULL) { 802 V_ahstat.ahs_crypto++; 803 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__)); 804 error = EINVAL; 805 goto bad; 806 } 807 808 /* Figure out header size. */ 809 rplen = HDRSIZE(sav); 810 authsize = AUTHSIZE(sav); 811 812 /* Copy authenticator off the packet. */ 813 m_copydata(m, skip + rplen, authsize, calc); 814 815 /* 816 * If we have an mtag, we don't need to verify the authenticator -- 817 * it has been verified by an IPsec-aware NIC. 818 */ 819 if (mtag == NULL) { 820 ptr = (caddr_t) (tc + 1); 821 822 /* Verify authenticator. */ 823 if (bcmp(ptr + skip + rplen, calc, authsize)) { 824 DPRINTF(("%s: authentication hash mismatch for packet " 825 "in SA %s/%08lx\n", __func__, 826 ipsec_address(&saidx->dst), 827 (u_long) ntohl(sav->spi))); 828 V_ahstat.ahs_badauth++; 829 error = EACCES; 830 goto bad; 831 } 832 833 /* Fix the Next Protocol field. */ 834 ((u_int8_t *) ptr)[protoff] = nxt; 835 836 /* Copyback the saved (uncooked) network headers. */ 837 m_copyback(m, 0, skip, ptr); 838 } else { 839 /* Fix the Next Protocol field. */ 840 m_copyback(m, protoff, sizeof(u_int8_t), &nxt); 841 } 842 843 free(tc, M_XDATA), tc = NULL; /* No longer needed */ 844 845 /* 846 * Header is now authenticated. 847 */ 848 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM; 849 850 /* 851 * Update replay sequence number, if appropriate. 852 */ 853 if (sav->replay) { 854 u_int32_t seq; 855 856 m_copydata(m, skip + offsetof(struct newah, ah_seq), 857 sizeof (seq), (caddr_t) &seq); 858 if (ipsec_updatereplay(ntohl(seq), sav)) { 859 V_ahstat.ahs_replay++; 860 error = ENOBUFS; /*XXX as above*/ 861 goto bad; 862 } 863 } 864 865 /* 866 * Remove the AH header and authenticator from the mbuf. 867 */ 868 error = m_striphdr(m, skip, rplen + authsize); 869 if (error) { 870 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__, 871 ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi))); 872 873 V_ahstat.ahs_hdrops++; 874 goto bad; 875 } 876 877 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag); 878 879 KEY_FREESAV(&sav); 880 return error; 881bad: 882 if (sav) 883 KEY_FREESAV(&sav); 884 if (m != NULL) 885 m_freem(m); 886 if (tc != NULL) 887 free(tc, M_XDATA); 888 if (crp != NULL) 889 crypto_freereq(crp); 890 return error; 891} 892 893/* 894 * AH output routine, called by ipsec[46]_process_packet(). 895 */ 896static int 897ah_output( 898 struct mbuf *m, 899 struct ipsecrequest *isr, 900 struct mbuf **mp, 901 int skip, 902 int protoff) 903{
| 725 int rplen, error, skip, protoff; 726 unsigned char calc[AH_ALEN_MAX]; 727 struct mbuf *m; 728 struct cryptodesc *crd; 729 struct auth_hash *ahx; 730 struct tdb_crypto *tc; 731 struct m_tag *mtag; 732 struct secasvar *sav; 733 struct secasindex *saidx; 734 u_int8_t nxt; 735 caddr_t ptr; 736 int authsize; 737 738 crd = crp->crp_desc; 739 740 tc = (struct tdb_crypto *) crp->crp_opaque; 741 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!")); 742 skip = tc->tc_skip; 743 nxt = tc->tc_nxt; 744 protoff = tc->tc_protoff; 745 mtag = (struct m_tag *) tc->tc_ptr; 746 m = (struct mbuf *) crp->crp_buf; 747 748 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi); 749 if (sav == NULL) { 750 V_ahstat.ahs_notdb++; 751 DPRINTF(("%s: SA expired while in crypto\n", __func__)); 752 error = ENOBUFS; /*XXX*/ 753 goto bad; 754 } 755 756 saidx = &sav->sah->saidx; 757 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET || 758 saidx->dst.sa.sa_family == AF_INET6, 759 ("unexpected protocol family %u", saidx->dst.sa.sa_family)); 760 761 ahx = (struct auth_hash *) sav->tdb_authalgxform; 762 763 /* Check for crypto errors. */ 764 if (crp->crp_etype) { 765 if (sav->tdb_cryptoid != 0) 766 sav->tdb_cryptoid = crp->crp_sid; 767 768 if (crp->crp_etype == EAGAIN) { 769 error = crypto_dispatch(crp); 770 return error; 771 } 772 773 V_ahstat.ahs_noxform++; 774 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 775 error = crp->crp_etype; 776 goto bad; 777 } else { 778 V_ahstat.ahs_hist[sav->alg_auth]++; 779 crypto_freereq(crp); /* No longer needed. */ 780 crp = NULL; 781 } 782 783 /* Shouldn't happen... */ 784 if (m == NULL) { 785 V_ahstat.ahs_crypto++; 786 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__)); 787 error = EINVAL; 788 goto bad; 789 } 790 791 /* Figure out header size. */ 792 rplen = HDRSIZE(sav); 793 authsize = AUTHSIZE(sav); 794 795 /* Copy authenticator off the packet. */ 796 m_copydata(m, skip + rplen, authsize, calc); 797 798 /* 799 * If we have an mtag, we don't need to verify the authenticator -- 800 * it has been verified by an IPsec-aware NIC. 801 */ 802 if (mtag == NULL) { 803 ptr = (caddr_t) (tc + 1); 804 805 /* Verify authenticator. */ 806 if (bcmp(ptr + skip + rplen, calc, authsize)) { 807 DPRINTF(("%s: authentication hash mismatch for packet " 808 "in SA %s/%08lx\n", __func__, 809 ipsec_address(&saidx->dst), 810 (u_long) ntohl(sav->spi))); 811 V_ahstat.ahs_badauth++; 812 error = EACCES; 813 goto bad; 814 } 815 816 /* Fix the Next Protocol field. */ 817 ((u_int8_t *) ptr)[protoff] = nxt; 818 819 /* Copyback the saved (uncooked) network headers. */ 820 m_copyback(m, 0, skip, ptr); 821 } else { 822 /* Fix the Next Protocol field. */ 823 m_copyback(m, protoff, sizeof(u_int8_t), &nxt); 824 } 825 826 free(tc, M_XDATA), tc = NULL; /* No longer needed */ 827 828 /* 829 * Header is now authenticated. 830 */ 831 m->m_flags |= M_AUTHIPHDR|M_AUTHIPDGM; 832 833 /* 834 * Update replay sequence number, if appropriate. 835 */ 836 if (sav->replay) { 837 u_int32_t seq; 838 839 m_copydata(m, skip + offsetof(struct newah, ah_seq), 840 sizeof (seq), (caddr_t) &seq); 841 if (ipsec_updatereplay(ntohl(seq), sav)) { 842 V_ahstat.ahs_replay++; 843 error = ENOBUFS; /*XXX as above*/ 844 goto bad; 845 } 846 } 847 848 /* 849 * Remove the AH header and authenticator from the mbuf. 850 */ 851 error = m_striphdr(m, skip, rplen + authsize); 852 if (error) { 853 DPRINTF(("%s: mangled mbuf chain for SA %s/%08lx\n", __func__, 854 ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi))); 855 856 V_ahstat.ahs_hdrops++; 857 goto bad; 858 } 859 860 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag); 861 862 KEY_FREESAV(&sav); 863 return error; 864bad: 865 if (sav) 866 KEY_FREESAV(&sav); 867 if (m != NULL) 868 m_freem(m); 869 if (tc != NULL) 870 free(tc, M_XDATA); 871 if (crp != NULL) 872 crypto_freereq(crp); 873 return error; 874} 875 876/* 877 * AH output routine, called by ipsec[46]_process_packet(). 878 */ 879static int 880ah_output( 881 struct mbuf *m, 882 struct ipsecrequest *isr, 883 struct mbuf **mp, 884 int skip, 885 int protoff) 886{
|
904 INIT_VNET_IPSEC(curvnet);
| |
905 struct secasvar *sav; 906 struct auth_hash *ahx; 907 struct cryptodesc *crda; 908 struct tdb_crypto *tc; 909 struct mbuf *mi; 910 struct cryptop *crp; 911 u_int16_t iplen; 912 int error, rplen, authsize, maxpacketsize, roff; 913 u_int8_t prot; 914 struct newah *ah; 915 916 sav = isr->sav; 917 IPSEC_ASSERT(sav != NULL, ("null SA")); 918 ahx = sav->tdb_authalgxform; 919 IPSEC_ASSERT(ahx != NULL, ("null authentication xform")); 920 921 V_ahstat.ahs_output++; 922 923 /* Figure out header size. */ 924 rplen = HDRSIZE(sav); 925 926 /* Check for maximum packet size violations. */ 927 switch (sav->sah->saidx.dst.sa.sa_family) { 928#ifdef INET 929 case AF_INET: 930 maxpacketsize = IP_MAXPACKET; 931 break; 932#endif /* INET */ 933#ifdef INET6 934 case AF_INET6: 935 maxpacketsize = IPV6_MAXPACKET; 936 break; 937#endif /* INET6 */ 938 default: 939 DPRINTF(("%s: unknown/unsupported protocol family %u, " 940 "SA %s/%08lx\n", __func__, 941 sav->sah->saidx.dst.sa.sa_family, 942 ipsec_address(&sav->sah->saidx.dst), 943 (u_long) ntohl(sav->spi))); 944 V_ahstat.ahs_nopf++; 945 error = EPFNOSUPPORT; 946 goto bad; 947 } 948 authsize = AUTHSIZE(sav); 949 if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) { 950 DPRINTF(("%s: packet in SA %s/%08lx got too big " 951 "(len %u, max len %u)\n", __func__, 952 ipsec_address(&sav->sah->saidx.dst), 953 (u_long) ntohl(sav->spi), 954 rplen + authsize + m->m_pkthdr.len, maxpacketsize)); 955 V_ahstat.ahs_toobig++; 956 error = EMSGSIZE; 957 goto bad; 958 } 959 960 /* Update the counters. */ 961 V_ahstat.ahs_obytes += m->m_pkthdr.len - skip; 962 963 m = m_unshare(m, M_NOWAIT); 964 if (m == NULL) { 965 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__, 966 ipsec_address(&sav->sah->saidx.dst), 967 (u_long) ntohl(sav->spi))); 968 V_ahstat.ahs_hdrops++; 969 error = ENOBUFS; 970 goto bad; 971 } 972 973 /* Inject AH header. */ 974 mi = m_makespace(m, skip, rplen + authsize, &roff); 975 if (mi == NULL) { 976 DPRINTF(("%s: failed to inject %u byte AH header for SA " 977 "%s/%08lx\n", __func__, 978 rplen + authsize, 979 ipsec_address(&sav->sah->saidx.dst), 980 (u_long) ntohl(sav->spi))); 981 V_ahstat.ahs_hdrops++; /*XXX differs from openbsd */ 982 error = ENOBUFS; 983 goto bad; 984 } 985 986 /* 987 * The AH header is guaranteed by m_makespace() to be in 988 * contiguous memory, at roff bytes offset into the returned mbuf. 989 */ 990 ah = (struct newah *)(mtod(mi, caddr_t) + roff); 991 992 /* Initialize the AH header. */ 993 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt); 994 ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(u_int32_t); 995 ah->ah_reserve = 0; 996 ah->ah_spi = sav->spi; 997 998 /* Zeroize authenticator. */ 999 m_copyback(m, skip + rplen, authsize, ipseczeroes); 1000 1001 /* Insert packet replay counter, as requested. */ 1002 if (sav->replay) { 1003 if (sav->replay->count == ~0 && 1004 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) { 1005 DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n", 1006 __func__, 1007 ipsec_address(&sav->sah->saidx.dst), 1008 (u_long) ntohl(sav->spi))); 1009 V_ahstat.ahs_wrap++; 1010 error = EINVAL; 1011 goto bad; 1012 } 1013#ifdef REGRESSION 1014 /* Emulate replay attack when ipsec_replay is TRUE. */ 1015 if (!V_ipsec_replay) 1016#endif 1017 sav->replay->count++; 1018 ah->ah_seq = htonl(sav->replay->count); 1019 } 1020 1021 /* Get crypto descriptors. */ 1022 crp = crypto_getreq(1); 1023 if (crp == NULL) { 1024 DPRINTF(("%s: failed to acquire crypto descriptors\n", 1025 __func__)); 1026 V_ahstat.ahs_crypto++; 1027 error = ENOBUFS; 1028 goto bad; 1029 } 1030 1031 crda = crp->crp_desc; 1032 1033 crda->crd_skip = 0; 1034 crda->crd_inject = skip + rplen; 1035 crda->crd_len = m->m_pkthdr.len; 1036 1037 /* Authentication operation. */ 1038 crda->crd_alg = ahx->type; 1039 crda->crd_key = sav->key_auth->key_data; 1040 crda->crd_klen = _KEYBITS(sav->key_auth); 1041 1042 /* Allocate IPsec-specific opaque crypto info. */ 1043 tc = (struct tdb_crypto *) malloc( 1044 sizeof(struct tdb_crypto) + skip, M_XDATA, M_NOWAIT|M_ZERO); 1045 if (tc == NULL) { 1046 crypto_freereq(crp); 1047 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__)); 1048 V_ahstat.ahs_crypto++; 1049 error = ENOBUFS; 1050 goto bad; 1051 } 1052 1053 /* Save the skipped portion of the packet. */ 1054 m_copydata(m, 0, skip, (caddr_t) (tc + 1)); 1055 1056 /* 1057 * Fix IP header length on the header used for 1058 * authentication. We don't need to fix the original 1059 * header length as it will be fixed by our caller. 1060 */ 1061 switch (sav->sah->saidx.dst.sa.sa_family) { 1062#ifdef INET 1063 case AF_INET: 1064 bcopy(((caddr_t)(tc + 1)) + 1065 offsetof(struct ip, ip_len), 1066 (caddr_t) &iplen, sizeof(u_int16_t)); 1067 iplen = htons(ntohs(iplen) + rplen + authsize); 1068 m_copyback(m, offsetof(struct ip, ip_len), 1069 sizeof(u_int16_t), (caddr_t) &iplen); 1070 break; 1071#endif /* INET */ 1072 1073#ifdef INET6 1074 case AF_INET6: 1075 bcopy(((caddr_t)(tc + 1)) + 1076 offsetof(struct ip6_hdr, ip6_plen), 1077 (caddr_t) &iplen, sizeof(u_int16_t)); 1078 iplen = htons(ntohs(iplen) + rplen + authsize); 1079 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen), 1080 sizeof(u_int16_t), (caddr_t) &iplen); 1081 break; 1082#endif /* INET6 */ 1083 } 1084 1085 /* Fix the Next Header field in saved header. */ 1086 ((u_int8_t *) (tc + 1))[protoff] = IPPROTO_AH; 1087 1088 /* Update the Next Protocol field in the IP header. */ 1089 prot = IPPROTO_AH; 1090 m_copyback(m, protoff, sizeof(u_int8_t), (caddr_t) &prot); 1091 1092 /* "Massage" the packet headers for crypto processing. */ 1093 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family, 1094 skip, ahx->type, 1); 1095 if (error != 0) { 1096 m = NULL; /* mbuf was free'd by ah_massage_headers. */ 1097 free(tc, M_XDATA); 1098 crypto_freereq(crp); 1099 goto bad; 1100 } 1101 1102 /* Crypto operation descriptor. */ 1103 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */ 1104 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 1105 crp->crp_buf = (caddr_t) m; 1106 crp->crp_callback = ah_output_cb; 1107 crp->crp_sid = sav->tdb_cryptoid; 1108 crp->crp_opaque = (caddr_t) tc; 1109 1110 /* These are passed as-is to the callback. */ 1111 tc->tc_isr = isr; 1112 tc->tc_spi = sav->spi; 1113 tc->tc_dst = sav->sah->saidx.dst; 1114 tc->tc_proto = sav->sah->saidx.proto; 1115 tc->tc_skip = skip; 1116 tc->tc_protoff = protoff; 1117 1118 return crypto_dispatch(crp); 1119bad: 1120 if (m) 1121 m_freem(m); 1122 return (error); 1123} 1124 1125/* 1126 * AH output callback from the crypto driver. 1127 */ 1128static int 1129ah_output_cb(struct cryptop *crp) 1130{
| 887 struct secasvar *sav; 888 struct auth_hash *ahx; 889 struct cryptodesc *crda; 890 struct tdb_crypto *tc; 891 struct mbuf *mi; 892 struct cryptop *crp; 893 u_int16_t iplen; 894 int error, rplen, authsize, maxpacketsize, roff; 895 u_int8_t prot; 896 struct newah *ah; 897 898 sav = isr->sav; 899 IPSEC_ASSERT(sav != NULL, ("null SA")); 900 ahx = sav->tdb_authalgxform; 901 IPSEC_ASSERT(ahx != NULL, ("null authentication xform")); 902 903 V_ahstat.ahs_output++; 904 905 /* Figure out header size. */ 906 rplen = HDRSIZE(sav); 907 908 /* Check for maximum packet size violations. */ 909 switch (sav->sah->saidx.dst.sa.sa_family) { 910#ifdef INET 911 case AF_INET: 912 maxpacketsize = IP_MAXPACKET; 913 break; 914#endif /* INET */ 915#ifdef INET6 916 case AF_INET6: 917 maxpacketsize = IPV6_MAXPACKET; 918 break; 919#endif /* INET6 */ 920 default: 921 DPRINTF(("%s: unknown/unsupported protocol family %u, " 922 "SA %s/%08lx\n", __func__, 923 sav->sah->saidx.dst.sa.sa_family, 924 ipsec_address(&sav->sah->saidx.dst), 925 (u_long) ntohl(sav->spi))); 926 V_ahstat.ahs_nopf++; 927 error = EPFNOSUPPORT; 928 goto bad; 929 } 930 authsize = AUTHSIZE(sav); 931 if (rplen + authsize + m->m_pkthdr.len > maxpacketsize) { 932 DPRINTF(("%s: packet in SA %s/%08lx got too big " 933 "(len %u, max len %u)\n", __func__, 934 ipsec_address(&sav->sah->saidx.dst), 935 (u_long) ntohl(sav->spi), 936 rplen + authsize + m->m_pkthdr.len, maxpacketsize)); 937 V_ahstat.ahs_toobig++; 938 error = EMSGSIZE; 939 goto bad; 940 } 941 942 /* Update the counters. */ 943 V_ahstat.ahs_obytes += m->m_pkthdr.len - skip; 944 945 m = m_unshare(m, M_NOWAIT); 946 if (m == NULL) { 947 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__, 948 ipsec_address(&sav->sah->saidx.dst), 949 (u_long) ntohl(sav->spi))); 950 V_ahstat.ahs_hdrops++; 951 error = ENOBUFS; 952 goto bad; 953 } 954 955 /* Inject AH header. */ 956 mi = m_makespace(m, skip, rplen + authsize, &roff); 957 if (mi == NULL) { 958 DPRINTF(("%s: failed to inject %u byte AH header for SA " 959 "%s/%08lx\n", __func__, 960 rplen + authsize, 961 ipsec_address(&sav->sah->saidx.dst), 962 (u_long) ntohl(sav->spi))); 963 V_ahstat.ahs_hdrops++; /*XXX differs from openbsd */ 964 error = ENOBUFS; 965 goto bad; 966 } 967 968 /* 969 * The AH header is guaranteed by m_makespace() to be in 970 * contiguous memory, at roff bytes offset into the returned mbuf. 971 */ 972 ah = (struct newah *)(mtod(mi, caddr_t) + roff); 973 974 /* Initialize the AH header. */ 975 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &ah->ah_nxt); 976 ah->ah_len = (rplen + authsize - sizeof(struct ah)) / sizeof(u_int32_t); 977 ah->ah_reserve = 0; 978 ah->ah_spi = sav->spi; 979 980 /* Zeroize authenticator. */ 981 m_copyback(m, skip + rplen, authsize, ipseczeroes); 982 983 /* Insert packet replay counter, as requested. */ 984 if (sav->replay) { 985 if (sav->replay->count == ~0 && 986 (sav->flags & SADB_X_EXT_CYCSEQ) == 0) { 987 DPRINTF(("%s: replay counter wrapped for SA %s/%08lx\n", 988 __func__, 989 ipsec_address(&sav->sah->saidx.dst), 990 (u_long) ntohl(sav->spi))); 991 V_ahstat.ahs_wrap++; 992 error = EINVAL; 993 goto bad; 994 } 995#ifdef REGRESSION 996 /* Emulate replay attack when ipsec_replay is TRUE. */ 997 if (!V_ipsec_replay) 998#endif 999 sav->replay->count++; 1000 ah->ah_seq = htonl(sav->replay->count); 1001 } 1002 1003 /* Get crypto descriptors. */ 1004 crp = crypto_getreq(1); 1005 if (crp == NULL) { 1006 DPRINTF(("%s: failed to acquire crypto descriptors\n", 1007 __func__)); 1008 V_ahstat.ahs_crypto++; 1009 error = ENOBUFS; 1010 goto bad; 1011 } 1012 1013 crda = crp->crp_desc; 1014 1015 crda->crd_skip = 0; 1016 crda->crd_inject = skip + rplen; 1017 crda->crd_len = m->m_pkthdr.len; 1018 1019 /* Authentication operation. */ 1020 crda->crd_alg = ahx->type; 1021 crda->crd_key = sav->key_auth->key_data; 1022 crda->crd_klen = _KEYBITS(sav->key_auth); 1023 1024 /* Allocate IPsec-specific opaque crypto info. */ 1025 tc = (struct tdb_crypto *) malloc( 1026 sizeof(struct tdb_crypto) + skip, M_XDATA, M_NOWAIT|M_ZERO); 1027 if (tc == NULL) { 1028 crypto_freereq(crp); 1029 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__)); 1030 V_ahstat.ahs_crypto++; 1031 error = ENOBUFS; 1032 goto bad; 1033 } 1034 1035 /* Save the skipped portion of the packet. */ 1036 m_copydata(m, 0, skip, (caddr_t) (tc + 1)); 1037 1038 /* 1039 * Fix IP header length on the header used for 1040 * authentication. We don't need to fix the original 1041 * header length as it will be fixed by our caller. 1042 */ 1043 switch (sav->sah->saidx.dst.sa.sa_family) { 1044#ifdef INET 1045 case AF_INET: 1046 bcopy(((caddr_t)(tc + 1)) + 1047 offsetof(struct ip, ip_len), 1048 (caddr_t) &iplen, sizeof(u_int16_t)); 1049 iplen = htons(ntohs(iplen) + rplen + authsize); 1050 m_copyback(m, offsetof(struct ip, ip_len), 1051 sizeof(u_int16_t), (caddr_t) &iplen); 1052 break; 1053#endif /* INET */ 1054 1055#ifdef INET6 1056 case AF_INET6: 1057 bcopy(((caddr_t)(tc + 1)) + 1058 offsetof(struct ip6_hdr, ip6_plen), 1059 (caddr_t) &iplen, sizeof(u_int16_t)); 1060 iplen = htons(ntohs(iplen) + rplen + authsize); 1061 m_copyback(m, offsetof(struct ip6_hdr, ip6_plen), 1062 sizeof(u_int16_t), (caddr_t) &iplen); 1063 break; 1064#endif /* INET6 */ 1065 } 1066 1067 /* Fix the Next Header field in saved header. */ 1068 ((u_int8_t *) (tc + 1))[protoff] = IPPROTO_AH; 1069 1070 /* Update the Next Protocol field in the IP header. */ 1071 prot = IPPROTO_AH; 1072 m_copyback(m, protoff, sizeof(u_int8_t), (caddr_t) &prot); 1073 1074 /* "Massage" the packet headers for crypto processing. */ 1075 error = ah_massage_headers(&m, sav->sah->saidx.dst.sa.sa_family, 1076 skip, ahx->type, 1); 1077 if (error != 0) { 1078 m = NULL; /* mbuf was free'd by ah_massage_headers. */ 1079 free(tc, M_XDATA); 1080 crypto_freereq(crp); 1081 goto bad; 1082 } 1083 1084 /* Crypto operation descriptor. */ 1085 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */ 1086 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC; 1087 crp->crp_buf = (caddr_t) m; 1088 crp->crp_callback = ah_output_cb; 1089 crp->crp_sid = sav->tdb_cryptoid; 1090 crp->crp_opaque = (caddr_t) tc; 1091 1092 /* These are passed as-is to the callback. */ 1093 tc->tc_isr = isr; 1094 tc->tc_spi = sav->spi; 1095 tc->tc_dst = sav->sah->saidx.dst; 1096 tc->tc_proto = sav->sah->saidx.proto; 1097 tc->tc_skip = skip; 1098 tc->tc_protoff = protoff; 1099 1100 return crypto_dispatch(crp); 1101bad: 1102 if (m) 1103 m_freem(m); 1104 return (error); 1105} 1106 1107/* 1108 * AH output callback from the crypto driver. 1109 */ 1110static int 1111ah_output_cb(struct cryptop *crp) 1112{
|
1131 INIT_VNET_IPSEC(curvnet);
| |
1132 int skip, protoff, error; 1133 struct tdb_crypto *tc; 1134 struct ipsecrequest *isr; 1135 struct secasvar *sav; 1136 struct mbuf *m; 1137 caddr_t ptr; 1138 int err; 1139 1140 tc = (struct tdb_crypto *) crp->crp_opaque; 1141 IPSEC_ASSERT(tc != NULL, ("null opaque data area!")); 1142 skip = tc->tc_skip; 1143 protoff = tc->tc_protoff; 1144 ptr = (caddr_t) (tc + 1); 1145 m = (struct mbuf *) crp->crp_buf; 1146 1147 isr = tc->tc_isr; 1148 IPSECREQUEST_LOCK(isr); 1149 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi); 1150 if (sav == NULL) { 1151 V_ahstat.ahs_notdb++; 1152 DPRINTF(("%s: SA expired while in crypto\n", __func__)); 1153 error = ENOBUFS; /*XXX*/ 1154 goto bad; 1155 } 1156 IPSEC_ASSERT(isr->sav == sav, ("SA changed\n")); 1157 1158 /* Check for crypto errors. */ 1159 if (crp->crp_etype) { 1160 if (sav->tdb_cryptoid != 0) 1161 sav->tdb_cryptoid = crp->crp_sid; 1162 1163 if (crp->crp_etype == EAGAIN) { 1164 KEY_FREESAV(&sav); 1165 IPSECREQUEST_UNLOCK(isr); 1166 error = crypto_dispatch(crp); 1167 return error; 1168 } 1169 1170 V_ahstat.ahs_noxform++; 1171 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 1172 error = crp->crp_etype; 1173 goto bad; 1174 } 1175 1176 /* Shouldn't happen... */ 1177 if (m == NULL) { 1178 V_ahstat.ahs_crypto++; 1179 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__)); 1180 error = EINVAL; 1181 goto bad; 1182 } 1183 V_ahstat.ahs_hist[sav->alg_auth]++; 1184 1185 /* 1186 * Copy original headers (with the new protocol number) back 1187 * in place. 1188 */ 1189 m_copyback(m, 0, skip, ptr); 1190 1191 /* No longer needed. */ 1192 free(tc, M_XDATA); 1193 crypto_freereq(crp); 1194 1195#ifdef REGRESSION 1196 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */ 1197 if (V_ipsec_integrity) { 1198 int alen; 1199 1200 /* 1201 * Corrupt HMAC if we want to test integrity verification of 1202 * the other side. 1203 */ 1204 alen = AUTHSIZE(sav); 1205 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes); 1206 } 1207#endif 1208 1209 /* NB: m is reclaimed by ipsec_process_done. */ 1210 err = ipsec_process_done(m, isr); 1211 KEY_FREESAV(&sav); 1212 IPSECREQUEST_UNLOCK(isr); 1213 return err; 1214bad: 1215 if (sav) 1216 KEY_FREESAV(&sav); 1217 IPSECREQUEST_UNLOCK(isr); 1218 if (m) 1219 m_freem(m); 1220 free(tc, M_XDATA); 1221 crypto_freereq(crp); 1222 return error; 1223} 1224 1225static struct xformsw ah_xformsw = { 1226 XF_AH, XFT_AUTH, "IPsec AH", 1227 ah_init, ah_zeroize, ah_input, ah_output, 1228}; 1229 1230static void 1231ah_attach(void) 1232{ 1233 1234 xform_register(&ah_xformsw);
| 1113 int skip, protoff, error; 1114 struct tdb_crypto *tc; 1115 struct ipsecrequest *isr; 1116 struct secasvar *sav; 1117 struct mbuf *m; 1118 caddr_t ptr; 1119 int err; 1120 1121 tc = (struct tdb_crypto *) crp->crp_opaque; 1122 IPSEC_ASSERT(tc != NULL, ("null opaque data area!")); 1123 skip = tc->tc_skip; 1124 protoff = tc->tc_protoff; 1125 ptr = (caddr_t) (tc + 1); 1126 m = (struct mbuf *) crp->crp_buf; 1127 1128 isr = tc->tc_isr; 1129 IPSECREQUEST_LOCK(isr); 1130 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi); 1131 if (sav == NULL) { 1132 V_ahstat.ahs_notdb++; 1133 DPRINTF(("%s: SA expired while in crypto\n", __func__)); 1134 error = ENOBUFS; /*XXX*/ 1135 goto bad; 1136 } 1137 IPSEC_ASSERT(isr->sav == sav, ("SA changed\n")); 1138 1139 /* Check for crypto errors. */ 1140 if (crp->crp_etype) { 1141 if (sav->tdb_cryptoid != 0) 1142 sav->tdb_cryptoid = crp->crp_sid; 1143 1144 if (crp->crp_etype == EAGAIN) { 1145 KEY_FREESAV(&sav); 1146 IPSECREQUEST_UNLOCK(isr); 1147 error = crypto_dispatch(crp); 1148 return error; 1149 } 1150 1151 V_ahstat.ahs_noxform++; 1152 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype)); 1153 error = crp->crp_etype; 1154 goto bad; 1155 } 1156 1157 /* Shouldn't happen... */ 1158 if (m == NULL) { 1159 V_ahstat.ahs_crypto++; 1160 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__)); 1161 error = EINVAL; 1162 goto bad; 1163 } 1164 V_ahstat.ahs_hist[sav->alg_auth]++; 1165 1166 /* 1167 * Copy original headers (with the new protocol number) back 1168 * in place. 1169 */ 1170 m_copyback(m, 0, skip, ptr); 1171 1172 /* No longer needed. */ 1173 free(tc, M_XDATA); 1174 crypto_freereq(crp); 1175 1176#ifdef REGRESSION 1177 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */ 1178 if (V_ipsec_integrity) { 1179 int alen; 1180 1181 /* 1182 * Corrupt HMAC if we want to test integrity verification of 1183 * the other side. 1184 */ 1185 alen = AUTHSIZE(sav); 1186 m_copyback(m, m->m_pkthdr.len - alen, alen, ipseczeroes); 1187 } 1188#endif 1189 1190 /* NB: m is reclaimed by ipsec_process_done. */ 1191 err = ipsec_process_done(m, isr); 1192 KEY_FREESAV(&sav); 1193 IPSECREQUEST_UNLOCK(isr); 1194 return err; 1195bad: 1196 if (sav) 1197 KEY_FREESAV(&sav); 1198 IPSECREQUEST_UNLOCK(isr); 1199 if (m) 1200 m_freem(m); 1201 free(tc, M_XDATA); 1202 crypto_freereq(crp); 1203 return error; 1204} 1205 1206static struct xformsw ah_xformsw = { 1207 XF_AH, XFT_AUTH, "IPsec AH", 1208 ah_init, ah_zeroize, ah_input, ah_output, 1209}; 1210 1211static void 1212ah_attach(void) 1213{ 1214 1215 xform_register(&ah_xformsw);
|
1235#ifndef VIMAGE_GLOBALS 1236 vnet_mod_register(&vnet_ah_modinfo); 1237#else 1238 ah_iattach(NULL); 1239#endif
| |
1240} 1241
| 1216} 1217
|
1242static int 1243ah_iattach(const void *unused __unused) 1244{ 1245 INIT_VNET_IPSEC(curvnet); 1246 1247 V_ah_enable = 1; /* control flow of packets with AH */ 1248 V_ah_cleartos = 1; /* clear ip_tos when doing AH calc */ 1249 1250 return (0); 1251}
| |
1252SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ah_attach, NULL);
| 1218SYSINIT(ah_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ah_attach, NULL);
|