1#ifndef _IP_CONNTRACK_TUPLE_H
2#define _IP_CONNTRACK_TUPLE_H
3
4/* A `tuple' is a structure containing the information to uniquely
5  identify a connection.  ie. if two packets have the same tuple, they
6  are in the same connection; if not, they are not.
7
8  We divide the structure along "manipulatable" and
9  "non-manipulatable" lines, for the benefit of the NAT code.
10*/
11
12/* The protocol-specific manipulable parts of the tuple: always in
13   network order! */
14union ip_conntrack_manip_proto
15{
16	/* Add other protocols here. */
17	u_int32_t all;
18
19	struct {
20		u_int16_t port;
21	} tcp;
22	struct {
23		u_int16_t port;
24	} udp;
25	struct {
26		u_int16_t id;
27	} icmp;
28	struct {
29		u_int32_t key;
30	} gre;
31};
32
33/* The manipulable part of the tuple. */
34struct ip_conntrack_manip
35{
36	u_int32_t ip;
37	union ip_conntrack_manip_proto u;
38};
39
40/* This contains the information to distinguish a connection. */
41struct ip_conntrack_tuple
42{
43	struct ip_conntrack_manip src;
44
45	/* These are the parts of the tuple which are fixed. */
46	struct {
47		u_int32_t ip;
48		union {
49			/* Add other protocols here. */
50			u_int64_t all;
51
52			struct {
53				u_int16_t port;
54			} tcp;
55			struct {
56				u_int16_t port;
57			} udp;
58			struct {
59				u_int8_t type, code;
60			} icmp;
61			struct {
62				u_int16_t protocol;
63				u_int8_t version;
64				u_int32_t key;
65			} gre;
66		} u;
67
68		/* The protocol. */
69		u_int16_t protonum;
70	} dst;
71};
72
73enum ip_conntrack_dir
74{
75	IP_CT_DIR_ORIGINAL,
76	IP_CT_DIR_REPLY,
77	IP_CT_DIR_MAX
78};
79
80#ifdef __KERNEL__
81
82#define DUMP_TUPLE(tp)						\
83DEBUGP("tuple %p: %u %u.%u.%u.%u:%u -> %u.%u.%u.%u:%u\n",	\
84       (tp), (tp)->dst.protonum,				\
85       NIPQUAD((tp)->src.ip), ntohl((tp)->src.u.all),		\
86       NIPQUAD((tp)->dst.ip), ntohl((tp)->dst.u.all))
87
88#define DUMP_TUPLE_RAW(x) 						\
89	DEBUGP("tuple %p: %u %u.%u.%u.%u:0x%08x -> %u.%u.%u.%u:0x%08x\n",\
90	(x), (x)->dst.protonum,						\
91	NIPQUAD((x)->src.ip), ntohl((x)->src.u.all), 			\
92	NIPQUAD((x)->dst.ip), ntohl((x)->dst.u.all))
93
94#define CTINFO2DIR(ctinfo) ((ctinfo) >= IP_CT_IS_REPLY ? IP_CT_DIR_REPLY : IP_CT_DIR_ORIGINAL)
95
96/* If we're the first tuple, it's the original dir. */
97#define DIRECTION(h) ((enum ip_conntrack_dir)(&(h)->ctrack->tuplehash[1] == (h)))
98
99#define IP_TRACK_MAX			10
100#define IP_TRACK_COMPARE		40
101
102#define IP_TRACK_FULL		0x01
103#define IP_TRACK_PORT		0x02
104#define IP_TRACK_DATA		0x04
105#define IP_TRACK_PD		0x08
106#define IP_TRACK_DOWN		0x10
107#define IP_TRACK_TIME		0x20
108
109struct ip_track
110{
111	int flag;
112	u_int8_t number;
113//	struct timeval tv;
114	int length[IP_TRACK_MAX];
115};
116
117/* Connections have two entries in the hash table: one for each way */
118struct ip_conntrack_tuple_hash
119{
120	struct list_head list;
121
122	struct ip_conntrack_tuple tuple;
123
124	struct ip_track track;
125
126	/* this == &ctrack->tuplehash[DIRECTION(this)]. */
127	struct ip_conntrack *ctrack;
128};
129
130#endif /* __KERNEL__ */
131
132static inline int ip_ct_tuple_src_equal(const struct ip_conntrack_tuple *t1,
133				        const struct ip_conntrack_tuple *t2)
134{
135	return t1->src.ip == t2->src.ip
136		&& t1->src.u.all == t2->src.u.all;
137}
138
139static inline int ip_ct_tuple_dst_equal(const struct ip_conntrack_tuple *t1,
140				        const struct ip_conntrack_tuple *t2)
141{
142	return t1->dst.ip == t2->dst.ip
143		&& t1->dst.u.all == t2->dst.u.all
144		&& t1->dst.protonum == t2->dst.protonum;
145}
146
147static inline int ip_ct_tuple_equal(const struct ip_conntrack_tuple *t1,
148				    const struct ip_conntrack_tuple *t2)
149{
150	return ip_ct_tuple_src_equal(t1, t2) && ip_ct_tuple_dst_equal(t1, t2);
151}
152
153static inline int ip_ct_tuple_mask_cmp(const struct ip_conntrack_tuple *t,
154				       const struct ip_conntrack_tuple *tuple,
155				       const struct ip_conntrack_tuple *mask)
156{
157	return !(((t->src.ip ^ tuple->src.ip) & mask->src.ip)
158		 || ((t->dst.ip ^ tuple->dst.ip) & mask->dst.ip)
159		 || ((t->src.u.all ^ tuple->src.u.all) & mask->src.u.all)
160		 || ((t->dst.u.all ^ tuple->dst.u.all) & mask->dst.u.all)
161		 || ((t->dst.protonum ^ tuple->dst.protonum)
162		     & mask->dst.protonum));
163}
164
165#endif /* _IP_CONNTRACK_TUPLE_H */
166