]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - can-utils/isotptun.c
Add kernel version depency for Kernel 3.1.x which extended __rtnl_register().
[socketcan-devel.git] / can-utils / 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 #define DEFAULT_NAME "ctun%d"
74
75 static volatile int running = 1;
76
77 void print_usage(char *prg)
78 {
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>    (extended addressing mode.)\n");
86         fprintf(stderr, "         -p <byte>    (padding byte rx path)\n");
87         fprintf(stderr, "         -q <byte>    (padding byte tx path)\n");
88         fprintf(stderr, "         -P <mode>    (check padding. (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");
99 }
100
101 void sigterm(int signo)
102 {
103         running = 0;
104 }
105
106 int main(int argc, char **argv)
107 {
108         fd_set rdfs;
109         int s, t;
110         struct sockaddr_can addr;
111         struct ifreq ifr;
112         static struct can_isotp_options opts;
113         static struct can_isotp_fc_options fcopts;
114         int opt, ret;
115         extern int optind, opterr, optopt;
116         static int verbose;
117         unsigned char buffer[4096];
118         static char name[IFNAMSIZ] = DEFAULT_NAME;
119         int nbytes;
120
121         signal(SIGTERM, sigterm);
122         signal(SIGHUP, sigterm);
123         signal(SIGINT, sigterm);
124
125         addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
126
127         while ((opt = getopt(argc, argv, "s:d:n:x:p:q:P:t:b:m:whv?")) != -1) {
128                 switch (opt) {
129                 case 's':
130                         addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
131                         if (strlen(optarg) > 7)
132                                 addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
133                         break;
134
135                 case 'd':
136                         addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
137                         if (strlen(optarg) > 7)
138                                 addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
139                         break;
140
141                 case 'n':
142                         strncpy(name, optarg, IFNAMSIZ-1);
143                         break;
144
145                 case 'x':
146                         opts.flags |= CAN_ISOTP_EXTEND_ADDR;
147                         opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
148                         break;
149
150                 case 'p':
151                         opts.flags |= CAN_ISOTP_RX_PADDING;
152                         opts.rxpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
153                         break;
154
155                 case 'q':
156                         opts.flags |= CAN_ISOTP_TX_PADDING;
157                         opts.txpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
158                         break;
159
160                 case 'P':
161                         if (optarg[0] == 'l')
162                                 opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
163                         else if (optarg[0] == 'c')
164                                 opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
165                         else if (optarg[0] == 'a')
166                                 opts.flags |= (CAN_ISOTP_CHK_PAD_DATA | CAN_ISOTP_CHK_PAD_DATA);
167                         else {
168                                 printf("unknown padding check option '%c'.\n", optarg[0]);
169                                 print_usage(basename(argv[0]));
170                                 exit(0);
171                         }
172                         break;
173
174                 case 't':
175                         opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
176                         break;
177
178                 case 'b':
179                         fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
180                         break;
181
182                 case 'm':
183                         fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
184                         break;
185
186                 case 'w':
187                         fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
188                         break;
189
190                 case 'h':
191                         opts.flags |= CAN_ISOTP_HALF_DUPLEX;
192                         break;
193
194                 case 'v':
195                         verbose = 1;
196                         break;
197
198                 case '?':
199                         print_usage(basename(argv[0]));
200                         exit(0);
201                         break;
202
203                 default:
204                         fprintf(stderr, "Unknown option %c\n", opt);
205                         print_usage(basename(argv[0]));
206                         exit(1);
207                         break;
208                 }
209         }
210
211         if ((argc - optind != 1) ||
212             (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
213             (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
214                 print_usage(basename(argv[0]));
215                 exit(1);
216         }
217   
218         if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
219                 perror("socket");
220                 exit(1);
221         }
222
223         setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
224         setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
225
226         addr.can_family = AF_CAN;
227         strcpy(ifr.ifr_name, argv[optind]);
228         ioctl(s, SIOCGIFINDEX, &ifr);
229         addr.can_ifindex = ifr.ifr_ifindex;
230
231         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
232                 perror("bind");
233                 close(s);
234                 exit(1);
235         }
236
237         if ((t = open("/dev/net/tun", O_RDWR)) < 0) {
238                 perror("open tunfd");
239                 close(s);
240                 close(t);
241                 exit(1);
242         }
243
244         memset(&ifr, 0, sizeof(ifr));
245         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
246         strncpy(ifr.ifr_name, name, IFNAMSIZ);
247
248         if (ioctl(t, TUNSETIFF, (void *) &ifr) < 0) {
249                 perror("ioctl tunfd");
250                 close(s);
251                 close(t);
252                 exit(1);
253         }
254
255         while (running) {
256
257                 FD_ZERO(&rdfs);
258                 FD_SET(s, &rdfs);
259                 FD_SET(t, &rdfs);
260
261                 if ((ret = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
262                         perror("select");
263                         continue;
264                 }
265
266                 if (FD_ISSET(s, &rdfs)) {
267                         nbytes = read(s, buffer, 4096);
268                         if (nbytes < 0) {
269                                 perror("read isotp socket");
270                                 return -1;
271                         }
272                         if (nbytes > 4095)
273                                 return -1;
274                         ret = write(t, buffer, nbytes);
275                         if (verbose) {
276                                 if (ret < 0 && errno == EAGAIN)
277                                         printf(";");
278                                 else
279                                         printf(",");
280                                 fflush(stdout);
281                         }
282                 }
283
284                 if (FD_ISSET(t, &rdfs)) {
285                         nbytes = read(t, buffer, 4096);
286                         if (nbytes < 0) {
287                                 perror("read tunfd");
288                                 return -1;
289                         }
290                         if (nbytes > 4095)
291                                 return -1;
292                         ret = write(s, buffer, nbytes);
293                         if (verbose) {
294                                 if (ret < 0 && errno == EAGAIN)
295                                         printf(":");
296                                 else
297                                         printf(".");
298                                 fflush(stdout);
299                         }
300                 }
301         }
302
303         close(s);
304         close(t);
305         return 0;
306 }