Skip to content

Instantly share code, notes, and snippets.

@misskecupbung
Forked from khooi8913/mac_filter.py
Created June 4, 2022 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misskecupbung/44f2429919f5b05c053d25e752923bbd to your computer and use it in GitHub Desktop.
Save misskecupbung/44f2429919f5b05c053d25e752923bbd to your computer and use it in GitHub Desktop.
# Copyright 2012 James McCauley
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This component is for use with the OpenFlow tutorial.
It acts as a simple hub, but can be modified to act like an L2
learning switch.
It's roughly similar to the one Brandon Heller did for NOX.
"""
from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.lib.packet.ethernet import ethernet
from pox.lib.packet.ipv4 import ipv4
from pox.lib.packet.arp import arp
from pox.lib.packet.icmp import icmp, echo
from pox.lib.addresses import IPAddr, EthAddr
from pox.lib.util import str_to_bool, dpid_to_str
log = core.getLogger()
def add_entry(table, ethernet_addr):
table[EthAddr(ethernet_addr)] = 1
class Tutorial (object):
"""
A Tutorial object is created for each switch that connects.
A Connection object for that switch is passed to the __init__ function.
"""
def __init__ (self, connection):
# Keep track of the connection to the switch so that we can
# send it messages!
self.connection = connection
# This binds our PacketIn event listener
connection.addListeners(self)
# Use this table to keep track of which ethernet address is on
# which switch port (keys are MACs, values are ports).
self.mac_to_port = {}
# Use this set to keep track of a list of MAC address
# to be filtered and dropped.
self.mac_filter = []
# TODO: add MAC addresses that are to be filtered into this list.
self.mac_filter.append(EthAddr("00:00:00:00:00:01"))
def resend_packet (self, packet_in, out_port):
"""
Instructs the switch to resend a packet that it had sent to us.
"packet_in" is the ofp_packet_in object the switch had sent to the
controller due to a table-miss.
"""
msg = of.ofp_packet_out()
msg.data = packet_in
# Add an action to send to the specified port
action = of.ofp_action_output(port = out_port)
msg.actions.append(action)
# Send message to switch
self.connection.send(msg)
def drop (self, packet, packet_in):
"""
Drops this packet and optionally installs a flow to continue
dropping similar ones for a while
"""
msg = of.ofp_flow_mod()
# Match is hardcoded to use the source MAC
msg.match.dl_src = packet.src
# No Action, thus by default it will resort to drop
msg.buffer_id = packet_in.buffer_id
self.connection.send(msg)
def act_like_hub (self, packet, packet_in):
"""
Implement hub-like behavior -- send all packets to all ports besides
the input port.
"""
# We want to output to all ports -- we do that using the special
# OFPP_ALL port as the output port. (We could have also used
# OFPP_FLOOD.)
self.resend_packet(packet_in, of.OFPP_ALL)
msg = of.ofp_flow_mod()
msg.actions.append(of.ofp_action_output(port=of.OFPP_ALL))
self.connection.send(msg)
def act_like_switch (self, packet, packet_in):
"""
Implement switch-like behavior.
"""
if not packet.src in self.mac_to_port:
# record the MAC, and also the port number
self.mac_to_port[packet.src] = packet_in.in_port
# TODO: MAC filter here
if packet.src in self.mac_filter:
self.drop(packet, packet_in)
elif packet.dst in self.mac_to_port:
out_port = self.mac_to_port[packet.dst]
self.resend_packet(packet_in, out_port)
msg = of.ofp_flow_mod()
msg.match.dl_dst = packet.dst
msg.actions.append(of.ofp_action_output(port=self.mac_to_port[packet.dst]))
self.connection.send(msg)
else:
self.resend_packet(packet_in, of.OFPP_ALL)
def _handle_PacketIn (self, event):
"""
Handles packet in messages from the switch.
"""
packet = event.parsed # This is the parsed packet data.
if not packet.parsed:
log.warning("Ignoring incomplete packet")
return
packet_in = event.ofp # The actual ofp_packet_in message.
print("Packet in!")
# Comment out the following line and uncomment the one after
# when starting the exercise.
# self.act_like_hub(packet, packet_in)
self.act_like_switch(packet, packet_in)
def launch ():
"""
Starts the component
"""
def start_switch (event):
log.debug("Controlling %s" % (event.connection,))
Tutorial(event.connection)
core.openflow.addListenerByName("ConnectionUp", start_switch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment