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 write(socket
, &mhdr
, sizeof(mhdr
));
565 memcpy(buf
, nlh
, buflen
);
567 gnlh
= nlmsg_data(nlh
);
568 data
= genlmsg_data(gnlh
);
569 datalen
= genlmsg_len(gnlh
);
571 wprobe_swap_nested(data
, datalen
, true);
572 swap_genlmsghdr(gnlh
);
574 ret
= write(socket
, buf
, buflen
);
581 wprobe_remote_send_msg(struct wprobe_iface
*dev
, struct nl_msg
*msg
, wprobe_cb_t callback
, void *arg
)
583 struct wprobe_msg_hdr mhdr
;
586 wprobe_msg_to_network(dev
->sockfd
, msg
);
589 if (read(dev
->sockfd
, &mhdr
, sizeof(mhdr
)) != sizeof(mhdr
)) {
590 DPRINTF("Failed to read response header\n");
593 wprobe_swap_msg_hdr(&mhdr
);
595 switch(mhdr
.status
) {
596 case WPROBE_MSG_DATA
:
597 if (mhdr
.len
> WPROBE_MAX_MSGLEN
) {
598 fprintf(stderr
, "Invalid length in received response message.\n");
602 msg
= wprobe_msg_from_network(dev
->sockfd
, mhdr
.len
);
611 } while (mhdr
.status
!= WPROBE_MSG_DONE
);
621 wprobe_socket_dev_free(struct wprobe_iface
*dev
)
623 if (dev
->sockfd
>= 0)
627 static const struct wprobe_iface_ops wprobe_remote_ops
= {
628 .send_msg
= wprobe_remote_send_msg
,
629 .free
= wprobe_socket_dev_free
,
633 #ifndef NO_LOCAL_ACCESS
635 wprobe_server_init(int socket
)
637 struct wprobe_init_hdr hdr
;
640 ret
= wprobe_local_init();
644 memset(&hdr
, 0, sizeof(hdr
));
645 memcpy(hdr
.pre
.magic
, WPROBE_MAGIC_STR
, sizeof(WPROBE_MAGIC_STR
));
647 hdr
.v0
.genl_family
= genl_family_get_id(family
);
648 SWAP16(hdr
.v0
.genl_family
);
649 write(socket
, (unsigned char *)&hdr
, sizeof(hdr
));
655 wprobe_server_cb(struct nl_msg
*msg
, void *arg
)
660 ret
= wprobe_msg_to_network(*socket
, msg
);
669 wprobe_server_handle(int socket
)
671 struct wprobe_msg_hdr mhdr
;
675 ret
= read(socket
, &mhdr
, sizeof(mhdr
));
676 if (ret
!= sizeof(mhdr
)) {
680 DPRINTF("Failed to read request header\n");
683 wprobe_swap_msg_hdr(&mhdr
);
685 switch(mhdr
.status
) {
686 case WPROBE_MSG_DATA
:
687 if (mhdr
.len
> WPROBE_MAX_MSGLEN
) {
688 DPRINTF("Invalid length in received response message.\n");
691 msg
= wprobe_msg_from_network(socket
, mhdr
.len
);
694 DPRINTF("Invalid request header type\n");
699 DPRINTF("Failed to get message\n");
703 ret
= wprobe_local_send_msg(NULL
, msg
, wprobe_server_cb
, &socket
);
705 memset(&mhdr
, 0, sizeof(mhdr
));
706 mhdr
.status
= WPROBE_MSG_DONE
;
708 mhdr
.error
= (uint16_t) -ret
;
710 ret
= write(socket
, (unsigned char *)&mhdr
, sizeof(mhdr
));
718 wprobe_server_done(void)
720 wprobe_local_free(NULL
);
724 struct wprobe_iface
*
725 wprobe_get_from_socket(int socket
, const char *name
)
727 struct wprobe_iface
*dev
;
728 struct wprobe_init_hdr hdr
;
730 dev
= wprobe_alloc_dev();
734 dev
->ops
= &wprobe_remote_ops
;
735 dev
->sockfd
= socket
;
736 dev
->ifname
= strdup(name
);
738 /* read version and header length */
739 if (read(socket
, &hdr
.pre
, sizeof(hdr
.pre
)) != sizeof(hdr
.pre
)) {
740 DPRINTF("Could not read header\n");
744 /* magic not found */
745 if (memcmp(hdr
.pre
.magic
, WPROBE_MAGIC_STR
, sizeof(hdr
.pre
.magic
)) != 0) {
746 DPRINTF("Magic does not match\n");
750 /* unsupported version */
751 if (hdr
.pre
.version
!= 0) {
752 DPRINTF("Protocol version does not match\n");
756 if (read(socket
, &hdr
.v0
, sizeof(hdr
.v0
)) != sizeof(hdr
.v0
)) {
757 DPRINTF("Could not read header data\n");
761 SWAP16(hdr
.pre
.extra
);
762 SWAP16(hdr
.v0
.genl_family
);
763 dev
->genl_family
= hdr
.v0
.genl_family
;
765 if (wprobe_init_dev(dev
) < 0) {
766 DPRINTF("Could not initialize device\n");
774 wprobe_free_dev(dev
);
778 struct wprobe_iface
*
779 wprobe_get_auto(const char *arg
, char **err
)
781 static struct sockaddr_in sa
;
782 static char errbuf
[512];
784 struct wprobe_iface
*dev
= NULL
;
786 char *devstr
= strdup(arg
);
794 sep
= strchr(devstr
, ':');
796 #ifndef NO_LOCAL_ACCESS
798 return wprobe_get_dev(arg
);
801 *err
= "Invalid argument";
809 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
813 h
= gethostbyname(devstr
);
815 sprintf(errbuf
, "Host not found");
819 memcpy(&sa
.sin_addr
, h
->h_addr
, h
->h_length
);
820 sa
.sin_family
= AF_INET
;
821 sa
.sin_port
= htons(wprobe_port
);
822 if (connect(sock
, (struct sockaddr
*)&sa
, sizeof(sa
)) < 0)
825 dev
= wprobe_get_from_socket(sock
, sep
);
827 sprintf(errbuf
, "wprobe connection initialization failed");
834 strcpy(errbuf
, "Connection failed: ");
835 len
= strlen(errbuf
);
836 strerror_r(errno
, errbuf
+ len
, sizeof(errbuf
) - len
- 1);
850 free_attr_list(struct list_head
*list
)
852 struct wprobe_attribute
*attr
, *tmp
;
854 list_for_each_entry_safe(attr
, tmp
, list
, list
) {
855 list_del(&attr
->list
);
861 wprobe_free_dev(struct wprobe_iface
*dev
)
865 free_attr_list(&dev
->global_attr
);
866 free_attr_list(&dev
->link_attr
);
867 free((void *)dev
->ifname
);
871 static struct wprobe_link
*
872 get_link(struct list_head
*list
, const char *addr
)
874 struct wprobe_link
*l
;
876 list_for_each_entry(l
, list
, list
) {
877 if (!memcmp(l
->addr
, addr
, 6)) {
878 list_del_init(&l
->list
);
883 /* no previous link found, allocate a new one */
884 l
= malloc(sizeof(struct wprobe_link
));
888 memset(l
, 0, sizeof(struct wprobe_link
));
889 memcpy(l
->addr
, addr
, sizeof(l
->addr
));
890 INIT_LIST_HEAD(&l
->list
);
896 struct wprobe_save_cb
{
897 struct list_head
*list
;
898 struct list_head old_list
;
902 save_link_handler(struct nl_msg
*msg
, void *arg
)
904 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
905 struct wprobe_link
*link
;
906 struct wprobe_save_cb
*cb
= arg
;
909 nla_parse(tb
, WPROBE_ATTR_LAST
, genlmsg_attrdata(gnlh
, 0),
910 genlmsg_attrlen(gnlh
, 0), attribute_policy
);
912 if (!tb
[WPROBE_ATTR_MAC
] || (nla_len(tb
[WPROBE_ATTR_MAC
]) != 6))
915 addr
= nla_data(tb
[WPROBE_ATTR_MAC
]);
916 link
= get_link(&cb
->old_list
, addr
);
920 if (tb
[WPROBE_ATTR_FLAGS
])
921 link
->flags
= nla_get_u32(tb
[WPROBE_ATTR_FLAGS
]);
923 list_add_tail(&link
->list
, cb
->list
);
929 wprobe_update_links(struct wprobe_iface
*dev
)
931 struct wprobe_link
*l
, *tmp
;
933 struct wprobe_save_cb cb
;
936 INIT_LIST_HEAD(&cb
.old_list
);
937 list_splice_init(&dev
->links
, &cb
.old_list
);
938 cb
.list
= &dev
->links
;
940 msg
= wprobe_new_msg(dev
, WPROBE_CMD_GET_LINKS
, true);
944 err
= dev
->ops
->send_msg(dev
, msg
, save_link_handler
, &cb
);
948 list_for_each_entry_safe(l
, tmp
, &cb
.old_list
, list
) {
957 struct wprobe_filter_data
961 struct wprobe_filter_item
*buf
;
966 dump_filter_handler(struct nl_msg
*msg
, void *arg
)
968 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
969 struct wprobe_filter_data
*data
= arg
;
975 nla_parse(tb
, WPROBE_ATTR_LAST
, genlmsg_attrdata(gnlh
, 0),
976 genlmsg_attrlen(gnlh
, 0), attribute_policy
);
978 if (!tb
[WPROBE_ATTR_NAME
] || !tb
[WPROBE_ATTR_FILTER_GROUP
])
981 name
= nla_data(tb
[WPROBE_ATTR_NAME
]);
982 nla_for_each_nested(p
, tb
[WPROBE_ATTR_FILTER_GROUP
], len
) {
986 if (data
->buflen
< count
) {
989 data
->buflen
= count
;
990 data
->buf
= malloc(sizeof(struct wprobe_filter_item
) * count
);
991 memset(data
->buf
, 0, sizeof(struct wprobe_filter_item
) * count
);
995 nla_for_each_nested(p
, tb
[WPROBE_ATTR_FILTER_GROUP
], len
) {
996 struct wprobe_filter_item
*fi
;
998 nla_parse(tb
, WPROBE_ATTR_LAST
, nla_data(p
),
999 nla_len(p
), attribute_policy
);
1001 if (!tb
[WPROBE_ATTR_NAME
] || !tb
[WPROBE_ATTR_RXCOUNT
]
1002 || !tb
[WPROBE_ATTR_TXCOUNT
])
1005 fi
= &data
->buf
[count
++];
1006 strncpy(fi
->name
, nla_data(tb
[WPROBE_ATTR_NAME
]), sizeof(fi
->name
) - 1);
1007 fi
->name
[sizeof(fi
->name
) - 1] = 0;
1008 fi
->rx
= nla_get_u64(tb
[WPROBE_ATTR_RXCOUNT
]);
1009 fi
->tx
= nla_get_u64(tb
[WPROBE_ATTR_TXCOUNT
]);
1011 data
->cb(data
->arg
, name
, data
->buf
, count
);
1017 wprobe_dump_filters(struct wprobe_iface
*dev
, wprobe_filter_cb cb
, void *arg
)
1019 struct wprobe_filter_data data
;
1028 msg
= wprobe_new_msg(dev
, WPROBE_CMD_GET_FILTER
, true);
1032 err
= dev
->ops
->send_msg(dev
, msg
, dump_filter_handler
, &data
);
1040 wprobe_apply_config(struct wprobe_iface
*dev
)
1044 msg
= wprobe_new_msg(dev
, WPROBE_CMD_CONFIG
, false);
1048 if (dev
->interval
>= 0)
1049 NLA_PUT_MSECS(msg
, WPROBE_ATTR_INTERVAL
, dev
->interval
);
1051 if (dev
->filter_len
< 0) {
1052 NLA_PUT(msg
, WPROBE_ATTR_FILTER
, 0, NULL
);
1053 dev
->filter_len
= 0;
1054 } else if (dev
->filter
&& dev
->filter_len
> 0) {
1055 NLA_PUT(msg
, WPROBE_ATTR_FILTER
, dev
->filter_len
, dev
->filter
);
1059 dev
->ops
->send_msg(dev
, msg
, NULL
, NULL
);
1068 wprobe_measure(struct wprobe_iface
*dev
)
1072 msg
= wprobe_new_msg(dev
, WPROBE_CMD_MEASURE
, false);
1076 dev
->ops
->send_msg(dev
, msg
, NULL
, NULL
);
1080 struct wprobe_request_cb
{
1081 struct list_head
*list
;
1082 struct list_head old_list
;
1087 save_attrdata_handler(struct nl_msg
*msg
, void *arg
)
1089 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
1090 struct wprobe_request_cb
*cb
= arg
;
1091 struct wprobe_attribute
*attr
;
1094 nla_parse(tb
, WPROBE_ATTR_LAST
, genlmsg_attrdata(gnlh
, 0),
1095 genlmsg_attrlen(gnlh
, 0), attribute_policy
);
1097 if (!tb
[WPROBE_ATTR_ID
])
1100 if (!tb
[WPROBE_ATTR_TYPE
])
1103 id
= nla_get_u32(tb
[WPROBE_ATTR_ID
]);
1104 list_for_each_entry(attr
, &cb
->old_list
, list
) {
1112 list_del_init(&attr
->list
);
1114 type
= nla_get_u8(tb
[WPROBE_ATTR_TYPE
]);
1115 if (type
!= attr
->type
) {
1116 DPRINTF("WARNING: type mismatch for %s attribute '%s' (%d != %d)\n",
1117 (cb
->addr
? "link" : "global"),
1123 if ((type
< WPROBE_VAL_STRING
) ||
1124 (type
> WPROBE_VAL_U64
))
1127 memset(&attr
->val
, 0, sizeof(attr
->val
));
1129 #define HANDLE_INT_TYPE(_idx, _type) \
1130 case WPROBE_VAL_S##_type: \
1131 case WPROBE_VAL_U##_type: \
1132 attr->val.U##_type = nla_get_u##_type(tb[_idx]); \
1136 HANDLE_INT_TYPE(type
, 8);
1137 HANDLE_INT_TYPE(type
, 16);
1138 HANDLE_INT_TYPE(type
, 32);
1139 HANDLE_INT_TYPE(type
, 64);
1140 case WPROBE_VAL_STRING
:
1146 if (attr
->flags
& WPROBE_F_KEEPSTAT
) {
1147 if (tb
[WPROBE_VAL_SUM
])
1148 attr
->val
.s
= nla_get_u64(tb
[WPROBE_VAL_SUM
]);
1150 if (tb
[WPROBE_VAL_SUM_SQ
])
1151 attr
->val
.ss
= nla_get_u64(tb
[WPROBE_VAL_SUM_SQ
]);
1153 if (tb
[WPROBE_VAL_SAMPLES
])
1154 attr
->val
.n
= nla_get_u32(tb
[WPROBE_VAL_SAMPLES
]);
1156 if (attr
->val
.n
> 0) {
1157 float avg
= ((float) attr
->val
.s
) / attr
->val
.n
;
1158 float stdev
= sqrt((((float) attr
->val
.ss
) / attr
->val
.n
) - (avg
* avg
));
1163 attr
->val
.avg
= avg
;
1164 attr
->val
.stdev
= stdev
;
1169 list_add_tail(&attr
->list
, cb
->list
);
1175 wprobe_request_data(struct wprobe_iface
*dev
, const unsigned char *addr
)
1177 struct wprobe_request_cb cb
;
1178 struct list_head
*attrs
;
1182 msg
= wprobe_new_msg(dev
, WPROBE_CMD_GET_INFO
, true);
1187 attrs
= &dev
->link_attr
;
1188 NLA_PUT(msg
, WPROBE_ATTR_MAC
, 6, addr
);
1190 attrs
= &dev
->global_attr
;
1193 INIT_LIST_HEAD(&cb
.old_list
);
1194 list_splice_init(attrs
, &cb
.old_list
);
1197 err
= dev
->ops
->send_msg(dev
, msg
, save_attrdata_handler
, &cb
);
1198 list_splice(&cb
.old_list
, attrs
->prev
);