History log of /openbsd-current/sbin/iked/config.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.97 15-Feb-2024 tobhe

Delay enabling sockets until ikev2 process is ready.

from markus@


# 1.96 13-Feb-2024 tobhe

Control startup of PROC_CERT and PROC_IKEV2.
Currenly PROC_PARENT sends the configuration to both PROC_CERT and
PROC_IKEV2 and finishes by sending IMSG_CTL_ACTIVE to PROC_IKEV2.

However, when PROC_IKEV2 receives IMSG_CTL_ACTIVE it does not know
the state of PROC_CERT: PROC_CERT might not have processed the
initial configuration while PROC_IKEV2 already sends requests to
PROC_CERT, causing failed requests, or even crashes (NULL deref of
ca_certs).

In order to make sure that PROC_CERT is ready before
IMSG_CTL_ACTIVE is sent to PROC_IKEV2 that startup protocol
is changed as follows:

(1) PROC_PARENT sends configuration to both PROC_CERT and PROC_IKEV2
(2) PROC_PARENT sends IMSG_CTL_ACTIVE to PROC_CERT
(3) PROC_CERT acks IMSG_CTL_ACTIVE by sending it back to PROC_PARENT
(4) PROC_PARENT now knows that PROC_CERT is ready and has processed
all messages from step (1)
(5) PROC_PARENT sends IMSG_CTL_ACTIVE to PROC_IKEV2 and knows that
IMSG_CTL_ACTIVE will be processed by PROC_IKEV2 after all
messages from step (1)
(6) PROC_IKEV2 can now assume that PROC_CERT is ready because it has
already processed IMSG_CTL_ACTIVE

from markus@


# 1.95 17-Jan-2024 claudio

Convert to use imsg_get_fd()

proc_forward_imsg() does not need to forward file descriptors so just use
-1 there. In other places shuffle debug messages around or use a helper
variable since imsg_get_fd() can only be called once.

OK tb@ tobhe@


# 1.94 15-Jan-2024 tobhe

Include cert_partial_chain in iked_static instead of sending a separate
message.

from markus@


Revision tags: OPENBSD_7_4_BASE
# 1.93 04-Aug-2023 claudio

Convert calls to ibuf_length() where it is clear that the ibuf is not
NULL to ibuf_size(). In some cases it is clear that the ibuf pointer
should just be checked for NULL since afterwards a new ibuf is allocated
in its place.
OK tb@


# 1.92 23-May-2023 claudio

Replace ibuf_release() with ibuf_free() since the former just calls the latter
OK kn@ tb@


Revision tags: OPENBSD_7_3_BASE
# 1.91 03-Dec-2022 tobhe

Consistently use uintXX_t from <stdint.h> instead of u_intXX_t.


# 1.90 18-Nov-2022 mbuhl

Revert my last two changes.
If csa_bundled is set, exactly two csas exist and they point to
each other. Therefore childsa_free already sets the bundled csa
pointer to NULL and it shouldn't be done after calling childsa_free.
ok tobhe@


# 1.89 13-Nov-2022 mbuhl

Make sure csa->csa_bundled is NULL after freeing to prevent a
possible use after free.
ok tobhe@


# 1.88 10-Oct-2022 tobhe

Move enabling the policy refcounting from policy_ref() to config_free_policy().
In config_free_policy() the refcounting is unchanged and each SA linked to the
policy will trigger a call to policy_ref() and increase the references as
before the change. This allows unconditional calls to policy_ref() and
policy_unref() and the callers no longer have to check if IKED_POLICY_REFCNT
is set.

From and ok markus@


Revision tags: OPENBSD_7_2_BASE
# 1.87 19-Sep-2022 tobhe

Add iked connection statistics for successful and failed connections, common
error types and other events that help analyze errors in larger setups.
The counters can be printed with 'ikectl show stats'.

ok bluhm@ patrick@
from and ok markus@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.96 13-Feb-2024 tobhe

Control startup of PROC_CERT and PROC_IKEV2.
Currenly PROC_PARENT sends the configuration to both PROC_CERT and
PROC_IKEV2 and finishes by sending IMSG_CTL_ACTIVE to PROC_IKEV2.

However, when PROC_IKEV2 receives IMSG_CTL_ACTIVE it does not know
the state of PROC_CERT: PROC_CERT might not have processed the
initial configuration while PROC_IKEV2 already sends requests to
PROC_CERT, causing failed requests, or even crashes (NULL deref of
ca_certs).

In order to make sure that PROC_CERT is ready before
IMSG_CTL_ACTIVE is sent to PROC_IKEV2 that startup protocol
is changed as follows:

(1) PROC_PARENT sends configuration to both PROC_CERT and PROC_IKEV2
(2) PROC_PARENT sends IMSG_CTL_ACTIVE to PROC_CERT
(3) PROC_CERT acks IMSG_CTL_ACTIVE by sending it back to PROC_PARENT
(4) PROC_PARENT now knows that PROC_CERT is ready and has processed
all messages from step (1)
(5) PROC_PARENT sends IMSG_CTL_ACTIVE to PROC_IKEV2 and knows that
IMSG_CTL_ACTIVE will be processed by PROC_IKEV2 after all
messages from step (1)
(6) PROC_IKEV2 can now assume that PROC_CERT is ready because it has
already processed IMSG_CTL_ACTIVE

from markus@


# 1.95 17-Jan-2024 claudio

Convert to use imsg_get_fd()

proc_forward_imsg() does not need to forward file descriptors so just use
-1 there. In other places shuffle debug messages around or use a helper
variable since imsg_get_fd() can only be called once.

OK tb@ tobhe@


# 1.94 15-Jan-2024 tobhe

Include cert_partial_chain in iked_static instead of sending a separate
message.

from markus@


Revision tags: OPENBSD_7_4_BASE
# 1.93 04-Aug-2023 claudio

Convert calls to ibuf_length() where it is clear that the ibuf is not
NULL to ibuf_size(). In some cases it is clear that the ibuf pointer
should just be checked for NULL since afterwards a new ibuf is allocated
in its place.
OK tb@


# 1.92 23-May-2023 claudio

Replace ibuf_release() with ibuf_free() since the former just calls the latter
OK kn@ tb@


Revision tags: OPENBSD_7_3_BASE
# 1.91 03-Dec-2022 tobhe

Consistently use uintXX_t from <stdint.h> instead of u_intXX_t.


# 1.90 18-Nov-2022 mbuhl

Revert my last two changes.
If csa_bundled is set, exactly two csas exist and they point to
each other. Therefore childsa_free already sets the bundled csa
pointer to NULL and it shouldn't be done after calling childsa_free.
ok tobhe@


# 1.89 13-Nov-2022 mbuhl

Make sure csa->csa_bundled is NULL after freeing to prevent a
possible use after free.
ok tobhe@


# 1.88 10-Oct-2022 tobhe

Move enabling the policy refcounting from policy_ref() to config_free_policy().
In config_free_policy() the refcounting is unchanged and each SA linked to the
policy will trigger a call to policy_ref() and increase the references as
before the change. This allows unconditional calls to policy_ref() and
policy_unref() and the callers no longer have to check if IKED_POLICY_REFCNT
is set.

From and ok markus@


Revision tags: OPENBSD_7_2_BASE
# 1.87 19-Sep-2022 tobhe

Add iked connection statistics for successful and failed connections, common
error types and other events that help analyze errors in larger setups.
The counters can be printed with 'ikectl show stats'.

ok bluhm@ patrick@
from and ok markus@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.95 17-Jan-2024 claudio

Convert to use imsg_get_fd()

proc_forward_imsg() does not need to forward file descriptors so just use
-1 there. In other places shuffle debug messages around or use a helper
variable since imsg_get_fd() can only be called once.

OK tb@ tobhe@


# 1.94 15-Jan-2024 tobhe

Include cert_partial_chain in iked_static instead of sending a separate
message.

from markus@


Revision tags: OPENBSD_7_4_BASE
# 1.93 04-Aug-2023 claudio

Convert calls to ibuf_length() where it is clear that the ibuf is not
NULL to ibuf_size(). In some cases it is clear that the ibuf pointer
should just be checked for NULL since afterwards a new ibuf is allocated
in its place.
OK tb@


# 1.92 23-May-2023 claudio

Replace ibuf_release() with ibuf_free() since the former just calls the latter
OK kn@ tb@


Revision tags: OPENBSD_7_3_BASE
# 1.91 03-Dec-2022 tobhe

Consistently use uintXX_t from <stdint.h> instead of u_intXX_t.


# 1.90 18-Nov-2022 mbuhl

Revert my last two changes.
If csa_bundled is set, exactly two csas exist and they point to
each other. Therefore childsa_free already sets the bundled csa
pointer to NULL and it shouldn't be done after calling childsa_free.
ok tobhe@


# 1.89 13-Nov-2022 mbuhl

Make sure csa->csa_bundled is NULL after freeing to prevent a
possible use after free.
ok tobhe@


# 1.88 10-Oct-2022 tobhe

Move enabling the policy refcounting from policy_ref() to config_free_policy().
In config_free_policy() the refcounting is unchanged and each SA linked to the
policy will trigger a call to policy_ref() and increase the references as
before the change. This allows unconditional calls to policy_ref() and
policy_unref() and the callers no longer have to check if IKED_POLICY_REFCNT
is set.

From and ok markus@


Revision tags: OPENBSD_7_2_BASE
# 1.87 19-Sep-2022 tobhe

Add iked connection statistics for successful and failed connections, common
error types and other events that help analyze errors in larger setups.
The counters can be printed with 'ikectl show stats'.

ok bluhm@ patrick@
from and ok markus@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.93 04-Aug-2023 claudio

Convert calls to ibuf_length() where it is clear that the ibuf is not
NULL to ibuf_size(). In some cases it is clear that the ibuf pointer
should just be checked for NULL since afterwards a new ibuf is allocated
in its place.
OK tb@


# 1.92 23-May-2023 claudio

Replace ibuf_release() with ibuf_free() since the former just calls the latter
OK kn@ tb@


Revision tags: OPENBSD_7_3_BASE
# 1.91 03-Dec-2022 tobhe

Consistently use uintXX_t from <stdint.h> instead of u_intXX_t.


# 1.90 18-Nov-2022 mbuhl

Revert my last two changes.
If csa_bundled is set, exactly two csas exist and they point to
each other. Therefore childsa_free already sets the bundled csa
pointer to NULL and it shouldn't be done after calling childsa_free.
ok tobhe@


# 1.89 13-Nov-2022 mbuhl

Make sure csa->csa_bundled is NULL after freeing to prevent a
possible use after free.
ok tobhe@


# 1.88 10-Oct-2022 tobhe

Move enabling the policy refcounting from policy_ref() to config_free_policy().
In config_free_policy() the refcounting is unchanged and each SA linked to the
policy will trigger a call to policy_ref() and increase the references as
before the change. This allows unconditional calls to policy_ref() and
policy_unref() and the callers no longer have to check if IKED_POLICY_REFCNT
is set.

From and ok markus@


Revision tags: OPENBSD_7_2_BASE
# 1.87 19-Sep-2022 tobhe

Add iked connection statistics for successful and failed connections, common
error types and other events that help analyze errors in larger setups.
The counters can be printed with 'ikectl show stats'.

ok bluhm@ patrick@
from and ok markus@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.92 23-May-2023 claudio

Replace ibuf_release() with ibuf_free() since the former just calls the latter
OK kn@ tb@


Revision tags: OPENBSD_7_3_BASE
# 1.91 03-Dec-2022 tobhe

Consistently use uintXX_t from <stdint.h> instead of u_intXX_t.


# 1.90 18-Nov-2022 mbuhl

Revert my last two changes.
If csa_bundled is set, exactly two csas exist and they point to
each other. Therefore childsa_free already sets the bundled csa
pointer to NULL and it shouldn't be done after calling childsa_free.
ok tobhe@


# 1.89 13-Nov-2022 mbuhl

Make sure csa->csa_bundled is NULL after freeing to prevent a
possible use after free.
ok tobhe@


# 1.88 10-Oct-2022 tobhe

Move enabling the policy refcounting from policy_ref() to config_free_policy().
In config_free_policy() the refcounting is unchanged and each SA linked to the
policy will trigger a call to policy_ref() and increase the references as
before the change. This allows unconditional calls to policy_ref() and
policy_unref() and the callers no longer have to check if IKED_POLICY_REFCNT
is set.

From and ok markus@


Revision tags: OPENBSD_7_2_BASE
# 1.87 19-Sep-2022 tobhe

Add iked connection statistics for successful and failed connections, common
error types and other events that help analyze errors in larger setups.
The counters can be printed with 'ikectl show stats'.

ok bluhm@ patrick@
from and ok markus@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.91 03-Dec-2022 tobhe

Consistently use uintXX_t from <stdint.h> instead of u_intXX_t.


# 1.90 18-Nov-2022 mbuhl

Revert my last two changes.
If csa_bundled is set, exactly two csas exist and they point to
each other. Therefore childsa_free already sets the bundled csa
pointer to NULL and it shouldn't be done after calling childsa_free.
ok tobhe@


# 1.89 13-Nov-2022 mbuhl

Make sure csa->csa_bundled is NULL after freeing to prevent a
possible use after free.
ok tobhe@


# 1.88 10-Oct-2022 tobhe

Move enabling the policy refcounting from policy_ref() to config_free_policy().
In config_free_policy() the refcounting is unchanged and each SA linked to the
policy will trigger a call to policy_ref() and increase the references as
before the change. This allows unconditional calls to policy_ref() and
policy_unref() and the callers no longer have to check if IKED_POLICY_REFCNT
is set.

From and ok markus@


Revision tags: OPENBSD_7_2_BASE
# 1.87 19-Sep-2022 tobhe

Add iked connection statistics for successful and failed connections, common
error types and other events that help analyze errors in larger setups.
The counters can be printed with 'ikectl show stats'.

ok bluhm@ patrick@
from and ok markus@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.90 18-Nov-2022 mbuhl

Revert my last two changes.
If csa_bundled is set, exactly two csas exist and they point to
each other. Therefore childsa_free already sets the bundled csa
pointer to NULL and it shouldn't be done after calling childsa_free.
ok tobhe@


# 1.89 13-Nov-2022 mbuhl

Make sure csa->csa_bundled is NULL after freeing to prevent a
possible use after free.
ok tobhe@


# 1.88 10-Oct-2022 tobhe

Move enabling the policy refcounting from policy_ref() to config_free_policy().
In config_free_policy() the refcounting is unchanged and each SA linked to the
policy will trigger a call to policy_ref() and increase the references as
before the change. This allows unconditional calls to policy_ref() and
policy_unref() and the callers no longer have to check if IKED_POLICY_REFCNT
is set.

From and ok markus@


Revision tags: OPENBSD_7_2_BASE
# 1.87 19-Sep-2022 tobhe

Add iked connection statistics for successful and failed connections, common
error types and other events that help analyze errors in larger setups.
The counters can be printed with 'ikectl show stats'.

ok bluhm@ patrick@
from and ok markus@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.89 13-Nov-2022 mbuhl

Make sure csa->csa_bundled is NULL after freeing to prevent a
possible use after free.
ok tobhe@


# 1.88 10-Oct-2022 tobhe

Move enabling the policy refcounting from policy_ref() to config_free_policy().
In config_free_policy() the refcounting is unchanged and each SA linked to the
policy will trigger a call to policy_ref() and increase the references as
before the change. This allows unconditional calls to policy_ref() and
policy_unref() and the callers no longer have to check if IKED_POLICY_REFCNT
is set.

From and ok markus@


Revision tags: OPENBSD_7_2_BASE
# 1.87 19-Sep-2022 tobhe

Add iked connection statistics for successful and failed connections, common
error types and other events that help analyze errors in larger setups.
The counters can be printed with 'ikectl show stats'.

ok bluhm@ patrick@
from and ok markus@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.88 10-Oct-2022 tobhe

Move enabling the policy refcounting from policy_ref() to config_free_policy().
In config_free_policy() the refcounting is unchanged and each SA linked to the
policy will trigger a call to policy_ref() and increase the references as
before the change. This allows unconditional calls to policy_ref() and
policy_unref() and the callers no longer have to check if IKED_POLICY_REFCNT
is set.

From and ok markus@


Revision tags: OPENBSD_7_2_BASE
# 1.87 19-Sep-2022 tobhe

Add iked connection statistics for successful and failed connections, common
error types and other events that help analyze errors in larger setups.
The counters can be printed with 'ikectl show stats'.

ok bluhm@ patrick@
from and ok markus@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.87 19-Sep-2022 tobhe

Add iked connection statistics for successful and failed connections, common
error types and other events that help analyze errors in larger setups.
The counters can be printed with 'ikectl show stats'.

ok bluhm@ patrick@
from and ok markus@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.86 08-Jul-2022 tobhe

Support sending certificate chains with intermediate CAs in multiple CERT
payloads. Local certificate chains as required with LetsEncrypt certs will
work between iked and other IKEv2 implementations, iked to iked connections
won't work yet because of missing support to receive multiple CERT
payloads.

from Katsuhiro Ueno
tested by and ok sthen@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.85 08-May-2022 tobhe

Move ikev2_reset_alive_timer() to a place where it makes more sense. The idea
is to renew the timer every time sc_alive_timeout is reset after loading a new
config.

ok patrick@


Revision tags: OPENBSD_7_1_BASE
# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.84 25-Nov-2021 tobhe

Silence unitialized variable warnings.


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.83 24-Nov-2021 tobhe

Pass env to pfkey API. Consistently call pfkey file descriptor fd.

ok bluhm@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.82 12-Oct-2021 tobhe

Make sure all copies of MSCHAPv2 passphrase are zeroed after use.

ok patrick@


Revision tags: OPENBSD_7_0_BASE
# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.81 18-Sep-2021 deraadt

freezero() instead of free(), because the object may contain a password
ok tobhe


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.80 01-Sep-2021 tobhe

Add client side support for DNS configuration. Use RTM_PROPOSAL_STATIC
route messages to propose the name server to resolvd(8).
For now, iked will only propose a single name server from the first
established connection.

Automatic name server configuration is enabled by default for policies using
the 'iface' option.

discussed with deraadt@
ok for the DNS parts florian@
ok for the rest patrick@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.79 13-May-2021 tobhe

Refactor iked process shutdown and cleanup. Remember configured
addresses and routes in iked_vroute_sc to not depend on ikev2
process for cleanup.

This makes sure that all flows, routes and addresses are deleted
no matter which process is killed first.

ok patrick@


Revision tags: OPENBSD_6_9_BASE
# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.78 22-Feb-2021 tobhe

Don't pass 'id' as argument to make function signature match similar
functions. config_setpfkey() is always called with id PROC_IKEV2.


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.77 13-Feb-2021 tobhe

Add dynamic address configuration for roadwarrior clients.
The new 'iface' config option can be used to specify an interface
for the virtual addresses received from the peer.
Routes are automatically added based on the configured flows.

Input from sthen@ and claudio@
ok patrick@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.76 08-Feb-2021 tobhe

Clean up kernel IPsec flows and security associations on shutdown.

Discussed with sthen@
ok patrick@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.75 21-Jan-2021 tobhe

Add support for INVALID_KE_PAYLOAD in CREATE_CHILD_SA
exchange. In the case of an invalid KE error, retry
CREATE_CHILD_SA exchange with different group instead
of restarting the full IKE handshake.

ok markus@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.74 29-Nov-2020 tobhe

Add 'set stickyaddress' option. If this option is enabled, iked will try
to assign the same 'config address' when an IKESA is negotiated with the
DSTID of an existing IKESA. The original IKESA will be closed and the
address will be transferred to the new IKESA.

ok patrick@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.73 25-Nov-2020 tobhe

Fix proposal error handling. If a proposal contains an unknown transform
type or id, ignore the proposal instead of failing the exchange.

ok patrick@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.72 29-Oct-2020 tobhe

Add initial support to request IP addresses as IKEv2 initiator.
At the moment the address is only negotiated and printed to the
log. If 'request addr 0.0.0.0' is configured, any address will
be accepted.

ok patrick@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.71 21-Oct-2020 tobhe

Remove SAs from ike_dstid_sas on 'ikectl reset sa' to prevent use after free.

Found by csszep <csszep (at) gmail (dot) com>
ok patrick@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.70 09-Oct-2020 tobhe

More unused headers.


# 1.69 09-Oct-2020 tobhe

Remove unused "wait.h" includes.


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.68 30-Sep-2020 tobhe

Don't leak sa->sa_peerauth.id_buf.

ok patrick@


Revision tags: OPENBSD_6_8_BASE
# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.67 25-Sep-2020 tobhe

Simplify RB_TREE cleanup loops.

ok markus@


# 1.66 23-Sep-2020 tobhe

Add new 'set cert_partial_chain' config option to allow verification of
partial certificate chains if a trusted intermediate CA is found in
/etc/iked/ca/.

ok patrick@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.65 26-Aug-2020 tobhe

Allow disabling DPD liveness checks by setting dpd_check_interval to 0.

ok patrick@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.64 25-Aug-2020 tobhe

Add dpd_check_interval configuration option. If for any IKE SA no IPsec
or IKE message has been received within the specified time interval,
iked will start sending DPD messages.

ok patrick@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.63 24-Aug-2020 tobhe

Reduce the amount of boilerplate code and imsgs for config options by
grouping fixed-size values in 'struct iked_static' which is sent in
a single message.

ok patrick@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.62 23-Aug-2020 tobhe

Add a new configuration option to limit the number of connections for
each peer (identified by their 'dstid'). When 'set enforcesingleikesa'
is enabled, each peer can only have one active IKE SA at a time.
On successful authentication of a new connection, the old IKE SA is
automatically deleted.

ok patrick@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.61 18-Aug-2020 tobhe

Add optional time-stamp validaten for ocsp. The new optional 'tolerate'
parameter specifies how many seconds leeway are allowed in the check.
The optional maxage parameter indicates the allowed maximum age of
the `thisUpdate' OCSP attribute value.

ok patrick@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.60 16-Aug-2020 tobhe

Clean up unused parameters.


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.59 14-Aug-2020 tobhe

Clean up unused variables.


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.58 23-Jul-2020 tobhe

Fix ibuf leak in sa_localauth when SA is freed.

ok patrick@


Revision tags: OPENBSD_6_7_BASE
# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.57 13-Apr-2020 tobhe

Try to send a DELETE message if the SA is reset with 'ikectl reset id'.
This way the peer can delete its SAs and eventually reestablish the
connection without having to wait for a timeout.

ok markus@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.56 09-Apr-2020 tobhe

Simplify socket creation logic. Normally iked needs two sockets, one
for normal operation (UDP port 500) and one for NAT traversal (UDP 4500).
There are several command line options resulting in only one of the sockets
being created (-T, -t and -p). Add a new 'enum natt_mode' to make the
logic for those somewhat less complicated as well as some comments where
it makes sense.

From Wataru Ashihara <wataash (at) wataash (dot) com>
ok patrick@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.55 24-Mar-2020 tobhe

Fix user database corruption from 'ikectl reload'. Copy only the new password
instead of the full iked_user struct to preserve the RB_TREE pointers.

From Bernardo Cunha Vieira <bernardo (dot) vieira (at) almg (dot) gov (dot) br>
ok patrick@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.54 09-Mar-2020 tobhe

Use TAILQ_FOREACH_SAFE instead of hand rolled loops.

From Wataru <wataash at wataash dot com>
ok patrick@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.53 16-Jan-2020 tobhe

Add '-p' command line option which allows to configure
the UDP encapsulation port, similar to isakmpd's '-N' flag.
Being able to change the UDP encapsulation port is useful in cases
where ESP and UDP ports 500 and 4500 are blocked or rate limited.

ok sthen@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.52 07-Jan-2020 tobhe

Link ESP-SA and IPcomp-SA using GRPSPIS instead of using a self-built
solution for multi-SA flows. As a result we only need a single
outgoing IPCOMP flow and can get rid of the two extra transport mode flows
for ESP.

ok bluhm@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.51 03-Dec-2019 tobhe

Correctly represent flows as traffic selectors as described in RFC 7296. This
allows us to deduplicate the network ranges sent in the TS payload and saves
some bytes on the wire.

ok patrick@


Revision tags: OPENBSD_6_6_BASE
# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.50 11-May-2019 patrick

Add support for IKEv2 Message Fragmentation as defined in RFC 7383.

ok sthen@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@


# 1.49 27-Nov-2017 patrick

Implement MOBIKE (RFC 4555) support in iked(8), with us acting as
responder. In practice this support means that clients like iPhones
can roam in different networks (LTE, WiFi) and change their external
addresses without having to re-do the whole handshake. It allows the
client to choose how and when to change the external tunnel endpoint
addresses on demand, depending on which network is better or even is
connected at all.

ok sthen@
tweaks from jmc@
tested by a handful


Revision tags: OPENBSD_6_2_BASE
# 1.48 13-Apr-2017 patrick

Add a NAT-T keepalive timer in case we are behind a NAT gateway.

See RFC 5996, section 2.23, NAT Traversal:
In the case of a mismatching NAT_DETECTION_DESTINATION_IP hash, it
means that the system receiving the NAT_DETECTION_DESTINATION_IP
payload is behind a NAT and that system SHOULD start sending
keepalive packets as defined in [UDPENCAPS].

With markus@, ok reyk@


Revision tags: OPENBSD_6_1_BASE
# 1.47 27-Mar-2017 mikeb

Factor out flows into separate configuration messages

We reach an imsg payload limit with just a few traffic selectors
so in order to load more we need to split them up and send separately.

Suggested and OK reyk


# 1.46 27-Mar-2017 reyk

spacing


# 1.45 27-Mar-2017 reyk

Fix another iked leak of SAs in pfkey_sa(), copy tags correctly.

Diff from markus@
OK mikeb@ patrick@


# 1.44 13-Mar-2017 mikeb

Resolve simultaneous Child SA rekeying

From and OK markus, OK reyk


# 1.43 03-Jan-2017 reyk

Fix pledge of the ca process by calling the right function on startup.
As a related change, load the local.pub and local.key keys after
privsep and reload them on SIGHUP/reload.

OK mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.42 01-Jun-2016 patrick

Implement a second address pool specifically for IPv6, so that
clients can be given an IPv4 and IPv6 address at the same time,
thus enabling dual stack usage.

ok markus@ mikeb@


Revision tags: OPENBSD_5_9_BASE
# 1.41 07-Dec-2015 reyk

Sync proc.c, use shorter proc_compose[v]()


# 1.40 18-Nov-2015 reyk

pledge exposed a simple bug: the unprivileged child tried to print the
policy after receiving it from the parent. print_policy ->
print_proto -> getprotobynumber -> pledge abort because it tried to
access /etc/protocols without rpath. It was just a debugging message
that can be moved to the parent (printing the policy on the sender
side and not the receiver side). The parent has rpath and dns.

Issue found by sthen@ with "proto etherip"
OK sthen@ benno@


# 1.39 19-Oct-2015 reyk

Remove the ikev1 stub - Since I started iked, it has an empty privsep
process for ISAKMP+IKEv1. I kept it to let somebody either contribute
the old protocol one day, I never intended to implement IKEv1 myself,
or to add a new kind of pipe to isakmpd to hand off IKEv1 messages.
As IKEv2 is widely supported by all major OS and networking vendors
now, I'm happy to scrap the idea of supporting ISAKMP+IKEv1. It is
still possible to use isakmpd for legacy VPNs.

OK mikeb@


# 1.38 15-Oct-2015 mmcc

Remove some unnecessary NULL-checks before free(). Change two bzero()
calls on pf data to explicit_bzero().

ok mikeb@


# 1.37 21-Aug-2015 reyk

Switch iked to C99-style fixed-width integer types.

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.36 07-Jul-2015 markus

repair policy-ikesa-linking by replacing the broken RB_TREE w/TAILQ
(e.g. the policy might be used-after-free on 'ikectl reconfig')
ok mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.35 06-Feb-2015 deraadt

unneeded getopt.h


# 1.34 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.33 03-Dec-2014 deraadt

Init SPI using arc4random_buf, rather than (r << 32) | r
ok matthew


# 1.32 08-Oct-2014 deraadt

trivial use of reallocarray()


Revision tags: OPENBSD_5_6_BASE
# 1.31 06-May-2014 markus

change the create-child-sa responder code, so it does not store any
state in the ikesa structure. this way we can initiate a create-child-sa
and process requests for the peer at the same time. ok mikeb@


# 1.30 06-May-2014 markus

initiate ike sa rekeying (ikesalifetime keyword), re-queue pfkey
events while we are busy initiating child-SAs; ok mikeb@


# 1.29 06-May-2014 markus

cleanup IKE-SA tree handling (fixes repeated-insert & double-remove)

sa_new() always re-inserts an SA into the SA tree. in case of a key
collision it would try to free the new SA. While doing that it would
accidentially free the existing SA, since config_free_sa() does
RB_REMOVE() uncoditionally. This change fixes this by:
a) moving the responsibility for RB_REMOVE() to CALLER of config_free_sa() and
b) by calling config_free_sa() instead of sa_free() from sa_new()
It also changes to code to NEVER re-add an SA to the tree. So
RB_INSERT() is ONLY called once per SA. The code also makes sure
that there is always a KEY defined for this tree (ispi).

ok mikeb@


# 1.28 06-May-2014 markus

initial support for PFS; ok reyk@


# 1.27 22-Apr-2014 reyk

Update iked to use the same proc.c that relayd uses.
Less differences, less code to audit.

ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.26 17-Feb-2014 markus

interpret 'config address net/prefix' as a pool of addresses and
randomly choose the address for CFG_REQUEST. this address will be used
to replace 0.0.0.0/32 in the specified flow. e.g.
> ikev2 passive esp from 192.168.1.0/24 to 0.0.0.0 \
> config address 192.168.10.200/24
will assign an address between 192.168.10.200 and 192.168.10.254
and replace 0.0.0.0 with this address.
ok mikeb@ on older version of this diff.


# 1.25 17-Feb-2014 markus

basic OCSP support. enable with 'set ocsp "http://10.0.0.10:8888/"'
ok mikeb@


# 1.24 24-Jan-2014 mikeb

use a bit saner timer api


# 1.23 03-Dec-2013 markus

never cast to sockaddr_storage, always cast to the abstract 'class' sockaddr
this fixes an out-of-bounds-memcpy in pfkey_process(); ok mikeb@


# 1.22 28-Nov-2013 markus

don't leak duplicate flows; ok mikeb@


# 1.21 24-Oct-2013 deraadt

no need for netinet/ip_var.h (and friends)


Revision tags: OPENBSD_5_4_BASE
# 1.20 21-Mar-2013 deraadt

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.19 08-Jan-2013 reyk

Remove private CVS tag from an obsolete repository and bump copyright
to 2013 while I'm here... this is my way of saying "happy new year!".


# 1.18 15-Dec-2012 reyk

Remove unused variables.


# 1.17 22-Oct-2012 reyk

Fix NAT-T support in iked, both on the initiator and the responder
side. Also add a new command line option -t to optionally enforce
NAT-T with UDP encapsulation on port 4500.

Tested by mikeb@ and me
ok mikeb@


# 1.16 18-Sep-2012 reyk

update email addresses to match reality.
sure jsg@ mikeb@


Revision tags: OPENBSD_5_2_BASE
# 1.15 02-Jul-2012 mikeb

Don't close IKE SA immediately after creating a new one when rekeying.
Instead set a timeout that will shut it down in case we don't get an SA
delete notification.


# 1.14 22-Jun-2012 mikeb

Add initial support for retransmition timeouts and response retries.
This should still be considered an experimental work in progress.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.13 05-Jul-2011 tedu

fix memcpy sizeof. found by jsg. ok deraadt krw mikeb


# 1.12 09-May-2011 reyk

rename functions in proc.c to proc_* and move some code from imsg_util.c to
proc.c. this is the first sync to what i did for relayd but does not include
the multi-instance handling - so no functional change.


# 1.11 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


# 1.10 18-Apr-2011 reyk

When the kernel wants to acquire an SA for an unknown flow, lookup a
matching policy and init a new IKE SA. This adds support for "acquire mode"
from static flows.

ok mikeb@


Revision tags: OPENBSD_4_9_BASE
# 1.9 26-Jan-2011 mikeb

get rid of acquire flows completely, as they tend to pass traffic
when there's no sa established (as pointed out by reyk). instead
use require mode feature to send acquires from the kernel. this
allows us to get rid of the code that changes flow mode to acquire
and keep all installed flows in the tree and save up on some code
that deals with renegotiation. also several entities were renamed
(iked_acqflows -> iked_activeflows, iked_ipsecsas -> iked_activesas,
ikev2_acquire -> ikev2_acquire_sa). ok reyk


# 1.8 21-Jan-2011 reyk

Reimplement the iked(8) policy evaluation for incoming connections to
use the last matching semantics of PF. The previous rbtree-based
implementation was broken and tried to do a longest prefix match. But
instead of prefix match and using radix-trees to fix it I decided with
mikeb@ to implement it as last matching policy evaluation. The last
matching policy wins; the "quick" keyword can enforce first matching;
additional keywords like "skip" are specific to iked(8). See
iked.conf(5) for more details.

The implementation also uses skip steps based on PF's code. It
significantly speeds up the evaluation of many policies but also adds
a little delay when loading them (only noticeable with thousands of
policies). This allows iked(8) to scale well with thousands of
configured policies but I also liked the fact to have skip steps in
another piece of code.

ok dhartmei@ for using his skip step code under the ISC license in policy.c
ok mikeb@, jmc@


# 1.7 21-Jan-2011 reyk

split pfkey initialization into a privileged and unprivileged part to
prevent a possible crash.

ok mikeb@


# 1.6 12-Jan-2011 mikeb

postpone processing of pfkey messages received in pfkey_reply instead of
just dropping them; ok reyk


# 1.5 22-Dec-2010 mikeb

child sa rekeying revamp plus numerous bugfixes;
with suggestions and OK from reyk


Revision tags: OPENBSD_4_8_BASE
# 1.4 14-Jun-2010 reyk

More code for initiator mode (not finished yet)


# 1.3 10-Jun-2010 reyk

add new commands: the couple/decouple commands will set loading of the
learned flows and SAs to the kernel which is useful for testing and
debugging. the active/passive commands are required to use iked
with sasyncd(8); sasyncd just needs to call "ikectl active/passive" or
send the appropriate imsg to support iked but this is not implemented yet.


# 1.2 10-Jun-2010 reyk

Add another tree to lookup policy SAs by peer address.


# 1.1 03-Jun-2010 reyk

Import iked, a new implementation of the IKEv2 protocol.

iked(8) is an automatic keying daemon for IPsec, like isakmpd(8), that
IPsec creates flows and SAs automatically. Unlike isakmpd, iked(8)
implements the newer IKEv2 protocol instead of IKEv1/ISAKMP. The
daemon is still work-in-progress and not enabled in the builds, but is
already able to establish IKEv2 sessions with some other IKEv2
implementations as a responder.

with lots of help and debugging by jsg@
ok deraadt@