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 #define _ISOC99_SOURCE
18 #include <sys/types.h>
19 #include <sys/socket.h>
28 #include <linux/wprobe.h>
29 #include <netlink/netlink.h>
30 #include <netlink/attr.h>
31 #include <netlink/genl/genl.h>
32 #ifndef NO_LOCAL_ACCESS
33 #include <netlink/genl/ctrl.h>
34 #include <netlink/genl/family.h>
41 #define DPRINTF(fmt, ...) fprintf(stderr, "%s(%d): " fmt, __func__, __LINE__, ##__VA_ARGS__)
43 #define DPRINTF(fmt, ...) do {} while (0)
46 #if defined(BYTE_ORDER) && !defined(__BYTE_ORDER)
47 #define __LITTLE_ENDIAN LITTLE_ENDIAN
48 #define __BIG_ENDIAN BIG_ENDIAN
49 #define __BYTE_ORDER BYTE_ORDER
53 #error Unknown endian type
56 #define WPROBE_MAX_MSGLEN 65536
58 static inline __u16 __swab16(__u16 x)
63 static inline __u32 __swab32(__u32 x)
65 return x<<24 | x>>24 |
66 (x & (__u32)0x0000ff00UL)<<8 |
67 (x & (__u32)0x00ff0000UL)>>8;
70 static inline __u64 __swab64(__u64 x)
72 return x<<56 | x>>56 |
73 (x & (__u64)0x000000000000ff00ULL)<<40 |
74 (x & (__u64)0x0000000000ff0000ULL)<<24 |
75 (x & (__u64)0x00000000ff000000ULL)<< 8 |
76 (x & (__u64)0x000000ff00000000ULL)>> 8 |
77 (x & (__u64)0x0000ff0000000000ULL)>>24 |
78 (x & (__u64)0x00ff000000000000ULL)>>40;
82 #if __BYTE_ORDER == __LITTLE_ENDIAN
83 #define SWAP16(var) var = __swab16(var)
84 #define SWAP32(var) var = __swab32(var)
85 #define SWAP64(var) var = __swab64(var)
87 #define SWAP16(var) do {} while(0)
88 #define SWAP32(var) do {} while(0)
89 #define SWAP64(var) do {} while(0)
92 int wprobe_port = 17990;
93 static struct nlattr *tb[WPROBE_ATTR_LAST+1];
94 static struct nla_policy attribute_policy[WPROBE_ATTR_LAST+1] = {
95 [WPROBE_ATTR_ID] = { .type = NLA_U32 },
96 [WPROBE_ATTR_MAC] = { .type = NLA_UNSPEC, .minlen = 6, .maxlen = 6 },
97 [WPROBE_ATTR_NAME] = { .type = NLA_STRING },
98 [WPROBE_ATTR_FLAGS] = { .type = NLA_U32 },
99 [WPROBE_ATTR_TYPE] = { .type = NLA_U8 },
100 [WPROBE_ATTR_FLAGS] = { .type = NLA_U32 },
101 [WPROBE_VAL_S8] = { .type = NLA_U8 },
102 [WPROBE_VAL_S16] = { .type = NLA_U16 },
103 [WPROBE_VAL_S32] = { .type = NLA_U32 },
104 [WPROBE_VAL_S64] = { .type = NLA_U64 },
105 [WPROBE_VAL_U8] = { .type = NLA_U8 },
106 [WPROBE_VAL_U16] = { .type = NLA_U16 },
107 [WPROBE_VAL_U32] = { .type = NLA_U32 },
108 [WPROBE_VAL_U64] = { .type = NLA_U64 },
109 [WPROBE_VAL_SUM] = { .type = NLA_U64 },
110 [WPROBE_VAL_SUM_SQ] = { .type = NLA_U64 },
111 [WPROBE_VAL_SAMPLES] = { .type = NLA_U32 },
112 [WPROBE_VAL_SCALE_TIME] = { .type = NLA_U64 },
113 [WPROBE_ATTR_INTERVAL] = { .type = NLA_U64 },
114 [WPROBE_ATTR_SAMPLES_MIN] = { .type = NLA_U32 },
115 [WPROBE_ATTR_SAMPLES_MAX] = { .type = NLA_U32 },
116 [WPROBE_ATTR_SAMPLES_SCALE_M] = { .type = NLA_U32 },
117 [WPROBE_ATTR_SAMPLES_SCALE_D] = { .type = NLA_U32 },
118 [WPROBE_ATTR_FILTER_GROUP] = { .type = NLA_NESTED },
119 [WPROBE_ATTR_RXCOUNT] = { .type = NLA_U64 },
120 [WPROBE_ATTR_TXCOUNT] = { .type = NLA_U64 },
123 typedef int (*wprobe_cb_t)(struct nl_msg *, void *);
125 struct wprobe_iface_ops {
126 int (*send_msg)(struct wprobe_iface *dev, struct nl_msg *msg, wprobe_cb_t cb, void *arg);
127 void (*free)(struct wprobe_iface *dev);
130 struct wprobe_attr_cb {
131 struct list_head *list;
135 #define WPROBE_MAGIC_STR "WPROBE"
136 struct wprobe_init_hdr {
138 char magic[sizeof(WPROBE_MAGIC_STR)];
140 /* protocol version */
143 /* extra header length (unused for now) */
145 } pre __attribute__((packed));
148 uint16_t genl_family;
149 } v0 __attribute__((packed));
151 } __attribute__((packed));
153 struct wprobe_msg_hdr {
159 enum wprobe_resp_status {
165 wprobe_swap_msg_hdr(struct wprobe_msg_hdr *mhdr)
167 SWAP16(mhdr->status);
173 save_attribute_handler(struct nl_msg *msg, void *arg)
175 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
176 const char *name = "N/A";
177 struct wprobe_attribute *attr;
179 struct wprobe_attr_cb *cb = arg;
181 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
182 genlmsg_attrlen(gnlh, 0), attribute_policy);
184 if (tb[WPROBE_ATTR_NAME])
185 name = nla_data(tb[WPROBE_ATTR_NAME]);
187 attr = malloc(sizeof(struct wprobe_attribute) + strlen(name) + 1);
191 memset(attr, 0, sizeof(struct wprobe_attribute));
193 if (tb[WPROBE_ATTR_ID])
194 attr->id = nla_get_u32(tb[WPROBE_ATTR_ID]);
196 if (tb[WPROBE_ATTR_MAC] && cb->addr)
197 memcpy(cb->addr, nla_data(tb[WPROBE_ATTR_MAC]), 6);
199 if (tb[WPROBE_ATTR_FLAGS])
200 attr->flags = nla_get_u32(tb[WPROBE_ATTR_FLAGS]);
202 if (tb[WPROBE_ATTR_TYPE])
203 type = nla_get_u8(tb[WPROBE_ATTR_TYPE]);
205 if ((type < WPROBE_VAL_STRING) ||
206 (type > WPROBE_VAL_U64))
210 strcpy(attr->name, name);
211 INIT_LIST_HEAD(&attr->list);
212 list_add(&attr->list, cb->list);
216 static struct nl_msg *
217 wprobe_new_msg(struct wprobe_iface *dev, int cmd, bool dump)
222 msg = nlmsg_alloc_size(65536);
229 genlmsg_put(msg, 0, 0, dev->genl_family,
232 NLA_PUT_STRING(msg, WPROBE_ATTR_INTERFACE, dev->ifname);
239 dump_attributes(struct wprobe_iface *dev, bool link, struct list_head *list, char *addr)
242 struct wprobe_attr_cb cb;
246 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_LIST, true);
251 NLA_PUT(msg, WPROBE_ATTR_MAC, 6, "\x00\x00\x00\x00\x00\x00");
253 return dev->ops->send_msg(dev, msg, save_attribute_handler, &cb);
260 static struct wprobe_iface *
261 wprobe_alloc_dev(void)
263 struct wprobe_iface *dev;
265 dev = malloc(sizeof(struct wprobe_iface));
269 memset(dev, 0, sizeof(struct wprobe_iface));
278 INIT_LIST_HEAD(&dev->global_attr);
279 INIT_LIST_HEAD(&dev->link_attr);
280 INIT_LIST_HEAD(&dev->links);
285 wprobe_init_dev(struct wprobe_iface *dev)
287 dump_attributes(dev, false, &dev->global_attr, NULL);
288 dump_attributes(dev, true, &dev->link_attr, NULL);
292 #ifndef NO_LOCAL_ACCESS
293 static int n_devs = 0;
294 static struct nl_sock *handle = NULL;
295 static struct nl_cache *cache = NULL;
296 static struct genl_family *family = NULL;
299 error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
307 finish_handler(struct nl_msg *msg, void *arg)
315 ack_handler(struct nl_msg *msg, void *arg)
323 wprobe_local_free(struct wprobe_iface *dev)
325 /* should not happen */
333 nl_cache_free(cache);
335 nl_socket_free(handle);
341 wprobe_local_init(void)
348 handle = nl_socket_alloc();
350 DPRINTF("Failed to create handle\n");
354 if (genl_connect(handle)) {
355 DPRINTF("Failed to connect to generic netlink\n");
359 ret = genl_ctrl_alloc_cache(handle, &cache);
361 DPRINTF("Failed to allocate netlink cache\n");
365 family = genl_ctrl_search_by_name(cache, "wprobe");
367 DPRINTF("wprobe API not present\n");
373 wprobe_local_free(NULL);
379 wprobe_local_send_msg(struct wprobe_iface *dev, struct nl_msg *msg, wprobe_cb_t callback, void *arg)
384 cb = nl_cb_alloc(NL_CB_DEFAULT);
389 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, callback, arg);
391 err = nl_send_auto_complete(handle, msg);
397 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
398 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
399 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
402 nl_recvmsgs(handle, cb);
411 static const struct wprobe_iface_ops wprobe_local_ops = {
412 .send_msg = wprobe_local_send_msg,
413 .free = wprobe_local_free,
416 struct wprobe_iface *
417 wprobe_get_dev(const char *ifname)
419 struct wprobe_iface *dev;
421 if (wprobe_local_init() != 0)
424 dev = wprobe_alloc_dev();
428 dev->ifname = strdup(ifname);
429 dev->ops = &wprobe_local_ops;
430 dev->genl_family = genl_family_get_id(family);
432 if (wprobe_init_dev(dev) < 0)
440 wprobe_local_free(NULL);
446 static void swap_nlmsghdr(struct nlmsghdr *nlh)
448 SWAP32(nlh->nlmsg_len);
449 SWAP16(nlh->nlmsg_type);
450 SWAP16(nlh->nlmsg_flags);
451 SWAP32(nlh->nlmsg_seq);
452 SWAP32(nlh->nlmsg_pid);
455 static void swap_genlmsghdr(struct genlmsghdr *gnlh)
457 #if 0 /* probably unnecessary */
458 SWAP16(gnlh->reserved);
463 wprobe_swap_nested(void *data, int len, bool outgoing)
465 void *end = data + len;
468 struct nlattr *nla = data;
469 unsigned int type, len;
472 SWAP16(nla->nla_len);
473 SWAP16(nla->nla_type);
475 /* required for further sanity checks */
476 if (data + nla->nla_len > end)
477 nla->nla_len = end - data;
480 len = NLA_ALIGN(nla->nla_len);
481 type = nla->nla_type & NLA_TYPE_MASK;
483 if (type <= WPROBE_ATTR_LAST) {
484 #if __BYTE_ORDER == __LITTLE_ENDIAN
485 switch(attribute_policy[type].type) {
487 SWAP16(*(__u16 *)nla_data(nla));
490 SWAP32(*(__u32 *)nla_data(nla));
493 SWAP64(*(__u64 *)nla_data(nla));
496 wprobe_swap_nested(nla_data(nla), nla_len(nla), outgoing);
504 SWAP16(nla->nla_len);
505 SWAP16(nla->nla_type);
512 static struct nl_msg *
513 wprobe_msg_from_network(int socket, int len)
515 struct genlmsghdr *gnlh;
516 struct nlmsghdr *nlh;
520 msg = nlmsg_alloc_size(len + 32);
524 nlh = nlmsg_hdr(msg);
525 if (read(socket, nlh, len) != len)
529 if (nlh->nlmsg_len > len)
532 gnlh = nlmsg_data(nlh);
533 swap_genlmsghdr(gnlh);
535 data = genlmsg_data(gnlh);
536 wprobe_swap_nested(data, genlmsg_len(gnlh), false);
545 wprobe_msg_to_network(int socket, struct nl_msg *msg)
547 struct nlmsghdr *nlh = nlmsg_hdr(msg);
548 struct wprobe_msg_hdr mhdr;
549 struct genlmsghdr *gnlh;
554 buflen = nlh->nlmsg_len;
555 buf = malloc(buflen);
559 memset(&mhdr, 0, sizeof(mhdr));
560 mhdr.status = WPROBE_MSG_DATA;
562 wprobe_swap_msg_hdr(&mhdr);
563 ret = write(socket, &mhdr, sizeof(mhdr));
567 memcpy(buf, nlh, buflen);
569 gnlh = nlmsg_data(nlh);
570 data = genlmsg_data(gnlh);
571 datalen = genlmsg_len(gnlh);
573 wprobe_swap_nested(data, datalen, true);
574 swap_genlmsghdr(gnlh);
576 ret = write(socket, buf, buflen);
585 wprobe_remote_send_msg(struct wprobe_iface *dev, struct nl_msg *msg, wprobe_cb_t callback, void *arg)
587 struct wprobe_msg_hdr mhdr;
590 wprobe_msg_to_network(dev->sockfd, msg);
593 if (read(dev->sockfd, &mhdr, sizeof(mhdr)) != sizeof(mhdr)) {
594 DPRINTF("Failed to read response header\n");
597 wprobe_swap_msg_hdr(&mhdr);
599 switch(mhdr.status) {
600 case WPROBE_MSG_DATA:
601 if (mhdr.len > WPROBE_MAX_MSGLEN) {
602 fprintf(stderr, "Invalid length in received response message.\n");
606 msg = wprobe_msg_from_network(dev->sockfd, mhdr.len);
615 } while (mhdr.status != WPROBE_MSG_DONE);
625 wprobe_socket_dev_free(struct wprobe_iface *dev)
627 if (dev->sockfd >= 0)
631 static const struct wprobe_iface_ops wprobe_remote_ops = {
632 .send_msg = wprobe_remote_send_msg,
633 .free = wprobe_socket_dev_free,
637 #ifndef NO_LOCAL_ACCESS
639 wprobe_server_init(int socket)
641 struct wprobe_init_hdr hdr;
644 ret = wprobe_local_init();
648 memset(&hdr, 0, sizeof(hdr));
649 memcpy(hdr.pre.magic, WPROBE_MAGIC_STR, sizeof(WPROBE_MAGIC_STR));
651 hdr.v0.genl_family = genl_family_get_id(family);
652 SWAP16(hdr.v0.genl_family);
653 write(socket, (unsigned char *)&hdr, sizeof(hdr));
659 wprobe_server_cb(struct nl_msg *msg, void *arg)
664 ret = wprobe_msg_to_network(*socket, msg);
673 wprobe_server_handle(int socket)
675 struct wprobe_msg_hdr mhdr;
679 ret = read(socket, &mhdr, sizeof(mhdr));
680 if (ret != sizeof(mhdr)) {
684 DPRINTF("Failed to read request header\n");
687 wprobe_swap_msg_hdr(&mhdr);
689 switch(mhdr.status) {
690 case WPROBE_MSG_DATA:
691 if (mhdr.len > WPROBE_MAX_MSGLEN) {
692 DPRINTF("Invalid length in received response message.\n");
695 msg = wprobe_msg_from_network(socket, mhdr.len);
698 DPRINTF("Invalid request header type\n");
703 DPRINTF("Failed to get message\n");
707 ret = wprobe_local_send_msg(NULL, msg, wprobe_server_cb, &socket);
709 memset(&mhdr, 0, sizeof(mhdr));
710 mhdr.status = WPROBE_MSG_DONE;
712 mhdr.error = (uint16_t) -ret;
714 ret = write(socket, (unsigned char *)&mhdr, sizeof(mhdr));
722 wprobe_server_done(void)
724 wprobe_local_free(NULL);
728 struct wprobe_iface *
729 wprobe_get_from_socket(int socket, const char *name)
731 struct wprobe_iface *dev;
732 struct wprobe_init_hdr hdr;
734 dev = wprobe_alloc_dev();
738 dev->ops = &wprobe_remote_ops;
739 dev->sockfd = socket;
740 dev->ifname = strdup(name);
742 /* read version and header length */
743 if (read(socket, &hdr.pre, sizeof(hdr.pre)) != sizeof(hdr.pre)) {
744 DPRINTF("Could not read header\n");
748 /* magic not found */
749 if (memcmp(hdr.pre.magic, WPROBE_MAGIC_STR, sizeof(hdr.pre.magic)) != 0) {
750 DPRINTF("Magic does not match\n");
754 /* unsupported version */
755 if (hdr.pre.version != 0) {
756 DPRINTF("Protocol version does not match\n");
760 if (read(socket, &hdr.v0, sizeof(hdr.v0)) != sizeof(hdr.v0)) {
761 DPRINTF("Could not read header data\n");
765 SWAP16(hdr.pre.extra);
766 SWAP16(hdr.v0.genl_family);
767 dev->genl_family = hdr.v0.genl_family;
769 if (wprobe_init_dev(dev) < 0) {
770 DPRINTF("Could not initialize device\n");
778 wprobe_free_dev(dev);
782 struct wprobe_iface *
783 wprobe_get_auto(const char *arg, char **err)
785 static struct sockaddr_in sa;
786 static char errbuf[512];
788 struct wprobe_iface *dev = NULL;
790 char *devstr = strdup(arg);
798 sep = strchr(devstr, ':');
800 #ifndef NO_LOCAL_ACCESS
802 return wprobe_get_dev(arg);
805 *err = "Invalid argument";
813 sock = socket(AF_INET, SOCK_STREAM, 0);
817 h = gethostbyname(devstr);
819 sprintf(errbuf, "Host not found");
823 memcpy(&sa.sin_addr, h->h_addr, h->h_length);
824 sa.sin_family = AF_INET;
825 sa.sin_port = htons(wprobe_port);
826 if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0)
829 dev = wprobe_get_from_socket(sock, sep);
831 sprintf(errbuf, "wprobe connection initialization failed");
838 strcpy(errbuf, "Connection failed: ");
839 len = strlen(errbuf);
840 strerror_r(errno, errbuf + len, sizeof(errbuf) - len - 1);
854 free_attr_list(struct list_head *list)
856 struct wprobe_attribute *attr, *tmp;
858 list_for_each_entry_safe(attr, tmp, list, list) {
859 list_del(&attr->list);
865 wprobe_free_dev(struct wprobe_iface *dev)
869 free_attr_list(&dev->global_attr);
870 free_attr_list(&dev->link_attr);
871 free((void *)dev->ifname);
875 static struct wprobe_link *
876 get_link(struct list_head *list, const char *addr)
878 struct wprobe_link *l;
880 list_for_each_entry(l, list, list) {
881 if (!memcmp(l->addr, addr, 6)) {
882 list_del_init(&l->list);
887 /* no previous link found, allocate a new one */
888 l = malloc(sizeof(struct wprobe_link));
892 memset(l, 0, sizeof(struct wprobe_link));
893 memcpy(l->addr, addr, sizeof(l->addr));
894 INIT_LIST_HEAD(&l->list);
900 struct wprobe_save_cb {
901 struct list_head *list;
902 struct list_head old_list;
906 save_link_handler(struct nl_msg *msg, void *arg)
908 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
909 struct wprobe_link *link;
910 struct wprobe_save_cb *cb = arg;
913 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
914 genlmsg_attrlen(gnlh, 0), attribute_policy);
916 if (!tb[WPROBE_ATTR_MAC] || (nla_len(tb[WPROBE_ATTR_MAC]) != 6))
919 addr = nla_data(tb[WPROBE_ATTR_MAC]);
920 link = get_link(&cb->old_list, addr);
924 if (tb[WPROBE_ATTR_FLAGS])
925 link->flags = nla_get_u32(tb[WPROBE_ATTR_FLAGS]);
927 list_add_tail(&link->list, cb->list);
933 wprobe_update_links(struct wprobe_iface *dev)
935 struct wprobe_link *l, *tmp;
937 struct wprobe_save_cb cb;
940 INIT_LIST_HEAD(&cb.old_list);
941 list_splice_init(&dev->links, &cb.old_list);
942 cb.list = &dev->links;
944 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_LINKS, true);
948 err = dev->ops->send_msg(dev, msg, save_link_handler, &cb);
952 list_for_each_entry_safe(l, tmp, &cb.old_list, list) {
961 struct wprobe_filter_data
965 struct wprobe_filter_item *buf;
970 dump_filter_handler(struct nl_msg *msg, void *arg)
972 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
973 struct wprobe_filter_data *data = arg;
979 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
980 genlmsg_attrlen(gnlh, 0), attribute_policy);
982 if (!tb[WPROBE_ATTR_NAME] || !tb[WPROBE_ATTR_FILTER_GROUP])
985 name = nla_data(tb[WPROBE_ATTR_NAME]);
986 nla_for_each_nested(p, tb[WPROBE_ATTR_FILTER_GROUP], len) {
990 if (data->buflen < count) {
993 data->buflen = count;
994 data->buf = malloc(sizeof(struct wprobe_filter_item) * count);
995 memset(data->buf, 0, sizeof(struct wprobe_filter_item) * count);
999 nla_for_each_nested(p, tb[WPROBE_ATTR_FILTER_GROUP], len) {
1000 struct wprobe_filter_item *fi;
1002 nla_parse(tb, WPROBE_ATTR_LAST, nla_data(p),
1003 nla_len(p), attribute_policy);
1005 if (!tb[WPROBE_ATTR_NAME] || !tb[WPROBE_ATTR_RXCOUNT]
1006 || !tb[WPROBE_ATTR_TXCOUNT])
1009 fi = &data->buf[count++];
1010 strncpy(fi->name, nla_data(tb[WPROBE_ATTR_NAME]), sizeof(fi->name) - 1);
1011 fi->name[sizeof(fi->name) - 1] = 0;
1012 fi->rx = nla_get_u64(tb[WPROBE_ATTR_RXCOUNT]);
1013 fi->tx = nla_get_u64(tb[WPROBE_ATTR_TXCOUNT]);
1015 data->cb(data->arg, name, data->buf, count);
1021 wprobe_dump_filters(struct wprobe_iface *dev, wprobe_filter_cb cb, void *arg)
1023 struct wprobe_filter_data data;
1032 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_FILTER, true);
1036 err = dev->ops->send_msg(dev, msg, dump_filter_handler, &data);
1044 wprobe_apply_config(struct wprobe_iface *dev)
1048 msg = wprobe_new_msg(dev, WPROBE_CMD_CONFIG, false);
1052 if (dev->interval >= 0)
1053 NLA_PUT_MSECS(msg, WPROBE_ATTR_INTERVAL, dev->interval);
1055 if (dev->filter_len < 0) {
1056 NLA_PUT(msg, WPROBE_ATTR_FILTER, 0, NULL);
1057 dev->filter_len = 0;
1058 } else if (dev->filter && dev->filter_len > 0) {
1059 NLA_PUT(msg, WPROBE_ATTR_FILTER, dev->filter_len, dev->filter);
1063 dev->ops->send_msg(dev, msg, NULL, NULL);
1072 wprobe_measure(struct wprobe_iface *dev)
1076 msg = wprobe_new_msg(dev, WPROBE_CMD_MEASURE, false);
1080 dev->ops->send_msg(dev, msg, NULL, NULL);
1084 struct wprobe_request_cb {
1085 struct list_head *list;
1086 struct list_head old_list;
1091 save_attrdata_handler(struct nl_msg *msg, void *arg)
1093 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1094 struct wprobe_request_cb *cb = arg;
1095 struct wprobe_attribute *attr;
1098 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
1099 genlmsg_attrlen(gnlh, 0), attribute_policy);
1101 if (!tb[WPROBE_ATTR_ID])
1104 if (!tb[WPROBE_ATTR_TYPE])
1107 id = nla_get_u32(tb[WPROBE_ATTR_ID]);
1108 list_for_each_entry(attr, &cb->old_list, list) {
1116 list_del_init(&attr->list);
1118 type = nla_get_u8(tb[WPROBE_ATTR_TYPE]);
1119 if (type != attr->type) {
1120 DPRINTF("WARNING: type mismatch for %s attribute '%s' (%d != %d)\n",
1121 (cb->addr ? "link" : "global"),
1127 if ((type < WPROBE_VAL_STRING) ||
1128 (type > WPROBE_VAL_U64))
1131 memset(&attr->val, 0, sizeof(attr->val));
1133 #define HANDLE_INT_TYPE(_idx, _type) \
1134 case WPROBE_VAL_S##_type: \
1135 case WPROBE_VAL_U##_type: \
1136 attr->val.U##_type = nla_get_u##_type(tb[_idx]); \
1140 HANDLE_INT_TYPE(type, 8);
1141 HANDLE_INT_TYPE(type, 16);
1142 HANDLE_INT_TYPE(type, 32);
1143 HANDLE_INT_TYPE(type, 64);
1144 case WPROBE_VAL_STRING:
1150 if (attr->flags & WPROBE_F_KEEPSTAT) {
1151 if (tb[WPROBE_VAL_SUM])
1152 attr->val.s = nla_get_u64(tb[WPROBE_VAL_SUM]);
1154 if (tb[WPROBE_VAL_SUM_SQ])
1155 attr->val.ss = nla_get_u64(tb[WPROBE_VAL_SUM_SQ]);
1157 if (tb[WPROBE_VAL_SAMPLES])
1158 attr->val.n = nla_get_u32(tb[WPROBE_VAL_SAMPLES]);
1160 if (attr->val.n > 0) {
1161 float avg = ((float) attr->val.s) / attr->val.n;
1162 float stdev = sqrt((((float) attr->val.ss) / attr->val.n) - (avg * avg));
1167 attr->val.avg = avg;
1168 attr->val.stdev = stdev;
1173 list_add_tail(&attr->list, cb->list);
1179 wprobe_request_data(struct wprobe_iface *dev, const unsigned char *addr)
1181 struct wprobe_request_cb cb;
1182 struct list_head *attrs;
1186 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_INFO, true);
1191 attrs = &dev->link_attr;
1192 NLA_PUT(msg, WPROBE_ATTR_MAC, 6, addr);
1194 attrs = &dev->global_attr;
1197 INIT_LIST_HEAD(&cb.old_list);
1198 list_splice_init(attrs, &cb.old_list);
1201 err = dev->ops->send_msg(dev, msg, save_attrdata_handler, &cb);
1202 list_splice(&cb.old_list, attrs->prev);