]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - isotptun.c
Move BCM server into can-utils as it is not test application anymore.
[sojka/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
62 #include <net/if.h>
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <sys/ioctl.h>
66
67 #include <linux/can.h>
68 #include <linux/can/isotp.h>
69 #include <linux/if_tun.h>
70
71 #define NO_CAN_ID 0xFFFFFFFFU
72
73 void print_usage(char *prg)
74 {
75         fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n\n", prg);
76         fprintf(stderr, "This program creates a Linux tunnel netdevice 'ctunX' and transfers the\n");
77         fprintf(stderr, "ethernet frames inside ISO15765-2 (unreliable) datagrams on CAN.\n\n");
78         fprintf(stderr, "Options: -s <can_id>  (source can_id. Use 8 digits for extended IDs)\n");
79         fprintf(stderr, "         -d <can_id>  (destination can_id. Use 8 digits for extended IDs)\n");
80         fprintf(stderr, "         -x <addr>    (extended addressing mode.)\n");
81         fprintf(stderr, "         -p <byte>    (padding byte rx path)\n");
82         fprintf(stderr, "         -q <byte>    (padding byte tx path)\n");
83         fprintf(stderr, "         -P <mode>    (check padding. (l)ength (c)ontent (a)ll)\n");
84         fprintf(stderr, "         -t <time ns> (transmit time in nanosecs)\n");
85         fprintf(stderr, "         -b <bs>      (blocksize. 0 = off)\n");
86         fprintf(stderr, "         -m <val>     (STmin in ms/ns. See spec.)\n");
87         fprintf(stderr, "         -w <num>     (max. wait frame transmissions.)\n");
88         fprintf(stderr, "         -h           (half duplex mode.)\n");
89         fprintf(stderr, "         -v           (verbose mode. Print symbols for tunneled msgs.)\n");
90         fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
91         fprintf(stderr, "Use e.g. 'ifconfig ctun0 123.123.123.1 pointopoint 123.123.123.2 up'\n");
92         fprintf(stderr, "to create a point-to-point IP connection on CAN.\n");
93         fprintf(stderr, "\n");
94 }
95
96 int main(int argc, char **argv)
97 {
98         fd_set rdfs;
99         int s, t;
100         struct sockaddr_can addr;
101         struct ifreq ifr;
102         static struct can_isotp_options opts;
103         static struct can_isotp_fc_options fcopts;
104         int opt, ret;
105         extern int optind, opterr, optopt;
106         static int quit;
107         static int verbose;
108
109         unsigned char buffer[4096];
110         int nbytes;
111
112         addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
113
114         while ((opt = getopt(argc, argv, "s:d:x:p:q:P:t:b:m:whv?")) != -1) {
115                 switch (opt) {
116                 case 's':
117                         addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
118                         if (strlen(optarg) > 7)
119                                 addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
120                         break;
121
122                 case 'd':
123                         addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
124                         if (strlen(optarg) > 7)
125                                 addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
126                         break;
127
128                 case 'x':
129                         opts.flags |= CAN_ISOTP_EXTEND_ADDR;
130                         opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
131                         break;
132
133                 case 'p':
134                         opts.flags |= CAN_ISOTP_RX_PADDING;
135                         opts.rxpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
136                         break;
137
138                 case 'q':
139                         opts.flags |= CAN_ISOTP_TX_PADDING;
140                         opts.txpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
141                         break;
142
143                 case 'P':
144                         if (optarg[0] == 'l')
145                                 opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
146                         else if (optarg[0] == 'c')
147                                 opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
148                         else if (optarg[0] == 'a')
149                                 opts.flags |= (CAN_ISOTP_CHK_PAD_DATA | CAN_ISOTP_CHK_PAD_DATA);
150                         else {
151                                 printf("unknown padding check option '%c'.\n", optarg[0]);
152                                 print_usage(basename(argv[0]));
153                                 exit(0);
154                         }
155                         break;
156
157                 case 't':
158                         opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
159                         break;
160
161                 case 'b':
162                         fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
163                         break;
164
165                 case 'm':
166                         fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
167                         break;
168
169                 case 'w':
170                         fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
171                         break;
172
173                 case 'h':
174                         opts.flags |= CAN_ISOTP_HALF_DUPLEX;
175                         break;
176
177                 case 'v':
178                         verbose = 1;
179                         break;
180
181                 case '?':
182                         print_usage(basename(argv[0]));
183                         exit(0);
184                         break;
185
186                 default:
187                         fprintf(stderr, "Unknown option %c\n", opt);
188                         print_usage(basename(argv[0]));
189                         exit(1);
190                         break;
191                 }
192         }
193
194         if ((argc - optind != 1) ||
195             (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
196             (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
197                 print_usage(basename(argv[0]));
198                 exit(1);
199         }
200   
201         if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
202                 perror("socket");
203                 exit(1);
204         }
205
206         setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
207         setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
208
209         addr.can_family = AF_CAN;
210         strcpy(ifr.ifr_name, argv[optind]);
211         ioctl(s, SIOCGIFINDEX, &ifr);
212         addr.can_ifindex = ifr.ifr_ifindex;
213
214         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
215                 perror("bind");
216                 close(s);
217                 exit(1);
218         }
219
220         if ((t = open("/dev/net/tun", O_RDWR)) < 0) {
221                 perror("open tunfd");
222                 close(s);
223                 close(t);
224                 exit(1);
225         }
226
227         memset(&ifr, 0, sizeof(ifr));
228         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
229         strncpy(ifr.ifr_name, "ctun%d", IFNAMSIZ);
230
231         if (ioctl(t, TUNSETIFF, (void *) &ifr) < 0) {
232                 perror("ioctl tunfd");
233                 close(s);
234                 close(t);
235                 exit(1);
236         }
237
238         while (!quit) {
239
240                 FD_ZERO(&rdfs);
241                 FD_SET(s, &rdfs);
242                 FD_SET(t, &rdfs);
243                 FD_SET(0, &rdfs);
244
245                 if ((ret = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
246                         perror("select");
247                         continue;
248                 }
249
250                 if (FD_ISSET(0, &rdfs)) {
251                         getchar();
252                         quit = 1;
253                         printf("quit due to keyboard input.\n");
254                 }
255
256                 if (FD_ISSET(s, &rdfs)) {
257                         nbytes = read(s, buffer, 4096);
258                         if (nbytes < 0) {
259                                 perror("read isotp socket");
260                                 return -1;
261                         }
262                         if (nbytes > 4095)
263                                 return -1;
264                         ret = write(t, buffer, nbytes);
265                         if (verbose) {
266                                 if (ret < 0 && errno == EAGAIN)
267                                         printf(";");
268                                 else
269                                         printf(",");
270                                 fflush(stdout);
271                         }
272                 }
273
274                 if (FD_ISSET(t, &rdfs)) {
275                         nbytes = read(t, buffer, 4096);
276                         if (nbytes < 0) {
277                                 perror("read tunfd");
278                                 return -1;
279                         }
280                         if (nbytes > 4095)
281                                 return -1;
282                         ret = write(s, buffer, nbytes);
283                         if (verbose) {
284                                 if (ret < 0 && errno == EAGAIN)
285                                         printf(":");
286                                 else
287                                         printf(".");
288                                 fflush(stdout);
289                         }
290                 }
291         }
292
293         close(s);
294         close(t);
295         return 0;
296 }