-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmkfilter.py
More file actions
executable file
·182 lines (159 loc) · 6.44 KB
/
mkfilter.py
File metadata and controls
executable file
·182 lines (159 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#! /usr/bin/env python3
################################################################################
#
# File: mkfilter.py
# Date: July 24, 2015
# Author: Fred Mota (fred.mota@keysight.com)
#
# History:
# February 8, 2019:
# - Updated copyright note.
# - Use the ksvisionlib library.
#
# July 13, 2022:
# - Change the script to Python 3.
#
# Description:
# This script creates 2^n filters, from 0 to 2^n-1. Where the 0th filter
# contains all the source and destination IP addresses that when they are XORed
# together, the result is 0, the 1st filter conatins tall the source and
# destination IP addresses that when they are XORed together, the result is 1,
# and so on.
# The user can specify what type of IP addresses to use, IPv4 or IPv6, and also
# what bits to use in the IP addresses, that is, and offset from the least
# significant bit.
#
# For example, given:
# n (length) = 2
# version = IPv4
# offset = 0
#
# the script will create the following bidirectional filters:
#
# 0) IPv4 A= 0.0.0.0 IPv4 B= 0.0.0.0 Mask= 0.0.0.3 00 xor 00 = 00
# IPv4 A= 0.0.0.1 IPv4 B= 0.0.0.1 Mask= 0.0.0.3 01 xor 01 = 00
# IPv4 A= 0.0.0.2 IPv4 B= 0.0.0.2 Mask= 0.0.0.3 10 xor 10 = 00
# IPv4 A= 0.0.0.3 IPv4 B= 0.0.0.3 Mask= 0.0.0.3 11 xor 11 = 00
#
# 1) IPv4 A= 0.0.0.1 IPv4 B= 0.0.0.0 Mask= 0.0.0.3 01 xor 00 = 01
# IPv4 A= 0.0.0.3 IPv4 B= 0.0.0.2 Mask= 0.0.0.3 11 xor 10 = 01
#
# 2) IPv4 A= 0.0.0.2 IPv4 B= 0.0.0.0 Mask= 0.0.0.3 10 xor 00 = 10
# IPv4 A= 0.0.0.3 IPv4 B= 0.0.0.1 Mask= 0.0.0.3 11 xor 01 = 10
#
# 3) IPv4 A= 0.0.0.3 IPv4 B= 0.0.0.0 Mask= 0.0.0.3 11 xor 00 = 11
# IPv4 A= 0.0.0.2 IPv4 B= 0.0.0.1 Mask= 0.0.0.3 10 xor 01 = 11
#
# Note: If traffic with many different IP addresses is sent to these filters,
# the traffic should be load balanced evenly among the filters.
#
# COPYRIGHT 2015-2019 Keysight Technologies.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
################################################################################
import sys
import getopt
from ksvisionlib import *
def ipAddress (bits, version):
address = ''
if version == 4:
while len(bits) > 0:
address = address + str(int(bits[0:8], 2)) + '.'
bits = bits[8:]
if version == 6:
while len(bits) > 0:
address = address + str(hex(int(bits[0:16], 2))[2:]) + ':'
bits = bits[16:]
address = address[:-1]
return address
argv = sys.argv[1:]
username = ''
password = ''
host = ''
length = ''
offset = ''
version = ''
port = '8000'
try:
opts, args = getopt.getopt(argv,"u:p:h:l:o:v:r:", ["username=", "password=", "host=", "length=", "offset=", "version=", "port="])
except getopt.GetoptError:
print ('0mkfilters.py -u <username> -p <password> -h <host> -l <length> -o <offset> -v <version> [-r <port>]')
sys.exit(2)
for opt, arg in opts:
if opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
elif opt in ("-h", "--host"):
host = arg
elif opt in ("-l", "--length"):
length = int(arg)
elif opt in ("-o", "--offset"):
offset = int(arg)
elif opt in ("-v", "--version"):
version = int(arg)
elif opt in ("-r", "--port"):
port = arg
if username == '':
print ('1mkfilters.py -u <username> -p <password> -h <host> -l <length> -o <offset> -v <version> [-r <port>]')
sys.exit(2)
if password == '':
print ('2mkfilters.py -u <username> -p <password> -h <host> -l <length> -o <offset> -v <version> [-r <port>]')
sys.exit(2)
if (host == ''):
print ('3mkfilters.py -u <username> -p <password> -h <host> -l <length> -o <offset> -v <version> [-r <port>]')
sys.exit(2)
if length == '':
print ('4mkfilters.py -u <username> -p <password> -h <host> -l <length> -o <offset> -v <version> [-r <port>]')
sys.exit(2)
if offset == '':
print ('5mkfilters.py -u <username> -p <password> -h <host> -l <length> -o <offset> -v <version> [-r <port>]')
sys.exit(2)
if version == '':
print ('6mkfilters.py -u <username> -p <password> -h <host> -l <length> -o <offset> -v <version> [-r <port>]')
sys.exit(2)
nto = VisionWebApi(host=host, username=username, password=password, port=port)
if version == 4:
width = 32
else:
width = 128
if length <= 0 or offset < 0 or length + offset > width:
print ("Invalid length and offset specified.")
prefix = ''
fmt = "{0:0" + str(width - length - offset) + "b}"
prefix = fmt.format(0)
postfix = ''
if offset > 0:
fmt = "{0:0" + str(offset) + "b}"
postfix = fmt.format(0)
fmt = "{0:0" + str(length) + "b}"
mask = ipAddress(prefix + fmt.format(2**length - 1) + postfix, version)
for i in range(0, 2**length):
address_sets = []
for j in range(0, 2**length):
for k in range(0, 2**length):
if (((j ^ k) == i) and (j <= k)):
address1 = ipAddress(prefix + fmt.format(j) + postfix, version)
address2 = ipAddress(prefix + fmt.format(k) + postfix, version)
address_sets.append({'addr_a': [address1 + "/" + mask], 'addr_b': [address2 + "/" + mask]})
criteria = {'ipv' + str(version) +'_flow': {'address_sets': address_sets, 'flow_type': 'BIDI'}, 'logical_operation': 'AND'}
#criteria['vlan'] = {'priority': None, 'vlan_id': '408,3400-3409,3701,3703-3704'}
print (criteria)
nto.createFilter({'mode': 'PASS_BY_CRITERIA', 'criteria': criteria, 'name': "XOR IPv" + str(version) + " " + fmt.format(i)})