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