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. */
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"
20 " --fragfirst matches on the first fragment\n"
21 " [--fragmore|--fraglast] there are more fragments or this\n"
26 static struct option opts
[] = {
27 { .name
= "fragid", .has_arg
= 1, .flag
= 0, .val
= '1' },
28 { .name
= "fraglen", .has_arg
= 1, .flag
= 0, .val
= '2' },
29 { .name
= "fragres", .has_arg
= 0, .flag
= 0, .val
= '3' },
30 { .name
= "fragfirst", .has_arg
= 0, .flag
= 0, .val
= '4' },
31 { .name
= "fragmore", .has_arg
= 0, .flag
= 0, .val
= '5' },
32 { .name
= "fraglast", .has_arg
= 0, .flag
= 0, .val
= '6' },
37 parse_frag_id(const char *idstr
, const char *typestr
)
42 id
= strtoul(idstr
, &ep
, 0);
45 exit_error(PARAMETER_PROBLEM
,
46 "FRAG no valid digits in %s `%s'", typestr
, idstr
);
48 if ( id
== ULONG_MAX
&& errno
== ERANGE
) {
49 exit_error(PARAMETER_PROBLEM
,
50 "%s `%s' specified too big: would overflow",
53 if ( *idstr
!= '\0' && *ep
!= '\0' ) {
54 exit_error(PARAMETER_PROBLEM
,
55 "FRAG error parsing %s `%s'", typestr
, idstr
);
57 return (u_int32_t
) id
;
61 parse_frag_ids(const char *idstring
, u_int32_t
*ids
)
66 buffer
= strdup(idstring
);
67 if ((cp
= strchr(buffer
, ':')) == NULL
)
68 ids
[0] = ids
[1] = parse_frag_id(buffer
,"id");
73 ids
[0] = buffer
[0] ? parse_frag_id(buffer
,"id") : 0;
74 ids
[1] = cp
[0] ? parse_frag_id(cp
,"id") : 0xFFFFFFFF;
79 /* Initialize the match. */
81 init(struct ip6t_entry_match
*m
, unsigned int *nfcache
)
83 struct ip6t_frag
*fraginfo
= (struct ip6t_frag
*)m
->data
;
85 fraginfo
->ids
[0] = 0x0L
;
86 fraginfo
->ids
[1] = 0xFFFFFFFF;
89 fraginfo
->invflags
= 0;
92 /* Function which parses command options; returns true if it
95 parse(int c
, char **argv
, int invert
, unsigned int *flags
,
96 const struct ip6t_entry
*entry
,
97 unsigned int *nfcache
,
98 struct ip6t_entry_match
**match
)
100 struct ip6t_frag
*fraginfo
= (struct ip6t_frag
*)(*match
)->data
;
104 if (*flags
& IP6T_FRAG_IDS
)
105 exit_error(PARAMETER_PROBLEM
,
106 "Only one `--fragid' allowed");
107 check_inverse(optarg
, &invert
, &optind
, 0);
108 parse_frag_ids(argv
[optind
-1], fraginfo
->ids
);
110 fraginfo
->invflags
|= IP6T_FRAG_INV_IDS
;
111 fraginfo
->flags
|= IP6T_FRAG_IDS
;
112 *flags
|= IP6T_FRAG_IDS
;
115 if (*flags
& IP6T_FRAG_LEN
)
116 exit_error(PARAMETER_PROBLEM
,
117 "Only one `--fraglen' allowed");
118 check_inverse(optarg
, &invert
, &optind
, 0);
119 fraginfo
->hdrlen
= parse_frag_id(argv
[optind
-1], "length");
121 fraginfo
->invflags
|= IP6T_FRAG_INV_LEN
;
122 fraginfo
->flags
|= IP6T_FRAG_LEN
;
123 *flags
|= IP6T_FRAG_LEN
;
126 if (*flags
& IP6T_FRAG_RES
)
127 exit_error(PARAMETER_PROBLEM
,
128 "Only one `--fragres' allowed");
129 fraginfo
->flags
|= IP6T_FRAG_RES
;
130 *flags
|= IP6T_FRAG_RES
;
133 if (*flags
& IP6T_FRAG_FST
)
134 exit_error(PARAMETER_PROBLEM
,
135 "Only one `--fragfirst' allowed");
136 fraginfo
->flags
|= IP6T_FRAG_FST
;
137 *flags
|= IP6T_FRAG_FST
;
140 if (*flags
& (IP6T_FRAG_MF
|IP6T_FRAG_NMF
))
141 exit_error(PARAMETER_PROBLEM
,
142 "Only one `--fragmore' or `--fraglast' allowed");
143 fraginfo
->flags
|= IP6T_FRAG_MF
;
144 *flags
|= IP6T_FRAG_MF
;
147 if (*flags
& (IP6T_FRAG_MF
|IP6T_FRAG_NMF
))
148 exit_error(PARAMETER_PROBLEM
,
149 "Only one `--fragmore' or `--fraglast' allowed");
150 fraginfo
->flags
|= IP6T_FRAG_NMF
;
151 *flags
|= IP6T_FRAG_NMF
;
160 /* Final check; we don't care. */
162 final_check(unsigned int flags
)
167 print_ids(const char *name
, u_int32_t min
, u_int32_t max
,
170 const char *inv
= invert
? "!" : "";
172 if (min
!= 0 || max
!= 0xFFFFFFFF || invert
) {
175 printf(":%s%u ", inv
, min
);
177 printf("s:%s%u:%u ", inv
, min
, max
);
181 /* Prints out the union ip6t_matchinfo. */
183 print(const struct ip6t_ip6
*ip
,
184 const struct ip6t_entry_match
*match
, int numeric
)
186 const struct ip6t_frag
*frag
= (struct ip6t_frag
*)match
->data
;
189 print_ids("id", frag
->ids
[0], frag
->ids
[1],
190 frag
->invflags
& IP6T_FRAG_INV_IDS
);
192 if (frag
->flags
& IP6T_FRAG_LEN
) {
193 printf("length:%s%u ",
194 frag
->invflags
& IP6T_FRAG_INV_LEN
? "!" : "",
198 if (frag
->flags
& IP6T_FRAG_RES
)
201 if (frag
->flags
& IP6T_FRAG_FST
)
204 if (frag
->flags
& IP6T_FRAG_MF
)
207 if (frag
->flags
& IP6T_FRAG_NMF
)
210 if (frag
->invflags
& ~IP6T_FRAG_INV_MASK
)
211 printf("Unknown invflags: 0x%X ",
212 frag
->invflags
& ~IP6T_FRAG_INV_MASK
);
215 /* Saves the union ip6t_matchinfo in parsable form to stdout. */
216 static void save(const struct ip6t_ip6
*ip
, const struct ip6t_entry_match
*match
)
218 const struct ip6t_frag
*fraginfo
= (struct ip6t_frag
*)match
->data
;
220 if (!(fraginfo
->ids
[0] == 0
221 && fraginfo
->ids
[1] == 0xFFFFFFFF)) {
222 printf("--fragid %s",
223 (fraginfo
->invflags
& IP6T_FRAG_INV_IDS
) ? "! " : "");
234 if (fraginfo
->flags
& IP6T_FRAG_LEN
) {
235 printf("--fraglen %s%u ",
236 (fraginfo
->invflags
& IP6T_FRAG_INV_LEN
) ? "! " : "",
240 if (fraginfo
->flags
& IP6T_FRAG_RES
)
241 printf("--fragres ");
243 if (fraginfo
->flags
& IP6T_FRAG_FST
)
244 printf("--fragfirst ");
246 if (fraginfo
->flags
& IP6T_FRAG_MF
)
247 printf("--fragmore ");
249 if (fraginfo
->flags
& IP6T_FRAG_NMF
)
250 printf("--fraglast ");
254 struct ip6tables_match frag
= {
256 .version
= IPTABLES_VERSION
,
257 .size
= IP6T_ALIGN(sizeof(struct ip6t_frag
)),
258 .userspacesize
= IP6T_ALIGN(sizeof(struct ip6t_frag
)),
262 .final_check
= &final_check
,
271 register_match6(&frag
);