Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libxt_conntrack |
| 3 | * Shared library add-on to iptables for conntrack matching support. |
| 4 | * |
| 5 | * GPL (C) 2001 Marc Boucher (marc@mbsi.ca). |
| 6 | * Copyright © CC Computer Consultants GmbH, 2007 - 2008 |
| 7 | * Jan Engelhardt <jengelh@computergmbh.de> |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 8 | */ |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 9 | #include <sys/socket.h> |
| 10 | #include <sys/types.h> |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 11 | #include <ctype.h> |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 12 | #include <getopt.h> |
| 13 | #include <netdb.h> |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 14 | #include <stdbool.h> |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 18 | #include <xtables.h> |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 19 | #include <linux/netfilter.h> |
| 20 | #include <linux/netfilter/xt_conntrack.h> |
Patrick McHardy | 40d5475 | 2007-04-18 07:00:36 +0000 | [diff] [blame] | 21 | #include <linux/netfilter/nf_conntrack_common.h> |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 22 | #include <arpa/inet.h> |
Harald Welte | 4dc734c | 2003-10-07 18:55:13 +0000 | [diff] [blame] | 23 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 24 | static void conntrack_mt_help(void) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 25 | { |
| 26 | printf( |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 27 | "conntrack match options:\n" |
| 28 | "[!] --ctstate {INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED|SNAT|DNAT}[,...]\n" |
| 29 | " State(s) to match\n" |
| 30 | "[!] --ctproto proto Protocol to match; by number or name, e.g. \"tcp\"\n" |
| 31 | "[!] --ctorigsrc address[/mask]\n" |
| 32 | "[!] --ctorigdst address[/mask]\n" |
| 33 | "[!] --ctreplsrc address[/mask]\n" |
| 34 | "[!] --ctrepldst address[/mask]\n" |
| 35 | " Original/Reply source/destination address\n" |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 36 | "[!] --ctorigsrcport port\n" |
| 37 | "[!] --ctorigdstport port\n" |
| 38 | "[!] --ctreplsrcport port\n" |
| 39 | "[!] --ctrepldstport port\n" |
| 40 | " TCP/UDP/SCTP orig./reply source/destination port\n" |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 41 | "[!] --ctstatus {NONE|EXPECTED|SEEN_REPLY|ASSURED|CONFIRMED}[,...]\n" |
| 42 | " Status(es) to match\n" |
| 43 | "[!] --ctexpire time[:time] Match remaining lifetime in seconds against\n" |
| 44 | " value or range of values (inclusive)\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 45 | " --ctdir {ORIGINAL|REPLY} Flow direction of packet\n"); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 48 | static const struct option conntrack_mt_opts_v0[] = { |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 49 | {.name = "ctstate", .has_arg = true, .val = '1'}, |
| 50 | {.name = "ctproto", .has_arg = true, .val = '2'}, |
| 51 | {.name = "ctorigsrc", .has_arg = true, .val = '3'}, |
| 52 | {.name = "ctorigdst", .has_arg = true, .val = '4'}, |
| 53 | {.name = "ctreplsrc", .has_arg = true, .val = '5'}, |
| 54 | {.name = "ctrepldst", .has_arg = true, .val = '6'}, |
| 55 | {.name = "ctstatus", .has_arg = true, .val = '7'}, |
| 56 | {.name = "ctexpire", .has_arg = true, .val = '8'}, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 57 | { .name = NULL } |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 60 | static const struct option conntrack_mt_opts[] = { |
| 61 | {.name = "ctstate", .has_arg = true, .val = '1'}, |
| 62 | {.name = "ctproto", .has_arg = true, .val = '2'}, |
| 63 | {.name = "ctorigsrc", .has_arg = true, .val = '3'}, |
| 64 | {.name = "ctorigdst", .has_arg = true, .val = '4'}, |
| 65 | {.name = "ctreplsrc", .has_arg = true, .val = '5'}, |
| 66 | {.name = "ctrepldst", .has_arg = true, .val = '6'}, |
| 67 | {.name = "ctstatus", .has_arg = true, .val = '7'}, |
| 68 | {.name = "ctexpire", .has_arg = true, .val = '8'}, |
| 69 | {.name = "ctorigsrcport", .has_arg = true, .val = 'a'}, |
| 70 | {.name = "ctorigdstport", .has_arg = true, .val = 'b'}, |
| 71 | {.name = "ctreplsrcport", .has_arg = true, .val = 'c'}, |
| 72 | {.name = "ctrepldstport", .has_arg = true, .val = 'd'}, |
| 73 | {.name = "ctdir", .has_arg = true, .val = 'e'}, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 74 | {.name = NULL}, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 77 | static int |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 78 | parse_state(const char *state, size_t len, struct xt_conntrack_info *sinfo) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 79 | { |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 80 | if (strncasecmp(state, "INVALID", len) == 0) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 81 | sinfo->statemask |= XT_CONNTRACK_STATE_INVALID; |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 82 | else if (strncasecmp(state, "NEW", len) == 0) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 83 | sinfo->statemask |= XT_CONNTRACK_STATE_BIT(IP_CT_NEW); |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 84 | else if (strncasecmp(state, "ESTABLISHED", len) == 0) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 85 | sinfo->statemask |= XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED); |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 86 | else if (strncasecmp(state, "RELATED", len) == 0) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 87 | sinfo->statemask |= XT_CONNTRACK_STATE_BIT(IP_CT_RELATED); |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 88 | else if (strncasecmp(state, "UNTRACKED", len) == 0) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 89 | sinfo->statemask |= XT_CONNTRACK_STATE_UNTRACKED; |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 90 | else if (strncasecmp(state, "SNAT", len) == 0) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 91 | sinfo->statemask |= XT_CONNTRACK_STATE_SNAT; |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 92 | else if (strncasecmp(state, "DNAT", len) == 0) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 93 | sinfo->statemask |= XT_CONNTRACK_STATE_DNAT; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 94 | else |
| 95 | return 0; |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | static void |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 100 | parse_states(const char *arg, struct xt_conntrack_info *sinfo) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 101 | { |
| 102 | const char *comma; |
| 103 | |
| 104 | while ((comma = strchr(arg, ',')) != NULL) { |
| 105 | if (comma == arg || !parse_state(arg, comma-arg, sinfo)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 106 | xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 107 | arg = comma+1; |
| 108 | } |
Pablo Neira Ayuso | 0ec8c0f | 2008-11-19 19:01:26 +0100 | [diff] [blame] | 109 | if (!*arg) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 110 | xtables_error(PARAMETER_PROBLEM, "\"--ctstate\" requires a list of " |
Pablo Neira Ayuso | 0ec8c0f | 2008-11-19 19:01:26 +0100 | [diff] [blame] | 111 | "states with no spaces, e.g. " |
| 112 | "ESTABLISHED,RELATED"); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 113 | if (strlen(arg) == 0 || !parse_state(arg, strlen(arg), sinfo)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 114 | xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 117 | static bool |
| 118 | conntrack_ps_state(struct xt_conntrack_mtinfo1 *info, const char *state, |
| 119 | size_t z) |
| 120 | { |
| 121 | if (strncasecmp(state, "INVALID", z) == 0) |
| 122 | info->state_mask |= XT_CONNTRACK_STATE_INVALID; |
| 123 | else if (strncasecmp(state, "NEW", z) == 0) |
| 124 | info->state_mask |= XT_CONNTRACK_STATE_BIT(IP_CT_NEW); |
| 125 | else if (strncasecmp(state, "ESTABLISHED", z) == 0) |
| 126 | info->state_mask |= XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED); |
| 127 | else if (strncasecmp(state, "RELATED", z) == 0) |
| 128 | info->state_mask |= XT_CONNTRACK_STATE_BIT(IP_CT_RELATED); |
| 129 | else if (strncasecmp(state, "UNTRACKED", z) == 0) |
| 130 | info->state_mask |= XT_CONNTRACK_STATE_UNTRACKED; |
| 131 | else if (strncasecmp(state, "SNAT", z) == 0) |
| 132 | info->state_mask |= XT_CONNTRACK_STATE_SNAT; |
| 133 | else if (strncasecmp(state, "DNAT", z) == 0) |
| 134 | info->state_mask |= XT_CONNTRACK_STATE_DNAT; |
| 135 | else |
| 136 | return false; |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | conntrack_ps_states(struct xt_conntrack_mtinfo1 *info, const char *arg) |
| 142 | { |
| 143 | const char *comma; |
| 144 | |
| 145 | while ((comma = strchr(arg, ',')) != NULL) { |
| 146 | if (comma == arg || !conntrack_ps_state(info, arg, comma - arg)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 147 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 148 | "Bad ctstate \"%s\"", arg); |
| 149 | arg = comma + 1; |
| 150 | } |
| 151 | |
| 152 | if (strlen(arg) == 0 || !conntrack_ps_state(info, arg, strlen(arg))) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 153 | xtables_error(PARAMETER_PROBLEM, "Bad ctstate \"%s\"", arg); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 156 | static int |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 157 | parse_status(const char *status, size_t len, struct xt_conntrack_info *sinfo) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 158 | { |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 159 | if (strncasecmp(status, "NONE", len) == 0) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 160 | sinfo->statusmask |= 0; |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 161 | else if (strncasecmp(status, "EXPECTED", len) == 0) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 162 | sinfo->statusmask |= IPS_EXPECTED; |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 163 | else if (strncasecmp(status, "SEEN_REPLY", len) == 0) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 164 | sinfo->statusmask |= IPS_SEEN_REPLY; |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 165 | else if (strncasecmp(status, "ASSURED", len) == 0) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 166 | sinfo->statusmask |= IPS_ASSURED; |
Harald Welte | a643c3e | 2003-08-25 11:08:52 +0000 | [diff] [blame] | 167 | #ifdef IPS_CONFIRMED |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 168 | else if (strncasecmp(status, "CONFIRMED", len) == 0) |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 169 | sinfo->statusmask |= IPS_CONFIRMED; |
Harald Welte | a643c3e | 2003-08-25 11:08:52 +0000 | [diff] [blame] | 170 | #endif |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 171 | else |
| 172 | return 0; |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | static void |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 177 | parse_statuses(const char *arg, struct xt_conntrack_info *sinfo) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 178 | { |
| 179 | const char *comma; |
| 180 | |
| 181 | while ((comma = strchr(arg, ',')) != NULL) { |
| 182 | if (comma == arg || !parse_status(arg, comma-arg, sinfo)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 183 | xtables_error(PARAMETER_PROBLEM, "Bad ctstatus \"%s\"", arg); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 184 | arg = comma+1; |
| 185 | } |
| 186 | |
| 187 | if (strlen(arg) == 0 || !parse_status(arg, strlen(arg), sinfo)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 188 | xtables_error(PARAMETER_PROBLEM, "Bad ctstatus \"%s\"", arg); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 191 | static bool |
| 192 | conntrack_ps_status(struct xt_conntrack_mtinfo1 *info, const char *status, |
| 193 | size_t z) |
| 194 | { |
| 195 | if (strncasecmp(status, "NONE", z) == 0) |
| 196 | info->status_mask |= 0; |
| 197 | else if (strncasecmp(status, "EXPECTED", z) == 0) |
| 198 | info->status_mask |= IPS_EXPECTED; |
| 199 | else if (strncasecmp(status, "SEEN_REPLY", z) == 0) |
| 200 | info->status_mask |= IPS_SEEN_REPLY; |
| 201 | else if (strncasecmp(status, "ASSURED", z) == 0) |
| 202 | info->status_mask |= IPS_ASSURED; |
| 203 | else if (strncasecmp(status, "CONFIRMED", z) == 0) |
| 204 | info->status_mask |= IPS_CONFIRMED; |
| 205 | else |
| 206 | return false; |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | static void |
| 211 | conntrack_ps_statuses(struct xt_conntrack_mtinfo1 *info, const char *arg) |
| 212 | { |
| 213 | const char *comma; |
| 214 | |
| 215 | while ((comma = strchr(arg, ',')) != NULL) { |
| 216 | if (comma == arg || !conntrack_ps_status(info, arg, comma - arg)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 217 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 218 | "Bad ctstatus \"%s\"", arg); |
| 219 | arg = comma + 1; |
| 220 | } |
| 221 | |
| 222 | if (strlen(arg) == 0 || !conntrack_ps_status(info, arg, strlen(arg))) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 223 | xtables_error(PARAMETER_PROBLEM, "Bad ctstatus \"%s\"", arg); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 226 | static unsigned long |
| 227 | parse_expire(const char *s) |
| 228 | { |
| 229 | unsigned int len; |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 230 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 231 | if (!xtables_strtoui(s, NULL, &len, 0, UINT32_MAX)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 232 | xtables_error(PARAMETER_PROBLEM, "expire value invalid: \"%s\"\n", s); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 233 | else |
| 234 | return len; |
| 235 | } |
| 236 | |
| 237 | /* If a single value is provided, min and max are both set to the value */ |
| 238 | static void |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 239 | parse_expires(const char *s, struct xt_conntrack_info *sinfo) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 240 | { |
| 241 | char *buffer; |
| 242 | char *cp; |
| 243 | |
| 244 | buffer = strdup(s); |
| 245 | if ((cp = strchr(buffer, ':')) == NULL) |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 246 | sinfo->expires_min = sinfo->expires_max = |
| 247 | parse_expire(buffer); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 248 | else { |
| 249 | *cp = '\0'; |
| 250 | cp++; |
| 251 | |
| 252 | sinfo->expires_min = buffer[0] ? parse_expire(buffer) : 0; |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 253 | sinfo->expires_max = cp[0] |
| 254 | ? parse_expire(cp) |
| 255 | : (unsigned long)-1; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 256 | } |
| 257 | free(buffer); |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 258 | |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 259 | if (sinfo->expires_min > sinfo->expires_max) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 260 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 261 | "expire min. range value `%lu' greater than max. " |
| 262 | "range value `%lu'", sinfo->expires_min, sinfo->expires_max); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 265 | static void |
| 266 | conntrack_ps_expires(struct xt_conntrack_mtinfo1 *info, const char *s) |
| 267 | { |
| 268 | unsigned int min, max; |
| 269 | char *end; |
| 270 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 271 | if (!xtables_strtoui(s, &end, &min, 0, UINT32_MAX)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 272 | xtables_param_act(XTF_BAD_VALUE, "conntrack", "--expires", s); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 273 | max = min; |
| 274 | if (*end == ':') |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 275 | if (!xtables_strtoui(s, &end, &max, 0, UINT32_MAX)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 276 | xtables_param_act(XTF_BAD_VALUE, "conntrack", "--expires", s); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 277 | if (*end != '\0') |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 278 | xtables_param_act(XTF_BAD_VALUE, "conntrack", "--expires", s); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 279 | |
| 280 | if (min > max) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 281 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 282 | "expire min. range value \"%u\" greater than max. " |
| 283 | "range value \"%u\"", min, max); |
| 284 | |
| 285 | info->expires_min = min; |
| 286 | info->expires_max = max; |
| 287 | } |
| 288 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 289 | static int conntrack_parse(int c, char **argv, int invert, unsigned int *flags, |
| 290 | const void *entry, struct xt_entry_match **match) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 291 | { |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 292 | struct xt_conntrack_info *sinfo = (void *)(*match)->data; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 293 | char *protocol = NULL; |
| 294 | unsigned int naddrs = 0; |
| 295 | struct in_addr *addrs = NULL; |
| 296 | |
| 297 | |
| 298 | switch (c) { |
| 299 | case '1': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 300 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 301 | |
| 302 | parse_states(argv[optind-1], sinfo); |
| 303 | if (invert) { |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 304 | sinfo->invflags |= XT_CONNTRACK_STATE; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 305 | } |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 306 | sinfo->flags |= XT_CONNTRACK_STATE; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 307 | break; |
| 308 | |
| 309 | case '2': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 310 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 311 | |
| 312 | if(invert) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 313 | sinfo->invflags |= XT_CONNTRACK_PROTO; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 314 | |
| 315 | /* Canonicalize into lower case */ |
| 316 | for (protocol = argv[optind-1]; *protocol; protocol++) |
| 317 | *protocol = tolower(*protocol); |
| 318 | |
| 319 | protocol = argv[optind-1]; |
Jan Engelhardt | 1de7edf | 2009-01-30 05:38:11 +0100 | [diff] [blame] | 320 | sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum = |
| 321 | xtables_parse_protocol(protocol); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 322 | |
| 323 | if (sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum == 0 |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 324 | && (sinfo->invflags & XT_INV_PROTO)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 325 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 326 | "rule would never match protocol"); |
| 327 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 328 | sinfo->flags |= XT_CONNTRACK_PROTO; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 329 | break; |
| 330 | |
| 331 | case '3': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 332 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 333 | |
| 334 | if (invert) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 335 | sinfo->invflags |= XT_CONNTRACK_ORIGSRC; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 336 | |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 337 | xtables_ipparse_any(argv[optind-1], &addrs, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 338 | &sinfo->sipmsk[IP_CT_DIR_ORIGINAL], |
| 339 | &naddrs); |
| 340 | if(naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 341 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 342 | "multiple IP addresses not allowed"); |
| 343 | |
| 344 | if(naddrs == 1) { |
| 345 | sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip = addrs[0].s_addr; |
| 346 | } |
| 347 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 348 | sinfo->flags |= XT_CONNTRACK_ORIGSRC; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 349 | break; |
| 350 | |
| 351 | case '4': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 352 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 353 | |
| 354 | if (invert) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 355 | sinfo->invflags |= XT_CONNTRACK_ORIGDST; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 356 | |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 357 | xtables_ipparse_any(argv[optind-1], &addrs, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 358 | &sinfo->dipmsk[IP_CT_DIR_ORIGINAL], |
| 359 | &naddrs); |
| 360 | if(naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 361 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 362 | "multiple IP addresses not allowed"); |
| 363 | |
| 364 | if(naddrs == 1) { |
| 365 | sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip = addrs[0].s_addr; |
| 366 | } |
| 367 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 368 | sinfo->flags |= XT_CONNTRACK_ORIGDST; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 369 | break; |
| 370 | |
| 371 | case '5': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 372 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 373 | |
| 374 | if (invert) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 375 | sinfo->invflags |= XT_CONNTRACK_REPLSRC; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 376 | |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 377 | xtables_ipparse_any(argv[optind-1], &addrs, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 378 | &sinfo->sipmsk[IP_CT_DIR_REPLY], |
| 379 | &naddrs); |
| 380 | if(naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 381 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 382 | "multiple IP addresses not allowed"); |
| 383 | |
| 384 | if(naddrs == 1) { |
| 385 | sinfo->tuple[IP_CT_DIR_REPLY].src.ip = addrs[0].s_addr; |
| 386 | } |
| 387 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 388 | sinfo->flags |= XT_CONNTRACK_REPLSRC; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 389 | break; |
| 390 | |
| 391 | case '6': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 392 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 393 | |
| 394 | if (invert) |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 395 | sinfo->invflags |= XT_CONNTRACK_REPLDST; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 396 | |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 397 | xtables_ipparse_any(argv[optind-1], &addrs, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 398 | &sinfo->dipmsk[IP_CT_DIR_REPLY], |
| 399 | &naddrs); |
| 400 | if(naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 401 | xtables_error(PARAMETER_PROBLEM, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 402 | "multiple IP addresses not allowed"); |
| 403 | |
| 404 | if(naddrs == 1) { |
| 405 | sinfo->tuple[IP_CT_DIR_REPLY].dst.ip = addrs[0].s_addr; |
| 406 | } |
| 407 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 408 | sinfo->flags |= XT_CONNTRACK_REPLDST; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 409 | break; |
| 410 | |
| 411 | case '7': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 412 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 413 | |
| 414 | parse_statuses(argv[optind-1], sinfo); |
| 415 | if (invert) { |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 416 | sinfo->invflags |= XT_CONNTRACK_STATUS; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 417 | } |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 418 | sinfo->flags |= XT_CONNTRACK_STATUS; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 419 | break; |
| 420 | |
| 421 | case '8': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 422 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 423 | |
| 424 | parse_expires(argv[optind-1], sinfo); |
| 425 | if (invert) { |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 426 | sinfo->invflags |= XT_CONNTRACK_EXPIRES; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 427 | } |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 428 | sinfo->flags |= XT_CONNTRACK_EXPIRES; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 429 | break; |
| 430 | |
| 431 | default: |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | *flags = sinfo->flags; |
| 436 | return 1; |
| 437 | } |
| 438 | |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 439 | static int |
| 440 | conntrack_mt_parse(int c, char **argv, int invert, unsigned int *flags, |
| 441 | struct xt_entry_match **match) |
| 442 | { |
| 443 | struct xt_conntrack_mtinfo1 *info = (void *)(*match)->data; |
| 444 | unsigned int port; |
| 445 | char *p; |
| 446 | |
| 447 | switch (c) { |
| 448 | case '1': /* --ctstate */ |
| 449 | conntrack_ps_states(info, optarg); |
| 450 | info->match_flags |= XT_CONNTRACK_STATE; |
| 451 | if (invert) |
| 452 | info->invert_flags |= XT_CONNTRACK_STATE; |
| 453 | break; |
| 454 | |
| 455 | case '2': /* --ctproto */ |
| 456 | /* Canonicalize into lower case */ |
| 457 | for (p = optarg; *p != '\0'; ++p) |
| 458 | *p = tolower(*p); |
Jan Engelhardt | 1de7edf | 2009-01-30 05:38:11 +0100 | [diff] [blame] | 459 | info->l4proto = xtables_parse_protocol(optarg); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 460 | |
| 461 | if (info->l4proto == 0 && (info->invert_flags & XT_INV_PROTO)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 462 | xtables_error(PARAMETER_PROBLEM, "conntrack: rule would " |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 463 | "never match protocol"); |
| 464 | |
| 465 | info->match_flags |= XT_CONNTRACK_PROTO; |
| 466 | if (invert) |
| 467 | info->invert_flags |= XT_CONNTRACK_PROTO; |
| 468 | break; |
| 469 | |
| 470 | case '7': /* --ctstatus */ |
| 471 | conntrack_ps_statuses(info, optarg); |
| 472 | info->match_flags |= XT_CONNTRACK_STATUS; |
| 473 | if (invert) |
| 474 | info->invert_flags |= XT_CONNTRACK_STATUS; |
| 475 | break; |
| 476 | |
| 477 | case '8': /* --ctexpire */ |
| 478 | conntrack_ps_expires(info, optarg); |
| 479 | info->match_flags |= XT_CONNTRACK_EXPIRES; |
| 480 | if (invert) |
| 481 | info->invert_flags |= XT_CONNTRACK_EXPIRES; |
| 482 | break; |
| 483 | |
| 484 | case 'a': /* --ctorigsrcport */ |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 485 | if (!xtables_strtoui(optarg, NULL, &port, 0, UINT16_MAX)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 486 | xtables_param_act(XTF_BAD_VALUE, "conntrack", |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 487 | "--ctorigsrcport", optarg); |
| 488 | info->match_flags |= XT_CONNTRACK_ORIGSRC_PORT; |
| 489 | info->origsrc_port = htons(port); |
| 490 | if (invert) |
| 491 | info->invert_flags |= XT_CONNTRACK_ORIGSRC_PORT; |
| 492 | break; |
| 493 | |
| 494 | case 'b': /* --ctorigdstport */ |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 495 | if (!xtables_strtoui(optarg, NULL, &port, 0, UINT16_MAX)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 496 | xtables_param_act(XTF_BAD_VALUE, "conntrack", |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 497 | "--ctorigdstport", optarg); |
| 498 | info->match_flags |= XT_CONNTRACK_ORIGDST_PORT; |
| 499 | info->origdst_port = htons(port); |
| 500 | if (invert) |
| 501 | info->invert_flags |= XT_CONNTRACK_ORIGDST_PORT; |
| 502 | break; |
| 503 | |
| 504 | case 'c': /* --ctreplsrcport */ |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 505 | if (!xtables_strtoui(optarg, NULL, &port, 0, UINT16_MAX)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 506 | xtables_param_act(XTF_BAD_VALUE, "conntrack", |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 507 | "--ctreplsrcport", optarg); |
| 508 | info->match_flags |= XT_CONNTRACK_REPLSRC_PORT; |
| 509 | info->replsrc_port = htons(port); |
| 510 | if (invert) |
| 511 | info->invert_flags |= XT_CONNTRACK_REPLSRC_PORT; |
| 512 | break; |
| 513 | |
| 514 | case 'd': /* --ctrepldstport */ |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 515 | if (!xtables_strtoui(optarg, NULL, &port, 0, UINT16_MAX)) |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 516 | xtables_param_act(XTF_BAD_VALUE, "conntrack", |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 517 | "--ctrepldstport", optarg); |
| 518 | info->match_flags |= XT_CONNTRACK_REPLDST_PORT; |
| 519 | info->repldst_port = htons(port); |
| 520 | if (invert) |
| 521 | info->invert_flags |= XT_CONNTRACK_REPLDST_PORT; |
| 522 | break; |
| 523 | |
| 524 | case 'e': /* --ctdir */ |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 525 | xtables_param_act(XTF_NO_INVERT, "conntrack", "--ctdir", invert); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 526 | if (strcasecmp(optarg, "ORIGINAL") == 0) { |
| 527 | info->match_flags |= XT_CONNTRACK_DIRECTION; |
| 528 | info->invert_flags &= ~XT_CONNTRACK_DIRECTION; |
| 529 | } else if (strcasecmp(optarg, "REPLY") == 0) { |
| 530 | info->match_flags |= XT_CONNTRACK_DIRECTION; |
| 531 | info->invert_flags |= XT_CONNTRACK_DIRECTION; |
| 532 | } else { |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 533 | xtables_param_act(XTF_BAD_VALUE, "conntrack", "--ctdir", optarg); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 534 | } |
| 535 | break; |
| 536 | |
| 537 | default: |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | *flags = info->match_flags; |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | static int |
| 546 | conntrack_mt4_parse(int c, char **argv, int invert, unsigned int *flags, |
| 547 | const void *entry, struct xt_entry_match **match) |
| 548 | { |
| 549 | struct xt_conntrack_mtinfo1 *info = (void *)(*match)->data; |
| 550 | struct in_addr *addr = NULL; |
| 551 | unsigned int naddrs = 0; |
| 552 | |
| 553 | switch (c) { |
| 554 | case '3': /* --ctorigsrc */ |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 555 | xtables_ipparse_any(optarg, &addr, &info->origsrc_mask.in, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 556 | &naddrs); |
| 557 | if (naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 558 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 559 | "multiple IP addresses not allowed"); |
| 560 | if (naddrs == 1) |
| 561 | memcpy(&info->origsrc_addr.in, addr, sizeof(*addr)); |
| 562 | info->match_flags |= XT_CONNTRACK_ORIGSRC; |
| 563 | if (invert) |
| 564 | info->invert_flags |= XT_CONNTRACK_ORIGSRC; |
| 565 | break; |
| 566 | |
| 567 | case '4': /* --ctorigdst */ |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 568 | xtables_ipparse_any(optarg, &addr, &info->origdst_mask.in, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 569 | &naddrs); |
| 570 | if (naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 571 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 572 | "multiple IP addresses not allowed"); |
| 573 | if (naddrs == 1) |
| 574 | memcpy(&info->origdst_addr.in, addr, sizeof(*addr)); |
| 575 | info->match_flags |= XT_CONNTRACK_ORIGDST; |
| 576 | if (invert) |
| 577 | info->invert_flags |= XT_CONNTRACK_ORIGDST; |
| 578 | break; |
| 579 | |
| 580 | case '5': /* --ctreplsrc */ |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 581 | xtables_ipparse_any(optarg, &addr, &info->replsrc_mask.in, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 582 | &naddrs); |
| 583 | if (naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 584 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 585 | "multiple IP addresses not allowed"); |
| 586 | if (naddrs == 1) |
| 587 | memcpy(&info->replsrc_addr.in, addr, sizeof(*addr)); |
| 588 | info->match_flags |= XT_CONNTRACK_REPLSRC; |
| 589 | if (invert) |
| 590 | info->invert_flags |= XT_CONNTRACK_REPLSRC; |
| 591 | break; |
| 592 | |
| 593 | case '6': /* --ctrepldst */ |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 594 | xtables_ipparse_any(optarg, &addr, &info->repldst_mask.in, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 595 | &naddrs); |
| 596 | if (naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 597 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 598 | "multiple IP addresses not allowed"); |
| 599 | if (naddrs == 1) |
| 600 | memcpy(&info->repldst_addr.in, addr, sizeof(*addr)); |
| 601 | info->match_flags |= XT_CONNTRACK_REPLDST; |
| 602 | if (invert) |
| 603 | info->invert_flags |= XT_CONNTRACK_REPLDST; |
| 604 | break; |
| 605 | |
| 606 | |
| 607 | default: |
| 608 | return conntrack_mt_parse(c, argv, invert, flags, match); |
| 609 | } |
| 610 | |
| 611 | *flags = info->match_flags; |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | static int |
| 616 | conntrack_mt6_parse(int c, char **argv, int invert, unsigned int *flags, |
| 617 | const void *entry, struct xt_entry_match **match) |
| 618 | { |
| 619 | struct xt_conntrack_mtinfo1 *info = (void *)(*match)->data; |
| 620 | struct in6_addr *addr = NULL; |
| 621 | unsigned int naddrs = 0; |
| 622 | |
| 623 | switch (c) { |
| 624 | case '3': /* --ctorigsrc */ |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 625 | xtables_ip6parse_any(optarg, &addr, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 626 | &info->origsrc_mask.in6, &naddrs); |
| 627 | if (naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 628 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 629 | "multiple IP addresses not allowed"); |
| 630 | if (naddrs == 1) |
| 631 | memcpy(&info->origsrc_addr.in6, addr, sizeof(*addr)); |
| 632 | info->match_flags |= XT_CONNTRACK_ORIGSRC; |
| 633 | if (invert) |
| 634 | info->invert_flags |= XT_CONNTRACK_ORIGSRC; |
| 635 | break; |
| 636 | |
| 637 | case '4': /* --ctorigdst */ |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 638 | xtables_ip6parse_any(optarg, &addr, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 639 | &info->origdst_mask.in6, &naddrs); |
| 640 | if (naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 641 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 642 | "multiple IP addresses not allowed"); |
| 643 | if (naddrs == 1) |
| 644 | memcpy(&info->origdst_addr.in, addr, sizeof(*addr)); |
| 645 | info->match_flags |= XT_CONNTRACK_ORIGDST; |
| 646 | if (invert) |
| 647 | info->invert_flags |= XT_CONNTRACK_ORIGDST; |
| 648 | break; |
| 649 | |
| 650 | case '5': /* --ctreplsrc */ |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 651 | xtables_ip6parse_any(optarg, &addr, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 652 | &info->replsrc_mask.in6, &naddrs); |
| 653 | if (naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 654 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 655 | "multiple IP addresses not allowed"); |
| 656 | if (naddrs == 1) |
| 657 | memcpy(&info->replsrc_addr.in, addr, sizeof(*addr)); |
| 658 | info->match_flags |= XT_CONNTRACK_REPLSRC; |
| 659 | if (invert) |
| 660 | info->invert_flags |= XT_CONNTRACK_REPLSRC; |
| 661 | break; |
| 662 | |
| 663 | case '6': /* --ctrepldst */ |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 664 | xtables_ip6parse_any(optarg, &addr, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 665 | &info->repldst_mask.in6, &naddrs); |
| 666 | if (naddrs > 1) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 667 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 668 | "multiple IP addresses not allowed"); |
| 669 | if (naddrs == 1) |
| 670 | memcpy(&info->repldst_addr.in, addr, sizeof(*addr)); |
| 671 | info->match_flags |= XT_CONNTRACK_REPLDST; |
| 672 | if (invert) |
| 673 | info->invert_flags |= XT_CONNTRACK_REPLDST; |
| 674 | break; |
| 675 | |
| 676 | |
| 677 | default: |
| 678 | return conntrack_mt_parse(c, argv, invert, flags, match); |
| 679 | } |
| 680 | |
| 681 | *flags = info->match_flags; |
| 682 | return true; |
| 683 | } |
| 684 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 685 | static void conntrack_mt_check(unsigned int flags) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 686 | { |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 687 | if (flags == 0) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 688 | xtables_error(PARAMETER_PROBLEM, "conntrack: At least one option " |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 689 | "is required"); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | static void |
| 693 | print_state(unsigned int statemask) |
| 694 | { |
| 695 | const char *sep = ""; |
| 696 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 697 | if (statemask & XT_CONNTRACK_STATE_INVALID) { |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 698 | printf("%sINVALID", sep); |
| 699 | sep = ","; |
| 700 | } |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 701 | if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_NEW)) { |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 702 | printf("%sNEW", sep); |
| 703 | sep = ","; |
| 704 | } |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 705 | if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_RELATED)) { |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 706 | printf("%sRELATED", sep); |
| 707 | sep = ","; |
| 708 | } |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 709 | if (statemask & XT_CONNTRACK_STATE_BIT(IP_CT_ESTABLISHED)) { |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 710 | printf("%sESTABLISHED", sep); |
| 711 | sep = ","; |
| 712 | } |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 713 | if (statemask & XT_CONNTRACK_STATE_UNTRACKED) { |
Harald Welte | 4dc734c | 2003-10-07 18:55:13 +0000 | [diff] [blame] | 714 | printf("%sUNTRACKED", sep); |
| 715 | sep = ","; |
| 716 | } |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 717 | if (statemask & XT_CONNTRACK_STATE_SNAT) { |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 718 | printf("%sSNAT", sep); |
| 719 | sep = ","; |
| 720 | } |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 721 | if (statemask & XT_CONNTRACK_STATE_DNAT) { |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 722 | printf("%sDNAT", sep); |
| 723 | sep = ","; |
| 724 | } |
| 725 | printf(" "); |
| 726 | } |
| 727 | |
| 728 | static void |
| 729 | print_status(unsigned int statusmask) |
| 730 | { |
| 731 | const char *sep = ""; |
| 732 | |
| 733 | if (statusmask & IPS_EXPECTED) { |
| 734 | printf("%sEXPECTED", sep); |
| 735 | sep = ","; |
| 736 | } |
| 737 | if (statusmask & IPS_SEEN_REPLY) { |
| 738 | printf("%sSEEN_REPLY", sep); |
| 739 | sep = ","; |
| 740 | } |
| 741 | if (statusmask & IPS_ASSURED) { |
| 742 | printf("%sASSURED", sep); |
| 743 | sep = ","; |
| 744 | } |
Harald Welte | a643c3e | 2003-08-25 11:08:52 +0000 | [diff] [blame] | 745 | if (statusmask & IPS_CONFIRMED) { |
| 746 | printf("%sCONFIRMED", sep); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 747 | sep = ","; |
| 748 | } |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 749 | if (statusmask == 0) |
| 750 | printf("%sNONE", sep); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 751 | printf(" "); |
| 752 | } |
| 753 | |
| 754 | static void |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 755 | conntrack_dump_addr(const union nf_inet_addr *addr, |
| 756 | const union nf_inet_addr *mask, |
| 757 | unsigned int family, bool numeric) |
| 758 | { |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 759 | if (family == NFPROTO_IPV4) { |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 760 | if (!numeric && addr->ip == 0) { |
| 761 | printf("anywhere "); |
| 762 | return; |
| 763 | } |
Jan Engelhardt | 6b6c096 | 2008-11-10 17:08:07 +0100 | [diff] [blame] | 764 | if (numeric) |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 765 | printf("%s ", xtables_ipaddr_to_numeric(&addr->in)); |
Jan Engelhardt | 6b6c096 | 2008-11-10 17:08:07 +0100 | [diff] [blame] | 766 | else |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 767 | printf("%s ", xtables_ipaddr_to_anyname(&addr->in)); |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 768 | } else if (family == NFPROTO_IPV6) { |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 769 | if (!numeric && addr->ip6[0] == 0 && addr->ip6[1] == 0 && |
| 770 | addr->ip6[2] == 0 && addr->ip6[3] == 0) { |
| 771 | printf("anywhere "); |
| 772 | return; |
| 773 | } |
Jan Engelhardt | 6b6c096 | 2008-11-10 17:08:07 +0100 | [diff] [blame] | 774 | if (numeric) |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 775 | printf("%s ", xtables_ip6addr_to_numeric(&addr->in6)); |
Jan Engelhardt | 6b6c096 | 2008-11-10 17:08:07 +0100 | [diff] [blame] | 776 | else |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 777 | printf("%s ", xtables_ip6addr_to_anyname(&addr->in6)); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
| 781 | static void |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame^] | 782 | print_addr(const struct in_addr *addr, const struct in_addr *mask, |
| 783 | int inv, int numeric) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 784 | { |
| 785 | char buf[BUFSIZ]; |
| 786 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 787 | if (inv) |
| 788 | printf("! "); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 789 | |
| 790 | if (mask->s_addr == 0L && !numeric) |
| 791 | printf("%s ", "anywhere"); |
| 792 | else { |
| 793 | if (numeric) |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 794 | strcpy(buf, xtables_ipaddr_to_numeric(addr)); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 795 | else |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 796 | strcpy(buf, xtables_ipaddr_to_anyname(addr)); |
| 797 | strcat(buf, xtables_ipmask_to_numeric(mask)); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 798 | printf("%s ", buf); |
| 799 | } |
| 800 | } |
| 801 | |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 802 | static void |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 803 | matchinfo_print(const void *ip, const struct xt_entry_match *match, int numeric, const char *optpfx) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 804 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame^] | 805 | const struct xt_conntrack_info *sinfo = (const void *)match->data; |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 806 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 807 | if(sinfo->flags & XT_CONNTRACK_STATE) { |
| 808 | if (sinfo->invflags & XT_CONNTRACK_STATE) |
Michael Schwendt | dfba3ac | 2002-12-05 20:20:29 +0000 | [diff] [blame] | 809 | printf("! "); |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 810 | printf("%sctstate ", optpfx); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 811 | print_state(sinfo->statemask); |
| 812 | } |
| 813 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 814 | if(sinfo->flags & XT_CONNTRACK_PROTO) { |
| 815 | if (sinfo->invflags & XT_CONNTRACK_PROTO) |
Phil Oester | 5a4892b | 2005-11-17 13:34:51 +0000 | [diff] [blame] | 816 | printf("! "); |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 817 | printf("%sctproto ", optpfx); |
Phil Oester | 5a4892b | 2005-11-17 13:34:51 +0000 | [diff] [blame] | 818 | printf("%u ", sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum); |
| 819 | } |
| 820 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 821 | if(sinfo->flags & XT_CONNTRACK_ORIGSRC) { |
| 822 | if (sinfo->invflags & XT_CONNTRACK_ORIGSRC) |
| 823 | printf("! "); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 824 | printf("%sctorigsrc ", optpfx); |
| 825 | |
| 826 | print_addr( |
| 827 | (struct in_addr *)&sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip, |
| 828 | &sinfo->sipmsk[IP_CT_DIR_ORIGINAL], |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 829 | false, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 830 | numeric); |
| 831 | } |
| 832 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 833 | if(sinfo->flags & XT_CONNTRACK_ORIGDST) { |
| 834 | if (sinfo->invflags & XT_CONNTRACK_ORIGDST) |
| 835 | printf("! "); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 836 | printf("%sctorigdst ", optpfx); |
| 837 | |
| 838 | print_addr( |
| 839 | (struct in_addr *)&sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip, |
| 840 | &sinfo->dipmsk[IP_CT_DIR_ORIGINAL], |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 841 | false, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 842 | numeric); |
| 843 | } |
| 844 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 845 | if(sinfo->flags & XT_CONNTRACK_REPLSRC) { |
| 846 | if (sinfo->invflags & XT_CONNTRACK_REPLSRC) |
| 847 | printf("! "); |
Lutz Preßler | d0ae04e | 2003-03-04 14:50:50 +0000 | [diff] [blame] | 848 | printf("%sctreplsrc ", optpfx); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 849 | |
| 850 | print_addr( |
| 851 | (struct in_addr *)&sinfo->tuple[IP_CT_DIR_REPLY].src.ip, |
| 852 | &sinfo->sipmsk[IP_CT_DIR_REPLY], |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 853 | false, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 854 | numeric); |
| 855 | } |
| 856 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 857 | if(sinfo->flags & XT_CONNTRACK_REPLDST) { |
| 858 | if (sinfo->invflags & XT_CONNTRACK_REPLDST) |
| 859 | printf("! "); |
Lutz Preßler | d0ae04e | 2003-03-04 14:50:50 +0000 | [diff] [blame] | 860 | printf("%sctrepldst ", optpfx); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 861 | |
| 862 | print_addr( |
| 863 | (struct in_addr *)&sinfo->tuple[IP_CT_DIR_REPLY].dst.ip, |
| 864 | &sinfo->dipmsk[IP_CT_DIR_REPLY], |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 865 | false, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 866 | numeric); |
| 867 | } |
| 868 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 869 | if(sinfo->flags & XT_CONNTRACK_STATUS) { |
| 870 | if (sinfo->invflags & XT_CONNTRACK_STATUS) |
Michael Schwendt | dfba3ac | 2002-12-05 20:20:29 +0000 | [diff] [blame] | 871 | printf("! "); |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 872 | printf("%sctstatus ", optpfx); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 873 | print_status(sinfo->statusmask); |
| 874 | } |
| 875 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 876 | if(sinfo->flags & XT_CONNTRACK_EXPIRES) { |
| 877 | if (sinfo->invflags & XT_CONNTRACK_EXPIRES) |
Michael Schwendt | dfba3ac | 2002-12-05 20:20:29 +0000 | [diff] [blame] | 878 | printf("! "); |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 879 | printf("%sctexpire ", optpfx); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 880 | |
| 881 | if (sinfo->expires_max == sinfo->expires_min) |
| 882 | printf("%lu ", sinfo->expires_min); |
| 883 | else |
| 884 | printf("%lu:%lu ", sinfo->expires_min, sinfo->expires_max); |
| 885 | } |
Jan Engelhardt | c7fc1da | 2008-11-12 12:03:25 +0100 | [diff] [blame] | 886 | |
| 887 | if (sinfo->flags & XT_CONNTRACK_DIRECTION) { |
| 888 | if (sinfo->invflags & XT_CONNTRACK_DIRECTION) |
| 889 | printf("%sctdir REPLY", optpfx); |
| 890 | else |
| 891 | printf("%sctdir ORIGINAL", optpfx); |
| 892 | } |
| 893 | |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 896 | static void |
| 897 | conntrack_dump(const struct xt_conntrack_mtinfo1 *info, const char *prefix, |
| 898 | unsigned int family, bool numeric) |
| 899 | { |
| 900 | if (info->match_flags & XT_CONNTRACK_STATE) { |
| 901 | if (info->invert_flags & XT_CONNTRACK_STATE) |
| 902 | printf("! "); |
| 903 | printf("%sctstate ", prefix); |
| 904 | print_state(info->state_mask); |
| 905 | } |
| 906 | |
| 907 | if (info->match_flags & XT_CONNTRACK_PROTO) { |
| 908 | if (info->invert_flags & XT_CONNTRACK_PROTO) |
| 909 | printf("! "); |
| 910 | printf("%sctproto %u ", prefix, info->l4proto); |
| 911 | } |
| 912 | |
| 913 | if (info->match_flags & XT_CONNTRACK_ORIGSRC) { |
Jan Engelhardt | 093d5fc | 2009-04-05 00:05:30 +0200 | [diff] [blame] | 914 | if (info->invert_flags & XT_CONNTRACK_ORIGSRC) |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 915 | printf("! "); |
| 916 | printf("%sctorigsrc ", prefix); |
| 917 | conntrack_dump_addr(&info->origsrc_addr, &info->origsrc_mask, |
| 918 | family, numeric); |
| 919 | } |
| 920 | |
| 921 | if (info->match_flags & XT_CONNTRACK_ORIGDST) { |
Jan Engelhardt | 093d5fc | 2009-04-05 00:05:30 +0200 | [diff] [blame] | 922 | if (info->invert_flags & XT_CONNTRACK_ORIGDST) |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 923 | printf("! "); |
| 924 | printf("%sctorigdst ", prefix); |
| 925 | conntrack_dump_addr(&info->origdst_addr, &info->origdst_mask, |
| 926 | family, numeric); |
| 927 | } |
| 928 | |
| 929 | if (info->match_flags & XT_CONNTRACK_REPLSRC) { |
Jan Engelhardt | 093d5fc | 2009-04-05 00:05:30 +0200 | [diff] [blame] | 930 | if (info->invert_flags & XT_CONNTRACK_REPLSRC) |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 931 | printf("! "); |
| 932 | printf("%sctreplsrc ", prefix); |
| 933 | conntrack_dump_addr(&info->replsrc_addr, &info->replsrc_mask, |
| 934 | family, numeric); |
| 935 | } |
| 936 | |
| 937 | if (info->match_flags & XT_CONNTRACK_REPLDST) { |
Jan Engelhardt | 093d5fc | 2009-04-05 00:05:30 +0200 | [diff] [blame] | 938 | if (info->invert_flags & XT_CONNTRACK_REPLDST) |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 939 | printf("! "); |
| 940 | printf("%sctrepldst ", prefix); |
| 941 | conntrack_dump_addr(&info->repldst_addr, &info->repldst_mask, |
| 942 | family, numeric); |
| 943 | } |
| 944 | |
| 945 | if (info->match_flags & XT_CONNTRACK_ORIGSRC_PORT) { |
| 946 | if (info->invert_flags & XT_CONNTRACK_ORIGSRC_PORT) |
| 947 | printf("! "); |
| 948 | printf("%sctorigsrcport %u ", prefix, |
| 949 | ntohs(info->origsrc_port)); |
| 950 | } |
| 951 | |
| 952 | if (info->match_flags & XT_CONNTRACK_ORIGDST_PORT) { |
| 953 | if (info->invert_flags & XT_CONNTRACK_ORIGDST_PORT) |
| 954 | printf("! "); |
| 955 | printf("%sctorigdstport %u ", prefix, |
| 956 | ntohs(info->origdst_port)); |
| 957 | } |
| 958 | |
| 959 | if (info->match_flags & XT_CONNTRACK_REPLSRC_PORT) { |
| 960 | if (info->invert_flags & XT_CONNTRACK_REPLSRC_PORT) |
| 961 | printf("! "); |
| 962 | printf("%sctreplsrcport %u ", prefix, |
| 963 | ntohs(info->replsrc_port)); |
| 964 | } |
| 965 | |
| 966 | if (info->match_flags & XT_CONNTRACK_REPLDST_PORT) { |
| 967 | if (info->invert_flags & XT_CONNTRACK_REPLDST_PORT) |
| 968 | printf("! "); |
| 969 | printf("%sctrepldstport %u ", prefix, |
| 970 | ntohs(info->repldst_port)); |
| 971 | } |
| 972 | |
| 973 | if (info->match_flags & XT_CONNTRACK_STATUS) { |
| 974 | if (info->invert_flags & XT_CONNTRACK_STATUS) |
| 975 | printf("! "); |
| 976 | printf("%sctstatus ", prefix); |
| 977 | print_status(info->status_mask); |
| 978 | } |
| 979 | |
| 980 | if (info->match_flags & XT_CONNTRACK_EXPIRES) { |
| 981 | if (info->invert_flags & XT_CONNTRACK_EXPIRES) |
| 982 | printf("! "); |
| 983 | printf("%sctexpire ", prefix); |
| 984 | |
| 985 | if (info->expires_max == info->expires_min) |
| 986 | printf("%u ", (unsigned int)info->expires_min); |
| 987 | else |
| 988 | printf("%u:%u ", (unsigned int)info->expires_min, |
| 989 | (unsigned int)info->expires_max); |
| 990 | } |
Jan Engelhardt | c7fc1da | 2008-11-12 12:03:25 +0100 | [diff] [blame] | 991 | |
| 992 | if (info->match_flags & XT_CONNTRACK_DIRECTION) { |
| 993 | if (info->invert_flags & XT_CONNTRACK_DIRECTION) |
| 994 | printf("%sctdir REPLY", prefix); |
| 995 | else |
| 996 | printf("%sctdir ORIGINAL", prefix); |
| 997 | } |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 1000 | static void conntrack_print(const void *ip, const struct xt_entry_match *match, |
| 1001 | int numeric) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 1002 | { |
| 1003 | matchinfo_print(ip, match, numeric, ""); |
| 1004 | } |
| 1005 | |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1006 | static void |
| 1007 | conntrack_mt_print(const void *ip, const struct xt_entry_match *match, |
| 1008 | int numeric) |
| 1009 | { |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 1010 | conntrack_dump((const void *)match->data, "", NFPROTO_IPV4, numeric); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | static void |
| 1014 | conntrack_mt6_print(const void *ip, const struct xt_entry_match *match, |
| 1015 | int numeric) |
| 1016 | { |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 1017 | conntrack_dump((const void *)match->data, "", NFPROTO_IPV6, numeric); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Jan Engelhardt | 59d1640 | 2007-10-04 16:28:39 +0000 | [diff] [blame] | 1020 | static void conntrack_save(const void *ip, const struct xt_entry_match *match) |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 1021 | { |
Joszef Kadlecsik | db503f9 | 2004-05-05 10:10:33 +0000 | [diff] [blame] | 1022 | matchinfo_print(ip, match, 1, "--"); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1025 | static void conntrack_mt_save(const void *ip, |
| 1026 | const struct xt_entry_match *match) |
| 1027 | { |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 1028 | conntrack_dump((const void *)match->data, "--", NFPROTO_IPV4, true); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | static void conntrack_mt6_save(const void *ip, |
| 1032 | const struct xt_entry_match *match) |
| 1033 | { |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 1034 | conntrack_dump((const void *)match->data, "--", NFPROTO_IPV6, true); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 1037 | static struct xtables_match conntrack_match = { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 1038 | .version = XTABLES_VERSION, |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 1039 | .name = "conntrack", |
| 1040 | .revision = 0, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 1041 | .family = NFPROTO_IPV4, |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 1042 | .size = XT_ALIGN(sizeof(struct xt_conntrack_info)), |
| 1043 | .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_info)), |
| 1044 | .help = conntrack_mt_help, |
| 1045 | .parse = conntrack_parse, |
| 1046 | .final_check = conntrack_mt_check, |
| 1047 | .print = conntrack_print, |
| 1048 | .save = conntrack_save, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1049 | .extra_opts = conntrack_mt_opts_v0, |
| 1050 | }; |
| 1051 | |
| 1052 | static struct xtables_match conntrack_mt_reg = { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 1053 | .version = XTABLES_VERSION, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1054 | .name = "conntrack", |
| 1055 | .revision = 1, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 1056 | .family = NFPROTO_IPV4, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1057 | .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)), |
| 1058 | .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)), |
| 1059 | .help = conntrack_mt_help, |
| 1060 | .parse = conntrack_mt4_parse, |
| 1061 | .final_check = conntrack_mt_check, |
| 1062 | .print = conntrack_mt_print, |
| 1063 | .save = conntrack_mt_save, |
| 1064 | .extra_opts = conntrack_mt_opts, |
| 1065 | }; |
| 1066 | |
| 1067 | static struct xtables_match conntrack_mt6_reg = { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 1068 | .version = XTABLES_VERSION, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1069 | .name = "conntrack", |
| 1070 | .revision = 1, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 1071 | .family = NFPROTO_IPV6, |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1072 | .size = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)), |
| 1073 | .userspacesize = XT_ALIGN(sizeof(struct xt_conntrack_mtinfo1)), |
| 1074 | .help = conntrack_mt_help, |
| 1075 | .parse = conntrack_mt6_parse, |
| 1076 | .final_check = conntrack_mt_check, |
| 1077 | .print = conntrack_mt6_print, |
| 1078 | .save = conntrack_mt6_save, |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 1079 | .extra_opts = conntrack_mt_opts, |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 1080 | }; |
| 1081 | |
| 1082 | void _init(void) |
| 1083 | { |
Jan Engelhardt | a80b604 | 2008-01-20 13:34:07 +0000 | [diff] [blame] | 1084 | xtables_register_match(&conntrack_match); |
Jan Engelhardt | a8ad34c | 2008-01-29 13:37:21 +0000 | [diff] [blame] | 1085 | xtables_register_match(&conntrack_mt_reg); |
| 1086 | xtables_register_match(&conntrack_mt6_reg); |
Marc Boucher | 5054e85 | 2002-01-19 10:59:12 +0000 | [diff] [blame] | 1087 | } |