conntrack_linux.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package netlink
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "syscall"
  9. "github.com/vishvananda/netlink/nl"
  10. )
  11. // ConntrackTableType Conntrack table for the netlink operation
  12. type ConntrackTableType uint8
  13. const (
  14. // ConntrackTable Conntrack table
  15. // https://github.com/torvalds/linux/blob/master/include/uapi/linux/netfilter/nfnetlink.h -> #define NFNL_SUBSYS_CTNETLINK 1
  16. ConntrackTable = 1
  17. // ConntrackExpectTable Conntrack expect table
  18. // https://github.com/torvalds/linux/blob/master/include/uapi/linux/netfilter/nfnetlink.h -> #define NFNL_SUBSYS_CTNETLINK_EXP 2
  19. ConntrackExpectTable = 2
  20. )
  21. const (
  22. // For Parsing Mark
  23. TCP_PROTO = 6
  24. UDP_PROTO = 17
  25. )
  26. const (
  27. // backward compatibility with golang 1.6 which does not have io.SeekCurrent
  28. seekCurrent = 1
  29. )
  30. // InetFamily Family type
  31. type InetFamily uint8
  32. // -L [table] [options] List conntrack or expectation table
  33. // -G [table] parameters Get conntrack or expectation
  34. // -I [table] parameters Create a conntrack or expectation
  35. // -U [table] parameters Update a conntrack
  36. // -E [table] [options] Show events
  37. // -C [table] Show counter
  38. // -S Show statistics
  39. // ConntrackTableList returns the flow list of a table of a specific family
  40. // conntrack -L [table] [options] List conntrack or expectation table
  41. func ConntrackTableList(table ConntrackTableType, family InetFamily) ([]*ConntrackFlow, error) {
  42. return pkgHandle.ConntrackTableList(table, family)
  43. }
  44. // ConntrackTableFlush flushes all the flows of a specified table
  45. // conntrack -F [table] Flush table
  46. // The flush operation applies to all the family types
  47. func ConntrackTableFlush(table ConntrackTableType) error {
  48. return pkgHandle.ConntrackTableFlush(table)
  49. }
  50. // ConntrackDeleteFilter deletes entries on the specified table on the base of the filter
  51. // conntrack -D [table] parameters Delete conntrack or expectation
  52. func ConntrackDeleteFilter(table ConntrackTableType, family InetFamily, filter CustomConntrackFilter) (uint, error) {
  53. return pkgHandle.ConntrackDeleteFilter(table, family, filter)
  54. }
  55. // ConntrackTableList returns the flow list of a table of a specific family using the netlink handle passed
  56. // conntrack -L [table] [options] List conntrack or expectation table
  57. func (h *Handle) ConntrackTableList(table ConntrackTableType, family InetFamily) ([]*ConntrackFlow, error) {
  58. res, err := h.dumpConntrackTable(table, family)
  59. if err != nil {
  60. return nil, err
  61. }
  62. // Deserialize all the flows
  63. var result []*ConntrackFlow
  64. for _, dataRaw := range res {
  65. result = append(result, parseRawData(dataRaw))
  66. }
  67. return result, nil
  68. }
  69. // ConntrackTableFlush flushes all the flows of a specified table using the netlink handle passed
  70. // conntrack -F [table] Flush table
  71. // The flush operation applies to all the family types
  72. func (h *Handle) ConntrackTableFlush(table ConntrackTableType) error {
  73. req := h.newConntrackRequest(table, syscall.AF_INET, nl.IPCTNL_MSG_CT_DELETE, syscall.NLM_F_ACK)
  74. _, err := req.Execute(syscall.NETLINK_NETFILTER, 0)
  75. return err
  76. }
  77. // ConntrackDeleteFilter deletes entries on the specified table on the base of the filter using the netlink handle passed
  78. // conntrack -D [table] parameters Delete conntrack or expectation
  79. func (h *Handle) ConntrackDeleteFilter(table ConntrackTableType, family InetFamily, filter CustomConntrackFilter) (uint, error) {
  80. res, err := h.dumpConntrackTable(table, family)
  81. if err != nil {
  82. return 0, err
  83. }
  84. var matched uint
  85. for _, dataRaw := range res {
  86. flow := parseRawData(dataRaw)
  87. if match := filter.MatchConntrackFlow(flow); match {
  88. req2 := h.newConntrackRequest(table, family, nl.IPCTNL_MSG_CT_DELETE, syscall.NLM_F_ACK)
  89. // skip the first 4 byte that are the netfilter header, the newConntrackRequest is adding it already
  90. req2.AddRawData(dataRaw[4:])
  91. req2.Execute(syscall.NETLINK_NETFILTER, 0)
  92. matched++
  93. }
  94. }
  95. return matched, nil
  96. }
  97. func (h *Handle) newConntrackRequest(table ConntrackTableType, family InetFamily, operation, flags int) *nl.NetlinkRequest {
  98. // Create the Netlink request object
  99. req := h.newNetlinkRequest((int(table)<<8)|operation, flags)
  100. // Add the netfilter header
  101. msg := &nl.Nfgenmsg{
  102. NfgenFamily: uint8(family),
  103. Version: nl.NFNETLINK_V0,
  104. ResId: 0,
  105. }
  106. req.AddData(msg)
  107. return req
  108. }
  109. func (h *Handle) dumpConntrackTable(table ConntrackTableType, family InetFamily) ([][]byte, error) {
  110. req := h.newConntrackRequest(table, family, nl.IPCTNL_MSG_CT_GET, syscall.NLM_F_DUMP)
  111. return req.Execute(syscall.NETLINK_NETFILTER, 0)
  112. }
  113. // The full conntrack flow structure is very complicated and can be found in the file:
  114. // http://git.netfilter.org/libnetfilter_conntrack/tree/include/internal/object.h
  115. // For the time being, the structure below allows to parse and extract the base information of a flow
  116. type ipTuple struct {
  117. SrcIP net.IP
  118. DstIP net.IP
  119. Protocol uint8
  120. SrcPort uint16
  121. DstPort uint16
  122. }
  123. type ConntrackFlow struct {
  124. FamilyType uint8
  125. Forward ipTuple
  126. Reverse ipTuple
  127. Mark uint32
  128. }
  129. func (s *ConntrackFlow) String() string {
  130. // conntrack cmd output:
  131. // udp 17 src=127.0.0.1 dst=127.0.0.1 sport=4001 dport=1234 [UNREPLIED] src=127.0.0.1 dst=127.0.0.1 sport=1234 dport=4001 mark=0
  132. return fmt.Sprintf("%s\t%d src=%s dst=%s sport=%d dport=%d\tsrc=%s dst=%s sport=%d dport=%d mark=%d",
  133. nl.L4ProtoMap[s.Forward.Protocol], s.Forward.Protocol,
  134. s.Forward.SrcIP.String(), s.Forward.DstIP.String(), s.Forward.SrcPort, s.Forward.DstPort,
  135. s.Reverse.SrcIP.String(), s.Reverse.DstIP.String(), s.Reverse.SrcPort, s.Reverse.DstPort, s.Mark)
  136. }
  137. // This method parse the ip tuple structure
  138. // The message structure is the following:
  139. // <len, [CTA_IP_V4_SRC|CTA_IP_V6_SRC], 16 bytes for the IP>
  140. // <len, [CTA_IP_V4_DST|CTA_IP_V6_DST], 16 bytes for the IP>
  141. // <len, NLA_F_NESTED|nl.CTA_TUPLE_PROTO, 1 byte for the protocol, 3 bytes of padding>
  142. // <len, CTA_PROTO_SRC_PORT, 2 bytes for the source port, 2 bytes of padding>
  143. // <len, CTA_PROTO_DST_PORT, 2 bytes for the source port, 2 bytes of padding>
  144. func parseIpTuple(reader *bytes.Reader, tpl *ipTuple) uint8 {
  145. for i := 0; i < 2; i++ {
  146. _, t, _, v := parseNfAttrTLV(reader)
  147. switch t {
  148. case nl.CTA_IP_V4_SRC, nl.CTA_IP_V6_SRC:
  149. tpl.SrcIP = v
  150. case nl.CTA_IP_V4_DST, nl.CTA_IP_V6_DST:
  151. tpl.DstIP = v
  152. }
  153. }
  154. // Skip the next 4 bytes nl.NLA_F_NESTED|nl.CTA_TUPLE_PROTO
  155. reader.Seek(4, seekCurrent)
  156. _, t, _, v := parseNfAttrTLV(reader)
  157. if t == nl.CTA_PROTO_NUM {
  158. tpl.Protocol = uint8(v[0])
  159. }
  160. // Skip some padding 3 bytes
  161. reader.Seek(3, seekCurrent)
  162. for i := 0; i < 2; i++ {
  163. _, t, _ := parseNfAttrTL(reader)
  164. switch t {
  165. case nl.CTA_PROTO_SRC_PORT:
  166. parseBERaw16(reader, &tpl.SrcPort)
  167. case nl.CTA_PROTO_DST_PORT:
  168. parseBERaw16(reader, &tpl.DstPort)
  169. }
  170. // Skip some padding 2 byte
  171. reader.Seek(2, seekCurrent)
  172. }
  173. return tpl.Protocol
  174. }
  175. func parseNfAttrTLV(r *bytes.Reader) (isNested bool, attrType, len uint16, value []byte) {
  176. isNested, attrType, len = parseNfAttrTL(r)
  177. value = make([]byte, len)
  178. binary.Read(r, binary.BigEndian, &value)
  179. return isNested, attrType, len, value
  180. }
  181. func parseNfAttrTL(r *bytes.Reader) (isNested bool, attrType, len uint16) {
  182. binary.Read(r, nl.NativeEndian(), &len)
  183. len -= nl.SizeofNfattr
  184. binary.Read(r, nl.NativeEndian(), &attrType)
  185. isNested = (attrType & nl.NLA_F_NESTED) == nl.NLA_F_NESTED
  186. attrType = attrType & (nl.NLA_F_NESTED - 1)
  187. return isNested, attrType, len
  188. }
  189. func parseBERaw16(r *bytes.Reader, v *uint16) {
  190. binary.Read(r, binary.BigEndian, v)
  191. }
  192. func parseRawData(data []byte) *ConntrackFlow {
  193. s := &ConntrackFlow{}
  194. var proto uint8
  195. // First there is the Nfgenmsg header
  196. // consume only the family field
  197. reader := bytes.NewReader(data)
  198. binary.Read(reader, nl.NativeEndian(), &s.FamilyType)
  199. // skip rest of the Netfilter header
  200. reader.Seek(3, seekCurrent)
  201. // The message structure is the following:
  202. // <len, NLA_F_NESTED|CTA_TUPLE_ORIG> 4 bytes
  203. // <len, NLA_F_NESTED|CTA_TUPLE_IP> 4 bytes
  204. // flow information of the forward flow
  205. // <len, NLA_F_NESTED|CTA_TUPLE_REPLY> 4 bytes
  206. // <len, NLA_F_NESTED|CTA_TUPLE_IP> 4 bytes
  207. // flow information of the reverse flow
  208. for reader.Len() > 0 {
  209. nested, t, l := parseNfAttrTL(reader)
  210. if nested && t == nl.CTA_TUPLE_ORIG {
  211. if nested, t, _ = parseNfAttrTL(reader); nested && t == nl.CTA_TUPLE_IP {
  212. proto = parseIpTuple(reader, &s.Forward)
  213. }
  214. } else if nested && t == nl.CTA_TUPLE_REPLY {
  215. if nested, t, _ = parseNfAttrTL(reader); nested && t == nl.CTA_TUPLE_IP {
  216. parseIpTuple(reader, &s.Reverse)
  217. // Got all the useful information stop parsing
  218. break
  219. } else {
  220. // Header not recognized skip it
  221. reader.Seek(int64(l), seekCurrent)
  222. }
  223. }
  224. }
  225. if proto == TCP_PROTO {
  226. reader.Seek(64, seekCurrent)
  227. _, t, _, v := parseNfAttrTLV(reader)
  228. if t == nl.CTA_MARK {
  229. s.Mark = uint32(v[3])
  230. }
  231. } else if proto == UDP_PROTO {
  232. reader.Seek(16, seekCurrent)
  233. _, t, _, v := parseNfAttrTLV(reader)
  234. if t == nl.CTA_MARK {
  235. s.Mark = uint32(v[3])
  236. }
  237. }
  238. return s
  239. }
  240. // Conntrack parameters and options:
  241. // -n, --src-nat ip source NAT ip
  242. // -g, --dst-nat ip destination NAT ip
  243. // -j, --any-nat ip source or destination NAT ip
  244. // -m, --mark mark Set mark
  245. // -c, --secmark secmark Set selinux secmark
  246. // -e, --event-mask eventmask Event mask, eg. NEW,DESTROY
  247. // -z, --zero Zero counters while listing
  248. // -o, --output type[,...] Output format, eg. xml
  249. // -l, --label label[,...] conntrack labels
  250. // Common parameters and options:
  251. // -s, --src, --orig-src ip Source address from original direction
  252. // -d, --dst, --orig-dst ip Destination address from original direction
  253. // -r, --reply-src ip Source addres from reply direction
  254. // -q, --reply-dst ip Destination address from reply direction
  255. // -p, --protonum proto Layer 4 Protocol, eg. 'tcp'
  256. // -f, --family proto Layer 3 Protocol, eg. 'ipv6'
  257. // -t, --timeout timeout Set timeout
  258. // -u, --status status Set status, eg. ASSURED
  259. // -w, --zone value Set conntrack zone
  260. // --orig-zone value Set zone for original direction
  261. // --reply-zone value Set zone for reply direction
  262. // -b, --buffer-size Netlink socket buffer size
  263. // --mask-src ip Source mask address
  264. // --mask-dst ip Destination mask address
  265. // Filter types
  266. type ConntrackFilterType uint8
  267. const (
  268. ConntrackOrigSrcIP = iota // -orig-src ip Source address from original direction
  269. ConntrackOrigDstIP // -orig-dst ip Destination address from original direction
  270. ConntrackNatSrcIP // -src-nat ip Source NAT ip
  271. ConntrackNatDstIP // -dst-nat ip Destination NAT ip
  272. ConntrackNatAnyIP // -any-nat ip Source or destination NAT ip
  273. )
  274. type CustomConntrackFilter interface {
  275. // MatchConntrackFlow applies the filter to the flow and returns true if the flow matches
  276. // the filter or false otherwise
  277. MatchConntrackFlow(flow *ConntrackFlow) bool
  278. }
  279. type ConntrackFilter struct {
  280. ipFilter map[ConntrackFilterType]net.IP
  281. }
  282. // AddIP adds an IP to the conntrack filter
  283. func (f *ConntrackFilter) AddIP(tp ConntrackFilterType, ip net.IP) error {
  284. if f.ipFilter == nil {
  285. f.ipFilter = make(map[ConntrackFilterType]net.IP)
  286. }
  287. if _, ok := f.ipFilter[tp]; ok {
  288. return errors.New("Filter attribute already present")
  289. }
  290. f.ipFilter[tp] = ip
  291. return nil
  292. }
  293. // MatchConntrackFlow applies the filter to the flow and returns true if the flow matches the filter
  294. // false otherwise
  295. func (f *ConntrackFilter) MatchConntrackFlow(flow *ConntrackFlow) bool {
  296. if len(f.ipFilter) == 0 {
  297. // empty filter always not match
  298. return false
  299. }
  300. match := true
  301. // -orig-src ip Source address from original direction
  302. if elem, found := f.ipFilter[ConntrackOrigSrcIP]; found {
  303. match = match && elem.Equal(flow.Forward.SrcIP)
  304. }
  305. // -orig-dst ip Destination address from original direction
  306. if elem, found := f.ipFilter[ConntrackOrigDstIP]; match && found {
  307. match = match && elem.Equal(flow.Forward.DstIP)
  308. }
  309. // -src-nat ip Source NAT ip
  310. if elem, found := f.ipFilter[ConntrackNatSrcIP]; match && found {
  311. match = match && elem.Equal(flow.Reverse.SrcIP)
  312. }
  313. // -dst-nat ip Destination NAT ip
  314. if elem, found := f.ipFilter[ConntrackNatDstIP]; match && found {
  315. match = match && elem.Equal(flow.Reverse.DstIP)
  316. }
  317. // -any-nat ip Source or destination NAT ip
  318. if elem, found := f.ipFilter[ConntrackNatAnyIP]; match && found {
  319. match = match && (elem.Equal(flow.Reverse.SrcIP) || elem.Equal(flow.Reverse.DstIP))
  320. }
  321. return match
  322. }
  323. var _ CustomConntrackFilter = (*ConntrackFilter)(nil)