blob: 7551a0a65a9af3838dc894bee2392b50102adfa6 [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>
7#include <iptables.h>
8#include <linux/netfilter_ipv4/ip_tables.h>
9
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"
19" (equivalent to --tcp-flags SYN,RST,ACK SYN)\n"
20" --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
30static struct option opts[] = {
31 { "source-port", 1, 0, '1' },
32 { "sport", 1, 0, '1' }, /* synonym */
33 { "destination-port", 1, 0, '2' },
34 { "dport", 1, 0, '2' }, /* synonym */
35 { "syn", 0, 0, '3' },
36 { "tcp-flags", 1, 0, '4' },
37 { "tcp-option", 1, 0, '5' },
38 {0}
39};
40
Marc Bouchere6869a82000-03-20 06:03:29 +000041static u_int16_t
42parse_tcp_port(const char *port)
43{
Harald Welteb4719762001-07-23 02:14:22 +000044 unsigned int portnum;
Marc Bouchere6869a82000-03-20 06:03:29 +000045
Harald Welteb4719762001-07-23 02:14:22 +000046 if (string_to_number(port, 0, 65535, &portnum) != -1 ||
Phil Oester58179b12006-07-20 17:00:19 +000047 (portnum = service_to_port(port, "tcp")) != -1)
Marc Bouchere6869a82000-03-20 06:03:29 +000048 return (u_int16_t)portnum;
49
50 exit_error(PARAMETER_PROBLEM,
51 "invalid TCP port/service `%s' specified", port);
52}
53
54static void
55parse_tcp_ports(const char *portstring, u_int16_t *ports)
56{
57 char *buffer;
58 char *cp;
59
60 buffer = strdup(portstring);
61 if ((cp = strchr(buffer, ':')) == NULL)
62 ports[0] = ports[1] = parse_tcp_port(buffer);
63 else {
64 *cp = '\0';
65 cp++;
66
67 ports[0] = buffer[0] ? parse_tcp_port(buffer) : 0;
68 ports[1] = cp[0] ? parse_tcp_port(cp) : 0xFFFF;
Harald Welted15fb342002-07-26 16:27:57 +000069
70 if (ports[0] > ports[1])
71 exit_error(PARAMETER_PROBLEM,
72 "invalid portrange (min > max)");
Marc Bouchere6869a82000-03-20 06:03:29 +000073 }
74 free(buffer);
75}
76
77struct tcp_flag_names {
78 const char *name;
79 unsigned int flag;
80};
81
82static struct tcp_flag_names tcp_flag_names[]
83= { { "FIN", 0x01 },
84 { "SYN", 0x02 },
85 { "RST", 0x04 },
86 { "PSH", 0x08 },
87 { "ACK", 0x10 },
88 { "URG", 0x20 },
89 { "ALL", 0x3F },
90 { "NONE", 0 },
91};
92
93static unsigned int
94parse_tcp_flag(const char *flags)
95{
96 unsigned int ret = 0;
97 char *ptr;
98 char *buffer;
99
100 buffer = strdup(flags);
101
102 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
103 unsigned int i;
104 for (i = 0;
105 i < sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names);
106 i++) {
107 if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) {
108 ret |= tcp_flag_names[i].flag;
109 break;
110 }
111 }
112 if (i == sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names))
113 exit_error(PARAMETER_PROBLEM,
Harald Welte2aa78fe2003-03-30 18:29:56 +0000114 "Unknown TCP flag `%s'", ptr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000115 }
116
117 free(buffer);
118 return ret;
119}
120
121static void
122parse_tcp_flags(struct ipt_tcp *tcpinfo,
123 const char *mask,
124 const char *cmp,
125 int invert)
126{
127 tcpinfo->flg_mask = parse_tcp_flag(mask);
128 tcpinfo->flg_cmp = parse_tcp_flag(cmp);
129
130 if (invert)
131 tcpinfo->invflags |= IPT_TCP_INV_FLAGS;
132}
133
134static void
135parse_tcp_option(const char *option, u_int8_t *result)
136{
Harald Welteb4719762001-07-23 02:14:22 +0000137 unsigned int ret;
Marc Bouchere6869a82000-03-20 06:03:29 +0000138
Harald Welteb4719762001-07-23 02:14:22 +0000139 if (string_to_number(option, 1, 255, &ret) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000140 exit_error(PARAMETER_PROBLEM, "Bad TCP option `%s'", option);
141
142 *result = (u_int8_t)ret;
143}
144
145/* Initialize the match. */
146static void
147init(struct ipt_entry_match *m, unsigned int *nfcache)
148{
149 struct ipt_tcp *tcpinfo = (struct ipt_tcp *)m->data;
150
151 tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
152}
153
154#define TCP_SRC_PORTS 0x01
155#define TCP_DST_PORTS 0x02
156#define TCP_FLAGS 0x04
157#define TCP_OPTION 0x08
158
159/* Function which parses command options; returns true if it
160 ate an option. */
161static int
162parse(int c, char **argv, int invert, unsigned int *flags,
163 const struct ipt_entry *entry,
164 unsigned int *nfcache,
165 struct ipt_entry_match **match)
166{
167 struct ipt_tcp *tcpinfo = (struct ipt_tcp *)(*match)->data;
168
169 switch (c) {
170 case '1':
171 if (*flags & TCP_SRC_PORTS)
172 exit_error(PARAMETER_PROBLEM,
173 "Only one `--source-port' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000174 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000175 parse_tcp_ports(argv[optind-1], tcpinfo->spts);
176 if (invert)
177 tcpinfo->invflags |= IPT_TCP_INV_SRCPT;
178 *flags |= TCP_SRC_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000179 break;
180
181 case '2':
182 if (*flags & TCP_DST_PORTS)
183 exit_error(PARAMETER_PROBLEM,
184 "Only one `--destination-port' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000185 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000186 parse_tcp_ports(argv[optind-1], tcpinfo->dpts);
187 if (invert)
188 tcpinfo->invflags |= IPT_TCP_INV_DSTPT;
189 *flags |= TCP_DST_PORTS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000190 break;
191
192 case '3':
193 if (*flags & TCP_FLAGS)
194 exit_error(PARAMETER_PROBLEM,
195 "Only one of `--syn' or `--tcp-flags' "
196 " allowed");
Harald Welte38ed4212005-05-04 07:34:37 +0000197 parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
Marc Bouchere6869a82000-03-20 06:03:29 +0000198 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000199 break;
200
201 case '4':
202 if (*flags & TCP_FLAGS)
203 exit_error(PARAMETER_PROBLEM,
204 "Only one of `--syn' or `--tcp-flags' "
205 " allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000206 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000207
208 if (!argv[optind]
209 || argv[optind][0] == '-' || argv[optind][0] == '!')
210 exit_error(PARAMETER_PROBLEM,
211 "--tcp-flags requires two args.");
212
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000213 parse_tcp_flags(tcpinfo, argv[optind-1], argv[optind],
Rusty Russell78001fe2000-09-01 05:22:10 +0000214 invert);
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000215 optind++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000216 *flags |= TCP_FLAGS;
Marc Bouchere6869a82000-03-20 06:03:29 +0000217 break;
218
219 case '5':
220 if (*flags & TCP_OPTION)
221 exit_error(PARAMETER_PROBLEM,
222 "Only one `--tcp-option' allowed");
Harald Welteb77f1da2002-03-14 11:35:58 +0000223 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000224 parse_tcp_option(argv[optind-1], &tcpinfo->option);
225 if (invert)
226 tcpinfo->invflags |= IPT_TCP_INV_OPTION;
227 *flags |= TCP_OPTION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000228 break;
229
230 default:
231 return 0;
232 }
233
234 return 1;
235}
236
237/* Final check; we don't care. */
238static void
239final_check(unsigned int flags)
240{
241}
242
243static char *
244port_to_service(int port)
245{
246 struct servent *service;
247
248 if ((service = getservbyport(htons(port), "tcp")))
249 return service->s_name;
250
251 return NULL;
252}
253
254static void
255print_port(u_int16_t port, int numeric)
256{
257 char *service;
258
259 if (numeric || (service = port_to_service(port)) == NULL)
260 printf("%u", port);
261 else
262 printf("%s", service);
263}
264
265static void
266print_ports(const char *name, u_int16_t min, u_int16_t max,
267 int invert, int numeric)
268{
269 const char *inv = invert ? "!" : "";
270
271 if (min != 0 || max != 0xFFFF || invert) {
272 printf("%s", name);
273 if (min == max) {
274 printf(":%s", inv);
275 print_port(min, numeric);
276 } else {
277 printf("s:%s", inv);
278 print_port(min, numeric);
279 printf(":");
280 print_port(max, numeric);
281 }
282 printf(" ");
283 }
284}
285
286static void
287print_option(u_int8_t option, int invert, int numeric)
288{
289 if (option || invert)
290 printf("option=%s%u ", invert ? "!" : "", option);
291}
292
293static void
294print_tcpf(u_int8_t flags)
295{
Rusty Russelledf14cf2000-04-19 11:26:44 +0000296 int have_flag = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000297
Rusty Russelledf14cf2000-04-19 11:26:44 +0000298 while (flags) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000299 unsigned int i;
300
Rusty Russelledf14cf2000-04-19 11:26:44 +0000301 for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
Marc Bouchere6869a82000-03-20 06:03:29 +0000302
Rusty Russelledf14cf2000-04-19 11:26:44 +0000303 if (have_flag)
Marc Bouchere6869a82000-03-20 06:03:29 +0000304 printf(",");
305 printf("%s", tcp_flag_names[i].name);
Rusty Russelledf14cf2000-04-19 11:26:44 +0000306 have_flag = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000307
308 flags &= ~tcp_flag_names[i].flag;
Rusty Russelledf14cf2000-04-19 11:26:44 +0000309 }
310
311 if (!have_flag)
312 printf("NONE");
Marc Bouchere6869a82000-03-20 06:03:29 +0000313}
314
315static void
316print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
317{
318 if (mask || invert) {
319 printf("flags:%s", invert ? "!" : "");
320 if (numeric)
Harald Welte0b4efea2001-04-12 15:50:49 +0000321 printf("0x%02X/0x%02X ", mask, cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000322 else {
Marc Bouchere6869a82000-03-20 06:03:29 +0000323 print_tcpf(mask);
Rusty Russell31728072000-09-01 06:29:06 +0000324 printf("/");
325 print_tcpf(cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000326 printf(" ");
327 }
328 }
329}
330
331/* Prints out the union ipt_matchinfo. */
332static void
333print(const struct ipt_ip *ip,
334 const struct ipt_entry_match *match, int numeric)
335{
336 const struct ipt_tcp *tcp = (struct ipt_tcp *)match->data;
337
338 printf("tcp ");
339 print_ports("spt", tcp->spts[0], tcp->spts[1],
340 tcp->invflags & IPT_TCP_INV_SRCPT,
341 numeric);
342 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
343 tcp->invflags & IPT_TCP_INV_DSTPT,
344 numeric);
345 print_option(tcp->option,
346 tcp->invflags & IPT_TCP_INV_OPTION,
347 numeric);
348 print_flags(tcp->flg_mask, tcp->flg_cmp,
349 tcp->invflags & IPT_TCP_INV_FLAGS,
350 numeric);
351 if (tcp->invflags & ~IPT_TCP_INV_MASK)
352 printf("Unknown invflags: 0x%X ",
353 tcp->invflags & ~IPT_TCP_INV_MASK);
354}
355
356/* Saves the union ipt_matchinfo in parsable form to stdout. */
357static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
358{
359 const struct ipt_tcp *tcpinfo = (struct ipt_tcp *)match->data;
360
361 if (tcpinfo->spts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000362 || tcpinfo->spts[1] != 0xFFFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000363 if (tcpinfo->invflags & IPT_TCP_INV_SRCPT)
364 printf("! ");
365 if (tcpinfo->spts[0]
366 != tcpinfo->spts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000367 printf("--sport %u:%u ",
368 tcpinfo->spts[0],
369 tcpinfo->spts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000370 else
371 printf("--sport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000372 tcpinfo->spts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000373 }
374
375 if (tcpinfo->dpts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000376 || tcpinfo->dpts[1] != 0xFFFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000377 if (tcpinfo->invflags & IPT_TCP_INV_DSTPT)
378 printf("! ");
379 if (tcpinfo->dpts[0]
380 != tcpinfo->dpts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000381 printf("--dport %u:%u ",
382 tcpinfo->dpts[0],
383 tcpinfo->dpts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000384 else
385 printf("--dport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000386 tcpinfo->dpts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000387 }
388
389 if (tcpinfo->option
390 || (tcpinfo->invflags & IPT_TCP_INV_OPTION)) {
391 if (tcpinfo->invflags & IPT_TCP_INV_OPTION)
392 printf("! ");
393 printf("--tcp-option %u ", tcpinfo->option);
394 }
395
396 if (tcpinfo->flg_mask
397 || (tcpinfo->invflags & IPT_TCP_INV_FLAGS)) {
398 if (tcpinfo->invflags & IPT_TCP_INV_FLAGS)
399 printf("! ");
Harald Welte67f23b22000-11-05 17:53:06 +0000400 printf("--tcp-flags ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000401 if (tcpinfo->flg_mask != 0xFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000402 print_tcpf(tcpinfo->flg_mask);
403 }
Harald Welte67f23b22000-11-05 17:53:06 +0000404 printf(" ");
405 print_tcpf(tcpinfo->flg_cmp);
406 printf(" ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000407 }
408}
409
Pablo Neira8caee8b2004-12-28 13:11:59 +0000410static struct iptables_match tcp = {
411 .next = NULL,
412 .name = "tcp",
413 .version = IPTABLES_VERSION,
414 .size = IPT_ALIGN(sizeof(struct ipt_tcp)),
415 .userspacesize = IPT_ALIGN(sizeof(struct ipt_tcp)),
416 .help = &help,
417 .init = &init,
418 .parse = &parse,
419 .final_check = &final_check,
420 .print = &print,
421 .save = &save,
422 .extra_opts = opts
423};
Marc Bouchere6869a82000-03-20 06:03:29 +0000424
425void
426_init(void)
427{
428 register_match(&tcp);
429}