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.
virt/agent/hypervisor/vpc_port.go

109 lines
4.3 KiB
Go

package hypervisor
import (
"encoding/binary"
"net"
"net/netip"
"strings"
"git.luoli.live/root/virt/agent/vpcrouting"
)
// NormalizeVPCPortRequest validates the complete panel/IPAM assertion used by
// both attach and detach. Requiring the original host projection identity on
// detach prevents a stale request from removing a newly replaced port.
func NormalizeVPCPortRequest(request VPCPortRequest) (VPCPortRequest, error) {
if !operationIdentifierPattern.MatchString(request.OperationID) ||
!poolKeyPattern.MatchString(request.PortKey) ||
!poolKeyPattern.MatchString(request.VPCKey) || request.HostRevision == 0 ||
!digestPattern.MatchString(request.HostDigest) || !request.ConfirmRestart {
return VPCPortRequest{}, NewError(ErrInvalid, "invalid_vpc_port_request", "VPC port identity, host projection, and restart confirmation are required", nil)
}
if request.VPCRepair != (request.SupersedesOperationID != "") ||
(request.VPCRepair && (!operationIdentifierPattern.MatchString(request.SupersedesOperationID) ||
request.SupersedesOperationID == request.OperationID)) {
return VPCPortRequest{}, NewError(ErrInvalid, "invalid_vpc_repair", "VPC repair requires one different valid predecessor operation", nil)
}
prefix, err := netip.ParsePrefix(request.CIDR)
if err != nil || !prefix.Addr().Is4() || !prefix.Addr().IsPrivate() ||
prefix != prefix.Masked() || prefix.Bits() < 16 || prefix.Bits() > 28 {
return VPCPortRequest{}, NewError(ErrInvalid, "invalid_vpc_cidr", "VPC cidr must be a canonical RFC1918 IPv4 /16 through /28 prefix", err)
}
address, addressErr := netip.ParseAddr(request.PrivateIP)
gateway, gatewayErr := netip.ParseAddr(request.Gateway)
if addressErr != nil || !address.Is4() || !prefix.Contains(address) ||
address == prefix.Addr() || address == lastPrefixAddress(prefix) ||
gatewayErr != nil || gateway != prefix.Addr().Next() || address == gateway {
return VPCPortRequest{}, NewError(ErrInvalid, "invalid_vpc_private_ip", "VPC private address or gateway is invalid", errorsJoin(addressErr, gatewayErr))
}
hardware, macErr := net.ParseMAC(request.MAC)
canonicalMAC := strings.ToLower(request.MAC)
if macErr != nil || len(hardware) != 6 || hardware[0]&1 != 0 ||
canonicalMAC != request.MAC || isZeroHardwareAddress(hardware) {
return VPCPortRequest{}, NewError(ErrInvalid, "invalid_vpc_mac", "VPC port MAC must be one canonical unicast hardware address", macErr)
}
if _, err := vpcrouting.BridgeName(request.VPCKey); err != nil {
return VPCPortRequest{}, NewError(ErrInvalid, "invalid_vpc_key", "VPC key is invalid", err)
}
request.CIDR = prefix.String()
request.PrivateIP = address.String()
request.Gateway = gateway.String()
return request, nil
}
func vpcPortMetadata(request VPCPortRequest) VPCPortMetadata {
bridge, _ := vpcrouting.BridgeName(request.VPCKey)
return VPCPortMetadata{
PortKey: request.PortKey, VPCKey: request.VPCKey,
PrivateIP: request.PrivateIP, CIDR: request.CIDR,
Gateway: request.Gateway, MAC: request.MAC, Bridge: bridge,
HostRevision: request.HostRevision, HostDigest: request.HostDigest,
}
}
func validateVPCPortMetadata(value VPCPortMetadata) error {
request, err := NormalizeVPCPortRequest(VPCPortRequest{
OperationID: "validate-vpc-port", ConfirmRestart: true,
PortKey: value.PortKey, VPCKey: value.VPCKey,
PrivateIP: value.PrivateIP, CIDR: value.CIDR,
Gateway: value.Gateway, MAC: value.MAC,
HostRevision: value.HostRevision, HostDigest: value.HostDigest,
})
if err != nil {
return err
}
bridge, _ := vpcrouting.BridgeName(request.VPCKey)
if value.Bridge != bridge {
return NewError(ErrInvalid, "invalid_vpc_bridge", "VPC bridge does not match the deterministic VPC identity", nil)
}
return nil
}
func lastPrefixAddress(prefix netip.Prefix) netip.Addr {
bytes := prefix.Masked().Addr().As4()
value := binary.BigEndian.Uint32(bytes[:]) | (^uint32(0) >> prefix.Bits())
var result [4]byte
binary.BigEndian.PutUint32(result[:], value)
return netip.AddrFrom4(result)
}
func isZeroHardwareAddress(value net.HardwareAddr) bool {
for _, item := range value {
if item != 0 {
return false
}
}
return true
}
// errorsJoin avoids exposing validation implementation details while keeping
// the concrete parser cause available to local diagnostics.
func errorsJoin(values ...error) error {
for _, value := range values {
if value != nil {
return value
}
}
return nil
}