Skip to content

Commit

Permalink
v0.1.6 move phyadd to _depr_phyadd Issue #23
Browse files Browse the repository at this point in the history
  • Loading branch information
WraithWireless authored and WraithWireless committed Aug 3, 2016
1 parent 1a05696 commit 4918477
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 67 deletions.
3 changes: 2 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,5 @@ v 0.1.6 Continue with STA functionality
report No Such device as two seperate errors one identifying when there is
no device currently present on the machine and one identifying when the
specified dev's driver does not support nl80211
o fixed TypeError resulting from raising pyric.error incorrectly
o fixed TypeError resulting from raising pyric.error incorrectly
o moved phyadd to _depr_phyadd issue #23
133 changes: 67 additions & 66 deletions pyric/pyw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,69 +1517,6 @@ def devset(card, ndev, *argv):
raise
return new

def phyadd(card, vdev, mode, flags=None, *argv):
"""
REQUIRES ROOT PRIVILEGES
adds a virtual interface on device having type mode (iw phy <card.phy>
interface add <vnic> type <mode>
:param card: Card object or physical index
:param vdev: device name of new interface
:param mode: 'name' of mode to operate in (must be one of in {'unspecified'|
'ibss'|'managed'|'AP'|'AP VLAN'|'wds'|'monitor'|'mesh'|'p2p'}
:param flags: list of monitor flags (can only be used if creating monitor
mode) oneof {'invalid'|'fcsfail'|'plcpfail'|'control'|'other bss'
|'cook'|'active'}
:param argv: netlink socket at argv[0] (or empty)
:returns: the new Card
NOTE: due to a recent bug in kernel 4.4.0-x where x is APX 28, nl80211
commands to add interface are not "respected" by the kernel. Namely,
the vdev is not used and the kernel adds a card with a "predictable"
name and furthermore, the new card has a different hw address (1 up from
the original card)
"""
if mode not in IFTYPES: raise pyric.error(pyric.EINVAL, 'Invalid mode')
if flags:
if mode != 'monitor':
raise pyric.error(pyric.EINVAL, 'Can only set flags in monitor mode')
for flag in flags:
if flag not in MNTRFLAGS:
raise pyric.error(pyric.EINVAL, 'Invalid flag: {0}'.format(flag))
else: flags = []

try:
nlsock = argv[0]
except IndexError:
return _nlstub_(phyadd, card, vdev, mode, flags)

# if we have a Card, pull out phy index
try:
phy = card.phy
except AttributeError:
phy = card

try:
msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
cmd=nl80211h.NL80211_CMD_NEW_INTERFACE,
flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
nl.nla_put_u32(msg, phy, nl80211h.NL80211_ATTR_WIPHY)
nl.nla_put_string(msg, vdev, nl80211h.NL80211_ATTR_IFNAME)
nl.nla_put_u32(msg, IFTYPES.index(mode), nl80211h.NL80211_ATTR_IFTYPE)
for flag in flags:
nl.nla_put_u32(msg,
MNTRFLAGS.index(flag),
nl80211h.NL80211_ATTR_MNTR_FLAGS)
nl.nl_sendmsg(nlsock, msg)
rmsg = nl.nl_recvmsg(nlsock) # success returns new device attributes
except AttributeError as e:
raise pyric.error(pyric.EINVAL, e)
except nl.error as e:
raise pyric.error(e.errno, e.strerror)

# return the new Card with info from the results msg
return Card(nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY),
nl.nla_find(rmsg, nl80211h.NL80211_ATTR_IFNAME),
nl.nla_find(rmsg, nl80211h.NL80211_ATTR_IFINDEX))

def devadd(card, vdev, mode, flags=None, *argv):
"""
REQUIRES ROOT PRIVILEGES
Expand Down Expand Up @@ -1816,8 +1753,9 @@ def link(card, *argv):
# "\x06\x00\x00<l>SSID.....
# '\x06\x00' is the ie index & the ssid is the first element
# (from what I've seen). This is not nested. Not sure if the
# length is the first two bytes or just the second Get the length of the ssid which is the 3rd,4th byte, then unpack
# the string starting at the fifth byte up to the length
# length is the first two bytes or just the second Get the length
# of the ssid which is the 3rd,4th byte, then unpack the string
# starting at the fifth byte up to the specified length
try:
l = struct.unpack_from('>H', attr, 0)[0] # have to change the format
info['ssid'] = struct.unpack_from('{0}s'.format(l), attr, 2)[0]
Expand Down Expand Up @@ -2418,4 +2356,67 @@ def _fut_chset(card, ch, chw, *argv):
nl.nla_put_u32(msg, channels.ch2rf(ch), nl80211h.NL80211_ATTR_WIPHY_FREQ)
nl.nla_put_u32(msg, channels.CHTYPES.index(chw), nl80211h.NL80211_ATTR_WIPHY_CHANNEL_TYPE)
nl.nl_sendmsg(nlsock, msg)
_ = nl.nl_recvmsg(nlsock)
_ = nl.nl_recvmsg(nlsock)

def _depr_phyadd(card, vdev, mode, flags=None, *argv):
"""
REQUIRES ROOT PRIVILEGES
adds a virtual interface on device having type mode (iw phy <card.phy>
interface add <vnic> type <mode>
:param card: Card object or physical index
:param vdev: device name of new interface
:param mode: 'name' of mode to operate in (must be one of in {'unspecified'|
'ibss'|'managed'|'AP'|'AP VLAN'|'wds'|'monitor'|'mesh'|'p2p'}
:param flags: list of monitor flags (can only be used if creating monitor
mode) oneof {'invalid'|'fcsfail'|'plcpfail'|'control'|'other bss'
|'cook'|'active'}
:param argv: netlink socket at argv[0] (or empty)
:returns: the new Card
NOTE: due to a recent bug in kernel 4.4.0-x where x is APX 28, nl80211
commands to add interface are not "respected" by the kernel. Namely,
the vdev is not used and the kernel adds a card with a "predictable"
name and furthermore, the new card has a different hw address (1 up from
the original card)
"""
if mode not in IFTYPES: raise pyric.error(pyric.EINVAL, 'Invalid mode')
if flags:
if mode != 'monitor':
raise pyric.error(pyric.EINVAL, 'Can only set flags in monitor mode')
for flag in flags:
if flag not in MNTRFLAGS:
raise pyric.error(pyric.EINVAL, 'Invalid flag: {0}'.format(flag))
else: flags = []

try:
nlsock = argv[0]
except IndexError:
return _nlstub_(phyadd, card, vdev, mode, flags)

# if we have a Card, pull out phy index
try:
phy = card.phy
except AttributeError:
phy = card

try:
msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
cmd=nl80211h.NL80211_CMD_NEW_INTERFACE,
flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
nl.nla_put_u32(msg, phy, nl80211h.NL80211_ATTR_WIPHY)
nl.nla_put_string(msg, vdev, nl80211h.NL80211_ATTR_IFNAME)
nl.nla_put_u32(msg, IFTYPES.index(mode), nl80211h.NL80211_ATTR_IFTYPE)
for flag in flags:
nl.nla_put_u32(msg,
MNTRFLAGS.index(flag),
nl80211h.NL80211_ATTR_MNTR_FLAGS)
nl.nl_sendmsg(nlsock, msg)
rmsg = nl.nl_recvmsg(nlsock) # success returns new device attributes
except AttributeError as e:
raise pyric.error(pyric.EINVAL, e)
except nl.error as e:
raise pyric.error(e.errno, e.strerror)

# return the new Card with info from the results msg
return Card(nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY),
nl.nla_find(rmsg, nl80211h.NL80211_ATTR_IFNAME),
nl.nla_find(rmsg, nl80211h.NL80211_ATTR_IFINDEX))

0 comments on commit 4918477

Please sign in to comment.