gw.c 24 KB

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