networks.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. package logic
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "os/exec"
  8. "strings"
  9. "github.com/c-robinson/iplib"
  10. validator "github.com/go-playground/validator/v10"
  11. "github.com/gravitl/netmaker/database"
  12. "github.com/gravitl/netmaker/logger"
  13. "github.com/gravitl/netmaker/logic/acls/nodeacls"
  14. "github.com/gravitl/netmaker/logic/pro"
  15. "github.com/gravitl/netmaker/models"
  16. "github.com/gravitl/netmaker/netclient/ncutils"
  17. "github.com/gravitl/netmaker/validation"
  18. )
  19. // GetNetworks - returns all networks from database
  20. func GetNetworks() ([]models.Network, error) {
  21. var networks []models.Network
  22. collection, err := database.FetchRecords(database.NETWORKS_TABLE_NAME)
  23. if err != nil {
  24. return networks, err
  25. }
  26. for _, value := range collection {
  27. var network models.Network
  28. if err := json.Unmarshal([]byte(value), &network); err != nil {
  29. return networks, err
  30. }
  31. // add network our array
  32. networks = append(networks, network)
  33. }
  34. return networks, err
  35. }
  36. // DeleteNetwork - deletes a network
  37. func DeleteNetwork(network string) error {
  38. // remove ACL for network
  39. err := nodeacls.DeleteACLContainer(nodeacls.NetworkID(network))
  40. if err != nil {
  41. logger.Log(1, "failed to remove the node acls during network delete for network,", network)
  42. }
  43. nodeCount, err := GetNetworkNonServerNodeCount(network)
  44. if nodeCount == 0 || database.IsEmptyRecord(err) {
  45. // delete server nodes first then db records
  46. servers, err := GetSortedNetworkServerNodes(network)
  47. if err == nil {
  48. for _, s := range servers {
  49. if err = DeleteNode(&s, true); err != nil {
  50. logger.Log(2, "could not removed server", s.Name, "before deleting network", network)
  51. } else {
  52. logger.Log(2, "removed server", s.Name, "before deleting network", network)
  53. }
  54. }
  55. } else {
  56. logger.Log(1, "could not remove servers before deleting network", network)
  57. }
  58. if err = pro.RemoveAllNetworkUsers(network); err != nil {
  59. logger.Log(0, "failed to remove network users on network delete for network", network, err.Error())
  60. }
  61. return database.DeleteRecord(database.NETWORKS_TABLE_NAME, network)
  62. }
  63. return errors.New("node check failed. All nodes must be deleted before deleting network")
  64. }
  65. // CreateNetwork - creates a network in database
  66. func CreateNetwork(network models.Network) (models.Network, error) {
  67. if network.AddressRange != "" {
  68. normalizedRange, err := NormalizeCIDR(network.AddressRange)
  69. if err != nil {
  70. return models.Network{}, err
  71. }
  72. network.AddressRange = normalizedRange
  73. }
  74. if network.AddressRange6 != "" {
  75. normalizedRange, err := NormalizeCIDR(network.AddressRange6)
  76. if err != nil {
  77. return models.Network{}, err
  78. }
  79. network.AddressRange6 = normalizedRange
  80. }
  81. network.SetDefaults()
  82. network.SetNodesLastModified()
  83. network.SetNetworkLastModified()
  84. pro.AddProNetDefaults(&network)
  85. if len(network.ProSettings.AllowedGroups) == 0 {
  86. network.ProSettings.AllowedGroups = []string{pro.DEFAULT_ALLOWED_GROUPS}
  87. }
  88. err := ValidateNetwork(&network, false)
  89. if err != nil {
  90. //logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
  91. return models.Network{}, err
  92. }
  93. if err = pro.InitializeNetworkUsers(network.NetID); err != nil {
  94. return models.Network{}, err
  95. }
  96. data, err := json.Marshal(&network)
  97. if err != nil {
  98. return models.Network{}, err
  99. }
  100. if err = database.Insert(network.NetID, string(data), database.NETWORKS_TABLE_NAME); err != nil {
  101. return models.Network{}, err
  102. }
  103. // == add all current users to network as network users ==
  104. if err = InitializeNetUsers(&network); err != nil {
  105. return network, err
  106. }
  107. return network, nil
  108. }
  109. // GetNetworkNonServerNodeCount - get number of network non server nodes
  110. func GetNetworkNonServerNodeCount(networkName string) (int, error) {
  111. collection, err := database.FetchRecords(database.NODES_TABLE_NAME)
  112. count := 0
  113. if err != nil && !database.IsEmptyRecord(err) {
  114. return count, err
  115. }
  116. for _, value := range collection {
  117. var node models.Node
  118. if err = json.Unmarshal([]byte(value), &node); err != nil {
  119. return count, err
  120. } else {
  121. if node.Network == networkName && node.IsServer != "yes" {
  122. count++
  123. }
  124. }
  125. }
  126. return count, nil
  127. }
  128. // GetParentNetwork - get parent network
  129. func GetParentNetwork(networkname string) (models.Network, error) {
  130. var network models.Network
  131. networkData, err := database.FetchRecord(database.NETWORKS_TABLE_NAME, networkname)
  132. if err != nil {
  133. return network, err
  134. }
  135. if err = json.Unmarshal([]byte(networkData), &network); err != nil {
  136. return models.Network{}, err
  137. }
  138. return network, nil
  139. }
  140. // GetParentNetwork - get parent network
  141. func GetNetworkSettings(networkname string) (models.Network, error) {
  142. var network models.Network
  143. networkData, err := database.FetchRecord(database.NETWORKS_TABLE_NAME, networkname)
  144. if err != nil {
  145. return network, err
  146. }
  147. if err = json.Unmarshal([]byte(networkData), &network); err != nil {
  148. return models.Network{}, err
  149. }
  150. network.AccessKeys = []models.AccessKey{}
  151. return network, nil
  152. }
  153. // UniqueAddress - see if address is unique
  154. func UniqueAddress(networkName string, reverse bool) (string, error) {
  155. var network models.Network
  156. network, err := GetParentNetwork(networkName)
  157. if err != nil {
  158. logger.Log(0, "UniqueAddressServer encountered an error")
  159. return "666", err
  160. }
  161. if network.IsIPv4 == "no" {
  162. return "", fmt.Errorf("IPv4 not active on network " + networkName)
  163. }
  164. //ensure AddressRange is valid
  165. if _, _, err := net.ParseCIDR(network.AddressRange); err != nil {
  166. logger.Log(0, "UniqueAddress encountered an error")
  167. return "666", err
  168. }
  169. net4 := iplib.Net4FromStr(network.AddressRange)
  170. newAddrs := net4.FirstAddress()
  171. if reverse {
  172. newAddrs = net4.LastAddress()
  173. }
  174. for {
  175. if IsIPUnique(networkName, newAddrs.String(), database.NODES_TABLE_NAME, false) &&
  176. IsIPUnique(networkName, newAddrs.String(), database.EXT_CLIENT_TABLE_NAME, false) {
  177. return newAddrs.String(), nil
  178. }
  179. if reverse {
  180. newAddrs, err = net4.PreviousIP(newAddrs)
  181. } else {
  182. newAddrs, err = net4.NextIP(newAddrs)
  183. }
  184. if err != nil {
  185. break
  186. }
  187. }
  188. return "W1R3: NO UNIQUE ADDRESSES AVAILABLE", errors.New("ERROR: No unique addresses available. Check network subnet")
  189. }
  190. // IsIPUnique - checks if an IP is unique
  191. func IsIPUnique(network string, ip string, tableName string, isIpv6 bool) bool {
  192. isunique := true
  193. collection, err := database.FetchRecords(tableName)
  194. if err != nil {
  195. return isunique
  196. }
  197. for _, value := range collection { // filter
  198. var node models.Node
  199. if err = json.Unmarshal([]byte(value), &node); err != nil {
  200. continue
  201. }
  202. if isIpv6 {
  203. if node.Address6 == ip && node.Network == network {
  204. return false
  205. }
  206. } else {
  207. if node.Address == ip && node.Network == network {
  208. return false
  209. }
  210. }
  211. }
  212. return isunique
  213. }
  214. // UniqueAddress6 - see if ipv6 address is unique
  215. func UniqueAddress6(networkName string, reverse bool) (string, error) {
  216. var network models.Network
  217. network, err := GetParentNetwork(networkName)
  218. if err != nil {
  219. fmt.Println("Network Not Found")
  220. return "", err
  221. }
  222. if network.IsIPv6 == "no" {
  223. return "", fmt.Errorf("IPv6 not active on network " + networkName)
  224. }
  225. //ensure AddressRange is valid
  226. if _, _, err := net.ParseCIDR(network.AddressRange6); err != nil {
  227. return "666", err
  228. }
  229. net6 := iplib.Net6FromStr(network.AddressRange6)
  230. newAddrs, err := net6.NextIP(net6.FirstAddress())
  231. if reverse {
  232. newAddrs, err = net6.PreviousIP(net6.LastAddress())
  233. }
  234. if err != nil {
  235. return "", err
  236. }
  237. for {
  238. if IsIPUnique(networkName, newAddrs.String(), database.NODES_TABLE_NAME, true) &&
  239. IsIPUnique(networkName, newAddrs.String(), database.EXT_CLIENT_TABLE_NAME, true) {
  240. return newAddrs.String(), nil
  241. }
  242. if reverse {
  243. newAddrs, err = net6.PreviousIP(newAddrs)
  244. } else {
  245. newAddrs, err = net6.NextIP(newAddrs)
  246. }
  247. if err != nil {
  248. break
  249. }
  250. }
  251. return "W1R3: NO UNIQUE ADDRESSES AVAILABLE", errors.New("ERROR: No unique IPv6 addresses available. Check network subnet")
  252. }
  253. // GetLocalIP - gets the local ip
  254. func GetLocalIP(node models.Node) string {
  255. var local string
  256. ifaces, err := net.Interfaces()
  257. if err != nil {
  258. return local
  259. }
  260. _, localrange, err := net.ParseCIDR(node.LocalRange)
  261. if err != nil {
  262. return local
  263. }
  264. found := false
  265. for _, i := range ifaces {
  266. if i.Flags&net.FlagUp == 0 {
  267. continue // interface down
  268. }
  269. if i.Flags&net.FlagLoopback != 0 {
  270. continue // loopback interface
  271. }
  272. addrs, err := i.Addrs()
  273. if err != nil {
  274. return local
  275. }
  276. for _, addr := range addrs {
  277. var ip net.IP
  278. switch v := addr.(type) {
  279. case *net.IPNet:
  280. if !found {
  281. ip = v.IP
  282. local = ip.String()
  283. if node.IsLocal == "yes" {
  284. found = localrange.Contains(ip)
  285. } else {
  286. found = true
  287. }
  288. }
  289. case *net.IPAddr:
  290. if !found {
  291. ip = v.IP
  292. local = ip.String()
  293. if node.IsLocal == "yes" {
  294. found = localrange.Contains(ip)
  295. } else {
  296. found = true
  297. }
  298. }
  299. }
  300. }
  301. }
  302. return local
  303. }
  304. // UpdateNetworkLocalAddresses - updates network localaddresses
  305. func UpdateNetworkLocalAddresses(networkName string) error {
  306. collection, err := database.FetchRecords(database.NODES_TABLE_NAME)
  307. if err != nil {
  308. return err
  309. }
  310. for _, value := range collection {
  311. var node models.Node
  312. err := json.Unmarshal([]byte(value), &node)
  313. if err != nil {
  314. fmt.Println("error in node address assignment!")
  315. return err
  316. }
  317. if node.Network == networkName {
  318. var ipaddr string
  319. var iperr error
  320. if node.IsServer == "yes" {
  321. ipaddr, iperr = UniqueAddress(networkName, true)
  322. } else {
  323. ipaddr, iperr = UniqueAddress(networkName, false)
  324. }
  325. if iperr != nil {
  326. fmt.Println("error in node address assignment!")
  327. return iperr
  328. }
  329. node.Address = ipaddr
  330. newNodeData, err := json.Marshal(&node)
  331. if err != nil {
  332. logger.Log(1, "error in node address assignment!")
  333. return err
  334. }
  335. database.Insert(node.ID, string(newNodeData), database.NODES_TABLE_NAME)
  336. }
  337. }
  338. return nil
  339. }
  340. // UpdateNetworkLocalAddresses - updates network localaddresses
  341. func UpdateNetworkHolePunching(networkName string, holepunch string) error {
  342. nodes, err := GetNetworkNodes(networkName)
  343. if err != nil {
  344. return err
  345. }
  346. for _, node := range nodes {
  347. if node.IsServer != "yes" {
  348. node.UDPHolePunch = holepunch
  349. newNodeData, err := json.Marshal(&node)
  350. if err != nil {
  351. logger.Log(1, "error in node hole punch assignment")
  352. return err
  353. }
  354. database.Insert(node.ID, string(newNodeData), database.NODES_TABLE_NAME)
  355. }
  356. }
  357. return nil
  358. }
  359. // RemoveNetworkNodeIPv6Addresses - removes network node IPv6 addresses
  360. func RemoveNetworkNodeIPv6Addresses(networkName string) error {
  361. collections, err := database.FetchRecords(database.NODES_TABLE_NAME)
  362. if err != nil {
  363. return err
  364. }
  365. for _, value := range collections {
  366. var node models.Node
  367. err := json.Unmarshal([]byte(value), &node)
  368. if err != nil {
  369. fmt.Println("error in node address assignment!")
  370. return err
  371. }
  372. if node.Network == networkName {
  373. node.Address6 = ""
  374. data, err := json.Marshal(&node)
  375. if err != nil {
  376. return err
  377. }
  378. database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  379. }
  380. }
  381. return nil
  382. }
  383. // UpdateNetworkNodeAddresses - updates network node addresses
  384. func UpdateNetworkNodeAddresses(networkName string) error {
  385. collections, err := database.FetchRecords(database.NODES_TABLE_NAME)
  386. if err != nil {
  387. return err
  388. }
  389. for _, value := range collections {
  390. var node models.Node
  391. err := json.Unmarshal([]byte(value), &node)
  392. if err != nil {
  393. logger.Log(1, "error in node ipv4 address assignment!")
  394. return err
  395. }
  396. if node.Network == networkName {
  397. var ipaddr string
  398. var iperr error
  399. if node.IsServer == "yes" {
  400. ipaddr, iperr = UniqueAddress(networkName, true)
  401. } else {
  402. ipaddr, iperr = UniqueAddress(networkName, false)
  403. }
  404. if iperr != nil {
  405. logger.Log(1, "error in node ipv4 address assignment!")
  406. return iperr
  407. }
  408. node.Address = ipaddr
  409. data, err := json.Marshal(&node)
  410. if err != nil {
  411. return err
  412. }
  413. database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  414. }
  415. }
  416. return nil
  417. }
  418. // UpdateNetworkNodeAddresses6 - updates network node addresses
  419. func UpdateNetworkNodeAddresses6(networkName string) error {
  420. collections, err := database.FetchRecords(database.NODES_TABLE_NAME)
  421. if err != nil {
  422. return err
  423. }
  424. for _, value := range collections {
  425. var node models.Node
  426. err := json.Unmarshal([]byte(value), &node)
  427. if err != nil {
  428. logger.Log(1, "error in node ipv6 address assignment!")
  429. return err
  430. }
  431. if node.Network == networkName {
  432. var ipaddr string
  433. var iperr error
  434. if node.IsServer == "yes" {
  435. ipaddr, iperr = UniqueAddress6(networkName, true)
  436. } else {
  437. ipaddr, iperr = UniqueAddress6(networkName, false)
  438. }
  439. if iperr != nil {
  440. logger.Log(1, "error in node ipv6 address assignment!")
  441. return iperr
  442. }
  443. node.Address6 = ipaddr
  444. data, err := json.Marshal(&node)
  445. if err != nil {
  446. return err
  447. }
  448. database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  449. }
  450. }
  451. return nil
  452. }
  453. // IsNetworkNameUnique - checks to see if any other networks have the same name (id)
  454. func IsNetworkNameUnique(network *models.Network) (bool, error) {
  455. isunique := true
  456. dbs, err := GetNetworks()
  457. if err != nil && !database.IsEmptyRecord(err) {
  458. return false, err
  459. }
  460. for i := 0; i < len(dbs); i++ {
  461. if network.NetID == dbs[i].NetID {
  462. isunique = false
  463. }
  464. }
  465. return isunique, nil
  466. }
  467. // UpdateNetwork - updates a network with another network's fields
  468. func UpdateNetwork(currentNetwork *models.Network, newNetwork *models.Network) (bool, bool, bool, bool, []string, []string, error) {
  469. if err := ValidateNetwork(newNetwork, true); err != nil {
  470. return false, false, false, false, nil, nil, err
  471. }
  472. if newNetwork.NetID == currentNetwork.NetID {
  473. hasrangeupdate4 := newNetwork.AddressRange != currentNetwork.AddressRange
  474. hasrangeupdate6 := newNetwork.AddressRange6 != currentNetwork.AddressRange6
  475. localrangeupdate := newNetwork.LocalRange != currentNetwork.LocalRange
  476. hasholepunchupdate := newNetwork.DefaultUDPHolePunch != currentNetwork.DefaultUDPHolePunch
  477. groupDelta := append(StringDifference(newNetwork.ProSettings.AllowedGroups, currentNetwork.ProSettings.AllowedGroups),
  478. StringDifference(currentNetwork.ProSettings.AllowedGroups, newNetwork.ProSettings.AllowedGroups)...)
  479. userDelta := append(StringDifference(newNetwork.ProSettings.AllowedUsers, currentNetwork.ProSettings.AllowedUsers),
  480. StringDifference(currentNetwork.ProSettings.AllowedUsers, newNetwork.ProSettings.AllowedUsers)...)
  481. data, err := json.Marshal(newNetwork)
  482. if err != nil {
  483. return false, false, false, false, nil, nil, err
  484. }
  485. newNetwork.SetNetworkLastModified()
  486. err = database.Insert(newNetwork.NetID, string(data), database.NETWORKS_TABLE_NAME)
  487. return hasrangeupdate4, hasrangeupdate6, localrangeupdate, hasholepunchupdate, groupDelta, userDelta, err
  488. }
  489. // copy values
  490. return false, false, false, false, nil, nil, errors.New("failed to update network " + newNetwork.NetID + ", cannot change netid.")
  491. }
  492. // GetNetwork - gets a network from database
  493. func GetNetwork(networkname string) (models.Network, error) {
  494. var network models.Network
  495. networkData, err := database.FetchRecord(database.NETWORKS_TABLE_NAME, networkname)
  496. if err != nil {
  497. return network, err
  498. }
  499. if err = json.Unmarshal([]byte(networkData), &network); err != nil {
  500. return models.Network{}, err
  501. }
  502. return network, nil
  503. }
  504. // NetIDInNetworkCharSet - checks if a netid of a network uses valid characters
  505. func NetIDInNetworkCharSet(network *models.Network) bool {
  506. charset := "abcdefghijklmnopqrstuvwxyz1234567890-_"
  507. for _, char := range network.NetID {
  508. if !strings.Contains(charset, string(char)) {
  509. return false
  510. }
  511. }
  512. return true
  513. }
  514. // Validate - validates fields of an network struct
  515. func ValidateNetwork(network *models.Network, isUpdate bool) error {
  516. v := validator.New()
  517. _ = v.RegisterValidation("netid_valid", func(fl validator.FieldLevel) bool {
  518. inCharSet := NetIDInNetworkCharSet(network)
  519. if isUpdate {
  520. return inCharSet
  521. }
  522. isFieldUnique, _ := IsNetworkNameUnique(network)
  523. return isFieldUnique && inCharSet
  524. })
  525. //
  526. _ = v.RegisterValidation("checkyesorno", func(fl validator.FieldLevel) bool {
  527. return validation.CheckYesOrNo(fl)
  528. })
  529. err := v.Struct(network)
  530. if err != nil {
  531. for _, e := range err.(validator.ValidationErrors) {
  532. fmt.Println(e)
  533. }
  534. }
  535. if network.ProSettings != nil {
  536. if network.ProSettings.DefaultAccessLevel < pro.NET_ADMIN || network.ProSettings.DefaultAccessLevel > pro.NO_ACCESS {
  537. return fmt.Errorf("invalid access level")
  538. }
  539. if network.ProSettings.DefaultUserClientLimit < 0 || network.ProSettings.DefaultUserNodeLimit < 0 {
  540. return fmt.Errorf("invalid node/client limit provided")
  541. }
  542. }
  543. return err
  544. }
  545. // ParseNetwork - parses a network into a model
  546. func ParseNetwork(value string) (models.Network, error) {
  547. var network models.Network
  548. err := json.Unmarshal([]byte(value), &network)
  549. return network, err
  550. }
  551. // KeyUpdate - updates keys on network
  552. func KeyUpdate(netname string) (models.Network, error) {
  553. err := networkNodesUpdateAction(netname, models.NODE_UPDATE_KEY)
  554. if err != nil {
  555. return models.Network{}, err
  556. }
  557. return models.Network{}, nil
  558. }
  559. // SaveNetwork - save network struct to database
  560. func SaveNetwork(network *models.Network) error {
  561. data, err := json.Marshal(network)
  562. if err != nil {
  563. return err
  564. }
  565. if err := database.Insert(network.NetID, string(data), database.NETWORKS_TABLE_NAME); err != nil {
  566. return err
  567. }
  568. return nil
  569. }
  570. // NetworkExists - check if network exists
  571. func NetworkExists(name string) (bool, error) {
  572. var network string
  573. var err error
  574. if network, err = database.FetchRecord(database.NETWORKS_TABLE_NAME, name); err != nil {
  575. return false, err
  576. }
  577. return len(network) > 0, nil
  578. }
  579. // == Private ==
  580. func networkNodesUpdateAction(networkName string, action string) error {
  581. collections, err := database.FetchRecords(database.NODES_TABLE_NAME)
  582. if err != nil {
  583. if database.IsEmptyRecord(err) {
  584. return nil
  585. }
  586. return err
  587. }
  588. for _, value := range collections {
  589. var node models.Node
  590. err := json.Unmarshal([]byte(value), &node)
  591. if err != nil {
  592. fmt.Println("error in node address assignment!")
  593. return err
  594. }
  595. if action == models.NODE_UPDATE_KEY {
  596. continue
  597. }
  598. if node.Network == networkName {
  599. node.Action = action
  600. data, err := json.Marshal(&node)
  601. if err != nil {
  602. return err
  603. }
  604. database.Insert(node.ID, string(data), database.NODES_TABLE_NAME)
  605. }
  606. }
  607. return nil
  608. }
  609. func deleteInterface(ifacename string, postdown string) error {
  610. var err error
  611. if !ncutils.IsKernel() {
  612. err = RemoveConf(ifacename, true)
  613. } else {
  614. ipExec, errN := exec.LookPath("ip")
  615. err = errN
  616. if err != nil {
  617. logger.Log(1, err.Error())
  618. }
  619. _, err = ncutils.RunCmd(ipExec+" link del "+ifacename, false)
  620. if postdown != "" {
  621. _, err = ncutils.RunCmd(postdown, false)
  622. }
  623. }
  624. return err
  625. }
  626. func isInterfacePresent(iface string, address string) (string, bool) {
  627. var interfaces []net.Interface
  628. var err error
  629. interfaces, err = net.Interfaces()
  630. if err != nil {
  631. logger.Log(0, "ERROR: could not read interfaces")
  632. return "", true
  633. }
  634. for _, currIface := range interfaces {
  635. var currAddrs []net.Addr
  636. currAddrs, err = currIface.Addrs()
  637. if err != nil || len(currAddrs) == 0 {
  638. continue
  639. }
  640. for _, addr := range currAddrs {
  641. if strings.Contains(addr.String(), address) && currIface.Name != iface {
  642. // logger.Log(2, "found iface", addr.String(), currIface.Name)
  643. interfaces = nil
  644. currAddrs = nil
  645. return currIface.Name, false
  646. }
  647. }
  648. currAddrs = nil
  649. }
  650. interfaces = nil
  651. // logger.Log(2, "failed to find iface", iface)
  652. return "", true
  653. }