From 85089fea5732acc004dcc7e6aebcb0510c37f675 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Fri, 4 Jun 2010 10:48:16 -0700 Subject: Do filtering on USB and Bluetooth capturing. Do the standard userland filtering on USB and Bluetooth captures, rather than returning "success" when the filter is installed without doing anything with the filter. Also, squelch some "dereferencing type-punned pointer will break strict-aliasing rules" warnings in pcap-bt-linux.c, by using memcpy rather than pointer-casting. --- pcap-usb-linux.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'pcap-usb-linux.c') diff --git a/pcap-usb-linux.c b/pcap-usb-linux.c index fea527f..daaa900 100644 --- a/pcap-usb-linux.c +++ b/pcap-usb-linux.c @@ -122,7 +122,6 @@ static int usb_read_linux(pcap_t *, int , pcap_handler , u_char *); static int usb_read_linux_bin(pcap_t *, int , pcap_handler , u_char *); static int usb_read_linux_mmap(pcap_t *, int , pcap_handler , u_char *); static int usb_inject_linux(pcap_t *, const void *, size_t); -static int usb_setfilter_linux(pcap_t *, struct bpf_program *); static int usb_setdirection_linux(pcap_t *, pcap_direction_t); static void usb_cleanup_linux_mmap(pcap_t *); @@ -301,7 +300,7 @@ usb_activate(pcap_t* handle) handle->linktype = DLT_USB_LINUX; handle->inject_op = usb_inject_linux; - handle->setfilter_op = usb_setfilter_linux; + handle->setfilter_op = install_bpf_program; /* no kernel filtering */ handle->setdirection_op = usb_setdirection_linux; handle->set_datalink_op = NULL; /* can't change data link type */ handle->getnonblock_op = pcap_getnonblock_fd; @@ -597,12 +596,17 @@ usb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u got: uhdr->data_len = data_len; - handle->md.packets_read++; if (pkth.caplen > handle->snapshot) pkth.caplen = handle->snapshot; - callback(user, &pkth, handle->buffer); - return 1; + if (handle->fcode.bf_insns == NULL || + bpf_filter(handle->fcode.bf_insns, handle->buffer, + pkth.len, pkth.caplen)) { + handle->md.packets_read++; + callback(user, &pkth, handle->buffer); + return 1; + } + return 0; /* didn't pass filter */ } static int @@ -688,12 +692,6 @@ usb_stats_linux(pcap_t *handle, struct pcap_stat *stats) return 0; } -static int -usb_setfilter_linux(pcap_t *p, struct bpf_program *fp) -{ - return 0; -} - static int usb_setdirection_linux(pcap_t *p, pcap_direction_t d) { @@ -767,9 +765,15 @@ usb_read_linux_bin(pcap_t *handle, int max_packets, pcap_handler callback, u_cha pkth.ts.tv_sec = info.hdr->ts_sec; pkth.ts.tv_usec = info.hdr->ts_usec; - handle->md.packets_read++; - callback(user, &pkth, handle->buffer); - return 1; + if (handle->fcode.bf_insns == NULL || + bpf_filter(handle->fcode.bf_insns, handle->buffer, + pkth.len, pkth.caplen)) { + handle->md.packets_read++; + callback(user, &pkth, handle->buffer); + return 1; + } + + return 0; /* didn't pass filter */ } /* @@ -841,9 +845,13 @@ usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_ch pkth.ts.tv_sec = hdr->ts_sec; pkth.ts.tv_usec = hdr->ts_usec; - handle->md.packets_read++; - callback(user, &pkth, (u_char*) hdr); - packets++; + if (handle->fcode.bf_insns == NULL || + bpf_filter(handle->fcode.bf_insns, (u_char*) hdr, + pkth.len, pkth.caplen)) { + handle->md.packets_read++; + callback(user, &pkth, (u_char*) hdr); + packets++; + } } /* with max_packets <= 0 we stop afer the first chunk*/ -- cgit v1.2.3