blob: 276d0e2a39ebf5e5030b72f04743cf47c9fa03bb [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",
27NETFILTER_VERSION);
28}
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
41static int
42service_to_port(const char *name)
43{
44 struct servent *service;
45
46 if ((service = getservbyname(name, "tcp")) != NULL)
47 return ntohs((unsigned short) service->s_port);
48
49 return -1;
50}
51
52static u_int16_t
53parse_tcp_port(const char *port)
54{
55 int portnum;
56
57 if ((portnum = string_to_number(port, 0, 65535)) != -1 ||
58 (portnum = service_to_port(port)) != -1)
59 return (u_int16_t)portnum;
60
61 exit_error(PARAMETER_PROBLEM,
62 "invalid TCP port/service `%s' specified", port);
63}
64
65static void
66parse_tcp_ports(const char *portstring, u_int16_t *ports)
67{
68 char *buffer;
69 char *cp;
70
71 buffer = strdup(portstring);
72 if ((cp = strchr(buffer, ':')) == NULL)
73 ports[0] = ports[1] = parse_tcp_port(buffer);
74 else {
75 *cp = '\0';
76 cp++;
77
78 ports[0] = buffer[0] ? parse_tcp_port(buffer) : 0;
79 ports[1] = cp[0] ? parse_tcp_port(cp) : 0xFFFF;
80 }
81 free(buffer);
82}
83
84struct tcp_flag_names {
85 const char *name;
86 unsigned int flag;
87};
88
89static struct tcp_flag_names tcp_flag_names[]
90= { { "FIN", 0x01 },
91 { "SYN", 0x02 },
92 { "RST", 0x04 },
93 { "PSH", 0x08 },
94 { "ACK", 0x10 },
95 { "URG", 0x20 },
96 { "ALL", 0x3F },
97 { "NONE", 0 },
98};
99
100static unsigned int
101parse_tcp_flag(const char *flags)
102{
103 unsigned int ret = 0;
104 char *ptr;
105 char *buffer;
106
107 buffer = strdup(flags);
108
109 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
110 unsigned int i;
111 for (i = 0;
112 i < sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names);
113 i++) {
114 if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) {
115 ret |= tcp_flag_names[i].flag;
116 break;
117 }
118 }
119 if (i == sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names))
120 exit_error(PARAMETER_PROBLEM,
121 "Unknown TCP flag `%s'", buffer);
122 }
123
124 free(buffer);
125 return ret;
126}
127
128static void
129parse_tcp_flags(struct ipt_tcp *tcpinfo,
130 const char *mask,
131 const char *cmp,
132 int invert)
133{
134 tcpinfo->flg_mask = parse_tcp_flag(mask);
135 tcpinfo->flg_cmp = parse_tcp_flag(cmp);
136
137 if (invert)
138 tcpinfo->invflags |= IPT_TCP_INV_FLAGS;
139}
140
141static void
142parse_tcp_option(const char *option, u_int8_t *result)
143{
144 int ret;
145
146 ret = string_to_number(option, 1, 255);
147 if (ret == -1)
148 exit_error(PARAMETER_PROBLEM, "Bad TCP option `%s'", option);
149
150 *result = (u_int8_t)ret;
151}
152
153/* Initialize the match. */
154static void
155init(struct ipt_entry_match *m, unsigned int *nfcache)
156{
157 struct ipt_tcp *tcpinfo = (struct ipt_tcp *)m->data;
158
159 tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
160}
161
162#define TCP_SRC_PORTS 0x01
163#define TCP_DST_PORTS 0x02
164#define TCP_FLAGS 0x04
165#define TCP_OPTION 0x08
166
167/* Function which parses command options; returns true if it
168 ate an option. */
169static int
170parse(int c, char **argv, int invert, unsigned int *flags,
171 const struct ipt_entry *entry,
172 unsigned int *nfcache,
173 struct ipt_entry_match **match)
174{
175 struct ipt_tcp *tcpinfo = (struct ipt_tcp *)(*match)->data;
176
177 switch (c) {
178 case '1':
179 if (*flags & TCP_SRC_PORTS)
180 exit_error(PARAMETER_PROBLEM,
181 "Only one `--source-port' allowed");
182 if (check_inverse(optarg, &invert))
183 optind++;
184 parse_tcp_ports(argv[optind-1], tcpinfo->spts);
185 if (invert)
186 tcpinfo->invflags |= IPT_TCP_INV_SRCPT;
187 *flags |= TCP_SRC_PORTS;
188 *nfcache |= NFC_IP_SRC_PT;
189 break;
190
191 case '2':
192 if (*flags & TCP_DST_PORTS)
193 exit_error(PARAMETER_PROBLEM,
194 "Only one `--destination-port' allowed");
195 if (check_inverse(optarg, &invert))
196 optind++;
197 parse_tcp_ports(argv[optind-1], tcpinfo->dpts);
198 if (invert)
199 tcpinfo->invflags |= IPT_TCP_INV_DSTPT;
200 *flags |= TCP_DST_PORTS;
201 *nfcache |= NFC_IP_DST_PT;
202 break;
203
204 case '3':
205 if (*flags & TCP_FLAGS)
206 exit_error(PARAMETER_PROBLEM,
207 "Only one of `--syn' or `--tcp-flags' "
208 " allowed");
209 parse_tcp_flags(tcpinfo, "SYN,RST,ACK", "SYN", invert);
210 *flags |= TCP_FLAGS;
211 *nfcache |= NFC_IP_TCPFLAGS;
212 break;
213
214 case '4':
215 if (*flags & TCP_FLAGS)
216 exit_error(PARAMETER_PROBLEM,
217 "Only one of `--syn' or `--tcp-flags' "
218 " allowed");
219 if (check_inverse(optarg, &invert))
220 optind++;
221
222 if (!argv[optind]
223 || argv[optind][0] == '-' || argv[optind][0] == '!')
224 exit_error(PARAMETER_PROBLEM,
225 "--tcp-flags requires two args.");
226
227 parse_tcp_flags(tcpinfo, optarg, argv[optind++], invert);
228 *flags |= TCP_FLAGS;
229 *nfcache |= NFC_IP_TCPFLAGS;
230 break;
231
232 case '5':
233 if (*flags & TCP_OPTION)
234 exit_error(PARAMETER_PROBLEM,
235 "Only one `--tcp-option' allowed");
236 if (check_inverse(optarg, &invert))
237 optind++;
238 parse_tcp_option(argv[optind-1], &tcpinfo->option);
239 if (invert)
240 tcpinfo->invflags |= IPT_TCP_INV_OPTION;
241 *flags |= TCP_OPTION;
242 *nfcache |= NFC_IP_PROTO_UNKNOWN;
243 break;
244
245 default:
246 return 0;
247 }
248
249 return 1;
250}
251
252/* Final check; we don't care. */
253static void
254final_check(unsigned int flags)
255{
256}
257
258static char *
259port_to_service(int port)
260{
261 struct servent *service;
262
263 if ((service = getservbyport(htons(port), "tcp")))
264 return service->s_name;
265
266 return NULL;
267}
268
269static void
270print_port(u_int16_t port, int numeric)
271{
272 char *service;
273
274 if (numeric || (service = port_to_service(port)) == NULL)
275 printf("%u", port);
276 else
277 printf("%s", service);
278}
279
280static void
281print_ports(const char *name, u_int16_t min, u_int16_t max,
282 int invert, int numeric)
283{
284 const char *inv = invert ? "!" : "";
285
286 if (min != 0 || max != 0xFFFF || invert) {
287 printf("%s", name);
288 if (min == max) {
289 printf(":%s", inv);
290 print_port(min, numeric);
291 } else {
292 printf("s:%s", inv);
293 print_port(min, numeric);
294 printf(":");
295 print_port(max, numeric);
296 }
297 printf(" ");
298 }
299}
300
301static void
302print_option(u_int8_t option, int invert, int numeric)
303{
304 if (option || invert)
305 printf("option=%s%u ", invert ? "!" : "", option);
306}
307
308static void
309print_tcpf(u_int8_t flags)
310{
Rusty Russelledf14cf2000-04-19 11:26:44 +0000311 int have_flag = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000312
Rusty Russelledf14cf2000-04-19 11:26:44 +0000313 while (flags) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000314 unsigned int i;
315
Rusty Russelledf14cf2000-04-19 11:26:44 +0000316 for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
Marc Bouchere6869a82000-03-20 06:03:29 +0000317
Rusty Russelledf14cf2000-04-19 11:26:44 +0000318 if (have_flag)
Marc Bouchere6869a82000-03-20 06:03:29 +0000319 printf(",");
320 printf("%s", tcp_flag_names[i].name);
Rusty Russelledf14cf2000-04-19 11:26:44 +0000321 have_flag = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000322
323 flags &= ~tcp_flag_names[i].flag;
Rusty Russelledf14cf2000-04-19 11:26:44 +0000324 }
325
326 if (!have_flag)
327 printf("NONE");
Marc Bouchere6869a82000-03-20 06:03:29 +0000328}
329
330static void
331print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
332{
333 if (mask || invert) {
334 printf("flags:%s", invert ? "!" : "");
335 if (numeric)
336 printf("0x02%X/0x02%X ", mask, cmp);
337 else {
338 print_tcpf(cmp);
339 printf("/");
340 print_tcpf(mask);
341 printf(" ");
342 }
343 }
344}
345
346/* Prints out the union ipt_matchinfo. */
347static void
348print(const struct ipt_ip *ip,
349 const struct ipt_entry_match *match, int numeric)
350{
351 const struct ipt_tcp *tcp = (struct ipt_tcp *)match->data;
352
353 printf("tcp ");
354 print_ports("spt", tcp->spts[0], tcp->spts[1],
355 tcp->invflags & IPT_TCP_INV_SRCPT,
356 numeric);
357 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
358 tcp->invflags & IPT_TCP_INV_DSTPT,
359 numeric);
360 print_option(tcp->option,
361 tcp->invflags & IPT_TCP_INV_OPTION,
362 numeric);
363 print_flags(tcp->flg_mask, tcp->flg_cmp,
364 tcp->invflags & IPT_TCP_INV_FLAGS,
365 numeric);
366 if (tcp->invflags & ~IPT_TCP_INV_MASK)
367 printf("Unknown invflags: 0x%X ",
368 tcp->invflags & ~IPT_TCP_INV_MASK);
369}
370
371/* Saves the union ipt_matchinfo in parsable form to stdout. */
372static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
373{
374 const struct ipt_tcp *tcpinfo = (struct ipt_tcp *)match->data;
375
376 if (tcpinfo->spts[0] != 0
377 && tcpinfo->spts[1] != 0xFFFF) {
378 if (tcpinfo->invflags & IPT_TCP_INV_SRCPT)
379 printf("! ");
380 if (tcpinfo->spts[0]
381 != tcpinfo->spts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000382 printf("--sport %u:%u ",
383 tcpinfo->spts[0],
384 tcpinfo->spts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000385 else
386 printf("--sport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000387 tcpinfo->spts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000388 }
389
390 if (tcpinfo->dpts[0] != 0
391 && tcpinfo->dpts[1] != 0xFFFF) {
392 if (tcpinfo->invflags & IPT_TCP_INV_DSTPT)
393 printf("! ");
394 if (tcpinfo->dpts[0]
395 != tcpinfo->dpts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000396 printf("--dport %u:%u ",
397 tcpinfo->dpts[0],
398 tcpinfo->dpts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000399 else
400 printf("--dport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000401 tcpinfo->dpts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000402 }
403
404 if (tcpinfo->option
405 || (tcpinfo->invflags & IPT_TCP_INV_OPTION)) {
406 if (tcpinfo->invflags & IPT_TCP_INV_OPTION)
407 printf("! ");
408 printf("--tcp-option %u ", tcpinfo->option);
409 }
410
411 if (tcpinfo->flg_mask
412 || (tcpinfo->invflags & IPT_TCP_INV_FLAGS)) {
413 if (tcpinfo->invflags & IPT_TCP_INV_FLAGS)
414 printf("! ");
415
416 print_tcpf(tcpinfo->flg_cmp);
417 if (tcpinfo->flg_mask != 0xFF) {
418 printf("/");
419 print_tcpf(tcpinfo->flg_mask);
420 }
421 }
422}
423
424struct iptables_match tcp
425= { NULL,
426 "tcp",
427 NETFILTER_VERSION,
428 sizeof(struct ipt_tcp),
Rusty Russelledf14cf2000-04-19 11:26:44 +0000429 sizeof(struct ipt_tcp),
Marc Bouchere6869a82000-03-20 06:03:29 +0000430 &help,
431 &init,
432 &parse,
433 &final_check,
434 &print,
435 &save,
436 opts };
437
438void
439_init(void)
440{
441 register_match(&tcp);
442}