gw.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
  3. *
  4. * Copyright (c) 2011 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. */
  41. #include <linux/module.h>
  42. #include <linux/init.h>
  43. #include <linux/types.h>
  44. #include <linux/list.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/rcupdate.h>
  47. #include <linux/rculist.h>
  48. #include <linux/net.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/if_arp.h>
  51. #include <linux/skbuff.h>
  52. #include <linux/can.h>
  53. #include <linux/can/core.h>
  54. #include <linux/can/skb.h>
  55. #include <linux/can/gw.h>
  56. #include <net/rtnetlink.h>
  57. #include <net/net_namespace.h>
  58. #include <net/sock.h>
  59. #define CAN_GW_VERSION "20101209"
  60. static __initconst const char banner[] =
  61. KERN_INFO "can: netlink gateway (rev " CAN_GW_VERSION ")\n";
  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");
  66. static HLIST_HEAD(cgw_list);
  67. static struct notifier_block notifier;
  68. static struct kmem_cache *cgw_cache __read_mostly;
  69. /* structure that contains the (on-the-fly) CAN frame modifications */
  70. struct cf_mod {
  71. struct {
  72. struct can_frame and;
  73. struct can_frame or;
  74. struct can_frame xor;
  75. struct can_frame set;
  76. } modframe;
  77. struct {
  78. u8 and;
  79. u8 or;
  80. u8 xor;
  81. u8 set;
  82. } modtype;
  83. void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
  84. struct cf_mod *mod);
  85. /* CAN frame checksum calculation after CAN frame modifications */
  86. struct {
  87. struct cgw_csum_xor xor;
  88. struct cgw_csum_crc8 crc8;
  89. } csum;
  90. struct {
  91. void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
  92. void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
  93. } csumfunc;
  94. };
  95. /*
  96. * So far we just support CAN -> CAN routing and frame modifications.
  97. *
  98. * The internal can_can_gw structure contains data and attributes for
  99. * a CAN -> CAN gateway job.
  100. */
  101. struct can_can_gw {
  102. struct can_filter filter;
  103. int src_idx;
  104. int dst_idx;
  105. };
  106. /* list entry for CAN gateways jobs */
  107. struct cgw_job {
  108. struct hlist_node list;
  109. struct rcu_head rcu;
  110. u32 handled_frames;
  111. u32 dropped_frames;
  112. struct cf_mod mod;
  113. union {
  114. /* CAN frame data source */
  115. struct net_device *dev;
  116. } src;
  117. union {
  118. /* CAN frame data destination */
  119. struct net_device *dev;
  120. } dst;
  121. union {
  122. struct can_can_gw ccgw;
  123. /* tbc */
  124. };
  125. u8 gwtype;
  126. u16 flags;
  127. };
  128. /* modification functions that are invoked in the hot path in can_can_gw_rcv */
  129. #define MODFUNC(func, op) static void func(struct can_frame *cf, \
  130. struct cf_mod *mod) { op ; }
  131. MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
  132. MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc)
  133. MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
  134. MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
  135. MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
  136. MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
  137. MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
  138. MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
  139. MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
  140. MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
  141. MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
  142. MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
  143. static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
  144. {
  145. /*
  146. * Copy the struct members separately to ensure that no uninitialized
  147. * data are copied in the 3 bytes hole of the struct. This is needed
  148. * to make easy compares of the data in the struct cf_mod.
  149. */
  150. dst->can_id = src->can_id;
  151. dst->can_dlc = src->can_dlc;
  152. *(u64 *)dst->data = *(u64 *)src->data;
  153. }
  154. static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re)
  155. {
  156. /*
  157. * absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
  158. * relative to received dlc -1 .. -8 :
  159. * e.g. for received dlc = 8
  160. * -1 => index = 7 (data[7])
  161. * -3 => index = 5 (data[5])
  162. * -8 => index = 0 (data[0])
  163. */
  164. if (fr > -9 && fr < 8 &&
  165. to > -9 && to < 8 &&
  166. re > -9 && re < 8)
  167. return 0;
  168. else
  169. return -EINVAL;
  170. }
  171. static inline int calc_idx(int idx, int rx_dlc)
  172. {
  173. if (idx < 0)
  174. return rx_dlc + idx;
  175. else
  176. return idx;
  177. }
  178. static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
  179. {
  180. int from = calc_idx(xor->from_idx, cf->can_dlc);
  181. int to = calc_idx(xor->to_idx, cf->can_dlc);
  182. int res = calc_idx(xor->result_idx, cf->can_dlc);
  183. u8 val = xor->init_xor_val;
  184. int i;
  185. if (from < 0 || to < 0 || res < 0)
  186. return;
  187. if (from <= to) {
  188. for (i = from; i <= to; i++)
  189. val ^= cf->data[i];
  190. } else {
  191. for (i = from; i >= to; i--)
  192. val ^= cf->data[i];
  193. }
  194. cf->data[res] = val;
  195. }
  196. static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
  197. {
  198. u8 val = xor->init_xor_val;
  199. int i;
  200. for (i = xor->from_idx; i <= xor->to_idx; i++)
  201. val ^= cf->data[i];
  202. cf->data[xor->result_idx] = val;
  203. }
  204. static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
  205. {
  206. u8 val = xor->init_xor_val;
  207. int i;
  208. for (i = xor->from_idx; i >= xor->to_idx; i--)
  209. val ^= cf->data[i];
  210. cf->data[xor->result_idx] = val;
  211. }
  212. static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  213. {
  214. int from = calc_idx(crc8->from_idx, cf->can_dlc);
  215. int to = calc_idx(crc8->to_idx, cf->can_dlc);
  216. int res = calc_idx(crc8->result_idx, cf->can_dlc);
  217. u8 crc = crc8->init_crc_val;
  218. int i;
  219. if (from < 0 || to < 0 || res < 0)
  220. return;
  221. if (from <= to) {
  222. for (i = crc8->from_idx; i <= crc8->to_idx; i++)
  223. crc = crc8->crctab[crc^cf->data[i]];
  224. } else {
  225. for (i = crc8->from_idx; i >= crc8->to_idx; i--)
  226. crc = crc8->crctab[crc^cf->data[i]];
  227. }
  228. switch (crc8->profile) {
  229. case CGW_CRC8PRF_1U8:
  230. crc = crc8->crctab[crc^crc8->profile_data[0]];
  231. break;
  232. case CGW_CRC8PRF_16U8:
  233. crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  234. break;
  235. case CGW_CRC8PRF_SFFID_XOR:
  236. crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  237. (cf->can_id >> 8 & 0xFF)];
  238. break;
  239. }
  240. cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  241. }
  242. static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  243. {
  244. u8 crc = crc8->init_crc_val;
  245. int i;
  246. for (i = crc8->from_idx; i <= crc8->to_idx; i++)
  247. crc = crc8->crctab[crc^cf->data[i]];
  248. switch (crc8->profile) {
  249. case CGW_CRC8PRF_1U8:
  250. crc = crc8->crctab[crc^crc8->profile_data[0]];
  251. break;
  252. case CGW_CRC8PRF_16U8:
  253. crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  254. break;
  255. case CGW_CRC8PRF_SFFID_XOR:
  256. crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  257. (cf->can_id >> 8 & 0xFF)];
  258. break;
  259. }
  260. cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  261. }
  262. static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  263. {
  264. u8 crc = crc8->init_crc_val;
  265. int i;
  266. for (i = crc8->from_idx; i >= crc8->to_idx; i--)
  267. crc = crc8->crctab[crc^cf->data[i]];
  268. switch (crc8->profile) {
  269. case CGW_CRC8PRF_1U8:
  270. crc = crc8->crctab[crc^crc8->profile_data[0]];
  271. break;
  272. case CGW_CRC8PRF_16U8:
  273. crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  274. break;
  275. case CGW_CRC8PRF_SFFID_XOR:
  276. crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  277. (cf->can_id >> 8 & 0xFF)];
  278. break;
  279. }
  280. cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  281. }
  282. /* the receive & process & send function */
  283. static void can_can_gw_rcv(struct sk_buff *skb, void *data)
  284. {
  285. struct cgw_job *gwj = (struct cgw_job *)data;
  286. struct can_frame *cf;
  287. struct sk_buff *nskb;
  288. int modidx = 0;
  289. /* do not handle already routed frames - see comment below */
  290. if (skb_mac_header_was_set(skb))
  291. return;
  292. if (!(gwj->dst.dev->flags & IFF_UP)) {
  293. gwj->dropped_frames++;
  294. return;
  295. }
  296. /* is sending the skb back to the incoming interface not allowed? */
  297. if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) &&
  298. skb_headroom(skb) == sizeof(struct can_skb_priv) &&
  299. (((struct can_skb_priv *)(skb->head))->ifindex ==
  300. gwj->dst.dev->ifindex))
  301. return;
  302. /*
  303. * clone the given skb, which has not been done in can_rcv()
  304. *
  305. * When there is at least one modification function activated,
  306. * we need to copy the skb as we want to modify skb->data.
  307. */
  308. if (gwj->mod.modfunc[0])
  309. nskb = skb_copy(skb, GFP_ATOMIC);
  310. else
  311. nskb = skb_clone(skb, GFP_ATOMIC);
  312. if (!nskb) {
  313. gwj->dropped_frames++;
  314. return;
  315. }
  316. /*
  317. * Mark routed frames by setting some mac header length which is
  318. * not relevant for the CAN frames located in the skb->data section.
  319. *
  320. * As dev->header_ops is not set in CAN netdevices no one is ever
  321. * accessing the various header offsets in the CAN skbuffs anyway.
  322. * E.g. using the packet socket to read CAN frames is still working.
  323. */
  324. skb_set_mac_header(nskb, 8);
  325. nskb->dev = gwj->dst.dev;
  326. /* pointer to modifiable CAN frame */
  327. cf = (struct can_frame *)nskb->data;
  328. /* perform preprocessed modification functions if there are any */
  329. while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
  330. (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
  331. /* check for checksum updates when the CAN frame has been modified */
  332. if (modidx) {
  333. if (gwj->mod.csumfunc.crc8)
  334. (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
  335. if (gwj->mod.csumfunc.xor)
  336. (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
  337. }
  338. /* clear the skb timestamp if not configured the other way */
  339. if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
  340. nskb->tstamp.tv64 = 0;
  341. /* send to netdevice */
  342. if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
  343. gwj->dropped_frames++;
  344. else
  345. gwj->handled_frames++;
  346. }
  347. static inline int cgw_register_filter(struct cgw_job *gwj)
  348. {
  349. return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id,
  350. gwj->ccgw.filter.can_mask, can_can_gw_rcv,
  351. gwj, "gw");
  352. }
  353. static inline void cgw_unregister_filter(struct cgw_job *gwj)
  354. {
  355. can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id,
  356. gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
  357. }
  358. static int cgw_notifier(struct notifier_block *nb,
  359. unsigned long msg, void *data)
  360. {
  361. struct net_device *dev = (struct net_device *)data;
  362. if (!net_eq(dev_net(dev), &init_net))
  363. return NOTIFY_DONE;
  364. if (dev->type != ARPHRD_CAN)
  365. return NOTIFY_DONE;
  366. if (msg == NETDEV_UNREGISTER) {
  367. struct cgw_job *gwj = NULL;
  368. struct hlist_node *n, *nx;
  369. ASSERT_RTNL();
  370. hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
  371. if (gwj->src.dev == dev || gwj->dst.dev == dev) {
  372. hlist_del(&gwj->list);
  373. cgw_unregister_filter(gwj);
  374. kfree(gwj);
  375. }
  376. }
  377. }
  378. return NOTIFY_DONE;
  379. }
  380. static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
  381. u32 pid, u32 seq, int flags)
  382. {
  383. struct cgw_frame_mod mb;
  384. struct rtcanmsg *rtcan;
  385. struct nlmsghdr *nlh;
  386. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags);
  387. if (!nlh)
  388. return -EMSGSIZE;
  389. rtcan = nlmsg_data(nlh);
  390. rtcan->can_family = AF_CAN;
  391. rtcan->gwtype = gwj->gwtype;
  392. rtcan->flags = gwj->flags;
  393. /* add statistics if available */
  394. if (gwj->handled_frames) {
  395. if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
  396. goto cancel;
  397. }
  398. if (gwj->dropped_frames) {
  399. if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
  400. goto cancel;
  401. }
  402. /* check non default settings of attributes */
  403. if (gwj->mod.modtype.and) {
  404. memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
  405. mb.modtype = gwj->mod.modtype.and;
  406. if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
  407. goto cancel;
  408. }
  409. if (gwj->mod.modtype.or) {
  410. memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
  411. mb.modtype = gwj->mod.modtype.or;
  412. if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
  413. goto cancel;
  414. }
  415. if (gwj->mod.modtype.xor) {
  416. memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
  417. mb.modtype = gwj->mod.modtype.xor;
  418. if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
  419. goto cancel;
  420. }
  421. if (gwj->mod.modtype.set) {
  422. memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
  423. mb.modtype = gwj->mod.modtype.set;
  424. if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
  425. goto cancel;
  426. }
  427. if (gwj->mod.csumfunc.crc8) {
  428. if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
  429. &gwj->mod.csum.crc8) < 0)
  430. goto cancel;
  431. }
  432. if (gwj->mod.csumfunc.xor) {
  433. if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
  434. &gwj->mod.csum.xor) < 0)
  435. goto cancel;
  436. }
  437. if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
  438. if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
  439. if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
  440. &gwj->ccgw.filter) < 0)
  441. goto cancel;
  442. }
  443. if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
  444. goto cancel;
  445. if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
  446. goto cancel;
  447. }
  448. return nlmsg_end(skb, nlh);
  449. cancel:
  450. nlmsg_cancel(skb, nlh);
  451. return -EMSGSIZE;
  452. }
  453. /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
  454. static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
  455. {
  456. struct cgw_job *gwj = NULL;
  457. struct hlist_node *n;
  458. int idx = 0;
  459. int s_idx = cb->args[0];
  460. rcu_read_lock();
  461. hlist_for_each_entry_rcu(gwj, n, &cgw_list, list) {
  462. if (idx < s_idx)
  463. goto cont;
  464. if (cgw_put_job(skb, gwj, RTM_NEWROUTE, NETLINK_CB(cb->skb).portid,
  465. cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0)
  466. break;
  467. cont:
  468. idx++;
  469. }
  470. rcu_read_unlock();
  471. cb->args[0] = idx;
  472. return skb->len;
  473. }
  474. static const struct nla_policy cgw_policy[CGW_MAX+1] = {
  475. [CGW_MOD_AND] = { .len = sizeof(struct cgw_frame_mod) },
  476. [CGW_MOD_OR] = { .len = sizeof(struct cgw_frame_mod) },
  477. [CGW_MOD_XOR] = { .len = sizeof(struct cgw_frame_mod) },
  478. [CGW_MOD_SET] = { .len = sizeof(struct cgw_frame_mod) },
  479. [CGW_CS_XOR] = { .len = sizeof(struct cgw_csum_xor) },
  480. [CGW_CS_CRC8] = { .len = sizeof(struct cgw_csum_crc8) },
  481. [CGW_SRC_IF] = { .type = NLA_U32 },
  482. [CGW_DST_IF] = { .type = NLA_U32 },
  483. [CGW_FILTER] = { .len = sizeof(struct can_filter) },
  484. };
  485. /* check for common and gwtype specific attributes */
  486. static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
  487. u8 gwtype, void *gwtypeattr)
  488. {
  489. struct nlattr *tb[CGW_MAX+1];
  490. struct cgw_frame_mod mb;
  491. int modidx = 0;
  492. int err = 0;
  493. /* initialize modification & checksum data space */
  494. memset(mod, 0, sizeof(*mod));
  495. err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX,
  496. cgw_policy);
  497. if (err < 0)
  498. return err;
  499. /* check for AND/OR/XOR/SET modifications */
  500. if (tb[CGW_MOD_AND]) {
  501. nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
  502. canframecpy(&mod->modframe.and, &mb.cf);
  503. mod->modtype.and = mb.modtype;
  504. if (mb.modtype & CGW_MOD_ID)
  505. mod->modfunc[modidx++] = mod_and_id;
  506. if (mb.modtype & CGW_MOD_DLC)
  507. mod->modfunc[modidx++] = mod_and_dlc;
  508. if (mb.modtype & CGW_MOD_DATA)
  509. mod->modfunc[modidx++] = mod_and_data;
  510. }
  511. if (tb[CGW_MOD_OR]) {
  512. nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
  513. canframecpy(&mod->modframe.or, &mb.cf);
  514. mod->modtype.or = mb.modtype;
  515. if (mb.modtype & CGW_MOD_ID)
  516. mod->modfunc[modidx++] = mod_or_id;
  517. if (mb.modtype & CGW_MOD_DLC)
  518. mod->modfunc[modidx++] = mod_or_dlc;
  519. if (mb.modtype & CGW_MOD_DATA)
  520. mod->modfunc[modidx++] = mod_or_data;
  521. }
  522. if (tb[CGW_MOD_XOR]) {
  523. nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
  524. canframecpy(&mod->modframe.xor, &mb.cf);
  525. mod->modtype.xor = mb.modtype;
  526. if (mb.modtype & CGW_MOD_ID)
  527. mod->modfunc[modidx++] = mod_xor_id;
  528. if (mb.modtype & CGW_MOD_DLC)
  529. mod->modfunc[modidx++] = mod_xor_dlc;
  530. if (mb.modtype & CGW_MOD_DATA)
  531. mod->modfunc[modidx++] = mod_xor_data;
  532. }
  533. if (tb[CGW_MOD_SET]) {
  534. nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
  535. canframecpy(&mod->modframe.set, &mb.cf);
  536. mod->modtype.set = mb.modtype;
  537. if (mb.modtype & CGW_MOD_ID)
  538. mod->modfunc[modidx++] = mod_set_id;
  539. if (mb.modtype & CGW_MOD_DLC)
  540. mod->modfunc[modidx++] = mod_set_dlc;
  541. if (mb.modtype & CGW_MOD_DATA)
  542. mod->modfunc[modidx++] = mod_set_data;
  543. }
  544. /* check for checksum operations after CAN frame modifications */
  545. if (modidx) {
  546. if (tb[CGW_CS_CRC8]) {
  547. struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]);
  548. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  549. c->result_idx);
  550. if (err)
  551. return err;
  552. nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
  553. CGW_CS_CRC8_LEN);
  554. /*
  555. * select dedicated processing function to reduce
  556. * runtime operations in receive hot path.
  557. */
  558. if (c->from_idx < 0 || c->to_idx < 0 ||
  559. c->result_idx < 0)
  560. mod->csumfunc.crc8 = cgw_csum_crc8_rel;
  561. else if (c->from_idx <= c->to_idx)
  562. mod->csumfunc.crc8 = cgw_csum_crc8_pos;
  563. else
  564. mod->csumfunc.crc8 = cgw_csum_crc8_neg;
  565. }
  566. if (tb[CGW_CS_XOR]) {
  567. struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]);
  568. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  569. c->result_idx);
  570. if (err)
  571. return err;
  572. nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
  573. CGW_CS_XOR_LEN);
  574. /*
  575. * select dedicated processing function to reduce
  576. * runtime operations in receive hot path.
  577. */
  578. if (c->from_idx < 0 || c->to_idx < 0 ||
  579. c->result_idx < 0)
  580. mod->csumfunc.xor = cgw_csum_xor_rel;
  581. else if (c->from_idx <= c->to_idx)
  582. mod->csumfunc.xor = cgw_csum_xor_pos;
  583. else
  584. mod->csumfunc.xor = cgw_csum_xor_neg;
  585. }
  586. }
  587. if (gwtype == CGW_TYPE_CAN_CAN) {
  588. /* check CGW_TYPE_CAN_CAN specific attributes */
  589. struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
  590. memset(ccgw, 0, sizeof(*ccgw));
  591. /* check for can_filter in attributes */
  592. if (tb[CGW_FILTER])
  593. nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
  594. sizeof(struct can_filter));
  595. err = -ENODEV;
  596. /* specifying two interfaces is mandatory */
  597. if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
  598. return err;
  599. ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]);
  600. ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]);
  601. /* both indices set to 0 for flushing all routing entries */
  602. if (!ccgw->src_idx && !ccgw->dst_idx)
  603. return 0;
  604. /* only one index set to 0 is an error */
  605. if (!ccgw->src_idx || !ccgw->dst_idx)
  606. return err;
  607. }
  608. /* add the checks for other gwtypes here */
  609. return 0;
  610. }
  611. static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
  612. void *arg)
  613. {
  614. struct rtcanmsg *r;
  615. struct cgw_job *gwj;
  616. int err = 0;
  617. if (!capable(CAP_NET_ADMIN))
  618. return -EPERM;
  619. if (nlmsg_len(nlh) < sizeof(*r))
  620. return -EINVAL;
  621. r = nlmsg_data(nlh);
  622. if (r->can_family != AF_CAN)
  623. return -EPFNOSUPPORT;
  624. /* so far we only support CAN -> CAN routings */
  625. if (r->gwtype != CGW_TYPE_CAN_CAN)
  626. return -EINVAL;
  627. gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
  628. if (!gwj)
  629. return -ENOMEM;
  630. gwj->handled_frames = 0;
  631. gwj->dropped_frames = 0;
  632. gwj->flags = r->flags;
  633. gwj->gwtype = r->gwtype;
  634. err = cgw_parse_attr(nlh, &gwj->mod, CGW_TYPE_CAN_CAN, &gwj->ccgw);
  635. if (err < 0)
  636. goto out;
  637. err = -ENODEV;
  638. /* ifindex == 0 is not allowed for job creation */
  639. if (!gwj->ccgw.src_idx || !gwj->ccgw.dst_idx)
  640. goto out;
  641. gwj->src.dev = dev_get_by_index(&init_net, gwj->ccgw.src_idx);
  642. if (!gwj->src.dev)
  643. goto out;
  644. /* check for CAN netdev not using header_ops - see gw_rcv() */
  645. if (gwj->src.dev->type != ARPHRD_CAN || gwj->src.dev->header_ops)
  646. goto put_src_out;
  647. gwj->dst.dev = dev_get_by_index(&init_net, gwj->ccgw.dst_idx);
  648. if (!gwj->dst.dev)
  649. goto put_src_out;
  650. /* check for CAN netdev not using header_ops - see gw_rcv() */
  651. if (gwj->dst.dev->type != ARPHRD_CAN || gwj->dst.dev->header_ops)
  652. goto put_src_dst_out;
  653. ASSERT_RTNL();
  654. err = cgw_register_filter(gwj);
  655. if (!err)
  656. hlist_add_head_rcu(&gwj->list, &cgw_list);
  657. put_src_dst_out:
  658. dev_put(gwj->dst.dev);
  659. put_src_out:
  660. dev_put(gwj->src.dev);
  661. out:
  662. if (err)
  663. kmem_cache_free(cgw_cache, gwj);
  664. return err;
  665. }
  666. static void cgw_remove_all_jobs(void)
  667. {
  668. struct cgw_job *gwj = NULL;
  669. struct hlist_node *n, *nx;
  670. ASSERT_RTNL();
  671. hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
  672. hlist_del(&gwj->list);
  673. cgw_unregister_filter(gwj);
  674. kfree(gwj);
  675. }
  676. }
  677. static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  678. {
  679. struct cgw_job *gwj = NULL;
  680. struct hlist_node *n, *nx;
  681. struct rtcanmsg *r;
  682. struct cf_mod mod;
  683. struct can_can_gw ccgw;
  684. int err = 0;
  685. if (!capable(CAP_NET_ADMIN))
  686. return -EPERM;
  687. if (nlmsg_len(nlh) < sizeof(*r))
  688. return -EINVAL;
  689. r = nlmsg_data(nlh);
  690. if (r->can_family != AF_CAN)
  691. return -EPFNOSUPPORT;
  692. /* so far we only support CAN -> CAN routings */
  693. if (r->gwtype != CGW_TYPE_CAN_CAN)
  694. return -EINVAL;
  695. err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw);
  696. if (err < 0)
  697. return err;
  698. /* two interface indices both set to 0 => remove all entries */
  699. if (!ccgw.src_idx && !ccgw.dst_idx) {
  700. cgw_remove_all_jobs();
  701. return 0;
  702. }
  703. err = -EINVAL;
  704. ASSERT_RTNL();
  705. /* remove only the first matching entry */
  706. hlist_for_each_entry_safe(gwj, n, nx, &cgw_list, list) {
  707. if (gwj->flags != r->flags)
  708. continue;
  709. if (memcmp(&gwj->mod, &mod, sizeof(mod)))
  710. continue;
  711. /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
  712. if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
  713. continue;
  714. hlist_del(&gwj->list);
  715. cgw_unregister_filter(gwj);
  716. kfree(gwj);
  717. err = 0;
  718. break;
  719. }
  720. return err;
  721. }
  722. static __init int cgw_module_init(void)
  723. {
  724. printk(banner);
  725. cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
  726. 0, 0, NULL);
  727. if (!cgw_cache)
  728. return -ENOMEM;
  729. /* set notifier */
  730. notifier.notifier_call = cgw_notifier;
  731. register_netdevice_notifier(&notifier);
  732. if (__rtnl_register(PF_CAN, RTM_GETROUTE, NULL, cgw_dump_jobs, NULL)) {
  733. unregister_netdevice_notifier(&notifier);
  734. kmem_cache_destroy(cgw_cache);
  735. return -ENOBUFS;
  736. }
  737. /* Only the first call to __rtnl_register can fail */
  738. __rtnl_register(PF_CAN, RTM_NEWROUTE, cgw_create_job, NULL, NULL);
  739. __rtnl_register(PF_CAN, RTM_DELROUTE, cgw_remove_job, NULL, NULL);
  740. return 0;
  741. }
  742. static __exit void cgw_module_exit(void)
  743. {
  744. rtnl_unregister_all(PF_CAN);
  745. unregister_netdevice_notifier(&notifier);
  746. rtnl_lock();
  747. cgw_remove_all_jobs();
  748. rtnl_unlock();
  749. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  750. kmem_cache_destroy(cgw_cache);
  751. }
  752. module_init(cgw_module_init);
  753. module_exit(cgw_module_exit);