2009-03-14 17:45:51 +03:00
|
|
|
/* $NetBSD: ah_output.c,v 1.32 2009/03/14 14:46:10 dsl Exp $ */
|
2001-10-15 07:55:37 +04:00
|
|
|
/* $KAME: ah_output.c,v 1.31 2001/07/26 06:53:15 jinmei Exp $ */
|
1999-07-04 01:24:45 +04:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-06-02 22:20:15 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2000-06-02 22:20:15 +04:00
|
|
|
*
|
1999-06-28 10:36:47 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RFC1826/2402 authentication header.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 03:56:55 +03:00
|
|
|
#include <sys/cdefs.h>
|
2009-03-14 17:45:51 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ah_output.c,v 1.32 2009/03/14 14:46:10 dsl Exp $");
|
2001-11-13 03:56:55 +03:00
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
#include "opt_inet.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/domain.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
|
|
|
|
#ifdef INET6
|
2000-02-06 15:49:37 +03:00
|
|
|
#include <netinet/ip6.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netinet6/ip6_var.h>
|
2000-02-06 15:49:37 +03:00
|
|
|
#include <netinet/icmp6.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <netinet6/ipsec.h>
|
2008-04-23 10:09:04 +04:00
|
|
|
#include <netinet6/ipsec_private.h>
|
1999-06-28 10:36:47 +04:00
|
|
|
#include <netinet6/ah.h>
|
|
|
|
#include <netkey/key.h>
|
|
|
|
#include <netkey/keydb.h>
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
#include <net/net_osdep.h>
|
|
|
|
|
2000-10-02 07:55:41 +04:00
|
|
|
#ifdef INET
|
2009-03-14 17:45:51 +03:00
|
|
|
static struct in_addr *ah4_finaldst(struct mbuf *);
|
2000-10-02 07:55:41 +04:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* compute AH header size.
|
|
|
|
* transport mode only. for tunnel mode, we should implement
|
|
|
|
* virtual interface, and control MTU/MSS by the interface MTU.
|
|
|
|
*/
|
|
|
|
size_t
|
2007-05-23 21:14:59 +04:00
|
|
|
ah_hdrsiz(struct ipsecrequest *isr)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-07-18 18:56:42 +04:00
|
|
|
const struct ah_algorithm *algo;
|
1999-06-28 10:36:47 +04:00
|
|
|
size_t hdrsiz;
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (isr == NULL)
|
2002-09-27 19:35:29 +04:00
|
|
|
panic("ah_hdrsiz: NULL was passed.");
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (isr->saidx.proto != IPPROTO_AH)
|
1999-06-28 10:36:47 +04:00
|
|
|
panic("unsupported mode passed to ah_hdrsiz");
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if (isr->sav == NULL)
|
|
|
|
goto estimate;
|
|
|
|
if (isr->sav->state != SADB_SASTATE_MATURE
|
|
|
|
&& isr->sav->state != SADB_SASTATE_DYING)
|
|
|
|
goto estimate;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* we need transport mode AH. */
|
2000-07-18 18:56:42 +04:00
|
|
|
algo = ah_algorithm_lookup(isr->sav->alg_auth);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (!algo)
|
2000-01-31 17:18:52 +03:00
|
|
|
goto estimate;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* right now we don't calcurate the padding size. simply
|
|
|
|
* treat the padding size as constant, for simplicity.
|
|
|
|
*
|
|
|
|
* XXX variable size padding support
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
hdrsiz = (((*algo->sumsiz)(isr->sav) + 3) & ~(4 - 1));
|
|
|
|
if (isr->sav->flags & SADB_X_EXT_OLD)
|
1999-06-28 10:36:47 +04:00
|
|
|
hdrsiz += sizeof(struct ah);
|
|
|
|
else
|
|
|
|
hdrsiz += sizeof(struct newah);
|
|
|
|
|
|
|
|
return hdrsiz;
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
estimate:
|
1999-06-28 10:36:47 +04:00
|
|
|
/* ASSUMING:
|
|
|
|
* sizeof(struct newah) > sizeof(struct ah).
|
2002-08-09 11:01:21 +04:00
|
|
|
* AH_MAXSUMSIZE is multiple of 4.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
2002-08-09 11:01:21 +04:00
|
|
|
return sizeof(struct newah) + AH_MAXSUMSIZE;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-10-02 07:55:41 +04:00
|
|
|
#ifdef INET
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Modify the packet so that it includes the authentication data.
|
|
|
|
* The mbuf passed must start with IPv4 header.
|
|
|
|
*
|
|
|
|
* assumes that the first mbuf contains IPv4 header + option only.
|
|
|
|
* the function does not modify m.
|
|
|
|
*/
|
|
|
|
int
|
2007-05-23 21:14:59 +04:00
|
|
|
ah4_output(struct mbuf *m, struct ipsecrequest *isr)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *sav = isr->sav;
|
2000-07-18 18:56:42 +04:00
|
|
|
const struct ah_algorithm *algo;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int32_t spi;
|
|
|
|
u_char *ahdrpos;
|
2002-09-11 07:45:44 +04:00
|
|
|
u_int8_t *ahsumpos = NULL;
|
2001-10-15 07:55:37 +04:00
|
|
|
size_t hlen = 0; /* IP header+option in bytes */
|
|
|
|
size_t plen = 0; /* AH payload size in bytes */
|
|
|
|
size_t ahlen = 0; /* plen + sizeof(ah) */
|
1999-06-28 10:36:47 +04:00
|
|
|
struct ip *ip;
|
|
|
|
struct in_addr dst;
|
|
|
|
struct in_addr *finaldst;
|
|
|
|
int error;
|
2006-05-14 09:30:31 +04:00
|
|
|
dst.s_addr = 0; /* XXX: GCC */
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* sanity checks */
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((sav->flags & SADB_X_EXT_OLD) == 0 && !sav->replay) {
|
1999-06-28 10:36:47 +04:00
|
|
|
ip = mtod(m, struct ip *);
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_DEBUG, "ah4_output: internal error: "
|
|
|
|
"sav->replay is null: %x->%x, SPI=%u\n",
|
1999-06-28 10:36:47 +04:00
|
|
|
(u_int32_t)ntohl(ip->ip_src.s_addr),
|
|
|
|
(u_int32_t)ntohl(ip->ip_dst.s_addr),
|
2000-01-31 17:18:52 +03:00
|
|
|
(u_int32_t)ntohl(sav->spi)));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
|
2007-09-22 15:32:06 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-07-18 18:56:42 +04:00
|
|
|
algo = ah_algorithm_lookup(sav->alg_auth);
|
|
|
|
if (!algo) {
|
|
|
|
ipseclog((LOG_ERR, "ah4_output: unsupported algorithm: "
|
|
|
|
"SPI=%u\n", (u_int32_t)ntohl(sav->spi)));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
|
2007-09-22 15:32:06 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2000-07-18 18:56:42 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
spi = sav->spi;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* determine the size to grow.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->flags & SADB_X_EXT_OLD) {
|
1999-06-28 10:36:47 +04:00
|
|
|
/* RFC 1826 */
|
2001-10-15 07:55:37 +04:00
|
|
|
plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
|
1999-06-28 10:36:47 +04:00
|
|
|
ahlen = plen + sizeof(struct ah);
|
|
|
|
} else {
|
|
|
|
/* RFC 2402 */
|
2001-10-15 07:55:37 +04:00
|
|
|
plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
|
1999-06-28 10:36:47 +04:00
|
|
|
ahlen = plen + sizeof(struct newah);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-11-24 22:46:58 +03:00
|
|
|
* grow the mbuf to accommodate AH.
|
1999-06-28 10:36:47 +04:00
|
|
|
*/
|
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
hlen = ip->ip_hl << 2;
|
|
|
|
|
|
|
|
if (m->m_len != hlen)
|
2000-06-02 22:20:15 +04:00
|
|
|
panic("ah4_output: assumption failed (first mbuf length)");
|
1999-06-28 10:36:47 +04:00
|
|
|
if (M_LEADINGSPACE(m->m_next) < ahlen) {
|
|
|
|
struct mbuf *n;
|
|
|
|
MGET(n, M_DONTWAIT, MT_DATA);
|
|
|
|
if (!n) {
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_DEBUG, "ENOBUFS in ah4_output %d\n",
|
|
|
|
__LINE__));
|
2007-09-22 15:32:06 +04:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
n->m_len = ahlen;
|
|
|
|
n->m_next = m->m_next;
|
|
|
|
m->m_next = n;
|
|
|
|
m->m_pkthdr.len += ahlen;
|
|
|
|
ahdrpos = mtod(n, u_char *);
|
|
|
|
} else {
|
|
|
|
m->m_next->m_len += ahlen;
|
|
|
|
m->m_next->m_data -= ahlen;
|
|
|
|
m->m_pkthdr.len += ahlen;
|
|
|
|
ahdrpos = mtod(m->m_next, u_char *);
|
|
|
|
}
|
|
|
|
|
2001-10-15 07:55:37 +04:00
|
|
|
ip = mtod(m, struct ip *); /* just to be sure */
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize AH.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->flags & SADB_X_EXT_OLD) {
|
1999-06-28 10:36:47 +04:00
|
|
|
struct ah *ahdr;
|
|
|
|
|
|
|
|
ahdr = (struct ah *)ahdrpos;
|
|
|
|
ahsumpos = (u_char *)(ahdr + 1);
|
|
|
|
ahdr->ah_len = plen >> 2;
|
|
|
|
ahdr->ah_nxt = ip->ip_p;
|
|
|
|
ahdr->ah_reserve = htons(0);
|
|
|
|
ahdr->ah_spi = spi;
|
|
|
|
bzero(ahdr + 1, plen);
|
|
|
|
} else {
|
|
|
|
struct newah *ahdr;
|
|
|
|
|
|
|
|
ahdr = (struct newah *)ahdrpos;
|
|
|
|
ahsumpos = (u_char *)(ahdr + 1);
|
|
|
|
ahdr->ah_len = (plen >> 2) + 1; /* plus one for seq# */
|
|
|
|
ahdr->ah_nxt = ip->ip_p;
|
|
|
|
ahdr->ah_reserve = htons(0);
|
|
|
|
ahdr->ah_spi = spi;
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->replay->count == ~0) {
|
|
|
|
if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
|
|
|
|
/* XXX Is it noisy ? */
|
|
|
|
ipseclog((LOG_WARNING,
|
|
|
|
"replay counter overflowed. %s\n",
|
|
|
|
ipsec_logsastr(sav)));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
|
2007-09-22 15:32:06 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sav->replay->count++;
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* XXX sequence number must not be cycled, if the SA is
|
|
|
|
* installed by IKE daemon.
|
|
|
|
*/
|
2003-09-07 19:59:36 +04:00
|
|
|
ahdr->ah_seq = htonl(sav->replay->count & 0xffffffff);
|
1999-06-28 10:36:47 +04:00
|
|
|
bzero(ahdr + 1, plen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* modify IPv4 header.
|
|
|
|
*/
|
|
|
|
ip->ip_p = IPPROTO_AH;
|
|
|
|
if (ahlen < (IP_MAXPACKET - ntohs(ip->ip_len)))
|
|
|
|
ip->ip_len = htons(ntohs(ip->ip_len) + ahlen);
|
|
|
|
else {
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_ERR, "IPv4 AH output: size exceeds limit\n"));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
|
2007-09-22 15:32:06 +04:00
|
|
|
error = EMSGSIZE;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is source routing option, update destination field in
|
|
|
|
* the IPv4 header to the final destination.
|
|
|
|
* Note that we do not need to update source routing option itself
|
|
|
|
* (as done in IPv4 AH processing -- see ip6_output()), since
|
|
|
|
* source routing option is not part of the ICV computation.
|
|
|
|
*/
|
|
|
|
finaldst = ah4_finaldst(m);
|
|
|
|
if (finaldst) {
|
|
|
|
dst.s_addr = ip->ip_dst.s_addr;
|
|
|
|
ip->ip_dst.s_addr = finaldst->s_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* calcurate the checksum, based on security association
|
|
|
|
* and the algorithm specified.
|
|
|
|
*/
|
2002-09-11 07:45:44 +04:00
|
|
|
error = ah4_calccksum(m, ahsumpos, plen, algo, sav);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (error) {
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_ERR,
|
|
|
|
"error after ah4_calccksum, called from ah4_output"));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC_STATINC(IPSEC_STAT_OUT_INVAL);
|
2007-09-22 15:32:06 +04:00
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (finaldst) {
|
2001-10-15 07:55:37 +04:00
|
|
|
ip = mtod(m, struct ip *); /* just to make sure */
|
1999-06-28 10:36:47 +04:00
|
|
|
ip->ip_dst.s_addr = dst.s_addr;
|
|
|
|
}
|
2008-04-23 10:09:04 +04:00
|
|
|
{
|
|
|
|
uint64_t *ipss = IPSEC_STAT_GETREF();
|
|
|
|
ipss[IPSEC_STAT_OUT_SUCCESS]++;
|
|
|
|
ipss[IPSEC_STAT_OUT_AHHIST + sav->alg_auth]++;
|
|
|
|
IPSEC_STAT_PUTREF();
|
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
key_sa_recordxfer(sav, m);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
return 0;
|
2007-09-22 15:32:06 +04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
m_freem(m);
|
|
|
|
return error;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
2000-10-02 07:55:41 +04:00
|
|
|
#endif
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/* Calculate AH length */
|
|
|
|
int
|
2007-05-23 21:14:59 +04:00
|
|
|
ah_hdrlen(struct secasvar *sav)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
2000-07-18 18:56:42 +04:00
|
|
|
const struct ah_algorithm *algo;
|
1999-06-28 10:36:47 +04:00
|
|
|
int plen, ahlen;
|
2002-06-09 18:43:10 +04:00
|
|
|
|
2000-07-18 18:56:42 +04:00
|
|
|
algo = ah_algorithm_lookup(sav->alg_auth);
|
|
|
|
if (!algo)
|
|
|
|
return 0;
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->flags & SADB_X_EXT_OLD) {
|
1999-06-28 10:36:47 +04:00
|
|
|
/* RFC 1826 */
|
2001-10-15 07:55:37 +04:00
|
|
|
plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
|
1999-06-28 10:36:47 +04:00
|
|
|
ahlen = plen + sizeof(struct ah);
|
|
|
|
} else {
|
|
|
|
/* RFC 2402 */
|
2001-10-15 07:55:37 +04:00
|
|
|
plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
|
1999-06-28 10:36:47 +04:00
|
|
|
ahlen = plen + sizeof(struct newah);
|
|
|
|
}
|
|
|
|
|
2002-09-11 06:46:42 +04:00
|
|
|
return (ahlen);
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
/*
|
|
|
|
* Fill in the Authentication Header and calculate checksum.
|
|
|
|
*/
|
|
|
|
int
|
2007-05-23 21:14:59 +04:00
|
|
|
ah6_output(struct mbuf *m, u_char *nexthdrp, struct mbuf *md,
|
|
|
|
struct ipsecrequest *isr)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct mbuf *mprev;
|
|
|
|
struct mbuf *mah;
|
2000-01-31 17:18:52 +03:00
|
|
|
struct secasvar *sav = isr->sav;
|
2000-07-18 18:56:42 +04:00
|
|
|
const struct ah_algorithm *algo;
|
1999-06-28 10:36:47 +04:00
|
|
|
u_int32_t spi;
|
2002-09-11 07:45:44 +04:00
|
|
|
u_int8_t *ahsumpos = NULL;
|
2001-10-15 07:55:37 +04:00
|
|
|
size_t plen; /* AH payload size in bytes */
|
1999-06-28 10:36:47 +04:00
|
|
|
int error = 0;
|
|
|
|
int ahlen;
|
|
|
|
struct ip6_hdr *ip6;
|
|
|
|
|
|
|
|
if (m->m_len < sizeof(struct ip6_hdr)) {
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_DEBUG, "ah6_output: first mbuf too short\n"));
|
2007-09-22 15:32:06 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
ahlen = ah_hdrlen(sav);
|
1999-06-28 10:36:47 +04:00
|
|
|
if (ahlen == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
|
|
|
|
;
|
|
|
|
if (!mprev || mprev->m_next != md) {
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_DEBUG, "ah6_output: md is not in chain\n"));
|
2007-09-22 15:32:06 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MGET(mah, M_DONTWAIT, MT_DATA);
|
|
|
|
if (!mah) {
|
2007-09-22 15:32:06 +04:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
if (ahlen > MLEN) {
|
|
|
|
MCLGET(mah, M_DONTWAIT);
|
|
|
|
if ((mah->m_flags & M_EXT) == 0) {
|
|
|
|
m_free(mah);
|
2007-09-22 15:32:06 +04:00
|
|
|
error = ENOBUFS;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mah->m_len = ahlen;
|
|
|
|
mah->m_next = md;
|
|
|
|
mprev->m_next = mah;
|
|
|
|
m->m_pkthdr.len += ahlen;
|
|
|
|
|
|
|
|
/* fix plen */
|
|
|
|
if (m->m_pkthdr.len - sizeof(struct ip6_hdr) > IPV6_MAXPACKET) {
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_ERR,
|
2003-08-23 01:50:42 +04:00
|
|
|
"ah6_output: AH with IPv6 jumbogram is not supported\n"));
|
2007-09-22 15:32:06 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
ip6 = mtod(m, struct ip6_hdr *);
|
|
|
|
ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
|
|
|
|
|
2000-01-31 17:18:52 +03:00
|
|
|
if ((sav->flags & SADB_X_EXT_OLD) == 0 && !sav->replay) {
|
|
|
|
ipseclog((LOG_DEBUG, "ah6_output: internal error: "
|
|
|
|
"sav->replay is null: SPI=%u\n",
|
|
|
|
(u_int32_t)ntohl(sav->spi)));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC6_STATINC(IPSEC_STAT_OUT_INVAL);
|
2007-09-22 15:32:06 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
|
2000-07-18 18:56:42 +04:00
|
|
|
algo = ah_algorithm_lookup(sav->alg_auth);
|
|
|
|
if (!algo) {
|
|
|
|
ipseclog((LOG_ERR, "ah6_output: unsupported algorithm: "
|
|
|
|
"SPI=%u\n", (u_int32_t)ntohl(sav->spi)));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC6_STATINC(IPSEC_STAT_OUT_INVAL);
|
2007-09-22 15:32:06 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2000-07-18 18:56:42 +04:00
|
|
|
}
|
2000-01-31 17:18:52 +03:00
|
|
|
spi = sav->spi;
|
1999-06-28 10:36:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize AH.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->flags & SADB_X_EXT_OLD) {
|
1999-06-28 10:36:47 +04:00
|
|
|
struct ah *ahdr = mtod(mah, struct ah *);
|
|
|
|
|
|
|
|
plen = mah->m_len - sizeof(struct ah);
|
|
|
|
ahsumpos = (u_char *)(ahdr + 1);
|
|
|
|
ahdr->ah_nxt = *nexthdrp;
|
|
|
|
*nexthdrp = IPPROTO_AH;
|
|
|
|
ahdr->ah_len = plen >> 2;
|
|
|
|
ahdr->ah_reserve = htons(0);
|
|
|
|
ahdr->ah_spi = spi;
|
|
|
|
bzero(ahdr + 1, plen);
|
|
|
|
} else {
|
|
|
|
struct newah *ahdr = mtod(mah, struct newah *);
|
|
|
|
|
|
|
|
plen = mah->m_len - sizeof(struct newah);
|
|
|
|
ahsumpos = (u_char *)(ahdr + 1);
|
|
|
|
ahdr->ah_nxt = *nexthdrp;
|
|
|
|
*nexthdrp = IPPROTO_AH;
|
|
|
|
ahdr->ah_len = (plen >> 2) + 1; /* plus one for seq# */
|
|
|
|
ahdr->ah_reserve = htons(0);
|
|
|
|
ahdr->ah_spi = spi;
|
2000-01-31 17:18:52 +03:00
|
|
|
if (sav->replay->count == ~0) {
|
|
|
|
if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
|
|
|
|
/* XXX Is it noisy ? */
|
|
|
|
ipseclog((LOG_WARNING,
|
|
|
|
"replay counter overflowed. %s\n",
|
|
|
|
ipsec_logsastr(sav)));
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC6_STATINC(IPSEC_STAT_OUT_INVAL);
|
2007-09-22 15:32:06 +04:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2000-01-31 17:18:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sav->replay->count++;
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* XXX sequence number must not be cycled, if the SA is
|
|
|
|
* installed by IKE daemon.
|
|
|
|
*/
|
2000-01-31 17:18:52 +03:00
|
|
|
ahdr->ah_seq = htonl(sav->replay->count);
|
1999-06-28 10:36:47 +04:00
|
|
|
bzero(ahdr + 1, plen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* calcurate the checksum, based on security association
|
|
|
|
* and the algorithm specified.
|
|
|
|
*/
|
2002-09-11 07:45:44 +04:00
|
|
|
error = ah6_calccksum(m, ahsumpos, plen, algo, sav);
|
2000-03-22 02:53:30 +03:00
|
|
|
if (error) {
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC6_STATINC(IPSEC_STAT_OUT_INVAL);
|
2007-09-22 15:32:06 +04:00
|
|
|
goto fail;
|
2000-03-22 02:53:30 +03:00
|
|
|
} else {
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC6_STATINC(IPSEC_STAT_OUT_SUCCESS);
|
2000-03-22 02:53:30 +03:00
|
|
|
key_sa_recordxfer(sav, m);
|
|
|
|
}
|
2008-04-23 10:09:04 +04:00
|
|
|
IPSEC6_STATINC(IPSEC_STAT_OUT_AHHIST + sav->alg_auth);
|
1999-06-28 10:36:47 +04:00
|
|
|
|
2007-09-22 15:32:06 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
m_freem(m);
|
|
|
|
return error;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-10-02 07:55:41 +04:00
|
|
|
#ifdef INET
|
1999-06-28 10:36:47 +04:00
|
|
|
/*
|
|
|
|
* Find the final destination if there is loose/strict source routing option.
|
|
|
|
* Returns NULL if there's no source routing options.
|
|
|
|
* Returns NULL on errors too.
|
|
|
|
* Note that this function will return a pointer INTO the given parameter,
|
|
|
|
* struct mbuf *m.
|
|
|
|
* The mbuf must be pulled up toward, at least, ip option part.
|
|
|
|
*/
|
|
|
|
static struct in_addr *
|
2007-05-23 21:14:59 +04:00
|
|
|
ah4_finaldst(struct mbuf *m)
|
1999-06-28 10:36:47 +04:00
|
|
|
{
|
|
|
|
struct ip *ip;
|
|
|
|
int optlen;
|
|
|
|
u_char *q;
|
|
|
|
int i;
|
|
|
|
int hlen;
|
|
|
|
|
|
|
|
if (!m)
|
|
|
|
panic("ah4_finaldst: m == NULL");
|
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
hlen = (ip->ip_hl << 2);
|
|
|
|
|
|
|
|
if (m->m_len < hlen) {
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_DEBUG,
|
|
|
|
"ah4_finaldst: parameter mbuf wrong (not pulled up)\n"));
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hlen == sizeof(struct ip))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
optlen = hlen - sizeof(struct ip);
|
|
|
|
if (optlen < 0) {
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_DEBUG, "ah4_finaldst: wrong optlen %d\n",
|
|
|
|
optlen));
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
q = (u_char *)(ip + 1);
|
|
|
|
i = 0;
|
|
|
|
while (i < optlen) {
|
2001-02-19 07:24:27 +03:00
|
|
|
if (i + IPOPT_OPTVAL >= optlen)
|
|
|
|
return NULL;
|
|
|
|
if (q[i + IPOPT_OPTVAL] == IPOPT_EOL ||
|
|
|
|
q[i + IPOPT_OPTVAL] == IPOPT_NOP ||
|
|
|
|
i + IPOPT_OLEN < optlen)
|
|
|
|
;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
|
1999-06-28 10:36:47 +04:00
|
|
|
switch (q[i + IPOPT_OPTVAL]) {
|
|
|
|
case IPOPT_EOL:
|
|
|
|
i = optlen; /* bye */
|
|
|
|
break;
|
|
|
|
case IPOPT_NOP:
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
case IPOPT_LSRR:
|
|
|
|
case IPOPT_SSRR:
|
2001-02-21 04:27:58 +03:00
|
|
|
if (q[i + IPOPT_OLEN] < 2 + sizeof(struct in_addr) ||
|
|
|
|
optlen - i < q[i + IPOPT_OLEN]) {
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_ERR,
|
|
|
|
"ip_finaldst: invalid IP option "
|
|
|
|
"(code=%02x len=%02x)\n",
|
|
|
|
q[i + IPOPT_OPTVAL], q[i + IPOPT_OLEN]));
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
i += q[i + IPOPT_OLEN] - sizeof(struct in_addr);
|
|
|
|
return (struct in_addr *)(q + i);
|
|
|
|
default:
|
2001-02-21 04:27:58 +03:00
|
|
|
if (q[i + IPOPT_OLEN] < 2 ||
|
|
|
|
optlen - i < q[i + IPOPT_OLEN]) {
|
2000-01-31 17:18:52 +03:00
|
|
|
ipseclog((LOG_ERR,
|
|
|
|
"ip_finaldst: invalid IP option "
|
|
|
|
"(code=%02x len=%02x)\n",
|
|
|
|
q[i + IPOPT_OPTVAL], q[i + IPOPT_OLEN]));
|
1999-06-28 10:36:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
i += q[i + IPOPT_OLEN];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-10-02 07:55:41 +04:00
|
|
|
#endif
|