From owner-dev-commits-src-main@freebsd.org Fri Aug 13 07:23:30 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 25F26651942; Fri, 13 Aug 2021 07:23:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4GmFQp0XZxz4vsN; Fri, 13 Aug 2021 07:23:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id EEEFE1BCF; Fri, 13 Aug 2021 07:23:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 17D7NTES066263; Fri, 13 Aug 2021 07:23:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 17D7NTuB066262; Fri, 13 Aug 2021 07:23:29 GMT (envelope-from git) Date: Fri, 13 Aug 2021 07:23:29 GMT Message-Id: <202108130723.17D7NTuB066262@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Wojciech Macek Subject: git: 6b66194bcb7e - main - ipsec: Check PMTU before sending a frame. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: wma X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6b66194bcb7e43ef40b11005618544081c6e30ea Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 Aug 2021 07:23:30 -0000 The branch main has been updated by wma: URL: https://cgit.FreeBSD.org/src/commit/?id=6b66194bcb7e43ef40b11005618544081c6e30ea commit 6b66194bcb7e43ef40b11005618544081c6e30ea Author: Kornel Duleba AuthorDate: 2021-08-13 07:20:46 +0000 Commit: Wojciech Macek CommitDate: 2021-08-13 07:22:24 +0000 ipsec: Check PMTU before sending a frame. If an encapsulated frame is going to have DF bit set check its desitnitions' PMTU and if it won't fit drop it and: Generate ICMP 3/4 message if the packet was to be forwarded. Return EMSGSIZE error otherwise. Obtained from: Semihalf Sponsored by: Stormshield Differential revision: https://reviews.freebsd.org/D30993 --- sys/netipsec/ipsec.c | 2 +- sys/netipsec/ipsec.h | 2 ++ sys/netipsec/ipsec_output.c | 87 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/sys/netipsec/ipsec.c b/sys/netipsec/ipsec.c index dcc607a5b617..07eb67641f7b 100644 --- a/sys/netipsec/ipsec.c +++ b/sys/netipsec/ipsec.c @@ -1093,7 +1093,7 @@ ipsec_in_reject(struct secpolicy *sp, struct inpcb *inp, const struct mbuf *m) * Compute the byte size to be occupied by IPsec header. * In case it is tunnelled, it includes the size of outer IP header. */ -static size_t +size_t ipsec_hdrsiz_internal(struct secpolicy *sp) { size_t size; diff --git a/sys/netipsec/ipsec.h b/sys/netipsec/ipsec.h index 1f5aa1a43edc..85360f9b737c 100644 --- a/sys/netipsec/ipsec.h +++ b/sys/netipsec/ipsec.h @@ -332,6 +332,7 @@ int ipsec_chkreplay(uint32_t, uint32_t *, struct secasvar *); int ipsec_updatereplay(uint32_t, struct secasvar *); int ipsec_updateid(struct secasvar *, crypto_session_t *, crypto_session_t *); int ipsec_initialized(void); +size_t ipsec_hdrsiz_internal(struct secpolicy *); void ipsec_setspidx_inpcb(struct inpcb *, struct secpolicyindex *, u_int); @@ -345,6 +346,7 @@ int ipsec4_output(struct mbuf *, struct inpcb *); int ipsec4_capability(struct mbuf *, u_int); int ipsec4_common_input_cb(struct mbuf *, struct secasvar *, int, int); int ipsec4_ctlinput(int, struct sockaddr *, void *); +int ipsec4_check_pmtu(struct mbuf *, struct secpolicy *, int); int ipsec4_process_packet(struct mbuf *, struct secpolicy *, struct inpcb *); int ipsec_process_done(struct mbuf *, struct secpolicy *, struct secasvar *, u_int); diff --git a/sys/netipsec/ipsec_output.c b/sys/netipsec/ipsec_output.c index 86f06fd10947..a817b67fd93e 100644 --- a/sys/netipsec/ipsec_output.c +++ b/sys/netipsec/ipsec_output.c @@ -61,6 +61,8 @@ #ifdef INET6 #include #endif +#include +#include #include #ifdef INET6 @@ -292,6 +294,83 @@ ipsec4_process_packet(struct mbuf *m, struct secpolicy *sp, return (ipsec4_perform_request(m, sp, inp, 0)); } +int +ipsec4_check_pmtu(struct mbuf *m, struct secpolicy *sp, int forwarding) +{ + union sockaddr_union *dst; + struct in_conninfo inc; + struct secasvar *sav; + struct ip *ip; + size_t hlen, pmtu; + uint32_t idx; + int error; + + + /* Don't check PMTU if the frame won't have DF bit set. */ + if (!V_ip4_ipsec_dfbit) + return (0); + if (V_ip4_ipsec_dfbit == 1) + goto setdf; + + /* V_ip4_ipsec_dfbit > 1 - we will copy it from inner header. */ + ip = mtod(m, struct ip *); + if (!(ip->ip_off & htons(IP_DF))) + return (0); + +setdf: + idx = sp->tcount - 1; + sav = ipsec4_allocsa(m, sp, &idx, &error); + if (sav == NULL) { + key_freesp(&sp); + if (error != EJUSTRETURN) + m_freem(m); + + return (error); + } + + dst = &sav->sah->saidx.dst; + + /* Final header is not ipv4. */ + if (dst->sa.sa_family != AF_INET) { + key_freesav(&sav); + return (0); + } + + memset(&inc, 0, sizeof(inc)); + inc.inc_faddr = satosin(&dst->sa)->sin_addr; + key_freesav(&sav); + pmtu = tcp_hc_getmtu(&inc); + /* No entry in hostcache. */ + if (pmtu == 0) + return (0); + + hlen = ipsec_hdrsiz_internal(sp); + if (m_length(m, NULL) + hlen > pmtu) { + /* + * If we're forwarding generate ICMP message here, + * so that it contains pmtu and not link mtu. + * Set error to EINPROGRESS, in order for the frame + * to be dropped silently. + */ + if (forwarding) { + if (pmtu > hlen) + icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, + 0, pmtu - hlen); + else + m_freem(m); + + key_freesp(&sp); + return (EINPROGRESS); /* Pretend that we consumed it. */ + } else { + m_freem(m); + key_freesp(&sp); + return (EMSGSIZE); + } + } + + return (0); +} + static int ipsec4_common_output(struct mbuf *m, struct inpcb *inp, int forwarding) { @@ -349,6 +428,14 @@ ipsec4_common_output(struct mbuf *m, struct inpcb *inp, int forwarding) #endif } /* NB: callee frees mbuf and releases reference to SP */ + error = ipsec4_check_pmtu(m, sp, forwarding); + if (error != 0) { + if (error == EJUSTRETURN) + return (0); + + return (error); + } + error = ipsec4_process_packet(m, sp, inp); if (error == EJUSTRETURN) { /*