summaryrefslogtreecommitdiffstats
path: root/src/netlink.c
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2017-11-11 00:06:17 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2017-11-16 14:31:32 +0100
commit657fdefc7d5c70056b8738da214d3d78f421b19c (patch)
treeb059d870dd19ced851278f1c1401dc7e536f3987 /src/netlink.c
parent0ca812ad41bf2d89073a43f8efd3ee712031e3cb (diff)
Eliminate struct mnl_ctx
The issue leading to this patch was that debug output in nft_mnl_talk() bypasses the application-defined output_fp. While investigating, another problem was discovered: Most of the ad-hoc defined mnl_ctx objects have their field 'debug_mask' set to zero regardless of what netlink_ctx contains (this affects non-batch code path only). The intuitive solution to both of those issues required to extend function parameters of all the non-batch functions as well as the common nft_mnl_talk() one. Instead of complicating them even further, this patch instead makes them accept a pointer to netlink_ctx as first parameter to gather both the old (nf_sock, seqnum) and the new values (debug_mask, octx) from. Since after the above change struct mnl_ctx was not really used anymore, so the remaining places were adjusted as well to allow for removing the struct altogether. Note that cache routines needed special treatment: Although parameters of cache_update() make it a candidate for the same change, it can't be converted since it is called in evaluation phase sometimes in which there is no netlink context available (but just eval context instead). Since netlink_genid_get() needs a netlink context though, the ad-hoc netlink_ctx definition from cache_init() is moved into cache_update() to have it available there already. Signed-off-by: Phil Sutter <phil@nwl.cc> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/netlink.c')
-rw-r--r--src/netlink.c48
1 files changed, 22 insertions, 26 deletions
diff --git a/src/netlink.c b/src/netlink.c
index 845eeeff..6735971a 100644
--- a/src/netlink.c
+++ b/src/netlink.c
@@ -75,9 +75,9 @@ void netlink_restart(struct mnl_socket *nf_sock)
nf_sock = netlink_open_sock();
}
-uint16_t netlink_genid_get(struct mnl_socket *nf_sock, uint32_t seqnum)
+uint16_t netlink_genid_get(struct netlink_ctx *ctx)
{
- return mnl_genid_get(nf_sock, seqnum);
+ return mnl_genid_get(ctx);
}
void __noreturn __netlink_abi_error(const char *file, int line,
@@ -556,7 +556,7 @@ static int netlink_list_rules(struct netlink_ctx *ctx, const struct handle *h,
{
struct nftnl_rule_list *rule_cache;
- rule_cache = mnl_nft_rule_dump(ctx->nf_sock, h->family, ctx->seqnum);
+ rule_cache = mnl_nft_rule_dump(ctx, h->family);
if (rule_cache == NULL) {
if (errno == EINTR)
return -1;
@@ -611,7 +611,7 @@ static int netlink_add_chain_compat(struct netlink_ctx *ctx,
}
netlink_dump_chain(nlc, ctx);
- err = mnl_nft_chain_add(ctx->nf_sock, nlc, flags, ctx->seqnum);
+ err = mnl_nft_chain_add(ctx, nlc, flags);
nftnl_chain_free(nlc);
if (err < 0)
@@ -677,7 +677,7 @@ static int netlink_rename_chain_compat(struct netlink_ctx *ctx,
nlc = alloc_nftnl_chain(h);
nftnl_chain_set_str(nlc, NFTNL_CHAIN_NAME, name);
netlink_dump_chain(nlc, ctx);
- err = mnl_nft_chain_add(ctx->nf_sock, nlc, 0, ctx->seqnum);
+ err = mnl_nft_chain_add(ctx, nlc, 0);
nftnl_chain_free(nlc);
if (err < 0)
@@ -724,7 +724,7 @@ static int netlink_del_chain_compat(struct netlink_ctx *ctx,
nlc = alloc_nftnl_chain(h);
netlink_dump_chain(nlc, ctx);
- err = mnl_nft_chain_delete(ctx->nf_sock, nlc, 0, ctx->seqnum);
+ err = mnl_nft_chain_delete(ctx, nlc, 0);
nftnl_chain_free(nlc);
if (err < 0)
@@ -827,7 +827,7 @@ int netlink_list_chains(struct netlink_ctx *ctx, const struct handle *h,
struct nftnl_chain_list *chain_cache;
struct chain *chain;
- chain_cache = mnl_nft_chain_dump(ctx->nf_sock, h->family, ctx->seqnum);
+ chain_cache = mnl_nft_chain_dump(ctx, h->family);
if (chain_cache == NULL) {
if (errno == EINTR)
return -1;
@@ -870,7 +870,7 @@ static int netlink_add_table_compat(struct netlink_ctx *ctx,
int err;
nlt = alloc_nftnl_table(h);
- err = mnl_nft_table_add(ctx->nf_sock, nlt, flags, ctx->seqnum);
+ err = mnl_nft_table_add(ctx, nlt, flags);
nftnl_table_free(nlt);
if (err < 0)
@@ -920,7 +920,7 @@ static int netlink_del_table_compat(struct netlink_ctx *ctx,
int err;
nlt = alloc_nftnl_table(h);
- err = mnl_nft_table_delete(ctx->nf_sock, nlt, 0, ctx->seqnum);
+ err = mnl_nft_table_delete(ctx, nlt, 0);
nftnl_table_free(nlt);
if (err < 0)
@@ -984,7 +984,7 @@ int netlink_list_tables(struct netlink_ctx *ctx, const struct handle *h,
{
struct nftnl_table_list *table_cache;
- table_cache = mnl_nft_table_dump(ctx->nf_sock, h->family, ctx->seqnum);
+ table_cache = mnl_nft_table_dump(ctx, h->family);
if (table_cache == NULL) {
if (errno == EINTR)
return -1;
@@ -1173,8 +1173,7 @@ static int netlink_add_set_compat(struct netlink_ctx *ctx,
}
netlink_dump_set(nls, ctx);
- err = mnl_nft_set_add(ctx->nf_sock, nls, NLM_F_ECHO | flags,
- ctx->seqnum);
+ err = mnl_nft_set_add(ctx, nls, NLM_F_ECHO | flags);
if (err < 0)
netlink_io_error(ctx, &set->location, "Could not add set: %s",
strerror(errno));
@@ -1270,7 +1269,7 @@ static int netlink_del_set_compat(struct netlink_ctx *ctx,
int err;
nls = alloc_nftnl_set(h);
- err = mnl_nft_set_delete(ctx->nf_sock, nls, 0, ctx->seqnum);
+ err = mnl_nft_set_delete(ctx, nls, 0);
nftnl_set_free(nls);
if (err < 0)
@@ -1323,8 +1322,7 @@ int netlink_list_sets(struct netlink_ctx *ctx, const struct handle *h,
struct nftnl_set_list *set_cache;
int err;
- set_cache = mnl_nft_set_dump(ctx->nf_sock, h->family, h->table,
- ctx->seqnum);
+ set_cache = mnl_nft_set_dump(ctx, h->family, h->table);
if (set_cache == NULL) {
if (errno == EINTR)
return -1;
@@ -1379,7 +1377,7 @@ static int netlink_add_setelems_compat(struct netlink_ctx *ctx,
alloc_setelem_cache(expr, nls);
netlink_dump_set(nls, ctx);
- err = mnl_nft_setelem_add(ctx->nf_sock, nls, flags, ctx->seqnum);
+ err = mnl_nft_setelem_add(ctx, nls, flags);
nftnl_set_free(nls);
if (err < 0)
netlink_io_error(ctx, &expr->location,
@@ -1429,7 +1427,7 @@ static int netlink_del_setelems_compat(struct netlink_ctx *ctx,
alloc_setelem_cache(expr, nls);
netlink_dump_set(nls, ctx);
- err = mnl_nft_setelem_delete(ctx->nf_sock, nls, 0, ctx->seqnum);
+ err = mnl_nft_setelem_delete(ctx, nls, 0);
nftnl_set_free(nls);
if (err < 0)
netlink_io_error(ctx, &expr->location,
@@ -1634,7 +1632,7 @@ int netlink_get_setelems(struct netlink_ctx *ctx, const struct handle *h,
nls = alloc_nftnl_set(h);
- err = mnl_nft_setelem_get(ctx->nf_sock, nls, ctx->seqnum);
+ err = mnl_nft_setelem_get(ctx, nls);
if (err < 0) {
nftnl_set_free(nls);
if (errno == EINTR)
@@ -1780,7 +1778,7 @@ int netlink_list_objs(struct netlink_ctx *ctx, const struct handle *h,
struct nftnl_obj_list *obj_cache;
int err;
- obj_cache = mnl_nft_obj_dump(ctx->nf_sock, h->family, ctx->seqnum,
+ obj_cache = mnl_nft_obj_dump(ctx, h->family,
h->table, NULL, 0, true, false);
if (obj_cache == NULL) {
if (errno == EINTR)
@@ -1800,7 +1798,7 @@ int netlink_reset_objs(struct netlink_ctx *ctx, const struct handle *h,
struct nftnl_obj_list *obj_cache;
int err;
- obj_cache = mnl_nft_obj_dump(ctx->nf_sock, h->family, ctx->seqnum,
+ obj_cache = mnl_nft_obj_dump(ctx, h->family,
h->table, h->obj, type, dump, true);
if (obj_cache == NULL) {
if (errno == EINTR)
@@ -1846,7 +1844,7 @@ struct nftnl_ruleset *netlink_dump_ruleset(struct netlink_ctx *ctx,
{
struct nftnl_ruleset *rs;
- rs = mnl_nft_ruleset_dump(ctx->nf_sock, h->family, ctx->seqnum);
+ rs = mnl_nft_ruleset_dump(ctx, h->family);
if (rs == NULL) {
if (errno == EINTR)
return NULL;
@@ -3011,10 +3009,6 @@ int netlink_echo_callback(const struct nlmsghdr *nlh, void *data)
int netlink_monitor(struct netlink_mon_handler *monhandler,
struct mnl_socket *nf_sock)
{
- struct mnl_ctx ctx = {
- .nf_sock = nf_sock,
- .debug_mask = monhandler->debug_mask,
- };
int group;
if (monhandler->monitor_flags & (1 << NFT_MSG_TRACE)) {
@@ -3036,7 +3030,9 @@ int netlink_monitor(struct netlink_mon_handler *monhandler,
strerror(errno));
}
- return mnl_nft_event_listener(&ctx, netlink_events_cb, monhandler);
+ return mnl_nft_event_listener(nf_sock, monhandler->debug_mask,
+ monhandler->ctx->octx, netlink_events_cb,
+ monhandler);
}
bool netlink_batch_supported(struct mnl_socket *nf_sock, uint32_t *seqnum)