xt.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Package xt implements dedicated types for (some) of the "Info" payload in Match
  3. and Target expressions that bridge between the nftables and xtables worlds.
  4. Bridging between the more unified world of nftables and the slightly
  5. heterogenous world of xtables comes with some caveats. Unmarshalling the
  6. extension/translation information in Match and Target expressions requires
  7. information about the table family the information belongs to, as well as type
  8. and type revision information. In consequence, unmarshalling the Match and
  9. Target Info field payloads often (but not necessarily always) require the table
  10. family and revision information, so it gets passed to the type-specific
  11. unmarshallers.
  12. To complicate things more, even marshalling requires knowledge about the
  13. enclosing table family. The NatRange/NatRange2 types are an example, where it is
  14. necessary to differentiate between IPv4 and IPv6 address marshalling. Due to
  15. Go's net.IP habit to normally store IPv4 addresses as IPv4-compatible IPv6
  16. addresses (see also RFC 4291, section 2.5.5.1) marshalling must be handled
  17. differently in the context of an IPv6 table compared to an IPv4 table. In an
  18. IPv4 table, an IPv4-compatible IPv6 address must be marshalled as a 32bit
  19. address, whereas in an IPv6 table the IPv4 address must be marshalled as an
  20. 128bit IPv4-compatible IPv6 address. Not relying on heuristics here we avoid
  21. behavior unexpected and most probably unknown to our API users. The net.IP habit
  22. of storing IPv4 addresses in two different storage formats is already a source
  23. for trouble, especially when comparing net.IPs from different Go module sources.
  24. We won't add to this confusion. (...or maybe we can, because of it?)
  25. An important property of all types of Info extension/translation payloads is
  26. that their marshalling and unmarshalling doesn't follow netlink's TLV
  27. (tag-length-value) architecture. Instead, Info payloads a basically plain binary
  28. blobs of their respective type-specific data structures, so host
  29. platform/architecture alignment and data type sizes apply. The alignedbuff
  30. package implements the different required data types alignments.
  31. Please note that Info payloads are always padded at their end to the next uint64
  32. alignment. Kernel code is checking for the padded payload size and will reject
  33. payloads not correctly padded at their ends.
  34. Most of the time, we find explifcitly sized (unsigned integer) data types.
  35. However, there are notable exceptions where "unsigned int" is used: on 64bit
  36. platforms this mostly translates into 32bit(!). This differs from Go mapping
  37. uint to uint64 instead. This package currently clamps its mapping of C's
  38. "unsigned int" to Go's uint32 for marshalling and unmarshalling. If in the
  39. future 128bit platforms with a differently sized C unsigned int should come into
  40. production, then the alignedbuff package will need to be adapted accordingly, as
  41. it abstracts away this data type handling.
  42. */
  43. package xt