gateway.go 13 KB

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