From patchwork Thu May 12 00:45:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Aring X-Patchwork-Id: 9076111 Return-Path: X-Original-To: patchwork-linux-wpan@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 9E1DDBF29F for ; Thu, 12 May 2016 00:46:26 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3368E201BB for ; Thu, 12 May 2016 00:46:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 764872012B for ; Thu, 12 May 2016 00:46:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751450AbcELAqG (ORCPT ); Wed, 11 May 2016 20:46:06 -0400 Received: from metis.ext.4.pengutronix.de ([92.198.50.35]:45645 "EHLO metis.ext.4.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751434AbcELAqG (ORCPT ); Wed, 11 May 2016 20:46:06 -0400 Received: from gallifrey.ext.pengutronix.de ([2001:67c:670:201:5054:ff:fe8d:eefb] helo=omega.localdomain) by metis.ext.pengutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1b0elQ-0005Ku-2F; Thu, 12 May 2016 02:46:04 +0200 From: Alexander Aring To: linux-wpan@vger.kernel.org Cc: kernel@pengutronix.de, Alexander Aring Subject: [PATCHv2 wpan-tools 2/2] phy: add netns support Date: Thu, 12 May 2016 02:45:49 +0200 Message-Id: <1463013949-6028-2-git-send-email-aar@pengutronix.de> X-Mailer: git-send-email 2.8.2 In-Reply-To: <1463013949-6028-1-git-send-email-aar@pengutronix.de> References: <1463013949-6028-1-git-send-email-aar@pengutronix.de> X-SA-Exim-Connect-IP: 2001:67c:670:201:5054:ff:fe8d:eefb X-SA-Exim-Mail-From: aar@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-wpan@vger.kernel.org Sender: linux-wpan-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wpan@vger.kernel.org X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds netns support, basically most parts are copy&pasted from iw tool, same handling just wpan instead wireless. Reviewed-by: Stefan Schmidt Signed-off-by: Alexander Aring --- changes since v2: - add static to handle_netns - add Reviewed-by tags src/phy.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/phy.c b/src/phy.c index 816bdd8..f97900f 100644 --- a/src/phy.c +++ b/src/phy.c @@ -2,6 +2,10 @@ #include #include #include +#include +#include +#include +#include #include #include @@ -144,3 +148,65 @@ nla_put_failure: } COMMAND(set, cca_ed_level, "", NL802154_CMD_SET_CCA_ED_LEVEL, 0, CIB_PHY, handle_cca_ed_level, NULL); + +#ifndef NETNS_RUN_DIR +#define NETNS_RUN_DIR "/var/run/netns" +#endif +static int netns_get_fd(const char *name) +{ + char pathbuf[MAXPATHLEN]; + const char *path, *ptr; + + path = name; + ptr = strchr(name, '/'); + if (!ptr) { + snprintf(pathbuf, sizeof(pathbuf), "%s/%s", + NETNS_RUN_DIR, name ); + path = pathbuf; + } + return open(path, O_RDONLY); +} + +static int handle_netns(struct nl802154_state *state, + struct nl_cb *cb, + struct nl_msg *msg, + int argc, char **argv, + enum id_input id) +{ + char *end; + int fd; + + if (argc < 1 || !*argv[0]) + return 1; + + if (argc == 1) { + NLA_PUT_U32(msg, NL802154_ATTR_PID, + strtoul(argv[0], &end, 10)); + if (*end != '\0') { + printf("Invalid parameter: pid(%s)\n", argv[0]); + return 1; + } + return 0; + } + + if (argc != 2 || strcmp(argv[0], "name")) + return 1; + + if ((fd = netns_get_fd(argv[1])) >= 0) { + NLA_PUT_U32(msg, NL802154_ATTR_NETNS_FD, fd); + return 0; + } else { + printf("Invalid parameter: nsname(%s)\n", argv[0]); + } + + return 1; + + nla_put_failure: + return -ENOBUFS; +} +COMMAND(set, netns, "{ | name }", + NL802154_CMD_SET_WPAN_PHY_NETNS, 0, CIB_PHY, handle_netns, + "Put this wpan device into a different network namespace:\n" + " - change network namespace by process id\n" + " - change network namespace by name from "NETNS_RUN_DIR"\n" + " or by absolute path (man ip-netns)\n");