Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / 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/x_tables.h>
14 #include <linux/netfilter/nf_conntrack_tuple_common.h>
15 #include <linux/list_nulls.h>
16
17 /* A `tuple' is a structure containing the information to uniquely
18   identify a connection.  ie. if two packets have the same tuple, they
19   are in the same connection; if not, they are not.
20
21   We divide the structure along "manipulatable" and
22   "non-manipulatable" lines, for the benefit of the NAT code.
23 */
24
25 #define NF_CT_TUPLE_L3SIZE      ARRAY_SIZE(((union nf_inet_addr *)NULL)->all)
26
27 /* The manipulable part of the tuple. */
28 struct nf_conntrack_man {
29         union nf_inet_addr u3;
30         union nf_conntrack_man_proto u;
31         /* Layer 3 protocol */
32         u_int16_t l3num;
33 };
34
35 /* This contains the information to distinguish a connection. */
36 struct nf_conntrack_tuple {
37         struct nf_conntrack_man src;
38
39         /* These are the parts of the tuple which are fixed. */
40         struct {
41                 union nf_inet_addr u3;
42                 union {
43                         /* Add other protocols here. */
44                         __be16 all;
45
46                         struct {
47                                 __be16 port;
48                         } tcp;
49                         struct {
50                                 __be16 port;
51                         } udp;
52                         struct {
53                                 u_int8_t type, code;
54                         } icmp;
55                         struct {
56                                 __be16 port;
57                         } dccp;
58                         struct {
59                                 __be16 port;
60                         } sctp;
61                         struct {
62                                 __be16 key;
63                         } gre;
64                 } u;
65
66                 /* The protocol. */
67                 u_int8_t protonum;
68
69                 /* The direction (for tuplehash) */
70                 u_int8_t dir;
71         } dst;
72 };
73
74 struct nf_conntrack_tuple_mask {
75         struct {
76                 union nf_inet_addr u3;
77                 union nf_conntrack_man_proto u;
78         } src;
79 };
80
81 static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t)
82 {
83 #ifdef DEBUG
84         printk("tuple %p: %u %pI4:%hu -> %pI4:%hu\n",
85                t, t->dst.protonum,
86                &t->src.u3.ip, ntohs(t->src.u.all),
87                &t->dst.u3.ip, ntohs(t->dst.u.all));
88 #endif
89 }
90
91 static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t)
92 {
93 #ifdef DEBUG
94         printk("tuple %p: %u %pI6 %hu -> %pI6 %hu\n",
95                t, t->dst.protonum,
96                t->src.u3.all, ntohs(t->src.u.all),
97                t->dst.u3.all, ntohs(t->dst.u.all));
98 #endif
99 }
100
101 static inline void nf_ct_dump_tuple(const struct nf_conntrack_tuple *t)
102 {
103         switch (t->src.l3num) {
104         case AF_INET:
105                 nf_ct_dump_tuple_ip(t);
106                 break;
107         case AF_INET6:
108                 nf_ct_dump_tuple_ipv6(t);
109                 break;
110         }
111 }
112
113 /* If we're the first tuple, it's the original dir. */
114 #define NF_CT_DIRECTION(h)                                              \
115         ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
116
117 /* Connections have two entries in the hash table: one for each way */
118 struct nf_conntrack_tuple_hash {
119         struct hlist_nulls_node hnnode;
120         struct nf_conntrack_tuple tuple;
121 };
122
123 static inline bool __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
124                                            const struct nf_conntrack_tuple *t2)
125
126         return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) &&
127                 t1->src.u.all == t2->src.u.all &&
128                 t1->src.l3num == t2->src.l3num);
129 }
130
131 static inline bool __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
132                                            const struct nf_conntrack_tuple *t2)
133 {
134         return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) &&
135                 t1->dst.u.all == t2->dst.u.all &&
136                 t1->dst.protonum == t2->dst.protonum);
137 }
138
139 static inline bool nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
140                                      const struct nf_conntrack_tuple *t2)
141 {
142         return __nf_ct_tuple_src_equal(t1, t2) &&
143                __nf_ct_tuple_dst_equal(t1, t2);
144 }
145
146 static inline bool
147 nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
148                        const struct nf_conntrack_tuple_mask *m2)
149 {
150         return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) &&
151                 m1->src.u.all == m2->src.u.all);
152 }
153
154 static inline bool
155 nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
156                          const struct nf_conntrack_tuple *t2,
157                          const struct nf_conntrack_tuple_mask *mask)
158 {
159         int count;
160
161         for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
162                 if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
163                     mask->src.u3.all[count])
164                         return false;
165         }
166
167         if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
168                 return false;
169
170         if (t1->src.l3num != t2->src.l3num ||
171             t1->dst.protonum != t2->dst.protonum)
172                 return false;
173
174         return true;
175 }
176
177 static inline bool
178 nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
179                      const struct nf_conntrack_tuple *tuple,
180                      const struct nf_conntrack_tuple_mask *mask)
181 {
182         return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
183                __nf_ct_tuple_dst_equal(t, tuple);
184 }
185
186 #endif /* _NF_CONNTRACK_TUPLE_H */