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 */
68 static int can_activate(pcap_t *);
69 static int can_read_linux(pcap_t *, int , pcap_handler , u_char *);
70 static int can_inject_linux(pcap_t *, const void *, size_t);
71 static int can_setfilter_linux(pcap_t *, struct bpf_program *);
72 static int can_setdirection_linux(pcap_t *, pcap_direction_t);
73 static int can_stats_linux(pcap_t *, struct pcap_stat *);
74 
75 /*
76  * Private data for capturing on Linux CANbus devices.
77  */
78 struct pcap_can {
79 	int ifindex;		/* interface index of device we're bound to */
80 };
81 
82 int
can_findalldevs(pcap_if_t ** devlistp,char * errbuf)83 can_findalldevs(pcap_if_t **devlistp, char *errbuf)
84 {
85 	/*
86 	 * There are no platform-specific devices since each device
87 	 * exists as a regular network interface.
88 	 *
89 	 * XXX - true?
90 	 */
91 	return 0;
92 }
93 
94 pcap_t *
can_create(const char * device,char * ebuf,int * is_ours)95 can_create(const char *device, char *ebuf, int *is_ours)
96 {
97 	const char *cp;
98 	char *cpend;
99 	long devnum;
100 	pcap_t* p;
101 
102 	/* Does this look like a CANbus device? */
103 	cp = strrchr(device, '/');
104 	if (cp == NULL)
105 		cp = device;
106 	/* Does it begin with "can" or "vcan"? */
107 	if (strncmp(cp, "can", 3) == 0) {
108 		/* Begins with "can" */
109 		cp += 3;	/* skip past "can" */
110 	} else if (strncmp(cp, "vcan", 4) == 0) {
111 		/* Begins with "vcan" */
112 		cp += 4;
113 	} else {
114 		/* Nope, doesn't begin with "can" or "vcan" */
115 		*is_ours = 0;
116 		return NULL;
117 	}
118 	/* Yes - is "can" or "vcan" followed by a number from 0? */
119 	devnum = strtol(cp, &cpend, 10);
120 	if (cpend == cp || *cpend != '\0') {
121 		/* Not followed by a number. */
122 		*is_ours = 0;
123 		return NULL;
124 	}
125 	if (devnum < 0) {
126 		/* Followed by a non-valid number. */
127 		*is_ours = 0;
128 		return NULL;
129 	}
130 
131 	/* OK, it's probably ours. */
132 	*is_ours = 1;
133 
134 	p = pcap_create_common(device, ebuf, sizeof (struct pcap_can));
135 	if (p == NULL)
136 		return (NULL);
137 
138 	p->activate_op = can_activate;
139 	return (p);
140 }
141 
142 
143 static int
can_activate(pcap_t * handle)144 can_activate(pcap_t* handle)
145 {
146 	struct pcap_can *handlep = handle->priv;
147 	struct sockaddr_can addr;
148 	struct ifreq ifr;
149 
150 	/* Initialize some components of the pcap structure. */
151 	handle->bufsize = 24;
152 	handle->offset = 8;
153 	handle->linktype = DLT_CAN_SOCKETCAN;
154 	handle->read_op = can_read_linux;
155 	handle->inject_op = can_inject_linux;
156 	handle->setfilter_op = can_setfilter_linux;
157 	handle->setdirection_op = can_setdirection_linux;
158 	handle->set_datalink_op = NULL;
159 	handle->getnonblock_op = pcap_getnonblock_fd;
160 	handle->setnonblock_op = pcap_setnonblock_fd;
161 	handle->stats_op = can_stats_linux;
162 
163 	/* Create socket */
164 	handle->fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
165 	if (handle->fd < 0)
166 	{
167 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s",
168 			errno, strerror(errno));
169 		return PCAP_ERROR;
170 	}
171 
172 	/* get interface index */
173 	memset(&ifr, 0, sizeof(ifr));
174 	strlcpy(ifr.ifr_name, handle->opt.source, sizeof(ifr.ifr_name));
175 	if (ioctl(handle->fd, SIOCGIFINDEX, &ifr) < 0)
176 	{
177 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
178 				"Unable to get interface index: %s",
179 			pcap_strerror(errno));
180 		pcap_cleanup_live_common(handle);
181 		return PCAP_ERROR;
182 	}
183 	handlep->ifindex = ifr.ifr_ifindex;
184 
185 	/* allocate butter */
186 	handle->buffer = malloc(handle->bufsize);
187 	if (!handle->buffer)
188 	{
189 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
190 			pcap_strerror(errno));
191 		pcap_cleanup_live_common(handle);
192 		return PCAP_ERROR;
193 	}
194 
195 	/* Bind to the socket */
196 	addr.can_family = AF_CAN;
197 	addr.can_ifindex = handlep->ifindex;
198 	if( bind( handle->fd, (struct sockaddr*)&addr, sizeof(addr) ) < 0  )
199 	{
200 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't attach to device %d %d:%s",
201 			handlep->ifindex, errno, strerror(errno));
202 		pcap_cleanup_live_common(handle);
203 		return PCAP_ERROR;
204 	}
205 
206 	if (handle->opt.rfmon)
207 	{
208 		/* Monitor mode doesn't apply to CAN devices. */
209 		pcap_cleanup_live_common(handle);
210 		return PCAP_ERROR_RFMON_NOTSUP;
211 	}
212 
213 	handle->selectable_fd = handle->fd;
214 	return 0;
215 
216 }
217 
218 
219 static int
can_read_linux(pcap_t * handle,int max_packets,pcap_handler callback,u_char * user)220 can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
221 {
222 	struct msghdr msg;
223 	struct pcap_pkthdr pkth;
224 	struct iovec iv;
225 	struct can_frame* cf;
226 
227 	iv.iov_base = &handle->buffer[handle->offset];
228 	iv.iov_len = handle->snapshot;
229 
230 	memset(&msg, 0, sizeof(msg));
231 	msg.msg_iov = &iv;
232 	msg.msg_iovlen = 1;
233 	msg.msg_control = handle->buffer;
234 	msg.msg_controllen = handle->offset;
235 
236 	do
237 	{
238 		pkth.caplen = recvmsg(handle->fd, &msg, 0);
239 		if (handle->break_loop)
240 		{
241 			handle->break_loop = 0;
242 			return -2;
243 		}
244 	} while ((pkth.caplen == -1) && (errno == EINTR));
245 
246 	if (pkth.caplen == -1)
247 	{
248 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s",
249 			errno, strerror(errno));
250 		return -1;
251 	}
252 
253 	/* adjust capture len according to frame len */
254 	cf = (struct can_frame*)&handle->buffer[8];
255 	pkth.caplen -= 8 - cf->can_dlc;
256 	pkth.len = pkth.caplen;
257 
258 	cf->can_id = htonl( cf->can_id );
259 
260 	if( -1 == gettimeofday(&pkth.ts, NULL) )
261 	{
262 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get time of day %d:%s",
263 			errno, strerror(errno));
264 		return -1;
265 	}
266 
267 	callback(user, &pkth, &handle->buffer[8]);
268 
269 	return 1;
270 }
271 
272 
273 static int
can_inject_linux(pcap_t * handle,const void * buf,size_t size)274 can_inject_linux(pcap_t *handle, const void *buf, size_t size)
275 {
276 	/* not yet implemented */
277 	snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
278 		"can devices");
279 	return (-1);
280 }
281 
282 
283 static int
can_stats_linux(pcap_t * handle,struct pcap_stat * stats)284 can_stats_linux(pcap_t *handle, struct pcap_stat *stats)
285 {
286 	/* not yet implemented */
287 	stats->ps_recv = 0;			 /* number of packets received */
288 	stats->ps_drop = 0;			 /* number of packets dropped */
289 	stats->ps_ifdrop = 0;		 /* drops by interface -- only supported on some platforms */
290 	return 0;
291 }
292 
293 
294 static int
can_setfilter_linux(pcap_t * p,struct bpf_program * fp)295 can_setfilter_linux(pcap_t *p, struct bpf_program *fp)
296 {
297 	/* not yet implemented */
298 	return 0;
299 }
300 
301 
302 static int
can_setdirection_linux(pcap_t * p,pcap_direction_t d)303 can_setdirection_linux(pcap_t *p, pcap_direction_t d)
304 {
305 	/* no support for PCAP_D_OUT */
306 	if (d == PCAP_D_OUT)
307 	{
308 		snprintf(p->errbuf, sizeof(p->errbuf),
309 			"Setting direction to PCAP_D_OUT is not supported on can");
310 		return -1;
311 	}
312 
313 	p->direction = d;
314 
315 	return 0;
316 }
317 
318 
319 /* eof */
320