blob: eef94a18b93a74406a4cac52e7fe5d492d386727 [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
Jan Engelhardt181dead2007-10-04 16:27:07 +000010static void tcp_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000011{
12 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020013"tcp match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020014"[!] --tcp-flags mask comp match when TCP flags & mask == comp\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000015" (Flags: SYN ACK FIN RST URG PSH ALL NONE)\n"
16"[!] --syn match when only SYN flag set\n"
Patrick McHardyYasuyuki KOZAKAIa8a4f5d2007-07-16 15:27:38 +000017" (equivalent to --tcp-flags SYN,RST,ACK,FIN SYN)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020018"[!] --source-port port[:port]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000019" --sport ...\n"
20" match source port(s)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020021"[!] --destination-port port[:port]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000022" --dport ...\n"
23" match destination port(s)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020024"[!] --tcp-option number match if TCP option set\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000025}
26
Jan Engelhardt181dead2007-10-04 16:27:07 +000027static const struct option tcp_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000028 { "source-port", 1, NULL, '1' },
29 { "sport", 1, NULL, '1' }, /* synonym */
30 { "destination-port", 1, NULL, '2' },
31 { "dport", 1, NULL, '2' }, /* synonym */
32 { "syn", 0, NULL, '3' },
33 { "tcp-flags", 1, NULL, '4' },
34 { "tcp-option", 1, NULL, '5' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000035 { .name = NULL }
Marc Bouchere6869a82000-03-20 06:03:29 +000036};
37
Marc Bouchere6869a82000-03-20 06:03:29 +000038static void
39parse_tcp_ports(const char *portstring, u_int16_t *ports)
40{
41 char *buffer;
42 char *cp;
43
44 buffer = strdup(portstring);
45 if ((cp = strchr(buffer, ':')) == NULL)
Phil Oesterdbac8ad2006-07-20 17:01:54 +000046 ports[0] = ports[1] = parse_port(buffer, "tcp");
Marc Bouchere6869a82000-03-20 06:03:29 +000047 else {
48 *cp = '\0';
49 cp++;
50
Phil Oesterdbac8ad2006-07-20 17:01:54 +000051 ports[0] = buffer[0] ? parse_port(buffer, "tcp") : 0;
52 ports[1] = cp[0] ? parse_port(cp, "tcp") : 0xFFFF;
Harald Welted15fb342002-07-26 16:27:57 +000053
54 if (ports[0] > ports[1])
55 exit_error(PARAMETER_PROBLEM,
56 "invalid portrange (min > max)");
Marc Bouchere6869a82000-03-20 06:03:29 +000057 }
58 free(buffer);
59}
60
61struct tcp_flag_names {
62 const char *name;
63 unsigned int flag;
64};
65
Jan Engelhardt0e2abed2007-10-04 16:25:58 +000066static const struct tcp_flag_names tcp_flag_names[]
Marc Bouchere6869a82000-03-20 06:03:29 +000067= { { "FIN", 0x01 },
68 { "SYN", 0x02 },
69 { "RST", 0x04 },
70 { "PSH", 0x08 },
71 { "ACK", 0x10 },
72 { "URG", 0x20 },
73 { "ALL", 0x3F },
74 { "NONE", 0 },
75};
76
77static unsigned int
78parse_tcp_flag(const char *flags)
79{
80 unsigned int ret = 0;
81 char *ptr;
82 char *buffer;
83
84 buffer = strdup(flags);
85
86 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
87 unsigned int i;
88 for (i = 0;
89 i < sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names);
90 i++) {
91 if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) {
92 ret |= tcp_flag_names[i].flag;
93 break;
94 }
95 }
96 if (i == sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names))
97 exit_error(PARAMETER_PROBLEM,
Harald Welte2aa78fe2003-03-30 18:29:56 +000098 "Unknown TCP flag `%s'", ptr);
Marc Bouchere6869a82000-03-20 06:03:29 +000099 }
100
101 free(buffer);
102 return ret;
103}
104
105static void
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000106parse_tcp_flags(struct xt_tcp *tcpinfo,
Marc Bouchere6869a82000-03-20 06:03:29 +0000107 const char *mask,
108 const char *cmp,
109 int invert)
110{
111 tcpinfo->flg_mask = parse_tcp_flag(mask);
112 tcpinfo->flg_cmp = parse_tcp_flag(cmp);
113
114 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000115 tcpinfo->invflags |= XT_TCP_INV_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000116}
117
118static void
119parse_tcp_option(const char *option, u_int8_t *result)
120{
Harald Welteb4719762001-07-23 02:14:22 +0000121 unsigned int ret;
Marc Bouchere6869a82000-03-20 06:03:29 +0000122
Harald Welteb4719762001-07-23 02:14:22 +0000123 if (string_to_number(option, 1, 255, &ret) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000124 exit_error(PARAMETER_PROBLEM, "Bad TCP option `%s'", option);
125
126 *result = (u_int8_t)ret;
127}
128
Jan Engelhardt181dead2007-10-04 16:27:07 +0000129static void tcp_init(struct xt_entry_match *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000130{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000131 struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000132
133 tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
134}
135
136#define TCP_SRC_PORTS 0x01
137#define TCP_DST_PORTS 0x02
138#define TCP_FLAGS 0x04
139#define TCP_OPTION 0x08
140
Marc Bouchere6869a82000-03-20 06:03:29 +0000141static int
Jan Engelhardt181dead2007-10-04 16:27:07 +0000142tcp_parse(int c, char **argv, int invert, unsigned int *flags,
143 const void *entry, struct xt_entry_match **match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000144{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000145 struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000146
147 switch (c) {
148 case '1':
149 if (*flags & TCP_SRC_PORTS)
150 exit_error(PARAMETER_PROBLEM,
151 "Only one `--source-port' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000152 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000153 parse_tcp_ports(argv[optind-1], tcpinfo->spts);
154 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000155 tcpinfo->invflags |= XT_TCP_INV_SRCPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000156 *flags |= TCP_SRC_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000157 break;
158
159 case '2':
160 if (*flags & TCP_DST_PORTS)
161 exit_error(PARAMETER_PROBLEM,
162 "Only one `--destination-port' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000163 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000164 parse_tcp_ports(argv[optind-1], tcpinfo->dpts);
165 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000166 tcpinfo->invflags |= XT_TCP_INV_DSTPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000167 *flags |= TCP_DST_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000168 break;
169
170 case '3':
171 if (*flags & TCP_FLAGS)
172 exit_error(PARAMETER_PROBLEM,
173 "Only one of `--syn' or `--tcp-flags' "
174 " allowed");
Harald Welte38ed4212005-05-04 07:34:37 +0000175 parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
Marc Bouchere6869a82000-03-20 06:03:29 +0000176 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000177 break;
178
179 case '4':
180 if (*flags & TCP_FLAGS)
181 exit_error(PARAMETER_PROBLEM,
182 "Only one of `--syn' or `--tcp-flags' "
183 " allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000184 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000185
186 if (!argv[optind]
187 || argv[optind][0] == '-' || argv[optind][0] == '!')
188 exit_error(PARAMETER_PROBLEM,
189 "--tcp-flags requires two args.");
190
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000191 parse_tcp_flags(tcpinfo, argv[optind-1], argv[optind],
Rusty Russell78001fe2000-09-01 05:22:10 +0000192 invert);
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000193 optind++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000194 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000195 break;
196
197 case '5':
198 if (*flags & TCP_OPTION)
199 exit_error(PARAMETER_PROBLEM,
200 "Only one `--tcp-option' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000201 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000202 parse_tcp_option(argv[optind-1], &tcpinfo->option);
203 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000204 tcpinfo->invflags |= XT_TCP_INV_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000205 *flags |= TCP_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000206 break;
207
208 default:
209 return 0;
210 }
211
212 return 1;
213}
214
Marc Bouchere6869a82000-03-20 06:03:29 +0000215static char *
216port_to_service(int port)
217{
218 struct servent *service;
219
220 if ((service = getservbyport(htons(port), "tcp")))
221 return service->s_name;
222
223 return NULL;
224}
225
226static void
227print_port(u_int16_t port, int numeric)
228{
229 char *service;
230
231 if (numeric || (service = port_to_service(port)) == NULL)
232 printf("%u", port);
233 else
234 printf("%s", service);
235}
236
237static void
238print_ports(const char *name, u_int16_t min, u_int16_t max,
239 int invert, int numeric)
240{
241 const char *inv = invert ? "!" : "";
242
243 if (min != 0 || max != 0xFFFF || invert) {
244 printf("%s", name);
245 if (min == max) {
246 printf(":%s", inv);
247 print_port(min, numeric);
248 } else {
249 printf("s:%s", inv);
250 print_port(min, numeric);
251 printf(":");
252 print_port(max, numeric);
253 }
254 printf(" ");
255 }
256}
257
258static void
259print_option(u_int8_t option, int invert, int numeric)
260{
261 if (option || invert)
262 printf("option=%s%u ", invert ? "!" : "", option);
263}
264
265static void
266print_tcpf(u_int8_t flags)
267{
Rusty Russelledf14cf2000-04-19 11:26:44 +0000268 int have_flag = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000269
Rusty Russelledf14cf2000-04-19 11:26:44 +0000270 while (flags) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000271 unsigned int i;
272
Rusty Russelledf14cf2000-04-19 11:26:44 +0000273 for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
Marc Bouchere6869a82000-03-20 06:03:29 +0000274
Rusty Russelledf14cf2000-04-19 11:26:44 +0000275 if (have_flag)
Marc Bouchere6869a82000-03-20 06:03:29 +0000276 printf(",");
277 printf("%s", tcp_flag_names[i].name);
Rusty Russelledf14cf2000-04-19 11:26:44 +0000278 have_flag = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000279
280 flags &= ~tcp_flag_names[i].flag;
Rusty Russelledf14cf2000-04-19 11:26:44 +0000281 }
282
283 if (!have_flag)
284 printf("NONE");
Marc Bouchere6869a82000-03-20 06:03:29 +0000285}
286
287static void
288print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
289{
290 if (mask || invert) {
291 printf("flags:%s", invert ? "!" : "");
292 if (numeric)
Harald Welte0b4efea2001-04-12 15:50:49 +0000293 printf("0x%02X/0x%02X ", mask, cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000294 else {
Marc Bouchere6869a82000-03-20 06:03:29 +0000295 print_tcpf(mask);
Rusty Russell31728072000-09-01 06:29:06 +0000296 printf("/");
297 print_tcpf(cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000298 printf(" ");
299 }
300 }
301}
302
Marc Bouchere6869a82000-03-20 06:03:29 +0000303static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000304tcp_print(const void *ip, const struct xt_entry_match *match, int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000305{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000306 const struct xt_tcp *tcp = (struct xt_tcp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000307
308 printf("tcp ");
309 print_ports("spt", tcp->spts[0], tcp->spts[1],
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000310 tcp->invflags & XT_TCP_INV_SRCPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000311 numeric);
312 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000313 tcp->invflags & XT_TCP_INV_DSTPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000314 numeric);
315 print_option(tcp->option,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000316 tcp->invflags & XT_TCP_INV_OPTION,
Marc Bouchere6869a82000-03-20 06:03:29 +0000317 numeric);
318 print_flags(tcp->flg_mask, tcp->flg_cmp,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000319 tcp->invflags & XT_TCP_INV_FLAGS,
Marc Bouchere6869a82000-03-20 06:03:29 +0000320 numeric);
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000321 if (tcp->invflags & ~XT_TCP_INV_MASK)
Marc Bouchere6869a82000-03-20 06:03:29 +0000322 printf("Unknown invflags: 0x%X ",
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000323 tcp->invflags & ~XT_TCP_INV_MASK);
Marc Bouchere6869a82000-03-20 06:03:29 +0000324}
325
Jan Engelhardt181dead2007-10-04 16:27:07 +0000326static void tcp_save(const void *ip, const struct xt_entry_match *match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000327{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000328 const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000329
330 if (tcpinfo->spts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000331 || tcpinfo->spts[1] != 0xFFFF) {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000332 if (tcpinfo->invflags & XT_TCP_INV_SRCPT)
Marc Bouchere6869a82000-03-20 06:03:29 +0000333 printf("! ");
334 if (tcpinfo->spts[0]
335 != tcpinfo->spts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000336 printf("--sport %u:%u ",
337 tcpinfo->spts[0],
338 tcpinfo->spts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000339 else
340 printf("--sport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000341 tcpinfo->spts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000342 }
343
344 if (tcpinfo->dpts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000345 || tcpinfo->dpts[1] != 0xFFFF) {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000346 if (tcpinfo->invflags & XT_TCP_INV_DSTPT)
Marc Bouchere6869a82000-03-20 06:03:29 +0000347 printf("! ");
348 if (tcpinfo->dpts[0]
349 != tcpinfo->dpts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000350 printf("--dport %u:%u ",
351 tcpinfo->dpts[0],
352 tcpinfo->dpts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000353 else
354 printf("--dport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000355 tcpinfo->dpts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000356 }
357
358 if (tcpinfo->option
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000359 || (tcpinfo->invflags & XT_TCP_INV_OPTION)) {
360 if (tcpinfo->invflags & XT_TCP_INV_OPTION)
Marc Bouchere6869a82000-03-20 06:03:29 +0000361 printf("! ");
362 printf("--tcp-option %u ", tcpinfo->option);
363 }
364
365 if (tcpinfo->flg_mask
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000366 || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) {
367 if (tcpinfo->invflags & XT_TCP_INV_FLAGS)
Marc Bouchere6869a82000-03-20 06:03:29 +0000368 printf("! ");
Harald Welte67f23b22000-11-05 17:53:06 +0000369 printf("--tcp-flags ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000370 if (tcpinfo->flg_mask != 0xFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000371 print_tcpf(tcpinfo->flg_mask);
372 }
Harald Welte67f23b22000-11-05 17:53:06 +0000373 printf(" ");
374 print_tcpf(tcpinfo->flg_cmp);
375 printf(" ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000376 }
377}
378
Jan Engelhardt181dead2007-10-04 16:27:07 +0000379static struct xtables_match tcp_match = {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000380 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000381 .name = "tcp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200382 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000383 .size = XT_ALIGN(sizeof(struct xt_tcp)),
384 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000385 .help = tcp_help,
386 .init = tcp_init,
387 .parse = tcp_parse,
388 .print = tcp_print,
389 .save = tcp_save,
390 .extra_opts = tcp_opts,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000391};
392
Jan Engelhardt181dead2007-10-04 16:27:07 +0000393static struct xtables_match tcp_match6 = {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000394 .family = AF_INET6,
395 .name = "tcp",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200396 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000397 .size = XT_ALIGN(sizeof(struct xt_tcp)),
398 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000399 .help = tcp_help,
400 .init = tcp_init,
401 .parse = tcp_parse,
402 .print = tcp_print,
403 .save = tcp_save,
404 .extra_opts = tcp_opts,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000405};
Marc Bouchere6869a82000-03-20 06:03:29 +0000406
407void
408_init(void)
409{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000410 xtables_register_match(&tcp_match);
411 xtables_register_match(&tcp_match6);
Marc Bouchere6869a82000-03-20 06:03:29 +0000412}