acls.go 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. package logic
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "maps"
  7. "net"
  8. "github.com/gravitl/netmaker/db"
  9. "github.com/gravitl/netmaker/logic"
  10. "github.com/gravitl/netmaker/models"
  11. "github.com/gravitl/netmaker/schema"
  12. )
  13. func GetFwRulesForUserNodesOnGw(node models.Node, nodes []models.Node) (rules []models.FwRule) {
  14. defaultUserPolicy, _ := logic.GetDefaultPolicy(models.NetworkID(node.Network), models.UserPolicy)
  15. userNodes := logic.GetStaticUserNodesByNetwork(models.NetworkID(node.Network))
  16. for _, userNodeI := range userNodes {
  17. if defaultUserPolicy.Enabled {
  18. if userNodeI.StaticNode.Address != "" {
  19. rules = append(rules, models.FwRule{
  20. SrcIP: userNodeI.StaticNode.AddressIPNet4(),
  21. DstIP: net.IPNet{},
  22. AllowedProtocol: models.ALL,
  23. AllowedPorts: []string{},
  24. Allow: true,
  25. })
  26. }
  27. if userNodeI.StaticNode.Address6 != "" {
  28. rules = append(rules, models.FwRule{
  29. SrcIP: userNodeI.StaticNode.AddressIPNet6(),
  30. DstIP: net.IPNet{},
  31. AllowedProtocol: models.ALL,
  32. AllowedPorts: []string{},
  33. Allow: true,
  34. })
  35. }
  36. continue
  37. }
  38. for _, peer := range nodes {
  39. if peer.IsUserNode {
  40. continue
  41. }
  42. if ok, allowedPolicies := IsUserAllowedToCommunicate(userNodeI.StaticNode.OwnerID, peer); ok {
  43. if peer.IsStatic {
  44. peer = peer.StaticNode.ConvertToStaticNode()
  45. }
  46. for _, policy := range allowedPolicies {
  47. if userNodeI.StaticNode.Address != "" {
  48. rules = append(rules, models.FwRule{
  49. SrcIP: userNodeI.StaticNode.AddressIPNet4(),
  50. DstIP: net.IPNet{
  51. IP: peer.Address.IP,
  52. Mask: net.CIDRMask(32, 32),
  53. },
  54. AllowedProtocol: policy.Proto,
  55. AllowedPorts: policy.Port,
  56. Allow: true,
  57. })
  58. }
  59. if userNodeI.StaticNode.Address6 != "" {
  60. rules = append(rules, models.FwRule{
  61. SrcIP: userNodeI.StaticNode.AddressIPNet6(),
  62. DstIP: net.IPNet{
  63. IP: peer.Address6.IP,
  64. Mask: net.CIDRMask(128, 128),
  65. },
  66. AllowedProtocol: policy.Proto,
  67. AllowedPorts: policy.Port,
  68. Allow: true,
  69. })
  70. }
  71. // add egress ranges
  72. for _, dstI := range policy.Dst {
  73. if dstI.Value == "*" {
  74. rules = append(rules, models.FwRule{
  75. SrcIP: userNodeI.StaticNode.AddressIPNet4(),
  76. DstIP: net.IPNet{},
  77. AllowedProtocol: policy.Proto,
  78. AllowedPorts: policy.Port,
  79. Allow: true,
  80. })
  81. break
  82. }
  83. if dstI.ID == models.EgressID {
  84. e := schema.Egress{ID: dstI.Value}
  85. err := e.Get(db.WithContext(context.TODO()))
  86. if err != nil {
  87. continue
  88. }
  89. if e.Range != "" {
  90. dstI.Value = e.Range
  91. ip, cidr, err := net.ParseCIDR(dstI.Value)
  92. if err == nil {
  93. if ip.To4() != nil && userNodeI.StaticNode.Address != "" {
  94. rules = append(rules, models.FwRule{
  95. SrcIP: userNodeI.StaticNode.AddressIPNet4(),
  96. DstIP: *cidr,
  97. AllowedProtocol: policy.Proto,
  98. AllowedPorts: policy.Port,
  99. Allow: true,
  100. })
  101. } else if ip.To16() != nil && userNodeI.StaticNode.Address6 != "" {
  102. rules = append(rules, models.FwRule{
  103. SrcIP: userNodeI.StaticNode.AddressIPNet6(),
  104. DstIP: *cidr,
  105. AllowedProtocol: policy.Proto,
  106. AllowedPorts: policy.Port,
  107. Allow: true,
  108. })
  109. }
  110. }
  111. } else if len(e.DomainAns) > 0 {
  112. for _, domainAns := range e.DomainAns {
  113. dstI.Value = domainAns
  114. ip, cidr, err := net.ParseCIDR(dstI.Value)
  115. if err == nil {
  116. if ip.To4() != nil && userNodeI.StaticNode.Address != "" {
  117. rules = append(rules, models.FwRule{
  118. SrcIP: userNodeI.StaticNode.AddressIPNet4(),
  119. DstIP: *cidr,
  120. AllowedProtocol: policy.Proto,
  121. AllowedPorts: policy.Port,
  122. Allow: true,
  123. })
  124. } else if ip.To16() != nil && userNodeI.StaticNode.Address6 != "" {
  125. rules = append(rules, models.FwRule{
  126. SrcIP: userNodeI.StaticNode.AddressIPNet6(),
  127. DstIP: *cidr,
  128. AllowedProtocol: policy.Proto,
  129. AllowedPorts: policy.Port,
  130. Allow: true,
  131. })
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. return
  143. }
  144. func GetFwRulesForNodeAndPeerOnGw(node, peer models.Node, allowedPolicies []models.Acl) (rules []models.FwRule) {
  145. for _, policy := range allowedPolicies {
  146. // if static peer dst rule not for ingress node -> skip
  147. if node.Address.IP != nil {
  148. rules = append(rules, models.FwRule{
  149. SrcIP: net.IPNet{
  150. IP: node.Address.IP,
  151. Mask: net.CIDRMask(32, 32),
  152. },
  153. DstIP: net.IPNet{
  154. IP: peer.Address.IP,
  155. Mask: net.CIDRMask(32, 32),
  156. },
  157. AllowedProtocol: policy.Proto,
  158. AllowedPorts: policy.Port,
  159. Allow: true,
  160. })
  161. }
  162. if node.Address6.IP != nil {
  163. rules = append(rules, models.FwRule{
  164. SrcIP: net.IPNet{
  165. IP: node.Address6.IP,
  166. Mask: net.CIDRMask(128, 128),
  167. },
  168. DstIP: net.IPNet{
  169. IP: peer.Address6.IP,
  170. Mask: net.CIDRMask(128, 128),
  171. },
  172. AllowedProtocol: policy.Proto,
  173. AllowedPorts: policy.Port,
  174. Allow: true,
  175. })
  176. }
  177. if policy.AllowedDirection == models.TrafficDirectionBi {
  178. if node.Address.IP != nil {
  179. rules = append(rules, models.FwRule{
  180. SrcIP: net.IPNet{
  181. IP: peer.Address.IP,
  182. Mask: net.CIDRMask(32, 32),
  183. },
  184. DstIP: net.IPNet{
  185. IP: node.Address.IP,
  186. Mask: net.CIDRMask(32, 32),
  187. },
  188. AllowedProtocol: policy.Proto,
  189. AllowedPorts: policy.Port,
  190. Allow: true,
  191. })
  192. }
  193. if node.Address6.IP != nil {
  194. rules = append(rules, models.FwRule{
  195. SrcIP: net.IPNet{
  196. IP: peer.Address6.IP,
  197. Mask: net.CIDRMask(128, 128),
  198. },
  199. DstIP: net.IPNet{
  200. IP: node.Address6.IP,
  201. Mask: net.CIDRMask(128, 128),
  202. },
  203. AllowedProtocol: policy.Proto,
  204. AllowedPorts: policy.Port,
  205. Allow: true,
  206. })
  207. }
  208. }
  209. if len(node.StaticNode.ExtraAllowedIPs) > 0 {
  210. for _, additionalAllowedIPNet := range node.StaticNode.ExtraAllowedIPs {
  211. _, ipNet, err := net.ParseCIDR(additionalAllowedIPNet)
  212. if err != nil {
  213. continue
  214. }
  215. if ipNet.IP.To4() != nil && peer.Address.IP != nil {
  216. rules = append(rules, models.FwRule{
  217. SrcIP: net.IPNet{
  218. IP: peer.Address.IP,
  219. Mask: net.CIDRMask(32, 32),
  220. },
  221. DstIP: *ipNet,
  222. Allow: true,
  223. })
  224. } else if peer.Address6.IP != nil {
  225. rules = append(rules, models.FwRule{
  226. SrcIP: net.IPNet{
  227. IP: peer.Address6.IP,
  228. Mask: net.CIDRMask(128, 128),
  229. },
  230. DstIP: *ipNet,
  231. Allow: true,
  232. })
  233. }
  234. }
  235. }
  236. if len(peer.StaticNode.ExtraAllowedIPs) > 0 {
  237. for _, additionalAllowedIPNet := range peer.StaticNode.ExtraAllowedIPs {
  238. _, ipNet, err := net.ParseCIDR(additionalAllowedIPNet)
  239. if err != nil {
  240. continue
  241. }
  242. if ipNet.IP.To4() != nil && node.Address.IP != nil {
  243. rules = append(rules, models.FwRule{
  244. SrcIP: net.IPNet{
  245. IP: node.Address.IP,
  246. Mask: net.CIDRMask(32, 32),
  247. },
  248. DstIP: *ipNet,
  249. Allow: true,
  250. })
  251. } else if node.Address6.IP != nil {
  252. rules = append(rules, models.FwRule{
  253. SrcIP: net.IPNet{
  254. IP: node.Address6.IP,
  255. Mask: net.CIDRMask(128, 128),
  256. },
  257. DstIP: *ipNet,
  258. Allow: true,
  259. })
  260. }
  261. }
  262. }
  263. // add egress range rules
  264. for _, dstI := range policy.Dst {
  265. if dstI.ID == models.EgressID {
  266. e := schema.Egress{ID: dstI.Value}
  267. err := e.Get(db.WithContext(context.TODO()))
  268. if err != nil {
  269. continue
  270. }
  271. if e.Range != "" {
  272. dstI.Value = e.Range
  273. ip, cidr, err := net.ParseCIDR(dstI.Value)
  274. if err == nil {
  275. if ip.To4() != nil {
  276. if node.Address.IP != nil {
  277. rules = append(rules, models.FwRule{
  278. SrcIP: net.IPNet{
  279. IP: node.Address.IP,
  280. Mask: net.CIDRMask(32, 32),
  281. },
  282. DstIP: *cidr,
  283. AllowedProtocol: policy.Proto,
  284. AllowedPorts: policy.Port,
  285. Allow: true,
  286. })
  287. }
  288. } else {
  289. if node.Address6.IP != nil {
  290. rules = append(rules, models.FwRule{
  291. SrcIP: net.IPNet{
  292. IP: node.Address6.IP,
  293. Mask: net.CIDRMask(128, 128),
  294. },
  295. DstIP: *cidr,
  296. AllowedProtocol: policy.Proto,
  297. AllowedPorts: policy.Port,
  298. Allow: true,
  299. })
  300. }
  301. }
  302. }
  303. } else if len(e.DomainAns) > 0 {
  304. for _, domainAnsI := range e.DomainAns {
  305. dstI.Value = domainAnsI
  306. ip, cidr, err := net.ParseCIDR(dstI.Value)
  307. if err == nil {
  308. if ip.To4() != nil {
  309. if node.Address.IP != nil {
  310. rules = append(rules, models.FwRule{
  311. SrcIP: net.IPNet{
  312. IP: node.Address.IP,
  313. Mask: net.CIDRMask(32, 32),
  314. },
  315. DstIP: *cidr,
  316. AllowedProtocol: policy.Proto,
  317. AllowedPorts: policy.Port,
  318. Allow: true,
  319. })
  320. }
  321. } else {
  322. if node.Address6.IP != nil {
  323. rules = append(rules, models.FwRule{
  324. SrcIP: net.IPNet{
  325. IP: node.Address6.IP,
  326. Mask: net.CIDRMask(128, 128),
  327. },
  328. DstIP: *cidr,
  329. AllowedProtocol: policy.Proto,
  330. AllowedPorts: policy.Port,
  331. Allow: true,
  332. })
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. }
  340. }
  341. return
  342. }
  343. func checkIfAclTagisValid(a models.Acl, t models.AclPolicyTag, isSrc bool) (err error) {
  344. switch t.ID {
  345. case models.NodeTagID:
  346. if a.RuleType == models.UserPolicy && isSrc {
  347. return errors.New("user policy source mismatch")
  348. }
  349. // check if tag is valid
  350. _, err := GetTag(models.TagID(t.Value))
  351. if err != nil {
  352. return errors.New("invalid tag " + t.Value)
  353. }
  354. case models.NodeID:
  355. if a.RuleType == models.UserPolicy && isSrc {
  356. return errors.New("user policy source mismatch")
  357. }
  358. _, nodeErr := logic.GetNodeByID(t.Value)
  359. if nodeErr != nil {
  360. _, staticNodeErr := logic.GetExtClient(t.Value, a.NetworkID.String())
  361. if staticNodeErr != nil {
  362. return errors.New("invalid node " + t.Value)
  363. }
  364. }
  365. case models.EgressID, models.EgressRange:
  366. e := schema.Egress{
  367. ID: t.Value,
  368. }
  369. err := e.Get(db.WithContext(context.TODO()))
  370. if err != nil {
  371. return errors.New("invalid egress")
  372. }
  373. case models.UserAclID:
  374. if a.RuleType == models.DevicePolicy {
  375. return errors.New("device policy source mismatch")
  376. }
  377. if !isSrc {
  378. return errors.New("user cannot be added to destination")
  379. }
  380. _, err := logic.GetUser(t.Value)
  381. if err != nil {
  382. return errors.New("invalid user " + t.Value)
  383. }
  384. case models.UserGroupAclID:
  385. if a.RuleType == models.DevicePolicy {
  386. return errors.New("device policy source mismatch")
  387. }
  388. if !isSrc {
  389. return errors.New("user cannot be added to destination")
  390. }
  391. err := IsGroupValid(models.UserGroupID(t.Value))
  392. if err != nil {
  393. return errors.New("invalid user group " + t.Value)
  394. }
  395. // check if group belongs to this network
  396. netGrps := GetUserGroupsInNetwork(a.NetworkID)
  397. if _, ok := netGrps[models.UserGroupID(t.Value)]; !ok {
  398. return errors.New("invalid user group " + t.Value)
  399. }
  400. default:
  401. return errors.New("invalid policy")
  402. }
  403. return nil
  404. }
  405. // IsAclPolicyValid - validates if acl policy is valid
  406. func IsAclPolicyValid(acl models.Acl) (err error) {
  407. //check if src and dst are valid
  408. if acl.AllowedDirection != models.TrafficDirectionBi &&
  409. acl.AllowedDirection != models.TrafficDirectionUni {
  410. return errors.New("invalid traffic direction")
  411. }
  412. switch acl.RuleType {
  413. case models.UserPolicy:
  414. // src list should only contain users
  415. for _, srcI := range acl.Src {
  416. if srcI.Value == "*" {
  417. continue
  418. }
  419. // check if user group is valid
  420. if err = checkIfAclTagisValid(acl, srcI, true); err != nil {
  421. return
  422. }
  423. }
  424. for _, dstI := range acl.Dst {
  425. if dstI.Value == "*" {
  426. continue
  427. }
  428. // check if user group is valid
  429. if err = checkIfAclTagisValid(acl, dstI, false); err != nil {
  430. return
  431. }
  432. }
  433. case models.DevicePolicy:
  434. for _, srcI := range acl.Src {
  435. if srcI.Value == "*" {
  436. continue
  437. }
  438. // check if user group is valid
  439. if err = checkIfAclTagisValid(acl, srcI, true); err != nil {
  440. return err
  441. }
  442. }
  443. for _, dstI := range acl.Dst {
  444. if dstI.Value == "*" {
  445. continue
  446. }
  447. // check if user group is valid
  448. if err = checkIfAclTagisValid(acl, dstI, false); err != nil {
  449. return
  450. }
  451. }
  452. }
  453. return nil
  454. }
  455. // ListUserPolicies - lists all acl policies enforced on an user
  456. func ListUserPolicies(u models.User) []models.Acl {
  457. allAcls := logic.ListAcls()
  458. userAcls := []models.Acl{}
  459. for _, acl := range allAcls {
  460. if acl.RuleType == models.UserPolicy {
  461. srcMap := logic.ConvAclTagToValueMap(acl.Src)
  462. if _, ok := srcMap[u.UserName]; ok {
  463. userAcls = append(userAcls, acl)
  464. } else {
  465. // check for user groups
  466. for gID := range u.UserGroups {
  467. if _, ok := srcMap[gID.String()]; ok {
  468. userAcls = append(userAcls, acl)
  469. break
  470. }
  471. }
  472. }
  473. }
  474. }
  475. return userAcls
  476. }
  477. // listPoliciesOfUser - lists all user acl policies applied to user in an network
  478. func listPoliciesOfUser(user models.User, netID models.NetworkID) []models.Acl {
  479. allAcls := logic.ListAcls()
  480. userAcls := []models.Acl{}
  481. for _, acl := range allAcls {
  482. if acl.NetworkID == netID && acl.RuleType == models.UserPolicy {
  483. srcMap := logic.ConvAclTagToValueMap(acl.Src)
  484. if _, ok := srcMap[user.UserName]; ok {
  485. userAcls = append(userAcls, acl)
  486. continue
  487. }
  488. for netRole := range user.NetworkRoles {
  489. if _, ok := srcMap[netRole.String()]; ok {
  490. userAcls = append(userAcls, acl)
  491. continue
  492. }
  493. }
  494. for userG := range user.UserGroups {
  495. if _, ok := srcMap[userG.String()]; ok {
  496. userAcls = append(userAcls, acl)
  497. continue
  498. }
  499. }
  500. }
  501. }
  502. return userAcls
  503. }
  504. // listUserPolicies - lists all user policies in a network
  505. func listUserPolicies(netID models.NetworkID) []models.Acl {
  506. allAcls := logic.ListAcls()
  507. deviceAcls := []models.Acl{}
  508. for _, acl := range allAcls {
  509. if acl.NetworkID == netID && acl.RuleType == models.UserPolicy {
  510. deviceAcls = append(deviceAcls, acl)
  511. }
  512. }
  513. return deviceAcls
  514. }
  515. // IsUserAllowedToCommunicate - check if user is allowed to communicate with peer
  516. func IsUserAllowedToCommunicate(userName string, peer models.Node) (bool, []models.Acl) {
  517. var peerId string
  518. if peer.IsStatic {
  519. peerId = peer.StaticNode.ClientID
  520. peer = peer.StaticNode.ConvertToStaticNode()
  521. } else {
  522. peerId = peer.ID.String()
  523. }
  524. var peerTags map[models.TagID]struct{}
  525. if peer.Mutex != nil {
  526. peer.Mutex.Lock()
  527. peerTags = maps.Clone(peer.Tags)
  528. peer.Mutex.Unlock()
  529. } else {
  530. peerTags = peer.Tags
  531. }
  532. if peerTags == nil {
  533. peerTags = make(map[models.TagID]struct{})
  534. }
  535. peerTags[models.TagID(peerId)] = struct{}{}
  536. peerTags[models.TagID("*")] = struct{}{}
  537. acl, _ := logic.GetDefaultPolicy(models.NetworkID(peer.Network), models.UserPolicy)
  538. if acl.Enabled {
  539. return true, []models.Acl{acl}
  540. }
  541. user, err := logic.GetUser(userName)
  542. if err != nil {
  543. return false, []models.Acl{}
  544. }
  545. allowedPolicies := []models.Acl{}
  546. policies := listPoliciesOfUser(*user, models.NetworkID(peer.Network))
  547. for _, policy := range policies {
  548. if !policy.Enabled {
  549. continue
  550. }
  551. dstMap := logic.ConvAclTagToValueMap(policy.Dst)
  552. for _, dst := range policy.Dst {
  553. if dst.ID == models.EgressID {
  554. e := schema.Egress{ID: dst.Value}
  555. err := e.Get(db.WithContext(context.TODO()))
  556. if err == nil && e.Status {
  557. for nodeID := range e.Nodes {
  558. dstMap[nodeID] = struct{}{}
  559. }
  560. }
  561. }
  562. }
  563. if _, ok := dstMap["*"]; ok {
  564. allowedPolicies = append(allowedPolicies, policy)
  565. continue
  566. }
  567. if _, ok := dstMap[peer.ID.String()]; ok {
  568. allowedPolicies = append(allowedPolicies, policy)
  569. continue
  570. }
  571. for tagID := range peerTags {
  572. if _, ok := dstMap[tagID.String()]; ok {
  573. allowedPolicies = append(allowedPolicies, policy)
  574. break
  575. }
  576. }
  577. }
  578. if len(allowedPolicies) > 0 {
  579. return true, allowedPolicies
  580. }
  581. return false, []models.Acl{}
  582. }
  583. // IsPeerAllowed - checks if peer needs to be added to the interface
  584. func IsPeerAllowed(node, peer models.Node, checkDefaultPolicy bool) bool {
  585. var nodeId, peerId string
  586. // if peer.IsFailOver && node.FailedOverBy != uuid.Nil && node.FailedOverBy == peer.ID {
  587. // return true
  588. // }
  589. // if node.IsFailOver && peer.FailedOverBy != uuid.Nil && peer.FailedOverBy == node.ID {
  590. // return true
  591. // }
  592. // if node.IsGw && peer.IsRelayed && peer.RelayedBy == node.ID.String() {
  593. // return true
  594. // }
  595. // if peer.IsGw && node.IsRelayed && node.RelayedBy == peer.ID.String() {
  596. // return true
  597. // }
  598. if node.IsStatic {
  599. nodeId = node.StaticNode.ClientID
  600. node = node.StaticNode.ConvertToStaticNode()
  601. } else {
  602. nodeId = node.ID.String()
  603. }
  604. if peer.IsStatic {
  605. peerId = peer.StaticNode.ClientID
  606. peer = peer.StaticNode.ConvertToStaticNode()
  607. } else {
  608. peerId = peer.ID.String()
  609. }
  610. var nodeTags, peerTags map[models.TagID]struct{}
  611. if node.Mutex != nil {
  612. node.Mutex.Lock()
  613. nodeTags = maps.Clone(node.Tags)
  614. node.Mutex.Unlock()
  615. } else {
  616. nodeTags = node.Tags
  617. }
  618. if peer.Mutex != nil {
  619. peer.Mutex.Lock()
  620. peerTags = maps.Clone(peer.Tags)
  621. peer.Mutex.Unlock()
  622. } else {
  623. peerTags = peer.Tags
  624. }
  625. if nodeTags == nil {
  626. nodeTags = make(map[models.TagID]struct{})
  627. }
  628. if peerTags == nil {
  629. peerTags = make(map[models.TagID]struct{})
  630. }
  631. nodeTags[models.TagID(nodeId)] = struct{}{}
  632. peerTags[models.TagID(peerId)] = struct{}{}
  633. if checkDefaultPolicy {
  634. // check default policy if all allowed return true
  635. defaultPolicy, err := logic.GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  636. if err == nil {
  637. if defaultPolicy.Enabled {
  638. return true
  639. }
  640. }
  641. }
  642. // list device policies
  643. policies := logic.ListDevicePolicies(models.NetworkID(peer.Network))
  644. srcMap := make(map[string]struct{})
  645. dstMap := make(map[string]struct{})
  646. defer func() {
  647. srcMap = nil
  648. dstMap = nil
  649. }()
  650. for _, policy := range policies {
  651. if !policy.Enabled {
  652. continue
  653. }
  654. srcMap = logic.ConvAclTagToValueMap(policy.Src)
  655. dstMap = logic.ConvAclTagToValueMap(policy.Dst)
  656. for _, dst := range policy.Dst {
  657. if dst.ID == models.EgressID {
  658. e := schema.Egress{ID: dst.Value}
  659. err := e.Get(db.WithContext(context.TODO()))
  660. if err == nil && e.Status {
  661. for nodeID := range e.Nodes {
  662. dstMap[nodeID] = struct{}{}
  663. }
  664. }
  665. }
  666. }
  667. if logic.CheckTagGroupPolicy(srcMap, dstMap, node, peer, nodeTags, peerTags) {
  668. return true
  669. }
  670. }
  671. return false
  672. }
  673. func RemoveUserFromAclPolicy(userName string) {
  674. acls := logic.ListAcls()
  675. for _, acl := range acls {
  676. delete := false
  677. update := false
  678. if acl.RuleType == models.UserPolicy {
  679. for i := len(acl.Src) - 1; i >= 0; i-- {
  680. if acl.Src[i].ID == models.UserAclID && acl.Src[i].Value == userName {
  681. if len(acl.Src) == 1 {
  682. // delete policy
  683. delete = true
  684. break
  685. } else {
  686. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  687. update = true
  688. }
  689. }
  690. }
  691. if delete {
  692. logic.DeleteAcl(acl)
  693. continue
  694. }
  695. if update {
  696. logic.UpsertAcl(acl)
  697. }
  698. }
  699. }
  700. }
  701. // IsNodeAllowedToCommunicate - check node is allowed to communicate with the peer // ADD ALLOWED DIRECTION - 0 => node -> peer, 1 => peer-> node,
  702. func IsNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool) (bool, []models.Acl) {
  703. var nodeId, peerId string
  704. // if peer.IsFailOver && node.FailedOverBy != uuid.Nil && node.FailedOverBy == peer.ID {
  705. // return true, []models.Acl{}
  706. // }
  707. // if node.IsFailOver && peer.FailedOverBy != uuid.Nil && peer.FailedOverBy == node.ID {
  708. // return true, []models.Acl{}
  709. // }
  710. // if node.IsGw && peer.IsRelayed && peer.RelayedBy == node.ID.String() {
  711. // return true, []models.Acl{}
  712. // }
  713. // if peer.IsGw && node.IsRelayed && node.RelayedBy == peer.ID.String() {
  714. // return true, []models.Acl{}
  715. // }
  716. if node.IsStatic {
  717. nodeId = node.StaticNode.ClientID
  718. node = node.StaticNode.ConvertToStaticNode()
  719. } else {
  720. nodeId = node.ID.String()
  721. }
  722. if peer.IsStatic {
  723. peerId = peer.StaticNode.ClientID
  724. peer = peer.StaticNode.ConvertToStaticNode()
  725. } else {
  726. peerId = peer.ID.String()
  727. }
  728. var nodeTags, peerTags map[models.TagID]struct{}
  729. if node.Mutex != nil {
  730. node.Mutex.Lock()
  731. nodeTags = maps.Clone(node.Tags)
  732. node.Mutex.Unlock()
  733. } else {
  734. nodeTags = node.Tags
  735. }
  736. if peer.Mutex != nil {
  737. peer.Mutex.Lock()
  738. peerTags = maps.Clone(peer.Tags)
  739. peer.Mutex.Unlock()
  740. } else {
  741. peerTags = peer.Tags
  742. }
  743. if nodeTags == nil {
  744. nodeTags = make(map[models.TagID]struct{})
  745. }
  746. if peerTags == nil {
  747. peerTags = make(map[models.TagID]struct{})
  748. }
  749. nodeTags[models.TagID(nodeId)] = struct{}{}
  750. peerTags[models.TagID(peerId)] = struct{}{}
  751. if checkDefaultPolicy {
  752. // check default policy if all allowed return true
  753. defaultPolicy, err := logic.GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
  754. if err == nil {
  755. if defaultPolicy.Enabled {
  756. return true, []models.Acl{defaultPolicy}
  757. }
  758. }
  759. }
  760. allowedPolicies := []models.Acl{}
  761. defer func() {
  762. allowedPolicies = logic.UniquePolicies(allowedPolicies)
  763. }()
  764. // list device policies
  765. policies := logic.ListDevicePolicies(models.NetworkID(peer.Network))
  766. srcMap := make(map[string]struct{})
  767. dstMap := make(map[string]struct{})
  768. defer func() {
  769. srcMap = nil
  770. dstMap = nil
  771. }()
  772. for _, policy := range policies {
  773. if !policy.Enabled {
  774. continue
  775. }
  776. allowed := false
  777. srcMap = logic.ConvAclTagToValueMap(policy.Src)
  778. dstMap = logic.ConvAclTagToValueMap(policy.Dst)
  779. for _, dst := range policy.Dst {
  780. if dst.ID == models.EgressID {
  781. e := schema.Egress{ID: dst.Value}
  782. err := e.Get(db.WithContext(context.TODO()))
  783. if err == nil && e.Status {
  784. for nodeID := range e.Nodes {
  785. dstMap[nodeID] = struct{}{}
  786. }
  787. }
  788. }
  789. }
  790. _, srcAll := srcMap["*"]
  791. _, dstAll := dstMap["*"]
  792. if policy.AllowedDirection == models.TrafficDirectionBi {
  793. if _, ok := srcMap[nodeId]; ok || srcAll {
  794. if _, ok := dstMap[peerId]; ok || dstAll {
  795. allowedPolicies = append(allowedPolicies, policy)
  796. continue
  797. }
  798. }
  799. if _, ok := dstMap[nodeId]; ok || dstAll {
  800. if _, ok := srcMap[peerId]; ok || srcAll {
  801. allowedPolicies = append(allowedPolicies, policy)
  802. continue
  803. }
  804. }
  805. }
  806. if _, ok := dstMap[peerId]; ok || dstAll {
  807. if _, ok := srcMap[nodeId]; ok || srcAll {
  808. allowedPolicies = append(allowedPolicies, policy)
  809. continue
  810. }
  811. }
  812. if policy.AllowedDirection == models.TrafficDirectionBi {
  813. for tagID := range nodeTags {
  814. if _, ok := dstMap[tagID.String()]; ok || dstAll {
  815. if srcAll {
  816. allowed = true
  817. break
  818. }
  819. for tagID := range peerTags {
  820. if _, ok := srcMap[tagID.String()]; ok {
  821. allowed = true
  822. break
  823. }
  824. }
  825. }
  826. if allowed {
  827. allowedPolicies = append(allowedPolicies, policy)
  828. break
  829. }
  830. if _, ok := srcMap[tagID.String()]; ok || srcAll {
  831. if dstAll {
  832. allowed = true
  833. break
  834. }
  835. for tagID := range peerTags {
  836. if _, ok := dstMap[tagID.String()]; ok {
  837. allowed = true
  838. break
  839. }
  840. }
  841. }
  842. if allowed {
  843. break
  844. }
  845. }
  846. if allowed {
  847. allowedPolicies = append(allowedPolicies, policy)
  848. continue
  849. }
  850. }
  851. for tagID := range peerTags {
  852. if _, ok := dstMap[tagID.String()]; ok || dstAll {
  853. if srcAll {
  854. allowed = true
  855. break
  856. }
  857. for tagID := range nodeTags {
  858. if _, ok := srcMap[tagID.String()]; ok {
  859. allowed = true
  860. break
  861. }
  862. }
  863. }
  864. if allowed {
  865. break
  866. }
  867. }
  868. if allowed {
  869. allowedPolicies = append(allowedPolicies, policy)
  870. }
  871. }
  872. if len(allowedPolicies) > 0 {
  873. return true, allowedPolicies
  874. }
  875. return false, allowedPolicies
  876. }
  877. // UpdateDeviceTag - updates device tag on acl policies
  878. func UpdateDeviceTag(OldID, newID models.TagID, netID models.NetworkID) {
  879. acls := logic.ListDevicePolicies(netID)
  880. update := false
  881. for _, acl := range acls {
  882. for i, srcTagI := range acl.Src {
  883. if srcTagI.ID == models.NodeTagID {
  884. if OldID.String() == srcTagI.Value {
  885. acl.Src[i].Value = newID.String()
  886. update = true
  887. }
  888. }
  889. }
  890. for i, dstTagI := range acl.Dst {
  891. if dstTagI.ID == models.NodeTagID {
  892. if OldID.String() == dstTagI.Value {
  893. acl.Dst[i].Value = newID.String()
  894. update = true
  895. }
  896. }
  897. }
  898. if update {
  899. logic.UpsertAcl(acl)
  900. }
  901. }
  902. }
  903. func CheckIfTagAsActivePolicy(tagID models.TagID, netID models.NetworkID) bool {
  904. acls := logic.ListDevicePolicies(netID)
  905. for _, acl := range acls {
  906. for _, srcTagI := range acl.Src {
  907. if srcTagI.ID == models.NodeTagID {
  908. if tagID.String() == srcTagI.Value {
  909. return true
  910. }
  911. }
  912. }
  913. for _, dstTagI := range acl.Dst {
  914. if dstTagI.ID == models.NodeTagID {
  915. if tagID.String() == dstTagI.Value {
  916. return true
  917. }
  918. }
  919. }
  920. }
  921. return false
  922. }
  923. // RemoveDeviceTagFromAclPolicies - remove device tag from acl policies
  924. func RemoveDeviceTagFromAclPolicies(tagID models.TagID, netID models.NetworkID) error {
  925. acls := logic.ListDevicePolicies(netID)
  926. update := false
  927. for _, acl := range acls {
  928. for i := len(acl.Src) - 1; i >= 0; i-- {
  929. if acl.Src[i].ID == models.NodeTagID {
  930. if tagID.String() == acl.Src[i].Value {
  931. acl.Src = append(acl.Src[:i], acl.Src[i+1:]...)
  932. update = true
  933. }
  934. }
  935. }
  936. for i := len(acl.Dst) - 1; i >= 0; i-- {
  937. if acl.Dst[i].ID == models.NodeTagID {
  938. if tagID.String() == acl.Dst[i].Value {
  939. acl.Dst = append(acl.Dst[:i], acl.Dst[i+1:]...)
  940. update = true
  941. }
  942. }
  943. }
  944. if update {
  945. logic.UpsertAcl(acl)
  946. }
  947. }
  948. return nil
  949. }
  950. func getEgressUserRulesForNode(targetnode *models.Node,
  951. rules map[string]models.AclRule) map[string]models.AclRule {
  952. userNodes := logic.GetStaticUserNodesByNetwork(models.NetworkID(targetnode.Network))
  953. userGrpMap := GetUserGrpMap()
  954. allowedUsers := make(map[string][]models.Acl)
  955. acls := listUserPolicies(models.NetworkID(targetnode.Network))
  956. var targetNodeTags = make(map[models.TagID]struct{})
  957. targetNodeTags["*"] = struct{}{}
  958. egs, _ := (&schema.Egress{Network: targetnode.Network}).ListByNetwork(db.WithContext(context.TODO()))
  959. if len(egs) == 0 {
  960. return rules
  961. }
  962. defaultPolicy, _ := logic.GetDefaultPolicy(models.NetworkID(targetnode.Network), models.UserPolicy)
  963. for _, egI := range egs {
  964. if !egI.Status {
  965. continue
  966. }
  967. if _, ok := egI.Nodes[targetnode.ID.String()]; ok {
  968. if egI.Range != "" {
  969. targetNodeTags[models.TagID(egI.Range)] = struct{}{}
  970. } else if len(egI.DomainAns) > 0 {
  971. for _, domainAnsI := range egI.DomainAns {
  972. targetNodeTags[models.TagID(domainAnsI)] = struct{}{}
  973. }
  974. }
  975. targetNodeTags[models.TagID(egI.ID)] = struct{}{}
  976. }
  977. }
  978. if !defaultPolicy.Enabled {
  979. for _, acl := range acls {
  980. if !acl.Enabled {
  981. continue
  982. }
  983. dstTags := logic.ConvAclTagToValueMap(acl.Dst)
  984. for _, dst := range acl.Dst {
  985. if dst.ID == models.EgressID {
  986. e := schema.Egress{ID: dst.Value}
  987. err := e.Get(db.WithContext(context.TODO()))
  988. if err == nil && e.Status {
  989. for nodeID := range e.Nodes {
  990. dstTags[nodeID] = struct{}{}
  991. }
  992. if e.Range != "" {
  993. dstTags[e.Range] = struct{}{}
  994. } else if len(e.DomainAns) > 0 {
  995. for _, domainAnsI := range e.DomainAns {
  996. dstTags[domainAnsI] = struct{}{}
  997. }
  998. }
  999. }
  1000. }
  1001. }
  1002. _, all := dstTags["*"]
  1003. addUsers := false
  1004. if !all {
  1005. for nodeTag := range targetNodeTags {
  1006. if _, ok := dstTags[nodeTag.String()]; ok {
  1007. addUsers = true
  1008. break
  1009. }
  1010. }
  1011. } else {
  1012. addUsers = true
  1013. }
  1014. if addUsers {
  1015. // get all src tags
  1016. for _, srcAcl := range acl.Src {
  1017. if srcAcl.ID == models.UserAclID {
  1018. allowedUsers[srcAcl.Value] = append(allowedUsers[srcAcl.Value], acl)
  1019. } else if srcAcl.ID == models.UserGroupAclID {
  1020. // fetch all users in the group
  1021. if usersMap, ok := userGrpMap[models.UserGroupID(srcAcl.Value)]; ok {
  1022. for userName := range usersMap {
  1023. allowedUsers[userName] = append(allowedUsers[userName], acl)
  1024. }
  1025. }
  1026. }
  1027. }
  1028. }
  1029. }
  1030. }
  1031. if defaultPolicy.Enabled {
  1032. r := models.AclRule{
  1033. ID: defaultPolicy.ID,
  1034. AllowedProtocol: defaultPolicy.Proto,
  1035. AllowedPorts: defaultPolicy.Port,
  1036. Direction: defaultPolicy.AllowedDirection,
  1037. Allowed: true,
  1038. }
  1039. for _, userNode := range userNodes {
  1040. if !userNode.StaticNode.Enabled {
  1041. continue
  1042. }
  1043. // Get peers in the tags and add allowed rules
  1044. if userNode.StaticNode.Address != "" {
  1045. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  1046. }
  1047. if userNode.StaticNode.Address6 != "" {
  1048. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  1049. }
  1050. }
  1051. rules[defaultPolicy.ID] = r
  1052. } else {
  1053. for _, userNode := range userNodes {
  1054. if !userNode.StaticNode.Enabled {
  1055. continue
  1056. }
  1057. acls, ok := allowedUsers[userNode.StaticNode.OwnerID]
  1058. if !ok {
  1059. continue
  1060. }
  1061. for _, acl := range acls {
  1062. if !acl.Enabled {
  1063. continue
  1064. }
  1065. r := models.AclRule{
  1066. ID: acl.ID,
  1067. AllowedProtocol: acl.Proto,
  1068. AllowedPorts: acl.Port,
  1069. Direction: acl.AllowedDirection,
  1070. Allowed: true,
  1071. }
  1072. // Get peers in the tags and add allowed rules
  1073. if userNode.StaticNode.Address != "" {
  1074. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  1075. }
  1076. if userNode.StaticNode.Address6 != "" {
  1077. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  1078. }
  1079. for _, dstI := range acl.Dst {
  1080. if dstI.ID == models.EgressID {
  1081. e := schema.Egress{ID: dstI.Value}
  1082. err := e.Get(db.WithContext(context.TODO()))
  1083. if err != nil {
  1084. continue
  1085. }
  1086. if e.Range != "" {
  1087. ip, cidr, err := net.ParseCIDR(e.Range)
  1088. if err == nil {
  1089. if ip.To4() != nil {
  1090. r.Dst = append(r.Dst, *cidr)
  1091. } else {
  1092. r.Dst6 = append(r.Dst6, *cidr)
  1093. }
  1094. }
  1095. } else if len(e.DomainAns) > 0 {
  1096. for _, domainAnsI := range e.DomainAns {
  1097. ip, cidr, err := net.ParseCIDR(domainAnsI)
  1098. if err == nil {
  1099. if ip.To4() != nil {
  1100. r.Dst = append(r.Dst, *cidr)
  1101. } else {
  1102. r.Dst6 = append(r.Dst6, *cidr)
  1103. }
  1104. }
  1105. }
  1106. }
  1107. }
  1108. }
  1109. if aclRule, ok := rules[acl.ID]; ok {
  1110. aclRule.IPList = append(aclRule.IPList, r.IPList...)
  1111. aclRule.IP6List = append(aclRule.IP6List, r.IP6List...)
  1112. rules[acl.ID] = aclRule
  1113. } else {
  1114. rules[acl.ID] = r
  1115. }
  1116. }
  1117. }
  1118. }
  1119. return rules
  1120. }
  1121. func getUserAclRulesForNode(targetnode *models.Node,
  1122. rules map[string]models.AclRule) map[string]models.AclRule {
  1123. userNodes := logic.GetStaticUserNodesByNetwork(models.NetworkID(targetnode.Network))
  1124. userGrpMap := GetUserGrpMap()
  1125. allowedUsers := make(map[string][]models.Acl)
  1126. acls := listUserPolicies(models.NetworkID(targetnode.Network))
  1127. var targetNodeTags = make(map[models.TagID]struct{})
  1128. if targetnode.Mutex != nil {
  1129. targetnode.Mutex.Lock()
  1130. targetNodeTags = maps.Clone(targetnode.Tags)
  1131. targetnode.Mutex.Unlock()
  1132. } else {
  1133. targetNodeTags = maps.Clone(targetnode.Tags)
  1134. }
  1135. if targetNodeTags == nil {
  1136. targetNodeTags = make(map[models.TagID]struct{})
  1137. }
  1138. defaultPolicy, _ := logic.GetDefaultPolicy(models.NetworkID(targetnode.Network), models.UserPolicy)
  1139. targetNodeTags[models.TagID(targetnode.ID.String())] = struct{}{}
  1140. if !defaultPolicy.Enabled {
  1141. for _, acl := range acls {
  1142. if !acl.Enabled {
  1143. continue
  1144. }
  1145. dstTags := logic.ConvAclTagToValueMap(acl.Dst)
  1146. _, all := dstTags["*"]
  1147. addUsers := false
  1148. if !all {
  1149. for nodeTag := range targetNodeTags {
  1150. if _, ok := dstTags[nodeTag.String()]; ok {
  1151. addUsers = true
  1152. break
  1153. }
  1154. }
  1155. } else {
  1156. addUsers = true
  1157. }
  1158. if addUsers {
  1159. // get all src tags
  1160. for _, srcAcl := range acl.Src {
  1161. if srcAcl.ID == models.UserAclID {
  1162. allowedUsers[srcAcl.Value] = append(allowedUsers[srcAcl.Value], acl)
  1163. } else if srcAcl.ID == models.UserGroupAclID {
  1164. // fetch all users in the group
  1165. if usersMap, ok := userGrpMap[models.UserGroupID(srcAcl.Value)]; ok {
  1166. for userName := range usersMap {
  1167. allowedUsers[userName] = append(allowedUsers[userName], acl)
  1168. }
  1169. }
  1170. }
  1171. }
  1172. }
  1173. }
  1174. }
  1175. if defaultPolicy.Enabled {
  1176. r := models.AclRule{
  1177. ID: defaultPolicy.ID,
  1178. AllowedProtocol: defaultPolicy.Proto,
  1179. AllowedPorts: defaultPolicy.Port,
  1180. Direction: defaultPolicy.AllowedDirection,
  1181. Allowed: true,
  1182. }
  1183. for _, userNode := range userNodes {
  1184. if !userNode.StaticNode.Enabled {
  1185. continue
  1186. }
  1187. // Get peers in the tags and add allowed rules
  1188. if userNode.StaticNode.Address != "" {
  1189. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  1190. }
  1191. if userNode.StaticNode.Address6 != "" {
  1192. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  1193. }
  1194. }
  1195. rules[defaultPolicy.ID] = r
  1196. } else {
  1197. for _, userNode := range userNodes {
  1198. if !userNode.StaticNode.Enabled {
  1199. continue
  1200. }
  1201. acls, ok := allowedUsers[userNode.StaticNode.OwnerID]
  1202. if !ok {
  1203. continue
  1204. }
  1205. for _, acl := range acls {
  1206. if !acl.Enabled {
  1207. continue
  1208. }
  1209. r := models.AclRule{
  1210. ID: acl.ID,
  1211. AllowedProtocol: acl.Proto,
  1212. AllowedPorts: acl.Port,
  1213. Direction: acl.AllowedDirection,
  1214. Allowed: true,
  1215. }
  1216. // Get peers in the tags and add allowed rules
  1217. if userNode.StaticNode.Address != "" {
  1218. r.IPList = append(r.IPList, userNode.StaticNode.AddressIPNet4())
  1219. }
  1220. if userNode.StaticNode.Address6 != "" {
  1221. r.IP6List = append(r.IP6List, userNode.StaticNode.AddressIPNet6())
  1222. }
  1223. if aclRule, ok := rules[acl.ID]; ok {
  1224. aclRule.IPList = append(aclRule.IPList, r.IPList...)
  1225. aclRule.IP6List = append(aclRule.IP6List, r.IP6List...)
  1226. aclRule.IPList = logic.UniqueIPNetList(aclRule.IPList)
  1227. aclRule.IP6List = logic.UniqueIPNetList(aclRule.IP6List)
  1228. rules[acl.ID] = aclRule
  1229. } else {
  1230. r.IPList = logic.UniqueIPNetList(r.IPList)
  1231. r.IP6List = logic.UniqueIPNetList(r.IP6List)
  1232. rules[acl.ID] = r
  1233. }
  1234. }
  1235. }
  1236. }
  1237. return rules
  1238. }
  1239. func CheckIfAnyActiveEgressPolicy(targetNode models.Node, acls []models.Acl) bool {
  1240. if !targetNode.EgressDetails.IsEgressGateway {
  1241. return false
  1242. }
  1243. var targetNodeTags = make(map[models.TagID]struct{})
  1244. if targetNode.Mutex != nil {
  1245. targetNode.Mutex.Lock()
  1246. targetNodeTags = maps.Clone(targetNode.Tags)
  1247. targetNode.Mutex.Unlock()
  1248. } else {
  1249. targetNodeTags = maps.Clone(targetNode.Tags)
  1250. }
  1251. if targetNodeTags == nil {
  1252. targetNodeTags = make(map[models.TagID]struct{})
  1253. }
  1254. targetNodeTags[models.TagID(targetNode.ID.String())] = struct{}{}
  1255. targetNodeTags["*"] = struct{}{}
  1256. for _, acl := range acls {
  1257. if !acl.Enabled {
  1258. continue
  1259. }
  1260. srcTags := logic.ConvAclTagToValueMap(acl.Src)
  1261. for _, dst := range acl.Dst {
  1262. if dst.ID == models.EgressID {
  1263. e := schema.Egress{ID: dst.Value}
  1264. err := e.Get(db.WithContext(context.TODO()))
  1265. if err == nil && e.Status {
  1266. for nodeTag := range targetNodeTags {
  1267. if _, ok := srcTags[nodeTag.String()]; ok {
  1268. return true
  1269. }
  1270. if _, ok := srcTags[targetNode.ID.String()]; ok {
  1271. return true
  1272. }
  1273. }
  1274. }
  1275. }
  1276. }
  1277. }
  1278. return false
  1279. }
  1280. func CheckIfAnyPolicyisUniDirectional(targetNode models.Node, acls []models.Acl) bool {
  1281. var targetNodeTags = make(map[models.TagID]struct{})
  1282. if targetNode.Mutex != nil {
  1283. targetNode.Mutex.Lock()
  1284. targetNodeTags = maps.Clone(targetNode.Tags)
  1285. targetNode.Mutex.Unlock()
  1286. } else {
  1287. targetNodeTags = maps.Clone(targetNode.Tags)
  1288. }
  1289. if targetNodeTags == nil {
  1290. targetNodeTags = make(map[models.TagID]struct{})
  1291. }
  1292. targetNodeTags[models.TagID(targetNode.ID.String())] = struct{}{}
  1293. targetNodeTags["*"] = struct{}{}
  1294. for _, acl := range acls {
  1295. if !acl.Enabled {
  1296. continue
  1297. }
  1298. if acl.AllowedDirection == models.TrafficDirectionBi && acl.Proto == models.ALL && acl.ServiceType == models.Any {
  1299. continue
  1300. }
  1301. if acl.Proto != models.ALL || acl.ServiceType != models.Any {
  1302. return true
  1303. }
  1304. srcTags := logic.ConvAclTagToValueMap(acl.Src)
  1305. dstTags := logic.ConvAclTagToValueMap(acl.Dst)
  1306. for nodeTag := range targetNodeTags {
  1307. if acl.RuleType == models.DevicePolicy {
  1308. if _, ok := srcTags[nodeTag.String()]; ok {
  1309. return true
  1310. }
  1311. if _, ok := srcTags[targetNode.ID.String()]; ok {
  1312. return true
  1313. }
  1314. }
  1315. if _, ok := dstTags[nodeTag.String()]; ok {
  1316. return true
  1317. }
  1318. if _, ok := dstTags[targetNode.ID.String()]; ok {
  1319. return true
  1320. }
  1321. }
  1322. }
  1323. return false
  1324. }
  1325. func GetAclRulesForNode(targetnodeI *models.Node) (rules map[string]models.AclRule) {
  1326. targetnode := *targetnodeI
  1327. defer func() {
  1328. //if !targetnode.IsIngressGateway {
  1329. rules = getUserAclRulesForNode(&targetnode, rules)
  1330. //}
  1331. }()
  1332. rules = make(map[string]models.AclRule)
  1333. var taggedNodes map[models.TagID][]models.Node
  1334. if targetnode.IsIngressGateway {
  1335. taggedNodes = GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), false)
  1336. } else {
  1337. taggedNodes = GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), true)
  1338. }
  1339. acls := logic.ListDevicePolicies(models.NetworkID(targetnode.Network))
  1340. var targetNodeTags = make(map[models.TagID]struct{})
  1341. if targetnode.Mutex != nil {
  1342. targetnode.Mutex.Lock()
  1343. targetNodeTags = maps.Clone(targetnode.Tags)
  1344. targetnode.Mutex.Unlock()
  1345. } else {
  1346. targetNodeTags = maps.Clone(targetnode.Tags)
  1347. }
  1348. if targetNodeTags == nil {
  1349. targetNodeTags = make(map[models.TagID]struct{})
  1350. }
  1351. targetNodeTags[models.TagID(targetnode.ID.String())] = struct{}{}
  1352. targetNodeTags["*"] = struct{}{}
  1353. for _, acl := range acls {
  1354. if !acl.Enabled {
  1355. continue
  1356. }
  1357. srcTags := logic.ConvAclTagToValueMap(acl.Src)
  1358. dstTags := logic.ConvAclTagToValueMap(acl.Dst)
  1359. for _, dst := range acl.Dst {
  1360. if dst.ID == models.EgressID {
  1361. e := schema.Egress{ID: dst.Value}
  1362. err := e.Get(db.WithContext(context.TODO()))
  1363. if err == nil && e.Status {
  1364. for nodeID := range e.Nodes {
  1365. dstTags[nodeID] = struct{}{}
  1366. }
  1367. }
  1368. }
  1369. }
  1370. _, srcAll := srcTags["*"]
  1371. _, dstAll := dstTags["*"]
  1372. aclRule := models.AclRule{
  1373. ID: acl.ID,
  1374. AllowedProtocol: acl.Proto,
  1375. AllowedPorts: acl.Port,
  1376. Direction: acl.AllowedDirection,
  1377. Allowed: true,
  1378. }
  1379. for nodeTag := range targetNodeTags {
  1380. if acl.AllowedDirection == models.TrafficDirectionBi {
  1381. var existsInSrcTag bool
  1382. var existsInDstTag bool
  1383. if _, ok := srcTags[nodeTag.String()]; ok || srcAll {
  1384. existsInSrcTag = true
  1385. }
  1386. if _, ok := srcTags[targetnode.ID.String()]; ok || srcAll {
  1387. existsInSrcTag = true
  1388. }
  1389. if _, ok := dstTags[nodeTag.String()]; ok || dstAll {
  1390. existsInDstTag = true
  1391. }
  1392. if _, ok := dstTags[targetnode.ID.String()]; ok || dstAll {
  1393. existsInDstTag = true
  1394. }
  1395. if existsInSrcTag /* && !existsInDstTag*/ {
  1396. // get all dst tags
  1397. for dst := range dstTags {
  1398. if dst == nodeTag.String() {
  1399. continue
  1400. }
  1401. // Get peers in the tags and add allowed rules
  1402. nodes := taggedNodes[models.TagID(dst)]
  1403. if dst != targetnode.ID.String() {
  1404. node, err := logic.GetNodeByID(dst)
  1405. if err == nil {
  1406. nodes = append(nodes, node)
  1407. }
  1408. }
  1409. for _, node := range nodes {
  1410. if node.ID == targetnode.ID {
  1411. continue
  1412. }
  1413. if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  1414. continue
  1415. }
  1416. if node.Address.IP != nil {
  1417. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1418. }
  1419. if node.Address6.IP != nil {
  1420. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1421. }
  1422. if node.IsStatic && node.StaticNode.Address != "" {
  1423. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1424. }
  1425. if node.IsStatic && node.StaticNode.Address6 != "" {
  1426. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1427. }
  1428. }
  1429. }
  1430. }
  1431. if existsInDstTag /*&& !existsInSrcTag*/ {
  1432. // get all src tags
  1433. for src := range srcTags {
  1434. if src == nodeTag.String() {
  1435. continue
  1436. }
  1437. // Get peers in the tags and add allowed rules
  1438. nodes := taggedNodes[models.TagID(src)]
  1439. if src != targetnode.ID.String() {
  1440. node, err := logic.GetNodeByID(src)
  1441. if err == nil {
  1442. nodes = append(nodes, node)
  1443. }
  1444. }
  1445. for _, node := range nodes {
  1446. if node.ID == targetnode.ID {
  1447. continue
  1448. }
  1449. if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  1450. continue
  1451. }
  1452. if node.Address.IP != nil {
  1453. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1454. }
  1455. if node.Address6.IP != nil {
  1456. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1457. }
  1458. if node.IsStatic && node.StaticNode.Address != "" {
  1459. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1460. }
  1461. if node.IsStatic && node.StaticNode.Address6 != "" {
  1462. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1463. }
  1464. }
  1465. }
  1466. }
  1467. // if existsInDstTag && existsInSrcTag {
  1468. // nodes := taggedNodes[nodeTag]
  1469. // for srcID := range srcTags {
  1470. // if srcID == targetnode.ID.String() {
  1471. // continue
  1472. // }
  1473. // node, err := GetNodeByID(srcID)
  1474. // if err == nil {
  1475. // nodes = append(nodes, node)
  1476. // }
  1477. // }
  1478. // for dstID := range dstTags {
  1479. // if dstID == targetnode.ID.String() {
  1480. // continue
  1481. // }
  1482. // node, err := GetNodeByID(dstID)
  1483. // if err == nil {
  1484. // nodes = append(nodes, node)
  1485. // }
  1486. // }
  1487. // for _, node := range nodes {
  1488. // if node.ID == targetnode.ID {
  1489. // continue
  1490. // }
  1491. // if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  1492. // continue
  1493. // }
  1494. // if node.Address.IP != nil {
  1495. // aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1496. // }
  1497. // if node.Address6.IP != nil {
  1498. // aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1499. // }
  1500. // if node.IsStatic && node.StaticNode.Address != "" {
  1501. // aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1502. // }
  1503. // if node.IsStatic && node.StaticNode.Address6 != "" {
  1504. // aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1505. // }
  1506. // }
  1507. // }
  1508. } else {
  1509. _, all := dstTags["*"]
  1510. if _, ok := dstTags[nodeTag.String()]; ok || all {
  1511. // get all src tags
  1512. for src := range srcTags {
  1513. if src == nodeTag.String() {
  1514. continue
  1515. }
  1516. // Get peers in the tags and add allowed rules
  1517. nodes := taggedNodes[models.TagID(src)]
  1518. for _, node := range nodes {
  1519. if node.ID == targetnode.ID {
  1520. continue
  1521. }
  1522. if node.IsStatic && node.StaticNode.IngressGatewayID == targetnode.ID.String() {
  1523. continue
  1524. }
  1525. if node.Address.IP != nil {
  1526. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1527. }
  1528. if node.Address6.IP != nil {
  1529. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1530. }
  1531. if node.IsStatic && node.StaticNode.Address != "" {
  1532. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1533. }
  1534. if node.IsStatic && node.StaticNode.Address6 != "" {
  1535. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1536. }
  1537. }
  1538. }
  1539. }
  1540. }
  1541. }
  1542. if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {
  1543. aclRule.IPList = logic.UniqueIPNetList(aclRule.IPList)
  1544. aclRule.IP6List = logic.UniqueIPNetList(aclRule.IP6List)
  1545. rules[acl.ID] = aclRule
  1546. }
  1547. }
  1548. return rules
  1549. }
  1550. func GetAclRuleForInetGw(targetnode models.Node) (rules map[string]models.AclRule) {
  1551. rules = make(map[string]models.AclRule)
  1552. if targetnode.IsInternetGateway {
  1553. aclRule := models.AclRule{
  1554. ID: fmt.Sprintf("%s-inet-gw-internal-rule", targetnode.ID.String()),
  1555. AllowedProtocol: models.ALL,
  1556. AllowedPorts: []string{},
  1557. Direction: models.TrafficDirectionBi,
  1558. Allowed: true,
  1559. }
  1560. if targetnode.NetworkRange.IP != nil {
  1561. aclRule.IPList = append(aclRule.IPList, targetnode.NetworkRange)
  1562. _, allIpv4, _ := net.ParseCIDR(logic.IPv4Network)
  1563. aclRule.Dst = append(aclRule.Dst, *allIpv4)
  1564. }
  1565. if targetnode.NetworkRange6.IP != nil {
  1566. aclRule.IP6List = append(aclRule.IP6List, targetnode.NetworkRange6)
  1567. _, allIpv6, _ := net.ParseCIDR(logic.IPv6Network)
  1568. aclRule.Dst6 = append(aclRule.Dst6, *allIpv6)
  1569. }
  1570. rules[aclRule.ID] = aclRule
  1571. }
  1572. return
  1573. }
  1574. func GetEgressRulesForNode(targetnode models.Node) (rules map[string]models.AclRule) {
  1575. rules = make(map[string]models.AclRule)
  1576. defer func() {
  1577. rules = getEgressUserRulesForNode(&targetnode, rules)
  1578. }()
  1579. taggedNodes := GetTagMapWithNodesByNetwork(models.NetworkID(targetnode.Network), true)
  1580. acls := logic.ListDevicePolicies(models.NetworkID(targetnode.Network))
  1581. var targetNodeTags = make(map[models.TagID]struct{})
  1582. targetNodeTags["*"] = struct{}{}
  1583. /*
  1584. if target node is egress gateway
  1585. if acl policy has egress route and it is present in target node egress ranges
  1586. fetch all the nodes in that policy and add rules
  1587. */
  1588. egs, _ := (&schema.Egress{Network: targetnode.Network}).ListByNetwork(db.WithContext(context.TODO()))
  1589. if len(egs) == 0 {
  1590. return
  1591. }
  1592. for _, egI := range egs {
  1593. if !egI.Status {
  1594. continue
  1595. }
  1596. if _, ok := egI.Nodes[targetnode.ID.String()]; ok {
  1597. if egI.Range != "" {
  1598. targetNodeTags[models.TagID(egI.Range)] = struct{}{}
  1599. } else if len(egI.DomainAns) > 0 {
  1600. for _, domainAnsI := range egI.DomainAns {
  1601. targetNodeTags[models.TagID(domainAnsI)] = struct{}{}
  1602. }
  1603. }
  1604. targetNodeTags[models.TagID(egI.ID)] = struct{}{}
  1605. }
  1606. }
  1607. for _, acl := range acls {
  1608. if !acl.Enabled {
  1609. continue
  1610. }
  1611. srcTags := logic.ConvAclTagToValueMap(acl.Src)
  1612. dstTags := logic.ConvAclTagToValueMap(acl.Dst)
  1613. _, srcAll := srcTags["*"]
  1614. _, dstAll := dstTags["*"]
  1615. aclRule := models.AclRule{
  1616. ID: acl.ID,
  1617. AllowedProtocol: acl.Proto,
  1618. AllowedPorts: acl.Port,
  1619. Direction: acl.AllowedDirection,
  1620. Allowed: true,
  1621. }
  1622. for nodeTag := range targetNodeTags {
  1623. if nodeTag != "*" {
  1624. ip, cidr, err := net.ParseCIDR(nodeTag.String())
  1625. if err == nil {
  1626. if ip.To4() != nil {
  1627. aclRule.Dst = append(aclRule.Dst, *cidr)
  1628. } else {
  1629. aclRule.Dst6 = append(aclRule.Dst6, *cidr)
  1630. }
  1631. }
  1632. }
  1633. if acl.AllowedDirection == models.TrafficDirectionBi {
  1634. var existsInSrcTag bool
  1635. var existsInDstTag bool
  1636. if _, ok := srcTags[nodeTag.String()]; ok || srcAll {
  1637. existsInSrcTag = true
  1638. }
  1639. if _, ok := dstTags[nodeTag.String()]; ok || dstAll {
  1640. existsInDstTag = true
  1641. }
  1642. // if srcAll || dstAll {
  1643. // if targetnode.NetworkRange.IP != nil {
  1644. // aclRule.IPList = append(aclRule.IPList, targetnode.NetworkRange)
  1645. // }
  1646. // if targetnode.NetworkRange6.IP != nil {
  1647. // aclRule.IP6List = append(aclRule.IP6List, targetnode.NetworkRange6)
  1648. // }
  1649. // break
  1650. // }
  1651. if existsInSrcTag && !existsInDstTag {
  1652. // get all dst tags
  1653. for dst := range dstTags {
  1654. if dst == nodeTag.String() {
  1655. continue
  1656. }
  1657. // Get peers in the tags and add allowed rules
  1658. nodes := taggedNodes[models.TagID(dst)]
  1659. if dst != targetnode.ID.String() {
  1660. node, err := logic.GetNodeByID(dst)
  1661. if err == nil {
  1662. nodes = append(nodes, node)
  1663. }
  1664. extclient, err := logic.GetExtClient(dst, targetnode.Network)
  1665. if err == nil {
  1666. nodes = append(nodes, extclient.ConvertToStaticNode())
  1667. }
  1668. }
  1669. for _, node := range nodes {
  1670. if node.ID == targetnode.ID {
  1671. continue
  1672. }
  1673. if node.Address.IP != nil {
  1674. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1675. }
  1676. if node.Address6.IP != nil {
  1677. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1678. }
  1679. if node.IsStatic && node.StaticNode.Address != "" {
  1680. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1681. }
  1682. if node.IsStatic && node.StaticNode.Address6 != "" {
  1683. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1684. }
  1685. }
  1686. }
  1687. }
  1688. if existsInDstTag && !existsInSrcTag {
  1689. // get all src tags
  1690. for src := range srcTags {
  1691. if src == nodeTag.String() {
  1692. continue
  1693. }
  1694. // Get peers in the tags and add allowed rules
  1695. nodes := taggedNodes[models.TagID(src)]
  1696. if src != targetnode.ID.String() {
  1697. node, err := logic.GetNodeByID(src)
  1698. if err == nil {
  1699. nodes = append(nodes, node)
  1700. }
  1701. extclient, err := logic.GetExtClient(src, targetnode.Network)
  1702. if err == nil {
  1703. nodes = append(nodes, extclient.ConvertToStaticNode())
  1704. }
  1705. }
  1706. for _, node := range nodes {
  1707. if node.ID == targetnode.ID {
  1708. continue
  1709. }
  1710. if node.Address.IP != nil {
  1711. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1712. }
  1713. if node.Address6.IP != nil {
  1714. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1715. }
  1716. if node.IsStatic && node.StaticNode.Address != "" {
  1717. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1718. }
  1719. if node.IsStatic && node.StaticNode.Address6 != "" {
  1720. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1721. }
  1722. }
  1723. }
  1724. }
  1725. if existsInDstTag && existsInSrcTag {
  1726. nodes := taggedNodes[nodeTag]
  1727. for srcID := range srcTags {
  1728. if srcID == targetnode.ID.String() {
  1729. continue
  1730. }
  1731. node, err := logic.GetNodeByID(srcID)
  1732. if err == nil {
  1733. nodes = append(nodes, node)
  1734. }
  1735. extclient, err := logic.GetExtClient(srcID, targetnode.Network)
  1736. if err == nil {
  1737. nodes = append(nodes, extclient.ConvertToStaticNode())
  1738. }
  1739. }
  1740. for dstID := range dstTags {
  1741. if dstID == targetnode.ID.String() {
  1742. continue
  1743. }
  1744. node, err := logic.GetNodeByID(dstID)
  1745. if err == nil {
  1746. nodes = append(nodes, node)
  1747. }
  1748. extclient, err := logic.GetExtClient(dstID, targetnode.Network)
  1749. if err == nil {
  1750. nodes = append(nodes, extclient.ConvertToStaticNode())
  1751. }
  1752. }
  1753. for _, node := range nodes {
  1754. if node.ID == targetnode.ID {
  1755. continue
  1756. }
  1757. if node.Address.IP != nil {
  1758. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1759. }
  1760. if node.Address6.IP != nil {
  1761. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1762. }
  1763. if node.IsStatic && node.StaticNode.Address != "" {
  1764. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1765. }
  1766. if node.IsStatic && node.StaticNode.Address6 != "" {
  1767. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1768. }
  1769. }
  1770. }
  1771. } else {
  1772. if dstAll {
  1773. if targetnode.NetworkRange.IP != nil {
  1774. aclRule.IPList = append(aclRule.IPList, targetnode.NetworkRange)
  1775. }
  1776. if targetnode.NetworkRange6.IP != nil {
  1777. aclRule.IP6List = append(aclRule.IP6List, targetnode.NetworkRange6)
  1778. }
  1779. break
  1780. }
  1781. if _, ok := dstTags[nodeTag.String()]; ok || dstAll {
  1782. // get all src tags
  1783. for src := range srcTags {
  1784. if src == nodeTag.String() {
  1785. continue
  1786. }
  1787. // Get peers in the tags and add allowed rules
  1788. nodes := taggedNodes[models.TagID(src)]
  1789. for _, node := range nodes {
  1790. if node.ID == targetnode.ID {
  1791. continue
  1792. }
  1793. if node.Address.IP != nil {
  1794. aclRule.IPList = append(aclRule.IPList, node.AddressIPNet4())
  1795. }
  1796. if node.Address6.IP != nil {
  1797. aclRule.IP6List = append(aclRule.IP6List, node.AddressIPNet6())
  1798. }
  1799. if node.IsStatic && node.StaticNode.Address != "" {
  1800. aclRule.IPList = append(aclRule.IPList, node.StaticNode.AddressIPNet4())
  1801. }
  1802. if node.IsStatic && node.StaticNode.Address6 != "" {
  1803. aclRule.IP6List = append(aclRule.IP6List, node.StaticNode.AddressIPNet6())
  1804. }
  1805. }
  1806. }
  1807. }
  1808. }
  1809. }
  1810. if len(aclRule.IPList) > 0 || len(aclRule.IP6List) > 0 {
  1811. aclRule.IPList = logic.UniqueIPNetList(aclRule.IPList)
  1812. aclRule.IP6List = logic.UniqueIPNetList(aclRule.IP6List)
  1813. rules[acl.ID] = aclRule
  1814. }
  1815. }
  1816. return
  1817. }
  1818. func GetInetClientsFromAclPolicies(eID string) (inetClientIDs []string) {
  1819. e := schema.Egress{ID: eID}
  1820. err := e.Get(db.WithContext(context.TODO()))
  1821. if err != nil || !e.Status {
  1822. return
  1823. }
  1824. acls, _ := logic.ListAclsByNetwork(models.NetworkID(e.Network))
  1825. for _, acl := range acls {
  1826. for _, dstI := range acl.Dst {
  1827. if dstI.ID == models.EgressID {
  1828. if dstI.Value != eID {
  1829. continue
  1830. }
  1831. for _, srcI := range acl.Src {
  1832. if srcI.Value == "*" {
  1833. continue
  1834. }
  1835. if srcI.ID == models.NodeID {
  1836. inetClientIDs = append(inetClientIDs, srcI.Value)
  1837. }
  1838. if srcI.ID == models.NodeTagID {
  1839. inetClientIDs = append(inetClientIDs, GetNodeIDsWithTag(models.TagID(srcI.Value))...)
  1840. }
  1841. }
  1842. }
  1843. }
  1844. }
  1845. return
  1846. }