From patchwork Wed Apr 3 18:01:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 17879 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from open-mesh.org (localhost [IPv6:::1]) by open-mesh.org (Postfix) with ESMTP id A2F8F82550; Wed, 3 Apr 2019 20:01:42 +0200 (CEST) Authentication-Results: open-mesh.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=narfation.org header.i=@narfation.org header.b="HNYYJewd"; dkim-atps=neutral Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=2001:4d88:2000:7::2; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver= Received: from v3-1039.vlinux.de (narfation.org [IPv6:2001:4d88:2000:7::2]) by open-mesh.org (Postfix) with ESMTPS id 2C5398254A for ; Wed, 3 Apr 2019 20:01:40 +0200 (CEST) Received: from sven-desktop.home.narfation.org (unknown [IPv6:2a00:1ca0:1480:f1fc::4065]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id 89CC01100DA; Wed, 3 Apr 2019 20:01:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=narfation.org; s=20121; t=1554314499; bh=RtT5taSXVCb/ZidrRUaNrBq/g5j6ddUYKW/CznW+nUE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HNYYJewdtoJDVPbjwrzpuxeY/7w7m5tStT/OhbczoT+uztKj5U/PRWjAWR3Qq5FXo aAuUfOXAa3w1RN9DNk6rLzglhhEpfnFQ99eGAs3+Un3TqDXmVmkfKKeZ1WusXNDLVg ZpNGHsn2Rs2oupoogsz6F1qypdyuJlFntYkbxAaU= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Wed, 3 Apr 2019 20:01:19 +0200 Message-Id: <20190403180119.26800-4-sven@narfation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190403180119.26800-1-sven@narfation.org> References: <20190403180119.26800-1-sven@narfation.org> MIME-Version: 1.0 Subject: [B.A.T.M.A.N.] [PATCH 3/3] batctl: Add netlink fallback for sysfs' iface_status X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking Errors-To: b.a.t.m.a.n-bounces@lists.open-mesh.org Sender: "B.A.T.M.A.N" The batman-adv kernel module can now be compiled without support for sysfs. But the status of an interface in the list generated by `batctl interface` can only get the status via the per hardif sysfs file iface_status. To still have some information, fallback to BATADV_CMD_GET_HARDIF if the sysfs file is not available. Signed-off-by: Sven Eckelmann --- interface.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/interface.c b/interface.c index 667a308..630dd91 100644 --- a/interface.c +++ b/interface.c @@ -24,6 +24,8 @@ #include "sys.h" #include "functions.h" +#define IFACE_STATUS_LEN 256 + static void interface_usage(void) { fprintf(stderr, "Usage: batctl [options] interface [parameters] [add|del iface(s)]\n"); @@ -33,6 +35,92 @@ static void interface_usage(void) fprintf(stderr, " \t -h print this help\n"); } +static int get_iface_status_netlink_parse(struct nl_msg *msg, void *arg) +{ + + struct nlattr *attrs[NUM_BATADV_ATTR]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + char *iface_status = arg; + struct genlmsghdr *ghdr; + + if (!genlmsg_valid_hdr(nlh, 0)) + return NL_OK; + + ghdr = nlmsg_data(nlh); + if (ghdr->cmd != BATADV_CMD_GET_HARDIF) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) + return NL_OK; + + if (attrs[BATADV_ATTR_ACTIVE]) + strncpy(iface_status, "active\n", IFACE_STATUS_LEN); + else + strncpy(iface_status, "inactive\n", IFACE_STATUS_LEN); + + iface_status[IFACE_STATUS_LEN - 1] = '\0'; + + return NL_STOP; +} + +static char *get_iface_status_netlink(unsigned int meshif, unsigned int hardif, + char *iface_status) +{ + struct nl_sock *sock; + struct nl_msg *msg; + int batadv_family; + struct nl_cb *cb; + int ret; + + strncpy(iface_status, "\n", IFACE_STATUS_LEN); + iface_status[IFACE_STATUS_LEN - 1] = '\0'; + + sock = nl_socket_alloc(); + if (!sock) + return iface_status; + + ret = genl_connect(sock); + if (ret < 0) + goto err_free_sock; + + batadv_family = genl_ctrl_resolve(sock, BATADV_NL_NAME); + if (ret < 0) + goto err_free_sock; + + cb = nl_cb_alloc(NL_CB_DEFAULT); + if (!cb) + goto err_free_sock; + + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_iface_status_netlink_parse, + iface_status); + + msg = nlmsg_alloc(); + if (!msg) + goto err_free_cb; + + genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, batadv_family, + 0, 0, BATADV_CMD_GET_HARDIF, 1); + + nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, meshif); + nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX, hardif); + + ret = nl_send_auto_complete(sock, msg); + if (ret < 0) + goto err_free_msg; + + nl_recvmsgs(sock, cb); + +err_free_msg: + nlmsg_free(msg); +err_free_cb: + nl_cb_put(cb); +err_free_sock: + nl_socket_free(sock); + + return iface_status; +} + static struct nla_policy link_policy[IFLA_MAX + 1] = { [IFLA_IFNAME] = { .type = NLA_STRING, .maxlen = IFNAMSIZ }, [IFLA_MASTER] = { .type = NLA_U32 }, @@ -45,6 +133,7 @@ struct print_interfaces_rtnl_arg { static int print_interfaces_rtnl_parse(struct nl_msg *msg, void *arg) { struct print_interfaces_rtnl_arg *print_arg = arg; + char iface_status[IFACE_STATUS_LEN]; struct nlattr *attrs[IFLA_MAX + 1]; char path_buff[PATH_BUFF_LEN]; struct ifinfomsg *ifm; @@ -75,7 +164,8 @@ static int print_interfaces_rtnl_parse(struct nl_msg *msg, void *arg) snprintf(path_buff, sizeof(path_buff), SYS_IFACE_STATUS_FMT, ifname); ret = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS, 0, 0, 0); if (ret != EXIT_SUCCESS) - status = "\n"; + status = get_iface_status_netlink(master, ifm->ifi_index, + iface_status); else status = line_ptr;