hcnpolicy.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //go:build windows
  2. package hcn
  3. import (
  4. "encoding/json"
  5. )
  6. // EndpointPolicyType are the potential Policies that apply to Endpoints.
  7. type EndpointPolicyType string
  8. // EndpointPolicyType const
  9. const (
  10. PortMapping EndpointPolicyType = "PortMapping"
  11. ACL EndpointPolicyType = "ACL"
  12. QOS EndpointPolicyType = "QOS"
  13. L2Driver EndpointPolicyType = "L2Driver"
  14. OutBoundNAT EndpointPolicyType = "OutBoundNAT"
  15. SDNRoute EndpointPolicyType = "SDNRoute"
  16. L4Proxy EndpointPolicyType = "L4Proxy"
  17. L4WFPPROXY EndpointPolicyType = "L4WFPPROXY"
  18. PortName EndpointPolicyType = "PortName"
  19. EncapOverhead EndpointPolicyType = "EncapOverhead"
  20. IOV EndpointPolicyType = "Iov"
  21. // Endpoint and Network have InterfaceConstraint and ProviderAddress
  22. NetworkProviderAddress EndpointPolicyType = "ProviderAddress"
  23. NetworkInterfaceConstraint EndpointPolicyType = "InterfaceConstraint"
  24. TierAcl EndpointPolicyType = "TierAcl"
  25. )
  26. // EndpointPolicy is a collection of Policy settings for an Endpoint.
  27. type EndpointPolicy struct {
  28. Type EndpointPolicyType `json:""`
  29. Settings json.RawMessage `json:",omitempty"`
  30. }
  31. // NetworkPolicyType are the potential Policies that apply to Networks.
  32. type NetworkPolicyType string
  33. // NetworkPolicyType const
  34. const (
  35. SourceMacAddress NetworkPolicyType = "SourceMacAddress"
  36. NetAdapterName NetworkPolicyType = "NetAdapterName"
  37. VSwitchExtension NetworkPolicyType = "VSwitchExtension"
  38. DrMacAddress NetworkPolicyType = "DrMacAddress"
  39. AutomaticDNS NetworkPolicyType = "AutomaticDNS"
  40. InterfaceConstraint NetworkPolicyType = "InterfaceConstraint"
  41. ProviderAddress NetworkPolicyType = "ProviderAddress"
  42. RemoteSubnetRoute NetworkPolicyType = "RemoteSubnetRoute"
  43. VxlanPort NetworkPolicyType = "VxlanPort"
  44. HostRoute NetworkPolicyType = "HostRoute"
  45. SetPolicy NetworkPolicyType = "SetPolicy"
  46. NetworkL4Proxy NetworkPolicyType = "L4Proxy"
  47. LayerConstraint NetworkPolicyType = "LayerConstraint"
  48. NetworkACL NetworkPolicyType = "NetworkACL"
  49. )
  50. // NetworkPolicy is a collection of Policy settings for a Network.
  51. type NetworkPolicy struct {
  52. Type NetworkPolicyType `json:""`
  53. Settings json.RawMessage `json:",omitempty"`
  54. }
  55. // SubnetPolicyType are the potential Policies that apply to Subnets.
  56. type SubnetPolicyType string
  57. // SubnetPolicyType const
  58. const (
  59. VLAN SubnetPolicyType = "VLAN"
  60. VSID SubnetPolicyType = "VSID"
  61. )
  62. // SubnetPolicy is a collection of Policy settings for a Subnet.
  63. type SubnetPolicy struct {
  64. Type SubnetPolicyType `json:""`
  65. Settings json.RawMessage `json:",omitempty"`
  66. }
  67. // NatFlags are flags for portmappings.
  68. type NatFlags uint32
  69. const (
  70. NatFlagsNone NatFlags = iota
  71. NatFlagsLocalRoutedVip
  72. NatFlagsIPv6
  73. )
  74. /// Endpoint Policy objects
  75. // PortMappingPolicySetting defines Port Mapping (NAT)
  76. type PortMappingPolicySetting struct {
  77. Protocol uint32 `json:",omitempty"` // EX: TCP = 6, UDP = 17
  78. InternalPort uint16 `json:",omitempty"`
  79. ExternalPort uint16 `json:",omitempty"`
  80. VIP string `json:",omitempty"`
  81. Flags NatFlags `json:",omitempty"`
  82. }
  83. // ActionType associated with ACLs. Value is either Allow or Block.
  84. type ActionType string
  85. // DirectionType associated with ACLs. Value is either In or Out.
  86. type DirectionType string
  87. // RuleType associated with ACLs. Value is either Host (WFP) or Switch (VFP).
  88. type RuleType string
  89. const (
  90. // Allow traffic
  91. ActionTypeAllow ActionType = "Allow"
  92. // Block traffic
  93. ActionTypeBlock ActionType = "Block"
  94. // Pass traffic
  95. ActionTypePass ActionType = "Pass"
  96. // In is traffic coming to the Endpoint
  97. DirectionTypeIn DirectionType = "In"
  98. // Out is traffic leaving the Endpoint
  99. DirectionTypeOut DirectionType = "Out"
  100. // Host creates WFP (Windows Firewall) rules
  101. RuleTypeHost RuleType = "Host"
  102. // Switch creates VFP (Virtual Filter Platform) rules
  103. RuleTypeSwitch RuleType = "Switch"
  104. )
  105. // AclPolicySetting creates firewall rules on an endpoint
  106. type AclPolicySetting struct {
  107. Protocols string `json:",omitempty"` // EX: 6 (TCP), 17 (UDP), 1 (ICMPv4), 58 (ICMPv6), 2 (IGMP)
  108. Action ActionType `json:","`
  109. Direction DirectionType `json:","`
  110. LocalAddresses string `json:",omitempty"`
  111. RemoteAddresses string `json:",omitempty"`
  112. LocalPorts string `json:",omitempty"`
  113. RemotePorts string `json:",omitempty"`
  114. RuleType RuleType `json:",omitempty"`
  115. Priority uint16 `json:",omitempty"`
  116. }
  117. // QosPolicySetting sets Quality of Service bandwidth caps on an Endpoint.
  118. type QosPolicySetting struct {
  119. MaximumOutgoingBandwidthInBytes uint64
  120. }
  121. // OutboundNatPolicySetting sets outbound Network Address Translation on an Endpoint.
  122. type OutboundNatPolicySetting struct {
  123. VirtualIP string `json:",omitempty"`
  124. Exceptions []string `json:",omitempty"`
  125. Destinations []string `json:",omitempty"`
  126. Flags NatFlags `json:",omitempty"`
  127. }
  128. // SDNRoutePolicySetting sets SDN Route on an Endpoint.
  129. type SDNRoutePolicySetting struct {
  130. DestinationPrefix string `json:",omitempty"`
  131. NextHop string `json:",omitempty"`
  132. NeedEncap bool `json:",omitempty"`
  133. }
  134. // NetworkACLPolicySetting creates ACL rules on a network
  135. type NetworkACLPolicySetting struct {
  136. Protocols string `json:",omitempty"` // EX: 6 (TCP), 17 (UDP), 1 (ICMPv4), 58 (ICMPv6), 2 (IGMP)
  137. Action ActionType `json:","`
  138. Direction DirectionType `json:","`
  139. LocalAddresses string `json:",omitempty"`
  140. RemoteAddresses string `json:",omitempty"`
  141. LocalPorts string `json:",omitempty"`
  142. RemotePorts string `json:",omitempty"`
  143. RuleType RuleType `json:",omitempty"`
  144. Priority uint16 `json:",omitempty"`
  145. }
  146. // FiveTuple is nested in L4ProxyPolicySetting for WFP support.
  147. type FiveTuple struct {
  148. Protocols string `json:",omitempty"`
  149. LocalAddresses string `json:",omitempty"`
  150. RemoteAddresses string `json:",omitempty"`
  151. LocalPorts string `json:",omitempty"`
  152. RemotePorts string `json:",omitempty"`
  153. Priority uint16 `json:",omitempty"`
  154. }
  155. // ProxyExceptions exempts traffic to IpAddresses and Ports
  156. type ProxyExceptions struct {
  157. IpAddressExceptions []string `json:",omitempty"`
  158. PortExceptions []string `json:",omitempty"`
  159. }
  160. // L4WfpProxyPolicySetting sets Layer-4 Proxy on an endpoint.
  161. type L4WfpProxyPolicySetting struct {
  162. InboundProxyPort string `json:",omitempty"`
  163. OutboundProxyPort string `json:",omitempty"`
  164. FilterTuple FiveTuple `json:",omitempty"`
  165. UserSID string `json:",omitempty"`
  166. InboundExceptions ProxyExceptions `json:",omitempty"`
  167. OutboundExceptions ProxyExceptions `json:",omitempty"`
  168. }
  169. // PortnameEndpointPolicySetting sets the port name for an endpoint.
  170. type PortnameEndpointPolicySetting struct {
  171. Name string `json:",omitempty"`
  172. }
  173. // EncapOverheadEndpointPolicySetting sets the encap overhead for an endpoint.
  174. type EncapOverheadEndpointPolicySetting struct {
  175. Overhead uint16 `json:",omitempty"`
  176. }
  177. // IovPolicySetting sets the Iov settings for an endpoint.
  178. type IovPolicySetting struct {
  179. IovOffloadWeight uint32 `json:",omitempty"`
  180. QueuePairsRequested uint32 `json:",omitempty"`
  181. InterruptModeration uint32 `json:",omitempty"`
  182. }
  183. /// Endpoint and Network Policy objects
  184. // ProviderAddressEndpointPolicySetting sets the PA for an endpoint.
  185. type ProviderAddressEndpointPolicySetting struct {
  186. ProviderAddress string `json:",omitempty"`
  187. }
  188. // InterfaceConstraintPolicySetting limits an Endpoint or Network to a specific Nic.
  189. type InterfaceConstraintPolicySetting struct {
  190. InterfaceGuid string `json:",omitempty"`
  191. InterfaceLuid uint64 `json:",omitempty"`
  192. InterfaceIndex uint32 `json:",omitempty"`
  193. InterfaceMediaType uint32 `json:",omitempty"`
  194. InterfaceAlias string `json:",omitempty"`
  195. InterfaceDescription string `json:",omitempty"`
  196. }
  197. /// Network Policy objects
  198. // SourceMacAddressNetworkPolicySetting sets source MAC for a network.
  199. type SourceMacAddressNetworkPolicySetting struct {
  200. SourceMacAddress string `json:",omitempty"`
  201. }
  202. // NetAdapterNameNetworkPolicySetting sets network adapter of a network.
  203. type NetAdapterNameNetworkPolicySetting struct {
  204. NetworkAdapterName string `json:",omitempty"`
  205. }
  206. // VSwitchExtensionNetworkPolicySetting enables/disabled VSwitch extensions for a network.
  207. type VSwitchExtensionNetworkPolicySetting struct {
  208. ExtensionID string `json:",omitempty"`
  209. Enable bool `json:",omitempty"`
  210. }
  211. // DrMacAddressNetworkPolicySetting sets the DR MAC for a network.
  212. type DrMacAddressNetworkPolicySetting struct {
  213. Address string `json:",omitempty"`
  214. }
  215. // AutomaticDNSNetworkPolicySetting enables/disables automatic DNS on a network.
  216. type AutomaticDNSNetworkPolicySetting struct {
  217. Enable bool `json:",omitempty"`
  218. }
  219. type LayerConstraintNetworkPolicySetting struct {
  220. LayerId string `json:",omitempty"`
  221. }
  222. /// Subnet Policy objects
  223. // VlanPolicySetting isolates a subnet with VLAN tagging.
  224. type VlanPolicySetting struct {
  225. IsolationId uint32 `json:","`
  226. }
  227. // VsidPolicySetting isolates a subnet with VSID tagging.
  228. type VsidPolicySetting struct {
  229. IsolationId uint32 `json:","`
  230. }
  231. // RemoteSubnetRoutePolicySetting creates remote subnet route rules on a network
  232. type RemoteSubnetRoutePolicySetting struct {
  233. DestinationPrefix string
  234. IsolationId uint16
  235. ProviderAddress string
  236. DistributedRouterMacAddress string
  237. }
  238. // SetPolicyTypes associated with SetPolicy. Value is IPSET.
  239. type SetPolicyType string
  240. const (
  241. SetPolicyTypeIpSet SetPolicyType = "IPSET"
  242. SetPolicyTypeNestedIpSet SetPolicyType = "NESTEDIPSET"
  243. )
  244. // SetPolicySetting creates IPSets on network
  245. type SetPolicySetting struct {
  246. Id string
  247. Name string
  248. Type SetPolicyType `json:"PolicyType"`
  249. Values string
  250. }
  251. // VxlanPortPolicySetting allows configuring the VXLAN TCP port
  252. type VxlanPortPolicySetting struct {
  253. Port uint16
  254. }
  255. // ProtocolType associated with L4ProxyPolicy
  256. type ProtocolType uint32
  257. const (
  258. ProtocolTypeUnknown ProtocolType = 0
  259. ProtocolTypeICMPv4 ProtocolType = 1
  260. ProtocolTypeIGMP ProtocolType = 2
  261. ProtocolTypeTCP ProtocolType = 6
  262. ProtocolTypeUDP ProtocolType = 17
  263. ProtocolTypeICMPv6 ProtocolType = 58
  264. )
  265. // L4ProxyPolicySetting applies proxy policy on network/endpoint
  266. type L4ProxyPolicySetting struct {
  267. IP string `json:",omitempty"`
  268. Port string `json:",omitempty"`
  269. Protocol ProtocolType `json:",omitempty"`
  270. Exceptions []string `json:",omitempty"`
  271. Destination string
  272. OutboundNAT bool `json:",omitempty"`
  273. }
  274. // TierAclRule represents an ACL within TierAclPolicySetting
  275. type TierAclRule struct {
  276. Id string `json:",omitempty"`
  277. Protocols string `json:",omitempty"`
  278. TierAclRuleAction ActionType `json:","`
  279. LocalAddresses string `json:",omitempty"`
  280. RemoteAddresses string `json:",omitempty"`
  281. LocalPorts string `json:",omitempty"`
  282. RemotePorts string `json:",omitempty"`
  283. Priority uint16 `json:",omitempty"`
  284. }
  285. // TierAclPolicySetting represents a Tier containing ACLs
  286. type TierAclPolicySetting struct {
  287. Name string `json:","`
  288. Direction DirectionType `json:","`
  289. Order uint16 `json:""`
  290. TierAclRules []TierAclRule `json:",omitempty"`
  291. }