gw.c 26 KB

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