from libnl.linux_private.netlink import NETLINK_GENERIC, NLM_F_DUMP from libnl.list_ import nl_list_for_each_entry from libnl.misc import __init, c_int from libnl.msg import NL_AUTO_PORT, NL_AUTO_SEQ, nlmsg_alloc, nlmsg_hdr from libnl.netlink_private.cache_api import nl_cache_ops, nl_msgtype from libnl.netlink_private.netlink import BUG from libnl.netlink_private.types import genl_family_grp from libnl.nl import nl_recvmsgs, nl_send_auto, wait_for_ack from libnl.socket_ import nl_socket_get_cb CTRL_VERSION = 0x0001 ctrl_policy = dict((i, None) for i in range(CTRL_ATTR_MAX + 1)) ctrl_policy.update({ # https://github.com/thom311/libnl/blob/libnl3_2_25/lib/genl/ctrl.c#L43 CTRL_ATTR_FAMILY_ID: nla_policy(type_=NLA_U16), CTRL_ATTR_FAMILY_NAME: nla_policy(type_=NLA_STRING, maxlen=GENL_NAMSIZ), CTRL_ATTR_VERSION: nla_policy(type_=NLA_U32), CTRL_ATTR_HDRSIZE: nla_policy(type_=NLA_U32), CTRL_ATTR_MAXATTR: nla_policy(type_=NLA_U32), CTRL_ATTR_OPS: nla_policy(type_=NLA_NESTED), CTRL_ATTR_MCAST_GROUPS: nla_policy(type_=NLA_NESTED), }) family_op_policy = dict((i, None) for i in range(CTRL_ATTR_OP_MAX + 1)) family_op_policy.update({ # https://github.com/thom311/libnl/blob/libnl3_2_25/lib/genl/ctrl.c#L54 CTRL_ATTR_OP_ID: nla_policy(type_=NLA_U32), CTRL_ATTR_OP_FLAGS: nla_policy(type_=NLA_U32), })
key = ie[0] # Should be key in `ieprinters` dict. len_ = ie[1] # Length of this information element. data = ie[2:len_ + 2] # Data for this information element. if key in ieprinters and ieprinters[key].flags & 1: answers.update(get_ie(ieprinters[key], key, data)) elif key == 221: answers.update(get_vendor(data)) else: answers['Unknown IE ({0})'.format(key)] = ' '.join(format(x, '02x') for x in data) ie = ie[len_ + 2:] return answers bss_policy = dict((i, None) for i in range(nl80211.NL80211_BSS_MAX + 1)) bss_policy.update({ # http://git.kernel.org/cgit/linux/kernel/git/jberg/iw.git/tree/scan.c?id=v3.17#n1549 nl80211.NL80211_BSS_BSSID: nla_policy(), nl80211.NL80211_BSS_FREQUENCY: nla_policy(type_=NLA_U32), nl80211.NL80211_BSS_TSF: nla_policy(type_=NLA_U64), nl80211.NL80211_BSS_BEACON_INTERVAL: nla_policy(type_=NLA_U16), nl80211.NL80211_BSS_CAPABILITY: nla_policy(type_=NLA_U16), nl80211.NL80211_BSS_INFORMATION_ELEMENTS: nla_policy(), nl80211.NL80211_BSS_SIGNAL_MBM: nla_policy(type_=NLA_U32), nl80211.NL80211_BSS_SIGNAL_UNSPEC: nla_policy(type_=NLA_U8), nl80211.NL80211_BSS_STATUS: nla_policy(type_=NLA_U32), nl80211.NL80211_BSS_SEEN_MS_AGO: nla_policy(type_=NLA_U32), nl80211.NL80211_BSS_BEACON_IES: nla_policy(), nl80211.NL80211_BSS_CHAN_WIDTH: nla_policy(), nl80211.NL80211_BSS_BEACON_TSF: nla_policy(), nl80211.NL80211_BSS_PRESP_DATA: nla_policy(), })
NCSI_CHANNEL_ATTR_ACTIVE = 7 NCSI_CHANNEL_ATTR_FORCED = 8 NCSI_CHANNEL_ATTR_VLAN_LIST = 9 NCSI_CHANNEL_ATTR_VLAN_ID = 10 NCSI_CHANNEL_ATTR_MAX = 11 NCSI_VLAN_UNSPEC = 0 NCSI_VLAN_INFO = 1 NCSI_VLAN_INFO_ID = 2 NCSI_VLAN_INFO_PROTO = 3 NCSI_VLAN_INFO_MAX = 4 # NCSI Netlink attribte policies ncsi_policy = dict((i, None) for i in range(NCSI_ATTR_MAX)) ncsi_policy.update({ NCSI_ATTR_IFINDEX: nla_policy(type_=NLA_U32), NCSI_ATTR_PACKAGE_LIST: nla_policy(type_=NLA_NESTED), NCSI_ATTR_PACKAGE_ID: nla_policy(type_=NLA_U32), NCSI_ATTR_CHANNEL_ID: nla_policy(type_=NLA_U32), }) ncsi_package_policy = dict((i, None) for i in range(NCSI_PKG_ATTR_MAX)) ncsi_package_policy.update({ NCSI_PKG_ATTR: nla_policy(type_=NLA_NESTED), NCSI_PKG_ATTR_ID: nla_policy(type_=NLA_U32), NCSI_PKG_ATTR_FORCED: nla_policy(type_=NLA_FLAG), NCSI_PKG_ATTR_CHANNEL_LIST: nla_policy(type_=NLA_NESTED),
len_ = ie[1] # Length of this information element. data = ie[2:len_ + 2] # Data for this information element. if key in ieprinters and ieprinters[key].flags & 1: answers.update(get_ie(ieprinters[key], key, data)) elif key == 221: answers.update(get_vendor(data)) else: answers['Unknown IE ({0})'.format(key)] = ' '.join( format(x, '02x') for x in data) ie = ie[len_ + 2:] return answers bss_policy = dict((i, None) for i in range(nl80211.NL80211_BSS_MAX + 1)) bss_policy.update({ # http://git.kernel.org/cgit/linux/kernel/git/jberg/iw.git/tree/scan.c?id=v3.17#n1549 nl80211.NL80211_BSS_BSSID: nla_policy(), nl80211.NL80211_BSS_FREQUENCY: nla_policy(type_=NLA_U32), nl80211.NL80211_BSS_TSF: nla_policy(type_=NLA_U64), nl80211.NL80211_BSS_BEACON_INTERVAL: nla_policy(type_=NLA_U16), nl80211.NL80211_BSS_CAPABILITY: nla_policy(type_=NLA_U16), nl80211.NL80211_BSS_INFORMATION_ELEMENTS: nla_policy(), nl80211.NL80211_BSS_SIGNAL_MBM: nla_policy(type_=NLA_U32), nl80211.NL80211_BSS_SIGNAL_UNSPEC: nla_policy(type_=NLA_U8), nl80211.NL80211_BSS_STATUS: nla_policy(type_=NLA_U32), nl80211.NL80211_BSS_SEEN_MS_AGO: nla_policy(type_=NLA_U32), nl80211.NL80211_BSS_BEACON_IES: nla_policy(), nl80211.NL80211_BSS_CHAN_WIDTH: nla_policy(), nl80211.NL80211_BSS_BEACON_TSF: nla_policy(), nl80211.NL80211_BSS_PRESP_DATA: nla_policy(), })
def getStationInfo_callback(msg, results): # Dictionnary later populated with response message sub-attributes sinfo = dict() # Get the header of the message gnlh = genlmsghdr(nlmsg_data(nlmsg_hdr(msg))) tb = dict((i, None) for i in range(nl80211.NL80211_ATTR_MAX + 1)) # Define the data structure of the netlink attributes we will receive stats_policy = dict( (i, None) for i in range(nl80211.NL80211_STA_INFO_MAX + 1)) stats_policy.update({ nl80211.NL80211_STA_INFO_INACTIVE_TIME: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_RX_BYTES: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_TX_BYTES: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_RX_PACKETS: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_TX_PACKETS: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_SIGNAL: nla_policy(type_=NLA_U8), nl80211.NL80211_STA_INFO_SIGNAL_AVG: nla_policy(type_=NLA_U8), nl80211.NL80211_STA_INFO_T_OFFSET: nla_policy(type_=NLA_U64), nl80211.NL80211_STA_INFO_TX_BITRATE: nla_policy(type_=NLA_NESTED), nl80211.NL80211_STA_INFO_RX_BITRATE: nla_policy(type_=NLA_NESTED), nl80211.NL80211_STA_INFO_LLID: nla_policy(type_=NLA_U16), nl80211.NL80211_STA_INFO_PLID: nla_policy(type_=NLA_U16), nl80211.NL80211_STA_INFO_PLINK_STATE: nla_policy(type_=NLA_U8), nl80211.NL80211_STA_INFO_TX_RETRIES: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_TX_FAILED: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_LOCAL_PM: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_PEER_PM: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_NONPEER_PM: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_CHAIN_SIGNAL: nla_policy(type_=NLA_NESTED), nl80211.NL80211_STA_INFO_RX_BYTES64: nla_policy(type_=NLA_U64), nl80211.NL80211_STA_INFO_TX_BYTES64: nla_policy(type_=NLA_U64), nl80211.NL80211_STA_INFO_BEACON_LOSS: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_CONNECTED_TIME: nla_policy(type_=NLA_U32), nl80211.NL80211_STA_INFO_BSS_PARAM: nla_policy(type_=NLA_NESTED), }) # If any value in the stats_policy is empty, pad it with a default NLA_U8 type to avoid # any issue during validation for key in stats_policy: if stats_policy[key] is None: stats_policy[key] = nla_policy(type_=NLA_U8) # Parse the stream of attributes received into indexed chunks of data nla_parse(tb, nl80211.NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), None) # If we haven't received Station info data, don't go further and skip this message if tb[nl80211.NL80211_ATTR_STA_INFO] is None: return libnl.handlers.NL_SKIP # Finally, feed the attributes of the message into the chunk defined before nla_parse_nested(sinfo, nl80211.NL80211_STA_INFO_MAX, tb[nl80211.NL80211_ATTR_STA_INFO], stats_policy) # Create the Station object station = Station() # Finally, if an attribute of interest is present, save it in the object if tb[nl80211.NL80211_ATTR_MAC]: # Convert the station MAC address to something human readable raw_mac = nla_get_string(tb[nl80211.NL80211_ATTR_MAC]) if len(raw_mac) == 6: station.mac_addr = "%x:%x:%x:%x:%x:%x" % struct.unpack( "BBBBBB", raw_mac) if sinfo[nl80211.NL80211_STA_INFO_RX_BYTES]: station.rx_bytes = nla_get_u32( sinfo[nl80211.NL80211_STA_INFO_RX_BYTES]) if sinfo[nl80211.NL80211_STA_INFO_TX_BYTES]: station.tx_bytes = nla_get_u32( sinfo[nl80211.NL80211_STA_INFO_TX_BYTES]) if sinfo[nl80211.NL80211_STA_INFO_RX_PACKETS]: station.rx_packets = nla_get_u32( sinfo[nl80211.NL80211_STA_INFO_RX_PACKETS]) if sinfo[nl80211.NL80211_STA_INFO_TX_PACKETS]: station.tx_packets = nla_get_u32( sinfo[nl80211.NL80211_STA_INFO_TX_PACKETS]) if sinfo[nl80211.NL80211_STA_INFO_TX_FAILED]: station.tx_failed = nla_get_u32( sinfo[nl80211.NL80211_STA_INFO_TX_FAILED]) if sinfo[nl80211.NL80211_STA_INFO_SIGNAL]: # Signal level is saved as an 8-bit byte, so we convert it to a signed integer raw_signal = nla_get_u8(sinfo[nl80211.NL80211_STA_INFO_SIGNAL]) if raw_signal > 127: station.signal = raw_signal - 256 else: station.signal = raw_signal # Append the station to the list of station and iterate to the next result results.append(station) return libnl.handlers.NL_SKIP
GENL_NAMSIZ) from libnl.linux_private.netlink import NETLINK_GENERIC, NLM_F_DUMP from libnl.list_ import nl_list_for_each_entry from libnl.misc import __init, c_int from libnl.msg import NL_AUTO_PORT, NL_AUTO_SEQ, nlmsg_alloc, nlmsg_hdr from libnl.netlink_private.cache_api import nl_cache_ops, nl_msgtype from libnl.netlink_private.netlink import BUG from libnl.netlink_private.types import genl_family_grp from libnl.nl import nl_recvmsgs, nl_send_auto, wait_for_ack from libnl.socket_ import nl_socket_get_cb CTRL_VERSION = 0x0001 ctrl_policy = dict((i, None) for i in range(CTRL_ATTR_MAX + 1)) ctrl_policy.update({ # https://github.com/thom311/libnl/blob/libnl3_2_25/lib/genl/ctrl.c#L43 CTRL_ATTR_FAMILY_ID: nla_policy(type_=NLA_U16), CTRL_ATTR_FAMILY_NAME: nla_policy(type_=NLA_STRING, maxlen=GENL_NAMSIZ), CTRL_ATTR_VERSION: nla_policy(type_=NLA_U32), CTRL_ATTR_HDRSIZE: nla_policy(type_=NLA_U32), CTRL_ATTR_MAXATTR: nla_policy(type_=NLA_U32), CTRL_ATTR_OPS: nla_policy(type_=NLA_NESTED), CTRL_ATTR_MCAST_GROUPS: nla_policy(type_=NLA_NESTED), }) family_op_policy = dict((i, None) for i in range(CTRL_ATTR_OP_MAX + 1)) family_op_policy.update({ # https://github.com/thom311/libnl/blob/libnl3_2_25/lib/genl/ctrl.c#L54 CTRL_ATTR_OP_ID: nla_policy(type_=NLA_U32), CTRL_ATTR_OP_FLAGS: nla_policy(type_=NLA_U32), }) family_grp_policy = dict((i, None) for i in range(CTRL_ATTR_MCAST_GRP_MAX + 1))