blob: 49044a53c7736f987df646e0400e0578bc74e8a2 [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
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000159static void
160print_ids(const char *name, u_int32_t min, u_int32_t max,
161 int invert)
162{
163 const char *inv = invert ? "!" : "";
164
165 if (min != 0 || max != 0xFFFFFFFF || invert) {
166 printf("%s", name);
Harald Welte46a73cf2003-09-05 12:53:44 +0000167 if (min == max)
168 printf(":%s%u ", inv, min);
169 else
170 printf("s:%s%u:%u ", inv, min, max);
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000171 }
172}
173
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000174/* Prints out the union ip6t_matchinfo. */
175static void
Yasuyuki KOZAKAIa620c612007-07-24 06:03:45 +0000176print(const void *ip,
Yasuyuki KOZAKAIb85256b2007-07-24 05:58:56 +0000177 const struct xt_entry_match *match, int numeric)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000178{
179 const struct ip6t_frag *frag = (struct ip6t_frag *)match->data;
180
181 printf("frag ");
182 print_ids("id", frag->ids[0], frag->ids[1],
183 frag->invflags & IP6T_FRAG_INV_IDS);
Harald Welte46a73cf2003-09-05 12:53:44 +0000184
András Kis-Szabód8a12a82002-04-24 09:36:30 +0000185 if (frag->flags & IP6T_FRAG_LEN) {
Harald Welte46a73cf2003-09-05 12:53:44 +0000186 printf("length:%s%u ",
187 frag->invflags & IP6T_FRAG_INV_LEN ? "!" : "",
188 frag->hdrlen);
András Kis-Szabód8a12a82002-04-24 09:36:30 +0000189 }
Harald Welte46a73cf2003-09-05 12:53:44 +0000190
191 if (frag->flags & IP6T_FRAG_RES)
192 printf("reserved ");
193
194 if (frag->flags & IP6T_FRAG_FST)
195 printf("first ");
196
197 if (frag->flags & IP6T_FRAG_MF)
198 printf("more ");
199
200 if (frag->flags & IP6T_FRAG_NMF)
201 printf("last ");
202
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000203 if (frag->invflags & ~IP6T_FRAG_INV_MASK)
204 printf("Unknown invflags: 0x%X ",
205 frag->invflags & ~IP6T_FRAG_INV_MASK);
206}
207
208/* Saves the union ip6t_matchinfo in parsable form to stdout. */
Yasuyuki KOZAKAIa620c612007-07-24 06:03:45 +0000209static void save(const void *ip, const struct xt_entry_match *match)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000210{
211 const struct ip6t_frag *fraginfo = (struct ip6t_frag *)match->data;
212
213 if (!(fraginfo->ids[0] == 0
214 && fraginfo->ids[1] == 0xFFFFFFFF)) {
215 printf("--fragid %s",
216 (fraginfo->invflags & IP6T_FRAG_INV_IDS) ? "! " : "");
217 if (fraginfo->ids[0]
218 != fraginfo->ids[1])
219 printf("%u:%u ",
220 fraginfo->ids[0],
221 fraginfo->ids[1]);
222 else
223 printf("%u ",
224 fraginfo->ids[0]);
225 }
226
András Kis-Szabód8a12a82002-04-24 09:36:30 +0000227 if (fraginfo->flags & IP6T_FRAG_LEN) {
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000228 printf("--fraglen %s%u ",
229 (fraginfo->invflags & IP6T_FRAG_INV_LEN) ? "! " : "",
230 fraginfo->hdrlen);
231 }
232
Harald Welte46a73cf2003-09-05 12:53:44 +0000233 if (fraginfo->flags & IP6T_FRAG_RES)
234 printf("--fragres ");
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000235
Harald Welte46a73cf2003-09-05 12:53:44 +0000236 if (fraginfo->flags & IP6T_FRAG_FST)
237 printf("--fragfirst ");
238
239 if (fraginfo->flags & IP6T_FRAG_MF)
240 printf("--fragmore ");
241
242 if (fraginfo->flags & IP6T_FRAG_NMF)
243 printf("--fraglast ");
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000244}
245
246static
Harald Welte46a73cf2003-09-05 12:53:44 +0000247struct ip6tables_match frag = {
248 .name = "frag",
249 .version = IPTABLES_VERSION,
250 .size = IP6T_ALIGN(sizeof(struct ip6t_frag)),
251 .userspacesize = IP6T_ALIGN(sizeof(struct ip6t_frag)),
252 .help = &help,
253 .init = &init,
254 .parse = &parse,
Harald Welte46a73cf2003-09-05 12:53:44 +0000255 .print = &print,
256 .save = &save,
257 .extra_opts = opts
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000258};
259
260void
261_init(void)
262{
263 register_match6(&frag);
264}