gw.c 26 KB

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