| 1 |
# Copyright (c) 2016 Yi Jing Zhu, IBM. |
| 2 |
# Copyright (c) 2023 SysEleven GmbH |
| 3 |
# All Rights Reserved. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 |
# not use this file except in compliance with the License. You may obtain |
| 7 |
# a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 |
# License for the specific language governing permissions and limitations |
| 15 |
# under the License. |
| 16 |
|
| 17 |
import netaddr |
| 18 |
|
| 19 |
from neutron.agent.common import utils as agent_common_utils |
| 20 |
from neutron.agent.linux import ip_lib |
| 21 |
from neutron_lib import constants as lib_constants |
| 22 |
from neutron_lib import context as nctx |
| 23 |
from oslo_concurrency import lockutils |
| 24 |
from oslo_log import log as logging |
| 25 |
|
| 26 |
from neutron_vpnaas.services.vpn.common import topics |
| 27 |
from neutron_vpnaas.services.vpn.device_drivers import ipsec |
| 28 |
from neutron_vpnaas.services.vpn.device_drivers import libreswan_ipsec |
| 29 |
from neutron_vpnaas.services.vpn.device_drivers import strongswan_ipsec |
| 30 |
|
| 31 |
PORT_PREFIX_INTERNAL = 'vr' |
| 32 |
PORT_PREFIX_EXTERNAL = 'vg' |
| 33 |
PORT_PREFIXES = { |
| 34 |
'internal': PORT_PREFIX_INTERNAL, |
| 35 |
'external': PORT_PREFIX_EXTERNAL, |
| 36 |
} |
| 37 |
|
| 38 |
LOG = logging.getLogger(__name__) |
| 39 |
|
| 40 |
|
| 41 |
class DeviceManager: |
| 42 |
"""Device Manager for ports in qvpn-xx namespace. |
| 43 |
It is a veth pair, one side in qvpn and the other |
| 44 |
side is attached to ovs. |
| 45 |
""" |
| 46 |
|
| 47 |
OVN_NS_PREFIX = "qvpn-" |
| 48 |
|
| 49 |
def __init__(self, conf, host, plugin, context): |
| 50 |
self.conf = conf |
| 51 |
self.host = host |
| 52 |
self.plugin = plugin |
| 53 |
self.context = context |
| 54 |
self.driver = agent_common_utils.load_interface_driver(conf) |
| 55 |
|
| 56 |
def get_interface_name(self, port, ptype): |
| 57 |
suffix = port['id'] |
| 58 |
return (PORT_PREFIXES[ptype] + suffix)[:self.driver.DEV_NAME_LEN] |
| 59 |
|
| 60 |
def get_namespace_name(self, process_id): |
| 61 |
return self.OVN_NS_PREFIX + process_id |
| 62 |
|
| 63 |
def get_existing_process_ids(self): |
| 64 |
"""Return the process IDs derived from the existing VPN namespaces.""" |
| 65 |
return [ns[len(self.OVN_NS_PREFIX):] |
| 66 |
for ns in ip_lib.list_network_namespaces() |
| 67 |
if ns.startswith(self.OVN_NS_PREFIX)] |
| 68 |
|
| 69 |
def set_default_route(self, namespace, subnet, device_name): |
| 70 |
device = ip_lib.IPDevice(device_name, namespace=namespace) |
| 71 |
gateway = device.route.get_gateway(ip_version=subnet['ip_version']) |
| 72 |
if gateway: |
| 73 |
gateway = gateway.get('gateway') |
| 74 |
new_gateway = subnet['gateway_ip'] |
| 75 |
if gateway == new_gateway: |
| 76 |
return |
| 77 |
device.route.add_gateway(subnet['gateway_ip']) |
| 78 |
|
| 79 |
def add_routes(self, namespace, cidrs, via): |
| 80 |
device = ip_lib.IPDevice(None, namespace=namespace) |
| 81 |
for cidr in cidrs: |
| 82 |
device.route.add_route(cidr, via=via, metric=100, proto='static') |
| 83 |
|
| 84 |
def delete_routes(self, namespace, cidrs, via): |
| 85 |
device = ip_lib.IPDevice(None, namespace=namespace) |
| 86 |
for cidr in cidrs: |
| 87 |
device.route.delete_route(cidr, via=via, metric=100, |
| 88 |
proto='static') |
| 89 |
|
| 90 |
def list_routes(self, namespace, via=None): |
| 91 |
device = ip_lib.IPDevice(None, namespace=namespace) |
| 92 |
return device.route.list_routes( |
| 93 |
lib_constants.IP_VERSION_4, proto='static', via=via) |
| 94 |
|
| 95 |
def del_static_routes(self, namespace): |
| 96 |
device = ip_lib.IPDevice(None, namespace=namespace) |
| 97 |
routes = device.route.list_routes( |
| 98 |
lib_constants.IP_VERSION_4, proto='static') |
| 99 |
|
| 100 |
for r in routes: |
| 101 |
device.route.delete_route(r['cidr'], via=r['via']) |
| 102 |
|
| 103 |
def _del_port(self, process_id, ptype): |
| 104 |
namespace = self.get_namespace_name(process_id) |
| 105 |
prefix = PORT_PREFIXES[ptype] |
| 106 |
device = ip_lib.IPDevice(None, namespace=namespace) |
| 107 |
ports = device.addr.list() |
| 108 |
for p in ports: |
| 109 |
if not p['name'].startswith(prefix): |
| 110 |
continue |
| 111 |
interface_name = p['name'] |
| 112 |
self.driver.unplug(interface_name, namespace=namespace) |
| 113 |
|
| 114 |
def del_internal_port(self, process_id): |
| 115 |
self._del_port(process_id, 'internal') |
| 116 |
|
| 117 |
def del_external_port(self, process_id): |
| 118 |
self._del_port(process_id, 'external') |
| 119 |
|
| 120 |
def setup_external(self, process_id, network_details): |
| 121 |
network = network_details["external_network"] |
| 122 |
vpn_port = network_details['gw_port'] |
| 123 |
ns_name = self.get_namespace_name(process_id) |
| 124 |
interface_name = self.get_interface_name(vpn_port, 'external') |
| 125 |
|
| 126 |
if not ip_lib.ensure_device_is_ready(interface_name, |
| 127 |
namespace=ns_name): |
| 128 |
try: |
| 129 |
self.driver.plug(network['id'], |
| 130 |
vpn_port['id'], |
| 131 |
interface_name, |
| 132 |
vpn_port['mac_address'], |
| 133 |
namespace=ns_name, |
| 134 |
mtu=network.get('mtu'), |
| 135 |
prefix=PORT_PREFIX_EXTERNAL) |
| 136 |
except Exception: |
| 137 |
LOG.exception('plug external port %s failed', vpn_port) |
| 138 |
return None |
| 139 |
|
| 140 |
ip_cidrs = [] |
| 141 |
subnets = [] |
| 142 |
for fixed_ip in vpn_port['fixed_ips']: |
| 143 |
subnet_id = fixed_ip['subnet_id'] |
| 144 |
subnet = self.plugin.get_subnet_info(subnet_id) |
| 145 |
net = netaddr.IPNetwork(subnet['cidr']) |
| 146 |
ip_cidr = '{}/{}'.format(fixed_ip['ip_address'], net.prefixlen) |
| 147 |
ip_cidrs.append(ip_cidr) |
| 148 |
subnets.append(subnet) |
| 149 |
self.driver.init_l3(interface_name, ip_cidrs, |
| 150 |
namespace=ns_name) |
| 151 |
for subnet in subnets: |
| 152 |
self.set_default_route(ns_name, subnet, interface_name) |
| 153 |
return interface_name |
| 154 |
|
| 155 |
def setup_internal(self, process_id, network_details): |
| 156 |
vpn_port = network_details["transit_port"] |
| 157 |
ns_name = self.get_namespace_name(process_id) |
| 158 |
interface_name = self.get_interface_name(vpn_port, 'internal') |
| 159 |
|
| 160 |
if not ip_lib.ensure_device_is_ready(interface_name, |
| 161 |
namespace=ns_name): |
| 162 |
try: |
| 163 |
self.driver.plug('', |
| 164 |
vpn_port['id'], |
| 165 |
interface_name, |
| 166 |
vpn_port['mac_address'], |
| 167 |
namespace=ns_name, |
| 168 |
prefix=PORT_PREFIX_INTERNAL) |
| 169 |
except Exception: |
| 170 |
LOG.exception('plug internal port %s failed', vpn_port['id']) |
| 171 |
return None |
| 172 |
|
| 173 |
ip_cidrs = [] |
| 174 |
for fixed_ip in vpn_port['fixed_ips']: |
| 175 |
ip_cidr = '{}/{}'.format(fixed_ip['ip_address'], 28) |
| 176 |
ip_cidrs.append(ip_cidr) |
| 177 |
self.driver.init_l3(interface_name, ip_cidrs, |
| 178 |
namespace=ns_name) |
| 179 |
return interface_name |
| 180 |
|
| 181 |
|
| 182 |
class NamespaceManager: |
| 183 |
def __init__(self, use_ipv6=False): |
| 184 |
self.ip_wrapper_root = ip_lib.IPWrapper() |
| 185 |
self.use_ipv6 = use_ipv6 |
| 186 |
|
| 187 |
def exists(self, name): |
| 188 |
return ip_lib.network_namespace_exists(name) |
| 189 |
|
| 190 |
def create(self, name): |
| 191 |
ip_wrapper = self.ip_wrapper_root.ensure_namespace(name) |
| 192 |
cmd = ['sysctl', '-w', 'net.ipv4.ip_forward=1'] |
| 193 |
ip_wrapper.netns.execute(cmd) |
| 194 |
if self.use_ipv6: |
| 195 |
cmd = ['sysctl', '-w', 'net.ipv6.conf.all.forwarding=1'] |
| 196 |
ip_wrapper.netns.execute(cmd) |
| 197 |
|
| 198 |
def delete(self, name): |
| 199 |
try: |
| 200 |
self.ip_wrapper_root.netns.delete(name) |
| 201 |
except RuntimeError: |
| 202 |
msg = 'Failed trying to delete namespace: %s' |
| 203 |
LOG.exception(msg, name) |
| 204 |
|
| 205 |
|
| 206 |
class OvnOpenSwanProcess(ipsec.OpenSwanProcess): |
| 207 |
pass |
| 208 |
|
| 209 |
|
| 210 |
class OvnStrongSwanProcess(strongswan_ipsec.StrongSwanProcess): |
| 211 |
pass |
| 212 |
|
| 213 |
|
| 214 |
class OvnLibreSwanProcess(libreswan_ipsec.LibreSwanProcess): |
| 215 |
pass |
| 216 |
|
| 217 |
|
| 218 |
class IPsecOvnDriverApi(ipsec.IPsecVpnDriverApi): |
| 219 |
def __init__(self, topic): |
| 220 |
super().__init__(topic) |
| 221 |
self.admin_ctx = nctx.get_admin_context_without_session() |
| 222 |
|
| 223 |
def get_vpn_transit_network_details(self, router_id): |
| 224 |
cctxt = self.client.prepare() |
| 225 |
return cctxt.call(self.admin_ctx, 'get_vpn_transit_network_details', |
| 226 |
router_id=router_id) |
| 227 |
|
| 228 |
def get_subnet_info(self, subnet_id): |
| 229 |
cctxt = self.client.prepare() |
| 230 |
return cctxt.call(self.admin_ctx, 'get_subnet_info', |
| 231 |
subnet_id=subnet_id) |
| 232 |
|
| 233 |
|
| 234 |
class OvnIPsecDriver(ipsec.IPsecDriver): |
| 235 |
|
| 236 |
def __init__(self, vpn_service, host): |
| 237 |
self.nsmgr = NamespaceManager() |
| 238 |
super().__init__(vpn_service, host) |
| 239 |
self.agent_rpc = IPsecOvnDriverApi(topics.IPSEC_DRIVER_TOPIC) |
| 240 |
self.devmgr = DeviceManager(self.conf, self.host, |
| 241 |
self.agent_rpc, self.context) |
| 242 |
|
| 243 |
get_router_based_iptables_manager = None |
| 244 |
|
| 245 |
def get_namespace(self, router_id): |
| 246 |
"""Get namespace for VPN services of router. |
| 247 |
|
| 248 |
:router_id: router_id |
| 249 |
:returns: namespace string. |
| 250 |
""" |
| 251 |
return self.devmgr.get_namespace_name(router_id) |
| 252 |
|
| 253 |
def _cleanup_namespace(self, router_id): |
| 254 |
ns_name = self.devmgr.get_namespace_name(router_id) |
| 255 |
if not self.nsmgr.exists(ns_name): |
| 256 |
return |
| 257 |
|
| 258 |
self.devmgr.del_internal_port(router_id) |
| 259 |
self.devmgr.del_external_port(router_id) |
| 260 |
self.nsmgr.delete(ns_name) |
| 261 |
|
| 262 |
def _ensure_namespace(self, router_id, network_details): |
| 263 |
ns_name = self.get_namespace(router_id) |
| 264 |
if not self.nsmgr.exists(ns_name): |
| 265 |
self.nsmgr.create(ns_name) |
| 266 |
|
| 267 |
# set up vpn external port on provider net |
| 268 |
self.devmgr.setup_external(router_id, network_details) |
| 269 |
|
| 270 |
# set up vpn internal port on transit net |
| 271 |
self.devmgr.setup_internal(router_id, network_details) |
| 272 |
|
| 273 |
return ns_name |
| 274 |
|
| 275 |
def destroy_process(self, process_id): |
| 276 |
LOG.info('process %s is destroyed', process_id) |
| 277 |
namespace = self.devmgr.get_namespace_name(process_id) |
| 278 |
|
| 279 |
# If the namespace exists but the process_id is not in the table |
| 280 |
# there may be an active swan process from a previous run of the agent |
| 281 |
# which does not have a process object in memory. |
| 282 |
# To be able to clean it up we need to create a dummy process object |
| 283 |
# here (without a vpnservice), so that destroy_process will stop |
| 284 |
# the swan. |
| 285 |
if self.nsmgr.exists(namespace) and process_id not in self.processes: |
| 286 |
self.ensure_process(process_id) |
| 287 |
super().destroy_process(process_id) |
| 288 |
self._cleanup_namespace(process_id) |
| 289 |
|
| 290 |
def create_router(self, router): |
| 291 |
pass |
| 292 |
|
| 293 |
def destroy_router(self, process_id): |
| 294 |
pass |
| 295 |
|
| 296 |
def _update_nat(self, vpnservice, func): |
| 297 |
pass |
| 298 |
|
| 299 |
def _update_route(self, vpnservice, network_details): |
| 300 |
router_id = vpnservice['router_id'] |
| 301 |
gateway_ip = network_details['transit_gateway_ip'] |
| 302 |
namespace = self.devmgr.get_namespace_name(router_id) |
| 303 |
|
| 304 |
old_local_cidrs = set() |
| 305 |
for route in self.devmgr.list_routes(namespace, via=gateway_ip): |
| 306 |
old_local_cidrs.add(route['cidr']) |
| 307 |
|
| 308 |
new_local_cidrs = set() |
| 309 |
for ipsec_site_conn in vpnservice['ipsec_site_connections']: |
| 310 |
new_local_cidrs.update(ipsec_site_conn['local_cidrs']) |
| 311 |
|
| 312 |
self.devmgr.delete_routes(namespace, |
| 313 |
old_local_cidrs - new_local_cidrs, |
| 314 |
gateway_ip) |
| 315 |
self.devmgr.add_routes(namespace, |
| 316 |
new_local_cidrs - old_local_cidrs, |
| 317 |
gateway_ip) |
| 318 |
|
| 319 |
def _sync_vpn_processes(self, vpnservices, sync_router_ids): |
| 320 |
# Ensure the ipsec process is enabled only for |
| 321 |
# - the vpn services which are not yet in self.processes |
| 322 |
# - vpn services whose router id is in 'sync_router_ids' |
| 323 |
for vpnservice in vpnservices: |
| 324 |
router_id = vpnservice['router_id'] |
| 325 |
if router_id not in self.processes or router_id in sync_router_ids: |
| 326 |
net_details = self.agent_rpc.get_vpn_transit_network_details( |
| 327 |
router_id) |
| 328 |
|
| 329 |
self._ensure_namespace(router_id, net_details) |
| 330 |
self._update_route(vpnservice, net_details) |
| 331 |
process = self.ensure_process(router_id, vpnservice=vpnservice) |
| 332 |
process.update() |
| 333 |
|
| 334 |
def _cleanup_stale_vpn_processes(self, vpn_router_ids): |
| 335 |
super()._cleanup_stale_vpn_processes(vpn_router_ids) |
| 336 |
# Look for additional namespaces on this node that we don't know |
| 337 |
# and that should be deleted |
| 338 |
for router_id in self.devmgr.get_existing_process_ids(): |
| 339 |
if router_id not in vpn_router_ids: |
| 340 |
self.destroy_process(router_id) |
| 341 |
|
| 342 |
@lockutils.synchronized('vpn-agent', 'neutron-') |
| 343 |
def vpnservice_removed_from_agent(self, context, router_id): |
| 344 |
# must run under the same lock as sync() |
| 345 |
self.destroy_process(router_id) |
| 346 |
|
| 347 |
def vpnservice_added_to_agent(self, context, router_ids): |
| 348 |
routers = [{'id': router_id} for router_id in router_ids] |
| 349 |
self.sync(context, routers) |
| 350 |
|
| 351 |
|
| 352 |
class OvnStrongSwanDriver(OvnIPsecDriver): |
| 353 |
def create_process(self, process_id, vpnservice, namespace): |
| 354 |
return OvnStrongSwanProcess( |
| 355 |
self.conf, |
| 356 |
process_id, |
| 357 |
vpnservice, |
| 358 |
namespace) |
| 359 |
|
| 360 |
|
| 361 |
class OvnOpenSwanDriver(OvnIPsecDriver): |
| 362 |
def create_process(self, process_id, vpnservice, namespace): |
| 363 |
return OvnOpenSwanProcess( |
| 364 |
self.conf, |
| 365 |
process_id, |
| 366 |
vpnservice, |
| 367 |
namespace) |
| 368 |
|
| 369 |
|
| 370 |
class OvnLibreSwanDriver(OvnIPsecDriver): |
| 371 |
def create_process(self, process_id, vpnservice, namespace): |
| 372 |
return OvnLibreSwanProcess( |
| 373 |
self.conf, |
| 374 |
process_id, |
| 375 |
vpnservice, |
| 376 |
namespace) |