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