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