This commit was manufactured by cvs2svn to create tag 'after-xenU'.
[linux-2.6.git] / include / linux / netfilter_ipv4 / ip_conntrack_tuple.h
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! */
14 union 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         struct {
32                 u_int16_t port;
33         } sctp;
34 };
35
36 /* The manipulable part of the tuple. */
37 struct ip_conntrack_manip
38 {
39         u_int32_t ip;
40         union ip_conntrack_manip_proto u;
41 };
42
43 /* This contains the information to distinguish a connection. */
44 struct ip_conntrack_tuple
45 {
46         struct ip_conntrack_manip src;
47
48         /* These are the parts of the tuple which are fixed. */
49         struct {
50                 u_int32_t ip;
51                 union {
52                         /* Add other protocols here. */
53                         u_int32_t all;
54
55                         struct {
56                                 u_int16_t port;
57                         } tcp;
58                         struct {
59                                 u_int16_t port;
60                         } udp;
61                         struct {
62                                 u_int8_t type, code;
63                         } icmp;
64                         struct {
65                                 u_int32_t key;
66                         } gre;
67                         struct {
68                                 u_int16_t port;
69                         } sctp;
70                 } u;
71
72                 /* The protocol. */
73                 u_int8_t protonum;
74
75                 /* The direction (for tuplehash) */
76                 u_int8_t dir;
77         } dst;
78 };
79
80 /* This is optimized opposed to a memset of the whole structure.  Everything we
81  * really care about is the  source/destination unions */
82 #define IP_CT_TUPLE_U_BLANK(tuple)                              \
83         do {                                                    \
84                 (tuple)->src.u.all = 0;                         \
85                 (tuple)->dst.u.all = 0;                         \
86         } while (0)
87
88 enum ip_conntrack_dir
89 {
90         IP_CT_DIR_ORIGINAL,
91         IP_CT_DIR_REPLY,
92         IP_CT_DIR_MAX
93 };
94
95 #ifdef __KERNEL__
96
97 #define DUMP_TUPLE(tp)                                          \
98 DEBUGP("tuple %p: %u %u.%u.%u.%u:%u -> %u.%u.%u.%u:%u\n",       \
99        (tp), (tp)->dst.protonum,                                \
100        NIPQUAD((tp)->src.ip), ntohl((tp)->src.u.all),           \
101        NIPQUAD((tp)->dst.ip), ntohl((tp)->dst.u.all))
102
103 #define DUMP_TUPLE_RAW(x)                                               \
104         DEBUGP("tuple %p: %u %u.%u.%u.%u:0x%08x -> %u.%u.%u.%u:0x%08x\n",\
105         (x), (x)->dst.protonum,                                         \
106         NIPQUAD((x)->src.ip), ntohl((x)->src.u.all),                    \
107         NIPQUAD((x)->dst.ip), ntohl((x)->dst.u.all))
108
109 #define CTINFO2DIR(ctinfo) ((ctinfo) >= IP_CT_IS_REPLY ? IP_CT_DIR_REPLY : IP_CT_DIR_ORIGINAL)
110
111 /* If we're the first tuple, it's the original dir. */
112 #define DIRECTION(h) ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
113
114 /* Connections have two entries in the hash table: one for each way */
115 struct ip_conntrack_tuple_hash
116 {
117         struct list_head list;
118
119         struct ip_conntrack_tuple tuple;
120 };
121
122 #endif /* __KERNEL__ */
123
124 static inline int ip_ct_tuple_src_equal(const struct ip_conntrack_tuple *t1,
125                                         const struct ip_conntrack_tuple *t2)
126 {
127         return t1->src.ip == t2->src.ip
128                 && t1->src.u.all == t2->src.u.all;
129 }
130
131 static inline int ip_ct_tuple_dst_equal(const struct ip_conntrack_tuple *t1,
132                                         const struct ip_conntrack_tuple *t2)
133 {
134         return t1->dst.ip == t2->dst.ip
135                 && t1->dst.u.all == t2->dst.u.all
136                 && t1->dst.protonum == t2->dst.protonum;
137 }
138
139 static inline int ip_ct_tuple_equal(const struct ip_conntrack_tuple *t1,
140                                     const struct ip_conntrack_tuple *t2)
141 {
142         return ip_ct_tuple_src_equal(t1, t2) && ip_ct_tuple_dst_equal(t1, t2);
143 }
144
145 static inline int ip_ct_tuple_mask_cmp(const struct ip_conntrack_tuple *t,
146                                        const struct ip_conntrack_tuple *tuple,
147                                        const struct ip_conntrack_tuple *mask)
148 {
149         return !(((t->src.ip ^ tuple->src.ip) & mask->src.ip)
150                  || ((t->dst.ip ^ tuple->dst.ip) & mask->dst.ip)
151                  || ((t->src.u.all ^ tuple->src.u.all) & mask->src.u.all)
152                  || ((t->dst.u.all ^ tuple->dst.u.all) & mask->dst.u.all)
153                  || ((t->dst.protonum ^ tuple->dst.protonum)
154                      & mask->dst.protonum));
155 }
156
157 #endif /* _IP_CONNTRACK_TUPLE_H */