| 1 |
# Copyright (c) 2015 Canonical, 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 |
|
| 16 |
import os |
| 17 |
|
| 18 |
from oslo_config import cfg |
| 19 |
from oslo_log import log as logging |
| 20 |
|
| 21 |
from neutron.agent.linux import ip_lib |
| 22 |
from neutron.agent.linux import utils |
| 23 |
from neutron_lib import constants |
| 24 |
|
| 25 |
from neutron_vpnaas._i18n import _ |
| 26 |
from neutron_vpnaas.services.vpn.device_drivers import ipsec |
| 27 |
|
| 28 |
LOG = logging.getLogger(__name__) |
| 29 |
TEMPLATE_PATH = os.path.dirname(os.path.abspath(__file__)) |
| 30 |
|
| 31 |
strongswan_opts = [ |
| 32 |
cfg.StrOpt( |
| 33 |
'ipsec_config_template', |
| 34 |
default=os.path.join( |
| 35 |
TEMPLATE_PATH, |
| 36 |
'template/strongswan/ipsec.conf.template'), |
| 37 |
help=_('Template file for ipsec configuration.')), |
| 38 |
cfg.StrOpt( |
| 39 |
'strongswan_config_template', |
| 40 |
default=os.path.join( |
| 41 |
TEMPLATE_PATH, |
| 42 |
'template/strongswan/strongswan.conf.template'), |
| 43 |
help=_('Template file for strongswan configuration.')), |
| 44 |
cfg.StrOpt( |
| 45 |
'ipsec_secret_template', |
| 46 |
default=os.path.join( |
| 47 |
TEMPLATE_PATH, |
| 48 |
'template/strongswan/ipsec.secret.template'), |
| 49 |
help=_('Template file for ipsec secret configuration.')), |
| 50 |
cfg.StrOpt( |
| 51 |
'default_config_area', |
| 52 |
default=os.path.join( |
| 53 |
TEMPLATE_PATH, |
| 54 |
'/etc/strongswan.d'), |
| 55 |
help=_('The area where default StrongSwan configuration ' |
| 56 |
'files are located.')) |
| 57 |
] |
| 58 |
cfg.CONF.register_opts(strongswan_opts, 'strongswan') |
| 59 |
|
| 60 |
|
| 61 |
class StrongSwanProcess(ipsec.BaseSwanProcess): |
| 62 |
|
| 63 |
# ROUTED means route created. (only for auto=route mode) |
| 64 |
# CONNECTING means route created, connection tunnel is negotiating. |
| 65 |
# INSTALLED means route created, |
| 66 |
# also connection tunnel installed. (traffic can pass) |
| 67 |
|
| 68 |
DIALECT_MAP = dict(ipsec.BaseSwanProcess.DIALECT_MAP) |
| 69 |
|
| 70 |
STATUS_DICT = { |
| 71 |
'ROUTED': constants.DOWN, |
| 72 |
'CONNECTING': constants.DOWN, |
| 73 |
'INSTALLED': constants.ACTIVE |
| 74 |
} |
| 75 |
STATUS_RE = r'([a-f0-9\-]+).* (ROUTED|CONNECTING|INSTALLED)' |
| 76 |
STATUS_NOT_RUNNING_RE = 'Command:.*ipsec.*status.*Exit code: [1|3] ' |
| 77 |
|
| 78 |
def __init__(self, conf, process_id, vpnservice, namespace): |
| 79 |
dialect_map_update = { |
| 80 |
'v1': 'ikev1', |
| 81 |
'v2': 'ikev2', |
| 82 |
# ENCR_AES_CTR |
| 83 |
'aes-128-ctr': 'aes128ctr', |
| 84 |
'aes-192-ctr': 'aes192ctr', |
| 85 |
'aes-256-ctr': 'aes256ctr', |
| 86 |
# ENCR_AES_CCM_8 |
| 87 |
'aes-128-ccm-8': 'aes128ccm8', |
| 88 |
'aes-192-ccm-8': 'aes192ccm8', |
| 89 |
'aes-256-ccm-8': 'aes256ccm8', |
| 90 |
# ENCR_AES_CCM_12 |
| 91 |
'aes-128-ccm-12': 'aes128ccm12', |
| 92 |
'aes-192-ccm-12': 'aes192ccm12', |
| 93 |
'aes-256-ccm-12': 'aes256ccm12', |
| 94 |
# ENCR_AES_CCM_16 |
| 95 |
'aes-128-ccm-16': 'aes128ccm16', |
| 96 |
'aes-192-ccm-16': 'aes192ccm16', |
| 97 |
'aes-256-ccm-16': 'aes256ccm16', |
| 98 |
# ENCR_AES_GCM_8 |
| 99 |
'aes-128-gcm-8': 'aes128gcm8', |
| 100 |
'aes-192-gcm-8': 'aes192gcm8', |
| 101 |
'aes-256-gcm-8': 'aes256gcm8', |
| 102 |
# ENCR_AES_GCM_12 |
| 103 |
'aes-128-gcm-12': 'aes128gcm12', |
| 104 |
'aes-192-gcm-12': 'aes192gcm12', |
| 105 |
'aes-256-gcm-12': 'aes256gcm12', |
| 106 |
# ENCR_AES_GCM_16 |
| 107 |
'aes-128-gcm-16': 'aes128gcm16', |
| 108 |
'aes-192-gcm-16': 'aes192gcm16', |
| 109 |
'aes-256-gcm-16': 'aes256gcm16', |
| 110 |
# AUTH |
| 111 |
'sha256': 'sha256', |
| 112 |
'aes-xcbc': 'aesxcbc', |
| 113 |
'aes-cmac': 'aescmac', |
| 114 |
# PFS |
| 115 |
'group22': 'modp1024s160', |
| 116 |
'group23': 'modp2048s224', |
| 117 |
'group24': 'modp2048s256', |
| 118 |
'group25': 'ecp192', |
| 119 |
'group26': 'ecp224', |
| 120 |
'group27': 'ecp224bp', |
| 121 |
'group28': 'ecp256bp', |
| 122 |
'group29': 'ecp384bp', |
| 123 |
'group30': 'ecp512bp', |
| 124 |
} |
| 125 |
self.DIALECT_MAP.update(dialect_map_update) |
| 126 |
self._strongswan_piddir = self._get_strongswan_piddir() |
| 127 |
self._rootwrap_cfg = self._get_rootwrap_config() |
| 128 |
LOG.debug("strongswan piddir is '%s'", (self._strongswan_piddir)) |
| 129 |
super().__init__(conf, process_id, vpnservice, namespace) |
| 130 |
|
| 131 |
def _get_strongswan_piddir(self): |
| 132 |
return utils.execute( |
| 133 |
cmd=[self.binary, "--piddir"], run_as_root=True).strip() |
| 134 |
|
| 135 |
def _check_status_line(self, line): |
| 136 |
"""Parse a line and search for status information. |
| 137 |
|
| 138 |
If a given line contains status information for a connection, |
| 139 |
extract the status and mark the connection as ACTIVE or DOWN |
| 140 |
according to the STATUS_MAP. |
| 141 |
""" |
| 142 |
m = self.STATUS_PATTERN.search(line) |
| 143 |
if m: |
| 144 |
connection_id = m.group(1) |
| 145 |
status = self.STATUS_MAP[m.group(2)] |
| 146 |
return connection_id, status |
| 147 |
return None, None |
| 148 |
|
| 149 |
def _execute(self, cmd, check_exit_code=True, extra_ok_codes=None): |
| 150 |
"""Execute command on namespace. |
| 151 |
|
| 152 |
This execute is wrapped by namespace wrapper. |
| 153 |
The namespace wrapper will bind /etc/ and /var/run |
| 154 |
""" |
| 155 |
ip_wrapper = ip_lib.IPWrapper(namespace=self.namespace) |
| 156 |
ns_wrapper = self.get_ns_wrapper() |
| 157 |
return ip_wrapper.netns.execute( |
| 158 |
[ns_wrapper, |
| 159 |
'--mount_paths=/etc:{}/etc,{}:{}/var/run'.format( |
| 160 |
self.config_dir, self._strongswan_piddir, self.config_dir), |
| 161 |
('--rootwrap_config=%s' % self._rootwrap_cfg |
| 162 |
if self._rootwrap_cfg else ''), |
| 163 |
'--cmd=%s' % ','.join(cmd)], |
| 164 |
check_exit_code=check_exit_code, |
| 165 |
extra_ok_codes=extra_ok_codes) |
| 166 |
|
| 167 |
def copy_and_overwrite(self, from_path, to_path): |
| 168 |
# NOTE(toabctl): the agent may run as non-root user, so rm/copy as root |
| 169 |
if os.path.exists(to_path): |
| 170 |
utils.execute( |
| 171 |
cmd=["rm", "-rf", to_path], run_as_root=True) |
| 172 |
utils.execute( |
| 173 |
cmd=["cp", "-a", from_path, to_path], run_as_root=True) |
| 174 |
|
| 175 |
def ensure_configs(self): |
| 176 |
"""Generate config files which are needed for StrongSwan. |
| 177 |
|
| 178 |
If there is no directory, this function will create |
| 179 |
dirs. |
| 180 |
""" |
| 181 |
self.ensure_config_dir(self.vpnservice) |
| 182 |
self.ensure_config_file( |
| 183 |
'ipsec.conf', |
| 184 |
cfg.CONF.strongswan.ipsec_config_template, |
| 185 |
self.vpnservice) |
| 186 |
self.ensure_config_file( |
| 187 |
'strongswan.conf', |
| 188 |
cfg.CONF.strongswan.strongswan_config_template, |
| 189 |
self.vpnservice) |
| 190 |
self.ensure_config_file( |
| 191 |
'ipsec.secrets', |
| 192 |
cfg.CONF.strongswan.ipsec_secret_template, |
| 193 |
self.vpnservice, |
| 194 |
0o600) |
| 195 |
self.copy_and_overwrite(cfg.CONF.strongswan.default_config_area, |
| 196 |
self._get_config_filename('strongswan.d')) |
| 197 |
|
| 198 |
def get_status(self): |
| 199 |
return self._execute([self.binary, 'status'], |
| 200 |
extra_ok_codes=[1, 3]) |
| 201 |
|
| 202 |
def restart(self): |
| 203 |
"""Restart the process.""" |
| 204 |
self.reload_secrets() |
| 205 |
self.reload() |
| 206 |
|
| 207 |
def reload_secrets(self): |
| 208 |
"""Reload the ipsec.secrets file. |
| 209 |
|
| 210 |
Flushes and rereads all secrets defined in ipsec.secrets. This needs |
| 211 |
to be done each time when a new site connection is associated with |
| 212 |
a VPN service which already hosts a site connection - 'ipsec reload' |
| 213 |
does not reload the secrets and new connections will not authenticate |
| 214 |
properly. |
| 215 |
""" |
| 216 |
self._execute([self.binary, 'rereadsecrets']) |
| 217 |
|
| 218 |
def reload(self): |
| 219 |
"""Reload the process. |
| 220 |
|
| 221 |
Sends a USR1 signal to ipsec starter which in turn reloads the whole |
| 222 |
configuration on the running IKE daemon charon based on the actual |
| 223 |
ipsec.conf. Currently established connections are not affected by |
| 224 |
configuration changes. |
| 225 |
""" |
| 226 |
self._execute([self.binary, 'reload']) |
| 227 |
|
| 228 |
def start(self): |
| 229 |
"""Start the process for only auto=route mode now. |
| 230 |
|
| 231 |
Note: if there is no namespace yet, |
| 232 |
just do nothing, and wait next event. |
| 233 |
""" |
| 234 |
if not self.namespace: |
| 235 |
return |
| 236 |
self._execute([self.binary, 'start']) |
| 237 |
# initiate ipsec connection |
| 238 |
for ipsec_site_conn in self.vpnservice['ipsec_site_connections']: |
| 239 |
self._execute([self.binary, 'stroke', 'up-nb', |
| 240 |
ipsec_site_conn['id']]) |
| 241 |
|
| 242 |
def stop(self): |
| 243 |
self._execute([self.binary, 'stop']) |
| 244 |
self.connection_status = {} |
| 245 |
|
| 246 |
|
| 247 |
class StrongSwanDriver(ipsec.IPsecDriver): |
| 248 |
|
| 249 |
def create_process(self, process_id, vpnservice, namespace): |
| 250 |
return StrongSwanProcess( |
| 251 |
self.conf, |
| 252 |
process_id, |
| 253 |
vpnservice, |
| 254 |
namespace) |