@@ -134,89 +134,37 @@ class ControlPlaneAclManager(object):
134
134
return tcp_flags_str
135
135
136
136
def generate_block_ip2me_traffic_iptables_commands (self ):
137
- LOOPBACK_INTERFACE_TABLE_NAME = "LOOPBACK_INTERFACE"
138
- MGMT_INTERFACE_TABLE_NAME = "MGMT_INTERFACE"
139
- VLAN_INTERFACE_TABLE_NAME = "VLAN_INTERFACE"
140
- PORTCHANNEL_INTERFACE_TABLE_NAME = "PORTCHANNEL_INTERFACE"
141
- INTERFACE_TABLE_NAME = "INTERFACE"
137
+ INTERFACE_TABLE_NAME_LIST = [
138
+ "LOOPBACK_INTERFACE" ,
139
+ "MGMT_INTERFACE" ,
140
+ "VLAN_INTERFACE" ,
141
+ "PORTCHANNEL_INTERFACE" ,
142
+ "INTERFACE"
143
+ ]
142
144
143
145
block_ip2me_cmds = []
144
146
145
- # Add iptables rules to drop all packets destined for loopback interface IP addresses
146
- loopback_iface_table = self .config_db .get_table (LOOPBACK_INTERFACE_TABLE_NAME )
147
- if loopback_iface_table :
148
- for key , _ in loopback_iface_table .iteritems ():
149
- if not _ip_prefix_in_key (key ):
150
- continue
151
- iface_name , iface_cidr = key
152
- ip_ntwrk = ipaddress .ip_network (iface_cidr , strict = False )
153
- if isinstance (ip_ntwrk , ipaddress .IPv4Network ):
154
- block_ip2me_cmds .append ("iptables -A INPUT -d {}/{} -j DROP" .format (ip_ntwrk .network_address , ip_ntwrk .max_prefixlen ))
155
- elif isinstance (ip_ntwrk , ipaddress .IPv6Network ):
156
- block_ip2me_cmds .append ("ip6tables -A INPUT -d {}/{} -j DROP" .format (ip_ntwrk .network_address , ip_ntwrk .max_prefixlen ))
157
- else :
158
- log_warning ("Unrecognized IP address type on interface '{}': {}" .format (iface_name , ip_ntwrk ))
159
-
160
- # Add iptables rules to drop all packets destined for management interface IP addresses
161
- mgmt_iface_table = self .config_db .get_table (MGMT_INTERFACE_TABLE_NAME )
162
- if mgmt_iface_table :
163
- for key , _ in mgmt_iface_table .iteritems ():
164
- if not _ip_prefix_in_key (key ):
165
- continue
166
- iface_name , iface_cidr = key
167
- ip_ntwrk = ipaddress .ip_network (iface_cidr , strict = False )
168
- if isinstance (ip_ntwrk , ipaddress .IPv4Network ):
169
- block_ip2me_cmds .append ("iptables -A INPUT -d {}/{} -j DROP" .format (ip_ntwrk .network_address , ip_ntwrk .max_prefixlen ))
170
- elif isinstance (ip_ntwrk , ipaddress .IPv6Network ):
171
- block_ip2me_cmds .append ("ip6tables -A INPUT -d {}/{} -j DROP" .format (ip_ntwrk .network_address , ip_ntwrk .max_prefixlen ))
172
- else :
173
- log_warning ("Unrecognized IP address type on interface '{}': {}" .format (iface_name , ip_ntwrk ))
174
-
175
- # Add iptables rules to drop all packets destined for our VLAN interface gateway IP addresses
176
- vlan_iface_table = self .config_db .get_table (VLAN_INTERFACE_TABLE_NAME )
177
- if vlan_iface_table :
178
- for key , _ in vlan_iface_table .iteritems ():
179
- if not _ip_prefix_in_key (key ):
180
- continue
181
- iface_name , iface_cidr = key
182
- ip_ntwrk = ipaddress .ip_network (iface_cidr , strict = False )
183
- first_host = next (ip_ntwrk .hosts ())
184
- if isinstance (ip_ntwrk , ipaddress .IPv4Network ):
185
- block_ip2me_cmds .append ("iptables -A INPUT -d {}/{} -j DROP" .format (first_host , ip_ntwrk .max_prefixlen ))
186
- elif isinstance (ip_ntwrk , ipaddress .IPv6Network ):
187
- block_ip2me_cmds .append ("ip6tables -A INPUT -d {}/{} -j DROP" .format (first_host , ip_ntwrk .max_prefixlen ))
188
- else :
189
- log_warning ("Unrecognized IP address type on interface '{}': {}" .format (iface_name , ip_ntwrk ))
190
-
191
- # Add iptables rules to drop all packets destined for point-to-point interface IP addresses
192
- # (All portchannel interfaces and configured front-panel interfaces)
193
- portchannel_iface_table = self .config_db .get_table (PORTCHANNEL_INTERFACE_TABLE_NAME )
194
- if portchannel_iface_table :
195
- for key , _ in portchannel_iface_table .iteritems ():
196
- if not _ip_prefix_in_key (key ):
197
- continue
198
- iface_name , iface_cidr = key
199
- ip_ntwrk = ipaddress .ip_network (iface_cidr , strict = False )
200
- if isinstance (ip_ntwrk , ipaddress .IPv4Network ):
201
- block_ip2me_cmds .append ("iptables -A INPUT -d {}/{} -j DROP" .format (ip_ntwrk .network_address , ip_ntwrk .max_prefixlen ))
202
- elif isinstance (ip_ntwrk , ipaddress .IPv6Network ):
203
- block_ip2me_cmds .append ("ip6tables -A INPUT -d {}/{} -j DROP" .format (ip_ntwrk .network_address , ip_ntwrk .max_prefixlen ))
204
- else :
205
- log_warning ("Unrecognized IP address type on interface '{}': {}" .format (iface_name , ip_ntwrk ))
206
-
207
- iface_table = self .config_db .get_table (INTERFACE_TABLE_NAME )
208
- if iface_table :
209
- for key , _ in iface_table .iteritems ():
210
- if not _ip_prefix_in_key (key ):
211
- continue
212
- iface_name , iface_cidr = key
213
- ip_ntwrk = ipaddress .ip_network (iface_cidr , strict = False )
214
- if isinstance (ip_ntwrk , ipaddress .IPv4Network ):
215
- block_ip2me_cmds .append ("iptables -A INPUT -d {}/{} -j DROP" .format (ip_ntwrk .network_address , ip_ntwrk .max_prefixlen ))
216
- elif isinstance (ip_ntwrk , ipaddress .IPv6Network ):
217
- block_ip2me_cmds .append ("ip6tables -A INPUT -d {}/{} -j DROP" .format (ip_ntwrk .network_address , ip_ntwrk .max_prefixlen ))
218
- else :
219
- log_warning ("Unrecognized IP address type on interface '{}': {}" .format (iface_name , ip_ntwrk ))
147
+ # Add iptables rules to drop all packets destined for peer-to-peer interface IP addresses
148
+ for iface_table_name in INTERFACE_TABLE_NAME_LIST :
149
+ iface_table = self .config_db .get_table (iface_table_name )
150
+ if iface_table :
151
+ for key , _ in iface_table .iteritems ():
152
+ if not _ip_prefix_in_key (key ):
153
+ continue
154
+
155
+ iface_name , iface_cidr = key
156
+ ip_ntwrk = ipaddress .ip_network (iface_cidr , strict = False )
157
+
158
+ # For VLAN interfaces, the IP address we want to block is the default gateway (i.e.,
159
+ # the first available host IP address of the VLAN subnet)
160
+ ip_addr = next (ip_ntwrk .hosts ()) if iface_table_name == "VLAN_INTERFACE" else ip_ntwrk .network_address
161
+
162
+ if isinstance (ip_ntwrk , ipaddress .IPv4Network ):
163
+ block_ip2me_cmds .append ("iptables -A INPUT -d {}/{} -j DROP" .format (ip_addr , ip_ntwrk .max_prefixlen ))
164
+ elif isinstance (ip_ntwrk , ipaddress .IPv6Network ):
165
+ block_ip2me_cmds .append ("ip6tables -A INPUT -d {}/{} -j DROP" .format (ip_addr , ip_ntwrk .max_prefixlen ))
166
+ else :
167
+ log_warning ("Unrecognized IP address type on interface '{}': {}" .format (iface_name , ip_ntwrk ))
220
168
221
169
return block_ip2me_cmds
222
170
0 commit comments