blob: 19751d72fb0ba58fa4e4b787ad9698a5843e3f8c [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* 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. */
12static void
13help(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
22static struct option opts[] = {
23 { "state", 1, 0, '1' },
24 {0}
25};
26
27/* Initialize the match. */
28static void
29init(struct ipt_entry_match *m, unsigned int *nfcache)
30{
31 /* Can't cache this */
32 *nfcache |= NFC_UNKNOWN;
33}
34
35static int
36parse_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
51static void
52parse_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 */
68static int
69parse(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. */
95static void final_check(unsigned int flags)
96{
97 if (!flags)
98 exit_error(PARAMETER_PROBLEM, "You must specify `--state'");
99}
100
101static 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. */
125static void
126print(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. */
137static 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
145struct iptables_match state
146= { NULL,
147 "state",
148 NETFILTER_VERSION,
149 sizeof(struct ipt_state_info),
150 &help,
151 &init,
152 &parse,
153 &final_check,
154 &print,
155 &save,
156 opts
157};
158
159void _init(void)
160{
161 register_match(&state);
162}