iface.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. log "k8s.io/klog"
  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 GetInterfaceIP4Addr(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 GetInterfaceIP4AddrMatch(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 GetDefaultGatewayInterface() (*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 := GetInterfaceIP4AddrMatch(&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` within the `ipn` address space and it equals `ipa`.
  112. func EnsureV4AddressOnLink(ipa IP4Net, ipn IP4Net, link netlink.Link) error {
  113. addr := netlink.Addr{IPNet: ipa.ToIPNet()}
  114. existingAddrs, err := netlink.AddrList(link, netlink.FAMILY_V4)
  115. if err != nil {
  116. return err
  117. }
  118. var hasAddr bool
  119. for _, existingAddr := range existingAddrs {
  120. if existingAddr.Equal(addr) {
  121. hasAddr = true
  122. continue
  123. }
  124. if ipn.Contains(FromIP(existingAddr.IP)) {
  125. if err := netlink.AddrDel(link, &existingAddr); err != nil {
  126. return fmt.Errorf("failed to remove IP address %s from %s: %s", existingAddr.String(), link.Attrs().Name, err)
  127. }
  128. log.Infof("removed IP address %s from %s", existingAddr.String(), link.Attrs().Name)
  129. }
  130. }
  131. // Actually add the desired address to the interface if needed.
  132. if !hasAddr {
  133. if err := netlink.AddrAdd(link, &addr); err != nil {
  134. return fmt.Errorf("failed to add IP address %s to %s: %s", addr.String(), link.Attrs().Name, err)
  135. }
  136. }
  137. return nil
  138. }