]> err.no Git - linux-2.6/blob - include/net/netfilter/nf_conntrack_tuple.h
[NETFILTER]: nf_conntrack: add helper function for expectation initialization
[linux-2.6] / include / net / netfilter / nf_conntrack_tuple.h
1 /*
2  * Definitions and Declarations for tuple.
3  *
4  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
5  *      - generalize L3 protocol dependent part.
6  *
7  * Derived from include/linux/netfiter_ipv4/ip_conntrack_tuple.h
8  */
9
10 #ifndef _NF_CONNTRACK_TUPLE_H
11 #define _NF_CONNTRACK_TUPLE_H
12
13 #include <linux/netfilter/nf_conntrack_tuple_common.h>
14
15 /* A `tuple' is a structure containing the information to uniquely
16   identify a connection.  ie. if two packets have the same tuple, they
17   are in the same connection; if not, they are not.
18
19   We divide the structure along "manipulatable" and
20   "non-manipulatable" lines, for the benefit of the NAT code.
21 */
22
23 #define NF_CT_TUPLE_L3SIZE      4
24
25 /* The l3 protocol-specific manipulable parts of the tuple: always in
26    network order! */
27 union nf_conntrack_address {
28         u_int32_t all[NF_CT_TUPLE_L3SIZE];
29         __be32 ip;
30         __be32 ip6[4];
31 };
32
33 /* The protocol-specific manipulable parts of the tuple: always in
34    network order! */
35 union nf_conntrack_man_proto
36 {
37         /* Add other protocols here. */
38         u_int16_t all;
39
40         struct {
41                 __be16 port;
42         } tcp;
43         struct {
44                 __be16 port;
45         } udp;
46         struct {
47                 __be16 id;
48         } icmp;
49         struct {
50                 __be16 port;
51         } sctp;
52 };
53
54 /* The manipulable part of the tuple. */
55 struct nf_conntrack_man
56 {
57         union nf_conntrack_address u3;
58         union nf_conntrack_man_proto u;
59         /* Layer 3 protocol */
60         u_int16_t l3num;
61 };
62
63 /* This contains the information to distinguish a connection. */
64 struct nf_conntrack_tuple
65 {
66         struct nf_conntrack_man src;
67
68         /* These are the parts of the tuple which are fixed. */
69         struct {
70                 union nf_conntrack_address u3;
71                 union {
72                         /* Add other protocols here. */
73                         u_int16_t all;
74
75                         struct {
76                                 __be16 port;
77                         } tcp;
78                         struct {
79                                 __be16 port;
80                         } udp;
81                         struct {
82                                 u_int8_t type, code;
83                         } icmp;
84                         struct {
85                                 __be16 port;
86                         } sctp;
87                 } u;
88
89                 /* The protocol. */
90                 u_int8_t protonum;
91
92                 /* The direction (for tuplehash) */
93                 u_int8_t dir;
94         } dst;
95 };
96
97 /* This is optimized opposed to a memset of the whole structure.  Everything we
98  * really care about is the  source/destination unions */
99 #define NF_CT_TUPLE_U_BLANK(tuple)                                      \
100         do {                                                            \
101                 (tuple)->src.u.all = 0;                                 \
102                 (tuple)->dst.u.all = 0;                                 \
103                 memset(&(tuple)->src.u3, 0, sizeof((tuple)->src.u3));   \
104                 memset(&(tuple)->dst.u3, 0, sizeof((tuple)->dst.u3));   \
105         } while (0)
106
107 #ifdef __KERNEL__
108
109 #define NF_CT_DUMP_TUPLE(tp)                                                \
110 DEBUGP("tuple %p: %u %u " NIP6_FMT " %hu -> " NIP6_FMT " %hu\n",            \
111         (tp), (tp)->src.l3num, (tp)->dst.protonum,                          \
112         NIP6(*(struct in6_addr *)(tp)->src.u3.all), ntohs((tp)->src.u.all), \
113         NIP6(*(struct in6_addr *)(tp)->dst.u3.all), ntohs((tp)->dst.u.all))
114
115 /* If we're the first tuple, it's the original dir. */
116 #define NF_CT_DIRECTION(h)                                              \
117         ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
118
119 /* Connections have two entries in the hash table: one for each way */
120 struct nf_conntrack_tuple_hash
121 {
122         struct list_head list;
123
124         struct nf_conntrack_tuple tuple;
125 };
126
127 #endif /* __KERNEL__ */
128
129 static inline int nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
130                                         const struct nf_conntrack_tuple *t2)
131
132         return (t1->src.u3.all[0] == t2->src.u3.all[0] &&
133                 t1->src.u3.all[1] == t2->src.u3.all[1] &&
134                 t1->src.u3.all[2] == t2->src.u3.all[2] &&
135                 t1->src.u3.all[3] == t2->src.u3.all[3] &&
136                 t1->src.u.all == t2->src.u.all &&
137                 t1->src.l3num == t2->src.l3num &&
138                 t1->dst.protonum == t2->dst.protonum);
139 }
140
141 static inline int nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
142                                         const struct nf_conntrack_tuple *t2)
143 {
144         return (t1->dst.u3.all[0] == t2->dst.u3.all[0] &&
145                 t1->dst.u3.all[1] == t2->dst.u3.all[1] &&
146                 t1->dst.u3.all[2] == t2->dst.u3.all[2] &&
147                 t1->dst.u3.all[3] == t2->dst.u3.all[3] &&
148                 t1->dst.u.all == t2->dst.u.all &&
149                 t1->src.l3num == t2->src.l3num &&
150                 t1->dst.protonum == t2->dst.protonum);
151 }
152
153 static inline int nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
154                                     const struct nf_conntrack_tuple *t2)
155 {
156         return nf_ct_tuple_src_equal(t1, t2) && nf_ct_tuple_dst_equal(t1, t2);
157 }
158
159 static inline int nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
160                                        const struct nf_conntrack_tuple *tuple,
161                                        const struct nf_conntrack_tuple *mask)
162 {
163         int count = 0;
164
165         for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
166                 if ((t->src.u3.all[count] ^ tuple->src.u3.all[count]) &
167                     mask->src.u3.all[count])
168                         return 0;
169         }
170
171         for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
172                 if ((t->dst.u3.all[count] ^ tuple->dst.u3.all[count]) &
173                     mask->dst.u3.all[count])
174                         return 0;
175         }
176
177         if ((t->src.u.all ^ tuple->src.u.all) & mask->src.u.all ||
178             (t->dst.u.all ^ tuple->dst.u.all) & mask->dst.u.all ||
179             (t->src.l3num ^ tuple->src.l3num) & mask->src.l3num ||
180             (t->dst.protonum ^ tuple->dst.protonum) & mask->dst.protonum)
181                 return 0;
182
183         return 1;
184 }
185
186 #endif /* _NF_CONNTRACK_TUPLE_H */