gw.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
  3. *
  4. * Copyright (c) 2011 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 <socketcan-users@lists.berlios.de>
  41. *
  42. */
  43. #include <linux/module.h>
  44. #include <linux/init.h>
  45. #include <linux/types.h>
  46. #include <linux/list.h>
  47. #include <linux/spinlock.h>
  48. #include <linux/rcupdate.h>
  49. #include <linux/rculist.h>
  50. #include <linux/net.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/if_arp.h>
  53. #include <linux/skbuff.h>
  54. #include <linux/can.h>
  55. #include <linux/can/core.h>
  56. #include <linux/can/gw.h>
  57. #include <net/rtnetlink.h>
  58. #include <net/net_namespace.h>
  59. #include <net/sock.h>
  60. #define CAN_GW_VERSION "20101209"
  61. static __initdata const char banner[] =
  62. KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n";
  63. MODULE_DESCRIPTION("PF_CAN netlink gateway");
  64. MODULE_LICENSE("Dual BSD/GPL");
  65. MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
  66. MODULE_ALIAS("can-gw");
  67. HLIST_HEAD(cgw_list);
  68. static struct notifier_block notifier;
  69. static struct kmem_cache *cgw_cache __read_mostly;
  70. /* structure that contains the (on-the-fly) CAN frame modifications */
  71. struct cf_mod {
  72. struct {
  73. struct can_frame and;
  74. struct can_frame or;
  75. struct can_frame xor;
  76. struct can_frame set;
  77. } modframe;
  78. struct {
  79. u8 and;
  80. u8 or;
  81. u8 xor;
  82. u8 set;
  83. } modtype;
  84. void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
  85. struct cf_mod *mod);
  86. /* CAN frame checksum calculation after CAN frame modifications */
  87. struct {
  88. struct cgw_csum_xor xor;
  89. struct cgw_csum_crc8 crc8;
  90. } csum;
  91. struct {
  92. void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
  93. void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
  94. } csumfunc;
  95. };
  96. /*
  97. * So far we just support CAN -> CAN routing and frame modifications.
  98. *
  99. * The internal can_can_gw structure contains data and attributes for
  100. * a CAN -> CAN gateway job.
  101. */
  102. struct can_can_gw {
  103. struct can_filter filter;
  104. int src_idx;
  105. int dst_idx;
  106. };
  107. /* list entry for CAN gateways jobs */
  108. struct cgw_job {
  109. struct hlist_node list;
  110. struct rcu_head rcu;
  111. u32 handled_frames;
  112. u32 dropped_frames;
  113. struct cf_mod mod;
  114. union {
  115. /* CAN frame data source */
  116. struct net_device *dev;
  117. } src;
  118. union {
  119. /* CAN frame data destination */
  120. struct net_device *dev;
  121. } dst;
  122. union {
  123. struct can_can_gw ccgw;
  124. /* tbc */
  125. };
  126. u8 gwtype;
  127. u16 flags;
  128. };
  129. /* modification functions that are invoked in the hot path in can_can_gw_rcv */
  130. #define MODFUNC(func, op) static void func(struct can_frame *cf, \
  131. struct cf_mod *mod) { op ; }
  132. MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
  133. MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc)
  134. MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
  135. MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
  136. MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
  137. MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
  138. MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
  139. MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
  140. MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
  141. MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
  142. MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
  143. MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
  144. static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
  145. {
  146. /*
  147. * Copy the struct members separately to ensure that no uninitialized
  148. * data are copied in the 3 bytes hole of the struct. This is needed
  149. * to make easy compares of the data in the struct cf_mod.
  150. */
  151. dst->can_id = src->can_id;
  152. dst->can_dlc = src->can_dlc;
  153. *(u64 *)dst->data = *(u64 *)src->data;
  154. }
  155. static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re)
  156. {
  157. /*
  158. * absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
  159. * relative to received dlc -1 .. -8 :
  160. * e.g. for received dlc = 8
  161. * -1 => index = 7 (data[7])
  162. * -3 => index = 5 (data[5])
  163. * -8 => index = 0 (data[0])
  164. */
  165. if (fr > -9 && fr < 8 &&
  166. to > -9 && to < 8 &&
  167. re > -9 && re < 8)
  168. return 0;
  169. else
  170. return -EINVAL;
  171. }
  172. static inline int calc_idx(int idx, int rx_dlc)
  173. {
  174. if (idx < 0)
  175. return rx_dlc + idx;
  176. else
  177. return idx;
  178. }
  179. static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
  180. {
  181. int from = calc_idx(xor->from_idx, cf->can_dlc);
  182. int to = calc_idx(xor->to_idx, cf->can_dlc);
  183. int res = calc_idx(xor->result_idx, cf->can_dlc);
  184. u8 val = xor->init_xor_val;
  185. int i;
  186. if (from < 0 || to < 0 || res < 0)
  187. return;
  188. if (from <= to) {
  189. for (i = from; i <= to; i++)
  190. val ^= cf->data[i];
  191. } else {
  192. for (i = from; i >= to; i--)
  193. val ^= cf->data[i];
  194. }
  195. cf->data[res] = val;
  196. }
  197. static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
  198. {
  199. u8 val = xor->init_xor_val;
  200. int i;
  201. for (i = xor->from_idx; i <= xor->to_idx; i++)
  202. val ^= cf->data[i];
  203. cf->data[xor->result_idx] = val;
  204. }
  205. static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
  206. {
  207. u8 val = xor->init_xor_val;
  208. int i;
  209. for (i = xor->from_idx; i >= xor->to_idx; i--)
  210. val ^= cf->data[i];
  211. cf->data[xor->result_idx] = val;
  212. }
  213. static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  214. {
  215. int from = calc_idx(crc8->from_idx, cf->can_dlc);
  216. int to = calc_idx(crc8->to_idx, cf->can_dlc);
  217. int res = calc_idx(crc8->result_idx, cf->can_dlc);
  218. u8 crc = crc8->init_crc_val;
  219. int i;
  220. if (from < 0 || to < 0 || res < 0)
  221. return;
  222. if (from <= to) {
  223. for (i = crc8->from_idx; i <= crc8->to_idx; i++)
  224. crc = crc8->crctab[crc^cf->data[i]];
  225. } else {
  226. for (i = crc8->from_idx; i >= crc8->to_idx; i--)
  227. crc = crc8->crctab[crc^cf->data[i]];
  228. }
  229. switch (crc8->profile) {
  230. case CGW_CRC8PRF_1U8:
  231. crc = crc8->crctab[crc^crc8->profile_data[0]];
  232. break;
  233. case CGW_CRC8PRF_16U8:
  234. crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  235. break;
  236. case CGW_CRC8PRF_SFFID_XOR:
  237. crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  238. (cf->can_id >> 8 & 0xFF)];
  239. break;
  240. }
  241. cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  242. }
  243. static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  244. {
  245. u8 crc = crc8->init_crc_val;
  246. int i;
  247. for (i = crc8->from_idx; i <= crc8->to_idx; i++)
  248. crc = crc8->crctab[crc^cf->data[i]];
  249. switch (crc8->profile) {
  250. case CGW_CRC8PRF_1U8:
  251. crc = crc8->crctab[crc^crc8->profile_data[0]];
  252. break;
  253. case CGW_CRC8PRF_16U8:
  254. crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  255. break;
  256. case CGW_CRC8PRF_SFFID_XOR:
  257. crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  258. (cf->can_id >> 8 & 0xFF)];
  259. break;
  260. }
  261. cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  262. }
  263. static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  264. {
  265. u8 crc = crc8->init_crc_val;
  266. int i;
  267. for (i = crc8->from_idx; i >= crc8->to_idx; i--)
  268. crc = crc8->crctab[crc^cf->data[i]];
  269. switch (crc8->profile) {
  270. case CGW_CRC8PRF_1U8:
  271. crc = crc8->crctab[crc^crc8->profile_data[0]];
  272. break;
  273. case CGW_CRC8PRF_16U8:
  274. crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  275. break;
  276. case CGW_CRC8PRF_SFFID_XOR:
  277. crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  278. (cf->can_id >> 8 & 0xFF)];
  279. break;
  280. }
  281. cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  282. }
  283. /* the receive & process & send function */
  284. static void can_can_gw_rcv(struct sk_buff *skb, void *data)
  285. {
  286. struct cgw_job *gwj = (struct cgw_job *)data;
  287. struct can_frame *cf;
  288. struct sk_buff *nskb;
  289. int modidx = 0;
  290. /* do not handle already routed frames - see comment below */
  291. if (skb_mac_header_was_set(skb))
  292. return;
  293. if (!(gwj->dst.dev->flags & IFF_UP)) {
  294. gwj->dropped_frames++;
  295. return;
  296. }
  297. /*
  298. * clone the given skb, which has not been done in can_rcv()
  299. *
  300. * When there is at least one modification function activated,
  301. * we need to copy the skb as we want to modify skb->data.
  302. */
  303. if (gwj->mod.modfunc[0])
  304. nskb = skb_copy(skb, GFP_ATOMIC);
  305. else
  306. nskb = skb_clone(skb, GFP_ATOMIC);
  307. if (!nskb) {
  308. gwj->dropped_frames++;
  309. return;
  310. }
  311. /*
  312. * Mark routed frames by setting some mac header length which is
  313. * not relevant for the CAN frames located in the skb->data section.
  314. *
  315. * As dev->header_ops is not set in CAN netdevices no one is ever
  316. * accessing the various header offsets in the CAN skbuffs anyway.
  317. * E.g. using the packet socket to read CAN frames is still working.
  318. */
  319. skb_set_mac_header(nskb, 8);
  320. nskb->dev = gwj->dst.dev;
  321. /* pointer to modifiable CAN frame */
  322. cf = (struct can_frame *)nskb->data;
  323. /* perform preprocessed modification functions if there are any */
  324. while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
  325. (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
  326. /* check for checksum updates when the CAN frame has been modified */
  327. if (modidx) {
  328. if (gwj->mod.csumfunc.crc8)
  329. (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
  330. if (gwj->mod.csumfunc.xor)
  331. (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
  332. }
  333. /* clear the skb timestamp if not configured the other way */
  334. if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
  335. nskb->tstamp.tv64 = 0;
  336. /* send to netdevice */
  337. if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
  338. gwj->dropped_frames++;
  339. else
  340. gwj->handled_frames++;
  341. }
  342. static inline int cgw_register_filter(struct cgw_job *gwj)
  343. {
  344. return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id,
  345. gwj->ccgw.filter.can_mask, can_can_gw_rcv,
  346. gwj, "gw");
  347. }
  348. static inline void cgw_unregister_filter(struct cgw_job *gwj)
  349. {
  350. can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id,
  351. gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
  352. }
  353. static int cgw_notifier(struct notifier_block *nb,
  354. unsigned long msg, void *data)
  355. {
  356. struct net_device *dev = (struct net_device *)data;
  357. if (!net_eq(dev_net(dev), &init_net))
  358. return NOTIFY_DONE;
  359. if (dev->type != ARPHRD_CAN)
  360. return NOTIFY_DONE;
  361. if (msg == NETDEV_UNREGISTER) {
  362. struct cgw_job *gwj = NULL;
  363. struct hlist_node *n, *nx;
  364. ASSERT_RTNL();
  365. hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
  366. if (gwj->src.dev == dev || gwj->dst.dev == dev) {
  367. hlist_del(&gwj->list);
  368. cgw_unregister_filter(gwj);
  369. kfree(gwj);
  370. }
  371. }
  372. }
  373. return NOTIFY_DONE;
  374. }
  375. static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj)
  376. {
  377. struct cgw_frame_mod mb;
  378. struct rtcanmsg *rtcan;
  379. struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
  380. if (!nlh)
  381. return -EMSGSIZE;
  382. rtcan = nlmsg_data(nlh);
  383. rtcan->can_family = AF_CAN;
  384. rtcan->gwtype = gwj->gwtype;
  385. rtcan->flags = gwj->flags;
  386. /* add statistics if available */
  387. if (gwj->handled_frames) {
  388. if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
  389. goto cancel;
  390. else
  391. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
  392. }
  393. if (gwj->dropped_frames) {
  394. if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
  395. goto cancel;
  396. else
  397. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
  398. }
  399. /* check non default settings of attributes */
  400. if (gwj->mod.modtype.and) {
  401. memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
  402. mb.modtype = gwj->mod.modtype.and;
  403. if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
  404. goto cancel;
  405. else
  406. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
  407. }
  408. if (gwj->mod.modtype.or) {
  409. memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
  410. mb.modtype = gwj->mod.modtype.or;
  411. if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
  412. goto cancel;
  413. else
  414. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
  415. }
  416. if (gwj->mod.modtype.xor) {
  417. memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
  418. mb.modtype = gwj->mod.modtype.xor;
  419. if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
  420. goto cancel;
  421. else
  422. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
  423. }
  424. if (gwj->mod.modtype.set) {
  425. memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
  426. mb.modtype = gwj->mod.modtype.set;
  427. if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
  428. goto cancel;
  429. else
  430. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
  431. }
  432. if (gwj->mod.csumfunc.crc8) {
  433. if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
  434. &gwj->mod.csum.crc8) < 0)
  435. goto cancel;
  436. else
  437. nlh->nlmsg_len += NLA_HDRLEN + \
  438. NLA_ALIGN(CGW_CS_CRC8_LEN);
  439. }
  440. if (gwj->mod.csumfunc.xor) {
  441. if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
  442. &gwj->mod.csum.xor) < 0)
  443. goto cancel;
  444. else
  445. nlh->nlmsg_len += NLA_HDRLEN + \
  446. NLA_ALIGN(CGW_CS_XOR_LEN);
  447. }
  448. if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
  449. if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
  450. if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
  451. &gwj->ccgw.filter) < 0)
  452. goto cancel;
  453. else
  454. nlh->nlmsg_len += NLA_HDRLEN +
  455. NLA_ALIGN(sizeof(struct can_filter));
  456. }
  457. if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
  458. goto cancel;
  459. else
  460. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
  461. if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
  462. goto cancel;
  463. else
  464. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
  465. }
  466. return skb->len;
  467. cancel:
  468. nlmsg_cancel(skb, nlh);
  469. return -EMSGSIZE;
  470. }
  471. /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
  472. static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
  473. {
  474. struct cgw_job *gwj = NULL;
  475. struct hlist_node *n;
  476. int idx = 0;
  477. int s_idx = cb->args[0];
  478. rcu_read_lock();
  479. hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) {
  480. if (idx < s_idx)
  481. goto cont;
  482. if (cgw_put_job(skb, gwj) < 0)
  483. break;
  484. cont:
  485. idx++;
  486. }
  487. rcu_read_unlock();
  488. cb->args[0] = idx;
  489. return skb->len;
  490. }
  491. /* check for common and gwtype specific attributes */
  492. static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
  493. u8 gwtype, void *gwtypeattr)
  494. {
  495. struct nlattr *tb[CGW_MAX+1];
  496. struct cgw_frame_mod mb;
  497. int modidx = 0;
  498. int err = 0;
  499. /* initialize modification & checksum data space */
  500. memset(mod, 0, sizeof(*mod));
  501. err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
  502. if (err < 0)
  503. return err;
  504. /* check for AND/OR/XOR/SET modifications */
  505. if (tb[CGW_MOD_AND] &&
  506. nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
  507. nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
  508. canframecpy(&mod->modframe.and, &mb.cf);
  509. mod->modtype.and = mb.modtype;
  510. if (mb.modtype & CGW_MOD_ID)
  511. mod->modfunc[modidx++] = mod_and_id;
  512. if (mb.modtype & CGW_MOD_DLC)
  513. mod->modfunc[modidx++] = mod_and_dlc;
  514. if (mb.modtype & CGW_MOD_DATA)
  515. mod->modfunc[modidx++] = mod_and_data;
  516. }
  517. if (tb[CGW_MOD_OR] &&
  518. nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
  519. nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
  520. canframecpy(&mod->modframe.or, &mb.cf);
  521. mod->modtype.or = mb.modtype;
  522. if (mb.modtype & CGW_MOD_ID)
  523. mod->modfunc[modidx++] = mod_or_id;
  524. if (mb.modtype & CGW_MOD_DLC)
  525. mod->modfunc[modidx++] = mod_or_dlc;
  526. if (mb.modtype & CGW_MOD_DATA)
  527. mod->modfunc[modidx++] = mod_or_data;
  528. }
  529. if (tb[CGW_MOD_XOR] &&
  530. nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
  531. nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
  532. canframecpy(&mod->modframe.xor, &mb.cf);
  533. mod->modtype.xor = mb.modtype;
  534. if (mb.modtype & CGW_MOD_ID)
  535. mod->modfunc[modidx++] = mod_xor_id;
  536. if (mb.modtype & CGW_MOD_DLC)
  537. mod->modfunc[modidx++] = mod_xor_dlc;
  538. if (mb.modtype & CGW_MOD_DATA)
  539. mod->modfunc[modidx++] = mod_xor_data;
  540. }
  541. if (tb[CGW_MOD_SET] &&
  542. nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
  543. nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
  544. canframecpy(&mod->modframe.set, &mb.cf);
  545. mod->modtype.set = mb.modtype;
  546. if (mb.modtype & CGW_MOD_ID)
  547. mod->modfunc[modidx++] = mod_set_id;
  548. if (mb.modtype & CGW_MOD_DLC)
  549. mod->modfunc[modidx++] = mod_set_dlc;
  550. if (mb.modtype & CGW_MOD_DATA)
  551. mod->modfunc[modidx++] = mod_set_data;
  552. }
  553. /* check for checksum operations after CAN frame modifications */
  554. if (modidx) {
  555. if (tb[CGW_CS_CRC8] &&
  556. nla_len(tb[CGW_CS_CRC8]) == CGW_CS_CRC8_LEN) {
  557. struct cgw_csum_crc8 *c = (struct cgw_csum_crc8 *)\
  558. nla_data(tb[CGW_CS_CRC8]);
  559. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  560. c->result_idx);
  561. if (err)
  562. return err;
  563. nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
  564. CGW_CS_CRC8_LEN);
  565. /*
  566. * select dedicated processing function to reduce
  567. * runtime operations in receive hot path.
  568. */
  569. if (c->from_idx < 0 || c->to_idx < 0 ||
  570. c->result_idx < 0)
  571. mod->csumfunc.crc8 = cgw_csum_crc8_rel;
  572. else if (c->from_idx <= c->to_idx)
  573. mod->csumfunc.crc8 = cgw_csum_crc8_pos;
  574. else
  575. mod->csumfunc.crc8 = cgw_csum_crc8_neg;
  576. }
  577. if (tb[CGW_CS_XOR] &&
  578. nla_len(tb[CGW_CS_XOR]) == CGW_CS_XOR_LEN) {
  579. struct cgw_csum_xor *c = (struct cgw_csum_xor *)\
  580. nla_data(tb[CGW_CS_XOR]);
  581. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  582. c->result_idx);
  583. if (err)
  584. return err;
  585. nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
  586. CGW_CS_XOR_LEN);
  587. /*
  588. * select dedicated processing function to reduce
  589. * runtime operations in receive hot path.
  590. */
  591. if (c->from_idx < 0 || c->to_idx < 0 ||
  592. c->result_idx < 0)
  593. mod->csumfunc.xor = cgw_csum_xor_rel;
  594. else if (c->from_idx <= c->to_idx)
  595. mod->csumfunc.xor = cgw_csum_xor_pos;
  596. else
  597. mod->csumfunc.xor = cgw_csum_xor_neg;
  598. }
  599. }
  600. if (gwtype == CGW_TYPE_CAN_CAN) {
  601. /* check CGW_TYPE_CAN_CAN specific attributes */
  602. struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
  603. memset(ccgw, 0, sizeof(*ccgw));
  604. /* check for can_filter in attributes */
  605. if (tb[CGW_FILTER] &&
  606. nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
  607. nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
  608. sizeof(struct can_filter));
  609. err = -ENODEV;
  610. /* specifying two interfaces is mandatory */
  611. if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
  612. return err;
  613. if (nla_len(tb[CGW_SRC_IF]) == sizeof(u32))
  614. nla_memcpy(&ccgw->src_idx, tb[CGW_SRC_IF],
  615. sizeof(u32));
  616. if (nla_len(tb[CGW_DST_IF]) == sizeof(u32))
  617. nla_memcpy(&ccgw->dst_idx, tb[CGW_DST_IF],
  618. sizeof(u32));
  619. /* both indices set to 0 for flushing all routing entries */
  620. if (!ccgw->src_idx && !ccgw->dst_idx)
  621. return 0;
  622. /* only one index set to 0 is an error */
  623. if (!ccgw->src_idx || !ccgw->dst_idx)
  624. return err;
  625. }
  626. /* add the checks for other gwtypes here */
  627. return 0;
  628. }
  629. static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
  630. void *arg)
  631. {
  632. struct rtcanmsg *r;
  633. struct cgw_job *gwj;
  634. int err = 0;
  635. if (nlmsg_len(nlh) < sizeof(*r))
  636. return -EINVAL;
  637. r = nlmsg_data(nlh);
  638. if (r->can_family != AF_CAN)
  639. return -EPFNOSUPPORT;
  640. /* so far we only support CAN -> CAN routings */
  641. if (r->gwtype != CGW_TYPE_CAN_CAN)
  642. return -EINVAL;
  643. gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
  644. if (!gwj)
  645. return -ENOMEM;
  646. gwj->handled_frames = 0;
  647. gwj->dropped_frames = 0;
  648. gwj->flags = r->flags;
  649. gwj->gwtype = r->gwtype;
  650. err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
  651. if (err < 0)
  652. goto out;
  653. err = -ENODEV;
  654. /* ifindex == 0 is not allowed for job creation */
  655. if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
  656. goto out;
  657. gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
  658. if (!gwj->src.dev)
  659. goto out;
  660. /* check for CAN netdev not using header_ops - see gw_rcv() */
  661. if (gwj->src.dev->type != ARPHRD_CAN || gwj->src.dev->header_ops)
  662. goto put_src_out;
  663. gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
  664. if (!gwj->dst.dev)
  665. goto put_src_out;
  666. /* check for CAN netdev not using header_ops - see gw_rcv() */
  667. if (gwj->dst.dev->type != ARPHRD_CAN || gwj->dst.dev->header_ops)
  668. goto put_src_dst_out;
  669. ASSERT_RTNL();
  670. err = cgw_register_filter(gwj);
  671. if (!err)
  672. hlist_add_head_rcu(&gwj->list, &cgw_list);
  673. put_src_dst_out:
  674. dev_put(gwj->dst.dev);
  675. put_src_out:
  676. dev_put(gwj->src.dev);
  677. out:
  678. if (err)
  679. kmem_cache_free(cgw_cache, gwj);
  680. return err;
  681. }
  682. static void cgw_remove_all_jobs(void)
  683. {
  684. struct cgw_job *gwj = NULL;
  685. struct hlist_node *n, *nx;
  686. ASSERT_RTNL();
  687. hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
  688. hlist_del(&gwj->list);
  689. cgw_unregister_filter(gwj);
  690. kfree(gwj);
  691. }
  692. }
  693. static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  694. {
  695. struct cgw_job *gwj = NULL;
  696. struct hlist_node *n, *nx;
  697. struct rtcanmsg *r;
  698. struct cf_mod mod;
  699. struct can_can_gw ccgw;
  700. int err = 0;
  701. if (nlmsg_len(nlh) < sizeof(*r))
  702. return -EINVAL;
  703. r = nlmsg_data(nlh);
  704. if (r->can_family != AF_CAN)
  705. return -EPFNOSUPPORT;
  706. /* so far we only support CAN -> CAN routings */
  707. if (r->gwtype != CGW_TYPE_CAN_CAN)
  708. return -EINVAL;
  709. err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw);
  710. if (err < 0)
  711. return err;
  712. /* two interface indices both set to 0 => remove all entries */
  713. if (!ccgw.src_idx && !ccgw.dst_idx) {
  714. cgw_remove_all_jobs();
  715. return 0;
  716. }
  717. err = -EINVAL;
  718. ASSERT_RTNL();
  719. /* remove only the first matching entry */
  720. hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
  721. if (gwj->flags != r->flags)
  722. continue;
  723. if (memcmp(&gwj->mod, &mod, sizeof(mod)))
  724. continue;
  725. /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
  726. if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
  727. continue;
  728. hlist_del(&gwj->list);
  729. cgw_unregister_filter(gwj);
  730. kfree(gwj);
  731. err = 0;
  732. break;
  733. }
  734. return err;
  735. }
  736. static __init int cgw_module_init(void)
  737. {
  738. printk(banner);
  739. cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
  740. 0, 0, NULL);
  741. if (!cgw_cache)
  742. return -ENOMEM;
  743. /* set notifier */
  744. notifier.notifier_call = cgw_notifier;
  745. register_netdevice_notifier(&notifier);
  746. if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs, NULL)) {
  747. unregister_netdevice_notifier(&notifier);
  748. kmem_cache_destroy(cgw_cache);
  749. return -ENOBUFS;
  750. }
  751. /* Only the first call to __rtnl_register can fail */
  752. __rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL, NULL);
  753. __rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL, NULL);
  754. return 0;
  755. }
  756. static __exit void cgw_module_exit(void)
  757. {
  758. rtnl_unregister_all(PF_CAN);
  759. unregister_netdevice_notifier(&notifier);
  760. rtnl_lock();
  761. cgw_remove_all_jobs();
  762. rtnl_unlock();
  763. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  764. kmem_cache_destroy(cgw_cache);
  765. }
  766. module_init(cgw_module_init);
  767. module_exit(cgw_module_exit);