You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
330 lines
12 KiB
Go
330 lines
12 KiB
Go
package vpcrouting
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/netip"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
CapabilityVersion = uint64(1)
|
|
stateVersion = 1
|
|
maximumPorts = 4096
|
|
maximumPeerRoutes = 4096
|
|
)
|
|
|
|
var (
|
|
vpcKeyPattern = regexp.MustCompile(`^[a-f0-9]{32}$`)
|
|
operationIDPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{8,80}$`)
|
|
interfacePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_.-]{0,14}$`)
|
|
lowerHexDigest = regexp.MustCompile(`^[a-f0-9]{64}$`)
|
|
canonicalMACPattern = regexp.MustCompile(`^[0-9a-f]{2}(?::[0-9a-f]{2}){5}$`)
|
|
)
|
|
|
|
type Transport struct {
|
|
AddressCIDR string `json:"address_cidr"`
|
|
Interface string `json:"interface"`
|
|
MTU int `json:"mtu"`
|
|
}
|
|
|
|
type LocalPort struct {
|
|
IP string `json:"ip"`
|
|
MAC string `json:"mac"`
|
|
}
|
|
|
|
type PeerRoute struct {
|
|
IP string `json:"ip"`
|
|
Via string `json:"via"`
|
|
}
|
|
|
|
// Network is the complete host-local projection for one regional VPC. The
|
|
// panel sends the whole projection on every revision so the Agent can remove
|
|
// stale routes without replaying imperative commands.
|
|
type Network struct {
|
|
VPCKey string `json:"vpc_key"`
|
|
DesiredRevision uint64 `json:"desired_revision"`
|
|
CIDR string `json:"cidr"`
|
|
Gateway string `json:"gateway"`
|
|
Transport Transport `json:"transport"`
|
|
LocalPorts []LocalPort `json:"local_ports"`
|
|
PeerRoutes []PeerRoute `json:"peer_routes"`
|
|
}
|
|
|
|
type ApplyRequest struct {
|
|
OperationID string `json:"operation_id,omitempty"`
|
|
DesiredDigest string `json:"desired_digest"`
|
|
Network Network `json:"network"`
|
|
VPCRepair bool `json:"vpc_repair,omitempty"`
|
|
SupersedesOperationID string `json:"supersedes_operation_id,omitempty"`
|
|
}
|
|
|
|
type RemoveRequest struct {
|
|
OperationID string `json:"operation_id,omitempty"`
|
|
VPCKey string `json:"vpc_key"`
|
|
ExpectedRevision uint64 `json:"expected_revision"`
|
|
ExpectedDigest string `json:"expected_digest"`
|
|
VPCRepair bool `json:"vpc_repair,omitempty"`
|
|
SupersedesOperationID string `json:"supersedes_operation_id,omitempty"`
|
|
}
|
|
|
|
type Result struct {
|
|
VPCKey string `json:"vpc_key"`
|
|
DesiredRevision uint64 `json:"desired_revision"`
|
|
DesiredDigest string `json:"desired_digest"`
|
|
Bridge string `json:"bridge,omitempty"`
|
|
Applied bool `json:"applied,omitempty"`
|
|
Removed bool `json:"removed,omitempty"`
|
|
}
|
|
|
|
type Inspection struct {
|
|
Applied bool
|
|
Removed bool
|
|
Result Result
|
|
}
|
|
|
|
type publicError struct {
|
|
code string
|
|
message string
|
|
cause error
|
|
}
|
|
|
|
func (e *publicError) Error() string {
|
|
if e.message != "" {
|
|
return e.message
|
|
}
|
|
if e.cause != nil {
|
|
return e.cause.Error()
|
|
}
|
|
return e.code
|
|
}
|
|
|
|
func (e *publicError) Unwrap() error { return e.cause }
|
|
func (e *publicError) PublicCode() string { return e.code }
|
|
func (e *publicError) PublicMessage() string { return e.message }
|
|
|
|
type notAppliedError struct{ error }
|
|
|
|
func (e *notAppliedError) FailureSemantics() (bool, bool) { return false, true }
|
|
|
|
func invalid(code, message string) error {
|
|
return &publicError{code: code, message: message}
|
|
}
|
|
|
|
func unavailable(code, message string, cause error) error {
|
|
return &publicError{code: code, message: message, cause: cause}
|
|
}
|
|
|
|
func rolledBack(cause error) error {
|
|
return ¬AppliedError{error: &publicError{
|
|
code: "vpc_reconcile_rolled_back",
|
|
message: "VPC host routing could not be applied and the previous state was restored",
|
|
cause: cause,
|
|
}}
|
|
}
|
|
|
|
func Normalize(network Network) (Network, error) {
|
|
if !vpcKeyPattern.MatchString(network.VPCKey) {
|
|
return Network{}, invalid("invalid_vpc_key", "vpc_key must contain 32 lowercase hexadecimal characters")
|
|
}
|
|
if network.DesiredRevision == 0 {
|
|
return Network{}, invalid("invalid_vpc_revision", "desired_revision must be positive")
|
|
}
|
|
prefix, err := netip.ParsePrefix(network.CIDR)
|
|
if err != nil || !prefix.Addr().Is4() || prefix.Bits() < 16 || prefix.Bits() > 28 || prefix != prefix.Masked() || !prefix.Addr().IsPrivate() {
|
|
return Network{}, invalid("invalid_vpc_cidr", "cidr must be a canonical RFC1918 IPv4 prefix between /16 and /28")
|
|
}
|
|
gateway, err := netip.ParseAddr(network.Gateway)
|
|
if err != nil || !gateway.Is4() || gateway != prefix.Addr().Next() {
|
|
return Network{}, invalid("invalid_vpc_gateway", "gateway must be the first usable address in the VPC prefix")
|
|
}
|
|
transport, err := normalizeTransport(network.Transport)
|
|
if err != nil {
|
|
return Network{}, err
|
|
}
|
|
if len(network.LocalPorts) > maximumPorts || len(network.PeerRoutes) > maximumPeerRoutes {
|
|
return Network{}, invalid("vpc_projection_too_large", "VPC host routing projection exceeds the supported size")
|
|
}
|
|
|
|
locals := make([]LocalPort, 0, len(network.LocalPorts))
|
|
localIPs := make(map[netip.Addr]struct{}, len(network.LocalPorts))
|
|
localMACs := make(map[string]struct{}, len(network.LocalPorts))
|
|
for _, port := range network.LocalPorts {
|
|
address, parseErr := netip.ParseAddr(port.IP)
|
|
mac, macErr := net.ParseMAC(port.MAC)
|
|
if parseErr != nil || !usableVPCAddress(prefix, gateway, address) || macErr != nil || len(mac) != 6 || !canonicalMACPattern.MatchString(port.MAC) || mac[0]&1 != 0 {
|
|
return Network{}, invalid("invalid_vpc_local_port", "local_ports contains an invalid IP or MAC address")
|
|
}
|
|
if _, exists := localIPs[address]; exists {
|
|
return Network{}, invalid("duplicate_vpc_local_ip", "local_ports contains a duplicate IP address")
|
|
}
|
|
if _, exists := localMACs[port.MAC]; exists {
|
|
return Network{}, invalid("duplicate_vpc_local_mac", "local_ports contains a duplicate MAC address")
|
|
}
|
|
localIPs[address] = struct{}{}
|
|
localMACs[port.MAC] = struct{}{}
|
|
locals = append(locals, LocalPort{IP: address.String(), MAC: port.MAC})
|
|
}
|
|
|
|
transportPrefix, _ := netip.ParsePrefix(transport.AddressCIDR)
|
|
transportAddress := transportPrefix.Addr()
|
|
peers := make([]PeerRoute, 0, len(network.PeerRoutes))
|
|
peerIPs := make(map[netip.Addr]struct{}, len(network.PeerRoutes))
|
|
for _, route := range network.PeerRoutes {
|
|
address, addressErr := netip.ParseAddr(route.IP)
|
|
via, viaErr := netip.ParseAddr(route.Via)
|
|
if addressErr != nil || !usableVPCAddress(prefix, gateway, address) || viaErr != nil || !via.Is4() || via == transportAddress || !transportPrefix.Contains(via) {
|
|
return Network{}, invalid("invalid_vpc_peer_route", "peer_routes contains an invalid destination or next hop")
|
|
}
|
|
if _, local := localIPs[address]; local {
|
|
return Network{}, invalid("vpc_route_local_conflict", "a VPC address cannot be both local and remote")
|
|
}
|
|
if _, exists := peerIPs[address]; exists {
|
|
return Network{}, invalid("duplicate_vpc_peer_ip", "peer_routes contains a duplicate destination")
|
|
}
|
|
peerIPs[address] = struct{}{}
|
|
peers = append(peers, PeerRoute{IP: address.String(), Via: via.String()})
|
|
}
|
|
|
|
sort.Slice(locals, func(left, right int) bool {
|
|
return compareAddress(locals[left].IP, locals[right].IP) < 0
|
|
})
|
|
sort.Slice(peers, func(left, right int) bool {
|
|
if order := compareAddress(peers[left].IP, peers[right].IP); order != 0 {
|
|
return order < 0
|
|
}
|
|
return compareAddress(peers[left].Via, peers[right].Via) < 0
|
|
})
|
|
|
|
return Network{
|
|
VPCKey: network.VPCKey, DesiredRevision: network.DesiredRevision,
|
|
CIDR: prefix.String(), Gateway: gateway.String(), Transport: transport,
|
|
LocalPorts: locals, PeerRoutes: peers,
|
|
}, nil
|
|
}
|
|
|
|
func normalizeTransport(transport Transport) (Transport, error) {
|
|
prefix, err := netip.ParsePrefix(strings.TrimSpace(transport.AddressCIDR))
|
|
if err != nil || !prefix.Addr().Is4() || !prefix.Addr().IsPrivate() || prefix.Bits() < 1 || prefix.Bits() > 32 || prefix.Addr().IsLoopback() || prefix.Addr().IsLinkLocalUnicast() || prefix.Addr().IsUnspecified() || prefix.Addr().IsMulticast() {
|
|
return Transport{}, invalid("invalid_vpc_transport", "transport address must be an RFC1918 IPv4 interface address")
|
|
}
|
|
if !interfacePattern.MatchString(transport.Interface) || transport.MTU < 576 || transport.MTU > 9216 {
|
|
return Transport{}, invalid("invalid_vpc_transport", "transport interface or MTU is invalid")
|
|
}
|
|
return Transport{
|
|
AddressCIDR: prefix.String(), Interface: transport.Interface, MTU: transport.MTU,
|
|
}, nil
|
|
}
|
|
|
|
func ValidateApplyRequest(request ApplyRequest) (ApplyRequest, error) {
|
|
if !operationIDPattern.MatchString(request.OperationID) {
|
|
return ApplyRequest{}, invalid("invalid_operation_id", "operation_id must contain 8-80 URL-safe characters")
|
|
}
|
|
if request.VPCRepair != (request.SupersedesOperationID != "") ||
|
|
(request.VPCRepair && (!operationIDPattern.MatchString(request.SupersedesOperationID) ||
|
|
request.SupersedesOperationID == request.OperationID)) {
|
|
return ApplyRequest{}, invalid("invalid_vpc_repair", "VPC repair requires one different valid predecessor operation")
|
|
}
|
|
network, err := Normalize(request.Network)
|
|
if err != nil {
|
|
return ApplyRequest{}, err
|
|
}
|
|
digest, err := Digest(network)
|
|
if err != nil {
|
|
return ApplyRequest{}, unavailable("vpc_digest_failed", "VPC desired state could not be reduced", err)
|
|
}
|
|
if !lowerHexDigest.MatchString(request.DesiredDigest) || request.DesiredDigest != digest {
|
|
return ApplyRequest{}, invalid("vpc_digest_mismatch", "desired_digest does not match the canonical VPC projection")
|
|
}
|
|
request.Network = network
|
|
return request, nil
|
|
}
|
|
|
|
func ValidateRemoveRequest(request RemoveRequest) error {
|
|
if !operationIDPattern.MatchString(request.OperationID) || !vpcKeyPattern.MatchString(request.VPCKey) || request.ExpectedRevision == 0 || !lowerHexDigest.MatchString(request.ExpectedDigest) {
|
|
return invalid("invalid_vpc_remove", "VPC removal identity, revision, or digest is invalid")
|
|
}
|
|
if request.VPCRepair != (request.SupersedesOperationID != "") ||
|
|
(request.VPCRepair && (!operationIDPattern.MatchString(request.SupersedesOperationID) ||
|
|
request.SupersedesOperationID == request.OperationID)) {
|
|
return invalid("invalid_vpc_repair", "VPC repair requires one different valid predecessor operation")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Digest(network Network) (string, error) {
|
|
normalized, err := Normalize(network)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
encoded, err := json.Marshal(normalized)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
digest := sha256.Sum256(encoded)
|
|
return hex.EncodeToString(digest[:]), nil
|
|
}
|
|
|
|
func BridgeName(vpcKey string) (string, error) {
|
|
if !vpcKeyPattern.MatchString(vpcKey) {
|
|
return "", errors.New("invalid VPC key")
|
|
}
|
|
digest := sha256.Sum256([]byte("virt-cloud-vpc-bridge:" + vpcKey))
|
|
return "vc" + hex.EncodeToString(digest[:6]), nil
|
|
}
|
|
|
|
func bridgeMAC(vpcKey string) (string, error) {
|
|
if !vpcKeyPattern.MatchString(vpcKey) {
|
|
return "", errors.New("invalid VPC key")
|
|
}
|
|
digest := sha256.Sum256([]byte("virt-cloud-vpc-bridge-mac:" + vpcKey))
|
|
return fmt.Sprintf("02:%02x:%02x:%02x:%02x:%02x", digest[0], digest[1], digest[2], digest[3], digest[4]), nil
|
|
}
|
|
|
|
func nftObjectSuffix(vpcKey string) (string, error) {
|
|
bridge, err := BridgeName(vpcKey)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return strings.TrimPrefix(bridge, "vc"), nil
|
|
}
|
|
|
|
func usableVPCAddress(prefix netip.Prefix, gateway, address netip.Addr) bool {
|
|
if !address.IsValid() || !address.Is4() || !prefix.Contains(address) || address == prefix.Addr() || address == gateway {
|
|
return false
|
|
}
|
|
broadcast := prefix.Masked().Addr().As4()
|
|
hostBits := 32 - prefix.Bits()
|
|
value := uint32(broadcast[0])<<24 | uint32(broadcast[1])<<16 | uint32(broadcast[2])<<8 | uint32(broadcast[3])
|
|
value |= uint32(1<<hostBits) - 1
|
|
last := netip.AddrFrom4([4]byte{byte(value >> 24), byte(value >> 16), byte(value >> 8), byte(value)})
|
|
return address != last
|
|
}
|
|
|
|
func compareAddress(left, right string) int {
|
|
leftAddress, _ := netip.ParseAddr(left)
|
|
rightAddress, _ := netip.ParseAddr(right)
|
|
return leftAddress.Compare(rightAddress)
|
|
}
|
|
|
|
func validateUniqueBridgeNames(networks map[string]Network) error {
|
|
seen := make(map[string]string, len(networks))
|
|
for key := range networks {
|
|
bridge, err := BridgeName(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if previous, exists := seen[bridge]; exists && previous != key {
|
|
return fmt.Errorf("VPC bridge name collision")
|
|
}
|
|
seen[bridge] = key
|
|
}
|
|
return nil
|
|
}
|