gateway.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package logic
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/gravitl/netmaker/database"
  9. "github.com/gravitl/netmaker/logger"
  10. "github.com/gravitl/netmaker/models"
  11. )
  12. // CreateEgressGateway - creates an egress gateway
  13. func CreateEgressGateway(gateway models.EgressGatewayRequest) (models.Node, error) {
  14. node, err := GetNodeByID(gateway.NodeID)
  15. if err != nil {
  16. return models.Node{}, err
  17. }
  18. if node.OS != "linux" && node.OS != "freebsd" { // add in darwin later
  19. return models.Node{}, errors.New(node.OS + " is unsupported for egress gateways")
  20. }
  21. if gateway.NatEnabled == "" {
  22. gateway.NatEnabled = "yes"
  23. }
  24. err = ValidateEgressGateway(gateway)
  25. if err != nil {
  26. return models.Node{}, err
  27. }
  28. node.IsEgressGateway = "yes"
  29. node.EgressGatewayRanges = gateway.Ranges
  30. node.EgressGatewayNatEnabled = gateway.NatEnabled
  31. node.EgressGatewayRequest = gateway // store entire request for use when preserving the egress gateway
  32. postUpCmd := ""
  33. postDownCmd := ""
  34. logger.Log(3, "creating egress gateway firewall in use is '", node.FirewallInUse, "'")
  35. if node.OS == "linux" {
  36. switch node.FirewallInUse {
  37. case models.FIREWALL_NFTABLES:
  38. // nftables only supported on Linux
  39. // assumes chains eg FORWARD and POSTROUTING already exist
  40. logger.Log(3, "creating egress gateway nftables is present")
  41. // down commands don't remove as removal of the rules leaves an empty chain while
  42. // removing the chain with rules in it would remove all rules in that section (not safe
  43. // if there are remaining rules on the host that need to stay). In practice the chain is removed
  44. // when non-empty even though the removal of a non-empty chain should not be possible per nftables wiki.
  45. postUpCmd, postDownCmd = firewallNFTCommandsCreateEgress(node.Interface, gateway.Interface, node.EgressGatewayNatEnabled)
  46. default: // iptables assumed
  47. logger.Log(3, "creating egress gateway nftables is not present")
  48. postUpCmd, postDownCmd = firewallIPTablesCommandsCreateEgress(node.Interface, gateway.Interface, node.EgressGatewayNatEnabled)
  49. }
  50. }
  51. if node.OS == "freebsd" {
  52. postUpCmd = "kldload ipfw ipfw_nat ; "
  53. postUpCmd += "ipfw disable one_pass ; "
  54. postUpCmd += "ipfw nat 1 config if " + gateway.Interface + " same_ports unreg_only reset ; "
  55. postUpCmd += "ipfw add 64000 reass all from any to any in ; "
  56. postUpCmd += "ipfw add 64000 nat 1 ip from any to any in via " + gateway.Interface + " ; "
  57. postUpCmd += "ipfw add 64000 check-state ; "
  58. postUpCmd += "ipfw add 64000 nat 1 ip from any to any out via " + gateway.Interface + " ; "
  59. postUpCmd += "ipfw add 65534 allow ip from any to any ; "
  60. postDownCmd = "ipfw delete 64000 ; "
  61. postDownCmd += "ipfw delete 65534 ; "
  62. postDownCmd += "kldunload ipfw_nat ipfw"
  63. }
  64. if gateway.PostUp != "" {
  65. postUpCmd = gateway.PostUp
  66. }
  67. if gateway.PostDown != "" {
  68. postDownCmd = gateway.PostDown
  69. }
  70. if node.PostUp != "" {
  71. if !strings.Contains(node.PostUp, postUpCmd) {
  72. postUpCmd = node.PostUp + "; " + postUpCmd
  73. }
  74. }
  75. if node.PostDown != "" {
  76. if !strings.Contains(node.PostDown, postDownCmd) {
  77. postDownCmd = node.PostDown + "; " + postDownCmd
  78. }
  79. }
  80. node.PostUp = postUpCmd
  81. node.PostDown = postDownCmd
  82. node.SetLastModified()
  83. nodeData, err := json.Marshal(&node)
  84. if err != nil {
  85. return node, err
  86. }
  87. if err = database.Insert(node.ID, string(nodeData), database.NODES_TABLE_NAME); err != nil {
  88. return models.Node{}, err
  89. }
  90. if err = NetworkNodesUpdatePullChanges(node.Network); err != nil {
  91. return models.Node{}, err
  92. }
  93. return node, nil
  94. }
  95. // ValidateEgressGateway - validates the egress gateway model
  96. func ValidateEgressGateway(gateway models.EgressGatewayRequest) error {
  97. var err error
  98. empty := len(gateway.Ranges) == 0
  99. if empty {
  100. err = errors.New("IP Ranges Cannot Be Empty")
  101. }
  102. empty = gateway.Interface == ""
  103. if empty {
  104. err = errors.New("interface cannot be empty")
  105. }
  106. return err
  107. }
  108. // DeleteEgressGateway - deletes egress from node
  109. func DeleteEgressGateway(network, nodeid string) (models.Node, error) {
  110. node, err := GetNodeByID(nodeid)
  111. if err != nil {
  112. return models.Node{}, err
  113. }
  114. node.IsEgressGateway = "no"
  115. node.EgressGatewayRanges = []string{}
  116. node.EgressGatewayRequest = models.EgressGatewayRequest{} // remove preserved request as the egress gateway is gone
  117. // needed in case we don't preserve a gateway (i.e., no ingress to preserve)
  118. node.PostUp = ""
  119. node.PostDown = ""
  120. logger.Log(3, "deleting egress gateway firewall in use is '", node.FirewallInUse, "'")
  121. if node.IsIngressGateway == "yes" { // check if node is still an ingress gateway before completely deleting postdown/up rules
  122. // still have an ingress gateway so preserve it
  123. if node.OS == "linux" {
  124. switch node.FirewallInUse {
  125. case models.FIREWALL_NFTABLES:
  126. // nftables only supported on Linux
  127. // assumes chains eg FORWARD and POSTROUTING already exist
  128. logger.Log(3, "deleting egress gateway nftables is present")
  129. node.PostUp, node.PostDown = firewallNFTCommandsCreateIngress(node.Interface)
  130. default:
  131. logger.Log(3, "deleting egress gateway nftables is not present")
  132. node.PostUp, node.PostDown = firewallIPTablesCommandsCreateIngress(node.Interface)
  133. }
  134. }
  135. // no need to preserve ingress gateway on FreeBSD as ingress is not supported on that OS
  136. }
  137. node.SetLastModified()
  138. data, err := json.Marshal(&node)
  139. if err != nil {
  140. return models.Node{}, err
  141. }
  142. if err = database.Insert(node.ID, string(data), database.NODES_TABLE_NAME); err != nil {
  143. return models.Node{}, err
  144. }
  145. if err = NetworkNodesUpdatePullChanges(network); err != nil {
  146. return models.Node{}, err
  147. }
  148. return node, nil
  149. }
  150. // CreateIngressGateway - creates an ingress gateway
  151. func CreateIngressGateway(netid string, nodeid string) (models.Node, error) {
  152. var postUpCmd, postDownCmd string
  153. node, err := GetNodeByID(nodeid)
  154. if node.OS != "linux" { // add in darwin later
  155. return models.Node{}, errors.New(node.OS + " is unsupported for ingress gateways")
  156. }
  157. if err != nil {
  158. return models.Node{}, err
  159. }
  160. network, err := GetParentNetwork(netid)
  161. if err != nil {
  162. return models.Node{}, err
  163. }
  164. node.IsIngressGateway = "yes"
  165. node.IngressGatewayRange = network.AddressRange
  166. logger.Log(3, "creating ingress gateway firewall in use is '", node.FirewallInUse, "'")
  167. switch node.FirewallInUse {
  168. case models.FIREWALL_NFTABLES:
  169. // nftables only supported on Linux
  170. // assumes chains eg FORWARD and POSTROUTING already exist
  171. logger.Log(3, "creating ingress gateway nftables is present")
  172. postUpCmd, postDownCmd = firewallNFTCommandsCreateIngress(node.Interface)
  173. default:
  174. logger.Log(3, "creating ingress gateway using nftables is not present")
  175. postUpCmd, postDownCmd = firewallIPTablesCommandsCreateIngress(node.Interface)
  176. }
  177. if node.PostUp != "" {
  178. if !strings.Contains(node.PostUp, postUpCmd) {
  179. postUpCmd = node.PostUp + "; " + postUpCmd
  180. }
  181. }
  182. if node.PostDown != "" {
  183. if !strings.Contains(node.PostDown, postDownCmd) {
  184. postDownCmd = node.PostDown + "; " + postDownCmd
  185. }
  186. }
  187. node.SetLastModified()
  188. node.PostUp = postUpCmd
  189. node.PostDown = postDownCmd
  190. node.UDPHolePunch = "no"
  191. data, err := json.Marshal(&node)
  192. if err != nil {
  193. return models.Node{}, err
  194. }
  195. err = database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  196. if err != nil {
  197. return models.Node{}, err
  198. }
  199. err = SetNetworkNodesLastModified(netid)
  200. return node, err
  201. }
  202. // DeleteIngressGateway - deletes an ingress gateway
  203. func DeleteIngressGateway(networkName string, nodeid string) (models.Node, error) {
  204. node, err := GetNodeByID(nodeid)
  205. if err != nil {
  206. return models.Node{}, err
  207. }
  208. network, err := GetParentNetwork(networkName)
  209. if err != nil {
  210. return models.Node{}, err
  211. }
  212. // delete ext clients belonging to ingress gateway
  213. if err = DeleteGatewayExtClients(node.ID, networkName); err != nil {
  214. return models.Node{}, err
  215. }
  216. logger.Log(3, "deleting ingress gateway")
  217. node.UDPHolePunch = network.DefaultUDPHolePunch
  218. node.LastModified = time.Now().Unix()
  219. node.IsIngressGateway = "no"
  220. node.IngressGatewayRange = ""
  221. // default to removing postup and postdown
  222. node.PostUp = ""
  223. node.PostDown = ""
  224. logger.Log(3, "deleting ingress gateway firewall in use is '", node.FirewallInUse, "' and isEgressGateway is", node.IsEgressGateway)
  225. if node.EgressGatewayRequest.NodeID != "" {
  226. _, err := CreateEgressGateway(node.EgressGatewayRequest)
  227. if err != nil {
  228. logger.Log(0, fmt.Sprintf("failed to create egress gateway on node [%s] on network [%s]: %v",
  229. node.EgressGatewayRequest.NodeID, node.EgressGatewayRequest.NetID, err))
  230. }
  231. }
  232. data, err := json.Marshal(&node)
  233. if err != nil {
  234. return models.Node{}, err
  235. }
  236. err = database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  237. if err != nil {
  238. return models.Node{}, err
  239. }
  240. err = SetNetworkNodesLastModified(networkName)
  241. return node, err
  242. }
  243. // DeleteGatewayExtClients - deletes ext clients based on gateway (mac) of ingress node and network
  244. func DeleteGatewayExtClients(gatewayID string, networkName string) error {
  245. currentExtClients, err := GetNetworkExtClients(networkName)
  246. if err != nil && !database.IsEmptyRecord(err) {
  247. return err
  248. }
  249. for _, extClient := range currentExtClients {
  250. if extClient.IngressGatewayID == gatewayID {
  251. if err = DeleteExtClient(networkName, extClient.ClientID); err != nil {
  252. logger.Log(1, "failed to remove ext client", extClient.ClientID)
  253. continue
  254. }
  255. }
  256. }
  257. return nil
  258. }
  259. // firewallNFTCommandsCreateIngress - used to centralize firewall command maintenance for creating an ingress gateway using the nftables firewall.
  260. func firewallNFTCommandsCreateIngress(networkInterface string) (string, string) {
  261. postUp := "nft add table ip filter ; "
  262. postUp += "nft add chain ip filter FORWARD ; "
  263. postUp += "nft add rule ip filter FORWARD iifname " + networkInterface + " counter accept ; "
  264. postUp += "nft add rule ip filter FORWARD oifname " + networkInterface + " counter accept ; "
  265. postUp += "nft add table nat ; "
  266. postUp += "nft add chain nat POSTROUTING ; "
  267. postUp += "nft add rule ip nat POSTROUTING oifname " + networkInterface + " counter masquerade"
  268. // doesn't remove potentially empty tables or chains
  269. postDown := "nft flush table filter; "
  270. postDown += "nft flush table nat; "
  271. return postUp, postDown
  272. }
  273. // firewallNFTCommandsCreateEgress - used to centralize firewall command maintenance for creating an egress gateway using the nftables firewall.
  274. func firewallNFTCommandsCreateEgress(networkInterface string, gatewayInterface string, egressNatEnabled string) (string, string) {
  275. postUp := "nft add table ip filter ; "
  276. postUp += "nft add chain ip filter FORWARD ; "
  277. postUp += "nft add rule ip filter FORWARD iifname " + networkInterface + " counter accept ; "
  278. postUp += "nft add rule ip filter FORWARD oifname " + networkInterface + " counter accept ; "
  279. postDown := "nft flush table filter; "
  280. if egressNatEnabled == "yes" {
  281. postUp += "nft add table nat ; "
  282. postUp += "nft add chain nat POSTROUTING ; "
  283. postUp += "nft add rule ip nat POSTROUTING oifname " + gatewayInterface + " counter masquerade ;"
  284. postDown += "nft flush table nat; "
  285. }
  286. return postUp, postDown
  287. }
  288. // firewallIPTablesCommandsCreateIngress - used to centralize firewall command maintenance for creating an ingress gateway using the iptables firewall.
  289. func firewallIPTablesCommandsCreateIngress(networkInterface string) (string, string) {
  290. postUp := "iptables -A FORWARD -i " + networkInterface + " -j ACCEPT ; "
  291. postUp += "iptables -A FORWARD -o " + networkInterface + " -j ACCEPT ; "
  292. postUp += "iptables -t nat -A POSTROUTING -o " + networkInterface + " -j MASQUERADE"
  293. // doesn't remove potentially empty tables or chains
  294. postDown := "iptables -D FORWARD -i " + networkInterface + " -j ACCEPT ; "
  295. postDown += "iptables -D FORWARD -o " + networkInterface + " -j ACCEPT ; "
  296. postDown += "iptables -t nat -D POSTROUTING -o " + networkInterface + " -j MASQUERADE"
  297. return postUp, postDown
  298. }
  299. // firewallIPTablesCommandsCreateEgress - used to centralize firewall command maintenance for creating an egress gateway using the iptables firewall.
  300. func firewallIPTablesCommandsCreateEgress(networkInterface string, gatewayInterface string, egressNatEnabled string) (string, string) {
  301. postUp := "iptables -A FORWARD -i " + networkInterface + " -j ACCEPT; "
  302. postUp += "iptables -A FORWARD -o " + networkInterface + " -j ACCEPT"
  303. postDown := "iptables -D FORWARD -i " + networkInterface + " -j ACCEPT; "
  304. postDown += "iptables -D FORWARD -o " + networkInterface + " -j ACCEPT"
  305. if egressNatEnabled == "yes" {
  306. postUp += "; iptables -t nat -A POSTROUTING -o " + gatewayInterface + " -j MASQUERADE"
  307. postDown += "; iptables -t nat -D POSTROUTING -o " + gatewayInterface + " -j MASQUERADE"
  308. }
  309. return postUp, postDown
  310. }