blob: fb99bec76b2e4694dd2e03e4790020515b11e3c1 [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
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000227 parse_tcp_flags(tcpinfo, argv[optind-1], argv[optind],
Rusty Russell78001fe2000-09-01 05:22:10 +0000228 invert);
Rusty Russellfa9f9f92000-09-01 05:39:52 +0000229 optind++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000230 *flags |= TCP_FLAGS;
231 *nfcache |= NFC_IP_TCPFLAGS;
232 break;
233
234 case '5':
235 if (*flags & TCP_OPTION)
236 exit_error(PARAMETER_PROBLEM,
237 "Only one `--tcp-option' allowed");
238 if (check_inverse(optarg, &invert))
239 optind++;
240 parse_tcp_option(argv[optind-1], &tcpinfo->option);
241 if (invert)
242 tcpinfo->invflags |= IPT_TCP_INV_OPTION;
243 *flags |= TCP_OPTION;
244 *nfcache |= NFC_IP_PROTO_UNKNOWN;
245 break;
246
247 default:
248 return 0;
249 }
250
251 return 1;
252}
253
254/* Final check; we don't care. */
255static void
256final_check(unsigned int flags)
257{
258}
259
260static char *
261port_to_service(int port)
262{
263 struct servent *service;
264
265 if ((service = getservbyport(htons(port), "tcp")))
266 return service->s_name;
267
268 return NULL;
269}
270
271static void
272print_port(u_int16_t port, int numeric)
273{
274 char *service;
275
276 if (numeric || (service = port_to_service(port)) == NULL)
277 printf("%u", port);
278 else
279 printf("%s", service);
280}
281
282static void
283print_ports(const char *name, u_int16_t min, u_int16_t max,
284 int invert, int numeric)
285{
286 const char *inv = invert ? "!" : "";
287
288 if (min != 0 || max != 0xFFFF || invert) {
289 printf("%s", name);
290 if (min == max) {
291 printf(":%s", inv);
292 print_port(min, numeric);
293 } else {
294 printf("s:%s", inv);
295 print_port(min, numeric);
296 printf(":");
297 print_port(max, numeric);
298 }
299 printf(" ");
300 }
301}
302
303static void
304print_option(u_int8_t option, int invert, int numeric)
305{
306 if (option || invert)
307 printf("option=%s%u ", invert ? "!" : "", option);
308}
309
310static void
311print_tcpf(u_int8_t flags)
312{
Rusty Russelledf14cf2000-04-19 11:26:44 +0000313 int have_flag = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000314
Rusty Russelledf14cf2000-04-19 11:26:44 +0000315 while (flags) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000316 unsigned int i;
317
Rusty Russelledf14cf2000-04-19 11:26:44 +0000318 for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
Marc Bouchere6869a82000-03-20 06:03:29 +0000319
Rusty Russelledf14cf2000-04-19 11:26:44 +0000320 if (have_flag)
Marc Bouchere6869a82000-03-20 06:03:29 +0000321 printf(",");
322 printf("%s", tcp_flag_names[i].name);
Rusty Russelledf14cf2000-04-19 11:26:44 +0000323 have_flag = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000324
325 flags &= ~tcp_flag_names[i].flag;
Rusty Russelledf14cf2000-04-19 11:26:44 +0000326 }
327
328 if (!have_flag)
329 printf("NONE");
Marc Bouchere6869a82000-03-20 06:03:29 +0000330}
331
332static void
333print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
334{
335 if (mask || invert) {
336 printf("flags:%s", invert ? "!" : "");
337 if (numeric)
Harald Welte0b4efea2001-04-12 15:50:49 +0000338 printf("0x%02X/0x%02X ", mask, cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000339 else {
Marc Bouchere6869a82000-03-20 06:03:29 +0000340 print_tcpf(mask);
Rusty Russell31728072000-09-01 06:29:06 +0000341 printf("/");
342 print_tcpf(cmp);
Marc Bouchere6869a82000-03-20 06:03:29 +0000343 printf(" ");
344 }
345 }
346}
347
348/* Prints out the union ipt_matchinfo. */
349static void
350print(const struct ipt_ip *ip,
351 const struct ipt_entry_match *match, int numeric)
352{
353 const struct ipt_tcp *tcp = (struct ipt_tcp *)match->data;
354
355 printf("tcp ");
356 print_ports("spt", tcp->spts[0], tcp->spts[1],
357 tcp->invflags & IPT_TCP_INV_SRCPT,
358 numeric);
359 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
360 tcp->invflags & IPT_TCP_INV_DSTPT,
361 numeric);
362 print_option(tcp->option,
363 tcp->invflags & IPT_TCP_INV_OPTION,
364 numeric);
365 print_flags(tcp->flg_mask, tcp->flg_cmp,
366 tcp->invflags & IPT_TCP_INV_FLAGS,
367 numeric);
368 if (tcp->invflags & ~IPT_TCP_INV_MASK)
369 printf("Unknown invflags: 0x%X ",
370 tcp->invflags & ~IPT_TCP_INV_MASK);
371}
372
373/* Saves the union ipt_matchinfo in parsable form to stdout. */
374static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
375{
376 const struct ipt_tcp *tcpinfo = (struct ipt_tcp *)match->data;
377
378 if (tcpinfo->spts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000379 || tcpinfo->spts[1] != 0xFFFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000380 if (tcpinfo->invflags & IPT_TCP_INV_SRCPT)
381 printf("! ");
382 if (tcpinfo->spts[0]
383 != tcpinfo->spts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000384 printf("--sport %u:%u ",
385 tcpinfo->spts[0],
386 tcpinfo->spts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000387 else
388 printf("--sport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000389 tcpinfo->spts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000390 }
391
392 if (tcpinfo->dpts[0] != 0
Rusty Russell73f72f52000-07-03 10:17:57 +0000393 || tcpinfo->dpts[1] != 0xFFFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000394 if (tcpinfo->invflags & IPT_TCP_INV_DSTPT)
395 printf("! ");
396 if (tcpinfo->dpts[0]
397 != tcpinfo->dpts[1])
Marc Boucher9f2009c2000-04-07 17:30:28 +0000398 printf("--dport %u:%u ",
399 tcpinfo->dpts[0],
400 tcpinfo->dpts[1]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000401 else
402 printf("--dport %u ",
Marc Boucher9f2009c2000-04-07 17:30:28 +0000403 tcpinfo->dpts[0]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000404 }
405
406 if (tcpinfo->option
407 || (tcpinfo->invflags & IPT_TCP_INV_OPTION)) {
408 if (tcpinfo->invflags & IPT_TCP_INV_OPTION)
409 printf("! ");
410 printf("--tcp-option %u ", tcpinfo->option);
411 }
412
413 if (tcpinfo->flg_mask
414 || (tcpinfo->invflags & IPT_TCP_INV_FLAGS)) {
415 if (tcpinfo->invflags & IPT_TCP_INV_FLAGS)
416 printf("! ");
Harald Welte67f23b22000-11-05 17:53:06 +0000417 printf("--tcp-flags ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000418 if (tcpinfo->flg_mask != 0xFF) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000419 print_tcpf(tcpinfo->flg_mask);
420 }
Harald Welte67f23b22000-11-05 17:53:06 +0000421 printf(" ");
422 print_tcpf(tcpinfo->flg_cmp);
423 printf(" ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000424 }
425}
426
427struct iptables_match tcp
428= { NULL,
429 "tcp",
430 NETFILTER_VERSION,
Rusty Russell73f72f52000-07-03 10:17:57 +0000431 IPT_ALIGN(sizeof(struct ipt_tcp)),
432 IPT_ALIGN(sizeof(struct ipt_tcp)),
Marc Bouchere6869a82000-03-20 06:03:29 +0000433 &help,
434 &init,
435 &parse,
436 &final_check,
437 &print,
438 &save,
439 opts };
440
441void
442_init(void)
443{
444 register_match(&tcp);
445}