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