1 /* Shared library add-on to iptables to add state tracking support. */
8 #include <linux/netfilter_ipv4/ip_conntrack.h>
9 #include <linux/netfilter_ipv4/ipt_state.h>
11 #ifndef IPT_STATE_UNTRACKED
12 #define IPT_STATE_UNTRACKED (1 << (IP_CT_NUMBER + 1))
15 /* Function which prints out usage message. */
20 "state v%s options:\n"
21 " [!] --state [INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED][,...]\n"
22 " State(s) to match\n"
23 "\n", IPTABLES_VERSION);
26 static struct option opts[] = {
27 { "state", 1, 0, '1' },
31 /* Initialize the match. */
33 init(struct ipt_entry_match *m, unsigned int *nfcache)
35 /* Can't cache this */
36 *nfcache |= NFC_UNKNOWN;
40 parse_state(const char *state, size_t strlen, struct ipt_state_info *sinfo)
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);
50 else if (strncasecmp(state, "UNTRACKED", strlen) == 0)
51 sinfo->statemask |= IPT_STATE_UNTRACKED;
58 parse_states(const char *arg, struct ipt_state_info *sinfo)
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);
68 if (strlen(arg) == 0 || !parse_state(arg, strlen(arg), sinfo))
69 exit_error(PARAMETER_PROBLEM, "Bad state `%s'", arg);
72 /* Function which parses command options; returns true if it
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)
80 struct ipt_state_info *sinfo = (struct ipt_state_info *)(*match)->data;
84 check_inverse(optarg, &invert, &optind, 0);
86 parse_states(argv[optind-1], sinfo);
88 sinfo->statemask = ~sinfo->statemask;
99 /* Final check; must have specified --state. */
100 static void final_check(unsigned int flags)
103 exit_error(PARAMETER_PROBLEM, "You must specify `--state'");
106 static void print_state(unsigned int statemask)
108 const char *sep = "";
110 if (statemask & IPT_STATE_INVALID) {
111 printf("%sINVALID", sep);
114 if (statemask & IPT_STATE_BIT(IP_CT_NEW)) {
115 printf("%sNEW", sep);
118 if (statemask & IPT_STATE_BIT(IP_CT_RELATED)) {
119 printf("%sRELATED", sep);
122 if (statemask & IPT_STATE_BIT(IP_CT_ESTABLISHED)) {
123 printf("%sESTABLISHED", sep);
126 if (statemask & IPT_STATE_UNTRACKED) {
127 printf("%sUNTRACKED", sep);
133 /* Prints out the matchinfo. */
135 print(const struct ipt_ip *ip,
136 const struct ipt_entry_match *match,
139 struct ipt_state_info *sinfo = (struct ipt_state_info *)match->data;
142 print_state(sinfo->statemask);
145 /* Saves the matchinfo in parsable form to stdout. */
146 static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
148 struct ipt_state_info *sinfo = (struct ipt_state_info *)match->data;
151 print_state(sinfo->statemask);
155 struct iptables_match state
159 IPT_ALIGN(sizeof(struct ipt_state_info)),
160 IPT_ALIGN(sizeof(struct ipt_state_info)),
172 register_match(&state);