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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/* Shared library add-on to iptables for DCCP matching
*
* (C) 2005 by Harald Welte <laforge@netfilter.org>
*
* This program is distributed under the terms of GNU GPL v2, 1991
*
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <xtables.h>
#include <linux/dccp.h>
#include <linux/netfilter/x_tables.h>
#include <linux/netfilter/xt_dccp.h>
#if 0
#define DEBUGP(format, first...) printf(format, ##first)
#define static
#else
#define DEBUGP(format, fist...)
#endif
enum {
O_SOURCE_PORT = 0,
O_DEST_PORT,
O_DCCP_TYPES,
O_DCCP_OPTION,
};
static void dccp_help(void)
{
printf(
"dccp match options\n"
"[!] --source-port port[:port] match source port(s)\n"
" --sport ...\n"
"[!] --destination-port port[:port] match destination port(s)\n"
" --dport ...\n"
"[!] --dccp-types type[,...] match when packet is one of the given types\n"
"[!] --dccp-option option match if option (by number!) is set\n"
);
}
#define s struct xt_dccp_info
static const struct xt_option_entry dccp_opts[] = {
{.name = "source-port", .id = O_SOURCE_PORT, .type = XTTYPE_PORTRC,
.flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, spts)},
{.name = "sport", .id = O_SOURCE_PORT, .type = XTTYPE_PORTRC,
.flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, spts)},
{.name = "destination-port", .id = O_DEST_PORT, .type = XTTYPE_PORTRC,
.flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, dpts)},
{.name = "dport", .id = O_DEST_PORT, .type = XTTYPE_PORTRC,
.flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, dpts)},
{.name = "dccp-types", .id = O_DCCP_TYPES, .type = XTTYPE_STRING,
.flags = XTOPT_INVERT},
{.name = "dccp-option", .id = O_DCCP_OPTION, .type = XTTYPE_UINT8,
.min = 1, .max = UINT8_MAX, .flags = XTOPT_INVERT | XTOPT_PUT,
XTOPT_POINTER(s, option)},
XTOPT_TABLEEND,
};
#undef s
static const char *const dccp_pkt_types[] = {
[DCCP_PKT_REQUEST] = "REQUEST",
[DCCP_PKT_RESPONSE] = "RESPONSE",
[DCCP_PKT_DATA] = "DATA",
[DCCP_PKT_ACK] = "ACK",
[DCCP_PKT_DATAACK] = "DATAACK",
[DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
[DCCP_PKT_CLOSE] = "CLOSE",
[DCCP_PKT_RESET] = "RESET",
[DCCP_PKT_SYNC] = "SYNC",
[DCCP_PKT_SYNCACK] = "SYNCACK",
[DCCP_PKT_INVALID] = "INVALID",
};
static uint16_t
parse_dccp_types(const char *typestring)
{
uint16_t typemask = 0;
char *ptr, *buffer;
buffer = strdup(typestring);
for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
unsigned int i;
for (i = 0; i < ARRAY_SIZE(dccp_pkt_types); ++i)
if (!strcasecmp(dccp_pkt_types[i], ptr)) {
typemask |= (1 << i);
break;
}
if (i == ARRAY_SIZE(dccp_pkt_types))
xtables_error(PARAMETER_PROBLEM,
"Unknown DCCP type `%s'", ptr);
}
free(buffer);
return typemask;
}
static void dccp_parse(struct xt_option_call *cb)
{
struct xt_dccp_info *einfo = cb->data;
xtables_option_parse(cb);
switch (cb->entry->id) {
case O_SOURCE_PORT:
einfo->flags |= XT_DCCP_SRC_PORTS;
if (cb->invert)
einfo->invflags |= XT_DCCP_SRC_PORTS;
break;
case O_DEST_PORT:
einfo->flags |= XT_DCCP_DEST_PORTS;
if (cb->invert)
einfo->invflags |= XT_DCCP_DEST_PORTS;
break;
case O_DCCP_TYPES:
einfo->flags |= XT_DCCP_TYPE;
einfo->typemask = parse_dccp_types(cb->arg);
if (cb->invert)
einfo->invflags |= XT_DCCP_TYPE;
break;
case O_DCCP_OPTION:
einfo->flags |= XT_DCCP_OPTION;
if (cb->invert)
einfo->invflags |= XT_DCCP_OPTION;
break;
}
}
static const char *
port_to_service(int port)
{
const struct servent *service;
if ((service = getservbyport(htons(port), "dccp")))
return service->s_name;
return NULL;
}
static void
print_port(uint16_t port, int numeric)
{
const char *service;
if (numeric || (service = port_to_service(port)) == NULL)
printf("%u", port);
else
printf("%s", service);
}
static void
print_ports(const char *name, uint16_t min, uint16_t max,
int invert, int numeric)
{
const char *inv = invert ? "!" : "";
if (min != 0 || max != 0xFFFF || invert) {
printf(" %s", name);
if (min == max) {
printf(":%s", inv);
print_port(min, numeric);
} else {
printf("s:%s", inv);
print_port(min, numeric);
printf(":");
print_port(max, numeric);
}
}
}
static void
print_types(uint16_t types, int inverted, int numeric)
{
int have_type = 0;
if (inverted)
printf(" !");
printf(" ");
while (types) {
unsigned int i;
for (i = 0; !(types & (1 << i)); i++);
if (have_type)
printf(",");
else
have_type = 1;
if (numeric)
printf("%u", i);
else
printf("%s", dccp_pkt_types[i]);
types &= ~(1 << i);
}
}
static void
print_option(uint8_t option, int invert, int numeric)
{
if (option || invert)
printf(" option=%s%u", invert ? "!" : "", option);
}
static void
dccp_print(const void *ip, const struct xt_entry_match *match, int numeric)
{
const struct xt_dccp_info *einfo =
(const struct xt_dccp_info *)match->data;
printf(" dccp");
if (einfo->flags & XT_DCCP_SRC_PORTS) {
print_ports("spt", einfo->spts[0], einfo->spts[1],
einfo->invflags & XT_DCCP_SRC_PORTS,
numeric);
}
if (einfo->flags & XT_DCCP_DEST_PORTS) {
print_ports("dpt", einfo->dpts[0], einfo->dpts[1],
einfo->invflags & XT_DCCP_DEST_PORTS,
numeric);
}
if (einfo->flags & XT_DCCP_TYPE) {
print_types(einfo->typemask,
einfo->invflags & XT_DCCP_TYPE,
numeric);
}
if (einfo->flags & XT_DCCP_OPTION) {
print_option(einfo->option,
einfo->invflags & XT_DCCP_OPTION, numeric);
}
}
static void dccp_save(const void *ip, const struct xt_entry_match *match)
{
const struct xt_dccp_info *einfo =
(const struct xt_dccp_info *)match->data;
if (einfo->flags & XT_DCCP_SRC_PORTS) {
if (einfo->invflags & XT_DCCP_SRC_PORTS)
printf(" !");
if (einfo->spts[0] != einfo->spts[1])
printf(" --sport %u:%u",
einfo->spts[0], einfo->spts[1]);
else
printf(" --sport %u", einfo->spts[0]);
}
if (einfo->flags & XT_DCCP_DEST_PORTS) {
if (einfo->invflags & XT_DCCP_DEST_PORTS)
printf(" !");
if (einfo->dpts[0] != einfo->dpts[1])
printf(" --dport %u:%u",
einfo->dpts[0], einfo->dpts[1]);
else
printf(" --dport %u", einfo->dpts[0]);
}
if (einfo->flags & XT_DCCP_TYPE) {
printf("%s --dccp-types",
einfo->invflags & XT_DCCP_TYPE ? " !" : "");
print_types(einfo->typemask, false, 0);
}
if (einfo->flags & XT_DCCP_OPTION) {
printf("%s --dccp-option %u",
einfo->invflags & XT_DCCP_OPTION ? " !" : "",
einfo->option);
}
}
static const char *const dccp_pkt_types_xlate[] = {
[DCCP_PKT_REQUEST] = "request",
[DCCP_PKT_RESPONSE] = "response",
[DCCP_PKT_DATA] = "data",
[DCCP_PKT_ACK] = "ack",
[DCCP_PKT_DATAACK] = "dataack",
[DCCP_PKT_CLOSEREQ] = "closereq",
[DCCP_PKT_CLOSE] = "close",
[DCCP_PKT_RESET] = "reset",
[DCCP_PKT_SYNC] = "sync",
[DCCP_PKT_SYNCACK] = "syncack",
};
static int dccp_type_xlate(const struct xt_dccp_info *einfo,
struct xt_xlate *xl)
{
bool have_type = false, set_need = false;
uint16_t types = einfo->typemask;
if (types & (1 << DCCP_PKT_INVALID))
return 0;
xt_xlate_add(xl, " dccp type%s ", einfo->invflags ? " !=" : "");
if ((types != 0) && !(types == (types & -types))) {
xt_xlate_add(xl, "{");
set_need = true;
}
while (types) {
unsigned int i;
for (i = 0; !(types & (1 << i)); i++);
if (have_type)
xt_xlate_add(xl, ", ");
else
have_type = true;
xt_xlate_add(xl, "%s", dccp_pkt_types_xlate[i]);
types &= ~(1 << i);
}
if (set_need)
xt_xlate_add(xl, "}");
return 1;
}
static int dccp_xlate(struct xt_xlate *xl,
const struct xt_xlate_mt_params *params)
{
const struct xt_dccp_info *einfo =
(const struct xt_dccp_info *)params->match->data;
char *space = "";
int ret = 1;
xt_xlate_add(xl, "dccp ");
if (einfo->flags & XT_DCCP_SRC_PORTS) {
if (einfo->spts[0] != einfo->spts[1])
xt_xlate_add(xl, "sport%s %u-%u",
einfo->invflags & XT_DCCP_SRC_PORTS ? " !=" : "",
einfo->spts[0], einfo->spts[1]);
else
xt_xlate_add(xl, "sport%s %u",
einfo->invflags & XT_DCCP_SRC_PORTS ? " !=" : "",
einfo->spts[0]);
space = " ";
}
if (einfo->flags & XT_DCCP_DEST_PORTS) {
if (einfo->dpts[0] != einfo->dpts[1])
xt_xlate_add(xl, "%sdport%s %u-%u", space,
einfo->invflags & XT_DCCP_DEST_PORTS ? " !=" : "",
einfo->dpts[0], einfo->dpts[1]);
else
xt_xlate_add(xl, "%sdport%s %u", space,
einfo->invflags & XT_DCCP_DEST_PORTS ? " !=" : "",
einfo->dpts[0]);
}
if (einfo->flags & XT_DCCP_TYPE)
ret = dccp_type_xlate(einfo, xl);
if (einfo->flags & XT_DCCP_OPTION)
ret = 0;
return ret;
}
static struct xtables_match dccp_match = {
.name = "dccp",
.family = NFPROTO_UNSPEC,
.version = XTABLES_VERSION,
.size = XT_ALIGN(sizeof(struct xt_dccp_info)),
.userspacesize = XT_ALIGN(sizeof(struct xt_dccp_info)),
.help = dccp_help,
.print = dccp_print,
.save = dccp_save,
.x6_parse = dccp_parse,
.x6_options = dccp_opts,
.xlate = dccp_xlate,
};
void _init(void)
{
xtables_register_match(&dccp_match);
}
|