blob: b1f12a47a0c44b9c2f9ca4ef1ffb2aac9daa7741 [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(
14"TCP v%s options:\n"
15" --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"
25" --tcp-option [!] number match if TCP option set\n\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000026IPTABLES_VERSION);
Marc Bouchere6869a82000-03-20 06:03:29 +000027}
28
Jan Engelhardt181dead2007-10-04 16:27:07 +000029static const struct option tcp_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000030 { "source-port", 1, NULL, '1' },
31 { "sport", 1, NULL, '1' }, /* synonym */
32 { "destination-port", 1, NULL, '2' },
33 { "dport", 1, NULL, '2' }, /* synonym */
34 { "syn", 0, NULL, '3' },
35 { "tcp-flags", 1, NULL, '4' },
36 { "tcp-option", 1, NULL, '5' },
37 { }
Marc Bouchere6869a82000-03-20 06:03:29 +000038};
39
Marc Bouchere6869a82000-03-20 06:03:29 +000040static void
41parse_tcp_ports(const char *portstring, u_int16_t *ports)
42{
43 char *buffer;
44 char *cp;
45
46 buffer = strdup(portstring);
47 if ((cp = strchr(buffer, ':')) == NULL)
Phil Oesterdbac8ad2006-07-20 17:01:54 +000048 ports[0] = ports[1] = parse_port(buffer, "tcp");
Marc Bouchere6869a82000-03-20 06:03:29 +000049 else {
50 *cp = '\0';
51 cp++;
52
Phil Oesterdbac8ad2006-07-20 17:01:54 +000053 ports[0] = buffer[0] ? parse_port(buffer, "tcp") : 0;
54 ports[1] = cp[0] ? parse_port(cp, "tcp") : 0xFFFF;
Harald Welted15fb342002-07-26 16:27:57 +000055
56 if (ports[0] > ports[1])
57 exit_error(PARAMETER_PROBLEM,
58 "invalid portrange (min > max)");
Marc Bouchere6869a82000-03-20 06:03:29 +000059 }
60 free(buffer);
61}
62
63struct tcp_flag_names {
64 const char *name;
65 unsigned int flag;
66};
67
Jan Engelhardt0e2abed2007-10-04 16:25:58 +000068static const struct tcp_flag_names tcp_flag_names[]
Marc Bouchere6869a82000-03-20 06:03:29 +000069= { { "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
79static unsigned int
80parse_tcp_flag(const char *flags)
81{
82 unsigned int ret = 0;
83 char *ptr;
84 char *buffer;
85
86 buffer = strdup(flags);
87
88 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
89 unsigned int i;
90 for (i = 0;
91 i < sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names);
92 i++) {
93 if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) {
94 ret |= tcp_flag_names[i].flag;
95 break;
96 }
97 }
98 if (i == sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names))
99 exit_error(PARAMETER_PROBLEM,
Harald Welte2aa78fe2003-03-30 18:29:56 +0000100 "Unknown TCP flag `%s'", ptr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000101 }
102
103 free(buffer);
104 return ret;
105}
106
107static void
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000108parse_tcp_flags(struct xt_tcp *tcpinfo,
Marc Bouchere6869a82000-03-20 06:03:29 +0000109 const char *mask,
110 const char *cmp,
111 int invert)
112{
113 tcpinfo->flg_mask = parse_tcp_flag(mask);
114 tcpinfo->flg_cmp = parse_tcp_flag(cmp);
115
116 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000117 tcpinfo->invflags |= XT_TCP_INV_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000118}
119
120static void
121parse_tcp_option(const char *option, u_int8_t *result)
122{
Harald Welteb4719762001-07-23 02:14:22 +0000123 unsigned int ret;
Marc Bouchere6869a82000-03-20 06:03:29 +0000124
Harald Welteb4719762001-07-23 02:14:22 +0000125 if (string_to_number(option, 1, 255, &ret) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000126 exit_error(PARAMETER_PROBLEM, "Bad TCP option `%s'", option);
127
128 *result = (u_int8_t)ret;
129}
130
131/* Initialize the match. */
Jan Engelhardt181dead2007-10-04 16:27:07 +0000132static void tcp_init(struct xt_entry_match *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000133{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000134 struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000135
136 tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
137}
138
139#define TCP_SRC_PORTS 0x01
140#define TCP_DST_PORTS 0x02
141#define TCP_FLAGS 0x04
142#define TCP_OPTION 0x08
143
144/* Function which parses command options; returns true if it
145 ate an option. */
146static int
Jan Engelhardt181dead2007-10-04 16:27:07 +0000147tcp_parse(int c, char **argv, int invert, unsigned int *flags,
148 const void *entry, struct xt_entry_match **match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000149{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000150 struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000151
152 switch (c) {
153 case '1':
154 if (*flags & TCP_SRC_PORTS)
155 exit_error(PARAMETER_PROBLEM,
156 "Only one `--source-port' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000157 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000158 parse_tcp_ports(argv[optind-1], tcpinfo->spts);
159 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000160 tcpinfo->invflags |= XT_TCP_INV_SRCPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000161 *flags |= TCP_SRC_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000162 break;
163
164 case '2':
165 if (*flags & TCP_DST_PORTS)
166 exit_error(PARAMETER_PROBLEM,
167 "Only one `--destination-port' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000168 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000169 parse_tcp_ports(argv[optind-1], tcpinfo->dpts);
170 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000171 tcpinfo->invflags |= XT_TCP_INV_DSTPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000172 *flags |= TCP_DST_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000173 break;
174
175 case '3':
176 if (*flags & TCP_FLAGS)
177 exit_error(PARAMETER_PROBLEM,
178 "Only one of `--syn' or `--tcp-flags' "
179 " allowed");
Harald Welte38ed4212005-05-04 07:34:37 +0000180 parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
Marc Bouchere6869a82000-03-20 06:03:29 +0000181 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000182 break;
183
184 case '4':
185 if (*flags & TCP_FLAGS)
186 exit_error(PARAMETER_PROBLEM,
187 "Only one of `--syn' or `--tcp-flags' "
188 " allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000189 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000190
191 if (!argv[optind]
192 || argv[optind][0] == '-' || argv[optind][0] == '!')
193 exit_error(PARAMETER_PROBLEM,
194 "--tcp-flags requires two args.");
195
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000196 parse_tcp_flags(tcpinfo, argv[optind-1], argv[optind],
Rusty Russell78001fe2000-09-01 05:22:10 +0000197 invert);
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000198 optind++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000199 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000200 break;
201
202 case '5':
203 if (*flags & TCP_OPTION)
204 exit_error(PARAMETER_PROBLEM,
205 "Only one `--tcp-option' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000206 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000207 parse_tcp_option(argv[optind-1], &tcpinfo->option);
208 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000209 tcpinfo->invflags |= XT_TCP_INV_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000210 *flags |= TCP_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000211 break;
212
213 default:
214 return 0;
215 }
216
217 return 1;
218}
219
Marc Bouchere6869a82000-03-20 06:03:29 +0000220static char *
221port_to_service(int port)
222{
223 struct servent *service;
224
225 if ((service = getservbyport(htons(port), "tcp")))
226 return service->s_name;
227
228 return NULL;
229}
230
231static void
232print_port(u_int16_t port, int numeric)
233{
234 char *service;
235
236 if (numeric || (service = port_to_service(port)) == NULL)
237 printf("%u", port);
238 else
239 printf("%s", service);
240}
241
242static void
243print_ports(const char *name, u_int16_t min, u_int16_t max,
244 int invert, int numeric)
245{
246 const char *inv = invert ? "!" : "";
247
248 if (min != 0 || max != 0xFFFF || invert) {
249 printf("%s", name);
250 if (min == max) {
251 printf(":%s", inv);
252 print_port(min, numeric);
253 } else {
254 printf("s:%s", inv);
255 print_port(min, numeric);
256 printf(":");
257 print_port(max, numeric);
258 }
259 printf(" ");
260 }
261}
262
263static void
264print_option(u_int8_t option, int invert, int numeric)
265{
266 if (option || invert)
267 printf("option=%s%u ", invert ? "!" : "", option);
268}
269
270static void
271print_tcpf(u_int8_t flags)
272{
Rusty Russelledf14cf2000-04-19 11:26:44 +0000273 int have_flag = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000274
Rusty Russelledf14cf2000-04-19 11:26:44 +0000275 while (flags) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000276 unsigned int i;
277
Rusty Russelledf14cf2000-04-19 11:26:44 +0000278 for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
Marc Bouchere6869a82000-03-20 06:03:29 +0000279
Rusty Russelledf14cf2000-04-19 11:26:44 +0000280 if (have_flag)
Marc Bouchere6869a82000-03-20 06:03:29 +0000281 printf(",");
282 printf("%s", tcp_flag_names[i].name);
Rusty Russelledf14cf2000-04-19 11:26:44 +0000283 have_flag = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000284
285 flags &= ~tcp_flag_names[i].flag;
Rusty Russelledf14cf2000-04-19 11:26:44 +0000286 }
287
288 if (!have_flag)
289 printf("NONE");
Marc Bouchere6869a82000-03-20 06:03:29 +0000290}
291
292static void
293print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
294{
295 if (mask || invert) {
296 printf("flags:%s", invert ? "!" : "");
297 if (numeric)
Harald Welte0b4efea2001-04-12 15:50:49 +0000298 printf("0x%02X/0x%02X ", mask, cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000299 else {
Marc Bouchere6869a82000-03-20 06:03:29 +0000300 print_tcpf(mask);
Rusty Russell31728072000-09-01 06:29:06 +0000301 printf("/");
302 print_tcpf(cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000303 printf(" ");
304 }
305 }
306}
307
308/* Prints out the union ipt_matchinfo. */
309static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000310tcp_print(const void *ip, const struct xt_entry_match *match, int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000311{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000312 const struct xt_tcp *tcp = (struct xt_tcp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000313
314 printf("tcp ");
315 print_ports("spt", tcp->spts[0], tcp->spts[1],
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000316 tcp->invflags & XT_TCP_INV_SRCPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000317 numeric);
318 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000319 tcp->invflags & XT_TCP_INV_DSTPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000320 numeric);
321 print_option(tcp->option,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000322 tcp->invflags & XT_TCP_INV_OPTION,
Marc Bouchere6869a82000-03-20 06:03:29 +0000323 numeric);
324 print_flags(tcp->flg_mask, tcp->flg_cmp,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000325 tcp->invflags & XT_TCP_INV_FLAGS,
Marc Bouchere6869a82000-03-20 06:03:29 +0000326 numeric);
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000327 if (tcp->invflags & ~XT_TCP_INV_MASK)
Marc Bouchere6869a82000-03-20 06:03:29 +0000328 printf("Unknown invflags: 0x%X ",
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000329 tcp->invflags & ~XT_TCP_INV_MASK);
Marc Bouchere6869a82000-03-20 06:03:29 +0000330}
331
332/* Saves the union ipt_matchinfo in parsable form to stdout. */
Jan Engelhardt181dead2007-10-04 16:27:07 +0000333static void tcp_save(const void *ip, const struct xt_entry_match *match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000334{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000335 const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000336
337 if (tcpinfo->spts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000338 || tcpinfo->spts[1] != 0xFFFF) {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000339 if (tcpinfo->invflags & XT_TCP_INV_SRCPT)
Marc Bouchere6869a82000-03-20 06:03:29 +0000340 printf("! ");
341 if (tcpinfo->spts[0]
342 != tcpinfo->spts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000343 printf("--sport %u:%u ",
344 tcpinfo->spts[0],
345 tcpinfo->spts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000346 else
347 printf("--sport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000348 tcpinfo->spts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000349 }
350
351 if (tcpinfo->dpts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000352 || tcpinfo->dpts[1] != 0xFFFF) {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000353 if (tcpinfo->invflags & XT_TCP_INV_DSTPT)
Marc Bouchere6869a82000-03-20 06:03:29 +0000354 printf("! ");
355 if (tcpinfo->dpts[0]
356 != tcpinfo->dpts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000357 printf("--dport %u:%u ",
358 tcpinfo->dpts[0],
359 tcpinfo->dpts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000360 else
361 printf("--dport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000362 tcpinfo->dpts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000363 }
364
365 if (tcpinfo->option
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000366 || (tcpinfo->invflags & XT_TCP_INV_OPTION)) {
367 if (tcpinfo->invflags & XT_TCP_INV_OPTION)
Marc Bouchere6869a82000-03-20 06:03:29 +0000368 printf("! ");
369 printf("--tcp-option %u ", tcpinfo->option);
370 }
371
372 if (tcpinfo->flg_mask
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000373 || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) {
374 if (tcpinfo->invflags & XT_TCP_INV_FLAGS)
Marc Bouchere6869a82000-03-20 06:03:29 +0000375 printf("! ");
Harald Welte67f23b22000-11-05 17:53:06 +0000376 printf("--tcp-flags ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000377 if (tcpinfo->flg_mask != 0xFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000378 print_tcpf(tcpinfo->flg_mask);
379 }
Harald Welte67f23b22000-11-05 17:53:06 +0000380 printf(" ");
381 print_tcpf(tcpinfo->flg_cmp);
382 printf(" ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000383 }
384}
385
Jan Engelhardt181dead2007-10-04 16:27:07 +0000386static struct xtables_match tcp_match = {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000387 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000388 .name = "tcp",
389 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000390 .size = XT_ALIGN(sizeof(struct xt_tcp)),
391 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000392 .help = tcp_help,
393 .init = tcp_init,
394 .parse = tcp_parse,
395 .print = tcp_print,
396 .save = tcp_save,
397 .extra_opts = tcp_opts,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000398};
399
Jan Engelhardt181dead2007-10-04 16:27:07 +0000400static struct xtables_match tcp_match6 = {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000401 .family = AF_INET6,
402 .name = "tcp",
403 .version = IPTABLES_VERSION,
404 .size = XT_ALIGN(sizeof(struct xt_tcp)),
405 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000406 .help = tcp_help,
407 .init = tcp_init,
408 .parse = tcp_parse,
409 .print = tcp_print,
410 .save = tcp_save,
411 .extra_opts = tcp_opts,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000412};
Marc Bouchere6869a82000-03-20 06:03:29 +0000413
414void
415_init(void)
416{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000417 xtables_register_match(&tcp_match);
418 xtables_register_match(&tcp_match6);
Marc Bouchere6869a82000-03-20 06:03:29 +0000419}