Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add TCP support. */ |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 2 | #include <stdbool.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <netdb.h> |
| 5 | #include <string.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <getopt.h> |
Thomas Jarosch | ecae0c3 | 2008-10-23 15:41:27 +0200 | [diff] [blame] | 8 | #include <netinet/in.h> |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 9 | #include <xtables.h> |
| 10 | #include <linux/netfilter/xt_tcpudp.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 11 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 12 | static void tcp_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 13 | { |
| 14 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 15 | "tcp match options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 16 | "[!] --tcp-flags mask comp match when TCP flags & mask == comp\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 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" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 20 | "[!] --source-port port[:port]\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 21 | " --sport ...\n" |
| 22 | " match source port(s)\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 23 | "[!] --destination-port port[:port]\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 24 | " --dport ...\n" |
| 25 | " match destination port(s)\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 26 | "[!] --tcp-option number match if TCP option set\n"); |
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[] = { |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 30 | {.name = "source-port", .has_arg = true, .val = '1'}, |
| 31 | {.name = "sport", .has_arg = true, .val = '1'}, /* synonym */ |
| 32 | {.name = "destination-port", .has_arg = true, .val = '2'}, |
| 33 | {.name = "dport", .has_arg = true, .val = '2'}, /* synonym */ |
| 34 | {.name = "syn", .has_arg = false, .val = '3'}, |
| 35 | {.name = "tcp-flags", .has_arg = true, .val = '4'}, |
| 36 | {.name = "tcp-option", .has_arg = true, .val = '5'}, |
| 37 | XT_GETOPT_TABLEEND, |
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 |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 41 | parse_tcp_ports(const char *portstring, uint16_t *ports) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 42 | { |
| 43 | char *buffer; |
| 44 | char *cp; |
| 45 | |
| 46 | buffer = strdup(portstring); |
| 47 | if ((cp = strchr(buffer, ':')) == NULL) |
Jan Engelhardt | aae6be9 | 2009-01-30 04:24:47 +0100 | [diff] [blame] | 48 | ports[0] = ports[1] = xtables_parse_port(buffer, "tcp"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 49 | else { |
| 50 | *cp = '\0'; |
| 51 | cp++; |
| 52 | |
Jan Engelhardt | aae6be9 | 2009-01-30 04:24:47 +0100 | [diff] [blame] | 53 | ports[0] = buffer[0] ? xtables_parse_port(buffer, "tcp") : 0; |
| 54 | ports[1] = cp[0] ? xtables_parse_port(cp, "tcp") : 0xFFFF; |
Harald Welte | d15fb34 | 2002-07-26 16:27:57 +0000 | [diff] [blame] | 55 | |
| 56 | if (ports[0] > ports[1]) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 57 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | d15fb34 | 2002-07-26 16:27:57 +0000 | [diff] [blame] | 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; |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 90 | for (i = 0; i < ARRAY_SIZE(tcp_flag_names); ++i) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 91 | if (strcasecmp(tcp_flag_names[i].name, ptr) == 0) { |
| 92 | ret |= tcp_flag_names[i].flag; |
| 93 | break; |
| 94 | } |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 95 | if (i == ARRAY_SIZE(tcp_flag_names)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 96 | xtables_error(PARAMETER_PROBLEM, |
Harald Welte | 2aa78fe | 2003-03-30 18:29:56 +0000 | [diff] [blame] | 97 | "Unknown TCP flag `%s'", ptr); |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 98 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 99 | |
| 100 | free(buffer); |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static void |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 105 | parse_tcp_flags(struct xt_tcp *tcpinfo, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 106 | const char *mask, |
| 107 | const char *cmp, |
| 108 | int invert) |
| 109 | { |
| 110 | tcpinfo->flg_mask = parse_tcp_flag(mask); |
| 111 | tcpinfo->flg_cmp = parse_tcp_flag(cmp); |
| 112 | |
| 113 | if (invert) |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 114 | tcpinfo->invflags |= XT_TCP_INV_FLAGS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 118 | parse_tcp_option(const char *option, uint8_t *result) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 119 | { |
Harald Welte | b471976 | 2001-07-23 02:14:22 +0000 | [diff] [blame] | 120 | unsigned int ret; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 121 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 122 | if (!xtables_strtoui(option, NULL, &ret, 1, UINT8_MAX)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 123 | xtables_error(PARAMETER_PROBLEM, "Bad TCP option \"%s\"", option); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 124 | |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 125 | *result = ret; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 128 | static void tcp_init(struct xt_entry_match *m) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 129 | { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 130 | struct xt_tcp *tcpinfo = (struct xt_tcp *)m->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 131 | |
| 132 | tcpinfo->spts[1] = tcpinfo->dpts[1] = 0xFFFF; |
| 133 | } |
| 134 | |
| 135 | #define TCP_SRC_PORTS 0x01 |
| 136 | #define TCP_DST_PORTS 0x02 |
| 137 | #define TCP_FLAGS 0x04 |
| 138 | #define TCP_OPTION 0x08 |
| 139 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 140 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 141 | tcp_parse(int c, char **argv, int invert, unsigned int *flags, |
| 142 | const void *entry, struct xt_entry_match **match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 143 | { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 144 | struct xt_tcp *tcpinfo = (struct xt_tcp *)(*match)->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 145 | |
| 146 | switch (c) { |
| 147 | case '1': |
| 148 | if (*flags & TCP_SRC_PORTS) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 149 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 150 | "Only one `--source-port' allowed"); |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 151 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 152 | parse_tcp_ports(optarg, tcpinfo->spts); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 153 | if (invert) |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 154 | tcpinfo->invflags |= XT_TCP_INV_SRCPT; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 155 | *flags |= TCP_SRC_PORTS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 156 | break; |
| 157 | |
| 158 | case '2': |
| 159 | if (*flags & TCP_DST_PORTS) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 160 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 161 | "Only one `--destination-port' allowed"); |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 162 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 163 | parse_tcp_ports(optarg, tcpinfo->dpts); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 164 | if (invert) |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 165 | tcpinfo->invflags |= XT_TCP_INV_DSTPT; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 166 | *flags |= TCP_DST_PORTS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 167 | break; |
| 168 | |
| 169 | case '3': |
| 170 | if (*flags & TCP_FLAGS) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 171 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 172 | "Only one of `--syn' or `--tcp-flags' " |
| 173 | " allowed"); |
Harald Welte | 38ed421 | 2005-05-04 07:34:37 +0000 | [diff] [blame] | 174 | parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 175 | *flags |= TCP_FLAGS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 176 | break; |
| 177 | |
| 178 | case '4': |
| 179 | if (*flags & TCP_FLAGS) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 180 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 181 | "Only one of `--syn' or `--tcp-flags' " |
| 182 | " allowed"); |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 183 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 184 | |
| 185 | if (!argv[optind] |
| 186 | || argv[optind][0] == '-' || argv[optind][0] == '!') |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 187 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 188 | "--tcp-flags requires two args."); |
| 189 | |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 190 | parse_tcp_flags(tcpinfo, optarg, argv[optind], |
Rusty Russell | 78001fe | 2000-09-01 05:22:10 +0000 | [diff] [blame] | 191 | invert); |
Rusty Russell | fa9f9f9 | 2000-09-01 05:39:52 +0000 | [diff] [blame] | 192 | optind++; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 193 | *flags |= TCP_FLAGS; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 194 | break; |
| 195 | |
| 196 | case '5': |
| 197 | if (*flags & TCP_OPTION) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 198 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 199 | "Only one `--tcp-option' allowed"); |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 200 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 201 | parse_tcp_option(optarg, &tcpinfo->option); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 202 | if (invert) |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 203 | tcpinfo->invflags |= XT_TCP_INV_OPTION; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 204 | *flags |= TCP_OPTION; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 205 | break; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return 1; |
| 209 | } |
| 210 | |
Jan Engelhardt | dd6e4b9 | 2011-05-07 00:05:24 +0200 | [diff] [blame] | 211 | static const char * |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 212 | port_to_service(int port) |
| 213 | { |
Jan Engelhardt | dd6e4b9 | 2011-05-07 00:05:24 +0200 | [diff] [blame] | 214 | const struct servent *service; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 215 | |
| 216 | if ((service = getservbyport(htons(port), "tcp"))) |
| 217 | return service->s_name; |
| 218 | |
| 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 223 | print_port(uint16_t port, int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 224 | { |
Jan Engelhardt | dd6e4b9 | 2011-05-07 00:05:24 +0200 | [diff] [blame] | 225 | const char *service; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 226 | |
| 227 | if (numeric || (service = port_to_service(port)) == NULL) |
| 228 | printf("%u", port); |
| 229 | else |
| 230 | printf("%s", service); |
| 231 | } |
| 232 | |
| 233 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 234 | print_ports(const char *name, uint16_t min, uint16_t max, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 235 | int invert, int numeric) |
| 236 | { |
| 237 | const char *inv = invert ? "!" : ""; |
| 238 | |
| 239 | if (min != 0 || max != 0xFFFF || invert) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 240 | printf(" %s", name); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 241 | if (min == max) { |
| 242 | printf(":%s", inv); |
| 243 | print_port(min, numeric); |
| 244 | } else { |
| 245 | printf("s:%s", inv); |
| 246 | print_port(min, numeric); |
| 247 | printf(":"); |
| 248 | print_port(max, numeric); |
| 249 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 254 | print_option(uint8_t option, int invert, int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 255 | { |
| 256 | if (option || invert) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 257 | printf(" option=%s%u", invert ? "!" : "", option); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 261 | print_tcpf(uint8_t flags) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 262 | { |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 263 | int have_flag = 0; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 264 | |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 265 | while (flags) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 266 | unsigned int i; |
| 267 | |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 268 | for (i = 0; (flags & tcp_flag_names[i].flag) == 0; i++); |
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 | if (have_flag) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 271 | printf(","); |
| 272 | printf("%s", tcp_flag_names[i].name); |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 273 | have_flag = 1; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 274 | |
| 275 | flags &= ~tcp_flag_names[i].flag; |
Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if (!have_flag) |
| 279 | printf("NONE"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 283 | print_flags(uint8_t mask, uint8_t cmp, int invert, int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 284 | { |
| 285 | if (mask || invert) { |
| 286 | printf("flags:%s", invert ? "!" : ""); |
| 287 | if (numeric) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 288 | printf(" 0x%02X/0x%02X", mask, cmp); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 289 | else { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 290 | printf(" "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 291 | print_tcpf(mask); |
Rusty Russell | 3172807 | 2000-09-01 06:29:06 +0000 | [diff] [blame] | 292 | printf("/"); |
| 293 | print_tcpf(cmp); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 298 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 299 | 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] | 300 | { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 301 | const struct xt_tcp *tcp = (struct xt_tcp *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 302 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 303 | printf(" tcp"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 304 | print_ports("spt", tcp->spts[0], tcp->spts[1], |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 305 | tcp->invflags & XT_TCP_INV_SRCPT, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 306 | numeric); |
| 307 | print_ports("dpt", tcp->dpts[0], tcp->dpts[1], |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 308 | tcp->invflags & XT_TCP_INV_DSTPT, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 309 | numeric); |
| 310 | print_option(tcp->option, |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 311 | tcp->invflags & XT_TCP_INV_OPTION, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 312 | numeric); |
| 313 | print_flags(tcp->flg_mask, tcp->flg_cmp, |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 314 | tcp->invflags & XT_TCP_INV_FLAGS, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 315 | numeric); |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 316 | if (tcp->invflags & ~XT_TCP_INV_MASK) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 317 | printf(" Unknown invflags: 0x%X", |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 318 | tcp->invflags & ~XT_TCP_INV_MASK); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 321 | 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] | 322 | { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 323 | const struct xt_tcp *tcpinfo = (struct xt_tcp *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 324 | |
| 325 | if (tcpinfo->spts[0] != 0 |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 326 | || tcpinfo->spts[1] != 0xFFFF) { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 327 | if (tcpinfo->invflags & XT_TCP_INV_SRCPT) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 328 | printf(" !"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 329 | if (tcpinfo->spts[0] |
| 330 | != tcpinfo->spts[1]) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 331 | printf(" --sport %u:%u", |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 332 | tcpinfo->spts[0], |
| 333 | tcpinfo->spts[1]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 334 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 335 | printf(" --sport %u", |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 336 | tcpinfo->spts[0]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | if (tcpinfo->dpts[0] != 0 |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 340 | || tcpinfo->dpts[1] != 0xFFFF) { |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 341 | if (tcpinfo->invflags & XT_TCP_INV_DSTPT) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 342 | printf(" !"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 343 | if (tcpinfo->dpts[0] |
| 344 | != tcpinfo->dpts[1]) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 345 | printf(" --dport %u:%u", |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 346 | tcpinfo->dpts[0], |
| 347 | tcpinfo->dpts[1]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 348 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 349 | printf(" --dport %u", |
Marc Boucher | 9f2009c | 2000-04-07 17:30:28 +0000 | [diff] [blame] | 350 | tcpinfo->dpts[0]); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | if (tcpinfo->option |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 354 | || (tcpinfo->invflags & XT_TCP_INV_OPTION)) { |
| 355 | if (tcpinfo->invflags & XT_TCP_INV_OPTION) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 356 | printf(" !"); |
| 357 | printf(" --tcp-option %u", tcpinfo->option); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | if (tcpinfo->flg_mask |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 361 | || (tcpinfo->invflags & XT_TCP_INV_FLAGS)) { |
| 362 | if (tcpinfo->invflags & XT_TCP_INV_FLAGS) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 363 | printf(" !"); |
| 364 | printf(" --tcp-flags "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 365 | if (tcpinfo->flg_mask != 0xFF) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 366 | print_tcpf(tcpinfo->flg_mask); |
| 367 | } |
Harald Welte | 67f23b2 | 2000-11-05 17:53:06 +0000 | [diff] [blame] | 368 | printf(" "); |
| 369 | print_tcpf(tcpinfo->flg_cmp); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 373 | static struct xtables_match tcp_match = { |
Jan Engelhardt | c5e8573 | 2009-06-12 20:55:44 +0200 | [diff] [blame] | 374 | .family = NFPROTO_UNSPEC, |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 375 | .name = "tcp", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 376 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 95f186e | 2007-07-24 06:59:00 +0000 | [diff] [blame] | 377 | .size = XT_ALIGN(sizeof(struct xt_tcp)), |
| 378 | .userspacesize = XT_ALIGN(sizeof(struct xt_tcp)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 379 | .help = tcp_help, |
| 380 | .init = tcp_init, |
| 381 | .parse = tcp_parse, |
| 382 | .print = tcp_print, |
| 383 | .save = tcp_save, |
| 384 | .extra_opts = tcp_opts, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 385 | }; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 386 | |
| 387 | void |
| 388 | _init(void) |
| 389 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 390 | xtables_register_match(&tcp_match); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 391 | } |