blob: 8e6a90c566af5b29ec83da2e78efbe30fcd661f1 [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
Harald Welte4dc734c2003-10-07 18:55:13 +000011#ifndef IPT_STATE_UNTRACKED
12#define IPT_STATE_UNTRACKED (1 << (IP_CT_NUMBER + 1))
13#endif
14
Marc Bouchere6869a82000-03-20 06:03:29 +000015/* Function which prints out usage message. */
16static void
17help(void)
18{
19 printf(
20"state v%s options:\n"
Harald Welte4dc734c2003-10-07 18:55:13 +000021" [!] --state [INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED][,...]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000022" State(s) to match\n"
Harald Welte80fe35d2002-05-29 13:08:15 +000023"\n", IPTABLES_VERSION);
Marc Bouchere6869a82000-03-20 06:03:29 +000024}
25
26static struct option opts[] = {
27 { "state", 1, 0, '1' },
28 {0}
29};
30
31/* Initialize the match. */
32static void
33init(struct ipt_entry_match *m, unsigned int *nfcache)
34{
35 /* Can't cache this */
36 *nfcache |= NFC_UNKNOWN;
37}
38
39static int
40parse_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);
Harald Welte4dc734c2003-10-07 18:55:13 +000050 else if (strncasecmp(state, "UNTRACKED", strlen) == 0)
51 sinfo->statemask |= IPT_STATE_UNTRACKED;
Marc Bouchere6869a82000-03-20 06:03:29 +000052 else
53 return 0;
54 return 1;
55}
56
57static void
58parse_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 */
74static int
75parse(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':
Harald Welteb77f1da2002-03-14 11:35:58 +000084 check_inverse(optarg, &invert, &optind, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +000085
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. */
100static void final_check(unsigned int flags)
101{
102 if (!flags)
103 exit_error(PARAMETER_PROBLEM, "You must specify `--state'");
104}
105
106static 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 }
Harald Welte4dc734c2003-10-07 18:55:13 +0000126 if (statemask & IPT_STATE_UNTRACKED) {
127 printf("%sUNTRACKED", sep);
128 sep = ",";
129 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000130 printf(" ");
131}
132
133/* Prints out the matchinfo. */
134static void
135print(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. */
146static 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
Pablo Neira8caee8b2004-12-28 13:11:59 +0000154static struct iptables_match state = {
155 .next = NULL,
156 .name = "state",
157 .version = IPTABLES_VERSION,
158 .size = IPT_ALIGN(sizeof(struct ipt_state_info)),
159 .userspacesize = IPT_ALIGN(sizeof(struct ipt_state_info)),
160 .help = &help,
161 .init = &init,
162 .parse = &parse,
163 .final_check = &final_check,
164 .print = &print,
165 .save = &save,
166 .extra_opts = opts
Marc Bouchere6869a82000-03-20 06:03:29 +0000167};
168
169void _init(void)
170{
171 register_match(&state);
172}