iface.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // +build !windows
  2. // Copyright 2015 flannel authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package ip
  16. import (
  17. "errors"
  18. "fmt"
  19. "net"
  20. "syscall"
  21. "github.com/vishvananda/netlink"
  22. )
  23. func getIfaceAddrs(iface *net.Interface) ([]netlink.Addr, error) {
  24. link := &netlink.Device{
  25. netlink.LinkAttrs{
  26. Index: iface.Index,
  27. },
  28. }
  29. return netlink.AddrList(link, syscall.AF_INET)
  30. }
  31. func GetInterfaceIP4Addr(iface *net.Interface) (net.IP, error) {
  32. addrs, err := getIfaceAddrs(iface)
  33. if err != nil {
  34. return nil, err
  35. }
  36. // prefer non link-local addr
  37. var ll net.IP
  38. for _, addr := range addrs {
  39. if addr.IP.To4() == nil {
  40. continue
  41. }
  42. if addr.IP.IsGlobalUnicast() {
  43. return addr.IP, nil
  44. }
  45. if addr.IP.IsLinkLocalUnicast() {
  46. ll = addr.IP
  47. }
  48. }
  49. if ll != nil {
  50. // didn't find global but found link-local. it'll do.
  51. return ll, nil
  52. }
  53. return nil, errors.New("No IPv4 address found for given interface")
  54. }
  55. func GetInterfaceIP4AddrMatch(iface *net.Interface, matchAddr net.IP) error {
  56. addrs, err := getIfaceAddrs(iface)
  57. if err != nil {
  58. return err
  59. }
  60. for _, addr := range addrs {
  61. // Attempt to parse the address in CIDR notation
  62. // and assert it is IPv4
  63. if addr.IP.To4() != nil {
  64. if addr.IP.To4().Equal(matchAddr) {
  65. return nil
  66. }
  67. }
  68. }
  69. return errors.New("No IPv4 address found for given interface")
  70. }
  71. func GetDefaultGatewayInterface() (*net.Interface, error) {
  72. routes, err := netlink.RouteList(nil, syscall.AF_INET)
  73. if err != nil {
  74. return nil, err
  75. }
  76. for _, route := range routes {
  77. if route.Dst == nil || route.Dst.String() == "0.0.0.0/0" {
  78. if route.LinkIndex <= 0 {
  79. return nil, errors.New("Found default route but could not determine interface")
  80. }
  81. return net.InterfaceByIndex(route.LinkIndex)
  82. }
  83. }
  84. return nil, errors.New("Unable to find default route")
  85. }
  86. func GetInterfaceByIP(ip net.IP) (*net.Interface, error) {
  87. ifaces, err := net.Interfaces()
  88. if err != nil {
  89. return nil, err
  90. }
  91. for _, iface := range ifaces {
  92. err := GetInterfaceIP4AddrMatch(&iface, ip)
  93. if err == nil {
  94. return &iface, nil
  95. }
  96. }
  97. return nil, errors.New("No interface with given IP found")
  98. }
  99. func DirectRouting(ip net.IP) (bool, error) {
  100. routes, err := netlink.RouteGet(ip)
  101. if err != nil {
  102. return false, fmt.Errorf("couldn't lookup route to %v: %v", ip, err)
  103. }
  104. if len(routes) == 1 && routes[0].Gw == nil {
  105. // There is only a single route and there's no gateway (i.e. it's directly connected)
  106. return true, nil
  107. }
  108. return false, nil
  109. }
  110. // EnsureV4AddressOnLink ensures that there is only one v4 Addr on `link` and it equals `ipn`.
  111. // If there exist multiple addresses on link, it returns an error message to tell callers to remove additional address.
  112. func EnsureV4AddressOnLink(ipn IP4Net, link netlink.Link) error {
  113. addr := netlink.Addr{IPNet: ipn.ToIPNet()}
  114. existingAddrs, err := netlink.AddrList(link, netlink.FAMILY_V4)
  115. if err != nil {
  116. return err
  117. }
  118. // flannel will never make this happen. This situation can only be caused by a user, so get them to sort it out.
  119. if len(existingAddrs) > 1 {
  120. return fmt.Errorf("link has incompatible addresses. Remove additional addresses and try again. %#v", link)
  121. }
  122. // If the device has an incompatible address then delete it. This can happen if the lease changes for example.
  123. if len(existingAddrs) == 1 && !existingAddrs[0].Equal(addr) {
  124. if err := netlink.AddrDel(link, &existingAddrs[0]); err != nil {
  125. return fmt.Errorf("failed to remove IP address %s from %s: %s", ipn.String(), link.Attrs().Name, err)
  126. }
  127. existingAddrs = []netlink.Addr{}
  128. }
  129. // Actually add the desired address to the interface if needed.
  130. if len(existingAddrs) == 0 {
  131. if err := netlink.AddrAdd(link, &addr); err != nil {
  132. return fmt.Errorf("failed to add IP address %s to %s: %s", ipn.String(), link.Attrs().Name, err)
  133. }
  134. }
  135. return nil
  136. }