isotptun.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * isotptun.c - IP over CAN ISO-TP (ISO15765-2) tunnel / proof-of-concept
  3. *
  4. * This program creates a Linux tunnel netdevice 'ctunX' and transfers the
  5. * ethernet frames inside ISO15765-2 (unreliable) datagrams on CAN.
  6. *
  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.
  9. *
  10. * Copyright (c) 2008 Volkswagen Group Electronic Research
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  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.
  24. *
  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.
  29. *
  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.
  32. *
  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
  44. * DAMAGE.
  45. *
  46. * Send feedback to <linux-can@vger.kernel.org>
  47. *
  48. */
  49. #include <stdio.h>
  50. #include <fcntl.h>
  51. #include <unistd.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <libgen.h>
  55. #include <errno.h>
  56. #include <signal.h>
  57. #include <stdarg.h>
  58. #include <syslog.h>
  59. #include <net/if.h>
  60. #include <sys/types.h>
  61. #include <sys/socket.h>
  62. #include <sys/ioctl.h>
  63. #include <linux/can.h>
  64. #include <linux/can/isotp.h>
  65. #include <linux/if_tun.h>
  66. /* Change this to whatever your daemon is called */
  67. #define DAEMON_NAME "isotptun"
  68. #define NO_CAN_ID 0xFFFFFFFFU
  69. #define DEFAULT_NAME "ctun%d"
  70. /* stay on 4095 bytes for the max. PDU length which is still much more than the standard ethernet MTU */
  71. #define MAX_PDU_LENGTH 4095
  72. #define BUF_LEN (MAX_PDU_LENGTH + 1)
  73. static volatile int running = 1;
  74. static void fake_syslog(int priority, const char *format, ...)
  75. {
  76. va_list ap;
  77. fprintf(stderr, "[%d] ", priority);
  78. va_start(ap, format);
  79. vfprintf(stderr, format, ap);
  80. va_end(ap);
  81. fprintf(stderr, "\n");
  82. }
  83. typedef void (*syslog_t)(int priority, const char *format, ...);
  84. static syslog_t syslogger = syslog;
  85. void perror_syslog(const char *s)
  86. {
  87. const char *colon = s ? ": " : "";
  88. syslogger(LOG_ERR, "%s%s%s", s, colon, strerror(errno));
  89. }
  90. void print_usage(char *prg)
  91. {
  92. fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n\n", prg);
  93. fprintf(stderr, "This program creates a Linux tunnel netdevice 'ctunX' and transfers the\n");
  94. fprintf(stderr, "ethernet frames inside ISO15765-2 (unreliable) datagrams on CAN.\n\n");
  95. fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
  96. fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
  97. fprintf(stderr, " -n <name> (name of created IP netdevice. Default: '%s')\n", DEFAULT_NAME);
  98. fprintf(stderr, " -x <addr>[:<rxaddr>] (extended addressing / opt. separate rxaddr)\n");
  99. fprintf(stderr, " -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n");
  100. fprintf(stderr, " -p [tx]:[rx] (set and enable tx/rx padding bytes)\n");
  101. fprintf(stderr, " -P <mode> (check rx padding for (l)ength (c)ontent (a)ll)\n");
  102. fprintf(stderr, " -t <time ns> (transmit time in nanosecs)\n");
  103. fprintf(stderr, " -b <bs> (blocksize. 0 = off)\n");
  104. fprintf(stderr, " -m <val> (STmin in ms/ns. See spec.)\n");
  105. fprintf(stderr, " -w <num> (max. wait frame transmissions.)\n");
  106. fprintf(stderr, " -D (daemonize to background when tun device created)\n");
  107. fprintf(stderr, " -h (half duplex mode.)\n");
  108. fprintf(stderr, " -v (verbose mode. Print symbols for tunneled msgs.)\n");
  109. fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
  110. fprintf(stderr, "Use e.g. 'ifconfig ctun0 123.123.123.1 pointopoint 123.123.123.2 up'\n");
  111. fprintf(stderr, "to create a point-to-point IP connection on CAN.\n");
  112. fprintf(stderr, "\n");
  113. }
  114. void sigterm(int signo)
  115. {
  116. running = 0;
  117. }
  118. int main(int argc, char **argv)
  119. {
  120. fd_set rdfs;
  121. int s, t;
  122. struct sockaddr_can addr;
  123. struct ifreq ifr;
  124. static struct can_isotp_options opts;
  125. static struct can_isotp_fc_options fcopts;
  126. static struct can_isotp_ll_options llopts;
  127. int opt, ret;
  128. extern int optind, opterr, optopt;
  129. static int verbose;
  130. unsigned char buffer[BUF_LEN];
  131. static char name[IFNAMSIZ] = DEFAULT_NAME;
  132. int nbytes;
  133. int run_as_daemon = 0;
  134. addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
  135. while ((opt = getopt(argc, argv, "s:d:n:x:p:P:t:b:m:whL:vD?")) != -1) {
  136. switch (opt) {
  137. case 's':
  138. addr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
  139. if (strlen(optarg) > 7)
  140. addr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
  141. break;
  142. case 'd':
  143. addr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
  144. if (strlen(optarg) > 7)
  145. addr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
  146. break;
  147. case 'n':
  148. strncpy(name, optarg, IFNAMSIZ-1);
  149. break;
  150. case 'x':
  151. {
  152. int elements = sscanf(optarg, "%hhx:%hhx",
  153. &opts.ext_address,
  154. &opts.rx_ext_address);
  155. if (elements == 1)
  156. opts.flags |= CAN_ISOTP_EXTEND_ADDR;
  157. else if (elements == 2)
  158. opts.flags |= (CAN_ISOTP_EXTEND_ADDR | CAN_ISOTP_RX_EXT_ADDR);
  159. else {
  160. fprintf(stderr, "incorrect extended addr values '%s'.\n", optarg);
  161. print_usage(basename(argv[0]));
  162. exit(EXIT_FAILURE);
  163. }
  164. break;
  165. }
  166. case 'p':
  167. {
  168. int elements = sscanf(optarg, "%hhx:%hhx",
  169. &opts.txpad_content,
  170. &opts.rxpad_content);
  171. if (elements == 1)
  172. opts.flags |= CAN_ISOTP_TX_PADDING;
  173. else if (elements == 2)
  174. opts.flags |= (CAN_ISOTP_TX_PADDING | CAN_ISOTP_RX_PADDING);
  175. else if (sscanf(optarg, ":%hhx", &opts.rxpad_content) == 1)
  176. opts.flags |= CAN_ISOTP_RX_PADDING;
  177. else {
  178. fprintf(stderr, "incorrect padding values '%s'.\n", optarg);
  179. print_usage(basename(argv[0]));
  180. exit(EXIT_FAILURE);
  181. }
  182. break;
  183. }
  184. case 'P':
  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);
  191. else {
  192. fprintf(stderr, "unknown padding check option '%c'.\n", optarg[0]);
  193. print_usage(basename(argv[0]));
  194. exit(EXIT_FAILURE);
  195. }
  196. break;
  197. case 't':
  198. opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
  199. break;
  200. case 'b':
  201. fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  202. break;
  203. case 'm':
  204. fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  205. break;
  206. case 'w':
  207. fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  208. break;
  209. case 'h':
  210. opts.flags |= CAN_ISOTP_HALF_DUPLEX;
  211. break;
  212. case 'L':
  213. if (sscanf(optarg, "%hhu:%hhu:%hhu",
  214. &llopts.mtu,
  215. &llopts.tx_dl,
  216. &llopts.tx_flags) != 3) {
  217. fprintf(stderr, "unknown link layer options '%s'.\n", optarg);
  218. print_usage(basename(argv[0]));
  219. exit(EXIT_FAILURE);
  220. }
  221. break;
  222. case 'v':
  223. verbose = 1;
  224. break;
  225. case 'D':
  226. run_as_daemon = 1;
  227. break;
  228. case '?':
  229. print_usage(basename(argv[0]));
  230. exit(EXIT_SUCCESS);
  231. break;
  232. default:
  233. fprintf(stderr, "Unknown option %c\n", opt);
  234. print_usage(basename(argv[0]));
  235. exit(EXIT_FAILURE);
  236. break;
  237. }
  238. }
  239. if ((argc - optind != 1) ||
  240. (addr.can_addr.tp.tx_id == NO_CAN_ID) ||
  241. (addr.can_addr.tp.rx_id == NO_CAN_ID)) {
  242. print_usage(basename(argv[0]));
  243. exit(EXIT_FAILURE);
  244. }
  245. if (!run_as_daemon)
  246. syslogger = fake_syslog;
  247. /* Initialize the logging interface */
  248. openlog(DAEMON_NAME, LOG_PID, LOG_LOCAL5);
  249. if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
  250. perror_syslog("socket");
  251. exit(EXIT_FAILURE);
  252. }
  253. setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
  254. setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
  255. if (llopts.tx_dl) {
  256. if (setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts)) < 0) {
  257. perror_syslog("link layer sockopt");
  258. exit(EXIT_FAILURE);
  259. }
  260. }
  261. strncpy(ifr.ifr_name, argv[optind], IFNAMSIZ);
  262. ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
  263. if (!ifr.ifr_ifindex) {
  264. perror_syslog("if_nametoindex");
  265. close(s);
  266. exit(EXIT_FAILURE);
  267. }
  268. addr.can_family = AF_CAN;
  269. addr.can_ifindex = ifr.ifr_ifindex;
  270. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  271. perror_syslog("bind");
  272. close(s);
  273. exit(EXIT_FAILURE);
  274. }
  275. if ((t = open("/dev/net/tun", O_RDWR)) < 0) {
  276. perror_syslog("open tunfd");
  277. close(s);
  278. close(t);
  279. exit(EXIT_FAILURE);
  280. }
  281. memset(&ifr, 0, sizeof(ifr));
  282. ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  283. strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
  284. ifr.ifr_name[IFNAMSIZ - 1] = '\0';
  285. if (ioctl(t, TUNSETIFF, (void *) &ifr) < 0) {
  286. perror_syslog("ioctl tunfd");
  287. close(s);
  288. close(t);
  289. exit(EXIT_FAILURE);
  290. }
  291. /* Now the tun device exists. We can daemonize to let the
  292. * parent continue and use the network interface. */
  293. if (run_as_daemon) {
  294. if (daemon(0, 0)) {
  295. syslogger(LOG_ERR, "failed to daemonize");
  296. exit(EXIT_FAILURE);
  297. }
  298. }
  299. signal(SIGTERM, sigterm);
  300. signal(SIGHUP, sigterm);
  301. signal(SIGINT, sigterm);
  302. while (running) {
  303. FD_ZERO(&rdfs);
  304. FD_SET(s, &rdfs);
  305. FD_SET(t, &rdfs);
  306. if ((ret = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
  307. perror_syslog("select");
  308. continue;
  309. }
  310. if (FD_ISSET(s, &rdfs)) {
  311. nbytes = read(s, buffer, BUF_LEN);
  312. if (nbytes < 0) {
  313. perror_syslog("read isotp socket");
  314. return -1;
  315. }
  316. if (nbytes > MAX_PDU_LENGTH)
  317. return -1;
  318. ret = write(t, buffer, nbytes);
  319. if (verbose) {
  320. if (ret < 0 && errno == EAGAIN)
  321. printf(";");
  322. else
  323. printf(",");
  324. fflush(stdout);
  325. }
  326. }
  327. if (FD_ISSET(t, &rdfs)) {
  328. nbytes = read(t, buffer, BUF_LEN);
  329. if (nbytes < 0) {
  330. perror_syslog("read tunfd");
  331. return -1;
  332. }
  333. if (nbytes > MAX_PDU_LENGTH)
  334. return -1;
  335. ret = write(s, buffer, nbytes);
  336. if (verbose) {
  337. if (ret < 0 && errno == EAGAIN)
  338. printf(":");
  339. else
  340. printf(".");
  341. fflush(stdout);
  342. }
  343. }
  344. }
  345. close(s);
  346. close(t);
  347. return EXIT_SUCCESS;
  348. }