From patchwork Wed Jul 13 15:30:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 16507 X-Patchwork-Delegate: mareklindner@neomailbox.ch 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 A8D8B8270B; Wed, 13 Jul 2016 17:30:46 +0200 (CEST) Authentication-Results: open-mesh.org; dmarc=none header.from=narfation.org Authentication-Results: open-mesh.org; dkim=fail reason="verification failed; unprotected key" header.d=narfation.org header.i=@narfation.org header.b=inN4ZvQ+; dkim-adsp=fail (unprotected policy); dkim-atps=neutral Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=79.140.41.39; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver=b.a.t.m.a.n@lists.open-mesh.org Authentication-Results: open-mesh.org; dmarc=pass header.from=narfation.org Received: from v3-1039.vlinux.de (narfation.org [79.140.41.39]) by open-mesh.org (Postfix) with ESMTPS id 21159824B8 for ; Wed, 13 Jul 2016 17:30:32 +0200 (CEST) Received: from sven-desktop.home.narfation.org (p2003007C6F04E6FED85BF65F15063EF0.dip0.t-ipconnect.de [IPv6:2003:7c:6f04:e6fe:d85b:f65f:1506:3ef0]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id A73921100F1; Wed, 13 Jul 2016 17:30:31 +0200 (CEST) Authentication-Results: v3-1039.vlinux.de; dmarc=none header.from=narfation.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=narfation.org; s=20121; t=1468423831; bh=gyo2/NLnVVOIPG6Br6TSxgSDpBj/11wK1qyFCXFv9eE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=inN4ZvQ+j9jY7g3Sq0nvlpVlQ25fbAzYfVMQSfJR6N/lda8Xfq3BFhd43UuTyClXb 9h3TShHtWRDVNdiFkry7pYlG9m2rUy1Rw7cR77AqtzX7/r/+LY2dZe3/yI7kvJzRQ8 9sxY6CcuwzqjoJWQDUqshFjdf4TXRp+sa+d/x7lk= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Wed, 13 Jul 2016 17:30:18 +0200 Message-Id: <1468423822-28324-3-git-send-email-sven@narfation.org> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1468423822-28324-1-git-send-email-sven@narfation.org> References: <35819279.yt4EnXKmx6@bentobox> <1468423822-28324-1-git-send-email-sven@narfation.org> Subject: [B.A.T.M.A.N.] [PATCH 3/7] batctl: Use rtnl to add/remove interfaces X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.18 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 sysfs interface to add/remove interfaces to/from a batman-adv soft-interface was downgraded in batman-adv master to a second-class citizen. This was done because it has conceptional problems (for example locking of sysfs vs. locking of the network core code). The only direct way to modify network interfaces is rtnetlink. sysfs still exists but has to use workers which delay the actual add/del to a later point. It is therefore prefered to use the modern rtnetlink. Only batman-adv versions older than v2013.2.0 (Linux 3.10) will not yet support the rtnl_link operations required for it to work. Signed-off-by: Sven Eckelmann --- sys.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 145 insertions(+), 27 deletions(-) diff --git a/sys.c b/sys.c index 252864b..ac78d37 100644 --- a/sys.c +++ b/sys.c @@ -199,6 +199,58 @@ static int print_interfaces(char *mesh_iface) return EXIT_SUCCESS; } +struct count_interfaces_rtnl_arg { + int ifindex; + unsigned int count; +}; + +static int count_interfaces_rtnl_parse(struct nl_msg *msg, void *arg) +{ + struct count_interfaces_rtnl_arg *count_arg = arg; + struct nlattr *attrs[IFLA_MAX + 1]; + struct ifinfomsg *ifm; + int ret; + int master; + + ifm = nlmsg_data(nlmsg_hdr(msg)); + ret = nlmsg_parse(nlmsg_hdr(msg), sizeof(*ifm), attrs, IFLA_MAX, + link_policy); + if (ret < 0) + goto err; + + if (!attrs[IFLA_IFNAME]) + goto err; + + if (!attrs[IFLA_MASTER]) + goto err; + + master = nla_get_u32(attrs[IFLA_MASTER]); + + /* required on older kernels which don't prefilter the results */ + if (master != count_arg->ifindex) + goto err; + + count_arg->count++; + +err: + return NL_OK; +} + +static unsigned int count_interfaces(char *mesh_iface) +{ + struct count_interfaces_rtnl_arg count_arg; + + count_arg.count = 0; + count_arg.ifindex = if_nametoindex(mesh_iface); + if (!count_arg.ifindex) + return 0; + + query_rtnl_link(count_arg.ifindex, count_interfaces_rtnl_parse, + &count_arg); + + return count_arg.count; +} + static int new_interface(const char *mesh_iface) { struct ifinfomsg rt_hdr = { @@ -283,11 +335,54 @@ err_free_msg: return err; } +static int set_master_interface(const char *iface, unsigned int ifmaster) +{ + struct ifinfomsg rt_hdr = { + .ifi_family = IFLA_UNSPEC, + }; + struct nl_msg *msg; + int err = 0; + int ret; + + msg = nlmsg_alloc_simple(RTM_SETLINK, NLM_F_REQUEST | NLM_F_ACK); + if (!msg) { + return -ENOMEM; + } + + ret = nlmsg_append(msg, &rt_hdr, sizeof(rt_hdr), NLMSG_ALIGNTO); + if (ret < 0) { + err = -ENOMEM; + goto err_free_msg; + } + + ret = nla_put_string(msg, IFLA_IFNAME, iface); + if (ret < 0) { + err = -ENOMEM; + goto err_free_msg; + } + + ret = nla_put_u32(msg, IFLA_MASTER, ifmaster); + if (ret < 0) { + err = -ENOMEM; + goto err_free_msg; + } + + err = netlink_simple_request(msg); + +err_free_msg: + nlmsg_free(msg); + + return err; +} + int interface(char *mesh_iface, int argc, char **argv) { - char *path_buff; - int i, res, optchar; + int i, optchar; int ret; + unsigned int ifindex; + unsigned int ifmaster; + const char *long_op; + unsigned int cnt; while ((optchar = getopt(argc, argv, "h")) != -1) { switch (optchar) { @@ -363,46 +458,69 @@ int interface(char *mesh_iface, int argc, char **argv) break; } - path_buff = malloc(PATH_BUFF_LEN); - if (!path_buff) { - fprintf(stderr, "Error - could not allocate path buffer: out of memory ?\n"); - goto err; - } + /* get index of batman-adv interface - or try to create it */ + ifmaster = if_nametoindex(mesh_iface); + if (!ifmaster && argv[1][0] == 'a') { + ret = new_interface(mesh_iface); + if (ret < 0) { + fprintf(stderr, + "Error - failed to add new batman-adv interface: %s\n", + strerror(-ret)); + goto err; + } - for (i = 2; i < argc; i++) { - snprintf(path_buff, PATH_BUFF_LEN, SYS_MESH_IFACE_FMT, argv[i]); + ifmaster = if_nametoindex(mesh_iface); + } - if (!file_exists(path_buff)) { - snprintf(path_buff, PATH_BUFF_LEN, SYS_IFACE_DIR, argv[i]); + if (!ifmaster) { + ret = -ENODEV; + fprintf(stderr, + "Error - failed to find batman-adv interface: %s\n", + strerror(-ret)); + goto err; + } - if (!file_exists(path_buff)) { - fprintf(stderr, "Error - interface does not exist: %s\n", argv[i]); - continue; - } + /* make sure that batman-adv is loaded or was loaded by new_interface */ + if (!file_exists(module_ver_path)) { + fprintf(stderr, "Error - batman-adv module has not been loaded\n"); + goto err; + } - if (!file_exists(module_ver_path)) { - fprintf(stderr, "Error - batman-adv module has not been loaded\n"); - goto err_buff; - } + for (i = 2; i < argc; i++) { + ifindex = if_nametoindex(argv[i]); - fprintf(stderr, "Error - interface type not supported by batman-adv: %s\n", argv[i]); + if (!ifindex) { + fprintf(stderr, "Error - interface does not exist: %s\n", argv[i]); continue; } if (argv[1][0] == 'a') - res = write_file("", path_buff, mesh_iface, NULL); + ifindex = ifmaster; else - res = write_file("", path_buff, "none", NULL); + ifindex = 0; - if (res != EXIT_SUCCESS) - goto err_buff; + ret = set_master_interface(argv[i], ifindex); + if (ret < 0) { + if (argv[1][0] == 'a') + long_op = "add"; + else + long_op = "delete"; + + fprintf(stderr, "Error - failed to %s interface %s: %s\n", + long_op, argv[i], strerror(-ret)); + goto err; + } + } + + /* check if there is no interface left and then destroy mesh_iface */ + if (argv[1][0] == 'd') { + cnt = count_interfaces(mesh_iface); + if (cnt == 0) + destroy_interface(mesh_iface); } - free(path_buff); return EXIT_SUCCESS; -err_buff: - free(path_buff); err: return EXIT_FAILURE; }