1/*
2 * Copyright (c) 2009 Felix Obenhuber
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * SocketCan sniffing API implementation for Linux platform
31 * By Felix Obenhuber <felix@obenhuber.de>
32 *
33 */
34
35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
39#include "pcap-int.h"
40#include "pcap-can-linux.h"
41
42#ifdef NEED_STRERROR_H
43#include "strerror.h"
44#endif
45
46#include <errno.h>
47#include <stdlib.h>
48#include <unistd.h>
49#include <fcntl.h>
50#include <string.h>
51#include <sys/ioctl.h>
52#include <sys/socket.h>
53#include <net/if.h>
54#include <arpa/inet.h>
55
56#include <linux/can.h>
57#include <linux/can/raw.h>
58
59/* not yet defined anywhere */
60#ifndef PF_CAN
61#define PF_CAN 29
62#endif
63#ifndef AF_CAN
64#define AF_CAN PF_CAN
65#endif
66
67/* forward declaration */
68static int can_activate(pcap_t *);
69static int can_read_linux(pcap_t *, int , pcap_handler , u_char *);
70static int can_inject_linux(pcap_t *, const void *, size_t);
71static int can_setfilter_linux(pcap_t *, struct bpf_program *);
72static int can_setdirection_linux(pcap_t *, pcap_direction_t);
73static int can_stats_linux(pcap_t *, struct pcap_stat *);
74
75int
76can_findalldevs(pcap_if_t **devlistp, char *errbuf)
77{
78	/*
79	 * There are no platform-specific devices since each device
80	 * exists as a regular network interface.
81	 *
82	 * XXX - true?
83	 */
84	return 0;
85}
86
87pcap_t *
88can_create(const char *device, char *ebuf, int *is_ours)
89{
90	const char *cp;
91	char *cpend;
92	long devnum;
93	pcap_t* p;
94
95	/* Does this look like a CANbus device? */
96	cp = strrchr(device, '/');
97	if (cp == NULL)
98		cp = device;
99	/* Does it begin with "can" or "vcan"? */
100	if (strncmp(cp, "can", 3) == 0) {
101		/* Begins with "can" */
102		cp += 3;	/* skip past "can" */
103	} else if (strncmp(cp, "vcan", 4) == 0) {
104		/* Begins with "vcan" */
105		cp += 4;
106	} else {
107		/* Nope, doesn't begin with "can" or "vcan" */
108		*is_ours = 0;
109		return NULL;
110	}
111	/* Yes - is "can" or "vcan" followed by a number from 0? */
112	devnum = strtol(cp, &cpend, 10);
113	if (cpend == cp || *cpend != '\0') {
114		/* Not followed by a number. */
115		*is_ours = 0;
116		return NULL;
117	}
118	if (devnum < 0) {
119		/* Followed by a non-valid number. */
120		*is_ours = 0;
121		return NULL;
122	}
123
124	/* OK, it's probably ours. */
125	*is_ours = 1;
126
127	p = pcap_create_common(device, ebuf);
128	if (p == NULL)
129		return (NULL);
130
131	p->activate_op = can_activate;
132	return (p);
133}
134
135
136static int
137can_activate(pcap_t* handle)
138{
139	struct sockaddr_can addr;
140	struct ifreq ifr;
141
142	/* Initialize some components of the pcap structure. */
143	handle->bufsize = 24;
144	handle->offset = 8;
145	handle->linktype = DLT_CAN_SOCKETCAN;
146	handle->read_op = can_read_linux;
147	handle->inject_op = can_inject_linux;
148	handle->setfilter_op = can_setfilter_linux;
149	handle->setdirection_op = can_setdirection_linux;
150	handle->set_datalink_op = NULL;
151	handle->getnonblock_op = pcap_getnonblock_fd;
152	handle->setnonblock_op = pcap_setnonblock_fd;
153	handle->stats_op = can_stats_linux;
154
155	/* Create socket */
156	handle->fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
157	if (handle->fd < 0)
158	{
159		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s",
160			errno, strerror(errno));
161		return PCAP_ERROR;
162	}
163
164	/* get interface index */
165	memset(&ifr, 0, sizeof(ifr));
166	strncpy(ifr.ifr_name, handle->opt.source, sizeof(ifr.ifr_name));
167	if (ioctl(handle->fd, SIOCGIFINDEX, &ifr) < 0)
168	{
169		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
170				"Unable to get interface index: %s",
171			pcap_strerror(errno));
172		pcap_cleanup_live_common(handle);
173		return PCAP_ERROR;
174	}
175	handle->md.ifindex = ifr.ifr_ifindex;
176
177	/* allocate butter */
178	handle->buffer = malloc(handle->bufsize);
179	if (!handle->buffer)
180	{
181		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
182			pcap_strerror(errno));
183		pcap_cleanup_live_common(handle);
184		return PCAP_ERROR;
185	}
186
187	/* Bind to the socket */
188	addr.can_family = AF_CAN;
189	addr.can_ifindex = handle->md.ifindex;
190	if( bind( handle->fd, (struct sockaddr*)&addr, sizeof(addr) ) < 0  )
191	{
192		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't attach to device %d %d:%s",
193			handle->md.ifindex, errno, strerror(errno));
194		pcap_cleanup_live_common(handle);
195		return PCAP_ERROR;
196	}
197
198	if (handle->opt.rfmon)
199	{
200		/* Monitor mode doesn't apply to CAN devices. */
201		pcap_cleanup_live_common(handle);
202		return PCAP_ERROR;
203	}
204
205	handle->selectable_fd = handle->fd;
206	return 0;
207
208}
209
210
211static int
212can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
213{
214	struct msghdr msg;
215	struct pcap_pkthdr pkth;
216	struct iovec iv;
217	struct can_frame* cf;
218
219	iv.iov_base = &handle->buffer[handle->offset];
220	iv.iov_len = handle->snapshot;
221
222	memset(&msg, 0, sizeof(msg));
223	msg.msg_iov = &iv;
224	msg.msg_iovlen = 1;
225	msg.msg_control = handle->buffer;
226	msg.msg_controllen = handle->offset;
227
228	do
229	{
230		pkth.caplen = recvmsg(handle->fd, &msg, 0);
231		if (handle->break_loop)
232		{
233			handle->break_loop = 0;
234			return -2;
235		}
236	} while ((pkth.caplen == -1) && (errno == EINTR));
237
238	if (pkth.caplen < 0)
239	{
240		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s",
241			errno, strerror(errno));
242		return -1;
243	}
244
245	/* adjust capture len according to frame len */
246	cf = (struct can_frame*)&handle->buffer[8];
247	pkth.caplen -= 8 - cf->can_dlc;
248	pkth.len = pkth.caplen;
249
250	cf->can_id = htonl( cf->can_id );
251
252	if( -1 == gettimeofday(&pkth.ts, NULL) )
253	{
254		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get time of day %d:%s",
255			errno, strerror(errno));
256		return -1;
257	}
258
259	callback(user, &pkth, &handle->buffer[8]);
260
261	return 1;
262}
263
264
265static int
266can_inject_linux(pcap_t *handle, const void *buf, size_t size)
267{
268	/* not yet implemented */
269	snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
270		"can devices");
271	return (-1);
272}
273
274
275static int
276can_stats_linux(pcap_t *handle, struct pcap_stat *stats)
277{
278	/* not yet implemented */
279	stats->ps_recv = 0;			 /* number of packets received */
280	stats->ps_drop = 0;			 /* number of packets dropped */
281	stats->ps_ifdrop = 0;		 /* drops by interface -- only supported on some platforms */
282	return 0;
283}
284
285
286static int
287can_setfilter_linux(pcap_t *p, struct bpf_program *fp)
288{
289	/* not yet implemented */
290	return 0;
291}
292
293
294static int
295can_setdirection_linux(pcap_t *p, pcap_direction_t d)
296{
297	/* no support for PCAP_D_OUT */
298	if (d == PCAP_D_OUT)
299	{
300		snprintf(p->errbuf, sizeof(p->errbuf),
301			"Setting direction to PCAP_D_OUT is not supported on can");
302		return -1;
303	}
304
305	p->direction = d;
306
307	return 0;
308}
309
310
311/* eof */
312