iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libipt_MARK.c
1 /* Shared library add-on to iptables to add MARK target support. */
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <getopt.h>
6
7 #include <iptables.h>
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter_ipv4/ipt_MARK.h>
10
11 struct markinfo {
12         struct ipt_entry_target t;
13         struct ipt_mark_target_info mark;
14 };
15
16 /* Function which prints out usage message. */
17 static void
18 help(void)
19 {
20         printf(
21 "MARK target v%s options:\n"
22 "  --set-mark value                   Set nfmark value\n"
23 "\n",
24 IPTABLES_VERSION);
25 }
26
27 static struct option opts[] = {
28         { "set-mark", 1, 0, '1' },
29         { 0 }
30 };
31
32 /* Initialize the target. */
33 static void
34 init(struct ipt_entry_target *t, unsigned int *nfcache)
35 {
36 }
37
38 /* Function which parses command options; returns true if it
39    ate an option */
40 static int
41 parse(int c, char **argv, int invert, unsigned int *flags,
42       const struct ipt_entry *entry,
43       struct ipt_entry_target **target)
44 {
45         struct ipt_mark_target_info *markinfo
46                 = (struct ipt_mark_target_info *)(*target)->data;
47
48         switch (c) {
49         case '1':
50                 if (string_to_number(optarg, 0, 0xffffffff, 
51                                      (unsigned int *)&markinfo->mark))
52                         exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
53                 if (*flags)
54                         exit_error(PARAMETER_PROBLEM,
55                                    "MARK target: Can't specify --set-mark twice");
56                 *flags = 1;
57                 break;
58
59         default:
60                 return 0;
61         }
62
63         return 1;
64 }
65
66 static void
67 final_check(unsigned int flags)
68 {
69         if (!flags)
70                 exit_error(PARAMETER_PROBLEM,
71                            "MARK target: Parameter --set-mark is required");
72 }
73
74 static void
75 print_mark(unsigned long mark, int numeric)
76 {
77         printf("0x%lx ", mark);
78 }
79
80 /* Prints out the targinfo. */
81 static void
82 print(const struct ipt_ip *ip,
83       const struct ipt_entry_target *target,
84       int numeric)
85 {
86         const struct ipt_mark_target_info *markinfo =
87                 (const struct ipt_mark_target_info *)target->data;
88         printf("MARK set ");
89         print_mark(markinfo->mark, numeric);
90 }
91
92 /* Saves the union ipt_targinfo in parsable form to stdout. */
93 static void
94 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
95 {
96         const struct ipt_mark_target_info *markinfo =
97                 (const struct ipt_mark_target_info *)target->data;
98
99         printf("--set-mark 0x%lx ", markinfo->mark);
100 }
101
102 static
103 struct iptables_target mark
104 = { NULL,
105     "MARK",
106     IPTABLES_VERSION,
107     IPT_ALIGN(sizeof(struct ipt_mark_target_info)),
108     IPT_ALIGN(sizeof(struct ipt_mark_target_info)),
109     &help,
110     &init,
111     &parse,
112     &final_check,
113     &print,
114     &save,
115     opts
116 };
117
118 void _init(void)
119 {
120         register_target(&mark);
121 }