Debian Codegraph @

neutron_vpnaas/services/vpn/service_drivers/base_ipsec.py

raw
1 # Copyright 2015, 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
17 import netaddr
18 import oslo_messaging
19
20 from neutron.db.models import l3agent
21 from neutron.db.models import servicetype
22 from neutron_lib import constants as lib_constants
23 from neutron_lib.db import api as db_api
24 from neutron_lib.plugins import directory
25
26 from neutron_vpnaas.db.vpn import vpn_models
27 from neutron_vpnaas.services.vpn import service_drivers
28
29
30 IPSEC = 'ipsec'
31 BASE_IPSEC_VERSION = '1.0'
32
33
34 class IPsecVpnDriverCallBack:
35 """Callback for IPSecVpnDriver rpc."""
36
37 # history
38 # 1.0 Initial version
39
40 target = oslo_messaging.Target(version=BASE_IPSEC_VERSION)
41
42 def __init__(self, driver):
43 super().__init__()
44 self.driver = driver
45
46 def _get_agent_hosting_vpn_services(self, context, host):
47 plugin = directory.get_plugin()
48 agent = plugin._get_agent_by_type_and_host(
49 context, lib_constants.AGENT_TYPE_L3, host)
50 agent_conf = plugin.get_configuration_dict(agent)
51 # Retrieve the agent_mode to check if this is the
52 # right agent to deploy the vpn service. In the
53 # case of distributed the vpn service should reside
54 # only on a dvr_snat node.
55 agent_mode = agent_conf.get(
56 'agent_mode', lib_constants.L3_AGENT_MODE_LEGACY)
57 if (not agent.admin_state_up or
58 agent_mode == lib_constants.L3_AGENT_MODE_DVR):
59 return []
60 query = context.session.query(vpn_models.VPNService)
61 query = query.join(vpn_models.IPsecSiteConnection)
62 query = query.join(l3agent.RouterL3AgentBinding,
63 l3agent.RouterL3AgentBinding.router_id ==
64 vpn_models.VPNService.router_id)
65 query = query.join(
66 servicetype.ProviderResourceAssociation,
67 servicetype.ProviderResourceAssociation.resource_id ==
68 vpn_models.VPNService.id)
69 query = query.filter(
70 l3agent.RouterL3AgentBinding.l3_agent_id == agent.id)
71 query = query.filter(
72 servicetype.ProviderResourceAssociation.provider_name ==
73 self.driver.name)
74 return query
75
76 @db_api.CONTEXT_WRITER
77 def get_vpn_services_on_host(self, context, host=None):
78 """Returns the vpnservices on the host."""
79 vpnservices = self._get_agent_hosting_vpn_services(
80 context, host)
81 plugin = self.driver.service_plugin
82 local_cidr_map = plugin._build_local_subnet_cidr_map(context)
83 return [self.driver.make_vpnservice_dict(vpnservice, local_cidr_map)
84 for vpnservice in vpnservices]
85
86 def update_status(self, context, status):
87 """Update status of vpnservices."""
88 plugin = self.driver.service_plugin
89 plugin.update_status_by_agent(context, status)
90
91
92 class IPsecVpnAgentApi(service_drivers.BaseIPsecVpnAgentApi):
93 """Agent RPC API for IPsecVPNAgent."""
94
95 target = oslo_messaging.Target(version=BASE_IPSEC_VERSION)
96
97 # pylint: disable=useless-super-delegation
98 def __init__(self, topic, default_version, driver):
99 super().__init__(topic, default_version, driver)
100
101
102 class BaseIPsecVPNDriver(service_drivers.VpnDriver, metaclass=abc.ABCMeta):
103 """Base VPN Service Driver class."""
104
105 def __init__(self, service_plugin, validator=None):
106 super().__init__(service_plugin, validator)
107 self.create_rpc_conn()
108
109 @property
110 def service_type(self):
111 return IPSEC
112
113 @abc.abstractmethod
114 def create_rpc_conn(self):
115 pass
116
117 def create_ipsec_site_connection(self, context, ipsec_site_connection):
118 router_id = self.service_plugin.get_vpnservice_router_id(
119 context, ipsec_site_connection['vpnservice_id'])
120 self.agent_rpc.vpnservice_updated(context, router_id)
121
122 def update_ipsec_site_connection(
123 self, context, old_ipsec_site_connection, ipsec_site_connection):
124 router_id = self.service_plugin.get_vpnservice_router_id(
125 context, ipsec_site_connection['vpnservice_id'])
126 self.agent_rpc.vpnservice_updated(context, router_id)
127
128 def delete_ipsec_site_connection(self, context, ipsec_site_connection):
129 router_id = self.service_plugin.get_vpnservice_router_id(
130 context, ipsec_site_connection['vpnservice_id'])
131 self.agent_rpc.vpnservice_updated(context, router_id)
132
133 def create_ikepolicy(self, context, ikepolicy):
134 pass
135
136 def delete_ikepolicy(self, context, ikepolicy):
137 pass
138
139 def update_ikepolicy(self, context, old_ikepolicy, ikepolicy):
140 pass
141
142 def create_ipsecpolicy(self, context, ipsecpolicy):
143 pass
144
145 def delete_ipsecpolicy(self, context, ipsecpolicy):
146 pass
147
148 def update_ipsecpolicy(self, context, old_ipsec_policy, ipsecpolicy):
149 pass
150
151 def _get_gateway_ips(self, router):
152 """Obtain the IPv4 and/or IPv6 GW IP for the router.
153
154 If there are multiples, (arbitrarily) use the first one.
155 """
156 v4_ip = v6_ip = None
157 for fixed_ip in router.gw_port['fixed_ips']:
158 addr = fixed_ip['ip_address']
159 vers = netaddr.IPAddress(addr).version
160 if vers == 4:
161 if v4_ip is None:
162 v4_ip = addr
163 elif v6_ip is None:
164 v6_ip = addr
165 return v4_ip, v6_ip
166
167 @db_api.CONTEXT_WRITER
168 def create_vpnservice(self, context, vpnservice_dict):
169 """Get the gateway IP(s) and save for later use.
170
171 For the reference implementation, this side's tunnel IP (external_ip)
172 will be the router's GW IP. IPSec connections will use a GW IP of
173 the same version, as is used for the peer, so we will collect the
174 first IP for each version (if they exist) and save them.
175 """
176 vpnservice = self.service_plugin._get_vpnservice(context,
177 vpnservice_dict['id'])
178 v4_ip, v6_ip = self._get_gateway_ips(vpnservice.router)
179 vpnservice_dict['external_v4_ip'] = v4_ip
180 vpnservice_dict['external_v6_ip'] = v6_ip
181 self.service_plugin.set_external_tunnel_ips(context,
182 vpnservice_dict['id'],
183 v4_ip=v4_ip, v6_ip=v6_ip)
184
185 def update_vpnservice(self, context, old_vpnservice, vpnservice):
186 self.agent_rpc.vpnservice_updated(context, vpnservice['router_id'])
187
188 def delete_vpnservice(self, context, vpnservice):
189 self.agent_rpc.vpnservice_updated(context, vpnservice['router_id'])
190
191 def get_external_ip_based_on_peer(self, vpnservice, ipsec_site_con):
192 """Use service's external IP, based on peer IP version."""
193 vers = netaddr.IPAddress(ipsec_site_con['peer_address']).version
194 if vers == 4:
195 ip_to_use = vpnservice.external_v4_ip
196 else:
197 ip_to_use = vpnservice.external_v6_ip
198 # TODO(pcm): Add validator to check that connection's peer address has
199 # a version that is available in service table, so can fail early and
200 # don't need a check here.
201 return ip_to_use
202
203 def make_vpnservice_dict(self, vpnservice, local_cidr_map):
204 """Convert vpnservice information for vpn agent.
205
206 also converting parameter name for vpn agent driver
207 """
208 vpnservice_dict = dict(vpnservice)
209 # Populate tenant_id for RPC compat
210 vpnservice_dict['tenant_id'] = vpnservice_dict['project_id']
211 vpnservice_dict['ipsec_site_connections'] = []
212 if vpnservice.subnet:
213 vpnservice_dict['subnet'] = dict(vpnservice.subnet)
214 else:
215 vpnservice_dict['subnet'] = None
216 # NOTE: Following is used for rolling upgrades, where agent may be
217 # at version N, and server at N+1. We need to populate the subnet
218 # with (only) the CIDR from the first connection's local endpoint
219 # group.
220 subnet_cidr = None
221 # Not removing external_ip from vpnservice_dict, as some providers
222 # may be still using it from vpnservice_dict. Will use whichever IP
223 # is specified.
224 vpnservice_dict['external_ip'] = (
225 vpnservice.external_v4_ip or vpnservice.external_v6_ip)
226 for ipsec_site_connection in vpnservice.ipsec_site_connections:
227 ipsec_site_connection_dict = dict(ipsec_site_connection)
228 try:
229 netaddr.IPAddress(ipsec_site_connection_dict['peer_id'])
230 if ipsec_site_connection_dict['local_id']:
231 netaddr.IPAddress(ipsec_site_connection_dict['local_id'])
232 except netaddr.core.AddrFormatError:
233 ipsec_site_connection_dict['peer_id'] = (
234 '@' + ipsec_site_connection_dict['peer_id'])
235 if ipsec_site_connection_dict['local_id']:
236 ipsec_site_connection_dict['local_id'] = (
237 '@' + ipsec_site_connection_dict['local_id'])
238 ipsec_site_connection_dict['ikepolicy'] = dict(
239 ipsec_site_connection.ikepolicy)
240 ipsec_site_connection_dict['ipsecpolicy'] = dict(
241 ipsec_site_connection.ipsecpolicy)
242 vpnservice_dict['ipsec_site_connections'].append(
243 ipsec_site_connection_dict)
244 if vpnservice.subnet:
245 local_cidrs = [vpnservice.subnet.cidr]
246 peer_cidrs = [
247 peer_cidr.cidr
248 for peer_cidr in ipsec_site_connection.peer_cidrs]
249 else:
250 local_cidrs = [
251 local_cidr_map[ep.endpoint]
252 for ep in ipsec_site_connection.local_ep_group.endpoints]
253 peer_cidrs = [
254 ep.endpoint
255 for ep in ipsec_site_connection.peer_ep_group.endpoints]
256 if not subnet_cidr:
257 epg = ipsec_site_connection.local_ep_group
258 subnet_cidr = local_cidr_map[epg.endpoints[0].endpoint]
259 ipsec_site_connection_dict['peer_cidrs'] = peer_cidrs
260 ipsec_site_connection_dict['local_cidrs'] = local_cidrs
261 ipsec_site_connection_dict['local_ip_vers'] = netaddr.IPNetwork(
262 local_cidrs[0]).version
263 ipsec_site_connection_dict['external_ip'] = (
264 self.get_external_ip_based_on_peer(vpnservice,
265 ipsec_site_connection_dict))
266 if not vpnservice.subnet:
267 vpnservice_dict['subnet'] = {'cidr': subnet_cidr}
268
269 return vpnservice_dict