1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/* Shared library add-on to iptables to add ipv4 options matching support. */
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <iptables.h>
#include <linux/netfilter_ipv4/ipt_ipv4options.h>
/* Function which prints out usage message. */
static void
help(void)
{
printf(
"ipv4options v%s options:\n"
" --ssrr (match strict source routing flag)\n"
" --lsrr (match loose source routing flag)\n"
" --no-srr (match packets with no source routing)\n\n"
" [!] --rr (match record route flag)\n\n"
" [!] --ts (match timestamp flag)\n\n"
" [!] --ra (match router-alert option)\n\n"
" [!] --any-opt (match any option or no option at all if used with '!')\n",
NETFILTER_VERSION);
}
static struct option opts[] = {
{ "ssrr", 0, 0, '1' },
{ "lsrr", 0, 0, '2' },
{ "no-srr", 0, 0, '3'},
{ "rr", 0, 0, '4'},
{ "ts", 0, 0, '5'},
{ "ra", 0, 0, '6'},
{ "any-opt", 0, 0, '7'},
{0}
};
/* Initialize the match. */
static void
init(struct ipt_entry_match *m, unsigned int *nfcache)
{
/* caching not yet implemented */
*nfcache |= NFC_UNKNOWN;
}
/* Function which parses command options; returns true if it
ate an option */
static int
parse(int c, char **argv, int invert, unsigned int *flags,
const struct ipt_entry *entry,
unsigned int *nfcache,
struct ipt_entry_match **match)
{
struct ipt_ipv4options_info *info = (struct ipt_ipv4options_info *)(*match)->data;
switch (c)
{
/* strict-source-routing */
case '1':
if (invert)
exit_error(PARAMETER_PROBLEM,
"ipv4options: unexpected `!' with --ssrr");
if (*flags & IPT_IPV4OPTION_MATCH_SSRR)
exit_error(PARAMETER_PROBLEM,
"Can't specify --ssrr twice");
if (*flags & IPT_IPV4OPTION_MATCH_LSRR)
exit_error(PARAMETER_PROBLEM,
"Can't specify --ssrr with --lsrr");
if (*flags & IPT_IPV4OPTION_DONT_MATCH_SRR)
exit_error(PARAMETER_PROBLEM,
"Can't specify --ssrr with --no-srr");
info->options |= IPT_IPV4OPTION_MATCH_SSRR;
*flags |= IPT_IPV4OPTION_MATCH_SSRR;
break;
/* loose-source-routing */
case '2':
if (invert)
exit_error(PARAMETER_PROBLEM,
"ipv4options: unexpected `!' with --lsrr");
if (*flags & IPT_IPV4OPTION_MATCH_SSRR)
exit_error(PARAMETER_PROBLEM,
"Can't specify --lsrr twice");
if (*flags & IPT_IPV4OPTION_MATCH_LSRR)
exit_error(PARAMETER_PROBLEM,
"Can't specify --lsrr with --ssrr");
if (*flags & IPT_IPV4OPTION_DONT_MATCH_SRR)
exit_error(PARAMETER_PROBLEM,
"Can't specify --lsrr with --no-srr");
info->options |= IPT_IPV4OPTION_MATCH_LSRR;
*flags |= IPT_IPV4OPTION_MATCH_LSRR;
break;
/* no-source-routing */
case '3':
if (invert)
exit_error(PARAMETER_PROBLEM,
"ipv4options: unexpected `!' with --no-srr");
if (*flags & IPT_IPV4OPTION_DONT_MATCH_SRR)
exit_error(PARAMETER_PROBLEM,
"Can't specify --no-srr twice");
if (*flags & IPT_IPV4OPTION_MATCH_SSRR)
exit_error(PARAMETER_PROBLEM,
"Can't specify --no-srr with --ssrr");
if (*flags & IPT_IPV4OPTION_MATCH_LSRR)
exit_error(PARAMETER_PROBLEM,
"Can't specify --no-srr with --lsrr");
info->options |= IPT_IPV4OPTION_DONT_MATCH_SRR;
*flags |= IPT_IPV4OPTION_DONT_MATCH_SRR;
break;
/* record-route */
case '4':
if ((!invert) && (*flags & IPT_IPV4OPTION_MATCH_RR))
exit_error(PARAMETER_PROBLEM,
"Can't specify --rr twice");
if (invert && (*flags & IPT_IPV4OPTION_DONT_MATCH_RR))
exit_error(PARAMETER_PROBLEM,
"Can't specify ! --rr twice");
if ((!invert) && (*flags & IPT_IPV4OPTION_DONT_MATCH_RR))
exit_error(PARAMETER_PROBLEM,
"Can't specify --rr with ! --rr");
if (invert && (*flags & IPT_IPV4OPTION_MATCH_RR))
exit_error(PARAMETER_PROBLEM,
"Can't specify ! --rr with --rr");
if (invert) {
info->options |= IPT_IPV4OPTION_DONT_MATCH_RR;
*flags |= IPT_IPV4OPTION_DONT_MATCH_RR;
}
else {
info->options |= IPT_IPV4OPTION_MATCH_RR;
*flags |= IPT_IPV4OPTION_MATCH_RR;
}
break;
/* timestamp */
case '5':
if ((!invert) && (*flags & IPT_IPV4OPTION_MATCH_TIMESTAMP))
exit_error(PARAMETER_PROBLEM,
"Can't specify --ts twice");
if (invert && (*flags & IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP))
exit_error(PARAMETER_PROBLEM,
"Can't specify ! --ts twice");
if ((!invert) && (*flags & IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP))
exit_error(PARAMETER_PROBLEM,
"Can't specify --ts with ! --ts");
if (invert && (*flags & IPT_IPV4OPTION_MATCH_TIMESTAMP))
exit_error(PARAMETER_PROBLEM,
"Can't specify ! --ts with --ts");
if (invert) {
info->options |= IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP;
*flags |= IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP;
}
else {
info->options |= IPT_IPV4OPTION_MATCH_TIMESTAMP;
*flags |= IPT_IPV4OPTION_MATCH_TIMESTAMP;
}
break;
/* router-alert */
case '6':
if ((!invert) && (*flags & IPT_IPV4OPTION_MATCH_ROUTER_ALERT))
exit_error(PARAMETER_PROBLEM,
"Can't specify --ra twice");
if (invert && (*flags & IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT))
exit_error(PARAMETER_PROBLEM,
"Can't specify ! --rr twice");
if ((!invert) && (*flags & IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT))
exit_error(PARAMETER_PROBLEM,
"Can't specify --ra with ! --ra");
if (invert && (*flags & IPT_IPV4OPTION_MATCH_ROUTER_ALERT))
exit_error(PARAMETER_PROBLEM,
"Can't specify ! --ra with --ra");
if (invert) {
info->options |= IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT;
*flags |= IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT;
}
else {
info->options |= IPT_IPV4OPTION_MATCH_ROUTER_ALERT;
*flags |= IPT_IPV4OPTION_MATCH_ROUTER_ALERT;
}
break;
/* any option */
case '7' :
if ((!invert) && (*flags & IPT_IPV4OPTION_MATCH_ANY_OPT))
exit_error(PARAMETER_PROBLEM,
"Can't specify --any-opt twice");
if (invert && (*flags & IPT_IPV4OPTION_MATCH_ANY_OPT))
exit_error(PARAMETER_PROBLEM,
"Can't specify ! --any-opt with --any-opt");
if (invert && (*flags & IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT))
exit_error(PARAMETER_PROBLEM,
"Can't specify ! --any-opt twice");
if ((!invert) &&
((*flags & IPT_IPV4OPTION_DONT_MATCH_SRR) ||
(*flags & IPT_IPV4OPTION_DONT_MATCH_RR) ||
(*flags & IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP) ||
(*flags & IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT)))
exit_error(PARAMETER_PROBLEM,
"Can't specify --any-opt with any other negative ipv4options match");
if (invert &&
((*flags & IPT_IPV4OPTION_MATCH_LSRR) ||
(*flags & IPT_IPV4OPTION_MATCH_SSRR) ||
(*flags & IPT_IPV4OPTION_MATCH_RR) ||
(*flags & IPT_IPV4OPTION_MATCH_TIMESTAMP) ||
(*flags & IPT_IPV4OPTION_MATCH_ROUTER_ALERT)))
exit_error(PARAMETER_PROBLEM,
"Can't specify ! --any-opt with any other positive ipv4options match");
if (invert) {
info->options |= IPT_IPV4OPTION_DONT_MATCH_ANY_OPT;
*flags |= IPT_IPV4OPTION_DONT_MATCH_ANY_OPT;
}
else {
info->options |= IPT_IPV4OPTION_MATCH_ANY_OPT;
*flags |= IPT_IPV4OPTION_MATCH_ANY_OPT;
}
break;
default:
return 0;
}
return 1;
}
static void
final_check(unsigned int flags)
{
if (flags == 0)
exit_error(PARAMETER_PROBLEM,
"ipv4options match: you must specify some parameters. See iptables -m ipv4options --help for help.'");
}
/* Prints out the matchinfo. */
static void
print(const struct ipt_ip *ip,
const struct ipt_entry_match *match,
int numeric)
{
struct ipt_ipv4options_info *info = ((struct ipt_ipv4options_info *)match->data);
printf(" IPV4OPTS");
if (info->options & IPT_IPV4OPTION_MATCH_SSRR)
printf(" SSRR");
else if (info->options & IPT_IPV4OPTION_MATCH_LSRR)
printf(" LSRR");
else if (info->options & IPT_IPV4OPTION_DONT_MATCH_SRR)
printf(" !SRR");
if (info->options & IPT_IPV4OPTION_MATCH_RR)
printf(" RR");
else if (info->options & IPT_IPV4OPTION_DONT_MATCH_RR)
printf(" !RR");
if (info->options & IPT_IPV4OPTION_MATCH_TIMESTAMP)
printf(" TS");
else if (info->options & IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP)
printf(" !TS");
if (info->options & IPT_IPV4OPTION_MATCH_ROUTER_ALERT)
printf(" RA");
else if (info->options & IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT)
printf(" !RA");
if (info->options & IPT_IPV4OPTION_MATCH_ANY_OPT)
printf(" ANYOPT ");
else if (info->options & IPT_IPV4OPTION_DONT_MATCH_ANY_OPT)
printf(" NOOPT");
printf(" ");
}
/* Saves the data in parsable form to stdout. */
static void
save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
{
struct ipt_ipv4options_info *info = ((struct ipt_ipv4options_info *)match->data);
if (info->options & IPT_IPV4OPTION_MATCH_SSRR)
printf(" --ssrr");
else if (info->options & IPT_IPV4OPTION_MATCH_LSRR)
printf(" --lsrr");
else if (info->options & IPT_IPV4OPTION_DONT_MATCH_SRR)
printf(" --no-srr");
if (info->options & IPT_IPV4OPTION_MATCH_RR)
printf(" --rr");
else if (info->options & IPT_IPV4OPTION_DONT_MATCH_RR)
printf(" ! --rr");
if (info->options & IPT_IPV4OPTION_MATCH_TIMESTAMP)
printf(" --ts");
else if (info->options & IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP)
printf(" ! --ts");
if (info->options & IPT_IPV4OPTION_MATCH_ROUTER_ALERT)
printf(" --ra");
else if (info->options & IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT)
printf(" ! --ra");
if (info->options & IPT_IPV4OPTION_MATCH_ANY_OPT)
printf(" --any-opt");
if (info->options & IPT_IPV4OPTION_DONT_MATCH_ANY_OPT)
printf(" ! --any-opt");
printf(" ");
}
static
struct iptables_match ipv4options_struct
= { NULL,
"ipv4options",
NETFILTER_VERSION,
IPT_ALIGN(sizeof(struct ipt_ipv4options_info)),
IPT_ALIGN(sizeof(struct ipt_ipv4options_info)),
&help,
&init,
&parse,
&final_check,
&print,
&save,
opts
};
void _init(void)
{
register_match(&ipv4options_struct);
}
|