bool TaskstatsSocket::Open() { std::unique_ptr<nl_sock, decltype(&nl_socket_free)> nl( nl_socket_alloc(), nl_socket_free); if (!nl.get()) { LOG(ERROR) << "Failed to allocate netlink socket"; return false; } int ret = genl_connect(nl.get()); if (ret < 0) { LOG(ERROR) << nl_geterror(ret) << std::endl << "Unable to open netlink socket (are you root?)"; return false; } int family_id = genl_ctrl_resolve(nl.get(), TASKSTATS_GENL_NAME); if (family_id < 0) { LOG(ERROR) << nl_geterror(family_id) << std::endl << "Unable to determine taskstats family id (does your kernel support taskstats?)"; return false; } nl_ = std::move(nl); family_id_ = family_id; return true; }
int dhd_nl_sock_connect(struct dhd_netlink_info *dhd_nli) { dhd_nli->nl = nl_socket_alloc(); if (dhd_nli->nl == NULL) return -1; if (genl_connect(dhd_nli->nl) < 0) { fprintf(stderr, "netlink connection failed\n"); goto err; } dhd_nli->nl_id = genl_ctrl_resolve(dhd_nli->nl, "nl80211"); if (dhd_nli->nl_id < 0) { fprintf(stderr, "'nl80211' netlink not found\n"); goto err; } dhd_nli->cb = nl_cb_alloc(NL_CB_DEBUG); if (dhd_nli->cb == NULL) goto err; nl_socket_set_cb(dhd_nli->nl, dhd_nli->cb); return 0; err: nl_cb_put(dhd_nli->cb); nl_socket_free(dhd_nli->nl); fprintf(stderr, "nl80211 connection failed\n"); return -1; }
static int nl80211_init(struct nl80211_state *state) { int err; state->nl_sock = nl_socket_alloc(); if (!state->nl_sock) { fprintf(stderr, "Failed to allocate netlink socket.\n"); return -ENOMEM; } nl_socket_set_buffer_size(state->nl_sock, 8192, 8192); if (genl_connect(state->nl_sock)) { fprintf(stderr, "Failed to connect to generic netlink.\n"); err = -ENOLINK; goto out_handle_destroy; } state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211"); if (state->nl80211_id < 0) { fprintf(stderr, "nl80211 not found.\n"); err = -ENOENT; goto out_handle_destroy; } return 0; out_handle_destroy: nl_socket_free(state->nl_sock); return err; }
int ac_kmod_init(void) { int result; /* */ capwap_lock_init(&g_ac.kmodhandle.msglock); /* Configure netlink callback */ g_ac.kmodhandle.nl_cb = nl_cb_alloc(NL_CB_DEFAULT); if (!g_ac.kmodhandle.nl_cb) { ac_kmod_free(); return -1; } /* Create netlink socket */ g_ac.kmodhandle.nl = nl_create_handle(g_ac.kmodhandle.nl_cb); if (!g_ac.kmodhandle.nl) { ac_kmod_free(); return -1; } g_ac.kmodhandle.nl_fd = nl_socket_get_fd(g_ac.kmodhandle.nl); /* Get nlsmartcapwap netlink family */ g_ac.kmodhandle.nlsmartcapwap_id = genl_ctrl_resolve(g_ac.kmodhandle.nl, NLSMARTCAPWAP_GENL_NAME); if (g_ac.kmodhandle.nlsmartcapwap_id < 0) { log_printf(LOG_WARNING, "Unable to found kernel module"); ac_kmod_free(); return -1; } /* Configure callback function */ nl_cb_set(g_ac.kmodhandle.nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, ac_kmod_no_seq_check, NULL); nl_cb_set(g_ac.kmodhandle.nl_cb, NL_CB_VALID, NL_CB_CUSTOM, ac_kmod_valid_handler, NULL); /* Link to kernel module */ result = ac_kmod_link(); if (result) { ac_kmod_free(); return result; } /* Configure netlink message socket */ g_ac.kmodhandle.nlmsg_cb = nl_cb_alloc(NL_CB_DEFAULT); if (!g_ac.kmodhandle.nlmsg_cb) { ac_kmod_free(); return -1; } /* */ g_ac.kmodhandle.nlmsg = nl_create_handle(g_ac.kmodhandle.nlmsg_cb); if (!g_ac.kmodhandle.nlmsg) { ac_kmod_free(); return -1; } return 0; }
void lowpansocket::getAddress(struct ieee802154_addr *ret, const vinterface &iface) { #if defined HAVE_LIBNL || HAVE_LIBNL3 #ifdef HAVE_LIBNL3 struct nl_sock *nl = nl_socket_alloc(); #else struct nl_handle *nl = nl_handle_alloc(); #endif unsigned char *buf = NULL; struct sockaddr_nl nla; struct nlattr *attrs[IEEE802154_ATTR_MAX+1]; struct genlmsghdr *ghdr; struct nlmsghdr *nlh; struct nl_msg *msg; int family; if (!nl) return; genl_connect(nl); /* Build and send message */ msg = nlmsg_alloc(); family = genl_ctrl_resolve(nl, "802.15.4 MAC"); genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_ECHO, IEEE802154_LIST_IFACE, 1); nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, iface.toString().c_str()); nl_send_auto_complete(nl, msg); nlmsg_free(msg); /* Receive and parse answer */ nl_recv(nl, &nla, &buf, NULL); nlh = (struct nlmsghdr*)buf; genlmsg_parse(nlh, 0, attrs, IEEE802154_ATTR_MAX, ieee802154_policy); ghdr = (genlmsghdr*)nlmsg_data(nlh); if (!attrs[IEEE802154_ATTR_SHORT_ADDR] || !attrs[IEEE802154_ATTR_SHORT_ADDR]) return; // We only handle short addresses right now ret->addr_type = IEEE802154_ADDR_SHORT; ret->pan_id = nla_get_u16(attrs[IEEE802154_ATTR_PAN_ID]); ret->short_addr = nla_get_u16(attrs[IEEE802154_ATTR_SHORT_ADDR]); free(buf); nl_close(nl); #ifdef HAVE_LIBNL3 nl_socket_free(nl); #else nl_handle_destroy(nl); #endif #endif }
int main() { struct nl_sock * sk; int cbarg; // nl_debug = 4; // setup netlink socket sk = nl_socket_alloc(); nl_socket_disable_seq_check(sk); // disable sequence number check genl_connect(sk); int id = genl_ctrl_resolve(sk, DEMO_FAMILY_NAME); struct nl_msg * msg; // create a messgae msg = nlmsg_alloc(); genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, id, 0, // hdrlen 0, // flags DEMO_CMD, // numeric command identifier DEMO_VERSION // interface version ); nla_put_string(msg, DEMO_ATTR1_STRING, "hola"); nla_put_u16(msg, DEMO_ATTR2_UINT16, 0xf1); // send it nl_send_auto(sk, msg); // handle reply struct nl_cb * cb = NULL; cb = nl_cb_alloc(NL_CB_CUSTOM); //nl_cb_set_all(cb, NL_CB_DEBUG, NULL, NULL); nl_cb_set_all(cb, NL_CB_CUSTOM, cb_handler, &cbarg); nl_cb_err(cb, NL_CB_DEBUG, NULL, NULL); int nrecv = nl_recvmsgs_report(sk, cb); printf("cbarg %d nrecv %d\n", cbarg, nrecv); // cleanup nlmsg_free(msg); nl_close(sk); nl_socket_free(sk); return 0; }
int ipvs_nl_send_message(struct nl_msg *msg, nl_recvmsg_msg_cb_t func, void *arg) { int err = EINVAL; sock = nl_socket_alloc(); if (!sock) { nlmsg_free(msg); return -1; } if (genl_connect(sock) < 0) goto fail_genl; family = genl_ctrl_resolve(sock, IPVS_GENL_NAME); if (family < 0) goto fail_genl; /* To test connections and set the family */ if (msg == NULL) { nl_socket_free(sock); sock = NULL; return 0; } if (nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, func, arg) != 0) goto fail_genl; if (nl_send_auto_complete(sock, msg) < 0) goto fail_genl; if ((err = -nl_recvmsgs_default(sock)) > 0) goto fail_genl; nlmsg_free(msg); nl_socket_free(sock); return 0; fail_genl: nl_socket_free(sock); sock = NULL; nlmsg_free(msg); errno = err; #ifndef FALLBACK_LIBNL1 errno = nlerr2syserr(err); #endif return -1; }
/** * Query the wireless device for information * about the current connection */ bool wireless_network::query(bool accumulate) { if (!network::query(accumulate)) { return false; } struct nl_sock* sk = nl_socket_alloc(); if (sk == nullptr) { return false; } if (genl_connect(sk) < 0) { return false; } int driver_id = genl_ctrl_resolve(sk, "nl80211"); if (driver_id < 0) { nl_socket_free(sk); return false; } if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, scan_cb, this) != 0) { nl_socket_free(sk); return false; } struct nl_msg* msg = nlmsg_alloc(); if (msg == nullptr) { nl_socket_free(sk); return false; } if ((genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, driver_id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0) == nullptr) || nla_put_u32(msg, NL80211_ATTR_IFINDEX, m_ifid) < 0) { nlmsg_free(msg); nl_socket_free(sk); return false; } // nl_send_sync always frees msg if (nl_send_sync(sk, msg) < 0) { nl_socket_free(sk); return false; } nl_socket_free(sk); return true; }
int main() { struct nl_handle *handle; struct nl_msg *msg; struct myhdr { char mychar[20]; } *hdr; int id; handle = nl_handle_alloc(); if (handle == NULL) goto open_failure; if (genl_connect(handle) != 0) goto open_failure; id = genl_ctrl_resolve(handle, "CONTROL_EXMPL"); if(id < 0) { perror("genl_ctrl_resolve\n"); goto open_failure; } else printf("id %i\n", id); msg = nlmsg_alloc(); memset(msg, 0, 16); int pid = getpid(); int seq = 1; hdr = genlmsg_put(msg, pid, seq, id, sizeof(struct myhdr), NLM_F_REQUEST, 1, 1); memcpy(hdr->mychar, "hello world", strlen("hello world") + 1); int ret = nl_send(handle, msg); printf("message sent %i\n", ret); nlmsg_free(msg); return 0; open_failure: if (handle) { nl_close(handle); nl_handle_destroy(handle); } printf("Erreur open_failure\n"); return 0; }
static int unl_genl_multicast_id(struct unl *unl, const char *name) { struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX + 1]; struct nlattr *groups, *group; struct nl_msg *msg; int ctrlid; int ret = -1; int rem; msg = nlmsg_alloc(); if (!msg) return -1; ctrlid = genl_ctrl_resolve(unl->sock, "nlctrl"); genlmsg_put(msg, 0, 0, ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0); NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, unl->family_name); unl_genl_request_single(unl, msg, &msg); if (!msg) goto nla_put_failure; groups = unl_find_attr(unl, msg, CTRL_ATTR_MCAST_GROUPS); if (!groups) goto fail; nla_for_each_nested(group, groups, rem) { const char *gn; nla_parse(tb, CTRL_ATTR_MCAST_GRP_MAX, nla_data(group), nla_len(group), NULL); if (!tb[CTRL_ATTR_MCAST_GRP_NAME] || !tb[CTRL_ATTR_MCAST_GRP_ID]) continue; gn = nla_data(tb[CTRL_ATTR_MCAST_GRP_NAME]); if (strcmp(gn, name) != 0) continue; ret = nla_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]); break; } fail: nlmsg_free(msg); nla_put_failure: return ret; }
int netlink_init(void) { int error; sk = nl_socket_alloc(); if (!sk) { log_err("Could not allocate the socket to kernelspace; it seems we're out of memory."); return -ENOMEM; } /* * We handle ACKs ourselves. The reason is that Netlink ACK errors do * not contain the friendly error string, so they're useless to us. * https://github.com/NICMx/Jool/issues/169 */ nl_socket_disable_auto_ack(sk); error = genl_connect(sk); if (error) { log_err("Could not open the socket to kernelspace."); goto fail; } family = genl_ctrl_resolve(sk, GNL_JOOL_FAMILY_NAME); if (family < 0) { log_err("Jool's socket family doesn't seem to exist."); log_err("(This probably means Jool hasn't been modprobed.)"); error = family; goto fail; } /* error = nl_socket_modify_err_cb(sk, NL_CB_CUSTOM, error_handler, NULL); if (error) { log_err("Could not register the error handler function."); log_err("This means the socket to kernelspace cannot be used."); goto fail; } */ return 0; fail: nl_socket_free(sk); return netlink_print_error(error); }
int nlt_get_ifinfo(struct nl_sock *sk, struct nlt_ifinfo *ifinfo) { struct nl_msg *msg = nlmsg_alloc(); if (!msg) return -1; int flags = NLM_F_DUMP; int family_id = genl_ctrl_resolve(sk, "nl80211"); genlmsg_put(msg, 0, NL_AUTO_SEQ, family_id, 0, flags, NL80211_CMD_GET_INTERFACE, 0); // NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, 0); nl_send_auto(sk, msg); int err; struct nl_cb *nl_cb = nl_cb_alloc(NL_CB_CUSTOM); nl_cb_set(nl_cb, NL_CB_VALID, NL_CB_CUSTOM, get_ifinfo_cb, ifinfo); nl_cb_err(nl_cb, NL_CB_CUSTOM, error_handler, &err); int nlr; // do { nlr = nl_recvmsgs(sk, nl_cb); // printf("round %d\n",nlr); // }while(1);; //int nlr = nl_recvmsgs_default(sk); // cw_log(LOG_ERR, "iGet if index: Make if %d - %s", nlr, nl_geterror(nlr)); // nla_put_failure: nlmsg_free(msg); return 0; }
int ws80211_init(void) { int err; #ifdef HAVE_NL80211_SPLIT_WIPHY_DUMP int features = 0; #endif /* HAVE_NL80211_SPLIT_WIPHY_DUMP */ struct nl80211_state *state = &nl_state; state->nl_sock = nl_socket_alloc(); if (!state->nl_sock) { fprintf(stderr, "Failed to allocate netlink socket.\n"); return -ENOMEM; } if (genl_connect(state->nl_sock)) { fprintf(stderr, "Failed to connect to generic netlink.\n"); err = -ENOLINK; goto out_handle_destroy; } state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211"); if (state->nl80211_id < 0) { fprintf(stderr, "nl80211 not found.\n"); err = -ENOENT; goto out_handle_destroy; } #ifdef HAVE_NL80211_SPLIT_WIPHY_DUMP ws80211_get_protocol_features(&features); if (features & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP) state->have_split_wiphy = TRUE; #endif /* HAVE_NL80211_SPLIT_WIPHY_DUMP */ return 0; out_handle_destroy: nl_socket_free(state->nl_sock); state->nl_sock = 0; return err; }
int main() { /* create socket */ struct nl_sock *sk = NULL; sk = nl_socket_alloc(); genl_connect(sk); nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, get_cw, NULL); /* setup message */ int driver_id, if_index; struct nl_msg *msg = NULL; driver_id = genl_ctrl_resolve(sk, "nl80211"); if_index = if_nametoindex("wlan0"); msg = nlmsg_alloc(); genlmsg_put(msg, 0, 0, driver_id, 0, 0, NL80211_CMD_GET_WIPHY, 0); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_index); //struct nlattr *nested; //nested = nla_nest_start(msg,NL80211_ATTR_WIPHY_TXQ_PARAMS); //NLA_PUT_U16(msg,NL80211_TXQ_ATTR_CWMIN,cw_min); //NLA_PUT_U16(msg,NL80211_TXQ_ATTR_CWMAX,cw_max); //nla_nest_end(msg,nested); /* send/recv/ack */ int r; r = nl_send_auto_complete(sk, msg); printf("atlasd: nl_send_auto_complete returned %d\n", r); nl_recvmsgs_default(sk); nl_wait_for_ack(sk); nla_put_failure: /* from NLA_PUT_* macros */ fail: if (sk) nl_socket_free(sk); if (msg) nlmsg_free(msg); }
static int driver_connect() { int err; nf10_genl_sock = nl_socket_alloc(); if(nf10_genl_sock == NULL) return -NLE_NOMEM; err = genl_connect(nf10_genl_sock); if(err) { nl_socket_free(nf10_genl_sock); return err; } nf10_genl_family = genl_ctrl_resolve(nf10_genl_sock, NF10_GENL_FAMILY_NAME); if(nf10_genl_family < 0) { nl_socket_free(nf10_genl_sock); return nf10_genl_family; } return 0; }
static struct nl_handle *gen_handle(struct nl_cb* cb){ struct nl_handle *handle; uint32_t pid = getpid() & 0x3FFFFF; int i; handle = nl_handle_alloc_cb(cb); for (i = 0; i < 1024; i++) { if (port_bitmap[i / 32] & (1 << (i % 32))) continue; port_bitmap[i / 32] |= 1 << (i % 32); pid += i << 22; break; } nl_socket_set_local_port(handle, pid); if (!handle){ flooder_log(FLOODER_DEBUG, "Failed to allocate a handle"); return handle; } if(genl_connect(handle)){ flooder_log(FLOODER_DEBUG, "Cannot connect to handle"); handle_destroy(handle); return NULL; } if ((handle_id = genl_ctrl_resolve(handle, "nl80211")) < 0){ flooder_log(FLOODER_DEBUG, "Cannot resolve nl80211"); handle_destroy(handle); return NULL; } return handle; }
int main(int argc, char **argv) { int c; int i; int family; int group; struct nl_sock *nl; struct nl_msg *msg; char *dummy = NULL; /* Currently processed command info */ struct iz_cmd cmd; /* Parse options */ while (1) { #ifdef HAVE_GETOPT_LONG int opt_idx = -1; c = getopt_long(argc, argv, "d::vh", iz_long_opts, &opt_idx); #else c = getopt(argc, argv, "d::vh"); #endif if (c == -1) break; switch(c) { case 'd': if (optarg) { i = strtol(optarg, &dummy, 10); if (*dummy == '\0') iz_debug = nl_debug = i; else { fprintf(stderr, "Error: incorrect debug level: '%s'\n", optarg); exit(1); } } else iz_debug = nl_debug = 1; break; case 'v': printf( "iz " VERSION "\n" "Copyright (C) 2008, 2009 by Siemens AG\n" "License GPLv2 GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.\n" "This is free software: you are free to change and redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law.\n" "\n" "Written by Dmitry Eremin-Solenikov, Sergey Lapin and Maxim Osipov\n"); return 0; case 'h': iz_help(argv[0]); return 0; default: iz_help(argv[0]); return 1; } } if (optind >= argc) { iz_help(argv[0]); return 1; } memset(&cmd, 0, sizeof(cmd)); cmd.argc = argc - optind; cmd.argv = argv + optind; /* Parse command */ cmd.desc = get_cmd(argv[optind]); if (!cmd.desc) { printf("Unknown command %s!\n", argv[optind]); return 1; } if (cmd.desc->parse) { i = cmd.desc->parse(&cmd); if (i == IZ_STOP_OK) { return 0; } else if (i == IZ_STOP_ERR) { printf("Command line parsing error!\n"); return 1; } } /* Prepare NL command */ nl = nl_socket_alloc(); if (!nl) { nl_perror(NLE_NOMEM, "Could not allocate NL handle"); return 1; } genl_connect(nl); family = genl_ctrl_resolve(nl, IEEE802154_NL_NAME); group = nl_get_multicast_id(nl, IEEE802154_NL_NAME, IEEE802154_MCAST_COORD_NAME); if (group < 0) { fprintf(stderr, "Could not get multicast group ID: %s\n", strerror(-group)); return 1; } nl_socket_add_membership(nl, group); iz_seq = nl_socket_use_seq(nl) + 1; nl_socket_modify_cb(nl, NL_CB_VALID, NL_CB_CUSTOM, iz_cb_valid, (void*)&cmd); nl_socket_modify_cb(nl, NL_CB_FINISH, NL_CB_CUSTOM, iz_cb_finish, (void*)&cmd); nl_socket_modify_cb(nl, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, iz_cb_seq_check, (void*)&cmd); /* Send request, if necessary */ if (cmd.desc->request) { msg = nlmsg_alloc(); if (!msg) { nl_perror(NLE_NOMEM, "Could not allocate NL message!\n"); /* FIXME: err */ return 1; } genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, cmd.flags, cmd.desc->nl_cmd, 1); if (cmd.desc->request(&cmd, msg) != IZ_CONT_OK) { printf("Request processing error!\n"); return 1; } dprintf(1, "nl_send_auto_complete\n"); nl_send_auto_complete(nl, msg); cmd.seq = nlmsg_hdr(msg)->nlmsg_seq; dprintf(1, "nlmsg_free\n"); nlmsg_free(msg); } /* Received message handling loop */ while (iz_exit == IZ_CONT_OK) { int err = nl_recvmsgs_default(nl); if (err != NLE_SUCCESS) { nl_perror(err, "Receive failed"); return 1; } } nl_close(nl); if (iz_exit == IZ_STOP_ERR) return 1; return 0; }
wifi_error wifi_initialize(wifi_handle *handle) { int err = 0; bool driver_loaded = false; wifi_error ret = WIFI_SUCCESS; wifi_interface_handle iface_handle; srand(getpid()); ALOGI("Initializing wifi"); hal_info *info = (hal_info *)malloc(sizeof(hal_info)); if (info == NULL) { ALOGE("Could not allocate hal_info"); return WIFI_ERROR_UNKNOWN; } memset(info, 0, sizeof(*info)); ALOGI("Creating socket"); struct nl_sock *cmd_sock = wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT); if (cmd_sock == NULL) { ALOGE("Could not create handle"); return WIFI_ERROR_UNKNOWN; } struct nl_sock *event_sock = wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT); if (event_sock == NULL) { ALOGE("Could not create handle"); nl_socket_free(cmd_sock); return WIFI_ERROR_UNKNOWN; } struct nl_cb *cb = nl_socket_get_cb(event_sock); if (cb == NULL) { ALOGE("Could not create handle"); return WIFI_ERROR_UNKNOWN; } err = 1; nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, internal_valid_message_handler, info); nl_cb_put(cb); info->cmd_sock = cmd_sock; info->event_sock = event_sock; info->clean_up = false; info->in_event_loop = false; info->event_cb = (cb_info *)malloc(sizeof(cb_info) * DEFAULT_EVENT_CB_SIZE); info->alloc_event_cb = DEFAULT_EVENT_CB_SIZE; info->num_event_cb = 0; info->cmd = (cmd_info *)malloc(sizeof(cmd_info) * DEFAULT_CMD_SIZE); info->alloc_cmd = DEFAULT_CMD_SIZE; info->num_cmd = 0; info->nl80211_family_id = genl_ctrl_resolve(cmd_sock, "nl80211"); if (info->nl80211_family_id < 0) { ALOGE("Could not resolve nl80211 familty id"); nl_socket_free(cmd_sock); nl_socket_free(event_sock); free(info); return WIFI_ERROR_UNKNOWN; } ALOGI("%s: family_id:%d", __func__, info->nl80211_family_id); *handle = (wifi_handle) info; wifi_add_membership(*handle, "scan"); wifi_add_membership(*handle, "mlme"); wifi_add_membership(*handle, "regulatory"); wifi_add_membership(*handle, "vendor"); if (!is_wifi_driver_loaded()) { ret = (wifi_error)wifi_load_driver(); if(ret != WIFI_SUCCESS) { ALOGE("%s Failed to load driver : %d\n", __func__, ret); return WIFI_ERROR_UNKNOWN; } driver_loaded = true; } ret = wifi_init_interfaces(*handle); if (ret != WIFI_SUCCESS) { ALOGI("Failed to init interfaces"); goto unload; } if (info->num_interfaces == 0) { ALOGI("No interfaces found"); ret = WIFI_ERROR_UNINITIALIZED; goto unload; } iface_handle = wifi_get_iface_handle((info->interfaces[0])->handle, (info->interfaces[0])->name); if (iface_handle == NULL) { int i; for (i = 0; i < info->num_interfaces; i++) { free(info->interfaces[i]); } ALOGE("%s no iface with %s\n", __func__, info->interfaces[0]->name); return WIFI_ERROR_UNKNOWN; } ret = acquire_supported_features(iface_handle, &info->supported_feature_set); if (ret != WIFI_SUCCESS) { ALOGI("Failed to get supported feature set : %d", ret); //acquire_supported_features failure is acceptable condition as legacy //drivers might not support the required vendor command. So, do not //consider it as failure of wifi_initialize ret = WIFI_SUCCESS; } ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d Supported" " features : %x", NL80211_CMD_VENDOR, info->supported_feature_set); unload: if (driver_loaded) wifi_unload_driver(); return ret; }
int main (int argc, char * argv[]) { int n; struct nl_sock * sk; int oveth_family, oveth_group; sk = nl_socket_alloc (); if (genl_connect (sk) < 0) { printf ("genl connect failed\n"); return -1; } oveth_family = genl_ctrl_resolve (sk, OVETH_GENL_NAME); oveth_group = genl_ctrl_resolve_grp (sk, OVETH_GENL_NAME, OVETH_GENL_MC_GROUP); printf ("family = %d, group = %d\n", oveth_family, oveth_group); nl_socket_free (sk); int fd; struct sockaddr_nl local; fd = socket (AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); if (fd < 0) { perror ("socket"); return -1; } memset (&local, 0, sizeof (local)); local.nl_family = AF_NETLINK; local.nl_groups = oveth_group; if (bind (fd, (struct sockaddr *) &local, sizeof (local)) < 0) { perror ("bind"); return -1; } if (setsockopt(fd, 270, NETLINK_ADD_MEMBERSHIP, &oveth_group, sizeof (oveth_group)) < 0) { perror ("setsockopt"); return -1; } struct nlmsghdr * nlh; struct genlmsghdr * gnlh; struct oveth_event * event; char buf[1024]; while (1) { memset (&buf, 0, sizeof (buf)); n = recv (fd, &buf, sizeof (buf), 0); printf ("received length is %d\n", n); nlh = (struct nlmsghdr *) buf; if (!NLMSG_OK (nlh, n)) { printf ("invalid nlmsg length\n"); } gnlh = (struct genlmsghdr *) NLMSG_DATA (nlh); event = (struct oveth_event *) (buf + sizeof (struct nlmsghdr) + sizeof (struct genlmsghdr) + 4); nlmsghdr_dump (nlh); genlmsghdr_dump (gnlh); if (event->type == OVETH_EVENT_UNKNOWN_MAC) printf ("type : Unknwon Destination MAC\n"); else if (event->type == OVETH_EVENT_UNDER_MAC) printf ("type : New Accommodated MAC\n"); else printf ("type : unknown %d\n", event->type); printf ("app : %d\n", event->app); printf ("vni : %d\n", event->vni); printf ("mac : %02x:%02x:%02x:%02x:%02x:%02x\n", event->mac[0], event->mac[1], event->mac[2], event->mac[3], event->mac[4], event->mac[5]); printf ("\n"); } return 0; }
int main(int argc, char *argv[]) { int opt; while ((opt = getopt(argc, argv, PARAMS)) != -1) { switch (opt) { case 'h': fprintf(stdout, HELP, argv[0]); exit(EXIT_SUCCESS); default: fprintf(stderr, USAGE, argv[0]); exit(EXIT_FAILURE); } } if (optind >= argc - 3) { fprintf(stderr, USAGE, argv[0]); exit(EXIT_FAILURE); } pthread_create(&monthread, NULL, mon_run, NULL); ifacename = argv[optind]; monifname = argv[optind+1]; ssid = argv[optind+2]; int chan = atoi(argv[optind+3]); struct ifreq s; int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); strcpy(s.ifr_name, ifacename); if (ioctl(fd, SIOCGIFHWADDR, &s)) { die("Unable to retrieve hardware address"); } close(fd); memcpy(stamac, s.ifr_addr.sa_data, 6); int iface = if_nametoindex(ifacename); struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT); if(!cb) die("Can't allocate cb"); struct nl_handle *handle; handle = nl_handle_alloc_cb(cb); if(!handle) die("Can't allocate handle"); if(genl_connect(handle)) die("Can't connect to generic netlink"); if ((handle_id = genl_ctrl_resolve(handle, "nl80211")) < 0) die("Can't resolve generic netlink"); usleep(100000); int ret; struct nl_msg* msg; do { msg = gen_msg(iface, ssid, chan); ret = send_and_recv(handle, msg, cb); }while(ret == DEVICE_BUSY); if (ret) printf("Sending failed %s\n", strerror(-ret)); int ctr = 0; while(state != 2) { usleep(100); ctr++; if(ctr == 40000) { die("No probe response within timeout"); } } }
wifi_error wifi_initialize(wifi_handle *handle) { int err = 0; wifi_error ret = WIFI_SUCCESS; wifi_interface_handle iface_handle; struct nl_sock *cmd_sock = NULL; struct nl_sock *event_sock = NULL; struct nl_cb *cb = NULL; int status = 0; ALOGI("Initializing wifi"); hal_info *info = (hal_info *)malloc(sizeof(hal_info)); if (info == NULL) { ALOGE("Could not allocate hal_info"); return WIFI_ERROR_OUT_OF_MEMORY; } memset(info, 0, sizeof(*info)); cmd_sock = wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT, NETLINK_GENERIC); if (cmd_sock == NULL) { ALOGE("Failed to create command socket port"); ret = WIFI_ERROR_UNKNOWN; goto unload; } /* Set the socket buffer size */ if (nl_socket_set_buffer_size(cmd_sock, (256*1024), 0) < 0) { ALOGE("Could not set nl_socket RX buffer size for cmd_sock: %s", strerror(errno)); /* continue anyway with the default (smaller) buffer */ } event_sock = wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT, NETLINK_GENERIC); if (event_sock == NULL) { ALOGE("Failed to create event socket port"); ret = WIFI_ERROR_UNKNOWN; goto unload; } /* Set the socket buffer size */ if (nl_socket_set_buffer_size(event_sock, (256*1024), 0) < 0) { ALOGE("Could not set nl_socket RX buffer size for event_sock: %s", strerror(errno)); /* continue anyway with the default (smaller) buffer */ } cb = nl_socket_get_cb(event_sock); if (cb == NULL) { ALOGE("Failed to get NL control block for event socket port"); ret = WIFI_ERROR_UNKNOWN; goto unload; } err = 1; nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, internal_valid_message_handler, info); nl_cb_put(cb); info->cmd_sock = cmd_sock; info->event_sock = event_sock; info->clean_up = false; info->in_event_loop = false; info->event_cb = (cb_info *)malloc(sizeof(cb_info) * DEFAULT_EVENT_CB_SIZE); if (info->event_cb == NULL) { ALOGE("Could not allocate event_cb"); ret = WIFI_ERROR_OUT_OF_MEMORY; goto unload; } info->alloc_event_cb = DEFAULT_EVENT_CB_SIZE; info->num_event_cb = 0; info->cmd = (cmd_info *)malloc(sizeof(cmd_info) * DEFAULT_CMD_SIZE); if (info->cmd == NULL) { ALOGE("Could not allocate cmd info"); ret = WIFI_ERROR_OUT_OF_MEMORY; goto unload; } info->alloc_cmd = DEFAULT_CMD_SIZE; info->num_cmd = 0; info->nl80211_family_id = genl_ctrl_resolve(cmd_sock, "nl80211"); if (info->nl80211_family_id < 0) { ALOGE("Could not resolve nl80211 familty id"); ret = WIFI_ERROR_UNKNOWN; goto unload; } pthread_mutex_init(&info->cb_lock, NULL); pthread_mutex_init(&info->pkt_fate_stats_lock, NULL); *handle = (wifi_handle) info; wifi_add_membership(*handle, "scan"); wifi_add_membership(*handle, "mlme"); wifi_add_membership(*handle, "regulatory"); wifi_add_membership(*handle, "vendor"); info->cldctx = cld80211_init(); if (info->cldctx != NULL) { info->user_sock = info->cldctx->sock; ret = wifi_init_cld80211_sock_cb(info); if (ret != WIFI_SUCCESS) { ALOGE("Could not set cb for CLD80211 family"); goto cld80211_cleanup; } status = cld80211_add_mcast_group(info->cldctx, "host_logs"); if (status) { ALOGE("Failed to add mcast group host_logs :%d", status); goto cld80211_cleanup; } status = cld80211_add_mcast_group(info->cldctx, "fw_logs"); if (status) { ALOGE("Failed to add mcast group fw_logs :%d", status); goto cld80211_cleanup; } status = cld80211_add_mcast_group(info->cldctx, "per_pkt_stats"); if (status) { ALOGE("Failed to add mcast group per_pkt_stats :%d", status); goto cld80211_cleanup; } status = cld80211_add_mcast_group(info->cldctx, "diag_events"); if (status) { ALOGE("Failed to add mcast group diag_events :%d", status); goto cld80211_cleanup; } status = cld80211_add_mcast_group(info->cldctx, "fatal_events"); if (status) { ALOGE("Failed to add mcast group fatal_events :%d", status); goto cld80211_cleanup; } } else { ret = wifi_init_user_sock(info); if (ret != WIFI_SUCCESS) { ALOGE("Failed to alloc user socket"); goto unload; } } ret = wifi_init_interfaces(*handle); if (ret != WIFI_SUCCESS) { ALOGE("Failed to init interfaces"); goto unload; } if (info->num_interfaces == 0) { ALOGE("No interfaces found"); ret = WIFI_ERROR_UNINITIALIZED; goto unload; } iface_handle = wifi_get_iface_handle((info->interfaces[0])->handle, (info->interfaces[0])->name); if (iface_handle == NULL) { int i; for (i = 0; i < info->num_interfaces; i++) { free(info->interfaces[i]); } ALOGE("%s no iface with %s\n", __func__, info->interfaces[0]->name); goto unload; } ret = acquire_supported_features(iface_handle, &info->supported_feature_set); if (ret != WIFI_SUCCESS) { ALOGI("Failed to get supported feature set : %d", ret); //acquire_supported_features failure is acceptable condition as legacy //drivers might not support the required vendor command. So, do not //consider it as failure of wifi_initialize ret = WIFI_SUCCESS; } ret = get_firmware_bus_max_size_supported(iface_handle); if (ret != WIFI_SUCCESS) { ALOGE("Failed to get supported bus size, error : %d", ret); info->firmware_bus_max_size = 1520; } ret = wifi_logger_ring_buffers_init(info); if (ret != WIFI_SUCCESS) { ALOGE("Wifi Logger Ring Initialization Failed"); goto unload; } ret = wifi_get_capabilities(iface_handle); if (ret != WIFI_SUCCESS) ALOGE("Failed to get wifi Capabilities, error: %d", ret); info->pkt_stats = (struct pkt_stats_s *)malloc(sizeof(struct pkt_stats_s)); if (!info->pkt_stats) { ALOGE("%s: malloc Failed for size: %zu", __FUNCTION__, sizeof(struct pkt_stats_s)); ret = WIFI_ERROR_OUT_OF_MEMORY; goto unload; } info->rx_buf_size_allocated = MAX_RXMPDUS_PER_AMPDU * MAX_MSDUS_PER_MPDU * PKT_STATS_BUF_SIZE; info->rx_aggr_pkts = (wifi_ring_buffer_entry *)malloc(info->rx_buf_size_allocated); if (!info->rx_aggr_pkts) { ALOGE("%s: malloc Failed for size: %d", __FUNCTION__, info->rx_buf_size_allocated); ret = WIFI_ERROR_OUT_OF_MEMORY; info->rx_buf_size_allocated = 0; goto unload; } memset(info->rx_aggr_pkts, 0, info->rx_buf_size_allocated); info->exit_sockets[0] = -1; info->exit_sockets[1] = -1; if (socketpair(AF_UNIX, SOCK_STREAM, 0, info->exit_sockets) == -1) { ALOGE("Failed to create exit socket pair"); ret = WIFI_ERROR_UNKNOWN; goto unload; } ALOGV("Initializing Gscan Event Handlers"); ret = initializeGscanHandlers(info); if (ret != WIFI_SUCCESS) { ALOGE("Initializing Gscan Event Handlers Failed"); goto unload; } ret = initializeRSSIMonitorHandler(info); if (ret != WIFI_SUCCESS) { ALOGE("Initializing RSSI Event Handler Failed"); goto unload; } ALOGV("Initialized Wifi HAL Successfully; vendor cmd = %d Supported" " features : %x", NL80211_CMD_VENDOR, info->supported_feature_set); cld80211_cleanup: if (status != 0 || ret != WIFI_SUCCESS) { ret = WIFI_ERROR_UNKNOWN; cld80211lib_cleanup(info); } unload: if (ret != WIFI_SUCCESS) { if (cmd_sock) nl_socket_free(cmd_sock); if (event_sock) nl_socket_free(event_sock); if (info) { if (info->cmd) free(info->cmd); if (info->event_cb) free(info->event_cb); if (info->cldctx) { cld80211lib_cleanup(info); } else if (info->user_sock) { nl_socket_free(info->user_sock); } if (info->pkt_stats) free(info->pkt_stats); if (info->rx_aggr_pkts) free(info->rx_aggr_pkts); cleanupGscanHandlers(info); cleanupRSSIMonitorHandler(info); free(info); } } return ret; }
void new_comunication(){ sock = nl_handle_alloc (); genl_connect ( sock ); family = genl_ctrl_resolve ( sock , " SNF_GENL " ); msg = nlmsg_alloc(); }
static int nl80211_cmd_handler(const wapi_nl80211_ctx_t *ctx) { struct nl_sock *sock; struct nl_msg *msg; struct nl_cb *cb; int family; int ifidx; int ret; /* Allocate netlink socket. */ sock = nl_socket_alloc(); if (!sock) { WAPI_ERROR("Failed to allocate netlink socket!\n"); return -ENOMEM; } /* Reset "msg" and "cb". */ msg = NULL; cb = NULL; /* Connect to generic netlink socket on kernel side. */ if (genl_connect(sock)) { WAPI_ERROR("Failed to connect to generic netlink!\n"); ret = -ENOLINK; goto exit; } /* Ask kernel to resolve family name to family id. */ ret = family = genl_ctrl_resolve(sock, "nl80211"); if (ret < 0) { WAPI_ERROR("genl_ctrl_resolve() failed!\n"); goto exit; } /* Map given network interface name (ifname) to its corresponding index. */ ifidx = if_nametoindex(ctx->ifname); if (!ifidx) { WAPI_STRERROR("if_nametoindex(\"%s\")", ctx->ifname); ret = -errno; goto exit; } /* Construct a generic netlink by allocating a new message. */ msg = nlmsg_alloc(); if (!msg) { WAPI_ERROR("nlmsg_alloc() failed!\n"); ret = -ENOMEM; goto exit; } /* Append the requested command to the message. */ switch (ctx->cmd) { case WAPI_NL80211_CMD_IFADD: { enum nl80211_iftype iftype; genlmsg_put( msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, 0, NL80211_CMD_NEW_INTERFACE, 0); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx); /* Get NL80211_IFTYPE_* for the given WAPI mode. */ ret = wapi_mode_to_iftype(ctx->u.ifadd.mode, &iftype); if (ret < 0) goto exit; NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ctx->u.ifadd.name); NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype); break; } case WAPI_NL80211_CMD_IFDEL: genlmsg_put( msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, 0, NL80211_CMD_DEL_INTERFACE, 0); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx); break; } /* Finalize (send) the message. */ ret = nl_send_auto_complete(sock, msg); if (ret < 0) { WAPI_ERROR("nl_send_auto_complete() failed!\n"); goto exit; } /* Allocate a new callback handle. */ cb = nl_cb_alloc(NL_CB_VERBOSE); if (!cb) { WAPI_ERROR("nl_cb_alloc() failed\n"); ret = -1; goto exit; } /* Configure callback handlers. */ nl_cb_err(cb, NL_CB_CUSTOM, nl80211_err_handler, &ret); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_fin_handler, &ret); nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_ack_handler, &ret); /* Consume netlink replies. */ for (ret = 1; ret > 0; ) nl_recvmsgs(sock, cb); if (ret) WAPI_ERROR("nl_recvmsgs() failed!\n"); exit: /* Release resources and exit with "ret". */ nl_socket_free(sock); if (msg) nlmsg_free(msg); if (cb) free(cb); return ret; nla_put_failure: WAPI_ERROR("nla_put_failure!\n"); ret = -1; goto exit; }
wifi_error wifi_initialize(wifi_handle *handle) { int err = 0; srand(getpid()); ALOGI("Initializing wifi"); hal_info *info = (hal_info *)malloc(sizeof(hal_info)); if (info == NULL) { ALOGE("Could not allocate hal_info"); return WIFI_ERROR_UNKNOWN; } memset(info, 0, sizeof(*info)); ALOGI("Creating socket"); struct nl_sock *cmd_sock = wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT); if (cmd_sock == NULL) { ALOGE("Could not create handle"); return WIFI_ERROR_UNKNOWN; } struct nl_sock *event_sock = wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT); if (event_sock == NULL) { ALOGE("Could not create handle"); nl_socket_free(cmd_sock); return WIFI_ERROR_UNKNOWN; } struct nl_cb *cb = nl_socket_get_cb(event_sock); if (cb == NULL) { ALOGE("Could not create handle"); return WIFI_ERROR_UNKNOWN; } err = 1; nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, internal_valid_message_handler, info); nl_cb_put(cb); info->cmd_sock = cmd_sock; info->event_sock = event_sock; info->clean_up = false; info->in_event_loop = false; info->event_cb = (cb_info *)malloc(sizeof(cb_info) * DEFAULT_EVENT_CB_SIZE); info->alloc_event_cb = DEFAULT_EVENT_CB_SIZE; info->num_event_cb = 0; info->cmd = (cmd_info *)malloc(sizeof(cmd_info) * DEFAULT_CMD_SIZE); info->alloc_cmd = DEFAULT_CMD_SIZE; info->num_cmd = 0; info->nl80211_family_id = genl_ctrl_resolve(cmd_sock, "nl80211"); if (info->nl80211_family_id < 0) { ALOGE("Could not resolve nl80211 familty id"); nl_socket_free(cmd_sock); nl_socket_free(event_sock); free(info); return WIFI_ERROR_UNKNOWN; } ALOGI("%s: family_id:%d", __func__, info->nl80211_family_id); *handle = (wifi_handle) info; wifi_add_membership(*handle, "scan"); wifi_add_membership(*handle, "mlme"); wifi_add_membership(*handle, "regulatory"); wifi_add_membership(*handle, "vendor"); wifi_init_interfaces(*handle); // ALOGI("Found %d interfaces", info->num_interfaces); ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d handle %p", NL80211_CMD_VENDOR , *handle); return WIFI_SUCCESS; }
int main(int argc, char *argv[]) { sk = create_genl_socket(); sk2 = create_genl_socket(); /* Resolve generic netlink families. */ ovs_datapath_family = genl_ctrl_resolve(sk, OVS_DATAPATH_FAMILY); ovs_packet_family = genl_ctrl_resolve(sk, OVS_PACKET_FAMILY); ovs_vport_family = genl_ctrl_resolve(sk, OVS_VPORT_FAMILY); ovs_flow_family = genl_ctrl_resolve(sk, OVS_FLOW_FAMILY); if (ovs_datapath_family < 0 || ovs_packet_family < 0 || ovs_vport_family < 0 || ovs_flow_family < 0) { fprintf(stderr, "Failed to resolve Open vSwitch generic netlink families; module not loaded?\n"); return 1; } parse_options(argc, argv); argc -= optind; argv += optind; if (argc < 1) { help(); return 1; } const char *cmd = argv[0]; if (!strcmp(cmd, "help")) { help(); } else if (!strcmp(cmd, "show")) { show(); } else if (!strcmp(cmd, "add-port") ||!strcmp(cmd, "add-if")) { if (argc != 2) { fprintf(stderr, "Wrong number of arguments for the %s command (try help)\n", cmd); return 1; } add_port(datapath_name, argv[1]); } else if (!strcmp(cmd, "add-internal-port")) { if (argc != 2) { fprintf(stderr, "Wrong number of arguments for the %s command (try help)\n", cmd); return 1; } add_internal_port(datapath_name, argv[1]); } else if (!strcmp(cmd, "del-port") ||!strcmp(cmd, "del-if")) { if (argc != 2) { fprintf(stderr, "Wrong number of arguments for the %s command (try help)\n", cmd); return 1; } del_port(datapath_name, argv[1]); } else if (!strcmp(cmd, "del-br") ||!strcmp(cmd, "del-dp")) { if (argc != 1) { fprintf(stderr, "Wrong number of arguments for the %s command (try help)\n", cmd); return 1; } del_dp(datapath_name); } else { fprintf(stderr, "Unknown command '%s' (try help)\n", cmd); return 1; } return 0; }