gateway.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. return node, nil
  91. }
  92. // ValidateEgressGateway - validates the egress gateway model
  93. func ValidateEgressGateway(gateway models.EgressGatewayRequest) error {
  94. var err error
  95. empty := len(gateway.Ranges) == 0
  96. if empty {
  97. err = errors.New("IP Ranges Cannot Be Empty")
  98. }
  99. empty = gateway.Interface == ""
  100. if empty {
  101. err = errors.New("interface cannot be empty")
  102. }
  103. return err
  104. }
  105. // DeleteEgressGateway - deletes egress from node
  106. func DeleteEgressGateway(network, nodeid string) (models.Node, error) {
  107. node, err := GetNodeByID(nodeid)
  108. if err != nil {
  109. return models.Node{}, err
  110. }
  111. node.IsEgressGateway = "no"
  112. node.EgressGatewayRanges = []string{}
  113. node.EgressGatewayRequest = models.EgressGatewayRequest{} // remove preserved request as the egress gateway is gone
  114. // needed in case we don't preserve a gateway (i.e., no ingress to preserve)
  115. node.PostUp = ""
  116. node.PostDown = ""
  117. logger.Log(3, "deleting egress gateway firewall in use is '", node.FirewallInUse, "'")
  118. if node.IsIngressGateway == "yes" { // check if node is still an ingress gateway before completely deleting postdown/up rules
  119. // still have an ingress gateway so preserve it
  120. if node.OS == "linux" {
  121. switch node.FirewallInUse {
  122. case models.FIREWALL_NFTABLES:
  123. // nftables only supported on Linux
  124. // assumes chains eg FORWARD and POSTROUTING already exist
  125. logger.Log(3, "deleting egress gateway nftables is present")
  126. node.PostUp, node.PostDown = firewallNFTCommandsCreateIngress(node.Interface)
  127. default:
  128. logger.Log(3, "deleting egress gateway nftables is not present")
  129. node.PostUp, node.PostDown = firewallIPTablesCommandsCreateIngress(node.Interface)
  130. }
  131. }
  132. // no need to preserve ingress gateway on FreeBSD as ingress is not supported on that OS
  133. }
  134. node.SetLastModified()
  135. data, err := json.Marshal(&node)
  136. if err != nil {
  137. return models.Node{}, err
  138. }
  139. if err = database.Insert(node.ID, string(data), database.NODES_TABLE_NAME); err != nil {
  140. return models.Node{}, err
  141. }
  142. return node, nil
  143. }
  144. // CreateIngressGateway - creates an ingress gateway
  145. func CreateIngressGateway(netid string, nodeid string) (models.Node, error) {
  146. var postUpCmd, postDownCmd string
  147. node, err := GetNodeByID(nodeid)
  148. if node.OS != "linux" { // add in darwin later
  149. return models.Node{}, errors.New(node.OS + " is unsupported for ingress gateways")
  150. }
  151. if err != nil {
  152. return models.Node{}, err
  153. }
  154. network, err := GetParentNetwork(netid)
  155. if err != nil {
  156. return models.Node{}, err
  157. }
  158. node.IsIngressGateway = "yes"
  159. node.IngressGatewayRange = network.AddressRange
  160. logger.Log(3, "creating ingress gateway firewall in use is '", node.FirewallInUse, "'")
  161. switch node.FirewallInUse {
  162. case models.FIREWALL_NFTABLES:
  163. // nftables only supported on Linux
  164. // assumes chains eg FORWARD and POSTROUTING already exist
  165. logger.Log(3, "creating ingress gateway nftables is present")
  166. postUpCmd, postDownCmd = firewallNFTCommandsCreateIngress(node.Interface)
  167. default:
  168. logger.Log(3, "creating ingress gateway using nftables is not present")
  169. postUpCmd, postDownCmd = firewallIPTablesCommandsCreateIngress(node.Interface)
  170. }
  171. if node.PostUp != "" {
  172. if !strings.Contains(node.PostUp, postUpCmd) {
  173. postUpCmd = node.PostUp + "; " + postUpCmd
  174. }
  175. }
  176. if node.PostDown != "" {
  177. if !strings.Contains(node.PostDown, postDownCmd) {
  178. postDownCmd = node.PostDown + "; " + postDownCmd
  179. }
  180. }
  181. node.SetLastModified()
  182. node.PostUp = postUpCmd
  183. node.PostDown = postDownCmd
  184. node.UDPHolePunch = "no"
  185. data, err := json.Marshal(&node)
  186. if err != nil {
  187. return models.Node{}, err
  188. }
  189. err = database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  190. if err != nil {
  191. return models.Node{}, err
  192. }
  193. err = SetNetworkNodesLastModified(netid)
  194. return node, err
  195. }
  196. // DeleteIngressGateway - deletes an ingress gateway
  197. func DeleteIngressGateway(networkName string, nodeid string) (models.Node, error) {
  198. node, err := GetNodeByID(nodeid)
  199. if err != nil {
  200. return models.Node{}, err
  201. }
  202. network, err := GetParentNetwork(networkName)
  203. if err != nil {
  204. return models.Node{}, err
  205. }
  206. // delete ext clients belonging to ingress gateway
  207. if err = DeleteGatewayExtClients(node.ID, networkName); err != nil {
  208. return models.Node{}, err
  209. }
  210. logger.Log(3, "deleting ingress gateway")
  211. node.UDPHolePunch = network.DefaultUDPHolePunch
  212. node.LastModified = time.Now().Unix()
  213. node.IsIngressGateway = "no"
  214. node.IngressGatewayRange = ""
  215. // default to removing postup and postdown
  216. node.PostUp = ""
  217. node.PostDown = ""
  218. logger.Log(3, "deleting ingress gateway firewall in use is '", node.FirewallInUse, "' and isEgressGateway is", node.IsEgressGateway)
  219. if node.EgressGatewayRequest.NodeID != "" {
  220. _, err := CreateEgressGateway(node.EgressGatewayRequest)
  221. if err != nil {
  222. logger.Log(0, fmt.Sprintf("failed to create egress gateway on node [%s] on network [%s]: %v",
  223. node.EgressGatewayRequest.NodeID, node.EgressGatewayRequest.NetID, err))
  224. }
  225. }
  226. data, err := json.Marshal(&node)
  227. if err != nil {
  228. return models.Node{}, err
  229. }
  230. err = database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  231. if err != nil {
  232. return models.Node{}, err
  233. }
  234. err = SetNetworkNodesLastModified(networkName)
  235. return node, err
  236. }
  237. // DeleteGatewayExtClients - deletes ext clients based on gateway (mac) of ingress node and network
  238. func DeleteGatewayExtClients(gatewayID string, networkName string) error {
  239. currentExtClients, err := GetNetworkExtClients(networkName)
  240. if err != nil && !database.IsEmptyRecord(err) {
  241. return err
  242. }
  243. for _, extClient := range currentExtClients {
  244. if extClient.IngressGatewayID == gatewayID {
  245. if err = DeleteExtClient(networkName, extClient.ClientID); err != nil {
  246. logger.Log(1, "failed to remove ext client", extClient.ClientID)
  247. continue
  248. }
  249. }
  250. }
  251. return nil
  252. }
  253. // firewallNFTCommandsCreateIngress - used to centralize firewall command maintenance for creating an ingress gateway using the nftables firewall.
  254. func firewallNFTCommandsCreateIngress(networkInterface string) (string, string) {
  255. postUp := "nft add table ip filter ; "
  256. postUp += "nft add chain ip filter FORWARD ; "
  257. postUp += "nft add rule ip filter FORWARD iifname " + networkInterface + " counter accept ; "
  258. postUp += "nft add rule ip filter FORWARD oifname " + networkInterface + " counter accept ; "
  259. postUp += "nft add table nat ; "
  260. postUp += "nft add chain nat POSTROUTING ; "
  261. postUp += "nft add rule ip nat POSTROUTING oifname " + networkInterface + " counter masquerade"
  262. // doesn't remove potentially empty tables or chains
  263. postDown := "nft flush table filter; "
  264. postDown += "nft flush table nat; "
  265. return postUp, postDown
  266. }
  267. // firewallNFTCommandsCreateEgress - used to centralize firewall command maintenance for creating an egress gateway using the nftables firewall.
  268. func firewallNFTCommandsCreateEgress(networkInterface string, gatewayInterface string, egressNatEnabled string) (string, string) {
  269. postUp := "nft add table ip filter ; "
  270. postUp += "nft add chain ip filter FORWARD ; "
  271. postUp += "nft add rule ip filter FORWARD iifname " + networkInterface + " counter accept ; "
  272. postUp += "nft add rule ip filter FORWARD oifname " + networkInterface + " counter accept ; "
  273. postDown := "nft flush table filter; "
  274. if egressNatEnabled == "yes" {
  275. postUp += "nft add table nat ; "
  276. postUp += "nft add chain nat POSTROUTING ; "
  277. postUp += "nft add rule ip nat POSTROUTING oifname " + gatewayInterface + " counter masquerade ;"
  278. postDown += "nft flush table nat; "
  279. }
  280. return postUp, postDown
  281. }
  282. // firewallIPTablesCommandsCreateIngress - used to centralize firewall command maintenance for creating an ingress gateway using the iptables firewall.
  283. func firewallIPTablesCommandsCreateIngress(networkInterface string) (string, string) {
  284. postUp := "iptables -A FORWARD -i " + networkInterface + " -j ACCEPT ; "
  285. postUp += "iptables -A FORWARD -o " + networkInterface + " -j ACCEPT ; "
  286. postUp += "iptables -t nat -A POSTROUTING -o " + networkInterface + " -j MASQUERADE"
  287. // doesn't remove potentially empty tables or chains
  288. postDown := "iptables -D FORWARD -i " + networkInterface + " -j ACCEPT ; "
  289. postDown += "iptables -D FORWARD -o " + networkInterface + " -j ACCEPT ; "
  290. postDown += "iptables -t nat -D POSTROUTING -o " + networkInterface + " -j MASQUERADE"
  291. return postUp, postDown
  292. }
  293. // firewallIPTablesCommandsCreateEgress - used to centralize firewall command maintenance for creating an egress gateway using the iptables firewall.
  294. func firewallIPTablesCommandsCreateEgress(networkInterface string, gatewayInterface string, egressNatEnabled string) (string, string) {
  295. postUp := "iptables -A FORWARD -i " + networkInterface + " -j ACCEPT; "
  296. postUp += "iptables -A FORWARD -o " + networkInterface + " -j ACCEPT"
  297. postDown := "iptables -D FORWARD -i " + networkInterface + " -j ACCEPT; "
  298. postDown += "iptables -D FORWARD -o " + networkInterface + " -j ACCEPT"
  299. if egressNatEnabled == "yes" {
  300. postUp += "; iptables -t nat -A POSTROUTING -o " + gatewayInterface + " -j MASQUERADE"
  301. postDown += "; iptables -t nat -D POSTROUTING -o " + gatewayInterface + " -j MASQUERADE"
  302. }
  303. return postUp, postDown
  304. }