blob: 069bb7fa59572d37d473da348bed0427ac0066c3 [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>
Thomas Jaroschecae0c32008-10-23 15:41:27 +02007#include <netinet/in.h>
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +00008#include <xtables.h>
9#include <linux/netfilter/xt_tcpudp.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000010
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"
Jan Engelhardt96727922008-08-13 14:42:41 +020015"[!] --tcp-flags mask comp match when TCP flags & mask == comp\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000016" (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"
Jan Engelhardt96727922008-08-13 14:42:41 +020019"[!] --source-port port[:port]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000020" --sport ...\n"
21" match source port(s)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020022"[!] --destination-port port[:port]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000023" --dport ...\n"
24" match destination port(s)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +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)
Jan Engelhardtaae6be92009-01-30 04:24:47 +010047 ports[0] = ports[1] = xtables_parse_port(buffer, "tcp");
Marc Bouchere6869a82000-03-20 06:03:29 +000048 else {
49 *cp = '\0';
50 cp++;
51
Jan Engelhardtaae6be92009-01-30 04:24:47 +010052 ports[0] = buffer[0] ? xtables_parse_port(buffer, "tcp") : 0;
53 ports[1] = cp[0] ? xtables_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
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100124 if (!xtables_strtoui(option, NULL, &ret, 1, UINT8_MAX))
Marc Bouchere6869a82000-03-20 06:03:29 +0000125 exit_error(PARAMETER_PROBLEM, "Bad TCP option `%s'", option);
126
Jan Engelhardt213e1852009-01-27 17:24:34 +0100127 *result = ret;
Marc Bouchere6869a82000-03-20 06:03:29 +0000128}
129
Jan Engelhardt181dead2007-10-04 16:27:07 +0000130static void tcp_init(struct xt_entry_match *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000131{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000132 struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000133
134 tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
135}
136
137#define TCP_SRC_PORTS 0x01
138#define TCP_DST_PORTS 0x02
139#define TCP_FLAGS 0x04
140#define TCP_OPTION 0x08
141
Marc Bouchere6869a82000-03-20 06:03:29 +0000142static int
Jan Engelhardt181dead2007-10-04 16:27:07 +0000143tcp_parse(int c, char **argv, int invert, unsigned int *flags,
144 const void *entry, struct xt_entry_match **match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000145{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000146 struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000147
148 switch (c) {
149 case '1':
150 if (*flags & TCP_SRC_PORTS)
151 exit_error(PARAMETER_PROBLEM,
152 "Only one `--source-port' allowed");
Jan Engelhardt0f16c722009-01-30 04:55:38 +0100153 xtables_check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000154 parse_tcp_ports(argv[optind-1], tcpinfo->spts);
155 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000156 tcpinfo->invflags |= XT_TCP_INV_SRCPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000157 *flags |= TCP_SRC_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000158 break;
159
160 case '2':
161 if (*flags & TCP_DST_PORTS)
162 exit_error(PARAMETER_PROBLEM,
163 "Only one `--destination-port' allowed");
Jan Engelhardt0f16c722009-01-30 04:55:38 +0100164 xtables_check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000165 parse_tcp_ports(argv[optind-1], tcpinfo->dpts);
166 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000167 tcpinfo->invflags |= XT_TCP_INV_DSTPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000168 *flags |= TCP_DST_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000169 break;
170
171 case '3':
172 if (*flags & TCP_FLAGS)
173 exit_error(PARAMETER_PROBLEM,
174 "Only one of `--syn' or `--tcp-flags' "
175 " allowed");
Harald Welte38ed4212005-05-04 07:34:37 +0000176 parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
Marc Bouchere6869a82000-03-20 06:03:29 +0000177 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000178 break;
179
180 case '4':
181 if (*flags & TCP_FLAGS)
182 exit_error(PARAMETER_PROBLEM,
183 "Only one of `--syn' or `--tcp-flags' "
184 " allowed");
Jan Engelhardt0f16c722009-01-30 04:55:38 +0100185 xtables_check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000186
187 if (!argv[optind]
188 || argv[optind][0] == '-' || argv[optind][0] == '!')
189 exit_error(PARAMETER_PROBLEM,
190 "--tcp-flags requires two args.");
191
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000192 parse_tcp_flags(tcpinfo, argv[optind-1], argv[optind],
Rusty Russell78001fe2000-09-01 05:22:10 +0000193 invert);
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000194 optind++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000195 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000196 break;
197
198 case '5':
199 if (*flags & TCP_OPTION)
200 exit_error(PARAMETER_PROBLEM,
201 "Only one `--tcp-option' allowed");
Jan Engelhardt0f16c722009-01-30 04:55:38 +0100202 xtables_check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000203 parse_tcp_option(argv[optind-1], &tcpinfo->option);
204 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000205 tcpinfo->invflags |= XT_TCP_INV_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000206 *flags |= TCP_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000207 break;
208
209 default:
210 return 0;
211 }
212
213 return 1;
214}
215
Marc Bouchere6869a82000-03-20 06:03:29 +0000216static char *
217port_to_service(int port)
218{
219 struct servent *service;
220
221 if ((service = getservbyport(htons(port), "tcp")))
222 return service->s_name;
223
224 return NULL;
225}
226
227static void
228print_port(u_int16_t port, int numeric)
229{
230 char *service;
231
232 if (numeric || (service = port_to_service(port)) == NULL)
233 printf("%u", port);
234 else
235 printf("%s", service);
236}
237
238static void
239print_ports(const char *name, u_int16_t min, u_int16_t max,
240 int invert, int numeric)
241{
242 const char *inv = invert ? "!" : "";
243
244 if (min != 0 || max != 0xFFFF || invert) {
245 printf("%s", name);
246 if (min == max) {
247 printf(":%s", inv);
248 print_port(min, numeric);
249 } else {
250 printf("s:%s", inv);
251 print_port(min, numeric);
252 printf(":");
253 print_port(max, numeric);
254 }
255 printf(" ");
256 }
257}
258
259static void
260print_option(u_int8_t option, int invert, int numeric)
261{
262 if (option || invert)
263 printf("option=%s%u ", invert ? "!" : "", option);
264}
265
266static void
267print_tcpf(u_int8_t flags)
268{
Rusty Russelledf14cf2000-04-19 11:26:44 +0000269 int have_flag = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000270
Rusty Russelledf14cf2000-04-19 11:26:44 +0000271 while (flags) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000272 unsigned int i;
273
Rusty Russelledf14cf2000-04-19 11:26:44 +0000274 for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
Marc Bouchere6869a82000-03-20 06:03:29 +0000275
Rusty Russelledf14cf2000-04-19 11:26:44 +0000276 if (have_flag)
Marc Bouchere6869a82000-03-20 06:03:29 +0000277 printf(",");
278 printf("%s", tcp_flag_names[i].name);
Rusty Russelledf14cf2000-04-19 11:26:44 +0000279 have_flag = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000280
281 flags &= ~tcp_flag_names[i].flag;
Rusty Russelledf14cf2000-04-19 11:26:44 +0000282 }
283
284 if (!have_flag)
285 printf("NONE");
Marc Bouchere6869a82000-03-20 06:03:29 +0000286}
287
288static void
289print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
290{
291 if (mask || invert) {
292 printf("flags:%s", invert ? "!" : "");
293 if (numeric)
Harald Welte0b4efea2001-04-12 15:50:49 +0000294 printf("0x%02X/0x%02X ", mask, cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000295 else {
Marc Bouchere6869a82000-03-20 06:03:29 +0000296 print_tcpf(mask);
Rusty Russell31728072000-09-01 06:29:06 +0000297 printf("/");
298 print_tcpf(cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000299 printf(" ");
300 }
301 }
302}
303
Marc Bouchere6869a82000-03-20 06:03:29 +0000304static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000305tcp_print(const void *ip, const struct xt_entry_match *match, int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000306{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000307 const struct xt_tcp *tcp = (struct xt_tcp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000308
309 printf("tcp ");
310 print_ports("spt", tcp->spts[0], tcp->spts[1],
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000311 tcp->invflags & XT_TCP_INV_SRCPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000312 numeric);
313 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000314 tcp->invflags & XT_TCP_INV_DSTPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000315 numeric);
316 print_option(tcp->option,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000317 tcp->invflags & XT_TCP_INV_OPTION,
Marc Bouchere6869a82000-03-20 06:03:29 +0000318 numeric);
319 print_flags(tcp->flg_mask, tcp->flg_cmp,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000320 tcp->invflags & XT_TCP_INV_FLAGS,
Marc Bouchere6869a82000-03-20 06:03:29 +0000321 numeric);
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000322 if (tcp->invflags & ~XT_TCP_INV_MASK)
Marc Bouchere6869a82000-03-20 06:03:29 +0000323 printf("Unknown invflags: 0x%X ",
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000324 tcp->invflags & ~XT_TCP_INV_MASK);
Marc Bouchere6869a82000-03-20 06:03:29 +0000325}
326
Jan Engelhardt181dead2007-10-04 16:27:07 +0000327static void tcp_save(const void *ip, const struct xt_entry_match *match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000328{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000329 const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000330
331 if (tcpinfo->spts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000332 || tcpinfo->spts[1] != 0xFFFF) {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000333 if (tcpinfo->invflags & XT_TCP_INV_SRCPT)
Marc Bouchere6869a82000-03-20 06:03:29 +0000334 printf("! ");
335 if (tcpinfo->spts[0]
336 != tcpinfo->spts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000337 printf("--sport %u:%u ",
338 tcpinfo->spts[0],
339 tcpinfo->spts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000340 else
341 printf("--sport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000342 tcpinfo->spts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000343 }
344
345 if (tcpinfo->dpts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000346 || tcpinfo->dpts[1] != 0xFFFF) {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000347 if (tcpinfo->invflags & XT_TCP_INV_DSTPT)
Marc Bouchere6869a82000-03-20 06:03:29 +0000348 printf("! ");
349 if (tcpinfo->dpts[0]
350 != tcpinfo->dpts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000351 printf("--dport %u:%u ",
352 tcpinfo->dpts[0],
353 tcpinfo->dpts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000354 else
355 printf("--dport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000356 tcpinfo->dpts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000357 }
358
359 if (tcpinfo->option
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000360 || (tcpinfo->invflags & XT_TCP_INV_OPTION)) {
361 if (tcpinfo->invflags & XT_TCP_INV_OPTION)
Marc Bouchere6869a82000-03-20 06:03:29 +0000362 printf("! ");
363 printf("--tcp-option %u ", tcpinfo->option);
364 }
365
366 if (tcpinfo->flg_mask
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000367 || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) {
368 if (tcpinfo->invflags & XT_TCP_INV_FLAGS)
Marc Bouchere6869a82000-03-20 06:03:29 +0000369 printf("! ");
Harald Welte67f23b22000-11-05 17:53:06 +0000370 printf("--tcp-flags ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000371 if (tcpinfo->flg_mask != 0xFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000372 print_tcpf(tcpinfo->flg_mask);
373 }
Harald Welte67f23b22000-11-05 17:53:06 +0000374 printf(" ");
375 print_tcpf(tcpinfo->flg_cmp);
376 printf(" ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000377 }
378}
379
Jan Engelhardt181dead2007-10-04 16:27:07 +0000380static struct xtables_match tcp_match = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100381 .family = NFPROTO_IPV4,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000382 .name = "tcp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200383 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000384 .size = XT_ALIGN(sizeof(struct xt_tcp)),
385 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000386 .help = tcp_help,
387 .init = tcp_init,
388 .parse = tcp_parse,
389 .print = tcp_print,
390 .save = tcp_save,
391 .extra_opts = tcp_opts,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000392};
393
Jan Engelhardt181dead2007-10-04 16:27:07 +0000394static struct xtables_match tcp_match6 = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100395 .family = NFPROTO_IPV6,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000396 .name = "tcp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200397 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000398 .size = XT_ALIGN(sizeof(struct xt_tcp)),
399 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000400 .help = tcp_help,
401 .init = tcp_init,
402 .parse = tcp_parse,
403 .print = tcp_print,
404 .save = tcp_save,
405 .extra_opts = tcp_opts,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000406};
Marc Bouchere6869a82000-03-20 06:03:29 +0000407
408void
409_init(void)
410{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000411 xtables_register_match(&tcp_match);
412 xtables_register_match(&tcp_match6);
Marc Bouchere6869a82000-03-20 06:03:29 +0000413}