[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] ip link: add set netns support
From:       Alex Xu <alex_y_xu () yahoo ! ca>
Date:       2016-07-29 21:12:41
Message-ID: 20160729211241.21144-1-alex_y_xu () yahoo ! ca
[Download RAW message or body]

"ip link add netns" support was not added because nsenter -n ip link add
has the same effect.

Signed-off-by: Alex Xu <alex_y_xu@yahoo.ca>
---
 networking/libiproute/iplink.c | 45 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 7 deletions(-)

diff --git a/networking/libiproute/iplink.c b/networking/libiproute/iplink.c
index ae3ef0c..4d1946e 100644
--- a/networking/libiproute/iplink.c
+++ b/networking/libiproute/iplink.c
@@ -43,6 +43,12 @@ struct ifla_vlan_flags {
 };
 #endif
 
+struct iplink_req {
+        struct nlmsghdr         n;
+        struct ifinfomsg        i;
+        char                    buf[1024];
+};
+
 /* taken from linux/sockios.h */
 #define SIOCSIFNAME  0x8923  /* set interface name */
 
@@ -124,6 +130,24 @@ static void set_mtu(char *dev, int mtu)
 	close(s);
 }
 
+static int set_netns(char *dev, int netns)
+{
+	struct iplink_req req;
+	struct rtnl_handle rth;
+	memset(&req, 0, sizeof(req));
+	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
+	req.n.nlmsg_flags = NLM_F_REQUEST;
+	req.n.nlmsg_type = RTM_NEWLINK;
+	req.i.ifi_family = preferred_family;
+	req.i.ifi_index = xll_name_to_index(dev);
+	addattr_l(&req.n, sizeof(req), IFLA_NET_NS_FD, &netns, 4);
+	xrtnl_open(&rth);
+	ll_init_map(&rth);
+	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
+		return 2;
+	return 0;
+}
+
 /* Exits on error */
 static int get_address(char *dev, int *htype)
 {
@@ -206,12 +230,13 @@ static int do_set(char **argv)
 	int htype, halen;
 	static const char keywords[] ALIGN1 =
 		"up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
-		"arp\0""address\0""dev\0";
+		"arp\0""address\0""netns\0""dev\0";
 	enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
-		ARG_arp, ARG_addr, ARG_dev };
+		ARG_arp, ARG_addr, ARG_netns, ARG_dev };
 	static const char str_on_off[] ALIGN1 = "on\0""off\0";
 	enum { PARM_on = 0, PARM_off };
 	smalluint key;
+	int netns = -1;
 
 	while (*argv) {
 		/* substring search ensures that e.g. "addr" and "address"
@@ -239,6 +264,13 @@ static int do_set(char **argv)
 		} else if (key == ARG_addr) {
 			NEXT_ARG();
 			newaddr = *argv;
+		} else if (key == ARG_netns) {
+			NEXT_ARG();
+			if (netns != -1)
+				duparg("netns", *argv);
+			netns = open(*argv, O_RDONLY);
+			if (netns == -1)
+				invarg_1_to_2(*argv, "netns");
 		} else if (key >= ARG_dev) {
 			if (key == ARG_dev) {
 				NEXT_ARG();
@@ -297,6 +329,9 @@ static int do_set(char **argv)
 	if (mtu != -1) {
 		set_mtu(dev, mtu);
 	}
+	if (netns != -1)
+		if (set_netns(dev, netns))
+			return 2;
 	if (mask)
 		do_chflags(dev, flags, mask);
 	return 0;
@@ -416,11 +451,7 @@ static int do_add_or_delete(char **argv, const unsigned rtm)
 		ARG_address,
 	};
 	struct rtnl_handle rth;
-	struct {
-		struct nlmsghdr  n;
-		struct ifinfomsg i;
-		char             buf[1024];
-	} req;
+	struct iplink_req req;
 	smalluint arg;
 	char *name_str = NULL;
 	char *link_str = NULL;
-- 
2.9.2

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic