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