pcap-can-linux.c revision 256281
150477Speter/*
212029Sache * Copyright (c) 2009 Felix Obenhuber
312029Sache * All rights reserved.
412029Sache *
512029Sache * Redistribution and use in source and binary forms, with or without
6118459Smtm * modification, are permitted provided that the following conditions
7118459Smtm * are met:
886072Sache *
988473Sphantom * 1. Redistributions of source code must retain the above copyright
10117259Sache * notice, this list of conditions and the following disclaimer.
1188473Sphantom * 2. Redistributions in binary form must reproduce the above copyright
1277977Sache * notice, this list of conditions and the following disclaimer in the
13118652Sache * documentation and/or other materials provided with the distribution.
1477977Sache * 3. The name of the author may not be used to endorse or promote
1577977Sache * products derived from this software without specific prior written
1688473Sphantom * permission.
1777977Sache *
1877977Sache * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1988473Sphantom * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20105965Sache * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2170484Sphantom * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2277977Sache * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2370484Sphantom * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2452388Sache * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2577977Sache * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26118174Sache * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27115628Sache * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2847831Sfoxfair * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2923222Swosch *
3012029Sache * SocketCan sniffing API implementation for Linux platform
3112029Sache * By Felix Obenhuber <felix@obenhuber.de>
3288473Sphantom *
33108428Sache */
34108428Sache
35108428Sache#ifdef HAVE_CONFIG_H
3693885Sphantom#include "config.h"
3793885Sphantom#endif
38105445Sache
3988473Sphantom#include "pcap-int.h"
40105445Sache#include "pcap-can-linux.h"
41115912Sache
4278002Sache#ifdef NEED_STRERROR_H
4312029Sache#include "strerror.h"
4412029Sache#endif
4512029Sache
4637481Sbde#include <errno.h>
4712029Sache#include <stdlib.h>
4812029Sache#include <unistd.h>
4912029Sache#include <fcntl.h>
5012029Sache#include <string.h>
5123224Sadam#include <sys/ioctl.h>
52100872Sru#include <sys/socket.h>
5323224Sadam#include <net/if.h>
5423222Swosch#include <arpa/inet.h>
5523222Swosch
5677977Sache#include <linux/can.h>
5777988Sache#include <linux/can/raw.h>
5823222Swosch
5974398Sache/* not yet defined anywhere */
6077977Sache#ifndef PF_CAN
6177988Sache#define PF_CAN 29
6241760Sdillon#endif
6323222Swosch#ifndef AF_CAN
6477977Sache#define AF_CAN PF_CAN
6577988Sache#endif
6623222Swosch
6755075Sache/* forward declaration */
6877977Sachestatic int can_activate(pcap_t *);
6977988Sachestatic int can_read_linux(pcap_t *, int , pcap_handler , u_char *);
7055075Sachestatic int can_inject_linux(pcap_t *, const void *, size_t);
7188314Sachestatic int can_setfilter_linux(pcap_t *, struct bpf_program *);
7288314Sachestatic int can_setdirection_linux(pcap_t *, pcap_direction_t);
7388314Sachestatic int can_stats_linux(pcap_t *, struct pcap_stat *);
7488314Sache
75115912Sacheint
76115912Sachecan_findalldevs(pcap_if_t **devlistp, char *errbuf)
77115912Sache{
78115912Sache	/*
7978002Sache	 * There are no platform-specific devices since each device
8077977Sache	 * exists as a regular network interface.
8178002Sache	 *
8278002Sache	 * XXX - true?
8393889Sphantom	 */
8493889Sphantom	return 0;
8512029Sache}
8612029Sache
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