blob: ed076678580ae70ab89177ca9c90829e931a3960 [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. */
11static void
12help(void)
13{
14 printf(
15"TCP v%s 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"
Patrick McHardyYasuyuki KOZAKAIa8a4f5d2007-07-16 15:27:38 +000019" (equivalent to --tcp-flags SYN,RST,ACK,FIN SYN)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000020" --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\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000027IPTABLES_VERSION);
Marc Bouchere6869a82000-03-20 06:03:29 +000028}
29
Jan Engelhardt661f1122007-07-30 14:46:51 +000030static const struct option opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000031 { "source-port", 1, NULL, '1' },
32 { "sport", 1, NULL, '1' }, /* synonym */
33 { "destination-port", 1, NULL, '2' },
34 { "dport", 1, NULL, '2' }, /* synonym */
35 { "syn", 0, NULL, '3' },
36 { "tcp-flags", 1, NULL, '4' },
37 { "tcp-option", 1, NULL, '5' },
38 { }
Marc Bouchere6869a82000-03-20 06:03:29 +000039};
40
Marc Bouchere6869a82000-03-20 06:03:29 +000041static void
42parse_tcp_ports(const char *portstring, u_int16_t *ports)
43{
44 char *buffer;
45 char *cp;
46
47 buffer = strdup(portstring);
48 if ((cp = strchr(buffer, ':')) == NULL)
Phil Oesterdbac8ad2006-07-20 17:01:54 +000049 ports[0] = ports[1] = parse_port(buffer, "tcp");
Marc Bouchere6869a82000-03-20 06:03:29 +000050 else {
51 *cp = '\0';
52 cp++;
53
Phil Oesterdbac8ad2006-07-20 17:01:54 +000054 ports[0] = buffer[0] ? parse_port(buffer, "tcp") : 0;
55 ports[1] = cp[0] ? parse_port(cp, "tcp") : 0xFFFF;
Harald Welted15fb342002-07-26 16:27:57 +000056
57 if (ports[0] > ports[1])
58 exit_error(PARAMETER_PROBLEM,
59 "invalid portrange (min > max)");
Marc Bouchere6869a82000-03-20 06:03:29 +000060 }
61 free(buffer);
62}
63
64struct tcp_flag_names {
65 const char *name;
66 unsigned int flag;
67};
68
Jan Engelhardt0e2abed2007-10-04 16:25:58 +000069static const struct tcp_flag_names tcp_flag_names[]
Marc Bouchere6869a82000-03-20 06:03:29 +000070= { { "FIN", 0x01 },
71 { "SYN", 0x02 },
72 { "RST", 0x04 },
73 { "PSH", 0x08 },
74 { "ACK", 0x10 },
75 { "URG", 0x20 },
76 { "ALL", 0x3F },
77 { "NONE", 0 },
78};
79
80static unsigned int
81parse_tcp_flag(const char *flags)
82{
83 unsigned int ret = 0;
84 char *ptr;
85 char *buffer;
86
87 buffer = strdup(flags);
88
89 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
90 unsigned int i;
91 for (i = 0;
92 i < sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names);
93 i++) {
94 if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) {
95 ret |= tcp_flag_names[i].flag;
96 break;
97 }
98 }
99 if (i == sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names))
100 exit_error(PARAMETER_PROBLEM,
Harald Welte2aa78fe2003-03-30 18:29:56 +0000101 "Unknown TCP flag `%s'", ptr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000102 }
103
104 free(buffer);
105 return ret;
106}
107
108static void
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000109parse_tcp_flags(struct xt_tcp *tcpinfo,
Marc Bouchere6869a82000-03-20 06:03:29 +0000110 const char *mask,
111 const char *cmp,
112 int invert)
113{
114 tcpinfo->flg_mask = parse_tcp_flag(mask);
115 tcpinfo->flg_cmp = parse_tcp_flag(cmp);
116
117 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000118 tcpinfo->invflags |= XT_TCP_INV_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000119}
120
121static void
122parse_tcp_option(const char *option, u_int8_t *result)
123{
Harald Welteb4719762001-07-23 02:14:22 +0000124 unsigned int ret;
Marc Bouchere6869a82000-03-20 06:03:29 +0000125
Harald Welteb4719762001-07-23 02:14:22 +0000126 if (string_to_number(option, 1, 255, &ret) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000127 exit_error(PARAMETER_PROBLEM, "Bad TCP option `%s'", option);
128
129 *result = (u_int8_t)ret;
130}
131
132/* Initialize the match. */
133static void
Peter Rileyea146a92007-09-02 13:09:07 +0000134init(struct xt_entry_match *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000135{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000136 struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000137
138 tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
139}
140
141#define TCP_SRC_PORTS 0x01
142#define TCP_DST_PORTS 0x02
143#define TCP_FLAGS 0x04
144#define TCP_OPTION 0x08
145
146/* Function which parses command options; returns true if it
147 ate an option. */
148static int
149parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000150 const void *entry,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +0000151 struct xt_entry_match **match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000152{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000153 struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000154
155 switch (c) {
156 case '1':
157 if (*flags & TCP_SRC_PORTS)
158 exit_error(PARAMETER_PROBLEM,
159 "Only one `--source-port' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000160 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000161 parse_tcp_ports(argv[optind-1], tcpinfo->spts);
162 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000163 tcpinfo->invflags |= XT_TCP_INV_SRCPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000164 *flags |= TCP_SRC_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000165 break;
166
167 case '2':
168 if (*flags & TCP_DST_PORTS)
169 exit_error(PARAMETER_PROBLEM,
170 "Only one `--destination-port' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000171 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000172 parse_tcp_ports(argv[optind-1], tcpinfo->dpts);
173 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000174 tcpinfo->invflags |= XT_TCP_INV_DSTPT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000175 *flags |= TCP_DST_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000176 break;
177
178 case '3':
179 if (*flags & TCP_FLAGS)
180 exit_error(PARAMETER_PROBLEM,
181 "Only one of `--syn' or `--tcp-flags' "
182 " allowed");
Harald Welte38ed4212005-05-04 07:34:37 +0000183 parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
Marc Bouchere6869a82000-03-20 06:03:29 +0000184 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000185 break;
186
187 case '4':
188 if (*flags & TCP_FLAGS)
189 exit_error(PARAMETER_PROBLEM,
190 "Only one of `--syn' or `--tcp-flags' "
191 " allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000192 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000193
194 if (!argv[optind]
195 || argv[optind][0] == '-' || argv[optind][0] == '!')
196 exit_error(PARAMETER_PROBLEM,
197 "--tcp-flags requires two args.");
198
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000199 parse_tcp_flags(tcpinfo, argv[optind-1], argv[optind],
Rusty Russell78001fe2000-09-01 05:22:10 +0000200 invert);
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000201 optind++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000202 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000203 break;
204
205 case '5':
206 if (*flags & TCP_OPTION)
207 exit_error(PARAMETER_PROBLEM,
208 "Only one `--tcp-option' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000209 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000210 parse_tcp_option(argv[optind-1], &tcpinfo->option);
211 if (invert)
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000212 tcpinfo->invflags |= XT_TCP_INV_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000213 *flags |= TCP_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000214 break;
215
216 default:
217 return 0;
218 }
219
220 return 1;
221}
222
Marc Bouchere6869a82000-03-20 06:03:29 +0000223static char *
224port_to_service(int port)
225{
226 struct servent *service;
227
228 if ((service = getservbyport(htons(port), "tcp")))
229 return service->s_name;
230
231 return NULL;
232}
233
234static void
235print_port(u_int16_t port, int numeric)
236{
237 char *service;
238
239 if (numeric || (service = port_to_service(port)) == NULL)
240 printf("%u", port);
241 else
242 printf("%s", service);
243}
244
245static void
246print_ports(const char *name, u_int16_t min, u_int16_t max,
247 int invert, int numeric)
248{
249 const char *inv = invert ? "!" : "";
250
251 if (min != 0 || max != 0xFFFF || invert) {
252 printf("%s", name);
253 if (min == max) {
254 printf(":%s", inv);
255 print_port(min, numeric);
256 } else {
257 printf("s:%s", inv);
258 print_port(min, numeric);
259 printf(":");
260 print_port(max, numeric);
261 }
262 printf(" ");
263 }
264}
265
266static void
267print_option(u_int8_t option, int invert, int numeric)
268{
269 if (option || invert)
270 printf("option=%s%u ", invert ? "!" : "", option);
271}
272
273static void
274print_tcpf(u_int8_t flags)
275{
Rusty Russelledf14cf2000-04-19 11:26:44 +0000276 int have_flag = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000277
Rusty Russelledf14cf2000-04-19 11:26:44 +0000278 while (flags) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000279 unsigned int i;
280
Rusty Russelledf14cf2000-04-19 11:26:44 +0000281 for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
Marc Bouchere6869a82000-03-20 06:03:29 +0000282
Rusty Russelledf14cf2000-04-19 11:26:44 +0000283 if (have_flag)
Marc Bouchere6869a82000-03-20 06:03:29 +0000284 printf(",");
285 printf("%s", tcp_flag_names[i].name);
Rusty Russelledf14cf2000-04-19 11:26:44 +0000286 have_flag = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000287
288 flags &= ~tcp_flag_names[i].flag;
Rusty Russelledf14cf2000-04-19 11:26:44 +0000289 }
290
291 if (!have_flag)
292 printf("NONE");
Marc Bouchere6869a82000-03-20 06:03:29 +0000293}
294
295static void
296print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
297{
298 if (mask || invert) {
299 printf("flags:%s", invert ? "!" : "");
300 if (numeric)
Harald Welte0b4efea2001-04-12 15:50:49 +0000301 printf("0x%02X/0x%02X ", mask, cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000302 else {
Marc Bouchere6869a82000-03-20 06:03:29 +0000303 print_tcpf(mask);
Rusty Russell31728072000-09-01 06:29:06 +0000304 printf("/");
305 print_tcpf(cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000306 printf(" ");
307 }
308 }
309}
310
311/* Prints out the union ipt_matchinfo. */
312static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000313print(const void *ip,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +0000314 const struct xt_entry_match *match, int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000315{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000316 const struct xt_tcp *tcp = (struct xt_tcp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000317
318 printf("tcp ");
319 print_ports("spt", tcp->spts[0], tcp->spts[1],
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000320 tcp->invflags & XT_TCP_INV_SRCPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000321 numeric);
322 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000323 tcp->invflags & XT_TCP_INV_DSTPT,
Marc Bouchere6869a82000-03-20 06:03:29 +0000324 numeric);
325 print_option(tcp->option,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000326 tcp->invflags & XT_TCP_INV_OPTION,
Marc Bouchere6869a82000-03-20 06:03:29 +0000327 numeric);
328 print_flags(tcp->flg_mask, tcp->flg_cmp,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000329 tcp->invflags & XT_TCP_INV_FLAGS,
Marc Bouchere6869a82000-03-20 06:03:29 +0000330 numeric);
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000331 if (tcp->invflags & ~XT_TCP_INV_MASK)
Marc Bouchere6869a82000-03-20 06:03:29 +0000332 printf("Unknown invflags: 0x%X ",
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000333 tcp->invflags & ~XT_TCP_INV_MASK);
Marc Bouchere6869a82000-03-20 06:03:29 +0000334}
335
336/* Saves the union ipt_matchinfo in parsable form to stdout. */
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +0000337static void save(const void *ip, const struct xt_entry_match *match)
Marc Bouchere6869a82000-03-20 06:03:29 +0000338{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000339 const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data;
Marc Bouchere6869a82000-03-20 06:03:29 +0000340
341 if (tcpinfo->spts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000342 || tcpinfo->spts[1] != 0xFFFF) {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000343 if (tcpinfo->invflags & XT_TCP_INV_SRCPT)
Marc Bouchere6869a82000-03-20 06:03:29 +0000344 printf("! ");
345 if (tcpinfo->spts[0]
346 != tcpinfo->spts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000347 printf("--sport %u:%u ",
348 tcpinfo->spts[0],
349 tcpinfo->spts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000350 else
351 printf("--sport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000352 tcpinfo->spts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000353 }
354
355 if (tcpinfo->dpts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000356 || tcpinfo->dpts[1] != 0xFFFF) {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000357 if (tcpinfo->invflags & XT_TCP_INV_DSTPT)
Marc Bouchere6869a82000-03-20 06:03:29 +0000358 printf("! ");
359 if (tcpinfo->dpts[0]
360 != tcpinfo->dpts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000361 printf("--dport %u:%u ",
362 tcpinfo->dpts[0],
363 tcpinfo->dpts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000364 else
365 printf("--dport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000366 tcpinfo->dpts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000367 }
368
369 if (tcpinfo->option
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000370 || (tcpinfo->invflags & XT_TCP_INV_OPTION)) {
371 if (tcpinfo->invflags & XT_TCP_INV_OPTION)
Marc Bouchere6869a82000-03-20 06:03:29 +0000372 printf("! ");
373 printf("--tcp-option %u ", tcpinfo->option);
374 }
375
376 if (tcpinfo->flg_mask
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000377 || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) {
378 if (tcpinfo->invflags & XT_TCP_INV_FLAGS)
Marc Bouchere6869a82000-03-20 06:03:29 +0000379 printf("! ");
Harald Welte67f23b22000-11-05 17:53:06 +0000380 printf("--tcp-flags ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000381 if (tcpinfo->flg_mask != 0xFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000382 print_tcpf(tcpinfo->flg_mask);
383 }
Harald Welte67f23b22000-11-05 17:53:06 +0000384 printf(" ");
385 print_tcpf(tcpinfo->flg_cmp);
386 printf(" ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000387 }
388}
389
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000390static struct xtables_match tcp = {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000391 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000392 .name = "tcp",
393 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000394 .size = XT_ALIGN(sizeof(struct xt_tcp)),
395 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
Pablo Neira8caee8b2004-12-28 13:11:59 +0000396 .help = &help,
397 .init = &init,
398 .parse = &parse,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000399 .print = &print,
400 .save = &save,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000401 .extra_opts = opts,
402};
403
404static struct xtables_match tcp6 = {
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000405 .family = AF_INET6,
406 .name = "tcp",
407 .version = IPTABLES_VERSION,
408 .size = XT_ALIGN(sizeof(struct xt_tcp)),
409 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
410 .help = &help,
411 .init = &init,
412 .parse = &parse,
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000413 .print = &print,
414 .save = &save,
415 .extra_opts = opts,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000416};
Marc Bouchere6869a82000-03-20 06:03:29 +0000417
418void
419_init(void)
420{
Yasuyuki KOZAKAI95f186e2007-07-24 06:59:00 +0000421 xtables_register_match(&tcp);
422 xtables_register_match(&tcp6);
Marc Bouchere6869a82000-03-20 06:03:29 +0000423}