xref: /freebsd/contrib/libpcap/pcap-dbus.c (revision 6f9cba8f)
1681ed54cSXin LI /*
2681ed54cSXin LI  * Copyright (c) 2012 Jakub Zawadzki
3681ed54cSXin LI  * All rights reserved.
4681ed54cSXin LI  *
5681ed54cSXin LI  * Redistribution and use in source and binary forms, with or without
6681ed54cSXin LI  * modification, are permitted provided that the following conditions
7681ed54cSXin LI  * are met:
8681ed54cSXin LI  *
9681ed54cSXin LI  * 1. Redistributions of source code must retain the above copyright
10681ed54cSXin LI  * notice, this list of conditions and the following disclaimer.
11681ed54cSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
12681ed54cSXin LI  * notice, this list of conditions and the following disclaimer in the
13681ed54cSXin LI  * documentation and/or other materials provided with the distribution.
14681ed54cSXin LI  * 3. The name of the author may not be used to endorse or promote
15681ed54cSXin LI  * products derived from this software without specific prior written
16681ed54cSXin LI  * permission.
17681ed54cSXin LI  *
18681ed54cSXin LI  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19681ed54cSXin LI  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20681ed54cSXin LI  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21681ed54cSXin LI  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22681ed54cSXin LI  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23681ed54cSXin LI  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24681ed54cSXin LI  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25681ed54cSXin LI  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26681ed54cSXin LI  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27681ed54cSXin LI  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28681ed54cSXin LI  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29681ed54cSXin LI  */
30681ed54cSXin LI 
31681ed54cSXin LI #ifdef HAVE_CONFIG_H
32b00ab754SHans Petter Selasky #include <config.h>
33681ed54cSXin LI #endif
34681ed54cSXin LI 
35681ed54cSXin LI #include <string.h>
36681ed54cSXin LI 
37681ed54cSXin LI #include <time.h>
38681ed54cSXin LI #include <sys/time.h>
39681ed54cSXin LI 
40681ed54cSXin LI #include <dbus/dbus.h>
41681ed54cSXin LI 
42681ed54cSXin LI #include "pcap-int.h"
43681ed54cSXin LI #include "pcap-dbus.h"
44681ed54cSXin LI 
45681ed54cSXin LI /*
46681ed54cSXin LI  * Private data for capturing on D-Bus.
47681ed54cSXin LI  */
48681ed54cSXin LI struct pcap_dbus {
49681ed54cSXin LI 	DBusConnection *conn;
50681ed54cSXin LI 	u_int	packets_read;	/* count of packets read */
51681ed54cSXin LI };
52681ed54cSXin LI 
53681ed54cSXin LI static int
dbus_read(pcap_t * handle,int max_packets _U_,pcap_handler callback,u_char * user)54b00ab754SHans Petter Selasky dbus_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
55681ed54cSXin LI {
56681ed54cSXin LI 	struct pcap_dbus *handlep = handle->priv;
57681ed54cSXin LI 
58681ed54cSXin LI 	struct pcap_pkthdr pkth;
59681ed54cSXin LI 	DBusMessage *message;
60681ed54cSXin LI 
61681ed54cSXin LI 	char *raw_msg;
62681ed54cSXin LI 	int raw_msg_len;
63681ed54cSXin LI 
64681ed54cSXin LI 	int count = 0;
65681ed54cSXin LI 
66681ed54cSXin LI 	message = dbus_connection_pop_message(handlep->conn);
67681ed54cSXin LI 
68681ed54cSXin LI 	while (!message) {
69ada6f083SXin LI 		/* XXX handle->opt.timeout = timeout_ms; */
70681ed54cSXin LI 		if (!dbus_connection_read_write(handlep->conn, 100)) {
71*6f9cba8fSJoseph Mingrone 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Connection closed");
72681ed54cSXin LI 			return -1;
73681ed54cSXin LI 		}
74681ed54cSXin LI 
75681ed54cSXin LI 		if (handle->break_loop) {
76681ed54cSXin LI 			handle->break_loop = 0;
77681ed54cSXin LI 			return -2;
78681ed54cSXin LI 		}
79681ed54cSXin LI 
80681ed54cSXin LI 		message = dbus_connection_pop_message(handlep->conn);
81681ed54cSXin LI 	}
82681ed54cSXin LI 
83681ed54cSXin LI 	if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
84*6f9cba8fSJoseph Mingrone 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Disconnected");
85681ed54cSXin LI 		return -1;
86681ed54cSXin LI 	}
87681ed54cSXin LI 
88681ed54cSXin LI 	if (dbus_message_marshal(message, &raw_msg, &raw_msg_len)) {
89681ed54cSXin LI 		pkth.caplen = pkth.len = raw_msg_len;
90681ed54cSXin LI 		/* pkth.caplen = min (payload_len, handle->snapshot); */
91681ed54cSXin LI 
92681ed54cSXin LI 		gettimeofday(&pkth.ts, NULL);
93681ed54cSXin LI 		if (handle->fcode.bf_insns == NULL ||
94*6f9cba8fSJoseph Mingrone 		    pcap_filter(handle->fcode.bf_insns, (u_char *)raw_msg, pkth.len, pkth.caplen)) {
95681ed54cSXin LI 			handlep->packets_read++;
96681ed54cSXin LI 			callback(user, &pkth, (u_char *)raw_msg);
97681ed54cSXin LI 			count++;
98681ed54cSXin LI 		}
99681ed54cSXin LI 
100681ed54cSXin LI 		dbus_free(raw_msg);
101681ed54cSXin LI 	}
102681ed54cSXin LI 	return count;
103681ed54cSXin LI }
104681ed54cSXin LI 
105681ed54cSXin LI static int
dbus_write(pcap_t * handle,const void * buf,int size)106*6f9cba8fSJoseph Mingrone dbus_write(pcap_t *handle, const void *buf, int size)
107681ed54cSXin LI {
108681ed54cSXin LI 	/* XXX, not tested */
109681ed54cSXin LI 	struct pcap_dbus *handlep = handle->priv;
110681ed54cSXin LI 
111681ed54cSXin LI 	DBusError error = DBUS_ERROR_INIT;
112681ed54cSXin LI 	DBusMessage *msg;
113681ed54cSXin LI 
114681ed54cSXin LI 	if (!(msg = dbus_message_demarshal(buf, size, &error))) {
115*6f9cba8fSJoseph Mingrone 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dbus_message_demarshal() failed: %s", error.message);
116681ed54cSXin LI 		dbus_error_free(&error);
117681ed54cSXin LI 		return -1;
118681ed54cSXin LI 	}
119681ed54cSXin LI 
120681ed54cSXin LI 	dbus_connection_send(handlep->conn, msg, NULL);
121681ed54cSXin LI 	dbus_connection_flush(handlep->conn);
122681ed54cSXin LI 
123681ed54cSXin LI 	dbus_message_unref(msg);
124681ed54cSXin LI 	return 0;
125681ed54cSXin LI }
126681ed54cSXin LI 
127681ed54cSXin LI static int
dbus_stats(pcap_t * handle,struct pcap_stat * stats)128681ed54cSXin LI dbus_stats(pcap_t *handle, struct pcap_stat *stats)
129681ed54cSXin LI {
130681ed54cSXin LI 	struct pcap_dbus *handlep = handle->priv;
131681ed54cSXin LI 
132681ed54cSXin LI 	stats->ps_recv = handlep->packets_read;
133681ed54cSXin LI 	stats->ps_drop = 0;
134681ed54cSXin LI 	stats->ps_ifdrop = 0;
135681ed54cSXin LI 	return 0;
136681ed54cSXin LI }
137681ed54cSXin LI 
138681ed54cSXin LI static void
dbus_cleanup(pcap_t * handle)139681ed54cSXin LI dbus_cleanup(pcap_t *handle)
140681ed54cSXin LI {
141681ed54cSXin LI 	struct pcap_dbus *handlep = handle->priv;
142681ed54cSXin LI 
143681ed54cSXin LI 	dbus_connection_unref(handlep->conn);
144681ed54cSXin LI 
145681ed54cSXin LI 	pcap_cleanup_live_common(handle);
146681ed54cSXin LI }
147681ed54cSXin LI 
148b00ab754SHans Petter Selasky /*
149b00ab754SHans Petter Selasky  * We don't support non-blocking mode.  I'm not sure what we'd
150b00ab754SHans Petter Selasky  * do to support it and, given that we don't support select()/
151b00ab754SHans Petter Selasky  * poll()/epoll_wait()/kevent() etc., it probably doesn't
152b00ab754SHans Petter Selasky  * matter.
153b00ab754SHans Petter Selasky  */
154b00ab754SHans Petter Selasky static int
dbus_getnonblock(pcap_t * p)155b00ab754SHans Petter Selasky dbus_getnonblock(pcap_t *p)
156b00ab754SHans Petter Selasky {
157*6f9cba8fSJoseph Mingrone 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
158b00ab754SHans Petter Selasky 	    "Non-blocking mode isn't supported for capturing on D-Bus");
159b00ab754SHans Petter Selasky 	return (-1);
160b00ab754SHans Petter Selasky }
161b00ab754SHans Petter Selasky 
162b00ab754SHans Petter Selasky static int
dbus_setnonblock(pcap_t * p,int nonblock _U_)163b00ab754SHans Petter Selasky dbus_setnonblock(pcap_t *p, int nonblock _U_)
164b00ab754SHans Petter Selasky {
165*6f9cba8fSJoseph Mingrone 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
166b00ab754SHans Petter Selasky 	    "Non-blocking mode isn't supported for capturing on D-Bus");
167b00ab754SHans Petter Selasky 	return (-1);
168b00ab754SHans Petter Selasky }
169b00ab754SHans Petter Selasky 
170681ed54cSXin LI static int
dbus_activate(pcap_t * handle)171681ed54cSXin LI dbus_activate(pcap_t *handle)
172681ed54cSXin LI {
173681ed54cSXin LI #define EAVESDROPPING_RULE "eavesdrop=true,"
174681ed54cSXin LI 
175681ed54cSXin LI 	static const char *rules[] = {
176681ed54cSXin LI 		EAVESDROPPING_RULE "type='signal'",
177681ed54cSXin LI 		EAVESDROPPING_RULE "type='method_call'",
178681ed54cSXin LI 		EAVESDROPPING_RULE "type='method_return'",
179681ed54cSXin LI 		EAVESDROPPING_RULE "type='error'",
180681ed54cSXin LI 	};
181681ed54cSXin LI 
182681ed54cSXin LI 	#define N_RULES sizeof(rules)/sizeof(rules[0])
183681ed54cSXin LI 
184681ed54cSXin LI 	struct pcap_dbus *handlep = handle->priv;
185ada6f083SXin LI 	const char *dev = handle->opt.device;
186681ed54cSXin LI 
187681ed54cSXin LI 	DBusError error = DBUS_ERROR_INIT;
188ada6f083SXin LI 	u_int i;
189681ed54cSXin LI 
190681ed54cSXin LI 	if (strcmp(dev, "dbus-system") == 0) {
191681ed54cSXin LI 		if (!(handlep->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
192*6f9cba8fSJoseph Mingrone 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get system bus: %s", error.message);
193681ed54cSXin LI 			dbus_error_free(&error);
194681ed54cSXin LI 			return PCAP_ERROR;
195681ed54cSXin LI 		}
196681ed54cSXin LI 
197681ed54cSXin LI 	} else if (strcmp(dev, "dbus-session") == 0) {
198681ed54cSXin LI 		if (!(handlep->conn = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
199*6f9cba8fSJoseph Mingrone 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to get session bus: %s", error.message);
200681ed54cSXin LI 			dbus_error_free(&error);
201681ed54cSXin LI 			return PCAP_ERROR;
202681ed54cSXin LI 		}
203681ed54cSXin LI 
204681ed54cSXin LI 	} else if (strncmp(dev, "dbus://", 7) == 0) {
205681ed54cSXin LI 		const char *addr = dev + 7;
206681ed54cSXin LI 
207681ed54cSXin LI 		if (!(handlep->conn = dbus_connection_open(addr, &error))) {
208*6f9cba8fSJoseph Mingrone 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to open connection to: %s: %s", addr, error.message);
209681ed54cSXin LI 			dbus_error_free(&error);
210681ed54cSXin LI 			return PCAP_ERROR;
211681ed54cSXin LI 		}
212681ed54cSXin LI 
213681ed54cSXin LI 		if (!dbus_bus_register(handlep->conn, &error)) {
214*6f9cba8fSJoseph Mingrone 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to register bus %s: %s\n", addr, error.message);
215681ed54cSXin LI 			dbus_error_free(&error);
216681ed54cSXin LI 			return PCAP_ERROR;
217681ed54cSXin LI 		}
218681ed54cSXin LI 
219681ed54cSXin LI 	} else {
220*6f9cba8fSJoseph Mingrone 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get bus address from %s", handle->opt.device);
221681ed54cSXin LI 		return PCAP_ERROR;
222681ed54cSXin LI 	}
223681ed54cSXin LI 
224681ed54cSXin LI 	/* Initialize some components of the pcap structure. */
225681ed54cSXin LI 	handle->bufsize = 0;
226681ed54cSXin LI 	handle->offset = 0;
227681ed54cSXin LI 	handle->linktype = DLT_DBUS;
228681ed54cSXin LI 	handle->read_op = dbus_read;
229681ed54cSXin LI 	handle->inject_op = dbus_write;
230681ed54cSXin LI 	handle->setfilter_op = install_bpf_program; /* XXX, later add support for dbus_bus_add_match() */
231681ed54cSXin LI 	handle->setdirection_op = NULL;
232681ed54cSXin LI 	handle->set_datalink_op = NULL;      /* can't change data link type */
233b00ab754SHans Petter Selasky 	handle->getnonblock_op = dbus_getnonblock;
234b00ab754SHans Petter Selasky 	handle->setnonblock_op = dbus_setnonblock;
235681ed54cSXin LI 	handle->stats_op = dbus_stats;
236b00ab754SHans Petter Selasky 	handle->cleanup_op = dbus_cleanup;
237681ed54cSXin LI 
238b00ab754SHans Petter Selasky #ifndef _WIN32
239b00ab754SHans Petter Selasky 	/*
240b00ab754SHans Petter Selasky 	 * Unfortunately, trying to do a select()/poll()/epoll_wait()/
241b00ab754SHans Petter Selasky 	 * kevent()/etc. on a D-Bus connection isn't a simple
242b00ab754SHans Petter Selasky 	 * case of "give me an FD on which to wait".
243b00ab754SHans Petter Selasky 	 *
244b00ab754SHans Petter Selasky 	 * Apparently, you have to register "add watch", "remove watch",
245b00ab754SHans Petter Selasky 	 * and "toggle watch" functions with
246b00ab754SHans Petter Selasky 	 * dbus_connection_set_watch_functions(),
247b00ab754SHans Petter Selasky 	 * keep a *set* of FDs, add to that set in the "add watch"
248b00ab754SHans Petter Selasky 	 * function, subtract from it in the "remove watch" function,
249b00ab754SHans Petter Selasky 	 * and either add to or subtract from that set in the "toggle
250b00ab754SHans Petter Selasky 	 * watch" function, and do the wait on *all* of the FDs in the
251b00ab754SHans Petter Selasky 	 * set.  (Yes, you need the "toggle watch" function, so that
252b00ab754SHans Petter Selasky 	 * the main loop doesn't itself need to check for whether
253b00ab754SHans Petter Selasky 	 * a given watch is enabled or disabled - most libpcap programs
254b00ab754SHans Petter Selasky 	 * know nothing about D-Bus and shouldn't *have* to know anything
255b00ab754SHans Petter Selasky 	 * about D-Bus other than how to decode D-Bus messages.)
256b00ab754SHans Petter Selasky 	 *
257b00ab754SHans Petter Selasky 	 * Implementing that would require considerable changes in
258b00ab754SHans Petter Selasky 	 * the way libpcap exports "selectable FDs" to its client.
259b00ab754SHans Petter Selasky 	 * Until that's done, we just say "you can't do that".
260b00ab754SHans Petter Selasky 	 */
261681ed54cSXin LI 	handle->selectable_fd = handle->fd = -1;
262b00ab754SHans Petter Selasky #endif
263681ed54cSXin LI 
264681ed54cSXin LI 	if (handle->opt.rfmon) {
265681ed54cSXin LI 		/*
266681ed54cSXin LI 		 * Monitor mode doesn't apply to dbus connections.
267681ed54cSXin LI 		 */
268681ed54cSXin LI 		dbus_cleanup(handle);
269681ed54cSXin LI 		return PCAP_ERROR_RFMON_NOTSUP;
270681ed54cSXin LI 	}
271681ed54cSXin LI 
272b00ab754SHans Petter Selasky 	/*
273b00ab754SHans Petter Selasky 	 * Turn a negative snapshot value (invalid), a snapshot value of
274b00ab754SHans Petter Selasky 	 * 0 (unspecified), or a value bigger than the normal maximum
275b00ab754SHans Petter Selasky 	 * value, into the maximum message length for D-Bus (128MB).
276b00ab754SHans Petter Selasky 	 */
277b00ab754SHans Petter Selasky 	if (handle->snapshot <= 0 || handle->snapshot > 134217728)
278b00ab754SHans Petter Selasky 		handle->snapshot = 134217728;
279b00ab754SHans Petter Selasky 
280681ed54cSXin LI 	/* dbus_connection_set_max_message_size(handlep->conn, handle->snapshot); */
281681ed54cSXin LI 	if (handle->opt.buffer_size != 0)
282681ed54cSXin LI 		dbus_connection_set_max_received_size(handlep->conn, handle->opt.buffer_size);
283681ed54cSXin LI 
284681ed54cSXin LI 	for (i = 0; i < N_RULES; i++) {
285681ed54cSXin LI 		dbus_bus_add_match(handlep->conn, rules[i], &error);
286681ed54cSXin LI 		if (dbus_error_is_set(&error)) {
287681ed54cSXin LI 			dbus_error_free(&error);
288681ed54cSXin LI 
289681ed54cSXin LI 			/* try without eavesdrop */
290681ed54cSXin LI 			dbus_bus_add_match(handlep->conn, rules[i] + strlen(EAVESDROPPING_RULE), &error);
291681ed54cSXin LI 			if (dbus_error_is_set(&error)) {
292*6f9cba8fSJoseph Mingrone 				snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Failed to add bus match: %s\n", error.message);
293681ed54cSXin LI 				dbus_error_free(&error);
294681ed54cSXin LI 				dbus_cleanup(handle);
295681ed54cSXin LI 				return PCAP_ERROR;
296681ed54cSXin LI 			}
297681ed54cSXin LI 		}
298681ed54cSXin LI 	}
299681ed54cSXin LI 
300681ed54cSXin LI 	return 0;
301681ed54cSXin LI }
302681ed54cSXin LI 
303681ed54cSXin LI pcap_t *
dbus_create(const char * device,char * ebuf,int * is_ours)304681ed54cSXin LI dbus_create(const char *device, char *ebuf, int *is_ours)
305681ed54cSXin LI {
306681ed54cSXin LI 	pcap_t *p;
307681ed54cSXin LI 
308681ed54cSXin LI 	if (strcmp(device, "dbus-system") &&
309681ed54cSXin LI 		strcmp(device, "dbus-session") &&
310681ed54cSXin LI 		strncmp(device, "dbus://", 7))
311681ed54cSXin LI 	{
312681ed54cSXin LI 		*is_ours = 0;
313681ed54cSXin LI 		return NULL;
314681ed54cSXin LI 	}
315681ed54cSXin LI 
316681ed54cSXin LI 	*is_ours = 1;
317*6f9cba8fSJoseph Mingrone 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_dbus);
318681ed54cSXin LI 	if (p == NULL)
319681ed54cSXin LI 		return (NULL);
320681ed54cSXin LI 
321681ed54cSXin LI 	p->activate_op = dbus_activate;
322b00ab754SHans Petter Selasky 	/*
323b00ab754SHans Petter Selasky 	 * Set these up front, so that, even if our client tries
324b00ab754SHans Petter Selasky 	 * to set non-blocking mode before we're activated, or
325b00ab754SHans Petter Selasky 	 * query the state of non-blocking mode, they get an error,
326b00ab754SHans Petter Selasky 	 * rather than having the non-blocking mode option set
327b00ab754SHans Petter Selasky 	 * for use later.
328b00ab754SHans Petter Selasky 	 */
329b00ab754SHans Petter Selasky 	p->getnonblock_op = dbus_getnonblock;
330b00ab754SHans Petter Selasky 	p->setnonblock_op = dbus_setnonblock;
331681ed54cSXin LI 	return (p);
332681ed54cSXin LI }
333681ed54cSXin LI 
334681ed54cSXin LI int
dbus_findalldevs(pcap_if_list_t * devlistp,char * err_str)335b00ab754SHans Petter Selasky dbus_findalldevs(pcap_if_list_t *devlistp, char *err_str)
336681ed54cSXin LI {
337b00ab754SHans Petter Selasky 	/*
338b00ab754SHans Petter Selasky 	 * The notion of "connected" vs. "disconnected" doesn't apply.
339b00ab754SHans Petter Selasky 	 * XXX - what about the notions of "up" and "running"?
340b00ab754SHans Petter Selasky 	 */
341b00ab754SHans Petter Selasky 	if (add_dev(devlistp, "dbus-system",
342b00ab754SHans Petter Selasky 	    PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, "D-Bus system bus",
343b00ab754SHans Petter Selasky 	    err_str) == NULL)
344681ed54cSXin LI 		return -1;
345b00ab754SHans Petter Selasky 	if (add_dev(devlistp, "dbus-session",
346b00ab754SHans Petter Selasky 	    PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, "D-Bus session bus",
347b00ab754SHans Petter Selasky 	    err_str) == NULL)
348681ed54cSXin LI 		return -1;
349681ed54cSXin LI 	return 0;
350681ed54cSXin LI }
351681ed54cSXin LI 
352