History log of /freebsd-head/sys/netipsec/key.c
Revision Date Author Comments
9b3292e515f4b43ee6a0054b1f1e97eb91d4bf5e 25-Jun-2020 jhb <jhb@FreeBSD.org> Simplify IPsec transform-specific teardown.

- Rename from the teardown callback from 'zeroize' to 'cleanup' since
this no longer zeroes keys.

- Change the callback return type to void. Nothing checked the return
value and it was always zero.

- Don't have esp call into ah since it no longer needs to depend on
this to clear the auth key. Instead, both are now private and
self-contained.

Reviewed by: delphij
Sponsored by: Chelsio Communications
Differential Revision: https://reviews.freebsd.org/D25443
3dca204b2421d9116c0c53028a7ef9e29133dd50 25-Jun-2020 jhb <jhb@FreeBSD.org> Use zfree() to explicitly zero IPsec keys.

Reviewed by: delphij
Sponsored by: Chelsio Communications
Differential Revision: https://reviews.freebsd.org/D25442
a643b1d159ec0627e0115839e8af2d80fb87c2d7 02-May-2020 jhb <jhb@FreeBSD.org> Remove support for IPsec algorithms deprecated in r348205 and r360202.

Examples of depecrated algorithms in manual pages and sample configs
are updated where relevant. I removed the one example of combining
ESP and AH (vs using a cipher and auth in ESP) as RFC 8221 says this
combination is NOT RECOMMENDED.

Specifically, this removes support for the following ciphers:
- des-cbc
- 3des-cbc
- blowfish-cbc
- cast128-cbc
- des-deriv
- des-32iv
- camellia-cbc

This also removes support for the following authentication algorithms:
- hmac-md5
- keyed-md5
- keyed-sha1
- hmac-ripemd160

Reviewed by: cem, gnn (older verisons)
Relnotes: yes
Sponsored by: Chelsio Communications
Differential Revision: https://reviews.freebsd.org/D24342
ad355b0a9dbd6a8aabe7c081a731d24904a0f2c1 26-Feb-2020 kaktus <kaktus@FreeBSD.org> Mark more nodes as CTLFLAG_MPSAFE or CTLFLAG_NEEDGIANT (17 of many)

r357614 added CTLFLAG_NEEDGIANT to make it easier to find nodes that are
still not MPSAFE (or already are but aren’t properly marked).
Use it in preparation for a general review of all nodes.

This is non-functional change that adds annotations to SYSCTL_NODE and
SYSCTL_PROC nodes using one of the soon-to-be-required flags.

Mark all obvious cases as MPSAFE. All entries that haven't been marked
as MPSAFE before are by default marked as NEEDGIANT

Approved by: kib (mentor, blanket)
Commented by: kib, gallatin, melifaro
Differential Revision: https://reviews.freebsd.org/D23718
22bac2640db2bce2651a492b1d5e89cf85eb3e95 12-Feb-2020 mjg <mjg@FreeBSD.org> netipsec: fix a mismatched uma_zfree -> uma_zfree_pcpu

PR: 244077
Reported by: lwhsu
Fixes: r357805 ("amd64: store per-cpu allocations subtracted by __pcpu")
d3d0bd153cf3e76effd2e9e8c66a847d1c5defe3 09-Aug-2019 ae <ae@FreeBSD.org> Add missing new line in several log messages.

PR: 239694
MFC after: 1 week
1658622e7f48b6ac086e13a53c6d274608566af9 25-Jun-2019 rlibby <rlibby@FreeBSD.org> netipsec key_register: check for M_NOWAIT alloc failure

Reviewed by: ae, cem
Sponsored by: Dell EMC Isilon
Differential Revision: https://reviews.freebsd.org/D20742
33133b3b41a3dfd6204155c22b7d0d14039152ed 04-Apr-2019 cem <cem@FreeBSD.org> Replace read_random(9) with more appropriate arc4rand(9) KPIs

Reviewed by: ae, delphij
Sponsored by: Dell EMC Isilon
Differential Revision: https://reviews.freebsd.org/D19760
0a6e5e58576ae8a1d3f4818aa10d98a2524193a6 21-Oct-2018 ae <ae@FreeBSD.org> Add sadb_x_sa2 extension to SADB_ACQUIRE requests.

SADB_ACQUIRE requests are send by kernel, when security policy doesn't
have corresponding security association for outbound packet. IKE daemon
usually registers its handler for such messages and when the kernel asks
for SA it can handle this request. Now such requests will contain
additional fields that can help IKE daemon to create SA. And IKE now
can create SAs using only information from SADB_ACQUIRE request, this
is useful when many if_ipsec(4) interfaces are in use and IKE doesn track
security policies that was installed by kernel.

Obtained from: Yandex LLC
MFC after: 3 weeks
Sponsored by: Yandex LLC
6c193bda23c9338cb29ac48cd7a42450ac0f2de8 26-Sep-2018 ae <ae@FreeBSD.org> Fix witness warning in xform_init().

Do not call crypto_newsession() while holding xforms_lock mutex.
Release mutex before invoking crypto_newsession(), and use
ipsec_kmod_enter()/ipsec_kmod_exit() functions to protect from doing
access to unloaded kernel module memory.

Move xform-releated functions into subr_ipsec.c to be able use
ipsec_kmod_* functions. Also unconditionally build ipsec_kmod_*
functions, since now they are always used by IPSec code.

Add xf_cntr field to struct xformsw, it is used by ipsec_kmod_*
functions. Also constify xf_name field, since it is not expected to be
modified.

Approved by: re (kib)
Differential Revision: https://reviews.freebsd.org/D17302
a6605d2938cce3ab84122eb8f7e12469b7d3356f 24-Jul-2018 andrew <andrew@FreeBSD.org> Use the new VNET_DEFINE_STATIC macro when we are defining static VNET
variables.

Reviewed by: bz
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D16147
844f744aa6ed915974da33596bb2488e821b8a4c 08-Jun-2018 mjg <mjg@FreeBSD.org> uma: implement provisional api for per-cpu zones

Per-cpu zone allocations are very rarely done compared to regular zones.
The intent is to avoid pessimizing the latter case with per-cpu specific
code.

In particular contrary to the claim in r334824, M_ZERO is sometimes being
used for such zones. But the zeroing method is completely different and
braching on it in the fast path for regular zones is a waste of time.
41a142c9f3df6f523c3e36691a82ccd8579b6e03 28-May-2018 ae <ae@FreeBSD.org> Temporary disable SPDCACHE statistic accounting until proper fix will be
committed. This fixes the kernel build without option IPSEC.
05db59540b12974858716ac7f1831dd2907ab7e4 24-May-2018 mmacy <mmacy@FreeBSD.org> netipsec/!VIMAGE: don't declare/define spdcache_destroy on non-VIMAGE builds
this breaks MIPS compiles in universe
97541ca8049a6442cf7600942e87538d35735dd5 22-May-2018 fabient <fabient@FreeBSD.org> Add a SPD cache to speed up lookups.

When large SPDs are used, we face two problems:

- too many CPU cycles are spent during the linear searches in the SPD
for each packet
- too much contention on multi socket systems, since we use a single
shared lock.

Main changes:

- added the sysctl tree 'net.key.spdcache' to control the SPD cache
(disabled by default).
- cache the sp indexes that are used to perform SP lookups.
- use a range of dedicated mutexes to protect the cache lines.

Submitted by: Emeric Poupon <emeric.poupon@stormshield.eu>
Reviewed by: ae
Sponsored by: Stormshield
Differential Revision: https://reviews.freebsd.org/D15050
c8da6fae2c8073f216b7a35739f7dfa140c9a8d9 25-Dec-2017 kan <kan@FreeBSD.org> Do pass removing some write-only variables from the kernel.

This reduces noise when kernel is compiled by newer GCC versions,
such as one used by external toolchain ports.

Reviewed by: kib, andrew(sys/arm and sys/arm64), emaste(partial), erj(partial)
Reviewed by: jhb (sys/dev/pci/* sys/kern/vfs_aio.c and sys/kern/kern_synch.c)
Differential Revision: https://reviews.freebsd.org/D10385
43571ceb2f8c0e7ab5cf8b00ea2efdfaea7773bb 01-Dec-2017 ae <ae@FreeBSD.org> Do better cleaning in key_destroy() for VIMAGE case.

SPDB was cleaned using TAILQ_CONCAT() instead of calling key_unlink()
for each SP, thus we need to properly clean lists in each bucket of
V_sphashtbl to avoid panic in hashdestroy() when INVARIANTS is enabled.

Do the same for V_acqaddrhashtbl and V_acqseqhashtbl.

When we are called in DEFAULT_VNET, destroy also all global locks and
drain key_timer callout.

Reported by: kp
Tested by: kp
MFC after: 1 week
4736ccfd9c3411d50371d7f21f9450a47c19047e 20-Nov-2017 pfg <pfg@FreeBSD.org> sys: further adoption of SPDX licensing ID tags.

Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
bca53fe4f96e5378f583893e8825f607b4225911 15-Nov-2017 cem <cem@FreeBSD.org> ipsec: Use the same keysize values for HMAC as prior to r324017

The HMAC construction natively permits any key size between 0 and the input
block length. Before r324017, the auth_hash 'keysize' member was the hash
output length, which was used by ipsec for key sizes. (Non-ipsec consumers
need the ability to use other keysizes, hence, r324017.)

The ipsec SADB code blindly uses the auth_hash 'keysize' member for both
minimum and maximum key size, which is wrong (from an HMAC perspective).
For now, just switch it to 'hashsize', which matches the existing
expectations.

Instead it should probably use the range [0, keysize]. But there may be
other broken code in ipsec that rejects hashes with too small a minimum
key size.

Reported by: olivier@
Reviewed by: olivier, no objection from ae
Sponsored by: Dell EMC Isilon
Differential Revision: https://reviews.freebsd.org/D12770
437b210e7429a2ece8366341a837d5f2add7a13d 03-Nov-2017 ae <ae@FreeBSD.org> Use correct pointer in key_updateaddresses() when updating NAT-T config.

key_updateaddresses() is used to update SA addresses and NAT-T
configuration in SADB_UPDATE message. This is done using cloning SA
content from old SA into new one. But addresses and NAT-T configuration
are taking from SADB_UPDATE message. Use newsa pointer to set NAT-T
properties into cloned SA.

PR: 223382
MFC after: 1 week
f7286cb332acc216825c2a6f59adb7dd2d7dac59 09-Aug-2017 ae <ae@FreeBSD.org> Make user supplied data checks a bit stricter.

key_msg2sp() is used for parsing data from setsockopt(IP[V6]_IPSEC_POLICY)
call. This socket option is usually used to configure IPsec bypass for
socket. Only privileged user can set this socket option.
The message syntax is described here
http://www.kame.net/newsletter/20021210/

and our libipsec is usually used to create the correct request.
Add additional checks:
* that sadb_x_ipsecrequest_len is not out of bounds of user supplied buffer
* that src/dst's sa_len is the same
* that 2*sa_len is not out of bounds of user supplied buffer
* that 2*sa_len fits into bounds of sadb_x_ipsecrequest

Reported by: Ilja van Sprundel
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D11796
9dcb2b08f1b770b30ea760da24f890b0ff419423 04-Apr-2017 ae <ae@FreeBSD.org> When we are doing SA lookup for TCP-MD5, check both source and
destination addresses. Previous code has used only destination address
for lookup. But for inbound packets the source address was used as SA
destination address. Thus only outbound SA were used for both directions.
Now we use addresses from a packet as is, thus SAs for both directions are
needed.

Reported by: Mike Tancsa
MFC after: 1 week
c95632b3a32ba357eff34e7295c3faca6d5a0718 29-Mar-2017 ae <ae@FreeBSD.org> Fix bug in r308972 that leads to panic when non-compressed IPComp
packet is received.

Reported by: Denis Ahrens <denis h3q com>
MFC after: 3 days
749be37c286cfa2cc68b1dad3483c31a472528d5 07-Mar-2017 ae <ae@FreeBSD.org> Introduce the concept of IPsec security policies scope.

Currently are defined three scopes: global, ifnet, and pcb.
Generic security policies that IKE daemon can add via PF_KEY interface
or an administrator creates with setkey(8) utility have GLOBAL scope.
Such policies can be applied by the kernel to outgoing packets and checked
agains inbound packets after IPsec processing.
Security policies created by if_ipsec(4) interfaces have IFNET scope.
Such policies are applied to packets that are passed through if_ipsec(4)
interface.
And security policies created by application using setsockopt()
IP_IPSEC_POLICY option have PCB scope. Such policies are applied to
packets related to specific socket. Currently there is no way to list
PCB policies via setkey(8) utility.

Modify setkey(8) and libipsec(3) to be able distinguish the scope of
security policies in the `setkey -DP` listing. Add two optional flags:
'-t' to list only policies related to virtual *tunneling* interfaces,
i.e. policies with IFNET scope, and '-g' to list only policies with GLOBAL
scope. By default policies from all scopes are listed.

To implement this PF_KEY's sadb_x_policy structure was modified.
sadb_x_policy_reserved field is used to pass the policy scope from the
kernel to userland. SADB_SPDDUMP message extended to support filtering
by scope: sadb_msg_satype field is used to specify bit mask of requested
scopes.

For IFNET policies the sadb_x_policy_priority field of struct sadb_x_policy
is used to pass if_ipsec's interface if_index to the userland. For GLOBAL
policies sadb_x_policy_priority is used only to manage order of security
policies in the SPDB. For IFNET policies it is not used, so it can be used
to keep if_index.

After this change the output of `setkey -DP` now looks like:
# setkey -DPt
0.0.0.0/0[any] 0.0.0.0/0[any] any
in ipsec
esp/tunnel/87.250.242.144-87.250.242.145/unique:145
spid=7 seq=3 pid=58025 scope=ifnet ifname=ipsec0
refcnt=1
# setkey -DPg
::/0 ::/0 icmp6 135,0
out none
spid=5 seq=1 pid=872 scope=global
refcnt=1

No objection from: #network
Obtained from: Yandex LLC
MFC after: 2 weeks
Sponsored by: Yandex LLC
Differential Revision: https://reviews.freebsd.org/D9805
0fb6ad528e5c3b098a7410f8f804d39fcd21ee10 06-Feb-2017 ae <ae@FreeBSD.org> Merge projects/ipsec into head/.

Small summary
-------------

o Almost all IPsec releated code was moved into sys/netipsec.
o New kernel modules added: ipsec.ko and tcpmd5.ko. New kernel
option IPSEC_SUPPORT added. It enables support for loading
and unloading of ipsec.ko and tcpmd5.ko kernel modules.
o IPSEC_NAT_T option was removed. Now NAT-T support is enabled by
default. The UDP_ENCAP_ESPINUDP_NON_IKE encapsulation type
support was removed. Added TCP/UDP checksum handling for
inbound packets that were decapsulated by transport mode SAs.
setkey(8) modified to show run-time NAT-T configuration of SA.
o New network pseudo interface if_ipsec(4) added. For now it is
build as part of ipsec.ko module (or with IPSEC kernel).
It implements IPsec virtual tunnels to create route-based VPNs.
o The network stack now invokes IPsec functions using special
methods. The only one header file <netipsec/ipsec_support.h>
should be included to declare all the needed things to work
with IPsec.
o All IPsec protocols handlers (ESP/AH/IPCOMP protosw) were removed.
Now these protocols are handled directly via IPsec methods.
o TCP_SIGNATURE support was reworked to be more close to RFC.
o PF_KEY SADB was reworked:
- now all security associations stored in the single SPI namespace,
and all SAs MUST have unique SPI.
- several hash tables added to speed up lookups in SADB.
- SADB now uses rmlock to protect access, and concurrent threads
can do SA lookups in the same time.
- many PF_KEY message handlers were reworked to reflect changes
in SADB.
- SADB_UPDATE message was extended to support new PF_KEY headers:
SADB_X_EXT_NEW_ADDRESS_SRC and SADB_X_EXT_NEW_ADDRESS_DST. They
can be used by IKE daemon to change SA addresses.
o ipsecrequest and secpolicy structures were cardinally changed to
avoid locking protection for ipsecrequest. Now we support
only limited number (4) of bundled SAs, but they are supported
for both INET and INET6.
o INPCB security policy cache was introduced. Each PCB now caches
used security policies to avoid SP lookup for each packet.
o For inbound security policies added the mode, when the kernel does
check for full history of applied IPsec transforms.
o References counting rules for security policies and security
associations were changed. The proper SA locking added into xform
code.
o xform code was also changed. Now it is possible to unregister xforms.
tdb_xxx structures were changed and renamed to reflect changes in
SADB/SPDB, and changed rules for locking and refcounting.

Reviewed by: gnn, wblock
Obtained from: Yandex LLC
Relnotes: yes
Sponsored by: Yandex LLC
Differential Revision: https://reviews.freebsd.org/D9352
2a3ca2933d0efd0f486eebb1a575528440873633 25-Nov-2016 fabient <fabient@FreeBSD.org> IPsec RFC6479 support for replay window sizes up to 2^32 - 32 packets.

Since the previous algorithm, based on bit shifting, does not scale
with large replay windows, the algorithm used here is based on
RFC 6479: IPsec Anti-Replay Algorithm without Bit Shifting.
The replay window will be fast to be updated, but will cost as many bits
in RAM as its size.

The previous implementation did not provide a lock on the replay window,
which may lead to replay issues.

Reviewed by: ae
Obtained from: emeric.poupon@stormshield.eu
Sponsored by: Stormshield
Differential Revision: https://reviews.freebsd.org/D8468
44074e3ef1e88b7ee813efa2b4657ef5e3e16817 10-Jun-2016 ngie <ngie@FreeBSD.org> MFC r299387:
r299387 (by cem):

netipsec: Fix minor style nit

Coverity points out that 'continue' is equivalent to 'break' in a do {}
while(false) loop.

CID: 1354983
00d578928eca75be320b36d37543a7e2a4f9fbdb 27-May-2016 grehan <grehan@FreeBSD.org> Create branch for bhyve graphics import.
1a6ffca1188015ec2f63a8178d23959a20e4926f 10-May-2016 cem <cem@FreeBSD.org> netipsec: Fix minor style nit

Coverity points out that 'continue' is equivalent to 'break' in a do {}
while(false) loop.

Reported by: Coverity
CID: 1354983
Sponsored by: EMC / Isilon Storage Division
d9c9113377a2f19d01848ae8dcc470e9306ce932 03-May-2016 pfg <pfg@FreeBSD.org> sys/net*: minor spelling fixes.

No functional change.
dcf50398ab66cfcba0ae4484efe3b5ce40fb9824 24-Apr-2016 ae <ae@FreeBSD.org> Handle non-compressed packets for IPComp in tunnel mode.

RFC3173 says that the IP datagram MUST be sent in the original
non-compressed form, when the total size of a compressed payload
and the IPComp header is not smaller than the size of the original
payload. In tunnel mode for small packets IPComp will send
encapsulated IP datagrams without IPComp header.
Add ip_encap handler for IPPROTO_IPV4 and IPPROTO_IPV6 to handle
these datagrams. The handler does lookup for SA related to IPComp
protocol and given from mbuf source and destination addresses as
tunnel endpoints. It decapsulates packets only when corresponding SA
is found.

Reported by: gnn
Reviewed by: gnn
Differential Revision: https://reviews.freebsd.org/D6062
a7d40a88c91d105dcfe2f235bc84a522bfea3de2 19-Apr-2016 pfg <pfg@FreeBSD.org> kernel: use our nitems() macro when it is available through param.h.

No functional change, only trivial cases are done in this sweep,

Discussed in: freebsd-current
12232f84636cebecfa250541cfbf09b07fe2f520 15-Apr-2016 pfg <pfg@FreeBSD.org> sys/net* : for pointers replace 0 with NULL.

Mostly cosmetical, no functional change.

Found with devel/coccinelle.
4f8c2039d8be63a0418cf26f53ba68c401a87cda 13-Mar-2016 rwatson <rwatson@FreeBSD.org> Put IPSec's anouncement of its successful intialisation under bootverbose:
now that it's a default kernel option, we don't really need to tell the
world about it on every boot, especially as it won't be used by most users.
1febe34ecc1d0cb1a72a6dda1f699f469d7b2c69 09-Mar-2016 ae <ae@FreeBSD.org> MFC r295967:
Fix useless check. m_pkthdr.len should be equal to orglen.
33b572c3379931cdf4c3cc9b7dfadbb894c3024b 02-Mar-2016 markj <markj@FreeBSD.org> Set tres to NULL to avoid a double free if the m_pullup() below fails.

Reviewed by: glebius
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D5497
96081091e737f454bfb88ec5b1fa6e285a535027 24-Feb-2016 ae <ae@FreeBSD.org> Fix useless check. m_pkthdr.len should be equal to orglen.

MFC after: 2 weeks
5bb5d55de914d628b9b9284b85c9043cf9785abe 17-Nov-2015 fabient <fabient@FreeBSD.org> Implement the sadb_x_policy_priority field as it is done in Linux:
lower priority policies are inserted first.

Submitted by: Emeric Poupon <emeric.poupon@stormshield.eu>
Reviewed by: ae
Sponsored by: Stormshield
4a54e95cd79f25bad1d55f8ddbf1aecdd098869a 16-Nov-2015 ae <ae@FreeBSD.org> Use explicitly specified ivsize instead of blocksize when we mean IV size.
Set zero ivsize for enc_xform_null and remove special handling from
xform_esp.c.

Reviewed by: gnn
Differential Revision: https://reviews.freebsd.org/D1503
20a77876c747677e14860ccb77e37ba572e7b1f2 04-Aug-2015 jmg <jmg@FreeBSD.org> Make IPsec work with AES-GCM and AES-ICM (aka CTR) in OCF... IPsec
defines the keys differently than NIST does, so we have to muck with
key lengths and nonce/IVs to be standard compliant...

Remove the iv from secasvar as it was unused...

Add a counter protected by a mutex to ensure that the counter for GCM
and ICM will never be repeated.. This is a requirement for security..
I would use atomics, but we don't have a 64bit one on all platforms..

Fix a bug where IPsec was depending upon the OCF to ensure that the
blocksize was always at least 4 bytes to maintain alignment... Move
this logic into IPsec so changes to OCF won't break IPsec...

In one place, espx was always non-NULL, so don't test that it's
non-NULL before doing work..

minor style cleanups...

drop setting key and klen as they were not used...

Enforce that OCF won't pass invalid key lengths to AES that would
panic the machine...

This was has been tested by others too... I tested this against
NetBSD 6.1.5 using mini-test suite in
https://github.com/jmgurney/ipseccfgs and the only things that don't
pass are keyed md5 and sha1, and 3des-deriv (setkey syntax error),
all other modes listed in setkey's man page... The nice thing is
that NetBSD uses setkey, so same config files were used on both...

Reviewed by: gnn
1392531c0fe9b8815ac27613f4a9a26af79223d0 15-Jul-2015 ae <ae@FreeBSD.org> MFC r285204:
Fill the port and protocol information in the SADB_ACQUIRE message
in case when security policy has it as required by RFC 2367.

PR: 192774

Approved by: re (delphij)
f3d95907d47f500513877f43dbffeb5e34a91da7 06-Jul-2015 ae <ae@FreeBSD.org> Fill the port and protocol information in the SADB_ACQUIRE message
in case when security policy has it as required by RFC 2367.

PR: 192774
Differential Revision: https://reviews.freebsd.org/D2972
MFC after: 1 week
628de4933c8bc383f3892c2a4f874827570b2a01 11-Jun-2015 jmg <jmg@FreeBSD.org> drop key_sa_stir_iv as it isn't used...

Reviewed by: eri, ae
78931b5bd8d2c14e89ec9ef7932e7b4f10e3baf4 03-Jun-2015 ae <ae@FreeBSD.org> MFC r283146:
In the reply to SADB_X_SPDGET message use the same sequence number that
was in the request. Some IKE deamons expect it will the same. Linux and
NetBSD also follow this behaviour.

PR: 137309
3c9fd219636d39ff34d957bf7cb13945826529b8 02-Jun-2015 ae <ae@FreeBSD.org> MFC r283101:
Teach key_expire() send SADB_EXPIRE message with the SADB_EXT_LIFETIME_HARD
extension header type. The key_flush_sad() now will send SADB_EXPIRE
message when HARD lifetime expires. This is required by RFC 2367 and some
keying daemons rely on these messages. HARD lifetime messages have
precedence over SOFT lifetime messages, so now they will be checked first.
Also now SADB_EXPIRE messages will be send even the SA has not been used,
because keying daemons might want to rekey such SA.

PR: 200282, 200283

MFC r283102:
Change SA's state before sending SADB_EXPIRE message. This state will
be reported to keying daemon.
bf9acc9587cc1734e0b30b1318463ab75428ba52 02-Jun-2015 ae <ae@FreeBSD.org> MFC r275390:
Remove unused declartations.

MFC r275437:
ANSIfy function declarations.

MFC r275438:
Remove __P() macro.

Sponsored by: Yandex LLC
fcbaea954867d54540644ca91c5c45fa835060d6 02-Jun-2015 ae <ae@FreeBSD.org> MFC r275392:
Remove route chaching support from ipsec code. It isn't used for some time.
* remove sa_route_union declaration and route_cache member from struct secashead;
* remove key_sa_routechange() call from ICMP and ICMPv6 code;
* simplify ip_ipsec_mtu();
* remove #include <net/route.h>;

Sponsored by: Yandex LLC
318c4f97e694c1972b55450cafe914f39977d179 22-May-2015 jkim <jkim@FreeBSD.org> CALLOUT_MPSAFE has lost its meaning since r141428, i.e., for more than ten
years for head. However, it is continuously misused as the mpsafe argument
for callout_init(9). Deprecate the flag and clean up callout_init() calls
to make them more consistent.

Differential Revision: https://reviews.freebsd.org/D2613
Reviewed by: jhb
MFC after: 2 weeks
22e6920de8ec2fded221d35dcbc4c29fc30ca6e5 20-May-2015 ae <ae@FreeBSD.org> In the reply to SADB_X_SPDGET message use the same sequence number that
was in the request. Some IKE deamons expect it will the same. Linux and
NetBSD also follow this behaviour.

PR: 137309
MFC after: 2 weeks
c2b339d22562ef3e50640cebc0fcf30e20f0b27c 19-May-2015 ae <ae@FreeBSD.org> Change SA's state before sending SADB_EXPIRE message. This state will
be reported to keying daemon.

MFC after: 2 weeks
a7c61420eddd68df464b2459cd3297b704a86d5c 19-May-2015 ae <ae@FreeBSD.org> Teach key_expire() send SADB_EXPIRE message with the SADB_EXT_LIFETIME_HARD
extension header type. The key_flush_sad() now will send SADB_EXPIRE
message when HARD lifetime expires. This is required by RFC 2367 and some
keying daemons rely on these messages. HARD lifetime messages have
precedence over SOFT lifetime messages, so now they will be checked first.
Also now SADB_EXPIRE messages will be send even the SA has not been used,
because keying daemons might want to rekey such SA.

PR: 200282, 200283
Submitted by: Tobias Brunner <tobias at strongswan dot org>
MFC after: 2 weeks
0e635affcb1c17159cb21d85b42dbd79cc5b2faf 18-Apr-2015 ae <ae@FreeBSD.org> Fix handling of scoped IPv6 addresses in IPSec code.

* in ipsec_encap() embed scope zone ids into link-local addresses
in the new IPv6 header, this helps ip6_output() disambiguate the
scope;
* teach key_ismyaddr6() use in6_localip(). in6_localip() is less
strict than key_sockaddrcmp(). It doesn't compare all fileds of
struct sockaddr_in6, but it is faster and it should be safe,
because all SA's data was checked for correctness. Also, since
IPv6 link-local addresses in the &V_in6_ifaddrhead are stored in
kernel-internal form, we need to embed scope zone id from SA into
the address before calling in6_localip.
* in ipsec_common_input() take scope zone id embedded in the address
and use it to initialize sin6_scope_id, then use this sockaddr
structure to lookup SA, because we keep addresses in the SADB without
embedded scope zone id.

Differential Revision: https://reviews.freebsd.org/D2304
Reviewed by: gnn
Sponsored by: Yandex LLC
96d5981c369fdf42633dace4e955f91d0f7b0a66 14-Mar-2015 ae <ae@FreeBSD.org> MFC r279735:
Remove extra '&'. sin6 is already a pointer.

PR: 195011
47f3ef1d0286b34745e2ee2f352631109f91f3e5 07-Mar-2015 ae <ae@FreeBSD.org> Remove extra '&'. sin6 is already a pointer.

PR: 195011
MFC after: 1 week
217cb9ff265db473319230edb49f91a57d80923e 24-Feb-2015 ae <ae@FreeBSD.org> Fix possible memory leak and several races in the IPsec policy management
code.

Resurrect the state field in the struct secpolicy, it has
IPSEC_SPSTATE_ALIVE value when security policy linked in the chain,
and IPSEC_SPSTATE_DEAD value in all other cases. This field protects
from trying to unlink one security policy several times from the different
threads.

Take additional reference in the key_flush_spd() to be sure that policy
won't be freed from the different thread while we are sending SPDEXPIRE message.

Add KEY_FREESP() call to the key_unlink() to release additional reference
that we take when use key_getsp*() functions.

Differential Revision: https://reviews.freebsd.org/D1914
Tested by: Emeric POUPON <emeric.poupon at stormshield dot eu>
Reviewed by: hrs
Sponsored by: Yandex LLC
93d1b629ef6cec47baba33817978d997e284d16f 27-Jan-2015 dim <dim@FreeBSD.org> Merging ^/head r277777 through r277803.
a77bf6c232e17ab45b0fee221b4f9dd89d421f9e 27-Jan-2015 ae <ae@FreeBSD.org> key_spdget uses key_setdumpsp() without SPTREE_RLOCK held (it uses
referenced pointer to sp). Remove SPTREE_RLOCK_ASSERT from
key_setdumpsp() to fix wrong assertion.

Reported by: Emeric POUPON
Obtained from: Yandex LLC
Sponsored by: Yandex LLC
60909669f00c3675bab15d82d9d63aaff61dfafa 06-Jan-2015 rwatson <rwatson@FreeBSD.org> In order to reduce use of M_EXT outside of the mbuf allocator and
socket-buffer implementations, introduce a return value for MCLGET()
(and m_cljget() that underlies it) to allow the caller to avoid testing
M_EXT itself. Update all callers to use the return value.

With this change, very few network device drivers remain aware of
M_EXT; the primary exceptions lie in mbuf-chain pretty printers for
debugging, and in a few cases, custom mbuf and cluster allocation
implementations.

NB: This is a difficult-to-test change as it touches many drivers for
which I don't have physical devices. Instead we've gone for intensive
review, but further post-commit review would definitely be appreciated
to spot errors where changes could not easily be made mechanically,
but were largely mechanical in nature.

Differential Revision: https://reviews.freebsd.org/D1440
Reviewed by: adrian, bz, gnn
Sponsored by: EMC / Isilon Storage Division
7d079a2d8e912ab85eeaa8a1283b7201aae51a59 25-Dec-2014 ae <ae@FreeBSD.org> Fix VIMAGE build.
84b82e8873b6d396b258781a7d053358c25c50c7 24-Dec-2014 ae <ae@FreeBSD.org> Rename ip4_def_policy variable to def_policy. It is used by both IPv4 and
IPv6. Initialize it only once in def_policy_init(). Remove its
initialization from key_init() and make it static.

Remove several fields from struct secpolicy:
* lock - it isn't so useful having mutex in the structure, but the only
thing we do with it is initialization and destroying.
* state - it has only two values - DEAD and ALIVE. Instead of take a lock
and change the state to DEAD, then take lock again in GC function and
delete policy from the chain - keep in the chain only ALIVE policies.
* scangen - it was used in GC function to protect from sending several
SADB_SPDEXPIRE messages for one SPD entry. Now we don't keep DEAD entries
in the chain and there is no need to have scangen variable.

Use TAILQ to implement SPD entries chain. Use rmlock to protect access
to SPD entries chain. Protect all SP lookup with RLOCK, and use WLOCK
when we are inserting (or removing) SP entry in the chain.

Instead of using pattern "LOCK(); refcnt++; UNLOCK();", use refcount(9)
API to implement refcounting in SPD. Merge code from key_delsp() and
_key_delsp() into _key_freesp(). And use KEY_FREESP() macro in all cases
when we want to release reference or just delete SP entry.

Obtained from: Yandex LLC
Sponsored by: Yandex LLC
6fc2bb76afe442ed3756e1a4ccf889430c6b0134 16-Dec-2014 ae <ae@FreeBSD.org> MFC r275575:
key_getspacq() returns holding the spacq_lock. Unlock it in all cases.

Sponsored by: Yandex LLC
9b3ccf0ab3f462a1a474a55cf53c880f31c4193d 07-Dec-2014 ae <ae@FreeBSD.org> key_getspacq() returns holding the spacq_lock. Unlock it in all cases.

MFC after: 1 week
Sponsored by: Yandex LLC
90acc683520c6db1d5028b27d87acd7a8dee43ea 03-Dec-2014 ae <ae@FreeBSD.org> Remove __P() macro.

Suggested by: kevlo
Sponsored by: Yandex LLC
ef3a17b83cc98b496c2556252713e540b3b3c5a9 03-Dec-2014 ae <ae@FreeBSD.org> ANSIfy function declarations.

Sponsored by: Yandex LLC
b82eb2f5d9fa4d0676e71863fa3931a1b8ced3dc 02-Dec-2014 ae <ae@FreeBSD.org> Remove route chaching support from ipsec code. It isn't used for some time.
* remove sa_route_union declaration and route_cache member from struct secashead;
* remove key_sa_routechange() call from ICMP and ICMPv6 code;
* simplify ip_ipsec_mtu();
* remove #include <net/route.h>;

Sponsored by: Yandex LLC
189aa5130fa47074600a59f6406e31af5aac4b3a 13-Nov-2014 ae <ae@FreeBSD.org> MFC r273904:
Use in_localip() instead of handmade implementation.

Sponsored by: Yandex LLC
99f4ec50e80b1d866aac04ceef8235524e0c274f 07-Nov-2014 glebius <glebius@FreeBSD.org> Remove SYSCTL_VNET_* macros, and simply put CTLFLAG_VNET where needed.

Sponsored by: Nginx, Inc.
2495e4b948471df01338932bff6123b06aee4063 31-Oct-2014 ae <ae@FreeBSD.org> Use in_localip() instead of handmade implementation.

MFC after: 1 week
Sponsored by: Yandex LLC
93ccee215cd3e965f2ce15f5a20fd10b99f40653 23-Oct-2014 jhb <jhb@FreeBSD.org> Use a static callout to drive key_timehandler() instead of timeout().
While here, make key_timehandler() private to key.c.

Submitted by: bz (2)
Tested by: bz
5876bf013badaaddcea6d1c75b5b24fa230315ca 16-Aug-2014 bz <bz@FreeBSD.org> MFC r266606:

Only do a ports check if this is a NAT-T SA. Otherwise other
lookups providing ports may get unexpected results.
7d2507a09db7441bf0ec5a2b33729f964a99d074 24-May-2014 bz <bz@FreeBSD.org> Only do a ports check if this is a NAT-T SA. Otherwise other
lookups providing ports may get unexpected results.

MFC After: 2 weeks
eb1a5f8de9f7ea602c373a710f531abbf81141c4 21-Feb-2014 gjb <gjb@FreeBSD.org> Move ^/user/gjb/hacking/release-embedded up one directory, and remove
^/user/gjb/hacking since this is likely to be merged to head/ soon.

Sponsored by: The FreeBSD Foundation
6b01bbf146ab195243a8e7d43bb11f8835c76af8 27-Dec-2013 gjb <gjb@FreeBSD.org> Copy head@r259933 -> user/gjb/hacking/release-embedded for initial
inclusion of (at least) arm builds with the release.

Sponsored by: The FreeBSD Foundation
ff6e113f1b21a30485c28c63a1779c32b67b5840 26-Oct-2013 glebius <glebius@FreeBSD.org> The r48589 promised to remove implicit inclusion of if_var.h soon. Prepare
to this event, adding if_var.h to files that do need it. Also, include
all includes that now are included due to implicit pollution via if_var.h

Sponsored by: Netflix
Sponsored by: Nginx, Inc.
b05df49af65d3ab697c586f71af5d5fe6f368656 20-Jun-2013 ae <ae@FreeBSD.org> Use corresponding macros to update statistics for AH, ESP, IPIP, IPCOMP,
PFKEY.

MFC after: 2 weeks
f1574e6b22a8e4c942bcaa09c0a3559a9a7e82ee 15-Mar-2013 glebius <glebius@FreeBSD.org> Use m_get2() + m_align() instead of hand made key_alloc_mbuf(). Code
examination shows, that although key_alloc_mbuf() could return chains,
the callers never use chains, so m_get2() should suffice.

Sponsored by: Nginx, Inc.
8e20fa5ae93243e19700ca06c01524b90fe3b784 05-Dec-2012 glebius <glebius@FreeBSD.org> Mechanically substitute flags from historic mbuf allocator with
malloc(9) flags within sys.

Exceptions:

- sys/contrib not touched
- sys/mbuf.h edited manually
34a9a386cb4df8844bca8e43dae20e4a15710fcc 18-Oct-2012 andre <andre@FreeBSD.org> Mechanically remove the last stray remains of spl* calls from net*/*.
They have been Noop's for a long time now.
05d54cfa61ef04e334ab3137b05cb4021e4f8515 12-Sep-2012 vanhu <vanhu@FreeBSD.org> In NAT-T transport mode, allow a client to open a new connection just after
closing another.
It worked only in tunnel mode before.

Submitted by: Andreas Longwitz <longwitz@incore.de>
MFC after: 1M
8f6ba08bcc1f3f26ee8f250d869760a21ca81f56 17-Aug-2012 jhb <jhb@FreeBSD.org> Unexpand a couple of TAILQ_FOREACH()s.
890596362264259cad90c931168bb69cad04ce59 07-Oct-2011 brueffer <brueffer@FreeBSD.org> Add missing va_end() in an error case to clean up after va_start()
(already done in the non-error case).

CID: 4726
Found with: Coverity Prevent(tm)
MFC after: 1 week
684e2951a0b583b62312d4849a6eb4b63cf14a78 09-May-2011 vanhu <vanhu@FreeBSD.org> Release SP's refcount in key_get_spdbyid().

PR: 156676
Submitted by: Tobias Brunner (tobias@strongswan.org)
MFC after: 1 week
d28e675043d30fd2673b02842a810b8aec8b1696 27-Apr-2011 bz <bz@FreeBSD.org> Make IPsec compile without INET adding appropriate #ifdef checks.

Unfold the IPSEC_COMMON_INPUT_CB() macro in xform_{ah,esp,ipcomp}.c
to not need three different versions depending on INET, INET6 or both.

Mark two places preparing for not yet supported functionality with IPv6.

Reviewed by: gnn
Sponsored by: The FreeBSD Foundation
Sponsored by: iXsystems
MFC after: 4 days
d56170701edd485dd31c6c68254daab567f5d249 31-Mar-2011 fabient <fabient@FreeBSD.org> Optimisation in IPSEC(4):
- Remove contention on ISR during the crypto operation by using rwlock(9).
- Remove a second lookup of the SA in the callback.

Gain on 6 cores CPU with SHA1/AES128 can be up to 30%.

Reviewed by: vanhu
MFC after: 1 month
b5386e15c14dd35dcd82a748b00a7a741b1238f9 18-Feb-2011 vanhu <vanhu@FreeBSD.org> Fixed IPsec's HMAC_SHA256-512 support to be RFC4868 compliant.
This will break interoperability with all older versions of
FreeBSD for those algorithms.

Reviewed by: bz, gnn
Obtained from: NETASQ
MFC after: 1w
fb307d7d1de2e083e767df27714dca5352792ac3 22-Nov-2010 dim <dim@FreeBSD.org> After some off-list discussion, revert a number of changes to the
DPCPU_DEFINE and VNET_DEFINE macros, as these cause problems for various
people working on the affected files. A better long-term solution is
still being considered. This reversal may give some modules empty
set_pcpu or set_vnet sections, but these are harmless.

Changes reverted:

------------------------------------------------------------------------
r215318 | dim | 2010-11-14 21:40:55 +0100 (Sun, 14 Nov 2010) | 4 lines

Instead of unconditionally emitting .globl's for the __start_set_xxx and
__stop_set_xxx symbols, only emit them when the set_vnet or set_pcpu
sections are actually defined.

------------------------------------------------------------------------
r215317 | dim | 2010-11-14 21:38:11 +0100 (Sun, 14 Nov 2010) | 3 lines

Apply the STATIC_VNET_DEFINE and STATIC_DPCPU_DEFINE macros throughout
the tree.

------------------------------------------------------------------------
r215316 | dim | 2010-11-14 21:23:02 +0100 (Sun, 14 Nov 2010) | 2 lines

Add macros to define static instances of VNET_DEFINE and DPCPU_DEFINE.
b355314642d1e24edf4e05d08b793f8b9ea76458 14-Nov-2010 dim <dim@FreeBSD.org> Sync: merge r215273 through r215318 from ^/head.
fda4020a881f36122bca03f2d8327da8bc67863d 14-Nov-2010 dim <dim@FreeBSD.org> Apply the STATIC_VNET_DEFINE and STATIC_DPCPU_DEFINE macros throughout
the tree.
de9392f9e014151a889a4d86de9f6736a94f158e 23-Oct-2010 bz <bz@FreeBSD.org> Make the IPsec SADB embedded route cache a union to be able to hold both the
legacy and IPv6 route destination address.
Previously in case of IPv6, there was a memory overwrite due to not enough
space for the IPv6 address.

PR: kern/122565
MFC After: 2 weeks
09f9c897d33c41618ada06fbbcf1a9b3812dee53 19-Oct-2010 jamie <jamie@FreeBSD.org> A new jail(8) with a configuration file, to replace the work currently done
by /etc/rc.d/jail.
8bca11513bec04b329a7ce2c4feae120d3175694 17-May-2010 vanhu <vanhu@FreeBSD.org> MFC: Locks SPTREE when setting some SP entries to state DEAD.
This can prevent kernel panics when updating SPs while there is
some traffic for them.

Obtained from: NETASQ
5bcdf64805f7eb94260d01a2635d9ca1aa8cceb7 12-May-2010 vanhu <vanhu@FreeBSD.org> MFC:
Set/update SA's NAT-T stuff before calling key_mature() in
key_add() and key_update(), as the SA may be used as soon as
key_mature() has been called

Obtained from: NETASQ
70e6f14e44387c54e20403484badf917d9b74589 06-May-2010 bz <bz@FreeBSD.org> MFC r207369:
MFP4: @176978-176982, 176984, 176990-176994, 177441

"Whitspace" churn after the VIMAGE/VNET whirls.

Remove the need for some "init" functions within the network
stack, like pim6_init(), icmp_init() or significantly shorten
others like ip6_init() and nd6_init(), using static initialization
again where possible and formerly missed.

Move (most) variables back to the place they used to be before the
container structs and VIMAGE_GLOABLS (before r185088) and try to
reduce the diff to stable/7 and earlier as good as possible,
to help out-of-tree consumers to update from 6.x or 7.x to 8 or 9.

This also removes some header file pollution for putatively
static global variables.

Revert VIMAGE specific changes in ipfilter::ip_auth.c, that are
no longer needed.

Reviewed by: jhb
Discussed with: rwatson
Sponsored by: The FreeBSD Foundation
Sponsored by: CK Software GmbH
b9358a210eef916d6fd2297113ab434502924bd2 05-May-2010 vanhu <vanhu@FreeBSD.org> Set SA's natt_type before calling key_mature() in key_add(),
as the SA may be used as soon as key_mature() has been done.

Obtained from: NETASQ
MFC after: 1 week
33dc72ec8c82736f5d5c2ab5c7e57aad0f4ba647 05-May-2010 vanhu <vanhu@FreeBSD.org> Update SA's NAT-T stuff before calling key_mature() in key_update(),
as SA may be used as soon as key_mature() has been called.

Obtained from: NETASQ
MFC after: 1 week
0a90ef17283bc848eee90c3bdd5bec3fcc1bc879 29-Apr-2010 bz <bz@FreeBSD.org> MFP4: @176978-176982, 176984, 176990-176994, 177441

"Whitspace" churn after the VIMAGE/VNET whirls.

Remove the need for some "init" functions within the network
stack, like pim6_init(), icmp_init() or significantly shorten
others like ip6_init() and nd6_init(), using static initialization
again where possible and formerly missed.

Move (most) variables back to the place they used to be before the
container structs and VIMAGE_GLOABLS (before r185088) and try to
reduce the diff to stable/7 and earlier as good as possible,
to help out-of-tree consumers to update from 6.x or 7.x to 8 or 9.

This also removes some header file pollution for putatively
static global variables.

Revert VIMAGE specific changes in ipfilter::ip_auth.c, that are
no longer needed.

Reviewed by: jhb
Discussed with: rwatson
Sponsored by: The FreeBSD Foundation
Sponsored by: CK Software GmbH
MFC after: 6 days
1f00f9ada8a046ee4db4a1aa486a9d5b6fc39998 15-Apr-2010 vanhu <vanhu@FreeBSD.org> Locks SPTREE when setting some SP entries to state DEAD.
This can prevent kernel panics when updating SPs while
there is some traffic for them.

Obtained from: NETASQ
MFC after: 1m
ac6d5f65a5a07b7bc02d10485f21fe86a923b97d 02-Apr-2010 bz <bz@FreeBSD.org> MFC r205789:

When tearing down IPsec as part of a (virtual) network stack,
do not try to free the same list twice but free both the
acquiring list and the security policy acquiring list.

Reviewed by: anchie
cb7afff0b8e3e70dd6ec8433f046113b67df2f77 28-Mar-2010 bz <bz@FreeBSD.org> When tearing down IPsec as part of a (virtual) network stack,
do not try to free the same list twice but free both the
acquiring list and the security policy acquiring list.

Reviewed by: anchie
MFC after: 3 days
f1216d1f0ade038907195fc114b7e630623b402c 19-Mar-2010 delphij <delphij@FreeBSD.org> Create a custom branch where I will be able to do the merge.
7b642517df2ee2c6cb82eb5cd127c7afe7309dc7 17-Nov-2009 vanhu <vanhu@FreeBSD.org> fixed two race conditions when inserting/removing SAs via PFKey,
which can both lead to a kernel panic when adding/removing quickly
a lot of SAs.

Obtained from: NETASQ
MFC after: 2w (MFC on 8 before 8.0 release ???)
550a925d5c0272c65a2722727a6a6c0d90818cf9 16-Sep-2009 vanhu <vanhu@FreeBSD.org> When checking traffic endpoint's adresses families in key_spdadd(),
compare them together instead of comparing each one with respective
tunnel endpoint.

PR: kern/138439
Submitted by: aurelien.ansel@netasq.com
Obtained from: NETASQ
MFC after: 1 m
dfd5ed3d446ef47de180761fc9814652df9f85e1 06-Sep-2009 pjd <pjd@FreeBSD.org> Silent gcc? Yeah, you wish. What I ment was to silence gcc.

Spotted by: julian
fe152cceaff5c45acd2712a5451f81769eb32dff 06-Sep-2009 pjd <pjd@FreeBSD.org> Initialize state_valid and arraysize variable so gcc won't complain.

Reported by: bz
aa12b3e9c9ecf42d7b88c1c9d1f52e2147073c90 06-Sep-2009 pjd <pjd@FreeBSD.org> Improve code a bit by eliminating goto and having one unlock per lock.
fb9ffed6504601ed9da2c6b9a620b133c838964c 01-Aug-2009 rwatson <rwatson@FreeBSD.org> Merge the remainder of kern_vimage.c and vimage.h into vnet.c and
vnet.h, we now use jails (rather than vimages) as the abstraction
for virtualization management, and what remained was specific to
virtual network stacks. Minor cleanups are done in the process,
and comments updated to reflect these changes.

Reviewed by: bz
Approved by: re (vimage blanket)
69550679325e630b5720649bc0f8787e31161476 19-Jul-2009 rwatson <rwatson@FreeBSD.org> Reimplement and/or implement vnet list locking by replacing a mostly
unused custom mutex/condvar-based sleep locks with two locks: an
rwlock (for non-sleeping use) and sxlock (for sleeping use). Either
acquired for read is sufficient to stabilize the vnet list, but both
must be acquired for write to modify the list.

Replace previous no-op read locking macros, used in various places
in the stack, with actual locking to prevent race conditions. Callers
must declare when they may perform unbounded sleeps or not when
selecting how to lock.

Refactor vnet sysinits so that the vnet list and locks are initialized
before kernel modules are linked, as the kernel linker will use them
for modules loaded by the boot loader.

Update various consumers of these KPIs based on whether they may sleep
or not.

Reviewed by: bz
Approved by: re (kib)
88f8de4d4001c74946458579ca0710df70161c90 16-Jul-2009 rwatson <rwatson@FreeBSD.org> Remove unused VNET_SET() and related macros; only VNET_GET() is
ever actually used. Rename VNET_GET() to VNET() to shorten
variable references.

Discussed with: bz, julian
Reviewed by: bz
Approved by: re (kensmith, kib)
57ca4583e728cab422fba8f15de10bd0b637b3dd 14-Jul-2009 rwatson <rwatson@FreeBSD.org> Build on Jeff Roberson's linker-set based dynamic per-CPU allocator
(DPCPU), as suggested by Peter Wemm, and implement a new per-virtual
network stack memory allocator. Modify vnet to use the allocator
instead of monolithic global container structures (vinet, ...). This
change solves many binary compatibility problems associated with
VIMAGE, and restores ELF symbols for virtualized global variables.

Each virtualized global variable exists as a "reference copy", and also
once per virtual network stack. Virtualized global variables are
tagged at compile-time, placing the in a special linker set, which is
loaded into a contiguous region of kernel memory. Virtualized global
variables in the base kernel are linked as normal, but those in modules
are copied and relocated to a reserved portion of the kernel's vnet
region with the help of a the kernel linker.

Virtualized global variables exist in per-vnet memory set up when the
network stack instance is created, and are initialized statically from
the reference copy. Run-time access occurs via an accessor macro, which
converts from the current vnet and requested symbol to a per-vnet
address. When "options VIMAGE" is not compiled into the kernel, normal
global ELF symbols will be used instead and indirection is avoided.

This change restores static initialization for network stack global
variables, restores support for non-global symbols and types, eliminates
the need for many subsystem constructors, eliminates large per-subsystem
structures that caused many binary compatibility issues both for
monitoring applications (netstat) and kernel modules, removes the
per-function INIT_VNET_*() macros throughout the stack, eliminates the
need for vnet_symmap ksym(2) munging, and eliminates duplicate
definitions of virtualized globals under VIMAGE_GLOBALS.

Bump __FreeBSD_version and update UPDATING.

Portions submitted by: bz
Reviewed by: bz, zec
Discussed with: gnn, jamie, jeff, jhb, julian, sam
Suggested by: peter
Approved by: re (kensmith)
bd6eb7be79d81290efa6dcaa9f492a05b1966344 25-Jun-2009 rwatson <rwatson@FreeBSD.org> Add address list locking for in6_ifaddrhead/ia_link: as with locking
for in_ifaddrhead, we stick with an rwlock for the time being, which
we will revisit in the future with a possible move to rmlocks.

Some pieces of code require significant further reworking to be
safe from all classes of writer-writer races.

Reviewed by: bz
MFC after: 6 weeks
ea70a3542dc7bf7a2e16d5d9c4639fd211a395b6 25-Jun-2009 rwatson <rwatson@FreeBSD.org> Add a new global rwlock, in_ifaddr_lock, which will synchronize use of the
in_ifaddrhead and INADDR_HASH address lists.

Previously, these lists were used unsynchronized as they were effectively
never changed in steady state, but we've seen increasing reports of
writer-writer races on very busy VPN servers as core count has gone up
(and similar configurations where address lists change frequently and
concurrently).

For the time being, use rwlocks rather than rmlocks in order to take
advantage of their better lock debugging support. As a result, we don't
enable ip_input()'s read-locking of INADDR_HASH until an rmlock conversion
is complete and a performance analysis has been done. This means that one
class of reader-writer races still exists.

MFC after: 6 weeks
Reviewed by: bz
9c4380a8eea873952968c44b6e2567cd55ba5011 24-Jun-2009 rwatson <rwatson@FreeBSD.org> Convert netinet6 to using queue(9) rather than hand-crafted linked lists
for the global IPv6 address list (in6_ifaddr -> in6_ifaddrhead). Adopt
the code styles and conventions present in netinet where possible.

Reviewed by: gnn, bz
MFC after: 6 weeks (possibly not MFCable?)
55f686804402b9ab9f0b0a61f96997ad8ec34878 19-Jun-2009 bz <bz@FreeBSD.org> Move setting of ports from NAT-T below key_getsah() and actually
below key_setsaval().
Without that, the lookup for the SA had failed as we were looking for
a SA with the new, updated port numbers instead of the old ones and
were comparing the ports in key_cmpsaidx().
This makes updating the remote -> local SA on the initiator work again.

Problem introduced with: p4 changeset 152114
16c1346b9a6c737fd054d4d0644bf5104fcb32aa 12-Jun-2009 vanhu <vanhu@FreeBSD.org> Added support for NAT-Traversal (RFC 3948) in IPsec stack.

Thanks to (no special order) Emmanuel Dreyfus (manu@netbsd.org), Larry
Baird (lab@gta.com), gnn, bz, and other FreeBSD devs, Julien Vanherzeele
(julien.vanherzeele@netasq.com, for years of bug reporting), the PFSense
team, and all people who used / tried the NAT-T patch for years and
reported bugs, patches, etc...

X-MFC: never

Reviewed by: bz
Approved by: gnn(mentor)
Obtained from: NETASQ
8b1f38241aaf07621c062901b7946145be2862b6 08-Jun-2009 zec <zec@FreeBSD.org> Introduce an infrastructure for dismantling vnet instances.

Vnet modules and protocol domains may now register destructor
functions to clean up and release per-module state. The destructor
mechanisms can be triggered by invoking "vimage -d", or a future
equivalent command which will be provided via the new jail framework.

While this patch introduces numerous placeholder destructor functions,
many of those are currently incomplete, thus leaking memory or (even
worse) failing to stop all running timers. Many of such issues are
already known and will be incrementaly fixed over the next weeks in
smaller incremental commits.

Apart from introducing new fields in structs ifnet, domain, protosw
and vnet_net, which requires the kernel and modules to be rebuilt, this
change should have no impact on nooptions VIMAGE builds, since vnet
destructors can only be called in VIMAGE kernels. Moreover,
destructor functions should be in general compiled in only in
options VIMAGE builds, except for kernel modules which can be safely
kldunloaded at run time.

Bump __FreeBSD_version to 800097.
Reviewed by: bz, julian
Approved by: rwatson, kib (re), julian (mentor)
48cef84e5fd897fd091510cbaa9c81f73e2d7fa6 27-May-2009 vanhu <vanhu@FreeBSD.org> Lock SPTREE before parsing it in key_spddump()

Approved by: gnn(mentor)
Obtained from: NETASQ
MFC after: 2 weeks
6e1cb07c00e4d2ec529383b09d8f166eec355949 27-May-2009 vanhu <vanhu@FreeBSD.org> Only decrease refcnt once when flushing SPD entries, to
avoid flushing entries which are still used.

Approved by: gnn(mentor)
Obtained from: NETASQ
MFC after: 1 month
0915b81c7646c45ceab3d001dcbfbb6e4734f61a 29-Apr-2009 bms <bms@FreeBSD.org> Stub out IN6_LOOKUP_MULTI() for GETSPI requests, for now.

This has the effect that IPv6 multicast traffic won't trigger
an SPI allocation when IPSEC is in use, however, this obviously
needs to stomp on locks, and IN6_LOOKUP_MULTI() is about to go away.

This definitely needs to be revisited before 8.x is branched as
a release branch.
a12cc82f1a9f7b092795ca96ff1a6fac818cb536 27-Apr-2009 bz <bz@FreeBSD.org> key_gettunnel() has been unsued with FAST_IPSEC (now IPSEC).
KAME had explicit checks at one point using it, so just hide it behind
#if 0 for now until we are sure if we can completely dump it or not.

MFC after: 1 month
c85551e0bc714ce0e1634c2d308b1616c8dd88ca 06-Apr-2009 zec <zec@FreeBSD.org> First pass at separating per-vnet initializer functions
from existing functions for initializing global state.

At this stage, the new per-vnet initializer functions are
directly called from the existing global initialization code,
which should in most cases result in compiler inlining those
new functions, hence yielding a near-zero functional change.

Modify the existing initializer functions which are invoked via
protosw, like ip_init() et. al., to allow them to be invoked
multiple times, i.e. per each vnet. Global state, if any,
is initialized only if such functions are called within the
context of vnet0, which will be determined via the
IS_DEFAULT_VNET(curvnet) check (currently always true).

While here, V_irtualize a few remaining global UMA zones
used by net/netinet/netipsec networking code. While it is
not yet clear to me or anybody else whether this is the right
thing to do, at this stage this makes the code more readable,
and makes it easier to track uncollected UMA-zone-backed
objects on vnet removal. In the long run, it's quite possible
that some form of shared use of UMA zone pools among multiple
vnets should be considered.

Bump __FreeBSD_version due to changes in layout of structs
vnet_ipfw, vnet_inet and vnet_net.

Approved by: julian (mentor)
7e0f7398ba9967462bbada5db3f2e4e9de7632aa 23-Mar-2009 vanhu <vanhu@FreeBSD.org> Fixed comments so it stays in 80 chars by line
with hard tabs of 8 chars....

Approved by: gnn(mentor)
21967caaf20a0e02ee5864ca4d372e88b8f85159 20-Mar-2009 vanhu <vanhu@FreeBSD.org> Spelling fix in a comment

Approved by: gnn(mentor)
cea6d30cdc246bbd6e0284739528eb15efcc6ac2 19-Mar-2009 vanhu <vanhu@FreeBSD.org> Fixed style for some comments

Approved by: gnn(mentor)
72aca0d9479f7f4c8fbc003cd979485165832cde 19-Mar-2009 vanhu <vanhu@FreeBSD.org> Fixed style for some comments

Approved by: gnn(mentor)
e33d6fbff66f2eccce1a11a22272916f2ec6ae54 18-Mar-2009 vanhu <vanhu@FreeBSD.org> Fixed deletion of sav entries in key_delsah()

Approved by: gnn(mentor)
Obtained from: NETASQ
MFC after: 1 month
a5f4a557440b665d9f45332d692d6b071433d401 05-Mar-2009 vanhu <vanhu@FreeBSD.org> SAs are valid (but dying) when they reached soft lifetime,
even if they have never been used.

Approved by: gnn(mentor)
MFC after: 2 weeks
e5bfcba0804aec97eaffc409174ebf4d4fe91cca 24-Feb-2009 rdivacky <rdivacky@FreeBSD.org> Change the functions to ANSI in those cases where it breaks promotion
to int rule. See ISO C Standard: SS6.7.5.3:15.

Approved by: kib (mentor)
Reviewed by: warner
Tested by: silence on -current
03f6bb9dc9b98a515613b815d92f251d470edfeb 15-Dec-2008 bz <bz@FreeBSD.org> Another step assimilating IPv[46] PCB code - directly use
the inpcb names rather than the following IPv6 compat macros:
in6pcb,in6p_sp, in6p_ip6_nxt,in6p_flowinfo,in6p_vflag,
in6p_flags,in6p_socket,in6p_lport,in6p_fport,in6p_ppcb and
sotoin6pcb().

Apart from removing duplicate code in netipsec, this is a pure
whitespace, not a functional change.

Discussed with: rwatson
Reviewed by: rwatson (version before review requested changes)
MFC after: 4 weeks (set the timer and see then)
604d89458ab94ec81eaefa2d55ef219cba461e31 02-Dec-2008 bz <bz@FreeBSD.org> Rather than using hidden includes (with cicular dependencies),
directly include only the header files needed. This reduces the
unneeded spamming of various headers into lots of files.

For now, this leaves us with very few modules including vnet.h
and thus needing to depend on opt_route.h.

Reviewed by: brooks, gnn, des, zec, imp
Sponsored by: The FreeBSD Foundation
95a15f5c8435d206ae030c1ed698f4fca6afdeae 26-Nov-2008 zec <zec@FreeBSD.org> Merge more of currently non-functional (i.e. resolving to
whitespace) macros from p4/vimage branch.

Do a better job at enclosing all instantiations of globals
scheduled for virtualization in #ifdef VIMAGE_GLOBALS blocks.

De-virtualize and mark as const saorder_state_alive and
saorder_state_any arrays from ipsec code, given that they are never
updated at runtime, so virtualizing them would be pointless.

Reviewed by: bz, julian
Approved by: julian (mentor)
Obtained from: //depot/projects/vimage-commit2/...
X-MFC after: never
Sponsored by: NLnet Foundation, The FreeBSD Foundation
19b6af98ec71398e77874582eb84ec5310c7156f 22-Nov-2008 dfr <dfr@FreeBSD.org> Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
performance in Xen's HVM mode.
815d52c5df6a76286604478e5223d2f2c87b2c04 19-Nov-2008 zec <zec@FreeBSD.org> Change the initialization methodology for global variables scheduled
for virtualization.

Instead of initializing the affected global variables at instatiation,
assign initial values to them in initializer functions. As a rule,
initialization at instatiation for such variables should never be
introduced again from now on. Furthermore, enclose all instantiations
of such global variables in #ifdef VIMAGE_GLOBALS blocks.

Essentialy, this change should have zero functional impact. In the next
phase of merging network stack virtualization infrastructure from
p4/vimage branch, the new initialization methology will allow us to
switch between using global variables and their counterparts residing in
virtualization containers with minimum code churn, and in the long run
allow us to intialize multiple instances of such container structures.

Discussed at: devsummit Strassburg
Reviewed by: bz, julian
Approved by: julian (mentor)
Obtained from: //depot/projects/vimage-commit2/...
X-MFC after: never
Sponsored by: NLnet Foundation, The FreeBSD Foundation
cf5320822f93810742e3d4a1ac8202db8482e633 19-Oct-2008 lulf <lulf@FreeBSD.org> - Import the HEAD csup code which is the basis for the cvsmode work.
8797d4caecd5881e312923ee1d07be3de68755dc 02-Oct-2008 zec <zec@FreeBSD.org> Step 1.5 of importing the network stack virtualization infrastructure
from the vimage project, as per plan established at devsummit 08/08:
http://wiki.freebsd.org/Image/Notes200808DevSummit

Introduce INIT_VNET_*() initializer macros, VNET_FOREACH() iterator
macros, and CURVNET_SET() context setting macros, all currently
resolving to NOPs.

Prepare for virtualization of selected SYSCTL objects by introducing a
family of SYSCTL_V_*() macros, currently resolving to their global
counterparts, i.e. SYSCTL_V_INT() == SYSCTL_INT().

Move selected #defines from sys/sys/vimage.h to newly introduced header
files specific to virtualized subsystems (sys/net/vnet.h,
sys/netinet/vinet.h etc.).

All the changes are verified to have zero functional impact at this
point in time by doing MD5 comparision between pre- and post-change
object files(*).

(*) netipsec/keysock.c did not validate depending on compile time options.

Implemented by: julian, bz, brooks, zec
Reviewed by: julian, bz, brooks, kris, rwatson, ...
Approved by: julian (mentor)
Obtained from: //depot/projects/vimage-commit2/...
X-MFC after: never
Sponsored by: NLnet Foundation, The FreeBSD Foundation
b9e15393f3200bb0b80000f91a52c111623013d0 02-Sep-2008 vanhu <vanhu@FreeBSD.org> MFC: Add lifetime informations to generated SPD entries when SPDDUMP

Approved by: re
1021d43b569bfc8d2c5544bde2f540fa432b011f 17-Aug-2008 bz <bz@FreeBSD.org> Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).

This is the first in a series of commits over the course
of the next few weeks.

Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.

We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.

Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
3a946f98dc7d355c76b373b26abead5eb6f369d9 05-Aug-2008 vanhu <vanhu@FreeBSD.org> Add lifetime informations to generated SPD entries when SPDDUMP

Approved by: gnn (mentor)
MFC after: 4 weeks
4dcc97b12c61ad6e675735c3e7e377bc86d74ccc 29-Jun-2008 julian <julian@FreeBSD.org> Enter the 1990s. Use real function declaration.
f0cccb315e8806fe671c00556acc1072cc5333d0 28-Apr-2008 bz <bz@FreeBSD.org> MFC: sys/netipsec/key.c rev. 1.31,
also apply to sys/netkey/key.c not present in 7.x/8.

Import change from KAME, rev. 1.362 kame/kame/sys/netkey/key.c

In case of "new SA", we must check the hard lifetime of the old SA
to find out if it is not permanent and we can delete it.

Submitted by: sakane via gnn
b46e8d69ee88220f1cf068380706a4e497d7e79f 29-Mar-2008 bz <bz@FreeBSD.org> MFC: rev. 1.32 key.c

Fix a bug that when getting/dumping the soft lifetime we reported
the hard lifetime instead.
0175898aced51988b28c44f4ee41efaca1194392 29-Mar-2008 bz <bz@FreeBSD.org> MFC: rev. 1.31 key.c

Import change from KAME, rev. 1.362 kame/kame/sys/netkey/key.c

In case of "new SA", we must check the hard lifetime of the old SA
to find out if it is not permanent and we can delete it.
e1cf25141cc4502c7399cd734710dadf1abf0abd 24-Mar-2008 bz <bz@FreeBSD.org> Fix a bug that when getting/dumping the soft lifetime we reported
the hard lifetime instead.

MFC after: 3 days
42fbad307be1017720e678ecda4d07a89a7f75ef 24-Mar-2008 bz <bz@FreeBSD.org> Import change from KAME, rev. 1.362 kame/kame/sys/netkey/key.c

In case of "new SA", we must check the hard lifetime of the old SA
to find out if it is not permanent and we can delete it.

Submitted by: sakane via gnn
MFC after: 3 days
a205a8dfd19754af27f722c44b257338f2ec3c31 21-Mar-2008 bz <bz@FreeBSD.org> MFC: rev. 1.30 key.c

Remove the "Fast " from the
"Fast IPsec: Initialized Security Association Processing." printf.
People kept asking questions about this after the IPsec shuffle.

This still is the Fast IPsec implementation so no worries that it would
be any slower now. There are no functional changes.
ee90b5b6c8e3d3effb8be636e2e720dca2befc97 14-Mar-2008 bz <bz@FreeBSD.org> Remove the "Fast " from the
"Fast IPsec: Initialized Security Association Processing." printf.
People kept asking questions about this after the IPsec shuffle.

This still is the Fast IPsec implementation so no worries that it would
be any slower now. There are no functional changes.

Discussed with: sam
MFC after: 4 days
eb3f079300f3a85c4ec39214226db49c5e9e32f2 08-Mar-2008 bz <bz@FreeBSD.org> MFC rev. 1.27 key.c

'spi' and the return value of ntohl are unsigned. Remove the extra >=0
check which was always true.
Document the special meaning of spi values of 0 and 1-255 with a comment.

Found with: Coverity Prevent(tm)
CID: 2047
360f674775ddffbab3307fb0976d265cf6b58d85 08-Mar-2008 bz <bz@FreeBSD.org> MFC rev. 1.26 key.c

In case of failure we can directly return ENOBUFS because
'result' is still NULL and we do not need to free anything.
That allows us to gc the entire goto parts and a now unused variable.

Found with: Coverity Prevent(tm)
CID: 2519
633096586a47e36f3a9726fb43a308b583239735 08-Mar-2008 bz <bz@FreeBSD.org> MFC rev. 1.25

Add a missing return so that we drop out in case of an error and
do not continue with a NULL pointer. [1]

While here change the return of the error handling code path above.
I cannot see why we should always return 0 there. Neither does KAME
nor do we in here for the similar check in all the other functions.

Found with: Coverity Prevent(tm) [1]
CID: 2521
5226c518f1f294880ef61379472c2b13ea1cb53f 08-Mar-2008 bz <bz@FreeBSD.org> MFC rev. 1.23 key.c

Add missing
break;
so when comparing AF_INET6 addresses, scope and ports we do not run into
the default case and return 'no match' instead of 'match'.
eb5b6b61e4d3592bf77cbbd2830ff22357136657 08-Mar-2008 bz <bz@FreeBSD.org> MFC: rev. 1.29 key.c, 1.6 key_debug.c

Fix bugs when allocating and passing information of current lifetime and
soft lifetime [1] introduced in rev. 1.21 of key.c.

Along with that, fix a related problem in key_debug
printing the correct data.
While there replace a printf by panic in a sanity check.

PR: 120751
Submitted by: Kazuaki ODA (kazuaki aliceblue.jp) [1]
767a2621f07a4ba9a59a9d383581ed5558f667db 02-Mar-2008 bz <bz@FreeBSD.org> Fix bugs when allocating and passing information of current lifetime and
soft lifetime [1] introduced in rev. 1.21 of key.c.

Along with that, fix a related problem in key_debug
printing the correct data.
While there replace a printf by panic in a sanity check.

PR: 120751
Submitted by: Kazuaki ODA (kazuaki aliceblue.jp) [1]
MFC after: 5 days
0cd74db89b7c7ca5bface8b05ae8263c0a54217b 01-Jul-2007 gnn <gnn@FreeBSD.org> Commit IPv6 support for FAST_IPSEC to the tree.
This commit includes only the kernel files, the rest of the files
will follow in a second commit.

Reviewed by: bz
Approved by: re
Supported by: Secure Computing
028d7c7c98c788d122890a23c12143b9e9bcc8d9 16-Jun-2007 bz <bz@FreeBSD.org> 'spi' and the return value of ntohl are unsigned. Remove the extra >=0
check which was always true.
Document the special meaning of spi values of 0 and 1-255 with a comment.

Found with: Coverity Prevent(tm)
CID: 2047
e1f2e7690497be9f169ae2ca111f08881f575df0 16-Jun-2007 bz <bz@FreeBSD.org> In case of failure we can directly return ENOBUFS because
'result' is still NULL and we do not need to free anything.
That allows us to gc the entire goto parts and a now unused variable.

Found with: Coverity Prevent(tm)
CID: 2519
e622d327e5f02efe846738ac48071275cdd3a234 15-Jun-2007 bz <bz@FreeBSD.org> Add a missing return so that we drop out in case of an error and
do not continue with a NULL pointer. [1]

While here change the return of the error handling code path above.
I cannot see why we should always return 0 there. Neither does KAME
nor do we in here for the similar check in all the other functions.

Found with: Coverity Prevent(tm) [1]
CID: 2521
3a2d39f8a2c5a1506f00bdd70936c4bce749b9f5 15-Jun-2007 bz <bz@FreeBSD.org> With the current code 'src' is never NULL. Nevertheless move the check for
NULL before dereferencing the pointer.

Found with: Coverity Prevent(tm)
CID: 2528
4662f48b4e0c12ccb03bc7c976f73806ac13d742 29-May-2007 bz <bz@FreeBSD.org> Add missing
break;
so when comparing AF_INET6 addresses, scope and ports we do not run into
the default case and return 'no match' instead of 'match'.
1d18621829162824339d30df4411bd71830fd229 04-Sep-2006 pjd <pjd@FreeBSD.org> MFC: sys/netipsec/key.c
sys/netipsec/xform_ah.c
sys/netipsec/xform_esp.c
sys/netipsec/xform_ipcomp.c

- Allow to use fast_ipsec(4) on debug.mpsafenet=0 and INVARIANTS-enabled
systems. Without the change it will panic on assertions.
- Update the code after opencrypto changes.
c1590119a64caa45fdd2f78f22fd55031b1ac64b 20-May-2006 pjd <pjd@FreeBSD.org> Prevent disappearing SAD entries by implementing MPsafe refcounting.
"Why didn't he use SECASVAR_LOCK()/SECASVAR_UNLOCK() macros to
synchronize access to the secasvar structure's fields?" one may ask.
There were two reasons:
1. refcount(9) is faster then mutex(9) synchronization (one atomic
operation instead of two).
2. Those macros are not used now at all, so at some point we may decide
to remove them entirely.

OK'ed by: gnn
MFC after: 2 weeks
4a5d9d5dd3dfb4c03c8d3ff47b1ac35206dcae01 25-Mar-2006 gnn <gnn@FreeBSD.org> First steps towards IPSec cleanup.

Make the kernel side of FAST_IPSEC not depend on the shared
structures defined in /usr/include/net/pfkeyv2.h The kernel now
defines all the necessary in kernel structures in sys/netipsec/keydb.h
and does the proper massaging when moving messages around.

Sponsored By: Secure Computing
a50ffc29129a52835a39bf4868cd5facdc7dce30 07-Jan-2005 imp <imp@FreeBSD.org> /* -> /*- for license, minor formatting changes
9cd015b873f04af0a8e3e6bd8562851660b6de2b 02-Oct-2004 sam <sam@FreeBSD.org> Remove extraneous SECPOLICY_LOCK_DESTROY calls that cause the mutex to be
destroyed twice.

Submitted by: Roselyn Lee
3234aae2af1b6938eaae524891568de12db7c8cc 30-Sep-2004 sam <sam@FreeBSD.org> Add missing locking for secpolicy refcnt manipulations.

Submitted by: Roselyn Lee
806751e928b4a0004f46b4c9f3e6f2668c0572c1 26-Sep-2004 sam <sam@FreeBSD.org> Correct handling of SADB_UPDATE and SADB_ADD requests. key_align may split
the mbuf due to use of m_pulldown. Discarding the result because of this
does not make sense as no subsequent code depends on the entire msg being
linearized (only the individual pieces). It's likely something else is wrong
here but for now this appears to get things back to a working state.

Submitted by: Roselyn Lee
211ce911d54b19971c181b448943c9921fa42f45 22-Jun-2004 bms <bms@FreeBSD.org> Fix a paste-o in key_cmpspidx_withmask().

PR: misc/67013
Submitted by: Zhenmin <zli4@cs.uiuc.edu>
047c7305945b8362821d30070098022bc0c18749 03-May-2004 sam <sam@FreeBSD.org> use correct address for SADB_EXT_ADDRESS_DST in key_do_allocsa_policy
(was using src instead of dst)

Submitted by: Bjoern A. Zeeb
Obtained from: KAME
MFC after: 1 day
7cd921a9fc2f726a5299e7995bb8d52ab24a4aa0 03-May-2004 sam <sam@FreeBSD.org> correct behaviour of key_getsavbyspi broken in rev 1.7; corrects problems with
removing specific SPIs

Submitted by: Bjoern A. Zeeb
7f15aed8bdb45ef1c1d7835c4ac1e7c89a9f2570 03-May-2004 sam <sam@FreeBSD.org> add support to prefer old SA to new SA during allocation
(makes net.key.preferred_oldsa work as for KAME)

Submitted by: gabor@sentex.net
Reviewed by: Bjoern A. Zeeb
MFC after: 1 day
91d40e47d6d7cd7190283cfdbe6ca0ed2dd0bb02 07-Apr-2004 pjd <pjd@FreeBSD.org> Unbreak FAST_IPSEC build on 64 bit archs with INVARIANTS.

Approved by: sam
349bb30cb998b7cfd0491763bcf4f4ba1549f06b 16-Feb-2004 guido <guido@FreeBSD.org> Fix type in a sysctl. It used to be: net.key.prefered_oldsa
and is corrected to net.key.preferred_oldsa
This makes it consistent with the KAME IPsec implementation.

Approved by: sam
903cdeea1a6d0c99fecc1d8aeeab65bdfbab46d7 11-Feb-2004 bms <bms@FreeBSD.org> Initial import of RFC 2385 (TCP-MD5) digest support.

This is the first of two commits; bringing in the kernel support first.
This can be enabled by compiling a kernel with options TCP_SIGNATURE
and FAST_IPSEC.

For the uninitiated, this is a TCP option which provides for a means of
authenticating TCP sessions which came into being before IPSEC. It is
still relevant today, however, as it is used by many commercial router
vendors, particularly with BGP, and as such has become a requirement for
interconnect at many major Internet points of presence.

Several parts of the TCP and IP headers, including the segment payload,
are digested with MD5, including a shared secret. The PF_KEY interface
is used to manage the secrets using security associations in the SADB.

There is a limitation here in that as there is no way to map a TCP flow
per-port back to an SPI without polluting tcpcb or using the SPD; the
code to do the latter is unstable at this time. Therefore this code only
supports per-host keying granularity.

Whilst FAST_IPSEC is mutually exclusive with KAME IPSEC (and thus IPv6),
TCP_SIGNATURE applies only to IPv4. For the vast majority of prospective
users of this feature, this will not pose any problem.

This implementation is output-only; that is, the option is honoured when
responding to a host initiating a TCP session, but no effort is made
[yet] to authenticate inbound traffic. This is, however, sufficient to
interwork with Cisco equipment.

Tested with a Cisco 2501 running IOS 12.0(27), and Quagga 0.96.4 with
local patches. Patches for tcpdump to validate TCP-MD5 sessions are also
available from me upon request.

Sponsored by: sentex.net
02e3f748684e0de7afa3df5467480f2951e07993 05-Feb-2004 sam <sam@FreeBSD.org> must convert protocol to sa type when preparing a DELETE message

Submitted by: Roselyn Lee <rosel@verniernetworks.com>
MFC after: 1 week
0a6c1d4242d4d23328bf77b152fe0864e2db7f35 29-Sep-2003 sam <sam@FreeBSD.org> MFp4: portability work, general cleanup, locking fixes

change 38496
o add ipsec_osdep.h that holds os-specific definitions for portability
o s/KASSERT/IPSEC_ASSERT/ for portability
o s/SPLASSERT/IPSEC_SPLASSERT/ for portability
o remove function names from ASSERT strings since line#+file pinpints
the location
o use __func__ uniformly to reduce string storage
o convert some random #ifdef DIAGNOSTIC code to assertions
o remove some debuggging assertions no longer needed

change 38498
o replace numerous bogus panic's with equally bogus assertions
that at least go away on a production system

change 38502 + 38530
o change explicit mtx operations to #defines to simplify
future changes to a different lock type

change 38531
o hookup ipv4 ctlinput paths to a noop routine; we should be
handling path mtu changes at least
o correct potential null pointer deref in ipsec4_common_input_cb

chnage 38685
o fix locking for bundled SA's and for when key exchange is required

change 38770
o eliminate recursion on the SAHTREE lock

change 38804
o cleanup some types: long -> time_t
o remove refrence to dead #define

change 38805
o correct some types: long -> time_t
o add scan generation # to secpolicy to deal with locking issues

change 38806
o use LIST_FOREACH_SAFE instead of handrolled code
o change key_flush_spd to drop the sptree lock before purging
an entry to avoid lock recursion and to avoid holding the lock
over a long-running operation
o misc cleanups of tangled and twisty code

There is still much to do here but for now things look to be
working again.

Supported by: FreeBSD Foundation
7a8c89dde15c19a8c1e8eb2976dc9936d9b7329c 01-Sep-2003 sam <sam@FreeBSD.org> Locking and misc cleanups; most of which I've been running for >4 months:

o add locking
o strip irrelevant spl's
o split malloc types to better account for memory use
o remove unused IPSEC_NONBLOCK_ACQUIRE code
o remove dead code

Sponsored by: FreeBSD Foundation
68cba5536261ecfb6a1ae7d66fbdd97474906746 29-Jun-2003 sam <sam@FreeBSD.org> plug xform memory leaks:

o add missing zeroize op when deleting an SA
o don't re-initialize an xform for an SA that already has one

Submitted by: Doug Ambrisko <ambrisko@verniernetworks.com>
MFC after: 1 day
cf874b345d0f766fb64cf4737e1c85ccc78d2bee 19-Feb-2003 imp <imp@FreeBSD.org> Back out M_* changes, per decision of the TRB.

Approved by: trb
61f86586d8546ec9291c4bbe9bb92b3c02b46f07 05-Feb-2003 ache <ache@FreeBSD.org> Comment out srandom():
1) Already called in init_main.c:proc0_post()
2) Seed is bad
bf8e8a6e8f0bd9165109f0a258730dd242299815 21-Jan-2003 alfred <alfred@FreeBSD.org> Remove M_TRYWAIT/M_WAITOK/M_WAIT. Callers should use 0.
Merge M_NOWAIT/M_DONTWAIT into a single flag M_NOWAIT.
d3367c5f5d3ddcc6824d8f41c4cf179f9a5588f8 01-Jan-2003 schweikh <schweikh@FreeBSD.org> Correct typos, mostly s/ a / an / where appropriate. Some whitespace cleanup,
especially in troff files.
f6bdcf8ff2c152663769d4f1bcdb9872cdcb5453 16-Oct-2002 sam <sam@FreeBSD.org> "Fast IPsec": this is an experimental IPsec implementation that is derived
from the KAME IPsec implementation, but with heavy borrowing and influence
of openbsd. A key feature of this implementation is that it uses the kernel
crypto framework to do all crypto work so when h/w crypto support is present
IPsec operation is automatically accelerated. Otherwise the protocol
implementations are rather differet while the SADB and policy management
code is very similar to KAME (for the moment).

Note that this implementation is enabled with a FAST_IPSEC option. With this
you get all protocols; i.e. there is no FAST_IPSEC_ESP option.

FAST_IPSEC and IPSEC are mutually exclusive; you cannot build both into a
single system.

This software is well tested with IPv4 but should be considered very
experimental (i.e. do not deploy in production environments). This software
does NOT currently support IPv6. In fact do not configure FAST_IPSEC and
INET6 in the same system.

Obtained from: KAME + openbsd
Supported by: Vernier Networks