blob: 94285a09e436d05e56b702b30e605043d0f403c3 [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{
311 int sole_flag = 1;
312
313 do {
314 unsigned int i;
315
316 /* Terminates because last flag is 0 */
317 for (i = 0; !(flags & tcp_flag_names[i].flag); i++);
318
319 if (!sole_flag)
320 printf(",");
321 printf("%s", tcp_flag_names[i].name);
322 sole_flag = 0;
323
324 flags &= ~tcp_flag_names[i].flag;
325 } while (flags);
326}
327
328static void
329print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
330{
331 if (mask || invert) {
332 printf("flags:%s", invert ? "!" : "");
333 if (numeric)
334 printf("0x02%X/0x02%X ", mask, cmp);
335 else {
336 print_tcpf(cmp);
337 printf("/");
338 print_tcpf(mask);
339 printf(" ");
340 }
341 }
342}
343
344/* Prints out the union ipt_matchinfo. */
345static void
346print(const struct ipt_ip *ip,
347 const struct ipt_entry_match *match, int numeric)
348{
349 const struct ipt_tcp *tcp = (struct ipt_tcp *)match->data;
350
351 printf("tcp ");
352 print_ports("spt", tcp->spts[0], tcp->spts[1],
353 tcp->invflags & IPT_TCP_INV_SRCPT,
354 numeric);
355 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
356 tcp->invflags & IPT_TCP_INV_DSTPT,
357 numeric);
358 print_option(tcp->option,
359 tcp->invflags & IPT_TCP_INV_OPTION,
360 numeric);
361 print_flags(tcp->flg_mask, tcp->flg_cmp,
362 tcp->invflags & IPT_TCP_INV_FLAGS,
363 numeric);
364 if (tcp->invflags & ~IPT_TCP_INV_MASK)
365 printf("Unknown invflags: 0x%X ",
366 tcp->invflags & ~IPT_TCP_INV_MASK);
367}
368
369/* Saves the union ipt_matchinfo in parsable form to stdout. */
370static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
371{
372 const struct ipt_tcp *tcpinfo = (struct ipt_tcp *)match->data;
373
374 if (tcpinfo->spts[0] != 0
375 && tcpinfo->spts[1] != 0xFFFF) {
376 if (tcpinfo->invflags & IPT_TCP_INV_SRCPT)
377 printf("! ");
378 if (tcpinfo->spts[0]
379 != tcpinfo->spts[1])
380 printf("--sport %u-%u ",
381 ntohs(tcpinfo->spts[0]),
382 ntohs(tcpinfo->spts[1]));
383 else
384 printf("--sport %u ",
385 ntohs(tcpinfo->spts[0]));
386 }
387
388 if (tcpinfo->dpts[0] != 0
389 && tcpinfo->dpts[1] != 0xFFFF) {
390 if (tcpinfo->invflags & IPT_TCP_INV_DSTPT)
391 printf("! ");
392 if (tcpinfo->dpts[0]
393 != tcpinfo->dpts[1])
394 printf("--dport %u-%u ",
395 ntohs(tcpinfo->dpts[0]),
396 ntohs(tcpinfo->dpts[1]));
397 else
398 printf("--dport %u ",
399 ntohs(tcpinfo->dpts[0]));
400 }
401
402 if (tcpinfo->option
403 || (tcpinfo->invflags & IPT_TCP_INV_OPTION)) {
404 if (tcpinfo->invflags & IPT_TCP_INV_OPTION)
405 printf("! ");
406 printf("--tcp-option %u ", tcpinfo->option);
407 }
408
409 if (tcpinfo->flg_mask
410 || (tcpinfo->invflags & IPT_TCP_INV_FLAGS)) {
411 if (tcpinfo->invflags & IPT_TCP_INV_FLAGS)
412 printf("! ");
413
414 print_tcpf(tcpinfo->flg_cmp);
415 if (tcpinfo->flg_mask != 0xFF) {
416 printf("/");
417 print_tcpf(tcpinfo->flg_mask);
418 }
419 }
420}
421
422struct iptables_match tcp
423= { NULL,
424 "tcp",
425 NETFILTER_VERSION,
426 sizeof(struct ipt_tcp),
427 &help,
428 &init,
429 &parse,
430 &final_check,
431 &print,
432 &save,
433 opts };
434
435void
436_init(void)
437{
438 register_match(&tcp);
439}