1 /* Shared library add-on to iptables to add CONNMARK target support. */
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter_ipv4/ipt_CONNMARK.h>
13 struct ipt_entry_target t;
14 struct ipt_connmark_target_info mark;
18 /* Function which prints out usage message. */
23 "CONNMARK target v%s options:\n"
24 " --set-mark value Set conntrack mark value\n"
25 " --save-mark Save the packet nfmark on the connection\n"
26 " --restore-mark Restore saved nfmark value\n"
31 static struct option opts[] = {
32 { "set-mark", 1, 0, '1' },
33 { "save-mark", 0, 0, '2' },
34 { "restore-mark", 0, 0, '3' },
38 /* Initialize the target. */
40 init(struct ipt_entry_target *t, unsigned int *nfcache)
44 /* Function which parses command options; returns true if it
47 parse(int c, char **argv, int invert, unsigned int *flags,
48 const struct ipt_entry *entry,
49 struct ipt_entry_target **target)
51 struct ipt_connmark_target_info *markinfo
52 = (struct ipt_connmark_target_info *)(*target)->data;
57 markinfo->mode = IPT_CONNMARK_SET;
58 markinfo->mark = strtoul(optarg, &end, 0);
59 if (*end != '\0' || end == optarg)
60 exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
62 exit_error(PARAMETER_PROBLEM,
63 "CONNMARK target: Can't specify --set-mark twice");
67 markinfo->mode = IPT_CONNMARK_SAVE;
69 exit_error(PARAMETER_PROBLEM,
70 "CONNMARK target: Can't specify --save-mark twice");
74 markinfo->mode = IPT_CONNMARK_RESTORE;
76 exit_error(PARAMETER_PROBLEM,
77 "CONNMARK target: Can't specify --restore-mark twice");
88 final_check(unsigned int flags)
91 exit_error(PARAMETER_PROBLEM,
92 "CONNMARK target: Parameter --set-mark is required");
96 print_mark(unsigned long mark, int numeric)
98 printf("0x%lx ", mark);
101 /* Prints out the targinfo. */
103 print(const struct ipt_ip *ip,
104 const struct ipt_entry_target *target,
107 const struct ipt_connmark_target_info *markinfo =
108 (const struct ipt_connmark_target_info *)target->data;
109 switch (markinfo->mode) {
110 case IPT_CONNMARK_SET:
111 printf("CONNMARK set ");
112 print_mark(markinfo->mark, numeric);
114 case IPT_CONNMARK_SAVE:
115 printf("CONNMARK save ");
117 case IPT_CONNMARK_RESTORE:
118 printf("CONNMARK restore ");
121 printf("ERROR: UNKNOWN CONNMARK MODE ");
126 /* Saves the union ipt_targinfo in parsable form to stdout. */
128 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
130 const struct ipt_connmark_target_info *markinfo =
131 (const struct ipt_connmark_target_info *)target->data;
133 switch (markinfo->mode) {
134 case IPT_CONNMARK_SET:
135 printf("--set-mark 0x%lx ", markinfo->mark);
137 case IPT_CONNMARK_SAVE:
138 printf("--save-mark ");
140 case IPT_CONNMARK_RESTORE:
141 printf("--restore-mark ");
144 printf("ERROR: UNKNOWN CONNMARK MODE ");
150 struct iptables_target mark
154 IPT_ALIGN(sizeof(struct ipt_connmark_target_info)),
155 IPT_ALIGN(sizeof(struct ipt_connmark_target_info)),
167 register_target(&mark);