nf_conntrack_tuple.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/nf_conntrack_tuple_common.h>
  12. /* A `tuple' is a structure containing the information to uniquely
  13. identify a connection. ie. if two packets have the same tuple, they
  14. are in the same connection; if not, they are not.
  15. We divide the structure along "manipulatable" and
  16. "non-manipulatable" lines, for the benefit of the NAT code.
  17. */
  18. #define NF_CT_TUPLE_L3SIZE 4
  19. /* The l3 protocol-specific manipulable parts of the tuple: always in
  20. network order! */
  21. union nf_conntrack_address {
  22. u_int32_t all[NF_CT_TUPLE_L3SIZE];
  23. __be32 ip;
  24. __be32 ip6[4];
  25. };
  26. /* The protocol-specific manipulable parts of the tuple: always in
  27. network order! */
  28. union nf_conntrack_man_proto
  29. {
  30. /* Add other protocols here. */
  31. u_int16_t all;
  32. struct {
  33. __be16 port;
  34. } tcp;
  35. struct {
  36. __be16 port;
  37. } udp;
  38. struct {
  39. __be16 id;
  40. } icmp;
  41. struct {
  42. __be16 port;
  43. } sctp;
  44. struct {
  45. __be16 key; /* GRE key is 32bit, PPtP only uses 16bit */
  46. } gre;
  47. };
  48. /* The manipulable part of the tuple. */
  49. struct nf_conntrack_man
  50. {
  51. union nf_conntrack_address u3;
  52. union nf_conntrack_man_proto u;
  53. /* Layer 3 protocol */
  54. u_int16_t l3num;
  55. };
  56. /* This contains the information to distinguish a connection. */
  57. struct nf_conntrack_tuple
  58. {
  59. struct nf_conntrack_man src;
  60. /* These are the parts of the tuple which are fixed. */
  61. struct {
  62. union nf_conntrack_address u3;
  63. union {
  64. /* Add other protocols here. */
  65. u_int16_t all;
  66. struct {
  67. __be16 port;
  68. } tcp;
  69. struct {
  70. __be16 port;
  71. } udp;
  72. struct {
  73. u_int8_t type, code;
  74. } icmp;
  75. struct {
  76. __be16 port;
  77. } sctp;
  78. struct {
  79. __be16 key;
  80. } gre;
  81. } u;
  82. /* The protocol. */
  83. u_int8_t protonum;
  84. /* The direction (for tuplehash) */
  85. u_int8_t dir;
  86. } dst;
  87. };
  88. /* This is optimized opposed to a memset of the whole structure. Everything we
  89. * really care about is the source/destination unions */
  90. #define NF_CT_TUPLE_U_BLANK(tuple) \
  91. do { \
  92. (tuple)->src.u.all = 0; \
  93. (tuple)->dst.u.all = 0; \
  94. memset(&(tuple)->src.u3, 0, sizeof((tuple)->src.u3)); \
  95. memset(&(tuple)->dst.u3, 0, sizeof((tuple)->dst.u3)); \
  96. } while (0)
  97. #ifdef __KERNEL__
  98. #define NF_CT_DUMP_TUPLE(tp) \
  99. DEBUGP("tuple %p: %u %u " NIP6_FMT " %hu -> " NIP6_FMT " %hu\n", \
  100. (tp), (tp)->src.l3num, (tp)->dst.protonum, \
  101. NIP6(*(struct in6_addr *)(tp)->src.u3.all), ntohs((tp)->src.u.all), \
  102. NIP6(*(struct in6_addr *)(tp)->dst.u3.all), ntohs((tp)->dst.u.all))
  103. /* If we're the first tuple, it's the original dir. */
  104. #define NF_CT_DIRECTION(h) \
  105. ((enum ip_conntrack_dir)(h)->tuple.dst.dir)
  106. /* Connections have two entries in the hash table: one for each way */
  107. struct nf_conntrack_tuple_hash
  108. {
  109. struct list_head list;
  110. struct nf_conntrack_tuple tuple;
  111. };
  112. #endif /* __KERNEL__ */
  113. static inline int nf_ct_tuple_src_equal(const struct nf_conntrack_tuple *t1,
  114. const struct nf_conntrack_tuple *t2)
  115. {
  116. return (t1->src.u3.all[0] == t2->src.u3.all[0] &&
  117. t1->src.u3.all[1] == t2->src.u3.all[1] &&
  118. t1->src.u3.all[2] == t2->src.u3.all[2] &&
  119. t1->src.u3.all[3] == t2->src.u3.all[3] &&
  120. t1->src.u.all == t2->src.u.all &&
  121. t1->src.l3num == t2->src.l3num &&
  122. t1->dst.protonum == t2->dst.protonum);
  123. }
  124. static inline int nf_ct_tuple_dst_equal(const struct nf_conntrack_tuple *t1,
  125. const struct nf_conntrack_tuple *t2)
  126. {
  127. return (t1->dst.u3.all[0] == t2->dst.u3.all[0] &&
  128. t1->dst.u3.all[1] == t2->dst.u3.all[1] &&
  129. t1->dst.u3.all[2] == t2->dst.u3.all[2] &&
  130. t1->dst.u3.all[3] == t2->dst.u3.all[3] &&
  131. t1->dst.u.all == t2->dst.u.all &&
  132. t1->src.l3num == t2->src.l3num &&
  133. t1->dst.protonum == t2->dst.protonum);
  134. }
  135. static inline int nf_ct_tuple_equal(const struct nf_conntrack_tuple *t1,
  136. const struct nf_conntrack_tuple *t2)
  137. {
  138. return nf_ct_tuple_src_equal(t1, t2) && nf_ct_tuple_dst_equal(t1, t2);
  139. }
  140. static inline int nf_ct_tuple_mask_cmp(const struct nf_conntrack_tuple *t,
  141. const struct nf_conntrack_tuple *tuple,
  142. const struct nf_conntrack_tuple *mask)
  143. {
  144. int count = 0;
  145. for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
  146. if ((t->src.u3.all[count] ^ tuple->src.u3.all[count]) &
  147. mask->src.u3.all[count])
  148. return 0;
  149. }
  150. for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
  151. if ((t->dst.u3.all[count] ^ tuple->dst.u3.all[count]) &
  152. mask->dst.u3.all[count])
  153. return 0;
  154. }
  155. if ((t->src.u.all ^ tuple->src.u.all) & mask->src.u.all ||
  156. (t->dst.u.all ^ tuple->dst.u.all) & mask->dst.u.all ||
  157. (t->src.l3num ^ tuple->src.l3num) & mask->src.l3num ||
  158. (t->dst.protonum ^ tuple->dst.protonum) & mask->dst.protonum)
  159. return 0;
  160. return 1;
  161. }
  162. #endif /* _NF_CONNTRACK_TUPLE_H */