Add translation for match comment to nftables.
This patch also adds the relevant infrastructure for carrying out
the translation.
Example:
$ sudo iptables-translate -A INPUT -s 192.168.0.0 -m comment --comment "A privatized IP block"
nft add rule ip filter INPUT ip saddr 192.168.0.0 counter comment \"A privatized IP block\"
Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
xtables_save_string(commentinfo->comment);
}
+static int
+comment_xlate(const struct xt_entry_match *match,
+ struct xt_xlate *xl, int numeric)
+{
+ struct xt_comment_info *commentinfo = (void *)match->data;
+
+ commentinfo->comment[XT_MAX_COMMENT_LEN - 1] = '\0';
+ xt_xlate_add_comment(xl, commentinfo->comment);
+
+ return 1;
+}
+
static struct xtables_match comment_match = {
.family = NFPROTO_UNSPEC,
.name = "comment",
.save = comment_save,
.x6_parse = xtables_option_parse,
.x6_options = comment_opts,
+ .xlate = comment_xlate,
};
void _init(void)
void xt_xlate_free(struct xt_xlate *xl);
void xt_xlate_add(struct xt_xlate *xl, const char *fmt, ...);
void xt_xlate_add_comment(struct xt_xlate *xl, const char *comment);
+const char *xt_xlate_get_comment(struct xt_xlate *xl);
const char *xt_xlate_get(struct xt_xlate *xl);
#ifdef XTABLES_INTERNAL
static int nft_ipv4_xlate(const void *data, struct xt_xlate *xl)
{
const struct iptables_command_state *cs = data;
+ const char *comment;
int ret;
if (cs->fw.ip.iniface[0] != '\0') {
/* Always add counters per rule, as in iptables */
xt_xlate_add(xl, "counter ");
+ comment = xt_xlate_get_comment(xl);
+ if (comment)
+ xt_xlate_add(xl, "comment \\\"%s\\\" ", comment);
+
ret = xlate_action(cs, !!(cs->fw.ip.flags & IPT_F_GOTO), xl);
return ret;
static int nft_ipv6_xlate(const void *data, struct xt_xlate *xl)
{
const struct iptables_command_state *cs = data;
+ const char *comment;
int ret;
if (cs->fw6.ipv6.iniface[0] != '\0') {
/* Always add counters per rule, as in iptables */
xt_xlate_add(xl, "counter ");
+ comment = xt_xlate_get_comment(xl);
+ if (comment)
+ xt_xlate_add(xl, "comment \\\"%s\\\" ", comment);
+
ret = xlate_action(cs, !!(cs->fw6.ipv6.flags & IP6T_F_GOTO), xl);
return ret;
xl->comment[NFT_USERDATA_MAXLEN - 1] = '\0';
}
+const char *xt_xlate_get_comment(struct xt_xlate *xl)
+{
+ return xl->comment[0] ? xl->comment : NULL;
+}
+
const char *xt_xlate_get(struct xt_xlate *xl)
{
return xl->buf.data;