Debian Codegraph @

neutron_vpnaas/services/vpn/device_drivers/ipsec.py

raw
1 # Copyright 2013, Nachi Ueno, NTT I3, Inc.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 import abc
16 import base64
17 import copy
18 import filecmp
19 import os
20 import re
21 import shutil
22 import socket
23 import sys
24 import time
25 import typing as ty
26
27 import jinja2
28 import netaddr
29 from neutron.agent.l3.router_info import RouterInfo
30 from neutron.agent.linux import ip_lib
31 from neutron.agent.linux import utils as agent_utils
32 from neutron_lib.api import validators
33 from neutron_lib import constants
34 from neutron_lib import context
35 from neutron_lib.exceptions import vpn as vpn_exception
36 from neutron_lib.plugins import utils as plugin_utils
37 from neutron_lib import rpc as n_rpc
38 from neutron_lib.utils import file as file_utils
39 from oslo_concurrency import lockutils
40 from oslo_config import cfg
41 from oslo_log import helpers as log_helpers
42 from oslo_log import log as logging
43 import oslo_messaging
44 from oslo_service import loopingcall
45 from oslo_utils import encodeutils
46 from oslo_utils import fileutils
47
48 from neutron_vpnaas._i18n import _
49 from neutron_vpnaas.services.vpn.common import topics
50 from neutron_vpnaas.services.vpn import device_drivers
51
52 LOG = logging.getLogger(__name__)
53 TEMPLATE_PATH = os.path.dirname(os.path.abspath(__file__))
54
55 ipsec_opts = [
56 cfg.StrOpt(
57 'config_base_dir',
58 default='$state_path/ipsec',
59 help=_('Location to store ipsec server config files')),
60 cfg.IntOpt('ipsec_status_check_interval',
61 default=60,
62 help=_("Interval for checking ipsec status")),
63 cfg.BoolOpt('enable_detailed_logging',
64 default=False,
65 help=_("Enable detail logging for ipsec pluto process. "
66 "If the flag set to True, the detailed logging will "
67 "be written into config_base_dir/<pid>/log. "
68 "Note: This setting applies to OpenSwan and LibreSwan "
69 "only. StrongSwan logs to syslog.")),
70 ]
71 cfg.CONF.register_opts(ipsec_opts, 'ipsec')
72
73 openswan_opts = [
74 cfg.StrOpt(
75 'ipsec_config_template',
76 default=os.path.join(
77 TEMPLATE_PATH,
78 'template/openswan/ipsec.conf.template'),
79 help=_('Template file for ipsec configuration')),
80 cfg.StrOpt(
81 'ipsec_secret_template',
82 default=os.path.join(
83 TEMPLATE_PATH,
84 'template/openswan/ipsec.secret.template'),
85 help=_('Template file for ipsec secret configuration'))
86 ]
87
88 cfg.CONF.register_opts(openswan_opts, 'openswan')
89
90 pluto_opts = [
91 cfg.IntOpt('shutdown_check_timeout',
92 default=1,
93 help=_('Initial interval in seconds for checking if pluto '
94 'daemon is shutdown'),
95 deprecated_group='libreswan'),
96 cfg.IntOpt('shutdown_check_retries',
97 default=5,
98 help=_('The maximum number of retries for checking for '
99 'pluto daemon shutdown'),
100 deprecated_group='libreswan'),
101 cfg.FloatOpt('shutdown_check_back_off',
102 default=1.5,
103 help=_('A factor to increase the retry interval for '
104 'each retry'),
105 deprecated_group='libreswan'),
106 cfg.BoolOpt('restart_check_config',
107 default=False,
108 help=_('Enable this flag to avoid from unnecessary restart'),
109 deprecated_group='libreswan')
110 ]
111
112 cfg.CONF.register_opts(pluto_opts, 'pluto')
113
114 JINJA_ENV = None
115
116 IPSEC_CONNS = 'ipsec_site_connections'
117
118 # *Swan supports Base64 encoded binary values as PSKs. In such cases,
119 # a character sequence beginning with 0s is interpreted as Base64
120 # encoded binary data.
121 # - StrongSwan
122 # - https://wiki.strongswan.org/projects/strongswan/wiki/PskSecret
123 # - LibreSwan
124 # - https://libreswan.org/man/ipsec.secrets.5.html
125 # - https://libreswan.org/man/ipsec_ttodata.3.html
126 # - OpenSwan (no online documents, see manpages sources in the repository)
127 # - https://github.com/xelerance/Openswan
128 PSK_BASE64_PREFIX = '0s'
129
130
131 def _get_template(template_file):
132 global JINJA_ENV
133 if not JINJA_ENV:
134 templateLoader = jinja2.FileSystemLoader(searchpath="/")
135 JINJA_ENV = jinja2.Environment(loader=templateLoader, autoescape=True)
136 return JINJA_ENV.get_template(template_file)
137
138
139 class BaseSwanProcess(metaclass=abc.ABCMeta):
140 """Swan Family Process Manager
141
142 This class manages start/restart/stop ipsec process.
143 This class create/delete config template
144 """
145
146 binary = "ipsec"
147 CONFIG_DIRS = [
148 'var/run',
149 'log',
150 'etc',
151 'etc/ipsec.d/aacerts',
152 'etc/ipsec.d/acerts',
153 'etc/ipsec.d/cacerts',
154 'etc/ipsec.d/certs',
155 'etc/ipsec.d/crls',
156 'etc/ipsec.d/ocspcerts',
157 'etc/ipsec.d/policies',
158 'etc/ipsec.d/private',
159 'etc/ipsec.d/reqs',
160 'etc/pki/nssdb/'
161 ]
162
163 DIALECT_MAP = {
164 "3des": "3des",
165 "aes-128": "aes128",
166 "aes-192": "aes192",
167 "aes-256": "aes256",
168 "aes-ctr-128": "aesctr128",
169 "aes-ctr-192": "aesctr192",
170 "aes-ctr-256": "aesctr256",
171 "aes-xcbc": "aes_xcbc",
172 "sha256": "sha2_256",
173 "sha384": "sha2_384",
174 "sha512": "sha2_512",
175 "group2": "modp1024",
176 "group5": "modp1536",
177 "group14": "modp2048",
178 "group15": "modp3072",
179 "group16": "modp4096",
180 "group17": "modp6144",
181 "group18": "modp8192",
182 "group19": "ecp256",
183 "group20": "ecp384",
184 "group21": "ecp521",
185 "group22": "dh22",
186 "group23": "dh23",
187 "group24": "dh24",
188 "group31": "curve25519",
189 "bi-directional": "start",
190 "response-only": "add",
191 "v2": "insist",
192 "v1": "never"
193 }
194
195 NS_WRAPPER = 'neutron-vpn-netns-wrapper'
196
197 STATUS_DICT = {
198 'erouted': constants.ACTIVE,
199 'unrouted': constants.DOWN
200 }
201 STATUS_RE = r'\d\d\d "([a-f0-9\-]+).* (unrouted|erouted);'
202 STATUS_NOT_RUNNING_RE = 'Command:.*ipsec.*status.*Exit code: [1|3]$'
203 STATUS_IPSEC_SA_ESTABLISHED_RE = (
204 r'\d{3} #\d+: "([a-f0-9\-]+).*established.*newest')
205 STATUS_IPSEC_SA_ESTABLISHED_RE2 = (
206 r'\d{3} #\d+: "([a-f0-9\-\/x]+).*established.*newest')
207
208 def __init__(self, conf, process_id, vpnservice, namespace):
209 self.conf = conf
210 self.id = process_id
211 self.updated_pending_status = False
212 self.namespace = namespace
213 self.connection_status = {}
214 self.config_dir = os.path.join(
215 self.conf.ipsec.config_base_dir, self.id)
216 self.etc_dir = os.path.join(self.config_dir, 'etc')
217 self.log_dir = os.path.join(self.config_dir, 'log')
218 self.update_vpnservice(vpnservice)
219 self.STATUS_PATTERN = re.compile(self.STATUS_RE)
220 self.STATUS_NOT_RUNNING_PATTERN = re.compile(
221 self.STATUS_NOT_RUNNING_RE)
222 self.STATUS_IPSEC_SA_ESTABLISHED_PATTERN = re.compile(
223 self.STATUS_IPSEC_SA_ESTABLISHED_RE)
224 self.STATUS_IPSEC_SA_ESTABLISHED_PATTERN2 = re.compile(
225 self.STATUS_IPSEC_SA_ESTABLISHED_RE2)
226 self.STATUS_MAP = self.STATUS_DICT
227
228 def translate_dialect(self):
229 if not self.vpnservice:
230 return
231 for ipsec_site_conn in self.vpnservice['ipsec_site_connections']:
232 self._dialect(ipsec_site_conn, 'initiator')
233 self._dialect(ipsec_site_conn['ikepolicy'], 'ike_version')
234 for key in ['encryption_algorithm',
235 'auth_algorithm',
236 'pfs']:
237 self._dialect(ipsec_site_conn['ikepolicy'], key)
238 self._dialect(ipsec_site_conn['ipsecpolicy'], key)
239 if (('local_id' not in ipsec_site_conn.keys()) or
240 (not ipsec_site_conn['local_id'])):
241 ipsec_site_conn['local_id'] = ipsec_site_conn['external_ip']
242
243 def base64_encode_psk(self):
244 if not self.vpnservice:
245 return
246 for ipsec_site_conn in self.vpnservice['ipsec_site_connections']:
247 psk = ipsec_site_conn['psk']
248 encoded_psk = base64.b64encode(encodeutils.safe_encode(psk))
249 # NOTE(huntxu): base64.b64encode returns an instance of 'bytes'
250 # in Python 3, convert it to a str. For Python 2, after calling
251 # safe_decode, psk is converted into a unicode not containing any
252 # non-ASCII characters so it doesn't matter.
253 psk = encodeutils.safe_decode(encoded_psk, incoming='utf_8')
254 ipsec_site_conn['psk'] = PSK_BASE64_PREFIX + psk
255
256 def get_ns_wrapper(self):
257 """
258 Check if we're inside a virtualenv. If we are, then we should
259 respect this and launch wrapper from venv as well.
260 """
261 if (hasattr(sys, 'real_prefix') or
262 (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix)):
263 ns_wrapper = os.path.join(sys.prefix, "bin/", self.NS_WRAPPER)
264 else:
265 ns_wrapper = self.NS_WRAPPER
266 return ns_wrapper
267
268 def update_vpnservice(self, vpnservice):
269 self.vpnservice = vpnservice
270 self.translate_dialect()
271 self.base64_encode_psk()
272
273 def _dialect(self, obj, key):
274 obj[key] = self.DIALECT_MAP.get(obj[key], obj[key])
275
276 @abc.abstractmethod
277 def ensure_configs(self):
278 pass
279
280 def ensure_config_file(self, kind, template, vpnservice, file_mode=None):
281 """Update config file, based on current settings for service."""
282 config_str = self._gen_config_content(template, vpnservice)
283 config_file_name = self._get_config_filename(kind)
284 if file_mode is None:
285 file_utils.replace_file(config_file_name, config_str)
286 else:
287 file_utils.replace_file(config_file_name, config_str, file_mode)
288
289 def remove_config(self):
290 """Remove whole config file."""
291 agent_utils.execute(
292 cmd=["rm", "-rf", self.config_dir], run_as_root=True)
293
294 def _get_config_filename(self, kind):
295 config_dir = self.etc_dir
296 return os.path.join(config_dir, kind)
297
298 def ensure_config_dir(self, vpnservice):
299 """Create config directory if it does not exist."""
300 fileutils.ensure_tree(self.config_dir, 0o755)
301 for subdir in self.CONFIG_DIRS:
302 dir_path = os.path.join(self.config_dir, subdir)
303 fileutils.ensure_tree(dir_path, 0o755)
304
305 def _gen_config_content(self, template_file, vpnservice):
306 template = _get_template(template_file)
307 return template.render(
308 {'vpnservice': vpnservice,
309 'state_path': self.conf.state_path})
310
311 def _get_rootwrap_config(self):
312 if 'neutron-rootwrap' in cfg.CONF.AGENT.root_helper:
313 rh_tokens = cfg.CONF.AGENT.root_helper.split(' ')
314 if len(rh_tokens) == 3 and os.path.exists(rh_tokens[2]):
315 return rh_tokens[2]
316 return None
317
318 @abc.abstractmethod
319 def get_status(self):
320 pass
321
322 @property
323 def status(self):
324 if self.active:
325 return constants.ACTIVE
326 return constants.DOWN
327
328 @property
329 def active(self):
330 """Check if the process is active or not."""
331 if not self.namespace:
332 return False
333 try:
334 status = self.get_status()
335 self._extract_and_record_connection_status(status)
336 if not self.connection_status:
337 return False
338 except RuntimeError:
339 return False
340 return True
341
342 def update(self):
343 """Update Status based on vpnservice configuration."""
344
345 # Disable the process if a vpnservice is disabled or it has no
346 # enabled IPSec site connections.
347 vpnservice_has_active_ipsec_site_conns = any(
348 ipsec_site_conn['admin_state_up']
349 for ipsec_site_conn in self.vpnservice['ipsec_site_connections'])
350 if (not self.vpnservice['admin_state_up'] or
351 not vpnservice_has_active_ipsec_site_conns):
352 self.disable()
353 else:
354 self.enable()
355
356 if plugin_utils.in_pending_status(self.vpnservice['status']):
357 self.updated_pending_status = True
358
359 self.vpnservice['status'] = self.status
360 for ipsec_site_conn in self.vpnservice['ipsec_site_connections']:
361 if plugin_utils.in_pending_status(ipsec_site_conn['status']):
362 conn_id = ipsec_site_conn['id']
363 conn_status = self.connection_status.get(conn_id)
364 if not conn_status:
365 continue
366 conn_status['updated_pending_status'] = True
367 ipsec_site_conn['status'] = conn_status['status']
368
369 def enable(self):
370 """Enabling the process."""
371 try:
372 self.ensure_configs()
373 if self.active:
374 self.restart()
375 else:
376 self.start()
377 except RuntimeError:
378 LOG.exception(
379 "Failed to enable vpn process on router %s",
380 self.id)
381
382 def disable(self):
383 """Disabling the process."""
384 try:
385 if self.active:
386 self.stop()
387 self.remove_config()
388 except RuntimeError:
389 LOG.exception(
390 "Failed to disable vpn process on router %s",
391 self.id)
392
393 @abc.abstractmethod
394 def restart(self):
395 """Restart process."""
396
397 @abc.abstractmethod
398 def start(self):
399 """Start process."""
400
401 @abc.abstractmethod
402 def stop(self):
403 """Stop process."""
404
405 def _check_status_line(self, line):
406 """Parse a line and search for status information.
407
408 If a connection has an established Security Association,
409 it will be considered ACTIVE. Otherwise, even if a status
410 line shows that a connection is active, it will be marked
411 as DOWN-ed.
412 """
413
414 # pluto is not running so just exit
415 if self.STATUS_NOT_RUNNING_PATTERN.search(line):
416 self.connection_status = {}
417 raise StopIteration()
418
419 m = self.STATUS_IPSEC_SA_ESTABLISHED_PATTERN.search(line)
420 if m:
421 connection_id = m.group(1)
422 return connection_id, constants.ACTIVE
423 else:
424 m = self.STATUS_PATTERN.search(line)
425 if m:
426 connection_id = m.group(1)
427 return connection_id, constants.DOWN
428 return None, None
429
430 def _extract_and_record_connection_status(self, status_output):
431 if not status_output:
432 self.connection_status = {}
433 return
434 for line in status_output.split('\n'):
435 try:
436 conn_id, conn_status = self._check_status_line(line)
437 except StopIteration:
438 break
439 if conn_id:
440 self._record_connection_status(conn_id, conn_status)
441
442 def _record_connection_status(self, connection_id, status,
443 force_status_update=False):
444 conn_info = self.connection_status.get(connection_id)
445 if not conn_info:
446 self.connection_status[connection_id] = {
447 'status': status,
448 'updated_pending_status': force_status_update
449 }
450 else:
451 conn_info['status'] = status
452 if force_status_update:
453 conn_info['updated_pending_status'] = True
454
455
456 class OpenSwanProcess(BaseSwanProcess):
457 """OpenSwan Process manager class.
458
459 This process class uses three commands
460 (1) ipsec pluto: IPsec IKE keying daemon
461 (2) ipsec addconn: Adds new ipsec addconn
462 (3) ipsec whack: control interface for IPSEC keying daemon
463 """
464 # Derived classes can set IPSEC_CONF_NAT_TRAVERSAL to None to
465 # omit the nat_traversal line in ipsec.conf
466 IPSEC_CONF_NAT_TRAVERSAL = "yes"
467
468 def __init__(self, conf, process_id, vpnservice, namespace):
469 super().__init__(conf, process_id, vpnservice, namespace)
470 self.secrets_file = os.path.join(
471 self.etc_dir, 'ipsec.secrets')
472 self.config_file = os.path.join(
473 self.etc_dir, 'ipsec.conf')
474 self.pid_path = os.path.join(
475 self.config_dir, 'var', 'run', 'pluto')
476 self.pid_file = '%s.pid' % self.pid_path
477
478 def _execute(self, cmd, check_exit_code=True, extra_ok_codes=None):
479 """Execute command on namespace."""
480 ip_wrapper = ip_lib.IPWrapper(namespace=self.namespace)
481 return ip_wrapper.netns.execute(cmd, check_exit_code=check_exit_code,
482 extra_ok_codes=extra_ok_codes)
483
484 def ensure_configs(self):
485 """Generate config files which are needed for OpenSwan.
486
487 If there is no directory, this function will create
488 dirs.
489 """
490 self.ensure_config_dir(self.vpnservice)
491 self.ensure_config_file(
492 'ipsec.conf',
493 self.conf.openswan.ipsec_config_template,
494 self.vpnservice)
495 self.ensure_config_file(
496 'ipsec.secrets',
497 self.conf.openswan.ipsec_secret_template,
498 self.vpnservice,
499 0o600)
500
501 def _copy_configs(self):
502 if not cfg.CONF.pluto.restart_check_config:
503 return
504 config_file_name = self._get_config_filename('ipsec.conf')
505 if os.path.isfile(config_file_name):
506 shutil.copyfile(config_file_name, config_file_name + '.old')
507 config_file_name = self._get_config_filename('ipsec.secrets')
508 if os.path.isfile(config_file_name):
509 shutil.copyfile(config_file_name, config_file_name + '.old')
510 os.chmod(config_file_name + '.old', 0o600)
511
512 def _process_running(self):
513 """Checks if process is still running."""
514
515 # If no PID file, we assume the process is not running.
516 if not os.path.exists(self.pid_file):
517 return False
518
519 try:
520 # We take an ask-forgiveness-not-permission approach and rely
521 # on throwing to tell us something. If the pid file exists,
522 # delve into the process information and check if it matches
523 # our expected command line.
524 with open(self.pid_file) as f:
525 pid = f.readline().strip()
526 with open('/proc/%s/cmdline' % pid) as cmd_line_file:
527 cmd_line = cmd_line_file.readline()
528 if self.pid_path in cmd_line and 'pluto' in cmd_line:
529 # Okay the process is probably a pluto process
530 # and it contains the pid_path in the command
531 # line... could be a race. Log to error and return
532 # that it is *NOT* okay to clean up files. We are
533 # logging to error instead of debug because it
534 # indicates something bad has happened and this is
535 # valuable information for figuring it out.
536 LOG.error('Process %(pid)s exists with command '
537 'line %(cmd_line)s.',
538 {'pid': pid, 'cmd_line': cmd_line})
539 return True
540
541 except OSError as e:
542 # This is logged as "info" instead of error because it simply
543 # means that we couldn't find the files to check on them.
544 LOG.info('Unable to find control files on startup for '
545 'router %(router)s: %(msg)s',
546 {'router': self.id, 'msg': e})
547 return False
548
549 def _cleanup_control_files(self):
550 try:
551 ctl_file = '%s.ctl' % self.pid_path
552 LOG.debug('Removing %(pidfile)s and %(ctlfile)s',
553 {'pidfile': self.pid_file,
554 'ctlfile': ctl_file})
555
556 if os.path.exists(self.pid_file):
557 os.remove(self.pid_file)
558
559 if os.path.exists(ctl_file):
560 os.remove(ctl_file)
561
562 except OSError as e:
563 LOG.error('Unable to remove pluto control '
564 'files for router %(router)s. %(msg)s',
565 {'router': self.id, 'msg': e})
566
567 def get_status(self):
568 return self._execute([self.binary,
569 'whack',
570 '--ctlbase',
571 self.pid_path,
572 '--status'], extra_ok_codes=[1, 3])
573
574 def _config_changed(self):
575 secrets_file = os.path.join(
576 self.etc_dir, 'ipsec.secrets')
577 config_file = os.path.join(
578 self.etc_dir, 'ipsec.conf')
579
580 if not os.path.isfile(secrets_file + '.old'):
581 return True
582 if not os.path.isfile(config_file + '.old'):
583 return True
584
585 if not filecmp.cmp(secrets_file, secrets_file + '.old'):
586 return True
587 if not filecmp.cmp(config_file, config_file + '.old'):
588 return True
589
590 return False
591
592 def restart(self):
593 """Restart the process."""
594 if cfg.CONF.pluto.restart_check_config and not self._config_changed():
595 return
596 # stop() followed immediately by a start() runs the risk that the
597 # current pluto daemon has not had a chance to shutdown. We check
598 # the current process information to see if the daemon is still
599 # running and if so, wait a short interval and retry.
600 self.stop()
601 wait_interval = cfg.CONF.pluto.shutdown_check_timeout
602 for i in range(cfg.CONF.pluto.shutdown_check_retries):
603 if not self._process_running():
604 self._cleanup_control_files()
605 break
606 time.sleep(wait_interval)
607 wait_interval *= cfg.CONF.pluto.shutdown_check_back_off
608 else:
609 LOG.warning('Server appears to still be running, restart '
610 'of router %s may fail', self.id)
611 self.start()
612 return
613
614 def _resolve_fqdn(self, fqdn):
615 # The first addrinfo member from the list returned by
616 # socket.getaddrinfo is used for the address resolution.
617 # The code doesn't filter for ipv4 or ipv6 address.
618 try:
619 addrinfo = socket.getaddrinfo(fqdn, None)[0]
620 return addrinfo[-1][0]
621 except socket.gaierror:
622 LOG.exception("Peer address %s cannot be resolved", fqdn)
623
624 def _get_nexthop(self, address, connection_id):
625 # check if address is an ip address or fqdn
626 invalid_ip_address = validators.validate_ip_address(address)
627 if invalid_ip_address:
628 ip_addr = self._resolve_fqdn(address)
629 if not ip_addr:
630 self._record_connection_status(connection_id, constants.ERROR,
631 force_status_update=True)
632 raise vpn_exception.VPNPeerAddressNotResolved(
633 peer_address=address)
634 else:
635 ip_addr = address
636 routes = self._execute(['ip', 'route', 'get', ip_addr])
637 if routes.find('via') >= 0:
638 return routes.split(' ')[2]
639 return address
640
641 def _virtual_privates(self, vpnservice):
642 """Returns line of virtual_privates.
643
644 virtual_private contains the networks
645 that are allowed as subnet for the remote client.
646 """
647 virtual_privates = []
648 nets = []
649 for ipsec_site_conn in vpnservice['ipsec_site_connections']:
650 nets += ipsec_site_conn['local_cidrs']
651 nets += ipsec_site_conn['peer_cidrs']
652 for net in nets:
653 version = netaddr.IPNetwork(net).version
654 virtual_privates.append('%v{}:{}'.format(version, net))
655 virtual_privates.sort()
656 return ','.join(virtual_privates)
657
658 def _gen_config_content(self, template_file, vpnservice):
659 template = _get_template(template_file)
660 virtual_privates = self._virtual_privates(vpnservice)
661 return template.render(
662 {'vpnservice': vpnservice,
663 'virtual_privates': virtual_privates,
664 'nat_traversal': self.IPSEC_CONF_NAT_TRAVERSAL})
665
666 def start_pluto(self):
667 cmd = [self.binary,
668 'pluto',
669 '--ctlbase', self.pid_path,
670 '--ipsecdir', self.etc_dir,
671 '--use-netkey',
672 '--uniqueids',
673 '--nat_traversal',
674 '--secretsfile', self.secrets_file]
675
676 if self.conf.ipsec.enable_detailed_logging:
677 cmd += ['--perpeerlog', '--perpeerlogbase', self.log_dir]
678 self._execute(cmd)
679
680 def add_ipsec_connection(self, nexthop, conn_id):
681 self._execute([self.binary,
682 'addconn',
683 '--ctlbase', '%s.ctl' % self.pid_path,
684 '--defaultroutenexthop', nexthop,
685 '--config', self.config_file, conn_id
686 ])
687
688 def start_whack_listening(self):
689 #TODO(nati) fix this when openswan is fixed
690 #Due to openswan bug, this command always exit with 3
691 self._execute([self.binary,
692 'whack',
693 '--ctlbase', self.pid_path,
694 '--listen'
695 ], check_exit_code=False)
696
697 def shutdown_whack(self):
698 self._execute([self.binary,
699 'whack',
700 '--ctlbase', self.pid_path,
701 '--shutdown'
702 ])
703
704 def initiate_connection(self, conn_name):
705 self._execute([self.binary,
706 'whack',
707 '--ctlbase', self.pid_path,
708 '--name', conn_name,
709 '--asynchronous',
710 '--initiate'
711 ])
712
713 def terminate_connection(self, conn_name):
714 self._execute([self.binary,
715 'whack',
716 '--ctlbase', self.pid_path,
717 '--name', conn_name,
718 '--terminate'
719 ])
720
721 def start(self):
722 """Start the process.
723
724 Note: if there is not namespace yet,
725 just do nothing, and wait next event.
726 """
727 if not self.namespace:
728 return
729
730 # NOTE: The restart operation calls the parent's start() instead of
731 # this one to avoid having to special case the startup file check.
732 # If anything is added to this method that needs to run whenever
733 # a restart occurs, it should be either added to the restart()
734 # override or things refactored to special-case start() when
735 # called from restart().
736
737 # If, by any reason, ctl and pid files weren't cleaned up, pluto
738 # won't be able to rewrite them and will fail to start. So we check
739 # to see if the process is running and if not, attempt a cleanup.
740 # In either case we fall through to allow the pluto process to
741 # start or fail in the usual way.
742 if not self._process_running():
743 self._cleanup_control_files()
744
745 #start pluto IKE keying daemon
746 self.start_pluto()
747
748 #add connections
749 for ipsec_site_conn in self.vpnservice['ipsec_site_connections']:
750 # Don't add a connection if its admin state is down
751 if not ipsec_site_conn['admin_state_up']:
752 continue
753 nexthop = self._get_nexthop(ipsec_site_conn['peer_address'],
754 ipsec_site_conn['id'])
755 self.add_ipsec_connection(nexthop, ipsec_site_conn['id'])
756
757 #start whack ipsec keying daemon
758 self.start_whack_listening()
759
760 for ipsec_site_conn in self.vpnservice['ipsec_site_connections']:
761 if (not ipsec_site_conn['initiator'] == 'start' or
762 not ipsec_site_conn['admin_state_up']):
763 continue
764 #initiate ipsec connection
765 self.initiate_connection(ipsec_site_conn['id'])
766 self._copy_configs()
767
768 def get_established_connections(self):
769 connections = []
770 status_output = self.get_status()
771
772 if not status_output:
773 return connections
774
775 for line in status_output.split('\n'):
776 if self.STATUS_NOT_RUNNING_PATTERN.search(line):
777 return connections
778 m = self.STATUS_IPSEC_SA_ESTABLISHED_PATTERN2.search(line)
779 if m:
780 connection = m.group(1)
781 if connection in connections:
782 continue
783 connections.append(connection)
784 return connections
785
786 def disconnect(self):
787 if not self.namespace:
788 return
789 if not self.vpnservice:
790 return
791
792 connections = self.get_established_connections()
793 for conn_name in connections:
794 self.terminate_connection(conn_name)
795
796 def stop(self):
797 #Stop process using whack
798 #Note this will also stop pluto
799 self.disconnect()
800 self.shutdown_whack()
801 self.connection_status = {}
802
803
804 class IPsecVpnDriverApi:
805 """IPSecVpnDriver RPC api."""
806
807 @log_helpers.log_method_call
808 def __init__(self, topic):
809 target = oslo_messaging.Target(topic=topic, version='1.0')
810 self.client = n_rpc.get_client(target)
811
812 @log_helpers.log_method_call
813 def get_vpn_services_on_host(self, context, host):
814 """Get list of vpnservices.
815
816 The vpnservices including related ipsec_site_connection,
817 ikepolicy and ipsecpolicy on this host
818 """
819 cctxt = self.client.prepare()
820 return cctxt.call(context, 'get_vpn_services_on_host', host=host)
821
822 @log_helpers.log_method_call
823 def update_status(self, context, status):
824 """Update local status.
825
826 This method call updates status attribute of
827 VPNServices.
828 """
829 cctxt = self.client.prepare()
830 return cctxt.call(context, 'update_status', status=status)
831
832
833 class IPsecDriver(device_drivers.DeviceDriver, metaclass=abc.ABCMeta):
834 """VPN Device Driver for IPSec.
835
836 This class is designed for use with L3-agent now.
837 However this driver will be used with another agent in future
838 so the use of "Router" is kept minimal now.
839 Instead of router_id, we are using process_id in this code.
840 """
841
842 # history
843 # 1.0 Initial version
844 target = oslo_messaging.Target(version='1.0')
845
846 def __init__(self, vpn_service, host):
847 # TODO(pc_m) Replace vpn_service with config arg, once all driver
848 # implementations no longer need vpn_service.
849 self.conf = vpn_service.conf
850 self.host = host
851 self.conn = n_rpc.Connection()
852 self.context = context.get_admin_context_without_session()
853 self.topic = topics.IPSEC_AGENT_TOPIC
854 node_topic = '{}.{}'.format(self.topic, self.host)
855
856 self.processes = {}
857 self.routers: ty.Dict[str, ty.Any] = {}
858 self.process_status_cache = {}
859
860 self.endpoints = [self]
861 self.conn.create_consumer(node_topic, self.endpoints, fanout=False)
862 self.conn.consume_in_threads()
863 self.agent_rpc = IPsecVpnDriverApi(topics.IPSEC_DRIVER_TOPIC)
864 self.process_status_cache_check = loopingcall.FixedIntervalLoopingCall(
865 self.report_status, self.context)
866 self.process_status_cache_check.start(
867 interval=self.conf.ipsec.ipsec_status_check_interval)
868
869 def get_namespace(self, router_id):
870 """Get namespace of router.
871
872 :router_id: router_id
873 :returns: namespace string.
874 Note: If the router is a DVR, then the SNAT namespace will be
875 provided. If the router does not exist, return None.
876 """
877 router_info = self.routers.get(router_id)
878 if not router_info:
879 return
880 # For DVR, use SNAT namespace
881 # TODO(pcm): Use router object method to tell if DVR, when available
882 if router_info.router['distributed']:
883 return router_info.snat_namespace.name
884 return router_info.ns_name
885
886 def get_router_based_iptables_manager(self, ri):
887 """Returns router based iptables manager
888
889 In DVR routers the IPsec VPN service should run inside
890 the snat namespace. So the iptables manager used for
891 snat namespace is different from the iptables manager
892 used for the qr namespace in a non dvr based router.
893
894 This function will check the router type and then will
895 return the right iptables manager. If DVR enabled router
896 it will return the snat_iptables_manager otherwise it will
897 return the legacy iptables_manager.
898 """
899 # TODO(pcm): Use router object method to tell if DVR, when available
900 if ri.router['distributed']:
901 return ri.snat_iptables_manager
902 return ri.iptables_manager
903
904 def ensure_nat_rules(self, vpnservice):
905 """Ensure the required nat rules for ipsec exist in iptables.
906
907 :param vpnservice: vpnservices
908 """
909 LOG.debug("ensure_nat_rules called for router %s",
910 vpnservice['router_id'])
911 ri = self.routers.get(vpnservice['router_id'])
912 if not ri:
913 LOG.debug("No router info for router %s", vpnservice['router_id'])
914 return
915
916 iptables_manager = self.get_router_based_iptables_manager(ri)
917 # clear all existing rules first
918 LOG.debug("Clearing vpnaas tagged NAT rules for router %s",
919 ri.router_id)
920 iptables_manager.ipv4['nat'].clear_rules_by_tag('vpnaas')
921
922 for ipsec_site_connection in vpnservice['ipsec_site_connections']:
923 for local_cidr in ipsec_site_connection['local_cidrs']:
924 # This ipsec rule is not needed for ipv6.
925 if netaddr.IPNetwork(local_cidr).version == 6:
926 continue
927
928 for peer_cidr in ipsec_site_connection['peer_cidrs']:
929 LOG.debug("Adding an ipsec policy NAT rule"
930 "%s <-> %s to router id %s",
931 peer_cidr, local_cidr, vpnservice['router_id'])
932
933 iptables_manager.ipv4['nat'].add_rule(
934 'POSTROUTING',
935 '-s %s -d %s -m policy '
936 '--dir out --pol ipsec '
937 '-j ACCEPT ' % (local_cidr, peer_cidr),
938 top=True, tag='vpnaas')
939
940 LOG.debug("Applying iptables for router id %s",
941 vpnservice['router_id'])
942 iptables_manager.apply()
943
944 @log_helpers.log_method_call
945 def remove_nat_rules(self, router_id):
946 """Remove all our iptables rules in namespace.
947
948 :param router_id: router_id
949 """
950 router = self.routers.get(router_id)
951 if not router:
952 return
953 iptables_manager = self.get_router_based_iptables_manager(router)
954 iptables_manager.ipv4['nat'].clear_rules_by_tag('vpnaas')
955 iptables_manager.apply()
956
957 @log_helpers.log_method_call
958 def vpnservice_updated(self, context, **kwargs):
959 """Vpnservice updated rpc handler
960
961 VPN Service Driver will call this method
962 when vpnservices updated.
963 Then this method start sync with server.
964 """
965 router = kwargs.get('router', None)
966 self.sync(context, [router] if router else [])
967
968 @abc.abstractmethod
969 def create_process(self, process_id, vpnservice, namespace):
970 pass
971
972 def ensure_process(self, process_id, vpnservice=None):
973 """Ensuring process.
974
975 If the process doesn't exist, it will create process
976 and store it in self.process
977 """
978 process = self.processes.get(process_id)
979 if not process or not process.namespace:
980 namespace = self.get_namespace(process_id)
981 process = self.create_process(
982 process_id,
983 vpnservice,
984 namespace)
985 self.processes[process_id] = process
986 elif vpnservice:
987 process.update_vpnservice(vpnservice)
988 return process
989
990 def create_router(self, router_info: RouterInfo):
991 """Handling create router event.
992
993 Agent calls this method, when the process namespace is ready.
994 Note: process_id == router_id == vpnservice_id
995 """
996 process_id = router_info.router_id
997 self.routers[process_id] = router_info
998 if process_id in self.processes:
999 # In case of vpnservice is created
1000 # before router's namespace
1001 process = self.processes[process_id]
1002 self.ensure_nat_rules(process.vpnservice)
1003 # Don't run ipsec process for backup HA router
1004 if router_info.router['ha'] and router_info.ha_state == 'backup':
1005 return
1006 process.enable()
1007
1008 def destroy_process(self, process_id):
1009 """Destroy process.
1010
1011 Disable the process, remove the iptables rules, and remove the process
1012 manager for the processes that no longer are running vpn service.
1013 """
1014 if process_id in self.processes:
1015 process = self.processes[process_id]
1016 process.disable()
1017 vpnservice = process.vpnservice
1018 if vpnservice:
1019 self.remove_nat_rules(process_id)
1020 del self.processes[process_id]
1021
1022 def destroy_router(self, process_id):
1023 """Handling destroy_router event.
1024
1025 Agent calls this method, when the process namespace
1026 is deleted.
1027 """
1028 self.destroy_process(process_id)
1029 if process_id in self.routers:
1030 del self.routers[process_id]
1031
1032 def get_process_status_cache(self, process):
1033 if not self.process_status_cache.get(process.id):
1034 self.process_status_cache[process.id] = {
1035 'status': None,
1036 'id': process.vpnservice['id'],
1037 'updated_pending_status': False,
1038 'ipsec_site_connections': {}}
1039 return self.process_status_cache[process.id]
1040
1041 def is_status_updated(self, process, previous_status):
1042 if process.updated_pending_status:
1043 return True
1044 if process.status != previous_status['status']:
1045 return True
1046 if (process.connection_status !=
1047 previous_status['ipsec_site_connections']):
1048 return True
1049
1050 def unset_updated_pending_status(self, process):
1051 process.updated_pending_status = False
1052 for connection_status in process.connection_status.values():
1053 connection_status['updated_pending_status'] = False
1054
1055 def copy_process_status(self, process):
1056 return {
1057 'id': process.vpnservice['id'],
1058 'status': process.status,
1059 'updated_pending_status': process.updated_pending_status,
1060 'ipsec_site_connections': copy.deepcopy(process.connection_status)
1061 }
1062
1063 def update_downed_connections(self, process_id, new_status):
1064 """Update info to be reported, if connections just went down.
1065
1066 If there is no longer any information for a connection, because it
1067 has been removed (e.g. due to an admin down of VPN service or IPSec
1068 connection), but there was previous status information for the
1069 connection, mark the connection as down for reporting purposes.
1070 """
1071 if process_id in self.process_status_cache:
1072 for conn in self.process_status_cache[process_id][IPSEC_CONNS]:
1073 if conn not in new_status[IPSEC_CONNS]:
1074 new_status[IPSEC_CONNS][conn] = {
1075 'status': constants.DOWN,
1076 'updated_pending_status': True
1077 }
1078
1079 def should_be_reported(self, context, process):
1080 if (context.is_admin or
1081 process.vpnservice["tenant_id"] == context.tenant_id):
1082 return True
1083
1084 @log_helpers.log_method_call
1085 def report_status(self, context):
1086 status_changed_vpn_services = []
1087 for process_id, process in list(self.processes.items()):
1088 # NOTE(mnaser): It's not necessary to check status for processes
1089 # of a backup L3 agent
1090 router = self.routers.get(process_id)
1091 if (router and
1092 isinstance(router, RouterInfo) and
1093 router.router['ha'] and
1094 router.ha_state == 'backup'
1095 ):
1096 LOG.debug("%s router in backup state, skipping", process_id)
1097 continue
1098 if not self.should_be_reported(context, process):
1099 continue
1100 previous_status = self.get_process_status_cache(process)
1101 if self.is_status_updated(process, previous_status):
1102 new_status = self.copy_process_status(process)
1103 self.update_downed_connections(process.id, new_status)
1104 status_changed_vpn_services.append(new_status)
1105 self.process_status_cache[process.id] = (
1106 self.copy_process_status(process))
1107 # We need unset updated_pending status after it
1108 # is reported to the server side
1109 self.unset_updated_pending_status(process)
1110
1111 if status_changed_vpn_services:
1112 self.agent_rpc.update_status(
1113 context,
1114 status_changed_vpn_services)
1115
1116 @log_helpers.log_method_call
1117 @lockutils.synchronized('vpn-agent', 'neutron-')
1118 def sync(self, context, router_information: ty.List):
1119 """Sync status with server side.
1120
1121 :param context: context object for RPC call
1122 :param router_information: RouterInfo objects with updated state
1123
1124 There could be many failure cases should be
1125 considered including the followings.
1126 1) Agent class restarted
1127 2) Failure on process creation
1128 3) VpnService is deleted during agent down
1129 4) RPC failure
1130
1131 In order to handle these failure cases,
1132 This driver takes simple sync strategies.
1133 """
1134 vpnservices = self.agent_rpc.get_vpn_services_on_host(
1135 context, self.host)
1136 router_ids = [vpnservice['router_id'] for vpnservice in vpnservices]
1137 sync_router_ids = []
1138
1139 for ri in router_information:
1140 # Update our router_info with the updated one
1141 router_id = None
1142 if isinstance(ri, RouterInfo):
1143 if ri.router_id:
1144 router_id = ri.router_id
1145 self.routers[router_id] = ri
1146 elif isinstance(ri, dict):
1147 router_id = ri.get("id")
1148
1149 if router_id:
1150 sync_router_ids.append(router_id)
1151
1152 self._sync_vpn_processes(vpnservices, sync_router_ids)
1153 self._delete_vpn_processes(sync_router_ids, router_ids)
1154 self._cleanup_stale_vpn_processes(router_ids)
1155
1156 self.report_status(context)
1157
1158 def _sync_vpn_processes(self, vpnservices, sync_router_ids):
1159 # Ensure the ipsec process is enabled only for
1160 # - the vpn services which are not yet in self.processes
1161 # - vpn services whose router id is in 'sync_router_ids'
1162 for vpnservice in vpnservices:
1163 if vpnservice['router_id'] not in self.processes or (
1164 vpnservice['router_id'] in sync_router_ids):
1165 process = self.ensure_process(vpnservice['router_id'],
1166 vpnservice=vpnservice)
1167 self.ensure_nat_rules(vpnservice)
1168 ri = self.routers.get(vpnservice['router_id'])
1169 if not ri:
1170 continue
1171 # For HA router, spawn vpn process on master router
1172 # and terminate vpn process on backup router
1173 if ri.router['ha'] and ri.ha_state == 'backup':
1174 process.disable()
1175 else:
1176 process.update()
1177
1178 def _delete_vpn_processes(self, sync_router_ids, vpn_router_ids):
1179 # Delete any IPSec processes that are
1180 # associated with routers, but are not running the VPN service.
1181 for process_id in sync_router_ids:
1182 if process_id not in vpn_router_ids:
1183 self.destroy_process(process_id)
1184
1185 def _cleanup_stale_vpn_processes(self, vpn_router_ids):
1186 # Delete any IPSec processes running
1187 # VPN that do not have an associated router.
1188 process_ids = [pid for pid in self.processes
1189 if pid not in vpn_router_ids]
1190 for process_id in process_ids:
1191 self.destroy_process(process_id)
1192
1193
1194 class OpenSwanDriver(IPsecDriver):
1195 def create_process(self, process_id, vpnservice, namespace):
1196 return OpenSwanProcess(
1197 self.conf,
1198 process_id,
1199 vpnservice,
1200 namespace)