gw.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. 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. kmem_cache_free(cgw_cache, gwj);
  368. }
  369. }
  370. }
  371. return NOTIFY_DONE;
  372. }
  373. static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj)
  374. {
  375. struct cgw_frame_mod mb;
  376. struct rtcanmsg *rtcan;
  377. struct nlmsghdr *nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*rtcan), 0);
  378. if (!nlh)
  379. return -EMSGSIZE;
  380. rtcan = nlmsg_data(nlh);
  381. rtcan->can_family = AF_CAN;
  382. rtcan->gwtype = gwj->gwtype;
  383. rtcan->flags = gwj->flags;
  384. /* add statistics if available */
  385. if (gwj->handled_frames) {
  386. if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
  387. goto cancel;
  388. else
  389. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
  390. }
  391. if (gwj->dropped_frames) {
  392. if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
  393. goto cancel;
  394. else
  395. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
  396. }
  397. /* check non default settings of attributes */
  398. if (gwj->mod.modtype.and) {
  399. memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
  400. mb.modtype = gwj->mod.modtype.and;
  401. if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
  402. goto cancel;
  403. else
  404. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
  405. }
  406. if (gwj->mod.modtype.or) {
  407. memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
  408. mb.modtype = gwj->mod.modtype.or;
  409. if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
  410. goto cancel;
  411. else
  412. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
  413. }
  414. if (gwj->mod.modtype.xor) {
  415. memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
  416. mb.modtype = gwj->mod.modtype.xor;
  417. if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
  418. goto cancel;
  419. else
  420. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
  421. }
  422. if (gwj->mod.modtype.set) {
  423. memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
  424. mb.modtype = gwj->mod.modtype.set;
  425. if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
  426. goto cancel;
  427. else
  428. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(mb));
  429. }
  430. if (gwj->mod.csumfunc.crc8) {
  431. if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
  432. &gwj->mod.csum.crc8) < 0)
  433. goto cancel;
  434. else
  435. nlh->nlmsg_len += NLA_HDRLEN + \
  436. NLA_ALIGN(CGW_CS_CRC8_LEN);
  437. }
  438. if (gwj->mod.csumfunc.xor) {
  439. if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
  440. &gwj->mod.csum.xor) < 0)
  441. goto cancel;
  442. else
  443. nlh->nlmsg_len += NLA_HDRLEN + \
  444. NLA_ALIGN(CGW_CS_XOR_LEN);
  445. }
  446. if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
  447. if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
  448. if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
  449. &gwj->ccgw.filter) < 0)
  450. goto cancel;
  451. else
  452. nlh->nlmsg_len += NLA_HDRLEN +
  453. NLA_ALIGN(sizeof(struct can_filter));
  454. }
  455. if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
  456. goto cancel;
  457. else
  458. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
  459. if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
  460. goto cancel;
  461. else
  462. nlh->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(sizeof(u32));
  463. }
  464. return skb->len;
  465. cancel:
  466. nlmsg_cancel(skb, nlh);
  467. return -EMSGSIZE;
  468. }
  469. /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
  470. static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
  471. {
  472. struct cgw_job *gwj = NULL;
  473. struct hlist_node *n;
  474. int idx = 0;
  475. int s_idx = cb->args[0];
  476. rcu_read_lock();
  477. hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) {
  478. if (idx < s_idx)
  479. goto cont;
  480. if (cgw_put_job(skb, gwj) < 0)
  481. break;
  482. cont:
  483. idx++;
  484. }
  485. rcu_read_unlock();
  486. cb->args[0] = idx;
  487. return skb->len;
  488. }
  489. /* check for common and gwtype specific attributes */
  490. static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
  491. u8 gwtype, void *gwtypeattr)
  492. {
  493. struct nlattr *tb[CGW_MAX+1];
  494. struct cgw_frame_mod mb;
  495. int modidx = 0;
  496. int err = 0;
  497. /* initialize modification & checksum data space */
  498. memset(mod, 0, sizeof(*mod));
  499. err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX, NULL);
  500. if (err < 0)
  501. return err;
  502. /* check for AND/OR/XOR/SET modifications */
  503. if (tb[CGW_MOD_AND] &&
  504. nla_len(tb[CGW_MOD_AND]) == CGW_MODATTR_LEN) {
  505. nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
  506. canframecpy(&mod->modframe.and, &mb.cf);
  507. mod->modtype.and = mb.modtype;
  508. if (mb.modtype & CGW_MOD_ID)
  509. mod->modfunc[modidx++] = mod_and_id;
  510. if (mb.modtype & CGW_MOD_DLC)
  511. mod->modfunc[modidx++] = mod_and_dlc;
  512. if (mb.modtype & CGW_MOD_DATA)
  513. mod->modfunc[modidx++] = mod_and_data;
  514. }
  515. if (tb[CGW_MOD_OR] &&
  516. nla_len(tb[CGW_MOD_OR]) == CGW_MODATTR_LEN) {
  517. nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
  518. canframecpy(&mod->modframe.or, &mb.cf);
  519. mod->modtype.or = mb.modtype;
  520. if (mb.modtype & CGW_MOD_ID)
  521. mod->modfunc[modidx++] = mod_or_id;
  522. if (mb.modtype & CGW_MOD_DLC)
  523. mod->modfunc[modidx++] = mod_or_dlc;
  524. if (mb.modtype & CGW_MOD_DATA)
  525. mod->modfunc[modidx++] = mod_or_data;
  526. }
  527. if (tb[CGW_MOD_XOR] &&
  528. nla_len(tb[CGW_MOD_XOR]) == CGW_MODATTR_LEN) {
  529. nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
  530. canframecpy(&mod->modframe.xor, &mb.cf);
  531. mod->modtype.xor = mb.modtype;
  532. if (mb.modtype & CGW_MOD_ID)
  533. mod->modfunc[modidx++] = mod_xor_id;
  534. if (mb.modtype & CGW_MOD_DLC)
  535. mod->modfunc[modidx++] = mod_xor_dlc;
  536. if (mb.modtype & CGW_MOD_DATA)
  537. mod->modfunc[modidx++] = mod_xor_data;
  538. }
  539. if (tb[CGW_MOD_SET] &&
  540. nla_len(tb[CGW_MOD_SET]) == CGW_MODATTR_LEN) {
  541. nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
  542. canframecpy(&mod->modframe.set, &mb.cf);
  543. mod->modtype.set = mb.modtype;
  544. if (mb.modtype & CGW_MOD_ID)
  545. mod->modfunc[modidx++] = mod_set_id;
  546. if (mb.modtype & CGW_MOD_DLC)
  547. mod->modfunc[modidx++] = mod_set_dlc;
  548. if (mb.modtype & CGW_MOD_DATA)
  549. mod->modfunc[modidx++] = mod_set_data;
  550. }
  551. /* check for checksum operations after CAN frame modifications */
  552. if (modidx) {
  553. if (tb[CGW_CS_CRC8] &&
  554. nla_len(tb[CGW_CS_CRC8]) == CGW_CS_CRC8_LEN) {
  555. struct cgw_csum_crc8 *c = (struct cgw_csum_crc8 *)\
  556. nla_data(tb[CGW_CS_CRC8]);
  557. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  558. c->result_idx);
  559. if (err)
  560. return err;
  561. nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
  562. CGW_CS_CRC8_LEN);
  563. /*
  564. * select dedicated processing function to reduce
  565. * runtime operations in receive hot path.
  566. */
  567. if (c->from_idx < 0 || c->to_idx < 0 ||
  568. c->result_idx < 0)
  569. mod->csumfunc.crc8 = cgw_csum_crc8_rel;
  570. else if (c->from_idx <= c->to_idx)
  571. mod->csumfunc.crc8 = cgw_csum_crc8_pos;
  572. else
  573. mod->csumfunc.crc8 = cgw_csum_crc8_neg;
  574. }
  575. if (tb[CGW_CS_XOR] &&
  576. nla_len(tb[CGW_CS_XOR]) == CGW_CS_XOR_LEN) {
  577. struct cgw_csum_xor *c = (struct cgw_csum_xor *)\
  578. nla_data(tb[CGW_CS_XOR]);
  579. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  580. c->result_idx);
  581. if (err)
  582. return err;
  583. nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
  584. CGW_CS_XOR_LEN);
  585. /*
  586. * select dedicated processing function to reduce
  587. * runtime operations in receive hot path.
  588. */
  589. if (c->from_idx < 0 || c->to_idx < 0 ||
  590. c->result_idx < 0)
  591. mod->csumfunc.xor = cgw_csum_xor_rel;
  592. else if (c->from_idx <= c->to_idx)
  593. mod->csumfunc.xor = cgw_csum_xor_pos;
  594. else
  595. mod->csumfunc.xor = cgw_csum_xor_neg;
  596. }
  597. }
  598. if (gwtype == CGW_TYPE_CAN_CAN) {
  599. /* check CGW_TYPE_CAN_CAN specific attributes */
  600. struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
  601. memset(ccgw, 0, sizeof(*ccgw));
  602. /* check for can_filter in attributes */
  603. if (tb[CGW_FILTER] &&
  604. nla_len(tb[CGW_FILTER]) == sizeof(struct can_filter))
  605. nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
  606. sizeof(struct can_filter));
  607. err = -ENODEV;
  608. /* specifying two interfaces is mandatory */
  609. if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
  610. return err;
  611. if (nla_len(tb[CGW_SRC_IF]) == sizeof(u32))
  612. nla_memcpy(&ccgw->src_idx, tb[CGW_SRC_IF],
  613. sizeof(u32));
  614. if (nla_len(tb[CGW_DST_IF]) == sizeof(u32))
  615. nla_memcpy(&ccgw->dst_idx, tb[CGW_DST_IF],
  616. sizeof(u32));
  617. /* both indices set to 0 for flushing all routing entries */
  618. if (!ccgw->src_idx && !ccgw->dst_idx)
  619. return 0;
  620. /* only one index set to 0 is an error */
  621. if (!ccgw->src_idx || !ccgw->dst_idx)
  622. return err;
  623. }
  624. /* add the checks for other gwtypes here */
  625. return 0;
  626. }
  627. static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
  628. void *arg)
  629. {
  630. struct rtcanmsg *r;
  631. struct cgw_job *gwj;
  632. int err = 0;
  633. if (nlmsg_len(nlh) < sizeof(*r))
  634. return -EINVAL;
  635. r = nlmsg_data(nlh);
  636. if (r->can_family != AF_CAN)
  637. return -EPFNOSUPPORT;
  638. /* so far we only support CAN -> CAN routings */
  639. if (r->gwtype != CGW_TYPE_CAN_CAN)
  640. return -EINVAL;
  641. gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
  642. if (!gwj)
  643. return -ENOMEM;
  644. gwj->handled_frames = 0;
  645. gwj->dropped_frames = 0;
  646. gwj->flags = r->flags;
  647. gwj->gwtype = r->gwtype;
  648. err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
  649. if (err < 0)
  650. goto out;
  651. err = -ENODEV;
  652. /* ifindex == 0 is not allowed for job creation */
  653. if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
  654. goto out;
  655. gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
  656. if (!gwj->src.dev)
  657. goto out;
  658. /* check for CAN netdev not using header_ops - see gw_rcv() */
  659. if (gwj->src.dev->type != ARPHRD_CAN || gwj->src.dev->header_ops)
  660. goto put_src_out;
  661. gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
  662. if (!gwj->dst.dev)
  663. goto put_src_out;
  664. /* check for CAN netdev not using header_ops - see gw_rcv() */
  665. if (gwj->dst.dev->type != ARPHRD_CAN || gwj->dst.dev->header_ops)
  666. goto put_src_dst_out;
  667. ASSERT_RTNL();
  668. err = cgw_register_filter(gwj);
  669. if (!err)
  670. hlist_add_head_rcu(&gwj->list, &cgw_list);
  671. put_src_dst_out:
  672. dev_put(gwj->dst.dev);
  673. put_src_out:
  674. dev_put(gwj->src.dev);
  675. out:
  676. if (err)
  677. kmem_cache_free(cgw_cache, gwj);
  678. return err;
  679. }
  680. static void cgw_remove_all_jobs(void)
  681. {
  682. struct cgw_job *gwj = NULL;
  683. struct hlist_node *n, *nx;
  684. ASSERT_RTNL();
  685. hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
  686. hlist_del(&gwj->list);
  687. cgw_unregister_filter(gwj);
  688. kmem_cache_free(cgw_cache, gwj);
  689. }
  690. }
  691. static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  692. {
  693. struct cgw_job *gwj = NULL;
  694. struct hlist_node *n, *nx;
  695. struct rtcanmsg *r;
  696. struct cf_mod mod;
  697. struct can_can_gw ccgw;
  698. int err = 0;
  699. if (nlmsg_len(nlh) < sizeof(*r))
  700. return -EINVAL;
  701. r = nlmsg_data(nlh);
  702. if (r->can_family != AF_CAN)
  703. return -EPFNOSUPPORT;
  704. /* so far we only support CAN -> CAN routings */
  705. if (r->gwtype != CGW_TYPE_CAN_CAN)
  706. return -EINVAL;
  707. err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw);
  708. if (err < 0)
  709. return err;
  710. /* two interface indices both set to 0 => remove all entries */
  711. if (!ccgw.src_idx && !ccgw.dst_idx) {
  712. cgw_remove_all_jobs();
  713. return 0;
  714. }
  715. err = -EINVAL;
  716. ASSERT_RTNL();
  717. /* remove only the first matching entry */
  718. hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
  719. if (gwj->flags != r->flags)
  720. continue;
  721. if (memcmp(&gwj->mod, &mod, sizeof(mod)))
  722. continue;
  723. /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
  724. if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
  725. continue;
  726. hlist_del(&gwj->list);
  727. cgw_unregister_filter(gwj);
  728. kmem_cache_free(cgw_cache, gwj);
  729. err = 0;
  730. break;
  731. }
  732. return err;
  733. }
  734. static __init int cgw_module_init(void)
  735. {
  736. printk(banner);
  737. cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
  738. 0, 0, NULL);
  739. if (!cgw_cache)
  740. return -ENOMEM;
  741. /* set notifier */
  742. notifier.notifier_call = cgw_notifier;
  743. register_netdevice_notifier(&notifier);
  744. if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs, NULL)) {
  745. unregister_netdevice_notifier(&notifier);
  746. kmem_cache_destroy(cgw_cache);
  747. return -ENOBUFS;
  748. }
  749. /* Only the first call to __rtnl_register can fail */
  750. __rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL, NULL);
  751. __rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL, NULL);
  752. return 0;
  753. }
  754. static __exit void cgw_module_exit(void)
  755. {
  756. rtnl_unregister_all(PF_CAN);
  757. unregister_netdevice_notifier(&notifier);
  758. rtnl_lock();
  759. cgw_remove_all_jobs();
  760. rtnl_unlock();
  761. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  762. kmem_cache_destroy(cgw_cache);
  763. }
  764. module_init(cgw_module_init);
  765. module_exit(cgw_module_exit);