-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
565 lines (512 loc) · 24.4 KB
/
main.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
"""Main module of talitarp/contention Kytos Network Application."""
import json
import requests
from textwrap import wrap
from uuid import uuid4
from kytos.core import KytosEvent, KytosNApp, log, rest
from kytos.core.helpers import listen_to
from kytos.core.rest_api import HTTPException, JSONResponse, Request, get_json_or_400
from napps.kytos.of_core.v0x04.flow import Action
from napps.hackinsdn.containment.of_core.v0x04.action import (
ActionSetETHDst,
ActionSetFieldFactory,
ActionSetIPv4Dst,
ActionSetIPv6Dst,
ActionSetTCPDst,
ActionSetUDPDst,
)
from pyof.v0x04.common.action import ActionSetField as OFActionSetField
from .settings import COOKIE_PREFIX
class Main(KytosNApp):
"""Main class of talitarp/contention NApp.This class is the entry point for this napp."""
def setup(self):
"""Replace the '__init__' method for the KytosNApp subclass.
The setup method is automatically called by the controller when your
application is loaded.
log.info("Starting Kytos contention NApp!")
"""
log.info("Starting Kytos contention NApp!")
self.stored_blocks = {"blocks": {}}
"""
stored_blocks = {"blocks": {
"block_id" : {
"switch": "...",
"interface": "...",
"match": {in_port, dl_vlan, nw_src, nw_dst, nw_proto...},
"redirect_to": {outport},
"set": {set_vlan, set_ipv4_dst, set_ipv6_dst, set_tcp_dst, set_udp_dst ...}
}
}}
"""
self.list_blocks = []
# for new actions
self.new_actions = {
"set_ipv4_dst": ActionSetIPv4Dst,
"set_ipv6_dst": ActionSetIPv6Dst,
"set_tcp_dst": ActionSetTCPDst,
"set_udp_dst": ActionSetUDPDst,
"set_eth_dst": ActionSetETHDst,
OFActionSetField: ActionSetFieldFactory, # overwrite of_core definition
}
def execute(self):
"""This method is executed right after the setup method execution.
You can also use this method in loop mode if you add to the above setup
method a line like the following example:
self.execute_as_loop(30) # 30-second interval.
It is not necessary in this NApp.
"""
self.register_new_actions()
def shutdown(self):
"""This method is executed when your napp is unloaded.
If you have some cleanup procedure, insert it here:
log.info('SHUTDOWN contention_block')
It is not necessary in this NApp.
"""
def register_new_actions(self):
"""Add new actions to kytos/of_core."""
for name, action in self.new_actions.items():
Action.add_action_class(name, action)
def validate_input(self, data, type):
if type == "POST":
# Validate all user inputs
mandatory_fields = ["switch", "interface", "match"]
# check switch
switch_id = data.get("switch", "")
if ":" not in switch_id:
switch_id = ":".join(wrap(switch_id, 2))
data["switch"] = switch_id
if not switch_id or switch_id not in self.controller.switches:
return False, f"Invalid switch: {switch_id}"
switch = self.controller.switches[switch_id]
# check interface (port_no 1)
try:
port_no = data.get("interface", "")
port_no = int(port_no)
except:
return False, f"Invalid interface: {port_no}"
if port_no not in switch.interfaces:
return False, f"Unknown interface: {port_no}"
interface = switch.interfaces[port_no]
# check matching fields
match = data.get("match")
if not match:
return False, f"Invalid match: {match}"
if "vlan" not in match:
return False, "Missing mandatory field vlan on match"
expected_fields = [
"ipv4_src",
"ipv4_dst",
"ipv6_src",
"ipv6_dst",
"ip_proto",
"sport",
"dport",
"vlan",
"tcp_src",
"tcp_dst",
"udp_src",
"udp_dst",
"icmp_type",
"mac_src",
"mac_dst",
]
for key in match:
if key not in expected_fields:
return False, f"Unexpected input match field: {key}"
# check matching fields: TCP or UDP (Mandatory IPV4 or IPV6 specification)
if (
"tcp_src" in match
or "tcp_dst" in match
or "udp_src" in match
or "udp_dst" in match
or "icmp_type" in match
):
if all([
attr not in match for attr in
["ipv4_src", "ipv4_dst", "ipv6_src", "ipv6_dst"]
]):
return False, f"Missing mandatory ipv4 or ipv6 on match"
# Only Redirect Contention, the outport redirect is specification (mandatory).
if "redirect_to" in data:
redirect_to = data.get("redirect_to")
if "outport" not in redirect_to:
return False, f"Missing mandatory field on redirect_to: outport"
# Only on Redirect Contention, some fields are expected if exists change in pack data.
expected_fields2 = [
"set_vlan",
"set_ipv4_dst",
"set_ipv6_dst",
"set_tcp_dst",
"set_udp_dst",
"set_eth_dst",
]
if "set" in data:
set = data.get("set")
for key in set:
if key not in expected_fields2:
return False, f"Unexpected input set field: {key}"
if type == "DELETE":
if "block_id" not in data:
return False, "Missing mandatory field block_id on data"
return True, "success"
def get_payload(self, data, block_id, type):
# Call flow_manager's REST API to create the flow
# payload = {"flows": [{"priority": 30000, "hard_timeout": xxx, "cookie": 0xee00000000000001, "match": {"in_port": xxx, "dl_vlan": xxx, "nw_src": xxx, "nw_dst": xxx, "nw_proto": xxx, "ipv6_src"=xxx, "ipv6_dst"=xxx, "tcp_src"=xxx, "tcp_dst"=xxx, "udp_src"=xxx, "udp_dst"=xxx}, "actions": []}]}
cookie = COOKIE_PREFIX + block_id
cookie = int(cookie, 16)
if type == "POST":
if "redirect_to" not in data: # It's a block contention. Action is empty
payload = {
"flows": [
{
"priority": 30000,
"cookie": cookie,
"match": {
"in_port": int(data["interface"]),
"dl_vlan": data["match"]["vlan"],
},
"actions": [],
}
]
}
if "redirect_to" in data: # It's a redirect contention. Action isn't empty
# Is necessary verificy if exists more fields ("set_vlan", set_ipv4_dst", "set_ipv6_dst", "set_tcp_dst", "set_udp_dst", "set_eth_dst") on set.
if (
"set" not in data
): # The rule is redirect contention and doesn't exists modify pack data.
redirect_to = data["redirect_to"]["outport"]
payload = {
"flows": [
{
"priority": 30000,
"cookie": cookie,
"match": {
"in_port": int(data["interface"]),
"dl_vlan": data["match"]["vlan"],
},
"actions": [
{"action_type": "output", "port": redirect_to}
],
}
]
}
if (
"set" in data
): # The rule is redirect contention and exists modify pack data. Before redirect for outport especify, is necessary to modify the pack data.
set = data.get("set")
redirect_to = data["redirect_to"][
"outport"
] # Add an action to send to the specified port (last action)
if "set_vlan" in set:
# adicionar uma action no flow que será enviado para flow_manager com action_type: set_vlan, vlanid= vlan que o usuário pediu
vlan = data["set"]["set_vlan"]
payload = {
"flows": [
{
"priority": 30000,
"cookie": cookie,
"match": {
"in_port": int(data["interface"]),
"dl_vlan": data["match"]["vlan"],
},
"actions": [
{"action_type": "set_vlan", "vlan_id": vlan},
{"action_type": "output", "port": redirect_to},
],
}
]
}
if "set_ipv4_dst" in set:
# action_type: set_ipv4_dst, ipv4_dst= ipv4_dst que o usuário pediu
ipv4_dst = data["set"]["set_ipv4_dst"]
payload = {
"flows": [
{
"priority": 30000,
"cookie": cookie,
"match": {
"in_port": int(data["interface"]),
"dl_vlan": data["match"]["vlan"],
},
"actions": [
{
"action_type": "set_ipv4_dst",
"ipv4_dst": ipv4_dst,
},
{"action_type": "output", "port": redirect_to},
],
}
]
}
if "set_ipv6_dst" in set:
# action_type: set_ipv6_dst, ipv6_dst= ipv6_dst que o usuário pediu
ipv6_dst = data["set"]["set_ipv6_dst"]
payload = {
"flows": [
{
"priority": 30000,
"cookie": cookie,
"match": {
"in_port": int(data["interface"]),
"dl_vlan": data["match"]["vlan"],
},
"actions": [
{
"action_type": "set_ipv6_dst",
"ipv6_dst": ipv6_dst,
},
{"action_type": "output", "port": redirect_to},
],
}
]
}
if "set_tcp_dst" in set:
# action_type: set_tcp_dst, tcp_dst= tcp_dst que o usuário pediu
tcp_dst = data["set"]["set_tcp_dst"]
payload = {
"flows": [
{
"priority": 30000,
"cookie": cookie,
"match": {
"in_port": int(data["interface"]),
"dl_vlan": data["match"]["vlan"],
},
"actions": [
{
"action_type": "set_tcp_dst",
"tcp_dst": tcp_dst,
},
{"action_type": "output", "port": redirect_to},
],
}
]
}
if "set_udp_dst" in set:
# action_type: set_udp_dst, udp_dst= udp_dst que o usuário pediu
udp_dst = data["set"]["set_udp_dst"]
payload = {
"flows": [
{
"priority": 30000,
"cookie": cookie,
"match": {
"in_port": int(data["interface"]),
"dl_vlan": data["match"]["vlan"],
},
"actions": [
{
"action_type": "set_udp_dst",
"udp_dst": udp_dst,
},
{"action_type": "output", "port": redirect_to},
],
}
]
}
if "set_eth_dst" in set:
# action_type: set_eth_dst, eth_dst= eth_dst que o usuário pediu
eth_dst = data["set"]["set_eth_dst"]
payload = {
"flows": [
{
"priority": 30000,
"cookie": cookie,
"match": {
"in_port": int(data["interface"]),
"dl_vlan": data["match"]["vlan"],
},
"actions": [
{
"action_type": "set_eth_dst",
"eth_dst": eth_dst,
},
{"action_type": "output", "port": redirect_to},
],
}
]
}
if "ipv4_src" in data["match"]:
payload["flows"][0]["match"]["dl_type"] = 0x800
payload["flows"][0]["match"]["nw_src"] = data["match"]["ipv4_src"]
if "ipv4_dst" in data["match"]:
payload["flows"][0]["match"]["dl_type"] = 0x800
payload["flows"][0]["match"]["nw_dst"] = data["match"]["ipv4_dst"]
if "ipv6_src" in data["match"]:
payload["flows"][0]["match"]["dl_type"] = 0x86DD
payload["flows"][0]["match"]["ipv6_src"] = data["match"]["ipv6_src"]
if "ipv6_dst" in data["match"]:
payload["flows"][0]["match"]["dl_type"] = 0x86DD
payload["flows"][0]["match"]["ipv6_dst"] = data["match"]["ipv6_dst"]
if "ip_proto" in data["match"]:
payload["flows"][0]["match"]["nw_proto"] = data["match"]["ip_proto"]
if "tcp_src" in data["match"]:
payload["flows"][0]["match"]["nw_proto"] = 6
payload["flows"][0]["match"]["tp_src"] = data["match"]["tcp_src"]
if "tcp_dst" in data["match"]:
payload["flows"][0]["match"]["nw_proto"] = 6
payload["flows"][0]["match"]["tp_dst"] = data["match"]["tcp_dst"]
if "udp_src" in data["match"]:
payload["flows"][0]["match"]["nw_proto"] = 17
payload["flows"][0]["match"]["udp_src"] = data["match"]["udp_src"]
if "udp_dst" in data["match"]:
payload["flows"][0]["match"]["nw_proto"] = 17
payload["flows"][0]["match"]["udp_dst"] = data["match"]["udp_dst"]
if "icmp_type" in data["match"] and payload["flows"][0]["match"]["dl_type"] == 0x800:
payload["flows"][0]["match"]["nw_proto"] = 1
payload["flows"][0]["match"]["icmpv4_type"] = data["match"]["icmp_type"]
if "icmp_type" in data["match"] and payload["flows"][0]["match"]["dl_type"] == 0x86DD:
payload["flows"][0]["match"]["nw_proto"] = 58
payload["flows"][0]["match"]["icmpv6_type"] = data["match"]["icmp_type"]
if "mac_src" in data["match"]:
payload["flows"][0]["match"]["dl_src"] = data["match"]["mac_src"]
if "mac_dst" in data["match"]:
payload["flows"][0]["match"]["dl_dst"] = data["match"]["mac_dst"]
if type == "DELETE":
if (
"redirect_to" not in self.stored_blocks["blocks"][block_id]
): # It's a block contention.
payload = {
"flows": [
{
"priority": 30000,
"cookie": cookie,
"cookie_mask": 0xFFFFFFFFFFFFFFFF,
"match": {
"in_port": int(
self.stored_blocks["blocks"][block_id]["interface"]
),
"dl_vlan": self.stored_blocks["blocks"][block_id][
"match"
]["vlan"],
},
"actions": [],
}
]
}
if (
"redirect_to" in self.stored_blocks["blocks"][block_id]
): # It's a redirect contention.
redirect_to = self.stored_blocks["blocks"][block_id]["redirect_to"][
"outport"
]
payload = {
"flows": [
{
"priority": 30000,
"cookie": cookie,
"cookie_mask": 0xFFFFFFFFFFFFFFFF,
"match": {
"in_port": int(
self.stored_blocks["blocks"][block_id]["interface"]
),
"dl_vlan": self.stored_blocks["blocks"][block_id][
"match"
]["vlan"],
},
"actions": [{"action_type": "output", "port": redirect_to}],
}
]
}
return payload
def add_rule(self, data, payload, dpid, block_id, linha):
response = requests.post(
f"http://127.0.0.1:8181/api/kytos/flow_manager/v2/flows/{dpid}",
json=payload,
)
if response.status_code != 202:
log.info(f"Fail to add rule, flow_manager response: {response.text}")
raise HTTPException(
400, f"Invalid request to flow_manager: {response.text}"
)
port_no = data.get("interface")
port_no = int(port_no)
if "redirect_to" not in data:
self.stored_blocks["blocks"][block_id] = {
"switch": data["switch"],
"interface": port_no,
"match": data.get("match"),
}
if "redirect_to" in data:
self.stored_blocks["blocks"][block_id] = {
"switch": data["switch"],
"interface": port_no,
"match": data.get("match"),
"redirect_to": data.get("redirect_to"),
}
self.list_blocks.append(linha)
return True, "success"
def remove_rule(self, block_id, payload, dpid):
if block_id in self.stored_blocks["blocks"]:
response = requests.delete(
f"http://127.0.0.1:8181/api/kytos/flow_manager/v2/flows/{dpid}",
json=payload,
)
if response.status_code != 202:
log.info(f"Fail to remove rule, flow_manager response: {response.text}")
raise HTTPException(
400, f"Invalid request to flow_manager: {response.text}"
)
linha = (
str(self.stored_blocks["blocks"][block_id]["switch"])
+ str(self.stored_blocks["blocks"][block_id]["interface"])
+ str(self.stored_blocks["blocks"][block_id]["match"])
)
del self.stored_blocks["blocks"][block_id]
self.list_blocks.remove(linha)
return True, "success"
@rest("/v1/", methods=["POST"])
def contention_post(self, request: Request) -> JSONResponse:
type = "POST"
data = get_json_or_400(request, self.controller.loop) # access user request
result, msg = self.validate_input(data, type)
if not result:
log.info(f"Invalid request data: {msg} - request={data}")
raise HTTPException(400, f"Invalid request data: {msg}")
log.info(f"ADD REDIRECT contention called with data={data}")
dpid = data["switch"]
block_id = uuid4().hex[:14]
payload = self.get_payload(data, block_id, type)
if (
"block_id" in data
): # Para verificação se tentar inserir um ID já existente (proximo if)
block_id = data["block_id"]
if block_id in self.stored_blocks["blocks"]:
log.info("Fail to create containment: ID already exists.")
raise HTTPException(400, "Fail to create containment: ID already exists.")
else:
linha = (
str(data["switch"])
+ str(data.get("interface"))
+ str(data.get("match"))
) # eu guardo na lista de controle apenas ate o match. Pois é a regra de forma mais geral.
if linha not in self.list_blocks:
if self.add_rule(
data, payload, dpid, block_id, linha
): # Rule is inserted (add_rule)
log.info(f"Update contention list ADD={data}")
return JSONResponse({"containment_id": block_id})
else:
log.info("Fail to create containment: RULE already exists.")
raise HTTPException(
400, "Fail to create containment: RULE already exists in the list."
)
@rest("/v1/{containment_id}", methods=["DELETE"])
def contention_remove(self, request: Request) -> JSONResponse:
"""Remove a containment."""
containment_id = request.path_params["containment_id"]
if containment_id not in self.stored_blocks["blocks"]:
log.info(f"Invalid DELETE containment {containment_id}")
raise HTTPException(404, f"Invalid containment ID (not found)")
log.info(f"DELETE containment {containment_id}")
dpid = self.stored_blocks["blocks"][containment_id]["switch"]
payload = self.get_payload({}, containment_id, "DELETE")
if self.remove_rule(containment_id, payload, dpid):
log.info(f"Containment DELETE successfully {containment_id}")
return JSONResponse("Containment deleted successfully")
else:
raise HTTPException(400, "Fail to delete containment, check logs.")
@rest("/v1/", methods=["GET"])
def list_contention(self, request: Request) -> JSONResponse:
"""List contentions performed so far."""
return JSONResponse(self.stored_blocks)