bc739fcae0f07818c3ef05eb9bf48d9d02568895
[iptables.git] / extensions / libipt_CONNMARK.c
1 /* Shared library add-on to iptables to add CONNMARK target support.
2  *
3  * (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
4  * by Henrik Nordstrom <hno@marasystems.com>
5  *
6  * Version 1.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <getopt.h>
26
27 #include <iptables.h>
28 #include <linux/netfilter_ipv4/ip_tables.h>
29 #include "../include/linux/netfilter_ipv4/ipt_CONNMARK.h"
30
31 #if 0
32 struct markinfo {
33         struct ipt_entry_target t;
34         struct ipt_connmark_target_info mark;
35 };
36 #endif
37
38 /* Function which prints out usage message. */
39 static void
40 help(void)
41 {
42         printf(
43 "CONNMARK target v%s options:\n"
44 "  --set-mark value[/mask]       Set conntrack mark value\n"
45 "  --save-mark [--mask mask]     Save the packet nfmark in the connection\n"
46 "  --restore-mark [--mask mask]  Restore saved nfmark value\n"
47 "\n",
48 IPTABLES_VERSION);
49 }
50
51 static struct option opts[] = {
52         { "set-mark", 1, 0, '1' },
53         { "save-mark", 0, 0, '2' },
54         { "restore-mark", 0, 0, '3' },
55         { "mask", 1, 0, '4' },
56         { 0 }
57 };
58
59 /* Initialize the target. */
60 static void
61 init(struct ipt_entry_target *t, unsigned int *nfcache)
62 {
63 }
64
65 /* Function which parses command options; returns true if it
66    ate an option */
67 static int
68 parse(int c, char **argv, int invert, unsigned int *flags,
69       const struct ipt_entry *entry,
70       struct ipt_entry_target **target)
71 {
72         struct ipt_connmark_target_info *markinfo
73                 = (struct ipt_connmark_target_info *)(*target)->data;
74
75 #ifdef KERNEL_64_USERSPACE_32
76         markinfo->mask = ~0ULL;
77 #else
78         markinfo->mask = ~0UL;
79 #endif
80
81         switch (c) {
82                 char *end;
83         case '1':
84                 markinfo->mode = IPT_CONNMARK_SET;
85 #ifdef KERNEL_64_USERSPACE_32
86                 markinfo->mark = strtoull(optarg, &end, 0);
87                 if (*end == '/' && end[1] != '\0')
88                     markinfo->mask = strtoull(end+1, &end, 0);
89 #else
90                 markinfo->mark = strtoul(optarg, &end, 0);
91                 if (*end == '/' && end[1] != '\0')
92                     markinfo->mask = strtoul(end+1, &end, 0);
93 #endif
94                 if (*end != '\0' || end == optarg)
95                         exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
96                 if (*flags)
97                         exit_error(PARAMETER_PROBLEM,
98                                    "CONNMARK target: Can't specify --set-mark twice");
99                 *flags = 1;
100                 break;
101         case '2':
102                 markinfo->mode = IPT_CONNMARK_SAVE;
103                 if (*flags)
104                         exit_error(PARAMETER_PROBLEM,
105                                    "CONNMARK target: Can't specify --save-mark twice");
106                 *flags = 1;
107                 break;
108         case '3':
109                 markinfo->mode = IPT_CONNMARK_RESTORE;
110                 if (*flags)
111                         exit_error(PARAMETER_PROBLEM,
112                                    "CONNMARK target: Can't specify --restore-mark twice");
113                 *flags = 1;
114                 break;
115         case '4':
116                 if (!*flags)
117                         exit_error(PARAMETER_PROBLEM,
118                                    "CONNMARK target: Can't specify --mask without a operation");
119 #ifdef KERNEL_64_USERSPACE_32
120                 markinfo->mask = strtoull(optarg, &end, 0);
121 #else
122                 markinfo->mask = strtoul(optarg, &end, 0);
123 #endif
124                 if (*end != '\0' || end == optarg)
125                         exit_error(PARAMETER_PROBLEM, "Bad MASK value `%s'", optarg);
126                 break;
127         default:
128                 return 0;
129         }
130
131         return 1;
132 }
133
134 static void
135 final_check(unsigned int flags)
136 {
137         if (!flags)
138                 exit_error(PARAMETER_PROBLEM,
139                            "CONNMARK target: No operation specified");
140 }
141
142 #ifdef KERNEL_64_USERSPACE_32
143 static void
144 print_mark(unsigned long long mark)
145 {
146         printf("0x%llx", mark);
147 }
148
149 static void
150 print_mask(const char *text, unsigned long long mask)
151 {
152         if (mask != ~0ULL)
153                 printf("%s0x%llx", text, mask);
154 }
155
156 #else
157
158 static void
159 print_mark(unsigned long mark)
160 {
161         printf("0x%lx", mark);
162 }
163
164 static void
165 print_mask(const char *text, unsigned long mask)
166 {
167         if (mask != ~0UL)
168                 printf("%s0x%lx", text, mask);
169 }
170 #endif
171
172
173 /* Prints out the target info. */
174 static void
175 print(const struct ipt_ip *ip,
176       const struct ipt_entry_target *target,
177       int numeric)
178 {
179         const struct ipt_connmark_target_info *markinfo =
180                 (const struct ipt_connmark_target_info *)target->data;
181         switch (markinfo->mode) {
182         case IPT_CONNMARK_SET:
183             printf("CONNMARK set ");
184             print_mark(markinfo->mark);
185             print_mask("/", markinfo->mask);
186             printf(" ");
187             break;
188         case IPT_CONNMARK_SAVE:
189             printf("CONNMARK save ");
190             print_mask("mask ", markinfo->mask);
191             printf(" ");
192             break;
193         case IPT_CONNMARK_RESTORE:
194             printf("CONNMARK restore ");
195             print_mask("mask ", markinfo->mask);
196             break;
197         default:
198             printf("ERROR: UNKNOWN CONNMARK MODE ");
199             break;
200         }
201 }
202
203 /* Saves the target into in parsable form to stdout. */
204 static void
205 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
206 {
207         const struct ipt_connmark_target_info *markinfo =
208                 (const struct ipt_connmark_target_info *)target->data;
209
210         switch (markinfo->mode) {
211         case IPT_CONNMARK_SET:
212             printf("--set-mark ");
213             print_mark(markinfo->mark);
214             print_mask("/", markinfo->mask);
215             printf(" ");
216             break;
217         case IPT_CONNMARK_SAVE:
218             printf("--save-mark ");
219             print_mask("--mask ", markinfo->mask);
220             break;
221         case IPT_CONNMARK_RESTORE:
222             printf("--restore-mark ");
223             print_mask("--mask ", markinfo->mask);
224             break;
225         default:
226             printf("ERROR: UNKNOWN CONNMARK MODE ");
227             break;
228         }
229 }
230
231 static struct iptables_target connmark_target = {
232     .name          = "CONNMARK",
233     .version       = IPTABLES_VERSION,
234     .size          = IPT_ALIGN(sizeof(struct ipt_connmark_target_info)),
235     .userspacesize = IPT_ALIGN(sizeof(struct ipt_connmark_target_info)),
236     .help          = &help,
237     .init          = &init,
238     .parse         = &parse,
239     .final_check   = &final_check,
240     .print         = &print,
241     .save          = &save,
242     .extra_opts    = opts
243 };
244
245 void _init(void)
246 {
247         register_target(&connmark_target);
248 }