2 * wprobe.c: Wireless probe user space library
3 * Copyright (C) 2008-2009 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <sys/types.h>
17 #include <sys/socket.h>
26 #include <linux/wprobe.h>
27 #include <netlink/netlink.h>
28 #include <netlink/attr.h>
29 #include <netlink/genl/genl.h>
30 #ifndef NO_LOCAL_ACCESS
31 #include <netlink/genl/ctrl.h>
32 #include <netlink/genl/family.h>
39 #define DPRINTF(fmt, ...) fprintf(stderr, "%s(%d): " fmt, __func__, __LINE__, ##__VA_ARGS__)
41 #define DPRINTF(fmt, ...) do {} while (0)
44 #if defined(BYTE_ORDER) && !defined(__BYTE_ORDER)
45 #define __LITTLE_ENDIAN LITTLE_ENDIAN
46 #define __BIG_ENDIAN BIG_ENDIAN
47 #define __BYTE_ORDER BYTE_ORDER
51 #error Unknown endian type
54 #define WPROBE_MAX_MSGLEN 65536
56 static inline __u16 __swab16(__u16 x)
61 static inline __u32 __swab32(__u32 x)
63 return x<<24 | x>>24 |
64 (x & (__u32)0x0000ff00UL)<<8 |
65 (x & (__u32)0x00ff0000UL)>>8;
68 static inline __u64 __swab64(__u64 x)
70 return x<<56 | x>>56 |
71 (x & (__u64)0x000000000000ff00ULL)<<40 |
72 (x & (__u64)0x0000000000ff0000ULL)<<24 |
73 (x & (__u64)0x00000000ff000000ULL)<< 8 |
74 (x & (__u64)0x000000ff00000000ULL)>> 8 |
75 (x & (__u64)0x0000ff0000000000ULL)>>24 |
76 (x & (__u64)0x00ff000000000000ULL)>>40;
80 #if __BYTE_ORDER == __LITTLE_ENDIAN
81 #define SWAP16(var) var = __swab16(var)
82 #define SWAP32(var) var = __swab32(var)
83 #define SWAP64(var) var = __swab64(var)
85 #define SWAP16(var) do {} while(0)
86 #define SWAP32(var) do {} while(0)
87 #define SWAP64(var) do {} while(0)
90 int wprobe_port = 17990;
91 static struct nlattr *tb[WPROBE_ATTR_LAST+1];
92 static struct nla_policy attribute_policy[WPROBE_ATTR_LAST+1] = {
93 [WPROBE_ATTR_ID] = { .type = NLA_U32 },
94 [WPROBE_ATTR_MAC] = { .type = NLA_UNSPEC, .minlen = 6, .maxlen = 6 },
95 [WPROBE_ATTR_NAME] = { .type = NLA_STRING },
96 [WPROBE_ATTR_FLAGS] = { .type = NLA_U32 },
97 [WPROBE_ATTR_TYPE] = { .type = NLA_U8 },
98 [WPROBE_ATTR_FLAGS] = { .type = NLA_U32 },
99 [WPROBE_VAL_S8] = { .type = NLA_U8 },
100 [WPROBE_VAL_S16] = { .type = NLA_U16 },
101 [WPROBE_VAL_S32] = { .type = NLA_U32 },
102 [WPROBE_VAL_S64] = { .type = NLA_U64 },
103 [WPROBE_VAL_U8] = { .type = NLA_U8 },
104 [WPROBE_VAL_U16] = { .type = NLA_U16 },
105 [WPROBE_VAL_U32] = { .type = NLA_U32 },
106 [WPROBE_VAL_U64] = { .type = NLA_U64 },
107 [WPROBE_VAL_SUM] = { .type = NLA_U64 },
108 [WPROBE_VAL_SUM_SQ] = { .type = NLA_U64 },
109 [WPROBE_VAL_SAMPLES] = { .type = NLA_U32 },
110 [WPROBE_VAL_SCALE_TIME] = { .type = NLA_U64 },
111 [WPROBE_ATTR_INTERVAL] = { .type = NLA_U64 },
112 [WPROBE_ATTR_SAMPLES_MIN] = { .type = NLA_U32 },
113 [WPROBE_ATTR_SAMPLES_MAX] = { .type = NLA_U32 },
114 [WPROBE_ATTR_SAMPLES_SCALE_M] = { .type = NLA_U32 },
115 [WPROBE_ATTR_SAMPLES_SCALE_D] = { .type = NLA_U32 },
116 [WPROBE_ATTR_FILTER_GROUP] = { .type = NLA_NESTED },
117 [WPROBE_ATTR_RXCOUNT] = { .type = NLA_U64 },
118 [WPROBE_ATTR_TXCOUNT] = { .type = NLA_U64 },
121 typedef int (*wprobe_cb_t)(struct nl_msg *, void *);
123 struct wprobe_iface_ops {
124 int (*send_msg)(struct wprobe_iface *dev, struct nl_msg *msg, wprobe_cb_t cb, void *arg);
125 void (*free)(struct wprobe_iface *dev);
128 struct wprobe_attr_cb {
129 struct list_head *list;
133 #define WPROBE_MAGIC_STR "WPROBE"
134 struct wprobe_init_hdr {
136 char magic[sizeof(WPROBE_MAGIC_STR)];
138 /* protocol version */
141 /* extra header length (unused for now) */
143 } pre __attribute__((packed));
146 uint16_t genl_family;
147 } v0 __attribute__((packed));
149 } __attribute__((packed));
151 struct wprobe_msg_hdr {
157 enum wprobe_resp_status {
163 wprobe_swap_msg_hdr(struct wprobe_msg_hdr *mhdr)
165 SWAP16(mhdr->status);
171 save_attribute_handler(struct nl_msg *msg, void *arg)
173 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
174 const char *name = "N/A";
175 struct wprobe_attribute *attr;
177 struct wprobe_attr_cb *cb = arg;
179 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
180 genlmsg_attrlen(gnlh, 0), attribute_policy);
182 if (tb[WPROBE_ATTR_NAME])
183 name = nla_data(tb[WPROBE_ATTR_NAME]);
185 attr = malloc(sizeof(struct wprobe_attribute) + strlen(name) + 1);
189 memset(attr, 0, sizeof(struct wprobe_attribute));
191 if (tb[WPROBE_ATTR_ID])
192 attr->id = nla_get_u32(tb[WPROBE_ATTR_ID]);
194 if (tb[WPROBE_ATTR_MAC] && cb->addr)
195 memcpy(cb->addr, nla_data(tb[WPROBE_ATTR_MAC]), 6);
197 if (tb[WPROBE_ATTR_FLAGS])
198 attr->flags = nla_get_u32(tb[WPROBE_ATTR_FLAGS]);
200 if (tb[WPROBE_ATTR_TYPE])
201 type = nla_get_u8(tb[WPROBE_ATTR_TYPE]);
203 if ((type < WPROBE_VAL_STRING) ||
204 (type > WPROBE_VAL_U64))
208 strcpy(attr->name, name);
209 INIT_LIST_HEAD(&attr->list);
210 list_add(&attr->list, cb->list);
214 static struct nl_msg *
215 wprobe_new_msg(struct wprobe_iface *dev, int cmd, bool dump)
220 msg = nlmsg_alloc_size(65536);
227 genlmsg_put(msg, 0, 0, dev->genl_family,
230 NLA_PUT_STRING(msg, WPROBE_ATTR_INTERFACE, dev->ifname);
237 dump_attributes(struct wprobe_iface *dev, bool link, struct list_head *list, char *addr)
240 struct wprobe_attr_cb cb;
244 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_LIST, true);
249 NLA_PUT(msg, WPROBE_ATTR_MAC, 6, "\x00\x00\x00\x00\x00\x00");
251 return dev->ops->send_msg(dev, msg, save_attribute_handler, &cb);
258 static struct wprobe_iface *
259 wprobe_alloc_dev(void)
261 struct wprobe_iface *dev;
263 dev = malloc(sizeof(struct wprobe_iface));
267 memset(dev, 0, sizeof(struct wprobe_iface));
276 INIT_LIST_HEAD(&dev->global_attr);
277 INIT_LIST_HEAD(&dev->link_attr);
278 INIT_LIST_HEAD(&dev->links);
283 wprobe_init_dev(struct wprobe_iface *dev)
285 dump_attributes(dev, false, &dev->global_attr, NULL);
286 dump_attributes(dev, true, &dev->link_attr, NULL);
290 #ifndef NO_LOCAL_ACCESS
291 static int n_devs = 0;
292 static struct nl_sock *handle = NULL;
293 static struct nl_cache *cache = NULL;
294 static struct genl_family *family = NULL;
297 error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
305 finish_handler(struct nl_msg *msg, void *arg)
313 ack_handler(struct nl_msg *msg, void *arg)
321 wprobe_local_free(struct wprobe_iface *dev)
323 /* should not happen */
331 nl_cache_free(cache);
333 nl_socket_free(handle);
339 wprobe_local_init(void)
346 handle = nl_socket_alloc();
348 DPRINTF("Failed to create handle\n");
352 if (genl_connect(handle)) {
353 DPRINTF("Failed to connect to generic netlink\n");
357 ret = genl_ctrl_alloc_cache(handle, &cache);
359 DPRINTF("Failed to allocate netlink cache\n");
363 family = genl_ctrl_search_by_name(cache, "wprobe");
365 DPRINTF("wprobe API not present\n");
371 wprobe_local_free(NULL);
377 wprobe_local_send_msg(struct wprobe_iface *dev, struct nl_msg *msg, wprobe_cb_t callback, void *arg)
382 cb = nl_cb_alloc(NL_CB_DEFAULT);
387 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, callback, arg);
389 err = nl_send_auto_complete(handle, msg);
395 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
396 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
397 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
400 nl_recvmsgs(handle, cb);
409 static const struct wprobe_iface_ops wprobe_local_ops = {
410 .send_msg = wprobe_local_send_msg,
411 .free = wprobe_local_free,
414 struct wprobe_iface *
415 wprobe_get_dev(const char *ifname)
417 struct wprobe_iface *dev;
419 if (wprobe_local_init() != 0)
422 dev = wprobe_alloc_dev();
426 dev->ifname = strdup(ifname);
427 dev->ops = &wprobe_local_ops;
428 dev->genl_family = genl_family_get_id(family);
430 if (wprobe_init_dev(dev) < 0)
438 wprobe_local_free(NULL);
444 static void swap_nlmsghdr(struct nlmsghdr *nlh)
446 SWAP32(nlh->nlmsg_len);
447 SWAP16(nlh->nlmsg_type);
448 SWAP16(nlh->nlmsg_flags);
449 SWAP32(nlh->nlmsg_seq);
450 SWAP32(nlh->nlmsg_pid);
453 static void swap_genlmsghdr(struct genlmsghdr *gnlh)
455 #if 0 /* probably unnecessary */
456 SWAP16(gnlh->reserved);
461 wprobe_swap_nested(void *data, int len, bool outgoing)
463 void *end = data + len;
466 struct nlattr *nla = data;
467 unsigned int type, len;
470 SWAP16(nla->nla_len);
471 SWAP16(nla->nla_type);
473 /* required for further sanity checks */
474 if (data + nla->nla_len > end)
475 nla->nla_len = end - data;
478 len = NLA_ALIGN(nla->nla_len);
479 type = nla->nla_type & NLA_TYPE_MASK;
481 if (type <= WPROBE_ATTR_LAST) {
482 #if __BYTE_ORDER == __LITTLE_ENDIAN
483 switch(attribute_policy[type].type) {
485 SWAP16(*(__u16 *)nla_data(nla));
488 SWAP32(*(__u32 *)nla_data(nla));
491 SWAP64(*(__u64 *)nla_data(nla));
494 wprobe_swap_nested(nla_data(nla), nla_len(nla), outgoing);
502 SWAP16(nla->nla_len);
503 SWAP16(nla->nla_type);
510 static struct nl_msg *
511 wprobe_msg_from_network(int socket, int len)
513 struct genlmsghdr *gnlh;
514 struct nlmsghdr *nlh;
518 msg = nlmsg_alloc_size(len + 32);
522 nlh = nlmsg_hdr(msg);
523 if (read(socket, nlh, len) != len)
527 if (nlh->nlmsg_len > len)
530 gnlh = nlmsg_data(nlh);
531 swap_genlmsghdr(gnlh);
533 data = genlmsg_data(gnlh);
534 wprobe_swap_nested(data, genlmsg_len(gnlh), false);
543 wprobe_msg_to_network(int socket, struct nl_msg *msg)
545 struct nlmsghdr *nlh = nlmsg_hdr(msg);
546 struct wprobe_msg_hdr mhdr;
547 struct genlmsghdr *gnlh;
552 buflen = nlh->nlmsg_len;
553 buf = malloc(buflen);
557 memset(&mhdr, 0, sizeof(mhdr));
558 mhdr.status = WPROBE_MSG_DATA;
560 wprobe_swap_msg_hdr(&mhdr);
561 write(socket, &mhdr, sizeof(mhdr));
563 memcpy(buf, nlh, buflen);
565 gnlh = nlmsg_data(nlh);
566 data = genlmsg_data(gnlh);
567 datalen = genlmsg_len(gnlh);
569 wprobe_swap_nested(data, datalen, true);
570 swap_genlmsghdr(gnlh);
572 ret = write(socket, buf, buflen);
579 wprobe_remote_send_msg(struct wprobe_iface *dev, struct nl_msg *msg, wprobe_cb_t callback, void *arg)
581 struct wprobe_msg_hdr mhdr;
584 wprobe_msg_to_network(dev->sockfd, msg);
587 if (read(dev->sockfd, &mhdr, sizeof(mhdr)) != sizeof(mhdr)) {
588 DPRINTF("Failed to read response header\n");
591 wprobe_swap_msg_hdr(&mhdr);
593 switch(mhdr.status) {
594 case WPROBE_MSG_DATA:
595 if (mhdr.len > WPROBE_MAX_MSGLEN) {
596 fprintf(stderr, "Invalid length in received response message.\n");
600 msg = wprobe_msg_from_network(dev->sockfd, mhdr.len);
609 } while (mhdr.status != WPROBE_MSG_DONE);
619 wprobe_socket_dev_free(struct wprobe_iface *dev)
621 if (dev->sockfd >= 0)
625 static const struct wprobe_iface_ops wprobe_remote_ops = {
626 .send_msg = wprobe_remote_send_msg,
627 .free = wprobe_socket_dev_free,
631 #ifndef NO_LOCAL_ACCESS
633 wprobe_server_init(int socket)
635 struct wprobe_init_hdr hdr;
638 ret = wprobe_local_init();
642 memset(&hdr, 0, sizeof(hdr));
643 memcpy(hdr.pre.magic, WPROBE_MAGIC_STR, sizeof(WPROBE_MAGIC_STR));
645 hdr.v0.genl_family = genl_family_get_id(family);
646 SWAP16(hdr.v0.genl_family);
647 write(socket, (unsigned char *)&hdr, sizeof(hdr));
653 wprobe_server_cb(struct nl_msg *msg, void *arg)
658 ret = wprobe_msg_to_network(*socket, msg);
667 wprobe_server_handle(int socket)
669 struct wprobe_msg_hdr mhdr;
673 ret = read(socket, &mhdr, sizeof(mhdr));
674 if (ret != sizeof(mhdr)) {
678 DPRINTF("Failed to read request header\n");
681 wprobe_swap_msg_hdr(&mhdr);
683 switch(mhdr.status) {
684 case WPROBE_MSG_DATA:
685 if (mhdr.len > WPROBE_MAX_MSGLEN) {
686 DPRINTF("Invalid length in received response message.\n");
689 msg = wprobe_msg_from_network(socket, mhdr.len);
692 DPRINTF("Invalid request header type\n");
697 DPRINTF("Failed to get message\n");
701 ret = wprobe_local_send_msg(NULL, msg, wprobe_server_cb, &socket);
703 memset(&mhdr, 0, sizeof(mhdr));
704 mhdr.status = WPROBE_MSG_DONE;
706 mhdr.error = (uint16_t) -ret;
708 ret = write(socket, (unsigned char *)&mhdr, sizeof(mhdr));
716 wprobe_server_done(void)
718 wprobe_local_free(NULL);
722 struct wprobe_iface *
723 wprobe_get_from_socket(int socket, const char *name)
725 struct wprobe_iface *dev;
726 struct wprobe_init_hdr hdr;
728 dev = wprobe_alloc_dev();
732 dev->ops = &wprobe_remote_ops;
733 dev->sockfd = socket;
734 dev->ifname = strdup(name);
736 /* read version and header length */
737 if (read(socket, &hdr.pre, sizeof(hdr.pre)) != sizeof(hdr.pre)) {
738 DPRINTF("Could not read header\n");
742 /* magic not found */
743 if (memcmp(hdr.pre.magic, WPROBE_MAGIC_STR, sizeof(hdr.pre.magic)) != 0) {
744 DPRINTF("Magic does not match\n");
748 /* unsupported version */
749 if (hdr.pre.version != 0) {
750 DPRINTF("Protocol version does not match\n");
754 if (read(socket, &hdr.v0, sizeof(hdr.v0)) != sizeof(hdr.v0)) {
755 DPRINTF("Could not read header data\n");
759 SWAP16(hdr.pre.extra);
760 SWAP16(hdr.v0.genl_family);
761 dev->genl_family = hdr.v0.genl_family;
763 if (wprobe_init_dev(dev) < 0) {
764 DPRINTF("Could not initialize device\n");
772 wprobe_free_dev(dev);
776 struct wprobe_iface *
777 wprobe_get_auto(const char *arg, char **err)
779 static struct sockaddr_in sa;
780 static char errbuf[512];
782 struct wprobe_iface *dev = NULL;
784 char *devstr = strdup(arg);
792 sep = strchr(devstr, ':');
794 #ifndef NO_LOCAL_ACCESS
796 return wprobe_get_dev(arg);
798 *err = "Invalid argument";
806 sock = socket(AF_INET, SOCK_STREAM, 0);
810 h = gethostbyname(devstr);
812 sprintf(errbuf, "Host not found");
816 memcpy(&sa.sin_addr, h->h_addr, h->h_length);
817 sa.sin_family = AF_INET;
818 sa.sin_port = htons(wprobe_port);
819 if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0)
822 dev = wprobe_get_from_socket(sock, sep);
824 sprintf(errbuf, "wprobe connection initialization failed");
831 strcpy(errbuf, "Connection failed: ");
832 len = strlen(errbuf);
833 strerror_r(errno, errbuf + len, sizeof(errbuf) - len - 1);
847 free_attr_list(struct list_head *list)
849 struct wprobe_attribute *attr, *tmp;
851 list_for_each_entry_safe(attr, tmp, list, list) {
852 list_del(&attr->list);
858 wprobe_free_dev(struct wprobe_iface *dev)
862 free_attr_list(&dev->global_attr);
863 free_attr_list(&dev->link_attr);
864 free((void *)dev->ifname);
868 static struct wprobe_link *
869 get_link(struct list_head *list, const char *addr)
871 struct wprobe_link *l;
873 list_for_each_entry(l, list, list) {
874 if (!memcmp(l->addr, addr, 6)) {
875 list_del_init(&l->list);
880 /* no previous link found, allocate a new one */
881 l = malloc(sizeof(struct wprobe_link));
885 memset(l, 0, sizeof(struct wprobe_link));
886 memcpy(l->addr, addr, sizeof(l->addr));
887 INIT_LIST_HEAD(&l->list);
893 struct wprobe_save_cb {
894 struct list_head *list;
895 struct list_head old_list;
899 save_link_handler(struct nl_msg *msg, void *arg)
901 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
902 struct wprobe_link *link;
903 struct wprobe_save_cb *cb = arg;
906 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
907 genlmsg_attrlen(gnlh, 0), attribute_policy);
909 if (!tb[WPROBE_ATTR_MAC] || (nla_len(tb[WPROBE_ATTR_MAC]) != 6))
912 addr = nla_data(tb[WPROBE_ATTR_MAC]);
913 link = get_link(&cb->old_list, addr);
917 if (tb[WPROBE_ATTR_FLAGS])
918 link->flags = nla_get_u32(tb[WPROBE_ATTR_FLAGS]);
920 list_add_tail(&link->list, cb->list);
926 wprobe_update_links(struct wprobe_iface *dev)
928 struct wprobe_link *l, *tmp;
930 struct wprobe_save_cb cb;
933 INIT_LIST_HEAD(&cb.old_list);
934 list_splice_init(&dev->links, &cb.old_list);
935 cb.list = &dev->links;
937 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_LINKS, true);
941 err = dev->ops->send_msg(dev, msg, save_link_handler, &cb);
945 list_for_each_entry_safe(l, tmp, &cb.old_list, list) {
954 struct wprobe_filter_data
958 struct wprobe_filter_item *buf;
963 dump_filter_handler(struct nl_msg *msg, void *arg)
965 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
966 struct wprobe_filter_data *data = arg;
972 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
973 genlmsg_attrlen(gnlh, 0), attribute_policy);
975 if (!tb[WPROBE_ATTR_NAME] || !tb[WPROBE_ATTR_FILTER_GROUP])
978 name = nla_data(tb[WPROBE_ATTR_NAME]);
979 nla_for_each_nested(p, tb[WPROBE_ATTR_FILTER_GROUP], len) {
983 if (data->buflen < count) {
986 data->buflen = count;
987 data->buf = malloc(sizeof(struct wprobe_filter_item) * count);
988 memset(data->buf, 0, sizeof(struct wprobe_filter_item) * count);
992 nla_for_each_nested(p, tb[WPROBE_ATTR_FILTER_GROUP], len) {
993 struct wprobe_filter_item *fi;
995 nla_parse(tb, WPROBE_ATTR_LAST, nla_data(p),
996 nla_len(p), attribute_policy);
998 if (!tb[WPROBE_ATTR_NAME] || !tb[WPROBE_ATTR_RXCOUNT]
999 || !tb[WPROBE_ATTR_TXCOUNT])
1002 fi = &data->buf[count++];
1003 strncpy(fi->name, nla_data(tb[WPROBE_ATTR_NAME]), sizeof(fi->name) - 1);
1004 fi->name[sizeof(fi->name) - 1] = 0;
1005 fi->rx = nla_get_u64(tb[WPROBE_ATTR_RXCOUNT]);
1006 fi->tx = nla_get_u64(tb[WPROBE_ATTR_TXCOUNT]);
1008 data->cb(data->arg, name, data->buf, count);
1014 wprobe_dump_filters(struct wprobe_iface *dev, wprobe_filter_cb cb, void *arg)
1016 struct wprobe_filter_data data;
1025 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_FILTER, true);
1029 err = dev->ops->send_msg(dev, msg, dump_filter_handler, &data);
1037 wprobe_apply_config(struct wprobe_iface *dev)
1041 msg = wprobe_new_msg(dev, WPROBE_CMD_CONFIG, false);
1045 if (dev->interval >= 0)
1046 NLA_PUT_MSECS(msg, WPROBE_ATTR_INTERVAL, dev->interval);
1048 if (dev->filter_len < 0) {
1049 NLA_PUT(msg, WPROBE_ATTR_FILTER, 0, NULL);
1050 dev->filter_len = 0;
1051 } else if (dev->filter && dev->filter_len > 0) {
1052 NLA_PUT(msg, WPROBE_ATTR_FILTER, dev->filter_len, dev->filter);
1056 dev->ops->send_msg(dev, msg, NULL, NULL);
1065 wprobe_measure(struct wprobe_iface *dev)
1069 msg = wprobe_new_msg(dev, WPROBE_CMD_MEASURE, false);
1073 dev->ops->send_msg(dev, msg, NULL, NULL);
1077 struct wprobe_request_cb {
1078 struct list_head *list;
1079 struct list_head old_list;
1084 save_attrdata_handler(struct nl_msg *msg, void *arg)
1086 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1087 struct wprobe_request_cb *cb = arg;
1088 struct wprobe_attribute *attr;
1091 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
1092 genlmsg_attrlen(gnlh, 0), attribute_policy);
1094 if (!tb[WPROBE_ATTR_ID])
1097 if (!tb[WPROBE_ATTR_TYPE])
1100 id = nla_get_u32(tb[WPROBE_ATTR_ID]);
1101 list_for_each_entry(attr, &cb->old_list, list) {
1109 list_del_init(&attr->list);
1111 type = nla_get_u8(tb[WPROBE_ATTR_TYPE]);
1112 if (type != attr->type) {
1113 DPRINTF("WARNING: type mismatch for %s attribute '%s' (%d != %d)\n",
1114 (cb->addr ? "link" : "global"),
1120 if ((type < WPROBE_VAL_STRING) ||
1121 (type > WPROBE_VAL_U64))
1124 memset(&attr->val, 0, sizeof(attr->val));
1126 #define HANDLE_INT_TYPE(_idx, _type) \
1127 case WPROBE_VAL_S##_type: \
1128 case WPROBE_VAL_U##_type: \
1129 attr->val.U##_type = nla_get_u##_type(tb[_idx]); \
1133 HANDLE_INT_TYPE(type, 8);
1134 HANDLE_INT_TYPE(type, 16);
1135 HANDLE_INT_TYPE(type, 32);
1136 HANDLE_INT_TYPE(type, 64);
1137 case WPROBE_VAL_STRING:
1143 if (attr->flags & WPROBE_F_KEEPSTAT) {
1144 if (tb[WPROBE_VAL_SUM])
1145 attr->val.s = nla_get_u64(tb[WPROBE_VAL_SUM]);
1147 if (tb[WPROBE_VAL_SUM_SQ])
1148 attr->val.ss = nla_get_u64(tb[WPROBE_VAL_SUM_SQ]);
1150 if (tb[WPROBE_VAL_SAMPLES])
1151 attr->val.n = nla_get_u32(tb[WPROBE_VAL_SAMPLES]);
1153 if (attr->val.n > 0) {
1154 float avg = ((float) attr->val.s) / attr->val.n;
1155 float stdev = sqrt((((float) attr->val.ss) / attr->val.n) - (avg * avg));
1160 attr->val.avg = avg;
1161 attr->val.stdev = stdev;
1166 list_add_tail(&attr->list, cb->list);
1172 wprobe_request_data(struct wprobe_iface *dev, const unsigned char *addr)
1174 struct wprobe_request_cb cb;
1175 struct list_head *attrs;
1179 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_INFO, true);
1184 attrs = &dev->link_attr;
1185 NLA_PUT(msg, WPROBE_ATTR_MAC, 6, addr);
1187 attrs = &dev->global_attr;
1190 INIT_LIST_HEAD(&cb.old_list);
1191 list_splice_init(attrs, &cb.old_list);
1194 err = dev->ops->send_msg(dev, msg, save_attrdata_handler, &cb);
1195 list_splice(&cb.old_list, attrs->prev);