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