/* ----------------GET CMD WITH INT ARG REPLY-------------------- */ static int print_reply_rx_count_data(struct nl_msg *msg, void *arg) { struct nlattr *tb_msg[NLNPI_ATTR_MAX + 1]; struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); unsigned int count[3]; nla_parse(tb_msg, NLNPI_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (tb_msg[NLNPI_ATTR_REPLY_DATA]) { if (nla_len(tb_msg[NLNPI_ATTR_REPLY_DATA]) == 12) { memcpy(count, nla_data(tb_msg[NLNPI_ATTR_REPLY_DATA]), sizeof(count)); printf ("ret: reg value: rx_end_count=%d rx_err_end_count=%d fcs_fail_count=%d :end\n", count[0], count[1], count[2]); } else { printf("ret: Invild len %d :end\n", nla_len(tb_msg[NLNPI_ATTR_REPLY_DATA])); } } else { printf("ret: Failed to get result! :end\n"); } return NL_SKIP; }
// valid interface callback handler static int iface_handler(struct nl_msg *msg, void *arg) { struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (tb_msg[NL80211_ATTR_WIPHY]) { char wiphy[IFNAMSIZ]; sprintf(wiphy, "phy%d", nla_get_u32(tb_msg[NL80211_ATTR_WIPHY])); // If selected physical interface matches the result, search for a monitor interface and copy the name if(strcmp(phy_name, wiphy) == 0) { if (tb_msg[NL80211_ATTR_IFTYPE] && tb_msg[NL80211_ATTR_IFNAME]) { // If the interface type is monitor if(nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE]) == 6) strcpy(IF_name, nla_get_string(tb_msg[NL80211_ATTR_IFNAME])); } } } return NL_SKIP; }
static int display_rx_statcs(struct nl_msg *msg, void *arg) { struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct nlattr *td[WL1271_TM_ATTR_MAX + 1]; struct wl1271_radio_rx_statcs *prms; nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (!tb[NL80211_ATTR_TESTDATA]) { fprintf(stderr, "no data!\n"); return NL_SKIP; } nla_parse(td, WL1271_TM_ATTR_MAX, nla_data(tb[NL80211_ATTR_TESTDATA]), nla_len(tb[NL80211_ATTR_TESTDATA]), NULL); prms = (struct wl1271_radio_rx_statcs *) nla_data(td[WL1271_TM_ATTR_DATA]); printf("\n\tTotal number of pkts\t- %d\n\tAccepted pkts\t\t- %d\n\t" "FCS error pkts\t\t- %d\n\tAddress mismatch pkts\t- %d\n\t" "Average SNR\t\t- % d dBm\n\tAverage RSSI\t\t- % d dBm\n\n", prms->base_pkt_id, prms->rx_path_statcs.nbr_rx_valid_pkts, prms->rx_path_statcs.nbr_rx_fcs_err_pkts, prms->rx_path_statcs.nbr_rx_plcp_err_pkts, (signed short)prms->rx_path_statcs.ave_snr/8, (signed short)prms->rx_path_statcs.ave_rssi/8); return NL_SKIP; }
int mac80211_get_coverageclass(char *interface) { struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct nl_msg *msg; struct genlmsghdr *gnlh; int phy; unsigned char coverage=0; phy = mac80211_get_phyidx_by_vifname(interface); if (phy == -1) return 0; msg = unl_genl_msg(&unl, NL80211_CMD_GET_WIPHY, false); NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, phy); if (unl_genl_request_single(&unl, msg, &msg) < 0) return 0; if (!msg) return 0; gnlh=nlmsg_data(nlmsg_hdr(msg)); nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (tb[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) { coverage = nla_get_u8(tb[NL80211_ATTR_WIPHY_COVERAGE_CLASS]); /* See handle_distance() for an explanation where the '450' comes from */ // printf("\tCoverage class: %d (up to %dm)\n", coverage, 450 * coverage); } // printf ("%d\n", coverage); nlmsg_free(msg); return coverage; nla_put_failure: nlmsg_free(msg); return 0; }
static int nl80211_iface_info_handler (struct nl_msg *msg, void *arg) { struct nl80211_iface_info *info = arg; struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); struct nlattr *tb[NL80211_ATTR_MAX + 1]; if (nla_parse (tb, NL80211_ATTR_MAX, genlmsg_attrdata (gnlh, 0), genlmsg_attrlen (gnlh, 0), NULL) < 0) return NL_SKIP; if (!tb[NL80211_ATTR_IFTYPE]) return NL_SKIP; switch (nla_get_u32 (tb[NL80211_ATTR_IFTYPE])) { case NL80211_IFTYPE_ADHOC: info->mode = NM_802_11_MODE_ADHOC; break; case NL80211_IFTYPE_AP: info->mode = NM_802_11_MODE_AP; break; case NL80211_IFTYPE_STATION: info->mode = NM_802_11_MODE_INFRA; break; } return NL_SKIP; }
static int dump_mgmt_frame(struct nl_msg *msg, void *arg) { struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (tb_msg[NL80211_ATTR_WIPHY_FREQ]) { uint32_t freq = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY_FREQ]); printf("freq %u MHz\n", freq); } if (tb_msg[NL80211_ATTR_RX_SIGNAL_DBM]) { /* nl80211_send_mgmt sends signed dBm value as u32 */ int dbm = nla_get_u32(tb_msg[NL80211_ATTR_RX_SIGNAL_DBM]); printf("rssi %d dBm\n", dbm); } if (tb_msg[NL80211_ATTR_FRAME]) { int len = nla_len(tb_msg[NL80211_ATTR_FRAME]); uint8_t *data = nla_data(tb_msg[NL80211_ATTR_FRAME]); iw_hexdump("mgmt", data, len); } return 0; }
static int ac_kmod_valid_handler(struct nl_msg* msg, void* data) { struct nlattr* tb_msg[NLSMARTCAPWAP_ATTR_MAX + 1]; struct genlmsghdr* gnlh = nlmsg_data(nlmsg_hdr(msg)); nla_parse(tb_msg, NLSMARTCAPWAP_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); return ac_kmod_event_handler(gnlh, tb_msg, data); }
int SoftapController::linkDumpCbHandler(struct nl_msg *msg, void *arg) { struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct genlmsghdr *gnlh = (genlmsghdr *)nlmsg_data(nlmsg_hdr(msg)); struct nlattr *bss[NL80211_BSS_MAX + 1]; int *sta_freq = (int *)arg; // bah - cpp doesn't support C99 named initializers. do it manually memset(&link_bss_policy, 0, sizeof(link_bss_policy)); link_bss_policy[NL80211_BSS_TSF].type = NLA_U64; link_bss_policy[NL80211_BSS_FREQUENCY].type = NLA_U32; //link_bss_policy[NL80211_BSS_BSSID] = { }; link_bss_policy[NL80211_BSS_BEACON_INTERVAL].type = NLA_U16; link_bss_policy[NL80211_BSS_CAPABILITY].type = NLA_U16; //link_bss_policy[NL80211_BSS_INFORMATION_ELEMENTS] = { }; link_bss_policy[NL80211_BSS_SIGNAL_MBM].type = NLA_U32; link_bss_policy[NL80211_BSS_SIGNAL_UNSPEC].type = NLA_U8; link_bss_policy[NL80211_BSS_STATUS].type = NLA_U32; nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (!tb[NL80211_ATTR_BSS]) { LOGD("bss info missing!"); return NL_SKIP; } if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], link_bss_policy)) { LOGD("failed to parse nested attributes!"); return NL_SKIP; } if (!bss[NL80211_BSS_BSSID]) return NL_SKIP; if (!bss[NL80211_BSS_STATUS]) return NL_SKIP; switch (nla_get_u32(bss[NL80211_BSS_STATUS])) { case NL80211_BSS_STATUS_ASSOCIATED: break; case NL80211_BSS_STATUS_AUTHENTICATED: case NL80211_BSS_STATUS_IBSS_JOINED: default: return NL_SKIP; } /* only in the assoc case do we want more info from station get */ if (bss[NL80211_BSS_FREQUENCY]) { *sta_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]); LOGD("sta freq: %d", *sta_freq); } return NL_SKIP; }
static int calib_valid_handler(struct nl_msg *msg, void *arg) { struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct nlattr *td[WL1271_TM_ATTR_MAX + 1]; struct wl1271_cmd_cal_p2g *prms; #if 0 int i; unsigned char *pc; #endif nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (!tb[NL80211_ATTR_TESTDATA]) { fprintf(stderr, "no data!\n"); return NL_SKIP; } nla_parse(td, WL1271_TM_ATTR_MAX, nla_data(tb[NL80211_ATTR_TESTDATA]), nla_len(tb[NL80211_ATTR_TESTDATA]), NULL); prms = (struct wl1271_cmd_cal_p2g *)nla_data(td[WL1271_TM_ATTR_DATA]); if (prms->radio_status) { fprintf(stderr, "Fail to calibrate ith radio status (%d)\n", (signed short)prms->radio_status); return 2; } #if 0 printf("%s> id %04x status %04x\ntest id %02x ver %08x len %04x=%d\n", __func__, prms->header.id, prms->header.status, prms->test.id, prms->ver, prms->len, prms->len); pc = (unsigned char *)prms->buf; printf("++++++++++++++++++++++++\n"); for (i = 0; i < prms->len; i++) { if (i%0xf == 0) printf("\n"); printf("%02x ", *(unsigned char *)pc); pc += 1; } printf("++++++++++++++++++++++++\n"); #endif if (prepare_nvs_file(prms, arg)) { fprintf(stderr, "Fail to prepare calibrated NVS file\n"); return 2; } #if 0 printf("\n\tThe NVS file (%s) is ready\n\tCopy it to %s and " "reboot the system\n\n", NEW_NVS_NAME, CURRENT_NVS_NAME); #endif return NL_SKIP; }
static int print_iface_handler(struct nl_msg *msg, void *arg) { struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct nlattr *tb_msg[NL802154_ATTR_MAX + 1]; unsigned int *wpan_phy = arg; const char *indent = ""; nla_parse(tb_msg, NL802154_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (wpan_phy && tb_msg[NL802154_ATTR_WPAN_PHY]) { unsigned int thiswpan_phy = nla_get_u32(tb_msg[NL802154_ATTR_WPAN_PHY]); indent = "\t"; if (*wpan_phy != thiswpan_phy) printf("phy#%d\n", thiswpan_phy); *wpan_phy = thiswpan_phy; } if (tb_msg[NL802154_ATTR_IFNAME]) printf("%sInterface %s\n", indent, nla_get_string(tb_msg[NL802154_ATTR_IFNAME])); else printf("%sUnnamed/non-netdev interface\n", indent); if (tb_msg[NL802154_ATTR_IFINDEX]) printf("%s\tifindex %d\n", indent, nla_get_u32(tb_msg[NL802154_ATTR_IFINDEX])); if (tb_msg[NL802154_ATTR_WPAN_DEV]) printf("%s\twpan_dev 0x%llx\n", indent, (unsigned long long)nla_get_u64(tb_msg[NL802154_ATTR_WPAN_DEV])); if (tb_msg[NL802154_ATTR_EXTENDED_ADDR]) printf("%s\textended_addr 0x%016" PRIx64 "\n", indent, le64toh(nla_get_u64(tb_msg[NL802154_ATTR_EXTENDED_ADDR]))); if (tb_msg[NL802154_ATTR_SHORT_ADDR]) printf("%s\tshort_addr 0x%04x\n", indent, le16toh(nla_get_u16(tb_msg[NL802154_ATTR_SHORT_ADDR]))); if (tb_msg[NL802154_ATTR_PAN_ID]) printf("%s\tpan_id 0x%04x\n", indent, le16toh(nla_get_u16(tb_msg[NL802154_ATTR_PAN_ID]))); if (tb_msg[NL802154_ATTR_IFTYPE]) printf("%s\ttype %s\n", indent, iftype_name(nla_get_u32(tb_msg[NL802154_ATTR_IFTYPE]))); if (tb_msg[NL802154_ATTR_MAX_FRAME_RETRIES]) printf("%s\tmax_frame_retries %d\n", indent, nla_get_s8(tb_msg[NL802154_ATTR_MAX_FRAME_RETRIES])); if (tb_msg[NL802154_ATTR_MIN_BE]) printf("%s\tmin_be %d\n", indent, nla_get_u8(tb_msg[NL802154_ATTR_MIN_BE])); if (tb_msg[NL802154_ATTR_MAX_BE]) printf("%s\tmax_be %d\n", indent, nla_get_u8(tb_msg[NL802154_ATTR_MAX_BE])); if (tb_msg[NL802154_ATTR_MAX_CSMA_BACKOFFS]) printf("%s\tmax_csma_backoffs %d\n", indent, nla_get_u8(tb_msg[NL802154_ATTR_MAX_CSMA_BACKOFFS])); if (tb_msg[NL802154_ATTR_LBT_MODE]) printf("%s\tlbt %d\n", indent, nla_get_u8(tb_msg[NL802154_ATTR_LBT_MODE])); if (tb_msg[NL802154_ATTR_ACKREQ_DEFAULT]) printf("%s\tackreq_default %d\n", indent, nla_get_u8(tb_msg[NL802154_ATTR_ACKREQ_DEFAULT])); return NL_SKIP; }
/** * Validate Generic Netlink message including attributes * @arg nlh Pointer to Netlink message header * @arg hdrlen Length of user header * @arg maxtype Maximum attribtue id expected * @arg policy Attribute validation policy * * Verifies the validity of the Netlink and Generic Netlink headers using * genlmsg_valid_hdr() and calls nla_validate() on the message payload to * verify the integrity of eventual attributes. * * @note You may call genlmsg_parse() directly to perform validation and * parsing in a single step. * * @see genlmsg_valid_hdr() * @see nla_validate() * @see genlmsg_parse() * * @return 0 on success or a negative error code. */ int genlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype, const struct nla_policy *policy) { struct genlmsghdr *ghdr; if (!genlmsg_valid_hdr(nlh, hdrlen)) return -NLE_MSG_TOOSHORT; ghdr = nlmsg_data(nlh); return nla_validate(genlmsg_attrdata(ghdr, hdrlen), genlmsg_attrlen(ghdr, hdrlen), maxtype, policy); }
/** * Parse Generic Netlink message including attributes * @arg nlh Pointer to Netlink message header * @arg hdrlen Length of user header * @arg tb Array to store parsed attributes * @arg maxtype Maximum attribute id expected * @arg policy Attribute validation policy * * Verifies the validity of the Netlink and Generic Netlink headers using * genlmsg_valid_hdr() and calls nla_parse() on the message payload to * parse eventual attributes. * * @par Example: * @code * struct nlattr *attrs[MY_TYPE_MAX+1]; * * if ((err = genlsmg_parse(nlmsg_nlh(msg), sizeof(struct my_hdr), attrs, * MY_TYPE_MAX, attr_policy)) < 0) * // ERROR * @endcode * * @see genlmsg_valid_hdr() * @see genlmsg_validate() * @see nla_parse() * * @return 0 on success or a negative error code. */ int genlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy) { struct genlmsghdr *ghdr; if (!genlmsg_valid_hdr(nlh, hdrlen)) return -NLE_MSG_TOOSHORT; ghdr = nlmsg_data(nlh); return nla_parse(tb, maxtype, genlmsg_attrdata(ghdr, hdrlen), genlmsg_attrlen(ghdr, hdrlen), policy); }
int genlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype, struct nla_policy *policy) { struct genlmsghdr *ghdr; if (!genlmsg_valid_hdr(nlh, hdrlen)) return nl_errno(EINVAL); ghdr = nlmsg_data(nlh); return nla_validate(genlmsg_attrdata(ghdr, hdrlen), genlmsg_attrlen(ghdr, hdrlen), maxtype, policy); }
//scan netlink messages are nested (and may even be multipart) (see nl80211.c line 2591: nl80211_send_bss) int CNL80211::parseNlScanResult(nl_msg * msg) { struct nlattr * attr_buffer[NL80211_ATTR_MAX + 1]; struct nlmsghdr * msg_hdr = nlmsg_hdr(msg); struct genlmsghdr * msg_header = (struct genlmsghdr *) nlmsg_data(msg_hdr); struct nlattr * bss_buffer[NL80211_BSS_MAX + 1]; //bss = basic service set ScanResult scanresult; //This is the struct to check the validity of the attributes. See enum nl80211_bss struct nla_policy bss_policy[NL80211_BSS_MAX + 1]; bss_policy[NL80211_BSS_TSF].type = NLA_U64; bss_policy[NL80211_BSS_FREQUENCY].type = NLA_U32; bss_policy[NL80211_BSS_BEACON_INTERVAL].type = NLA_U16; bss_policy[NL80211_BSS_SIGNAL_MBM].type = NLA_U32; bss_policy[NL80211_BSS_SIGNAL_UNSPEC].type = NLA_U8; if (msg_hdr->nlmsg_flags & NLM_F_MULTI) qDebug() << "netlink: Mutlipart message"; //Parse the complete message nla_parse(attr_buffer, NL80211_ATTR_MAX, genlmsg_attrdata(msg_header, 0), genlmsg_attrlen(msg_header, 0), NULL); if (!attr_buffer[NL80211_ATTR_BSS]) { //Check if BSS return NL_SKIP; } //Parse the nested attributes. this is where the scan results are if (nla_parse_nested(bss_buffer, NL80211_BSS_MAX, attr_buffer[NL80211_ATTR_BSS], bss_policy)) { return NL_SKIP; } if (!bss_buffer[NL80211_BSS_BSSID]) return NL_SKIP; scanresult.bssid = libnutcommon::MacAddress((ether_addr*)(bss_buffer[NL80211_BSS_BSSID])); scanresult.signal.bssid = scanresult.bssid; if (bss_buffer[NL80211_BSS_FREQUENCY]) scanresult.freq = nla_get_u32(bss_buffer[NL80211_BSS_FREQUENCY]); if (bss_buffer[NL80211_BSS_SIGNAL_MBM]) { scanresult.signal.type = WSR_RCPI; scanresult.signal.quality.value = nla_get_u32(bss_buffer[NL80211_BSS_SIGNAL_MBM])/100; } if (bss_buffer[NL80211_BSS_SIGNAL_UNSPEC]) { scanresult.signal.type = WSR_UNKNOWN; scanresult.signal.quality.value = nla_get_u8(bss_buffer[NL80211_BSS_SIGNAL_UNSPEC]); } // if (bss_buffer[NL80211_BSS_INFORMATION_ELEMENTS]) // print_ies(nla_data(bss_buffer[NL80211_BSS_INFORMATION_ELEMENTS]), // nla_len(bss_buffer[NL80211_BSS_INFORMATION_ELEMENTS]), // params->unknown, params->type); m_scanResults.push_back(scanresult); return NL_SKIP; }
static int cb_kmod_create_iface(struct nl_msg* msg, void* data) { struct nlattr* tb_msg[NLSMARTCAPWAP_ATTR_MAX + 1]; struct genlmsghdr* gnlh = nlmsg_data(nlmsg_hdr(msg)); uint32_t* ifindex = (uint32_t*)data; nla_parse(tb_msg, NLSMARTCAPWAP_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (tb_msg[NLSMARTCAPWAP_ATTR_IFPHY_INDEX]) { *ifindex = nla_get_u32(tb_msg[NLSMARTCAPWAP_ATTR_IFPHY_INDEX]); } return NL_SKIP; }
static int event_handler(struct nl_msg *msg, void *arg) { struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct nlattr *tb[NL80211_ATTR_MAX + 1]; int cmd = gnlh->cmd; uint8_t *pos; int i; nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); switch (gnlh->cmd) { case NL80211_CMD_FRAME: if (tb[NL80211_ATTR_FRAME] && nla_len(tb[NL80211_ATTR_FRAME])) { pos = nla_data(tb[NL80211_ATTR_FRAME]); if (*(pos + 24) == 0x20) { switch (*(pos + 25)) { case MPATH_PREQ: printf("Path Request Frame "); break; case MPATH_PREP: printf("Path Reply Frame "); break; case MPATH_PERR: printf("Path Error Frame "); break; } printf("from %02x:%02x:%02x:%02x:%02x:%02x\n", *(pos + 10), *(pos + 11), *(pos + 12), *(pos + 13), *(pos + 14), *(pos + 15)); } printf("----------\n"); printf("frame hexdump: "); for (i=0; i<nla_len(tb[NL80211_ATTR_FRAME]); i++) { if (!(i%20)) printf("\n"); printf("%02x ", *pos++); } printf("\n----------\n\n"); } break; case NL80211_CMD_NEW_STATION: printf("NL80211_CMD_NEW_STATION :)\n"); break; default: printf("Ignored event\n"); break; } return NL_SKIP; }
static int protocol_feature_handler(struct nl_msg *msg, void *arg) { u32 *feat = arg; struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]) *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]); return NL_SKIP; }
static int get_cw(struct nl_msg *msg, void *arg) { struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; unsigned int *wiphy = arg; printf("atlasd: got message!\n"); nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); print_all_attrs(tb_msg); //nl_msg_dump(msg, stdout); return NL_SKIP; }
static int valid_handler(struct nl_msg *msg, void *arg) { struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct nlattr *head_attr = genlmsg_attrdata(gnlh, 0); int attr_len = genlmsg_attrlen(gnlh, 0); LOG_DBG_("%s\n", __func__); (void) arg; if (print_ascii) return write_ascii(1, (uint8_t *) head_attr, attr_len); (void) write(1, (uint8_t *) head_attr, attr_len); return NL_OK; }
static int cb_handler(struct nl_msg * msg, void * arg) { * (int*) arg = 123; // cbarg struct nlmsghdr * hdr = nlmsg_hdr(msg); struct genlmsghdr * gnlh = nlmsg_data(hdr); int valid = genlmsg_validate(hdr, 0, DEMO_ATTR_MAX, demo_gnl_policy); printf("valid %d %s\n", valid, valid ? "ERROR" : "OK"); // one way struct nlattr * attrs[DEMO_ATTR_MAX + 1]; if (genlmsg_parse(hdr, 0, attrs, DEMO_ATTR_MAX, demo_gnl_policy) < 0) { printf("genlsmg_parse ERROR\n"); } else { printf("genlsmg_parse OK\n"); printf("attr1 %s\n", nla_get_string(attrs[DEMO_ATTR1_STRING])); printf("attr2 %x\n", nla_get_u16(attrs[DEMO_ATTR2_UINT16])); struct attr_custom * cp = (struct attr_custom *) nla_data(attrs[DEMO_ATTR3_CUSTOM]); printf("attr3 %d %ld %f %lf\n", cp->a, cp->b, cp->c,cp->d); } // another way printf("gnlh->cmd %d\n", gnlh->cmd); //--- DEMO_CMD_ECHO int remaining = genlmsg_attrlen(gnlh, 0); struct nlattr * attr = genlmsg_attrdata(gnlh, 0); while (nla_ok(attr, remaining)) { printf("remaining %d\n", remaining); printf("attr @ %p\n", attr); // nla_get_string(attr) attr = nla_next(attr, &remaining); } nl_msg_dump(msg, stderr); return NL_STOP; }
int receive_ps_frames(struct nl_msg *msg, void *arg) { struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); struct nlattr *action_frame; printf("receive_ps_frames %d\n", __LINE__); nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (!tb[NL80211_ATTR_FRAME]) return NL_SKIP; else printf("frame size is %d in %p\n", nla_len(tb[NL80211_ATTR_FRAME]), nla_data(tb[NL80211_ATTR_FRAME])); return NL_SKIP; }
static int nfc_netlink_event_targets_found(struct genlmsghdr *gnlh) { struct nlattr *attr[NFC_ATTR_MAX + 1]; struct nl_msg *msg; void *hdr; int err; uint32_t adapter_idx; DBG(""); nla_parse(attr, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (!attr[NFC_ATTR_DEVICE_INDEX]) return -ENODEV; adapter_idx = nla_get_u32(attr[NFC_ATTR_DEVICE_INDEX]); DBG("adapter %d", adapter_idx); msg = nlmsg_alloc(); if (!msg) return -ENOMEM; hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0, NLM_F_DUMP, NFC_CMD_GET_TARGET, NFC_GENL_VERSION); if (!hdr) { err = -EINVAL; goto nla_put_failure; } err = -EMSGSIZE; NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, adapter_idx); err = __nl_send_msg(nfc_state->cmd_sock, msg, get_targets_handler, get_targets_finish_handler, &adapter_idx); nla_put_failure: nlmsg_free(msg); return err; }
static int nfc_netlink_event_adapter(struct genlmsghdr *gnlh, bool add) { struct nlattr *attrs[NFC_ATTR_MAX + 1]; uint32_t idx; DBG(""); nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (!attrs[NFC_ATTR_DEVICE_INDEX]) { near_error("Missing device index"); return -ENODEV; } idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]); if (add && (!attrs[NFC_ATTR_DEVICE_NAME] || !attrs[NFC_ATTR_PROTOCOLS])) { near_error("Missing attributes"); return -EINVAL; } if (add) { char *name; uint32_t protocols; bool powered; name = nla_get_string(attrs[NFC_ATTR_DEVICE_NAME]); protocols = nla_get_u32(attrs[NFC_ATTR_PROTOCOLS]); if (!attrs[NFC_ATTR_DEVICE_POWERED]) powered = false; else powered = nla_get_u8(attrs[NFC_ATTR_DEVICE_POWERED]); return __near_manager_adapter_add(idx, name, protocols, powered); } else { __near_manager_adapter_remove(idx); } return 0; }
/* ----------------GET CMD WITH INT USER DEFINED REPLY-------------------- */ static int print_get_tx_power_result(struct nl_msg *msg, void *arg) { struct nlattr *tb_msg[NLNPI_ATTR_MAX + 1]; struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); unsigned int data; unsigned short lev_a, lev_b; nla_parse(tb_msg, NLNPI_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (tb_msg[NLNPI_ATTR_REPLY_DATA]) { if (nla_len(tb_msg[NLNPI_ATTR_REPLY_DATA]) == 4) { data = *(unsigned int *) nla_data(tb_msg[NLNPI_ATTR_REPLY_DATA]); lev_a = data & 0x0000ffff; lev_b = (data & 0xffff0000) >> 16; printf("ret: level_a:%d,level_b:%d :end\n", lev_a, lev_b); } else {
static int save_attribute_handler(struct nl_msg *msg, void *arg) { struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); const char *name = "N/A"; struct wprobe_attribute *attr; int type = 0; struct wprobe_attr_cb *cb = arg; nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), attribute_policy); if (tb[WPROBE_ATTR_NAME]) name = nla_data(tb[WPROBE_ATTR_NAME]); attr = malloc(sizeof(struct wprobe_attribute) + strlen(name) + 1); if (!attr) return -1; memset(attr, 0, sizeof(struct wprobe_attribute)); if (tb[WPROBE_ATTR_ID]) attr->id = nla_get_u32(tb[WPROBE_ATTR_ID]); if (tb[WPROBE_ATTR_MAC] && cb->addr) memcpy(cb->addr, nla_data(tb[WPROBE_ATTR_MAC]), 6); if (tb[WPROBE_ATTR_FLAGS]) attr->flags = nla_get_u32(tb[WPROBE_ATTR_FLAGS]); if (tb[WPROBE_ATTR_TYPE]) type = nla_get_u8(tb[WPROBE_ATTR_TYPE]); if ((type < WPROBE_VAL_STRING) || (type > WPROBE_VAL_U64)) type = 0; attr->type = type; strcpy(attr->name, name); INIT_LIST_HEAD(&attr->list); list_add(&attr->list, cb->list); return 0; }
static int print_phy_handler(struct nl_msg *msg, void *arg) { struct nlattr *tb_msg[NL802154_ATTR_MAX + 1]; struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); int rem_page, i, ret; int64_t phy_id = -1; bool print_name = true; struct nlattr *nl_page; enum nl802154_cca_modes cca_mode; nla_parse(tb_msg, NL802154_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (tb_msg[NL802154_ATTR_WPAN_PHY]) { if (nla_get_u32(tb_msg[NL802154_ATTR_WPAN_PHY]) == phy_id) print_name = false; phy_id = nla_get_u32(tb_msg[NL802154_ATTR_WPAN_PHY]); } if (print_name && tb_msg[NL802154_ATTR_WPAN_PHY_NAME]) printf("wpan_phy %s\n", nla_get_string(tb_msg[NL802154_ATTR_WPAN_PHY_NAME])); /* TODO remove this handling it's deprecated */ if (tb_msg[NL802154_ATTR_CHANNELS_SUPPORTED]) { unsigned char page = 0; unsigned long channel; printf("supported channels:\n"); nla_for_each_nested(nl_page, tb_msg[NL802154_ATTR_CHANNELS_SUPPORTED], rem_page) { channel = nla_get_u32(nl_page); if (channel) { printf("\tpage %d: ", page); for (i = 0; i <= 31; i++) { if (channel & 0x1) printf("%d,", i); channel >>= 1; } /* TODO hack use sprintf here */ printf("\b \b\n"); } page++; } }
/** * Process NL80211_CMD_NEW_MPATH message * @param interf nl80211 listener interface * @param hdr pointer to netlink message header */ void nl80211_process_get_mpp_result(struct nl80211_if *interf, struct nlmsghdr *hdr) { struct oonf_layer2_destination *l2dst; struct oonf_layer2_neigh *l2neigh; struct netaddr remote_mac, destination_mac; #ifdef OONF_LOG_DEBUG_INFO struct netaddr_str nbuf1, nbuf2; #endif struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct genlmsghdr *gnlh; gnlh = nlmsg_data(hdr); nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (nl80211_get_if_index(interf) != nla_get_u32(tb[NL80211_ATTR_IFINDEX])) { /* wrong interface ? */ return; } netaddr_from_binary(&remote_mac, nla_data(tb[NL80211_ATTR_MPATH_NEXT_HOP]), 6, AF_MAC48); netaddr_from_binary(&destination_mac, nla_data(tb[NL80211_ATTR_MAC]), 6, AF_MAC48); l2neigh = oonf_layer2_neigh_get(interf->l2net, &remote_mac); if (!l2neigh) { /* don't create a neighbor, just ignore the MPP data */ return; } l2dst = nl80211_add_dst(l2neigh, &destination_mac); if (!l2dst) { return; } OONF_DEBUG(LOG_NL80211, "Neighbor %s was proxied by mesh node %s", netaddr_to_string(&nbuf1, &destination_mac), netaddr_to_string(&nbuf2, &remote_mac)); }
static int nfc_netlink_event_dep_down(struct genlmsghdr *gnlh) { struct nlattr *attrs[NFC_ATTR_MAX + 1]; uint32_t idx; DBG(""); nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (!attrs[NFC_ATTR_DEVICE_INDEX]) { near_error("Missing device index"); return -ENODEV; } idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]); __near_adapter_set_dep_state(idx, false); return 0; }
static int nfc_netlink_event_tm_deactivated(struct genlmsghdr *gnlh) { struct nlattr *attrs[NFC_ATTR_MAX + 1]; uint32_t idx; DBG(""); nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (!attrs[NFC_ATTR_DEVICE_INDEX]) { near_error("Missing device index"); return -ENODEV; } idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]); DBG("%d", idx); return __near_adapter_remove_device(idx); }
static int get_power_mode_handler(struct nl_msg *msg, void *arg) { struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); int *state = (int *)arg; nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL); if (!tb[NL80211_ATTR_PS_STATE]) return NL_SKIP; if (state) { *state = (int)nla_get_u32(tb[NL80211_ATTR_PS_STATE]); wpa_printf(MSG_DEBUG, "nl80211: Get power mode = %d", *state); *state = (*state == NL80211_PS_ENABLED) ? WPA_PS_ENABLED : WPA_PS_DISABLED; } return NL_SKIP; }