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