• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Shared library add-on to iptables for DCCP matching
2  *
3  * (C) 2005 by Harald Welte <laforge@netfilter.org>
4  *
5  * This program is distributed under the terms of GNU GPL v2, 1991
6  *
7  */
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <netdb.h>
13 #include <arpa/inet.h>
14 #include <xtables.h>
15 #include <linux/dccp.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_dccp.h>
18 
19 #if 0
20 #define DEBUGP(format, first...) printf(format, ##first)
21 #define static
22 #else
23 #define DEBUGP(format, fist...)
24 #endif
25 
26 enum {
27 	O_SOURCE_PORT = 0,
28 	O_DEST_PORT,
29 	O_DCCP_TYPES,
30 	O_DCCP_OPTION,
31 };
32 
dccp_help(void)33 static void dccp_help(void)
34 {
35 	printf(
36 "dccp match options\n"
37 "[!] --source-port port[:port]                          match source port(s)\n"
38 " --sport ...\n"
39 "[!] --destination-port port[:port]                     match destination port(s)\n"
40 " --dport ...\n"
41 "[!] --dccp-types type[,...]                            match when packet is one of the given types\n"
42 "[!] --dccp-option option                               match if option (by number!) is set\n"
43 );
44 }
45 
46 #define s struct xt_dccp_info
47 static const struct xt_option_entry dccp_opts[] = {
48 	{.name = "source-port", .id = O_SOURCE_PORT, .type = XTTYPE_PORTRC,
49 	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, spts)},
50 	{.name = "sport", .id = O_SOURCE_PORT, .type = XTTYPE_PORTRC,
51 	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, spts)},
52 	{.name = "destination-port", .id = O_DEST_PORT, .type = XTTYPE_PORTRC,
53 	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, dpts)},
54 	{.name = "dport", .id = O_DEST_PORT, .type = XTTYPE_PORTRC,
55 	 .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, dpts)},
56 	{.name = "dccp-types", .id = O_DCCP_TYPES, .type = XTTYPE_STRING,
57 	 .flags = XTOPT_INVERT},
58 	{.name = "dccp-option", .id = O_DCCP_OPTION, .type = XTTYPE_UINT8,
59 	 .min = 1, .max = UINT8_MAX, .flags = XTOPT_INVERT | XTOPT_PUT,
60 	 XTOPT_POINTER(s, option)},
61 	XTOPT_TABLEEND,
62 };
63 #undef s
64 
65 static const char *const dccp_pkt_types[] = {
66 	[DCCP_PKT_REQUEST] 	= "REQUEST",
67 	[DCCP_PKT_RESPONSE]	= "RESPONSE",
68 	[DCCP_PKT_DATA]		= "DATA",
69 	[DCCP_PKT_ACK]		= "ACK",
70 	[DCCP_PKT_DATAACK]	= "DATAACK",
71 	[DCCP_PKT_CLOSEREQ]	= "CLOSEREQ",
72 	[DCCP_PKT_CLOSE]	= "CLOSE",
73 	[DCCP_PKT_RESET]	= "RESET",
74 	[DCCP_PKT_SYNC]		= "SYNC",
75 	[DCCP_PKT_SYNCACK]	= "SYNCACK",
76 	[DCCP_PKT_INVALID]	= "INVALID",
77 };
78 
79 /* Bits for type values 11-15 */
80 #define INVALID_OTHER_TYPE_MASK		0xf800
81 
82 static uint16_t
parse_dccp_types(const char * typestring)83 parse_dccp_types(const char *typestring)
84 {
85 	uint16_t typemask = 0;
86 	char *ptr, *buffer;
87 
88 	buffer = strdup(typestring);
89 
90 	for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
91 		unsigned int i;
92 		for (i = 0; i < ARRAY_SIZE(dccp_pkt_types); ++i)
93 			if (!strcasecmp(dccp_pkt_types[i], ptr)) {
94 				typemask |= (1 << i);
95 				break;
96 			}
97 		if (i == ARRAY_SIZE(dccp_pkt_types))
98 			xtables_error(PARAMETER_PROBLEM,
99 				   "Unknown DCCP type `%s'", ptr);
100 	}
101 	if (typemask & (1 << DCCP_PKT_INVALID))
102 		typemask |= INVALID_OTHER_TYPE_MASK;
103 
104 
105 	free(buffer);
106 	return typemask;
107 }
108 
dccp_parse(struct xt_option_call * cb)109 static void dccp_parse(struct xt_option_call *cb)
110 {
111 	struct xt_dccp_info *einfo = cb->data;
112 
113 	xtables_option_parse(cb);
114 	switch (cb->entry->id) {
115 	case O_SOURCE_PORT:
116 		einfo->flags |= XT_DCCP_SRC_PORTS;
117 		if (cb->invert)
118 			einfo->invflags |= XT_DCCP_SRC_PORTS;
119 		break;
120 	case O_DEST_PORT:
121 		einfo->flags |= XT_DCCP_DEST_PORTS;
122 		if (cb->invert)
123 			einfo->invflags |= XT_DCCP_DEST_PORTS;
124 		break;
125 	case O_DCCP_TYPES:
126 		einfo->flags |= XT_DCCP_TYPE;
127 		einfo->typemask = parse_dccp_types(cb->arg);
128 		if (cb->invert)
129 			einfo->invflags |= XT_DCCP_TYPE;
130 		break;
131 	case O_DCCP_OPTION:
132 		einfo->flags |= XT_DCCP_OPTION;
133 		if (cb->invert)
134 			einfo->invflags |= XT_DCCP_OPTION;
135 		break;
136 	}
137 }
138 
139 static const char *
port_to_service(int port)140 port_to_service(int port)
141 {
142 	const struct servent *service;
143 
144 	if ((service = getservbyport(htons(port), "dccp")))
145 		return service->s_name;
146 
147 	return NULL;
148 }
149 
150 static void
print_port(uint16_t port,int numeric)151 print_port(uint16_t port, int numeric)
152 {
153 	const char *service;
154 
155 	if (numeric || (service = port_to_service(port)) == NULL)
156 		printf("%u", port);
157 	else
158 		printf("%s", service);
159 }
160 
161 static void
print_ports(const char * name,uint16_t min,uint16_t max,int invert,int numeric)162 print_ports(const char *name, uint16_t min, uint16_t max,
163 	    int invert, int numeric)
164 {
165 	const char *inv = invert ? "!" : "";
166 
167 	if (min != 0 || max != 0xFFFF || invert) {
168 		printf(" %s", name);
169 		if (min == max) {
170 			printf(":%s", inv);
171 			print_port(min, numeric);
172 		} else {
173 			printf("s:%s", inv);
174 			print_port(min, numeric);
175 			printf(":");
176 			print_port(max, numeric);
177 		}
178 	}
179 }
180 
181 static void
print_types(uint16_t types,int inverted,int numeric)182 print_types(uint16_t types, int inverted, int numeric)
183 {
184 	int have_type = 0;
185 
186 	if (inverted)
187 		printf(" !");
188 
189 	printf(" ");
190 	while (types) {
191 		unsigned int i;
192 
193 		for (i = 0; !(types & (1 << i)); i++);
194 
195 		if (have_type)
196 			printf(",");
197 		else
198 			have_type = 1;
199 
200 		if (numeric)
201 			printf("%u", i);
202 		else {
203 			printf("%s", dccp_pkt_types[i]);
204 
205 			if (i == DCCP_PKT_INVALID)
206 				break;
207 		}
208 
209 		types &= ~(1 << i);
210 	}
211 }
212 
213 static void
print_option(uint8_t option,int invert,int numeric)214 print_option(uint8_t option, int invert, int numeric)
215 {
216 	if (option || invert)
217 		printf(" option=%s%u", invert ? "!" : "", option);
218 }
219 
220 static void
dccp_print(const void * ip,const struct xt_entry_match * match,int numeric)221 dccp_print(const void *ip, const struct xt_entry_match *match, int numeric)
222 {
223 	const struct xt_dccp_info *einfo =
224 		(const struct xt_dccp_info *)match->data;
225 
226 	printf(" dccp");
227 
228 	if (einfo->flags & XT_DCCP_SRC_PORTS) {
229 		print_ports("spt", einfo->spts[0], einfo->spts[1],
230 			einfo->invflags & XT_DCCP_SRC_PORTS,
231 			numeric);
232 	}
233 
234 	if (einfo->flags & XT_DCCP_DEST_PORTS) {
235 		print_ports("dpt", einfo->dpts[0], einfo->dpts[1],
236 			einfo->invflags & XT_DCCP_DEST_PORTS,
237 			numeric);
238 	}
239 
240 	if (einfo->flags & XT_DCCP_TYPE) {
241 		print_types(einfo->typemask,
242 			   einfo->invflags & XT_DCCP_TYPE,
243 			   numeric);
244 	}
245 
246 	if (einfo->flags & XT_DCCP_OPTION) {
247 		print_option(einfo->option,
248 			     einfo->invflags & XT_DCCP_OPTION, numeric);
249 	}
250 }
251 
dccp_save(const void * ip,const struct xt_entry_match * match)252 static void dccp_save(const void *ip, const struct xt_entry_match *match)
253 {
254 	const struct xt_dccp_info *einfo =
255 		(const struct xt_dccp_info *)match->data;
256 
257 	if (einfo->flags & XT_DCCP_SRC_PORTS) {
258 		if (einfo->invflags & XT_DCCP_SRC_PORTS)
259 			printf(" !");
260 		if (einfo->spts[0] != einfo->spts[1])
261 			printf(" --sport %u:%u",
262 			       einfo->spts[0], einfo->spts[1]);
263 		else
264 			printf(" --sport %u", einfo->spts[0]);
265 	}
266 
267 	if (einfo->flags & XT_DCCP_DEST_PORTS) {
268 		if (einfo->invflags & XT_DCCP_DEST_PORTS)
269 			printf(" !");
270 		if (einfo->dpts[0] != einfo->dpts[1])
271 			printf(" --dport %u:%u",
272 			       einfo->dpts[0], einfo->dpts[1]);
273 		else
274 			printf(" --dport %u", einfo->dpts[0]);
275 	}
276 
277 	if (einfo->flags & XT_DCCP_TYPE) {
278 		printf("%s --dccp-types",
279 		       einfo->invflags & XT_DCCP_TYPE ? " !" : "");
280 		print_types(einfo->typemask, false, 0);
281 	}
282 
283 	if (einfo->flags & XT_DCCP_OPTION) {
284 		printf("%s --dccp-option %u",
285 			einfo->invflags & XT_DCCP_OPTION ? " !" : "",
286 			einfo->option);
287 	}
288 }
289 
290 static const char *const dccp_pkt_types_xlate[] = {
291 	[DCCP_PKT_REQUEST]      = "request",
292 	[DCCP_PKT_RESPONSE]     = "response",
293 	[DCCP_PKT_DATA]         = "data",
294 	[DCCP_PKT_ACK]          = "ack",
295 	[DCCP_PKT_DATAACK]      = "dataack",
296 	[DCCP_PKT_CLOSEREQ]     = "closereq",
297 	[DCCP_PKT_CLOSE]        = "close",
298 	[DCCP_PKT_RESET]        = "reset",
299 	[DCCP_PKT_SYNC]         = "sync",
300 	[DCCP_PKT_SYNCACK]      = "syncack",
301 	[DCCP_PKT_INVALID]	= "10-15",
302 };
303 
dccp_type_xlate(const struct xt_dccp_info * einfo,struct xt_xlate * xl)304 static int dccp_type_xlate(const struct xt_dccp_info *einfo,
305 			   struct xt_xlate *xl)
306 {
307 	bool have_type = false, set_need = false;
308 	uint16_t types = einfo->typemask;
309 
310 	if (types & INVALID_OTHER_TYPE_MASK) {
311 		types &= ~INVALID_OTHER_TYPE_MASK;
312 		types |= 1 << DCCP_PKT_INVALID;
313 	}
314 
315 	if ((types != 0) && !(types == (types & -types))) {
316 		xt_xlate_add(xl, "{");
317 		set_need = true;
318 	}
319 
320 	while (types) {
321 		unsigned int i;
322 
323 		for (i = 0; !(types & (1 << i)); i++);
324 
325 		if (have_type)
326 			xt_xlate_add(xl, ", ");
327 		else
328 			have_type = true;
329 
330 		xt_xlate_add(xl, "%s", dccp_pkt_types_xlate[i]);
331 
332 		types &= ~(1 << i);
333 	}
334 
335 	if (set_need)
336 		xt_xlate_add(xl, "}");
337 
338 	return 1;
339 }
340 
dccp_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)341 static int dccp_xlate(struct xt_xlate *xl,
342 		      const struct xt_xlate_mt_params *params)
343 {
344 	const struct xt_dccp_info *einfo =
345 		(const struct xt_dccp_info *)params->match->data;
346 	char *space = "";
347 	int ret = 1;
348 
349 	if (einfo->flags & XT_DCCP_SRC_PORTS) {
350 		xt_xlate_add(xl, "dccp sport%s %u",
351 			     einfo->invflags & XT_DCCP_SRC_PORTS ? " !=" : "",
352 			     einfo->spts[0]);
353 
354 		if (einfo->spts[0] != einfo->spts[1])
355 			xt_xlate_add(xl, "-%u", einfo->spts[1]);
356 
357 		space = " ";
358 	}
359 
360 	if (einfo->flags & XT_DCCP_DEST_PORTS) {
361 		xt_xlate_add(xl, "%sdccp dport%s %u", space,
362 			     einfo->invflags & XT_DCCP_DEST_PORTS ? " !=" : "",
363 			     einfo->dpts[0]);
364 
365 		if (einfo->dpts[0] != einfo->dpts[1])
366 			xt_xlate_add(xl, "-%u", einfo->dpts[1]);
367 
368 		space = " ";
369 	}
370 
371 	if (einfo->flags & XT_DCCP_TYPE && einfo->typemask) {
372 		xt_xlate_add(xl, "%sdccp type%s ", space,
373 			     einfo->invflags & XT_DCCP_TYPE ? " !=" : "");
374 		ret = dccp_type_xlate(einfo, xl);
375 
376 		space = " ";
377 	}
378 
379 	/* FIXME: no dccp option support in nftables yet */
380 	if (einfo->flags & XT_DCCP_OPTION)
381 		ret = 0;
382 
383 	return ret;
384 }
385 static struct xtables_match dccp_match = {
386 	.name		= "dccp",
387 	.family		= NFPROTO_UNSPEC,
388 	.version	= XTABLES_VERSION,
389 	.size		= XT_ALIGN(sizeof(struct xt_dccp_info)),
390 	.userspacesize	= XT_ALIGN(sizeof(struct xt_dccp_info)),
391 	.help		= dccp_help,
392 	.print		= dccp_print,
393 	.save		= dccp_save,
394 	.x6_parse	= dccp_parse,
395 	.x6_options	= dccp_opts,
396 	.xlate		= dccp_xlate,
397 };
398 
_init(void)399 void _init(void)
400 {
401 	xtables_register_match(&dccp_match);
402 }
403