forked from esha2008/SDN_firewall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirewall.py
84 lines (74 loc) · 2.66 KB
/
firewall.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
from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.lib.revent import *
from pox.lib.util import dpidToStr
from pox.lib.addresses import EthAddr, IPAddr
import pox.lib.packet as pkt
from collections import namedtuple
import os
import csv
log = core.getLogger()
policyFile = "/pox/pox/misc/firewallpolicies.csv"
class Firewall (EventMixin):
def __init__ (self):
self.listenTo(core.openflow)
log.info("Enabling Firewall Module")
# Our firewall table
self.firewall = {}
def sendRule (self, src, dst, duration = 0):
"""
Drops this packet and optionally installs a flow to continue
dropping similar ones for a while
"""
if not isinstance(duration, tuple):
duration = (duration,duration)
msg = of.ofp_flow_mod()
match = of.ofp_match(dl_type = 0x800,
nw_proto = pkt.ipv4.ICMP_PROTOCOL)
match.nw_src = IPAddr(src)
match.nw_dst = IPAddr(dst)
msg.match = match
msg.idle_timeout = duration[0]
msg.hard_timeout = duration[1]
msg.priority = 10
self.connection.send(msg)
# function that allows adding firewall rules into the firewall table
def AddRule (self, src=0, dst=0, value=True):
if (src, dst) in self.firewall:
log.info("Rule already present drop: src %s - dst %s", src, dst)
else:
log.info("Adding firewall rule drop: src %s - dst %s", src, dst)
self.firewall[(src, dst)]=value
self.sendRule(src, dst, 10000)
# function that allows deleting firewall rules from the firewall table
def DeleteRule (self, src=0, dst=0):
try:
del self.firewall[(src, dst)]
sendRule(src, dst, 0)
log.info("Deleting firewall rule drop: src %s - dst %s", src, dst)
except KeyError:
log.error("Cannot find in rule drop src %s - dst %s", src, dst)
def _handle_ConnectionUp (self, event):
''' Add your logic here ... '''
self.connection = event.connection
ifile = open(policyFile, "rb")
reader = csv.reader(ifile)
rownum = 0
for row in reader:
# Save header row.
if rownum == 0:
header = row
else:
colnum = 0
for col in row:
#print '%-8s: %s' % (header[colnum], col)
colnum += 1
self.AddRule(row[1], row[2])
rownum += 1
ifile.close()
log.info("Firewall rules installed on %s", dpidToStr(event.dpid))
def launch ():
'''
Starting the Firewall module
'''
core.registerNew(Firewall)