Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add state tracking support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 7 | #include <xtables.h> |
Patrick McHardy | 40d5475 | 2007-04-18 07:00:36 +0000 | [diff] [blame] | 8 | #include <linux/netfilter/nf_conntrack_common.h> |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 9 | #include <linux/netfilter/xt_state.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 10 | |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 11 | #ifndef XT_STATE_UNTRACKED |
| 12 | #define XT_STATE_UNTRACKED (1 << (IP_CT_NUMBER + 1)) |
Harald Welte | 4dc734c | 2003-10-07 18:55:13 +0000 | [diff] [blame] | 13 | #endif |
| 14 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 15 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 16 | state_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 17 | { |
| 18 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 19 | "state match options:\n" |
Harald Welte | 4dc734c | 2003-10-07 18:55:13 +0000 | [diff] [blame] | 20 | " [!] --state [INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED][,...]\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 21 | " State(s) to match\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 22 | } |
| 23 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 24 | static const struct option state_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 25 | { "state", 1, NULL, '1' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 26 | { .name = NULL } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 29 | static int |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 30 | state_parse_state(const char *state, size_t len, struct xt_state_info *sinfo) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 31 | { |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 32 | if (strncasecmp(state, "INVALID", len) == 0) |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 33 | sinfo->statemask |= XT_STATE_INVALID; |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 34 | else if (strncasecmp(state, "NEW", len) == 0) |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 35 | sinfo->statemask |= XT_STATE_BIT(IP_CT_NEW); |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 36 | else if (strncasecmp(state, "ESTABLISHED", len) == 0) |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 37 | sinfo->statemask |= XT_STATE_BIT(IP_CT_ESTABLISHED); |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 38 | else if (strncasecmp(state, "RELATED", len) == 0) |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 39 | sinfo->statemask |= XT_STATE_BIT(IP_CT_RELATED); |
Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 40 | else if (strncasecmp(state, "UNTRACKED", len) == 0) |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 41 | sinfo->statemask |= XT_STATE_UNTRACKED; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 42 | else |
| 43 | return 0; |
| 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 48 | state_parse_states(const char *arg, struct xt_state_info *sinfo) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 49 | { |
| 50 | const char *comma; |
| 51 | |
| 52 | while ((comma = strchr(arg, ',')) != NULL) { |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 53 | if (comma == arg || !state_parse_state(arg, comma-arg, sinfo)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 54 | xtables_error(PARAMETER_PROBLEM, "Bad state \"%s\"", arg); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 55 | arg = comma+1; |
| 56 | } |
Pablo Neira Ayuso | 0ec8c0f | 2008-11-19 19:01:26 +0100 | [diff] [blame] | 57 | if (!*arg) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 58 | xtables_error(PARAMETER_PROBLEM, "\"--state\" requires a list of " |
Pablo Neira Ayuso | 0ec8c0f | 2008-11-19 19:01:26 +0100 | [diff] [blame] | 59 | "states with no spaces, e.g. " |
| 60 | "ESTABLISHED,RELATED"); |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 61 | if (strlen(arg) == 0 || !state_parse_state(arg, strlen(arg), sinfo)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 62 | xtables_error(PARAMETER_PROBLEM, "Bad state \"%s\"", arg); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 65 | static int |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 66 | state_parse(int c, char **argv, int invert, unsigned int *flags, |
Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 67 | const void *entry, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 68 | struct xt_entry_match **match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 69 | { |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 70 | struct xt_state_info *sinfo = (struct xt_state_info *)(*match)->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 71 | |
| 72 | switch (c) { |
| 73 | case '1': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 74 | xtables_check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 75 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 76 | state_parse_states(argv[optind-1], sinfo); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 77 | if (invert) |
| 78 | sinfo->statemask = ~sinfo->statemask; |
| 79 | *flags = 1; |
| 80 | break; |
| 81 | |
| 82 | default: |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | return 1; |
| 87 | } |
| 88 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 89 | static void state_final_check(unsigned int flags) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 90 | { |
| 91 | if (!flags) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 92 | xtables_error(PARAMETER_PROBLEM, "You must specify \"--state\""); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 93 | } |
| 94 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 95 | static void state_print_state(unsigned int statemask) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 96 | { |
| 97 | const char *sep = ""; |
| 98 | |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 99 | if (statemask & XT_STATE_INVALID) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 100 | printf("%sINVALID", sep); |
| 101 | sep = ","; |
| 102 | } |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 103 | if (statemask & XT_STATE_BIT(IP_CT_NEW)) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 104 | printf("%sNEW", sep); |
| 105 | sep = ","; |
| 106 | } |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 107 | if (statemask & XT_STATE_BIT(IP_CT_RELATED)) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 108 | printf("%sRELATED", sep); |
| 109 | sep = ","; |
| 110 | } |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 111 | if (statemask & XT_STATE_BIT(IP_CT_ESTABLISHED)) { |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 112 | printf("%sESTABLISHED", sep); |
| 113 | sep = ","; |
| 114 | } |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 115 | if (statemask & XT_STATE_UNTRACKED) { |
Harald Welte | 4dc734c | 2003-10-07 18:55:13 +0000 | [diff] [blame] | 116 | printf("%sUNTRACKED", sep); |
| 117 | sep = ","; |
| 118 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 119 | printf(" "); |
| 120 | } |
| 121 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 122 | static void |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 123 | state_print(const void *ip, |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 124 | const struct xt_entry_match *match, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 125 | int numeric) |
| 126 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame^] | 127 | const struct xt_state_info *sinfo = (const void *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 128 | |
| 129 | printf("state "); |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 130 | state_print_state(sinfo->statemask); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 131 | } |
| 132 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 133 | static void state_save(const void *ip, const struct xt_entry_match *match) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 134 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame^] | 135 | const struct xt_state_info *sinfo = (const void *)match->data; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 136 | |
| 137 | printf("--state "); |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 138 | state_print_state(sinfo->statemask); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 139 | } |
| 140 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 141 | static struct xtables_match state_match = { |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 142 | .family = NFPROTO_IPV4, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 143 | .name = "state", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 144 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 145 | .size = XT_ALIGN(sizeof(struct xt_state_info)), |
| 146 | .userspacesize = XT_ALIGN(sizeof(struct xt_state_info)), |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 147 | .help = state_help, |
| 148 | .parse = state_parse, |
| 149 | .final_check = state_final_check, |
| 150 | .print = state_print, |
| 151 | .save = state_save, |
| 152 | .extra_opts = state_opts, |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 155 | static struct xtables_match state_match6 = { |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 156 | .family = NFPROTO_IPV6, |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 157 | .name = "state", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 158 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 1ff0b8d | 2007-08-04 08:09:51 +0000 | [diff] [blame] | 159 | .size = XT_ALIGN(sizeof(struct xt_state_info)), |
| 160 | .userspacesize = XT_ALIGN(sizeof(struct xt_state_info)), |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 161 | .help = state_help, |
| 162 | .parse = state_parse, |
| 163 | .final_check = state_final_check, |
| 164 | .print = state_print, |
| 165 | .save = state_save, |
| 166 | .extra_opts = state_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | void _init(void) |
| 170 | { |
László Attila Tóth | 7211888 | 2007-10-08 05:12:42 +0000 | [diff] [blame] | 171 | xtables_register_match(&state_match); |
| 172 | xtables_register_match(&state_match6); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 173 | } |