• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Shared library add-on to iptables to add TCP support. */
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <netdb.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <getopt.h>
8 #include <netinet/in.h>
9 #include <xtables.h>
10 #include <linux/netfilter/xt_tcpudp.h>
11 
tcp_help(void)12 static void tcp_help(void)
13 {
14 	printf(
15 "tcp match options:\n"
16 "[!] --tcp-flags mask comp	match when TCP flags & mask == comp\n"
17 "				(Flags: SYN ACK FIN RST URG PSH ALL NONE)\n"
18 "[!] --syn			match when only SYN flag set\n"
19 "				(equivalent to --tcp-flags SYN,RST,ACK,FIN SYN)\n"
20 "[!] --source-port port[:port]\n"
21 " --sport ...\n"
22 "				match source port(s)\n"
23 "[!] --destination-port port[:port]\n"
24 " --dport ...\n"
25 "				match destination port(s)\n"
26 "[!] --tcp-option number        match if TCP option set\n");
27 }
28 
29 static const struct option tcp_opts[] = {
30 	{.name = "source-port",      .has_arg = true,  .val = '1'},
31 	{.name = "sport",            .has_arg = true,  .val = '1'}, /* synonym */
32 	{.name = "destination-port", .has_arg = true,  .val = '2'},
33 	{.name = "dport",            .has_arg = true,  .val = '2'}, /* synonym */
34 	{.name = "syn",              .has_arg = false, .val = '3'},
35 	{.name = "tcp-flags",        .has_arg = true,  .val = '4'},
36 	{.name = "tcp-option",       .has_arg = true,  .val = '5'},
37 	XT_GETOPT_TABLEEND,
38 };
39 
40 static void
parse_tcp_ports(const char * portstring,uint16_t * ports)41 parse_tcp_ports(const char *portstring, uint16_t *ports)
42 {
43 	char *buffer;
44 	char *cp;
45 
46 	buffer = xtables_strdup(portstring);
47 	if ((cp = strchr(buffer, ':')) == NULL)
48 		ports[0] = ports[1] = xtables_parse_port(buffer, "tcp");
49 	else {
50 		*cp = '\0';
51 		cp++;
52 
53 		ports[0] = buffer[0] ? xtables_parse_port(buffer, "tcp") : 0;
54 		ports[1] = cp[0] ? xtables_parse_port(cp, "tcp") : 0xFFFF;
55 
56 		if (ports[0] > ports[1])
57 			xtables_error(PARAMETER_PROBLEM,
58 				   "invalid portrange (min > max)");
59 	}
60 	free(buffer);
61 }
62 
63 struct tcp_flag_names {
64 	const char *name;
65 	unsigned int flag;
66 };
67 
68 static const struct tcp_flag_names tcp_flag_names[]
69 = { { "FIN", 0x01 },
70     { "SYN", 0x02 },
71     { "RST", 0x04 },
72     { "PSH", 0x08 },
73     { "ACK", 0x10 },
74     { "URG", 0x20 },
75     { "ALL", 0x3F },
76     { "NONE", 0 },
77 };
78 
79 static unsigned int
parse_tcp_flag(const char * flags)80 parse_tcp_flag(const char *flags)
81 {
82 	unsigned int ret = 0;
83 	char *ptr;
84 	char *buffer;
85 
86 	buffer = xtables_strdup(flags);
87 
88 	for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
89 		unsigned int i;
90 		for (i = 0; i < ARRAY_SIZE(tcp_flag_names); ++i)
91 			if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) {
92 				ret |= tcp_flag_names[i].flag;
93 				break;
94 			}
95 		if (i == ARRAY_SIZE(tcp_flag_names))
96 			xtables_error(PARAMETER_PROBLEM,
97 				   "Unknown TCP flag `%s'", ptr);
98 	}
99 
100 	free(buffer);
101 	return ret;
102 }
103 
104 static void
parse_tcp_flags(struct xt_tcp * tcpinfo,const char * mask,const char * cmp,int invert)105 parse_tcp_flags(struct xt_tcp *tcpinfo,
106 		const char *mask,
107 		const char *cmp,
108 		int invert)
109 {
110 	tcpinfo->flg_mask = parse_tcp_flag(mask);
111 	tcpinfo->flg_cmp = parse_tcp_flag(cmp);
112 
113 	if (invert)
114 		tcpinfo->invflags |= XT_TCP_INV_FLAGS;
115 }
116 
117 static void
parse_tcp_option(const char * option,uint8_t * result)118 parse_tcp_option(const char *option, uint8_t *result)
119 {
120 	unsigned int ret;
121 
122 	if (!xtables_strtoui(option, NULL, &ret, 1, UINT8_MAX))
123 		xtables_error(PARAMETER_PROBLEM, "Bad TCP option \"%s\"", option);
124 
125 	*result = ret;
126 }
127 
tcp_init(struct xt_entry_match * m)128 static void tcp_init(struct xt_entry_match *m)
129 {
130 	struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data;
131 
132 	tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
133 }
134 
135 #define TCP_SRC_PORTS 0x01
136 #define TCP_DST_PORTS 0x02
137 #define TCP_FLAGS 0x04
138 #define TCP_OPTION	0x08
139 
140 static int
tcp_parse(int c,char ** argv,int invert,unsigned int * flags,const void * entry,struct xt_entry_match ** match)141 tcp_parse(int c, char **argv, int invert, unsigned int *flags,
142           const void *entry, struct xt_entry_match **match)
143 {
144 	struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data;
145 
146 	switch (c) {
147 	case '1':
148 		if (*flags & TCP_SRC_PORTS)
149 			xtables_error(PARAMETER_PROBLEM,
150 				   "Only one `--source-port' allowed");
151 		parse_tcp_ports(optarg, tcpinfo->spts);
152 		if (invert)
153 			tcpinfo->invflags |= XT_TCP_INV_SRCPT;
154 		*flags |= TCP_SRC_PORTS;
155 		break;
156 
157 	case '2':
158 		if (*flags & TCP_DST_PORTS)
159 			xtables_error(PARAMETER_PROBLEM,
160 				   "Only one `--destination-port' allowed");
161 		parse_tcp_ports(optarg, tcpinfo->dpts);
162 		if (invert)
163 			tcpinfo->invflags |= XT_TCP_INV_DSTPT;
164 		*flags |= TCP_DST_PORTS;
165 		break;
166 
167 	case '3':
168 		if (*flags & TCP_FLAGS)
169 			xtables_error(PARAMETER_PROBLEM,
170 				   "Only one of `--syn' or `--tcp-flags' "
171 				   " allowed");
172 		parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
173 		*flags |= TCP_FLAGS;
174 		break;
175 
176 	case '4':
177 		if (*flags & TCP_FLAGS)
178 			xtables_error(PARAMETER_PROBLEM,
179 				   "Only one of `--syn' or `--tcp-flags' "
180 				   " allowed");
181 		if (!argv[optind]
182 		    || argv[optind][0] == '-' || argv[optind][0] == '!')
183 			xtables_error(PARAMETER_PROBLEM,
184 				   "--tcp-flags requires two args.");
185 
186 		parse_tcp_flags(tcpinfo, optarg, argv[optind],
187 				invert);
188 		optind++;
189 		*flags |= TCP_FLAGS;
190 		break;
191 
192 	case '5':
193 		if (*flags & TCP_OPTION)
194 			xtables_error(PARAMETER_PROBLEM,
195 				   "Only one `--tcp-option' allowed");
196 		parse_tcp_option(optarg, &tcpinfo->option);
197 		if (invert)
198 			tcpinfo->invflags |= XT_TCP_INV_OPTION;
199 		*flags |= TCP_OPTION;
200 		break;
201 	}
202 
203 	return 1;
204 }
205 
206 static const char *
port_to_service(int port)207 port_to_service(int port)
208 {
209 	const struct servent *service;
210 
211 	if ((service = getservbyport(htons(port), "tcp")))
212 		return service->s_name;
213 
214 	return NULL;
215 }
216 
217 static void
print_port(uint16_t port,int numeric)218 print_port(uint16_t port, int numeric)
219 {
220 	const char *service;
221 
222 	if (numeric || (service = port_to_service(port)) == NULL)
223 		printf("%u", port);
224 	else
225 		printf("%s", service);
226 }
227 
skip_ports_match(uint16_t min,uint16_t max,bool inv)228 static bool skip_ports_match(uint16_t min, uint16_t max, bool inv)
229 {
230 	return min == 0 && max == UINT16_MAX && !inv;
231 }
232 
233 static void
print_ports(const char * name,uint16_t min,uint16_t max,int invert,int numeric)234 print_ports(const char *name, uint16_t min, uint16_t max,
235 	    int invert, int numeric)
236 {
237 	const char *inv = invert ? "!" : "";
238 
239 	if (!skip_ports_match(min, max, invert)) {
240 		printf(" %s", name);
241 		if (min == max) {
242 			printf(":%s", inv);
243 			print_port(min, numeric);
244 		} else {
245 			printf("s:%s", inv);
246 			print_port(min, numeric);
247 			printf(":");
248 			print_port(max, numeric);
249 		}
250 	}
251 }
252 
253 static void
print_option(uint8_t option,int invert,int numeric)254 print_option(uint8_t option, int invert, int numeric)
255 {
256 	if (option || invert)
257 		printf(" option=%s%u", invert ? "!" : "", option);
258 }
259 
260 static void
print_tcpf(uint8_t flags)261 print_tcpf(uint8_t flags)
262 {
263 	int have_flag = 0;
264 
265 	while (flags) {
266 		unsigned int i;
267 
268 		for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
269 
270 		if (have_flag)
271 			printf(",");
272 		printf("%s", tcp_flag_names[i].name);
273 		have_flag = 1;
274 
275 		flags &= ~tcp_flag_names[i].flag;
276 	}
277 
278 	if (!have_flag)
279 		printf("NONE");
280 }
281 
282 static void
print_flags(uint8_t mask,uint8_t cmp,int invert,int numeric)283 print_flags(uint8_t mask, uint8_t cmp, int invert, int numeric)
284 {
285 	if (mask || invert) {
286 		printf(" flags:%s", invert ? "!" : "");
287 		if (numeric)
288 			printf("0x%02X/0x%02X", mask, cmp);
289 		else {
290 			print_tcpf(mask);
291 			printf("/");
292 			print_tcpf(cmp);
293 		}
294 	}
295 }
296 
297 static void
tcp_print(const void * ip,const struct xt_entry_match * match,int numeric)298 tcp_print(const void *ip, const struct xt_entry_match *match, int numeric)
299 {
300 	const struct xt_tcp *tcp = (struct xt_tcp *)match->data;
301 
302 	printf(" tcp");
303 	print_ports("spt", tcp->spts[0], tcp->spts[1],
304 		    tcp->invflags & XT_TCP_INV_SRCPT,
305 		    numeric);
306 	print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
307 		    tcp->invflags & XT_TCP_INV_DSTPT,
308 		    numeric);
309 	print_option(tcp->option,
310 		     tcp->invflags & XT_TCP_INV_OPTION,
311 		     numeric);
312 	print_flags(tcp->flg_mask, tcp->flg_cmp,
313 		    tcp->invflags & XT_TCP_INV_FLAGS,
314 		    numeric);
315 	if (tcp->invflags & ~XT_TCP_INV_MASK)
316 		printf(" Unknown invflags: 0x%X",
317 		       tcp->invflags & ~XT_TCP_INV_MASK);
318 }
319 
tcp_save(const void * ip,const struct xt_entry_match * match)320 static void tcp_save(const void *ip, const struct xt_entry_match *match)
321 {
322 	const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data;
323 	bool inv_srcpt = tcpinfo->invflags & XT_TCP_INV_SRCPT;
324 	bool inv_dstpt = tcpinfo->invflags & XT_TCP_INV_DSTPT;
325 
326 	if (!skip_ports_match(tcpinfo->spts[0], tcpinfo->spts[1], inv_srcpt)) {
327 		if (inv_srcpt)
328 			printf(" !");
329 		if (tcpinfo->spts[0]
330 		    != tcpinfo->spts[1])
331 			printf(" --sport %u:%u",
332 			       tcpinfo->spts[0],
333 			       tcpinfo->spts[1]);
334 		else
335 			printf(" --sport %u",
336 			       tcpinfo->spts[0]);
337 	}
338 
339 	if (!skip_ports_match(tcpinfo->dpts[0], tcpinfo->dpts[1], inv_dstpt)) {
340 		if (inv_dstpt)
341 			printf(" !");
342 		if (tcpinfo->dpts[0]
343 		    != tcpinfo->dpts[1])
344 			printf(" --dport %u:%u",
345 			       tcpinfo->dpts[0],
346 			       tcpinfo->dpts[1]);
347 		else
348 			printf(" --dport %u",
349 			       tcpinfo->dpts[0]);
350 	}
351 
352 	if (tcpinfo->option
353 	    || (tcpinfo->invflags & XT_TCP_INV_OPTION)) {
354 		if (tcpinfo->invflags & XT_TCP_INV_OPTION)
355 			printf(" !");
356 		printf(" --tcp-option %u", tcpinfo->option);
357 	}
358 
359 	if (tcpinfo->flg_mask
360 	    || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) {
361 		if (tcpinfo->invflags & XT_TCP_INV_FLAGS)
362 			printf(" !");
363 		printf(" --tcp-flags ");
364 		print_tcpf(tcpinfo->flg_mask);
365 		printf(" ");
366 		print_tcpf(tcpinfo->flg_cmp);
367 	}
368 }
369 
370 static const struct tcp_flag_names tcp_flag_names_xlate[] = {
371 	{ "fin", 0x01 },
372 	{ "syn", 0x02 },
373 	{ "rst", 0x04 },
374 	{ "psh", 0x08 },
375 	{ "ack", 0x10 },
376 	{ "urg", 0x20 },
377 };
378 
print_tcp_xlate(struct xt_xlate * xl,uint8_t flags)379 static void print_tcp_xlate(struct xt_xlate *xl, uint8_t flags)
380 {
381 	int have_flag = 0;
382 
383 	while (flags) {
384 		unsigned int i;
385 
386 		for (i = 0; (flags & tcp_flag_names_xlate[i].flag) == 0; i++);
387 
388 		xt_xlate_add(xl, "%s%s",
389 			     have_flag ? "," : "",
390 			     tcp_flag_names_xlate[i].name);
391 		have_flag = 1;
392 
393 		flags &= ~tcp_flag_names_xlate[i].flag;
394 	}
395 
396 	if (!have_flag)
397 		xt_xlate_add(xl, "0x0");
398 }
399 
tcp_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)400 static int tcp_xlate(struct xt_xlate *xl,
401 		     const struct xt_xlate_mt_params *params)
402 {
403 	const struct xt_tcp *tcpinfo =
404 		(const struct xt_tcp *)params->match->data;
405 	bool inv_srcpt = tcpinfo->invflags & XT_TCP_INV_SRCPT;
406 	bool inv_dstpt = tcpinfo->invflags & XT_TCP_INV_DSTPT;
407 	bool xlated = false;
408 
409 	if (!skip_ports_match(tcpinfo->spts[0], tcpinfo->spts[1], inv_srcpt)) {
410 		if (tcpinfo->spts[0] != tcpinfo->spts[1]) {
411 			xt_xlate_add(xl, "tcp sport %s%u-%u",
412 				   inv_srcpt ? "!= " : "",
413 				   tcpinfo->spts[0], tcpinfo->spts[1]);
414 		} else {
415 			xt_xlate_add(xl, "tcp sport %s%u",
416 				   inv_srcpt ? "!= " : "",
417 				   tcpinfo->spts[0]);
418 		}
419 		xlated = true;
420 	}
421 
422 	if (!skip_ports_match(tcpinfo->dpts[0], tcpinfo->dpts[1], inv_dstpt)) {
423 		if (tcpinfo->dpts[0] != tcpinfo->dpts[1]) {
424 			xt_xlate_add(xl, "tcp dport %s%u-%u",
425 				   inv_dstpt ? "!= " : "",
426 				   tcpinfo->dpts[0], tcpinfo->dpts[1]);
427 		} else {
428 			xt_xlate_add(xl, "tcp dport %s%u",
429 				   inv_dstpt ? "!= " : "",
430 				   tcpinfo->dpts[0]);
431 		}
432 		xlated = true;
433 	}
434 
435 	if (tcpinfo->option) {
436 		xt_xlate_add(xl, "tcp option %u %s", tcpinfo->option,
437 			     tcpinfo->invflags & XT_TCP_INV_OPTION ?
438 			     "missing" : "exists");
439 		xlated = true;
440 	}
441 
442 	if (tcpinfo->flg_mask || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) {
443 		xt_xlate_add(xl, "tcp flags %s",
444 			     tcpinfo->invflags & XT_TCP_INV_FLAGS ? "!= ": "");
445 		print_tcp_xlate(xl, tcpinfo->flg_cmp);
446 		xt_xlate_add(xl, " / ");
447 		print_tcp_xlate(xl, tcpinfo->flg_mask);
448 		xlated = true;
449 	}
450 
451 	if (!xlated)
452 		xt_xlate_add(xl, "meta l4proto tcp");
453 
454 	return 1;
455 }
456 
457 static struct xtables_match tcp_match = {
458 	.family		= NFPROTO_UNSPEC,
459 	.name		= "tcp",
460 	.version	= XTABLES_VERSION,
461 	.size		= XT_ALIGN(sizeof(struct xt_tcp)),
462 	.userspacesize	= XT_ALIGN(sizeof(struct xt_tcp)),
463 	.help		= tcp_help,
464 	.init		= tcp_init,
465 	.parse		= tcp_parse,
466 	.print		= tcp_print,
467 	.save		= tcp_save,
468 	.extra_opts	= tcp_opts,
469 	.xlate		= tcp_xlate,
470 };
471 
472 void
_init(void)473 _init(void)
474 {
475 	xtables_register_match(&tcp_match);
476 }
477