/include/net/netfilter/nf_conntrack_tuple.h

https://bitbucket.org/thekraven/iscream_thunderc-2.6.35 · C++ Header · 216 lines · 158 code · 31 blank · 27 comment · 12 complexity · d077289706c478f4b97e85634c0a770f MD5 · raw file

  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. #ifndef _NF_CONNTRACK_TUPLE_H
  10. #define _NF_CONNTRACK_TUPLE_H
  11. #include <linux/netfilter/x_tables.h>
  12. #include <linux/netfilter/nf_conntrack_tuple_common.h>
  13. #include <linux/list_nulls.h>
  14. /* A `tuple' is a structure containing the information to uniquely
  15. identify a connection. ie. if two packets have the same tuple, they
  16. are in the same connection; if not, they are not.
  17. We divide the structure along "manipulatable" and
  18. "non-manipulatable" lines, for the benefit of the NAT code.
  19. */
  20. #define NF_CT_TUPLE_L3SIZE ARRAY_SIZE(((union nf_inet_addr *)NULL)->all)
  21. /* The protocol-specific manipulable parts of the tuple: always in
  22. network order! */
  23. union nf_conntrack_man_proto {
  24. /* Add other protocols here. */
  25. __be16 all;
  26. struct {
  27. __be16 port;
  28. } tcp;
  29. struct {
  30. __be16 port;
  31. } udp;
  32. struct {
  33. __be16 id;
  34. } icmp;
  35. struct {
  36. __be16 port;
  37. } dccp;
  38. struct {
  39. __be16 port;
  40. } sctp;
  41. struct {
  42. __be16 key; /* GRE key is 32bit, PPtP only uses 16bit */
  43. } gre;
  44. };
  45. /* The manipulable part of the tuple. */
  46. struct nf_conntrack_man {
  47. union nf_inet_addr u3;
  48. union nf_conntrack_man_proto u;
  49. /* Layer 3 protocol */
  50. u_int16_t l3num;
  51. };
  52. /* This contains the information to distinguish a connection. */
  53. struct nf_conntrack_tuple {
  54. struct nf_conntrack_man src;
  55. /* These are the parts of the tuple which are fixed. */
  56. struct {
  57. union nf_inet_addr u3;
  58. union {
  59. /* Add other protocols here. */
  60. __be16 all;
  61. struct {
  62. __be16 port;
  63. } tcp;
  64. struct {
  65. __be16 port;
  66. } udp;
  67. struct {
  68. u_int8_t type, code;
  69. } icmp;
  70. struct {
  71. __be16 port;
  72. } dccp;
  73. struct {
  74. __be16 port;
  75. } sctp;
  76. struct {
  77. __be16 key;
  78. } gre;
  79. } u;
  80. /* The protocol. */
  81. u_int8_t protonum;
  82. /* The direction (for tuplehash) */
  83. u_int8_t dir;
  84. } dst;
  85. };
  86. struct nf_conntrack_tuple_mask {
  87. struct {
  88. union nf_inet_addr u3;
  89. union nf_conntrack_man_proto u;
  90. } src;
  91. };
  92. #ifdef __KERNEL__
  93. static inline void nf_ct_dump_tuple_ip(const struct nf_conntrack_tuple *t)
  94. {
  95. #ifdef DEBUG
  96. printk("tuple %p: %u %pI4:%hu -> %pI4:%hu\n",
  97. t, t->dst.protonum,
  98. &t->src.u3.ip, ntohs(t->src.u.all),
  99. &t->dst.u3.ip, ntohs(t->dst.u.all));
  100. #endif
  101. }
  102. static inline void nf_ct_dump_tuple_ipv6(const struct nf_conntrack_tuple *t)
  103. {
  104. #ifdef DEBUG
  105. printk("tuple %p: %u %pI6 %hu -> %pI6 %hu\n",
  106. t, t->dst.protonum,
  107. t->src.u3.all, ntohs(t->src.u.all),
  108. t->dst.u3.all, ntohs(t->dst.u.all));
  109. #endif
  110. }
  111. static inline void nf_ct_dump_tuple(const struct nf_conntrack_tuple *t)
  112. {
  113. switch (t->src.l3num) {
  114. case AF_INET:
  115. nf_ct_dump_tuple_ip(t);
  116. break;
  117. case AF_INET6:
  118. nf_ct_dump_tuple_ipv6(t);
  119. break;
  120. }
  121. }
  122. /* If we're the first tuple, it's the original dir. */
  123. #define NF_CT_DIRECTION(h) \
  124. ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
  125. /* Connections have two entries in the hash table: one for each way */
  126. struct nf_conntrack_tuple_hash {
  127. struct hlist_nulls_node hnnode;
  128. struct nf_conntrack_tuple tuple;
  129. };
  130. #endif /* __KERNEL__ */
  131. static inline bool __nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
  132. const struct nf_conntrack_tuple *t2)
  133. {
  134. return (nf_inet_addr_cmp(&t1->src.u3, &t2->src.u3) &&
  135. t1->src.u.all == t2->src.u.all &&
  136. t1->src.l3num == t2->src.l3num);
  137. }
  138. static inline bool __nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
  139. const struct nf_conntrack_tuple *t2)
  140. {
  141. return (nf_inet_addr_cmp(&t1->dst.u3, &t2->dst.u3) &&
  142. t1->dst.u.all == t2->dst.u.all &&
  143. t1->dst.protonum == t2->dst.protonum);
  144. }
  145. static inline bool nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
  146. const struct nf_conntrack_tuple *t2)
  147. {
  148. return __nf_ct_tuple_src_equal(t1, t2) &&
  149. __nf_ct_tuple_dst_equal(t1, t2);
  150. }
  151. static inline bool
  152. nf_ct_tuple_mask_equal(const struct nf_conntrack_tuple_mask *m1,
  153. const struct nf_conntrack_tuple_mask *m2)
  154. {
  155. return (nf_inet_addr_cmp(&m1->src.u3, &m2->src.u3) &&
  156. m1->src.u.all == m2->src.u.all);
  157. }
  158. static inline bool
  159. nf_ct_tuple_src_mask_cmp(const struct nf_conntrack_tuple *t1,
  160. const struct nf_conntrack_tuple *t2,
  161. const struct nf_conntrack_tuple_mask *mask)
  162. {
  163. int count;
  164. for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++) {
  165. if ((t1->src.u3.all[count] ^ t2->src.u3.all[count]) &
  166. mask->src.u3.all[count])
  167. return false;
  168. }
  169. if ((t1->src.u.all ^ t2->src.u.all) & mask->src.u.all)
  170. return false;
  171. if (t1->src.l3num != t2->src.l3num ||
  172. t1->dst.protonum != t2->dst.protonum)
  173. return false;
  174. return true;
  175. }
  176. static inline bool
  177. nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
  178. const struct nf_conntrack_tuple *tuple,
  179. const struct nf_conntrack_tuple_mask *mask)
  180. {
  181. return nf_ct_tuple_src_mask_cmp(t, tuple, mask) &&
  182. __nf_ct_tuple_dst_equal(t, tuple);
  183. }
  184. #endif /* _NF_CONNTRACK_TUPLE_H */