1 /* Shared library add-on to iptables to add comment match support.
2 *
3 * ChangeLog
4 * 2003-05-13: Brad Fisher <brad@info-link.net>
5 * Initial comment match
6 * 2004-05-12: Brad Fisher <brad@info-link.net>
7 * Port to patch-o-matic-ng
8 */
9 #include <stdio.h>
10 #include <xtables.h>
11 #include <linux/netfilter/xt_comment.h>
12
13 enum {
14 O_COMMENT = 0,
15 };
16
comment_help(void)17 static void comment_help(void)
18 {
19 printf(
20 "comment match options:\n"
21 "--comment COMMENT Attach a comment to a rule\n");
22 }
23
24 static const struct xt_option_entry comment_opts[] = {
25 {.name = "comment", .id = O_COMMENT, .type = XTTYPE_STRING,
26 .flags = XTOPT_MAND | XTOPT_PUT,
27 XTOPT_POINTER(struct xt_comment_info, comment)},
28 XTOPT_TABLEEND,
29 };
30
31 static void
comment_print(const void * ip,const struct xt_entry_match * match,int numeric)32 comment_print(const void *ip, const struct xt_entry_match *match, int numeric)
33 {
34 struct xt_comment_info *commentinfo = (void *)match->data;
35
36 commentinfo->comment[XT_MAX_COMMENT_LEN-1] = '\0';
37 printf(" /* %s */", commentinfo->comment);
38 }
39
40 /* Saves the union ipt_matchinfo in parsable form to stdout. */
41 static void
comment_save(const void * ip,const struct xt_entry_match * match)42 comment_save(const void *ip, const struct xt_entry_match *match)
43 {
44 struct xt_comment_info *commentinfo = (void *)match->data;
45
46 commentinfo->comment[XT_MAX_COMMENT_LEN-1] = '\0';
47 printf(" --comment");
48 xtables_save_string(commentinfo->comment);
49 }
50
comment_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)51 static int comment_xlate(struct xt_xlate *xl,
52 const struct xt_xlate_mt_params *params)
53 {
54 struct xt_comment_info *commentinfo = (void *)params->match->data;
55 char comment[XT_MAX_COMMENT_LEN + sizeof("\\\"\\\"")];
56
57 commentinfo->comment[XT_MAX_COMMENT_LEN - 1] = '\0';
58 if (params->escape_quotes)
59 snprintf(comment, sizeof(comment), "\\\"%s\\\"",
60 commentinfo->comment);
61 else
62 snprintf(comment, sizeof(comment), "\"%s\"",
63 commentinfo->comment);
64
65 xt_xlate_add_comment(xl, comment);
66
67 return 1;
68 }
69
70 static struct xtables_match comment_match = {
71 .family = NFPROTO_UNSPEC,
72 .name = "comment",
73 .version = XTABLES_VERSION,
74 .size = XT_ALIGN(sizeof(struct xt_comment_info)),
75 .userspacesize = XT_ALIGN(sizeof(struct xt_comment_info)),
76 .help = comment_help,
77 .print = comment_print,
78 .save = comment_save,
79 .x6_parse = xtables_option_parse,
80 .x6_options = comment_opts,
81 .xlate = comment_xlate,
82 };
83
_init(void)84 void _init(void)
85 {
86 xtables_register_match(&comment_match);
87 }
88