From patchwork Wed May 11 09:46:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Aring X-Patchwork-Id: 9066911 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 4E274BF29F for ; Wed, 11 May 2016 09:46:37 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5BA102010B for ; Wed, 11 May 2016 09:46:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CF0F42017D for ; Wed, 11 May 2016 09:46:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752155AbcEKJqa (ORCPT ); Wed, 11 May 2016 05:46:30 -0400 Received: from metis.ext.4.pengutronix.de ([92.198.50.35]:41249 "EHLO metis.ext.4.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751565AbcEKJq3 (ORCPT ); Wed, 11 May 2016 05:46:29 -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 1b0Qip-0007bs-RO; Wed, 11 May 2016 11:46:27 +0200 From: Alexander Aring To: linux-wpan@vger.kernel.org Cc: kernel@pengutronix.de, Alexander Aring Subject: [PATCH wpan-tools 2/2] phy: add netns support Date: Wed, 11 May 2016 11:46:19 +0200 Message-Id: <1462959979-6755-2-git-send-email-aar@pengutronix.de> X-Mailer: git-send-email 2.8.2 In-Reply-To: <1462959979-6755-1-git-send-email-aar@pengutronix.de> References: <1462959979-6755-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. Signed-off-by: Alexander Aring Reviewed-by: Stefan Schmidt --- src/phy.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/phy.c b/src/phy.c index 816bdd8..887b610 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 +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");