1 #ifndef _IP_CONNTRACK_H
2 #define _IP_CONNTRACK_H
3 /* Connection state tracking for netfilter.  This is separated from,
4    but required by, the NAT layer; it can also be used by an iptables
5    extension. */
6 
7 #include <linux/config.h>
8 #include <linux/netfilter_ipv4/ip_conntrack_tuple.h>
9 #include <linux/bitops.h>
10 #include <asm/atomic.h>
11 
12 enum ip_conntrack_info
13 {
14 	/* Part of an established connection (either direction). */
15 	IP_CT_ESTABLISHED,
16 
17 	/* Like NEW, but related to an existing connection, or ICMP error
18 	   (in either direction). */
19 	IP_CT_RELATED,
20 
21 	/* Started a new connection to track (only
22            IP_CT_DIR_ORIGINAL); may be a retransmission. */
23 	IP_CT_NEW,
24 
25 	/* >= this indicates reply direction */
26 	IP_CT_IS_REPLY,
27 
28 	/* Number of distinct IP_CT types (no NEW in reply dirn). */
29 	IP_CT_NUMBER = IP_CT_IS_REPLY * 2 - 1
30 };
31 
32 /* Bitset representing status of connection. */
33 enum ip_conntrack_status {
34 	/* It's an expected connection: bit 0 set.  This bit never changed */
35 	IPS_EXPECTED_BIT = 0,
36 	IPS_EXPECTED = (1 << IPS_EXPECTED_BIT),
37 
38 	/* We've seen packets both ways: bit 1 set.  Can be set, not unset. */
39 	IPS_SEEN_REPLY_BIT = 1,
40 	IPS_SEEN_REPLY = (1 << IPS_SEEN_REPLY_BIT),
41 
42 	/* Conntrack should never be early-expired. */
43 	IPS_ASSURED_BIT = 2,
44 	IPS_ASSURED = (1 << IPS_ASSURED_BIT),
45 
46 	/* Connection is confirmed: originating packet has left box */
47 	IPS_CONFIRMED_BIT = 3,
48 	IPS_CONFIRMED = (1 << IPS_CONFIRMED_BIT),
49 };
50 
51 #include <linux/netfilter_ipv4/ip_conntrack_tcp.h>
52 #include <linux/netfilter_ipv4/ip_conntrack_icmp.h>
53 
54 /* per conntrack: protocol private data */
55 union ip_conntrack_proto {
56 	/* insert conntrack proto private data here */
57 	struct ip_ct_tcp tcp;
58 	struct ip_ct_icmp icmp;
59 };
60 
61 union ip_conntrack_expect_proto {
62 	/* insert expect proto private data here */
63 };
64 
65 /* Add protocol helper include file here */
66 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
67 
68 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
69 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
70 
71 /* per expectation: application helper private data */
72 union ip_conntrack_expect_help {
73 	/* insert conntrack helper private data (expect) here */
74 	struct ip_ct_amanda_expect exp_amanda_info;
75 	struct ip_ct_ftp_expect exp_ftp_info;
76 	struct ip_ct_irc_expect exp_irc_info;
77 
78 #ifdef CONFIG_IP_NF_NAT_NEEDED
79 	union {
80 		/* insert nat helper private data (expect) here */
81 	} nat;
82 #endif
83 };
84 
85 /* per conntrack: application helper private data */
86 union ip_conntrack_help {
87 	/* insert conntrack helper private data (master) here */
88 	struct ip_ct_ftp_master ct_ftp_info;
89 	struct ip_ct_irc_master ct_irc_info;
90 };
91 
92 #ifdef CONFIG_IP_NF_NAT_NEEDED
93 #include <linux/netfilter_ipv4/ip_nat.h>
94 
95 /* per conntrack: nat application helper private data */
96 union ip_conntrack_nat_help {
97 	/* insert nat helper private data here */
98 };
99 #endif
100 
101 #ifdef __KERNEL__
102 
103 #include <linux/types.h>
104 #include <linux/skbuff.h>
105 
106 #ifdef CONFIG_NETFILTER_DEBUG
107 #define IP_NF_ASSERT(x)							\
108 do {									\
109 	if (!(x))							\
110 		/* Wooah!  I'm tripping my conntrack in a frenzy of	\
111 		   netplay... */					\
112 		printk("NF_IP_ASSERT: %s:%i(%s)\n",			\
113 		       __FILE__, __LINE__, __FUNCTION__);		\
114 } while(0)
115 #else
116 #define IP_NF_ASSERT(x)
117 #endif
118 
119 struct ip_conntrack_expect
120 {
121 	/* Internal linked list (global expectation list) */
122 	struct list_head list;
123 
124 	/* reference count */
125 	atomic_t use;
126 
127 	/* expectation list for this master */
128 	struct list_head expected_list;
129 
130 	/* The conntrack of the master connection */
131 	struct ip_conntrack *expectant;
132 
133 	/* The conntrack of the sibling connection, set after
134 	 * expectation arrived */
135 	struct ip_conntrack *sibling;
136 
137 	/* Tuple saved for conntrack */
138 	struct ip_conntrack_tuple ct_tuple;
139 
140 	/* Timer function; deletes the expectation. */
141 	struct timer_list timeout;
142 
143 	/* Data filled out by the conntrack helpers follow: */
144 
145 	/* We expect this tuple, with the following mask */
146 	struct ip_conntrack_tuple tuple, mask;
147 
148 	/* Function to call after setup and insertion */
149 	int (*expectfn)(struct ip_conntrack *new);
150 
151 	/* At which sequence number did this expectation occur */
152 	u_int32_t seq;
153 
154 	union ip_conntrack_expect_proto proto;
155 
156 	union ip_conntrack_expect_help help;
157 };
158 
159 struct ip_conntrack_helper;
160 
161 struct ip_conntrack
162 {
163 	/* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
164            plus 1 for any connection(s) we are `master' for */
165 	struct nf_conntrack ct_general;
166 
167 	/* These are my tuples; original and reply */
168 	struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
169 
170 	/* Have we seen traffic both ways yet? (bitset) */
171 	unsigned long status;
172 
173 	/* Timer function; drops refcnt when it goes off. */
174 	struct timer_list timeout;
175 
176 	/* If we're expecting another related connection, this will be
177            in expected linked list */
178 	struct list_head sibling_list;
179 
180 	/* Current number of expected connections */
181 	unsigned int expecting;
182 
183 	/* If we were expected by an expectation, this will be it */
184 	struct ip_conntrack_expect *master;
185 
186 	/* Helper, if any. */
187 	struct ip_conntrack_helper *helper;
188 
189 	/* Our various nf_ct_info structs specify *what* relation this
190            packet has to the conntrack */
191 	struct nf_ct_info infos[IP_CT_NUMBER];
192 
193 	/* Storage reserved for other modules: */
194 
195 	union ip_conntrack_proto proto;
196 
197 	union ip_conntrack_help help;
198 
199 #ifdef CONFIG_IP_NF_NAT_NEEDED
200 	struct {
201 		struct ip_nat_info info;
202 		union ip_conntrack_nat_help help;
203 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
204 	defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
205 		int masq_index;
206 #endif
207 	} nat;
208 #endif /* CONFIG_IP_NF_NAT_NEEDED */
209 
210 };
211 
212 /* get master conntrack via master expectation */
213 #define master_ct(conntr) (conntr->master ? conntr->master->expectant : NULL)
214 
215 /* Alter reply tuple (maybe alter helper).  If it's already taken,
216    return 0 and don't do alteration. */
217 extern int
218 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
219 			 const struct ip_conntrack_tuple *newreply);
220 
221 /* Is this tuple taken? (ignoring any belonging to the given
222    conntrack). */
223 extern int
224 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
225 			 const struct ip_conntrack *ignored_conntrack);
226 
227 /* Return conntrack_info and tuple hash for given skb. */
228 extern struct ip_conntrack *
229 ip_conntrack_get(struct sk_buff *skb, enum ip_conntrack_info *ctinfo);
230 
231 /* decrement reference count on a conntrack */
232 extern void ip_conntrack_put(struct ip_conntrack *ct);
233 
234 /* find unconfirmed expectation based on tuple */
235 struct ip_conntrack_expect *
236 ip_conntrack_expect_find_get(const struct ip_conntrack_tuple *tuple);
237 
238 /* decrement reference count on an expectation */
239 void ip_conntrack_expect_put(struct ip_conntrack_expect *exp);
240 
241 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
242 			  const struct ip_conntrack_tuple *orig);
243 
244 /* Refresh conntrack for this many jiffies */
245 extern void ip_ct_refresh(struct ip_conntrack *ct,
246 			  unsigned long extra_jiffies);
247 
248 /* These are for NAT.  Icky. */
249 /* Call me when a conntrack is destroyed. */
250 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
251 
252 /* Returns new sk_buff, or NULL */
253 struct sk_buff *
254 ip_ct_gather_frags(struct sk_buff *skb, u_int32_t user);
255 
256 /* Iterate over all conntracks: if iter returns true, it's deleted. */
257 extern void
258 ip_ct_iterate_cleanup(int (*iter)(struct ip_conntrack *i, void *data),
259                       void *data);
260 
261 /* It's confirmed if it is, or has been in the hash table. */
is_confirmed(struct ip_conntrack * ct)262 static inline int is_confirmed(struct ip_conntrack *ct)
263 {
264 	return test_bit(IPS_CONFIRMED_BIT, &ct->status);
265 }
266 
267 extern unsigned int ip_conntrack_htable_size;
268 #endif /* __KERNEL__ */
269 #endif /* _IP_CONNTRACK_H */
270