summaryrefslogtreecommitdiffstats
path: root/include/internal/object.h
blob: 8d95aa1e695badaa9720190189f6d66db8a00ac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * WARNING: Do *NOT* ever include this file, only for internal use!
 * 	    Use the set/get API in order to set/get the conntrack attributes
 */

#ifndef _NFCT_OBJECT_H_
#define _NFCT_OBJECT_H_

/*
 * nfct callback handler object
 */

struct nfct_handle {
	struct nfnl_handle		*nfnlh;
	struct nfnl_subsys_handle	*nfnlssh_ct;
	struct nfnl_subsys_handle	*nfnlssh_exp;

	/* callback handler for the new API */
	struct nfnl_callback		nfnl_cb_ct;
	struct nfnl_callback		nfnl_cb_exp;

	int			(*cb)(enum nf_conntrack_msg_type type, 
				      struct nf_conntrack *ct,
				      void *data);

	/* This is the second version of the callback that includes
	 * the Netlink header. This is the result of an early design
	 * error, hiding Netlink details is evil. You end needing some
	 * internal information at some point like the Netlink PortID. */
	int			(*cb2)(const struct nlmsghdr *nlh,
				       enum nf_conntrack_msg_type type,
				       struct nf_conntrack *ct,
				       void *data);

	int			(*expect_cb)(enum nf_conntrack_msg_type type, 
					     struct nf_expect *exp,
					     void *data);

	/* second version of the expect callback: it includes netlink header */
	int			(*expect_cb2)(const struct nlmsghdr *nlh,
					      enum nf_conntrack_msg_type type, 
					      struct nf_expect *exp,
					      void *data);
};

/* container used to pass data to nfnl callbacks */
struct __data_container {
	struct nfct_handle *h;
	enum nf_conntrack_msg_type type;
	void *data;
};

/*
 * conntrack object
 */

union __nfct_l4_src {
	/* Add other protocols here. */
	u_int16_t 		all;
	struct {
		u_int16_t 	port;
	} tcp;
	struct {
		u_int16_t 	port;
	} udp;
	struct {
		u_int16_t 	id;
	} icmp;
	struct {
		u_int16_t 	port;
	} sctp;
	struct {
		u_int16_t 	port;
	} dccp;
};

union __nfct_l4_dst {
	/* Add other protocols here. */
	u_int16_t 		all;
	struct {
		u_int16_t 	port;
	} tcp;
	struct {
		u_int16_t 	port;
	} udp;
	struct {
		u_int8_t 	type, code;
	} icmp;
	struct {
		u_int16_t 	port;
	} sctp;
	struct {
		u_int16_t 	port;
	} dccp;
};

union __nfct_address {
	u_int32_t 		v4;
	struct in6_addr 	v6;
};

struct __nfct_tuple {
	union __nfct_address	src;
	union __nfct_address 	dst;

	u_int8_t		l3protonum;
	u_int8_t		protonum;
	union __nfct_l4_src	l4src;
	union __nfct_l4_dst	l4dst;

	struct {
		u_int32_t 	correction_pos;
		u_int32_t 	offset_before;
		u_int32_t 	offset_after;
	} natseq;
};

#define __DIR_ORIG 		0
#define __DIR_REPL 		1
#define __DIR_MASTER 		2
#define __DIR_MAX 		__DIR_MASTER+1

union __nfct_protoinfo {
	struct {
		u_int8_t 		state;
		struct {
			u_int8_t 	value;
			u_int8_t 	mask;
		} flags[__DIR_MAX];
		u_int8_t		wscale[__DIR_MAX];
	} tcp;
	struct {
		u_int8_t 		state;
		u_int32_t 		vtag[__DIR_MAX];
	} sctp;
	struct {
		u_int8_t 		state;
		u_int8_t		role;
		u_int64_t		handshake_seq;
	} dccp;
};

struct __nfct_counters {
	u_int64_t 	packets;
	u_int64_t 	bytes;
};

struct __nfct_nat {
	u_int32_t 		min_ip, max_ip;
	union __nfct_l4_src 	l4min, l4max;
};

struct nf_conntrack {
	struct __nfct_tuple 	tuple[__DIR_MAX];
	
	u_int32_t 	timeout;
	u_int32_t	mark;
	u_int32_t	secmark;
	u_int32_t 	status;
	u_int32_t	use;
	u_int32_t	id;
	u_int16_t	zone;

/* xt_helper uses a length size of 30 bytes, however, no helper name in
 * the tree has exceeded 16 bytes length. Since 2.6.29, the maximum
 * length accepted is 16 bytes, this limit is enforced during module load. */
#define __NFCT_HELPER_NAMELEN		16
	char 		helper_name[__NFCT_HELPER_NAMELEN];

	union __nfct_protoinfo 	protoinfo;
	struct __nfct_counters 	counters[__DIR_MAX];
	struct __nfct_nat 	snat;
	struct __nfct_nat 	dnat;

#define __NFCT_BITSET			2
	u_int32_t               set[__NFCT_BITSET];
};

/*
 * conntrack filter object
 */

struct nfct_filter {
	/*
	 * As many other objects in this library, the attributes are
	 * private. This gives us the chance to modify the layout and
	 * object size.
	 *
	 * Another observation, although this object might seem too
	 * memory consuming, it is only needed to build the filter. Thus,
	 * once it is attached, you can release this object.
	 */

	/*
	 * filter logic: use positive or negative logic
	 */
	enum nfct_filter_logic 	logic[NFCT_FILTER_MAX];

	/*
	 * This the layer 4 protocol map for filtering. Not more than 
	 * 255 protocols (maximum is IPPROTO_MAX which is 256). Actually,
	 * I doubt that anyone can reach such a limit.
	 */
#define __FILTER_L4PROTO_MAX	255
	u_int32_t 		l4proto_map[IPPROTO_MAX/32];
	u_int32_t		l4proto_len;

	struct {
	/*
	 * No limitations in the protocol filtering. We use a map of 
	 * 16 bits per protocol. As for now, DCCP has 10 states, TCP has
	 * 10 states, SCTP has 8 state. Therefore, 16 bits is enough.
	 */
#define __FILTER_PROTO_MAX	16
		u_int16_t 	map;
		u_int16_t	len;
	} l4proto_state[IPPROTO_MAX];

#define __FILTER_ADDR_SRC 0
#define __FILTER_ADDR_DST 1

	/*
	 * FIXME: For IPv4 filtering, up to 127 IPs by now.
	 * This limitation is related to the existing autogenerated BSF code
	 * (two BSF lines per comparison) and the fact that the maximum 
	 * jump offset is 0xff which is 255.
	 */
	u_int32_t 		l3proto_elems[2];
	struct {
#define __FILTER_ADDR_MAX	127
		u_int32_t 	addr;
		u_int32_t 	mask;
	} l3proto[2][__FILTER_ADDR_MAX];

	/*
	 * FIXME: For IPv6 filtering, up to 20 IPs/masks (12 BSF lines
	 * per comparison). I think that it is not worthy to try to support
	 * more than that for performance reasons. It seems that oprofile
	 * shows bad numbers for very large BSF code.
	 */
	u_int32_t 		l3proto_elems_ipv6[2];
	struct {
#define __FILTER_IPV6_MAX	20
		u_int32_t 	addr[4];
		u_int32_t 	mask[4];
	} l3proto_ipv6[2][__FILTER_IPV6_MAX];

	u_int32_t 		set[1];
};

/*
 * expectation object
 */

struct nf_expect {
	struct nf_conntrack 	master;
	struct nf_conntrack 	expected;
	struct nf_conntrack 	mask;
	u_int32_t 		timeout;
	u_int32_t 		id;
	u_int16_t		zone;
	u_int32_t		flags;

	u_int32_t 		set[1];
};

#endif