github.com/cilium/cilium@v1.16.2/pkg/datapath/connector/ipam.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package connector
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  
    10  	"github.com/cilium/cilium/api/v1/models"
    11  	"github.com/cilium/cilium/pkg/datapath/linux/route"
    12  	"github.com/cilium/cilium/pkg/defaults"
    13  )
    14  
    15  // IPv6Gateway returns the IPv6 gateway address for endpoints.
    16  func IPv6Gateway(addr *models.NodeAddressing) string {
    17  	// The host's IP is the gateway address
    18  	return addr.IPV6.IP
    19  }
    20  
    21  // IPv4Gateway returns the IPv4 gateway address for endpoints.
    22  func IPv4Gateway(addr *models.NodeAddressing) string {
    23  	// The host's IP is the gateway address
    24  	return addr.IPV4.IP
    25  }
    26  
    27  // IPv6Routes returns IPv6 routes to be installed in endpoint's networking namespace.
    28  func IPv6Routes(addr *models.NodeAddressing, linkMTU int) ([]route.Route, error) {
    29  	ip := net.ParseIP(addr.IPV6.IP)
    30  	if ip == nil {
    31  		return []route.Route{}, fmt.Errorf("Invalid IP address: %s", addr.IPV6.IP)
    32  	}
    33  	return []route.Route{
    34  		{
    35  			Prefix: net.IPNet{
    36  				IP:   ip,
    37  				Mask: defaults.ContainerIPv6Mask,
    38  			},
    39  		},
    40  		{
    41  			Prefix:  defaults.IPv6DefaultRoute,
    42  			Nexthop: &ip,
    43  			MTU:     linkMTU,
    44  		},
    45  	}, nil
    46  }
    47  
    48  // IPv4Routes returns IPv4 routes to be installed in endpoint's networking namespace.
    49  func IPv4Routes(addr *models.NodeAddressing, linkMTU int) ([]route.Route, error) {
    50  	ip := net.ParseIP(addr.IPV4.IP)
    51  	if ip == nil {
    52  		return []route.Route{}, fmt.Errorf("Invalid IP address: %s", addr.IPV4.IP)
    53  	}
    54  	return []route.Route{
    55  		{
    56  			Prefix: net.IPNet{
    57  				IP:   ip,
    58  				Mask: defaults.ContainerIPv4Mask,
    59  			},
    60  		},
    61  		{
    62  			Prefix:  defaults.IPv4DefaultRoute,
    63  			Nexthop: &ip,
    64  			MTU:     linkMTU,
    65  		},
    66  	}, nil
    67  }
    68  
    69  // SufficientAddressing returns an error if the provided NodeAddressing does
    70  // not provide sufficient information to derive all IPAM required settings.
    71  func SufficientAddressing(addr *models.NodeAddressing) error {
    72  	if addr == nil {
    73  		return fmt.Errorf("Cilium daemon did not provide addressing information")
    74  	}
    75  
    76  	if addr.IPV6 != nil && addr.IPV6.IP != "" {
    77  		return nil
    78  	}
    79  
    80  	if addr.IPV4 != nil && addr.IPV4.IP != "" {
    81  		return nil
    82  	}
    83  
    84  	return fmt.Errorf("Either IPv4 or IPv6 addressing must be provided")
    85  }