netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: "Carlos Falgueras García" <carlosfg@riseup.net>
Cc: netfilter-devel@vger.kernel.org, kaber@trash.net
Subject: Re: [PATCH 4/4 v5] nftables: rule: Change the field "rule->comment" for an nftnl_udata_buf
Date: Mon, 21 Mar 2016 23:13:30 +0100	[thread overview]
Message-ID: <20160321221330.GA2534@salvia> (raw)
In-Reply-To: <1458073687-23870-4-git-send-email-carlosfg@riseup.net>

On Tue, Mar 15, 2016 at 09:28:07PM +0100, Carlos Falgueras García wrote:
> Now it is possible to store multiple variable length user data into rule.
> Modify the parser in order to fill the nftnl_udata with the comment, and
> the print function for extract these commentary and print it to user.
> 
> Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
> ---
>  include/rule.h            |  7 +++++++
>  src/netlink_delinearize.c | 52 +++++++++++++++++++++++++++++++++++++++++++++--
>  src/netlink_linearize.c   | 16 +++++++++++++--
>  3 files changed, 71 insertions(+), 4 deletions(-)
> 
> diff --git a/include/rule.h b/include/rule.h
> index c848f0f..b52f0ac 100644
> --- a/include/rule.h
> +++ b/include/rule.h
> @@ -4,6 +4,7 @@
>  #include <stdint.h>
>  #include <nftables.h>
>  #include <list.h>
> +#include <libnftnl/udata.h>
>  
>  /**
>   * struct handle - handle for tables, chains, rules and sets
> @@ -396,4 +397,10 @@ extern int do_command(struct netlink_ctx *ctx, struct cmd *cmd);
>  extern int cache_update(enum cmd_ops cmd, struct list_head *msgs);
>  extern void cache_release(void);
>  
> +enum udata_type {
> +	UDATA_TYPE_COMMENT,
> +	__UDATA_TYPE_MAX,
> +};
> +#define UDATA_TYPE_MAX (__UDATA_TYPE_MAX - 1)
> +
>  #endif /* NFTABLES_RULE_H */
> diff --git a/src/netlink_delinearize.c b/src/netlink_delinearize.c
> index d431588..5fcb5c1 100644
> --- a/src/netlink_delinearize.c
> +++ b/src/netlink_delinearize.c
> @@ -25,6 +25,7 @@
>  #include <utils.h>
>  #include <erec.h>
>  #include <sys/socket.h>
> +#include <libnftnl/udata.h>
>  
>  struct netlink_parse_ctx {
>  	struct list_head	*msgs;
> @@ -1746,6 +1747,54 @@ static void rule_parse_postprocess(struct netlink_parse_ctx *ctx, struct rule *r
>  	}
>  }
>  
> +static int parse_udata_cb(const struct nftnl_udata *attr, void *data)
> +{
> +	unsigned char *value = nftnl_udata_attr_value(attr);
> +	uint8_t type = nftnl_udata_attr_type(attr);
> +	uint8_t len = nftnl_udata_attr_len(attr);
> +	const struct nftnl_udata **tb = data;
> +
> +	switch (type) {
> +	case UDATA_TYPE_COMMENT:
> +		if (value[len - 1] != '\0')
> +			return -1;
> +		break;
> +	default:
> +		break;
> +	};
> +
> +	tb[type] = attr;
> +	return 1;
> +}
> +
> +static char *udata_get_comment(const void *data, uint32_t data_len)
> +{
> +	const struct nftnl_udata *tb[UDATA_TYPE_MAX + 1] = {};
> +	struct nftnl_udata_buf *udata;
> +	uint8_t attr_len;
> +	char *comment = NULL;
> +
> +	udata = nftnl_udata_alloc(data_len);
> +	if (!udata)
> +		memory_allocation_error();
> +	nftnl_udata_copy_data(udata, data, data_len);
> +
> +	if (nftnl_udata_parse(udata, parse_udata_cb, tb) <= 0)
> +		goto exit;

I think this should be instead:

	if (nftnl_udata_parse(data, data_len, parse_udata_cb, tb) <= 0)

So you don't need to allocate the buffer then copy data into it.

I think the buffer infrastructure is only necessary to build the TLV
attributes, not to parse it.

> +	if (!tb[UDATA_TYPE_COMMENT])
> +		goto exit;
> +
> +	attr_len = nftnl_udata_attr_len(tb[UDATA_TYPE_COMMENT]);
> +	comment = xmalloc(attr_len);
> +	memcpy(comment, nftnl_udata_attr_value(tb[UDATA_TYPE_COMMENT]),
> +	       attr_len);

I'd suggest:

        comment = xstrdup(nftnl_udata_attr_get_str(tb[UDATA_TYPE_COMMENT]));

> +
> +exit:
> +	nftnl_udata_free(udata);
> +	return comment;
> +}
> +
>  struct rule *netlink_delinearize_rule(struct netlink_ctx *ctx,
>  				      const struct nftnl_rule *nlr)
>  {
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2016-03-21 22:13 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-15 20:28 [PATCH 1/4 v5] libnftnl: Implement new buffer of TLV objects Carlos Falgueras García
2016-03-15 20:28 ` [PATCH 2/4 v5] libnftnl: rule: Change the "userdata" attribute to use new TLV buffer Carlos Falgueras García
2016-03-15 20:28 ` [PATCH 3/4 v5] libnftnl: test: Update test to check new nftnl_udata features of nftnl_rule Carlos Falgueras García
2016-03-15 20:28 ` [PATCH 4/4 v5] nftables: rule: Change the field "rule->comment" for an nftnl_udata_buf Carlos Falgueras García
2016-03-21 22:13   ` Pablo Neira Ayuso [this message]
2016-03-22 11:37     ` Carlos Falgueras García
2016-03-21 22:10 ` [PATCH 1/4 v5] libnftnl: Implement new buffer of TLV objects Pablo Neira Ayuso
2016-03-22 11:36   ` Carlos Falgueras García
2016-03-22 16:55     ` Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160321221330.GA2534@salvia \
    --to=pablo@netfilter.org \
    --cc=carlosfg@riseup.net \
    --cc=kaber@trash.net \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).