blob: 64e110d3da0c18c2550d019c5bc340ead2202a2d [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. */
Jan Engelhardt997045f2007-10-04 16:29:21 +000012static void frag_help(void)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000013{
14 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020015"frag match options:\n"
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000016" --fragid [!] id[:id] match the id (range)\n"
17" --fraglen [!] length total length of this header\n"
18" --fragres check the reserved filed, too\n"
András Kis-Szabód8a12a82002-04-24 09:36:30 +000019" --fragfirst matches on the first fragment\n"
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000020" [--fragmore|--fraglast] there are more fragments or this\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020021" is the last one\n");
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000022}
23
Jan Engelhardt997045f2007-10-04 16:29:21 +000024static const struct option frag_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000025 { .name = "fragid", .has_arg = 1, .val = '1' },
26 { .name = "fraglen", .has_arg = 1, .val = '2' },
27 { .name = "fragres", .has_arg = 0, .val = '3' },
28 { .name = "fragfirst", .has_arg = 0, .val = '4' },
29 { .name = "fragmore", .has_arg = 0, .val = '5' },
30 { .name = "fraglast", .has_arg = 0, .val = '6' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000031 { .name = NULL }
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000032};
33
34static u_int32_t
35parse_frag_id(const char *idstr, const char *typestr)
36{
37 unsigned long int id;
38 char* ep;
39
Harald Welte46a73cf2003-09-05 12:53:44 +000040 id = strtoul(idstr, &ep, 0);
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000041
42 if ( idstr == ep ) {
43 exit_error(PARAMETER_PROBLEM,
44 "FRAG no valid digits in %s `%s'", typestr, idstr);
45 }
46 if ( id == ULONG_MAX && errno == ERANGE ) {
47 exit_error(PARAMETER_PROBLEM,
48 "%s `%s' specified too big: would overflow",
49 typestr, idstr);
50 }
51 if ( *idstr != '\0' && *ep != '\0' ) {
52 exit_error(PARAMETER_PROBLEM,
53 "FRAG error parsing %s `%s'", typestr, idstr);
54 }
55 return (u_int32_t) id;
56}
57
58static void
59parse_frag_ids(const char *idstring, u_int32_t *ids)
60{
61 char *buffer;
62 char *cp;
63
64 buffer = strdup(idstring);
65 if ((cp = strchr(buffer, ':')) == NULL)
66 ids[0] = ids[1] = parse_frag_id(buffer,"id");
67 else {
68 *cp = '\0';
69 cp++;
70
71 ids[0] = buffer[0] ? parse_frag_id(buffer,"id") : 0;
72 ids[1] = cp[0] ? parse_frag_id(cp,"id") : 0xFFFFFFFF;
73 }
74 free(buffer);
75}
76
77/* Initialize the match. */
Jan Engelhardt997045f2007-10-04 16:29:21 +000078static void frag_init(struct xt_entry_match *m)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000079{
80 struct ip6t_frag *fraginfo = (struct ip6t_frag *)m->data;
81
82 fraginfo->ids[0] = 0x0L;
83 fraginfo->ids[1] = 0xFFFFFFFF;
84 fraginfo->hdrlen = 0;
85 fraginfo->flags = 0;
86 fraginfo->invflags = 0;
87}
88
89/* Function which parses command options; returns true if it
90 ate an option */
Jan Engelhardt997045f2007-10-04 16:29:21 +000091static int frag_parse(int c, char **argv, int invert, unsigned int *flags,
92 const void *entry, struct xt_entry_match **match)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +000093{
94 struct ip6t_frag *fraginfo = (struct ip6t_frag *)(*match)->data;
95
96 switch (c) {
97 case '1':
98 if (*flags & IP6T_FRAG_IDS)
99 exit_error(PARAMETER_PROBLEM,
100 "Only one `--fragid' allowed");
101 check_inverse(optarg, &invert, &optind, 0);
102 parse_frag_ids(argv[optind-1], fraginfo->ids);
103 if (invert)
104 fraginfo->invflags |= IP6T_FRAG_INV_IDS;
105 fraginfo->flags |= IP6T_FRAG_IDS;
106 *flags |= IP6T_FRAG_IDS;
107 break;
108 case '2':
109 if (*flags & IP6T_FRAG_LEN)
110 exit_error(PARAMETER_PROBLEM,
111 "Only one `--fraglen' allowed");
112 check_inverse(optarg, &invert, &optind, 0);
113 fraginfo->hdrlen = parse_frag_id(argv[optind-1], "length");
114 if (invert)
115 fraginfo->invflags |= IP6T_FRAG_INV_LEN;
116 fraginfo->flags |= IP6T_FRAG_LEN;
117 *flags |= IP6T_FRAG_LEN;
118 break;
119 case '3':
120 if (*flags & IP6T_FRAG_RES)
121 exit_error(PARAMETER_PROBLEM,
122 "Only one `--fragres' allowed");
123 fraginfo->flags |= IP6T_FRAG_RES;
124 *flags |= IP6T_FRAG_RES;
125 break;
126 case '4':
127 if (*flags & IP6T_FRAG_FST)
128 exit_error(PARAMETER_PROBLEM,
129 "Only one `--fragfirst' allowed");
130 fraginfo->flags |= IP6T_FRAG_FST;
131 *flags |= IP6T_FRAG_FST;
132 break;
133 case '5':
134 if (*flags & (IP6T_FRAG_MF|IP6T_FRAG_NMF))
135 exit_error(PARAMETER_PROBLEM,
136 "Only one `--fragmore' or `--fraglast' allowed");
137 fraginfo->flags |= IP6T_FRAG_MF;
138 *flags |= IP6T_FRAG_MF;
139 break;
140 case '6':
141 if (*flags & (IP6T_FRAG_MF|IP6T_FRAG_NMF))
142 exit_error(PARAMETER_PROBLEM,
143 "Only one `--fragmore' or `--fraglast' allowed");
144 fraginfo->flags |= IP6T_FRAG_NMF;
145 *flags |= IP6T_FRAG_NMF;
146 break;
147 default:
148 return 0;
149 }
150
151 return 1;
152}
153
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000154static void
155print_ids(const char *name, u_int32_t min, u_int32_t max,
156 int invert)
157{
158 const char *inv = invert ? "!" : "";
159
160 if (min != 0 || max != 0xFFFFFFFF || invert) {
161 printf("%s", name);
Harald Welte46a73cf2003-09-05 12:53:44 +0000162 if (min == max)
163 printf(":%s%u ", inv, min);
164 else
165 printf("s:%s%u:%u ", inv, min, max);
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000166 }
167}
168
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000169/* Prints out the union ip6t_matchinfo. */
Jan Engelhardt997045f2007-10-04 16:29:21 +0000170static void frag_print(const void *ip, const struct xt_entry_match *match,
171 int numeric)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000172{
173 const struct ip6t_frag *frag = (struct ip6t_frag *)match->data;
174
175 printf("frag ");
176 print_ids("id", frag->ids[0], frag->ids[1],
177 frag->invflags & IP6T_FRAG_INV_IDS);
Harald Welte46a73cf2003-09-05 12:53:44 +0000178
András Kis-Szabód8a12a82002-04-24 09:36:30 +0000179 if (frag->flags & IP6T_FRAG_LEN) {
Harald Welte46a73cf2003-09-05 12:53:44 +0000180 printf("length:%s%u ",
181 frag->invflags & IP6T_FRAG_INV_LEN ? "!" : "",
182 frag->hdrlen);
András Kis-Szabód8a12a82002-04-24 09:36:30 +0000183 }
Harald Welte46a73cf2003-09-05 12:53:44 +0000184
185 if (frag->flags & IP6T_FRAG_RES)
186 printf("reserved ");
187
188 if (frag->flags & IP6T_FRAG_FST)
189 printf("first ");
190
191 if (frag->flags & IP6T_FRAG_MF)
192 printf("more ");
193
194 if (frag->flags & IP6T_FRAG_NMF)
195 printf("last ");
196
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000197 if (frag->invflags & ~IP6T_FRAG_INV_MASK)
198 printf("Unknown invflags: 0x%X ",
199 frag->invflags & ~IP6T_FRAG_INV_MASK);
200}
201
202/* Saves the union ip6t_matchinfo in parsable form to stdout. */
Jan Engelhardt997045f2007-10-04 16:29:21 +0000203static void frag_save(const void *ip, const struct xt_entry_match *match)
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000204{
205 const struct ip6t_frag *fraginfo = (struct ip6t_frag *)match->data;
206
207 if (!(fraginfo->ids[0] == 0
208 && fraginfo->ids[1] == 0xFFFFFFFF)) {
209 printf("--fragid %s",
210 (fraginfo->invflags & IP6T_FRAG_INV_IDS) ? "! " : "");
211 if (fraginfo->ids[0]
212 != fraginfo->ids[1])
213 printf("%u:%u ",
214 fraginfo->ids[0],
215 fraginfo->ids[1]);
216 else
217 printf("%u ",
218 fraginfo->ids[0]);
219 }
220
András Kis-Szabód8a12a82002-04-24 09:36:30 +0000221 if (fraginfo->flags & IP6T_FRAG_LEN) {
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000222 printf("--fraglen %s%u ",
223 (fraginfo->invflags & IP6T_FRAG_INV_LEN) ? "! " : "",
224 fraginfo->hdrlen);
225 }
226
Harald Welte46a73cf2003-09-05 12:53:44 +0000227 if (fraginfo->flags & IP6T_FRAG_RES)
228 printf("--fragres ");
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000229
Harald Welte46a73cf2003-09-05 12:53:44 +0000230 if (fraginfo->flags & IP6T_FRAG_FST)
231 printf("--fragfirst ");
232
233 if (fraginfo->flags & IP6T_FRAG_MF)
234 printf("--fragmore ");
235
236 if (fraginfo->flags & IP6T_FRAG_NMF)
237 printf("--fraglast ");
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000238}
239
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200240static struct xtables_match frag_mt6_reg = {
Harald Welte46a73cf2003-09-05 12:53:44 +0000241 .name = "frag",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200242 .version = XTABLES_VERSION,
243 .family = PF_INET6,
244 .size = XT_ALIGN(sizeof(struct ip6t_frag)),
245 .userspacesize = XT_ALIGN(sizeof(struct ip6t_frag)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000246 .help = frag_help,
247 .init = frag_init,
248 .parse = frag_parse,
249 .print = frag_print,
250 .save = frag_save,
251 .extra_opts = frag_opts,
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000252};
253
254void
255_init(void)
256{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200257 xtables_register_match(&frag_mt6_reg);
András Kis-Szabóf1f447b2002-03-26 12:45:19 +0000258}