1 /* Shared library add-on to ip6tables to add Fragmentation header support. */
9 #include <linux/netfilter_ipv6/ip6t_frag.h>
11 /* Function which prints out usage message. */
12 static void frag_help(void)
15 "frag match options:\n"
16 " --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"
19 " --fragfirst matches on the first fragment\n"
20 " [--fragmore|--fraglast] there are more fragments or this\n"
21 " is the last one\n");
24 static const struct option frag_opts[] = {
25 { .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' },
35 parse_frag_id(const char *idstr, const char *typestr)
40 id = strtoul(idstr, &ep, 0);
43 exit_error(PARAMETER_PROBLEM,
44 "FRAG no valid digits in %s `%s'", typestr, idstr);
46 if ( id == ULONG_MAX && errno == ERANGE ) {
47 exit_error(PARAMETER_PROBLEM,
48 "%s `%s' specified too big: would overflow",
51 if ( *idstr != '\0' && *ep != '\0' ) {
52 exit_error(PARAMETER_PROBLEM,
53 "FRAG error parsing %s `%s'", typestr, idstr);
55 return (u_int32_t) id;
59 parse_frag_ids(const char *idstring, u_int32_t *ids)
64 buffer = strdup(idstring);
65 if ((cp = strchr(buffer, ':')) == NULL)
66 ids[0] = ids[1] = parse_frag_id(buffer,"id");
71 ids[0] = buffer[0] ? parse_frag_id(buffer,"id") : 0;
72 ids[1] = cp[0] ? parse_frag_id(cp,"id") : 0xFFFFFFFF;
77 /* Initialize the match. */
78 static void frag_init(struct xt_entry_match *m)
80 struct ip6t_frag *fraginfo = (struct ip6t_frag *)m->data;
82 fraginfo->ids[0] = 0x0L;
83 fraginfo->ids[1] = 0xFFFFFFFF;
86 fraginfo->invflags = 0;
89 /* Function which parses command options; returns true if it
91 static int frag_parse(int c, char **argv, int invert, unsigned int *flags,
92 const void *entry, struct xt_entry_match **match)
94 struct ip6t_frag *fraginfo = (struct ip6t_frag *)(*match)->data;
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);
104 fraginfo->invflags |= IP6T_FRAG_INV_IDS;
105 fraginfo->flags |= IP6T_FRAG_IDS;
106 *flags |= IP6T_FRAG_IDS;
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");
115 fraginfo->invflags |= IP6T_FRAG_INV_LEN;
116 fraginfo->flags |= IP6T_FRAG_LEN;
117 *flags |= IP6T_FRAG_LEN;
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;
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;
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;
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;
155 print_ids(const char *name, u_int32_t min, u_int32_t max,
158 const char *inv = invert ? "!" : "";
160 if (min != 0 || max != 0xFFFFFFFF || invert) {
163 printf(":%s%u ", inv, min);
165 printf("s:%s%u:%u ", inv, min, max);
169 /* Prints out the union ip6t_matchinfo. */
170 static void frag_print(const void *ip, const struct xt_entry_match *match,
173 const struct ip6t_frag *frag = (struct ip6t_frag *)match->data;
176 print_ids("id", frag->ids[0], frag->ids[1],
177 frag->invflags & IP6T_FRAG_INV_IDS);
179 if (frag->flags & IP6T_FRAG_LEN) {
180 printf("length:%s%u ",
181 frag->invflags & IP6T_FRAG_INV_LEN ? "!" : "",
185 if (frag->flags & IP6T_FRAG_RES)
188 if (frag->flags & IP6T_FRAG_FST)
191 if (frag->flags & IP6T_FRAG_MF)
194 if (frag->flags & IP6T_FRAG_NMF)
197 if (frag->invflags & ~IP6T_FRAG_INV_MASK)
198 printf("Unknown invflags: 0x%X ",
199 frag->invflags & ~IP6T_FRAG_INV_MASK);
202 /* Saves the union ip6t_matchinfo in parsable form to stdout. */
203 static void frag_save(const void *ip, const struct xt_entry_match *match)
205 const struct ip6t_frag *fraginfo = (struct ip6t_frag *)match->data;
207 if (!(fraginfo->ids[0] == 0
208 && fraginfo->ids[1] == 0xFFFFFFFF)) {
209 printf("--fragid %s",
210 (fraginfo->invflags & IP6T_FRAG_INV_IDS) ? "! " : "");
221 if (fraginfo->flags & IP6T_FRAG_LEN) {
222 printf("--fraglen %s%u ",
223 (fraginfo->invflags & IP6T_FRAG_INV_LEN) ? "! " : "",
227 if (fraginfo->flags & IP6T_FRAG_RES)
228 printf("--fragres ");
230 if (fraginfo->flags & IP6T_FRAG_FST)
231 printf("--fragfirst ");
233 if (fraginfo->flags & IP6T_FRAG_MF)
234 printf("--fragmore ");
236 if (fraginfo->flags & IP6T_FRAG_NMF)
237 printf("--fraglast ");
240 static struct xtables_match frag_mt6_reg = {
242 .version = XTABLES_VERSION,
244 .size = XT_ALIGN(sizeof(struct ip6t_frag)),
245 .userspacesize = XT_ALIGN(sizeof(struct ip6t_frag)),
251 .extra_opts = frag_opts,
257 xtables_register_match(&frag_mt6_reg);