Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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 | |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 13 | #include <linux/netfilter/x_tables.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 14 | #include <linux/netfilter/nf_conntrack_tuple_common.h> |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 15 | #include <linux/list_nulls.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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 | |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 25 | #define NF_CT_TUPLE_L3SIZE ARRAY_SIZE(((union nf_inet_addr *)NULL)->all) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 26 | |
| 27 | /* The protocol-specific manipulable parts of the tuple: always in |
| 28 | network order! */ |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 29 | union nf_conntrack_man_proto { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 30 | /* Add other protocols here. */ |
Al Viro | a34c458 | 2007-07-26 17:33:19 +0100 | [diff] [blame] | 31 | __be16 all; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 32 | |
| 33 | struct { |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 34 | __be16 port; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 35 | } tcp; |
| 36 | struct { |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 37 | __be16 port; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 38 | } udp; |
| 39 | struct { |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 40 | __be16 id; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 41 | } icmp; |
| 42 | struct { |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 43 | __be16 port; |
Patrick McHardy | 2bc7804 | 2008-03-20 15:15:55 +0100 | [diff] [blame] | 44 | } dccp; |
| 45 | struct { |
| 46 | __be16 port; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 47 | } sctp; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 48 | struct { |
| 49 | __be16 key; /* GRE key is 32bit, PPtP only uses 16bit */ |
| 50 | } gre; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /* The manipulable part of the tuple. */ |
Eric Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 54 | struct nf_conntrack_man { |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 55 | union nf_inet_addr u3; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 56 | 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 Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 62 | struct nf_conntrack_tuple { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 63 | struct nf_conntrack_man src; |
| 64 | |
| 65 | /* These are the parts of the tuple which are fixed. */ |
| 66 | struct { |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 67 | union nf_inet_addr u3; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 68 | union { |
| 69 | /* Add other protocols here. */ |
Al Viro | a34c458 | 2007-07-26 17:33:19 +0100 | [diff] [blame] | 70 | __be16 all; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 71 | |
| 72 | struct { |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 73 | __be16 port; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 74 | } tcp; |
| 75 | struct { |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 76 | __be16 port; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 77 | } udp; |
| 78 | struct { |
| 79 | u_int8_t type, code; |
| 80 | } icmp; |
| 81 | struct { |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 82 | __be16 port; |
Patrick McHardy | 2bc7804 | 2008-03-20 15:15:55 +0100 | [diff] [blame] | 83 | } dccp; |
| 84 | struct { |
| 85 | __be16 port; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 86 | } sctp; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 87 | struct { |
| 88 | __be16 key; |
| 89 | } gre; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 90 | } 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 Dumazet | fd2c3ef | 2009-11-03 03:26:03 +0000 | [diff] [blame] | 100 | struct nf_conntrack_tuple_mask { |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 101 | struct { |
Jan Engelhardt | 643a2c1 | 2007-12-17 22:43:50 -0800 | [diff] [blame] | 102 | union nf_inet_addr u3; |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 103 | union nf_conntrack_man_proto u; |
| 104 | } src; |
| 105 | }; |
| 106 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 107 | #ifdef __KERNEL__ |
| 108 | |
Patrick McHardy | ef27559 | 2008-03-25 20:07:38 -0700 | [diff] [blame] | 109 | static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t) |
| 110 | { |
| 111 | #ifdef DEBUG |
Harvey Harrison | 3685f25d | 2008-10-31 00:56:49 -0700 | [diff] [blame] | 112 | printk("tuple %p: %u %pI4:%hu -> %pI4:%hu\n", |
Patrick McHardy | ef27559 | 2008-03-25 20:07:38 -0700 | [diff] [blame] | 113 | t, t->dst.protonum, |
Harvey Harrison | 3685f25d | 2008-10-31 00:56:49 -0700 | [diff] [blame] | 114 | &t->src.u3.ip, ntohs(t->src.u.all), |
| 115 | &t->dst.u3.ip, ntohs(t->dst.u.all)); |
Patrick McHardy | ef27559 | 2008-03-25 20:07:38 -0700 | [diff] [blame] | 116 | #endif |
| 117 | } |
| 118 | |
| 119 | static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t) |
| 120 | { |
| 121 | #ifdef DEBUG |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 122 | printk("tuple %p: %u %pI6 %hu -> %pI6 %hu\n", |
Patrick McHardy | ef27559 | 2008-03-25 20:07:38 -0700 | [diff] [blame] | 123 | t, t->dst.protonum, |
Harvey Harrison | 0c6ce78 | 2008-10-28 16:09:23 -0700 | [diff] [blame] | 124 | t->src.u3.all, ntohs(t->src.u.all), |
| 125 | t->dst.u3.all, ntohs(t->dst.u.all)); |
Patrick McHardy | ef27559 | 2008-03-25 20:07:38 -0700 | [diff] [blame] | 126 | #endif |
| 127 | } |
| 128 | |
| 129 | static 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 Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 141 | /* 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 Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 146 | struct nf_conntrack_tuple_hash { |
| 147 | struct hlist_nulls_node hnnode; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 148 | struct nf_conntrack_tuple tuple; |
| 149 | }; |
| 150 | |
| 151 | #endif /* __KERNEL__ */ |
| 152 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 153 | static inline bool __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1, |
| 154 | const struct nf_conntrack_tuple *t2) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 155 | { |
Patrick McHardy | b8beedd | 2008-03-25 20:09:33 -0700 | [diff] [blame] | 156 | return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) && |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 157 | t1->src.u.all == t2->src.u.all && |
Patrick McHardy | 380517d | 2008-01-31 04:40:04 -0800 | [diff] [blame] | 158 | t1->src.l3num == t2->src.l3num); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 161 | static inline bool __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1, |
| 162 | const struct nf_conntrack_tuple *t2) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 163 | { |
Patrick McHardy | b8beedd | 2008-03-25 20:09:33 -0700 | [diff] [blame] | 164 | return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) && |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 165 | t1->dst.u.all == t2->dst.u.all && |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 166 | t1->dst.protonum == t2->dst.protonum); |
| 167 | } |
| 168 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 169 | static inline bool nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1, |
| 170 | const struct nf_conntrack_tuple *t2) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 171 | { |
Patrick McHardy | 380517d | 2008-01-31 04:40:04 -0800 | [diff] [blame] | 172 | return __nf_ct_tuple_src_equal(t1, t2) && |
| 173 | __nf_ct_tuple_dst_equal(t1, t2); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 176 | static inline bool |
| 177 | nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1, |
| 178 | const struct nf_conntrack_tuple_mask *m2) |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 179 | { |
Patrick McHardy | b8beedd | 2008-03-25 20:09:33 -0700 | [diff] [blame] | 180 | return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) && |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 181 | m1->src.u.all == m2->src.u.all); |
| 182 | } |
| 183 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 184 | static inline bool |
| 185 | nf_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 McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 188 | { |
| 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 Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 194 | return false; |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all) |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 198 | return false; |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 199 | |
| 200 | if (t1->src.l3num != t2->src.l3num || |
| 201 | t1->dst.protonum != t2->dst.protonum) |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 202 | return false; |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 203 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 204 | return true; |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Jan Engelhardt | 5f2b4c9 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 207 | static inline bool |
| 208 | nf_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 Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 211 | { |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 212 | return nf_ct_tuple_src_mask_cmp(t, tuple, mask) && |
Patrick McHardy | 380517d | 2008-01-31 04:40:04 -0800 | [diff] [blame] | 213 | __nf_ct_tuple_dst_equal(t, tuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | #endif /* _NF_CONNTRACK_TUPLE_H */ |