static int wprobe_send_msg(struct nl_msg *msg, void *callback, void *arg) { struct nl_cb *cb; int err = 0; cb = nl_cb_alloc(NL_CB_DEFAULT); if (!cb) goto out_no_cb; if (callback) nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, callback, arg); err = nl_send_auto_complete(handle, msg); if (err < 0) goto out; err = 1; 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); while (err > 0) nl_recvmsgs(handle, cb); out: nl_cb_put(cb); out_no_cb: nlmsg_free(msg); return err; }
static struct nl80211_msg_conveyor * nl80211_send( struct nl80211_msg_conveyor *cv, int (*cb_func)(struct nl_msg *, void *), void *cb_arg ) { static struct nl80211_msg_conveyor rcv; int err = 1; if (cb_func) nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb_func, cb_arg); else nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv); if (nl_send_auto_complete(nls->nl_sock, cv->msg) < 0) goto err; nl_cb_err(cv->cb, NL_CB_CUSTOM, nl80211_msg_error, &err); nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err); nl_cb_set(cv->cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_msg_ack, &err); while (err > 0) nl_recvmsgs(nls->nl_sock, cv->cb); return &rcv; err: nl_cb_put(cv->cb); nlmsg_free(cv->msg); return NULL; }
int unl_genl_request(struct unl *unl, struct nl_msg *msg, unl_cb handler, void *arg) { struct nlmsghdr *nlh; struct nl_cb *cb; int err; cb = nl_cb_alloc(NL_CB_CUSTOM); nlh = nlmsg_hdr(msg); err = nl_send_auto_complete(unl->sock, msg); if (err < 0) goto out; err = 1; 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); if (handler) nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, handler, arg); while (err > 0) nl_recvmsgs(unl->sock, cb); out: nlmsg_free(msg); nl_cb_put(cb); return err; }
static int send_and_recv(struct nl_sock *nl_sock, struct nl_msg *msg, int (*valid_handler)(struct nl_msg *, void *), void *valid_data) { struct nl_cb *cb; int err = -ENOMEM; cb = nl_cb_clone(nlcfg.nl_cb); if (!cb) goto out; err = nl_send_auto_complete(nl_sock, msg); if (err < 0) goto out; err = 1; 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); if (valid_handler) nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, valid_data); while (err > 0) nl_recvmsgs(nl_sock, cb); out: nl_cb_put(cb); nlmsg_free(msg); return err; }
int SoftapController::executeScanLinkCmd(const char *iface, int *iface_freq) { struct nl_cb *cb; struct nl_msg *msg; int devidx = 0; int err; // initialize to non-valid freq *iface_freq = 0; devidx = if_nametoindex(iface); if (devidx == 0) { LOGE("failed to translate ifname to idx"); return -errno; } msg = nlmsg_alloc(); if (!msg) { LOGE("failed to allocate netlink message"); return 2; } cb = nl_cb_alloc(NL_CB_DEFAULT); if (!cb) { LOGE("failed to allocate netlink callbacks"); err = 2; goto out_free_msg; } genlmsg_put(msg, 0, 0, genl_family_get_id(nl80211), 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx); // iface_freq will be filled out by the callback nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, linkDumpCbHandler, iface_freq); err = nl_send_auto_complete(nl_soc, msg); if (err < 0) goto out; err = 1; nl_cb_err(cb, NL_CB_CUSTOM, NlErrorHandler, &err); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, NlFinishHandler, &err); nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, NlAckHandler, &err); while (err > 0) nl_recvmsgs(nl_soc, cb); out: nl_cb_put(cb); out_free_msg: nlmsg_free(msg); return err; nla_put_failure: LOGW("building message failed"); return 2; }
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; }
/* NOTE: this function consumes 'msg' */ static int _nl80211_send_and_recv (struct nl_sock *nl_sock, struct nl_cb *nl_cb, struct nl_msg *msg, int (*valid_handler) (struct nl_msg *, void *), void *valid_data) { struct nl_cb *cb; int err, done; g_return_val_if_fail (msg != NULL, -ENOMEM); cb = nl_cb_clone (nl_cb); if (!cb) { err = -ENOMEM; goto out; } err = nl_send_auto_complete (nl_sock, msg); if (err < 0) goto out; done = 0; nl_cb_err (cb, NL_CB_CUSTOM, error_handler, &done); nl_cb_set (cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &done); nl_cb_set (cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &done); if (valid_handler) nl_cb_set (cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, valid_data); /* Loop until one of our NL callbacks says we're done; on success * done will be 1, on error it will be < 0. */ while (!done) { err = nl_recvmsgs (nl_sock, cb); if (err && err != -NLE_AGAIN) { /* Kernel scan list can change while we are dumping it, as new scan * results from H/W can arrive. BSS info is assured to be consistent * and we don't need consistent view of whole scan list. Hence do * not warn on DUMP_INTR error for get scan command. */ if (err == -NLE_DUMP_INTR && genlmsg_hdr(nlmsg_hdr(msg))->cmd == NL80211_CMD_GET_SCAN) break; nm_log_warn (LOGD_WIFI, "nl_recvmsgs() error: (%d) %s", err, nl_geterror (err)); break; } } if (err == 0 && done < 0) err = done; out: nl_cb_put (cb); nlmsg_free (msg); return err; }
static int handle_mgmt_dump(struct nl80211_state *state, struct nl_msg *msg, int argc, char **argv, enum id_input id) { struct nl_cb *mgmt_cb; char *ndev = argv[0]; int mgmt_argc = 5; char **mgmt_argv; unsigned int count = 0; int err = 0; int i; mgmt_argv = calloc(mgmt_argc, sizeof(char*)); if (!mgmt_argv) return -ENOMEM; mgmt_argv[0] = ndev; mgmt_argv[1] = "mgmt"; mgmt_argv[2] = "reg"; for (i = 3; i < argc; i += 3) { if (strcmp(argv[i], "count") == 0) { count = 1 + atoi(argv[i + 1]); break; } if (strcmp(argv[i], "frame") != 0) { err = 1; goto out; } mgmt_argv[3] = argv[i + 1]; mgmt_argv[4] = argv[i + 2]; err = handle_cmd(state, II_NETDEV, mgmt_argc, mgmt_argv); if (err) goto out; } mgmt_cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); if (!mgmt_cb) { err = 1; goto out; } /* need to turn off sequence number checking */ nl_cb_set(mgmt_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, seq_handler, NULL); nl_cb_set(mgmt_cb, NL_CB_VALID, NL_CB_CUSTOM, dump_mgmt_frame, NULL); while (--count) nl_recvmsgs(state->nl_sock, mgmt_cb); nl_cb_put(mgmt_cb); out: free(mgmt_argv); return err; }
int main() { int ret; struct nl_cb * nl_cb; struct nl_handle * nl_handle; struct netlink_data * netlink; int ioctl_sock = -1; struct dl_list interfaces; int if_add_ifindex = -1; dl_list_init(&interfaces); /* * global_init */ ret = netlink_init_nl80211_event_rtm(&netlink,NULL,nl80211_event_rtm_newlink,nl80211_event_rtm_dellink); if (ret == -1 || netlink == NULL) { goto err; } ret = nl80211_init_event(&nl_cb, &nl_handle); if (ret == -1) { wpa_printf(MSG_DEBUG, "nl80211: Init failure "); goto err; } wpa_printf(MSG_INFO, "nl80211: wait for event... "); nl_cb_set(nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); nl_cb_set(nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, NULL); eloop_register_wrapper(nl_socket_get_fd(nl_handle), nl80211_event_receive, nl_cb, nl_handle); ioctl_sock = init_ioctl_sock(); if (ioctl_sock < 0) { perror("socket(PF_INET,SOCK_DGRAM)"); goto err; } /* * hapd_init */ eloop_run(); return 0; err: netlink_deinit(netlink); nl_destroy_handles(&nl_handle); nl_cb_put(nl_cb); if (ioctl_sock >= 0) { close(ioctl_sock); } return -1; }
static int go_offchan_freq(struct nl80211_state *state, int devidx, int freq) { struct nl_cb *cb; struct nl_cb *s_cb; struct nl_msg *msg; int err; msg = nlmsg_alloc(); if (!msg) { fprintf(stderr, "failed to allocate netlink message\n"); return 2; } cb = nl_cb_alloc(nl_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); s_cb = nl_cb_alloc(nl_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); if (!cb || !s_cb) { fprintf(stderr, "failed to allocate netlink callbacks\n"); err = 2; goto out_free_msg; } genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, 0, NL80211_CMD_REMAIN_ON_CHANNEL, 0); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx); NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); /* 5 seconds is the max allowed, values passed are in ms */ NLA_PUT_U32(msg, NL80211_ATTR_DURATION, 60); nl_socket_set_cb(state->nl_sock, s_cb); err = nl_send_auto_complete(state->nl_sock, msg); if (err < 0) goto out; err = 1; 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); while (err > 0) nl_recvmsgs(state->nl_sock, cb); out: nl_cb_put(cb); out_free_msg: nlmsg_free(msg); return err; nla_put_failure: fprintf(stderr, "building message failed\n"); return 2; }
// Retrieve monitor interface for a given physical interface int ret_mon_IF(struct nl80211_state *state) { struct nl_msg *msg; struct nl_cb *cb, *s_cb; int err = 1; msg = nlmsg_alloc(); if (!msg) { fprintf(stderr, "failed to allocate netlink message\n"); return 2; } // Allocate a new callback handle. cb = nl_cb_alloc(NL_CB_DEFAULT); s_cb = nl_cb_alloc(NL_CB_DEFAULT); if (!cb || !s_cb) { fprintf(stderr, "failed to allocate netlink callbacks\n"); err = 2; goto out_free_msg; } // Add Generic Netlink header (i.e. NL80211_CMD_GET_INTERFACE) to Netlink message genlmsg_put(msg, 0, 0, state->nl80211_id, 0, 768, NL80211_CMD_GET_INTERFACE, 0); // Set up a valid callback function if (nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, iface_handler, NULL)) goto out; // Attach the callback function to a netlink socket nl_socket_set_cb(state->nl_sock, s_cb); // Finalize and transmit Netlink message if(nl_send_auto_complete(state->nl_sock, msg) < 0) goto out; // Set up error, finish and acknowledgment callback function 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); while (err > 0) nl_recvmsgs(state->nl_sock, cb); out: nl_cb_put(cb); out_free_msg: nlmsg_free(msg); return err; }
static int call_survey_freq(struct nl80211_state *state, int devidx, int freq) { struct nl_cb *cb; struct nl_cb *s_cb; struct nl_msg *msg; int err; msg = nlmsg_alloc(); if (!msg) { fprintf(stderr, "failed to allocate netlink message\n"); return 2; } cb = nl_cb_alloc(nl_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); s_cb = nl_cb_alloc(nl_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); if (!cb || !s_cb) { fprintf(stderr, "failed to allocate netlink callbacks\n"); err = 2; goto out_free_msg; } genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, NLM_F_DUMP, NL80211_CMD_GET_SURVEY, 0); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, handle_survey_dump, (void *) &freq); nl_socket_set_cb(state->nl_sock, s_cb); err = nl_send_auto_complete(state->nl_sock, msg); if (err < 0) goto out; err = 1; 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); while (err > 0) nl_recvmsgs(state->nl_sock, cb); out: nl_cb_put(cb); out_free_msg: nlmsg_free(msg); return err; nla_put_failure: fprintf(stderr, "building message failed\n"); return 2; }
void unl_genl_loop(struct unl *unl, unl_cb handler, void *arg) { struct nl_cb *cb; cb = nl_cb_alloc(NL_CB_CUSTOM); unl->loop_done = false; nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, handler, arg); while (!unl->loop_done) nl_recvmsgs(unl->sock, cb); nl_cb_put(cb); }
/* NOTE: this function consumes 'msg' */ static int _nl80211_send_and_recv (struct nl_sock *nl_sock, struct nl_cb *nl_cb, struct nl_msg *msg, int (*valid_handler)(struct nl_msg *, void *), void *valid_data) { struct nl_cb *cb; int err, done; g_return_val_if_fail (msg != NULL, -ENOMEM); cb = nl_cb_clone (nl_cb); if (!cb) { err = -ENOMEM; goto out; } err = nl_send_auto_complete (nl_sock, msg); if (err < 0) goto out; done = 0; nl_cb_err (cb, NL_CB_CUSTOM, error_handler, &done); nl_cb_set (cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &done); nl_cb_set (cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &done); if (valid_handler) nl_cb_set (cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, valid_data); /* Loop until one of our NL callbacks says we're done; on success * done will be 1, on error it will be < 0. */ while (!done) { err = nl_recvmsgs (nl_sock, cb); if (err && err != -NLE_AGAIN) { nm_log_warn (LOGD_WIFI, "nl_recvmsgs() error: (%d) %s", err, nl_geterror (err)); break; } } if (err == 0 && done < 0) err = done; out: nl_cb_put (cb); nlmsg_free (msg); return err; }
int kern_bcache_init() { supsocket_t *sock; nl_bcache = nl_handle_alloc(); nl_disable_sequence_check(nl_bcache); nl_join_groups(nl_bcache, ~0); nl_cb_set(nl_handle_get_cb(nl_bcache), NL_CB_VALID, NL_CB_CUSTOM, nl_bcache_event, NULL); if (nl_connect(nl_bcache, NETLINK_IP6MBLTY) < 0) { debug_log(1, "nl_connect(NETLINK_IP6MBLTY) failed: %s.\n", strerror(errno)); nl_close(nl_bcache); nl_bcache = NULL; return -1; } fcntl(nl_handle_get_fd(nl_bcache), F_SETFL, O_NONBLOCK); sock = support_register_socket(nl_handle_get_fd(nl_bcache), nl_bcache_events_waiting, NULL, NULL); sock->mode = SUPSOCKET_READ; return 0; }
static int npi_set_noarg_cmd(struct nlnpi_state *state, struct nl_cb *cb, struct nl_msg *msg, int argc, char **argv) { nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_reply_status, NULL); return 0; }
/* data format is len(2 bytes) + name + value */ static int npi_set_debug_cmd(struct nlnpi_state *state, struct nl_cb *cb, struct nl_msg *msg, int argc, char **argv) { unsigned short len; unsigned char data[38]; unsigned int value; char **err = NULL; if (argc != 2 || !argv) return 1; len = strlen(argv[0]); if (len > 32) { fprintf(stderr, "Invild debug name length(%d)\n", len); return 2; } memcpy(data, &len, sizeof(len)); sscanf(argv[0], "%s", &(data[2])); value = strtol(argv[1], err, 16); if (err) { fprintf(stderr, "Invild value format\n"); return 2; } memcpy(&(data[2 + len]), &value, sizeof(value)); NLA_PUT(msg, NLNPI_ATTR_SET_DEBUG, (sizeof(len) + len + sizeof(value)), data); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_reply_status, NULL); return 0; nla_put_failure: return -ENOBUFS; }
static int plt_get_rx_statcs(struct nl80211_state *state, struct nl_cb *cb, struct nl_msg *msg, int argc, char **argv) { struct nlattr *key; struct wl1271_radio_rx_statcs prms; prms.test.id = TEST_CMD_RX_STAT_GET; key = nla_nest_start(msg, NL80211_ATTR_TESTDATA); if (!key) { fprintf(stderr, "%s> fail to nla_nest_start()\n", __func__); return 1; } NLA_PUT_U32(msg, WL1271_TM_ATTR_CMD_ID, WL1271_TM_CMD_TEST); NLA_PUT(msg, WL1271_TM_ATTR_DATA, sizeof(prms), &prms); NLA_PUT_U8(msg, WL1271_TM_ATTR_ANSWER, 1); nla_nest_end(msg, key); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, display_rx_statcs, NULL); /* Important: needed gap between tx_start and tx_get */ sleep(2); return 0; nla_put_failure: fprintf(stderr, "%s> building message failed\n", __func__); return 2; }
/** * Pickup netlink answer, parse is and return object * @arg sk Netlink socket * @arg parser Parser function to parse answer * @arg result Result pointer to return parsed object * * @return 0 on success or a negative error code. */ int nl_pickup(struct nl_sock *sk, int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *), struct nl_object **result) { struct nl_cb *cb; int err; struct pickup_param pp = { .parser = parser, }; cb = nl_cb_clone(sk->s_cb); if (cb == NULL) return -NLE_NOMEM; nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __pickup_answer, &pp); err = nl_recvmsgs(sk, cb); if (err < 0) goto errout; *result = pp.result; errout: nl_cb_put(cb); return err; }
static int handle_wowlan_show(struct nl80211_state *state, struct nl_cb *cb, struct nl_msg *msg, int argc, char **argv) { nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_wowlan_handler, NULL); return 0; }
static int get_power_save(struct nl80211_state *state, struct nl_cb *cb, struct nl_msg *msg) { nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_power_save_handler, NULL); return 0; }
static int nl80211_add_mon_if(struct nl80211_state *state, const char *device, const char *mondevice) { int ifindex, ret; struct nl_msg *msg; struct nl_cb *cb = NULL; int finished = 0; ifindex = device_ifindex(device); msg = nl80211_nlmsg_xalloc(); genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, 0, NL80211_CMD_NEW_INTERFACE, 0); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, mondevice); NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_MONITOR); ret = nl_send_auto_complete(state->nl_sock, msg); if (ret < 0) { if (ret == -ENFILE) { nlmsg_free(msg); return -EBUSY; } panic("Cannot send_auto_complete!\n"); } cb = nl_cb_alloc(NL_CB_CUSTOM); if (!cb) panic("Cannot alloc nl_cb!\n"); nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_wait_handler, &finished); nl_cb_err(cb, NL_CB_CUSTOM, nl80211_error_handler, NULL); nl_recvmsgs(state->nl_sock, cb); if (!finished) { ret = nl_wait_for_ack(state->nl_sock); if (ret < 0) { if (ret == -ENFILE) { nlmsg_free(msg); return -EBUSY; } panic("Waiting for netlink ack failed!\n"); } } nl_cb_put(cb); nlmsg_free(msg); return 0; nla_put_failure: panic("nla put failure!\n"); return -EIO; /* dummy */ }
static int handle_interface_info(struct nl80211_state *state, struct nl_cb *cb, struct nl_msg *msg, int argc, char **argv, enum id_input id) { nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL); return 0; }
static wifi_error wifi_init_user_sock(hal_info *info) { struct nl_sock *user_sock = wifi_create_nl_socket(WIFI_HAL_USER_SOCK_PORT, NETLINK_USERSOCK); if (user_sock == NULL) { ALOGE("Could not create diag sock"); return WIFI_ERROR_UNKNOWN; } /* Set the socket buffer size */ if (nl_socket_set_buffer_size(user_sock, (256*1024), 0) < 0) { ALOGE("Could not set size for user_sock: %s", strerror(errno)); /* continue anyway with the default (smaller) buffer */ } else { ALOGV("nl_socket_set_buffer_size successful for user_sock"); } struct nl_cb *cb = nl_socket_get_cb(user_sock); if (cb == NULL) { ALOGE("Could not get cb"); return WIFI_ERROR_UNKNOWN; } info->user_sock_arg = 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, &info->user_sock_arg); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &info->user_sock_arg); nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &info->user_sock_arg); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, user_sock_message_handler, info); nl_cb_put(cb); int ret = nl_socket_add_membership(user_sock, 1); if (ret < 0) { ALOGE("Could not add membership"); return WIFI_ERROR_UNKNOWN; } info->user_sock = user_sock; ALOGV("Initiialized diag sock successfully"); return WIFI_SUCCESS; }
static int handle_dev_dump(struct nl80211_state *state, struct nl_cb *cb, struct nl_msg *msg, int argc, char **argv, enum id_input id) { dev_dump_wiphy = -1; nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, &dev_dump_wiphy); return 0; }
static wifi_error wifi_init_cld80211_sock_cb(hal_info *info) { struct nl_cb *cb = nl_socket_get_cb(info->cldctx->sock); if (cb == NULL) { ALOGE("Could not get cb"); return WIFI_ERROR_UNKNOWN; } info->user_sock_arg = 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, &info->user_sock_arg); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &info->user_sock_arg); nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &info->user_sock_arg); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, user_sock_message_handler, info); nl_cb_put(cb); return WIFI_SUCCESS; }
indigo_error_t indigo_port_stats_get( of_port_stats_request_t *port_stats_request, of_port_stats_reply_t **port_stats_reply_ptr) { of_port_no_t req_of_port_num; of_port_stats_reply_t *port_stats_reply; indigo_error_t err = INDIGO_ERROR_NONE; port_stats_reply = of_port_stats_reply_new(port_stats_request->version); if (port_stats_reply == NULL) { err = INDIGO_ERROR_RESOURCE; goto out; } of_list_port_stats_entry_t list; of_port_stats_reply_entries_bind(port_stats_reply, &list); of_port_stats_request_port_no_get(port_stats_request, &req_of_port_num); int dump_all = req_of_port_num == OF_PORT_DEST_NONE_BY_VERSION(port_stats_request->version); /* Refresh statistics */ nl_cache_refill(route_cache_sock, link_cache); struct nl_msg *msg = ind_ovs_create_nlmsg(ovs_vport_family, OVS_VPORT_CMD_GET); if (dump_all) { nlmsg_hdr(msg)->nlmsg_flags |= NLM_F_DUMP; } else { nla_put_u32(msg, OVS_VPORT_ATTR_PORT_NO, req_of_port_num); } /* Ask kernel to send us one or more OVS_VPORT_CMD_NEW messages */ if (nl_send_auto(ind_ovs_socket, msg) < 0) { err = INDIGO_ERROR_UNKNOWN; goto out; } ind_ovs_nlmsg_freelist_free(msg); /* Handle OVS_VPORT_CMD_NEW messages */ nl_cb_set(netlink_callbacks, NL_CB_VALID, NL_CB_CUSTOM, port_stats_iterator, &list); if (nl_recvmsgs(ind_ovs_socket, netlink_callbacks) < 0) { err = INDIGO_ERROR_UNKNOWN; goto out; } out: if (err != INDIGO_ERROR_NONE) { of_port_stats_reply_delete(port_stats_reply); port_stats_reply = NULL; } *port_stats_reply_ptr = port_stats_reply; return err; }
static int __nl_send_msg(struct nl_sock *sock, struct nl_msg *msg, int (*rx_handler)(struct nl_msg *, void *), int (*finish_handler)(struct nl_msg *, void *), void *data) { struct nl_cb *cb; int err, done; struct send_msg_data send_data; DBG(""); cb = nl_cb_alloc(NL_CB_DEFAULT); if (!cb) return -ENOMEM; err = nl_send_auto_complete(sock, msg); if (err < 0) { nl_cb_put(cb); near_error("%s", strerror(err)); return err; } err = done = 0; send_data.done = &done; send_data.data = data; send_data.finish_handler = finish_handler; nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, __finish_handler, &send_data); nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &done); if (rx_handler) nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, rx_handler, data); while (err == 0 && done == 0) nl_recvmsgs(sock, cb); nl_cb_put(cb); return err; }
/* function: alloc_ack_callbacks * allocates a set of netlink callbacks. returns NULL on failure. callbacks will modify retval with <0 meaning failure * retval - shared state between caller and callback functions */ struct nl_cb *alloc_ack_callbacks(int *retval) { struct nl_cb *callbacks; callbacks = nl_cb_alloc(NL_CB_DEFAULT); if(!callbacks) { return NULL; } nl_cb_set(callbacks, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, retval); nl_cb_err(callbacks, NL_CB_CUSTOM, error_handler, retval); return callbacks; }
static int do_listen_events(void) { struct nl_cb *cb = nl_cb_alloc((log_level > LOG_WARNING) ? NL_CB_DEBUG : NL_CB_DEFAULT); if (!cb) { LOG_ERR_("failed to allocate netlink callbacks\n"); return -ENOMEM; } /* no sequence checking for multicast messages */ nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, NULL); for (;;) nl_recvmsgs(state.nl_sock, cb); nl_cb_put(cb); return 0; }