From nobody Thu Nov 12 20:20:41 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jQWwn2Jdsz71Byj for ; Thu, 12 Nov 2026 20:20:41 +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) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jQWwn1x5kz3JJf for ; Thu, 12 Nov 2026 20:20:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794514841; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mlmnPZ7nFiXGJi6Sqb9hWkbELGQIyOrAjkq0UNT6iDk=; b=P8gfy8B6JwFbmOyPnqpFLCyNOjos2wV9f2vnXljt41heLJsHXPObajqLY1FlUarGygBrnx /7Kn/OSnrxmeXFj7YbIRyvjownOtBG2erRlpl/+AeN5JGkFkXLour07ujZJgutm9jSmSmd 3j/Uu4QzzyTrToFB2guj7dObd31iAYHxfD9u7Lm/NPfQc3Q9IxGjARPcpLhGZz5nOkZHxs p5KlOa3odlEFDsYFmpL80+QRsK7yuC4BtR3DSqli7bys/eak1595+fMrSCZ+aMnizs/AYQ f5CFJYCh48ZSG+G6/A/z8W9+GbHPxirdyFr9OO6mRpopTyzJnP9vsk55GEtmiw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794514841; b=O4V1cHENL+7EBylj9qppb2WT5zYrZmqfVuqliTVUjBWuPTaDuP++er2M1ju8iBzfk9ZwXJ g/O5rW1Pqjxh34kjjRZibUZ4EVwHWzdrg0owdQQBERof4mXt5YsO388Xd1Ns3/+PR4Lh8l aPnk0wHvqEpVrBCR/gPuCbHfdkdTBmfjHpHsyLhZz/yynW6tOZt4OnokLosFv9RZzeMOQB d09b7YopU+3tTWPAf+1Cq2H0/MHGjCX7IzIfzHFfWY4V715gKNY3YU8CC7DoNrnIH77Vex b6EUs+lQ5FivaKC/YzXsbZuiyRhpzjHtj+f3fSvguaF5H+C2q0nH60Dp+M4jxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794514841; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mlmnPZ7nFiXGJi6Sqb9hWkbELGQIyOrAjkq0UNT6iDk=; b=TGoM4AnCVyiSJpi8eTZ/navDXkNaWS43w7lr8UPOP+wHfp5M+8iDTS0JobdEH7H2yOZcS9 pSa+lBRxDJU0Sol/A6JL0H0EmZUP98x/dH0dAWEKmHUV0VFMRsyiVKvCuvjwp4buf9dOVq 0zxuc2rZV86SAuwfuA/X7WegZEybOnP62CavAAETgd0pJW36sL/wUkjG33Z03qIicfm1iu Gm3dShcOtm5Gl1CmpwMe+av48vbx8s+zexQmOitp2Tmloh7GLb19CAVDtrLQXsz1pPtOMU BjHRQ8LoOVdWlvvmQ5IHoM+w9UNSeMK4vweSP+GjuUpAKyzgyZyAxshNivxEnA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jQWwn1bcJz1H7x for ; Thu, 12 Nov 2026 20:20:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1a1d5 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 12 Nov 2026 20:20:41 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Pouria Mousavizadeh Tehrani Subject: git: 1555dff3bf56 - main - gre(4): Add full support for gre key List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pouria X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1555dff3bf5619278c80ba843555ac2c84d671c3 Auto-Submitted: auto-generated Date: Thu, 12 Nov 2026 20:20:41 +0000 Message-Id: <6af61f99.1a1d5.138c7815@gitrepo.freebsd.org> The branch main has been updated by pouria: URL: https://cgit.FreeBSD.org/src/commit/?id=1555dff3bf5619278c80ba843555ac2c84d671c3 commit 1555dff3bf5619278c80ba843555ac2c84d671c3 Author: Pouria Mousavizadeh Tehrani AuthorDate: 2026-11-12 19:33:41 +0000 Commit: Pouria Mousavizadeh Tehrani CommitDate: 2026-11-12 20:09:22 +0000 gre(4): Add full support for gre key Allow multiple gre tunnels between the same source and destination addresses, distinguished by their incoming gre key. Add greikey and greokey to ifconfig(8) to set the incoming and outgoing keys separately and grekey now sets both. Add a net.link.gre.key_fallback sysctl for backward compatibility and set it to true by default. Also write multikey tests for both IPv4 and IPv6 families. Relnotes: yes Reviewed by: adrian Discussed with: zlei Differential Revision: https://reviews.freebsd.org/D59849 --- sbin/ifconfig/ifconfig.8 | 12 +- sbin/ifconfig/ifgre.c | 87 +++++++- share/man/man4/gre.4 | 25 ++- sys/net/if_gre.c | 110 +++++++--- sys/net/if_gre.h | 10 +- sys/netinet/ip_gre.c | 122 ++++++++--- sys/netinet6/ip6_gre.c | 124 +++++++++--- sys/netlink/route/interface.h | 1 + tests/sys/net/if_gre.sh | 458 +++++++++++++++++++++++++++++++++++++++++- 9 files changed, 839 insertions(+), 110 deletions(-) diff --git a/sbin/ifconfig/ifconfig.8 b/sbin/ifconfig/ifconfig.8 index 5474655a4ab1..0ef8f336821d 100644 --- a/sbin/ifconfig/ifconfig.8 +++ b/sbin/ifconfig/ifconfig.8 @@ -28,7 +28,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd September 10, 2026 +.Dd November 12, 2026 .Dt IFCONFIG 8 .Os .Sh NAME @@ -3099,10 +3099,16 @@ Another name for the .Fl tunnel parameter. .It Cm grekey Ar key +Configure the GRE key to be used for both incoming and outgoing packets. +This is equivalent to setting both +.Cm greokey +and +.Cm greikey . +.It Cm greokey Ar key Configure the GRE key to be used for outgoing packets. -Note that -.Xr gre 4 will always accept GRE packets with invalid or absent keys. This command will result in a four byte MTU reduction on the interface. +.It Cm greikey Ar key +Configure the GRE key to be used for incoming packets. .El .Ss Packet Filter State Table Sychronisation Parameters The following parameters are specific to diff --git a/sbin/ifconfig/ifgre.c b/sbin/ifconfig/ifgre.c index c704aa66b275..bb8633966e70 100644 --- a/sbin/ifconfig/ifgre.c +++ b/sbin/ifconfig/ifgre.c @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -55,6 +56,7 @@ static const char *GREBITS[] = { struct nl_parsed_gre { uint32_t ifla_flags; uint32_t ifla_okey; + uint32_t ifla_ikey; uint32_t ifla_encap_type; uint16_t ifla_encap_sport; }; @@ -92,7 +94,8 @@ gre_nl_fini(if_ctx *ctx, struct snl_writer *nw) err(1, "unable to send netlink message"); if (!snl_read_reply_code(ctx->io_ss, hdr->nlmsg_seq, &errmsg)) - errx(errmsg.error, "%s", errmsg.error_str); + errx(errmsg.error, "%s", errmsg.error_str != NULL ? + errmsg.error_str : strerror(errmsg.error)); } #define _OUT(_field) offsetof(struct nl_parsed_gre, _field) @@ -103,6 +106,7 @@ static const struct snl_attr_parser nla_p_gre[] = { .cb = snl_attr_get_uint32 }, { .type = IFLA_GRE_ENCAP_SPORT, .off = _OUT(ifla_encap_sport), .cb = snl_attr_get_uint16 }, + { .type = IFLA_GRE_IKEY, .off = _OUT(ifla_ikey), .cb = snl_attr_get_uint32 }, }; #undef _OUT SNL_DECLARE_ATTR_PARSER(gre_linkinfo_data_parser, nla_p_gre); @@ -173,8 +177,11 @@ gre_status_nl(if_ctx *ctx) struct nl_parsed_gre gre_data = gre_info.data; if (gre_data.ifla_okey != 0) - printf("\tgrekey: 0x%x (%u)\n", + printf("\tgreokey: 0x%x (%u)\n", gre_data.ifla_okey, gre_data.ifla_okey); + if (gre_data.ifla_ikey != 0) + printf("\tgreikey: 0x%x (%u)\n", + gre_data.ifla_ikey, gre_data.ifla_ikey); if (gre_data.ifla_flags == 0) return; @@ -200,6 +207,47 @@ setifgrekey_nl(if_ctx *ctx, const char *val, int dummy __unused) off2 = snl_add_msg_attr_nested(&nw, IFLA_INFO_DATA); snl_add_msg_attr_u32(&nw, IFLA_GRE_OKEY, grekey); + snl_add_msg_attr_u32(&nw, IFLA_GRE_IKEY, grekey); + + snl_end_attr_nested(&nw, off2); + snl_end_attr_nested(&nw, off); + + gre_nl_fini(ctx, &nw); +} + +static void +setifgreokey_nl(if_ctx *ctx, const char *val, int dummy __unused) +{ + struct snl_writer nw = {}; + int off, off2; + uint32_t grekey = strtol(val, NULL, 0); + + gre_nl_init(ctx, &nw, 0); + off = snl_add_msg_attr_nested(&nw, IFLA_LINKINFO); + snl_add_msg_attr_string(&nw, IFLA_INFO_KIND, "gre"); + + off2 = snl_add_msg_attr_nested(&nw, IFLA_INFO_DATA); + snl_add_msg_attr_u32(&nw, IFLA_GRE_OKEY, grekey); + + snl_end_attr_nested(&nw, off2); + snl_end_attr_nested(&nw, off); + + gre_nl_fini(ctx, &nw); +} + +static void +setifgreikey_nl(if_ctx *ctx, const char *val, int dummy __unused) +{ + struct snl_writer nw = {}; + int off, off2; + uint32_t grekey = strtol(val, NULL, 0); + + gre_nl_init(ctx, &nw, 0); + off = snl_add_msg_attr_nested(&nw, IFLA_LINKINFO); + snl_add_msg_attr_string(&nw, IFLA_INFO_KIND, "gre"); + + off2 = snl_add_msg_attr_nested(&nw, IFLA_INFO_DATA); + snl_add_msg_attr_u32(&nw, IFLA_GRE_IKEY, grekey); snl_end_attr_nested(&nw, off2); snl_end_attr_nested(&nw, off); @@ -302,6 +350,8 @@ setifgretype_nl(if_ctx *ctx, const char *val __unused, int d) static struct cmd gre_cmds[] = { DEF_CMD_ARG("grekey", setifgrekey_nl), + DEF_CMD_ARG("greokey", setifgreokey_nl), + DEF_CMD_ARG("greikey", setifgreikey_nl), DEF_CMD_ARG("udpport", setifgreport_nl), DEF_CMD("enable_csum", GRE_ENABLE_CSUM, setifgreopts_nl), DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts_nl), @@ -320,7 +370,11 @@ gre_status(if_ctx *ctx) if (ioctl_ctx_ifr(ctx, GREGKEY, &ifr) == 0) if (opts != 0) - printf("\tgrekey: 0x%x (%u)\n", opts, opts); + printf("\tgreokey: 0x%x (%u)\n", opts, opts); + opts = 0; + if (ioctl_ctx_ifr(ctx, GREGIKEY, &ifr) == 0) + if (opts != 0) + printf("\tgreikey: 0x%x (%u)\n", opts, opts); opts = 0; if (ioctl_ctx_ifr(ctx, GREGOPTS, &ifr) != 0 || opts == 0) return; @@ -340,9 +394,32 @@ setifgrekey(if_ctx *ctx, const char *val, int dummy __unused) uint32_t grekey = strtol(val, NULL, 0); struct ifreq ifr = { .ifr_data = (caddr_t)&grekey }; + /* Kernels without GRESIKEY return EINVAL, only set the outgoing key. */ + if (ioctl_ctx_ifr(ctx, GRESIKEY, &ifr) < 0 && errno != EINVAL) + err(1, "ioctl (set greikey)"); + if (ioctl_ctx_ifr(ctx, GRESKEY, &ifr) < 0) + warn("ioctl (set greokey)"); +} + +static void +setifgreokey(if_ctx *ctx, const char *val, int dummy __unused) +{ + uint32_t grekey = strtol(val, NULL, 0); + struct ifreq ifr = { .ifr_data = (caddr_t)&grekey }; + ifr.ifr_data = (caddr_t)&grekey; if (ioctl_ctx_ifr(ctx, GRESKEY, &ifr) < 0) - warn("ioctl (set grekey)"); + warn("ioctl (set greokey)"); +} + +static void +setifgreikey(if_ctx *ctx, const char *val, int dummy __unused) +{ + uint32_t grekey = strtol(val, NULL, 0); + struct ifreq ifr = { .ifr_data = (caddr_t)&grekey }; + + if (ioctl_ctx_ifr(ctx, GRESIKEY, &ifr) < 0) + err(1, "ioctl (set greikey)"); } static void @@ -380,6 +457,8 @@ setifgreopts(if_ctx *ctx, const char *val __unused, int d) static struct cmd gre_cmds[] = { DEF_CMD_ARG("grekey", setifgrekey), + DEF_CMD_ARG("greokey", setifgreokey), + DEF_CMD_ARG("greikey", setifgreikey), DEF_CMD_ARG("udpport", setifgreport), DEF_CMD("enable_csum", GRE_ENABLE_CSUM, setifgreopts), DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts), diff --git a/share/man/man4/gre.4 b/share/man/man4/gre.4 index 7f46e54e1207..a23e1d1e000c 100644 --- a/share/man/man4/gre.4 +++ b/share/man/man4/gre.4 @@ -27,7 +27,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd August 21, 2020 +.Dd November 12, 2026 .Dt GRE 4 .Os .Sh NAME @@ -81,8 +81,24 @@ interfaces support a number of additional parameters to the .Xr ifconfig 8 : .Bl -tag -width "enable_csum" .It Ar grekey +Set the GRE key for both incoming and outgoing packets. +.It Ar greokey Set the GRE key used for outgoing packets. A value of 0 disables the key option. +.It Ar greikey +The incoming key is used to choose the interface for incoming packets. +This allows configuring several tunnels between the same pair of addresses, +each tunnel must have a unique incoming key. +An incoming packet is accepted by the interface with same key, +otherwise if the +.Xr sysctl 8 +variable +.Va net.link.gre.key_fallback +is set to true, the packet is accepted by the interface +without a key that is configured with the same addresses. +If there is no such interface either, but only one interface is configured +with these addresses, it accepts the packet whatever its key is. +A value of 0 disables the key option. .It Ar enable_csum Enables checksum calculation for outgoing packets. .It Ar enable_seq @@ -268,8 +284,7 @@ to the desired level of nesting. .An Andrey V. Elsukov Aq Mt ae@FreeBSD.org .An Heiko W.Rupp Aq Mt hwr@pilhuhn.de .Sh BUGS -The current implementation uses the key only for outgoing packets. -Incoming packets with a different key or without a key will be treated as if they -would belong to this interface. +The current implementation cannot use the key 0 for outgoing packets. +Incoming packets with the key 0 are also ignored. .Pp -The sequence number field also used only for outgoing packets. +The sequence number field is used only for outgoing packets. diff --git a/sys/net/if_gre.c b/sys/net/if_gre.c index 971d7e01be0f..5e6ea6517654 100644 --- a/sys/net/if_gre.c +++ b/sys/net/if_gre.c @@ -129,12 +129,12 @@ static int gre_set_addr_nl(struct gre_softc *, struct nl_pstate *, struct sockaddr *, struct sockaddr *); static int gre_set_flags(struct gre_softc *, uint32_t); -static int gre_set_key(struct gre_softc *, uint32_t); +static int gre_set_key(struct gre_softc *, uint32_t, bool); static int gre_set_udp_sport(struct gre_softc *, uint16_t); static int gre_setopts(struct gre_softc *, u_long, uint32_t); static int gre_set_flags_nl(struct gre_softc *, struct nl_pstate *, uint32_t); -static int gre_set_key_nl(struct gre_softc *, struct nl_pstate *, uint32_t); +static int gre_set_key_nl(struct gre_softc *, struct nl_pstate *, uint32_t, bool); static int gre_set_encap_nl(struct gre_softc *, struct nl_pstate *, uint32_t); static int gre_set_udp_sport_nl(struct gre_softc *, struct nl_pstate *, uint16_t); @@ -158,11 +158,17 @@ VNET_DEFINE_STATIC(int, max_gre_nesting) = MAX_GRE_NEST; SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET, &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels"); +VNET_DEFINE(bool, gre_key_fallback) = true; +SYSCTL_BOOL(_net_link_gre, OID_AUTO, key_fallback, CTLFLAG_RW | CTLFLAG_VNET, + &VNET_NAME(gre_key_fallback), 0, + "Accept packets with unmatched key on keyless tunnel or on the only tunnel between same addresses"); + struct nl_parsed_gre { struct sockaddr *ifla_local; struct sockaddr *ifla_remote; uint32_t ifla_flags; uint32_t ifla_okey; + uint32_t ifla_ikey; uint32_t ifla_encap_type; uint16_t ifla_encap_sport; }; @@ -175,6 +181,7 @@ static const struct nlattr_parser nla_p_gre[] = { { .type = IFLA_GRE_OKEY, .off = _OUT(ifla_okey), .cb = nlattr_get_uint32 }, { .type = IFLA_GRE_ENCAP_TYPE, .off = _OUT(ifla_encap_type), .cb = nlattr_get_uint32 }, { .type = IFLA_GRE_ENCAP_SPORT, .off = _OUT(ifla_encap_sport), .cb = nlattr_get_uint16 }, + { .type = IFLA_GRE_IKEY, .off = _OUT(ifla_ikey), .cb = nlattr_get_uint32 }, }; #undef _OUT NL_DECLARE_ATTR_PARSER(gre_modify_parser, nla_p_gre); @@ -271,6 +278,12 @@ gre_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd) if (sc == NULL) goto generic; + /* Set the ikey before addresses, it is a part of the duplicate check. */ + if (nl_has_attr(&bm, IFLA_GRE_IKEY)) + error = gre_set_key_nl(sc, npt, params.ifla_ikey, false); + if (error != 0) + goto generic; + if (nl_has_attr(&bm, IFLA_GRE_LOCAL) && nl_has_attr(&bm, IFLA_GRE_REMOTE)) error = gre_set_addr_nl(sc, npt, params.ifla_local, params.ifla_remote); else if (nl_has_attr(&bm, IFLA_GRE_LOCAL) || nl_has_attr(&bm, IFLA_GRE_REMOTE)) { @@ -282,7 +295,7 @@ gre_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd) error = gre_set_flags_nl(sc, npt, params.ifla_flags); if (error == 0 && nl_has_attr(&bm, IFLA_GRE_OKEY)) - error = gre_set_key_nl(sc, npt, params.ifla_okey); + error = gre_set_key_nl(sc, npt, params.ifla_okey, true); if (error == 0 && nl_has_attr(&bm, IFLA_GRE_ENCAP_TYPE)) error = gre_set_encap_nl(sc, npt, params.ifla_encap_type); @@ -346,10 +359,11 @@ gre_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw) } nlattr_add_u32(nw, IFLA_GRE_FLAGS, sc->gre_options); - nlattr_add_u32(nw, IFLA_GRE_OKEY, sc->gre_key); + nlattr_add_u32(nw, IFLA_GRE_OKEY, sc->gre_okey); nlattr_add_u32(nw, IFLA_GRE_ENCAP_TYPE, sc->gre_options & GRE_UDPENCAP ? IFLA_TUNNEL_GRE_UDP : IFLA_TUNNEL_NONE); nlattr_add_u16(nw, IFLA_GRE_ENCAP_SPORT, sc->gre_port); + nlattr_add_u32(nw, IFLA_GRE_IKEY, sc->gre_ikey); ret: nlattr_set_len(nw, off2); @@ -424,15 +438,21 @@ gre_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) } static int -gre_set_key(struct gre_softc *sc, uint32_t key) +gre_set_key(struct gre_softc *sc, uint32_t key, bool out) { int error = 0; GRE_LOCK_ASSERT(); - if (sc->gre_key == key) - return (0); - error = gre_setopts(sc, GRESKEY, key); + if (out) { + if (sc->gre_okey == key) + return (0); + error = gre_setopts(sc, GRESKEY, key); + } else { + if (sc->gre_ikey == key) + return (0); + error = gre_setopts(sc, GRESIKEY, key); + } return (error); } @@ -502,7 +522,9 @@ gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t opt) * We can just change any parameters. */ if (cmd == GRESKEY) - sc->gre_key = opt; + sc->gre_okey = opt; + if (cmd == GRESIKEY) + sc->gre_ikey = opt; if (cmd == GRESOPTS) sc->gre_options = opt; if (cmd == GRESPORT) @@ -584,6 +606,7 @@ gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) sc->gre_fibnum = ifr->ifr_fib; break; case GRESKEY: + case GRESIKEY: case GRESOPTS: case GRESPORT: if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0) @@ -592,15 +615,21 @@ gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) sizeof(opt))) != 0) break; if (cmd == GRESKEY) - error = gre_set_key(sc, opt); + error = gre_set_key(sc, opt, true); + else if (cmd == GRESIKEY) + error = gre_set_key(sc, opt, false); else if (cmd == GRESOPTS) error = gre_set_flags(sc, opt); else if (cmd == GRESPORT) error = gre_set_udp_sport(sc, opt); break; case GREGKEY: - error = copyout(&sc->gre_key, ifr_data_get_ptr(ifr), - sizeof(sc->gre_key)); + error = copyout(&sc->gre_okey, ifr_data_get_ptr(ifr), + sizeof(sc->gre_okey)); + break; + case GREGIKEY: + error = copyout(&sc->gre_ikey, ifr_data_get_ptr(ifr), + sizeof(sc->gre_ikey)); break; case GREGOPTS: error = copyout(&sc->gre_options, ifr_data_get_ptr(ifr), @@ -725,10 +754,10 @@ gre_update_hdr(struct gre_softc *sc, struct grehdr *gh) sc->gre_hlen += 2 * sizeof(uint16_t); *opts++ = 0; } - if (sc->gre_key != 0) { + if (sc->gre_okey != 0) { flags |= GRE_FLAGS_KP; sc->gre_hlen += sizeof(uint32_t); - *opts++ = htonl(sc->gre_key); + *opts++ = htonl(sc->gre_okey); } if (sc->gre_options & GRE_ENABLE_SEQ) { flags |= GRE_FLAGS_SP; @@ -739,6 +768,31 @@ gre_update_hdr(struct gre_softc *sc, struct grehdr *gh) gh->gre_flags = htons(flags); } +bool +gre_getkey(const struct mbuf *m, int off, uint32_t *key) +{ + int koff; + uint16_t flags; + + if (m->m_pkthdr.len < off + sizeof(struct grehdr)) + return (false); + + m_copydata(m, off, sizeof(flags), (caddr_t)&flags); + flags = ntohs(flags); + if ((flags & GRE_FLAGS_KP) == 0) { + *key = 0; + return (true); + } + koff = off + sizeof(struct grehdr); + if ((flags & GRE_FLAGS_CP) != 0) + koff += sizeof(uint32_t); + if (m->m_pkthdr.len < koff + sizeof(*key)) + return (false); + m_copydata(m, koff, sizeof(*key), (caddr_t)key); + *key = ntohl(*key); + return (true); +} + int gre_input(struct mbuf *m, int off, int proto, void *arg) { @@ -746,9 +800,7 @@ gre_input(struct mbuf *m, int off, int proto, void *arg) struct grehdr *gh; struct ifnet *ifp; uint32_t *opts; -#ifdef notyet uint32_t key; -#endif uint16_t flags; int hlen, isr, af; @@ -774,24 +826,14 @@ gre_input(struct mbuf *m, int off, int proto, void *arg) opts++; } if (flags & GRE_FLAGS_KP) { -#ifdef notyet - /* - * XXX: The current implementation uses the key only for outgoing - * packets. But we can check the key value here, or even in the - * encapcheck function. - */ key = ntohl(*opts); -#endif + /* Packet with another key can only be here due to fallback. */ + if (key != sc->gre_ikey && !V_gre_key_fallback) + goto drop; hlen += sizeof(uint32_t); opts++; - } -#ifdef notyet - } else - key = 0; + } - if (sc->gre_key != 0 && (key != sc->gre_key || key != 0)) - goto drop; -#endif if (flags & GRE_FLAGS_SP) { #ifdef notyet seq = ntohl(*opts); @@ -1094,14 +1136,18 @@ gre_set_flags_nl(struct gre_softc *sc, struct nl_pstate *npt, uint32_t opt) } static int -gre_set_key_nl(struct gre_softc *sc, struct nl_pstate *npt, uint32_t key) +gre_set_key_nl(struct gre_softc *sc, struct nl_pstate *npt, + uint32_t key, bool dir) { int error; - error = gre_set_key(sc, key); + error = gre_set_key(sc, key, dir); if (error == EINVAL) nlmsg_report_err_msg(npt, "gre key is invalid: %u", key); + if (error == EEXIST) + nlmsg_report_err_msg(npt, + "tunnel with the same addresses and key %u exists", key); return (error); } diff --git a/sys/net/if_gre.h b/sys/net/if_gre.h index 61c8ef54523e..6a3f2b34096d 100644 --- a/sys/net/if_gre.h +++ b/sys/net/if_gre.h @@ -87,7 +87,8 @@ struct gre_softc { int gre_family; /* AF of delivery header */ uint32_t gre_iseq; uint32_t gre_oseq; - uint32_t gre_key; + uint32_t gre_okey; + uint32_t gre_ikey; uint32_t gre_options; uint32_t gre_csumflags; uint32_t gre_port; @@ -133,6 +134,7 @@ MALLOC_DECLARE(M_GRE); struct gre_list *gre_hashinit(void); void gre_hashdestroy(struct gre_list *); +bool gre_getkey(const struct mbuf *, int, uint32_t *); int gre_input(struct mbuf *, int, int, void *); void gre_update_hdr(struct gre_softc *, struct grehdr *); void gre_update_udphdr(struct gre_softc *, struct udphdr *, uint16_t); @@ -149,6 +151,10 @@ void in6_gre_uninit(void); int in6_gre_setopts(struct gre_softc *, u_long, uint32_t); int in6_gre_ioctl(struct gre_softc *, u_long, caddr_t); int in6_gre_output(struct mbuf *, int, int, uint32_t); + +VNET_DECLARE(bool, gre_key_fallback); +#define V_gre_key_fallback VNET(gre_key_fallback) + /* * CISCO uses special type for GRE tunnel created as part of WCCP * connection, while in fact those packets are just IPv4 encapsulated @@ -170,6 +176,8 @@ int in6_gre_output(struct mbuf *, int, int, uint32_t); #define GRESOPTS _IOW('i', 110, struct ifreq) #define GREGPORT _IOWR('i', 111, struct ifreq) #define GRESPORT _IOW('i', 112, struct ifreq) +#define GREGIKEY _IOWR('i', 113, struct ifreq) +#define GRESIKEY _IOW('i', 114, struct ifreq) /* GRE-in-UDP encapsulation destination port as defined in RFC8086 */ #define GRE_UDPPORT 4754 diff --git a/sys/netinet/ip_gre.c b/sys/netinet/ip_gre.c index 0eb7c27a621e..41691b93bc23 100644 --- a/sys/netinet/ip_gre.c +++ b/sys/netinet/ip_gre.c @@ -121,7 +121,7 @@ in_gre_lookup_socket(in_addr_t addr) static int in_gre_checkdup(const struct gre_softc *sc, in_addr_t src, in_addr_t dst, - uint32_t opts) + uint32_t opts, uint32_t key) { struct gre_list *head; struct gre_softc *tmp; @@ -130,6 +130,7 @@ in_gre_checkdup(const struct gre_softc *sc, in_addr_t src, in_addr_t dst, if (sc->gre_family == AF_INET && sc->gre_oip.ip_src.s_addr == src && sc->gre_oip.ip_dst.s_addr == dst && + sc->gre_ikey == key && (sc->gre_options & GRE_UDPENCAP) == (opts & GRE_UDPENCAP)) return (EEXIST); @@ -145,38 +146,73 @@ in_gre_checkdup(const struct gre_softc *sc, in_addr_t src, in_addr_t dst, if (tmp == sc) continue; if (tmp->gre_oip.ip_src.s_addr == src && - tmp->gre_oip.ip_dst.s_addr == dst) + tmp->gre_oip.ip_dst.s_addr == dst && + tmp->gre_ikey == key) return (EADDRNOTAVAIL); } return (0); } +static struct gre_softc * +in_gre_lookup_key(in_addr_t src, in_addr_t dst, uint32_t key) +{ + struct gre_softc *sc, *keyless, *only; + uint32_t count = 0; + + keyless = only = NULL; + CK_LIST_FOREACH(sc, &GRE_HASH(src, dst), chain) { + if (sc->gre_oip.ip_src.s_addr != src || + sc->gre_oip.ip_dst.s_addr != dst) + continue; + if (sc->gre_ikey == key) + return (sc); + if (sc->gre_ikey == 0) + keyless = sc; + only = sc; + count++; + } + if (!V_gre_key_fallback) + return (NULL); + /* + * When there is no tunnel with specified key and fallback + * is enabled, tunnel without key is preferred. + */ + if (keyless != NULL) + return (keyless); + /* + * Otherwise, if only one tunnel configured between these + * addresses, accept any key for backward compatibility. + */ + if (count == 1) + return (only); + + return (NULL); +} + static int in_gre_lookup(const struct mbuf *m, int off, int proto, void **arg) { const struct ip *ip; struct gre_softc *sc; + uint32_t key; if (V_ipv4_hashtbl == NULL) return (0); NET_EPOCH_ASSERT(); + if (!gre_getkey(m, off, &key)) + return (0); ip = mtod(m, const struct ip *); - CK_LIST_FOREACH(sc, &GRE_HASH(ip->ip_dst.s_addr, - ip->ip_src.s_addr), chain) { - /* - * This is an inbound packet, its ip_dst is source address - * in softc. - */ - if (sc->gre_oip.ip_src.s_addr == ip->ip_dst.s_addr && - sc->gre_oip.ip_dst.s_addr == ip->ip_src.s_addr) { - if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0) - return (0); - *arg = sc; - return (ENCAP_DRV_LOOKUP); - } - } - return (0); + /* + * This is an inbound packet, its ip_dst is source address + * in softc. + */ + sc = in_gre_lookup_key(ip->ip_dst.s_addr, ip->ip_src.s_addr, key); + if (sc == NULL || (GRE2IFP(sc)->if_flags & IFF_UP) == 0) + return (0); + + *arg = sc; + return (ENCAP_DRV_LOOKUP); } /* @@ -222,16 +258,34 @@ in_gre_udp_input(struct mbuf *m, int off, struct inpcb *inp, const struct sockaddr *sa, void *ctx) { struct gre_socket *gs; - struct gre_softc *sc; + struct gre_softc *sc, *keyless, *only; in_addr_t dst; + uint32_t key, count = 0; NET_EPOCH_ASSERT(); + keyless = only = NULL; gs = (struct gre_socket *)ctx; dst = ((const struct sockaddr_in *)sa)->sin_addr.s_addr; + if (!gre_getkey(m, off + sizeof(struct udphdr), &key)) { + m_freem(m); + return (true); + } CK_LIST_FOREACH(sc, &gs->list, chain) { - if (sc->gre_oip.ip_dst.s_addr == dst) + if (sc->gre_oip.ip_dst.s_addr != dst) + continue; + if (sc->gre_ikey == key) break; + if (sc->gre_ikey == 0) + keyless = sc; + only = sc; + count++; + } + if (sc == NULL && V_gre_key_fallback) { + if (keyless != NULL) + sc = keyless; + else if (count == 1) + sc = only; } if (sc != NULL && (GRE2IFP(sc)->if_flags & IFF_UP) != 0){ gre_input(m, off + sizeof(struct udphdr), IPPROTO_UDP, sc); @@ -394,26 +448,34 @@ in_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value) int error; /* NOTE: we are protected with gre_ioctl_sx lock */ - MPASS(cmd == GRESKEY || cmd == GRESOPTS || cmd == GRESPORT); + MPASS(cmd == GRESKEY || cmd == GRESIKEY || + cmd == GRESOPTS || cmd == GRESPORT); MPASS(sc->gre_family == AF_INET); /* - * If we are going to change encapsulation protocol, do check - * for duplicate tunnels. Return EEXIST here to do not confuse - * user. + * If we are going to change encapsulation protocol or the key, + * do check for duplicate tunnels. Return EEXIST here to do not + * confuse user. */ - if (cmd == GRESOPTS && - (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP) && - in_gre_checkdup(sc, sc->gre_oip.ip_src.s_addr, - sc->gre_oip.ip_dst.s_addr, value) == EADDRNOTAVAIL) - return (EEXIST); + if ((cmd == GRESOPTS && + (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP)) || + (cmd == GRESIKEY && sc->gre_ikey != value)) { + if (in_gre_checkdup(sc, sc->gre_oip.ip_src.s_addr, + sc->gre_oip.ip_dst.s_addr, + cmd == GRESOPTS ? value: sc->gre_options, + cmd == GRESIKEY ? value: sc->gre_ikey) == EADDRNOTAVAIL) + return (EEXIST); + } CK_LIST_REMOVE(sc, chain); CK_LIST_REMOVE(sc, srchash); GRE_WAIT(); switch (cmd) { case GRESKEY: - sc->gre_key = value; + sc->gre_okey = value; + break; + case GRESIKEY: + sc->gre_ikey = value; break; case GRESOPTS: sc->gre_options = value; @@ -462,7 +524,7 @@ in_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data) V_ipv4_sockets = (struct gre_sockets *)gre_hashinit(); } error = in_gre_checkdup(sc, src->sin_addr.s_addr, - dst->sin_addr.s_addr, sc->gre_options); + dst->sin_addr.s_addr, sc->gre_options, sc->gre_ikey); if (error == EADDRNOTAVAIL) break; if (error == EEXIST) { diff --git a/sys/netinet6/ip6_gre.c b/sys/netinet6/ip6_gre.c index 952b132e4cf7..f5d049e61f85 100644 --- a/sys/netinet6/ip6_gre.c +++ b/sys/netinet6/ip6_gre.c @@ -112,7 +112,7 @@ in6_gre_lookup_socket(const struct in6_addr *addr) static int in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src, - const struct in6_addr *dst, uint32_t opts) + const struct in6_addr *dst, uint32_t opts, uint32_t key) { struct gre_list *head; struct gre_softc *tmp; @@ -121,6 +121,7 @@ in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src, if (sc->gre_family == AF_INET6 && IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, src) && IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, dst) && + sc->gre_ikey == key && (sc->gre_options & GRE_UDPENCAP) == (opts & GRE_UDPENCAP)) return (EEXIST); @@ -136,39 +137,74 @@ in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src, if (tmp == sc) continue; if (IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_src, src) && - IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_dst, dst)) + IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_dst, dst) && + tmp->gre_ikey == key) return (EADDRNOTAVAIL); } return (0); } +static struct gre_softc * +in6_gre_lookup_key(const struct in6_addr *src, const struct in6_addr *dst, + uint32_t key) +{ + struct gre_softc *sc, *keyless, *only; + uint32_t count = 0; + + keyless = only = NULL; + CK_LIST_FOREACH(sc, &GRE_HASH(src, dst), chain) { + if (!IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, src) || + !IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, dst)) + continue; + if (sc->gre_ikey == key) + return (sc); + if (sc->gre_ikey == 0) + keyless = sc; + only = sc; + count++; + } + if (!V_gre_key_fallback) + return (NULL); + /* + * When there is no tunnel with specified key and fallback + * is enabled, tunnel without key is preferred. + */ + if (keyless != NULL) + return (keyless); + /* + * Otherwise, if only one tunnel configured between these + * addresses, accept any key for backward compatibility. + */ + if (count == 1) + return (only); + + return (NULL); +} + static int in6_gre_lookup(const struct mbuf *m, int off, int proto, void **arg) { const struct ip6_hdr *ip6; struct gre_softc *sc; + uint32_t key; if (V_ipv6_hashtbl == NULL) return (0); NET_EPOCH_ASSERT(); + if (!gre_getkey(m, off, &key)) + return (0); ip6 = mtod(m, const struct ip6_hdr *); - CK_LIST_FOREACH(sc, &GRE_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) { - /* - * This is an inbound packet, its ip6_dst is source address - * in softc. - */ - if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, - &ip6->ip6_dst) && - IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, - &ip6->ip6_src)) { - if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0) - return (0); - *arg = sc; - return (ENCAP_DRV_LOOKUP); - } - } - return (0); + /* + * This is an inbound packet, its ip6_dst is source address + * in softc. + */ + sc = in6_gre_lookup_key(&ip6->ip6_dst, &ip6->ip6_src, key); + if (sc == NULL || (GRE2IFP(sc)->if_flags & IFF_UP) == 0) + return (0); + + *arg = sc; + return (ENCAP_DRV_LOOKUP); } /* @@ -215,20 +251,36 @@ in6_gre_udp_input(struct mbuf *m, int off, struct inpcb *inp, const struct sockaddr *sa, void *ctx) { struct gre_socket *gs; - struct gre_softc *sc; + struct gre_softc *sc, *keyless, *only; struct sockaddr_in6 dst; + uint32_t key, count = 0; NET_EPOCH_ASSERT(); + keyless = only = NULL; gs = (struct gre_socket *)ctx; dst = *(const struct sockaddr_in6 *)sa; - if (sa6_embedscope(&dst, 0)) { + if (sa6_embedscope(&dst, 0) || + !gre_getkey(m, off + sizeof(struct udphdr), &key)) { m_freem(m); return (true); } CK_LIST_FOREACH(sc, &gs->list, chain) { - if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, &dst.sin6_addr)) + if (!IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, + &dst.sin6_addr)) + continue; + if (sc->gre_ikey == key) break; + if (sc->gre_ikey == 0) + keyless = sc; + only = sc; + count++; + } + if (sc == NULL && V_gre_key_fallback) { + if (keyless != NULL) + sc = keyless; + else if (count == 1) + sc = only; } if (sc != NULL && (GRE2IFP(sc)->if_flags & IFF_UP) != 0){ gre_input(m, off + sizeof(struct udphdr), IPPROTO_UDP, sc); @@ -392,26 +444,34 @@ in6_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value) int error; /* NOTE: we are protected with gre_ioctl_sx lock */ - MPASS(cmd == GRESKEY || cmd == GRESOPTS || cmd == GRESPORT); + MPASS(cmd == GRESKEY || cmd == GRESIKEY || + cmd == GRESOPTS || cmd == GRESPORT); MPASS(sc->gre_family == AF_INET6); /* - * If we are going to change encapsulation protocol, do check - * for duplicate tunnels. Return EEXIST here to do not confuse - * user. + * If we are going to change encapsulation protocol or the key, + * do check for duplicate tunnels. Return EEXIST here to do not + * confuse user. */ - if (cmd == GRESOPTS && - (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP) && - in6_gre_checkdup(sc, &sc->gre_oip6.ip6_src, - &sc->gre_oip6.ip6_dst, value) == EADDRNOTAVAIL) - return (EEXIST); + if ((cmd == GRESOPTS && + (sc->gre_options & GRE_UDPENCAP) != (value & GRE_UDPENCAP)) || + (cmd == GRESIKEY && sc->gre_ikey != value)) { + if (in6_gre_checkdup(sc, &sc->gre_oip6.ip6_src, + &sc->gre_oip6.ip6_dst, + cmd == GRESOPTS ? value: sc->gre_options, + cmd == GRESIKEY ? value: sc->gre_ikey) == EADDRNOTAVAIL) + return (EEXIST); + } CK_LIST_REMOVE(sc, chain); CK_LIST_REMOVE(sc, srchash); GRE_WAIT(); switch (cmd) { case GRESKEY: - sc->gre_key = value; + sc->gre_okey = value; + break; + case GRESIKEY: + sc->gre_ikey = value; break; case GRESOPTS: sc->gre_options = value; @@ -469,7 +529,7 @@ in6_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data) V_ipv6_sockets = (struct gre_sockets *)gre_hashinit(); } error = in6_gre_checkdup(sc, &src->sin6_addr, - &dst->sin6_addr, sc->gre_options); + &dst->sin6_addr, sc->gre_options, sc->gre_ikey); if (error == EADDRNOTAVAIL) break; *** 508 LINES SKIPPED ***