1 /* Shared library add-on to iptables to add TCP support. */
8 #include <linux/netfilter/xt_tcpudp.h>
10 /* Function which prints out usage message. */
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] = parse_port(buffer, "tcp");
52 ports[0] = buffer[0] ? parse_port(buffer, "tcp") : 0;
53 ports[1] = cp[0] ? parse_port(cp, "tcp") : 0xFFFF;
55 if (ports[0] > ports[1])
56 exit_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, ",")) {
90 i < sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names);
92 if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) {
93 ret |= tcp_flag_names[i].flag;
97 if (i == sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names))
98 exit_error(PARAMETER_PROBLEM,
99 "Unknown TCP flag `%s'", ptr);
107 parse_tcp_flags(struct xt_tcp *tcpinfo,
112 tcpinfo->flg_mask = parse_tcp_flag(mask);
113 tcpinfo->flg_cmp = parse_tcp_flag(cmp);
116 tcpinfo->invflags |= XT_TCP_INV_FLAGS;
120 parse_tcp_option(const char *option, u_int8_t *result)
124 if (string_to_number(option, 1, 255, &ret) == -1)
125 exit_error(PARAMETER_PROBLEM, "Bad TCP option `%s'", option);
127 *result = (u_int8_t)ret;
130 /* Initialize the match. */
131 static void tcp_init(struct xt_entry_match *m)
133 struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data;
135 tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF;
138 #define TCP_SRC_PORTS 0x01
139 #define TCP_DST_PORTS 0x02
140 #define TCP_FLAGS 0x04
141 #define TCP_OPTION 0x08
143 /* Function which parses command options; returns true if it
146 tcp_parse(int c, char **argv, int invert, unsigned int *flags,
147 const void *entry, struct xt_entry_match **match)
149 struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data;
153 if (*flags & TCP_SRC_PORTS)
154 exit_error(PARAMETER_PROBLEM,
155 "Only one `--source-port' allowed");
156 check_inverse(optarg, &invert, &optind, 0);
157 parse_tcp_ports(argv[optind-1], tcpinfo->spts);
159 tcpinfo->invflags |= XT_TCP_INV_SRCPT;
160 *flags |= TCP_SRC_PORTS;
164 if (*flags & TCP_DST_PORTS)
165 exit_error(PARAMETER_PROBLEM,
166 "Only one `--destination-port' allowed");
167 check_inverse(optarg, &invert, &optind, 0);
168 parse_tcp_ports(argv[optind-1], tcpinfo->dpts);
170 tcpinfo->invflags |= XT_TCP_INV_DSTPT;
171 *flags |= TCP_DST_PORTS;
175 if (*flags & TCP_FLAGS)
176 exit_error(PARAMETER_PROBLEM,
177 "Only one of `--syn' or `--tcp-flags' "
179 parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
184 if (*flags & TCP_FLAGS)
185 exit_error(PARAMETER_PROBLEM,
186 "Only one of `--syn' or `--tcp-flags' "
188 check_inverse(optarg, &invert, &optind, 0);
191 || argv[optind][0] == '-' || argv[optind][0] == '!')
192 exit_error(PARAMETER_PROBLEM,
193 "--tcp-flags requires two args.");
195 parse_tcp_flags(tcpinfo, argv[optind-1], argv[optind],
202 if (*flags & TCP_OPTION)
203 exit_error(PARAMETER_PROBLEM,
204 "Only one `--tcp-option' allowed");
205 check_inverse(optarg, &invert, &optind, 0);
206 parse_tcp_option(argv[optind-1], &tcpinfo->option);
208 tcpinfo->invflags |= XT_TCP_INV_OPTION;
209 *flags |= TCP_OPTION;
220 port_to_service(int port)
222 struct servent *service;
224 if ((service = getservbyport(htons(port), "tcp")))
225 return service->s_name;
231 print_port(u_int16_t port, int numeric)
235 if (numeric || (service = port_to_service(port)) == NULL)
238 printf("%s", service);
242 print_ports(const char *name, u_int16_t min, u_int16_t max,
243 int invert, int numeric)
245 const char *inv = invert ? "!" : "";
247 if (min != 0 || max != 0xFFFF || invert) {
251 print_port(min, numeric);
254 print_port(min, numeric);
256 print_port(max, numeric);
263 print_option(u_int8_t option, int invert, int numeric)
265 if (option || invert)
266 printf("option=%s%u ", invert ? "!" : "", option);
270 print_tcpf(u_int8_t flags)
277 for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++);
281 printf("%s", tcp_flag_names[i].name);
284 flags &= ~tcp_flag_names[i].flag;
292 print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric)
294 if (mask || invert) {
295 printf("flags:%s", invert ? "!" : "");
297 printf("0x%02X/0x%02X ", mask, cmp);
307 /* Prints out the union ipt_matchinfo. */
309 tcp_print(const void *ip, const struct xt_entry_match *match, int numeric)
311 const struct xt_tcp *tcp = (struct xt_tcp *)match->data;
314 print_ports("spt", tcp->spts[0], tcp->spts[1],
315 tcp->invflags & XT_TCP_INV_SRCPT,
317 print_ports("dpt", tcp->dpts[0], tcp->dpts[1],
318 tcp->invflags & XT_TCP_INV_DSTPT,
320 print_option(tcp->option,
321 tcp->invflags & XT_TCP_INV_OPTION,
323 print_flags(tcp->flg_mask, tcp->flg_cmp,
324 tcp->invflags & XT_TCP_INV_FLAGS,
326 if (tcp->invflags & ~XT_TCP_INV_MASK)
327 printf("Unknown invflags: 0x%X ",
328 tcp->invflags & ~XT_TCP_INV_MASK);
331 /* Saves the union ipt_matchinfo in parsable form to stdout. */
332 static void tcp_save(const void *ip, const struct xt_entry_match *match)
334 const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data;
336 if (tcpinfo->spts[0] != 0
337 || tcpinfo->spts[1] != 0xFFFF) {
338 if (tcpinfo->invflags & XT_TCP_INV_SRCPT)
342 printf("--sport %u:%u ",
346 printf("--sport %u ",
350 if (tcpinfo->dpts[0] != 0
351 || tcpinfo->dpts[1] != 0xFFFF) {
352 if (tcpinfo->invflags & XT_TCP_INV_DSTPT)
356 printf("--dport %u:%u ",
360 printf("--dport %u ",
365 || (tcpinfo->invflags & XT_TCP_INV_OPTION)) {
366 if (tcpinfo->invflags & XT_TCP_INV_OPTION)
368 printf("--tcp-option %u ", tcpinfo->option);
371 if (tcpinfo->flg_mask
372 || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) {
373 if (tcpinfo->invflags & XT_TCP_INV_FLAGS)
375 printf("--tcp-flags ");
376 if (tcpinfo->flg_mask != 0xFF) {
377 print_tcpf(tcpinfo->flg_mask);
380 print_tcpf(tcpinfo->flg_cmp);
385 static struct xtables_match tcp_match = {
388 .version = XTABLES_VERSION,
389 .size = XT_ALIGN(sizeof(struct xt_tcp)),
390 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
396 .extra_opts = tcp_opts,
399 static struct xtables_match tcp_match6 = {
402 .version = XTABLES_VERSION,
403 .size = XT_ALIGN(sizeof(struct xt_tcp)),
404 .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)),
410 .extra_opts = tcp_opts,
416 xtables_register_match(&tcp_match);
417 xtables_register_match(&tcp_match6);