blob: 4ee44c84a304b8ee97ee6ddcc38d08ae6d88356c [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>
Eric Dumazetea781f12009-03-25 21:05:46 +010015#include <linux/list_nulls.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080016
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
Jan Engelhardt643a2c12007-12-17 22:43:50 -080025#define NF_CT_TUPLE_L3SIZE ARRAY_SIZE(((union nf_inet_addr *)NULL)->all)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080026
27/* The protocol-specific manipulable parts of the tuple: always in
28 network order! */
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000029union nf_conntrack_man_proto {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030 /* 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. */
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000054struct nf_conntrack_man {
Jan Engelhardt643a2c12007-12-17 22:43:50 -080055 union nf_inet_addr u3;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080056 union nf_conntrack_man_proto u;
57 /* Layer 3 protocol */
58 u_int16_t l3num;
59};
60
61/* This contains the information to distinguish a connection. */
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000062struct nf_conntrack_tuple {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080063 struct nf_conntrack_man src;
64
65 /* These are the parts of the tuple which are fixed. */
66 struct {
Jan Engelhardt643a2c12007-12-17 22:43:50 -080067 union nf_inet_addr u3;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068 union {
69 /* Add other protocols here. */
Al Viroa34c4582007-07-26 17:33:19 +010070 __be16 all;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080071
72 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080073 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080074 } tcp;
75 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080076 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080077 } udp;
78 struct {
79 u_int8_t type, code;
80 } icmp;
81 struct {
Patrick McHardybff9a892006-12-02 22:05:08 -080082 __be16 port;
Patrick McHardy2bc78042008-03-20 15:15:55 +010083 } dccp;
84 struct {
85 __be16 port;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080086 } sctp;
Patrick McHardyf09943f2006-12-02 22:09:41 -080087 struct {
88 __be16 key;
89 } gre;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080090 } u;
91
92 /* The protocol. */
93 u_int8_t protonum;
94
95 /* The direction (for tuplehash) */
96 u_int8_t dir;
97 } dst;
98};
99
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000100struct nf_conntrack_tuple_mask {
Patrick McHardyd4156e82007-07-07 22:31:32 -0700101 struct {
Jan Engelhardt643a2c12007-12-17 22:43:50 -0800102 union nf_inet_addr u3;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700103 union nf_conntrack_man_proto u;
104 } src;
105};
106
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800107#ifdef __KERNEL__
108
Patrick McHardyef275592008-03-25 20:07:38 -0700109static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t)
110{
111#ifdef DEBUG
Harvey Harrison3685f25d2008-10-31 00:56:49 -0700112 printk("tuple %p: %u %pI4:%hu -> %pI4:%hu\n",
Patrick McHardyef275592008-03-25 20:07:38 -0700113 t, t->dst.protonum,
Harvey Harrison3685f25d2008-10-31 00:56:49 -0700114 &t->src.u3.ip, ntohs(t->src.u.all),
115 &t->dst.u3.ip, ntohs(t->dst.u.all));
Patrick McHardyef275592008-03-25 20:07:38 -0700116#endif
117}
118
119static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t)
120{
121#ifdef DEBUG
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700122 printk("tuple %p: %u %pI6 %hu -> %pI6 %hu\n",
Patrick McHardyef275592008-03-25 20:07:38 -0700123 t, t->dst.protonum,
Harvey Harrison0c6ce782008-10-28 16:09:23 -0700124 t->src.u3.all, ntohs(t->src.u.all),
125 t->dst.u3.all, ntohs(t->dst.u.all));
Patrick McHardyef275592008-03-25 20:07:38 -0700126#endif
127}
128
129static inline void nf_ct_dump_tuple(const struct nf_conntrack_tuple *t)
130{
131 switch (t->src.l3num) {
132 case AF_INET:
133 nf_ct_dump_tuple_ip(t);
134 break;
135 case AF_INET6:
136 nf_ct_dump_tuple_ipv6(t);
137 break;
138 }
139}
140
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800141/* If we're the first tuple, it's the original dir. */
142#define NF_CT_DIRECTION(h) \
143 ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
144
145/* Connections have two entries in the hash table: one for each way */
Eric Dumazetea781f12009-03-25 21:05:46 +0100146struct nf_conntrack_tuple_hash {
147 struct hlist_nulls_node hnnode;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800148 struct nf_conntrack_tuple tuple;
149};
150
151#endif /* __KERNEL__ */
152
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200153static inline bool __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
154 const struct nf_conntrack_tuple *t2)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800155{
Patrick McHardyb8beedd2008-03-25 20:09:33 -0700156 return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) &&
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800157 t1->src.u.all == t2->src.u.all &&
Patrick McHardy380517d2008-01-31 04:40:04 -0800158 t1->src.l3num == t2->src.l3num);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800159}
160
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200161static inline bool __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
162 const struct nf_conntrack_tuple *t2)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800163{
Patrick McHardyb8beedd2008-03-25 20:09:33 -0700164 return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) &&
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800165 t1->dst.u.all == t2->dst.u.all &&
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800166 t1->dst.protonum == t2->dst.protonum);
167}
168
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200169static inline bool nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
170 const struct nf_conntrack_tuple *t2)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800171{
Patrick McHardy380517d2008-01-31 04:40:04 -0800172 return __nf_ct_tuple_src_equal(t1, t2) &&
173 __nf_ct_tuple_dst_equal(t1, t2);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800174}
175
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200176static inline bool
177nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
178 const struct nf_conntrack_tuple_mask *m2)
Patrick McHardyd4156e82007-07-07 22:31:32 -0700179{
Patrick McHardyb8beedd2008-03-25 20:09:33 -0700180 return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) &&
Patrick McHardyd4156e82007-07-07 22:31:32 -0700181 m1->src.u.all == m2->src.u.all);
182}
183
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200184static inline bool
185nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
186 const struct nf_conntrack_tuple *t2,
187 const struct nf_conntrack_tuple_mask *mask)
Patrick McHardyd4156e82007-07-07 22:31:32 -0700188{
189 int count;
190
191 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
192 if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
193 mask->src.u3.all[count])
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200194 return false;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700195 }
196
197 if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200198 return false;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700199
200 if (t1->src.l3num != t2->src.l3num ||
201 t1->dst.protonum != t2->dst.protonum)
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200202 return false;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700203
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200204 return true;
Patrick McHardyd4156e82007-07-07 22:31:32 -0700205}
206
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200207static inline bool
208nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
209 const struct nf_conntrack_tuple *tuple,
210 const struct nf_conntrack_tuple_mask *mask)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211{
Patrick McHardyd4156e82007-07-07 22:31:32 -0700212 return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
Patrick McHardy380517d2008-01-31 04:40:04 -0800213 __nf_ct_tuple_dst_equal(t, tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214}
215
216#endif /* _NF_CONNTRACK_TUPLE_H */