github.com/elfadel/cilium@v1.6.12/pkg/datapath/linux/node_addressing.go (about)

     1  // Copyright 2018-2019 Authors of Cilium
     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  
    15  package linux
    16  
    17  import (
    18  	"net"
    19  
    20  	"github.com/cilium/cilium/pkg/cidr"
    21  	"github.com/cilium/cilium/pkg/datapath"
    22  	"github.com/cilium/cilium/pkg/defaults"
    23  	"github.com/cilium/cilium/pkg/ip"
    24  	"github.com/cilium/cilium/pkg/node"
    25  
    26  	"github.com/vishvananda/netlink"
    27  )
    28  
    29  // FIXME: This currently maps to the code in pkg/node/node_address.go. That
    30  // code should really move into this package.
    31  
    32  func listLocalAddresses(family int) ([]net.IP, error) {
    33  	ipsToExclude := ip.GetExcludedIPs()
    34  	addrs, err := netlink.AddrList(nil, family)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	var addresses []net.IP
    40  
    41  	for _, addr := range addrs {
    42  		if addr.Scope == int(netlink.SCOPE_LINK) {
    43  			continue
    44  		}
    45  		if ip.IsExcluded(ipsToExclude, addr.IP) {
    46  			continue
    47  		}
    48  		switch addr.IP.String() {
    49  		case "127.0.0.1", "::1":
    50  			continue
    51  		}
    52  
    53  		addresses = append(addresses, addr.IP)
    54  	}
    55  
    56  	if hostDevice, err := netlink.LinkByName(defaults.HostDevice); hostDevice != nil && err == nil {
    57  		addrs, err = netlink.AddrList(hostDevice, family)
    58  		if err != nil {
    59  			return nil, err
    60  		}
    61  		for _, addr := range addrs {
    62  			if addr.Scope == int(netlink.SCOPE_LINK) {
    63  				addresses = append(addresses, addr.IP)
    64  			}
    65  		}
    66  	}
    67  
    68  	return addresses, nil
    69  }
    70  
    71  type addressFamilyIPv4 struct{}
    72  
    73  func (a *addressFamilyIPv4) Router() net.IP {
    74  	return node.GetInternalIPv4()
    75  }
    76  
    77  func (a *addressFamilyIPv4) PrimaryExternal() net.IP {
    78  	return node.GetExternalIPv4()
    79  }
    80  
    81  func (a *addressFamilyIPv4) AllocationCIDR() *cidr.CIDR {
    82  	return node.GetIPv4AllocRange()
    83  }
    84  
    85  func (a *addressFamilyIPv4) LocalAddresses() ([]net.IP, error) {
    86  	return listLocalAddresses(netlink.FAMILY_V4)
    87  }
    88  
    89  type addressFamilyIPv6 struct{}
    90  
    91  func (a *addressFamilyIPv6) Router() net.IP {
    92  	return node.GetIPv6Router()
    93  }
    94  
    95  func (a *addressFamilyIPv6) PrimaryExternal() net.IP {
    96  	return node.GetIPv6()
    97  }
    98  
    99  func (a *addressFamilyIPv6) AllocationCIDR() *cidr.CIDR {
   100  	return node.GetIPv6AllocRange()
   101  }
   102  
   103  func (a *addressFamilyIPv6) LocalAddresses() ([]net.IP, error) {
   104  	return listLocalAddresses(netlink.FAMILY_V6)
   105  }
   106  
   107  type linuxNodeAddressing struct {
   108  	ipv6 addressFamilyIPv6
   109  	ipv4 addressFamilyIPv4
   110  }
   111  
   112  // NewNodeAddressing returns a new linux node addressing model
   113  func NewNodeAddressing() datapath.NodeAddressing {
   114  	return &linuxNodeAddressing{}
   115  }
   116  
   117  func (n *linuxNodeAddressing) IPv6() datapath.NodeAddressingFamily {
   118  	return &n.ipv6
   119  }
   120  
   121  func (n *linuxNodeAddressing) IPv4() datapath.NodeAddressingFamily {
   122  	return &n.ipv4
   123  }