Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* 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> |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 7 | #include <xtables.h> |
| 8 | #include <linux/netfilter/xt_tcpudp.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 9 | |
| 10 | /* Function which prints out usage message. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 11 | static void tcp_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 12 | { |
| 13 | printf( |
| 14 | "TCP v%s 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" |
Patrick McHardyYasuyuki KOZAKAI | a8a4f5d | 2007-07-16 15:27:38 +0000 | [diff] [blame] | 18 | " (equivalent to --tcp-flags SYN,RST,ACK,FIN SYN)\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 19 | " --source-port [!] port[:port]\n" |
| 20 | " --sport ...\n" |
| 21 | " match source port(s)\n" |
| 22 | " --destination-port [!] port[:port]\n" |
| 23 | " --dport ...\n" |
| 24 | " match destination port(s)\n" |
| 25 | " --tcp-option [!] number match if TCP option set\n\n", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 26 | IPTABLES_VERSION); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 29 | static const struct option tcp_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 30 | { "source-port", 1, NULL, '1' }, |
| 31 | { "sport", 1, NULL, '1' }, /* synonym */ |
| 32 | { "destination-port", 1, NULL, '2' }, |
| 33 | { "dport", 1, NULL, '2' }, /* synonym */ |
| 34 | { "syn", 0, NULL, '3' }, |
| 35 | { "tcp-flags", 1, NULL, '4' }, |
| 36 | { "tcp-option", 1, NULL, '5' }, |
| 37 | { } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 40 | static void |
| 41 | parse_tcp_ports(const char *portstring, u_int16_t *ports) |
| 42 | { |
| 43 | char *buffer; |
| 44 | char *cp; |
| 45 | |
| 46 | buffer = strdup(portstring); |
| 47 | if ((cp = strchr(buffer, ':')) == NULL) |
Phil Oester | dbac8ad | 2006-07-20 17:01:54 +0000 | [diff] [blame] | 48 | ports[0] = ports[1] = parse_port(buffer, "tcp"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 49 | else { |
| 50 | *cp = '\0'; |
| 51 | cp++; |
| 52 | |
Phil Oester | dbac8ad | 2006-07-20 17:01:54 +0000 | [diff] [blame] | 53 | ports[0] = buffer[0] ? parse_port(buffer, "tcp") : 0; |
| 54 | ports[1] = cp[0] ? parse_port(cp, "tcp") : 0xFFFF; |
Harald Welte | d15fb34 | 2002-07-26 16:27:57 +0000 | [diff] [blame] | 55 | |
| 56 | if (ports[0] > ports[1]) |
| 57 | exit_error(PARAMETER_PROBLEM, |
| 58 | "invalid portrange (min > max)"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 59 | } |
| 60 | free(buffer); |
| 61 | } |
| 62 | |
| 63 | struct tcp_flag_names { |
| 64 | const char *name; |
| 65 | unsigned int flag; |
| 66 | }; |
| 67 | |
Jan Engelhardt | 0e2abed | 2007-10-04 16:25:58 +0000 | [diff] [blame] | 68 | static const struct tcp_flag_names tcp_flag_names[] |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 69 | = { { "FIN", 0x01 }, |
| 70 | { "SYN", 0x02 }, |
| 71 | { "RST", 0x04 }, |
| 72 | { "PSH", 0x08 }, |
| 73 | { "ACK", 0x10 }, |
| 74 | { "URG", 0x20 }, |
| 75 | { "ALL", 0x3F }, |
| 76 | { "NONE", 0 }, |
| 77 | }; |
| 78 | |
| 79 | static unsigned int |
| 80 | parse_tcp_flag(const char *flags) |
| 81 | { |
| 82 | unsigned int ret = 0; |
| 83 | char *ptr; |
| 84 | char *buffer; |
| 85 | |
| 86 | buffer = strdup(flags); |
| 87 | |
| 88 | for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) { |
| 89 | unsigned int i; |
| 90 | for (i = 0; |
| 91 | i < sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names); |
| 92 | i++) { |
| 93 | if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) { |
| 94 | ret |= tcp_flag_names[i].flag; |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | if (i == sizeof(tcp_flag_names)/sizeof(struct tcp_flag_names)) |
| 99 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | 2aa78fe | 2003-03-30 18:29:56 +0000 | [diff] [blame] | 100 | "Unknown TCP flag `%s'", ptr); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | free(buffer); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | static void |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 108 | parse_tcp_flags(struct xt_tcp *tcpinfo, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 109 | const char *mask, |
| 110 | const char *cmp, |
| 111 | int invert) |
| 112 | { |
| 113 | tcpinfo->flg_mask = parse_tcp_flag(mask); |
| 114 | tcpinfo->flg_cmp = parse_tcp_flag(cmp); |
| 115 | |
| 116 | if (invert) |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 117 | tcpinfo->invflags |= XT_TCP_INV_FLAGS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void |
| 121 | parse_tcp_option(const char *option, u_int8_t *result) |
| 122 | { |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 123 | unsigned int ret; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 124 | |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 125 | if (string_to_number(option, 1, 255, &ret) == -1) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 126 | exit_error(PARAMETER_PROBLEM, "Bad TCP option `%s'", option); |
| 127 | |
| 128 | *result = (u_int8_t)ret; |
| 129 | } |
| 130 | |
| 131 | /* Initialize the match. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 132 | static void tcp_init(struct xt_entry_match *m) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 133 | { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 134 | struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 135 | |
| 136 | tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF; |
| 137 | } |
| 138 | |
| 139 | #define TCP_SRC_PORTS 0x01 |
| 140 | #define TCP_DST_PORTS 0x02 |
| 141 | #define TCP_FLAGS 0x04 |
| 142 | #define TCP_OPTION 0x08 |
| 143 | |
| 144 | /* Function which parses command options; returns true if it |
| 145 | ate an option. */ |
| 146 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 147 | tcp_parse(int c, char **argv, int invert, unsigned int *flags, |
| 148 | const void *entry, struct xt_entry_match **match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 149 | { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 150 | struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 151 | |
| 152 | switch (c) { |
| 153 | case '1': |
| 154 | if (*flags & TCP_SRC_PORTS) |
| 155 | exit_error(PARAMETER_PROBLEM, |
| 156 | "Only one `--source-port' allowed"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 157 | check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 158 | parse_tcp_ports(argv[optind-1], tcpinfo->spts); |
| 159 | if (invert) |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 160 | tcpinfo->invflags |= XT_TCP_INV_SRCPT; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 161 | *flags |= TCP_SRC_PORTS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 162 | break; |
| 163 | |
| 164 | case '2': |
| 165 | if (*flags & TCP_DST_PORTS) |
| 166 | exit_error(PARAMETER_PROBLEM, |
| 167 | "Only one `--destination-port' allowed"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 168 | check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 169 | parse_tcp_ports(argv[optind-1], tcpinfo->dpts); |
| 170 | if (invert) |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 171 | tcpinfo->invflags |= XT_TCP_INV_DSTPT; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 172 | *flags |= TCP_DST_PORTS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 173 | break; |
| 174 | |
| 175 | case '3': |
| 176 | if (*flags & TCP_FLAGS) |
| 177 | exit_error(PARAMETER_PROBLEM, |
| 178 | "Only one of `--syn' or `--tcp-flags' " |
| 179 | " allowed"); |
Harald Welte | 38ed421 | 2005-05-04 07:34:37 +0000 | [diff] [blame] | 180 | parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 181 | *flags |= TCP_FLAGS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 182 | break; |
| 183 | |
| 184 | case '4': |
| 185 | if (*flags & TCP_FLAGS) |
| 186 | exit_error(PARAMETER_PROBLEM, |
| 187 | "Only one of `--syn' or `--tcp-flags' " |
| 188 | " allowed"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 189 | check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 190 | |
| 191 | if (!argv[optind] |
| 192 | || argv[optind][0] == '-' || argv[optind][0] == '!') |
| 193 | exit_error(PARAMETER_PROBLEM, |
| 194 | "--tcp-flags requires two args."); |
| 195 | |
Rusty Russell | fa9f9f9 | 2000-09-01 05:39:52 +0000 | [diff] [blame] | 196 | parse_tcp_flags(tcpinfo, argv[optind-1], argv[optind], |
Rusty Russell | 78001fe | 2000-09-01 05:22:10 +0000 | [diff] [blame] | 197 | invert); |
Rusty Russell | fa9f9f9 | 2000-09-01 05:39:52 +0000 | [diff] [blame] | 198 | optind++; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 199 | *flags |= TCP_FLAGS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 200 | break; |
| 201 | |
| 202 | case '5': |
| 203 | if (*flags & TCP_OPTION) |
| 204 | exit_error(PARAMETER_PROBLEM, |
| 205 | "Only one `--tcp-option' allowed"); |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 206 | check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 207 | parse_tcp_option(argv[optind-1], &tcpinfo->option); |
| 208 | if (invert) |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 209 | tcpinfo->invflags |= XT_TCP_INV_OPTION; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 210 | *flags |= TCP_OPTION; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 211 | break; |
| 212 | |
| 213 | default: |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | return 1; |
| 218 | } |
| 219 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 220 | static char * |
| 221 | port_to_service(int port) |
| 222 | { |
| 223 | struct servent *service; |
| 224 | |
| 225 | if ((service = getservbyport(htons(port), "tcp"))) |
| 226 | return service->s_name; |
| 227 | |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | static void |
| 232 | print_port(u_int16_t port, int numeric) |
| 233 | { |
| 234 | char *service; |
| 235 | |
| 236 | if (numeric || (service = port_to_service(port)) == NULL) |
| 237 | printf("%u", port); |
| 238 | else |
| 239 | printf("%s", service); |
| 240 | } |
| 241 | |
| 242 | static void |
| 243 | print_ports(const char *name, u_int16_t min, u_int16_t max, |
| 244 | int invert, int numeric) |
| 245 | { |
| 246 | const char *inv = invert ? "!" : ""; |
| 247 | |
| 248 | if (min != 0 || max != 0xFFFF || invert) { |
| 249 | printf("%s", name); |
| 250 | if (min == max) { |
| 251 | printf(":%s", inv); |
| 252 | print_port(min, numeric); |
| 253 | } else { |
| 254 | printf("s:%s", inv); |
| 255 | print_port(min, numeric); |
| 256 | printf(":"); |
| 257 | print_port(max, numeric); |
| 258 | } |
| 259 | printf(" "); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static void |
| 264 | print_option(u_int8_t option, int invert, int numeric) |
| 265 | { |
| 266 | if (option || invert) |
| 267 | printf("option=%s%u ", invert ? "!" : "", option); |
| 268 | } |
| 269 | |
| 270 | static void |
| 271 | print_tcpf(u_int8_t flags) |
| 272 | { |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 273 | int have_flag = 0; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 274 | |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 275 | while (flags) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 276 | unsigned int i; |
| 277 | |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 278 | for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 279 | |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 280 | if (have_flag) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 281 | printf(","); |
| 282 | printf("%s", tcp_flag_names[i].name); |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 283 | have_flag = 1; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 284 | |
| 285 | flags &= ~tcp_flag_names[i].flag; |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | if (!have_flag) |
| 289 | printf("NONE"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void |
| 293 | print_flags(u_int8_t mask, u_int8_t cmp, int invert, int numeric) |
| 294 | { |
| 295 | if (mask || invert) { |
| 296 | printf("flags:%s", invert ? "!" : ""); |
| 297 | if (numeric) |
Harald Welte | 0b4efea | 2001-04-12 15:50:49 +0000 | [diff] [blame] | 298 | printf("0x%02X/0x%02X ", mask, cmp); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 299 | else { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 300 | print_tcpf(mask); |
Rusty Russell | 3172807 | 2000-09-01 06:29:06 +0000 | [diff] [blame] | 301 | printf("/"); |
| 302 | print_tcpf(cmp); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 303 | printf(" "); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* Prints out the union ipt_matchinfo. */ |
| 309 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 310 | tcp_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 311 | { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 312 | const struct xt_tcp *tcp = (struct xt_tcp *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 313 | |
| 314 | printf("tcp "); |
| 315 | print_ports("spt", tcp->spts[0], tcp->spts[1], |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 316 | tcp->invflags & XT_TCP_INV_SRCPT, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 317 | numeric); |
| 318 | print_ports("dpt", tcp->dpts[0], tcp->dpts[1], |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 319 | tcp->invflags & XT_TCP_INV_DSTPT, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 320 | numeric); |
| 321 | print_option(tcp->option, |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 322 | tcp->invflags & XT_TCP_INV_OPTION, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 323 | numeric); |
| 324 | print_flags(tcp->flg_mask, tcp->flg_cmp, |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 325 | tcp->invflags & XT_TCP_INV_FLAGS, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 326 | numeric); |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 327 | if (tcp->invflags & ~XT_TCP_INV_MASK) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 328 | printf("Unknown invflags: 0x%X ", |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 329 | tcp->invflags & ~XT_TCP_INV_MASK); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 333 | static void tcp_save(const void *ip, const struct xt_entry_match *match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 334 | { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 335 | const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 336 | |
| 337 | if (tcpinfo->spts[0] != 0 |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 338 | || tcpinfo->spts[1] != 0xFFFF) { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 339 | if (tcpinfo->invflags & XT_TCP_INV_SRCPT) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 340 | printf("! "); |
| 341 | if (tcpinfo->spts[0] |
| 342 | != tcpinfo->spts[1]) |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 343 | printf("--sport %u:%u ", |
| 344 | tcpinfo->spts[0], |
| 345 | tcpinfo->spts[1]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 346 | else |
| 347 | printf("--sport %u ", |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 348 | tcpinfo->spts[0]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if (tcpinfo->dpts[0] != 0 |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 352 | || tcpinfo->dpts[1] != 0xFFFF) { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 353 | if (tcpinfo->invflags & XT_TCP_INV_DSTPT) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 354 | printf("! "); |
| 355 | if (tcpinfo->dpts[0] |
| 356 | != tcpinfo->dpts[1]) |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 357 | printf("--dport %u:%u ", |
| 358 | tcpinfo->dpts[0], |
| 359 | tcpinfo->dpts[1]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 360 | else |
| 361 | printf("--dport %u ", |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 362 | tcpinfo->dpts[0]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | if (tcpinfo->option |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 366 | || (tcpinfo->invflags & XT_TCP_INV_OPTION)) { |
| 367 | if (tcpinfo->invflags & XT_TCP_INV_OPTION) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 368 | printf("! "); |
| 369 | printf("--tcp-option %u ", tcpinfo->option); |
| 370 | } |
| 371 | |
| 372 | if (tcpinfo->flg_mask |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 373 | || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) { |
| 374 | if (tcpinfo->invflags & XT_TCP_INV_FLAGS) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 375 | printf("! "); |
Harald Welte | 67f23b2 | 2000-11-05 17:53:06 +0000 | [diff] [blame] | 376 | printf("--tcp-flags "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 377 | if (tcpinfo->flg_mask != 0xFF) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 378 | print_tcpf(tcpinfo->flg_mask); |
| 379 | } |
Harald Welte | 67f23b2 | 2000-11-05 17:53:06 +0000 | [diff] [blame] | 380 | printf(" "); |
| 381 | print_tcpf(tcpinfo->flg_cmp); |
| 382 | printf(" "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 386 | static struct xtables_match tcp_match = { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 387 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 388 | .name = "tcp", |
| 389 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 390 | .size = XT_ALIGN(sizeof(struct xt_tcp)), |
| 391 | .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 392 | .help = tcp_help, |
| 393 | .init = tcp_init, |
| 394 | .parse = tcp_parse, |
| 395 | .print = tcp_print, |
| 396 | .save = tcp_save, |
| 397 | .extra_opts = tcp_opts, |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 398 | }; |
| 399 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 400 | static struct xtables_match tcp_match6 = { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 401 | .family = AF_INET6, |
| 402 | .name = "tcp", |
| 403 | .version = IPTABLES_VERSION, |
| 404 | .size = XT_ALIGN(sizeof(struct xt_tcp)), |
| 405 | .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 406 | .help = tcp_help, |
| 407 | .init = tcp_init, |
| 408 | .parse = tcp_parse, |
| 409 | .print = tcp_print, |
| 410 | .save = tcp_save, |
| 411 | .extra_opts = tcp_opts, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 412 | }; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 413 | |
| 414 | void |
| 415 | _init(void) |
| 416 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame^] | 417 | xtables_register_match(&tcp_match); |
| 418 | xtables_register_match(&tcp_match6); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 419 | } |