blob: 05fc8b276357a4667acb6bab278ac03319b5de67 [file] [log] [blame]
András Kis-Szabóf1f447b2002-03-26 12:45:19 +00001/* Shared library add-on to ip6tables to add Fragmentation header support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7#include <errno.h>
8#include <ip6tables.h>
9#include <linux/netfilter_ipv6/ip6t_frag.h>
10
11/* Function which prints out usage message. */
12static void
13help(void)
14{
15 printf(
16"FRAG v%s options:\n"
17" --fragid [!] id[:id] match the id (range)\n"
18" --fraglen [!] length total length of this header\n"
19" --fragres check the reserved filed, too\n"
András Kis-Szabód8a12a82002-04-24 09:36:30 +000020" --fragfirst matches on the first fragment\n"
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000021" [--fragmore|--fraglast] there are more fragments or this\n"
22" is the last one\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000023IPTABLES_VERSION);
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000024}
25
Jan Engelhardt661f1122007-07-30 14:46:51 +000026static const struct option opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000027 { .name = "fragid", .has_arg = 1, .val = '1' },
28 { .name = "fraglen", .has_arg = 1, .val = '2' },
29 { .name = "fragres", .has_arg = 0, .val = '3' },
30 { .name = "fragfirst", .has_arg = 0, .val = '4' },
31 { .name = "fragmore", .has_arg = 0, .val = '5' },
32 { .name = "fraglast", .has_arg = 0, .val = '6' },
33 { }
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000034};
35
36static u_int32_t
37parse_frag_id(const char *idstr, const char *typestr)
38{
39 unsigned long int id;
40 char* ep;
41
Harald Welte46a73cf2003-09-05 12:53:44 +000042 id = strtoul(idstr, &ep, 0);
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000043
44 if ( idstr == ep ) {
45 exit_error(PARAMETER_PROBLEM,
46 "FRAG no valid digits in %s `%s'", typestr, idstr);
47 }
48 if ( id == ULONG_MAX && errno == ERANGE ) {
49 exit_error(PARAMETER_PROBLEM,
50 "%s `%s' specified too big: would overflow",
51 typestr, idstr);
52 }
53 if ( *idstr != '\0' && *ep != '\0' ) {
54 exit_error(PARAMETER_PROBLEM,
55 "FRAG error parsing %s `%s'", typestr, idstr);
56 }
57 return (u_int32_t) id;
58}
59
60static void
61parse_frag_ids(const char *idstring, u_int32_t *ids)
62{
63 char *buffer;
64 char *cp;
65
66 buffer = strdup(idstring);
67 if ((cp = strchr(buffer, ':')) == NULL)
68 ids[0] = ids[1] = parse_frag_id(buffer,"id");
69 else {
70 *cp = '\0';
71 cp++;
72
73 ids[0] = buffer[0] ? parse_frag_id(buffer,"id") : 0;
74 ids[1] = cp[0] ? parse_frag_id(cp,"id") : 0xFFFFFFFF;
75 }
76 free(buffer);
77}
78
79/* Initialize the match. */
80static void
Peter Rileyea146a92007-09-02 13:09:07 +000081init(struct xt_entry_match *m)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000082{
83 struct ip6t_frag *fraginfo = (struct ip6t_frag *)m->data;
84
85 fraginfo->ids[0] = 0x0L;
86 fraginfo->ids[1] = 0xFFFFFFFF;
87 fraginfo->hdrlen = 0;
88 fraginfo->flags = 0;
89 fraginfo->invflags = 0;
90}
91
92/* Function which parses command options; returns true if it
93 ate an option */
94static int
95parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIa620c612007-07-24 06:03:45 +000096 const void *entry,
Yasuyuki KOZAKAIb85256b2007-07-24 05:58:56 +000097 struct xt_entry_match **match)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000098{
99 struct ip6t_frag *fraginfo = (struct ip6t_frag *)(*match)->data;
100
101 switch (c) {
102 case '1':
103 if (*flags & IP6T_FRAG_IDS)
104 exit_error(PARAMETER_PROBLEM,
105 "Only one `--fragid' allowed");
106 check_inverse(optarg, &invert, &optind, 0);
107 parse_frag_ids(argv[optind-1], fraginfo->ids);
108 if (invert)
109 fraginfo->invflags |= IP6T_FRAG_INV_IDS;
110 fraginfo->flags |= IP6T_FRAG_IDS;
111 *flags |= IP6T_FRAG_IDS;
112 break;
113 case '2':
114 if (*flags & IP6T_FRAG_LEN)
115 exit_error(PARAMETER_PROBLEM,
116 "Only one `--fraglen' allowed");
117 check_inverse(optarg, &invert, &optind, 0);
118 fraginfo->hdrlen = parse_frag_id(argv[optind-1], "length");
119 if (invert)
120 fraginfo->invflags |= IP6T_FRAG_INV_LEN;
121 fraginfo->flags |= IP6T_FRAG_LEN;
122 *flags |= IP6T_FRAG_LEN;
123 break;
124 case '3':
125 if (*flags & IP6T_FRAG_RES)
126 exit_error(PARAMETER_PROBLEM,
127 "Only one `--fragres' allowed");
128 fraginfo->flags |= IP6T_FRAG_RES;
129 *flags |= IP6T_FRAG_RES;
130 break;
131 case '4':
132 if (*flags & IP6T_FRAG_FST)
133 exit_error(PARAMETER_PROBLEM,
134 "Only one `--fragfirst' allowed");
135 fraginfo->flags |= IP6T_FRAG_FST;
136 *flags |= IP6T_FRAG_FST;
137 break;
138 case '5':
139 if (*flags & (IP6T_FRAG_MF|IP6T_FRAG_NMF))
140 exit_error(PARAMETER_PROBLEM,
141 "Only one `--fragmore' or `--fraglast' allowed");
142 fraginfo->flags |= IP6T_FRAG_MF;
143 *flags |= IP6T_FRAG_MF;
144 break;
145 case '6':
146 if (*flags & (IP6T_FRAG_MF|IP6T_FRAG_NMF))
147 exit_error(PARAMETER_PROBLEM,
148 "Only one `--fragmore' or `--fraglast' allowed");
149 fraginfo->flags |= IP6T_FRAG_NMF;
150 *flags |= IP6T_FRAG_NMF;
151 break;
152 default:
153 return 0;
154 }
155
156 return 1;
157}
158
159/* Final check; we don't care. */
160static void
161final_check(unsigned int flags)
162{
163}
164
165static void
166print_ids(const char *name, u_int32_t min, u_int32_t max,
167 int invert)
168{
169 const char *inv = invert ? "!" : "";
170
171 if (min != 0 || max != 0xFFFFFFFF || invert) {
172 printf("%s", name);
Harald Welte46a73cf2003-09-05 12:53:44 +0000173 if (min == max)
174 printf(":%s%u ", inv, min);
175 else
176 printf("s:%s%u:%u ", inv, min, max);
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000177 }
178}
179
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000180/* Prints out the union ip6t_matchinfo. */
181static void
Yasuyuki KOZAKAIa620c612007-07-24 06:03:45 +0000182print(const void *ip,
Yasuyuki KOZAKAIb85256b2007-07-24 05:58:56 +0000183 const struct xt_entry_match *match, int numeric)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000184{
185 const struct ip6t_frag *frag = (struct ip6t_frag *)match->data;
186
187 printf("frag ");
188 print_ids("id", frag->ids[0], frag->ids[1],
189 frag->invflags & IP6T_FRAG_INV_IDS);
Harald Welte46a73cf2003-09-05 12:53:44 +0000190
András Kis-Szabód8a12a82002-04-24 09:36:30 +0000191 if (frag->flags & IP6T_FRAG_LEN) {
Harald Welte46a73cf2003-09-05 12:53:44 +0000192 printf("length:%s%u ",
193 frag->invflags & IP6T_FRAG_INV_LEN ? "!" : "",
194 frag->hdrlen);
András Kis-Szabód8a12a82002-04-24 09:36:30 +0000195 }
Harald Welte46a73cf2003-09-05 12:53:44 +0000196
197 if (frag->flags & IP6T_FRAG_RES)
198 printf("reserved ");
199
200 if (frag->flags & IP6T_FRAG_FST)
201 printf("first ");
202
203 if (frag->flags & IP6T_FRAG_MF)
204 printf("more ");
205
206 if (frag->flags & IP6T_FRAG_NMF)
207 printf("last ");
208
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000209 if (frag->invflags & ~IP6T_FRAG_INV_MASK)
210 printf("Unknown invflags: 0x%X ",
211 frag->invflags & ~IP6T_FRAG_INV_MASK);
212}
213
214/* Saves the union ip6t_matchinfo in parsable form to stdout. */
Yasuyuki KOZAKAIa620c612007-07-24 06:03:45 +0000215static void save(const void *ip, const struct xt_entry_match *match)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000216{
217 const struct ip6t_frag *fraginfo = (struct ip6t_frag *)match->data;
218
219 if (!(fraginfo->ids[0] == 0
220 && fraginfo->ids[1] == 0xFFFFFFFF)) {
221 printf("--fragid %s",
222 (fraginfo->invflags & IP6T_FRAG_INV_IDS) ? "! " : "");
223 if (fraginfo->ids[0]
224 != fraginfo->ids[1])
225 printf("%u:%u ",
226 fraginfo->ids[0],
227 fraginfo->ids[1]);
228 else
229 printf("%u ",
230 fraginfo->ids[0]);
231 }
232
András Kis-Szabód8a12a82002-04-24 09:36:30 +0000233 if (fraginfo->flags & IP6T_FRAG_LEN) {
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000234 printf("--fraglen %s%u ",
235 (fraginfo->invflags & IP6T_FRAG_INV_LEN) ? "! " : "",
236 fraginfo->hdrlen);
237 }
238
Harald Welte46a73cf2003-09-05 12:53:44 +0000239 if (fraginfo->flags & IP6T_FRAG_RES)
240 printf("--fragres ");
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000241
Harald Welte46a73cf2003-09-05 12:53:44 +0000242 if (fraginfo->flags & IP6T_FRAG_FST)
243 printf("--fragfirst ");
244
245 if (fraginfo->flags & IP6T_FRAG_MF)
246 printf("--fragmore ");
247
248 if (fraginfo->flags & IP6T_FRAG_NMF)
249 printf("--fraglast ");
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000250}
251
252static
Harald Welte46a73cf2003-09-05 12:53:44 +0000253struct ip6tables_match frag = {
254 .name = "frag",
255 .version = IPTABLES_VERSION,
256 .size = IP6T_ALIGN(sizeof(struct ip6t_frag)),
257 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_frag)),
258 .help = &help,
259 .init = &init,
260 .parse = &parse,
261 .final_check = &final_check,
262 .print = &print,
263 .save = &save,
264 .extra_opts = opts
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000265};
266
267void
268_init(void)
269{
270 register_match6(&frag);
271}