www.usr.com/support/gpl/USR9107_release.1.4.tar.gz
[bcm963xx.git] / kernel / linux / 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 spi;
33         } esp;
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_int64_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_int16_t protocol;
66                                 u_int8_t version;
67                                 u_int32_t key;
68                         } gre;
69                         struct {
70                                 u_int16_t spi;
71                         } esp;
72                 } u;
73
74                 /* The protocol. */
75                 u_int16_t protonum;
76         } dst;
77 };
78
79 /* This is optimized opposed to a memset of the whole structure.  Everything we
80  * really care about is the  source/destination unions */
81 #define IP_CT_TUPLE_U_BLANK(tuple)                              \
82         do {                                                    \
83                 (tuple)->src.u.all = 0;                         \
84                 (tuple)->dst.u.all = 0;                         \
85         } while (0)
86
87 enum ip_conntrack_dir
88 {
89         IP_CT_DIR_ORIGINAL,
90         IP_CT_DIR_REPLY,
91         IP_CT_DIR_MAX
92 };
93
94 #ifdef __KERNEL__
95
96 #define DUMP_TUPLE(tp)                                          \
97 DEBUGP("tuple %p: %u %u.%u.%u.%u:%u -> %u.%u.%u.%u:%u\n",       \
98        (tp), (tp)->dst.protonum,                                \
99        NIPQUAD((tp)->src.ip), ntohl((tp)->src.u.all),           \
100        NIPQUAD((tp)->dst.ip), ntohl((tp)->dst.u.all))
101
102 #define DUMP_TUPLE_RAW(x)                                               \
103         DEBUGP("tuple %p: %u %u.%u.%u.%u:0x%08x -> %u.%u.%u.%u:0x%08x\n",\
104         (x), (x)->dst.protonum,                                         \
105         NIPQUAD((x)->src.ip), ntohl((x)->src.u.all),                    \
106         NIPQUAD((x)->dst.ip), ntohl((x)->dst.u.all))
107
108 #define CTINFO2DIR(ctinfo) ((ctinfo) >= IP_CT_IS_REPLY ? IP_CT_DIR_REPLY : IP_CT_DIR_ORIGINAL)
109
110 /* If we're the first tuple, it's the original dir. */
111 #define DIRECTION(h) ((enum ip_conntrack_dir)(&(h)->ctrack->tuplehash[1] == (h)))
112
113 /* Connections have two entries in the hash table: one for each way */
114 struct ip_conntrack_tuple_hash
115 {
116         struct list_head list;
117
118         struct ip_conntrack_tuple tuple;
119
120         /* this == &ctrack->tuplehash[DIRECTION(this)]. */
121         struct ip_conntrack *ctrack;
122 };
123
124 #endif /* __KERNEL__ */
125
126 static inline int ip_ct_tuple_src_equal(const struct ip_conntrack_tuple *t1,
127                                         const struct ip_conntrack_tuple *t2)
128 {
129         return t1->src.ip == t2->src.ip
130                 && t1->src.u.all == t2->src.u.all;
131 }
132
133 static inline int ip_ct_tuple_dst_equal(const struct ip_conntrack_tuple *t1,
134                                         const struct ip_conntrack_tuple *t2)
135 {
136         return t1->dst.ip == t2->dst.ip
137                 && t1->dst.u.all == t2->dst.u.all
138                 && t1->dst.protonum == t2->dst.protonum;
139 }
140
141 static inline int ip_ct_tuple_equal(const struct ip_conntrack_tuple *t1,
142                                     const struct ip_conntrack_tuple *t2)
143 {
144         return ip_ct_tuple_src_equal(t1, t2) && ip_ct_tuple_dst_equal(t1, t2);
145 }
146
147 static inline int ip_ct_tuple_mask_cmp(const struct ip_conntrack_tuple *t,
148                                        const struct ip_conntrack_tuple *tuple,
149                                        const struct ip_conntrack_tuple *mask)
150 {
151         return !(((t->src.ip ^ tuple->src.ip) & mask->src.ip)
152                  || ((t->dst.ip ^ tuple->dst.ip) & mask->dst.ip)
153                  || ((t->src.u.all ^ tuple->src.u.all) & mask->src.u.all)
154                  || ((t->dst.u.all ^ tuple->dst.u.all) & mask->dst.u.all)
155                  || ((t->dst.protonum ^ tuple->dst.protonum)
156                      & mask->dst.protonum));
157 }
158
159 #endif /* _IP_CONNTRACK_TUPLE_H */