def handle_freq(attr, pol): e, fattr = nl.py_nla_parse_nested(nl80211.NL80211_FREQUENCY_ATTR_MAX, attr, pol) if nl80211.NL80211_FREQUENCY_ATTR_FREQ in fattr: freq = nl.nla_get_u32(fattr[nl80211.NL80211_FREQUENCY_ATTR_FREQ]) sys.stdout.write("\t\tfreq %d MHz [%d]" % (freq, freq_to_ch(freq))) if nl80211.NL80211_FREQUENCY_ATTR_MAX_TX_POWER in fattr and not (nl80211.NL80211_FREQUENCY_ATTR_DISABLED in fattr): sys.stdout.write(" (%.1f dBm)" % (0.01 * nl.nla_get_u32(fattr[nl80211.NL80211_FREQUENCY_ATTR_MAX_TX_POWER]))) if nl80211.NL80211_FREQUENCY_ATTR_DISABLED in fattr: sys.stdout.write(" (disabled)") sys.stdout.write("\n")
def msg_handler(m, a): try: e, attr = genl.py_genlmsg_parse(nl.nlmsg_hdr(m), 0, nl80211.NL80211_ATTR_MAX, None) if nl80211.NL80211_ATTR_WIPHY in attr: thiswiphy = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY]) print("phy#%d" % thiswiphy) if nl80211.NL80211_ATTR_IFNAME in attr: print("\tinterface %s" % nl.nla_get_string(attr[nl80211.NL80211_ATTR_IFNAME])) if nl80211.NL80211_ATTR_IFINDEX in attr: print("\tifindex %d" % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFINDEX])) if nl80211.NL80211_ATTR_WDEV in attr: print("\twdev 0x%lx" % nl.nla_get_u64(attr[nl80211.NL80211_ATTR_WDEV])) if nl80211.NL80211_ATTR_MAC in attr: print("\tmac %02x:%02x:%02x:%02x:%02x:%02x" % tuple(nl.nla_data(attr[nl80211.NL80211_ATTR_MAC]))) if nl80211.NL80211_ATTR_SSID in attr: print("\tssid ", nl.nla_data(attr[nl80211.NL80211_ATTR_SSID])) if nl80211.NL80211_ATTR_IFTYPE in attr: iftype = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFTYPE]) print("\ttype %s" % nl80211.nl80211_iftype2str[iftype]) if nl80211.NL80211_ATTR_WIPHY_FREQ in attr: freq = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY_FREQ]) sys.stdout.write("\tfreq %d MHz" % freq) if nl80211.NL80211_ATTR_CHANNEL_WIDTH in attr: chanw = nl.nla_get_u32( attr[nl80211.NL80211_ATTR_CHANNEL_WIDTH]) sys.stdout.write(", width: %s" % nl80211.nl80211_chan_width2str[chanw]) if nl80211.NL80211_ATTR_CENTER_FREQ1 in attr: sys.stdout.write(", center1: %d MHz" % nl.nla_get_u32( attr[nl80211.NL80211_ATTR_CENTER_FREQ1])) if nl80211.NL80211_ATTR_CENTER_FREQ2 in attr: sys.stdout.write(", center2: %d MHz" % nl.nla_get_u32( attr[nl80211.NL80211_ATTR_CENTER_FREQ2])) elif nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE in attr: channel_type = nl.nla_get_u32( attr[nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE]) sys.stdout.write( " %s" % nl80211.nl80211_channel_type2str(channel_type)) sys.stdout.write("\n") return nl.NL_SKIP except Exception as e: (t, v, tb) = sys.exc_info() print v.message traceback.print_tb(tb)
def _protocol_feature_handler(self, m, a): try: e, attrs = py_genlmsg_parse(nlmsg_hdr(m), 0, nl80211.ATTR_MAX, None) if nl80211.ATTR_PROTOCOL_FEATURES in attrs: self._features = nla_get_u32(attrs[nl80211.ATTR_PROTOCOL_FEATURES]) return NL_SKIP except Exception as e: (t,v,tb) = sys.exc_info() print v.message traceback.print_tb(tb) pass
def _protocol_feature_handler(self, m, a): try: e, attrs = py_genlmsg_parse(nlmsg_hdr(m), 0, nl80211.ATTR_MAX, None) if nl80211.ATTR_PROTOCOL_FEATURES in attrs: self._features = nla_get_u32(attrs[nl80211.ATTR_PROTOCOL_FEATURES]) return NL_SKIP except Exception as e: (t,v,tb) = sys.exc_info() print(v.message) traceback.print_tb(tb) pass
def store_attrs(self, attrs): for aid in attrs.keys(): try: pol = self._policy[aid] if pol.type == NLA_S8: pol.type = nl.NLA_U8 pol.signed = True elif pol.type == NLA_S16: pol.type = nl.NLA_U16 pol.signed = True elif pol.type == NLA_S32: pol.type = nl.NLA_U32 pol.signed = True elif pol.type == NLA_S64: pol.type = nl.NLA_U64 pol.signed = True if pol.type == NLA_NUL_STRING: self._attrs[aid] = nl.nla_get_string(attrs[aid]) elif pol.type == nl.NLA_U64: self._attrs[aid] = nl.nla_get_u64(attrs[aid]) elif pol.type == nl.NLA_U32: self._attrs[aid] = nl.nla_get_u32(attrs[aid]) elif pol.type == nl.NLA_U16: self._attrs[aid] = nl.nla_get_u16(attrs[aid]) elif pol.type == nl.NLA_U8: self._attrs[aid] = nl.nla_get_u8(attrs[aid]) elif pol.type == nl.NLA_FLAG: self._attrs[aid] = True elif pol.type == nl.NLA_NESTED: if hasattr(pol, 'single') and pol.single: obj = self.create_nested(attrs[aid], aid) elif hasattr(pol, 'map') and pol.map: if not hasattr(pol, 'list_type'): raise Exception( 'need to specify "list_type" for map') obj = self.create_map(attrs[aid], pol) elif hasattr(pol, 'list_type'): obj = self.create_list(attrs[aid], pol) else: obj = self.create_nested_list(attrs[aid], aid) self.merge_nested_attrs(aid, obj) elif pol.type in [NLA_BINARY, nl.NLA_UNSPEC]: self._attrs[aid] = nl.nla_data(attrs[aid]) if hasattr(pol, 'signed') and pol.signed: self._attrs[aid] = self.convert_sign(self._attrs[aid], pol) except Exception as e: print e.message self._attrs[aid] = nl.nla_data(attrs[aid]) self.post_store_attrs(attrs)
def store_attrs(self, attrs): for aid in attrs.keys(): try: pol = self._policy[aid] if pol.type == NLA_S8: pol.type = NLA_U8 pol.signed = True elif pol.type == NLA_S16: pol.type = NLA_U16 pol.signed = True elif pol.type == NLA_S32: pol.type = NLA_U32 pol.signed = True elif pol.type == NLA_S64: pol.type = NLA_U64 pol.signed = True if pol.type == NLA_NUL_STRING: self._attrs[aid] = nla_get_string(attrs[aid]) elif pol.type == NLA_U64: self._attrs[aid] = nla_get_u64(attrs[aid]) elif pol.type == NLA_U32: self._attrs[aid] = nla_get_u32(attrs[aid]) elif pol.type == NLA_U16: self._attrs[aid] = nla_get_u16(attrs[aid]) elif pol.type == NLA_U8: self._attrs[aid] = nla_get_u8(attrs[aid]) elif pol.type == NLA_FLAG: self._attrs[aid] = True elif pol.type == NLA_NESTED: if hasattr(pol, 'single') and pol.single: obj = self.create_nested(attrs[aid], aid) elif hasattr(pol, 'map') and pol.map: if not hasattr(pol, 'list_type'): raise Exception('need to specify "list_type" for map') obj = self.create_map(attrs[aid], pol) elif hasattr(pol, 'list_type'): obj = self.create_list(attrs[aid], pol) else: obj = self.create_nested_list(attrs[aid], aid) self.merge_nested_attrs(aid, obj) elif pol.type in [ NLA_BINARY, NLA_UNSPEC ]: self._attrs[aid] = nla_data(attrs[aid]) if hasattr(pol, 'signed') and pol.signed: self._attrs[aid] = self.convert_sign(self._attrs[aid], pol) except Exception as e: print e.message self._attrs[aid] = nla_data(attrs[aid]) self.post_store_attrs(attrs)
def handle(self, msg, arg): try: e, attrs = genl.py_genlmsg_parse(nl.nlmsg_hdr(msg), 0, nl80211.ATTR_MAX, None) if nl80211.ATTR_WIPHY in attrs: phynum = nl.nla_get_u32(attrs[nl80211.ATTR_WIPHY]) if phynum in self._wiphy.keys(): self._wiphy[phynum].store_attrs(attrs) else: phy = wiphy(self._access, attrs) self._wiphy[phy.phynum] = phy return nl.NL_SKIP except Exception as e: (t,v,tb) = sys.exc_info() print v.message traceback.print_tb(tb)
def handle(self, msg, arg): try: e, attrs = genl.py_genlmsg_parse(nl.nlmsg_hdr(msg), 0, nl80211.ATTR_MAX, None) if nl80211.ATTR_WIPHY in attrs: phynum = nl.nla_get_u32(attrs[nl80211.ATTR_WIPHY]) if phynum in self._wiphy.keys(): self._wiphy[phynum].store_attrs(attrs) else: phy = factory.get_inst().create(wiphy, self._access, attrs) self._wiphy[phy.phynum] = phy return nl.NL_SKIP except Exception as e: (t,v,tb) = sys.exc_info() print v.message traceback.print_tb(tb)
def msg_handler(m, a): try: e, attr = genl.py_genlmsg_parse(nl.nlmsg_hdr(m), 0, nl80211.NL80211_ATTR_MAX, None) if nl80211.NL80211_ATTR_WIPHY in attr: thiswiphy = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY]) print("phy#%d" % thiswiphy) if nl80211.NL80211_ATTR_IFNAME in attr: print("\tinterface %s" % nl.nla_get_string(attr[nl80211.NL80211_ATTR_IFNAME])); if nl80211.NL80211_ATTR_IFINDEX in attr: print("\tifindex %d" % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFINDEX])) if nl80211.NL80211_ATTR_WDEV in attr: print("\twdev 0x%lx" % nl.nla_get_u64(attr[nl80211.NL80211_ATTR_WDEV])) if nl80211.NL80211_ATTR_MAC in attr: print("\tmac %02x:%02x:%02x:%02x:%02x:%02x" % tuple(nl.nla_data(attr[nl80211.NL80211_ATTR_MAC]))) if nl80211.NL80211_ATTR_SSID in attr: print("\tssid ", nl.nla_data(attr[nl80211.NL80211_ATTR_SSID])) if nl80211.NL80211_ATTR_IFTYPE in attr: iftype = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFTYPE]) print("\ttype %s" % nl80211.nl80211_iftype2str[iftype]) if nl80211.NL80211_ATTR_WIPHY_FREQ in attr: freq = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY_FREQ]) sys.stdout.write("\tfreq %d MHz" % freq); if nl80211.NL80211_ATTR_CHANNEL_WIDTH in attr: chanw = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CHANNEL_WIDTH]) sys.stdout.write(", width: %s" % nl80211.nl80211_chan_width2str[chanw]) if nl80211.NL80211_ATTR_CENTER_FREQ1 in attr: sys.stdout.write(", center1: %d MHz" % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CENTER_FREQ1])) if nl80211.NL80211_ATTR_CENTER_FREQ2 in attr: sys.stdout.write(", center2: %d MHz" % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CENTER_FREQ2])) elif nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE in attr: channel_type = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE]) sys.stdout.write(" %s" % nl80211.nl80211_channel_type2str(channel_type)); sys.stdout.write("\n"); return nl.NL_SKIP; except Exception as e: (t,v,tb) = sys.exc_info() print v.message traceback.print_tb(tb)
def create_list(self, attr_list, pol): nest_list = [] item_type = pol.list_type for item in nl.nla_get_nested(attr_list): if item_type == NLA_NUL_STRING: nest_obj = nl.nla_get_string(item) elif item_type == nl.NLA_U64: nest_obj = nl.nla_get_u64(item) elif item_type == nl.NLA_U32: nest_obj = nl.nla_get_u32(item) elif item_type == nl.NLA_U16: nest_obj = nl.nla_get_u16(item) elif item_type == nl.NLA_U8: nest_obj = nl.nla_get_u8(item) else: raise Exception("type (%d) not supported for list" % item_type) nest_list.append(nest_obj) return nest_list
def create_list(self, attr_list, pol): nest_list = [] item_type = pol.list_type for item in nla_get_nested(attr_list): if item_type == NLA_NUL_STRING: nest_obj = nla_get_string(item) elif item_type == NLA_U64: nest_obj = nla_get_u64(item) elif item_type == NLA_U32: nest_obj = nla_get_u32(item) elif item_type == NLA_U16: nest_obj = nla_get_u16(item) elif item_type == NLA_U8: nest_obj = nla_get_u8(item) else: raise Exception("type (%d) not supported for list" % item_type) nest_list.append(nest_obj) return nest_list
def store_attrs(self, attrs): for attr in attrs.keys(): try: pol = self._policy[attr] if pol.type == NLA_NUL_STRING: self._attrs[attr] = nl.nla_get_string(attrs[attr]) elif pol.type == nl.NLA_U64: self._attrs[attr] = nl.nla_get_u64(attrs[attr]) elif pol.type == nl.NLA_U32: self._attrs[attr] = nl.nla_get_u32(attrs[attr]) elif pol.type == nl.NLA_U16: self._attrs[attr] = nl.nla_get_u16(attrs[attr]) elif pol.type == nl.NLA_U8: self._attrs[attr] = nl.nla_get_u8(attrs[attr]) elif pol.type == nl.NLA_FLAG: self._attrs[attr] = True elif pol.type == nl.NLA_NESTED: self.store_nested(attrs[attr], attr) elif pol.type in [NLA_BINARY, nl.NLA_UNSPEC]: self._attrs[attr] = nl.nla_data(attrs[attr]) except Exception as e: print e.message self._attrs[attr] = nl.nla_data(attrs[attr])
def store_attrs(self, attrs): for attr in attrs.keys(): try: pol = self._policy[attr] if pol.type == NLA_NUL_STRING: self._attrs[attr] = nl.nla_get_string(attrs[attr]) elif pol.type == nl.NLA_U64: self._attrs[attr] = nl.nla_get_u64(attrs[attr]) elif pol.type == nl.NLA_U32: self._attrs[attr] = nl.nla_get_u32(attrs[attr]) elif pol.type == nl.NLA_U16: self._attrs[attr] = nl.nla_get_u16(attrs[attr]) elif pol.type == nl.NLA_U8: self._attrs[attr] = nl.nla_get_u8(attrs[attr]) elif pol.type == nl.NLA_FLAG: self._attrs[attr] = True elif pol.type == nl.NLA_NESTED: self.store_nested(attrs[attr], attr) elif pol.type in [ NLA_BINARY, nl.NLA_UNSPEC ]: self._attrs[attr] = nl.nla_data(attrs[attr]) except Exception as e: print e.message self._attrs[attr] = nl.nla_data(attrs[attr])
def __init__(self, access, attrs): nl80211_managed_object.__init__(self, access, attrs, nl80211_policy) self._phynum = nl.nla_get_u32(attrs[nl80211.ATTR_WIPHY])
def handle(self, m): logger.debug("Handle Msg from class") print("m",m) try: logger.debug("getting msg header") nlmsghdr = nl.nlmsg_hdr(m) # print("nlmsghdr: flags:", nlmsghdr.nlmsg_flags , "seq:", nlmsghdr.nlmsg_seq ) logger.debug("getting genlmsg header") genlhdr = genl.genlmsg_hdr( nlmsghdr ) # logger.debug("Generic Command", genlhdr.cmd) if not genlhdr: logger.error("Could not get generic header") return nl.NL_STOP if genlhdr.cmd == ELC_REQUEST_RLOCS_FOR_EID: logger.info("Request RLOC for an EID") # attrs = None # print("Message handler got called"); err, attrs = genl.py_genlmsg_parse( nlmsghdr, 0, # will be returned as an attribute ELA_MAX, None ) if err < 0: logger.error("An error happened while parsing attributes") return nl.NL_STOP; logger.info("Looking for ELA") if ELA_EID in attrs: # logger.debug ("hello", attrs[ELA_EID]) eid = nl.nla_get_u32(attrs[ELA_EID]); # logger.info ("eid", eid) # logger.debug ("token", attrs[ELA_MPTCP_TOKEN]) token = nl.nla_get_u32(attrs[ELA_MPTCP_TOKEN]); # logger.info("token", token) # print("Requested EID ",eid, " for token ",binascii.hexlify( token )) # I => unsigned int packed_value = struct.pack('I', eid) addr = socket.inet_ntoa(packed_value) nb = self.retrieve_number_of_rlocs( addr ) if nb < 0: logger.warning("An error happened while retrieveing nb of rlocs") return nl.NL_STOP else: #nlmsghdr.nlmsg_seq + 1 self.send_rlocs_list_for_eid( 0, token, nb ) return nl.NL_SKIP else: logger.error("Missing critical attribute in packet") else: logger.warning("Unhandled command %d"% genlhdr.cmd) # nlmsg_data returns void* so not usable straightaway # TODO need to retrieve command # print("e", err) return nl.NL_SKIP except Exception as e: (t,v,tb) = sys.exc_info() print( "test", v.message,e ) traceback.print_tb(tb) return nl.NL_SKIP
def handle(self, m): logger.debug("Handle Msg from class") print("m", m) try: logger.debug("getting msg header") nlmsghdr = nl.nlmsg_hdr(m) # print("nlmsghdr: flags:", nlmsghdr.nlmsg_flags , "seq:", nlmsghdr.nlmsg_seq ) logger.debug("getting genlmsg header") genlhdr = genl.genlmsg_hdr(nlmsghdr) # logger.debug("Generic Command", genlhdr.cmd) if not genlhdr: logger.error("Could not get generic header") return nl.NL_STOP if genlhdr.cmd == ELC_REQUEST_RLOCS_FOR_EID: logger.info("Request RLOC for an EID") # attrs = None # print("Message handler got called"); err, attrs = genl.py_genlmsg_parse( nlmsghdr, 0, # will be returned as an attribute ELA_MAX, None) if err < 0: logger.error("An error happened while parsing attributes") return nl.NL_STOP logger.info("Looking for ELA") if ELA_EID in attrs: # logger.debug ("hello", attrs[ELA_EID]) eid = nl.nla_get_u32(attrs[ELA_EID]) # logger.info ("eid", eid) # logger.debug ("token", attrs[ELA_MPTCP_TOKEN]) token = nl.nla_get_u32(attrs[ELA_MPTCP_TOKEN]) # logger.info("token", token) # print("Requested EID ",eid, " for token ",binascii.hexlify( token )) # I => unsigned int packed_value = struct.pack('I', eid) addr = socket.inet_ntoa(packed_value) nb = self.retrieve_number_of_rlocs(addr) if nb < 0: logger.warning( "An error happened while retrieveing nb of rlocs") return nl.NL_STOP else: #nlmsghdr.nlmsg_seq + 1 self.send_rlocs_list_for_eid(0, token, nb) return nl.NL_SKIP else: logger.error("Missing critical attribute in packet") else: logger.warning("Unhandled command %d" % genlhdr.cmd) # nlmsg_data returns void* so not usable straightaway # TODO need to retrieve command # print("e", err) return nl.NL_SKIP except Exception as e: (t, v, tb) = sys.exc_info() print("test", v.message, e) traceback.print_tb(tb) return nl.NL_SKIP
def __init__(self, access, attrs): nl80211_managed_object.__init__(self, access, attrs, nl80211_policy) self._phynum = nla_get_u32(attrs[nl80211.ATTR_WIPHY])