• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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];
56 
57 	commentinfo->comment[XT_MAX_COMMENT_LEN - 1] = '\0';
58 	if (params->escape_quotes)
59 		snprintf(comment, XT_MAX_COMMENT_LEN, "\\\"%s\\\"",
60 			 commentinfo->comment);
61 	else
62 		snprintf(comment, XT_MAX_COMMENT_LEN, "\"%s\"",
63 			 commentinfo->comment);
64 
65 	comment[XT_MAX_COMMENT_LEN - 1] = '\0';
66 	xt_xlate_add_comment(xl, comment);
67 
68 	return 1;
69 }
70 
71 static struct xtables_match comment_match = {
72 	.family		= NFPROTO_UNSPEC,
73 	.name		= "comment",
74 	.version	= XTABLES_VERSION,
75 	.size		= XT_ALIGN(sizeof(struct xt_comment_info)),
76 	.userspacesize	= XT_ALIGN(sizeof(struct xt_comment_info)),
77 	.help		= comment_help,
78 	.print 		= comment_print,
79 	.save 		= comment_save,
80 	.x6_parse	= xtables_option_parse,
81 	.x6_options	= comment_opts,
82 	.xlate		= comment_xlate,
83 };
84 
_init(void)85 void _init(void)
86 {
87 	xtables_register_match(&comment_match);
88 }
89