blob: 743c5a23645a8bd40b13d0f0ebc2d8e8ccaa4a74 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Shared library add-on to iptables to add TCP support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +00007#include <xtables.h>
8#include <linux/netfilter/xt_tcpudp.h>
Marc Bouchere6869a82000-03-20 06:03:29 +00009
10/* Function which prints out usage message. */
Jan Engelhardt181dead2007-10-04 16:27:07 +000011static void tcp_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000012{
13 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020014"tcp match options:\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000015" --tcp-flags [!] mask comp match when TCP flags & mask == comp\n"
16" (Flags: SYN ACK FIN RST URG PSH ALL NONE)\n"
17"[!] --syn match when only SYN flag set\n"
Patrick McHardyYasuyuki KOZAKAIa8a4f5d2007-07-16 15:27:38 +000018" (equivalent to --tcp-flags SYN,RST,ACK,FIN SYN)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000019" --source-port [!] port[:port]\n"
20" --sport ...\n"
21" match source port(s)\n"
22" --destination-port [!] port[:port]\n"
23" --dport ...\n"
24" match destination port(s)\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020025" --tcp-option [!] number match if TCP option set\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000026}
27
Jan Engelhardt181dead2007-10-04 16:27:07 +000028static const struct option tcp_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000029 { "source-port", 1, NULL, '1' },
30 { "sport", 1, NULL, '1' }, /* synonym */
31 { "destination-port", 1, NULL, '2' },
32 { "dport", 1, NULL, '2' }, /* synonym */
33 { "syn", 0, NULL, '3' },
34 { "tcp-flags", 1, NULL, '4' },
35 { "tcp-option", 1, NULL, '5' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000036 { .name = NULL }
Marc Bouchere6869a82000-03-20 06:03:29 +000037};
38
Marc Bouchere6869a82000-03-20 06:03:29 +000039static void
40parse_tcp_ports(const char *portstring, u_int16_t *ports)
41{
42 char *buffer;
43 char *cp;
44
45 buffer = strdup(portstring);
46 if ((cp = strchr(buffer, ':')) == NULL)
Phil Oesterdbac8ad2006-07-20 17:01:54 +000047 ports[0] = ports[1] = parse_port(buffer, "tcp");
Marc Bouchere6869a82000-03-20 06:03:29 +000048 else {
49 *cp = '\0';
50 cp++;
51
Phil Oesterdbac8ad2006-07-20 17:01:54 +000052 ports[0] = buffer[0] ? parse_port(buffer, "tcp") : 0;
53 ports[1] = cp[0] ? parse_port(cp, "tcp") : 0xFFFF;
Harald Welted15fb342002-07-26 16:27:57 +000054
55 if (ports[0] > ports[1])
56 exit_error(PARAMETER_PROBLEM,
57 "invalid portrange (min > max)");
Marc Bouchere6869a82000-03-20 06:03:29 +000058 }
59 free(buffer);
60}
61
62struct tcp_flag_names {
63 const char *name;
64 unsigned int flag;
65};
66
Jan Engelhardt0e2abed2007-10-04 16:25:58 +000067static const struct tcp_flag_names tcp_flag_names[]
Marc Bouchere6869a82000-03-20 06:03:29 +000068= { { "FIN", 0x01 },
69 { "SYN", 0x02 },
70 { "RST", 0x04 },
71 { "PSH", 0x08 },
72 { "ACK", 0x10 },
73 { "URG", 0x20 },
74 { "ALL", 0x3F },
75 { "NONE", 0 },
76};
77
78static unsigned int
79parse_tcp_flag(const char *flags)
80{
81 unsigned int ret = 0;
82 char *ptr;
83 char *buffer;
84
85 buffer = strdup(flags);
86
87 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
88 unsigned int i;
89 for (i = 0;
90 i < sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names);
91 i++) {
92 if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) {
93 ret |= tcp_flag_names[i].flag;
94 break;
95 }
96 }
97 if (i == sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names))
98 exit_error(PARAMETER_PROBLEM,
Harald Welte2aa78fe2003-03-30 18:29:56 +000099 "Unknown TCP flag `%s'", ptr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000100 }
101
102 free(buffer);
103 return ret;
104}
105
106static void
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000107parse_tcp_flags(struct xt_tcp *tcpinfo,
Marc Bouchere6869a82000-03-20 06:03:29 +0000108 const char *mask,
109 const char *cmp,
110 int invert)
111{
112 tcpinfo->flg_mask = parse_tcp_flag(mask);
113 tcpinfo->flg_cmp = parse_tcp_flag(cmp);
114
115 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000116 tcpinfo->invflags |= XT_TCP_INV_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000117}
118
119static void
120parse_tcp_option(const char *option, u_int8_t *result)
121{
Harald Welteb4719762001-07-23 02:14:22 +0000122 unsigned int ret;
Marc Bouchere6869a82000-03-20 06:03:29 +0000123
Harald Welteb4719762001-07-23 02:14:22 +0000124 if (string_to_number(option, 1, 255, &ret) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000125 exit_error(PARAMETER_PROBLEM, "Bad TCP option `%s'", option);
126
127 *result = (u_int8_t)ret;
128}
129
130/* Initialize the match. */
Jan Engelhardt181dead2007-10-04 16:27:07 +0000131static void tcp_init(struct xt_entry_match *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000132{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000133 struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000134
135 tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
136}
137
138#define TCP_SRC_PORTS 0x01
139#define TCP_DST_PORTS 0x02
140#define TCP_FLAGS 0x04
141#define TCP_OPTION 0x08
142
143/* Function which parses command options; returns true if it
144 ate an option. */
145static int
Jan Engelhardt181dead2007-10-04 16:27:07 +0000146tcp_parse(int c, char **argv, int invert, unsigned int *flags,
147 const void *entry, struct xt_entry_match **match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000148{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000149 struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000150
151 switch (c) {
152 case '1':
153 if (*flags & TCP_SRC_PORTS)
154 exit_error(PARAMETER_PROBLEM,
155 "Only one `--source-port' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000156 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000157 parse_tcp_ports(argv[optind-1], tcpinfo->spts);
158 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000159 tcpinfo->invflags |= XT_TCP_INV_SRCPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000160 *flags |= TCP_SRC_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000161 break;
162
163 case '2':
164 if (*flags & TCP_DST_PORTS)
165 exit_error(PARAMETER_PROBLEM,
166 "Only one `--destination-port' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000167 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000168 parse_tcp_ports(argv[optind-1], tcpinfo->dpts);
169 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000170 tcpinfo->invflags |= XT_TCP_INV_DSTPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000171 *flags |= TCP_DST_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000172 break;
173
174 case '3':
175 if (*flags & TCP_FLAGS)
176 exit_error(PARAMETER_PROBLEM,
177 "Only one of `--syn' or `--tcp-flags' "
178 " allowed");
Harald Welte38ed4212005-05-04 07:34:37 +0000179 parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
Marc Bouchere6869a82000-03-20 06:03:29 +0000180 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000181 break;
182
183 case '4':
184 if (*flags & TCP_FLAGS)
185 exit_error(PARAMETER_PROBLEM,
186 "Only one of `--syn' or `--tcp-flags' "
187 " allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000188 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000189
190 if (!argv[optind]
191 || argv[optind][0] == '-' || argv[optind][0] == '!')
192 exit_error(PARAMETER_PROBLEM,
193 "--tcp-flags requires two args.");
194
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000195 parse_tcp_flags(tcpinfo, argv[optind-1], argv[optind],
Rusty Russell78001fe2000-09-01 05:22:10 +0000196 invert);
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000197 optind++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000198 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000199 break;
200
201 case '5':
202 if (*flags & TCP_OPTION)
203 exit_error(PARAMETER_PROBLEM,
204 "Only one `--tcp-option' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000205 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000206 parse_tcp_option(argv[optind-1], &tcpinfo->option);
207 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000208 tcpinfo->invflags |= XT_TCP_INV_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000209 *flags |= TCP_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000210 break;
211
212 default:
213 return 0;
214 }
215
216 return 1;
217}
218
Marc Bouchere6869a82000-03-20 06:03:29 +0000219static char *
220port_to_service(int port)
221{
222 struct servent *service;
223
224 if ((service = getservbyport(htons(port), "tcp")))
225 return service->s_name;
226
227 return NULL;
228}
229
230static void
231print_port(u_int16_t port, int numeric)
232{
233 char *service;
234
235 if (numeric || (service = port_to_service(port)) == NULL)
236 printf("%u", port);
237 else
238 printf("%s", service);
239}
240
241static void
242print_ports(const char *name, u_int16_t min, u_int16_t max,
243 int invert, int numeric)
244{
245 const char *inv = invert ? "!" : "";
246
247 if (min != 0 || max != 0xFFFF || invert) {
248 printf("%s", name);
249 if (min == max) {
250 printf(":%s", inv);
251 print_port(min, numeric);
252 } else {
253 printf("s:%s", inv);
254 print_port(min, numeric);
255 printf(":");
256 print_port(max, numeric);
257 }
258 printf(" ");
259 }
260}
261
262static void
263print_option(u_int8_t option, int invert, int numeric)
264{
265 if (option || invert)
266 printf("option=%s%u ", invert ? "!" : "", option);
267}
268
269static void
270print_tcpf(u_int8_t flags)
271{
Rusty Russelledf14cf2000-04-19 11:26:44 +0000272 int have_flag = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000273
Rusty Russelledf14cf2000-04-19 11:26:44 +0000274 while (flags) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000275 unsigned int i;
276
Rusty Russelledf14cf2000-04-19 11:26:44 +0000277 for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
Marc Bouchere6869a82000-03-20 06:03:29 +0000278
Rusty Russelledf14cf2000-04-19 11:26:44 +0000279 if (have_flag)
Marc Bouchere6869a82000-03-20 06:03:29 +0000280 printf(",");
281 printf("%s", tcp_flag_names[i].name);
Rusty Russelledf14cf2000-04-19 11:26:44 +0000282 have_flag = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000283
284 flags &= ~tcp_flag_names[i].flag;
Rusty Russelledf14cf2000-04-19 11:26:44 +0000285 }
286
287 if (!have_flag)
288 printf("NONE");
Marc Bouchere6869a82000-03-20 06:03:29 +0000289}
290
291static void
292print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
293{
294 if (mask || invert) {
295 printf("flags:%s", invert ? "!" : "");
296 if (numeric)
Harald Welte0b4efea2001-04-12 15:50:49 +0000297 printf("0x%02X/0x%02X ", mask, cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000298 else {
Marc Bouchere6869a82000-03-20 06:03:29 +0000299 print_tcpf(mask);
Rusty Russell31728072000-09-01 06:29:06 +0000300 printf("/");
301 print_tcpf(cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000302 printf(" ");
303 }
304 }
305}
306
307/* Prints out the union ipt_matchinfo. */
308static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000309tcp_print(const void *ip, const struct xt_entry_match *match, int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000310{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000311 const struct xt_tcp *tcp = (struct xt_tcp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000312
313 printf("tcp ");
314 print_ports("spt", tcp->spts[0], tcp->spts[1],
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000315 tcp->invflags & XT_TCP_INV_SRCPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000316 numeric);
317 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000318 tcp->invflags & XT_TCP_INV_DSTPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000319 numeric);
320 print_option(tcp->option,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000321 tcp->invflags & XT_TCP_INV_OPTION,
Marc Bouchere6869a82000-03-20 06:03:29 +0000322 numeric);
323 print_flags(tcp->flg_mask, tcp->flg_cmp,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000324 tcp->invflags & XT_TCP_INV_FLAGS,
Marc Bouchere6869a82000-03-20 06:03:29 +0000325 numeric);
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000326 if (tcp->invflags & ~XT_TCP_INV_MASK)
Marc Bouchere6869a82000-03-20 06:03:29 +0000327 printf("Unknown invflags: 0x%X ",
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000328 tcp->invflags & ~XT_TCP_INV_MASK);
Marc Bouchere6869a82000-03-20 06:03:29 +0000329}
330
331/* Saves the union ipt_matchinfo in parsable form to stdout. */
Jan Engelhardt181dead2007-10-04 16:27:07 +0000332static void tcp_save(const void *ip, const struct xt_entry_match *match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000333{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000334 const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000335
336 if (tcpinfo->spts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000337 || tcpinfo->spts[1] != 0xFFFF) {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000338 if (tcpinfo->invflags & XT_TCP_INV_SRCPT)
Marc Bouchere6869a82000-03-20 06:03:29 +0000339 printf("! ");
340 if (tcpinfo->spts[0]
341 != tcpinfo->spts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000342 printf("--sport %u:%u ",
343 tcpinfo->spts[0],
344 tcpinfo->spts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000345 else
346 printf("--sport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000347 tcpinfo->spts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000348 }
349
350 if (tcpinfo->dpts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000351 || tcpinfo->dpts[1] != 0xFFFF) {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000352 if (tcpinfo->invflags & XT_TCP_INV_DSTPT)
Marc Bouchere6869a82000-03-20 06:03:29 +0000353 printf("! ");
354 if (tcpinfo->dpts[0]
355 != tcpinfo->dpts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000356 printf("--dport %u:%u ",
357 tcpinfo->dpts[0],
358 tcpinfo->dpts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000359 else
360 printf("--dport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000361 tcpinfo->dpts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000362 }
363
364 if (tcpinfo->option
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000365 || (tcpinfo->invflags & XT_TCP_INV_OPTION)) {
366 if (tcpinfo->invflags & XT_TCP_INV_OPTION)
Marc Bouchere6869a82000-03-20 06:03:29 +0000367 printf("! ");
368 printf("--tcp-option %u ", tcpinfo->option);
369 }
370
371 if (tcpinfo->flg_mask
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000372 || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) {
373 if (tcpinfo->invflags & XT_TCP_INV_FLAGS)
Marc Bouchere6869a82000-03-20 06:03:29 +0000374 printf("! ");
Harald Welte67f23b22000-11-05 17:53:06 +0000375 printf("--tcp-flags ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000376 if (tcpinfo->flg_mask != 0xFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000377 print_tcpf(tcpinfo->flg_mask);
378 }
Harald Welte67f23b22000-11-05 17:53:06 +0000379 printf(" ");
380 print_tcpf(tcpinfo->flg_cmp);
381 printf(" ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000382 }
383}
384
Jan Engelhardt181dead2007-10-04 16:27:07 +0000385static struct xtables_match tcp_match = {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000386 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000387 .name = "tcp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200388 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000389 .size = XT_ALIGN(sizeof(struct xt_tcp)),
390 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000391 .help = tcp_help,
392 .init = tcp_init,
393 .parse = tcp_parse,
394 .print = tcp_print,
395 .save = tcp_save,
396 .extra_opts = tcp_opts,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000397};
398
Jan Engelhardt181dead2007-10-04 16:27:07 +0000399static struct xtables_match tcp_match6 = {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000400 .family = AF_INET6,
401 .name = "tcp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200402 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000403 .size = XT_ALIGN(sizeof(struct xt_tcp)),
404 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000405 .help = tcp_help,
406 .init = tcp_init,
407 .parse = tcp_parse,
408 .print = tcp_print,
409 .save = tcp_save,
410 .extra_opts = tcp_opts,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000411};
Marc Bouchere6869a82000-03-20 06:03:29 +0000412
413void
414_init(void)
415{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000416 xtables_register_match(&tcp_match);
417 xtables_register_match(&tcp_match6);
Marc Bouchere6869a82000-03-20 06:03:29 +0000418}