gw.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. * gw.c - CAN frame Gateway/Router/Bridge with netlink interface
  3. *
  4. * Copyright (c) 2017 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. */
  41. #include <linux/module.h>
  42. #include <linux/init.h>
  43. #include <linux/types.h>
  44. #include <linux/kernel.h>
  45. #include <linux/list.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/rcupdate.h>
  48. #include <linux/rculist.h>
  49. #include <linux/net.h>
  50. #include <linux/netdevice.h>
  51. #include <linux/if_arp.h>
  52. #include <linux/skbuff.h>
  53. #include <linux/can.h>
  54. #include <linux/can/core.h>
  55. #include <linux/can/skb.h>
  56. #include <linux/can/gw.h>
  57. #include <net/rtnetlink.h>
  58. #include <net/net_namespace.h>
  59. #include <net/sock.h>
  60. #define CAN_GW_VERSION "20170425"
  61. #define CAN_GW_NAME "can-gw"
  62. MODULE_DESCRIPTION("PF_CAN netlink gateway");
  63. MODULE_LICENSE("Dual BSD/GPL");
  64. MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
  65. MODULE_ALIAS(CAN_GW_NAME);
  66. #define CGW_MIN_HOPS 1
  67. #define CGW_MAX_HOPS 6
  68. #define CGW_DEFAULT_HOPS 1
  69. static unsigned int max_hops __read_mostly = CGW_DEFAULT_HOPS;
  70. module_param(max_hops, uint, 0444);
  71. MODULE_PARM_DESC(max_hops,
  72. "maximum " CAN_GW_NAME " routing hops for CAN frames "
  73. "(valid values: " __stringify(CGW_MIN_HOPS) "-"
  74. __stringify(CGW_MAX_HOPS) " hops, "
  75. "default: " __stringify(CGW_DEFAULT_HOPS) ")");
  76. static struct notifier_block notifier;
  77. static struct kmem_cache *cgw_cache __read_mostly;
  78. /* structure that contains the (on-the-fly) CAN frame modifications */
  79. struct cf_mod {
  80. struct {
  81. struct can_frame and;
  82. struct can_frame or;
  83. struct can_frame xor;
  84. struct can_frame set;
  85. } modframe;
  86. struct {
  87. u8 and;
  88. u8 or;
  89. u8 xor;
  90. u8 set;
  91. } modtype;
  92. void (*modfunc[MAX_MODFUNCTIONS])(struct can_frame *cf,
  93. struct cf_mod *mod);
  94. /* CAN frame checksum calculation after CAN frame modifications */
  95. struct {
  96. struct cgw_csum_xor xor;
  97. struct cgw_csum_crc8 crc8;
  98. } csum;
  99. struct {
  100. void (*xor)(struct can_frame *cf, struct cgw_csum_xor *xor);
  101. void (*crc8)(struct can_frame *cf, struct cgw_csum_crc8 *crc8);
  102. } csumfunc;
  103. u32 uid;
  104. };
  105. /*
  106. * So far we just support CAN -> CAN routing and frame modifications.
  107. *
  108. * The internal can_can_gw structure contains data and attributes for
  109. * a CAN -> CAN gateway job.
  110. */
  111. struct can_can_gw {
  112. struct can_filter filter;
  113. int src_idx;
  114. int dst_idx;
  115. };
  116. /* list entry for CAN gateways jobs */
  117. struct cgw_job {
  118. struct hlist_node list;
  119. struct rcu_head rcu;
  120. u32 handled_frames;
  121. u32 dropped_frames;
  122. u32 deleted_frames;
  123. struct cf_mod mod;
  124. union {
  125. /* CAN frame data source */
  126. struct net_device *dev;
  127. } src;
  128. union {
  129. /* CAN frame data destination */
  130. struct net_device *dev;
  131. } dst;
  132. union {
  133. struct can_can_gw ccgw;
  134. /* tbc */
  135. };
  136. u8 gwtype;
  137. u8 limit_hops;
  138. u16 flags;
  139. };
  140. /* modification functions that are invoked in the hot path in can_can_gw_rcv */
  141. #define MODFUNC(func, op) static void func(struct can_frame *cf, \
  142. struct cf_mod *mod) { op ; }
  143. MODFUNC(mod_and_id, cf->can_id &= mod->modframe.and.can_id)
  144. MODFUNC(mod_and_dlc, cf->can_dlc &= mod->modframe.and.can_dlc)
  145. MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
  146. MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
  147. MODFUNC(mod_or_dlc, cf->can_dlc |= mod->modframe.or.can_dlc)
  148. MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
  149. MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
  150. MODFUNC(mod_xor_dlc, cf->can_dlc ^= mod->modframe.xor.can_dlc)
  151. MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
  152. MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
  153. MODFUNC(mod_set_dlc, cf->can_dlc = mod->modframe.set.can_dlc)
  154. MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
  155. static inline void canframecpy(struct can_frame *dst, struct can_frame *src)
  156. {
  157. /*
  158. * Copy the struct members separately to ensure that no uninitialized
  159. * data are copied in the 3 bytes hole of the struct. This is needed
  160. * to make easy compares of the data in the struct cf_mod.
  161. */
  162. dst->can_id = src->can_id;
  163. dst->can_dlc = src->can_dlc;
  164. *(u64 *)dst->data = *(u64 *)src->data;
  165. }
  166. static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re)
  167. {
  168. /*
  169. * absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
  170. * relative to received dlc -1 .. -8 :
  171. * e.g. for received dlc = 8
  172. * -1 => index = 7 (data[7])
  173. * -3 => index = 5 (data[5])
  174. * -8 => index = 0 (data[0])
  175. */
  176. if (fr > -9 && fr < 8 &&
  177. to > -9 && to < 8 &&
  178. re > -9 && re < 8)
  179. return 0;
  180. else
  181. return -EINVAL;
  182. }
  183. static inline int calc_idx(int idx, int rx_dlc)
  184. {
  185. if (idx < 0)
  186. return rx_dlc + idx;
  187. else
  188. return idx;
  189. }
  190. static void cgw_csum_xor_rel(struct can_frame *cf, struct cgw_csum_xor *xor)
  191. {
  192. int from = calc_idx(xor->from_idx, cf->can_dlc);
  193. int to = calc_idx(xor->to_idx, cf->can_dlc);
  194. int res = calc_idx(xor->result_idx, cf->can_dlc);
  195. u8 val = xor->init_xor_val;
  196. int i;
  197. if (from < 0 || to < 0 || res < 0)
  198. return;
  199. if (from <= to) {
  200. for (i = from; i <= to; i++)
  201. val ^= cf->data[i];
  202. } else {
  203. for (i = from; i >= to; i--)
  204. val ^= cf->data[i];
  205. }
  206. cf->data[res] = val;
  207. }
  208. static void cgw_csum_xor_pos(struct can_frame *cf, struct cgw_csum_xor *xor)
  209. {
  210. u8 val = xor->init_xor_val;
  211. int i;
  212. for (i = xor->from_idx; i <= xor->to_idx; i++)
  213. val ^= cf->data[i];
  214. cf->data[xor->result_idx] = val;
  215. }
  216. static void cgw_csum_xor_neg(struct can_frame *cf, struct cgw_csum_xor *xor)
  217. {
  218. u8 val = xor->init_xor_val;
  219. int i;
  220. for (i = xor->from_idx; i >= xor->to_idx; i--)
  221. val ^= cf->data[i];
  222. cf->data[xor->result_idx] = val;
  223. }
  224. static void cgw_csum_crc8_rel(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  225. {
  226. int from = calc_idx(crc8->from_idx, cf->can_dlc);
  227. int to = calc_idx(crc8->to_idx, cf->can_dlc);
  228. int res = calc_idx(crc8->result_idx, cf->can_dlc);
  229. u8 crc = crc8->init_crc_val;
  230. int i;
  231. if (from < 0 || to < 0 || res < 0)
  232. return;
  233. if (from <= to) {
  234. for (i = crc8->from_idx; i <= crc8->to_idx; i++)
  235. crc = crc8->crctab[crc^cf->data[i]];
  236. } else {
  237. for (i = crc8->from_idx; i >= crc8->to_idx; i--)
  238. crc = crc8->crctab[crc^cf->data[i]];
  239. }
  240. switch (crc8->profile) {
  241. case CGW_CRC8PRF_1U8:
  242. crc = crc8->crctab[crc^crc8->profile_data[0]];
  243. break;
  244. case CGW_CRC8PRF_16U8:
  245. crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  246. break;
  247. case CGW_CRC8PRF_SFFID_XOR:
  248. crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  249. (cf->can_id >> 8 & 0xFF)];
  250. break;
  251. }
  252. cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  253. }
  254. static void cgw_csum_crc8_pos(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  255. {
  256. u8 crc = crc8->init_crc_val;
  257. int i;
  258. for (i = crc8->from_idx; i <= crc8->to_idx; i++)
  259. crc = crc8->crctab[crc^cf->data[i]];
  260. switch (crc8->profile) {
  261. case CGW_CRC8PRF_1U8:
  262. crc = crc8->crctab[crc^crc8->profile_data[0]];
  263. break;
  264. case CGW_CRC8PRF_16U8:
  265. crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  266. break;
  267. case CGW_CRC8PRF_SFFID_XOR:
  268. crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  269. (cf->can_id >> 8 & 0xFF)];
  270. break;
  271. }
  272. cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  273. }
  274. static void cgw_csum_crc8_neg(struct can_frame *cf, struct cgw_csum_crc8 *crc8)
  275. {
  276. u8 crc = crc8->init_crc_val;
  277. int i;
  278. for (i = crc8->from_idx; i >= crc8->to_idx; i--)
  279. crc = crc8->crctab[crc^cf->data[i]];
  280. switch (crc8->profile) {
  281. case CGW_CRC8PRF_1U8:
  282. crc = crc8->crctab[crc^crc8->profile_data[0]];
  283. break;
  284. case CGW_CRC8PRF_16U8:
  285. crc = crc8->crctab[crc^crc8->profile_data[cf->data[1] & 0xF]];
  286. break;
  287. case CGW_CRC8PRF_SFFID_XOR:
  288. crc = crc8->crctab[crc^(cf->can_id & 0xFF)^
  289. (cf->can_id >> 8 & 0xFF)];
  290. break;
  291. }
  292. cf->data[crc8->result_idx] = crc^crc8->final_xor_val;
  293. }
  294. /* the receive & process & send function */
  295. static void can_can_gw_rcv(struct sk_buff *skb, void *data)
  296. {
  297. struct cgw_job *gwj = (struct cgw_job *)data;
  298. struct can_frame *cf;
  299. struct sk_buff *nskb;
  300. int modidx = 0;
  301. /*
  302. * Do not handle CAN frames routed more than 'max_hops' times.
  303. * In general we should never catch this delimiter which is intended
  304. * to cover a misconfiguration protection (e.g. circular CAN routes).
  305. *
  306. * The Controller Area Network controllers only accept CAN frames with
  307. * correct CRCs - which are not visible in the controller registers.
  308. * According to skbuff.h documentation the csum_start element for IP
  309. * checksums is undefined/unused when ip_summed == CHECKSUM_UNNECESSARY.
  310. * Only CAN skbs can be processed here which already have this property.
  311. */
  312. #define cgw_hops(skb) ((skb)->csum_start)
  313. BUG_ON(skb->ip_summed != CHECKSUM_UNNECESSARY);
  314. if (cgw_hops(skb) >= max_hops) {
  315. /* indicate deleted frames due to misconfiguration */
  316. gwj->deleted_frames++;
  317. return;
  318. }
  319. if (!(gwj->dst.dev->flags & IFF_UP)) {
  320. gwj->dropped_frames++;
  321. return;
  322. }
  323. /* is sending the skb back to the incoming interface not allowed? */
  324. if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) &&
  325. can_skb_prv(skb)->ifindex == gwj->dst.dev->ifindex)
  326. return;
  327. /*
  328. * clone the given skb, which has not been done in can_rcv()
  329. *
  330. * When there is at least one modification function activated,
  331. * we need to copy the skb as we want to modify skb->data.
  332. */
  333. if (gwj->mod.modfunc[0])
  334. nskb = skb_copy(skb, GFP_ATOMIC);
  335. else
  336. nskb = skb_clone(skb, GFP_ATOMIC);
  337. if (!nskb) {
  338. gwj->dropped_frames++;
  339. return;
  340. }
  341. /* put the incremented hop counter in the cloned skb */
  342. cgw_hops(nskb) = cgw_hops(skb) + 1;
  343. /* first processing of this CAN frame -> adjust to private hop limit */
  344. if (gwj->limit_hops && cgw_hops(nskb) == 1)
  345. cgw_hops(nskb) = max_hops - gwj->limit_hops + 1;
  346. nskb->dev = gwj->dst.dev;
  347. /* pointer to modifiable CAN frame */
  348. cf = (struct can_frame *)nskb->data;
  349. /* perform preprocessed modification functions if there are any */
  350. while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
  351. (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
  352. /* Has the CAN frame been modified? */
  353. if (modidx) {
  354. /* get available space for the processed CAN frame type */
  355. int max_len = nskb->len - offsetof(struct can_frame, data);
  356. /* dlc may have changed, make sure it fits to the CAN frame */
  357. if (cf->can_dlc > max_len)
  358. goto out_delete;
  359. /* check for checksum updates in classic CAN length only */
  360. if (gwj->mod.csumfunc.crc8) {
  361. if (cf->can_dlc > 8)
  362. goto out_delete;
  363. (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
  364. }
  365. if (gwj->mod.csumfunc.xor) {
  366. if (cf->can_dlc > 8)
  367. goto out_delete;
  368. (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
  369. }
  370. }
  371. /* clear the skb timestamp if not configured the other way */
  372. if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
  373. nskb->tstamp = 0;
  374. /* send to netdevice */
  375. if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
  376. gwj->dropped_frames++;
  377. else
  378. gwj->handled_frames++;
  379. return;
  380. out_delete:
  381. /* delete frame due to misconfiguration */
  382. gwj->deleted_frames++;
  383. kfree_skb(nskb);
  384. return;
  385. }
  386. static inline int cgw_register_filter(struct net *net, struct cgw_job *gwj)
  387. {
  388. return can_rx_register(net, gwj->src.dev, gwj->ccgw.filter.can_id,
  389. gwj->ccgw.filter.can_mask, can_can_gw_rcv,
  390. gwj, "gw", NULL);
  391. }
  392. static inline void cgw_unregister_filter(struct net *net, struct cgw_job *gwj)
  393. {
  394. can_rx_unregister(net, gwj->src.dev, gwj->ccgw.filter.can_id,
  395. gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
  396. }
  397. static int cgw_notifier(struct notifier_block *nb,
  398. unsigned long msg, void *ptr)
  399. {
  400. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  401. struct net *net = dev_net(dev);
  402. if (dev->type != ARPHRD_CAN)
  403. return NOTIFY_DONE;
  404. if (msg == NETDEV_UNREGISTER) {
  405. struct cgw_job *gwj = NULL;
  406. struct hlist_node *nx;
  407. ASSERT_RTNL();
  408. hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
  409. if (gwj->src.dev == dev || gwj->dst.dev == dev) {
  410. hlist_del(&gwj->list);
  411. cgw_unregister_filter(net, gwj);
  412. kmem_cache_free(cgw_cache, gwj);
  413. }
  414. }
  415. }
  416. return NOTIFY_DONE;
  417. }
  418. static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
  419. u32 pid, u32 seq, int flags)
  420. {
  421. struct cgw_frame_mod mb;
  422. struct rtcanmsg *rtcan;
  423. struct nlmsghdr *nlh;
  424. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags);
  425. if (!nlh)
  426. return -EMSGSIZE;
  427. rtcan = nlmsg_data(nlh);
  428. rtcan->can_family = AF_CAN;
  429. rtcan->gwtype = gwj->gwtype;
  430. rtcan->flags = gwj->flags;
  431. /* add statistics if available */
  432. if (gwj->handled_frames) {
  433. if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
  434. goto cancel;
  435. }
  436. if (gwj->dropped_frames) {
  437. if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
  438. goto cancel;
  439. }
  440. if (gwj->deleted_frames) {
  441. if (nla_put_u32(skb, CGW_DELETED, gwj->deleted_frames) < 0)
  442. goto cancel;
  443. }
  444. /* check non default settings of attributes */
  445. if (gwj->limit_hops) {
  446. if (nla_put_u8(skb, CGW_LIM_HOPS, gwj->limit_hops) < 0)
  447. goto cancel;
  448. }
  449. if (gwj->mod.modtype.and) {
  450. memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
  451. mb.modtype = gwj->mod.modtype.and;
  452. if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
  453. goto cancel;
  454. }
  455. if (gwj->mod.modtype.or) {
  456. memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
  457. mb.modtype = gwj->mod.modtype.or;
  458. if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
  459. goto cancel;
  460. }
  461. if (gwj->mod.modtype.xor) {
  462. memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
  463. mb.modtype = gwj->mod.modtype.xor;
  464. if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
  465. goto cancel;
  466. }
  467. if (gwj->mod.modtype.set) {
  468. memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
  469. mb.modtype = gwj->mod.modtype.set;
  470. if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
  471. goto cancel;
  472. }
  473. if (gwj->mod.uid) {
  474. if (nla_put_u32(skb, CGW_MOD_UID, gwj->mod.uid) < 0)
  475. goto cancel;
  476. }
  477. if (gwj->mod.csumfunc.crc8) {
  478. if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
  479. &gwj->mod.csum.crc8) < 0)
  480. goto cancel;
  481. }
  482. if (gwj->mod.csumfunc.xor) {
  483. if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
  484. &gwj->mod.csum.xor) < 0)
  485. goto cancel;
  486. }
  487. if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
  488. if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
  489. if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
  490. &gwj->ccgw.filter) < 0)
  491. goto cancel;
  492. }
  493. if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
  494. goto cancel;
  495. if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
  496. goto cancel;
  497. }
  498. nlmsg_end(skb, nlh);
  499. return 0;
  500. cancel:
  501. nlmsg_cancel(skb, nlh);
  502. return -EMSGSIZE;
  503. }
  504. /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
  505. static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
  506. {
  507. struct net *net = sock_net(skb->sk);
  508. struct cgw_job *gwj = NULL;
  509. int idx = 0;
  510. int s_idx = cb->args[0];
  511. rcu_read_lock();
  512. hlist_for_each_entry_rcu(gwj, &net->can.cgw_list, list) {
  513. if (idx < s_idx)
  514. goto cont;
  515. if (cgw_put_job(skb, gwj, RTM_NEWROUTE, NETLINK_CB(cb->skb).portid,
  516. cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0)
  517. break;
  518. cont:
  519. idx++;
  520. }
  521. rcu_read_unlock();
  522. cb->args[0] = idx;
  523. return skb->len;
  524. }
  525. static const struct nla_policy cgw_policy[CGW_MAX+1] = {
  526. [CGW_MOD_AND] = { .len = sizeof(struct cgw_frame_mod) },
  527. [CGW_MOD_OR] = { .len = sizeof(struct cgw_frame_mod) },
  528. [CGW_MOD_XOR] = { .len = sizeof(struct cgw_frame_mod) },
  529. [CGW_MOD_SET] = { .len = sizeof(struct cgw_frame_mod) },
  530. [CGW_CS_XOR] = { .len = sizeof(struct cgw_csum_xor) },
  531. [CGW_CS_CRC8] = { .len = sizeof(struct cgw_csum_crc8) },
  532. [CGW_SRC_IF] = { .type = NLA_U32 },
  533. [CGW_DST_IF] = { .type = NLA_U32 },
  534. [CGW_FILTER] = { .len = sizeof(struct can_filter) },
  535. [CGW_LIM_HOPS] = { .type = NLA_U8 },
  536. [CGW_MOD_UID] = { .type = NLA_U32 },
  537. };
  538. /* check for common and gwtype specific attributes */
  539. static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
  540. u8 gwtype, void *gwtypeattr, u8 *limhops)
  541. {
  542. struct nlattr *tb[CGW_MAX+1];
  543. struct cgw_frame_mod mb;
  544. int modidx = 0;
  545. int err = 0;
  546. /* initialize modification & checksum data space */
  547. memset(mod, 0, sizeof(*mod));
  548. err = nlmsg_parse(nlh, sizeof(struct rtcanmsg), tb, CGW_MAX,
  549. cgw_policy, NULL);
  550. if (err < 0)
  551. return err;
  552. if (tb[CGW_LIM_HOPS]) {
  553. *limhops = nla_get_u8(tb[CGW_LIM_HOPS]);
  554. if (*limhops < 1 || *limhops > max_hops)
  555. return -EINVAL;
  556. }
  557. /* check for AND/OR/XOR/SET modifications */
  558. if (tb[CGW_MOD_AND]) {
  559. nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
  560. canframecpy(&mod->modframe.and, &mb.cf);
  561. mod->modtype.and = mb.modtype;
  562. if (mb.modtype & CGW_MOD_ID)
  563. mod->modfunc[modidx++] = mod_and_id;
  564. if (mb.modtype & CGW_MOD_DLC)
  565. mod->modfunc[modidx++] = mod_and_dlc;
  566. if (mb.modtype & CGW_MOD_DATA)
  567. mod->modfunc[modidx++] = mod_and_data;
  568. }
  569. if (tb[CGW_MOD_OR]) {
  570. nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
  571. canframecpy(&mod->modframe.or, &mb.cf);
  572. mod->modtype.or = mb.modtype;
  573. if (mb.modtype & CGW_MOD_ID)
  574. mod->modfunc[modidx++] = mod_or_id;
  575. if (mb.modtype & CGW_MOD_DLC)
  576. mod->modfunc[modidx++] = mod_or_dlc;
  577. if (mb.modtype & CGW_MOD_DATA)
  578. mod->modfunc[modidx++] = mod_or_data;
  579. }
  580. if (tb[CGW_MOD_XOR]) {
  581. nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
  582. canframecpy(&mod->modframe.xor, &mb.cf);
  583. mod->modtype.xor = mb.modtype;
  584. if (mb.modtype & CGW_MOD_ID)
  585. mod->modfunc[modidx++] = mod_xor_id;
  586. if (mb.modtype & CGW_MOD_DLC)
  587. mod->modfunc[modidx++] = mod_xor_dlc;
  588. if (mb.modtype & CGW_MOD_DATA)
  589. mod->modfunc[modidx++] = mod_xor_data;
  590. }
  591. if (tb[CGW_MOD_SET]) {
  592. nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
  593. canframecpy(&mod->modframe.set, &mb.cf);
  594. mod->modtype.set = mb.modtype;
  595. if (mb.modtype & CGW_MOD_ID)
  596. mod->modfunc[modidx++] = mod_set_id;
  597. if (mb.modtype & CGW_MOD_DLC)
  598. mod->modfunc[modidx++] = mod_set_dlc;
  599. if (mb.modtype & CGW_MOD_DATA)
  600. mod->modfunc[modidx++] = mod_set_data;
  601. }
  602. /* check for checksum operations after CAN frame modifications */
  603. if (modidx) {
  604. if (tb[CGW_CS_CRC8]) {
  605. struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]);
  606. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  607. c->result_idx);
  608. if (err)
  609. return err;
  610. nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
  611. CGW_CS_CRC8_LEN);
  612. /*
  613. * select dedicated processing function to reduce
  614. * runtime operations in receive hot path.
  615. */
  616. if (c->from_idx < 0 || c->to_idx < 0 ||
  617. c->result_idx < 0)
  618. mod->csumfunc.crc8 = cgw_csum_crc8_rel;
  619. else if (c->from_idx <= c->to_idx)
  620. mod->csumfunc.crc8 = cgw_csum_crc8_pos;
  621. else
  622. mod->csumfunc.crc8 = cgw_csum_crc8_neg;
  623. }
  624. if (tb[CGW_CS_XOR]) {
  625. struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]);
  626. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  627. c->result_idx);
  628. if (err)
  629. return err;
  630. nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
  631. CGW_CS_XOR_LEN);
  632. /*
  633. * select dedicated processing function to reduce
  634. * runtime operations in receive hot path.
  635. */
  636. if (c->from_idx < 0 || c->to_idx < 0 ||
  637. c->result_idx < 0)
  638. mod->csumfunc.xor = cgw_csum_xor_rel;
  639. else if (c->from_idx <= c->to_idx)
  640. mod->csumfunc.xor = cgw_csum_xor_pos;
  641. else
  642. mod->csumfunc.xor = cgw_csum_xor_neg;
  643. }
  644. if (tb[CGW_MOD_UID]) {
  645. nla_memcpy(&mod->uid, tb[CGW_MOD_UID], sizeof(u32));
  646. }
  647. }
  648. if (gwtype == CGW_TYPE_CAN_CAN) {
  649. /* check CGW_TYPE_CAN_CAN specific attributes */
  650. struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
  651. memset(ccgw, 0, sizeof(*ccgw));
  652. /* check for can_filter in attributes */
  653. if (tb[CGW_FILTER])
  654. nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
  655. sizeof(struct can_filter));
  656. err = -ENODEV;
  657. /* specifying two interfaces is mandatory */
  658. if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
  659. return err;
  660. ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]);
  661. ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]);
  662. /* both indices set to 0 for flushing all routing entries */
  663. if (!ccgw->src_idx && !ccgw->dst_idx)
  664. return 0;
  665. /* only one index set to 0 is an error */
  666. if (!ccgw->src_idx || !ccgw->dst_idx)
  667. return err;
  668. }
  669. /* add the checks for other gwtypes here */
  670. return 0;
  671. }
  672. static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
  673. struct netlink_ext_ack *extack)
  674. {
  675. struct net *net = sock_net(skb->sk);
  676. struct rtcanmsg *r;
  677. struct cgw_job *gwj;
  678. struct cf_mod mod;
  679. struct can_can_gw ccgw;
  680. u8 limhops = 0;
  681. int err = 0;
  682. if (!netlink_capable(skb, CAP_NET_ADMIN))
  683. return -EPERM;
  684. if (nlmsg_len(nlh) < sizeof(*r))
  685. return -EINVAL;
  686. r = nlmsg_data(nlh);
  687. if (r->can_family != AF_CAN)
  688. return -EPFNOSUPPORT;
  689. /* so far we only support CAN -> CAN routings */
  690. if (r->gwtype != CGW_TYPE_CAN_CAN)
  691. return -EINVAL;
  692. err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
  693. if (err < 0)
  694. return err;
  695. if (mod.uid) {
  696. ASSERT_RTNL();
  697. /* check for updating an existing job with identical uid */
  698. hlist_for_each_entry(gwj, &net->can.cgw_list, list) {
  699. if (gwj->mod.uid != mod.uid)
  700. continue;
  701. /* interfaces & filters must be identical */
  702. if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
  703. return -EINVAL;
  704. /* update modifications with disabled softirq & quit */
  705. local_bh_disable();
  706. memcpy(&gwj->mod, &mod, sizeof(mod));
  707. local_bh_enable();
  708. return 0;
  709. }
  710. }
  711. /* ifindex == 0 is not allowed for job creation */
  712. if (!ccgw.src_idx || !ccgw.dst_idx)
  713. return -ENODEV;
  714. gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
  715. if (!gwj)
  716. return -ENOMEM;
  717. gwj->handled_frames = 0;
  718. gwj->dropped_frames = 0;
  719. gwj->deleted_frames = 0;
  720. gwj->flags = r->flags;
  721. gwj->gwtype = r->gwtype;
  722. gwj->limit_hops = limhops;
  723. /* insert already parsed information */
  724. memcpy(&gwj->mod, &mod, sizeof(mod));
  725. memcpy(&gwj->ccgw, &ccgw, sizeof(ccgw));
  726. err = -ENODEV;
  727. gwj->src.dev = __dev_get_by_index(net, gwj->ccgw.src_idx);
  728. if (!gwj->src.dev)
  729. goto out;
  730. if (gwj->src.dev->type != ARPHRD_CAN)
  731. goto out;
  732. gwj->dst.dev = __dev_get_by_index(net, gwj->ccgw.dst_idx);
  733. if (!gwj->dst.dev)
  734. goto out;
  735. if (gwj->dst.dev->type != ARPHRD_CAN)
  736. goto out;
  737. ASSERT_RTNL();
  738. err = cgw_register_filter(net, gwj);
  739. if (!err)
  740. hlist_add_head_rcu(&gwj->list, &net->can.cgw_list);
  741. out:
  742. if (err)
  743. kmem_cache_free(cgw_cache, gwj);
  744. return err;
  745. }
  746. static void cgw_remove_all_jobs(struct net *net)
  747. {
  748. struct cgw_job *gwj = NULL;
  749. struct hlist_node *nx;
  750. ASSERT_RTNL();
  751. hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
  752. hlist_del(&gwj->list);
  753. cgw_unregister_filter(net, gwj);
  754. kmem_cache_free(cgw_cache, gwj);
  755. }
  756. }
  757. static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh,
  758. struct netlink_ext_ack *extack)
  759. {
  760. struct net *net = sock_net(skb->sk);
  761. struct cgw_job *gwj = NULL;
  762. struct hlist_node *nx;
  763. struct rtcanmsg *r;
  764. struct cf_mod mod;
  765. struct can_can_gw ccgw;
  766. u8 limhops = 0;
  767. int err = 0;
  768. if (!netlink_capable(skb, CAP_NET_ADMIN))
  769. return -EPERM;
  770. if (nlmsg_len(nlh) < sizeof(*r))
  771. return -EINVAL;
  772. r = nlmsg_data(nlh);
  773. if (r->can_family != AF_CAN)
  774. return -EPFNOSUPPORT;
  775. /* so far we only support CAN -> CAN routings */
  776. if (r->gwtype != CGW_TYPE_CAN_CAN)
  777. return -EINVAL;
  778. err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
  779. if (err < 0)
  780. return err;
  781. /* two interface indices both set to 0 => remove all entries */
  782. if (!ccgw.src_idx && !ccgw.dst_idx) {
  783. cgw_remove_all_jobs(net);
  784. return 0;
  785. }
  786. err = -EINVAL;
  787. ASSERT_RTNL();
  788. /* remove only the first matching entry */
  789. hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
  790. if (gwj->flags != r->flags)
  791. continue;
  792. if (gwj->limit_hops != limhops)
  793. continue;
  794. /* we have a match when uid is enabled and identical */
  795. if (gwj->mod.uid || mod.uid) {
  796. if (gwj->mod.uid != mod.uid)
  797. continue;
  798. } else {
  799. /* no uid => check for identical modifications */
  800. if (memcmp(&gwj->mod, &mod, sizeof(mod)))
  801. continue;
  802. }
  803. /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
  804. if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
  805. continue;
  806. hlist_del(&gwj->list);
  807. cgw_unregister_filter(net, gwj);
  808. kmem_cache_free(cgw_cache, gwj);
  809. err = 0;
  810. break;
  811. }
  812. return err;
  813. }
  814. static int __net_init cangw_pernet_init(struct net *net)
  815. {
  816. INIT_HLIST_HEAD(&net->can.cgw_list);
  817. return 0;
  818. }
  819. static void __net_exit cangw_pernet_exit(struct net *net)
  820. {
  821. rtnl_lock();
  822. cgw_remove_all_jobs(net);
  823. rtnl_unlock();
  824. }
  825. static struct pernet_operations cangw_pernet_ops = {
  826. .init = cangw_pernet_init,
  827. .exit = cangw_pernet_exit,
  828. };
  829. static __init int cgw_module_init(void)
  830. {
  831. int ret;
  832. /* sanitize given module parameter */
  833. max_hops = clamp_t(unsigned int, max_hops, CGW_MIN_HOPS, CGW_MAX_HOPS);
  834. pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
  835. max_hops);
  836. ret = register_pernet_subsys(&cangw_pernet_ops);
  837. if (ret)
  838. return ret;
  839. ret = -ENOMEM;
  840. cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
  841. 0, 0, NULL);
  842. if (!cgw_cache)
  843. goto out_cache_create;
  844. /* set notifier */
  845. notifier.notifier_call = cgw_notifier;
  846. ret = register_netdevice_notifier(&notifier);
  847. if (ret)
  848. goto out_register_notifier;
  849. ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
  850. NULL, cgw_dump_jobs, 0);
  851. if (ret)
  852. goto out_rtnl_register1;
  853. ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
  854. cgw_create_job, NULL, 0);
  855. if (ret)
  856. goto out_rtnl_register2;
  857. ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
  858. cgw_remove_job, NULL, 0);
  859. if (ret)
  860. goto out_rtnl_register3;
  861. return 0;
  862. out_rtnl_register3:
  863. rtnl_unregister(PF_CAN, RTM_NEWROUTE);
  864. out_rtnl_register2:
  865. rtnl_unregister(PF_CAN, RTM_GETROUTE);
  866. out_rtnl_register1:
  867. unregister_netdevice_notifier(&notifier);
  868. out_register_notifier:
  869. kmem_cache_destroy(cgw_cache);
  870. out_cache_create:
  871. unregister_pernet_subsys(&cangw_pernet_ops);
  872. return ret;
  873. }
  874. static __exit void cgw_module_exit(void)
  875. {
  876. rtnl_unregister_all(PF_CAN);
  877. unregister_netdevice_notifier(&notifier);
  878. unregister_pernet_subsys(&cangw_pernet_ops);
  879. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  880. kmem_cache_destroy(cgw_cache);
  881. }
  882. module_init(cgw_module_init);
  883. module_exit(cgw_module_exit);