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