gw.c 25 KB

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