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 * Private data for capturing on Linux CANbus devices.
79 int ifindex; /* interface index of device we're bound to */
83 can_findalldevs(pcap_if_t **devlistp, char *errbuf)
86 * There are no platform-specific devices since each device
87 * exists as a regular network interface.
95 can_create(const char *device, char *ebuf, int *is_ours)
102 /* Does this look like a CANbus device? */
103 cp = strrchr(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" */
114 /* Nope, doesn't begin with "can" or "vcan" */
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. */
126 /* Followed by a non-valid number. */
131 /* OK, it's probably ours. */
134 p = pcap_create_common(device, ebuf, sizeof (struct pcap_can));
138 p->activate_op = can_activate;
144 can_activate(pcap_t* handle)
146 struct pcap_can *handlep = handle->priv;
147 struct sockaddr_can addr;
150 /* Initialize some components of the pcap structure. */
151 handle->bufsize = 24;
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;
164 handle->fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
167 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s",
168 errno, strerror(errno));
172 /* get interface index */
173 memset(&ifr, 0, sizeof(ifr));
174 strncpy(ifr.ifr_name, handle->opt.source, sizeof(ifr.ifr_name));
175 if (ioctl(handle->fd, SIOCGIFINDEX, &ifr) < 0)
177 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
178 "Unable to get interface index: %s",
179 pcap_strerror(errno));
180 pcap_cleanup_live_common(handle);
183 handlep->ifindex = ifr.ifr_ifindex;
185 /* allocate butter */
186 handle->buffer = malloc(handle->bufsize);
189 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s",
190 pcap_strerror(errno));
191 pcap_cleanup_live_common(handle);
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 )
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);
206 if (handle->opt.rfmon)
208 /* Monitor mode doesn't apply to CAN devices. */
209 pcap_cleanup_live_common(handle);
210 return PCAP_ERROR_RFMON_NOTSUP;
213 handle->selectable_fd = handle->fd;
220 can_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
223 struct pcap_pkthdr pkth;
225 struct can_frame* cf;
227 iv.iov_base = &handle->buffer[handle->offset];
228 iv.iov_len = handle->snapshot;
230 memset(&msg, 0, sizeof(msg));
233 msg.msg_control = handle->buffer;
234 msg.msg_controllen = handle->offset;
238 pkth.caplen = recvmsg(handle->fd, &msg, 0);
239 if (handle->break_loop)
241 handle->break_loop = 0;
244 } while ((pkth.caplen == -1) && (errno == EINTR));
246 if (pkth.caplen == -1)
248 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s",
249 errno, strerror(errno));
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;
258 cf->can_id = htonl( cf->can_id );
260 if( -1 == gettimeofday(&pkth.ts, NULL) )
262 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't get time of day %d:%s",
263 errno, strerror(errno));
267 callback(user, &pkth, &handle->buffer[8]);
274 can_inject_linux(pcap_t *handle, const void *buf, size_t size)
276 /* not yet implemented */
277 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
284 can_stats_linux(pcap_t *handle, struct pcap_stat *stats)
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 */
295 can_setfilter_linux(pcap_t *p, struct bpf_program *fp)
297 /* not yet implemented */
303 can_setdirection_linux(pcap_t *p, pcap_direction_t d)
305 /* no support for PCAP_D_OUT */
308 snprintf(p->errbuf, sizeof(p->errbuf),
309 "Setting direction to PCAP_D_OUT is not supported on can");