2 * wprobe-core.c: Wireless probe interface core
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 <linux/kernel.h>
17 #include <linux/version.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/spinlock.h>
21 #include <linux/rcupdate.h>
22 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
23 #include <linux/rculist.h>
25 #include <linux/list.h>
27 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
28 #include <linux/prefetch.h>
30 #include <linux/skbuff.h>
31 #include <linux/wprobe.h>
32 #include <linux/math64.h>
36 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
37 #define list_for_each_rcu(pos, head) \
38 for (pos = rcu_dereference((head)->next); \
39 prefetch(pos->next), pos != (head); \
40 pos = rcu_dereference(pos->next))
43 #define WPROBE_MIN_INTERVAL 100 /* minimum measurement interval in msecs */
44 #define WPROBE_MAX_FILTER_SIZE 1024
45 #define WPROBE_MAX_FRAME_SIZE 1900
47 static struct list_head wprobe_if
;
48 static spinlock_t wprobe_lock
;
50 static struct genl_family wprobe_fam
= {
51 .id
= GENL_ID_GENERATE
,
55 /* only the first set of attributes is used for queries */
56 .maxattr
= WPROBE_ATTR_LAST
,
59 /* fake radiotap header */
60 struct wprobe_rtap_hdr
{
67 static void wprobe_update_stats(struct wprobe_iface
*dev
, struct wprobe_link
*l
);
68 static int wprobe_sync_data(struct wprobe_iface
*dev
, struct wprobe_link
*l
, bool query
);
69 static void wprobe_free_filter(struct wprobe_filter
*f
);
72 wprobe_add_link(struct wprobe_iface
*s
, struct wprobe_link
*l
, const char *addr
)
76 INIT_LIST_HEAD(&l
->list
);
77 l
->val
= kzalloc(sizeof(struct wprobe_value
) * s
->n_link_items
, GFP_ATOMIC
);
82 memcpy(&l
->addr
, addr
, ETH_ALEN
);
83 spin_lock_irqsave(&wprobe_lock
, flags
);
84 list_add_tail_rcu(&l
->list
, &s
->links
);
85 spin_unlock_irqrestore(&wprobe_lock
, flags
);
89 EXPORT_SYMBOL(wprobe_add_link
);
92 wprobe_remove_link(struct wprobe_iface
*s
, struct wprobe_link
*l
)
96 spin_lock_irqsave(&wprobe_lock
, flags
);
97 list_del_rcu(&l
->list
);
98 spin_unlock_irqrestore(&wprobe_lock
, flags
);
102 EXPORT_SYMBOL(wprobe_remove_link
);
105 wprobe_measure_timer(unsigned long data
)
107 struct wprobe_iface
*dev
= (struct wprobe_iface
*) data
;
109 /* set next measurement interval */
110 mod_timer(&dev
->measure_timer
, jiffies
+
111 msecs_to_jiffies(dev
->measure_interval
));
113 /* perform measurement */
114 wprobe_sync_data(dev
, NULL
, false);
118 wprobe_add_iface(struct wprobe_iface
*s
)
123 /* reset only wprobe private area */
124 memset(&s
->list
, 0, sizeof(struct wprobe_iface
) - offsetof(struct wprobe_iface
, list
));
127 INIT_LIST_HEAD(&s
->list
);
128 INIT_LIST_HEAD(&s
->links
);
129 setup_timer(&s
->measure_timer
, wprobe_measure_timer
, (unsigned long) s
);
131 s
->val
= kzalloc(sizeof(struct wprobe_value
) * s
->n_global_items
, GFP_ATOMIC
);
135 vsize
= max(s
->n_link_items
, s
->n_global_items
);
136 s
->query_val
= kzalloc(sizeof(struct wprobe_value
) * vsize
, GFP_ATOMIC
);
140 /* initialize defaults to be able to handle overflow,
141 * user space will need to handle this if it keeps an
142 * internal histogram */
144 s
->scale_max
= (1 << 31);
149 spin_lock_irqsave(&wprobe_lock
, flags
);
150 list_add_rcu(&s
->list
, &wprobe_if
);
151 spin_unlock_irqrestore(&wprobe_lock
, flags
);
160 EXPORT_SYMBOL(wprobe_add_iface
);
163 wprobe_remove_iface(struct wprobe_iface
*s
)
167 BUG_ON(!list_empty(&s
->links
));
169 del_timer_sync(&s
->measure_timer
);
170 spin_lock_irqsave(&wprobe_lock
, flags
);
171 list_del_rcu(&s
->list
);
172 spin_unlock_irqrestore(&wprobe_lock
, flags
);
174 /* wait for all queries to finish before freeing the
175 * temporary value storage buffer */
180 if (s
->active_filter
)
181 wprobe_free_filter(s
->active_filter
);
183 EXPORT_SYMBOL(wprobe_remove_iface
);
185 static struct wprobe_iface
*
186 wprobe_get_dev(struct nlattr
*attr
)
188 struct wprobe_iface
*dev
= NULL
;
189 struct wprobe_iface
*p
;
196 name
= nla_data(attr
);
197 list_for_each_entry_rcu(p
, &wprobe_if
, list
) {
199 if (strcmp(name
, p
->name
) != 0)
210 wprobe_add_frame(struct wprobe_iface
*dev
, const struct wprobe_wlan_hdr
*hdr
, void *data
, int len
)
212 struct wprobe_wlan_hdr
*new_hdr
;
213 struct wprobe_filter
*f
;
219 f
= rcu_dereference(dev
->active_filter
);
223 spin_lock_irqsave(&f
->lock
, flags
);
226 skb
->len
= sizeof(struct wprobe_rtap_hdr
);
227 skb
->tail
= skb
->data
+ skb
->len
;
228 if (len
+ skb
->len
> WPROBE_MAX_FRAME_SIZE
)
229 len
= WPROBE_MAX_FRAME_SIZE
- skb
->len
;
231 new_hdr
= (struct wprobe_wlan_hdr
*) skb_put(skb
, f
->hdrlen
);
232 memcpy(new_hdr
, hdr
, sizeof(struct wprobe_wlan_hdr
));
233 new_hdr
->len
= cpu_to_be16(new_hdr
->len
);
235 memcpy(skb_put(skb
, len
), data
, len
);
237 for(i
= 0; i
< f
->n_groups
; i
++) {
238 struct wprobe_filter_group
*fg
= &f
->groups
[i
];
242 for (j
= 0; j
< fg
->n_items
; j
++) {
243 struct wprobe_filter_item
*fi
= fg
->items
[j
];
245 if (!fi
->hdr
.n_items
) {
249 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,38)
250 if (sk_run_filter(skb
, fi
->filter
) == 0)
253 if (sk_run_filter(skb
, fi
->filter
, fi
->hdr
.n_items
) == 0)
260 if (!found
&& def
>= 0) {
265 struct wprobe_filter_counter
*c
= &fg
->counters
[j
];
267 if (hdr
->type
>= WPROBE_PKT_TX
)
274 spin_unlock_irqrestore(&f
->lock
, flags
);
279 EXPORT_SYMBOL(wprobe_add_frame
);
282 wprobe_sync_data(struct wprobe_iface
*dev
, struct wprobe_link
*l
, bool query
)
284 struct wprobe_value
*val
;
289 n
= dev
->n_link_items
;
292 n
= dev
->n_global_items
;
296 spin_lock_irqsave(&dev
->lock
, flags
);
297 err
= dev
->sync_data(dev
, l
, val
, !query
);
302 memcpy(dev
->query_val
, val
, sizeof(struct wprobe_value
) * n
);
304 wprobe_update_stats(dev
, l
);
306 spin_unlock_irqrestore(&dev
->lock
, flags
);
309 EXPORT_SYMBOL(wprobe_sync_data
);
312 wprobe_scale_stats(struct wprobe_iface
*dev
, const struct wprobe_item
*item
,
313 struct wprobe_value
*val
, int n
)
315 u64 scale_ts
= jiffies_64
;
318 for (i
= 0; i
< n
; i
++) {
319 if (!(item
[i
].flags
& WPROBE_F_KEEPSTAT
))
322 if (val
[i
].n
<= dev
->scale_min
)
325 /* FIXME: div_s64 seems to be very imprecise here, even when
326 * the values are scaled up */
327 val
[i
].s
*= dev
->scale_m
;
328 val
[i
].s
= div_s64(val
[i
].s
, dev
->scale_d
);
330 val
[i
].ss
*= dev
->scale_m
;
331 val
[i
].ss
= div_s64(val
[i
].ss
, dev
->scale_d
);
333 val
[i
].n
= (val
[i
].n
* dev
->scale_m
) / dev
->scale_d
;
334 val
[i
].scale_timestamp
= scale_ts
;
340 wprobe_update_stats(struct wprobe_iface
*dev
, struct wprobe_link
*l
)
342 const struct wprobe_item
*item
;
343 struct wprobe_value
*val
;
344 bool scale_stats
= false;
348 n
= dev
->n_link_items
;
349 item
= dev
->link_items
;
352 n
= dev
->n_global_items
;
353 item
= dev
->global_items
;
357 /* process statistics */
358 for (i
= 0; i
< n
; i
++) {
365 if ((item
[i
].flags
& WPROBE_F_KEEPSTAT
) &&
366 (dev
->scale_max
> 0) && (val
[i
].n
> dev
->scale_max
)) {
370 switch(item
[i
].type
) {
401 val
[i
].pending
= false;
404 wprobe_scale_stats(dev
, item
, val
, n
);
406 EXPORT_SYMBOL(wprobe_update_stats
);
408 static const struct nla_policy wprobe_policy
[WPROBE_ATTR_LAST
+1] = {
409 [WPROBE_ATTR_INTERFACE
] = { .type
= NLA_NUL_STRING
},
410 [WPROBE_ATTR_MAC
] = { .type
= NLA_STRING
},
411 [WPROBE_ATTR_FLAGS
] = { .type
= NLA_U32
},
414 [WPROBE_ATTR_INTERVAL
] = { .type
= NLA_MSECS
},
415 [WPROBE_ATTR_SAMPLES_MIN
] = { .type
= NLA_U32
},
416 [WPROBE_ATTR_SAMPLES_MAX
] = { .type
= NLA_U32
},
417 [WPROBE_ATTR_SAMPLES_SCALE_M
] = { .type
= NLA_U32
},
418 [WPROBE_ATTR_SAMPLES_SCALE_D
] = { .type
= NLA_U32
},
419 [WPROBE_ATTR_FILTER
] = { .type
= NLA_BINARY
, .len
= 32768 },
423 wprobe_check_ptr(struct list_head
*list
, struct list_head
*ptr
)
427 list_for_each_rcu(p
, list
) {
435 wprobe_send_item_value(struct sk_buff
*msg
, struct netlink_callback
*cb
,
436 struct wprobe_iface
*dev
, struct wprobe_link
*l
,
437 const struct wprobe_item
*item
,
440 struct genlmsghdr
*hdr
;
441 struct wprobe_value
*val
= dev
->query_val
;
442 u64 time
= val
[i
].last
- val
[i
].first
;
444 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
445 &wprobe_fam
, NLM_F_MULTI
, WPROBE_CMD_GET_INFO
);
447 NLA_PUT_U32(msg
, WPROBE_ATTR_ID
, i
);
448 NLA_PUT_U32(msg
, WPROBE_ATTR_FLAGS
, flags
);
449 NLA_PUT_U8(msg
, WPROBE_ATTR_TYPE
, item
[i
].type
);
450 NLA_PUT_U64(msg
, WPROBE_ATTR_DURATION
, time
);
452 switch(item
[i
].type
) {
455 NLA_PUT_U8(msg
, item
[i
].type
, val
[i
].U8
);
459 NLA_PUT_U16(msg
, item
[i
].type
, val
[i
].U16
);
463 NLA_PUT_U32(msg
, item
[i
].type
, val
[i
].U32
);
467 NLA_PUT_U64(msg
, item
[i
].type
, val
[i
].U64
);
469 case WPROBE_VAL_STRING
:
471 NLA_PUT_STRING(msg
, item
[i
].type
, val
[i
].STRING
);
473 NLA_PUT_STRING(msg
, item
[i
].type
, "");
474 /* bypass avg/stdev */
477 /* skip unknown values */
480 if (item
[i
].flags
& WPROBE_F_KEEPSTAT
) {
481 NLA_PUT_U64(msg
, WPROBE_VAL_SUM
, val
[i
].s
);
482 NLA_PUT_U64(msg
, WPROBE_VAL_SUM_SQ
, val
[i
].ss
);
483 NLA_PUT_U32(msg
, WPROBE_VAL_SAMPLES
, (u32
) val
[i
].n
);
484 NLA_PUT_MSECS(msg
, WPROBE_VAL_SCALE_TIME
, val
[i
].scale_timestamp
);
487 genlmsg_end(msg
, hdr
);
491 genlmsg_cancel(msg
, hdr
);
496 wprobe_send_item_info(struct sk_buff
*msg
, struct netlink_callback
*cb
,
497 struct wprobe_iface
*dev
,
498 const struct wprobe_item
*item
, int i
)
500 struct genlmsghdr
*hdr
;
502 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
503 &wprobe_fam
, NLM_F_MULTI
, WPROBE_CMD_GET_LIST
);
505 if ((i
== 0) && (dev
->addr
!= NULL
))
506 NLA_PUT(msg
, WPROBE_ATTR_MAC
, 6, dev
->addr
);
507 NLA_PUT_U32(msg
, WPROBE_ATTR_ID
, (u32
) i
);
508 NLA_PUT_STRING(msg
, WPROBE_ATTR_NAME
, item
[i
].name
);
509 NLA_PUT_U8(msg
, WPROBE_ATTR_TYPE
, item
[i
].type
);
510 NLA_PUT_U32(msg
, WPROBE_ATTR_FLAGS
, item
[i
].flags
);
511 genlmsg_end(msg
, hdr
);
515 genlmsg_cancel(msg
, hdr
);
520 static struct wprobe_link
*
521 wprobe_find_link(struct wprobe_iface
*dev
, const char *mac
)
523 struct wprobe_link
*l
;
525 list_for_each_entry_rcu(l
, &dev
->links
, list
) {
526 if (!memcmp(l
->addr
, mac
, 6))
533 wprobe_dump_filter_group(struct sk_buff
*msg
, struct wprobe_filter_group
*fg
, struct netlink_callback
*cb
)
535 struct genlmsghdr
*hdr
;
536 struct nlattr
*group
, *item
;
539 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
540 &wprobe_fam
, NLM_F_MULTI
, WPROBE_CMD_GET_FILTER
);
544 NLA_PUT_STRING(msg
, WPROBE_ATTR_NAME
, fg
->name
);
545 group
= nla_nest_start(msg
, WPROBE_ATTR_FILTER_GROUP
);
546 for (i
= 0; i
< fg
->n_items
; i
++) {
547 struct wprobe_filter_item
*fi
= fg
->items
[i
];
548 struct wprobe_filter_counter
*fc
= &fg
->counters
[i
];
550 item
= nla_nest_start(msg
, WPROBE_ATTR_FILTER_GROUP
);
551 NLA_PUT_STRING(msg
, WPROBE_ATTR_NAME
, fi
->hdr
.name
);
552 NLA_PUT_U64(msg
, WPROBE_ATTR_RXCOUNT
, fc
->rx
);
553 NLA_PUT_U64(msg
, WPROBE_ATTR_TXCOUNT
, fc
->tx
);
554 nla_nest_end(msg
, item
);
557 nla_nest_end(msg
, group
);
558 genlmsg_end(msg
, hdr
);
562 genlmsg_cancel(msg
, hdr
);
567 wprobe_dump_filters(struct sk_buff
*skb
, struct netlink_callback
*cb
)
569 struct wprobe_iface
*dev
= (struct wprobe_iface
*)cb
->args
[0];
570 struct wprobe_filter
*f
;
575 err
= nlmsg_parse(cb
->nlh
, GENL_HDRLEN
+ wprobe_fam
.hdrsize
,
576 wprobe_fam
.attrbuf
, wprobe_fam
.maxattr
, wprobe_policy
);
580 dev
= wprobe_get_dev(wprobe_fam
.attrbuf
[WPROBE_ATTR_INTERFACE
]);
586 cb
->args
[0] = (long) dev
;
589 if (!wprobe_check_ptr(&wprobe_if
, &dev
->list
)) {
596 f
= rcu_dereference(dev
->active_filter
);
600 for (i
= cb
->args
[1]; i
< f
->n_groups
; i
++) {
601 if (unlikely(!wprobe_dump_filter_group(skb
, &f
->groups
[i
], cb
)))
613 wprobe_dump_link(struct sk_buff
*msg
, struct wprobe_link
*l
, struct netlink_callback
*cb
)
615 struct genlmsghdr
*hdr
;
617 hdr
= genlmsg_put(msg
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
618 &wprobe_fam
, NLM_F_MULTI
, WPROBE_CMD_GET_LINKS
);
622 NLA_PUT(msg
, WPROBE_ATTR_MAC
, 6, l
->addr
);
623 genlmsg_end(msg
, hdr
);
627 genlmsg_cancel(msg
, hdr
);
632 wprobe_dump_links(struct sk_buff
*skb
, struct netlink_callback
*cb
)
634 struct wprobe_iface
*dev
= (struct wprobe_iface
*)cb
->args
[0];
635 struct wprobe_link
*l
;
640 err
= nlmsg_parse(cb
->nlh
, GENL_HDRLEN
+ wprobe_fam
.hdrsize
,
641 wprobe_fam
.attrbuf
, wprobe_fam
.maxattr
, wprobe_policy
);
645 dev
= wprobe_get_dev(wprobe_fam
.attrbuf
[WPROBE_ATTR_INTERFACE
]);
651 cb
->args
[0] = (long) dev
;
653 if (!wprobe_check_ptr(&wprobe_if
, &dev
->list
)) {
660 list_for_each_entry_rcu(l
, &dev
->links
, list
) {
664 if (unlikely(!wprobe_dump_link(skb
, l
, cb
)))
676 #define WPROBE_F_LINK (1 << 31) /* for internal use */
678 wprobe_dump_info(struct sk_buff
*skb
, struct netlink_callback
*cb
)
680 struct wprobe_iface
*dev
= (struct wprobe_iface
*)cb
->args
[0];
681 struct wprobe_link
*l
= (struct wprobe_link
*)cb
->args
[1];
682 struct wprobe_value
*val
;
683 const struct wprobe_item
*item
;
684 struct genlmsghdr
*hdr
;
686 int cmd
, n
, i
= cb
->args
[3];
687 u32 vflags
= cb
->args
[2];
690 hdr
= (struct genlmsghdr
*)nlmsg_data(cb
->nlh
);
693 /* since the attribute value list might be too big for a single netlink
694 * message, the device, link and offset get stored in the netlink callback.
695 * if this is the first request, we need to do the full lookup for the device.
697 * access to the device and link structure is synchronized through rcu.
701 err
= nlmsg_parse(cb
->nlh
, GENL_HDRLEN
+ wprobe_fam
.hdrsize
,
702 wprobe_fam
.attrbuf
, wprobe_fam
.maxattr
, wprobe_policy
);
707 dev
= wprobe_get_dev(wprobe_fam
.attrbuf
[WPROBE_ATTR_INTERFACE
]);
711 if (cmd
== WPROBE_CMD_GET_INFO
) {
712 if (wprobe_fam
.attrbuf
[WPROBE_ATTR_MAC
]) {
713 l
= wprobe_find_link(dev
, nla_data(wprobe_fam
.attrbuf
[WPROBE_ATTR_MAC
]));
721 item
= dev
->link_items
;
722 n
= dev
->n_link_items
;
725 item
= dev
->global_items
;
726 n
= dev
->n_global_items
;
730 /* sync data and move to temp storage for the query */
731 spin_lock_irqsave(&dev
->lock
, flags
);
732 err
= wprobe_sync_data(dev
, l
, true);
734 memcpy(dev
->query_val
, val
, n
* sizeof(struct wprobe_value
));
735 spin_unlock_irqrestore(&dev
->lock
, flags
);
741 if (wprobe_fam
.attrbuf
[WPROBE_ATTR_FLAGS
])
742 vflags
|= nla_get_u32(wprobe_fam
.attrbuf
[WPROBE_ATTR_FLAGS
]);
744 if (wprobe_fam
.attrbuf
[WPROBE_ATTR_MAC
])
745 vflags
|= WPROBE_F_LINK
;
747 cb
->args
[0] = (long) dev
;
748 cb
->args
[1] = (long) l
;
749 cb
->args
[2] = vflags
;
752 /* when pulling pointers from the callback, validate them
753 * against the list using rcu to make sure that we won't
754 * dereference pointers to free'd memory after the last
757 if (!wprobe_check_ptr(&wprobe_if
, &dev
->list
))
760 if (l
&& !wprobe_check_ptr(&dev
->links
, &l
->list
))
764 if (vflags
& WPROBE_F_LINK
) {
765 item
= dev
->link_items
;
766 n
= dev
->n_link_items
;
768 item
= dev
->global_items
;
769 n
= dev
->n_global_items
;
774 case WPROBE_CMD_GET_INFO
:
776 if (!wprobe_send_item_value(skb
, cb
, dev
, l
, item
, i
, vflags
))
781 case WPROBE_CMD_GET_LIST
:
783 if (!wprobe_send_item_info(skb
, cb
, dev
, item
, i
))
802 wprobe_update_auto_measurement(struct wprobe_iface
*dev
, u32 interval
)
804 if (interval
&& (interval
< WPROBE_MIN_INTERVAL
))
807 if (!interval
&& dev
->measure_interval
)
808 del_timer_sync(&dev
->measure_timer
);
810 dev
->measure_interval
= interval
;
814 /* kick of a new measurement immediately */
815 mod_timer(&dev
->measure_timer
, jiffies
+ 1);
821 wprobe_measure(struct sk_buff
*skb
, struct genl_info
*info
)
823 struct wprobe_iface
*dev
;
824 struct wprobe_link
*l
= NULL
;
828 dev
= wprobe_get_dev(info
->attrs
[WPROBE_ATTR_INTERFACE
]);
832 if (info
->attrs
[WPROBE_ATTR_MAC
]) {
833 l
= wprobe_find_link(dev
, nla_data(wprobe_fam
.attrbuf
[WPROBE_ATTR_MAC
]));
838 err
= wprobe_sync_data(dev
, l
, false);
846 wprobe_check_filter(void *data
, int datalen
, int gs
)
848 struct wprobe_filter_item_hdr
*hdr
;
849 void *orig_data
= data
;
850 void *end
= data
+ datalen
;
851 int i
, j
, k
, is
, cur_is
;
853 for (i
= j
= is
= 0; i
< gs
; i
++) {
855 data
+= sizeof(*hdr
);
861 cur_is
= be32_to_cpu(hdr
->n_items
);
862 hdr
->n_items
= cur_is
;
864 for (j
= 0; j
< cur_is
; j
++) {
865 struct sock_filter
*sf
;
869 data
+= sizeof(*hdr
);
874 n_items
= be32_to_cpu(hdr
->n_items
);
875 hdr
->n_items
= n_items
;
882 for (k
= 0; k
< n_items
; k
++) {
883 sf
->code
= be16_to_cpu(sf
->code
);
884 sf
->k
= be32_to_cpu(sf
->k
);
887 if (sk_chk_filter(data
, n_items
) != 0) {
888 printk("%s: filter check failed at group %d, item %d\n", __func__
, i
, j
);
892 data
+= n_items
* sizeof(struct sock_filter
);
898 printk(KERN_ERR
"%s: overrun during filter check at group %d, item %d, offset=%d, len=%d\n", __func__
, i
, j
, (data
- orig_data
), datalen
);
903 wprobe_free_filter(struct wprobe_filter
*f
)
918 wprobe_set_filter(struct wprobe_iface
*dev
, void *data
, int len
)
920 struct wprobe_filter_hdr
*fhdr
;
921 struct wprobe_rtap_hdr
*rtap
;
922 struct wprobe_filter
*f
;
923 int i
, j
, cur_is
, is
, gs
;
925 if (len
< sizeof(*fhdr
))
929 data
+= sizeof(*fhdr
);
930 len
-= sizeof(*fhdr
);
932 if (memcmp(fhdr
->magic
, "WPFF", 4) != 0) {
933 printk(KERN_ERR
"%s: filter rejected (invalid magic)\n", __func__
);
937 gs
= be16_to_cpu(fhdr
->n_groups
);
938 is
= wprobe_check_filter(data
, len
, gs
);
942 f
= kzalloc(sizeof(struct wprobe_filter
) +
943 gs
* sizeof(struct wprobe_filter_group
), GFP_ATOMIC
);
947 f
->skb
= alloc_skb(WPROBE_MAX_FRAME_SIZE
, GFP_ATOMIC
);
951 f
->data
= kmalloc(len
, GFP_ATOMIC
);
955 f
->items
= kzalloc(sizeof(struct wprobe_filter_item
*) * is
, GFP_ATOMIC
);
959 f
->counters
= kzalloc(sizeof(struct wprobe_filter_counter
) * is
, GFP_ATOMIC
);
963 spin_lock_init(&f
->lock
);
964 memcpy(f
->data
, data
, len
);
967 if (f
->hdrlen
< sizeof(struct wprobe_wlan_hdr
))
968 f
->hdrlen
= sizeof(struct wprobe_wlan_hdr
);
970 rtap
= (struct wprobe_rtap_hdr
*)skb_put(f
->skb
, sizeof(*rtap
));
971 memset(rtap
, 0, sizeof(*rtap
));
972 rtap
->len
= cpu_to_le16(sizeof(struct wprobe_rtap_hdr
) + f
->hdrlen
);
976 for (i
= 0; i
< gs
; i
++) {
977 struct wprobe_filter_item_hdr
*hdr
= data
;
978 struct wprobe_filter_group
*g
= &f
->groups
[i
];
980 data
+= sizeof(*hdr
);
982 g
->items
= &f
->items
[cur_is
];
983 g
->counters
= &f
->counters
[cur_is
];
984 g
->n_items
= hdr
->n_items
;
986 for (j
= 0; j
< g
->n_items
; j
++) {
988 f
->items
[cur_is
++] = data
;
989 data
+= sizeof(*hdr
) + hdr
->n_items
* sizeof(struct sock_filter
);
992 rcu_assign_pointer(dev
->active_filter
, f
);
996 wprobe_free_filter(f
);
1001 wprobe_set_config(struct sk_buff
*skb
, struct genl_info
*info
)
1003 struct wprobe_iface
*dev
;
1004 unsigned long flags
;
1006 u32 scale_min
, scale_max
;
1007 u32 scale_m
, scale_d
;
1008 struct nlattr
*attr
;
1009 struct wprobe_filter
*filter_free
= NULL
;
1012 dev
= wprobe_get_dev(info
->attrs
[WPROBE_ATTR_INTERFACE
]);
1017 spin_lock_irqsave(&dev
->lock
, flags
);
1018 if (info
->attrs
[WPROBE_ATTR_MAC
]) {
1019 /* not supported yet */
1023 if (info
->attrs
[WPROBE_ATTR_FLAGS
]) {
1024 u32 flags
= nla_get_u32(info
->attrs
[WPROBE_ATTR_FLAGS
]);
1026 if (flags
& BIT(WPROBE_F_RESET
)) {
1027 struct wprobe_link
*l
;
1029 memset(dev
->val
, 0, sizeof(struct wprobe_value
) * dev
->n_global_items
);
1030 list_for_each_entry_rcu(l
, &dev
->links
, list
) {
1031 memset(l
->val
, 0, sizeof(struct wprobe_value
) * dev
->n_link_items
);
1036 if (info
->attrs
[WPROBE_ATTR_SAMPLES_MIN
] ||
1037 info
->attrs
[WPROBE_ATTR_SAMPLES_MAX
]) {
1038 if ((attr
= info
->attrs
[WPROBE_ATTR_SAMPLES_MIN
]))
1039 scale_min
= nla_get_u32(attr
);
1041 scale_min
= dev
->scale_min
;
1043 if ((attr
= info
->attrs
[WPROBE_ATTR_SAMPLES_MAX
]))
1044 scale_max
= nla_get_u32(attr
);
1046 scale_max
= dev
->scale_max
;
1048 if ((!scale_min
&& !scale_max
) ||
1049 (scale_min
&& scale_max
&& (scale_min
< scale_max
))) {
1050 dev
->scale_min
= scale_min
;
1051 dev
->scale_max
= scale_max
;
1057 if (info
->attrs
[WPROBE_ATTR_SAMPLES_SCALE_M
] &&
1058 info
->attrs
[WPROBE_ATTR_SAMPLES_SCALE_D
]) {
1060 scale_m
= nla_get_u32(info
->attrs
[WPROBE_ATTR_SAMPLES_SCALE_M
]);
1061 scale_d
= nla_get_u32(info
->attrs
[WPROBE_ATTR_SAMPLES_SCALE_D
]);
1063 if (!scale_d
|| (scale_m
> scale_d
))
1066 dev
->scale_m
= scale_m
;
1067 dev
->scale_d
= scale_d
;
1070 if ((attr
= info
->attrs
[WPROBE_ATTR_FILTER
])) {
1071 filter_free
= rcu_dereference(dev
->active_filter
);
1072 rcu_assign_pointer(dev
->active_filter
, NULL
);
1073 if (nla_len(attr
) > 0)
1074 wprobe_set_filter(dev
, nla_data(attr
), nla_len(attr
));
1078 if (info
->attrs
[WPROBE_ATTR_INTERVAL
]) {
1079 /* change of measurement interval requested */
1080 err
= wprobe_update_auto_measurement(dev
,
1081 (u32
) nla_get_u64(info
->attrs
[WPROBE_ATTR_INTERVAL
]));
1085 spin_unlock_irqrestore(&dev
->lock
, flags
);
1090 wprobe_free_filter(filter_free
);
1095 static struct genl_ops wprobe_ops
[] = {
1097 .cmd
= WPROBE_CMD_GET_INFO
,
1098 .dumpit
= wprobe_dump_info
,
1099 .policy
= wprobe_policy
,
1102 .cmd
= WPROBE_CMD_GET_LIST
,
1103 .dumpit
= wprobe_dump_info
,
1104 .policy
= wprobe_policy
,
1107 .cmd
= WPROBE_CMD_MEASURE
,
1108 .doit
= wprobe_measure
,
1109 .policy
= wprobe_policy
,
1112 .cmd
= WPROBE_CMD_GET_LINKS
,
1113 .dumpit
= wprobe_dump_links
,
1114 .policy
= wprobe_policy
,
1117 .cmd
= WPROBE_CMD_CONFIG
,
1118 .doit
= wprobe_set_config
,
1119 .policy
= wprobe_policy
,
1122 .cmd
= WPROBE_CMD_GET_FILTER
,
1123 .dumpit
= wprobe_dump_filters
,
1124 .policy
= wprobe_policy
,
1131 BUG_ON(!list_empty(&wprobe_if
));
1132 genl_unregister_family(&wprobe_fam
);
1141 spin_lock_init(&wprobe_lock
);
1142 INIT_LIST_HEAD(&wprobe_if
);
1144 err
= genl_register_family(&wprobe_fam
);
1148 for (i
= 0; i
< ARRAY_SIZE(wprobe_ops
); i
++) {
1149 err
= genl_register_ops(&wprobe_fam
, &wprobe_ops
[i
]);
1157 genl_unregister_family(&wprobe_fam
);
1161 module_init(wprobe_init
);
1162 module_exit(wprobe_exit
);
1163 MODULE_LICENSE("GPL");