iface.go 4.3 KB

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