]> rtime.felk.cvut.cz Git - can-utils.git/blob - isotptun.c
Remove feature to quit the isotptun by keypress (from stdin) as it was not possible...
[can-utils.git] / isotptun.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * isotptun.c - IP over CAN ISO-TP (ISO15765-2) tunnel / proof-of-concept
7  *
8  * This program creates a Linux tunnel netdevice 'ctunX' and transfers the
9  * ethernet frames inside ISO15765-2 (unreliable) datagrams on CAN.
10  *
11  * Use e.g. "ifconfig ctun0 123.123.123.1 pointopoint 123.123.123.2 up"
12  * to create a point-to-point IP connection on CAN.
13  *
14  * Copyright (c) 2008 Volkswagen Group Electronic Research
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of Volkswagen nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * Alternatively, provided that this notice is retained in full, this
30  * software may be distributed under the terms of the GNU General
31  * Public License ("GPL") version 2, in which case the provisions of the
32  * GPL apply INSTEAD OF those given above.
33  *
34  * The provided data structures and external interfaces from this code
35  * are not restricted to be used by modules with a GPL compatible license.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
48  * DAMAGE.
49  *
50  * Send feedback to <socketcan-users@lists.berlios.de>
51  *
52  */
53
54 #include <stdio.h>
55 #include <fcntl.h>
56 #include <unistd.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <libgen.h>
60 #include <errno.h>
61 #include <signal.h>
62
63 #include <net/if.h>
64 #include <sys/types.h>
65 #include <sys/socket.h>
66 #include <sys/ioctl.h>
67
68 #include <linux/can.h>
69 #include <linux/can/isotp.h>
70 #include <linux/if_tun.h>
71
72 #define NO_CAN_ID 0xFFFFFFFFU
73
74 static volatile int running = 1;
75
76 void print_usage(char *prg)
77 {
78         fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n\n", prg);
79         fprintf(stderr, "This program creates a Linux tunnel netdevice 'ctunX' and transfers the\n");
80         fprintf(stderr, "ethernet frames inside ISO15765-2 (unreliable) datagrams on CAN.\n\n");
81         fprintf(stderr, "Options: -s <can_id>  (source can_id. Use 8 digits for extended IDs)\n");
82         fprintf(stderr, "         -d <can_id>  (destination can_id. Use 8 digits for extended IDs)\n");
83         fprintf(stderr, "         -x <addr>    (extended addressing mode.)\n");
84         fprintf(stderr, "         -p <byte>    (padding byte rx path)\n");
85         fprintf(stderr, "         -q <byte>    (padding byte tx path)\n");
86         fprintf(stderr, "         -P <mode>    (check padding. (l)ength (c)ontent (a)ll)\n");
87         fprintf(stderr, "         -t <time ns> (transmit time in nanosecs)\n");
88         fprintf(stderr, "         -b <bs>      (blocksize. 0 = off)\n");
89         fprintf(stderr, "         -m <val>     (STmin in ms/ns. See spec.)\n");
90         fprintf(stderr, "         -w <num>     (max. wait frame transmissions.)\n");
91         fprintf(stderr, "         -h           (half duplex mode.)\n");
92         fprintf(stderr, "         -v           (verbose mode. Print symbols for tunneled msgs.)\n");
93         fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
94         fprintf(stderr, "Use e.g. 'ifconfig ctun0 123.123.123.1 pointopoint 123.123.123.2 up'\n");
95         fprintf(stderr, "to create a point-to-point IP connection on CAN.\n");
96         fprintf(stderr, "\n");
97 }
98
99 void sigterm(int signo)
100 {
101         running = 0;
102 }
103
104 int main(int argc, char **argv)
105 {
106         fd_set rdfs;
107         int s, t;
108         struct sockaddr_can addr;
109         struct ifreq ifr;
110         static struct can_isotp_options opts;
111         static struct can_isotp_fc_options fcopts;
112         int opt, ret;
113         extern int optind, opterr, optopt;
114         static int verbose;
115
116         unsigned char buffer[4096];
117         int nbytes;
118
119         signal(SIGTERM, sigterm);
120         signal(SIGHUP, sigterm);
121         signal(SIGINT, sigterm);
122
123         addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
124
125         while ((opt = getopt(argc, argv, "s:d:x:p:q:P:t:b:m:whv?")) != -1) {
126                 switch (opt) {
127                 case 's':
128                         addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
129                         if (strlen(optarg) > 7)
130                                 addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
131                         break;
132
133                 case 'd':
134                         addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
135                         if (strlen(optarg) > 7)
136                                 addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
137                         break;
138
139                 case 'x':
140                         opts.flags |= CAN_ISOTP_EXTEND_ADDR;
141                         opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
142                         break;
143
144                 case 'p':
145                         opts.flags |= CAN_ISOTP_RX_PADDING;
146                         opts.rxpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
147                         break;
148
149                 case 'q':
150                         opts.flags |= CAN_ISOTP_TX_PADDING;
151                         opts.txpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
152                         break;
153
154                 case 'P':
155                         if (optarg[0] == 'l')
156                                 opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
157                         else if (optarg[0] == 'c')
158                                 opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
159                         else if (optarg[0] == 'a')
160                                 opts.flags |= (CAN_ISOTP_CHK_PAD_DATA | CAN_ISOTP_CHK_PAD_DATA);
161                         else {
162                                 printf("unknown padding check option '%c'.\n", optarg[0]);
163                                 print_usage(basename(argv[0]));
164                                 exit(0);
165                         }
166                         break;
167
168                 case 't':
169                         opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
170                         break;
171
172                 case 'b':
173                         fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
174                         break;
175
176                 case 'm':
177                         fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
178                         break;
179
180                 case 'w':
181                         fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
182                         break;
183
184                 case 'h':
185                         opts.flags |= CAN_ISOTP_HALF_DUPLEX;
186                         break;
187
188                 case 'v':
189                         verbose = 1;
190                         break;
191
192                 case '?':
193                         print_usage(basename(argv[0]));
194                         exit(0);
195                         break;
196
197                 default:
198                         fprintf(stderr, "Unknown option %c\n", opt);
199                         print_usage(basename(argv[0]));
200                         exit(1);
201                         break;
202                 }
203         }
204
205         if ((argc - optind != 1) ||
206             (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
207             (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
208                 print_usage(basename(argv[0]));
209                 exit(1);
210         }
211   
212         if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
213                 perror("socket");
214                 exit(1);
215         }
216
217         setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
218         setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
219
220         addr.can_family = AF_CAN;
221         strcpy(ifr.ifr_name, argv[optind]);
222         ioctl(s, SIOCGIFINDEX, &ifr);
223         addr.can_ifindex = ifr.ifr_ifindex;
224
225         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
226                 perror("bind");
227                 close(s);
228                 exit(1);
229         }
230
231         if ((t = open("/dev/net/tun", O_RDWR)) < 0) {
232                 perror("open tunfd");
233                 close(s);
234                 close(t);
235                 exit(1);
236         }
237
238         memset(&ifr, 0, sizeof(ifr));
239         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
240         strncpy(ifr.ifr_name, "ctun%d", IFNAMSIZ);
241
242         if (ioctl(t, TUNSETIFF, (void *) &ifr) < 0) {
243                 perror("ioctl tunfd");
244                 close(s);
245                 close(t);
246                 exit(1);
247         }
248
249         while (running) {
250
251                 FD_ZERO(&rdfs);
252                 FD_SET(s, &rdfs);
253                 FD_SET(t, &rdfs);
254
255                 if ((ret = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
256                         perror("select");
257                         continue;
258                 }
259
260                 if (FD_ISSET(s, &rdfs)) {
261                         nbytes = read(s, buffer, 4096);
262                         if (nbytes < 0) {
263                                 perror("read isotp socket");
264                                 return -1;
265                         }
266                         if (nbytes > 4095)
267                                 return -1;
268                         ret = write(t, buffer, nbytes);
269                         if (verbose) {
270                                 if (ret < 0 && errno == EAGAIN)
271                                         printf(";");
272                                 else
273                                         printf(",");
274                                 fflush(stdout);
275                         }
276                 }
277
278                 if (FD_ISSET(t, &rdfs)) {
279                         nbytes = read(t, buffer, 4096);
280                         if (nbytes < 0) {
281                                 perror("read tunfd");
282                                 return -1;
283                         }
284                         if (nbytes > 4095)
285                                 return -1;
286                         ret = write(s, buffer, nbytes);
287                         if (verbose) {
288                                 if (ret < 0 && errno == EAGAIN)
289                                         printf(":");
290                                 else
291                                         printf(".");
292                                 fflush(stdout);
293                         }
294                 }
295         }
296
297         close(s);
298         close(t);
299         return 0;
300 }