merge Linus head tree into my drm tree and fix up conflicts
[linux-2.6] / include / linux / netfilter_ipv4 / ip_conntrack_tuple.h
1 #ifndef _IP_CONNTRACK_TUPLE_H
2 #define _IP_CONNTRACK_TUPLE_H
3
4 #include <linux/types.h>
5
6 /* A `tuple' is a structure containing the information to uniquely
7   identify a connection.  ie. if two packets have the same tuple, they
8   are in the same connection; if not, they are not.
9
10   We divide the structure along "manipulatable" and
11   "non-manipulatable" lines, for the benefit of the NAT code.
12 */
13
14 /* The protocol-specific manipulable parts of the tuple: always in
15    network order! */
16 union ip_conntrack_manip_proto
17 {
18         /* Add other protocols here. */
19         u_int16_t all;
20
21         struct {
22                 __be16 port;
23         } tcp;
24         struct {
25                 u_int16_t port;
26         } udp;
27         struct {
28                 u_int16_t id;
29         } icmp;
30         struct {
31                 u_int16_t port;
32         } sctp;
33         struct {
34                 __be16 key;     /* key is 32bit, pptp only uses 16 */
35         } gre;
36 };
37
38 /* The manipulable part of the tuple. */
39 struct ip_conntrack_manip
40 {
41         u_int32_t ip;
42         union ip_conntrack_manip_proto u;
43 };
44
45 /* This contains the information to distinguish a connection. */
46 struct ip_conntrack_tuple
47 {
48         struct ip_conntrack_manip src;
49
50         /* These are the parts of the tuple which are fixed. */
51         struct {
52                 u_int32_t ip;
53                 union {
54                         /* Add other protocols here. */
55                         u_int16_t all;
56
57                         struct {
58                                 u_int16_t port;
59                         } tcp;
60                         struct {
61                                 u_int16_t port;
62                         } udp;
63                         struct {
64                                 u_int8_t type, code;
65                         } icmp;
66                         struct {
67                                 u_int16_t port;
68                         } sctp;
69                         struct {
70                                 __be16 key;     /* key is 32bit, 
71                                                  * pptp only uses 16 */
72                         } gre;
73                 } u;
74
75                 /* The protocol. */
76                 u_int8_t protonum;
77
78                 /* The direction (for tuplehash) */
79                 u_int8_t dir;
80         } dst;
81 };
82
83 /* This is optimized opposed to a memset of the whole structure.  Everything we
84  * really care about is the  source/destination unions */
85 #define IP_CT_TUPLE_U_BLANK(tuple)                              \
86         do {                                                    \
87                 (tuple)->src.u.all = 0;                         \
88                 (tuple)->dst.u.all = 0;                         \
89         } while (0)
90
91 enum ip_conntrack_dir
92 {
93         IP_CT_DIR_ORIGINAL,
94         IP_CT_DIR_REPLY,
95         IP_CT_DIR_MAX
96 };
97
98 #ifdef __KERNEL__
99
100 #define DUMP_TUPLE(tp)                                          \
101 DEBUGP("tuple %p: %u %u.%u.%u.%u:%hu -> %u.%u.%u.%u:%hu\n",     \
102        (tp), (tp)->dst.protonum,                                \
103        NIPQUAD((tp)->src.ip), ntohs((tp)->src.u.all),           \
104        NIPQUAD((tp)->dst.ip), ntohs((tp)->dst.u.all))
105
106 #define CTINFO2DIR(ctinfo) ((ctinfo) >= IP_CT_IS_REPLY ? IP_CT_DIR_REPLY : IP_CT_DIR_ORIGINAL)
107
108 /* If we're the first tuple, it's the original dir. */
109 #define DIRECTION(h) ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
110
111 /* Connections have two entries in the hash table: one for each way */
112 struct ip_conntrack_tuple_hash
113 {
114         struct list_head list;
115
116         struct ip_conntrack_tuple tuple;
117 };
118
119 #endif /* __KERNEL__ */
120
121 static inline int ip_ct_tuple_src_equal(const struct ip_conntrack_tuple *t1,
122                                         const struct ip_conntrack_tuple *t2)
123 {
124         return t1->src.ip == t2->src.ip
125                 && t1->src.u.all == t2->src.u.all;
126 }
127
128 static inline int ip_ct_tuple_dst_equal(const struct ip_conntrack_tuple *t1,
129                                         const struct ip_conntrack_tuple *t2)
130 {
131         return t1->dst.ip == t2->dst.ip
132                 && t1->dst.u.all == t2->dst.u.all
133                 && t1->dst.protonum == t2->dst.protonum;
134 }
135
136 static inline int ip_ct_tuple_equal(const struct ip_conntrack_tuple *t1,
137                                     const struct ip_conntrack_tuple *t2)
138 {
139         return ip_ct_tuple_src_equal(t1, t2) && ip_ct_tuple_dst_equal(t1, t2);
140 }
141
142 static inline int ip_ct_tuple_mask_cmp(const struct ip_conntrack_tuple *t,
143                                        const struct ip_conntrack_tuple *tuple,
144                                        const struct ip_conntrack_tuple *mask)
145 {
146         return !(((t->src.ip ^ tuple->src.ip) & mask->src.ip)
147                  || ((t->dst.ip ^ tuple->dst.ip) & mask->dst.ip)
148                  || ((t->src.u.all ^ tuple->src.u.all) & mask->src.u.all)
149                  || ((t->dst.u.all ^ tuple->dst.u.all) & mask->dst.u.all)
150                  || ((t->dst.protonum ^ tuple->dst.protonum)
151                      & mask->dst.protonum));
152 }
153
154 #endif /* _IP_CONNTRACK_TUPLE_H */