gateway.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. err = ValidateEgressGateway(gateway)
  21. if err != nil {
  22. return models.Node{}, err
  23. }
  24. node.IsEgressGateway = "yes"
  25. node.EgressGatewayRanges = gateway.Ranges
  26. postUpCmd := ""
  27. postDownCmd := ""
  28. if node.OS == "linux" {
  29. postUpCmd = "iptables -A FORWARD -i " + node.Interface + " -j ACCEPT ; "
  30. postUpCmd += "iptables -A FORWARD -o " + node.Interface + " -j ACCEPT ; "
  31. postUpCmd += "iptables -t nat -A POSTROUTING -o " + gateway.Interface + " -j MASQUERADE"
  32. postDownCmd = "iptables -D FORWARD -i " + node.Interface + " -j ACCEPT ; "
  33. postDownCmd += "iptables -D FORWARD -o " + node.Interface + " -j ACCEPT ; "
  34. postDownCmd += "iptables -t nat -D POSTROUTING -o " + gateway.Interface + " -j MASQUERADE"
  35. }
  36. if node.OS == "freebsd" {
  37. postUpCmd = "kldload ipfw ipfw_nat ; "
  38. postUpCmd += "ipfw disable one_pass ; "
  39. postUpCmd += "ipfw nat 1 config if " + gateway.Interface + " same_ports unreg_only reset ; "
  40. postUpCmd += "ipfw add 64000 reass all from any to any in ; "
  41. postUpCmd += "ipfw add 64000 nat 1 ip from any to any in via " + gateway.Interface + " ; "
  42. postUpCmd += "ipfw add 64000 check-state ; "
  43. postUpCmd += "ipfw add 64000 nat 1 ip from any to any out via " + gateway.Interface + " ; "
  44. postUpCmd += "ipfw add 65534 allow ip from any to any ; "
  45. postDownCmd = "ipfw delete 64000 ; "
  46. postDownCmd += "ipfw delete 65534 ; "
  47. postDownCmd += "kldunload ipfw_nat ipfw"
  48. }
  49. if gateway.PostUp != "" {
  50. postUpCmd = gateway.PostUp
  51. }
  52. if gateway.PostDown != "" {
  53. postDownCmd = gateway.PostDown
  54. }
  55. if node.PostUp != "" {
  56. if !strings.Contains(node.PostUp, postUpCmd) {
  57. postUpCmd = node.PostUp + "; " + postUpCmd
  58. }
  59. }
  60. if node.PostDown != "" {
  61. if !strings.Contains(node.PostDown, postDownCmd) {
  62. postDownCmd = node.PostDown + "; " + postDownCmd
  63. }
  64. }
  65. node.PostUp = postUpCmd
  66. node.PostDown = postDownCmd
  67. node.SetLastModified()
  68. nodeData, err := json.Marshal(&node)
  69. if err != nil {
  70. return node, err
  71. }
  72. if err = database.Insert(node.ID, string(nodeData), database.NODES_TABLE_NAME); err != nil {
  73. return models.Node{}, err
  74. }
  75. if err = NetworkNodesUpdatePullChanges(node.Network); err != nil {
  76. return models.Node{}, err
  77. }
  78. return node, nil
  79. }
  80. // ValidateEgressGateway - validates the egress gateway model
  81. func ValidateEgressGateway(gateway models.EgressGatewayRequest) error {
  82. var err error
  83. empty := len(gateway.Ranges) == 0
  84. if empty {
  85. err = errors.New("IP Ranges Cannot Be Empty")
  86. }
  87. empty = gateway.Interface == ""
  88. if empty {
  89. err = errors.New("interface cannot be empty")
  90. }
  91. return err
  92. }
  93. // DeleteEgressGateway - deletes egress from node
  94. func DeleteEgressGateway(network, nodeid string) (models.Node, error) {
  95. node, err := GetNodeByID(nodeid)
  96. if err != nil {
  97. return models.Node{}, err
  98. }
  99. node.IsEgressGateway = "no"
  100. node.EgressGatewayRanges = []string{}
  101. node.PostUp = ""
  102. node.PostDown = ""
  103. if node.IsIngressGateway == "yes" { // check if node is still an ingress gateway before completely deleting postdown/up rules
  104. if node.OS == "linux" {
  105. node.PostUp = "iptables -A FORWARD -i " + node.Interface + " -j ACCEPT ; "
  106. node.PostUp += "iptables -A FORWARD -o " + node.Interface + " -j ACCEPT ; "
  107. node.PostUp += "iptables -t nat -A POSTROUTING -o " + node.Interface + " -j MASQUERADE"
  108. node.PostDown = "iptables -D FORWARD -i " + node.Interface + " -j ACCEPT ; "
  109. node.PostDown += "iptables -D FORWARD -o " + node.Interface + " -j ACCEPT ; "
  110. node.PostDown += "iptables -t nat -D POSTROUTING -o " + node.Interface + " -j MASQUERADE"
  111. }
  112. if node.OS == "freebsd" {
  113. node.PostUp = ""
  114. node.PostDown = "ipfw delete 64000 ; "
  115. node.PostDown += "ipfw delete 65534 ; "
  116. node.PostDown += "kldunload ipfw_nat ipfw"
  117. }
  118. }
  119. node.SetLastModified()
  120. data, err := json.Marshal(&node)
  121. if err != nil {
  122. return models.Node{}, err
  123. }
  124. if err = database.Insert(node.ID, string(data), database.NODES_TABLE_NAME); err != nil {
  125. return models.Node{}, err
  126. }
  127. if err = NetworkNodesUpdatePullChanges(network); err != nil {
  128. return models.Node{}, err
  129. }
  130. return node, nil
  131. }
  132. // CreateIngressGateway - creates an ingress gateway
  133. func CreateIngressGateway(netid string, nodeid string) (models.Node, error) {
  134. node, err := GetNodeByID(nodeid)
  135. if node.OS != "linux" { // add in darwin later
  136. return models.Node{}, errors.New(node.OS + " is unsupported for ingress gateways")
  137. }
  138. if err != nil {
  139. return models.Node{}, err
  140. }
  141. network, err := GetParentNetwork(netid)
  142. if err != nil {
  143. return models.Node{}, err
  144. }
  145. node.IsIngressGateway = "yes"
  146. node.IngressGatewayRange = network.AddressRange
  147. postUpCmd := "iptables -A FORWARD -i " + node.Interface + " -j ACCEPT ; "
  148. postUpCmd += "iptables -A FORWARD -o " + node.Interface + " -j ACCEPT ; "
  149. postUpCmd += "iptables -t nat -A POSTROUTING -o " + node.Interface + " -j MASQUERADE"
  150. postDownCmd := "iptables -D FORWARD -i " + node.Interface + " -j ACCEPT ; "
  151. postDownCmd += "iptables -D FORWARD -o " + node.Interface + " -j ACCEPT ; "
  152. postDownCmd += "iptables -t nat -D POSTROUTING -o " + node.Interface + " -j MASQUERADE"
  153. if node.PostUp != "" {
  154. if !strings.Contains(node.PostUp, postUpCmd) {
  155. postUpCmd = node.PostUp + "; " + postUpCmd
  156. }
  157. }
  158. if node.PostDown != "" {
  159. if !strings.Contains(node.PostDown, postDownCmd) {
  160. postDownCmd = node.PostDown + "; " + postDownCmd
  161. }
  162. }
  163. node.SetLastModified()
  164. node.PostUp = postUpCmd
  165. node.PostDown = postDownCmd
  166. node.UDPHolePunch = "no"
  167. data, err := json.Marshal(&node)
  168. if err != nil {
  169. return models.Node{}, err
  170. }
  171. err = database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  172. if err != nil {
  173. return models.Node{}, err
  174. }
  175. err = SetNetworkNodesLastModified(netid)
  176. return node, err
  177. }
  178. // DeleteIngressGateway - deletes an ingress gateway
  179. func DeleteIngressGateway(networkName string, nodeid string) (models.Node, error) {
  180. node, err := GetNodeByID(nodeid)
  181. if err != nil {
  182. return models.Node{}, err
  183. }
  184. network, err := GetParentNetwork(networkName)
  185. if err != nil {
  186. return models.Node{}, err
  187. }
  188. // delete ext clients belonging to ingress gateway
  189. if err = DeleteGatewayExtClients(node.ID, networkName); err != nil {
  190. return models.Node{}, err
  191. }
  192. node.UDPHolePunch = network.DefaultUDPHolePunch
  193. node.LastModified = time.Now().Unix()
  194. node.IsIngressGateway = "no"
  195. node.IngressGatewayRange = ""
  196. data, err := json.Marshal(&node)
  197. if err != nil {
  198. return models.Node{}, err
  199. }
  200. err = database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  201. if err != nil {
  202. return models.Node{}, err
  203. }
  204. err = SetNetworkNodesLastModified(networkName)
  205. return node, err
  206. }
  207. // DeleteGatewayExtClients - deletes ext clients based on gateway (mac) of ingress node and network
  208. func DeleteGatewayExtClients(gatewayID string, networkName string) error {
  209. currentExtClients, err := GetNetworkExtClients(networkName)
  210. if err != nil && !database.IsEmptyRecord(err) {
  211. return err
  212. }
  213. for _, extClient := range currentExtClients {
  214. if extClient.IngressGatewayID == gatewayID {
  215. if err = DeleteExtClient(networkName, extClient.ClientID); err != nil {
  216. logger.Log(1, "failed to remove ext client", extClient.ClientID)
  217. continue
  218. }
  219. }
  220. }
  221. return nil
  222. }