conntrack_linux.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package nl
  2. import "unsafe"
  3. // Track the message sizes for the correct serialization/deserialization
  4. const (
  5. SizeofNfgenmsg = 4
  6. SizeofNfattr = 4
  7. SizeofNfConntrack = 376
  8. SizeofNfctTupleHead = 52
  9. )
  10. var L4ProtoMap = map[uint8]string{
  11. 6: "tcp",
  12. 17: "udp",
  13. }
  14. // All the following constants are coming from:
  15. // https://github.com/torvalds/linux/blob/master/include/uapi/linux/netfilter/nfnetlink_conntrack.h
  16. // enum cntl_msg_types {
  17. // IPCTNL_MSG_CT_NEW,
  18. // IPCTNL_MSG_CT_GET,
  19. // IPCTNL_MSG_CT_DELETE,
  20. // IPCTNL_MSG_CT_GET_CTRZERO,
  21. // IPCTNL_MSG_CT_GET_STATS_CPU,
  22. // IPCTNL_MSG_CT_GET_STATS,
  23. // IPCTNL_MSG_CT_GET_DYING,
  24. // IPCTNL_MSG_CT_GET_UNCONFIRMED,
  25. //
  26. // IPCTNL_MSG_MAX
  27. // };
  28. const (
  29. IPCTNL_MSG_CT_GET = 1
  30. IPCTNL_MSG_CT_DELETE = 2
  31. )
  32. // #define NFNETLINK_V0 0
  33. const (
  34. NFNETLINK_V0 = 0
  35. )
  36. // #define NLA_F_NESTED (1 << 15)
  37. const (
  38. NLA_F_NESTED = (1 << 15)
  39. )
  40. // enum ctattr_type {
  41. // CTA_UNSPEC,
  42. // CTA_TUPLE_ORIG,
  43. // CTA_TUPLE_REPLY,
  44. // CTA_STATUS,
  45. // CTA_PROTOINFO,
  46. // CTA_HELP,
  47. // CTA_NAT_SRC,
  48. // #define CTA_NAT CTA_NAT_SRC /* backwards compatibility */
  49. // CTA_TIMEOUT,
  50. // CTA_MARK,
  51. // CTA_COUNTERS_ORIG,
  52. // CTA_COUNTERS_REPLY,
  53. // CTA_USE,
  54. // CTA_ID,
  55. // CTA_NAT_DST,
  56. // CTA_TUPLE_MASTER,
  57. // CTA_SEQ_ADJ_ORIG,
  58. // CTA_NAT_SEQ_ADJ_ORIG = CTA_SEQ_ADJ_ORIG,
  59. // CTA_SEQ_ADJ_REPLY,
  60. // CTA_NAT_SEQ_ADJ_REPLY = CTA_SEQ_ADJ_REPLY,
  61. // CTA_SECMARK, /* obsolete */
  62. // CTA_ZONE,
  63. // CTA_SECCTX,
  64. // CTA_TIMESTAMP,
  65. // CTA_MARK_MASK,
  66. // CTA_LABELS,
  67. // CTA_LABELS_MASK,
  68. // __CTA_MAX
  69. // };
  70. const (
  71. CTA_TUPLE_ORIG = 1
  72. CTA_TUPLE_REPLY = 2
  73. CTA_STATUS = 3
  74. CTA_TIMEOUT = 7
  75. CTA_MARK = 8
  76. CTA_COUNTERS_ORIG = 9
  77. CTA_COUNTERS_REPLY = 10
  78. CTA_PROTOINFO = 4
  79. )
  80. // enum ctattr_tuple {
  81. // CTA_TUPLE_UNSPEC,
  82. // CTA_TUPLE_IP,
  83. // CTA_TUPLE_PROTO,
  84. // CTA_TUPLE_ZONE,
  85. // __CTA_TUPLE_MAX
  86. // };
  87. // #define CTA_TUPLE_MAX (__CTA_TUPLE_MAX - 1)
  88. const (
  89. CTA_TUPLE_IP = 1
  90. CTA_TUPLE_PROTO = 2
  91. )
  92. // enum ctattr_ip {
  93. // CTA_IP_UNSPEC,
  94. // CTA_IP_V4_SRC,
  95. // CTA_IP_V4_DST,
  96. // CTA_IP_V6_SRC,
  97. // CTA_IP_V6_DST,
  98. // __CTA_IP_MAX
  99. // };
  100. // #define CTA_IP_MAX (__CTA_IP_MAX - 1)
  101. const (
  102. CTA_IP_V4_SRC = 1
  103. CTA_IP_V4_DST = 2
  104. CTA_IP_V6_SRC = 3
  105. CTA_IP_V6_DST = 4
  106. )
  107. // enum ctattr_l4proto {
  108. // CTA_PROTO_UNSPEC,
  109. // CTA_PROTO_NUM,
  110. // CTA_PROTO_SRC_PORT,
  111. // CTA_PROTO_DST_PORT,
  112. // CTA_PROTO_ICMP_ID,
  113. // CTA_PROTO_ICMP_TYPE,
  114. // CTA_PROTO_ICMP_CODE,
  115. // CTA_PROTO_ICMPV6_ID,
  116. // CTA_PROTO_ICMPV6_TYPE,
  117. // CTA_PROTO_ICMPV6_CODE,
  118. // __CTA_PROTO_MAX
  119. // };
  120. // #define CTA_PROTO_MAX (__CTA_PROTO_MAX - 1)
  121. const (
  122. CTA_PROTO_NUM = 1
  123. CTA_PROTO_SRC_PORT = 2
  124. CTA_PROTO_DST_PORT = 3
  125. )
  126. // enum ctattr_protoinfo {
  127. // CTA_PROTOINFO_UNSPEC,
  128. // CTA_PROTOINFO_TCP,
  129. // CTA_PROTOINFO_DCCP,
  130. // CTA_PROTOINFO_SCTP,
  131. // __CTA_PROTOINFO_MAX
  132. // };
  133. // #define CTA_PROTOINFO_MAX (__CTA_PROTOINFO_MAX - 1)
  134. const (
  135. CTA_PROTOINFO_TCP = 1
  136. )
  137. // enum ctattr_protoinfo_tcp {
  138. // CTA_PROTOINFO_TCP_UNSPEC,
  139. // CTA_PROTOINFO_TCP_STATE,
  140. // CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
  141. // CTA_PROTOINFO_TCP_WSCALE_REPLY,
  142. // CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
  143. // CTA_PROTOINFO_TCP_FLAGS_REPLY,
  144. // __CTA_PROTOINFO_TCP_MAX
  145. // };
  146. // #define CTA_PROTOINFO_TCP_MAX (__CTA_PROTOINFO_TCP_MAX - 1)
  147. const (
  148. CTA_PROTOINFO_TCP_STATE = 1
  149. CTA_PROTOINFO_TCP_WSCALE_ORIGINAL = 2
  150. CTA_PROTOINFO_TCP_WSCALE_REPLY = 3
  151. CTA_PROTOINFO_TCP_FLAGS_ORIGINAL = 4
  152. CTA_PROTOINFO_TCP_FLAGS_REPLY = 5
  153. )
  154. // enum ctattr_counters {
  155. // CTA_COUNTERS_UNSPEC,
  156. // CTA_COUNTERS_PACKETS, /* 64bit counters */
  157. // CTA_COUNTERS_BYTES, /* 64bit counters */
  158. // CTA_COUNTERS32_PACKETS, /* old 32bit counters, unused */
  159. // CTA_COUNTERS32_BYTES, /* old 32bit counters, unused */
  160. // CTA_COUNTERS_PAD,
  161. // __CTA_COUNTERS_M
  162. // };
  163. // #define CTA_COUNTERS_MAX (__CTA_COUNTERS_MAX - 1)
  164. const (
  165. CTA_COUNTERS_PACKETS = 1
  166. CTA_COUNTERS_BYTES = 2
  167. )
  168. // /* General form of address family dependent message.
  169. // */
  170. // struct nfgenmsg {
  171. // __u8 nfgen_family; /* AF_xxx */
  172. // __u8 version; /* nfnetlink version */
  173. // __be16 res_id; /* resource id */
  174. // };
  175. type Nfgenmsg struct {
  176. NfgenFamily uint8
  177. Version uint8
  178. ResId uint16 // big endian
  179. }
  180. func (msg *Nfgenmsg) Len() int {
  181. return SizeofNfgenmsg
  182. }
  183. func DeserializeNfgenmsg(b []byte) *Nfgenmsg {
  184. return (*Nfgenmsg)(unsafe.Pointer(&b[0:SizeofNfgenmsg][0]))
  185. }
  186. func (msg *Nfgenmsg) Serialize() []byte {
  187. return (*(*[SizeofNfgenmsg]byte)(unsafe.Pointer(msg)))[:]
  188. }