Debian Codegraph @

neutron_fwaas/services/firewall/service_drivers/agents/drivers/linux/netlink_conntrack.py

raw symbols SCIP
1 # Copyright (c) 2017 Fujitsu Limited
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_lib import constants
17 from oslo_log import log as logging
18
19 from neutron_fwaas.privileged import netlink_lib as nl_lib
20 from neutron_fwaas.services.firewall.service_drivers.agents.drivers import\
21 conntrack_base
22
23 LOG = logging.getLogger(__name__)
24
25
26 class ConntrackNetlink(conntrack_base.ConntrackDriverBase):
27 def initialize(self, *args, **kwargs):
28 LOG.debug('Conntrack Netlink loaded')
29
30 def flush_entries(self, namespace):
31 """Flush all conntrack entries within the namespace
32
33 :param namespace: namespace to flush
34 :return: None
35 """
36 nl_lib.flush_entries(namespace)
37
38 def delete_entries(self, rules, namespace):
39 rule_filters = (self._get_filter_from_rule(r) for r in rules)
40 rule_filters = sorted(rule_filters)
41 entries = nl_lib.list_entries(namespace)
42 delete_entries = self._get_entries_to_delete(rule_filters, entries)
43 if delete_entries:
44 nl_lib.delete_entries(delete_entries, namespace)
45
46 def _get_entries_to_delete(self, rule_filters, entries):
47 """Specify conntrack entries to delete
48
49 :param rule_filters: List of filters parsed from firewall rules
50 :param entries: all entries within namespace
51 :return: conntrack entries to delete
52 """
53 # List all entries from namespace, they are already parsed
54 # to a list of tuples:
55 # [(4, 'icmp', 8, 0, '1.1.1.1', '2.2.2.2', 1234),
56 # (4, 'tcp', 1, 2, '1.1.1.1', '2.2.2.2')]
57 delete_entries = []
58 entry_index = 0
59 entry_number = len(entries)
60 for rule_filter in rule_filters:
61 while entry_index < entry_number:
62 # Compare entry with rule
63 comp = self._compare_entry_and_rule(rule_filter,
64 entries[entry_index])
65 # Increase entry_index when entry is under rule
66 if comp < 0:
67 entry_index += 1
68 # Append entry to delete_entry if it matches with rule
69 elif comp == 0:
70 delete_entries.append(entries[entry_index])
71 entry_index += 1
72 # Switch to new higher rule
73 else:
74 break
75 return delete_entries
76
77 @staticmethod
78 def _get_filter_from_rule(rule):
79 """Parse the firewall rule to a tuple
80
81 :param rule: firewall rule
82 :return: a tuple of parsed information
83 """
84 rule_filter = []
85 keys = ['ip_version', 'protocol',
86 'source_port', 'destination_port',
87 'source_ip_address', 'destination_ip_address']
88 for key in keys:
89 if key in ['source_port', 'destination_port']:
90 port_range = rule.get(key, [])
91 if port_range:
92 port_lower, sep, port_upper = port_range.partition(':')
93 port_upper = port_upper if sep else port_lower
94 port_range = [port_lower, port_upper]
95 rule_filter.append(port_range or [])
96 else:
97 rule_filter.append(rule.get(key, []))
98 return tuple(rule_filter)
99
100 @staticmethod
101 def _compare_entry_and_rule(rule_filter, entry):
102 """Define that the entry should be deleted or not
103
104 :param rule_filter: filter that is parsed from a firewall rule
105 ex: (4, 'tcp', 1, 2)
106 :param entry: parsed conntrack entry,
107 ex: (4, 'tcp', 1, 2, '1.1.1.1', '2.2.2.2')
108 :return: -1 if entry is lower than rule, 0 if entry matches rule,
109 1 if entry is higher than rule
110 """
111 ENTRY_IS_LOWER = -1
112 ENTRY_MATCHES = 0
113 ENTRY_IS_HIGHER = 1
114 rule_ipversion = rule_filter[0]
115
116 if entry[0] < rule_ipversion:
117 return ENTRY_IS_LOWER
118 elif entry[0] > rule_ipversion:
119 return ENTRY_IS_HIGHER
120 rule_protocol = rule_filter[1]
121
122 if rule_protocol:
123 if rule_protocol == constants.PROTO_NAME_IPV6_ICMP:
124 rule_protocol = constants.PROTO_NAME_IPV6_ICMP_LEGACY
125 if entry[1] < rule_protocol:
126 return ENTRY_IS_LOWER
127 elif entry[1] > rule_protocol:
128 return ENTRY_IS_HIGHER
129
130 sport_range = rule_filter[2]
131 if sport_range:
132 sport_range = [int(port) for port in sport_range]
133 if entry[2] < min(sport_range[0], sport_range[-1]):
134 return ENTRY_IS_LOWER
135 elif entry[2] > max(sport_range[0], sport_range[-1]):
136 return ENTRY_IS_HIGHER
137 dport_range = rule_filter[3]
138 if dport_range:
139 dport_range = [int(port) for port in dport_range]
140 if entry[3] < min(dport_range[0], dport_range[-1]):
141 return ENTRY_IS_LOWER
142 elif entry[3] > max(dport_range[0], dport_range[-1]):
143 return ENTRY_IS_HIGHER
144 return ENTRY_MATCHES