blob: ac3c0ba3a014f91818dd44a856a0a715106697b3 [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"
Harald Welte80fe35d2002-05-29 13:08:15 +000019"\n", IPTABLES_VERSION);
Marc Bouchere6869a82000-03-20 06:03:29 +000020}
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':
Harald Welteb77f1da2002-03-14 11:35:58 +000078 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +000079
80 parse_states(argv[optind-1], sinfo);
81 if (invert)
82 sinfo->statemask = ~sinfo->statemask;
83 *flags = 1;
84 break;
85
86 default:
87 return 0;
88 }
89
90 return 1;
91}
92
93/* Final check; must have specified --state. */
94static void final_check(unsigned int flags)
95{
96 if (!flags)
97 exit_error(PARAMETER_PROBLEM, "You must specify `--state'");
98}
99
100static void print_state(unsigned int statemask)
101{
102 const char *sep = "";
103
104 if (statemask & IPT_STATE_INVALID) {
105 printf("%sINVALID", sep);
106 sep = ",";
107 }
108 if (statemask & IPT_STATE_BIT(IP_CT_NEW)) {
109 printf("%sNEW", sep);
110 sep = ",";
111 }
112 if (statemask & IPT_STATE_BIT(IP_CT_RELATED)) {
113 printf("%sRELATED", sep);
114 sep = ",";
115 }
116 if (statemask & IPT_STATE_BIT(IP_CT_ESTABLISHED)) {
117 printf("%sESTABLISHED", sep);
118 sep = ",";
119 }
120 printf(" ");
121}
122
123/* Prints out the matchinfo. */
124static void
125print(const struct ipt_ip *ip,
126 const struct ipt_entry_match *match,
127 int numeric)
128{
129 struct ipt_state_info *sinfo = (struct ipt_state_info *)match->data;
130
131 printf("state ");
132 print_state(sinfo->statemask);
133}
134
135/* Saves the matchinfo in parsable form to stdout. */
136static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
137{
138 struct ipt_state_info *sinfo = (struct ipt_state_info *)match->data;
139
140 printf("--state ");
141 print_state(sinfo->statemask);
142}
143
Harald Welte3efb6ea2001-08-06 18:50:21 +0000144static
Marc Bouchere6869a82000-03-20 06:03:29 +0000145struct iptables_match state
146= { NULL,
147 "state",
Harald Welte80fe35d2002-05-29 13:08:15 +0000148 IPTABLES_VERSION,
Rusty Russell73f72f52000-07-03 10:17:57 +0000149 IPT_ALIGN(sizeof(struct ipt_state_info)),
150 IPT_ALIGN(sizeof(struct ipt_state_info)),
Marc Bouchere6869a82000-03-20 06:03:29 +0000151 &help,
152 &init,
153 &parse,
154 &final_check,
155 &print,
156 &save,
157 opts
158};
159
160void _init(void)
161{
162 register_match(&state);
163}