Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iptables / extensions / libipt_CONNMARK.c
blobebee3610e7b30c6bca61373f1c23952d28784c8e
1 /* Shared library add-on to iptables to add CONNMARK target support.
3 * (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
4 * by Henrik Nordstrom <hno@marasystems.com>
6 * Version 1.1
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.
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.
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
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <getopt.h>
27 #include <iptables.h>
28 #include <linux/netfilter_ipv4/ip_tables.h>
29 #include <../include/linux/netfilter_ipv4/ipt_CONNMARK.h>
31 #if 0
32 struct markinfo {
33 struct ipt_entry_target t;
34 struct ipt_connmark_target_info mark;
36 #endif
38 /* Function which prints out usage message. */
39 static void
40 help(void)
42 printf(
43 "CONNMARK target v%s options:\n"
44 " --set-mark value[/mask] Set conntrack mark value\n"
45 " --set-return [--mask mask] Set conntrack mark & nfmark, RETURN\n"
46 " --save-mark [--mask mask] Save the packet nfmark in the connection\n"
47 " --restore-mark [--mask mask] Restore saved nfmark value\n"
48 "\n",
49 IPTABLES_VERSION);
52 static struct option opts[] = {
53 { "set-mark", 1, 0, '1' },
54 { "save-mark", 0, 0, '2' },
55 { "restore-mark", 0, 0, '3' },
56 { "mask", 1, 0, '4' },
57 { "set-return", 1, 0, '9' },
58 { 0 }
61 /* Initialize the target. */
62 static void
63 init(struct ipt_entry_target *t, unsigned int *nfcache)
65 struct ipt_connmark_target_info *markinfo
66 = (struct ipt_connmark_target_info *)t->data;
68 markinfo->mask = 0xffffffffUL;
71 /* Function which parses command options; returns true if it
72 ate an option */
73 static int
74 parse(int c, char **argv, int invert, unsigned int *flags,
75 const struct ipt_entry *entry,
76 struct ipt_entry_target **target)
78 struct ipt_connmark_target_info *markinfo
79 = (struct ipt_connmark_target_info *)(*target)->data;
81 switch (c) {
82 char *end;
83 case '1':
84 case '9':
85 markinfo->mode = (c == '1') ? IPT_CONNMARK_SET : IPT_CONNMARK_SET_RETURN;
86 // markinfo->mode = IPT_CONNMARK_SET;
88 markinfo->mark = strtoul(optarg, &end, 0);
89 if (*end == '/' && end[1] != '\0')
90 markinfo->mask = strtoul(end+1, &end, 0);
92 if (*end != '\0' || end == optarg)
93 exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
94 if (*flags)
95 exit_error(PARAMETER_PROBLEM,
96 "CONNMARK target: Can't specify --set-mark twice");
97 *flags = 1;
98 break;
99 case '2':
100 markinfo->mode = IPT_CONNMARK_SAVE;
101 if (*flags)
102 exit_error(PARAMETER_PROBLEM,
103 "CONNMARK target: Can't specify --save-mark twice");
104 *flags = 1;
105 break;
106 case '3':
107 markinfo->mode = IPT_CONNMARK_RESTORE;
108 if (*flags)
109 exit_error(PARAMETER_PROBLEM,
110 "CONNMARK target: Can't specify --restore-mark twice");
111 *flags = 1;
112 break;
113 case '4':
114 if (!*flags)
115 exit_error(PARAMETER_PROBLEM,
116 "CONNMARK target: Can't specify --mask without a operation");
117 markinfo->mask = strtoul(optarg, &end, 0);
119 if (*end != '\0' || end == optarg)
120 exit_error(PARAMETER_PROBLEM, "Bad MASK value `%s'", optarg);
121 break;
122 default:
123 return 0;
126 return 1;
129 static void
130 final_check(unsigned int flags)
132 if (!flags)
133 exit_error(PARAMETER_PROBLEM,
134 "CONNMARK target: No operation specified");
137 static void
138 print_mark(unsigned long mark)
140 printf("0x%lx", mark);
143 static void
144 print_mask(const char *text, unsigned long mask)
146 if (mask != 0xffffffffUL)
147 printf("%s0x%lx", text, mask);
151 /* Prints out the target info. */
152 static void
153 print(const struct ipt_ip *ip,
154 const struct ipt_entry_target *target,
155 int numeric)
157 const struct ipt_connmark_target_info *markinfo =
158 (const struct ipt_connmark_target_info *)target->data;
159 switch (markinfo->mode) {
160 case IPT_CONNMARK_SET:
161 case IPT_CONNMARK_SET_RETURN:
162 printf("CONNMARK set%s ", (markinfo->mode == IPT_CONNMARK_SET_RETURN) ? "-return" : "");
164 // printf("CONNMARK set ");
165 print_mark(markinfo->mark);
166 print_mask("/", markinfo->mask);
167 printf(" ");
168 break;
169 case IPT_CONNMARK_SAVE:
170 printf("CONNMARK save ");
171 print_mask("mask ", markinfo->mask);
172 printf(" ");
173 break;
174 case IPT_CONNMARK_RESTORE:
175 printf("CONNMARK restore ");
176 print_mask("mask ", markinfo->mask);
177 break;
178 default:
179 printf("ERROR: UNKNOWN CONNMARK MODE ");
180 break;
184 /* Saves the target into in parsable form to stdout. */
185 static void
186 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
188 const struct ipt_connmark_target_info *markinfo =
189 (const struct ipt_connmark_target_info *)target->data;
191 switch (markinfo->mode) {
192 case IPT_CONNMARK_SET:
193 case IPT_CONNMARK_SET_RETURN:
194 printf("--set-%s ", (markinfo->mode == IPT_CONNMARK_SET_RETURN) ? "return" : "mark");
196 // printf("--set-mark ");
197 print_mark(markinfo->mark);
198 print_mask("/", markinfo->mask);
199 printf(" ");
200 break;
201 case IPT_CONNMARK_SAVE:
202 printf("--save-mark ");
203 print_mask("--mask ", markinfo->mask);
204 break;
205 case IPT_CONNMARK_RESTORE:
206 printf("--restore-mark ");
207 print_mask("--mask ", markinfo->mask);
208 break;
209 default:
210 printf("ERROR: UNKNOWN CONNMARK MODE ");
211 break;
215 static struct iptables_target connmark_target = {
216 .name = "CONNMARK",
217 .version = IPTABLES_VERSION,
218 .size = IPT_ALIGN(sizeof(struct ipt_connmark_target_info)),
219 .userspacesize = IPT_ALIGN(sizeof(struct ipt_connmark_target_info)),
220 .help = &help,
221 .init = &init,
222 .parse = &parse,
223 .final_check = &final_check,
224 .print = &print,
225 .save = &save,
226 .extra_opts = opts
229 void _init(void)
231 register_target(&connmark_target);