Commit Graph

758 Commits

Author SHA1 Message Date
maxv
824196ce25 style 2018-05-31 05:52:09 +00:00
maxv
d52acbb31e Correctly handle the padding for IPv6-AH, as specified by RFC4302. Seen in
a FreeBSD bug report, by Jason Mader.

The RFC specifies that under IPv6 the complete AH header must be 64bit-
aligned, and under IPv4 32bit-aligned. That's a rule we've never respected.
The other BSDs and MacOS never have either.

So respect it now.

This makes it possible to set up IPv6-AH between Linux and NetBSD, and also
probably between Windows and NetBSD.

Until now all the tests I made were between two *BSD hosts, and everything
worked "correctly" since both hosts were speaking the same non-standard
AHv6, so they could understand each other.

Tested with Fedora<->NetBSD, hmac-sha2-384.
2018-05-30 18:02:40 +00:00
maxv
7aac0885cf Introduce ah_authsiz, which computes the length of the ICV only. Use it in
esp_hdrsiz, and clarify.

Until now we were using ah_hdrsiz, and were relying on the fact that the
size of the AH header happens to be equal to that of the ESP trailer.

Now the size of the ESP trailer is added manually. This also fixes one
branch in esp_hdrsiz: we always append an ESP trailer, so it must always
be taken into account, and not just when an ICV is here.
2018-05-30 17:17:11 +00:00
maxv
de1c7e1421 Apply the previous change in esp_input too, same as esp_output. 2018-05-30 16:49:38 +00:00
maxv
f4ab40f822 Remove dead code, 'espx' is never NULL and dereferenced earlier, so no need
to NULL-check all the time.
2018-05-30 16:43:29 +00:00
maxv
173a0d4a18 Simplify the padding computation. Until now 'padlen' contained the ESP
Trailer (two bytes), and we were doing minus two all the time.

Declare 'tlen', which contains padlen+ESP_Trailer+ICV, and use 'struct
esptail' instead of hardcoding the construction of the trailer. 'padlen'
now indicates only the length of the padding, so no need to do -2.
2018-05-30 16:32:26 +00:00
maxv
bc40d0f4f9 Rename padding -> padlen, pad -> tail, and clarify. 2018-05-30 16:15:19 +00:00
maxv
4b1bee9844 Strengthen and simplify, once more. 2018-05-29 16:50:38 +00:00
ozaki-r
25ec1e8354 Fix non-INET6 builds 2018-05-29 09:25:44 +00:00
maxv
a1d8c752e7 IP6_EXTHDR_GET -> M_REGION_GET, no functional change. 2018-05-18 18:58:51 +00:00
maxv
00ff305a9c Merge ipsec4_input and ipsec6_input into ipsec_ip_input. Make the argument
a bool for clarity. Optimize the function: if M_CANFASTFWD is not there
(because already removed by the firewall) leave now.

Makes it easier to see that M_CANFASTFWD is not removed on IPv6.
2018-05-14 17:34:26 +00:00
ozaki-r
b51605482b Restore TCP header inclusions for TCP_SIGNATURE 2018-05-14 02:16:29 +00:00
maxv
88bfc8dc97 Remove unused calls to nat_t_ports_get. 2018-05-13 18:34:59 +00:00
maxv
336c2eba16 ENOBUFS -> EACCES when updating the replay counter. 2018-05-11 15:43:07 +00:00
maxv
2bf15a966b Clean up, and panic if we call functions that are not supposed to be
called.
2018-05-11 13:50:38 +00:00
maxv
59275f7378 Replace dumb code by M_VERIFY_PACKET. In fact, perhaps we should not even
call M_VERIFY_PACKET here, there is no particular reason for this place to
be more wrong than the rest.
2018-05-10 05:15:14 +00:00
maxv
f813c44d31 Rename ipsec4_forward -> ipsec_mtu, and switch to void. 2018-05-10 05:08:53 +00:00
maxv
858396682a static const on ipsecif4_encapsw 2018-05-09 07:33:31 +00:00
maxv
436305f8d3 Remove a dummy reference to XF_IP4, explain briefly why we don't use
ipe4_xformsw, and remove unused includes.
2018-05-07 09:33:51 +00:00
maxv
20668b06aa Remove now unused 'isr', 'skip' and 'protoff' arguments from ipip_output. 2018-05-07 09:25:04 +00:00
maxv
defd3aa31c Remove unused 'mp' argument from all the xf_output functions. Also clean
up xform.h a bit.
2018-05-07 09:16:46 +00:00
maxv
a3d9b92c8d Clarify IPIP: ipe4_xformsw is not allowed to call ipip_output, so replace
the pointer by ipe4_output, which just panics. Group the ipe4_* functions
together. Localify other functions.

ok ozaki-r@
2018-05-07 09:08:06 +00:00
maxv
69c944f0bf Remove some more dead code. 2018-05-01 08:34:08 +00:00
maxv
187c068ea2 When IP6_EXTHDR_GET fails, return ENOBUFS, and don't log an error (HDROPS
is not supposed to be used here).
2018-05-01 08:27:13 +00:00
maxv
84a7697bfe When the replay check fails, return EACCES instead of ENOBUFS. 2018-05-01 08:16:34 +00:00
maxv
82314dff45 Remove double include, opencrypto/xform.h is already included in
netipsec/xform.h.
2018-05-01 08:13:37 +00:00
maxv
c802aa1f31 Remove unused. 2018-05-01 08:08:46 +00:00
maxv
a7d0cc8839 Fix the checks in compute_ipsec_pos, otherwise m_copydata could crash. I
already fixed half of the problem two months ago in rev1.67, back then I
thought it was not triggerable because each packet we emit is guaranteed
to have correctly formed IPv6 options; but it is actually triggerable via
IPv6 forwarding, we emit a packet we just received, and we don't sanitize
its options before invoking IPsec.

Since it would be wrong to just stop the iteration and continue the IPsec
processing, allow compute_ipsec_pos to fail, and when it does, drop the
packet entirely.
2018-05-01 05:42:26 +00:00
maxv
417565290f Remove useless icmp6.h include, remove manual externs and include in6.h
to get proper definitions, and remove duplicate logic in
ipsec6_common_input_cb.
2018-04-29 14:54:09 +00:00
maxv
fa4d17703e Remove obsolete/dead code, the IP-in-IP encapsulation doesn't work this
way anymore (XF_IP4 partly dropped by FAST_IPSEC).
2018-04-29 14:35:35 +00:00
maxv
d920327205 Remove unused and misleading argument from ipsec_set_policy. 2018-04-29 11:51:08 +00:00
maxv
dc72a4cb13 Remove duplicate prototype. 2018-04-29 07:24:38 +00:00
maxv
68df48a8f0 Remove IPSEC_SPLASSERT_SOFTNET, it has always been a no-op. 2018-04-28 15:45:16 +00:00
maxv
f193022c1a Inline M_EXT_WRITABLE directly, and remove the XXX, there's nothing wrong
in the use of !M_READONLY.
2018-04-28 14:39:34 +00:00
maxv
c075b705a8 Move the ipsec6_input prototype into ipsec6.h, and style. 2018-04-28 14:25:56 +00:00
maxv
19f9cae6f8 Stop using a macro, rename the function to ipsec_init_pcbpolicy directly. 2018-04-28 14:21:03 +00:00
maxv
6f320ea66c Style and remove unused stuff. 2018-04-28 14:01:50 +00:00
maxv
ab18d0d461 Fix the net.inet6.ipsec6.def_policy node, the variable should be
&ip6_def_policy.policy, otherwise we're overwriting other fields of the
structure.
2018-04-28 13:44:19 +00:00
maxv
2f2fea5f0e Remove unused macros. 2018-04-28 13:23:17 +00:00
knakahara
b0c61d654b Fix LOCKDEBUG kernel panic when many(about 200) tunnel interfaces is created.
The tunnel interfaces are gif(4), l2tp(4), and ipsecif(4). They use mutex
itself in percpu area. When percpu_cpu_enlarge() run, the address of the
mutex in percpu area becomes different from the address which lockdebug
saved. That can cause "already initialized" false detection.
2018-04-27 09:55:27 +00:00
maxv
eee3723d53 Stop using m_copy(), use m_copym() directly. m_copy is useless,
undocumented and confusing.
2018-04-26 19:50:09 +00:00
maxv
3e02f4fab8 Remove the M_AUTHIPDGM flag. It is equivalent to M_AUTHIPHDR, both
are set in IPsec-AH, and they are always handled together.
2018-04-24 08:07:05 +00:00
maxv
64956f36ba Remove the kernel RH0 code. RH0 is deprecated by RFC5095, for security
reasons. RH0 was already removed in the kernel's input path, but some
parts were still present in the output path: they are now removed.

Sent on tech-net@ a few days ago.
2018-04-23 07:22:54 +00:00
maxv
48c5ec192c Rename ipip_allow->ipip_spoofcheck, and add net.inet.ipsec.ipip_spoofcheck.
Makes it simpler, and also fixes PR/39919.
2018-04-22 10:25:40 +00:00
christos
87fd18f8e5 s/static inline/static __inline/g for consistency. 2018-04-19 21:50:06 +00:00
maxv
e2c8a66404 Remove extra long file paths from the headers. 2018-04-19 08:27:38 +00:00
maxv
bad5f5991a Remove unused typedef, remove unused arguments from _ipip_input, sync
comment with reality, and change panic message.
2018-04-19 08:16:44 +00:00
maxv
96cdd49d30 Add a KASSERT (which is not triggerable since ipsec_common_input already
ensures 8 bytes are present), add an XXX (about the fact that it is
better to use m_copydata, because it is faster and less error-prone), and
improve two m_copybacks (remove useless casts).
2018-04-19 07:58:26 +00:00
maxv
ebd3d1799e Style, and remove meaningless XXX. 2018-04-19 07:36:23 +00:00
maxv
1656a2f332 cosmetic 2018-04-19 07:22:29 +00:00
maxv
6d238c84fb Simplify the IPv4 parser. Get the option length in 'optlen', and sanitize
it earlier. A new check is added (off + optlen > skip).

In the IPv6 parser we reuse 'optlen', and remove 'ad' as a result.
2018-04-18 17:58:07 +00:00
maxv
0ad30c0f1d Remove unused includes, remove misleading comments, and style. 2018-04-18 17:34:54 +00:00
maxv
e294edec3f Remove unused malloc.h include. 2018-04-18 07:38:02 +00:00
maxv
66077ec001 Style, and remove unused MALLOC_DECLARE. 2018-04-18 07:32:44 +00:00
maxv
4fbb7668fe Remove dead code.
ok ozaki-r@
2018-04-18 06:57:39 +00:00
maxv
1e45b2f110 style 2018-04-18 06:43:10 +00:00
maxv
c667608838 Style, and remove another misleading comment. 2018-04-18 06:22:47 +00:00
maxv
1da467ec29 Remove misleading comments. 2018-04-18 06:17:43 +00:00
maxv
7f6566cd4e Remove the
net.inet6.esp6
	net.inet6.ipcomp6
	net.inet6.ah6

subtrees. They are aliases to net.inet6.ipsec6, but they are not
consistent with the original intended naming. (eg there was
net.inet6.esp6.esp_trans_deflev instead of net.inet6.esp6.trans_deflev).
2018-04-18 06:13:23 +00:00
maxv
1a0e0fc19a Remove duplicate sysctls:
net.inet.esp.trans_deflev = net.inet.ipsec.esp_trans_deflev
	net.inet.esp.net_deflev   = net.inet.ipsec.esp_net_deflev
	net.inet.ah.cleartos      = net.inet.ipsec.ah_cleartos
	net.inet.ah.offsetmask    = net.inet.ipsec.ah_offsetmask
	net.inet.ah.trans_deflev  = net.inet.ipsec.ah_trans_deflev
	net.inet.ah.net_deflev    = net.inet.ipsec.ah_net_deflev

Use the convention on the right. Discussed a month ago on tech-net@.
2018-04-18 06:03:36 +00:00
maxv
3233810064 fix comments 2018-04-17 17:56:08 +00:00
maxv
55d4a29846 Add XXX. If this code really does something, it should use MCHTYPE. 2018-04-17 17:47:05 +00:00
maxv
25be83d544 Style, add XXX (about the mtu that goes negative), and remove #ifdef inet. 2018-04-17 17:40:38 +00:00
maxv
03bb22c3ac Fix a pretty bad mistake, that has always been there.
m_adj(m1, -(m1->m_len - roff));
		if (m1 != m)
			m->m_pkthdr.len -= (m1->m_len - roff);

This is wrong: m_adj will modify m1->m_len, so we're using a wrong value
when manually adjusting m->m_pkthdr.len.

Because of that, it is possible to exploit the attack I described in
uipc_mbuf.c::rev1.182. The exploit is more complicated, but works 100%
reliably.
2018-04-17 09:06:33 +00:00
maxv
cc059e555f Don't assume M_PKTHDR is set only on the first mbuf of the chain. It
should, but it looks like there are several places that can put M_PKTHDR
on secondary mbufs (PR/53189), so drop this assumption right now to
prevent further bugs.

The check is replaced by (m1 != m), which is equivalent to the previous
code: we want to modify m->m_pkthdr.len only when 'm' was not passed in
m_adj().
2018-04-17 06:23:30 +00:00
yamaguchi
a83f891441 Fix panic of SADB when the state of sav is changed in timeout
pointed out by ozaki-r@n.o, thanks
2018-04-17 04:22:58 +00:00
maxv
2f7eef4b1d Remove dead code.
ok ozaki-r@
2018-04-16 17:32:34 +00:00
yamaguchi
c149db4f9c Added a lookup table to find an sav quickly
key_sad.sahlists doesn't work well for inbound packets because
its key includes source address. For the reason, the
look-up-table for the inbound packets is newly added.
The table has all sav whose state is MATURE or DYING and uses a
key calculated by destination address, protocol, and spi instead
of saidx.

reviewd ozaki-r@n.o, thanks.
2018-04-16 08:56:08 +00:00
yamaguchi
9e7eb12d9d Introduced a hash table to sahlist
An saidx of sah included in the list is unique so that
the search can use a hash list whose hash is calculated by
the saidx to find an sah quickly.
The hash list of the sahlits is used in FreeBSD, too.

reviewed by ozaki-r@n.o, thanks.
2018-04-16 08:52:09 +00:00
maxv
f75d3c0566 Introduce a m_verify_packet function, that verifies the mbuf chain of a
packet to ensure it is not malformed. Call this function in "points of
interest", that are the IPv4/IPv6/IPsec entry points. There could be more.

We use M_VERIFY_PACKET(m), declared under DIAGNOSTIC only.

This function should not be called everywhere, especially not in places
that temporarily manipulate (and clobber) the mbuf structure; once they're
done they put the mbuf back in a correct format.
2018-04-15 07:35:49 +00:00
maxv
10581564f8 Remove duplicate, to better show that this place doesn't make a lot of
sense. The code should probably be removed, it's a leftover from when we
had #ifdef __FreeBSD__.
2018-04-13 09:34:20 +00:00
yamaguchi
18cffba26c Removed the unnecessary order check of key_lookup_sa
key_prefered_oldsa flag can change the sa to use if an sah
has multiple sav. However the multiple saves whose protocol
is ah, esp, or tcp cannot exist because their duplications
are checked by the spi value. Although the multiple saves
can exist in the case of ipcomp, the values using in the
post processing are same between the saves.

For those reasons, it is no need to select an sav by its
lifetime.
In addition, FreeBSD has already remove this.

reviewed by ozaki-r@n.o, thanks.
2018-04-09 06:26:05 +00:00
knakahara
d83c566ff6 Fix unexpected failure when ipsecif(4) over IPv6 is changed port number only.
Here is an example of the operation which causes this problem.
    # ifconfig ipsec0 create link0
    # ifconfig ipsec0 tunnel fc00:1001::2,4500 fc00:1001::1,4501
    # ifconfig ipsec0 tunnel fc00:1001::2,4500 fc00:1001::1,4502
2018-04-06 10:38:53 +00:00
knakahara
7c378391ba Add IPv4 ID when the ipsecif(4) packet can be fragmented. Implemented by hsuenaga@IIJ and ohishi@IIJ, thanks.
This modification reduces packet loss of fragmented packets on a
network where reordering occurs.

Alghough this modification has been applied, IPv4 ID is not set for
the packet smaller then IP_MINFRAGSIZE. According to RFC 6864, that
must not cause problems.

XXX pullup-8
2018-04-06 10:31:35 +00:00
maxv
d40270d9c9 Remove ipsec_copy_policy and ipsec_copy_pcbpolicy. No functional change,
since we used only ipsec_copy_pcbpolicy, and it was a no-op.

Originally we were using ipsec_copy_policy to optimize the IPsec-PCB
cache: when an ACK was received in response to a SYN, we used to copy the
SP cached in the SYN's PCB into the ACK's PCB, so that
ipsec_getpolicybysock could use the cached SP instead of requerying it.

Then we switched to ipsec_copy_pcbpolicy which has always been a no-op. As
a result the SP cached in the SYN was/is not copied in the ACK, and the
first call to ipsec_getpolicybysock had to query the SP and cache it
itself. It's not totally clear to me why this change was made.

But it has been this way for years, and after a conversation with Ryota
Ozaki it turns out the optimization is not valid anymore due to
MP-ification, so it won't be re-enabled.

ok ozaki-r@
2018-04-03 09:03:59 +00:00
maxv
65b46b60ed typo in comments 2018-03-31 19:27:14 +00:00
roy
320ba6e2bf socket: report receive buffer overflows
Add soroverflow() which increments the overflow counter, sets so_error
to ENOBUFS and wakes the receive socket up.
Replace all code that manually increments this counter with soroverflow().
Add soroverflow() to raw_input().

This allows userland to detect route(4) overflows so it can re-sync
with the current state.
2018-03-19 16:26:25 +00:00
knakahara
e7acdb682b comment out confusing (and incorrect) code and add comment. Pointed out by maxv@n.o, thanks. 2018-03-13 03:05:12 +00:00
maxv
7ddcc8beaa Add KASSERTs. 2018-03-10 17:52:50 +00:00
maxv
00c01d2fd1 Fix the computation. Normally that's harmless since ip6_output recomputes
ip6_plen.
2018-03-10 17:48:32 +00:00
knakahara
400c191493 Fix ipsec(4) I/F esp_frag support. 2018-03-09 11:05:21 +00:00
knakahara
4fda12c619 Fix fragment processing in ipsec4_fragout(). Pointed out by maxv@n.o, thanks.
XXX need pullup-8
2018-03-06 10:07:06 +00:00
maxv
f2110e8589 Improve stupid check, style, and fix leak (m, not m0). 2018-03-05 12:42:28 +00:00
maxv
052a8431bf Call m_pullup earlier, fixes one branch. 2018-03-05 11:50:25 +00:00
maxv
52489f2bdd Reduce the diff between ipsec4_output and ipsec6_check_policy. While here
style.
2018-03-03 09:54:55 +00:00
maxv
d219e3b4ad Dedup. 2018-03-03 09:47:01 +00:00
maxv
6c5d24ed60 Add KASSERTs, we don't want m_nextpkt in ipsec{4/6}_process_packet. 2018-03-03 09:39:29 +00:00
ozaki-r
c17f87d4e1 Avoid data races on lifetime counters by using percpu(9)
We don't make them percpu(9) directly because the structure is exposed to
userland and we don't want to break ABI.  So we add another member variable
for percpu(9) and use it internally.  When we export them to userland, they
are converted to the original format.
2018-03-02 07:37:13 +00:00
maxv
f3215192fd add missing static 2018-02-28 11:29:14 +00:00
maxv
d81439a881 Remove unused macros, and while here style. 2018-02-28 11:19:49 +00:00
maxv
e9a0140794 Remove duplicate IPSEC_STATINC(IPSEC_STAT_IN_POLVIO), ipsec_in_reject
already increases it. IPSEC6_STATINC is now unused, so remove it too.
2018-02-28 11:09:03 +00:00
maxv
247966d592 Dedup: merge ipsec4_setspidx_inpcb and ipsec6_setspidx_in6pcb. 2018-02-28 10:16:19 +00:00
maxv
f931621816 ipsec6_setspidx_in6pcb: call ipsec_setspidx() only once, just like the
IPv4 code. While here put the correct variable in sizeof.

ok ozaki-r@
2018-02-28 10:09:17 +00:00
maxv
faad9849ec Dedup: merge ipsec4_set_policy and ipsec6_set_policy. The content of the
original ipsec_set_policy function is inlined into the new one.
2018-02-27 15:01:30 +00:00
maxv
6fd7a9a762 Remove duplicate checks, and no need to initialize 'newsp' in
ipsec_set_policy.
2018-02-27 14:52:51 +00:00
maxv
580ac4be42 Dedup: merge
ipsec4_get_policy and ipsec6_get_policy
	ipsec4_delete_pcbpolicy and ipsec6_delete_pcbpolicy

The already-existing ipsec_get_policy() function is inlined in the new
one.
2018-02-27 14:44:10 +00:00
maxv
be32dea43c Use inpcb_hdr to reduce the diff between
ipsec4_set_policy and ipsec6_set_policy
	ipsec4_get_policy and ipsec6_get_policy
	ipsec4_delete_pcbpolicy and ipsec6_delete_pcbpolicy

No real functional change.
2018-02-27 13:36:21 +00:00
maxv
215e1b4181 Optimize: use ipsec_sp_hdrsiz instead of ipsec_hdrsiz, not to re-query
the SP.

ok ozaki-r@
2018-02-27 08:05:19 +00:00
maxv
0695fe90b1 Dedup: call ipsec_in_reject directly. IPSEC_STAT_IN_POLVIO also gets
increased now.
2018-02-26 10:36:24 +00:00
maxv
768c43840b Reduce the diff between ipsec6_input and ipsec4_input. 2018-02-26 10:19:13 +00:00