-
Notifications
You must be signed in to change notification settings - Fork 0
/
gates_mininet.py
147 lines (121 loc) · 5.69 KB
/
gates_mininet.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
# Model of Gates Network for Mininet
# Craig Riecke, CoSciN Developer/Analyst, June, 2015
#
# To make this work:
# - You need to be at Mininet > 2.2, which you should build from source if on Ubuntu 14.04 (the
# latest dpkg is Mininet 2.1). This will make the links use the right ports instead of
# sequentially renumbering them
# - You need vlan package, via "sudo apt-get install vlan"
# - You also need DHCP server and client, packages udhcpd and udhcpc
# - You NEED OpenVSwitch = 2.1. Install this from mininet - util/install.sh -V 2.1.3
# - Version 2.0.2, the one that comes in Ubuntu 14.04, has a
# bug which sends a truncated packet with packet_in, but with buffer_id=-1 (meaning no buffer)
# Since you don't have all the data, it's impossible to send out the correct data in packet_out.
# This really affects DHCP.
# - Version 2.3 installs a Table Miss rule to drop all packets, and you can't get rid of it.
# Dell switches are configured to Table-Miss to the controller, and that's REQUIRED because of
# the insane implementation of ACL and L2/L3 tables. So your scripts won't work here.
import re
import sys
from networkx import *
import pygraphviz as pgv
# Mininet imports
from mininet.log import lg, info, error, debug, output
from mininet.util import quietRun
from mininet.node import Host, OVSSwitch, RemoteController
from mininet.cli import CLI
from mininet.net import Mininet
# Mercilessly copied from https://github.com/mininet/mininet/blob/master/examples/vlanhost.py
#
class VLANHost( Host ):
"Host connected to VLAN interface"
def config( self, vlan=100, **params ):
"""Configure VLANHost according to (optional) parameters:
vlan: VLAN ID for default interface"""
r = super( VLANHost, self ).config( **params )
intf = self.defaultIntf()
# remove IP from default, "physical" interface
self.cmd( 'ifconfig %s inet 0' % intf )
# create VLAN interface
self.cmd( 'vconfig add %s %d' % ( intf, vlan ) )
# assign the host's IP to the VLAN interface
self.cmd( 'ifconfig %s.%d inet %s' % ( intf, vlan, params['ip'] ) )
# update the intf name and host's intf map
newName = '%s.%d' % ( intf, vlan )
# update the (Mininet) interface to refer to VLAN interface name
intf.name = newName
# add VLAN interface to host's name to intf map
self.nameToIntf[ newName ] = intf
return r
# DHCP server functions and data
DNSTemplate = """
start 128.253.154.100
end 128.253.154.250
option subnet 255.255.255.0
option domain local
option lease 3600 # seconds
"""
def makeDHCPconfig( filename, intf, gw, dns ):
"Create a DHCP configuration file"
config = (
'interface %s' % intf,
DNSTemplate,
'option router %s' % gw,
'option dns %s' % dns,
'' )
with open( filename, 'w' ) as f:
f.write( '\n'.join( config ) )
def startDHCPserver( host, gw, dns ):
"Start DHCP server on host with specified DNS server"
info( '* Starting DHCP server on', host, 'at', host.IP(), '\n' )
dhcpConfig = '/tmp/%s-udhcpd.conf' % host
makeDHCPconfig( dhcpConfig, host.defaultIntf(), gw, dns )
host.cmd( 'udhcpd -f', dhcpConfig,
'1>/tmp/%s-dhcp.log 2>&1 &' % host )
def start(ip="127.0.0.1",port=6633):
#def start(ip="127.0.0.1",port=6653):
ctrlr = lambda n: RemoteController(n, ip=ip, port=port, inNamespace=False)
net = Mininet(switch=OVSSwitch, controller=ctrlr, autoStaticArp=False)
c1 = net.addController('c1')
gates_agraph = pgv.AGraph("simplified_gates_topology.dot")
for sw in gates_agraph.nodes():
net.addSwitch(sw, dpid = hex( int(sw.attr['dpid']) )[2:])
for link in gates_agraph.edges():
(src_switch, dst_switch) = link
net.addLink(src_switch, dst_switch, int(link.attr['src_port']), int(link.attr['dport']) )
# Only one host and an Internet Router disguised as a host for now (because it's not part of the OF network)
h0 = net.addHost('h0', cls=VLANHost, mac='d4:c9:ef:b2:1b:80', ip='128.253.154.1', vlan=1356)
net.addLink("s_bdf", h0, 1, 0)
# To test DHCP functionality, ucnomment this line and comment out the fixed IP line. Then when mininet
# starts you issue:
# mininet> h1 dhclient -v -d -1 h1-eth0.1356
# and make sure it gets its IP by going through all protocol steps.
#h1 = net.addHost('h1', cls=VLANHost, mac='00:00:01:00:00:11', ip='0.0.0.0', vlan=1356)
h1 = net.addHost('h1', cls=VLANHost, mac='00:00:01:00:00:11', ip='128.253.154.100', vlan=1356)
net.addLink("s_f3a", h1, 32, 0)
# h4{a,b,c} are wireless nodes supposedly hooked up to a dumb AP. You can't just hook them up
# to the same mininet port. Y
h4a = net.addHost('h4a', cls=VLANHost, mac='00:00:01:00:00:14', ip='128.253.154.104', vlan=1356)
net.addLink("s_f3a", h4a, 33, 0)
#net.addHost('h4b', cls=VLANHost, mac='00:00:01:00:00:14', ip='128.253.154.104', vlan=1356)
#net.addHost('h4c', cls=VLANHost, mac='00:00:01:00:00:14', ip='128.253.154.104', vlan=1356)
# h2 is a syslab PC
h2 = net.addHost('h2', cls=VLANHost, mac='00:00:01:00:00:13', ip='128.253.154.102', vlan=1356)
net.addLink("s_lab_r6", h2, 1, 0)
# MAC spoofing attempt of h2
h3 = net.addHost('h3', cls=VLANHost, mac='00:00:01:00:00:13', ip='128.253.154.102', vlan=1356)
net.addLink("s_lab_r6", h3, 2, 0)
###### Start of static Mininet epilogue ######
# Set up logging etc.
lg.setLogLevel('info')
lg.setLogLevel('output')
# Start the network
net.start()
# Start the DHCP server on Internet Router. This will actually be a DHCP proxy in the real setup.
#startDHCPserver( h0, gw='128.253.154.1', dns='8.8.8.8')
# Enter CLI mode
output("Network ready\n")
output("Press Ctrl-d or type exit to quit\n")
CLI(net)
# net.stop()
start()