/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* 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.
*
* 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.
*/
/* assumes that ip header and ah header are contiguous on mbuf */
void *
ah4_ctlinput(int cmd, const struct sockaddr *sa, void *v)
{
struct ip *ip = v;
struct ah *ah;
struct icmp *icp;
struct secasvar *sav;
if (sa->sa_family != AF_INET ||
sa->sa_len != sizeof(struct sockaddr_in))
return NULL;
if ((unsigned)cmd >= PRC_NCMDS)
return NULL;
if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
/*
* Check to see if we have a valid SA corresponding to
* the address in the ICMP message payload.
*/
ah = (struct ah *)((char *)ip + (ip->ip_hl << 2));
sav = KEY_LOOKUP_SA((const union sockaddr_union *)sa,
IPPROTO_AH, ah->ah_spi, 0, 0);
if (sav) {
if (SADB_SASTATE_USABLE_P(sav)) {
/*
* Now that we've validated that we are actually
* communicating with the host indicated in the
* ICMP message, locate the ICMP header,
* recalculate the new MTU, and create the
* corresponding routing entry.
*/
icp = (struct icmp *)((char *)ip -
offsetof(struct icmp, icmp_ip));
icmp_mtudisc(icp, ip->ip_dst);
}
KEY_SA_UNREF(&sav);
}
}
return NULL;
}
/* assumes that ip header and esp header are contiguous on mbuf */
void *
esp4_ctlinput(int cmd, const struct sockaddr *sa, void *v)
{
struct ip *ip = v;
struct esp *esp;
struct icmp *icp;
struct secasvar *sav;
if (sa->sa_family != AF_INET ||
sa->sa_len != sizeof(struct sockaddr_in))
return NULL;
if ((unsigned)cmd >= PRC_NCMDS)
return NULL;
if (cmd == PRC_MSGSIZE && ip_mtudisc && ip && ip->ip_v == 4) {
/*
* Check to see if we have a valid SA corresponding to
* the address in the ICMP message payload.
*/
esp = (struct esp *)((char *)ip + (ip->ip_hl << 2));
sav = KEY_LOOKUP_SA((const union sockaddr_union *)sa,
IPPROTO_ESP, esp->esp_spi, 0, 0);
if (sav) {
if (SADB_SASTATE_USABLE_P(sav)) {
/*
* Now that we've validated that we are actually
* communicating with the host indicated in the
* ICMP message, locate the ICMP header,
* recalculate the new MTU, and create the
* corresponding routing entry.
*/
icp = (struct icmp *)((char *)ip -
offsetof(struct icmp, icmp_ip));
icmp_mtudisc(icp, ip->ip_dst);
}
KEY_SA_UNREF(&sav);
}
}
return NULL;
}
if (sa->sa_family != AF_INET6 ||
sa->sa_len != sizeof(struct sockaddr_in6))
return NULL;
if ((unsigned)cmd >= PRC_NCMDS)
return NULL;
/* if the parameter is from icmp6, decode it. */
if (d != NULL) {
ip6cp = (struct ip6ctlparam *)d;
m = ip6cp->ip6c_m;
ip6 = ip6cp->ip6c_ip6;
off = ip6cp->ip6c_off;
} else {
m = NULL;
ip6 = NULL;
off = 0;
}
if (ip6) {
/* check if we can safely examine src and dst ports */
if (m->m_pkthdr.len < off + sizeof(ah))
return NULL;
if (m->m_len < off + sizeof(ah)) {
/*
* this should be rare case,
* so we compromise on this copy...
*/
m_copydata(m, off, sizeof(ah), &ah);
ahp = &ah;
} else
ahp = (struct newah *)(mtod(m, char *) + off);
if (cmd == PRC_MSGSIZE) {
int valid = 0;
/*
* Check to see if we have a valid SA corresponding
* to the address in the ICMP message payload.
*/
sav = KEY_LOOKUP_SA((const union sockaddr_union *)sa,
IPPROTO_AH, ahp->ah_spi, 0, 0);
if (sav) {
if (SADB_SASTATE_USABLE_P(sav))
valid++;
KEY_SA_UNREF(&sav);
}
/* XXX Further validation? */
/*
* Depending on the value of "valid" and routing
* table size (mtudisc_{hi,lo}wat), we will:
* - recalculate the new MTU and create the
* corresponding routing entry, or
* - ignore the MTU change notification.
*/
icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
}
/* we normally notify single pcb here */
} else {
/* we normally notify any pcb here */
}
return NULL;
}
if (sa->sa_family != AF_INET6 ||
sa->sa_len != sizeof(struct sockaddr_in6))
return NULL;
if ((unsigned)cmd >= PRC_NCMDS)
return NULL;
/* if the parameter is from icmp6, decode it. */
if (d != NULL) {
ip6cp = (struct ip6ctlparam *)d;
m = ip6cp->ip6c_m;
ip6 = ip6cp->ip6c_ip6;
off = ip6cp->ip6c_off;
} else {
m = NULL;
ip6 = NULL;
off = 0;
}
if (ip6) {
/*
* Notify the error to all possible sockets via pfctlinput2.
* Since the upper layer information (such as protocol type,
* source and destination ports) is embedded in the encrypted
* data and might have been cut, we can't directly call
* an upper layer ctlinput function. However, the pcbnotify
* function will consider source and destination addresses
* as well as the flow info value, and may be able to find
* some PCB that should be notified.
* Although pfctlinput2 will call esp6_ctlinput(), there is
* no possibility of an infinite loop of function calls,
* because we don't pass the inner IPv6 header.
*/
memset(&ip6cp1, 0, sizeof(ip6cp1));
ip6cp1.ip6c_src = ip6cp->ip6c_src;
pfctlinput2(cmd, sa, &ip6cp1);
/* check if we can safely examine src and dst ports */
if (m->m_pkthdr.len < off + sizeof(esp))
return NULL;
if (m->m_len < off + sizeof(esp)) {
/*
* this should be rare case,
* so we compromise on this copy...
*/
m_copydata(m, off, sizeof(esp), &esp);
espp = &esp;
} else
espp = (struct newesp *)(mtod(m, char *) + off);
if (cmd == PRC_MSGSIZE) {
int valid = 0;
/*
* Check to see if we have a valid SA corresponding to
* the address in the ICMP message payload.
*/
sav = KEY_LOOKUP_SA((const union sockaddr_union *)sa,
IPPROTO_ESP, espp->esp_spi, 0, 0);
if (sav) {
if (SADB_SASTATE_USABLE_P(sav))
valid++;
KEY_SA_UNREF(&sav);
}
/* XXX Further validation? */
/*
* Depending on the value of "valid" and routing table
* size (mtudisc_{hi,lo}wat), we will:
* - recalculate the new MTU and create the
* corresponding routing entry, or
* - ignore the MTU change notification.
*/
icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
}
} else {
/* we normally notify any pcb here */
}
return NULL;
}
#endif /* INET6 */
static int
sysctl_ipsec(SYSCTLFN_ARGS)
{
int error, t;
struct sysctlnode node;
switch (rnode->sysctl_num) {
case IPSECCTL_DEF_ESP_TRANSLEV:
case IPSECCTL_DEF_ESP_NETLEV:
case IPSECCTL_DEF_AH_TRANSLEV:
case IPSECCTL_DEF_AH_NETLEV:
if (t != IPSEC_LEVEL_USE &&
t != IPSEC_LEVEL_REQUIRE)
return EINVAL;
ipsec_invalpcbcacheall();
break;
case IPSECCTL_DEF_POLICY:
if (t != IPSEC_POLICY_DISCARD &&
t != IPSEC_POLICY_NONE)
return EINVAL;
ipsec_invalpcbcacheall();
break;
default:
return EINVAL;
}
*(int *)rnode->sysctl_data = t;
return 0;
}
#ifdef IPSEC_DEBUG
static int
sysctl_ipsec_test(SYSCTLFN_ARGS)
{
int t, error;
struct sysctlnode node;
switch (newenabled) {
case 0:
if (key_get_used())
return EBUSY;
/*FALLTHROUGH*/
case 1:
case 2:
ipsec_enabled = newenabled;
key_update_used();
return 0;
default:
return EINVAL;
}
}
/* XXX will need a different oid at parent */
void
sysctl_net_inet_ipsec_setup(struct sysctllog **clog)
{
const struct sysctlnode *_ipsec;
int ipproto_ipsec;
/*
* in numerical order:
*
* net.inet.ipip: CTL_NET.PF_INET.IPPROTO_IPIP
* net.inet.esp: CTL_NET.PF_INET.IPPROTO_ESP
* net.inet.ah: CTL_NET.PF_INET.IPPROTO_AH
* net.inet.ipcomp: CTL_NET.PF_INET.IPPROTO_IPCOMP
* net.inet.ipsec: CTL_NET.PF_INET.CTL_CREATE
*
* this creates separate trees by name, but maintains that the
* ipsec name leads to all the old leaves.
*/