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> |
| 7 | #include <iptables.h> |
| 8 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
| 9 | #include <linux/netfilter_ipv4/ipt_state.h> |
| 10 | |
Harald Welte | 4dc734c | 2003-10-07 18:55:13 +0000 | [diff] [blame] | 11 | #ifndef IPT_STATE_UNTRACKED |
| 12 | #define IPT_STATE_UNTRACKED (1 << (IP_CT_NUMBER + 1)) |
| 13 | #endif |
| 14 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 15 | /* Function which prints out usage message. */ |
| 16 | static void |
| 17 | help(void) |
| 18 | { |
| 19 | printf( |
| 20 | "state v%s options:\n" |
Harald Welte | 4dc734c | 2003-10-07 18:55:13 +0000 | [diff] [blame] | 21 | " [!] --state [INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED][,...]\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 22 | " State(s) to match\n" |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 23 | "\n", IPTABLES_VERSION); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static struct option opts[] = { |
| 27 | { "state", 1, 0, '1' }, |
| 28 | {0} |
| 29 | }; |
| 30 | |
| 31 | /* Initialize the match. */ |
| 32 | static void |
| 33 | init(struct ipt_entry_match *m, unsigned int *nfcache) |
| 34 | { |
| 35 | /* Can't cache this */ |
| 36 | *nfcache |= NFC_UNKNOWN; |
| 37 | } |
| 38 | |
| 39 | static int |
| 40 | parse_state(const char *state, size_t strlen, struct ipt_state_info *sinfo) |
| 41 | { |
| 42 | if (strncasecmp(state, "INVALID", strlen) == 0) |
| 43 | sinfo->statemask |= IPT_STATE_INVALID; |
| 44 | else if (strncasecmp(state, "NEW", strlen) == 0) |
| 45 | sinfo->statemask |= IPT_STATE_BIT(IP_CT_NEW); |
| 46 | else if (strncasecmp(state, "ESTABLISHED", strlen) == 0) |
| 47 | sinfo->statemask |= IPT_STATE_BIT(IP_CT_ESTABLISHED); |
| 48 | else if (strncasecmp(state, "RELATED", strlen) == 0) |
| 49 | sinfo->statemask |= IPT_STATE_BIT(IP_CT_RELATED); |
Harald Welte | 4dc734c | 2003-10-07 18:55:13 +0000 | [diff] [blame] | 50 | else if (strncasecmp(state, "UNTRACKED", strlen) == 0) |
| 51 | sinfo->statemask |= IPT_STATE_UNTRACKED; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 52 | else |
| 53 | return 0; |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | static void |
| 58 | parse_states(const char *arg, struct ipt_state_info *sinfo) |
| 59 | { |
| 60 | const char *comma; |
| 61 | |
| 62 | while ((comma = strchr(arg, ',')) != NULL) { |
| 63 | if (comma == arg || !parse_state(arg, comma-arg, sinfo)) |
| 64 | exit_error(PARAMETER_PROBLEM, "Bad state `%s'", arg); |
| 65 | arg = comma+1; |
| 66 | } |
| 67 | |
| 68 | if (strlen(arg) == 0 || !parse_state(arg, strlen(arg), sinfo)) |
| 69 | exit_error(PARAMETER_PROBLEM, "Bad state `%s'", arg); |
| 70 | } |
| 71 | |
| 72 | /* Function which parses command options; returns true if it |
| 73 | ate an option */ |
| 74 | static int |
| 75 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 76 | const struct ipt_entry *entry, |
| 77 | unsigned int *nfcache, |
| 78 | struct ipt_entry_match **match) |
| 79 | { |
| 80 | struct ipt_state_info *sinfo = (struct ipt_state_info *)(*match)->data; |
| 81 | |
| 82 | switch (c) { |
| 83 | case '1': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 84 | check_inverse(optarg, &invert, &optind, 0); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 85 | |
| 86 | parse_states(argv[optind-1], sinfo); |
| 87 | if (invert) |
| 88 | sinfo->statemask = ~sinfo->statemask; |
| 89 | *flags = 1; |
| 90 | break; |
| 91 | |
| 92 | default: |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | /* Final check; must have specified --state. */ |
| 100 | static void final_check(unsigned int flags) |
| 101 | { |
| 102 | if (!flags) |
| 103 | exit_error(PARAMETER_PROBLEM, "You must specify `--state'"); |
| 104 | } |
| 105 | |
| 106 | static void print_state(unsigned int statemask) |
| 107 | { |
| 108 | const char *sep = ""; |
| 109 | |
| 110 | if (statemask & IPT_STATE_INVALID) { |
| 111 | printf("%sINVALID", sep); |
| 112 | sep = ","; |
| 113 | } |
| 114 | if (statemask & IPT_STATE_BIT(IP_CT_NEW)) { |
| 115 | printf("%sNEW", sep); |
| 116 | sep = ","; |
| 117 | } |
| 118 | if (statemask & IPT_STATE_BIT(IP_CT_RELATED)) { |
| 119 | printf("%sRELATED", sep); |
| 120 | sep = ","; |
| 121 | } |
| 122 | if (statemask & IPT_STATE_BIT(IP_CT_ESTABLISHED)) { |
| 123 | printf("%sESTABLISHED", sep); |
| 124 | sep = ","; |
| 125 | } |
Harald Welte | 4dc734c | 2003-10-07 18:55:13 +0000 | [diff] [blame] | 126 | if (statemask & IPT_STATE_UNTRACKED) { |
| 127 | printf("%sUNTRACKED", sep); |
| 128 | sep = ","; |
| 129 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 130 | printf(" "); |
| 131 | } |
| 132 | |
| 133 | /* Prints out the matchinfo. */ |
| 134 | static void |
| 135 | print(const struct ipt_ip *ip, |
| 136 | const struct ipt_entry_match *match, |
| 137 | int numeric) |
| 138 | { |
| 139 | struct ipt_state_info *sinfo = (struct ipt_state_info *)match->data; |
| 140 | |
| 141 | printf("state "); |
| 142 | print_state(sinfo->statemask); |
| 143 | } |
| 144 | |
| 145 | /* Saves the matchinfo in parsable form to stdout. */ |
| 146 | static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match) |
| 147 | { |
| 148 | struct ipt_state_info *sinfo = (struct ipt_state_info *)match->data; |
| 149 | |
| 150 | printf("--state "); |
| 151 | print_state(sinfo->statemask); |
| 152 | } |
| 153 | |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame^] | 154 | static struct iptables_match state = { |
| 155 | .next = NULL, |
| 156 | .name = "state", |
| 157 | .version = IPTABLES_VERSION, |
| 158 | .size = IPT_ALIGN(sizeof(struct ipt_state_info)), |
| 159 | .userspacesize = IPT_ALIGN(sizeof(struct ipt_state_info)), |
| 160 | .help = &help, |
| 161 | .init = &init, |
| 162 | .parse = &parse, |
| 163 | .final_check = &final_check, |
| 164 | .print = &print, |
| 165 | .save = &save, |
| 166 | .extra_opts = opts |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | void _init(void) |
| 170 | { |
| 171 | register_match(&state); |
| 172 | } |