1
|
|
|
"""IPv6 Match Fields.""" |
2
|
|
|
|
3
|
1 |
|
from pyof.foundation.basic_types import IPv6Address |
4
|
1 |
|
from pyof.v0x04.common.flow_match import OxmOfbMatchField, OxmTLV |
5
|
|
|
|
6
|
1 |
|
from napps.kytos.of_core.v0x04.match_fields_base import MatchField |
7
|
1 |
|
from napps.kytos.of_core.v0x04.utils import bytes_to_mask, mask_to_bytes |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
View Code Duplication |
class MatchIPv6Src(MatchField): |
|
|
|
|
11
|
|
|
"""Match for IPv6 source.""" |
12
|
|
|
|
13
|
1 |
|
name = 'ipv6_src' |
14
|
1 |
|
oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV6_SRC |
15
|
|
|
|
16
|
1 |
|
def as_of_tlv(self): |
17
|
|
|
"""Return a pyof OXM TLV instance.""" |
18
|
|
|
ip_addr = IPv6Address(self.value) |
19
|
|
|
value_bytes = ip_addr.pack() |
20
|
|
|
if ip_addr.netmask < 128: |
21
|
|
|
value_bytes += mask_to_bytes(ip_addr.netmask, 128) |
22
|
|
|
return OxmTLV(oxm_field=self.oxm_field, |
23
|
|
|
oxm_hasmask=ip_addr.netmask < 128, |
24
|
|
|
oxm_value=value_bytes) |
25
|
|
|
|
26
|
1 |
|
@classmethod |
27
|
|
|
def from_of_tlv(cls, tlv): |
28
|
|
|
"""Return an instance from a pyof OXM TLV.""" |
29
|
|
|
ip_address = IPv6Address() |
30
|
|
|
ip_address.unpack(tlv.oxm_value) |
31
|
|
|
addr_str = str(ip_address) |
32
|
|
|
value = addr_str |
33
|
|
|
if tlv.oxm_hasmask: |
34
|
|
|
value = f'{addr_str}/{bytes_to_mask(tlv.oxm_value[16:], 128)}' |
35
|
|
|
return cls(value) |
36
|
|
|
|
37
|
|
|
|
38
|
1 |
View Code Duplication |
class MatchIPv6Dst(MatchField): |
|
|
|
|
39
|
|
|
"""Match for IPv6 destination.""" |
40
|
|
|
|
41
|
1 |
|
name = 'ipv6_dst' |
42
|
1 |
|
oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV6_DST |
43
|
|
|
|
44
|
1 |
|
def as_of_tlv(self): |
45
|
|
|
"""Return a pyof OXM TLV instance.""" |
46
|
|
|
ip_addr = IPv6Address(self.value) |
47
|
|
|
value_bytes = ip_addr.pack() |
48
|
|
|
if ip_addr.netmask < 128: |
49
|
|
|
value_bytes += mask_to_bytes(ip_addr.netmask, 128) |
50
|
|
|
return OxmTLV(oxm_field=self.oxm_field, |
51
|
|
|
oxm_hasmask=ip_addr.netmask < 128, |
52
|
|
|
oxm_value=value_bytes) |
53
|
|
|
|
54
|
1 |
|
@classmethod |
55
|
|
|
def from_of_tlv(cls, tlv): |
56
|
|
|
"""Return an instance from a pyof OXM TLV.""" |
57
|
|
|
ip_address = IPv6Address() |
58
|
|
|
ip_address.unpack(tlv.oxm_value) |
59
|
|
|
addr_str = str(ip_address) |
60
|
|
|
value = addr_str |
61
|
|
|
if tlv.oxm_hasmask: |
62
|
|
|
value = f'{addr_str}/{bytes_to_mask(tlv.oxm_value[16:], 128)}' |
63
|
|
|
return cls(value) |
64
|
|
|
|
65
|
|
|
|
66
|
1 |
View Code Duplication |
class MatchIPv6FLabel(MatchField): |
|
|
|
|
67
|
|
|
"""Match for IPv6 Flow Label.""" |
68
|
|
|
|
69
|
1 |
|
name = 'ipv6_flabel' |
70
|
1 |
|
oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV6_FLABEL |
71
|
|
|
|
72
|
1 |
|
def as_of_tlv(self): |
73
|
|
|
"""Return a pyof OXM TLV instance.""" |
74
|
|
|
try: |
75
|
|
|
value = int(self.value) |
76
|
|
|
mask = None |
77
|
|
|
oxm_hasmask = False |
78
|
|
|
except ValueError: |
79
|
|
|
value, mask = map(int, self.value.split('/')) |
80
|
|
|
oxm_hasmask = True |
81
|
|
|
value_bytes = value.to_bytes(4, 'big') |
82
|
|
|
if mask: |
83
|
|
|
value_bytes += mask.to_bytes(4, 'big') |
84
|
|
|
return OxmTLV(oxm_field=self.oxm_field, |
85
|
|
|
oxm_hasmask=oxm_hasmask, |
86
|
|
|
oxm_value=value_bytes) |
87
|
|
|
|
88
|
1 |
|
@classmethod |
89
|
|
|
def from_of_tlv(cls, tlv): |
90
|
|
|
"""Return an instance from a pyof OXM TLV.""" |
91
|
|
|
value = int.from_bytes(tlv.oxm_value[:4], 'big') |
92
|
|
|
if tlv.oxm_hasmask: |
93
|
|
|
flabel_mask = int.from_bytes(tlv.oxm_value[4:], 'big') |
94
|
|
|
value = f'{value}/{flabel_mask}' |
95
|
|
|
return cls(value) |
96
|
|
|
|
97
|
|
|
|
98
|
1 |
|
class MatchICMPV6Type(MatchField): |
99
|
|
|
"""Match for ICMPV6 type.""" |
100
|
|
|
|
101
|
1 |
|
name = 'icmpv6_type' |
102
|
1 |
|
oxm_field = OxmOfbMatchField.OFPXMT_OFB_ICMPV6_TYPE |
103
|
|
|
|
104
|
1 |
|
def as_of_tlv(self): |
105
|
|
|
"""Return a pyof OXM TLV instance.""" |
106
|
|
|
value_bytes = self.value.to_bytes(1, 'big') |
107
|
|
|
return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes) |
108
|
|
|
|
109
|
1 |
|
@classmethod |
110
|
|
|
def from_of_tlv(cls, tlv): |
111
|
|
|
"""Return an instance from a pyof OXM TLV.""" |
112
|
|
|
port = int.from_bytes(tlv.oxm_value, 'big') |
113
|
|
|
return cls(port) |
114
|
|
|
|
115
|
|
|
|
116
|
1 |
|
class MatchICMPV6Code(MatchField): |
117
|
|
|
"""Match for ICMPV6 code.""" |
118
|
|
|
|
119
|
1 |
|
name = 'icmpv6_code' |
120
|
1 |
|
oxm_field = OxmOfbMatchField.OFPXMT_OFB_ICMPV6_CODE |
121
|
|
|
|
122
|
1 |
|
def as_of_tlv(self): |
123
|
|
|
"""Return a pyof OXM TLV instance.""" |
124
|
|
|
value_bytes = self.value.to_bytes(1, 'big') |
125
|
|
|
return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes) |
126
|
|
|
|
127
|
1 |
|
@classmethod |
128
|
|
|
def from_of_tlv(cls, tlv): |
129
|
|
|
"""Return an instance from a pyof OXM TLV.""" |
130
|
|
|
priority = int.from_bytes(tlv.oxm_value, 'big') |
131
|
|
|
return cls(priority) |
132
|
|
|
|
133
|
|
|
|
134
|
1 |
|
class MatchNDTarget(MatchField): |
135
|
|
|
"""Match for IPv6 ND Target.""" |
136
|
|
|
|
137
|
1 |
|
name = 'nd_tar' |
138
|
1 |
|
oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV6_ND_TARGET |
139
|
|
|
|
140
|
1 |
|
def as_of_tlv(self): |
141
|
|
|
"""Return a pyof OXM TLV instance.""" |
142
|
|
|
value_bytes = self.value.to_bytes(16, 'big') |
143
|
|
|
return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes) |
144
|
|
|
|
145
|
1 |
|
@classmethod |
146
|
|
|
def from_of_tlv(cls, tlv): |
147
|
|
|
"""Return an instance from a pyof OXM TLV.""" |
148
|
|
|
target = int.from_bytes(tlv.oxm_value, 'big') |
149
|
|
|
return cls(target) |
150
|
|
|
|
151
|
|
|
|
152
|
1 |
|
class MatchNDSLL(MatchField): |
153
|
|
|
"""Match for IPv6 ND SLL.""" |
154
|
|
|
|
155
|
1 |
|
name = 'nd_sll' |
156
|
1 |
|
oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV6_ND_SLL |
157
|
|
|
|
158
|
1 |
|
def as_of_tlv(self): |
159
|
|
|
"""Return a pyof OXM TLV instance.""" |
160
|
|
|
value_bytes = self.value.to_bytes(6, 'big') |
161
|
|
|
return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes) |
162
|
|
|
|
163
|
1 |
|
@classmethod |
164
|
|
|
def from_of_tlv(cls, tlv): |
165
|
|
|
"""Return an instance from a pyof OXM TLV.""" |
166
|
|
|
sll = int.from_bytes(tlv.oxm_value, 'big') |
167
|
|
|
return cls(sll) |
168
|
|
|
|
169
|
|
|
|
170
|
1 |
|
class MatchNDTLL(MatchField): |
171
|
|
|
"""Match for IPv6 ND TLL.""" |
172
|
|
|
|
173
|
1 |
|
name = 'nd_tll' |
174
|
1 |
|
oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV6_ND_TLL |
175
|
|
|
|
176
|
1 |
|
def as_of_tlv(self): |
177
|
|
|
"""Return a pyof OXM TLV instance.""" |
178
|
|
|
value_bytes = self.value.to_bytes(6, 'big') |
179
|
|
|
return OxmTLV(oxm_field=self.oxm_field, oxm_value=value_bytes) |
180
|
|
|
|
181
|
1 |
|
@classmethod |
182
|
|
|
def from_of_tlv(cls, tlv): |
183
|
|
|
"""Return an instance from a pyof OXM TLV.""" |
184
|
|
|
tll = int.from_bytes(tlv.oxm_value, 'big') |
185
|
|
|
return cls(tll) |
186
|
|
|
|
187
|
|
|
|
188
|
1 |
View Code Duplication |
class MatchEXTHDR(MatchField): |
|
|
|
|
189
|
|
|
"""Match for IPv6 EXTHDR.""" |
190
|
|
|
|
191
|
1 |
|
name = 'v6_hdr' |
192
|
1 |
|
oxm_field = OxmOfbMatchField.OFPXMT_OFB_IPV6_EXTHDR |
193
|
|
|
|
194
|
1 |
|
def as_of_tlv(self): |
195
|
|
|
"""Return a pyof OXM TLV instance.""" |
196
|
|
|
try: |
197
|
|
|
value = int(self.value) |
198
|
|
|
mask = None |
199
|
|
|
oxm_hasmask = False |
200
|
|
|
except ValueError: |
201
|
|
|
value, mask = map(int, self.value.split('/')) |
202
|
|
|
oxm_hasmask = True |
203
|
|
|
value_bytes = value.to_bytes(2, 'big') |
204
|
|
|
if mask: |
205
|
|
|
value_bytes += mask.to_bytes(2, 'big') |
206
|
|
|
return OxmTLV(oxm_field=self.oxm_field, |
207
|
|
|
oxm_hasmask=oxm_hasmask, |
208
|
|
|
oxm_value=value_bytes) |
209
|
|
|
|
210
|
1 |
|
@classmethod |
211
|
|
|
def from_of_tlv(cls, tlv): |
212
|
|
|
"""Return an instance from a pyof OXM TLV.""" |
213
|
|
|
value = int.from_bytes(tlv.oxm_value[:2], 'big') |
214
|
|
|
if tlv.oxm_hasmask: |
215
|
|
|
exhead_mask = int.from_bytes(tlv.oxm_value[2:], 'big') |
216
|
|
|
value = f'{value}/{exhead_mask}' |
217
|
|
|
return cls(value) |
218
|
|
|
|