Debian Codegraph @

neutron_fwaas/services/firewall/service_drivers/ovn/acl.py

raw symbols SCIP
1 # Copyright 2022 EasyStack, 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 from neutron.common.ovn import utils as ovn_utils
17 from neutron_lib.api.definitions import constants as api_const
18 from neutron_lib import constants as const
19
20 from neutron_fwaas._i18n import _
21 from neutron_fwaas.services.firewall.service_drivers.ovn import \
22 constants as ovn_const
23 from neutron_fwaas.services.firewall.service_drivers.ovn import \
24 exceptions as ovn_fw_exc
25
26
27 def acl_direction(direction, port_group=None):
28 if direction == const.INGRESS_DIRECTION:
29 portdir = 'inport'
30 else:
31 portdir = 'outport'
32 return '{} == @{}'.format(portdir, port_group)
33
34
35 def acl_ethertype(rule):
36 match = ''
37 ip_version = None
38 icmp = None
39 if rule['ip_version'] == const.IP_VERSION_4:
40 match = ' && ip4'
41 ip_version = 'ip4'
42 icmp = 'icmp4'
43 elif rule['ip_version'] == const.IP_VERSION_6:
44 match = ' && ip6'
45 ip_version = 'ip6'
46 icmp = 'icmp6'
47 return match, ip_version, icmp
48
49
50 def acl_ip(rule, ip_version):
51 src_ip = rule.get('source_ip_address')
52 dst_ip = rule.get('destination_ip_address')
53 src = ' && {}.src == {}'.format(ip_version, src_ip) if src_ip else ''
54 dst = ' && {}.dst == {}'.format(ip_version, dst_ip) if dst_ip else ''
55 return src + dst
56
57
58 def get_min_max_ports_from_range(port_range):
59 if not port_range:
60 return [None, None]
61 min_port, sep, max_port = port_range.partition(":")
62 if not max_port:
63 max_port = min_port
64 return [int(min_port), int(max_port)]
65
66
67 def acl_protocol_ports(protocol, port_range, is_dst=True):
68 match = ''
69 min_port, max_port = get_min_max_ports_from_range(port_range)
70 dir = 'dst' if is_dst else 'src'
71 if protocol in ovn_const.TRANSPORT_PROTOCOLS:
72 if min_port is not None and min_port == max_port:
73 match += ' && %s.%s == %d' % (protocol, dir, min_port)
74 else:
75 if min_port is not None:
76 match += ' && %s.%s >= %d' % (protocol, dir, min_port)
77 if max_port is not None:
78 match += ' && %s.%s <= %d' % (protocol, dir, max_port)
79 return match
80
81
82 def acl_protocol_and_ports(rule, icmp):
83 match = ''
84 protocol = rule.get('protocol')
85 if protocol is None:
86 return match
87 src_port = rule.get('source_port')
88 dst_port = rule.get('destination_port')
89 if protocol in ovn_const.TRANSPORT_PROTOCOLS:
90 match += ' && %s' % protocol
91 match += acl_protocol_ports(protocol, src_port, is_dst=False)
92 match += acl_protocol_ports(protocol, dst_port)
93 elif protocol in ovn_const.ICMP_PROTOCOLS:
94 protocol = icmp
95 match += ' && %s' % protocol
96 return match
97
98
99 def acl_action_and_priority(rule, direction):
100 action = rule['action']
101 pos = rule.get('position', 0)
102 if (action == api_const.FWAAS_DENY and
103 rule.get(ovn_const.DEFAULT_RULE, False)):
104 return (ovn_const.ACL_ACTION_DROP,
105 ovn_const.ACL_PRIORITY_DEFAULT)
106
107 if direction == const.INGRESS_DIRECTION:
108 priority = ovn_const.ACL_PRIORITY_INGRESS
109 else:
110 priority = ovn_const.ACL_PRIORITY_EGRESS
111
112 if action == api_const.FWAAS_ALLOW:
113 return (ovn_const.ACL_ACTION_ALLOW_STATELESS,
114 priority - pos)
115 elif action == api_const.FWAAS_DENY:
116 return (ovn_const.ACL_ACTION_DROP,
117 priority - pos)
118 elif action == api_const.FWAAS_REJECT:
119 return (ovn_const.ACL_ACTION_REJECT,
120 priority - pos)
121
122 # NOTE(tkajinam): This should not be reached, but is added for safe guard.
123 raise ValueError(_('Unkown action: %s' % action))
124
125
126 def acl_entry_for_port_group(port_group, rule, direction, match):
127 dir_map = {const.INGRESS_DIRECTION: 'from-lport',
128 const.EGRESS_DIRECTION: 'to-lport'}
129 action, priority = acl_action_and_priority(rule, direction)
130
131 acl = {"port_group": port_group,
132 "priority": priority,
133 "action": action,
134 "log": False,
135 "name": [],
136 "severity": [],
137 "direction": dir_map[direction],
138 "match": match,
139 ovn_const.OVN_FWR_EXT_ID_KEY: rule['id']}
140 return acl
141
142
143 def get_rule_acl_for_port_group(port_group, rule, direction):
144 match = acl_direction(direction, port_group=port_group)
145 ip_match, ip_version, icmp = acl_ethertype(rule)
146 match += ip_match
147 match += acl_ip(rule, ip_version)
148 match += acl_protocol_and_ports(rule, icmp)
149 return acl_entry_for_port_group(port_group, rule, direction, match)
150
151
152 def update_ports_for_pg(nb_idl, txn, pg_name, ports_add=None,
153 ports_delete=None):
154 if ports_add is None:
155 ports_add = []
156 if ports_delete is None:
157 ports_delete = []
158 # Add ports to port_group
159 for port_id in ports_add:
160 txn.add(nb_idl.pg_add_ports(
161 pg_name, port_id))
162 for port_id in ports_delete:
163 txn.add(nb_idl.pg_del_ports(
164 pg_name, port_id, if_exists=True))
165
166
167 def get_default_acls_for_pg(nb_idl, pg_name):
168 nb_acls = nb_idl.pg_acl_list(pg_name).execute(check_error=True)
169 default_acl_list = []
170 for nb_acl in nb_acls:
171 # Get acl whose external_ids has firewall_rule_id, then
172 # append it to list if its value equal to default_rule_id
173 ext_ids = getattr(nb_acl, 'external_ids', {})
174 if (ext_ids.get(ovn_const.OVN_FWR_EXT_ID_KEY) ==
175 ovn_const.DEFAULT_RULE_ID):
176 default_acl_list.append(nb_acl.uuid)
177 return default_acl_list
178
179
180 def process_rule_for_pg(nb_idl, txn, pg_name, rule, direction,
181 op=ovn_const.OP_ADD):
182 dir_map = {const.INGRESS_DIRECTION: 'from-lport',
183 const.EGRESS_DIRECTION: 'to-lport'}
184 supported_ops = [ovn_const.OP_ADD, ovn_const.OP_DEL,
185 ovn_const.OP_MOD]
186 if op not in supported_ops:
187 raise ovn_fw_exc.OperatorNotSupported(
188 operator=op, valid_operators=supported_ops)
189
190 acl = get_rule_acl_for_port_group(
191 pg_name, rule, direction)
192
193 # Add acl
194 if op == ovn_const.OP_ADD:
195 txn.add(nb_idl.pg_acl_add(**acl, may_exist=True))
196 # Modify/Delete acl
197 else:
198 nb_acls = nb_idl.pg_acl_list(pg_name).execute(check_error=True)
199 for nb_acl in nb_acls:
200 # Get acl whose external_ids has firewall_rule_id,
201 # then change it if its value equal to rule's
202 ext_ids = getattr(nb_acl, 'external_ids', {})
203 if (ext_ids.get(ovn_const.OVN_FWR_EXT_ID_KEY) ==
204 rule['id'] and dir_map[direction] == nb_acl.direction):
205 if op == ovn_const.OP_MOD:
206 txn.add(nb_idl.db_set(
207 'ACL', nb_acl.uuid,
208 ('match', acl['match']),
209 ('action', acl['action'])))
210 elif op == ovn_const.OP_DEL:
211 txn.add(nb_idl.pg_acl_del(
212 acl['port_group'],
213 acl['direction'],
214 nb_acl.priority,
215 acl['match']))
216 break
217
218
219 def create_pg_for_fwg(nb_idl, fwg_id):
220 pg_name = ovn_utils.ovn_port_group_name(fwg_id)
221 # Add port_group
222 with nb_idl.transaction(check_error=True) as txn:
223 ext_ids = {ovn_const.OVN_FWG_EXT_ID_KEY: fwg_id}
224 txn.add(nb_idl.pg_add(name=pg_name, acls=[],
225 external_ids=ext_ids, may_exist=True))
226
227
228 def add_default_acls_for_pg(nb_idl, txn, pg_name):
229 # Traffic is default denied, ipv4 or ipv6 with two directions,
230 # so number of default acls is 4
231 default_rule_v4 = {'action': api_const.FWAAS_DENY,
232 'ip_version': const.IP_VERSION_4,
233 'id': ovn_const.DEFAULT_RULE_ID,
234 ovn_const.DEFAULT_RULE: True}
235 default_rule_v6 = {'action': api_const.FWAAS_DENY,
236 'ip_version': const.IP_VERSION_6,
237 'id': ovn_const.DEFAULT_RULE_ID,
238 ovn_const.DEFAULT_RULE: True}
239 for dir in [const.EGRESS_DIRECTION, const.INGRESS_DIRECTION]:
240 process_rule_for_pg(nb_idl, txn, pg_name,
241 default_rule_v4,
242 dir,
243 op=ovn_const.OP_ADD)
244 process_rule_for_pg(nb_idl, txn, pg_name,
245 default_rule_v6,
246 dir,
247 op=ovn_const.OP_ADD)