blob: 168c91754d895ebda49e2bb5f520e4c3a7a7443a [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;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080044 } sctp;
Patrick McHardyf09943f2006-12-02 22:09:41 -080045 struct {
46 __be16 key; /* GRE key is 32bit, PPtP only uses 16bit */
47 } gre;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080048};
49
50/* The manipulable part of the tuple. */
51struct nf_conntrack_man
52{
Jan Engelhardt643a2c12007-12-17 22:43:50 -080053 union nf_inet_addr u3;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080054 union nf_conntrack_man_proto u;
55 /* Layer 3 protocol */
56 u_int16_t l3num;
57};
58
59/* This contains the information to distinguish a connection. */
60struct nf_conntrack_tuple
61{
62 struct nf_conntrack_man src;
63
64 /* These are the parts of the tuple which are fixed. */
65 struct {
Jan Engelhardt643a2c12007-12-17 22:43:50 -080066 union nf_inet_addr u3;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080067 union {
68 /* Add other protocols here. */
Al Viroa34c4582007-07-26 17:33:19 +010069 __be16 all;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080070
71 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080072 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080073 } tcp;
74 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080075 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080076 } udp;
77 struct {
78 u_int8_t type, code;
79 } icmp;
80 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080081 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080082 } sctp;
Patrick McHardyf09943f2006-12-02 22:09:41 -080083 struct {
84 __be16 key;
85 } gre;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080086 } u;
87
88 /* The protocol. */
89 u_int8_t protonum;
90
91 /* The direction (for tuplehash) */
92 u_int8_t dir;
93 } dst;
94};
95
Patrick McHardyd4156e82007-07-07 22:31:32 -070096struct nf_conntrack_tuple_mask
97{
98 struct {
Jan Engelhardt643a2c12007-12-17 22:43:50 -080099 union nf_inet_addr u3;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700100 union nf_conntrack_man_proto u;
101 } src;
102};
103
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800104/* This is optimized opposed to a memset of the whole structure. Everything we
105 * really care about is the source/destination unions */
106#define NF_CT_TUPLE_U_BLANK(tuple) \
107 do { \
108 (tuple)->src.u.all = 0; \
109 (tuple)->dst.u.all = 0; \
110 memset(&(tuple)->src.u3, 0, sizeof((tuple)->src.u3)); \
111 memset(&(tuple)->dst.u3, 0, sizeof((tuple)->dst.u3)); \
112 } while (0)
113
114#ifdef __KERNEL__
115
Patrick McHardyef275592008-03-25 20:07:38 -0700116static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t)
117{
118#ifdef DEBUG
119 printk("tuple %p: %u " NIPQUAD_FMT ":%hu -> " NIPQUAD_FMT ":%hu\n",
120 t, t->dst.protonum,
121 NIPQUAD(t->src.u3.ip), ntohs(t->src.u.all),
122 NIPQUAD(t->dst.u3.ip), ntohs(t->dst.u.all));
123#endif
124}
125
126static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t)
127{
128#ifdef DEBUG
129 printk("tuple %p: %u " NIP6_FMT " %hu -> " NIP6_FMT " %hu\n",
130 t, t->dst.protonum,
131 NIP6(*(struct in6_addr *)t->src.u3.all), ntohs(t->src.u.all),
132 NIP6(*(struct in6_addr *)t->dst.u3.all), ntohs(t->dst.u.all));
133#endif
134}
135
136static inline void nf_ct_dump_tuple(const struct nf_conntrack_tuple *t)
137{
138 switch (t->src.l3num) {
139 case AF_INET:
140 nf_ct_dump_tuple_ip(t);
141 break;
142 case AF_INET6:
143 nf_ct_dump_tuple_ipv6(t);
144 break;
145 }
146}
147
148#define NF_CT_DUMP_TUPLE(tp) nf_ct_dump_tuple(tp)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800149
150/* If we're the first tuple, it's the original dir. */
151#define NF_CT_DIRECTION(h) \
152 ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
153
154/* Connections have two entries in the hash table: one for each way */
155struct nf_conntrack_tuple_hash
156{
Patrick McHardyf205c5e2007-07-07 22:28:14 -0700157 struct hlist_node hnode;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800158 struct nf_conntrack_tuple tuple;
159};
160
161#endif /* __KERNEL__ */
162
Patrick McHardy380517d2008-01-31 04:40:04 -0800163static inline int __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
164 const struct nf_conntrack_tuple *t2)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800165{
Patrick McHardyb8beedd2008-03-25 20:09:33 -0700166 return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) &&
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800167 t1->src.u.all == t2->src.u.all &&
Patrick McHardy380517d2008-01-31 04:40:04 -0800168 t1->src.l3num == t2->src.l3num);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169}
170
Patrick McHardy380517d2008-01-31 04:40:04 -0800171static inline int __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
172 const struct nf_conntrack_tuple *t2)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800173{
Patrick McHardyb8beedd2008-03-25 20:09:33 -0700174 return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) &&
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800175 t1->dst.u.all == t2->dst.u.all &&
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176 t1->dst.protonum == t2->dst.protonum);
177}
178
179static inline int nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
180 const struct nf_conntrack_tuple *t2)
181{
Patrick McHardy380517d2008-01-31 04:40:04 -0800182 return __nf_ct_tuple_src_equal(t1, t2) &&
183 __nf_ct_tuple_dst_equal(t1, t2);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800184}
185
Patrick McHardyd4156e82007-07-07 22:31:32 -0700186static inline int nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
187 const struct nf_conntrack_tuple_mask *m2)
188{
Patrick McHardyb8beedd2008-03-25 20:09:33 -0700189 return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) &&
Patrick McHardyd4156e82007-07-07 22:31:32 -0700190 m1->src.u.all == m2->src.u.all);
191}
192
193static inline int nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
194 const struct nf_conntrack_tuple *t2,
195 const struct nf_conntrack_tuple_mask *mask)
196{
197 int count;
198
199 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
200 if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
201 mask->src.u3.all[count])
202 return 0;
203 }
204
205 if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
206 return 0;
207
208 if (t1->src.l3num != t2->src.l3num ||
209 t1->dst.protonum != t2->dst.protonum)
210 return 0;
211
212 return 1;
213}
214
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800215static inline int nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
216 const struct nf_conntrack_tuple *tuple,
Patrick McHardyd4156e82007-07-07 22:31:32 -0700217 const struct nf_conntrack_tuple_mask *mask)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800218{
Patrick McHardyd4156e82007-07-07 22:31:32 -0700219 return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
Patrick McHardy380517d2008-01-31 04:40:04 -0800220 __nf_ct_tuple_dst_equal(t, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800221}
222
223#endif /* _NF_CONNTRACK_TUPLE_H */