Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
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 | ||
643a2c15 | 13 | #include <linux/netfilter/x_tables.h> |
9fb9cbb1 | 14 | #include <linux/netfilter/nf_conntrack_tuple_common.h> |
ea781f19 | 15 | #include <linux/list_nulls.h> |
9fb9cbb1 YK |
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 | ||
643a2c15 | 25 | #define NF_CT_TUPLE_L3SIZE ARRAY_SIZE(((union nf_inet_addr *)NULL)->all) |
9fb9cbb1 | 26 | |
9fb9cbb1 | 27 | /* The manipulable part of the tuple. */ |
fd2c3ef7 | 28 | struct nf_conntrack_man { |
643a2c15 | 29 | union nf_inet_addr u3; |
9fb9cbb1 YK |
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. */ | |
fd2c3ef7 | 36 | struct nf_conntrack_tuple { |
9fb9cbb1 YK |
37 | struct nf_conntrack_man src; |
38 | ||
39 | /* These are the parts of the tuple which are fixed. */ | |
40 | struct { | |
643a2c15 | 41 | union nf_inet_addr u3; |
9fb9cbb1 YK |
42 | union { |
43 | /* Add other protocols here. */ | |
a34c4589 | 44 | __be16 all; |
9fb9cbb1 YK |
45 | |
46 | struct { | |
bff9a89b | 47 | __be16 port; |
9fb9cbb1 YK |
48 | } tcp; |
49 | struct { | |
bff9a89b | 50 | __be16 port; |
9fb9cbb1 YK |
51 | } udp; |
52 | struct { | |
53 | u_int8_t type, code; | |
54 | } icmp; | |
2bc78049 PM |
55 | struct { |
56 | __be16 port; | |
57 | } dccp; | |
9fb9cbb1 | 58 | struct { |
bff9a89b | 59 | __be16 port; |
9fb9cbb1 | 60 | } sctp; |
f09943fe PM |
61 | struct { |
62 | __be16 key; | |
63 | } gre; | |
9fb9cbb1 YK |
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 | ||
fd2c3ef7 | 74 | struct nf_conntrack_tuple_mask { |
d4156e8c | 75 | struct { |
643a2c15 | 76 | union nf_inet_addr u3; |
d4156e8c PM |
77 | union nf_conntrack_man_proto u; |
78 | } src; | |
79 | }; | |
80 | ||
ef27559b PM |
81 | static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t) |
82 | { | |
83 | #ifdef DEBUG | |
3685f25d | 84 | printk("tuple %p: %u %pI4:%hu -> %pI4:%hu\n", |
ef27559b | 85 | t, t->dst.protonum, |
3685f25d HH |
86 | &t->src.u3.ip, ntohs(t->src.u.all), |
87 | &t->dst.u3.ip, ntohs(t->dst.u.all)); | |
ef27559b PM |
88 | #endif |
89 | } | |
90 | ||
91 | static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t) | |
92 | { | |
93 | #ifdef DEBUG | |
5b095d98 | 94 | printk("tuple %p: %u %pI6 %hu -> %pI6 %hu\n", |
ef27559b | 95 | t, t->dst.protonum, |
0c6ce78a HH |
96 | t->src.u3.all, ntohs(t->src.u.all), |
97 | t->dst.u3.all, ntohs(t->dst.u.all)); | |
ef27559b PM |
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 | ||
9fb9cbb1 YK |
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 */ | |
ea781f19 ED |
118 | struct nf_conntrack_tuple_hash { |
119 | struct hlist_nulls_node hnnode; | |
9fb9cbb1 YK |
120 | struct nf_conntrack_tuple tuple; |
121 | }; | |
122 | ||
5f2b4c90 JE |
123 | static inline bool __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1, |
124 | const struct nf_conntrack_tuple *t2) | |
9fb9cbb1 | 125 | { |
b8beedd2 | 126 | return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) && |
9fb9cbb1 | 127 | t1->src.u.all == t2->src.u.all && |
380517de | 128 | t1->src.l3num == t2->src.l3num); |
9fb9cbb1 YK |
129 | } |
130 | ||
5f2b4c90 JE |
131 | static inline bool __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1, |
132 | const struct nf_conntrack_tuple *t2) | |
9fb9cbb1 | 133 | { |
b8beedd2 | 134 | return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) && |
9fb9cbb1 | 135 | t1->dst.u.all == t2->dst.u.all && |
9fb9cbb1 YK |
136 | t1->dst.protonum == t2->dst.protonum); |
137 | } | |
138 | ||
5f2b4c90 JE |
139 | static inline bool nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1, |
140 | const struct nf_conntrack_tuple *t2) | |
9fb9cbb1 | 141 | { |
380517de PM |
142 | return __nf_ct_tuple_src_equal(t1, t2) && |
143 | __nf_ct_tuple_dst_equal(t1, t2); | |
9fb9cbb1 YK |
144 | } |
145 | ||
5f2b4c90 JE |
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) | |
d4156e8c | 149 | { |
b8beedd2 | 150 | return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) && |
d4156e8c PM |
151 | m1->src.u.all == m2->src.u.all); |
152 | } | |
153 | ||
5f2b4c90 JE |
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) | |
d4156e8c PM |
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]) | |
5f2b4c90 | 164 | return false; |
d4156e8c PM |
165 | } |
166 | ||
167 | if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all) | |
5f2b4c90 | 168 | return false; |
d4156e8c PM |
169 | |
170 | if (t1->src.l3num != t2->src.l3num || | |
171 | t1->dst.protonum != t2->dst.protonum) | |
5f2b4c90 | 172 | return false; |
d4156e8c | 173 | |
5f2b4c90 | 174 | return true; |
d4156e8c PM |
175 | } |
176 | ||
5f2b4c90 JE |
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) | |
9fb9cbb1 | 181 | { |
d4156e8c | 182 | return nf_ct_tuple_src_mask_cmp(t, tuple, mask) && |
380517de | 183 | __nf_ct_tuple_dst_equal(t, tuple); |
9fb9cbb1 YK |
184 | } |
185 | ||
186 | #endif /* _NF_CONNTRACK_TUPLE_H */ |