gw.c 23 KB

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