| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
from neutron.agent.linux import iptables_manager |
| 17 |
from neutron.common import utils |
| 18 |
from neutron_lib.api.definitions import constants as api_const |
| 19 |
from neutron_lib import constants |
| 20 |
from neutron_lib.exceptions import firewall_v2 as fw_ext |
| 21 |
from oslo_log import log as logging |
| 22 |
|
| 23 |
from neutron_fwaas.services.firewall.service_drivers.agents.drivers import\ |
| 24 |
conntrack_base |
| 25 |
from neutron_fwaas.services.firewall.service_drivers.agents.drivers import\ |
| 26 |
fwaas_base_v2 |
| 27 |
|
| 28 |
LOG = logging.getLogger(__name__) |
| 29 |
FWAAS_DRIVER_NAME = 'Fwaas iptables driver' |
| 30 |
FWAAS_DEFAULT_CHAIN = 'fwaas-default-policy' |
| 31 |
|
| 32 |
|
| 33 |
ACCEPTED_CHAIN = 'accepted' |
| 34 |
DROPPED_CHAIN = 'dropped' |
| 35 |
REJECTED_CHAIN = 'rejected' |
| 36 |
|
| 37 |
FWAAS_TO_IPTABLE_ACTION_MAP = { |
| 38 |
api_const.FWAAS_ALLOW: ACCEPTED_CHAIN, |
| 39 |
api_const.FWAAS_DENY: DROPPED_CHAIN, |
| 40 |
api_const.FWAAS_REJECT: REJECTED_CHAIN |
| 41 |
} |
| 42 |
|
| 43 |
CHAIN_NAME_PREFIX = {constants.INGRESS_DIRECTION: 'i', |
| 44 |
constants.EGRESS_DIRECTION: 'o'} |
| 45 |
|
| 46 |
""" Firewall rules are applied on internal-interfaces of Neutron router. |
| 47 |
The packets ingressing tenant's network will be on the output |
| 48 |
direction on internal-interfaces. |
| 49 |
""" |
| 50 |
IPTABLES_DIR = {constants.INGRESS_DIRECTION: '-o', |
| 51 |
constants.EGRESS_DIRECTION: '-i'} |
| 52 |
IPV4 = 'ipv4' |
| 53 |
IPV6 = 'ipv6' |
| 54 |
IP_VER_TAG = {IPV4: 'v4', |
| 55 |
IPV6: 'v6'} |
| 56 |
|
| 57 |
INTERNAL_DEV_PREFIX = 'qr-' |
| 58 |
SNAT_INT_DEV_PREFIX = 'sg-' |
| 59 |
ROUTER_2_FIP_DEV_PREFIX = 'rfp-' |
| 60 |
|
| 61 |
MAX_INTF_NAME_LEN = 14 |
| 62 |
|
| 63 |
|
| 64 |
class IptablesFwaasDriver(fwaas_base_v2.FwaasDriverBase): |
| 65 |
"""IPTables driver for Firewall As A Service.""" |
| 66 |
|
| 67 |
def __init__(self): |
| 68 |
LOG.debug("Initializing fwaas iptables driver") |
| 69 |
self.pre_firewall = None |
| 70 |
self.conntrack = conntrack_base.load_and_init_conntrack_driver() |
| 71 |
|
| 72 |
def _get_intf_name(self, if_prefix, port_id): |
| 73 |
_name = "{}{}".format(if_prefix, port_id) |
| 74 |
return _name[:MAX_INTF_NAME_LEN] |
| 75 |
|
| 76 |
def create_firewall_group(self, agent_mode, apply_list, firewall): |
| 77 |
LOG.debug('Creating firewall %(fw_id)s for tenant %(tid)s', |
| 78 |
{'fw_id': firewall['id'], 'tid': firewall['tenant_id']}) |
| 79 |
try: |
| 80 |
if firewall['admin_state_up']: |
| 81 |
self._setup_firewall(agent_mode, apply_list, firewall) |
| 82 |
self._remove_conntrack_new_firewall(agent_mode, |
| 83 |
apply_list, firewall) |
| 84 |
self.pre_firewall = dict(firewall) |
| 85 |
else: |
| 86 |
self.apply_default_policy(agent_mode, apply_list, firewall) |
| 87 |
except (LookupError, RuntimeError): |
| 88 |
|
| 89 |
LOG.exception("Failed to create firewall: %s", firewall['id']) |
| 90 |
raise fw_ext.FirewallInternalDriverError(driver=FWAAS_DRIVER_NAME) |
| 91 |
|
| 92 |
def _get_ipt_mgrs_with_if_prefix(self, agent_mode, ri): |
| 93 |
"""Gets the iptables manager along with the if prefix to apply rules. |
| 94 |
|
| 95 |
With DVR we can have differing namespaces depending on which agent |
| 96 |
(on Network or Compute node). Also, there is an associated i/f for |
| 97 |
each namespace. The iptables on the relevant namespace and matching |
| 98 |
i/f are provided. On the Network node we could have both the snat |
| 99 |
namespace and a fip so this is provided back as a list - so in that |
| 100 |
scenario rules can be applied on both. |
| 101 |
""" |
| 102 |
if not ri.router.get('distributed'): |
| 103 |
return [{'ipt': ri.iptables_manager, |
| 104 |
'if_prefix': INTERNAL_DEV_PREFIX}] |
| 105 |
ipt_mgrs = [] |
| 106 |
|
| 107 |
if agent_mode == constants.L3_AGENT_MODE_DVR_SNAT: |
| 108 |
if ri.snat_iptables_manager: |
| 109 |
ipt_mgrs.append({'ipt': ri.snat_iptables_manager, |
| 110 |
'if_prefix': SNAT_INT_DEV_PREFIX}) |
| 111 |
if ri.rtr_fip_connect: |
| 112 |
|
| 113 |
ipt_mgrs.append({'ipt': ri.iptables_manager, |
| 114 |
'if_prefix': ROUTER_2_FIP_DEV_PREFIX}) |
| 115 |
return ipt_mgrs |
| 116 |
|
| 117 |
def delete_firewall_group(self, agent_mode, apply_list, firewall): |
| 118 |
LOG.debug('Deleting firewall %(fw_id)s for tenant %(tid)s', |
| 119 |
{'fw_id': firewall['id'], 'tid': firewall['tenant_id']}) |
| 120 |
fwid = firewall['id'] |
| 121 |
try: |
| 122 |
for ri, router_fw_ports in apply_list: |
| 123 |
ipt_if_prefix_list = self._get_ipt_mgrs_with_if_prefix( |
| 124 |
agent_mode, ri) |
| 125 |
for ipt_if_prefix in ipt_if_prefix_list: |
| 126 |
ipt_mgr = ipt_if_prefix['ipt'] |
| 127 |
self._remove_chains(fwid, ipt_mgr) |
| 128 |
self._remove_default_chains(ipt_mgr) |
| 129 |
|
| 130 |
ipt_mgr.defer_apply_off() |
| 131 |
self.pre_firewall = None |
| 132 |
except (LookupError, RuntimeError): |
| 133 |
|
| 134 |
LOG.exception("Failed to delete firewall: %s", fwid) |
| 135 |
raise fw_ext.FirewallInternalDriverError(driver=FWAAS_DRIVER_NAME) |
| 136 |
|
| 137 |
def update_firewall_group(self, agent_mode, apply_list, firewall): |
| 138 |
LOG.debug('Updating firewall %(fw_id)s for tenant %(tid)s', |
| 139 |
{'fw_id': firewall['id'], 'tid': firewall['tenant_id']}) |
| 140 |
try: |
| 141 |
if firewall['admin_state_up']: |
| 142 |
self._setup_firewall(agent_mode, apply_list, firewall) |
| 143 |
if self.pre_firewall: |
| 144 |
self._remove_conntrack_updated_firewall( |
| 145 |
agent_mode, |
| 146 |
apply_list, self.pre_firewall, firewall) |
| 147 |
else: |
| 148 |
self._remove_conntrack_new_firewall(agent_mode, |
| 149 |
apply_list, firewall) |
| 150 |
else: |
| 151 |
self.apply_default_policy(agent_mode, apply_list, firewall) |
| 152 |
self.pre_firewall = dict(firewall) |
| 153 |
except (LookupError, RuntimeError): |
| 154 |
|
| 155 |
LOG.exception("Failed to update firewall: %s", firewall['id']) |
| 156 |
raise fw_ext.FirewallInternalDriverError(driver=FWAAS_DRIVER_NAME) |
| 157 |
|
| 158 |
def apply_default_policy(self, agent_mode, apply_list, firewall): |
| 159 |
LOG.debug('Applying firewall %(fw_id)s for tenant %(tid)s', |
| 160 |
{'fw_id': firewall['id'], 'tid': firewall['tenant_id']}) |
| 161 |
fwid = firewall['id'] |
| 162 |
try: |
| 163 |
for ri, router_fw_ports in apply_list: |
| 164 |
ipt_if_prefix_list = self._get_ipt_mgrs_with_if_prefix( |
| 165 |
agent_mode, ri) |
| 166 |
for ipt_if_prefix in ipt_if_prefix_list: |
| 167 |
|
| 168 |
ipt_mgr = ipt_if_prefix['ipt'] |
| 169 |
self._remove_chains(fwid, ipt_mgr) |
| 170 |
self._remove_default_chains(ipt_mgr) |
| 171 |
|
| 172 |
|
| 173 |
self._add_accepted_chain_v4v6(ipt_mgr) |
| 174 |
self._add_dropped_chain_v4v6(ipt_mgr) |
| 175 |
self._add_rejected_chain_v4v6(ipt_mgr) |
| 176 |
|
| 177 |
|
| 178 |
self._add_default_policy_chain_v4v6(ipt_mgr) |
| 179 |
self._enable_policy_chain(fwid, ipt_if_prefix, |
| 180 |
router_fw_ports) |
| 181 |
|
| 182 |
|
| 183 |
ipt_mgr.defer_apply_off() |
| 184 |
except (LookupError, RuntimeError): |
| 185 |
|
| 186 |
LOG.exception( |
| 187 |
"Failed to apply default policy on firewall: %s", fwid) |
| 188 |
raise fw_ext.FirewallInternalDriverError(driver=FWAAS_DRIVER_NAME) |
| 189 |
|
| 190 |
def _setup_firewall(self, agent_mode, apply_list, firewall): |
| 191 |
fwid = firewall['id'] |
| 192 |
for ri, router_fw_ports in apply_list: |
| 193 |
ipt_if_prefix_list = self._get_ipt_mgrs_with_if_prefix( |
| 194 |
agent_mode, ri) |
| 195 |
for ipt_if_prefix in ipt_if_prefix_list: |
| 196 |
ipt_mgr = ipt_if_prefix['ipt'] |
| 197 |
|
| 198 |
self._remove_chains(fwid, ipt_mgr) |
| 199 |
self._remove_default_chains(ipt_mgr) |
| 200 |
|
| 201 |
|
| 202 |
self._add_accepted_chain_v4v6(ipt_mgr) |
| 203 |
self._add_dropped_chain_v4v6(ipt_mgr) |
| 204 |
self._add_rejected_chain_v4v6(ipt_mgr) |
| 205 |
|
| 206 |
|
| 207 |
self._add_default_policy_chain_v4v6(ipt_mgr) |
| 208 |
|
| 209 |
self._setup_chains(firewall, ipt_if_prefix, router_fw_ports) |
| 210 |
|
| 211 |
|
| 212 |
ipt_mgr.defer_apply_off() |
| 213 |
|
| 214 |
def _get_chain_name(self, fwid, ver, direction): |
| 215 |
return '{}{}{}'.format(CHAIN_NAME_PREFIX[direction], |
| 216 |
IP_VER_TAG[ver], |
| 217 |
fwid) |
| 218 |
|
| 219 |
def _setup_chains(self, firewall, ipt_if_prefix, router_fw_ports): |
| 220 |
"""Create Fwaas chain using the rules in the policy |
| 221 |
""" |
| 222 |
egress_rule_list = firewall['egress_rule_list'] |
| 223 |
ingress_rule_list = firewall['ingress_rule_list'] |
| 224 |
fwid = firewall['id'] |
| 225 |
ipt_mgr = ipt_if_prefix['ipt'] |
| 226 |
|
| 227 |
|
| 228 |
invalid_rule = self._drop_invalid_packets_rule() |
| 229 |
est_rule = self._allow_established_rule() |
| 230 |
|
| 231 |
for ver in [IPV4, IPV6]: |
| 232 |
if ver == IPV4: |
| 233 |
table = ipt_mgr.ipv4['filter'] |
| 234 |
else: |
| 235 |
table = ipt_mgr.ipv6['filter'] |
| 236 |
ichain_name = self._get_chain_name( |
| 237 |
fwid, ver, constants.INGRESS_DIRECTION) |
| 238 |
ochain_name = self._get_chain_name( |
| 239 |
fwid, ver, constants.EGRESS_DIRECTION) |
| 240 |
for name in [ichain_name, ochain_name]: |
| 241 |
table.add_chain(name) |
| 242 |
table.add_rule(name, invalid_rule) |
| 243 |
table.add_rule(name, est_rule) |
| 244 |
|
| 245 |
for rule in ingress_rule_list: |
| 246 |
if not rule['enabled']: |
| 247 |
continue |
| 248 |
iptbl_rule = self._convert_fwaas_to_iptables_rule(rule) |
| 249 |
if rule['ip_version'] == constants.IP_VERSION_4: |
| 250 |
ver = IPV4 |
| 251 |
table = ipt_mgr.ipv4['filter'] |
| 252 |
else: |
| 253 |
ver = IPV6 |
| 254 |
table = ipt_mgr.ipv6['filter'] |
| 255 |
ichain_name = self._get_chain_name( |
| 256 |
fwid, ver, constants.INGRESS_DIRECTION) |
| 257 |
table.add_rule(ichain_name, iptbl_rule) |
| 258 |
|
| 259 |
for rule in egress_rule_list: |
| 260 |
if not rule['enabled']: |
| 261 |
continue |
| 262 |
iptbl_rule = self._convert_fwaas_to_iptables_rule(rule) |
| 263 |
if rule['ip_version'] == constants.IP_VERSION_4: |
| 264 |
ver = IPV4 |
| 265 |
table = ipt_mgr.ipv4['filter'] |
| 266 |
else: |
| 267 |
ver = IPV6 |
| 268 |
table = ipt_mgr.ipv6['filter'] |
| 269 |
ochain_name = self._get_chain_name( |
| 270 |
fwid, ver, constants.EGRESS_DIRECTION) |
| 271 |
table.add_rule(ochain_name, iptbl_rule) |
| 272 |
|
| 273 |
self._enable_policy_chain(fwid, ipt_if_prefix, router_fw_ports) |
| 274 |
|
| 275 |
def _find_changed_rules(self, pre_firewall, firewall): |
| 276 |
"""Find the rules changed between the current firewall |
| 277 |
and the updating rule |
| 278 |
""" |
| 279 |
changed_rules = [] |
| 280 |
for fw_rule_list in ['egress_rule_list', 'ingress_rule_list']: |
| 281 |
pre_fw_rules = pre_firewall[fw_rule_list] |
| 282 |
fw_rules = firewall[fw_rule_list] |
| 283 |
for pre_fw_rule in pre_fw_rules: |
| 284 |
for fw_rule in fw_rules: |
| 285 |
if (pre_fw_rule.get('id') == fw_rule.get('id') and |
| 286 |
pre_fw_rule != fw_rule): |
| 287 |
changed_rules.append(pre_fw_rule) |
| 288 |
changed_rules.append(fw_rule) |
| 289 |
return changed_rules |
| 290 |
|
| 291 |
def _find_removed_rules(self, pre_firewall, firewall): |
| 292 |
removed_rules = [] |
| 293 |
for fw_rule_list in ['egress_rule_list', 'ingress_rule_list']: |
| 294 |
pre_fw_rules = pre_firewall[fw_rule_list] |
| 295 |
fw_rules = firewall[fw_rule_list] |
| 296 |
fw_rule_ids = [fw_rule['id'] for fw_rule in fw_rules] |
| 297 |
removed_rules.extend([pre_fw_rule for pre_fw_rule in pre_fw_rules |
| 298 |
if pre_fw_rule['id'] not in fw_rule_ids]) |
| 299 |
return removed_rules |
| 300 |
|
| 301 |
def _find_new_rules(self, pre_firewall, firewall): |
| 302 |
return self._find_removed_rules(firewall, pre_firewall) |
| 303 |
|
| 304 |
def _remove_conntrack_new_firewall(self, agent_mode, apply_list, firewall): |
| 305 |
"""Remove conntrack when create new firewall""" |
| 306 |
routers_list = list({apply_info[0] for apply_info in apply_list}) |
| 307 |
for ri in routers_list: |
| 308 |
ipt_if_prefix_list = self._get_ipt_mgrs_with_if_prefix( |
| 309 |
agent_mode, ri) |
| 310 |
for ipt_if_prefix in ipt_if_prefix_list: |
| 311 |
ipt_mgr = ipt_if_prefix['ipt'] |
| 312 |
self.conntrack.flush_entries(ipt_mgr.namespace) |
| 313 |
|
| 314 |
def _remove_conntrack_updated_firewall(self, agent_mode, |
| 315 |
apply_list, pre_firewall, firewall): |
| 316 |
"""Remove conntrack when updated firewall""" |
| 317 |
routers_list = list({apply_info[0] for apply_info in apply_list}) |
| 318 |
for ri in routers_list: |
| 319 |
ipt_if_prefix_list = self._get_ipt_mgrs_with_if_prefix( |
| 320 |
agent_mode, ri) |
| 321 |
for ipt_if_prefix in ipt_if_prefix_list: |
| 322 |
ipt_mgr = ipt_if_prefix['ipt'] |
| 323 |
ch_rules = self._find_changed_rules(pre_firewall, |
| 324 |
firewall) |
| 325 |
i_rules = self._find_new_rules(pre_firewall, firewall) |
| 326 |
r_rules = self._find_removed_rules(pre_firewall, firewall) |
| 327 |
removed_conntrack_rules_list = ch_rules + i_rules + r_rules |
| 328 |
self.conntrack.delete_entries(removed_conntrack_rules_list, |
| 329 |
ipt_mgr.namespace) |
| 330 |
|
| 331 |
def _remove_default_chains(self, nsid): |
| 332 |
"""Remove fwaas default policy chain.""" |
| 333 |
self._remove_chain_by_name(IPV4, FWAAS_DEFAULT_CHAIN, nsid) |
| 334 |
self._remove_chain_by_name(IPV6, FWAAS_DEFAULT_CHAIN, nsid) |
| 335 |
|
| 336 |
def _remove_chains(self, fwid, ipt_mgr): |
| 337 |
"""Remove fwaas policy chain.""" |
| 338 |
for ver in [IPV4, IPV6]: |
| 339 |
for direction in [constants.INGRESS_DIRECTION, |
| 340 |
constants.EGRESS_DIRECTION]: |
| 341 |
chain_name = self._get_chain_name(fwid, ver, direction) |
| 342 |
self._remove_chain_by_name(ver, chain_name, ipt_mgr) |
| 343 |
|
| 344 |
def _add_default_policy_chain_v4v6(self, ipt_mgr): |
| 345 |
dropped_chain = self._get_action_chain(DROPPED_CHAIN) |
| 346 |
ipt_mgr.ipv4['filter'].add_chain(FWAAS_DEFAULT_CHAIN) |
| 347 |
ipt_mgr.ipv4['filter'].add_rule( |
| 348 |
FWAAS_DEFAULT_CHAIN, '-j %s' % dropped_chain) |
| 349 |
ipt_mgr.ipv6['filter'].add_chain(FWAAS_DEFAULT_CHAIN) |
| 350 |
ipt_mgr.ipv6['filter'].add_rule( |
| 351 |
FWAAS_DEFAULT_CHAIN, '-j %s' % dropped_chain) |
| 352 |
|
| 353 |
def _add_accepted_chain_v4v6(self, ipt_mgr): |
| 354 |
v4rules_in_chain = \ |
| 355 |
ipt_mgr.get_chain("filter", ACCEPTED_CHAIN, |
| 356 |
ip_version=constants.IP_VERSION_4) |
| 357 |
if not v4rules_in_chain: |
| 358 |
ipt_mgr.ipv4['filter'].add_chain(ACCEPTED_CHAIN) |
| 359 |
ipt_mgr.ipv4['filter'].add_rule(ACCEPTED_CHAIN, '-j ACCEPT') |
| 360 |
|
| 361 |
v6rules_in_chain = \ |
| 362 |
ipt_mgr.get_chain("filter", ACCEPTED_CHAIN, |
| 363 |
ip_version=constants.IP_VERSION_6) |
| 364 |
if not v6rules_in_chain: |
| 365 |
ipt_mgr.ipv6['filter'].add_chain(ACCEPTED_CHAIN) |
| 366 |
ipt_mgr.ipv6['filter'].add_rule(ACCEPTED_CHAIN, '-j ACCEPT') |
| 367 |
|
| 368 |
def _add_dropped_chain_v4v6(self, ipt_mgr): |
| 369 |
v4rules_in_chain = \ |
| 370 |
ipt_mgr.get_chain("filter", DROPPED_CHAIN, |
| 371 |
ip_version=constants.IP_VERSION_4) |
| 372 |
if not v4rules_in_chain: |
| 373 |
ipt_mgr.ipv4['filter'].add_chain(DROPPED_CHAIN) |
| 374 |
ipt_mgr.ipv4['filter'].add_rule(DROPPED_CHAIN, '-j DROP') |
| 375 |
|
| 376 |
v6rules_in_chain = \ |
| 377 |
ipt_mgr.get_chain("filter", DROPPED_CHAIN, |
| 378 |
ip_version=constants.IP_VERSION_6) |
| 379 |
if not v6rules_in_chain: |
| 380 |
ipt_mgr.ipv6['filter'].add_chain(DROPPED_CHAIN) |
| 381 |
ipt_mgr.ipv6['filter'].add_rule(DROPPED_CHAIN, '-j DROP') |
| 382 |
|
| 383 |
def _add_rejected_chain_v4v6(self, ipt_mgr): |
| 384 |
v4rules_in_chain = \ |
| 385 |
ipt_mgr.get_chain("filter", REJECTED_CHAIN, |
| 386 |
ip_version=constants.IP_VERSION_4) |
| 387 |
if not v4rules_in_chain: |
| 388 |
ipt_mgr.ipv4['filter'].add_chain(REJECTED_CHAIN) |
| 389 |
ipt_mgr.ipv4['filter'].add_rule( |
| 390 |
REJECTED_CHAIN, |
| 391 |
'-j REJECT --reject-with icmp-port-unreachable') |
| 392 |
|
| 393 |
v6rules_in_chain = \ |
| 394 |
ipt_mgr.get_chain("filter", REJECTED_CHAIN, |
| 395 |
ip_version=constants.IP_VERSION_6) |
| 396 |
if not v6rules_in_chain: |
| 397 |
ipt_mgr.ipv6['filter'].add_chain(REJECTED_CHAIN) |
| 398 |
ipt_mgr.ipv6['filter'].add_rule( |
| 399 |
REJECTED_CHAIN, |
| 400 |
'-j REJECT --reject-with icmp6-port-unreachable') |
| 401 |
|
| 402 |
def _remove_chain_by_name(self, ver, chain_name, ipt_mgr): |
| 403 |
if ver == IPV4: |
| 404 |
ipt_mgr.ipv4['filter'].remove_chain(chain_name) |
| 405 |
else: |
| 406 |
ipt_mgr.ipv6['filter'].remove_chain(chain_name) |
| 407 |
|
| 408 |
def _remove_chain_by_name_v4v6(self, chain_name, ipt_mgr): |
| 409 |
ipt_mgr.ipv4['filter'].remove_chain(chain_name) |
| 410 |
ipt_mgr.ipv6['filter'].remove_chain(chain_name) |
| 411 |
|
| 412 |
def _add_rules_to_chain(self, ipt_mgr, ver, chain_name, rules): |
| 413 |
if ver == IPV4: |
| 414 |
table = ipt_mgr.ipv4['filter'] |
| 415 |
else: |
| 416 |
table = ipt_mgr.ipv6['filter'] |
| 417 |
for rule in rules: |
| 418 |
table.add_rule(chain_name, rule) |
| 419 |
|
| 420 |
def _get_action_chain(self, name): |
| 421 |
binary_name = iptables_manager.binary_name |
| 422 |
chain_name = iptables_manager.get_chain_name(name) |
| 423 |
return '{}-{}'.format(binary_name, chain_name) |
| 424 |
|
| 425 |
def _enable_policy_chain(self, fwid, ipt_if_prefix, router_fw_ports): |
| 426 |
bname = iptables_manager.binary_name |
| 427 |
ipt_mgr = ipt_if_prefix['ipt'] |
| 428 |
if_prefix = ipt_if_prefix['if_prefix'] |
| 429 |
|
| 430 |
for (ver, tbl) in [(IPV4, ipt_mgr.ipv4['filter']), |
| 431 |
(IPV6, ipt_mgr.ipv6['filter'])]: |
| 432 |
for direction in [constants.INGRESS_DIRECTION, |
| 433 |
constants.EGRESS_DIRECTION]: |
| 434 |
chain_name = self._get_chain_name(fwid, ver, direction) |
| 435 |
chain_name = iptables_manager.get_chain_name(chain_name) |
| 436 |
if chain_name in tbl.chains: |
| 437 |
for router_fw_port in router_fw_ports: |
| 438 |
intf_name = self._get_intf_name(if_prefix, |
| 439 |
router_fw_port) |
| 440 |
jump_rule = ['{} {} -j {}-{}'.format( |
| 441 |
IPTABLES_DIR[direction], intf_name, |
| 442 |
bname, chain_name)] |
| 443 |
self._add_rules_to_chain(ipt_mgr, ver, |
| 444 |
'FORWARD', jump_rule) |
| 445 |
|
| 446 |
|
| 447 |
chain_name = iptables_manager.get_chain_name(FWAAS_DEFAULT_CHAIN) |
| 448 |
for router_fw_port in router_fw_ports: |
| 449 |
intf_name = self._get_intf_name(if_prefix, |
| 450 |
router_fw_port) |
| 451 |
jump_rule = ['-o {} -j {}-{}'.format(intf_name, bname, chain_name)] |
| 452 |
self._add_rules_to_chain(ipt_mgr, IPV4, 'FORWARD', jump_rule) |
| 453 |
self._add_rules_to_chain(ipt_mgr, IPV6, 'FORWARD', jump_rule) |
| 454 |
|
| 455 |
|
| 456 |
chain_name = iptables_manager.get_chain_name(FWAAS_DEFAULT_CHAIN) |
| 457 |
for router_fw_port in router_fw_ports: |
| 458 |
intf_name = self._get_intf_name(if_prefix, |
| 459 |
router_fw_port) |
| 460 |
jump_rule = ['-i {} -j {}-{}'.format(intf_name, bname, chain_name)] |
| 461 |
self._add_rules_to_chain(ipt_mgr, IPV4, 'FORWARD', jump_rule) |
| 462 |
self._add_rules_to_chain(ipt_mgr, IPV6, 'FORWARD', jump_rule) |
| 463 |
|
| 464 |
def _convert_fwaas_to_iptables_rule(self, rule): |
| 465 |
action = FWAAS_TO_IPTABLE_ACTION_MAP[rule.get('action')] |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
args = [] |
| 471 |
|
| 472 |
args += self._protocol_arg(rule.get('protocol'), |
| 473 |
rule.get('ip_version')) |
| 474 |
|
| 475 |
args += self._ip_prefix_arg('s', rule.get('source_ip_address')) |
| 476 |
args += self._ip_prefix_arg('d', rule.get('destination_ip_address')) |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
if (rule.get('source_port') is not None or |
| 481 |
rule.get('destination_port') is not None): |
| 482 |
args += self._match_arg(rule.get('protocol')) |
| 483 |
|
| 484 |
args += self._port_arg('sport', |
| 485 |
rule.get('protocol'), |
| 486 |
rule.get('source_port')) |
| 487 |
|
| 488 |
args += self._port_arg('dport', |
| 489 |
rule.get('protocol'), |
| 490 |
rule.get('destination_port')) |
| 491 |
|
| 492 |
args += self._action_arg(action) |
| 493 |
|
| 494 |
iptables_rule = ' '.join(args) |
| 495 |
return iptables_rule |
| 496 |
|
| 497 |
def _drop_invalid_packets_rule(self): |
| 498 |
dropped_chain = self._get_action_chain(DROPPED_CHAIN) |
| 499 |
return '-m state --state INVALID -j %s' % dropped_chain |
| 500 |
|
| 501 |
def _allow_established_rule(self): |
| 502 |
return '-m state --state RELATED,ESTABLISHED -j ACCEPT' |
| 503 |
|
| 504 |
def _action_arg(self, action): |
| 505 |
if not action: |
| 506 |
return [] |
| 507 |
|
| 508 |
args = ['-j', self._get_action_chain(action)] |
| 509 |
|
| 510 |
return args |
| 511 |
|
| 512 |
def _protocol_arg(self, protocol, ip_version): |
| 513 |
if not protocol: |
| 514 |
return [] |
| 515 |
|
| 516 |
if (protocol == constants.PROTO_NAME_ICMP and |
| 517 |
ip_version == constants.IP_VERSION_6): |
| 518 |
protocol = constants.PROTO_NAME_IPV6_ICMP |
| 519 |
|
| 520 |
args = ['-p', protocol] |
| 521 |
|
| 522 |
return args |
| 523 |
|
| 524 |
def _match_arg(self, protocol): |
| 525 |
if not protocol: |
| 526 |
return [] |
| 527 |
|
| 528 |
protocol_modules = {constants.PROTO_NAME_UDP: 'udp', |
| 529 |
constants.PROTO_NAME_TCP: 'tcp', |
| 530 |
constants.PROTO_NAME_ICMP: 'icmp', |
| 531 |
constants.PROTO_NAME_IPV6_ICMP: 'icmp6'} |
| 532 |
|
| 533 |
args = ['-m', protocol_modules[protocol]] |
| 534 |
|
| 535 |
return args |
| 536 |
|
| 537 |
def _port_arg(self, direction, protocol, port): |
| 538 |
if protocol not in [constants.PROTO_NAME_UDP, |
| 539 |
constants.PROTO_NAME_TCP] or port is None: |
| 540 |
return [] |
| 541 |
|
| 542 |
args = ['--%s' % direction, '%s' % port] |
| 543 |
|
| 544 |
return args |
| 545 |
|
| 546 |
def _ip_prefix_arg(self, direction, ip_prefix): |
| 547 |
|
| 548 |
if not ip_prefix: |
| 549 |
return [] |
| 550 |
|
| 551 |
args = ['-%s' % direction, '%s' % utils.ip_to_cidr(ip_prefix)] |
| 552 |
return args |