2 /* $KAME: ah_output.c,v 1.31 2001/07/26 06:53:15 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * RFC1826/2402 authentication header.
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/errno.h>
49 #include <sys/syslog.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/in_var.h>
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet/icmp6.h>
66 #include <netinet6/ipsec.h>
68 #include <netinet6/ipsec6.h>
70 #include <netinet6/ah.h>
72 #include <netinet6/ah6.h>
74 #include <netkey/key.h>
75 #include <netkey/keydb.h>
77 #include <net/net_osdep.h>
80 static struct in_addr *ah4_finaldst __P((struct mbuf *));
84 * compute AH header size.
85 * transport mode only. for tunnel mode, we should implement
86 * virtual interface, and control MTU/MSS by the interface MTU.
90 struct ipsecrequest *isr;
92 const struct ah_algorithm *algo;
97 panic("ah_hdrsiz: NULL was passed.\n");
99 if (isr->saidx.proto != IPPROTO_AH)
100 panic("unsupported mode passed to ah_hdrsiz");
102 if (isr->sav == NULL)
104 if (isr->sav->state != SADB_SASTATE_MATURE
105 && isr->sav->state != SADB_SASTATE_DYING)
108 /* we need transport mode AH. */
109 algo = ah_algorithm_lookup(isr->sav->alg_auth);
115 * right now we don't calcurate the padding size. simply
116 * treat the padding size as constant, for simplicity.
118 * XXX variable size padding support
120 hdrsiz = (((*algo->sumsiz)(isr->sav) + 3) & ~(4 - 1));
121 if (isr->sav->flags & SADB_X_EXT_OLD)
122 hdrsiz += sizeof(struct ah);
124 hdrsiz += sizeof(struct newah);
130 * sizeof(struct newah) > sizeof(struct ah).
131 * 16 = (16 + 3) & ~(4 - 1).
133 return sizeof(struct newah) + 16;
138 * Modify the packet so that it includes the authentication data.
139 * The mbuf passed must start with IPv4 header.
141 * assumes that the first mbuf contains IPv4 header + option only.
142 * the function does not modify m.
147 struct ipsecrequest *isr;
149 struct secasvar *sav = isr->sav;
150 const struct ah_algorithm *algo;
153 u_char *ahsumpos = NULL;
154 size_t hlen = 0; /* IP header+option in bytes */
155 size_t plen = 0; /* AH payload size in bytes */
156 size_t ahlen = 0; /* plen + sizeof(ah) */
159 struct in_addr *finaldst;
163 if ((sav->flags & SADB_X_EXT_OLD) == 0 && !sav->replay) {
166 ip = mtod(m, struct ip *);
167 ipseclog((LOG_DEBUG, "ah4_output: internal error: "
168 "sav->replay is null: %x->%x, SPI=%u\n",
169 (u_int32_t)ntohl(ip->ip_src.s_addr),
170 (u_int32_t)ntohl(ip->ip_dst.s_addr),
171 (u_int32_t)ntohl(sav->spi)));
172 ipsecstat.out_inval++;
177 algo = ah_algorithm_lookup(sav->alg_auth);
179 ipseclog((LOG_ERR, "ah4_output: unsupported algorithm: "
180 "SPI=%u\n", (u_int32_t)ntohl(sav->spi)));
181 ipsecstat.out_inval++;
188 * determine the size to grow.
190 if (sav->flags & SADB_X_EXT_OLD) {
192 plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
193 ahlen = plen + sizeof(struct ah);
196 plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
197 ahlen = plen + sizeof(struct newah);
201 * grow the mbuf to accomodate AH.
203 ip = mtod(m, struct ip *);
205 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
207 hlen = ip->ip_hl << 2;
210 if (m->m_len != hlen)
211 panic("ah4_output: assumption failed (first mbuf length)");
212 if (M_LEADINGSPACE(m->m_next) < ahlen) {
214 MGET(n, M_DONTWAIT, MT_DATA);
216 ipseclog((LOG_DEBUG, "ENOBUFS in ah4_output %d\n",
222 n->m_next = m->m_next;
224 m->m_pkthdr.len += ahlen;
225 ahdrpos = mtod(n, u_char *);
227 m->m_next->m_len += ahlen;
228 m->m_next->m_data -= ahlen;
229 m->m_pkthdr.len += ahlen;
230 ahdrpos = mtod(m->m_next, u_char *);
233 ip = mtod(m, struct ip *); /* just to be sure */
238 if (sav->flags & SADB_X_EXT_OLD) {
241 ahdr = (struct ah *)ahdrpos;
242 ahsumpos = (u_char *)(ahdr + 1);
243 ahdr->ah_len = plen >> 2;
244 ahdr->ah_nxt = ip->ip_p;
245 ahdr->ah_reserve = htons(0);
247 bzero(ahdr + 1, plen);
251 ahdr = (struct newah *)ahdrpos;
252 ahsumpos = (u_char *)(ahdr + 1);
253 ahdr->ah_len = (plen >> 2) + 1; /* plus one for seq# */
254 ahdr->ah_nxt = ip->ip_p;
255 ahdr->ah_reserve = htons(0);
257 if (sav->replay->count == ~0) {
258 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
259 /* XXX Is it noisy ? */
260 ipseclog((LOG_WARNING,
261 "replay counter overflowed. %s\n",
262 ipsec_logsastr(sav)));
263 ipsecstat.out_inval++;
268 sav->replay->count++;
270 * XXX sequence number must not be cycled, if the SA is
271 * installed by IKE daemon.
273 ahdr->ah_seq = htonl(sav->replay->count);
274 bzero(ahdr + 1, plen);
278 * modify IPv4 header.
280 ip->ip_p = IPPROTO_AH;
281 if (ahlen < (IP_MAXPACKET - ntohs(ip->ip_len)))
282 ip->ip_len = htons(ntohs(ip->ip_len) + ahlen);
284 ipseclog((LOG_ERR, "IPv4 AH output: size exceeds limit\n"));
285 ipsecstat.out_inval++;
291 * If there is source routing option, update destination field in
292 * the IPv4 header to the final destination.
293 * Note that we do not need to update source routing option itself
294 * (as done in IPv4 AH processing -- see ip6_output()), since
295 * source routing option is not part of the ICV computation.
297 finaldst = ah4_finaldst(m);
299 dst.s_addr = ip->ip_dst.s_addr;
300 ip->ip_dst.s_addr = finaldst->s_addr;
304 * calcurate the checksum, based on security association
305 * and the algorithm specified.
307 error = ah4_calccksum(m, (caddr_t)ahsumpos, plen, algo, sav);
310 "error after ah4_calccksum, called from ah4_output"));
313 ipsecstat.out_inval++;
318 ip = mtod(m, struct ip *); /* just to make sure */
319 ip->ip_dst.s_addr = dst.s_addr;
321 ipsecstat.out_success++;
322 ipsecstat.out_ahhist[sav->alg_auth]++;
323 key_sa_recordxfer(sav, m);
329 /* Calculate AH length */
332 struct secasvar *sav;
334 const struct ah_algorithm *algo;
337 algo = ah_algorithm_lookup(sav->alg_auth);
340 if (sav->flags & SADB_X_EXT_OLD) {
342 plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
343 ahlen = plen + sizeof(struct ah);
346 plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
347 ahlen = plen + sizeof(struct newah);
355 * Fill in the Authentication Header and calculate checksum.
358 ah6_output(m, nexthdrp, md, isr)
362 struct ipsecrequest *isr;
366 struct secasvar *sav = isr->sav;
367 const struct ah_algorithm *algo;
369 u_char *ahsumpos = NULL;
370 size_t plen; /* AH payload size in bytes */
375 if (m->m_len < sizeof(struct ip6_hdr)) {
376 ipseclog((LOG_DEBUG, "ah6_output: first mbuf too short\n"));
381 ahlen = ah_hdrlen(sav);
385 for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
387 if (!mprev || mprev->m_next != md) {
388 ipseclog((LOG_DEBUG, "ah6_output: md is not in chain\n"));
393 MGET(mah, M_DONTWAIT, MT_DATA);
399 MCLGET(mah, M_DONTWAIT);
400 if ((mah->m_flags & M_EXT) == 0) {
409 m->m_pkthdr.len += ahlen;
412 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) > IPV6_MAXPACKET) {
414 "ip6_output: AH with IPv6 jumbogram is not supported\n"));
418 ip6 = mtod(m, struct ip6_hdr *);
419 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
421 if ((sav->flags & SADB_X_EXT_OLD) == 0 && !sav->replay) {
422 ipseclog((LOG_DEBUG, "ah6_output: internal error: "
423 "sav->replay is null: SPI=%u\n",
424 (u_int32_t)ntohl(sav->spi)));
425 ipsec6stat.out_inval++;
430 algo = ah_algorithm_lookup(sav->alg_auth);
432 ipseclog((LOG_ERR, "ah6_output: unsupported algorithm: "
433 "SPI=%u\n", (u_int32_t)ntohl(sav->spi)));
434 ipsec6stat.out_inval++;
443 if (sav->flags & SADB_X_EXT_OLD) {
444 struct ah *ahdr = mtod(mah, struct ah *);
446 plen = mah->m_len - sizeof(struct ah);
447 ahsumpos = (u_char *)(ahdr + 1);
448 ahdr->ah_nxt = *nexthdrp;
449 *nexthdrp = IPPROTO_AH;
450 ahdr->ah_len = plen >> 2;
451 ahdr->ah_reserve = htons(0);
453 bzero(ahdr + 1, plen);
455 struct newah *ahdr = mtod(mah, struct newah *);
457 plen = mah->m_len - sizeof(struct newah);
458 ahsumpos = (u_char *)(ahdr + 1);
459 ahdr->ah_nxt = *nexthdrp;
460 *nexthdrp = IPPROTO_AH;
461 ahdr->ah_len = (plen >> 2) + 1; /* plus one for seq# */
462 ahdr->ah_reserve = htons(0);
464 if (sav->replay->count == ~0) {
465 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
466 /* XXX Is it noisy ? */
467 ipseclog((LOG_WARNING,
468 "replay counter overflowed. %s\n",
469 ipsec_logsastr(sav)));
470 ipsec6stat.out_inval++;
475 sav->replay->count++;
477 * XXX sequence number must not be cycled, if the SA is
478 * installed by IKE daemon.
480 ahdr->ah_seq = htonl(sav->replay->count);
481 bzero(ahdr + 1, plen);
485 * calcurate the checksum, based on security association
486 * and the algorithm specified.
488 error = ah6_calccksum(m, (caddr_t)ahsumpos, plen, algo, sav);
490 ipsec6stat.out_inval++;
493 ipsec6stat.out_success++;
494 key_sa_recordxfer(sav, m);
496 ipsec6stat.out_ahhist[sav->alg_auth]++;
504 * Find the final destination if there is loose/strict source routing option.
505 * Returns NULL if there's no source routing options.
506 * Returns NULL on errors too.
507 * Note that this function will return a pointer INTO the given parameter,
509 * The mbuf must be pulled up toward, at least, ip option part.
511 static struct in_addr *
522 panic("ah4_finaldst: m == NULL");
523 ip = mtod(m, struct ip *);
524 hlen = (ip->ip_hl << 2);
526 if (m->m_len < hlen) {
528 "ah4_finaldst: parameter mbuf wrong (not pulled up)\n"));
532 if (hlen == sizeof(struct ip))
535 optlen = hlen - sizeof(struct ip);
537 ipseclog((LOG_DEBUG, "ah4_finaldst: wrong optlen %d\n",
542 q = (u_char *)(ip + 1);
545 if (i + IPOPT_OPTVAL >= optlen)
547 if (q[i + IPOPT_OPTVAL] == IPOPT_EOL ||
548 q[i + IPOPT_OPTVAL] == IPOPT_NOP ||
549 i + IPOPT_OLEN < optlen)
554 switch (q[i + IPOPT_OPTVAL]) {
556 i = optlen; /* bye */
563 if (q[i + IPOPT_OLEN] < 2 + sizeof(struct in_addr) ||
564 optlen - i < q[i + IPOPT_OLEN]) {
566 "ip_finaldst: invalid IP option "
567 "(code=%02x len=%02x)\n",
568 q[i + IPOPT_OPTVAL], q[i + IPOPT_OLEN]));
571 i += q[i + IPOPT_OLEN] - sizeof(struct in_addr);
572 return (struct in_addr *)(q + i);
574 if (q[i + IPOPT_OLEN] < 2 ||
575 optlen - i < q[i + IPOPT_OLEN]) {
577 "ip_finaldst: invalid IP option "
578 "(code=%02x len=%02x)\n",
579 q[i + IPOPT_OPTVAL], q[i + IPOPT_OLEN]));
582 i += q[i + IPOPT_OLEN];