netns support
Pablo Neira Ayuso
maintainercover netns support
Hi,
This patchset adds native netns support:
1) Add nft_ctx_set_netns() to libnftables to specify the network namespace.
2) Add new -w/--netns option to specify the network namespace from the command line.
Pablo Neira Ayuso (3):
libnftables: add nft_ctx_set_netns()
main: split parsing from libnftables initialization
main: add -w/--netns option
doc/nft.txt | 4 ++ include/namespace.h | 21 ++++++++ include/nftables/libnftables.h | 3 ++ src/Makefile.am | 1 + src/libnftables.c | 20 ++++++++ src/libnftables.map | 4 ++ src/main.c | 53 ++++++++++++-------- src/namespace.c | 107 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 194 insertions(+), 19 deletions(-) create mode 100644 include/namespace.h create mode 100644 src/namespace.c
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 1/3 libnftables: add nft_ctx_set_netns()
This new function allows you to specify the net namespace for the nftables command invocations.
--- include/namespace.h | 21 ++++++++ include/nftables/libnftables.h | 3 ++ src/Makefile.am | 1 + src/libnftables.c | 20 ++++++++ src/libnftables.map | 4 ++ src/namespace.c | 107 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 156 insertions(+) create mode 100644 include/namespace.h create mode 100644 src/namespace.c
include/namespace.h new file +21 -0
@@ -0,0 +1,21 @@ +#ifndef __NAMESPACE_H__ +#define __NAMESPACE_H__ + +#include <sched.h> +#include <sys/mount.h> +#include <unistd.h> +#include <sys/syscall.h> +#include <errno.h> + +/* Use the same default path as iproute2. */ +#ifndef NETNS_RUN_DIR +#define NETNS_RUN_DIR "/var/run/netns" +#endif + +#ifndef NETNS_ETC_DIR +#define NETNS_ETC_DIR "/etc/netns" +#endif + +int netns_switch (const char *netns); + +#endif /* __NAMESPACE_H__ */
include/nftables/libnftables.h +3 -0
@@ -34,10 +34,13 @@ enum nft_debug_level {* Possible flags to pass to nft_ctx_new() */ #define NFT_CTX_DEFAULT 0 +#define NFT_CTX_NETNS 1struct nft_ctx *nft_ctx_new (uint32_t flags);void nft_ctx_free (struct nft_ctx *ctx); +int nft_ctx_set_netns (struct nft_ctx *ctx,const char *netns); +bool nft_ctx_get_dry_run (struct nft_ctx *ctx);void nft_ctx_set_dry_run (struct nft_ctx *ctx,bool dry);
src/Makefile.am +1 -0
@@ -46,6 +46,7 @@ libnftables_la_SOURCES = \ numgen.c \ ct.c \ xfrm.c \ + namespace.c \ netlink.c \ netlink_linearize.c \ netlink_delinearize.c \
src/libnftables.c +20 -0
@@ -12,6 +12,7 @@#include <parser.h> #include <utils.h> #include <iface.h> +#include <namespace.h> #include <errno.h> #include <stdlib.h>
@@ -166,6 +167,25 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)return ctx; } +EXPORT_SYMBOL (nft_ctx_set_netns); +int nft_ctx_set_netns (struct nft_ctx *ctx,const char *netns) +{ +int err; + +if (ctx->flags !=NFT_CTX_NETNS ) { + errno =EOPNOTSUPP ; +return -1 ; + } + + err =netns_switch (netns); +if (err <0 ) +return err; + +nft_ctx_netlink_init (ctx); + +return 0 ; +} +static ssize_t cookie_write (void *cptr,const char *buf,size_t buflen) {struct cookie *cookie = cptr;
src/libnftables.map +4 -0
@@ -23,3 +23,7 @@ global: local: *; }; + +LIBNFTABLES_2 { + nft_ctx_set_netns; +} LIBNFTABLES_1;
src/namespace.c new file +107 -0
107 lines not on this page · raw message
Signature · 1 line
2.11.0
Phil Sutter
reviewerRe: [PATCH nft 1/3] libnftables: add nft_ctx_set_netns()
Hi Pablo,
On Thu, Jan 09, 2020 at 06:21:13PM +0100, Pablo Neira Ayuso wrote:
[...]
include/nftables/libnftables.h enum nft_debug_level { · line 37
Quoted 9 lines
include/nftables/libnftables.h +1
-0@@ -34,10 +34,13 @@ enum nft_debug_level {* Possible flags to pass to nft_ctx_new() */ #define NFT_CTX_DEFAULT 0 +#define NFT_CTX_NETNS 1
What is this needed for?
include/nftables/libnftables.h enum nft_debug_level { · line 42
Quoted 4 lines
include/nftables/libnftables.h +1
-0struct nft_ctx *nft_ctx_new (uint32_t flags);void nft_ctx_free (struct nft_ctx *ctx); +int nft_ctx_set_netns (struct nft_ctx *ctx,const char *netns);
Is there a way to select init ns again?
Thanks, Phil
Pablo Neira Ayuso
maintainerRe: [PATCH nft 1/3] libnftables: add nft_ctx_set_netns()
Quoted 4 lines · Phil Sutter
Hi Pablo,
On Thu, Jan 09, 2020 at 06:21:13PM +0100, Pablo Neira Ayuso wrote:
[...]
Quoted 11 lines
include/nftables/libnftables.h enum nft_debug_level { · line 37
Quoted 9 lines
include/nftables/libnftables.h +1
-0@@ -34,10 +34,13 @@ enum nft_debug_level {* Possible flags to pass to nft_ctx_new() */ #define NFT_CTX_DEFAULT 0 +#define NFT_CTX_NETNS 1What is this needed for?
The socket is initialized from nft_ctx_init(), and such initialization needs to happen after the netns switch.
Quoted 6 lines
include/nftables/libnftables.h enum nft_debug_level { · line 42
Quoted 4 lines
include/nftables/libnftables.h +1
-0struct nft_ctx *nft_ctx_new (uint32_t flags);void nft_ctx_free (struct nft_ctx *ctx); +int nft_ctx_set_netns (struct nft_ctx *ctx,const char *netns);Is there a way to select init ns again?
AFAIK, setns() does not let you go back to init ns once set.
Pablo Neira Ayuso
maintainerRe: [PATCH nft 1/3] libnftables: add nft_ctx_set_netns()
Quoted 17 lines · Pablo Neira Ayuso
Quoted 4 lines · Phil Sutter
Hi Pablo,
On Thu, Jan 09, 2020 at 06:21:13PM +0100, Pablo Neira Ayuso wrote:
[...]Quoted 11 lines
include/nftables/libnftables.h enum nft_debug_level { · line 37
Quoted 9 lines
include/nftables/libnftables.h +1
-0@@ -34,10 +34,13 @@ enum nft_debug_level {* Possible flags to pass to nft_ctx_new() */ #define NFT_CTX_DEFAULT 0 +#define NFT_CTX_NETNS 1What is this needed for?
Quoted 2 lines
The socket is initialized from
nft_ctx_init(), and such initialization needs to happen after the netns switch.
s/nft_ctx_init()/nft_ctx_new()
Quoted 8 lines
Quoted 6 lines
include/nftables/libnftables.h enum nft_debug_level { · line 42
Quoted 4 lines
include/nftables/libnftables.h +1
-0struct nft_ctx *nft_ctx_new (uint32_t flags);void nft_ctx_free (struct nft_ctx *ctx); +int nft_ctx_set_netns (struct nft_ctx *ctx,const char *netns);Is there a way to select init ns again?
AFAIK,
setns()does not let you go back to init ns once set.
Phil Sutter
reviewerRe: [PATCH nft 1/3] libnftables: add nft_ctx_set_netns()
Hi Pablo,
Quoted 16 lines · Pablo Neira Ayuso
Quoted 15 lines · Pablo Neira Ayuso
Quoted 13 lines · Phil Sutter
On Thu, Jan 09, 2020 at 06:21:13PM +0100, Pablo Neira Ayuso wrote:
[...]include/nftables/libnftables.h enum nft_debug_level { · line 37
Quoted 9 lines
include/nftables/libnftables.h +1
-0@@ -34,10 +34,13 @@ enum nft_debug_level {* Possible flags to pass to nft_ctx_new() */ #define NFT_CTX_DEFAULT 0 +#define NFT_CTX_NETNS 1What is this needed for?
Quoted 4 lines
Quoted 2 lines
The socket is initialized from
nft_ctx_init(), and such initialization needs to happen after the netns switch.s/nft_ctx_init()/nft_ctx_new()
Ah, I missed that socket init in nft_ctx_new() happens only if flags is zero.
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
include/nftables/libnftables.h enum nft_debug_level { · line 42
Quoted 4 lines
include/nftables/libnftables.h +1
-0struct nft_ctx *nft_ctx_new (uint32_t flags);void nft_ctx_free (struct nft_ctx *ctx); +int nft_ctx_set_netns (struct nft_ctx *ctx,const char *netns);Is there a way to select init ns again?
AFAIK,
setns()does not let you go back to init ns once set.
I noticed something I find worse, namely that libnftables as a library changes the application's netns. Anything it does after changing the context's netns applies to that netns only, no matter if it's creating a new nft context with NFT_CtX_DEFAULT flag or call iproute via system().
If we can't find a way to exit the netns again, one can safely assume that we are trapping a user's application in a netns with this feature.
Maybe we should restrict per-netns operation to nft utility and perform the netns switch there? Maybe we could provide a "switch_netns()" routine in libnftables which is not bound to nft context so users may use it in their application?
Cheers, Phil
Pablo Neira Ayuso
maintainerRe: [PATCH nft 1/3] libnftables: add nft_ctx_set_netns()
Quoted 36 lines · Phil Sutter
Hi Pablo,
Quoted 16 lines · Pablo Neira Ayuso
Quoted 15 lines · Pablo Neira Ayuso
Quoted 13 lines · Phil Sutter
On Thu, Jan 09, 2020 at 06:21:13PM +0100, Pablo Neira Ayuso wrote:
[...]include/nftables/libnftables.h enum nft_debug_level { · line 37
Quoted 9 lines
include/nftables/libnftables.h +1
-0@@ -34,10 +34,13 @@ enum nft_debug_level {* Possible flags to pass to nft_ctx_new() */ #define NFT_CTX_DEFAULT 0 +#define NFT_CTX_NETNS 1What is this needed for?
Quoted 4 lines
Quoted 2 lines
The socket is initialized from
nft_ctx_init(), and such initialization needs to happen after the netns switch.s/nft_ctx_init()/nft_ctx_new()
Ah, I missed that socket init in
nft_ctx_new()happens only if flags is zero.Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
include/nftables/libnftables.h enum nft_debug_level { · line 42
Quoted 4 lines
include/nftables/libnftables.h +1
-0struct nft_ctx *nft_ctx_new (uint32_t flags);void nft_ctx_free (struct nft_ctx *ctx); +int nft_ctx_set_netns (struct nft_ctx *ctx,const char *netns);Is there a way to select init ns again?
AFAIK,
setns()does not let you go back to init ns once set.
Quoted 7 lines
I noticed something I find worse, namely that libnftables as a library changes the application's netns. Anything it does after changing the context's netns applies to that netns only, no matter if it's creating a new nft context with
NFT_CtX_DEFAULTflag or call iproute viasystem().If we can't find a way to exit the netns again, one can safely assume that we are trapping a user's application in a netns with this feature.
IIRC, you can fork(), then let the child enter the netns while parent remain in the original netns.
Quoted 4 lines
Maybe we should restrict per-netns operation to nft utility and perform the netns switch there? Maybe we could provide a "
switch_netns()" routine in libnftables which is not bound to nft context so users may use it in their application?
That's another possibility, yes. In that case, there is no need for NFT_CTX_NETNS, which is just there to skip the socket initialization.
Phil Sutter
reviewerRe: [PATCH nft 1/3] libnftables: add nft_ctx_set_netns()
Hi Pablo,
Quoted 5 lines · Pablo Neira Ayuso
Quoted 4 lines · Phil Sutter
Quoted 3 lines · Pablo Neira Ayuso
Quoted 2 lines · Pablo Neira Ayuso
Quoted 1 line · Phil Sutter
On Thu, Jan 09, 2020 at 06:21:13PM +0100, Pablo Neira Ayuso wrote:
[...]
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
struct
nft_ctx*nft_ctx_new(uint32_t flags);
voidnft_ctx_free(struct nft_ctx *ctx);+int
nft_ctx_set_netns(struct nft_ctx *ctx, const char *netns);Is there a way to select init ns again?
AFAIK,
setns()does not let you go back to init ns once set.
FWIW, I found interesting Python code[1] dealing with that. The logic is to open /proc/$$/ns/net before switching netns and storing the fd for later. To exit the netns again, it is passed to setns() and then closed. Note that the code there is much simpler and doesn't deal with mounts or non-existing entries in /var/run/netns/. Maybe libnftables doesn't need to either and it is OK to just bail if given netns doesn't exist?
Quoted 10 lines
Quoted 7 lines
I noticed something I find worse, namely that libnftables as a library changes the application's netns. Anything it does after changing the context's netns applies to that netns only, no matter if it's creating a new nft context with
NFT_CtX_DEFAULTflag or call iproute viasystem().If we can't find a way to exit the netns again, one can safely assume that we are trapping a user's application in a netns with this feature.
IIRC, you can
fork(), then let the child enter the netns while parent remain in the original netns.
Yes, that's also the only way to operate in multiple netns in parallel.
Quoted 7 lines
Quoted 4 lines
Maybe we should restrict per-netns operation to nft utility and perform the netns switch there? Maybe we could provide a "
switch_netns()" routine in libnftables which is not bound to nft context so users may use it in their application?That's another possibility, yes. In that case, there is no need for
NFT_CTX_NETNS, which is just there to skip the socket initialization.
I just think that a routine which affects things outside of nft scope shouldn't be tied as closely with nft context.
Cheers, Phil
[1] https://github.com/larsks/python-netns/blob/master/netns.py
Pablo Neira Ayuso
maintainerPATCH 2/3 main: split parsing from libnftables initialization
In reply to Pablo Neira AyusoParse options first, then initialize the nft context object.
--- src/main.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-)
src/main.c +13 -16
@@ -247,18 +247,14 @@ static bool nft_options_check(int argc, char * const argv[])int main (int argc,char *const *argv) { -char *buf =NULL , *filename =NULL ; -unsigned int output_flags =0 ; -bool interactive = false; -unsigned int debug_mask; -unsigned int len; +char *buf =NULL , *filename =NULL , *includepath =NULL ; +unsigned int output_flags =0 , debug_mask =0 , len; +bool interactive = false, dry_run = false;int i, val, rc;if (!nft_options_check (argc, argv))exit (EXIT_FAILURE ); - nft =nft_ctx_new (NFT_CTX_DEFAULT ); -while (1 ) { val =getopt_long (argc, argv,OPTSTRING , options,NULL );if (val ==-1 )
@@ -273,7 +269,7 @@ int main(int argc, char * const *argv) PACKAGE_NAME, PACKAGE_VERSION, RELEASE_NAME);exit (EXIT_SUCCESS );case OPT_CHECK : -nft_ctx_set_dry_run (nft, true); + dry_run = true;break ;case OPT_FILE : filename = optarg;
@@ -282,12 +278,7 @@ int main(int argc, char * const *argv) interactive = true;break ;case OPT_INCLUDEPATH : -if (nft_ctx_add_include_path (nft, optarg)) { -fprintf (stderr, -"Failed to add include path '%s'\n" , - optarg); -exit (EXIT_FAILURE ); - } + includepath = optarg;break ;case OPT_NUMERIC : output_flags |=NFT_CTX_OUTPUT_NUMERIC_ALL ;
@@ -302,7 +293,6 @@ int main(int argc, char * const *argv) output_flags |=NFT_CTX_OUTPUT_SERVICE ;break ;case OPT_DEBUG : - debug_mask =nft_ctx_output_get_debug (nft);for (;;) {unsigned int i;char *end;
@@ -328,7 +318,6 @@ int main(int argc, char * const *argv)break ; optarg = end +1 ; } -nft_ctx_output_set_debug (nft, debug_mask);break ;case OPT_HANDLE_OUTPUT : output_flags |=NFT_CTX_OUTPUT_HANDLE ;
@@ -364,6 +353,14 @@ int main(int argc, char * const *argv) } } + nft =nft_ctx_new (NFT_CTX_DEFAULT ); +if (includepath &&nft_ctx_add_include_path (nft, includepath) <0 ) { +fprintf (stderr,"Failed to add include path '%s'\n" , optarg); +exit (EXIT_FAILURE ); + } + +nft_ctx_set_dry_run (nft, dry_run); +nft_ctx_output_set_debug (nft, debug_mask);nft_ctx_output_set_flags (nft, output_flags);if (optind != argc) {
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerPATCH 3/3 main: add -w/--netns option
In reply to Pablo Neira AyusoThis option allows you to specify which net namespace you want to run this command on.
--- doc/nft.txt | 4 ++++ src/main.c | 28 +++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-)
doc/nft.txt +4 -0
@@ -32,6 +32,10 @@ For a full summary of options, run *nft --help*. *--version*:: Show version. +*-w*:: +*--netns 'namespace'*:: + Run command on the specified net namespace. + *-n*:: *--numeric*:: Print fully numerical output.
src/main.c +23 -5
@@ -28,6 +28,7 @@ enum opt_vals {OPT_HELP ='h' ,OPT_VERSION ='v' ,OPT_CHECK ='c' , +OPT_NETNS ='w' ,OPT_FILE ='f' ,OPT_INTERACTIVE ='i' , OPT_INCLUDEPATH = 'I',
@@ -46,7 +47,7 @@ enum opt_vals {OPT_TERSE ='t' , OPT_INVALID = '?', }; -#define OPTSTRING "+hvd:cf:iI:jvnsNaeSupypTt" +#define OPTSTRING "+hvd:cf:iI:jvnsNaeSupypTtw:"static const struct option options[] = { {
@@ -129,6 +130,11 @@ static const struct option options[] = { .val = OPT_TERSE, }, { + .name = "netns", + .val = OPT_NETNS, + .has_arg = 1, + }, + { .name = NULL } };
@@ -150,6 +156,7 @@ static void show_help(const char *name)" -n, --numeric Print fully numerical output.\n" " -s, --stateless Omit stateful information of ruleset.\n" " -t, --terse Omit contents of sets.\n" +" -w, --netns Run command on network namespace\n" " -u, --guid Print UID/GID as defined in /etc/passwd and /etc/group.\n" " -N Translate IP addresses to names.\n" " -S, --service Translate ports to service names as described in /etc/services.\n"
@@ -231,9 +238,11 @@ static bool nft_options_check(int argc, char * const argv[]) }else if (argv[i][1 ] =='d' || argv[i][1 ] =='I' || argv[i][1 ] =='f' || + argv[i][1 ] =='w' || !strcmp (argv[i],"--debug" ) || !strcmp (argv[i],"--includepath" ) || - !strcmp (argv[i],"--file" )) { + !strcmp (argv[i],"--file" ) || + !strcmp (argv[i],"--netns" )) { skip = true;continue ; }
@@ -247,10 +256,10 @@ static bool nft_options_check(int argc, char * const argv[])int main (int argc,char *const *argv) { -char *buf =NULL , *filename =NULL , *includepath =NULL ; +char *buf =NULL , *filename =NULL , *includepath =NULL , *netns =NULL ;unsigned int output_flags =0 , debug_mask =0 , len;bool interactive = false, dry_run = false; -int i, val, rc; +int i, val, rc, ctx =NFT_CTX_DEFAULT ;if (!nft_options_check (argc, argv))exit (EXIT_FAILURE );
@@ -271,6 +280,9 @@ int main(int argc, char * const *argv)case OPT_CHECK : dry_run = true;break ; +case OPT_NETNS : + netns = optarg; +break ;case OPT_FILE : filename = optarg;break ;
@@ -353,12 +365,18 @@ int main(int argc, char * const *argv) } } - nft =nft_ctx_new (NFT_CTX_DEFAULT ); +if (netns) + ctx =NFT_CTX_NETNS ; + + nft =nft_ctx_new (ctx);if (includepath &&nft_ctx_add_include_path (nft, includepath) <0 ) {fprintf (stderr,"Failed to add include path '%s'\n" , optarg);exit (EXIT_FAILURE ); } +if (netns &&nft_ctx_set_netns (nft, netns) <0 ) +exit (EXIT_FAILURE ); +nft_ctx_set_dry_run (nft, dry_run);nft_ctx_output_set_debug (nft, debug_mask);nft_ctx_output_set_flags (nft, output_flags);
Signature · 1 line
2.11.0