| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from oslo_concurrency import lockutils |
| 16 |
from oslo_config import cfg |
| 17 |
from oslo_log import log as logging |
| 18 |
|
| 19 |
from neutron.agent import securitygroups_rpc |
| 20 |
from neutron import manager |
| 21 |
from neutron.plugins.ml2.drivers.openvswitch.agent import vlanmanager |
| 22 |
from neutron_lib.agent import l2_extension |
| 23 |
from neutron_lib import constants as nl_const |
| 24 |
from neutron_lib.exceptions import firewall_v2 as f_exc |
| 25 |
from neutron_lib import rpc as n_rpc |
| 26 |
from neutron_lib.utils import net as nl_net |
| 27 |
|
| 28 |
from neutron_fwaas._i18n import _ |
| 29 |
from neutron_fwaas.common import fwaas_constants as consts |
| 30 |
from neutron_fwaas.services.firewall.service_drivers.agents import\ |
| 31 |
firewall_agent_api as api |
| 32 |
|
| 33 |
LOG = logging.getLogger(__name__) |
| 34 |
|
| 35 |
FWAAS_L2_DRIVER = 'neutron.agent.l2.firewall_drivers' |
| 36 |
SG_OVS_DRIVER = 'openvswitch' |
| 37 |
|
| 38 |
|
| 39 |
class FWaaSL2PluginApi(api.FWaaSPluginApiMixin): |
| 40 |
"""L2 agent side of FWaaS agent-to-plugin RPC API""" |
| 41 |
|
| 42 |
def get_firewall_group_for_port(self, context, port_id): |
| 43 |
"""Get firewall group is associated with a port""" |
| 44 |
|
| 45 |
LOG.debug("Get firewall group is associated with port %s", port_id) |
| 46 |
cctxt = self.client.prepare() |
| 47 |
return cctxt.call(context, 'get_firewall_group_for_port', |
| 48 |
port_id=port_id) |
| 49 |
|
| 50 |
def set_firewall_group_status(self, context, fwg_id, status, host): |
| 51 |
"""Set the status of a group operation.""" |
| 52 |
|
| 53 |
LOG.debug("Fetch firewall group changing status") |
| 54 |
cctxt = self.client.prepare() |
| 55 |
return cctxt.call(context, 'set_firewall_group_status', |
| 56 |
fwg_id=fwg_id, status=status, host=host) |
| 57 |
|
| 58 |
def firewall_group_deleted(self, context, fwg_id, host): |
| 59 |
"""Notifies the plugin that a firewall group has been deleted.""" |
| 60 |
|
| 61 |
LOG.debug("Notify to the plugin that firewall group has been deleted") |
| 62 |
cctxt = self.client.prepare() |
| 63 |
return cctxt.call(context, 'firewall_group_deleted', |
| 64 |
fwg_id=fwg_id, host=host) |
| 65 |
|
| 66 |
|
| 67 |
class FWaaSV2AgentExtension(l2_extension.L2AgentExtension): |
| 68 |
|
| 69 |
def initialize(self, connection, driver_type): |
| 70 |
"""Perform Agent Extension initialization""" |
| 71 |
|
| 72 |
self.conf = cfg.CONF |
| 73 |
self.vlan_manager = vlanmanager.LocalVlanManager() |
| 74 |
fw_l2_driver_cls = self._load_l2_driver_class(driver_type) |
| 75 |
sg_enabled = securitygroups_rpc.is_firewall_enabled() |
| 76 |
sg_firewall_driver = self.conf.SECURITYGROUP.firewall_driver |
| 77 |
sg_with_ovs = sg_enabled and (sg_firewall_driver == SG_OVS_DRIVER) |
| 78 |
self.driver = manager.NeutronManager.load_class_for_provider( |
| 79 |
FWAAS_L2_DRIVER, fw_l2_driver_cls)(self.agent_api, sg_with_ovs) |
| 80 |
self.plugin_rpc = FWaaSL2PluginApi( |
| 81 |
consts.FIREWALL_PLUGIN, self.conf.host) |
| 82 |
self.start_rpc_listeners() |
| 83 |
self.fwg_map = PortFirewallGroupMap() |
| 84 |
|
| 85 |
def consume_api(self, agent_api): |
| 86 |
self.agent_api = agent_api |
| 87 |
|
| 88 |
def start_rpc_listeners(self): |
| 89 |
self.conn = n_rpc.Connection() |
| 90 |
endpoints = [self] |
| 91 |
self.conn.create_consumer(consts.FW_AGENT, endpoints, fanout=False) |
| 92 |
return self.conn.consume_in_threads() |
| 93 |
|
| 94 |
def _load_l2_driver_class(self, driver_type): |
| 95 |
driver = self.conf.fwaas.firewall_l2_driver or 'noop' |
| 96 |
if driver == api.FW_L2_NOOP_DRIVER: |
| 97 |
return driver |
| 98 |
|
| 99 |
if driver != driver_type: |
| 100 |
raise Exception( |
| 101 |
_("Firewall l2 driver: %s is not compatible"), driver_type) |
| 102 |
return driver |
| 103 |
|
| 104 |
def _is_port_layer2(self, port): |
| 105 |
"""This function checks if a port belongs to a L2 case. |
| 106 |
|
| 107 |
Currently both DHCP and router ports are eliminated. |
| 108 |
""" |
| 109 |
|
| 110 |
return port and port.get('device_owner', '').startswith( |
| 111 |
nl_const.DEVICE_OWNER_COMPUTE_PREFIX) |
| 112 |
|
| 113 |
def _get_firewall_group_ports(self, fwg, host, to_delete=False): |
| 114 |
port_list = [] |
| 115 |
port_ids = fwg['del-port-ids'] if to_delete else fwg['add-port-ids'] |
| 116 |
|
| 117 |
LOG.debug("_get_fwg fwg=%(fwg)s ports=%(port)s to_delete=%(delete)s", |
| 118 |
{'fwg': fwg, 'port': port_ids, 'delete': to_delete}) |
| 119 |
for fw_port in port_ids: |
| 120 |
port_detail = fwg['port_details'].get(fw_port) |
| 121 |
if (self._is_port_layer2(port_detail) and |
| 122 |
port_detail.get('host') == host): |
| 123 |
port_list.append(port_detail) |
| 124 |
return port_list |
| 125 |
|
| 126 |
@staticmethod |
| 127 |
def _has_ports(fwg, event): |
| 128 |
"""Verifying fwg has ports or not |
| 129 |
|
| 130 |
This function verify applying firewall group on ports |
| 131 |
:param fwg: a fwg object |
| 132 |
:param event: create/update firewall group or |
| 133 |
create/update/delete port |
| 134 |
:return: True if applying firewall group is fine. Otherwise is False |
| 135 |
""" |
| 136 |
if event == consts.UPDATE_FWG and 'last-port' in fwg: |
| 137 |
return not fwg['last-port'] |
| 138 |
else: |
| 139 |
return bool(fwg['ports']) |
| 140 |
|
| 141 |
@staticmethod |
| 142 |
def _has_policy(fwg): |
| 143 |
"""Verifying fwg has policy or not""" |
| 144 |
return bool(fwg['ingress_firewall_policy_id'] or |
| 145 |
fwg['egress_firewall_policy_id']) |
| 146 |
|
| 147 |
def _compute_status(self, fwg, result, event=consts.CREATE_FWG): |
| 148 |
"""Compute a status of specified firewall group for update |
| 149 |
|
| 150 |
Validates 'ACTIVE', 'DOWN', 'INACTIVE', 'ERROR' and None as follows: |
| 151 |
- "ERROR" : if result is not True |
| 152 |
- "ACTIVE" : admin_state_up is True and exists ports |
| 153 |
- "INACTIVE" : admin_state_up is True and with no ports |
| 154 |
- "DOWN" : admin_state_up is False |
| 155 |
- None : In case of 'delete_firewall_group' |
| 156 |
""" |
| 157 |
if not result: |
| 158 |
return nl_const.ERROR |
| 159 |
|
| 160 |
if not fwg['admin_state_up']: |
| 161 |
return nl_const.DOWN |
| 162 |
|
| 163 |
if event == consts.DELETE_FWG: |
| 164 |
|
| 165 |
return |
| 166 |
|
| 167 |
if (self._has_ports(fwg, event) and self._has_policy(fwg)): |
| 168 |
return nl_const.ACTIVE |
| 169 |
|
| 170 |
return nl_const.INACTIVE |
| 171 |
|
| 172 |
def _get_network_and_segmentation_id(self, fwg_port): |
| 173 |
port_id = fwg_port.get('port_id', fwg_port.get('id')) |
| 174 |
port_details = fwg_port.get('port_details') |
| 175 |
|
| 176 |
if port_details: |
| 177 |
target = port_details.get(port_id) |
| 178 |
if target: |
| 179 |
return target.get('network_id'), target.get('segmentation_id') |
| 180 |
return |
| 181 |
|
| 182 |
return fwg_port.get('network_id'), fwg_port.get('segmentation_id') |
| 183 |
|
| 184 |
def _add_local_vlan_to_ports(self, fwg_ports): |
| 185 |
"""Add local VLAN to ports if found |
| 186 |
|
| 187 |
This function tries to add local VLAN related to ports. |
| 188 |
""" |
| 189 |
|
| 190 |
ports_with_lvlan = [] |
| 191 |
for fwg_port in fwg_ports: |
| 192 |
try: |
| 193 |
network_id, segm_id = self._get_network_and_segmentation_id( |
| 194 |
fwg_port) |
| 195 |
l_vlan = self.vlan_manager.get(network_id, segm_id).vlan |
| 196 |
fwg_port['lvlan'] = int(l_vlan) |
| 197 |
except vlanmanager.MappingNotFound: |
| 198 |
LOG.warning("No Local VLAN found in network %s", network_id) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
ports_with_lvlan.append(fwg_port) |
| 203 |
|
| 204 |
return ports_with_lvlan |
| 205 |
|
| 206 |
def _apply_fwg_rules(self, fwg, ports, event=consts.UPDATE_FWG): |
| 207 |
"""This function invokes the driver create/update routine. """ |
| 208 |
|
| 209 |
|
| 210 |
if event in [consts.CREATE_FWG, consts.UPDATE_FWG]: |
| 211 |
ports_for_driver = self._add_local_vlan_to_ports(ports) |
| 212 |
else: |
| 213 |
ports_for_driver = ports |
| 214 |
|
| 215 |
|
| 216 |
try: |
| 217 |
if event == consts.UPDATE_FWG: |
| 218 |
self.driver.update_firewall_group(ports_for_driver, fwg) |
| 219 |
elif event == consts.DELETE_FWG: |
| 220 |
self.driver.delete_firewall_group(ports_for_driver, fwg) |
| 221 |
elif event == consts.CREATE_FWG: |
| 222 |
self.driver.create_firewall_group(ports_for_driver, fwg) |
| 223 |
except f_exc.FirewallInternalDriverError: |
| 224 |
msg = _("FWaaS driver error in %(event)s_firewall_group " |
| 225 |
"for firewall group: %(fwg_id)s") |
| 226 |
LOG.exception(msg, {'event': event, 'fwg_id': fwg['id']}) |
| 227 |
return False |
| 228 |
return True |
| 229 |
|
| 230 |
def _send_fwg_status(self, context, fwg_id, status, host): |
| 231 |
"""Send firewall group's status to plugin. |
| 232 |
|
| 233 |
:returns: True if no exception occurred otherwise False |
| 234 |
:rtype: boolean |
| 235 |
""" |
| 236 |
try: |
| 237 |
self.plugin_rpc.set_firewall_group_status( |
| 238 |
context, fwg_id, status, host) |
| 239 |
LOG.debug("Successfully sent status(%s) for firewall_group(%s)", |
| 240 |
status, fwg_id) |
| 241 |
except Exception: |
| 242 |
msg = _("Failed to send status for firewall_group(%s)") |
| 243 |
LOG.exception(msg, fwg_id) |
| 244 |
|
| 245 |
def _create_firewall_group(self, context, fwg, host, |
| 246 |
event=consts.CREATE_FWG): |
| 247 |
"""Handles RPC from plugin to create a firewall group. """ |
| 248 |
|
| 249 |
add_ports = self._get_firewall_group_ports(fwg, host) |
| 250 |
if not add_ports: |
| 251 |
status = nl_const.INACTIVE |
| 252 |
else: |
| 253 |
ret = self._apply_fwg_rules(fwg, add_ports, event) |
| 254 |
|
| 255 |
|
| 256 |
for port in add_ports: |
| 257 |
self.fwg_map.remove_port(port) |
| 258 |
|
| 259 |
status = self._compute_status(fwg, ret, event) |
| 260 |
for port in add_ports: |
| 261 |
self.fwg_map.set_port_fwg(port, fwg) |
| 262 |
|
| 263 |
|
| 264 |
self._send_fwg_status(context, fwg['id'], status, host) |
| 265 |
|
| 266 |
def _delete_firewall_group(self, context, fwg, host, |
| 267 |
event=consts.DELETE_FWG): |
| 268 |
"""Handles RPC from plugin to delete a firewall group. """ |
| 269 |
|
| 270 |
del_ports = self._get_firewall_group_ports(fwg, host, to_delete=True) |
| 271 |
if not del_ports: |
| 272 |
return |
| 273 |
|
| 274 |
|
| 275 |
ret = self._apply_fwg_rules(fwg, del_ports, event=consts.DELETE_FWG) |
| 276 |
del_port_ids = [] |
| 277 |
for port in del_ports: |
| 278 |
del_port_ids.append(port['id']) |
| 279 |
self.fwg_map.remove_port(port) |
| 280 |
|
| 281 |
if event == consts.DELETE_FWG: |
| 282 |
self.fwg_map.remove_fwg(fwg) |
| 283 |
self.plugin_rpc.firewall_group_deleted( |
| 284 |
context, fwg['id'], host=self.conf.host) |
| 285 |
else: |
| 286 |
status = self._compute_status(fwg, ret, event) |
| 287 |
self._send_fwg_status(context, fwg['id'], status, self.conf.host) |
| 288 |
|
| 289 |
@lockutils.synchronized('fwg') |
| 290 |
def create_firewall_group(self, context, firewall_group, host): |
| 291 |
"""Handles create firewall group event""" |
| 292 |
|
| 293 |
|
| 294 |
host = cfg.CONF.host |
| 295 |
with self.driver.defer_apply(): |
| 296 |
try: |
| 297 |
self._create_firewall_group(context, firewall_group, host) |
| 298 |
except Exception as exc: |
| 299 |
LOG.exception( |
| 300 |
"Exception caught in create_firewall_group %s", exc) |
| 301 |
self._send_fwg_status(context, firewall_group['id'], |
| 302 |
status=nl_const.ERROR, host=host) |
| 303 |
|
| 304 |
@lockutils.synchronized('fwg') |
| 305 |
def delete_firewall_group(self, context, firewall_group, host): |
| 306 |
"""Handles delete firewall group event""" |
| 307 |
|
| 308 |
|
| 309 |
host = cfg.CONF.host |
| 310 |
with self.driver.defer_apply(): |
| 311 |
try: |
| 312 |
self._delete_firewall_group(context, firewall_group, host) |
| 313 |
except Exception as exc: |
| 314 |
LOG.exception( |
| 315 |
"Exception caught in delete_firewall_group %s", exc) |
| 316 |
self._send_fwg_status(context, firewall_group['id'], |
| 317 |
status=nl_const.ERROR, host=host) |
| 318 |
|
| 319 |
@lockutils.synchronized('fwg') |
| 320 |
def update_firewall_group(self, context, firewall_group, host): |
| 321 |
"""Handles update firewall group event""" |
| 322 |
|
| 323 |
|
| 324 |
host = cfg.CONF.host |
| 325 |
with self.driver.defer_apply(): |
| 326 |
try: |
| 327 |
self._delete_firewall_group( |
| 328 |
context, firewall_group, host, event=consts.UPDATE_FWG) |
| 329 |
self._create_firewall_group( |
| 330 |
context, firewall_group, host, event=consts.UPDATE_FWG) |
| 331 |
except Exception as exc: |
| 332 |
LOG.exception( |
| 333 |
"Exception caught in update_firewall_group %s", exc) |
| 334 |
self._send_fwg_status(context, firewall_group['id'], |
| 335 |
status=nl_const.ERROR, host=host) |
| 336 |
|
| 337 |
@lockutils.synchronized('fwg-port') |
| 338 |
def handle_port(self, context, port): |
| 339 |
"""Handle port update event""" |
| 340 |
|
| 341 |
|
| 342 |
if nl_net.is_port_trusted(port) and not self.fwg_map.get_port(port): |
| 343 |
self._add_rule_for_trusted_port(port) |
| 344 |
self.fwg_map.set_port(port) |
| 345 |
return |
| 346 |
|
| 347 |
if not self._is_port_layer2(port): |
| 348 |
return |
| 349 |
|
| 350 |
|
| 351 |
if self.fwg_map.get_port_fwg(port): |
| 352 |
return |
| 353 |
|
| 354 |
fwg = self.plugin_rpc.get_firewall_group_for_port( |
| 355 |
context, port.get('port_id')) |
| 356 |
if not fwg: |
| 357 |
LOG.info("Firewall group applied to port %s is " |
| 358 |
"not available on server.", port['port_id']) |
| 359 |
return |
| 360 |
|
| 361 |
ret = self._apply_fwg_rules(fwg, [port]) |
| 362 |
status = self._compute_status(fwg, ret, event=consts.HANDLE_PORT) |
| 363 |
self.fwg_map.set_port_fwg(port, fwg) |
| 364 |
self._send_fwg_status( |
| 365 |
context, fwg_id=fwg['id'], status=status, host=self.conf.host) |
| 366 |
|
| 367 |
def _add_rule_for_trusted_port(self, port): |
| 368 |
self._add_local_vlan_to_ports([port]) |
| 369 |
self.driver.process_trusted_ports([port]) |
| 370 |
|
| 371 |
def _delete_rule_for_trusted_port(self, port): |
| 372 |
self.driver.remove_trusted_ports([port['port_id']]) |
| 373 |
|
| 374 |
def delete_port(self, context, port): |
| 375 |
"""This is being called when a port is deleted by the agent. """ |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
if 'vif_port' in port: |
| 381 |
return |
| 382 |
|
| 383 |
port = self.fwg_map.get_port(port) |
| 384 |
|
| 385 |
if port and nl_net.is_port_trusted(port): |
| 386 |
self._delete_rule_for_trusted_port(port) |
| 387 |
self.fwg_map.remove_port(port) |
| 388 |
return |
| 389 |
|
| 390 |
if not self._is_port_layer2(port): |
| 391 |
return |
| 392 |
|
| 393 |
fwg = self.fwg_map.get_port_fwg(port) |
| 394 |
if not fwg: |
| 395 |
LOG.info("Firewall group associated to port %(port_id)s is " |
| 396 |
"not available on server.", {'port_id': port['port_id']}) |
| 397 |
return |
| 398 |
|
| 399 |
ret = self._apply_fwg_rules(fwg, [port], event=consts.DELETE_FWG) |
| 400 |
|
| 401 |
port_id = self.fwg_map.port_id(port) |
| 402 |
if port_id in fwg['ports']: |
| 403 |
fwg['ports'].remove(port_id) |
| 404 |
|
| 405 |
|
| 406 |
self.fwg_map.set_fwg(fwg) |
| 407 |
self.fwg_map.remove_port(port) |
| 408 |
status = self._compute_status(fwg, ret, event=consts.DELETE_PORT) |
| 409 |
self._send_fwg_status(context, fwg['id'], status, self.conf.host) |
| 410 |
|
| 411 |
|
| 412 |
class PortFirewallGroupMap: |
| 413 |
"""Store relations between Port and Firewall Group and trusted port |
| 414 |
|
| 415 |
This map is used in deleting firewall_group because the firewall_group has |
| 416 |
been deleted at that time. Therefore, it is impossible to refer 'ports'. |
| 417 |
This map enables to refer 'ports' for specified firewall_group. |
| 418 |
Furthermore, it is necessary to check 'device_owner' for trusted port, this |
| 419 |
Map also stores trusted port data. |
| 420 |
""" |
| 421 |
def __init__(self): |
| 422 |
self.known_fwgs = {} |
| 423 |
self.port_fwg = {} |
| 424 |
self.port_detail = {} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
def port_id(self, port): |
| 429 |
return (port if isinstance(port, str) |
| 430 |
else port.get('port_id', port.get('id'))) |
| 431 |
|
| 432 |
def get_fwg(self, fwg_id): |
| 433 |
return self.known_fwgs.get(fwg_id) |
| 434 |
|
| 435 |
def set_fwg(self, fwg): |
| 436 |
self.known_fwgs[fwg['id']] = fwg |
| 437 |
|
| 438 |
def get_port(self, port): |
| 439 |
return self.port_detail.get(self.port_id(port)) |
| 440 |
|
| 441 |
def get_port_fwg(self, port): |
| 442 |
fwg_id = self.port_fwg.get(self.port_id(port)) |
| 443 |
if fwg_id: |
| 444 |
return self.get_fwg(fwg_id) |
| 445 |
|
| 446 |
def set_port(self, port): |
| 447 |
"""Add a new port into port_detail""" |
| 448 |
port_id = self.port_id(port) |
| 449 |
self.port_detail[port_id] = port |
| 450 |
|
| 451 |
def set_port_fwg(self, port, fwg): |
| 452 |
"""Add a new port into fwg['ports']""" |
| 453 |
port_id = self.port_id(port) |
| 454 |
|
| 455 |
fwg['ports'] = list(set(fwg['ports'] + [port_id])) |
| 456 |
|
| 457 |
self.known_fwgs[fwg['id']] = fwg |
| 458 |
|
| 459 |
self.port_detail[port_id] = port |
| 460 |
|
| 461 |
self.port_fwg[port_id] = fwg['id'] |
| 462 |
|
| 463 |
def remove_port(self, port): |
| 464 |
"""Remove port from fwg['ports'] and port_fwg dictionary |
| 465 |
|
| 466 |
When removing 'port' from several cases, the port should be removed |
| 467 |
from this map. |
| 468 |
""" |
| 469 |
port_id = self.port_id(port) |
| 470 |
|
| 471 |
|
| 472 |
if port_id in self.port_fwg: |
| 473 |
fwg_id = self.port_fwg.get(port_id) |
| 474 |
if not fwg_id: |
| 475 |
|
| 476 |
try: |
| 477 |
del self.port_detail[port_id] |
| 478 |
except KeyError: |
| 479 |
pass |
| 480 |
return |
| 481 |
new_fwg = self.known_fwgs[fwg_id] |
| 482 |
new_fwg['ports'] = [p for p in new_fwg['ports'] if p != port_id] |
| 483 |
self.known_fwgs[fwg_id] = new_fwg |
| 484 |
del self.port_fwg[port_id] |
| 485 |
del self.port_detail[port_id] |
| 486 |
|
| 487 |
def remove_fwg(self, fwg): |
| 488 |
"""Remove firewall_group from known_fwgs dictionary |
| 489 |
|
| 490 |
When removing firewall_group, it should be removed from this map |
| 491 |
""" |
| 492 |
if fwg['id'] in self.known_fwgs: |
| 493 |
del self.known_fwgs[fwg['id']] |