| 1 |
# Copyright 2014 Cisco Systems, Inc. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# 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, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import socket |
| 16 |
|
| 17 |
import netaddr |
| 18 |
from neutron.db import l3_db |
| 19 |
from neutron.db import models_v2 |
| 20 |
from neutron_lib.api import validators |
| 21 |
from neutron_lib import exceptions as nexception |
| 22 |
from neutron_lib.exceptions import vpn as vpn_exception |
| 23 |
from neutron_lib.plugins import constants as plugin_const |
| 24 |
from neutron_lib.plugins import directory |
| 25 |
|
| 26 |
from neutron_vpnaas._i18n import _ |
| 27 |
from neutron_vpnaas.services.vpn.common import constants |
| 28 |
|
| 29 |
|
| 30 |
class VpnReferenceValidator: |
| 31 |
|
| 32 |
""" |
| 33 |
Baseline validation routines for VPN resources. |
| 34 |
The validations here should be common to all VPN service providers and |
| 35 |
only raise exceptions from neutron_vpnaas.extensions.vpnaas. |
| 36 |
""" |
| 37 |
|
| 38 |
IP_MIN_MTU = {4: 68, 6: 1280} |
| 39 |
|
| 40 |
@property |
| 41 |
def l3_plugin(self): |
| 42 |
try: |
| 43 |
return self._l3_plugin |
| 44 |
except AttributeError: |
| 45 |
self._l3_plugin = directory.get_plugin(plugin_const.L3) |
| 46 |
return self._l3_plugin |
| 47 |
|
| 48 |
@property |
| 49 |
def core_plugin(self): |
| 50 |
try: |
| 51 |
return self._core_plugin |
| 52 |
except AttributeError: |
| 53 |
self._core_plugin = directory.get_plugin() |
| 54 |
return self._core_plugin |
| 55 |
|
| 56 |
def _check_dpd(self, ipsec_sitecon): |
| 57 |
"""Ensure that DPD timeout is greater than DPD interval.""" |
| 58 |
if ipsec_sitecon['dpd_timeout'] <= ipsec_sitecon['dpd_interval']: |
| 59 |
raise vpn_exception.IPsecSiteConnectionDpdIntervalValueError( |
| 60 |
attr='dpd_timeout') |
| 61 |
|
| 62 |
def _check_mtu(self, context, mtu, ip_version): |
| 63 |
if mtu < VpnReferenceValidator.IP_MIN_MTU[ip_version]: |
| 64 |
raise vpn_exception.IPsecSiteConnectionMtuError( |
| 65 |
mtu=mtu, version=ip_version) |
| 66 |
|
| 67 |
def _validate_peer_address(self, ip_version, router): |
| 68 |
# NOTE: peer_address ip version should match with |
| 69 |
# at least one external gateway address ip version. |
| 70 |
# ipsec won't work with IPv6 LLA and neutron unaware GUA. |
| 71 |
# So to support vpnaas with ipv6, external network must |
| 72 |
# have ipv6 subnet |
| 73 |
for fixed_ip in router.gw_port['fixed_ips']: |
| 74 |
addr = fixed_ip['ip_address'] |
| 75 |
if ip_version == netaddr.IPAddress(addr).version: |
| 76 |
return |
| 77 |
|
| 78 |
raise vpn_exception.ExternalNetworkHasNoSubnet( |
| 79 |
router_id=router.id, |
| 80 |
ip_version="IPv6" if ip_version == 6 else "IPv4") |
| 81 |
|
| 82 |
def resolve_peer_address(self, ipsec_sitecon, router): |
| 83 |
address = ipsec_sitecon['peer_address'] |
| 84 |
# check if address is an ip address or fqdn |
| 85 |
invalid_ip_address = validators.validate_ip_address(address) |
| 86 |
if invalid_ip_address: |
| 87 |
# resolve fqdn |
| 88 |
try: |
| 89 |
addrinfo = socket.getaddrinfo(address, None)[0] |
| 90 |
ipsec_sitecon['peer_address'] = addrinfo[-1][0] |
| 91 |
except socket.gaierror: |
| 92 |
raise vpn_exception.VPNPeerAddressNotResolved( |
| 93 |
peer_address=address) |
| 94 |
|
| 95 |
ip_version = netaddr.IPAddress(ipsec_sitecon['peer_address']).version |
| 96 |
self._validate_peer_address(ip_version, router) |
| 97 |
|
| 98 |
def _get_local_subnets(self, context, endpoint_group): |
| 99 |
if endpoint_group['type'] != constants.SUBNET_ENDPOINT: |
| 100 |
raise vpn_exception.WrongEndpointGroupType( |
| 101 |
group_type=endpoint_group['type'], which=endpoint_group['id'], |
| 102 |
expected=constants.SUBNET_ENDPOINT) |
| 103 |
subnet_ids = endpoint_group['endpoints'] |
| 104 |
return context.session.query(models_v2.Subnet).filter( |
| 105 |
models_v2.Subnet.id.in_(subnet_ids)).all() |
| 106 |
|
| 107 |
def _get_peer_cidrs(self, endpoint_group): |
| 108 |
if endpoint_group['type'] != constants.CIDR_ENDPOINT: |
| 109 |
raise vpn_exception.WrongEndpointGroupType( |
| 110 |
group_type=endpoint_group['type'], which=endpoint_group['id'], |
| 111 |
expected=constants.CIDR_ENDPOINT) |
| 112 |
return endpoint_group['endpoints'] |
| 113 |
|
| 114 |
def _check_local_endpoint_ip_versions(self, group_id, local_subnets): |
| 115 |
"""Ensure all subnets in endpoint group have the same IP version. |
| 116 |
|
| 117 |
Will return the IP version, so it can be used for inter-group testing. |
| 118 |
""" |
| 119 |
if len(local_subnets) == 1: |
| 120 |
return local_subnets[0]['ip_version'] |
| 121 |
ip_versions = {subnet['ip_version'] for subnet in local_subnets} |
| 122 |
if len(ip_versions) > 1: |
| 123 |
raise vpn_exception.MixedIPVersionsForIPSecEndpoints( |
| 124 |
group=group_id) |
| 125 |
return ip_versions.pop() |
| 126 |
|
| 127 |
def _check_peer_endpoint_ip_versions(self, group_id, peer_cidrs): |
| 128 |
"""Ensure all CIDRs in endpoint group have the same IP version. |
| 129 |
|
| 130 |
Will return the IP version, so it can be used for inter-group testing. |
| 131 |
""" |
| 132 |
if len(peer_cidrs) == 1: |
| 133 |
return netaddr.IPNetwork(peer_cidrs[0]).version |
| 134 |
ip_versions = {netaddr.IPNetwork(pc).version for pc in peer_cidrs} |
| 135 |
if len(ip_versions) > 1: |
| 136 |
raise vpn_exception.MixedIPVersionsForIPSecEndpoints( |
| 137 |
group=group_id) |
| 138 |
return ip_versions.pop() |
| 139 |
|
| 140 |
def _check_peer_cidrs(self, peer_cidrs): |
| 141 |
"""Ensure all CIDRs have the valid format.""" |
| 142 |
for peer_cidr in peer_cidrs: |
| 143 |
msg = validators.validate_subnet(peer_cidr) |
| 144 |
if msg: |
| 145 |
raise vpn_exception.IPsecSiteConnectionPeerCidrError( |
| 146 |
peer_cidr=peer_cidr) |
| 147 |
|
| 148 |
def _check_peer_cidrs_ip_versions(self, peer_cidrs): |
| 149 |
"""Ensure all CIDRs have the same IP version.""" |
| 150 |
if len(peer_cidrs) == 1: |
| 151 |
return netaddr.IPNetwork(peer_cidrs[0]).version |
| 152 |
ip_versions = {netaddr.IPNetwork(pc).version for pc in peer_cidrs} |
| 153 |
if len(ip_versions) > 1: |
| 154 |
raise vpn_exception.MixedIPVersionsForPeerCidrs() |
| 155 |
return ip_versions.pop() |
| 156 |
|
| 157 |
def _check_local_subnets_on_router(self, context, router, local_subnets): |
| 158 |
for subnet in local_subnets: |
| 159 |
self._check_subnet_id(context, router, subnet['id']) |
| 160 |
|
| 161 |
def _validate_compatible_ip_versions(self, local_ip_version, |
| 162 |
peer_ip_version): |
| 163 |
if local_ip_version != peer_ip_version: |
| 164 |
raise vpn_exception.MixedIPVersionsForIPSecConnection() |
| 165 |
|
| 166 |
def validate_ipsec_conn_optional_args(self, ipsec_sitecon, subnet): |
| 167 |
"""Ensure that proper combinations of optional args are used. |
| 168 |
|
| 169 |
When VPN service has a subnet, then we must have peer_cidrs, and |
| 170 |
cannot have any endpoint groups. If no subnet for the service, then |
| 171 |
we must have both endpoint groups, and no peer_cidrs. Method will |
| 172 |
form a string indicating which endpoints are incorrect, for any |
| 173 |
exception raised. |
| 174 |
""" |
| 175 |
|
| 176 |
local_epg_id = ipsec_sitecon.get('local_ep_group_id') |
| 177 |
peer_epg_id = ipsec_sitecon.get('peer_ep_group_id') |
| 178 |
peer_cidrs = ipsec_sitecon.get('peer_cidrs') |
| 179 |
if subnet: |
| 180 |
if not peer_cidrs: |
| 181 |
raise vpn_exception.MissingPeerCidrs() |
| 182 |
epgs = [] |
| 183 |
if local_epg_id: |
| 184 |
epgs.append('local') |
| 185 |
if peer_epg_id: |
| 186 |
epgs.append('peer') |
| 187 |
if epgs: |
| 188 |
which = ' and '.join(epgs) |
| 189 |
suffix = 's' if len(epgs) > 1 else '' |
| 190 |
raise vpn_exception.InvalidEndpointGroup(which=which, |
| 191 |
suffix=suffix) |
| 192 |
else: |
| 193 |
if peer_cidrs: |
| 194 |
raise vpn_exception.PeerCidrsInvalid() |
| 195 |
epgs = [] |
| 196 |
if not local_epg_id: |
| 197 |
epgs.append('local') |
| 198 |
if not peer_epg_id: |
| 199 |
epgs.append('peer') |
| 200 |
if epgs: |
| 201 |
which = ' and '.join(epgs) |
| 202 |
suffix = 's' if len(epgs) > 1 else '' |
| 203 |
raise vpn_exception.MissingRequiredEndpointGroup( |
| 204 |
which=which, suffix=suffix) |
| 205 |
|
| 206 |
def assign_sensible_ipsec_sitecon_defaults(self, ipsec_sitecon, |
| 207 |
prev_conn=None): |
| 208 |
"""Provide defaults for optional items, if missing. |
| 209 |
|
| 210 |
With endpoint groups capabilities, the peer_cidr (legacy mode) |
| 211 |
and endpoint group IDs (new mode), are optional. For updating, |
| 212 |
we need to provide the previous values for any missing values, |
| 213 |
so that we can detect if the update request is attempting to |
| 214 |
mix modes. |
| 215 |
|
| 216 |
Flatten the nested DPD information, and set default values for |
| 217 |
any missing information. For connection updates, the previous |
| 218 |
values will be used as defaults for any missing items. |
| 219 |
""" |
| 220 |
|
| 221 |
if prev_conn: |
| 222 |
ipsec_sitecon.setdefault( |
| 223 |
'peer_cidrs', [pc['cidr'] for pc in prev_conn['peer_cidrs']]) |
| 224 |
ipsec_sitecon.setdefault('local_ep_group_id', |
| 225 |
prev_conn['local_ep_group_id']) |
| 226 |
ipsec_sitecon.setdefault('peer_ep_group_id', |
| 227 |
prev_conn['peer_ep_group_id']) |
| 228 |
else: |
| 229 |
prev_conn = {'dpd_action': 'hold', |
| 230 |
'dpd_interval': 30, |
| 231 |
'dpd_timeout': 120} |
| 232 |
dpd = ipsec_sitecon.get('dpd', {}) |
| 233 |
ipsec_sitecon['dpd_action'] = dpd.get('action', |
| 234 |
prev_conn['dpd_action']) |
| 235 |
ipsec_sitecon['dpd_interval'] = dpd.get('interval', |
| 236 |
prev_conn['dpd_interval']) |
| 237 |
ipsec_sitecon['dpd_timeout'] = dpd.get('timeout', |
| 238 |
prev_conn['dpd_timeout']) |
| 239 |
|
| 240 |
def validate_ipsec_site_connection(self, context, ipsec_sitecon, |
| 241 |
local_ip_version, vpnservice=None): |
| 242 |
"""Reference implementation of validation for IPSec connection. |
| 243 |
|
| 244 |
This makes sure that IP versions are the same. For endpoint groups, |
| 245 |
we use the local subnet(s) IP versions, and peer CIDR(s) IP versions. |
| 246 |
For legacy mode, we use the (sole) subnet IP version, and the peer |
| 247 |
CIDR(s). All IP versions must be the same. |
| 248 |
|
| 249 |
This method also checks peer_cidrs format(legacy mode), |
| 250 |
MTU (based on the local IP version), and DPD settings. |
| 251 |
""" |
| 252 |
if not local_ip_version: |
| 253 |
# Using endpoint groups |
| 254 |
local_subnets = self._get_local_subnets( |
| 255 |
context, ipsec_sitecon['local_epg_subnets']) |
| 256 |
self._check_local_subnets_on_router( |
| 257 |
context, vpnservice['router_id'], local_subnets) |
| 258 |
local_ip_version = self._check_local_endpoint_ip_versions( |
| 259 |
ipsec_sitecon['local_ep_group_id'], local_subnets) |
| 260 |
peer_cidrs = self._get_peer_cidrs(ipsec_sitecon['peer_epg_cidrs']) |
| 261 |
peer_ip_version = self._check_peer_endpoint_ip_versions( |
| 262 |
ipsec_sitecon['peer_ep_group_id'], peer_cidrs) |
| 263 |
else: |
| 264 |
self._check_peer_cidrs(ipsec_sitecon['peer_cidrs']) |
| 265 |
peer_ip_version = self._check_peer_cidrs_ip_versions( |
| 266 |
ipsec_sitecon['peer_cidrs']) |
| 267 |
self._validate_compatible_ip_versions(local_ip_version, |
| 268 |
peer_ip_version) |
| 269 |
|
| 270 |
self._check_dpd(ipsec_sitecon) |
| 271 |
mtu = ipsec_sitecon.get('mtu') |
| 272 |
if mtu: |
| 273 |
self._check_mtu(context, mtu, local_ip_version) |
| 274 |
|
| 275 |
def _check_router(self, context, router_id): |
| 276 |
router = self.l3_plugin.get_router(context, router_id) |
| 277 |
if not router.get(l3_db.EXTERNAL_GW_INFO): |
| 278 |
raise vpn_exception.RouterIsNotExternal(router_id=router_id) |
| 279 |
|
| 280 |
def _check_subnet_id(self, context, router_id, subnet_id): |
| 281 |
ports = self.core_plugin.get_ports( |
| 282 |
context, |
| 283 |
filters={ |
| 284 |
'fixed_ips': {'subnet_id': [subnet_id]}, |
| 285 |
'device_id': [router_id]}) |
| 286 |
if not ports: |
| 287 |
raise vpn_exception.SubnetIsNotConnectedToRouter( |
| 288 |
subnet_id=subnet_id, |
| 289 |
router_id=router_id) |
| 290 |
|
| 291 |
def validate_vpnservice(self, context, vpnservice): |
| 292 |
self._check_router(context, vpnservice['router_id']) |
| 293 |
if vpnservice['subnet_id'] is not None: |
| 294 |
self._check_subnet_id(context, vpnservice['router_id'], |
| 295 |
vpnservice['subnet_id']) |
| 296 |
|
| 297 |
def validate_ipsec_policy(self, context, ipsec_policy): |
| 298 |
"""Reference implementation of validation for IPSec Policy. |
| 299 |
|
| 300 |
Service driver can override and implement specific logic |
| 301 |
for IPSec Policy validation. |
| 302 |
""" |
| 303 |
pass |
| 304 |
|
| 305 |
def _validate_cidrs(self, cidrs): |
| 306 |
"""Ensure valid IPv4/6 CIDRs.""" |
| 307 |
for cidr in cidrs: |
| 308 |
msg = validators.validate_subnet(cidr) |
| 309 |
if msg: |
| 310 |
raise vpn_exception.InvalidEndpointInEndpointGroup( |
| 311 |
group_type=constants.CIDR_ENDPOINT, endpoint=cidr, |
| 312 |
why=_("Invalid CIDR")) |
| 313 |
|
| 314 |
def _validate_subnets(self, context, subnet_ids): |
| 315 |
"""Ensure UUIDs OK and subnets exist.""" |
| 316 |
for subnet_id in subnet_ids: |
| 317 |
msg = validators.validate_uuid(subnet_id) |
| 318 |
if msg: |
| 319 |
raise vpn_exception.InvalidEndpointInEndpointGroup( |
| 320 |
group_type=constants.SUBNET_ENDPOINT, endpoint=subnet_id, |
| 321 |
why=_('Invalid UUID')) |
| 322 |
try: |
| 323 |
self.core_plugin.get_subnet(context, subnet_id) |
| 324 |
except nexception.SubnetNotFound: |
| 325 |
raise vpn_exception.NonExistingSubnetInEndpointGroup( |
| 326 |
subnet=subnet_id) |
| 327 |
|
| 328 |
def validate_endpoint_group(self, context, endpoint_group): |
| 329 |
"""Reference validator for endpoint group. |
| 330 |
|
| 331 |
Ensures that there is at least one endpoint, all the endpoints in the |
| 332 |
group are of the same type, and that the endpoints are "valid". |
| 333 |
Note: Only called for create, as endpoints cannot be changed. |
| 334 |
""" |
| 335 |
endpoints = endpoint_group['endpoints'] |
| 336 |
if not endpoints: |
| 337 |
raise vpn_exception.MissingEndpointForEndpointGroup( |
| 338 |
group=endpoint_group) |
| 339 |
|
| 340 |
self._check_endpoints_unique(endpoint_group) |
| 341 |
|
| 342 |
group_type = endpoint_group['type'] |
| 343 |
if group_type == constants.CIDR_ENDPOINT: |
| 344 |
self._validate_cidrs(endpoints) |
| 345 |
elif group_type == constants.SUBNET_ENDPOINT: |
| 346 |
self._validate_subnets(context, endpoints) |
| 347 |
|
| 348 |
def _check_endpoints_unique(self, endpoint_group): |
| 349 |
seen_endpoints = [] |
| 350 |
for endpoint in endpoint_group['endpoints']: |
| 351 |
if endpoint in seen_endpoints: |
| 352 |
raise vpn_exception.InvalidEndpointInEndpointGroup( |
| 353 |
group_type=endpoint_group['type'], endpoint=endpoint, |
| 354 |
why=_('Endpoints have to be unique')) |
| 355 |
seen_endpoints.append(endpoint) |
| 356 |
|
| 357 |
def validate_ike_policy(self, context, ike_policy): |
| 358 |
"""Reference implementation of validation for IKE Policy. |
| 359 |
|
| 360 |
Service driver can override and implement specific logic |
| 361 |
for IKE Policy validation. |
| 362 |
""" |
| 363 |
pass |