iface.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 getIfaceV6Addrs(iface *net.Interface) ([]netlink.Addr, error) {
  33. link := &netlink.Device{
  34. netlink.LinkAttrs{
  35. Index: iface.Index,
  36. },
  37. }
  38. return netlink.AddrList(link, syscall.AF_INET6)
  39. }
  40. func GetInterfaceIP4Addr(iface *net.Interface) (net.IP, error) {
  41. addrs, err := getIfaceAddrs(iface)
  42. if err != nil {
  43. return nil, err
  44. }
  45. // prefer non link-local addr
  46. var ll net.IP
  47. for _, addr := range addrs {
  48. if addr.IP.To4() == nil {
  49. continue
  50. }
  51. if addr.IP.IsGlobalUnicast() {
  52. return addr.IP, nil
  53. }
  54. if addr.IP.IsLinkLocalUnicast() {
  55. ll = addr.IP
  56. }
  57. }
  58. if ll != nil {
  59. // didn't find global but found link-local. it'll do.
  60. return ll, nil
  61. }
  62. return nil, errors.New("No IPv4 address found for given interface")
  63. }
  64. func GetInterfaceIP6Addr(iface *net.Interface) (net.IP, error) {
  65. addrs, err := getIfaceV6Addrs(iface)
  66. if err != nil {
  67. return nil, err
  68. }
  69. // prefer non link-local addr
  70. var ll net.IP
  71. for _, addr := range addrs {
  72. if addr.IP.To16() == nil {
  73. continue
  74. }
  75. if addr.IP.IsGlobalUnicast() {
  76. return addr.IP, nil
  77. }
  78. if addr.IP.IsLinkLocalUnicast() {
  79. ll = addr.IP
  80. }
  81. }
  82. if ll != nil {
  83. // didn't find global but found link-local. it'll do.
  84. return ll, nil
  85. }
  86. return nil, errors.New("No IPv6 address found for given interface")
  87. }
  88. func GetInterfaceIP4AddrMatch(iface *net.Interface, matchAddr net.IP) error {
  89. addrs, err := getIfaceAddrs(iface)
  90. if err != nil {
  91. return err
  92. }
  93. for _, addr := range addrs {
  94. // Attempt to parse the address in CIDR notation
  95. // and assert it is IPv4
  96. if addr.IP.To4() != nil {
  97. if addr.IP.To4().Equal(matchAddr) {
  98. return nil
  99. }
  100. }
  101. }
  102. return errors.New("No IPv4 address found for given interface")
  103. }
  104. func GetInterfaceIP6AddrMatch(iface *net.Interface, matchAddr net.IP) error {
  105. addrs, err := getIfaceV6Addrs(iface)
  106. if err != nil {
  107. return err
  108. }
  109. for _, addr := range addrs {
  110. // Attempt to parse the address in CIDR notation
  111. // and assert it is IPv6
  112. if addr.IP.To16() != nil {
  113. if addr.IP.To16().Equal(matchAddr) {
  114. return nil
  115. }
  116. }
  117. }
  118. return errors.New("No IPv6 address found for given interface")
  119. }
  120. func GetDefaultGatewayInterface() (*net.Interface, error) {
  121. routes, err := netlink.RouteList(nil, syscall.AF_INET)
  122. if err != nil {
  123. return nil, err
  124. }
  125. for _, route := range routes {
  126. if route.Dst == nil || route.Dst.String() == "0.0.0.0/0" {
  127. if route.LinkIndex <= 0 {
  128. return nil, errors.New("Found default route but could not determine interface")
  129. }
  130. return net.InterfaceByIndex(route.LinkIndex)
  131. }
  132. }
  133. return nil, errors.New("Unable to find default route")
  134. }
  135. func GetDefaultV6GatewayInterface() (*net.Interface, error) {
  136. routes, err := netlink.RouteList(nil, syscall.AF_INET6)
  137. if err != nil {
  138. return nil, err
  139. }
  140. for _, route := range routes {
  141. if route.Dst == nil || route.Dst.String() == "::/0" {
  142. if route.LinkIndex <= 0 {
  143. return nil, errors.New("Found default v6 route but could not determine interface")
  144. }
  145. return net.InterfaceByIndex(route.LinkIndex)
  146. }
  147. }
  148. return nil, errors.New("Unable to find default v6 route")
  149. }
  150. func GetInterfaceByIP(ip net.IP) (*net.Interface, error) {
  151. ifaces, err := net.Interfaces()
  152. if err != nil {
  153. return nil, err
  154. }
  155. for _, iface := range ifaces {
  156. err := GetInterfaceIP4AddrMatch(&iface, ip)
  157. if err == nil {
  158. return &iface, nil
  159. }
  160. }
  161. return nil, errors.New("No interface with given IP found")
  162. }
  163. func GetInterfaceByIP6(ip net.IP) (*net.Interface, error) {
  164. ifaces, err := net.Interfaces()
  165. if err != nil {
  166. return nil, err
  167. }
  168. for _, iface := range ifaces {
  169. err := GetInterfaceIP6AddrMatch(&iface, ip)
  170. if err == nil {
  171. return &iface, nil
  172. }
  173. }
  174. return nil, errors.New("No interface with given IPv6 found")
  175. }
  176. func DirectRouting(ip net.IP) (bool, error) {
  177. routes, err := netlink.RouteGet(ip)
  178. if err != nil {
  179. return false, fmt.Errorf("couldn't lookup route to %v: %v", ip, err)
  180. }
  181. if len(routes) == 1 && routes[0].Gw == nil {
  182. // There is only a single route and there's no gateway (i.e. it's directly connected)
  183. return true, nil
  184. }
  185. return false, nil
  186. }
  187. // EnsureV4AddressOnLink ensures that there is only one v4 Addr on `link` within the `ipn` address space and it equals `ipa`.
  188. func EnsureV4AddressOnLink(ipa IP4Net, ipn IP4Net, link netlink.Link) error {
  189. addr := netlink.Addr{IPNet: ipa.ToIPNet()}
  190. existingAddrs, err := netlink.AddrList(link, netlink.FAMILY_V4)
  191. if err != nil {
  192. return err
  193. }
  194. var hasAddr bool
  195. for _, existingAddr := range existingAddrs {
  196. if existingAddr.Equal(addr) {
  197. hasAddr = true
  198. continue
  199. }
  200. if ipn.Contains(FromIP(existingAddr.IP)) {
  201. if err := netlink.AddrDel(link, &existingAddr); err != nil {
  202. return fmt.Errorf("failed to remove IP address %s from %s: %s", existingAddr.String(), link.Attrs().Name, err)
  203. }
  204. log.Infof("removed IP address %s from %s", existingAddr.String(), link.Attrs().Name)
  205. }
  206. }
  207. // Actually add the desired address to the interface if needed.
  208. if !hasAddr {
  209. if err := netlink.AddrAdd(link, &addr); err != nil {
  210. return fmt.Errorf("failed to add IP address %s to %s: %s", addr.String(), link.Attrs().Name, err)
  211. }
  212. }
  213. return nil
  214. }
  215. // EnsureV6AddressOnLink ensures that there is only one v6 Addr on `link` and it equals `ipn`.
  216. // If there exist multiple addresses on link, it returns an error message to tell callers to remove additional address.
  217. func EnsureV6AddressOnLink(ipn IP6Net, link netlink.Link) error {
  218. addr := netlink.Addr{IPNet: ipn.ToIPNet()}
  219. existingAddrs, err := netlink.AddrList(link, netlink.FAMILY_V6)
  220. if err != nil {
  221. return err
  222. }
  223. // flannel will never make this happen. This situation can only be caused by a user, so get them to sort it out.
  224. if len(existingAddrs) > 2 {
  225. return fmt.Errorf("link has incompatible v6 addresses. Remove additional v6 addresses and try again. %#v", link)
  226. }
  227. onlyLinkLocal := true
  228. for _, existingAddr := range existingAddrs {
  229. if !existingAddr.IP.IsLinkLocalUnicast() {
  230. if !existingAddr.Equal(addr) {
  231. if err := netlink.AddrDel(link, &existingAddr); err != nil {
  232. return fmt.Errorf("failed to remove v6 IP address %s from %s: %s", ipn.String(), link.Attrs().Name, err)
  233. }
  234. existingAddrs = []netlink.Addr{}
  235. onlyLinkLocal = false
  236. } else {
  237. return nil
  238. }
  239. }
  240. }
  241. if onlyLinkLocal {
  242. existingAddrs = []netlink.Addr{}
  243. }
  244. // Actually add the desired address to the interface if needed.
  245. if len(existingAddrs) == 0 {
  246. if err := netlink.AddrAdd(link, &addr); err != nil {
  247. return fmt.Errorf("failed to add v6 IP address %s to %s: %s", ipn.String(), link.Attrs().Name, err)
  248. }
  249. }
  250. return nil
  251. }