1 #ifndef _IP_CONNTRACK_TUPLE_H
2 #define _IP_CONNTRACK_TUPLE_H
3 
4 /* A `tuple' is a structure containing the information to uniquely
5   identify a connection.  ie. if two packets have the same tuple, they
6   are in the same connection; if not, they are not.
7 
8   We divide the structure along "manipulatable" and
9   "non-manipulatable" lines, for the benefit of the NAT code.
10 */
11 
12 /* The protocol-specific manipulable parts of the tuple: always in
13    network order! */
14 union ip_conntrack_manip_proto
15 {
16 	/* Add other protocols here. */
17 	u_int16_t all;
18 
19 	struct {
20 		u_int16_t port;
21 	} tcp;
22 	struct {
23 		u_int16_t port;
24 	} udp;
25 	struct {
26 		u_int16_t id;
27 	} icmp;
28 };
29 
30 /* The manipulable part of the tuple. */
31 struct ip_conntrack_manip
32 {
33 	u_int32_t ip;
34 	union ip_conntrack_manip_proto u;
35 };
36 
37 /* This contains the information to distinguish a connection. */
38 struct ip_conntrack_tuple
39 {
40 	struct ip_conntrack_manip src;
41 
42 	/* These are the parts of the tuple which are fixed. */
43 	struct {
44 		u_int32_t ip;
45 		union {
46 			/* Add other protocols here. */
47 			u_int16_t all;
48 
49 			struct {
50 				u_int16_t port;
51 			} tcp;
52 			struct {
53 				u_int16_t port;
54 			} udp;
55 			struct {
56 				u_int8_t type, code;
57 			} icmp;
58 		} u;
59 
60 		/* The protocol. */
61 		u_int16_t protonum;
62 	} dst;
63 };
64 
65 /* This is optimized opposed to a memset of the whole structure.  Everything we
66  * really care about is the  source/destination unions */
67 #define IP_CT_TUPLE_U_BLANK(tuple) 				\
68 	do {							\
69 		(tuple)->src.u.all = 0;				\
70 		(tuple)->dst.u.all = 0;				\
71 	} while (0)
72 
73 enum ip_conntrack_dir
74 {
75 	IP_CT_DIR_ORIGINAL,
76 	IP_CT_DIR_REPLY,
77 	IP_CT_DIR_MAX
78 };
79 
80 #ifdef __KERNEL__
81 
82 #define DUMP_TUPLE(tp)						\
83 DEBUGP("tuple %p: %u %u.%u.%u.%u:%hu -> %u.%u.%u.%u:%hu\n",	\
84        (tp), (tp)->dst.protonum,				\
85        NIPQUAD((tp)->src.ip), ntohs((tp)->src.u.all),		\
86        NIPQUAD((tp)->dst.ip), ntohs((tp)->dst.u.all))
87 
88 #define CTINFO2DIR(ctinfo) ((ctinfo) >= IP_CT_IS_REPLY ? IP_CT_DIR_REPLY : IP_CT_DIR_ORIGINAL)
89 
90 /* If we're the first tuple, it's the original dir. */
91 #define DIRECTION(h) ((enum ip_conntrack_dir)(&(h)->ctrack->tuplehash[1] == (h)))
92 
93 /* Connections have two entries in the hash table: one for each way */
94 struct ip_conntrack_tuple_hash
95 {
96 	struct list_head list;
97 
98 	struct ip_conntrack_tuple tuple;
99 
100 	/* this == &ctrack->tuplehash[DIRECTION(this)]. */
101 	struct ip_conntrack *ctrack;
102 };
103 
104 #endif /* __KERNEL__ */
105 
ip_ct_tuple_src_equal(const struct ip_conntrack_tuple * t1,const struct ip_conntrack_tuple * t2)106 static inline int ip_ct_tuple_src_equal(const struct ip_conntrack_tuple *t1,
107 				        const struct ip_conntrack_tuple *t2)
108 {
109 	return t1->src.ip == t2->src.ip
110 		&& t1->src.u.all == t2->src.u.all;
111 }
112 
ip_ct_tuple_dst_equal(const struct ip_conntrack_tuple * t1,const struct ip_conntrack_tuple * t2)113 static inline int ip_ct_tuple_dst_equal(const struct ip_conntrack_tuple *t1,
114 				        const struct ip_conntrack_tuple *t2)
115 {
116 	return t1->dst.ip == t2->dst.ip
117 		&& t1->dst.u.all == t2->dst.u.all
118 		&& t1->dst.protonum == t2->dst.protonum;
119 }
120 
ip_ct_tuple_equal(const struct ip_conntrack_tuple * t1,const struct ip_conntrack_tuple * t2)121 static inline int ip_ct_tuple_equal(const struct ip_conntrack_tuple *t1,
122 				    const struct ip_conntrack_tuple *t2)
123 {
124 	return ip_ct_tuple_src_equal(t1, t2) && ip_ct_tuple_dst_equal(t1, t2);
125 }
126 
ip_ct_tuple_mask_cmp(const struct ip_conntrack_tuple * t,const struct ip_conntrack_tuple * tuple,const struct ip_conntrack_tuple * mask)127 static inline int ip_ct_tuple_mask_cmp(const struct ip_conntrack_tuple *t,
128 				       const struct ip_conntrack_tuple *tuple,
129 				       const struct ip_conntrack_tuple *mask)
130 {
131 	return !(((t->src.ip ^ tuple->src.ip) & mask->src.ip)
132 		 || ((t->dst.ip ^ tuple->dst.ip) & mask->dst.ip)
133 		 || ((t->src.u.all ^ tuple->src.u.all) & mask->src.u.all)
134 		 || ((t->dst.u.all ^ tuple->dst.u.all) & mask->dst.u.all)
135 		 || ((t->dst.protonum ^ tuple->dst.protonum)
136 		     & mask->dst.protonum));
137 }
138 
139 #endif /* _IP_CONNTRACK_TUPLE_H */
140