blob: 1bb7087833d374eb2429dd55318248ddc1747d50 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/*
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
Jan Engelhardt643a2c12007-12-17 22:43:50 -080013#include <linux/netfilter/x_tables.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080014#include <linux/netfilter/nf_conntrack_tuple_common.h>
15
16/* A `tuple' is a structure containing the information to uniquely
17 identify a connection. ie. if two packets have the same tuple, they
18 are in the same connection; if not, they are not.
19
20 We divide the structure along "manipulatable" and
21 "non-manipulatable" lines, for the benefit of the NAT code.
22*/
23
Jan Engelhardt643a2c12007-12-17 22:43:50 -080024#define NF_CT_TUPLE_L3SIZE ARRAY_SIZE(((union nf_inet_addr *)NULL)->all)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025
26/* The protocol-specific manipulable parts of the tuple: always in
27 network order! */
28union nf_conntrack_man_proto
29{
30 /* Add other protocols here. */
Al Viroa34c4582007-07-26 17:33:19 +010031 __be16 all;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080032
33 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080034 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080035 } tcp;
36 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080037 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080038 } udp;
39 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080040 __be16 id;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080041 } icmp;
42 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080043 __be16 port;
Patrick McHardy2bc78042008-03-20 15:15:55 +010044 } dccp;
45 struct {
46 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080047 } sctp;
Patrick McHardyf09943f2006-12-02 22:09:41 -080048 struct {
49 __be16 key; /* GRE key is 32bit, PPtP only uses 16bit */
50 } gre;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051};
52
53/* The manipulable part of the tuple. */
54struct nf_conntrack_man
55{
Jan Engelhardt643a2c12007-12-17 22:43:50 -080056 union nf_inet_addr u3;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080057 union nf_conntrack_man_proto u;
58 /* Layer 3 protocol */
59 u_int16_t l3num;
60};
61
62/* This contains the information to distinguish a connection. */
63struct nf_conntrack_tuple
64{
65 struct nf_conntrack_man src;
66
67 /* These are the parts of the tuple which are fixed. */
68 struct {
Jan Engelhardt643a2c12007-12-17 22:43:50 -080069 union nf_inet_addr u3;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080070 union {
71 /* Add other protocols here. */
Al Viroa34c4582007-07-26 17:33:19 +010072 __be16 all;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080073
74 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080075 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080076 } tcp;
77 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080078 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080079 } udp;
80 struct {
81 u_int8_t type, code;
82 } icmp;
83 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080084 __be16 port;
Patrick McHardy2bc78042008-03-20 15:15:55 +010085 } dccp;
86 struct {
87 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080088 } sctp;
Patrick McHardyf09943f2006-12-02 22:09:41 -080089 struct {
90 __be16 key;
91 } gre;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080092 } u;
93
94 /* The protocol. */
95 u_int8_t protonum;
96
97 /* The direction (for tuplehash) */
98 u_int8_t dir;
99 } dst;
100};
101
Patrick McHardyd4156e82007-07-07 22:31:32 -0700102struct nf_conntrack_tuple_mask
103{
104 struct {
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800105 union nf_inet_addr u3;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700106 union nf_conntrack_man_proto u;
107 } src;
108};
109
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800110/* This is optimized opposed to a memset of the whole structure. Everything we
111 * really care about is the source/destination unions */
112#define NF_CT_TUPLE_U_BLANK(tuple) \
113 do { \
114 (tuple)->src.u.all = 0; \
115 (tuple)->dst.u.all = 0; \
116 memset(&(tuple)->src.u3, 0, sizeof((tuple)->src.u3)); \
117 memset(&(tuple)->dst.u3, 0, sizeof((tuple)->dst.u3)); \
118 } while (0)
119
120#ifdef __KERNEL__
121
Patrick McHardyef275592008-03-25 20:07:38 -0700122static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t)
123{
124#ifdef DEBUG
125 printk("tuple %p: %u " NIPQUAD_FMT ":%hu -> " NIPQUAD_FMT ":%hu\n",
126 t, t->dst.protonum,
127 NIPQUAD(t->src.u3.ip), ntohs(t->src.u.all),
128 NIPQUAD(t->dst.u3.ip), ntohs(t->dst.u.all));
129#endif
130}
131
132static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t)
133{
134#ifdef DEBUG
135 printk("tuple %p: %u " NIP6_FMT " %hu -> " NIP6_FMT " %hu\n",
136 t, t->dst.protonum,
137 NIP6(*(struct in6_addr *)t->src.u3.all), ntohs(t->src.u.all),
138 NIP6(*(struct in6_addr *)t->dst.u3.all), ntohs(t->dst.u.all));
139#endif
140}
141
142static inline void nf_ct_dump_tuple(const struct nf_conntrack_tuple *t)
143{
144 switch (t->src.l3num) {
145 case AF_INET:
146 nf_ct_dump_tuple_ip(t);
147 break;
148 case AF_INET6:
149 nf_ct_dump_tuple_ipv6(t);
150 break;
151 }
152}
153
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800154/* If we're the first tuple, it's the original dir. */
155#define NF_CT_DIRECTION(h) \
156 ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
157
158/* Connections have two entries in the hash table: one for each way */
159struct nf_conntrack_tuple_hash
160{
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700161 struct hlist_node hnode;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800162 struct nf_conntrack_tuple tuple;
163};
164
165#endif /* __KERNEL__ */
166
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200167static inline bool __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
168 const struct nf_conntrack_tuple *t2)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169{
Patrick McHardyb8beedd2008-03-25 20:09:33 -0700170 return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) &&
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800171 t1->src.u.all == t2->src.u.all &&
Patrick McHardy380517d2008-01-31 04:40:04 -0800172 t1->src.l3num == t2->src.l3num);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800173}
174
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200175static inline bool __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
176 const struct nf_conntrack_tuple *t2)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800177{
Patrick McHardyb8beedd2008-03-25 20:09:33 -0700178 return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) &&
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800179 t1->dst.u.all == t2->dst.u.all &&
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800180 t1->dst.protonum == t2->dst.protonum);
181}
182
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200183static inline bool nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
184 const struct nf_conntrack_tuple *t2)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185{
Patrick McHardy380517d2008-01-31 04:40:04 -0800186 return __nf_ct_tuple_src_equal(t1, t2) &&
187 __nf_ct_tuple_dst_equal(t1, t2);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800188}
189
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200190static inline bool
191nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
192 const struct nf_conntrack_tuple_mask *m2)
Patrick McHardyd4156e82007-07-07 22:31:32 -0700193{
Patrick McHardyb8beedd2008-03-25 20:09:33 -0700194 return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) &&
Patrick McHardyd4156e82007-07-07 22:31:32 -0700195 m1->src.u.all == m2->src.u.all);
196}
197
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200198static inline bool
199nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
200 const struct nf_conntrack_tuple *t2,
201 const struct nf_conntrack_tuple_mask *mask)
Patrick McHardyd4156e82007-07-07 22:31:32 -0700202{
203 int count;
204
205 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
206 if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
207 mask->src.u3.all[count])
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200208 return false;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700209 }
210
211 if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200212 return false;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700213
214 if (t1->src.l3num != t2->src.l3num ||
215 t1->dst.protonum != t2->dst.protonum)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200216 return false;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700217
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200218 return true;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700219}
220
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200221static inline bool
222nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
223 const struct nf_conntrack_tuple *tuple,
224 const struct nf_conntrack_tuple_mask *mask)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800225{
Patrick McHardyd4156e82007-07-07 22:31:32 -0700226 return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
Patrick McHardy380517d2008-01-31 04:40:04 -0800227 __nf_ct_tuple_dst_equal(t, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800228}
229
230#endif /* _NF_CONNTRACK_TUPLE_H */