2 * Copyright (c) 2009 Felix Obenhuber
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
30 * SocketCan sniffing API implementation for Linux platform
31 * By Felix Obenhuber <felix@obenhuber.de>
40 #include "pcap-can-linux.h"
42 #ifdef NEED_STRERROR_H
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
54 #include <arpa/inet.h>
56 #include <linux/can.h>
57 #include <linux/can/raw.h>
59 /* not yet defined anywhere */
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 *);
76 can_findalldevs(pcap_if_t **devlistp, char *errbuf)
79 * There are no platform-specific devices since each device
80 * exists as a regular network interface.
88 can_create(const char *device, char *ebuf, int *is_ours)
95 /* Does this look like a CANbus device? */
96 cp = strrchr(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" */
107 /* Nope, doesn't begin with "can" or "vcan" */
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. */
119 /* Followed by a non-valid number. */
124 /* OK, it's probably ours. */
127 p = pcap_create_common(device, ebuf);
131 p->activate_op = can_activate;
137 can_activate(pcap_t* handle)
139 struct sockaddr_can addr;
142 /* Initialize some components of the pcap structure. */
143 handle->bufsize = 24;
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;
156 handle->fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
159 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s",
160 errno, strerror(errno));
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)
169 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
170 "Unable to get interface index: %s",
171 pcap_strerror(errno));
172 pcap_cleanup_live_common(handle);
175 handle->md.ifindex = ifr.ifr_ifindex;
177 /* allocate butter */
178 handle->buffer = malloc(handle->bufsize);
181 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
182 pcap_strerror(errno));
183 pcap_cleanup_live_common(handle);
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 )
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);
198 if (handle->opt.rfmon)
200 /* Monitor mode doesn't apply to CAN devices. */
201 pcap_cleanup_live_common(handle);
205 handle->selectable_fd = handle->fd;
212 can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
215 struct pcap_pkthdr pkth;
217 struct can_frame* cf;
219 iv.iov_base = &handle->buffer[handle->offset];
220 iv.iov_len = handle->snapshot;
222 memset(&msg, 0, sizeof(msg));
225 msg.msg_control = handle->buffer;
226 msg.msg_controllen = handle->offset;
230 pkth.caplen = recvmsg(handle->fd, &msg, 0);
231 if (handle->break_loop)
233 handle->break_loop = 0;
236 } while ((pkth.caplen == -1) && (errno == EINTR));
240 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s",
241 errno, strerror(errno));
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;
250 cf->can_id = htonl( cf->can_id );
252 if( -1 == gettimeofday(&pkth.ts, NULL) )
254 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get time of day %d:%s",
255 errno, strerror(errno));
259 callback(user, &pkth, &handle->buffer[8]);
266 can_inject_linux(pcap_t *handle, const void *buf, size_t size)
268 /* not yet implemented */
269 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
276 can_stats_linux(pcap_t *handle, struct pcap_stat *stats)
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 */
287 can_setfilter_linux(pcap_t *p, struct bpf_program *fp)
289 /* not yet implemented */
295 can_setdirection_linux(pcap_t *p, pcap_direction_t d)
297 /* no support for PCAP_D_OUT */
300 snprintf(p->errbuf, sizeof(p->errbuf),
301 "Setting direction to PCAP_D_OUT is not supported on can");