gateway.go 13 KB

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