1 /* Shared library add-on to iptables to add TCP support. */
7 #include <netinet/in.h>
9 #include <linux/netfilter/xt_tcpudp.h>
11 static void tcp_help(void)
14 "tcp match options:\n"
15 "[!] --tcp-flags mask comp match when TCP flags & mask == comp\n"
16 " (Flags: SYN ACK FIN RST URG PSH ALL NONE)\n"
17 "[!] --syn match when only SYN flag set\n"
18 " (equivalent to --tcp-flags SYN,RST,ACK,FIN SYN)\n"
19 "[!] --source-port port[:port]\n"
21 " match source port(s)\n"
22 "[!] --destination-port port[:port]\n"
24 " match destination port(s)\n"
25 "[!] --tcp-option number match if TCP option set\n");
28 static const struct option tcp_opts[] = {
29 { "source-port", 1, NULL, '1' },
30 { "sport", 1, NULL, '1' }, /* synonym */
31 { "destination-port", 1, NULL, '2' },
32 { "dport", 1, NULL, '2' }, /* synonym */
33 { "syn", 0, NULL, '3' },
34 { "tcp-flags", 1, NULL, '4' },
35 { "tcp-option", 1, NULL, '5' },
40 parse_tcp_ports(const char *portstring, u_int16_t *ports)
45 buffer = strdup(portstring);
46 if ((cp = strchr(buffer, ':')) == NULL)
47 ports[0] = ports[1] = xtables_parse_port(buffer, "tcp");
52 ports[0] = buffer[0] ? xtables_parse_port(buffer, "tcp") : 0;
53 ports[1] = cp[0] ? xtables_parse_port(cp, "tcp") : 0xFFFF;
55 if (ports[0] > ports[1])
56 xtables_error(PARAMETER_PROBLEM,
57 "invalid portrange (min > max)");
62 struct tcp_flag_names {
67 static const struct tcp_flag_names tcp_flag_names[]
79 parse_tcp_flag(const char *flags)
85 buffer = strdup(flags);
87 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
89 for (i = 0; i < ARRAY_SIZE(tcp_flag_names); ++i)
90 if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) {
91 ret |= tcp_flag_names[i].flag;
94 if (i == ARRAY_SIZE(tcp_flag_names))
95 xtables_error(PARAMETER_PROBLEM,
96 "Unknown TCP flag `%s'", ptr);
104 parse_tcp_flags(struct xt_tcp *tcpinfo,
109 tcpinfo->flg_mask = parse_tcp_flag(mask);
110 tcpinfo->flg_cmp = parse_tcp_flag(cmp);
113 tcpinfo->invflags |= XT_TCP_INV_FLAGS;
117 parse_tcp_option(const char *option, u_int8_t *result)
121 if (!xtables_strtoui(option, NULL, &ret, 1, UINT8_MAX))
122 xtables_error(PARAMETER_PROBLEM, "Bad TCP option \"%s\"", option);
127 static void tcp_init(struct xt_entry_match *m)
129 struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data;
131 tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
134 #define TCP_SRC_PORTS 0x01
135 #define TCP_DST_PORTS 0x02
136 #define TCP_FLAGS 0x04
137 #define TCP_OPTION 0x08
140 tcp_parse(int c, char **argv, int invert, unsigned int *flags,
141 const void *entry, struct xt_entry_match **match)
143 struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data;
147 if (*flags & TCP_SRC_PORTS)
148 xtables_error(PARAMETER_PROBLEM,
149 "Only one `--source-port' allowed");
150 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
151 parse_tcp_ports(optarg, tcpinfo->spts);
153 tcpinfo->invflags |= XT_TCP_INV_SRCPT;
154 *flags |= TCP_SRC_PORTS;
158 if (*flags & TCP_DST_PORTS)
159 xtables_error(PARAMETER_PROBLEM,
160 "Only one `--destination-port' allowed");
161 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
162 parse_tcp_ports(optarg, tcpinfo->dpts);
164 tcpinfo->invflags |= XT_TCP_INV_DSTPT;
165 *flags |= TCP_DST_PORTS;
169 if (*flags & TCP_FLAGS)
170 xtables_error(PARAMETER_PROBLEM,
171 "Only one of `--syn' or `--tcp-flags' "
173 parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
178 if (*flags & TCP_FLAGS)
179 xtables_error(PARAMETER_PROBLEM,
180 "Only one of `--syn' or `--tcp-flags' "
182 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
185 || argv[optind][0] == '-' || argv[optind][0] == '!')
186 xtables_error(PARAMETER_PROBLEM,
187 "--tcp-flags requires two args.");
189 parse_tcp_flags(tcpinfo, optarg, argv[optind],
196 if (*flags & TCP_OPTION)
197 xtables_error(PARAMETER_PROBLEM,
198 "Only one `--tcp-option' allowed");
199 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
200 parse_tcp_option(optarg, &tcpinfo->option);
202 tcpinfo->invflags |= XT_TCP_INV_OPTION;
203 *flags |= TCP_OPTION;
214 port_to_service(int port)
216 struct servent *service;
218 if ((service = getservbyport(htons(port), "tcp")))
219 return service->s_name;
225 print_port(u_int16_t port, int numeric)
229 if (numeric || (service = port_to_service(port)) == NULL)
232 printf("%s", service);
236 print_ports(const char *name, u_int16_t min, u_int16_t max,
237 int invert, int numeric)
239 const char *inv = invert ? "!" : "";
241 if (min != 0 || max != 0xFFFF || invert) {
245 print_port(min, numeric);
248 print_port(min, numeric);
250 print_port(max, numeric);
257 print_option(u_int8_t option, int invert, int numeric)
259 if (option || invert)
260 printf("option=%s%u ", invert ? "!" : "", option);
264 print_tcpf(u_int8_t flags)
271 for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
275 printf("%s", tcp_flag_names[i].name);
278 flags &= ~tcp_flag_names[i].flag;
286 print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
288 if (mask || invert) {
289 printf("flags:%s", invert ? "!" : "");
291 printf("0x%02X/0x%02X ", mask, cmp);
302 tcp_print(const void *ip, const struct xt_entry_match *match, int numeric)
304 const struct xt_tcp *tcp = (struct xt_tcp *)match->data;
307 print_ports("spt", tcp->spts[0], tcp->spts[1],
308 tcp->invflags & XT_TCP_INV_SRCPT,
310 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
311 tcp->invflags & XT_TCP_INV_DSTPT,
313 print_option(tcp->option,
314 tcp->invflags & XT_TCP_INV_OPTION,
316 print_flags(tcp->flg_mask, tcp->flg_cmp,
317 tcp->invflags & XT_TCP_INV_FLAGS,
319 if (tcp->invflags & ~XT_TCP_INV_MASK)
320 printf("Unknown invflags: 0x%X ",
321 tcp->invflags & ~XT_TCP_INV_MASK);
324 static void tcp_save(const void *ip, const struct xt_entry_match *match)
326 const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data;
328 if (tcpinfo->spts[0] != 0
329 || tcpinfo->spts[1] != 0xFFFF) {
330 if (tcpinfo->invflags & XT_TCP_INV_SRCPT)
334 printf("--sport %u:%u ",
338 printf("--sport %u ",
342 if (tcpinfo->dpts[0] != 0
343 || tcpinfo->dpts[1] != 0xFFFF) {
344 if (tcpinfo->invflags & XT_TCP_INV_DSTPT)
348 printf("--dport %u:%u ",
352 printf("--dport %u ",
357 || (tcpinfo->invflags & XT_TCP_INV_OPTION)) {
358 if (tcpinfo->invflags & XT_TCP_INV_OPTION)
360 printf("--tcp-option %u ", tcpinfo->option);
363 if (tcpinfo->flg_mask
364 || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) {
365 if (tcpinfo->invflags & XT_TCP_INV_FLAGS)
367 printf("--tcp-flags ");
368 if (tcpinfo->flg_mask != 0xFF) {
369 print_tcpf(tcpinfo->flg_mask);
372 print_tcpf(tcpinfo->flg_cmp);
377 static struct xtables_match tcp_match = {
378 .family = NFPROTO_UNSPEC,
380 .version = XTABLES_VERSION,
381 .size = XT_ALIGN(sizeof(struct xt_tcp)),
382 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
388 .extra_opts = tcp_opts,
394 xtables_register_match(&tcp_match);