iface.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2015 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ip
  15. import (
  16. "errors"
  17. "net"
  18. "syscall"
  19. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/vishvananda/netlink"
  20. )
  21. func getIfaceAddrs(iface *net.Interface) ([]netlink.Addr, error) {
  22. link := &netlink.Device{
  23. netlink.LinkAttrs{
  24. Index: iface.Index,
  25. },
  26. }
  27. return netlink.AddrList(link, syscall.AF_INET)
  28. }
  29. func GetIfaceIP4Addr(iface *net.Interface) (net.IP, error) {
  30. addrs, err := getIfaceAddrs(iface)
  31. if err != nil {
  32. return nil, err
  33. }
  34. // prefer non link-local addr
  35. var ll net.IP
  36. for _, addr := range addrs {
  37. if addr.IP.To4() == nil {
  38. continue
  39. }
  40. if addr.IP.IsGlobalUnicast() {
  41. return addr.IP, nil
  42. }
  43. if addr.IP.IsLinkLocalUnicast() {
  44. ll = addr.IP
  45. }
  46. }
  47. if ll != nil {
  48. // didn't find global but found link-local. it'll do.
  49. return ll, nil
  50. }
  51. return nil, errors.New("No IPv4 address found for given interface")
  52. }
  53. func GetIfaceIP4AddrMatch(iface *net.Interface, matchAddr net.IP) error {
  54. addrs, err := getIfaceAddrs(iface)
  55. if err != nil {
  56. return err
  57. }
  58. for _, addr := range addrs {
  59. // Attempt to parse the address in CIDR notation
  60. // and assert it is IPv4
  61. if addr.IP.To4() != nil {
  62. if addr.IP.To4().Equal(matchAddr) {
  63. return nil
  64. }
  65. }
  66. }
  67. return errors.New("No IPv4 address found for given interface")
  68. }
  69. func GetDefaultGatewayIface() (*net.Interface, error) {
  70. routes, err := netlink.RouteList(nil, syscall.AF_INET)
  71. if err != nil {
  72. return nil, err
  73. }
  74. for _, route := range routes {
  75. if route.Dst == nil || route.Dst.String() == "0.0.0.0/0" {
  76. if route.LinkIndex <= 0 {
  77. return nil, errors.New("Found default route but could not determine interface")
  78. }
  79. return net.InterfaceByIndex(route.LinkIndex)
  80. }
  81. }
  82. return nil, errors.New("Unable to find default route")
  83. }
  84. func GetInterfaceByIP(ip net.IP) (*net.Interface, error) {
  85. ifaces, err := net.Interfaces()
  86. if err != nil {
  87. return nil, err
  88. }
  89. for _, iface := range ifaces {
  90. err := GetIfaceIP4AddrMatch(&iface, ip)
  91. if err == nil {
  92. return &iface, nil
  93. }
  94. }
  95. return nil, errors.New("No interface with given IP found")
  96. }