-
Notifications
You must be signed in to change notification settings - Fork 716
/
isotpsniffer.c
407 lines (348 loc) · 10.1 KB
/
isotpsniffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
/*
* isotpsniffer.c - dump ISO15765-2 datagrams using PF_CAN isotp protocol
*
* Copyright (c) 2008 Volkswagen Group Electronic Research
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Volkswagen nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* Alternatively, provided that this notice is retained in full, this
* software may be distributed under the terms of the GNU General
* Public License ("GPL") version 2, in which case the provisions of the
* GPL apply INSTEAD OF those given above.
*
* The provided data structures and external interfaces from this code
* are not restricted to be used by modules with a GPL compatible license.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* Send feedback to <linux-can@vger.kernel.org>
*
*/
#include <ctype.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "terminal.h"
#include <linux/can.h>
#include <linux/can/isotp.h>
#include <linux/sockios.h>
#define NO_CAN_ID 0xFFFFFFFFU
#define FORMAT_HEX 1
#define FORMAT_ASCII 2
#define FORMAT_DEFAULT (FORMAT_ASCII | FORMAT_HEX)
void print_usage(char *prg)
{
fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
fprintf(stderr, " -x <addr> (extended addressing mode)\n");
fprintf(stderr, " -X <addr> (extended addressing mode - rx addr)\n");
fprintf(stderr, " -c (color mode)\n");
fprintf(stderr, " -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
fprintf(stderr, " -f <format> (1 = HEX, 2 = ASCII, 3 = HEX & ASCII - default: %d)\n", FORMAT_DEFAULT);
fprintf(stderr, " -L (set link layer options for CAN FD)\n");
fprintf(stderr, " -h <len> (head: print only first <len> bytes)\n");
fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
fprintf(stderr, "\n");
}
void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
int format, struct timeval *tv, struct timeval *last_tv,
canid_t src, int socket, char *candevice, int head)
{
int i;
if (color == 1)
printf("%s", FGRED);
if (color == 2)
printf("%s", FGBLUE);
if (timestamp) {
ioctl(socket, SIOCGSTAMP, tv);
switch (timestamp) {
case 'a': /* absolute with timestamp */
printf("(%llu.%06llu) ", (unsigned long long)tv->tv_sec, (unsigned long long)tv->tv_usec);
break;
case 'A': /* absolute with date */
{
struct tm tm;
char timestring[25];
tm = *localtime(&tv->tv_sec);
strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
printf("(%s.%06llu) ", timestring, (unsigned long long)tv->tv_usec);
}
break;
case 'd': /* delta */
case 'z': /* starting with zero */
{
struct timeval diff;
if (last_tv->tv_sec == 0) /* first init */
*last_tv = *tv;
diff.tv_sec = tv->tv_sec - last_tv->tv_sec;
diff.tv_usec = tv->tv_usec - last_tv->tv_usec;
if (diff.tv_usec < 0)
diff.tv_sec--, diff.tv_usec += 1000000;
if (diff.tv_sec < 0)
diff.tv_sec = diff.tv_usec = 0;
printf("(%llu.%06llu) ", (unsigned long long)diff.tv_sec, (unsigned long long)diff.tv_usec);
if (timestamp == 'd')
*last_tv = *tv; /* update for delta calculation */
}
break;
default: /* no timestamp output */
break;
}
}
/* the source socket gets pdu data from the destination id */
printf(" %s %03X [%d] ", candevice, src & CAN_EFF_MASK, nbytes);
if (format & FORMAT_HEX) {
for (i=0; i<nbytes; i++) {
printf("%02X ", buffer[i]);
if (head && i+1 >= head) {
printf("... ");
break;
}
}
if (format & FORMAT_ASCII)
printf(" - ");
}
if (format & FORMAT_ASCII) {
printf("'");
for (i=0; i<nbytes; i++) {
if (isprint(buffer[i]))
printf("%c", buffer[i]);
else
printf(".");
if (head && i+1 >= head)
break;
}
printf("'");
if (head && i+1 >= head)
printf(" ... ");
}
if (color)
printf("%s", ATTRESET);
printf("\n");
fflush(stdout);
}
int main(int argc, char **argv)
{
fd_set rdfs;
int s = -1, t = -1;
struct sockaddr_can addr;
char if_name[IFNAMSIZ];
static struct can_isotp_options opts;
static struct can_isotp_ll_options llopts;
int r = 0;
int opt, quit = 0;
int color = 0;
int head = 0;
int timestamp = 0;
int format = FORMAT_DEFAULT;
canid_t src = NO_CAN_ID;
canid_t dst = NO_CAN_ID;
extern int optind, opterr, optopt;
static struct timeval tv, last_tv;
unsigned char buffer[4096];
int nbytes;
while ((opt = getopt(argc, argv, "s:d:x:X:h:ct:f:L?")) != -1) {
switch (opt) {
case 's':
src = strtoul(optarg, NULL, 16);
if (strlen(optarg) > 7)
src |= CAN_EFF_FLAG;
break;
case 'd':
dst = strtoul(optarg, NULL, 16);
if (strlen(optarg) > 7)
dst |= CAN_EFF_FLAG;
break;
case 'x':
opts.flags |= CAN_ISOTP_EXTEND_ADDR;
opts.ext_address = strtoul(optarg, NULL, 16) & 0xFF;
break;
case 'X':
opts.flags |= CAN_ISOTP_RX_EXT_ADDR;
opts.rx_ext_address = strtoul(optarg, NULL, 16) & 0xFF;
break;
case 'f':
format = (atoi(optarg) & (FORMAT_ASCII | FORMAT_HEX));
break;
case 'L':
llopts.mtu = CANFD_MTU;
llopts.tx_dl = CANFD_MAX_DLEN;
llopts.tx_flags = CANFD_BRS;
break;
case 'h':
head = atoi(optarg);
break;
case 'c':
color = 1;
break;
case 't':
timestamp = optarg[0];
if ((timestamp != 'a') && (timestamp != 'A') &&
(timestamp != 'd') && (timestamp != 'z')) {
printf("%s: unknown timestamp mode '%c' - ignored\n",
basename(argv[0]), optarg[0]);
timestamp = 0;
}
break;
case '?':
print_usage(basename(argv[0]));
goto out;
default:
fprintf(stderr, "Unknown option %c\n", opt);
print_usage(basename(argv[0]));
goto out;
}
}
if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
print_usage(basename(argv[0]));
r = 1;
goto out;
}
if ((opts.flags & CAN_ISOTP_RX_EXT_ADDR) && (!(opts.flags & CAN_ISOTP_EXTEND_ADDR))) {
print_usage(basename(argv[0]));
r = 1;
goto out;
}
if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
perror("socket");
r = 1;
goto out;
}
if ((t = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
perror("socket");
r = 1;
goto out;
}
opts.flags |= CAN_ISOTP_LISTEN_MODE;
strncpy(if_name, argv[optind], IFNAMSIZ - 1);
if_name[IFNAMSIZ - 1] = '\0';
addr.can_family = AF_CAN;
addr.can_ifindex = if_nametoindex(if_name);
if (!addr.can_ifindex) {
perror("if_nametoindex");
r = 1;
goto out;
}
if (setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts)) < 0) {
perror("setsockopt");
r = 1;
goto out;
}
if ((llopts.mtu) && (setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts))) < 0) {
perror("setsockopt");
r = 1;
goto out;
}
addr.can_addr.tp.tx_id = src;
addr.can_addr.tp.rx_id = dst;
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
r = 1;
goto out;
}
if (opts.flags & CAN_ISOTP_RX_EXT_ADDR) {
/* flip extended address info due to separate rx ext addr */
__u8 tmpext;
tmpext = opts.ext_address;
opts.ext_address = opts.rx_ext_address;
opts.rx_ext_address = tmpext;
}
if (setsockopt(t, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts)) < 0) {
perror("setsockopt");
r = 1;
goto out;
}
if ((llopts.mtu) && (setsockopt(t, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts))) < 0) {
perror("setsockopt");
r = 1;
goto out;
}
addr.can_addr.tp.tx_id = dst;
addr.can_addr.tp.rx_id = src;
if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
r = 1;
goto out;
}
while (!quit) {
FD_ZERO(&rdfs);
FD_SET(s, &rdfs);
FD_SET(t, &rdfs);
FD_SET(0, &rdfs);
if ((nbytes = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
perror("select");
continue;
}
if (FD_ISSET(0, &rdfs)) {
getchar();
quit = 1;
printf("quit due to keyboard input.\n");
}
if (FD_ISSET(s, &rdfs)) {
nbytes = read(s, buffer, 4096);
if (nbytes < 0) {
perror("read socket s");
r = 1;
goto out;
}
if (nbytes > 4095) {
r = 1;
goto out;
}
printbuf(buffer, nbytes, color?2:0, timestamp, format,
&tv, &last_tv, dst, s, if_name, head);
}
if (FD_ISSET(t, &rdfs)) {
nbytes = read(t, buffer, 4096);
if (nbytes < 0) {
perror("read socket t");
r = 1;
goto out;
}
if (nbytes > 4095) {
r = 1;
goto out;
}
printbuf(buffer, nbytes, color?1:0, timestamp, format,
&tv, &last_tv, src, t, if_name, head);
}
}
out:
if (s != -1)
close(s);
if (t != -1)
close(t);
return r;
}