conntrack_linux.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. const (
  37. NLA_F_NESTED uint16 = (1 << 15) // #define NLA_F_NESTED (1 << 15)
  38. NLA_F_NET_BYTEORDER uint16 = (1 << 14) // #define NLA_F_NESTED (1 << 14)
  39. NLA_TYPE_MASK = ^(NLA_F_NESTED | NLA_F_NET_BYTEORDER)
  40. )
  41. // enum ctattr_type {
  42. // CTA_UNSPEC,
  43. // CTA_TUPLE_ORIG,
  44. // CTA_TUPLE_REPLY,
  45. // CTA_STATUS,
  46. // CTA_PROTOINFO,
  47. // CTA_HELP,
  48. // CTA_NAT_SRC,
  49. // #define CTA_NAT CTA_NAT_SRC /* backwards compatibility */
  50. // CTA_TIMEOUT,
  51. // CTA_MARK,
  52. // CTA_COUNTERS_ORIG,
  53. // CTA_COUNTERS_REPLY,
  54. // CTA_USE,
  55. // CTA_ID,
  56. // CTA_NAT_DST,
  57. // CTA_TUPLE_MASTER,
  58. // CTA_SEQ_ADJ_ORIG,
  59. // CTA_NAT_SEQ_ADJ_ORIG = CTA_SEQ_ADJ_ORIG,
  60. // CTA_SEQ_ADJ_REPLY,
  61. // CTA_NAT_SEQ_ADJ_REPLY = CTA_SEQ_ADJ_REPLY,
  62. // CTA_SECMARK, /* obsolete */
  63. // CTA_ZONE,
  64. // CTA_SECCTX,
  65. // CTA_TIMESTAMP,
  66. // CTA_MARK_MASK,
  67. // CTA_LABELS,
  68. // CTA_LABELS_MASK,
  69. // __CTA_MAX
  70. // };
  71. const (
  72. CTA_TUPLE_ORIG = 1
  73. CTA_TUPLE_REPLY = 2
  74. CTA_STATUS = 3
  75. CTA_PROTOINFO = 4
  76. CTA_TIMEOUT = 7
  77. CTA_MARK = 8
  78. CTA_COUNTERS_ORIG = 9
  79. CTA_COUNTERS_REPLY = 10
  80. CTA_USE = 11
  81. CTA_ID = 12
  82. CTA_TIMESTAMP = 20
  83. )
  84. // enum ctattr_tuple {
  85. // CTA_TUPLE_UNSPEC,
  86. // CTA_TUPLE_IP,
  87. // CTA_TUPLE_PROTO,
  88. // CTA_TUPLE_ZONE,
  89. // __CTA_TUPLE_MAX
  90. // };
  91. // #define CTA_TUPLE_MAX (__CTA_TUPLE_MAX - 1)
  92. const (
  93. CTA_TUPLE_IP = 1
  94. CTA_TUPLE_PROTO = 2
  95. )
  96. // enum ctattr_ip {
  97. // CTA_IP_UNSPEC,
  98. // CTA_IP_V4_SRC,
  99. // CTA_IP_V4_DST,
  100. // CTA_IP_V6_SRC,
  101. // CTA_IP_V6_DST,
  102. // __CTA_IP_MAX
  103. // };
  104. // #define CTA_IP_MAX (__CTA_IP_MAX - 1)
  105. const (
  106. CTA_IP_V4_SRC = 1
  107. CTA_IP_V4_DST = 2
  108. CTA_IP_V6_SRC = 3
  109. CTA_IP_V6_DST = 4
  110. )
  111. // enum ctattr_l4proto {
  112. // CTA_PROTO_UNSPEC,
  113. // CTA_PROTO_NUM,
  114. // CTA_PROTO_SRC_PORT,
  115. // CTA_PROTO_DST_PORT,
  116. // CTA_PROTO_ICMP_ID,
  117. // CTA_PROTO_ICMP_TYPE,
  118. // CTA_PROTO_ICMP_CODE,
  119. // CTA_PROTO_ICMPV6_ID,
  120. // CTA_PROTO_ICMPV6_TYPE,
  121. // CTA_PROTO_ICMPV6_CODE,
  122. // __CTA_PROTO_MAX
  123. // };
  124. // #define CTA_PROTO_MAX (__CTA_PROTO_MAX - 1)
  125. const (
  126. CTA_PROTO_NUM = 1
  127. CTA_PROTO_SRC_PORT = 2
  128. CTA_PROTO_DST_PORT = 3
  129. )
  130. // enum ctattr_protoinfo {
  131. // CTA_PROTOINFO_UNSPEC,
  132. // CTA_PROTOINFO_TCP,
  133. // CTA_PROTOINFO_DCCP,
  134. // CTA_PROTOINFO_SCTP,
  135. // __CTA_PROTOINFO_MAX
  136. // };
  137. // #define CTA_PROTOINFO_MAX (__CTA_PROTOINFO_MAX - 1)
  138. const (
  139. CTA_PROTOINFO_TCP = 1
  140. )
  141. // enum ctattr_protoinfo_tcp {
  142. // CTA_PROTOINFO_TCP_UNSPEC,
  143. // CTA_PROTOINFO_TCP_STATE,
  144. // CTA_PROTOINFO_TCP_WSCALE_ORIGINAL,
  145. // CTA_PROTOINFO_TCP_WSCALE_REPLY,
  146. // CTA_PROTOINFO_TCP_FLAGS_ORIGINAL,
  147. // CTA_PROTOINFO_TCP_FLAGS_REPLY,
  148. // __CTA_PROTOINFO_TCP_MAX
  149. // };
  150. // #define CTA_PROTOINFO_TCP_MAX (__CTA_PROTOINFO_TCP_MAX - 1)
  151. const (
  152. CTA_PROTOINFO_TCP_STATE = 1
  153. CTA_PROTOINFO_TCP_WSCALE_ORIGINAL = 2
  154. CTA_PROTOINFO_TCP_WSCALE_REPLY = 3
  155. CTA_PROTOINFO_TCP_FLAGS_ORIGINAL = 4
  156. CTA_PROTOINFO_TCP_FLAGS_REPLY = 5
  157. )
  158. // enum ctattr_counters {
  159. // CTA_COUNTERS_UNSPEC,
  160. // CTA_COUNTERS_PACKETS, /* 64bit counters */
  161. // CTA_COUNTERS_BYTES, /* 64bit counters */
  162. // CTA_COUNTERS32_PACKETS, /* old 32bit counters, unused */
  163. // CTA_COUNTERS32_BYTES, /* old 32bit counters, unused */
  164. // CTA_COUNTERS_PAD,
  165. // __CTA_COUNTERS_M
  166. // };
  167. // #define CTA_COUNTERS_MAX (__CTA_COUNTERS_MAX - 1)
  168. const (
  169. CTA_COUNTERS_PACKETS = 1
  170. CTA_COUNTERS_BYTES = 2
  171. )
  172. // enum CTA TIMESTAMP TLVs
  173. // CTA_TIMESTAMP_START /* 64bit value */
  174. // CTA_TIMESTAMP_STOP /* 64bit value */
  175. const (
  176. CTA_TIMESTAMP_START = 1
  177. CTA_TIMESTAMP_STOP = 2
  178. )
  179. // /* General form of address family dependent message.
  180. // */
  181. // struct nfgenmsg {
  182. // __u8 nfgen_family; /* AF_xxx */
  183. // __u8 version; /* nfnetlink version */
  184. // __be16 res_id; /* resource id */
  185. // };
  186. type Nfgenmsg struct {
  187. NfgenFamily uint8
  188. Version uint8
  189. ResId uint16 // big endian
  190. }
  191. func (msg *Nfgenmsg) Len() int {
  192. return SizeofNfgenmsg
  193. }
  194. func DeserializeNfgenmsg(b []byte) *Nfgenmsg {
  195. return (*Nfgenmsg)(unsafe.Pointer(&b[0:SizeofNfgenmsg][0]))
  196. }
  197. func (msg *Nfgenmsg) Serialize() []byte {
  198. return (*(*[SizeofNfgenmsg]byte)(unsafe.Pointer(msg)))[:]
  199. }