History log of /openbsd-current/sbin/iked/pfkey.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.84 14-Aug-2023 tobhe

Improve error message when if_indextoname() fails.


# 1.83 11-Aug-2023 tobhe

Add iked support for route based sec(4) tunnels.

To use sec(4) instead of policy based tunnels, create a sec(4)
interface and add 'iface secXX' to your policy config.
sec(4) interfaces also support auto configuration for dynamic client
IPs via 'request any' like all other interfaces.
The config won't work without traffic selectors, 'from any to any'
should work for now but I plan to make this optional in the future.

ok dlg@


# 1.82 13-Jun-2023 tb

iked: introduce and use print_addr()

The vast majority of print_host() callers cast the first argument
(usually a sockaddr_storage *) to (struct sockaddr *) and pass both
a NULL buffer and 0 length. Cast and useless arguments lead to
awkward line wrapping in many places. Clean this up by introducing a
helper. Make this helper take a void pointer, so all casts go away.

ok claudio kn tobhe


Revision tags: OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.81 22-Jul-2022 tobhe

Fix potential leak of reply in error case.

From markus@
ok bluhm@


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

Remove unused variable fd.


# 1.79 24-Nov-2021 tobhe

Unregister event on pfkey socket during pfkey_reply(). Using events
and poll() at the same time may lead to a race that locks up the
process in recv().

ok bluhm@


# 1.78 24-Nov-2021 tobhe

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

ok bluhm@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.77 02-Mar-2021 jsg

Increase the size of iov in pfkey_sa() to be large enough for all
possible options.

ok tobhe@


# 1.76 29-Jan-2021 tobhe

Add proper padding for pfkey messages. Use ROUNDUP() for auth and
enc keys.

ok patrick@


# 1.75 23-Jan-2021 tobhe

Fix typos.

From Ryan Kavanagh
ok patrick@


# 1.74 04-Dec-2020 tobhe

Log pfkey type and message length on write failure.


# 1.73 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 11-Aug-2023 tobhe

Add iked support for route based sec(4) tunnels.

To use sec(4) instead of policy based tunnels, create a sec(4)
interface and add 'iface secXX' to your policy config.
sec(4) interfaces also support auto configuration for dynamic client
IPs via 'request any' like all other interfaces.
The config won't work without traffic selectors, 'from any to any'
should work for now but I plan to make this optional in the future.

ok dlg@


# 1.82 13-Jun-2023 tb

iked: introduce and use print_addr()

The vast majority of print_host() callers cast the first argument
(usually a sockaddr_storage *) to (struct sockaddr *) and pass both
a NULL buffer and 0 length. Cast and useless arguments lead to
awkward line wrapping in many places. Clean this up by introducing a
helper. Make this helper take a void pointer, so all casts go away.

ok claudio kn tobhe


Revision tags: OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.81 22-Jul-2022 tobhe

Fix potential leak of reply in error case.

From markus@
ok bluhm@


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

Remove unused variable fd.


# 1.79 24-Nov-2021 tobhe

Unregister event on pfkey socket during pfkey_reply(). Using events
and poll() at the same time may lead to a race that locks up the
process in recv().

ok bluhm@


# 1.78 24-Nov-2021 tobhe

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

ok bluhm@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.77 02-Mar-2021 jsg

Increase the size of iov in pfkey_sa() to be large enough for all
possible options.

ok tobhe@


# 1.76 29-Jan-2021 tobhe

Add proper padding for pfkey messages. Use ROUNDUP() for auth and
enc keys.

ok patrick@


# 1.75 23-Jan-2021 tobhe

Fix typos.

From Ryan Kavanagh
ok patrick@


# 1.74 04-Dec-2020 tobhe

Log pfkey type and message length on write failure.


# 1.73 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 13-Jun-2023 tb

iked: introduce and use print_addr()

The vast majority of print_host() callers cast the first argument
(usually a sockaddr_storage *) to (struct sockaddr *) and pass both
a NULL buffer and 0 length. Cast and useless arguments lead to
awkward line wrapping in many places. Clean this up by introducing a
helper. Make this helper take a void pointer, so all casts go away.

ok claudio kn tobhe


Revision tags: OPENBSD_7_2_BASE OPENBSD_7_3_BASE
# 1.81 22-Jul-2022 tobhe

Fix potential leak of reply in error case.

From markus@
ok bluhm@


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

Remove unused variable fd.


# 1.79 24-Nov-2021 tobhe

Unregister event on pfkey socket during pfkey_reply(). Using events
and poll() at the same time may lead to a race that locks up the
process in recv().

ok bluhm@


# 1.78 24-Nov-2021 tobhe

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

ok bluhm@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.77 02-Mar-2021 jsg

Increase the size of iov in pfkey_sa() to be large enough for all
possible options.

ok tobhe@


# 1.76 29-Jan-2021 tobhe

Add proper padding for pfkey messages. Use ROUNDUP() for auth and
enc keys.

ok patrick@


# 1.75 23-Jan-2021 tobhe

Fix typos.

From Ryan Kavanagh
ok patrick@


# 1.74 04-Dec-2020 tobhe

Log pfkey type and message length on write failure.


# 1.73 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 22-Jul-2022 tobhe

Fix potential leak of reply in error case.

From markus@
ok bluhm@


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

Remove unused variable fd.


# 1.79 24-Nov-2021 tobhe

Unregister event on pfkey socket during pfkey_reply(). Using events
and poll() at the same time may lead to a race that locks up the
process in recv().

ok bluhm@


# 1.78 24-Nov-2021 tobhe

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

ok bluhm@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.77 02-Mar-2021 jsg

Increase the size of iov in pfkey_sa() to be large enough for all
possible options.

ok tobhe@


# 1.76 29-Jan-2021 tobhe

Add proper padding for pfkey messages. Use ROUNDUP() for auth and
enc keys.

ok patrick@


# 1.75 23-Jan-2021 tobhe

Fix typos.

From Ryan Kavanagh
ok patrick@


# 1.74 04-Dec-2020 tobhe

Log pfkey type and message length on write failure.


# 1.73 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 25-Nov-2021 tobhe

Remove unused variable fd.


# 1.79 24-Nov-2021 tobhe

Unregister event on pfkey socket during pfkey_reply(). Using events
and poll() at the same time may lead to a race that locks up the
process in recv().

ok bluhm@


# 1.78 24-Nov-2021 tobhe

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

ok bluhm@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.77 02-Mar-2021 jsg

Increase the size of iov in pfkey_sa() to be large enough for all
possible options.

ok tobhe@


# 1.76 29-Jan-2021 tobhe

Add proper padding for pfkey messages. Use ROUNDUP() for auth and
enc keys.

ok patrick@


# 1.75 23-Jan-2021 tobhe

Fix typos.

From Ryan Kavanagh
ok patrick@


# 1.74 04-Dec-2020 tobhe

Log pfkey type and message length on write failure.


# 1.73 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 24-Nov-2021 tobhe

Unregister event on pfkey socket during pfkey_reply(). Using events
and poll() at the same time may lead to a race that locks up the
process in recv().

ok bluhm@


# 1.78 24-Nov-2021 tobhe

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

ok bluhm@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.77 02-Mar-2021 jsg

Increase the size of iov in pfkey_sa() to be large enough for all
possible options.

ok tobhe@


# 1.76 29-Jan-2021 tobhe

Add proper padding for pfkey messages. Use ROUNDUP() for auth and
enc keys.

ok patrick@


# 1.75 23-Jan-2021 tobhe

Fix typos.

From Ryan Kavanagh
ok patrick@


# 1.74 04-Dec-2020 tobhe

Log pfkey type and message length on write failure.


# 1.73 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 02-Mar-2021 jsg

Increase the size of iov in pfkey_sa() to be large enough for all
possible options.

ok tobhe@


# 1.76 29-Jan-2021 tobhe

Add proper padding for pfkey messages. Use ROUNDUP() for auth and
enc keys.

ok patrick@


# 1.75 23-Jan-2021 tobhe

Fix typos.

From Ryan Kavanagh
ok patrick@


# 1.74 04-Dec-2020 tobhe

Log pfkey type and message length on write failure.


# 1.73 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 29-Jan-2021 tobhe

Add proper padding for pfkey messages. Use ROUNDUP() for auth and
enc keys.

ok patrick@


# 1.75 23-Jan-2021 tobhe

Fix typos.

From Ryan Kavanagh
ok patrick@


# 1.74 04-Dec-2020 tobhe

Log pfkey type and message length on write failure.


# 1.73 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 23-Jan-2021 tobhe

Fix typos.

From Ryan Kavanagh
ok patrick@


# 1.74 04-Dec-2020 tobhe

Log pfkey type and message length on write failure.


# 1.73 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 04-Dec-2020 tobhe

Log pfkey type and message length on write failure.


# 1.73 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 01-Dec-2020 tobhe

Don't log ESRCH as warning.


# 1.72 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 05-Nov-2020 phessler

Enable support for ASN1_DN ipsec identifiers.

Tested with multiple Window 10 Pro (ver 2004) clients, and OpenBSD+iked
as the server.

OK tobhe@ sthen@ kn@


Revision tags: OPENBSD_6_8_BASE
# 1.71 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 09-Sep-2020 tobhe

Delete dead code.


# 1.70 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 28-Aug-2020 tobhe

Rename ikev2_*_sa() functions to make clear they handle Child SAs.

ok patrick@


# 1.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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.69 13-Aug-2020 tobhe

Properly set flow_saproto for aquire.

ok patrick@


# 1.68 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 21-Jul-2020 tobhe

Handle TEMPORARY_FAILURE notification on IKESA rekeying.
If we rekey both the IKESA and an CHILDSA in a small time
window a strongswan peer might reposend with a TEMPORARY_FAILURE
notification.
In this case we retry the rekey of the IKESA after a short
timeout and queue PFKEY expire messages (by returning -1 in
ikev2_rekey_sa()), so the CHILDSA rekeying gets delayed.

ok markus@


# 1.67 19-Jul-2020 tobhe

Try to deal with no reply from PF_KEY on pfkey_sa_add.

ok markus@


# 1.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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.66 26-Jun-2020 bket

Replace SIMPLEQ concatenation loop with SIMPLEQ_CONCAT

OK florian@, kn@, millert@


# 1.65 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 13-May-2020 tobhe

Remove dead 'iked_flow' member 'flow_type'.


Revision tags: OPENBSD_6_7_BASE
# 1.64 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 23-Apr-2020 tobhe

Add support for switching rdomain on IPsec encryption/decryption.
It can be configured per policy with the new 'rdomain' option
(see iked.conf(5)).
Only the unencrypted (inner) rdomain has to be configured, the
encrypted rdomain is always the one the responsible iked instance
is running in.

The configured rdomain must exist before iked activates the IPsec SAs,
otherwise pfkey will return an error.

ok markus@, patrick@


# 1.63 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 14-Jan-2020 tobhe

Remove IPsec flow blocking unencrypted IPv6 traffic which was
meant to prevent VPN leakage but repeatedly broke people's
setups. The -6 flag which used to disable the blocking flow is
now ignored and prints a deprecation warning.

ok kn@ bluhm@ phessler@


# 1.62 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 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.61 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 29-Nov-2019 tobhe

Change the default security level for incoming IPsec flows from
isakmpd and iked to REQUIRE. Filter policy violations earlier.

ok sashan@ bluhm@


Revision tags: OPENBSD_6_5_BASE OPENBSD_6_6_BASE
# 1.60 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 07-Dec-2018 mpi

Make sure the TAP extension is only added to the vector when needed.

Fix a problem reported by Mark Patruck and dhill@

ok markus@, dhill@


Revision tags: OPENBSD_6_3_BASE OPENBSD_6_4_BASE
# 1.59 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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 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.58 18-Apr-2017 deraadt

use freezero()


Revision tags: OPENBSD_6_1_BASE
# 1.57 27-Mar-2017 reyk

spacing


# 1.56 27-Mar-2017 reyk

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

Diff from markus@
OK mikeb@ patrick@


# 1.55 13-Mar-2017 reyk

NAT-T improvements

Move repeated creation of the NAT-T payload into a function, remove
erroneous msg_offset, and improve NAT-T handling.

From and OK markus, OK mikeb


# 1.54 13-Mar-2017 patrick

When setting up IPcomp flows for the networks 'A' and 'B' between
gateways 'a' and 'b', we replace the ESP flow "A->B ESP" with an
IPCOMP flow "A->B IPCOMP" and add a matching (transport mode) ESP
flow between the gateways "a->b ESP". The later is now marked with
flow_ipcomp so it is not translated into "a->b IPCOMP" on rekeying.

When SAs get deleted we do an extra loop to figure out if matching
IPcomp SAs can now be removed, too. This allows faster expiry of
unused IPcomp SAs.

Disable bytes lifetime for IP compression.

ok markus@ reyk@


# 1.53 28-Feb-2017 bluhm

Depending on the addresses, ipsecctl(8) automatically groups sa
bundles together. Extend the kernel interface to export the bundle
information to userland. Then ipsecctl -ss -v can show the internal
relations. Unfortunately the header SADB_X_EXT_PROTOCOL was reused
by SADB_X_GRPSPIS, so it cannot be used to transfer the second sa
type with sysctl. Introduce a new SADB_X_EXT_SATYPE2 and use it
consistently.
OK hshoexer@ markus@


# 1.52 03-Sep-2016 vgross

Add the missing bits to have NAT on enc(4) support in iked.

Ok mikeb@


Revision tags: OPENBSD_6_0_BASE
# 1.51 07-Mar-2016 mmcc

http -> https for IETF/IANA URLs in comments


Revision tags: OPENBSD_5_9_BASE
# 1.50 10-Dec-2015 mmcc

comment typo


# 1.49 09-Dec-2015 naddy

Remove plain DES encryption from IPsec.

DES is insecure since brute force attacks are practical due to its
short key length.

This removes support for DES-CBC encryption in ESP and in IKE main
and quick mode from the kernel, isakmpd(8), ipsecctl(8), and iked(8).

ok mikeb@


# 1.48 02-Dec-2015 naddy

remove unimplemented PF_KEY algorithms; ok sthen@ mpi@ mikeb@


# 1.47 04-Nov-2015 mikeb

Support Chacha20-Poly1305 for Child SAs; ok reyk


# 1.46 15-Oct-2015 mmcc

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

ok mikeb@


# 1.45 26-Aug-2015 jsg

use 0xffff not 0xfffff for a 16 bit port constant
ok mikeb@


# 1.44 21-Aug-2015 reyk

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

OK mikeb@


Revision tags: OPENBSD_5_8_BASE
# 1.43 17-Jul-2015 mikeb

Assign correct destination port value for the destination netmask.

This repairs setup of SPD flows that specify port only on the one
side of the from-to specification.

ok markus


# 1.42 05-Jun-2015 vgross

Fix coupling and decoupling operations.

With help and ok from mikeb@


Revision tags: OPENBSD_5_7_BASE
# 1.41 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.40 29-Oct-2014 deraadt

convert simple cases of select() to poll()
ok doug


# 1.39 18-Oct-2014 doug

Simple malloc() to reallocarray() conversion to potentially avoid integer
overflow.

ok deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.38 09-Jul-2014 markus

expire IPcomp SAs too; ok mikeb (some time ago)


# 1.37 09-May-2014 markus

get rid of redundant {csa,flow}_{src,dst}id pointers, so we don't need
to update it on rekey (fixes use-after-free); ok mikeb@


# 1.36 09-May-2014 markus

replace iked_transform pointer with xform id, since target of pointer
might be freed (e.g. on ike sa rekey); ok mikeb@


# 1.35 07-May-2014 markus

try postponed requests first, so we do in-order processing; ok mikeb@


# 1.34 06-May-2014 markus

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


# 1.33 05-May-2014 markus

pfkey is unreliable, so add a select-timeout before MSG_PEEK;
similar code is in isakmpd; ok reyk@


# 1.32 05-May-2014 markus

don't leak on pid mismatch; ok mikeb


# 1.31 05-May-2014 blambert

change surprisingly consistent mispelling of length ("lenght")

no change in md5 of resulting object file

ok markus@, reyk@


# 1.30 25-Apr-2014 jsg

don't access a pointer till after the null check
ok mikeb@


Revision tags: OPENBSD_5_5_BASE
# 1.29 21-Feb-2014 markus

support rekeying for IPCOMP; ok mikeb@


# 1.28 14-Feb-2014 markus

initial support for IPComp
still experimental and rekeying needs some work; ok mikeb@


# 1.27 22-Jan-2014 markus

implement DPD similar to isakmpd, but only send DPD-messages 'on-demand'
(less aggressive, only if the ESP-SAs are actually used);
feedback & ok mikeb@


# 1.26 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.25 14-Nov-2013 markus

ignore messages for other daemons, like isakmpd does; ok mikeb


# 1.24 14-Nov-2013 markus

setup pfkey timer before use; ok mikeb


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

remove excessive includes


Revision tags: OPENBSD_5_3_BASE
# 1.22 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.21 29-Nov-2012 reyk

Prevent VPN traffic leakages in dual-stack hosts/networks.
See http://tools.ietf.org/html/draft-gont-opsec-vpn-leakages.

We forcibly block IPv6 traffic by loading a "flow esp out from ::/0 to
::/0 type deny" unless the protocol is used in any of the flows. Note
that this will block any IPv6 traffic, superseding routes and pf, on
the host by default when iked is running with IPv4 flows only. This
auto-blocking feature can be disabled by specifying the "-6" command
line flag to iked.

Thanks to Fernando Gont.

ok mikeb@


# 1.20 23-Oct-2012 reyk

Change the order of variables just to shrink the diff to the (not yet
released) portable version a bit. No functional changes.


# 1.19 18-Sep-2012 reyk

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


Revision tags: OPENBSD_5_2_BASE
# 1.18 29-Jun-2012 mikeb

Add missing ESN bits


# 1.17 24-Mar-2012 jsg

fix some leaks
ok mikeb@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.16 27-May-2011 reyk

spacing


# 1.15 05-May-2011 reyk

rename iked_proc* to privsep_proc*. no functional change.


Revision tags: OPENBSD_4_9_BASE
# 1.14 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.13 21-Jan-2011 reyk

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

ok mikeb@


# 1.12 17-Jan-2011 mikeb

Add initial acquire mode support and use it whenever Windows peers decide
to drop Child SA based on the inactivity timer. In this case we instruct
the kernel to send us an acquire message upon receiving a packet for those
hosts and initiate a Child SA creation exchange ourselves.

ok reyk


# 1.11 12-Jan-2011 mikeb

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


# 1.10 22-Dec-2010 reyk

move and rename util.c:print_id() to ikev2.c:ikev2_print_id() because
it is too specific to be in util.c. This will allow to link util.c
into ikectl later without all the other dependencies of pritn_id().


# 1.9 22-Dec-2010 mikeb

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


# 1.8 23-Sep-2010 mikeb

support for aes-gcm

OK reyk


Revision tags: OPENBSD_4_8_BASE
# 1.7 01-Jul-2010 reyk

Add support for the tap extension (ikev2 ... tap "enc1") that will
tell the kernel to send all IPsec traffic for derived SAs to the
specified enc(4) interface instead of enc0.


# 1.6 26-Jun-2010 reyk

Include the Id type in the generated SA tag that is passed to the
kernel, just like isakmpd does it. In difference to isakmpd, the Id
type is printed in capital letters, eg. FQDN/foo.example.com, because
it is using the existing print_map() API. For consistency, rename a
few Id types in grammar and code from the RFC-names to the
OpenBSD-style names; including RFC822_ADDR to UFQDN, IPV4_ADDR to just
IPV4, DER_ASN1_DN to ASN1_DN etc.


# 1.5 14-Jun-2010 reyk

Initial support for initiator mode which allows to run iked as a
"client" or to configure iked to iked (OpenBSD to OpenBSD) IKEv2 VPNs.

It currently only supports psk (pre-shared keys) and no certificates,
doesn't do any rekeying or SA timeouts, and needs more cleanup. So it
is not quite production ready yet - but ready for simple tests...


# 1.4 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.3 10-Jun-2010 reyk

simplify the pfkey code by adding a pfkey_write() function


# 1.2 04-Jun-2010 reyk

Fix NAT-T detection to enable UDP encapsulation. It was done before,
but not in the right order to run the IKEv2 NAT detection and check the
source port of the last IKE message which should be the NAT-T port 4500.

Tested with iked running on sparc64 and a NAT'ed windows box.


# 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@