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