Created
July 16, 2013 22:52
-
-
Save wh5a/6015943 to your computer and use it in GitHub Desktop.
SDN Module 4 OpenFlow Firewall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Coursera: | |
- Software Defined Networking (SDN) course | |
-- Module 4 Programming Assignment | |
Professor: Nick Feamster | |
Teaching Assistant: Muhammad Shahbaz | |
''' | |
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 | |
from collections import namedtuple | |
import os | |
import csv | |
log = core.getLogger() | |
policyFile = "%s/pox/pox/misc/firewall-policies.csv" % os.environ[ 'HOME' ] | |
''' Add your global variables here ... ''' | |
class Firewall (EventMixin): | |
def __init__ (self): | |
self.listenTo(core.openflow) | |
log.debug("Enabling Firewall Module") | |
self.deny = [] | |
with open(policyFile, 'rb') as f: | |
reader = csv.DictReader(f) | |
for row in reader: | |
self.deny.append((EthAddr(row['mac_0']), EthAddr(row['mac_1']))) | |
self.deny.append((EthAddr(row['mac_1']), EthAddr(row['mac_0']))) | |
def _handle_ConnectionUp (self, event): | |
for (src, dst) in self.deny: | |
match = of.ofp_match() | |
match.dl_src = src | |
match.dl_dst = dst | |
msg = of.ofp_flow_mod() | |
msg.match = match | |
event.connection.send(msg) | |
log.debug("Firewall rules installed on %s", dpidToStr(event.dpid)) | |
def launch (): | |
''' | |
Starting the Firewall module | |
''' | |
core.registerNew(Firewall) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
Note: Remove id,mac_0,mac_1 from first line of firewall-policies.csv file
Use the following code:
'''
Coursera:
-- Module 4 Programming Assignment
Professor: Nick Feamster
Teaching Assistant: Muhammad Shahbaz
Edited by: Mehran Pourvahab
'''
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
from collections import namedtuple
import os
''' Add your imports here ... '''
import csv
log = core.getLogger()
--> Remove id,mac_0,mac_1 from first line of firewall-policies.csv file
policyFile = "%s/pox/pox/misc/firewall-policies.csv" % os.environ[ 'HOME' ]
''' Add your global variables here ... '''
policyTable = []
with open(policyFile, 'rb') as f:
csv_entry = csv.reader(f, delimiter=',')
for row in csv_entry:
# --> ['1', '00:00:00:00:00:01', '00:00:00:00:00:02'] []
log.debug("row data from csv file %s ", row)
policyTable.append(row[1:])
class Firewall (EventMixin):
def launch ():
'''
Starting the Firewall module
'''
core.registerNew(Firewall)