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);
799 *err = "Invalid argument";
807 sock = socket(AF_INET, SOCK_STREAM, 0);
811 h = gethostbyname(devstr);
813 sprintf(errbuf, "Host not found");
817 memcpy(&sa.sin_addr, h->h_addr, h->h_length);
818 sa.sin_family = AF_INET;
819 sa.sin_port = htons(wprobe_port);
820 if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0)
823 dev = wprobe_get_from_socket(sock, sep);
825 sprintf(errbuf, "wprobe connection initialization failed");
832 strcpy(errbuf, "Connection failed: ");
833 len = strlen(errbuf);
834 strerror_r(errno, errbuf + len, sizeof(errbuf) - len - 1);
848 free_attr_list(struct list_head *list)
850 struct wprobe_attribute *attr, *tmp;
852 list_for_each_entry_safe(attr, tmp, list, list) {
853 list_del(&attr->list);
859 wprobe_free_dev(struct wprobe_iface *dev)
863 free_attr_list(&dev->global_attr);
864 free_attr_list(&dev->link_attr);
865 free((void *)dev->ifname);
869 static struct wprobe_link *
870 get_link(struct list_head *list, const char *addr)
872 struct wprobe_link *l;
874 list_for_each_entry(l, list, list) {
875 if (!memcmp(l->addr, addr, 6)) {
876 list_del_init(&l->list);
881 /* no previous link found, allocate a new one */
882 l = malloc(sizeof(struct wprobe_link));
886 memset(l, 0, sizeof(struct wprobe_link));
887 memcpy(l->addr, addr, sizeof(l->addr));
888 INIT_LIST_HEAD(&l->list);
894 struct wprobe_save_cb {
895 struct list_head *list;
896 struct list_head old_list;
900 save_link_handler(struct nl_msg *msg, void *arg)
902 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
903 struct wprobe_link *link;
904 struct wprobe_save_cb *cb = arg;
907 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
908 genlmsg_attrlen(gnlh, 0), attribute_policy);
910 if (!tb[WPROBE_ATTR_MAC] || (nla_len(tb[WPROBE_ATTR_MAC]) != 6))
913 addr = nla_data(tb[WPROBE_ATTR_MAC]);
914 link = get_link(&cb->old_list, addr);
918 if (tb[WPROBE_ATTR_FLAGS])
919 link->flags = nla_get_u32(tb[WPROBE_ATTR_FLAGS]);
921 list_add_tail(&link->list, cb->list);
927 wprobe_update_links(struct wprobe_iface *dev)
929 struct wprobe_link *l, *tmp;
931 struct wprobe_save_cb cb;
934 INIT_LIST_HEAD(&cb.old_list);
935 list_splice_init(&dev->links, &cb.old_list);
936 cb.list = &dev->links;
938 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_LINKS, true);
942 err = dev->ops->send_msg(dev, msg, save_link_handler, &cb);
946 list_for_each_entry_safe(l, tmp, &cb.old_list, list) {
955 struct wprobe_filter_data
959 struct wprobe_filter_item *buf;
964 dump_filter_handler(struct nl_msg *msg, void *arg)
966 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
967 struct wprobe_filter_data *data = arg;
973 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
974 genlmsg_attrlen(gnlh, 0), attribute_policy);
976 if (!tb[WPROBE_ATTR_NAME] || !tb[WPROBE_ATTR_FILTER_GROUP])
979 name = nla_data(tb[WPROBE_ATTR_NAME]);
980 nla_for_each_nested(p, tb[WPROBE_ATTR_FILTER_GROUP], len) {
984 if (data->buflen < count) {
987 data->buflen = count;
988 data->buf = malloc(sizeof(struct wprobe_filter_item) * count);
989 memset(data->buf, 0, sizeof(struct wprobe_filter_item) * count);
993 nla_for_each_nested(p, tb[WPROBE_ATTR_FILTER_GROUP], len) {
994 struct wprobe_filter_item *fi;
996 nla_parse(tb, WPROBE_ATTR_LAST, nla_data(p),
997 nla_len(p), attribute_policy);
999 if (!tb[WPROBE_ATTR_NAME] || !tb[WPROBE_ATTR_RXCOUNT]
1000 || !tb[WPROBE_ATTR_TXCOUNT])
1003 fi = &data->buf[count++];
1004 strncpy(fi->name, nla_data(tb[WPROBE_ATTR_NAME]), sizeof(fi->name) - 1);
1005 fi->name[sizeof(fi->name) - 1] = 0;
1006 fi->rx = nla_get_u64(tb[WPROBE_ATTR_RXCOUNT]);
1007 fi->tx = nla_get_u64(tb[WPROBE_ATTR_TXCOUNT]);
1009 data->cb(data->arg, name, data->buf, count);
1015 wprobe_dump_filters(struct wprobe_iface *dev, wprobe_filter_cb cb, void *arg)
1017 struct wprobe_filter_data data;
1026 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_FILTER, true);
1030 err = dev->ops->send_msg(dev, msg, dump_filter_handler, &data);
1038 wprobe_apply_config(struct wprobe_iface *dev)
1042 msg = wprobe_new_msg(dev, WPROBE_CMD_CONFIG, false);
1046 if (dev->interval >= 0)
1047 NLA_PUT_MSECS(msg, WPROBE_ATTR_INTERVAL, dev->interval);
1049 if (dev->filter_len < 0) {
1050 NLA_PUT(msg, WPROBE_ATTR_FILTER, 0, NULL);
1051 dev->filter_len = 0;
1052 } else if (dev->filter && dev->filter_len > 0) {
1053 NLA_PUT(msg, WPROBE_ATTR_FILTER, dev->filter_len, dev->filter);
1057 dev->ops->send_msg(dev, msg, NULL, NULL);
1066 wprobe_measure(struct wprobe_iface *dev)
1070 msg = wprobe_new_msg(dev, WPROBE_CMD_MEASURE, false);
1074 dev->ops->send_msg(dev, msg, NULL, NULL);
1078 struct wprobe_request_cb {
1079 struct list_head *list;
1080 struct list_head old_list;
1085 save_attrdata_handler(struct nl_msg *msg, void *arg)
1087 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1088 struct wprobe_request_cb *cb = arg;
1089 struct wprobe_attribute *attr;
1092 nla_parse(tb, WPROBE_ATTR_LAST, genlmsg_attrdata(gnlh, 0),
1093 genlmsg_attrlen(gnlh, 0), attribute_policy);
1095 if (!tb[WPROBE_ATTR_ID])
1098 if (!tb[WPROBE_ATTR_TYPE])
1101 id = nla_get_u32(tb[WPROBE_ATTR_ID]);
1102 list_for_each_entry(attr, &cb->old_list, list) {
1110 list_del_init(&attr->list);
1112 type = nla_get_u8(tb[WPROBE_ATTR_TYPE]);
1113 if (type != attr->type) {
1114 DPRINTF("WARNING: type mismatch for %s attribute '%s' (%d != %d)\n",
1115 (cb->addr ? "link" : "global"),
1121 if ((type < WPROBE_VAL_STRING) ||
1122 (type > WPROBE_VAL_U64))
1125 memset(&attr->val, 0, sizeof(attr->val));
1127 #define HANDLE_INT_TYPE(_idx, _type) \
1128 case WPROBE_VAL_S##_type: \
1129 case WPROBE_VAL_U##_type: \
1130 attr->val.U##_type = nla_get_u##_type(tb[_idx]); \
1134 HANDLE_INT_TYPE(type, 8);
1135 HANDLE_INT_TYPE(type, 16);
1136 HANDLE_INT_TYPE(type, 32);
1137 HANDLE_INT_TYPE(type, 64);
1138 case WPROBE_VAL_STRING:
1144 if (attr->flags & WPROBE_F_KEEPSTAT) {
1145 if (tb[WPROBE_VAL_SUM])
1146 attr->val.s = nla_get_u64(tb[WPROBE_VAL_SUM]);
1148 if (tb[WPROBE_VAL_SUM_SQ])
1149 attr->val.ss = nla_get_u64(tb[WPROBE_VAL_SUM_SQ]);
1151 if (tb[WPROBE_VAL_SAMPLES])
1152 attr->val.n = nla_get_u32(tb[WPROBE_VAL_SAMPLES]);
1154 if (attr->val.n > 0) {
1155 float avg = ((float) attr->val.s) / attr->val.n;
1156 float stdev = sqrt((((float) attr->val.ss) / attr->val.n) - (avg * avg));
1161 attr->val.avg = avg;
1162 attr->val.stdev = stdev;
1167 list_add_tail(&attr->list, cb->list);
1173 wprobe_request_data(struct wprobe_iface *dev, const unsigned char *addr)
1175 struct wprobe_request_cb cb;
1176 struct list_head *attrs;
1180 msg = wprobe_new_msg(dev, WPROBE_CMD_GET_INFO, true);
1185 attrs = &dev->link_attr;
1186 NLA_PUT(msg, WPROBE_ATTR_MAC, 6, addr);
1188 attrs = &dev->global_attr;
1191 INIT_LIST_HEAD(&cb.old_list);
1192 list_splice_init(attrs, &cb.old_list);
1195 err = dev->ops->send_msg(dev, msg, save_attrdata_handler, &cb);
1196 list_splice(&cb.old_list, attrs->prev);