From nobody Fri Aug 29 15:40:24 2025 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 4cD2XS54d6z65kny; Fri, 29 Aug 2025 15:40:24 +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 "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cD2XS499fz43ZM; Fri, 29 Aug 2025 15:40:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1756482024; 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=6/XF3a9VPyRJ1wL9szkyWA7TfeBCxgQ/6SoK3SuK8Qo=; b=QHz+1orZoYovZl7Cn58O38Uo4lR2rGelWBGr7T8iNHE3qLo/80+vgXOLzwFPLXK7Sh0Lf0 ffWEBg8QbL3l2ujTddHnQu4oZz1NGqAhNIibL4a5rbOtkKOqcsejKVmmJKIqgM2vrUmSCV TNpKieXp3CK7aDCjxQ3zN2JLjoF/cKuDvRHJH79yOwxvF6xVEbuyrWrboj47hDS/C+cd1r NsN5U6+a4zHsPH1PjH2IoT+SVGcjLoqEJRhsxOd37yON/Ou852wQHgL2CN8LAb3WUpo8Qs CwmS3A72/lW9Zzn610dAHlfBZjQLz3j71M6ZCVrwvAz6DvrRJ9WRP0hsu5n4Lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1756482024; 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=6/XF3a9VPyRJ1wL9szkyWA7TfeBCxgQ/6SoK3SuK8Qo=; b=RAzIkU03d6a4XKb3pqn4ebrlBCOnvi4fSGsNZz9CX2aLTTJkJeThw6eudvpay0KbZbtUSs RkNF+kXSGbOJrRIU2MLp4jTYExqrIYRaMRQkZiQf6KdEfnERIkWMpnGzVTghrsK7wqwise 3uWDnDtf2whPLORk3sG/1RpF0026AwV9l7LLYCN9bm1d95ffb4+Sk8mBBh9hGeNAZZ+SzQ zgNyt4UWt5UqnxGrjCkC37vzAFUOHBTb88M1DNveEWTIrcwCi+KAkCXjEDiYJ+Xli3NojB i1Ofw2F4RIit7oN3T4+TN5MHAI15iTmDu2ByfTIK6AaqJn58hQoJLPwY3pYsag== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1756482024; a=rsa-sha256; cv=none; b=TeggxjcC/dp24p0GDD5uAtXHRcTMxFi4EM3Jc/wV7SSW4CSeEMMvP6t5cS7ei8Uh9Jckf5 +Wxpeq0n8heFCrFvxsLGjNSq99OXgwP3KSUCRbXBebKcBF2aK5upH13/1W/jMTAkCAV/4J xM/Rms3/1axQNPdA+j8M+x0ntNgYWmg9wmLjiEqs0R0eWR+AvpBq302WRITldW6MvwFn7O SbZ4LV7+BP34Gzd603jighGVZT33YTGVdDHzBxsJIfD06lcG1O39uC3Z2ty8fz9Pjy7tAv eOxGL5VrbjZnl/Kd6C2qcJt9NeQ66IJhVMVlbnyvEfNLcjAraJLWF6b31Tf4kQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4cD2XS3ShSzm46; Fri, 29 Aug 2025 15:40:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 57TFeO2S047288; Fri, 29 Aug 2025 15:40:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 57TFeOKZ047285; Fri, 29 Aug 2025 15:40:24 GMT (envelope-from git) Date: Fri, 29 Aug 2025 15:40:24 GMT Message-Id: <202508291540.57TFeOKZ047285@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Aymeric Wibo Subject: git: 94ed4c1903cf - main - libifconfig: Add netlink based helper to bring the interface up/down 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 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: obiwac X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 94ed4c1903cf846a3f484373b8aae136762e9d09 Auto-Submitted: auto-generated The branch main has been updated by obiwac: URL: https://cgit.FreeBSD.org/src/commit/?id=94ed4c1903cf846a3f484373b8aae136762e9d09 commit 94ed4c1903cf846a3f484373b8aae136762e9d09 Author: Muhammad Saheed AuthorDate: 2025-08-29 15:36:10 +0000 Commit: Aymeric Wibo CommitDate: 2025-08-29 15:39:39 +0000 libifconfig: Add netlink based helper to bring the interface up/down Adds `ifconfig_set_up()` to set and unset the `IFF_UP` on a network interface using `RTM_NEWLINK`. Sponsored by: Google LLC (GSoC) Reviewed by: obiwac, mckusick (mentor), kp Approved by: obiwac, mckusick (mentor), kp Differential Revision: https://reviews.freebsd.org/D52128 --- lib/libifconfig/Makefile | 1 + lib/libifconfig/libifconfig.h | 11 ++++++ lib/libifconfig/libifconfig_nl.c | 72 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/lib/libifconfig/Makefile b/lib/libifconfig/Makefile index fb7c659e068c..02629eb88f25 100644 --- a/lib/libifconfig/Makefile +++ b/lib/libifconfig/Makefile @@ -17,6 +17,7 @@ SRCS= libifconfig.c \ libifconfig_internal.c \ libifconfig_lagg.c \ libifconfig_media.c \ + libifconfig_nl.c \ libifconfig_sfp.c GEN= libifconfig_sfp_tables.h \ diff --git a/lib/libifconfig/libifconfig.h b/lib/libifconfig/libifconfig.h index a5ce7b375830..817f52bd094e 100644 --- a/lib/libifconfig/libifconfig.h +++ b/lib/libifconfig/libifconfig.h @@ -35,6 +35,8 @@ #include #include +#include + #define ND6_IFF_DEFAULTIF 0x8000 typedef enum { @@ -381,3 +383,12 @@ int ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name, * length of *lenp * IFNAMSIZ bytes. */ int ifconfig_list_cloners(ifconfig_handle_t *h, char **bufp, size_t *lenp); + +/** Brings the interface up/down + * @param h An open ifconfig state object + * @param ifname The interface name + * @param up true to bring the interface up, false to bring it down + * @return 0 on success, nonzero on failure. + * On failure, the error info on the handle is set. + */ +int ifconfig_set_up(ifconfig_handle_t *h, const char *ifname, bool up); diff --git a/lib/libifconfig/libifconfig_nl.c b/lib/libifconfig/libifconfig_nl.c new file mode 100644 index 000000000000..7d9decabe26f --- /dev/null +++ b/lib/libifconfig/libifconfig_nl.c @@ -0,0 +1,72 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2025, Muhammad Saheed + */ + +#include +#include +#include +#include + +#include "libifconfig.h" +#include "libifconfig_internal.h" + +static int ifconfig_modify_flags(ifconfig_handle_t *h, const char *ifname, + int ifi_flags, int ifi_change); + +static int +ifconfig_modify_flags(ifconfig_handle_t *h, const char *ifname, int ifi_flags, + int ifi_change) +{ + int ret = 0; + struct snl_state ss; + struct snl_writer nw; + struct nlmsghdr *hdr; + struct ifinfomsg *ifi; + struct snl_errmsg_data e = { 0 }; + + if (!snl_init(&ss, NETLINK_ROUTE)) { + ifconfig_error(h, NETLINK, ENOTSUP); + return (-1); + } + + snl_init_writer(&ss, &nw); + hdr = snl_create_msg_request(&nw, NL_RTM_NEWLINK); + ifi = snl_reserve_msg_object(&nw, struct ifinfomsg); + snl_add_msg_attr_string(&nw, IFLA_IFNAME, ifname); + + ifi->ifi_flags = ifi_flags; + ifi->ifi_change = ifi_change; + + hdr = snl_finalize_msg(&nw); + if (hdr == NULL) { + ifconfig_error(h, NETLINK, ENOMEM); + ret = -1; + goto out; + } + + if (!snl_send_message(&ss, hdr)) { + ifconfig_error(h, NETLINK, EIO); + ret = -1; + goto out; + } + + if (!snl_read_reply_code(&ss, hdr->nlmsg_seq, &e)) { + ifconfig_error(h, NETLINK, e.error); + ret = -1; + goto out; + } + +out: + snl_free(&ss); + return (ret); +} + +int +ifconfig_set_up(ifconfig_handle_t *h, const char *ifname, bool up) +{ + int flag = up ? IFF_UP : ~IFF_UP; + + return (ifconfig_modify_flags(h, ifname, flag, IFF_UP)); +}