iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libipt_state.c
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 #ifndef IPT_STATE_UNTRACKED
12 #define IPT_STATE_UNTRACKED (1 << (IP_CT_NUMBER + 1))
13 #endif
14
15 /* Function which prints out usage message. */
16 static void
17 help(void)
18 {
19         printf(
20 "state v%s options:\n"
21 " [!] --state [INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED][,...]\n"
22 "                               State(s) to match\n"
23 "\n", IPTABLES_VERSION);
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);
50         else if (strncasecmp(state, "UNTRACKED", strlen) == 0)
51                 sinfo->statemask |= IPT_STATE_UNTRACKED;
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':
84                 check_inverse(optarg, &invert, &optind, 0);
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         }
126         if (statemask & IPT_STATE_UNTRACKED) {
127                 printf("%sUNTRACKED", sep);
128                 sep = ",";
129         }
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
154 static
155 struct iptables_match state
156 = { NULL,
157     "state",
158     IPTABLES_VERSION,
159     IPT_ALIGN(sizeof(struct ipt_state_info)),
160     IPT_ALIGN(sizeof(struct ipt_state_info)),
161     &help,
162     &init,
163     &parse,
164     &final_check,
165     &print,
166     &save,
167     opts
168 };
169
170 void _init(void)
171 {
172         register_match(&state);
173 }