2 * isotptun.c - IP over CAN ISO-TP (ISO15765-2) tunnel / proof-of-concept
4 * This program creates a Linux tunnel netdevice 'ctunX' and transfers the
5 * ethernet frames inside ISO15765-2 (unreliable) datagrams on CAN.
7 * Use e.g. "ifconfig ctun0 123.123.123.1 pointopoint 123.123.123.2 up"
8 * to create a point-to-point IP connection on CAN.
10 * Copyright (c) 2008 Volkswagen Group Electronic Research
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of Volkswagen nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * Alternatively, provided that this notice is retained in full, this
26 * software may be distributed under the terms of the GNU General
27 * Public License ("GPL") version 2, in which case the provisions of the
28 * GPL apply INSTEAD OF those given above.
30 * The provided data structures and external interfaces from this code
31 * are not restricted to be used by modules with a GPL compatible license.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
46 * Send feedback to <linux-can@vger.kernel.org>
60 #include <sys/types.h>
61 #include <sys/socket.h>
62 #include <sys/ioctl.h>
64 #include <linux/can.h>
65 #include <linux/can/isotp.h>
66 #include <linux/if_tun.h>
68 #define NO_CAN_ID 0xFFFFFFFFU
69 #define DEFAULT_NAME "ctun%d"
71 /* stay on 4095 bytes for the max. PDU length which is still much more than the standard ethernet MTU */
72 #define MAX_PDU_LENGTH 4095
73 #define BUF_LEN (MAX_PDU_LENGTH + 1)
75 static volatile int running = 1;
77 void print_usage(char *prg)
79 fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n\n", prg);
80 fprintf(stderr, "This program creates a Linux tunnel netdevice 'ctunX' and transfers the\n");
81 fprintf(stderr, "ethernet frames inside ISO15765-2 (unreliable) datagrams on CAN.\n\n");
82 fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
83 fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
84 fprintf(stderr, " -n <name> (name of created IP netdevice. Default: '%s')\n", DEFAULT_NAME);
85 fprintf(stderr, " -x <addr>[:<rxaddr>] (extended addressing / opt. separate rxaddr)\n");
86 fprintf(stderr, " -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n");
87 fprintf(stderr, " -p [tx]:[rx] (set and enable tx/rx padding bytes)\n");
88 fprintf(stderr, " -P <mode> (check rx padding for (l)ength (c)ontent (a)ll)\n");
89 fprintf(stderr, " -t <time ns> (transmit time in nanosecs)\n");
90 fprintf(stderr, " -b <bs> (blocksize. 0 = off)\n");
91 fprintf(stderr, " -m <val> (STmin in ms/ns. See spec.)\n");
92 fprintf(stderr, " -w <num> (max. wait frame transmissions.)\n");
93 fprintf(stderr, " -h (half duplex mode.)\n");
94 fprintf(stderr, " -v (verbose mode. Print symbols for tunneled msgs.)\n");
95 fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
96 fprintf(stderr, "Use e.g. 'ifconfig ctun0 123.123.123.1 pointopoint 123.123.123.2 up'\n");
97 fprintf(stderr, "to create a point-to-point IP connection on CAN.\n");
98 fprintf(stderr, "\n");
101 void sigterm(int signo)
106 int main(int argc, char **argv)
110 struct sockaddr_can addr;
112 static struct can_isotp_options opts;
113 static struct can_isotp_fc_options fcopts;
114 static struct can_isotp_ll_options llopts;
116 extern int optind, opterr, optopt;
118 unsigned char buffer[BUF_LEN];
119 static char name[IFNAMSIZ] = DEFAULT_NAME;
122 signal(SIGTERM, sigterm);
123 signal(SIGHUP, sigterm);
124 signal(SIGINT, sigterm);
126 addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
128 while ((opt = getopt(argc, argv, "s:d:n:x:p:P:t:b:m:whL:v?")) != -1) {
131 addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
132 if (strlen(optarg) > 7)
133 addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
137 addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
138 if (strlen(optarg) > 7)
139 addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
143 strncpy(name, optarg, IFNAMSIZ-1);
148 int elements = sscanf(optarg, "%hhx:%hhx",
150 &opts.rx_ext_address);
153 opts.flags |= CAN_ISOTP_EXTEND_ADDR;
154 else if (elements == 2)
155 opts.flags |= (CAN_ISOTP_EXTEND_ADDR | CAN_ISOTP_RX_EXT_ADDR);
157 printf("incorrect extended addr values '%s'.\n", optarg);
158 print_usage(basename(argv[0]));
166 int elements = sscanf(optarg, "%hhx:%hhx",
168 &opts.rxpad_content);
171 opts.flags |= CAN_ISOTP_TX_PADDING;
172 else if (elements == 2)
173 opts.flags |= (CAN_ISOTP_TX_PADDING | CAN_ISOTP_RX_PADDING);
174 else if (sscanf(optarg, ":%hhx", &opts.rxpad_content) == 1)
175 opts.flags |= CAN_ISOTP_RX_PADDING;
177 printf("incorrect padding values '%s'.\n", optarg);
178 print_usage(basename(argv[0]));
185 if (optarg[0] == 'l')
186 opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
187 else if (optarg[0] == 'c')
188 opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
189 else if (optarg[0] == 'a')
190 opts.flags |= (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA);
192 printf("unknown padding check option '%c'.\n", optarg[0]);
193 print_usage(basename(argv[0]));
199 opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
203 fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
207 fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
211 fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
215 opts.flags |= CAN_ISOTP_HALF_DUPLEX;
219 if (sscanf(optarg, "%hhu:%hhu:%hhu",
222 &llopts.tx_flags) != 3) {
223 printf("unknown link layer options '%s'.\n", optarg);
224 print_usage(basename(argv[0]));
234 print_usage(basename(argv[0]));
239 fprintf(stderr, "Unknown option %c\n", opt);
240 print_usage(basename(argv[0]));
246 if ((argc - optind != 1) ||
247 (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
248 (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
249 print_usage(basename(argv[0]));
253 if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
258 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
259 setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
262 if (setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts)) < 0) {
263 perror("link layer sockopt");
268 strncpy(ifr.ifr_name, argv[optind], IFNAMSIZ);
269 ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
270 if (!ifr.ifr_ifindex) {
271 perror("if_nametoindex");
276 addr.can_family = AF_CAN;
277 addr.can_ifindex = ifr.ifr_ifindex;
279 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
285 if ((t = open("/dev/net/tun", O_RDWR)) < 0) {
286 perror("open tunfd");
292 memset(&ifr, 0, sizeof(ifr));
293 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
294 strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
295 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
297 if (ioctl(t, TUNSETIFF, (void *) &ifr) < 0) {
298 perror("ioctl tunfd");
310 if ((ret = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
315 if (FD_ISSET(s, &rdfs)) {
316 nbytes = read(s, buffer, BUF_LEN);
318 perror("read isotp socket");
321 if (nbytes > MAX_PDU_LENGTH)
323 ret = write(t, buffer, nbytes);
325 if (ret < 0 && errno == EAGAIN)
333 if (FD_ISSET(t, &rdfs)) {
334 nbytes = read(t, buffer, BUF_LEN);
336 perror("read tunfd");
339 if (nbytes > MAX_PDU_LENGTH)
341 ret = write(s, buffer, nbytes);
343 if (ret < 0 && errno == EAGAIN)