From nobody Thu Jul 30 18:11:53 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 4h9y2d6X1Vz6mn2x for ; Thu, 30 Jul 2026 18:11:53 +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 "YR1" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4h9y2d3H8sz4D0T for ; Thu, 30 Jul 2026 18:11:53 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1785435113; 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=O7fVsAdOZUS/SdxFWL8+I3U5MF6ajlzn9lfxdNomTXc=; b=dmotVaGPclLa4YM+FtMbpp1ln3WXxIfrEZm1rg7hKo5S3mrGjtTzdTANs/yyC9fyTj9H6v 66+fZB3/Vz99TMZjHFZpojTfGpFxv7A9xge6gQMM+jJoz4fuXKC8unDwr4vtU0R32n1ikz Nd+SeCBz1zOYhXgC/dEaxu0U+sog0igIZ8u9jdNNfSoh+upHgJkv7i+zdDxLE2yC5SAUuq QWlVkLGyhw3PwruHJ0ieOAc46pIHFGi17V8qKWPyP1UFip/JSmfQVbOMBkrm8qp32cqMka m8ndjAq3axhabfKfXJcmWBGMJFMsX4it+zY9lUuasZXG5HtA3L9XGu8bip8/Lg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1785435113; a=rsa-sha256; cv=none; b=GVcI+9DeImIlb3fA8uS9xDYLsmy3PkAYK0BUvHpctalG3joAmj0UoaoKRyumb3VnEZsTmK WUfAfKfB4/LPS6bXUPUItyvJj+k7V1TCvXKQSdBbOZiNKClbex/50imANMlx5afBDA127q LFiO/d+MfH5uy5JtWREOBe9O7blgfXh54m29CmYrO7RvWjxyFYP0vNf2OY9a5PMu7WCxpA eN1wEsdFw2TPFl5VNm9UWz0crNGbvLw9Bvd7TSPE8UMMQ4toCr3K+Eahh7ufxzgOKUjANz T2Om3KkfwsLgy9N4beLJAr/LB+2UKSRAlpTn9bt41oKt4v8dtZJRZYjljN0gTg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1785435113; 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=O7fVsAdOZUS/SdxFWL8+I3U5MF6ajlzn9lfxdNomTXc=; b=LAacWUeRBuZrQnnoPZ/lBJLAY7bF27N6MVWNF5exVpy6Ijjpxg/m1VJAnlRvN2dwaf2rmi rs7Zd0eUZJJQvl4LUR+/yKKLg5H/mRNNHyhBWy348bpqXjHl7dpQHeDv2moQCUgQRLHZMd Q/e/PNDEQab8dmWg3SKeTZSH6x2070G8Zc7sJTXC5zVt3Ff9ZBOFvGB2TQv3pxGw0effvA BGPEyjibBZxZjIjtVxisKtssSNmbR2KU1jASe2aSIdwBCNIxrUbquU6mk9DNW/LN6Lvs29 2oydC7C7oSCqGYW6mCukvt03tYTXemUb0tmuzkJ+U56dZ4lwTjM4uHK7N42JbA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4h9y2d2Hgbz7t4 for ; Thu, 30 Jul 2026 18:11:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3cffb by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 30 Jul 2026 18:11:53 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Pouria Mousavizadeh Tehrani Subject: git: 1cd332adc86a - main - ifconfig: Add gre netlink support List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: 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: 1cd332adc86a551b9c498ee6e86f161eb5641b68 Auto-Submitted: auto-generated Date: Thu, 30 Jul 2026 18:11:53 +0000 Message-Id: <6a6b93e9.3cffb.66460803@gitrepo.freebsd.org> The branch main has been updated by pouria: URL: https://cgit.FreeBSD.org/src/commit/?id=1cd332adc86a551b9c498ee6e86f161eb5641b68 commit 1cd332adc86a551b9c498ee6e86f161eb5641b68 Author: Pouria Mousavizadeh Tehrani AuthorDate: 2026-07-30 17:53:39 +0000 Commit: Pouria Mousavizadeh Tehrani CommitDate: 2026-07-30 17:54:07 +0000 ifconfig: Add gre netlink support Implement netlink support for gre in ifconfig Differential Revision: https://reviews.freebsd.org/D55366 --- sbin/ifconfig/ifgre.c | 274 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 274 insertions(+) diff --git a/sbin/ifconfig/ifgre.c b/sbin/ifconfig/ifgre.c index c3d3856e0b09..c704aa66b275 100644 --- a/sbin/ifconfig/ifgre.c +++ b/sbin/ifconfig/ifgre.c @@ -41,12 +41,277 @@ #include "ifconfig.h" +#ifndef WITHOUT_NETLINK +#include "ifconfig_netlink.h" +#endif + static const char *GREBITS[] = { [0] = "ENABLE_CSUM", [1] = "ENABLE_SEQ", [2] = "UDPENCAP", }; +#ifndef WITHOUT_NETLINK +struct nl_parsed_gre { + uint32_t ifla_flags; + uint32_t ifla_okey; + uint32_t ifla_encap_type; + uint16_t ifla_encap_sport; +}; + +struct nla_gre_info { + const char *kind; + struct nl_parsed_gre data; +}; + +struct nla_gre_link { + uint32_t ifi_index; + struct nla_gre_info linkinfo; +}; + +static inline void +gre_nl_init(if_ctx *ctx, struct snl_writer *nw, uint32_t flags) +{ + struct nlmsghdr *hdr; + + snl_init_writer(ctx->io_ss, nw); + hdr = snl_create_msg_request(nw, NL_RTM_NEWLINK); + hdr->nlmsg_flags |= flags; + snl_reserve_msg_object(nw, struct ifinfomsg); + snl_add_msg_attr_string(nw, IFLA_IFNAME, ctx->ifname); +} + +static inline void +gre_nl_fini(if_ctx *ctx, struct snl_writer *nw) +{ + struct nlmsghdr *hdr; + struct snl_errmsg_data errmsg = {}; + + hdr = snl_finalize_msg(nw); + if (hdr == NULL || !snl_send_message(ctx->io_ss, hdr)) + 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); +} + +#define _OUT(_field) offsetof(struct nl_parsed_gre, _field) +static const struct snl_attr_parser nla_p_gre[] = { + { .type = IFLA_GRE_FLAGS, .off = _OUT(ifla_flags), .cb = snl_attr_get_uint32 }, + { .type = IFLA_GRE_OKEY, .off = _OUT(ifla_okey), .cb = snl_attr_get_uint32 }, + { .type = IFLA_GRE_ENCAP_TYPE, .off = _OUT(ifla_encap_type), + .cb = snl_attr_get_uint32 }, + { .type = IFLA_GRE_ENCAP_SPORT, .off = _OUT(ifla_encap_sport), + .cb = snl_attr_get_uint16 }, +}; +#undef _OUT +SNL_DECLARE_ATTR_PARSER(gre_linkinfo_data_parser, nla_p_gre); + +#define _OUT(_field) offsetof(struct nla_gre_info, _field) +static const struct snl_attr_parser ap_gre_linkinfo[] = { + { .type = IFLA_INFO_KIND, .off = _OUT(kind), .cb = snl_attr_get_string }, + { .type = IFLA_INFO_DATA, .off = _OUT(data), + .arg = &gre_linkinfo_data_parser, .cb = snl_attr_get_nested }, +}; +#undef _OUT +SNL_DECLARE_ATTR_PARSER(gre_linkinfo_parser, ap_gre_linkinfo); + +#define _IN(_field) offsetof(struct ifinfomsg, _field) +#define _OUT(_field) offsetof(struct nla_gre_link, _field) +static const struct snl_attr_parser ap_gre_link[] = { + { .type = IFLA_LINKINFO, .off = _OUT(linkinfo), + .arg = &gre_linkinfo_parser, .cb = snl_attr_get_nested }, +}; + +static const struct snl_field_parser fp_geneve_link[] = { + { .off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), + .cb = snl_field_get_uint32 }, +}; +#undef _IN +#undef _OUT +SNL_DECLARE_PARSER(gre_parser, struct ifinfomsg, fp_geneve_link, ap_gre_link); + +static const struct snl_hdr_parser *all_parsers[] = { + &gre_linkinfo_data_parser, + &gre_linkinfo_parser, + &gre_parser, +}; + +static void +gre_status_nl(if_ctx *ctx) +{ + struct snl_writer nw = {}; + struct nlmsghdr *hdr; + struct snl_errmsg_data errmsg = {}; + struct nla_gre_link gre_link = {}; + + if (strncmp(ctx->ifname, "gre", sizeof("gre") - 1) != 0) + return; + + snl_init_writer(ctx->io_ss, &nw); + hdr = snl_create_msg_request(&nw, NL_RTM_GETLINK); + hdr->nlmsg_flags |= NLM_F_DUMP; + snl_reserve_msg_object(&nw, struct ifinfomsg); + snl_add_msg_attr_string(&nw, IFLA_IFNAME, ctx->ifname); + + hdr = snl_finalize_msg(&nw); + if (hdr == NULL || !snl_send_message(ctx->io_ss, hdr)) + err(1, "unable to send netlink message"); + + hdr = snl_read_reply(ctx->io_ss, hdr->nlmsg_seq); + if (hdr->nlmsg_type != NL_RTM_NEWLINK) { + if (!snl_parse_errmsg(ctx->io_ss, hdr, &errmsg)) + errx(EINVAL, "(NETLINK)"); + if (errmsg.error_str != NULL) + errx(errmsg.error, "(NETLINK) %s", errmsg.error_str); + } + + if (!snl_parse_nlmsg(ctx->io_ss, hdr, &gre_parser, &gre_link)) + return; + + struct nla_gre_info gre_info = gre_link.linkinfo; + struct nl_parsed_gre gre_data = gre_info.data; + + if (gre_data.ifla_okey != 0) + printf("\tgrekey: 0x%x (%u)\n", + gre_data.ifla_okey, gre_data.ifla_okey); + + if (gre_data.ifla_flags == 0) + return; + + if (gre_data.ifla_encap_sport != 0) + printf("\tudpport: %u\n", gre_data.ifla_encap_sport); + + printf("\toptions=%x", gre_data.ifla_flags); + print_bits("options", &gre_data.ifla_flags, 1, GREBITS, nitems(GREBITS)); + putchar('\n'); +} + +static void +setifgrekey_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 +setifgreport_nl(if_ctx *ctx, const char *val, int dummy __unused) +{ + struct snl_writer nw = {}; + int off, off2; + uint16_t greport = 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_u16(&nw, IFLA_GRE_ENCAP_SPORT, greport); + + snl_end_attr_nested(&nw, off2); + snl_end_attr_nested(&nw, off); + + gre_nl_fini(ctx, &nw); +} + +static void +setifgreopts_nl(if_ctx *ctx, const char *val __unused, int d) +{ + struct snl_writer nw = {}; + struct nlmsghdr *hdr; + struct snl_errmsg_data errmsg; + struct nla_gre_link gre_link; + int off, off2; + + snl_init_writer(ctx->io_ss, &nw); + hdr = snl_create_msg_request(&nw, NL_RTM_GETLINK); + hdr->nlmsg_flags |= NLM_F_DUMP; + snl_reserve_msg_object(&nw, struct ifinfomsg); + snl_add_msg_attr_string(&nw, IFLA_IFNAME, ctx->ifname); + + hdr = snl_finalize_msg(&nw); + if (hdr == NULL || (!snl_send_message(ctx->io_ss, hdr))) + err(1, "unable to send netlink message"); + + hdr = snl_read_reply(ctx->io_ss, hdr->nlmsg_seq); + if (hdr->nlmsg_type != NL_RTM_NEWLINK) { + if (!snl_parse_errmsg(ctx->io_ss, hdr, &errmsg)) + errx(EINVAL, "(NETLINK)"); + if (errmsg.error_str != NULL) + errx(errmsg.error, "(NETLINK) %s", errmsg.error_str); + } + + if (!snl_parse_nlmsg(ctx->io_ss, hdr, &gre_parser, &gre_link)) + return; + + struct nla_gre_info gre_info = gre_link.linkinfo; + struct nl_parsed_gre gre_data = gre_info.data; + + if (d < 0) + gre_data.ifla_flags &= ~(-d); + else + gre_data.ifla_flags |= d; + + 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_FLAGS, gre_data.ifla_flags); + + snl_end_attr_nested(&nw, off2); + snl_end_attr_nested(&nw, off); + + gre_nl_fini(ctx, &nw); +} + +static void +setifgretype_nl(if_ctx *ctx, const char *val __unused, int d) +{ + struct snl_writer nw = {}; + int off, off2; + uint32_t type; + + 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); + type = d < 0 ? IFLA_TUNNEL_NONE : IFLA_TUNNEL_GRE_UDP; + snl_add_msg_attr_u32(&nw, IFLA_GRE_ENCAP_TYPE, type); + + snl_end_attr_nested(&nw, off2); + snl_end_attr_nested(&nw, off); + + gre_nl_fini(ctx, &nw); +} + + +static struct cmd gre_cmds[] = { + DEF_CMD_ARG("grekey", setifgrekey_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), + DEF_CMD("enable_seq", GRE_ENABLE_SEQ, setifgreopts_nl), + DEF_CMD("-enable_seq",-GRE_ENABLE_SEQ, setifgreopts_nl), + DEF_CMD("udpencap", GRE_UDPENCAP, setifgretype_nl), + DEF_CMD("-udpencap",-GRE_UDPENCAP, setifgretype_nl), +}; + +#else static void gre_status(if_ctx *ctx) { @@ -123,10 +388,16 @@ static struct cmd gre_cmds[] = { DEF_CMD("udpencap", GRE_UDPENCAP, setifgreopts), DEF_CMD("-udpencap",-GRE_UDPENCAP, setifgreopts), }; +#endif /* !WITHOUT_NETLINK */ + static struct afswtch af_gre = { .af_name = "af_gre", .af_af = AF_UNSPEC, +#ifndef WITHOUT_NETLINK + .af_other_status = gre_status_nl, +#else .af_other_status = gre_status, +#endif }; static __constructor void @@ -137,4 +408,7 @@ gre_ctor(void) for (i = 0; i < nitems(gre_cmds); i++) cmd_register(&gre_cmds[i]); af_register(&af_gre); +#ifndef WITHOUT_NETLINK + SNL_VERIFY_PARSERS(all_parsers); +#endif }