cangw.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * cangw.c - manage PF_CAN netlink gateway
  3. *
  4. * Copyright (c) 2010 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. * Send feedback to <linux-can@vger.kernel.org>
  41. *
  42. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <libgen.h>
  46. #include <string.h>
  47. #include <unistd.h>
  48. #include <errno.h>
  49. #include <sys/socket.h>
  50. #include <net/if.h>
  51. #include <linux/netlink.h>
  52. #include <linux/rtnetlink.h>
  53. #include <linux/can/gw.h>
  54. enum {
  55. UNSPEC,
  56. ADD,
  57. DEL,
  58. FLUSH,
  59. LIST
  60. };
  61. struct modattr {
  62. struct can_frame cf;
  63. __u8 modtype;
  64. __u8 instruction;
  65. } __attribute__((packed));
  66. #define RTCAN_RTA(r) ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct rtcanmsg))))
  67. #define RTCAN_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct rtcanmsg))
  68. /* some netlink helpers stolen from iproute2 package */
  69. #define NLMSG_TAIL(nmsg) \
  70. ((struct rtattr *)(((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
  71. int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
  72. int alen)
  73. {
  74. int len = RTA_LENGTH(alen);
  75. struct rtattr *rta;
  76. if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
  77. fprintf(stderr, "addattr_l: message exceeded bound of %d\n",
  78. maxlen);
  79. return -1;
  80. }
  81. rta = NLMSG_TAIL(n);
  82. rta->rta_type = type;
  83. rta->rta_len = len;
  84. memcpy(RTA_DATA(rta), data, alen);
  85. n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
  86. return 0;
  87. }
  88. void printfilter(const void *data)
  89. {
  90. struct can_filter *filter = (struct can_filter *)data;
  91. if (filter->can_id & CAN_INV_FILTER)
  92. printf("-f %03X~%X ", (filter->can_id & ~CAN_INV_FILTER), filter->can_mask);
  93. else
  94. printf("-f %03X:%X ", filter->can_id, filter->can_mask);
  95. }
  96. void printmod(const char *type, const void *data)
  97. {
  98. struct modattr mod;
  99. int i;
  100. memcpy (&mod, data, CGW_MODATTR_LEN);
  101. printf("-m %s:", type);
  102. if (mod.modtype & CGW_MOD_ID)
  103. printf("I");
  104. if (mod.modtype & CGW_MOD_DLC)
  105. printf("L");
  106. if (mod.modtype & CGW_MOD_DATA)
  107. printf("D");
  108. printf(":%03X.%X.", mod.cf.can_id, mod.cf.can_dlc);
  109. for (i = 0; i < 8; i++)
  110. printf("%02X", mod.cf.data[i]);
  111. printf(" ");
  112. }
  113. void print_cs_xor(struct cgw_csum_xor *cs_xor)
  114. {
  115. printf("-x %d:%d:%d:%02X ",
  116. cs_xor->from_idx, cs_xor->to_idx,
  117. cs_xor->result_idx, cs_xor->init_xor_val);
  118. }
  119. void print_cs_crc8_profile(struct cgw_csum_crc8 *cs_crc8)
  120. {
  121. int i;
  122. printf("-p %d:", cs_crc8->profile);
  123. switch (cs_crc8->profile) {
  124. case CGW_CRC8PRF_1U8:
  125. printf("%02X", cs_crc8->profile_data[0]);
  126. break;
  127. case CGW_CRC8PRF_16U8:
  128. for (i = 0; i < 16; i++)
  129. printf("%02X", cs_crc8->profile_data[i]);
  130. break;
  131. case CGW_CRC8PRF_SFFID_XOR:
  132. break;
  133. default:
  134. printf("<unknown profile #%d>", cs_crc8->profile);
  135. }
  136. printf(" ");
  137. }
  138. void print_cs_crc8(struct cgw_csum_crc8 *cs_crc8)
  139. {
  140. int i;
  141. printf("-c %d:%d:%d:%02X:%02X:",
  142. cs_crc8->from_idx, cs_crc8->to_idx,
  143. cs_crc8->result_idx, cs_crc8->init_crc_val,
  144. cs_crc8->final_xor_val);
  145. for (i = 0; i < 256; i++)
  146. printf("%02X", cs_crc8->crctab[i]);
  147. printf(" ");
  148. if (cs_crc8->profile != CGW_CRC8PRF_UNSPEC)
  149. print_cs_crc8_profile(cs_crc8);
  150. }
  151. void print_usage(char *prg)
  152. {
  153. fprintf(stderr, "\nUsage: %s [options]\n\n", prg);
  154. fprintf(stderr, "Commands: -A (add a new rule)\n");
  155. fprintf(stderr, " -D (delete a rule)\n");
  156. fprintf(stderr, " -F (flush / delete all rules)\n");
  157. fprintf(stderr, " -L (list all rules)\n");
  158. fprintf(stderr, "Mandatory: -s <src_dev> (source netdevice)\n");
  159. fprintf(stderr, " -d <dst_dev> (destination netdevice)\n");
  160. fprintf(stderr, "Options: -t (preserve src_dev rx timestamp)\n");
  161. fprintf(stderr, " -e (echo sent frames - recommended on vcanx)\n");
  162. fprintf(stderr, " -i (allow to route to incoming interface)\n");
  163. fprintf(stderr, " -u <uid> (user defined modification identifier)\n");
  164. fprintf(stderr, " -l <hops> (limit the number of frame hops / routings)\n");
  165. fprintf(stderr, " -f <filter> (set CAN filter)\n");
  166. fprintf(stderr, " -m <mod> (set frame modifications)\n");
  167. fprintf(stderr, " -x <from_idx>:<to_idx>:<result_idx>:<init_xor_val> (XOR checksum)\n");
  168. fprintf(stderr, " -c <from>:<to>:<result>:<init_val>:<xor_val>:<crctab[256]> (CRC8 cs)\n");
  169. fprintf(stderr, " -p <profile>:[<profile_data>] (CRC8 checksum profile & parameters)\n");
  170. fprintf(stderr, "\nValues are given and expected in hexadecimal values. Leading 0s can be omitted.\n");
  171. fprintf(stderr, "\n");
  172. fprintf(stderr, "<filter> is a <value><mask> CAN identifier filter\n");
  173. fprintf(stderr, " <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)\n");
  174. fprintf(stderr, " <can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)\n");
  175. fprintf(stderr, "\n");
  176. fprintf(stderr, "<mod> is a CAN frame modification instruction consisting of\n");
  177. fprintf(stderr, "<instruction>:<can_frame-elements>:<can_id>.<can_dlc>.<can_data>\n");
  178. fprintf(stderr, " - <instruction> is one of 'AND' 'OR' 'XOR' 'SET'\n");
  179. fprintf(stderr, " - <can_frame-elements> is _one_ or _more_ of 'I'dentifier 'L'ength 'D'ata\n");
  180. fprintf(stderr, " - <can_id> is an u32 value containing the CAN Identifier\n");
  181. fprintf(stderr, " - <can_dlc> is an u8 value containing the data length code (0 .. 8)\n");
  182. fprintf(stderr, " - <can_data> is always eight(!) u8 values containing the CAN frames data\n");
  183. fprintf(stderr, "The max. four modifications are performed in the order AND -> OR -> XOR -> SET\n");
  184. fprintf(stderr, "\n");
  185. fprintf(stderr, "Example:\n");
  186. fprintf(stderr, "%s -A -s can0 -d vcan3 -e -f 123:C00007FF -m SET:IL:333.4.1122334455667788\n", prg);
  187. fprintf(stderr, "\n");
  188. fprintf(stderr, "Supported CRC 8 profiles:\n");
  189. fprintf(stderr, "Profile '%d' (1U8) - add one additional u8 value\n", CGW_CRC8PRF_1U8);
  190. fprintf(stderr, "Profile '%d' (16U8) - add u8 value from table[16] indexed by (data[1] & 0xF)\n", CGW_CRC8PRF_16U8);
  191. fprintf(stderr, "Profile '%d' (SFFID_XOR) - add u8 value (can_id & 0xFF) ^ (can_id >> 8 & 0xFF)\n", CGW_CRC8PRF_SFFID_XOR);
  192. fprintf(stderr, "\n");
  193. }
  194. int b64hex(char *asc, unsigned char *bin, int len)
  195. {
  196. int i;
  197. for (i = 0; i < len; i++) {
  198. if (!sscanf(asc+(i*2), "%2hhx", bin+i))
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. int parse_crc8_profile(char *optarg, struct cgw_csum_crc8 *crc8)
  204. {
  205. int ret = 1;
  206. char *ptr;
  207. if (sscanf(optarg, "%hhd:", &crc8->profile) != 1)
  208. return ret;
  209. switch (crc8->profile) {
  210. case CGW_CRC8PRF_1U8:
  211. if (sscanf(optarg, "%hhd:%2hhx", &crc8->profile, &crc8->profile_data[0]) == 2)
  212. ret = 0;
  213. break;
  214. case CGW_CRC8PRF_16U8:
  215. ptr = strchr(optarg, ':');
  216. /* check if length contains 16 base64 hex values */
  217. if (ptr != NULL &&
  218. strlen(ptr) == strlen(":00112233445566778899AABBCCDDEEFF") &&
  219. b64hex(ptr+1, (unsigned char *)&crc8->profile_data[0], 16) == 0)
  220. ret = 0;
  221. break;
  222. case CGW_CRC8PRF_SFFID_XOR:
  223. /* no additional parameters needed */
  224. ret = 0;
  225. break;
  226. }
  227. return ret;
  228. }
  229. int parse_mod(char *optarg, struct modattr *modmsg)
  230. {
  231. char *ptr, *nptr;
  232. char hexdata[17] = {0};
  233. ptr = optarg;
  234. nptr = strchr(ptr, ':');
  235. if ((nptr - ptr > 3) || (nptr - ptr == 0))
  236. return 1;
  237. if (!strncmp(ptr, "AND", 3))
  238. modmsg->instruction = CGW_MOD_AND;
  239. else if (!strncmp(ptr, "OR", 2))
  240. modmsg->instruction = CGW_MOD_OR;
  241. else if (!strncmp(ptr, "XOR", 3))
  242. modmsg->instruction = CGW_MOD_XOR;
  243. else if (!strncmp(ptr, "SET", 3))
  244. modmsg->instruction = CGW_MOD_SET;
  245. else
  246. return 2;
  247. ptr = nptr+1;
  248. nptr = strchr(ptr, ':');
  249. if ((nptr - ptr > 3) || (nptr - ptr == 0))
  250. return 3;
  251. modmsg->modtype = 0;
  252. while (*ptr != ':') {
  253. switch (*ptr) {
  254. case 'I':
  255. modmsg->modtype |= CGW_MOD_ID;
  256. break;
  257. case 'L':
  258. modmsg->modtype |= CGW_MOD_DLC;
  259. break;
  260. case 'D':
  261. modmsg->modtype |= CGW_MOD_DATA;
  262. break;
  263. default:
  264. return 4;
  265. }
  266. ptr++;
  267. }
  268. if (sscanf(++ptr, "%x.%hhx.%16s", &modmsg->cf.can_id,
  269. (unsigned char *)&modmsg->cf.can_dlc, hexdata) != 3)
  270. return 5;
  271. /* 4-bit masks can have values from 0 to 0xF */
  272. if (modmsg->cf.can_dlc > 0xF)
  273. return 6;
  274. /* but when setting CAN_DLC the value has to be limited to 8 */
  275. if (modmsg->instruction == CGW_MOD_SET && modmsg->cf.can_dlc > 8)
  276. return 7;
  277. if (strlen(hexdata) != 16)
  278. return 8;
  279. if (b64hex(hexdata, &modmsg->cf.data[0], 8))
  280. return 9;
  281. return 0; /* ok */
  282. }
  283. int parse_rtlist(char *prgname, unsigned char *rxbuf, int len)
  284. {
  285. char ifname[IF_NAMESIZE]; /* internface name for if_indextoname() */
  286. struct rtcanmsg *rtc;
  287. struct rtattr *rta;
  288. struct nlmsghdr *nlh;
  289. unsigned int src_ifindex = 0;
  290. unsigned int dst_ifindex = 0;
  291. __u32 handled, dropped, deleted;
  292. int rtlen;
  293. nlh = (struct nlmsghdr *)rxbuf;
  294. while (1) {
  295. if (!NLMSG_OK(nlh, len))
  296. return 0;
  297. if (nlh->nlmsg_type == NLMSG_ERROR) {
  298. printf("NLMSG_ERROR\n");
  299. return 1;
  300. }
  301. if (nlh->nlmsg_type == NLMSG_DONE) {
  302. //printf("NLMSG_DONE\n");
  303. return 1;
  304. }
  305. rtc = (struct rtcanmsg *)NLMSG_DATA(nlh);
  306. if (rtc->can_family != AF_CAN) {
  307. printf("received msg from unknown family %d\n", rtc->can_family);
  308. return -EINVAL;
  309. }
  310. if (rtc->gwtype != CGW_TYPE_CAN_CAN) {
  311. printf("received msg with unknown gwtype %d\n", rtc->gwtype);
  312. return -EINVAL;
  313. }
  314. /*
  315. * print list in a representation that
  316. * can be used directly for start scripts.
  317. *
  318. * To order the mandatory and optional parameters in the
  319. * output string, the NLMSG is parsed twice.
  320. */
  321. handled = 0;
  322. dropped = 0;
  323. deleted = 0;
  324. src_ifindex = 0;
  325. dst_ifindex = 0;
  326. printf("%s -A ", basename(prgname));
  327. /* first parse for mandatory options */
  328. rta = (struct rtattr *) RTCAN_RTA(rtc);
  329. rtlen = RTCAN_PAYLOAD(nlh);
  330. for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
  331. {
  332. //printf("(A-%d)", rta->rta_type);
  333. switch(rta->rta_type) {
  334. case CGW_FILTER:
  335. case CGW_MOD_AND:
  336. case CGW_MOD_OR:
  337. case CGW_MOD_XOR:
  338. case CGW_MOD_SET:
  339. case CGW_MOD_UID:
  340. case CGW_LIM_HOPS:
  341. case CGW_CS_XOR:
  342. case CGW_CS_CRC8:
  343. break;
  344. case CGW_SRC_IF:
  345. src_ifindex = *(__u32 *)RTA_DATA(rta);
  346. break;
  347. case CGW_DST_IF:
  348. dst_ifindex = *(__u32 *)RTA_DATA(rta);
  349. break;
  350. case CGW_HANDLED:
  351. handled = *(__u32 *)RTA_DATA(rta);
  352. break;
  353. case CGW_DROPPED:
  354. dropped = *(__u32 *)RTA_DATA(rta);
  355. break;
  356. case CGW_DELETED:
  357. deleted = *(__u32 *)RTA_DATA(rta);
  358. break;
  359. default:
  360. printf("Unknown attribute %d!", rta->rta_type);
  361. return -EINVAL;
  362. break;
  363. }
  364. }
  365. printf("-s %s ", if_indextoname(src_ifindex, ifname));
  366. printf("-d %s ", if_indextoname(dst_ifindex, ifname));
  367. if (rtc->flags & CGW_FLAGS_CAN_ECHO)
  368. printf("-e ");
  369. if (rtc->flags & CGW_FLAGS_CAN_SRC_TSTAMP)
  370. printf("-t ");
  371. if (rtc->flags & CGW_FLAGS_CAN_IIF_TX_OK)
  372. printf("-i ");
  373. /* second parse for mod attributes */
  374. rta = (struct rtattr *) RTCAN_RTA(rtc);
  375. rtlen = RTCAN_PAYLOAD(nlh);
  376. for(;RTA_OK(rta, rtlen);rta=RTA_NEXT(rta,rtlen))
  377. {
  378. //printf("(B-%d)", rta->rta_type);
  379. switch(rta->rta_type) {
  380. case CGW_FILTER:
  381. printfilter(RTA_DATA(rta));
  382. break;
  383. case CGW_MOD_AND:
  384. printmod("AND", RTA_DATA(rta));
  385. break;
  386. case CGW_MOD_OR:
  387. printmod("OR", RTA_DATA(rta));
  388. break;
  389. case CGW_MOD_XOR:
  390. printmod("XOR", RTA_DATA(rta));
  391. break;
  392. case CGW_MOD_SET:
  393. printmod("SET", RTA_DATA(rta));
  394. break;
  395. case CGW_MOD_UID:
  396. printf("-u %X ", *(__u32 *)RTA_DATA(rta));
  397. break;
  398. case CGW_LIM_HOPS:
  399. printf("-l %d ", *(__u8 *)RTA_DATA(rta));
  400. break;
  401. case CGW_CS_XOR:
  402. print_cs_xor((struct cgw_csum_xor *)RTA_DATA(rta));
  403. break;
  404. case CGW_CS_CRC8:
  405. print_cs_crc8((struct cgw_csum_crc8 *)RTA_DATA(rta));
  406. break;
  407. case CGW_SRC_IF:
  408. case CGW_DST_IF:
  409. case CGW_HANDLED:
  410. case CGW_DROPPED:
  411. case CGW_DELETED:
  412. break;
  413. default:
  414. printf("Unknown attribute %d!", rta->rta_type);
  415. return -EINVAL;
  416. break;
  417. }
  418. }
  419. /* end of entry */
  420. printf("# %d handled %d dropped %d deleted\n",
  421. handled, dropped, deleted);
  422. /* jump to next NLMSG in the given buffer */
  423. nlh = NLMSG_NEXT(nlh, len);
  424. }
  425. }
  426. int main(int argc, char **argv)
  427. {
  428. int s;
  429. int err = 0;
  430. int opt;
  431. extern int optind, opterr, optopt;
  432. int cmd = UNSPEC;
  433. int have_filter = 0;
  434. int have_cs_xor = 0;
  435. int have_cs_crc8 = 0;
  436. struct {
  437. struct nlmsghdr nh;
  438. struct rtcanmsg rtcan;
  439. char buf[600];
  440. } req;
  441. unsigned char rxbuf[8192]; /* netlink receive buffer */
  442. struct nlmsghdr *nlh;
  443. struct nlmsgerr *rte;
  444. unsigned int src_ifindex = 0;
  445. unsigned int dst_ifindex = 0;
  446. __u32 uid = 0;
  447. __u8 limit_hops = 0;
  448. __u16 flags = 0;
  449. int len;
  450. struct can_filter filter;
  451. struct sockaddr_nl nladdr;
  452. struct cgw_csum_xor cs_xor;
  453. struct cgw_csum_crc8 cs_crc8;
  454. char crc8tab[513] = {0};
  455. struct modattr modmsg[CGW_MOD_FUNCS];
  456. int modidx = 0;
  457. int i;
  458. memset(&req, 0, sizeof(req));
  459. memset(&cs_xor, 0, sizeof(cs_xor));
  460. memset(&cs_crc8, 0, sizeof(cs_crc8));
  461. while ((opt = getopt(argc, argv, "ADFLs:d:teiu:l:f:c:p:x:m:?")) != -1) {
  462. switch (opt) {
  463. case 'A':
  464. if (cmd == UNSPEC)
  465. cmd = ADD;
  466. break;
  467. case 'D':
  468. if (cmd == UNSPEC)
  469. cmd = DEL;
  470. break;
  471. case 'F':
  472. if (cmd == UNSPEC)
  473. cmd = FLUSH;
  474. break;
  475. case 'L':
  476. if (cmd == UNSPEC)
  477. cmd = LIST;
  478. break;
  479. case 's':
  480. src_ifindex = if_nametoindex(optarg);
  481. break;
  482. case 'd':
  483. dst_ifindex = if_nametoindex(optarg);
  484. break;
  485. case 't':
  486. flags |= CGW_FLAGS_CAN_SRC_TSTAMP;
  487. break;
  488. case 'e':
  489. flags |= CGW_FLAGS_CAN_ECHO;
  490. break;
  491. case 'i':
  492. flags |= CGW_FLAGS_CAN_IIF_TX_OK;
  493. break;
  494. case 'u':
  495. uid = strtoul(optarg, (char **)NULL, 16);
  496. break;
  497. case 'l':
  498. if (sscanf(optarg, "%hhd", &limit_hops) != 1 || !(limit_hops)) {
  499. printf("Bad hop limit definition '%s'.\n", optarg);
  500. exit(1);
  501. }
  502. break;
  503. case 'f':
  504. if (sscanf(optarg, "%x:%x", &filter.can_id,
  505. &filter.can_mask) == 2) {
  506. have_filter = 1;
  507. } else if (sscanf(optarg, "%x~%x", &filter.can_id,
  508. &filter.can_mask) == 2) {
  509. filter.can_id |= CAN_INV_FILTER;
  510. have_filter = 1;
  511. } else {
  512. printf("Bad filter definition '%s'.\n", optarg);
  513. exit(1);
  514. }
  515. break;
  516. case 'x':
  517. if (sscanf(optarg, "%hhd:%hhd:%hhd:%hhx",
  518. &cs_xor.from_idx, &cs_xor.to_idx,
  519. &cs_xor.result_idx, &cs_xor.init_xor_val) == 4) {
  520. have_cs_xor = 1;
  521. } else {
  522. printf("Bad XOR checksum definition '%s'.\n", optarg);
  523. exit(1);
  524. }
  525. break;
  526. case 'c':
  527. if ((sscanf(optarg, "%hhd:%hhd:%hhd:%hhx:%hhx:%512s",
  528. &cs_crc8.from_idx, &cs_crc8.to_idx,
  529. &cs_crc8.result_idx, &cs_crc8.init_crc_val,
  530. &cs_crc8.final_xor_val, crc8tab) == 6) &&
  531. (strlen(crc8tab) == 512) &&
  532. (b64hex(crc8tab, (unsigned char *)&cs_crc8.crctab, 256) == 0)) {
  533. have_cs_crc8 = 1;
  534. } else {
  535. printf("Bad CRC8 checksum definition '%s'.\n", optarg);
  536. exit(1);
  537. }
  538. break;
  539. case 'p':
  540. if (parse_crc8_profile(optarg, &cs_crc8)) {
  541. printf("Bad CRC8 profile definition '%s'.\n", optarg);
  542. exit(1);
  543. }
  544. break;
  545. case 'm':
  546. /* may be triggered by each of the CGW_MOD_FUNCS functions */
  547. if ((modidx < CGW_MOD_FUNCS) && (err = parse_mod(optarg, &modmsg[modidx++]))) {
  548. printf("Problem %d with modification definition '%s'.\n", err, optarg);
  549. exit(1);
  550. }
  551. break;
  552. case '?':
  553. print_usage(basename(argv[0]));
  554. exit(0);
  555. break;
  556. default:
  557. fprintf(stderr, "Unknown option %c\n", opt);
  558. print_usage(basename(argv[0]));
  559. exit(1);
  560. break;
  561. }
  562. }
  563. if ((argc - optind != 0) || (cmd == UNSPEC)) {
  564. print_usage(basename(argv[0]));
  565. exit(1);
  566. }
  567. if ((cmd == ADD || cmd == DEL) &&
  568. ((!src_ifindex) || (!dst_ifindex))) {
  569. print_usage(basename(argv[0]));
  570. exit(1);
  571. }
  572. if (!modidx && (have_cs_crc8 || have_cs_xor)) {
  573. printf("-c or -x can only be used in conjunction with -m\n");
  574. exit(1);
  575. }
  576. s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  577. switch (cmd) {
  578. case ADD:
  579. req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
  580. req.nh.nlmsg_type = RTM_NEWROUTE;
  581. break;
  582. case DEL:
  583. req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
  584. req.nh.nlmsg_type = RTM_DELROUTE;
  585. break;
  586. case FLUSH:
  587. req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
  588. req.nh.nlmsg_type = RTM_DELROUTE;
  589. /* if_index set to 0 => remove all entries */
  590. src_ifindex = 0;
  591. dst_ifindex = 0;
  592. break;
  593. case LIST:
  594. req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
  595. req.nh.nlmsg_type = RTM_GETROUTE;
  596. break;
  597. default:
  598. printf("This function is not yet implemented.\n");
  599. exit(1);
  600. break;
  601. }
  602. req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtcanmsg));
  603. req.nh.nlmsg_seq = 0;
  604. req.rtcan.can_family = AF_CAN;
  605. req.rtcan.gwtype = CGW_TYPE_CAN_CAN;
  606. req.rtcan.flags = flags;
  607. addattr_l(&req.nh, sizeof(req), CGW_SRC_IF, &src_ifindex, sizeof(src_ifindex));
  608. addattr_l(&req.nh, sizeof(req), CGW_DST_IF, &dst_ifindex, sizeof(dst_ifindex));
  609. /* add new attributes here */
  610. if (have_filter)
  611. addattr_l(&req.nh, sizeof(req), CGW_FILTER, &filter, sizeof(filter));
  612. if (have_cs_crc8)
  613. addattr_l(&req.nh, sizeof(req), CGW_CS_CRC8, &cs_crc8, sizeof(cs_crc8));
  614. if (have_cs_xor)
  615. addattr_l(&req.nh, sizeof(req), CGW_CS_XOR, &cs_xor, sizeof(cs_xor));
  616. if (uid)
  617. addattr_l(&req.nh, sizeof(req), CGW_MOD_UID, &uid, sizeof(__u32));
  618. if (limit_hops)
  619. addattr_l(&req.nh, sizeof(req), CGW_LIM_HOPS, &limit_hops, sizeof(__u8));
  620. /*
  621. * a better example code
  622. * modmsg.modtype = CGW_MOD_ID;
  623. * addattr_l(&req.n, sizeof(req), CGW_MOD_SET, &modmsg, CGW_MODATTR_LEN);
  624. */
  625. /* add up to CGW_MOD_FUNCS modification definitions */
  626. for (i = 0; i < modidx; i++)
  627. addattr_l(&req.nh, sizeof(req), modmsg[i].instruction, &modmsg[i], CGW_MODATTR_LEN);
  628. memset(&nladdr, 0, sizeof(nladdr));
  629. nladdr.nl_family = AF_NETLINK;
  630. nladdr.nl_pid = 0;
  631. nladdr.nl_groups = 0;
  632. err = sendto(s, &req, req.nh.nlmsg_len, 0,
  633. (struct sockaddr*)&nladdr, sizeof(nladdr));
  634. if (err < 0) {
  635. perror("netlink sendto");
  636. return err;
  637. }
  638. /* clean netlink receive buffer */
  639. memset(rxbuf, 0x0, sizeof(rxbuf));
  640. if (cmd != LIST) {
  641. /*
  642. * cmd == ADD || cmd == DEL || cmd == FLUSH
  643. *
  644. * Parse the requested netlink acknowledge return values.
  645. */
  646. err = recv(s, &rxbuf, sizeof(rxbuf), 0);
  647. if (err < 0) {
  648. perror("netlink recv");
  649. return err;
  650. }
  651. nlh = (struct nlmsghdr *)rxbuf;
  652. if (nlh->nlmsg_type != NLMSG_ERROR) {
  653. fprintf(stderr, "unexpected netlink answer of type %d\n", nlh->nlmsg_type);
  654. return -EINVAL;
  655. }
  656. rte = (struct nlmsgerr *)NLMSG_DATA(nlh);
  657. err = rte->error;
  658. if (err < 0)
  659. fprintf(stderr, "netlink error %d (%s)\n", err, strerror(abs(err)));
  660. } else {
  661. /* cmd == LIST */
  662. while (1) {
  663. len = recv(s, &rxbuf, sizeof(rxbuf), 0);
  664. if (len < 0) {
  665. perror("netlink recv");
  666. return len;
  667. }
  668. #if 0
  669. printf("received msg len %d\n", len);
  670. for (i = 0; i < len; i++)
  671. printf("%02X ", rxbuf[i]);
  672. printf("\n");
  673. #endif
  674. /* leave on errors or NLMSG_DONE */
  675. if (parse_rtlist(argv[0], rxbuf, len))
  676. break;
  677. }
  678. }
  679. close(s);
  680. return err;
  681. }