Skip to content

Commit

Permalink
improve support to wpa
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonfontes committed Feb 21, 2017
1 parent 189c51c commit c504e54
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 99 deletions.
103 changes: 63 additions & 40 deletions mininet/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"""
import os
import re
from time import time

from mininet.log import info, error, debug
from mininet.util import makeIntfPair
Expand Down Expand Up @@ -912,30 +911,6 @@ def getMacAddress(self, sta, iface, wlan):
mac = self._macMatchRegex.findall(ifconfig)
sta.meshMac[wlan] = str(mac[0])

@classmethod
def confirmInfraAssociation(self, sta, ap, wlan):
"""
Confirm association when station associates to AP
:param sta: station
:param ap: access point
:param wlan: wlan ID
"""

if self.printCon:
iface = sta.params['wlan'][wlan]
info("Associating %s to %s\n" % (iface, ap))
if 'encrypt' in ap.params:
associated = ''
currentTime = time()
while(associated == '' or len(associated[0]) == 15):
associated = self.isAssociated(sta, wlan)
if time() >= currentTime + 10:
info("Error during the association process\n")
break
sta.params['frequency'][wlan] = setChannelParams.frequency(ap, 0)


@classmethod
def configureWirelessLink(self, sta, ap, wlan):
"""
Expand Down Expand Up @@ -980,12 +955,6 @@ def updateParams(self, sta, ap, wlan):

sta.params['frequency'][wlan] = setChannelParams.frequency(ap, 0)
sta.params['channel'][wlan] = ap.params['channel'][0]

@classmethod
def isAssociated(self, sta, iface):
""" Check if station is already associated to the Access Point """
associated = sta.pexec("iw dev %s-wlan%s link" % (sta, iface))
return associated

@classmethod
def associate(self, sta, ap):
Expand All @@ -1001,7 +970,13 @@ def associate(self, sta, ap):

@classmethod
def associate_infra(self, sta, ap, wlan):
""" Association when INFRA """
"""
Association when infra
:param sta: station
:param ap: access point
:param wlan: wlan ID
"""
if 'encrypt' not in ap.params:
sta.cmd('iwconfig %s essid %s ap %s' % (sta.params['wlan'][wlan], ap.params['ssid'][0], \
ap.params['mac'][0]))
Expand All @@ -1010,23 +985,71 @@ def associate_infra(self, sta, ap, wlan):
self.associate_wpa(sta, ap, wlan)
elif ap.params['encrypt'][0] == 'wep':
self.associate_wep(sta, ap, wlan)
cls = Association
cls.confirmInfraAssociation(sta, ap, wlan)

if self.printCon:
iface = sta.params['wlan'][wlan]
info("Associating %s to %s\n" % (iface, ap))
sta.params['frequency'][wlan] = setChannelParams.frequency(ap, 0)

@classmethod
def associate_wpa(self, sta, ap, wlan):
""" Association when WPA """
def wpaFile(self, sta, ap, wlan):
"""
It creates a wpa config file
:param sta: station
:param ap: access point
:param wlan: wlan ID
"""
if 'passwd' not in sta.params:
passwd = ap.params['passwd'][0]
else:
passwd = sta.params['passwd'][wlan]

content = ('ctrl_interface=/var/run/wpa_supplicant\n' \
'network={\n' \
' ssid=\"%s\"\n' \
' psk=\"%s\"\n' \
' key_mgmt=%s\n' \
' proto=%s\n' \
' pairwise=%s') % \
(ap.params['ssid'][0], passwd, ap.wpa_key_mgmt, ap.params['encrypt'][0].upper(), ap.rsn_pairwise)

if 'config' in sta.params.keys():
config = sta.params['config']
if(config != []):
config = sta.params['config'].split(',')
sta.params.pop("config", None)
for conf in config:
content = content + "\n " + conf
content = content + '\n}'

fileName = str(sta) + '.staconf'
os.system('echo \'%s\' > %s' % (content, fileName))

@classmethod
def associate_wpa(self, sta, ap, wlan):
"""
Association when WPA
:param sta: station
:param ap: access point
:param wlan: wlan ID
"""
pidfile = "mn%d_%s_%s_wpa.pid" % (os.getpid(), sta.name, wlan)
sta.cmd("wpa_supplicant -B -Dnl80211 -P %s -i %s-wlan%s -c <(wpa_passphrase \"%s\" \"%s\")"
% (pidfile, sta, wlan, ap.params['ssid'][0], passwd))
self.wpaFile(sta, ap, wlan)
debug("wpa_supplicant -B -Dnl80211 -P %s -i %s -c %s.staconf"
% (pidfile, sta.params['wlan'][wlan], sta))
sta.pexec("wpa_supplicant -B -Dnl80211 -P %s -i %s -c %s.staconf"
% (pidfile, sta.params['wlan'][wlan], sta))

@classmethod
def associate_wep(self, sta, ap, wlan):
""" Association when WEP """
"""
Association when WEP
:param sta: station
:param ap: access point
:param wlan: wlan ID
"""
if 'passwd' not in sta.params:
passwd = ap.params['passwd'][0]
else:
Expand Down
42 changes: 15 additions & 27 deletions mininet/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
from __builtin__ import True

# Mininet version: should be consistent with README and LICENSE
VERSION = "2.0"
VERSION = "2.0r1"

class Mininet(object):
"Network emulation with hosts spawned in network namespaces."
Expand Down Expand Up @@ -815,38 +815,25 @@ def configureAP(self, node, wlanID=0):
else:
iface = node.params.get('phywlan')

self.auth_algs = None
self.wpa = None
self.wpa_key_mgmt = None
self.rsn_pairwise = None
self.wpa_passphrase = None
self.wep_key0 = None
self.country_code = None
self.wmm_enabled = None

if 'encrypt' in node.params:
if node.params['encrypt'][wlan] == 'wpa':
self.auth_algs = 1
self.wpa = 1
self.wpa_key_mgmt = 'WPA-PSK'
self.wpa_passphrase = node.params['passwd'][0]
node.auth_algs = 1
node.wpa = 1
node.wpa_key_mgmt = 'WPA-EAP'
node.rsn_pairwise = 'TKIP CCMP'
node.wpa_passphrase = node.params['passwd'][0]
elif node.params['encrypt'][wlan] == 'wpa2':
self.auth_algs = 1
self.wpa = 2
self.wpa_key_mgmt = 'WPA-PSK'
self.rsn_pairwise = 'CCMP'
self.wpa_passphrase = node.params['passwd'][0]
node.auth_algs = 1
node.wpa = 2
node.wpa_key_mgmt = 'WPA-PSK'
node.rsn_pairwise = 'CCMP'
node.wpa_passphrase = node.params['passwd'][0]
elif node.params['encrypt'][wlan] == 'wep':
self.auth_algs = 2
self.wep_key0 = node.params['passwd'][0]

wifiparam.setdefault('country_code', self.country_code)
wifiparam.setdefault('auth_algs', self.auth_algs)
wifiparam.setdefault('wpa', self.wpa)
wifiparam.setdefault('wpa_key_mgmt', self.wpa_key_mgmt)
wifiparam.setdefault('rsn_pairwise', self.rsn_pairwise)
wifiparam.setdefault('wpa_passphrase', self.wpa_passphrase)
wifiparam.setdefault('wep_key0', self.wep_key0)
node.auth_algs = 2
node.wep_key0 = node.params['passwd'][0]

wifiparam.setdefault('wlan', wlan)

cls = AccessPoint
Expand All @@ -868,6 +855,7 @@ def configureAP(self, node, wlanID=0):
cls = TCLinkWireless
cls(node, **options)
node.params.pop("phywlan", None)
setChannelParams.recordParams(None, node)

def verifyNetworkManager(self, node, wlanID=0):
"""First verify if the mac address of the ap is included at NetworkManager.conf"""
Expand Down
41 changes: 16 additions & 25 deletions mininet/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1420,18 +1420,12 @@ class AccessPoint(AP):

writeMacAddress = False

def __init__ (self, ap, country_code=None, auth_algs=None, wpa=None, wlan=None,
wpa_key_mgmt=None, rsn_pairwise=None, wpa_passphrase=None,
wep_key0=None, **params):
def __init__ (self, ap, wlan=None, **params):

self.start_(ap, country_code, auth_algs, wpa, wlan,
wpa_key_mgmt, rsn_pairwise, wpa_passphrase,
wep_key0, **params)
self.start_(ap, wlan, **params)

@classmethod
def start_(self, ap, country_code=None, auth_algs=None, wpa=None, wlan=None,
wpa_key_mgmt=None, rsn_pairwise=None, wpa_passphrase=None,
wep_key0=None, **params):
def start_(self, ap, wlan=None, **params):
""" Starting Access Point """
cmd = ("echo \'")

Expand All @@ -1453,24 +1447,21 @@ def start_(self, ap, country_code=None, auth_algs=None, wpa=None, wlan=None,

if 'encrypt' in ap.params:
if ap.params['encrypt'][0] == 'wpa':
cmd = cmd + ("\nauth_algs=%s" % auth_algs)
cmd = cmd + ("\nwpa=%s" % wpa)
cmd = cmd + ("\nwpa_key_mgmt=%s" % wpa_key_mgmt)
cmd = cmd + ("\nwpa_passphrase=%s" % wpa_passphrase)
cmd = cmd + ("\nauth_algs=%s" % ap.auth_algs)
cmd = cmd + ("\nwpa=%s" % ap.wpa)
cmd = cmd + ("\nwpa_key_mgmt=%s" % ap.wpa_key_mgmt)
cmd = cmd + ("\nwpa_passphrase=%s" % ap.wpa_passphrase)
elif ap.params['encrypt'][0] == 'wpa2':
cmd = cmd + ("\nauth_algs=%s" % auth_algs)
cmd = cmd + ("\nwpa=%s" % wpa)
cmd = cmd + ("\nwpa_key_mgmt=%s" % wpa_key_mgmt)
# cmd = cmd + ("\nrsn_pairwise=%s" % rsn_pairwise)
cmd = cmd + ("\nwpa_passphrase=%s" % wpa_passphrase)
cmd = cmd + ("\nauth_algs=%s" % ap.auth_algs)
cmd = cmd + ("\nwpa=%s" % ap.wpa)
cmd = cmd + ("\nwpa_key_mgmt=%s" % ap.wpa_key_mgmt)
cmd = cmd + ("\nrsn_pairwise=%s" % ap.rsn_pairwise)
cmd = cmd + ("\nwpa_passphrase=%s" % ap.wpa_passphrase)
elif ap.params['encrypt'][0] == 'wep':
cmd = cmd + ("\nauth_algs=%s" % auth_algs)
cmd = cmd + ("\nauth_algs=%s" % ap.auth_algs)
cmd = cmd + ("\nwep_default_key=%s" % 0)
cmd = cmd + self.verifyWepKey(wep_key0)
cmd = cmd + self.verifyWepKey(ap.wep_key0)

cmd = cmd + '\nctrl_interface=/var/run/hostapd'
cmd = cmd + '\nctrl_interface_group=0'

if 'config' in ap.params.keys():
config = ap.params['config']
if(config != []):
Expand All @@ -1488,9 +1479,9 @@ def start_(self, ap, country_code=None, auth_algs=None, wpa=None, wlan=None,
cmd = cmd + ("\nssid=%s" % ssid)
if 'encrypt' in ap.params:
if (ap.params['encrypt'][i] == 'wep'):
cmd = cmd + ("\nauth_algs=%s" % auth_algs)
cmd = cmd + ("\nauth_algs=%s" % ap.auth_algs)
cmd = cmd + ("\nwep_default_key=0")
cmd = cmd + self.verifyWepKey(wep_key0)
cmd = cmd + self.verifyWepKey(ap.wep_key0)
ap.params['mac'][i] = ap.params['mac'][wlan][:-1] + str(i)
# self.checkNetworkManager(ap.params['mac'][i])
self.APConfigFile(cmd, ap, wlan)
Expand Down
55 changes: 48 additions & 7 deletions mininet/wifiMobility.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def apOutOfRange(self, sta, ap, wlan, dist):
"""
if ap == sta.params['associatedTo'][wlan]:
debug('iw dev %s disconnect' % sta.params['wlan'][wlan])
if 'encrypt' in ap.params:
if ap.params['encrypt'][0] == 'wpa' or ap.params['encrypt'][0] == 'wpa2':
os.system('rm %s.staconf' % sta)
pidfile = "mn%d_%s_%s_wpa.pid" % (os.getpid(), sta.name, wlan)
os.system('pkill -f \'wpa_supplicant -B -Dnl80211 -P %s -i %s\'' % (pidfile, sta.params['wlan'][wlan]))
sta.pexec('iw dev %s disconnect' % sta.params['wlan'][wlan])
sta.params['associatedTo'][wlan] = ''
sta.params['rssi'][wlan] = 0
Expand Down Expand Up @@ -214,7 +219,7 @@ def updateAssociation(self, sta, ap, wlan):
@classmethod
def associate_wep(self, sta, ap, wlan):
"""
Does association if WEP
Association when WEP
:param sta: station
:param ap: access point
Expand All @@ -224,26 +229,62 @@ def associate_wep(self, sta, ap, wlan):
sta.pexec('iw dev %s connect %s key d:0:%s' % (sta.params['wlan'][wlan], ap.params['ssid'][0], passwd))
self.updateAssociation(sta, ap, wlan)

@classmethod
def wpaFile(self, sta, ap, wlan):
"""
It creates a wpa config file
:param sta: station
:param ap: access point
:param wlan: wlan ID
"""
if 'passwd' not in sta.params:
passwd = ap.params['passwd'][0]
else:
passwd = sta.params['passwd'][wlan]

content = ('ctrl_interface=/var/run/wpa_supplicant\n' \
'network={\n' \
' ssid=\"%s\"\n' \
' psk=\"%s\"\n' \
' key_mgmt=%s\n' \
' proto=%s\n' \
' pairwise=%s') % \
(ap.params['ssid'][0], passwd, ap.wpa_key_mgmt, ap.params['encrypt'][0].upper(), ap.rsn_pairwise)

if 'config' in sta.params.keys():
config = sta.params['config']
if(config != []):
config = sta.params['config'].split(',')
sta.params.pop("config", None)
for conf in config:
content = content + "\n " + conf
content = content + '\n}'

fileName = str(sta) + '.staconf'
os.system('echo \'%s\' > %s' % (content, fileName))

@classmethod
def associate_wpa(self, sta, ap, wlan):
"""
Does association if WPA
Association when WPA
:param sta: station
:param ap: access point
:param wlan: wlan ID
"""
passwd = self.verifyPasswd(sta, ap, wlan)
pidfile = "mn%d_%s_%s_wpa.pid" % (os.getpid(), sta.name, wlan)
os.system('pkill -f \'wpa_supplicant -B -Dnl80211 -P %s -i %s\'' % (pidfile, sta.params['wlan'][wlan]))
sta.cmd("wpa_supplicant -B -Dnl80211 -P %s -i %s -c <(wpa_passphrase \"%s\" \"%s\")"
% (pidfile, sta.params['wlan'][wlan], ap.params['ssid'][0], passwd))
self.wpaFile(sta, ap, wlan)
debug("wpa_supplicant -B -Dnl80211 -P %s -i %s -c %s.staconf"
% (pidfile, sta.params['wlan'][wlan], sta))
sta.pexec("wpa_supplicant -B -Dnl80211 -P %s -i %s -c %s.staconf"
% (pidfile, sta.params['wlan'][wlan], sta))
self.updateAssociation(sta, ap, wlan)

@classmethod
def associate_infra(self, sta, ap, wlan):
"""
Does association if INFRA
Infra association
:param sta: station
:param ap: access point
Expand Down

0 comments on commit c504e54

Please sign in to comment.