ip_conntrack_tuple.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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