gw.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. /* gw.c - CAN frame Gateway/Router/Bridge with netlink interface
  3. *
  4. * Copyright (c) 2019 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_NAME "can-gw"
  61. MODULE_DESCRIPTION("PF_CAN netlink gateway");
  62. MODULE_LICENSE("Dual BSD/GPL");
  63. MODULE_AUTHOR("Oliver Hartkopp <[email protected]>");
  64. MODULE_ALIAS(CAN_GW_NAME);
  65. #define CGW_MIN_HOPS 1
  66. #define CGW_MAX_HOPS 6
  67. #define CGW_DEFAULT_HOPS 1
  68. static unsigned int max_hops __read_mostly = CGW_DEFAULT_HOPS;
  69. module_param(max_hops, uint, 0444);
  70. MODULE_PARM_DESC(max_hops,
  71. "maximum " CAN_GW_NAME " routing hops for CAN frames "
  72. "(valid values: " __stringify(CGW_MIN_HOPS) "-"
  73. __stringify(CGW_MAX_HOPS) " hops, "
  74. "default: " __stringify(CGW_DEFAULT_HOPS) ")");
  75. static struct notifier_block notifier;
  76. static struct kmem_cache *cgw_cache __read_mostly;
  77. /* structure that contains the (on-the-fly) CAN frame modifications */
  78. struct cf_mod {
  79. struct {
  80. struct canfd_frame and;
  81. struct canfd_frame or;
  82. struct canfd_frame xor;
  83. struct canfd_frame set;
  84. } modframe;
  85. struct {
  86. u8 and;
  87. u8 or;
  88. u8 xor;
  89. u8 set;
  90. } modtype;
  91. void (*modfunc[MAX_MODFUNCTIONS])(struct canfd_frame *cf,
  92. struct cf_mod *mod);
  93. /* CAN frame checksum calculation after CAN frame modifications */
  94. struct {
  95. struct cgw_csum_xor xor;
  96. struct cgw_csum_crc8 crc8;
  97. } csum;
  98. struct {
  99. void (*xor)(struct canfd_frame *cf,
  100. struct cgw_csum_xor *xor);
  101. void (*crc8)(struct canfd_frame *cf,
  102. struct cgw_csum_crc8 *crc8);
  103. } csumfunc;
  104. u32 uid;
  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 canfd_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_len, cf->len &= mod->modframe.and.len)
  145. MODFUNC(mod_and_flags, cf->flags &= mod->modframe.and.flags)
  146. MODFUNC(mod_and_data, *(u64 *)cf->data &= *(u64 *)mod->modframe.and.data)
  147. MODFUNC(mod_or_id, cf->can_id |= mod->modframe.or.can_id)
  148. MODFUNC(mod_or_len, cf->len |= mod->modframe.or.len)
  149. MODFUNC(mod_or_flags, cf->flags |= mod->modframe.or.flags)
  150. MODFUNC(mod_or_data, *(u64 *)cf->data |= *(u64 *)mod->modframe.or.data)
  151. MODFUNC(mod_xor_id, cf->can_id ^= mod->modframe.xor.can_id)
  152. MODFUNC(mod_xor_len, cf->len ^= mod->modframe.xor.len)
  153. MODFUNC(mod_xor_flags, cf->flags ^= mod->modframe.xor.flags)
  154. MODFUNC(mod_xor_data, *(u64 *)cf->data ^= *(u64 *)mod->modframe.xor.data)
  155. MODFUNC(mod_set_id, cf->can_id = mod->modframe.set.can_id)
  156. MODFUNC(mod_set_len, cf->len = mod->modframe.set.len)
  157. MODFUNC(mod_set_flags, cf->flags = mod->modframe.set.flags)
  158. MODFUNC(mod_set_data, *(u64 *)cf->data = *(u64 *)mod->modframe.set.data)
  159. static void mod_and_fddata(struct canfd_frame *cf, struct cf_mod *mod)
  160. {
  161. int i;
  162. for (i = 0; i < CANFD_MAX_DLEN; i += 8)
  163. *(u64 *)(cf->data + i) &= *(u64 *)(mod->modframe.and.data + i);
  164. }
  165. static void mod_or_fddata(struct canfd_frame *cf, struct cf_mod *mod)
  166. {
  167. int i;
  168. for (i = 0; i < CANFD_MAX_DLEN; i += 8)
  169. *(u64 *)(cf->data + i) |= *(u64 *)(mod->modframe.or.data + i);
  170. }
  171. static void mod_xor_fddata(struct canfd_frame *cf, struct cf_mod *mod)
  172. {
  173. int i;
  174. for (i = 0; i < CANFD_MAX_DLEN; i += 8)
  175. *(u64 *)(cf->data + i) ^= *(u64 *)(mod->modframe.xor.data + i);
  176. }
  177. static void mod_set_fddata(struct canfd_frame *cf, struct cf_mod *mod)
  178. {
  179. memcpy(cf->data, mod->modframe.set.data, CANFD_MAX_DLEN);
  180. }
  181. /* retrieve valid CC DLC value and store it into 'len' */
  182. static void mod_retrieve_ccdlc(struct canfd_frame *cf)
  183. {
  184. struct can_frame *ccf = (struct can_frame *)cf;
  185. /* len8_dlc is only valid if len == CAN_MAX_DLEN */
  186. if (ccf->len != CAN_MAX_DLEN)
  187. return;
  188. /* do we have a valid len8_dlc value from 9 .. 15 ? */
  189. if (ccf->len8_dlc > CAN_MAX_DLEN && ccf->len8_dlc <= CAN_MAX_RAW_DLC)
  190. ccf->len = ccf->len8_dlc;
  191. }
  192. /* convert valid CC DLC value in 'len' into struct can_frame elements */
  193. static void mod_store_ccdlc(struct canfd_frame *cf)
  194. {
  195. struct can_frame *ccf = (struct can_frame *)cf;
  196. /* clear potential leftovers */
  197. ccf->len8_dlc = 0;
  198. /* plain data length 0 .. 8 - that was easy */
  199. if (ccf->len <= CAN_MAX_DLEN)
  200. return;
  201. /* potentially broken values are caught in can_can_gw_rcv() */
  202. if (ccf->len > CAN_MAX_RAW_DLC)
  203. return;
  204. /* we have a valid dlc value from 9 .. 15 in ccf->len */
  205. ccf->len8_dlc = ccf->len;
  206. ccf->len = CAN_MAX_DLEN;
  207. }
  208. static void mod_and_ccdlc(struct canfd_frame *cf, struct cf_mod *mod)
  209. {
  210. mod_retrieve_ccdlc(cf);
  211. mod_and_len(cf, mod);
  212. mod_store_ccdlc(cf);
  213. }
  214. static void mod_or_ccdlc(struct canfd_frame *cf, struct cf_mod *mod)
  215. {
  216. mod_retrieve_ccdlc(cf);
  217. mod_or_len(cf, mod);
  218. mod_store_ccdlc(cf);
  219. }
  220. static void mod_xor_ccdlc(struct canfd_frame *cf, struct cf_mod *mod)
  221. {
  222. mod_retrieve_ccdlc(cf);
  223. mod_xor_len(cf, mod);
  224. mod_store_ccdlc(cf);
  225. }
  226. static void mod_set_ccdlc(struct canfd_frame *cf, struct cf_mod *mod)
  227. {
  228. mod_set_len(cf, mod);
  229. mod_store_ccdlc(cf);
  230. }
  231. static void canframecpy(struct canfd_frame *dst, struct can_frame *src)
  232. {
  233. /* Copy the struct members separately to ensure that no uninitialized
  234. * data are copied in the 3 bytes hole of the struct. This is needed
  235. * to make easy compares of the data in the struct cf_mod.
  236. */
  237. dst->can_id = src->can_id;
  238. dst->len = src->len;
  239. *(u64 *)dst->data = *(u64 *)src->data;
  240. }
  241. static void canfdframecpy(struct canfd_frame *dst, struct canfd_frame *src)
  242. {
  243. /* Copy the struct members separately to ensure that no uninitialized
  244. * data are copied in the 2 bytes hole of the struct. This is needed
  245. * to make easy compares of the data in the struct cf_mod.
  246. */
  247. dst->can_id = src->can_id;
  248. dst->flags = src->flags;
  249. dst->len = src->len;
  250. memcpy(dst->data, src->data, CANFD_MAX_DLEN);
  251. }
  252. static int cgw_chk_csum_parms(s8 fr, s8 to, s8 re, struct rtcanmsg *r)
  253. {
  254. s8 dlen = CAN_MAX_DLEN;
  255. if (r->flags & CGW_FLAGS_CAN_FD)
  256. dlen = CANFD_MAX_DLEN;
  257. /* absolute dlc values 0 .. 7 => 0 .. 7, e.g. data [0]
  258. * relative to received dlc -1 .. -8 :
  259. * e.g. for received dlc = 8
  260. * -1 => index = 7 (data[7])
  261. * -3 => index = 5 (data[5])
  262. * -8 => index = 0 (data[0])
  263. */
  264. if (fr >= -dlen && fr < dlen &&
  265. to >= -dlen && to < dlen &&
  266. re >= -dlen && re < dlen)
  267. return 0;
  268. else
  269. return -EINVAL;
  270. }
  271. static inline int calc_idx(int idx, int rx_len)
  272. {
  273. if (idx < 0)
  274. return rx_len + idx;
  275. else
  276. return idx;
  277. }
  278. static void cgw_csum_xor_rel(struct canfd_frame *cf, struct cgw_csum_xor *xor)
  279. {
  280. int from = calc_idx(xor->from_idx, cf->len);
  281. int to = calc_idx(xor->to_idx, cf->len);
  282. int res = calc_idx(xor->result_idx, cf->len);
  283. u8 val = xor->init_xor_val;
  284. int i;
  285. if (from < 0 || to < 0 || res < 0)
  286. return;
  287. if (from <= to) {
  288. for (i = from; i <= to; i++)
  289. val ^= cf->data[i];
  290. } else {
  291. for (i = from; i >= to; i--)
  292. val ^= cf->data[i];
  293. }
  294. cf->data[res] = val;
  295. }
  296. static void cgw_csum_xor_pos(struct canfd_frame *cf, struct cgw_csum_xor *xor)
  297. {
  298. u8 val = xor->init_xor_val;
  299. int i;
  300. for (i = xor->from_idx; i <= xor->to_idx; i++)
  301. val ^= cf->data[i];
  302. cf->data[xor->result_idx] = val;
  303. }
  304. static void cgw_csum_xor_neg(struct canfd_frame *cf, struct cgw_csum_xor *xor)
  305. {
  306. u8 val = xor->init_xor_val;
  307. int i;
  308. for (i = xor->from_idx; i >= xor->to_idx; i--)
  309. val ^= cf->data[i];
  310. cf->data[xor->result_idx] = val;
  311. }
  312. static void cgw_csum_crc8_rel(struct canfd_frame *cf,
  313. struct cgw_csum_crc8 *crc8)
  314. {
  315. int from = calc_idx(crc8->from_idx, cf->len);
  316. int to = calc_idx(crc8->to_idx, cf->len);
  317. int res = calc_idx(crc8->result_idx, cf->len);
  318. u8 crc = crc8->init_crc_val;
  319. int i;
  320. if (from < 0 || to < 0 || res < 0)
  321. return;
  322. if (from <= to) {
  323. for (i = crc8->from_idx; i <= crc8->to_idx; i++)
  324. crc = crc8->crctab[crc ^ cf->data[i]];
  325. } else {
  326. for (i = crc8->from_idx; i >= crc8->to_idx; i--)
  327. crc = crc8->crctab[crc ^ cf->data[i]];
  328. }
  329. switch (crc8->profile) {
  330. case CGW_CRC8PRF_1U8:
  331. crc = crc8->crctab[crc ^ crc8->profile_data[0]];
  332. break;
  333. case CGW_CRC8PRF_16U8:
  334. crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]];
  335. break;
  336. case CGW_CRC8PRF_SFFID_XOR:
  337. crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^
  338. (cf->can_id >> 8 & 0xFF)];
  339. break;
  340. }
  341. cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val;
  342. }
  343. static void cgw_csum_crc8_pos(struct canfd_frame *cf,
  344. struct cgw_csum_crc8 *crc8)
  345. {
  346. u8 crc = crc8->init_crc_val;
  347. int i;
  348. for (i = crc8->from_idx; i <= crc8->to_idx; i++)
  349. crc = crc8->crctab[crc ^ cf->data[i]];
  350. switch (crc8->profile) {
  351. case CGW_CRC8PRF_1U8:
  352. crc = crc8->crctab[crc ^ crc8->profile_data[0]];
  353. break;
  354. case CGW_CRC8PRF_16U8:
  355. crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]];
  356. break;
  357. case CGW_CRC8PRF_SFFID_XOR:
  358. crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^
  359. (cf->can_id >> 8 & 0xFF)];
  360. break;
  361. }
  362. cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val;
  363. }
  364. static void cgw_csum_crc8_neg(struct canfd_frame *cf,
  365. struct cgw_csum_crc8 *crc8)
  366. {
  367. u8 crc = crc8->init_crc_val;
  368. int i;
  369. for (i = crc8->from_idx; i >= crc8->to_idx; i--)
  370. crc = crc8->crctab[crc ^ cf->data[i]];
  371. switch (crc8->profile) {
  372. case CGW_CRC8PRF_1U8:
  373. crc = crc8->crctab[crc ^ crc8->profile_data[0]];
  374. break;
  375. case CGW_CRC8PRF_16U8:
  376. crc = crc8->crctab[crc ^ crc8->profile_data[cf->data[1] & 0xF]];
  377. break;
  378. case CGW_CRC8PRF_SFFID_XOR:
  379. crc = crc8->crctab[crc ^ (cf->can_id & 0xFF) ^
  380. (cf->can_id >> 8 & 0xFF)];
  381. break;
  382. }
  383. cf->data[crc8->result_idx] = crc ^ crc8->final_xor_val;
  384. }
  385. /* the receive & process & send function */
  386. static void can_can_gw_rcv(struct sk_buff *skb, void *data)
  387. {
  388. struct cgw_job *gwj = (struct cgw_job *)data;
  389. struct canfd_frame *cf;
  390. struct sk_buff *nskb;
  391. int modidx = 0;
  392. /* process strictly Classic CAN or CAN FD frames */
  393. if (gwj->flags & CGW_FLAGS_CAN_FD) {
  394. if (!can_is_canfd_skb(skb))
  395. return;
  396. } else {
  397. if (!can_is_can_skb(skb))
  398. return;
  399. }
  400. /* Do not handle CAN frames routed more than 'max_hops' times.
  401. * In general we should never catch this delimiter which is intended
  402. * to cover a misconfiguration protection (e.g. circular CAN routes).
  403. *
  404. * The Controller Area Network controllers only accept CAN frames with
  405. * correct CRCs - which are not visible in the controller registers.
  406. * According to skbuff.h documentation the csum_start element for IP
  407. * checksums is undefined/unused when ip_summed == CHECKSUM_UNNECESSARY.
  408. * Only CAN skbs can be processed here which already have this property.
  409. */
  410. #define cgw_hops(skb) ((skb)->csum_start)
  411. BUG_ON(skb->ip_summed != CHECKSUM_UNNECESSARY);
  412. if (cgw_hops(skb) >= max_hops) {
  413. /* indicate deleted frames due to misconfiguration */
  414. gwj->deleted_frames++;
  415. return;
  416. }
  417. if (!(gwj->dst.dev->flags & IFF_UP)) {
  418. gwj->dropped_frames++;
  419. return;
  420. }
  421. /* is sending the skb back to the incoming interface not allowed? */
  422. if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) &&
  423. can_skb_prv(skb)->ifindex == gwj->dst.dev->ifindex)
  424. return;
  425. /* clone the given skb, which has not been done in can_rcv()
  426. *
  427. * When there is at least one modification function activated,
  428. * we need to copy the skb as we want to modify skb->data.
  429. */
  430. if (gwj->mod.modfunc[0])
  431. nskb = skb_copy(skb, GFP_ATOMIC);
  432. else
  433. nskb = skb_clone(skb, GFP_ATOMIC);
  434. if (!nskb) {
  435. gwj->dropped_frames++;
  436. return;
  437. }
  438. /* put the incremented hop counter in the cloned skb */
  439. cgw_hops(nskb) = cgw_hops(skb) + 1;
  440. /* first processing of this CAN frame -> adjust to private hop limit */
  441. if (gwj->limit_hops && cgw_hops(nskb) == 1)
  442. cgw_hops(nskb) = max_hops - gwj->limit_hops + 1;
  443. nskb->dev = gwj->dst.dev;
  444. /* pointer to modifiable CAN frame */
  445. cf = (struct canfd_frame *)nskb->data;
  446. /* perform preprocessed modification functions if there are any */
  447. while (modidx < MAX_MODFUNCTIONS && gwj->mod.modfunc[modidx])
  448. (*gwj->mod.modfunc[modidx++])(cf, &gwj->mod);
  449. /* Has the CAN frame been modified? */
  450. if (modidx) {
  451. /* get available space for the processed CAN frame type */
  452. int max_len = nskb->len - offsetof(struct canfd_frame, data);
  453. /* dlc may have changed, make sure it fits to the CAN frame */
  454. if (cf->len > max_len) {
  455. /* delete frame due to misconfiguration */
  456. gwj->deleted_frames++;
  457. kfree_skb(nskb);
  458. return;
  459. }
  460. /* check for checksum updates */
  461. if (gwj->mod.csumfunc.crc8)
  462. (*gwj->mod.csumfunc.crc8)(cf, &gwj->mod.csum.crc8);
  463. if (gwj->mod.csumfunc.xor)
  464. (*gwj->mod.csumfunc.xor)(cf, &gwj->mod.csum.xor);
  465. }
  466. /* clear the skb timestamp if not configured the other way */
  467. if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
  468. nskb->tstamp = 0;
  469. /* send to netdevice */
  470. if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
  471. gwj->dropped_frames++;
  472. else
  473. gwj->handled_frames++;
  474. }
  475. static inline int cgw_register_filter(struct net *net, struct cgw_job *gwj)
  476. {
  477. return can_rx_register(net, gwj->src.dev, gwj->ccgw.filter.can_id,
  478. gwj->ccgw.filter.can_mask, can_can_gw_rcv,
  479. gwj, "gw", NULL);
  480. }
  481. static inline void cgw_unregister_filter(struct net *net, struct cgw_job *gwj)
  482. {
  483. can_rx_unregister(net, gwj->src.dev, gwj->ccgw.filter.can_id,
  484. gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
  485. }
  486. static void cgw_job_free_rcu(struct rcu_head *rcu_head)
  487. {
  488. struct cgw_job *gwj = container_of(rcu_head, struct cgw_job, rcu);
  489. kmem_cache_free(cgw_cache, gwj);
  490. }
  491. static int cgw_notifier(struct notifier_block *nb,
  492. unsigned long msg, void *ptr)
  493. {
  494. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  495. struct net *net = dev_net(dev);
  496. if (dev->type != ARPHRD_CAN)
  497. return NOTIFY_DONE;
  498. if (msg == NETDEV_UNREGISTER) {
  499. struct cgw_job *gwj = NULL;
  500. struct hlist_node *nx;
  501. ASSERT_RTNL();
  502. hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
  503. if (gwj->src.dev == dev || gwj->dst.dev == dev) {
  504. hlist_del(&gwj->list);
  505. cgw_unregister_filter(net, gwj);
  506. call_rcu(&gwj->rcu, cgw_job_free_rcu);
  507. }
  508. }
  509. }
  510. return NOTIFY_DONE;
  511. }
  512. static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
  513. u32 pid, u32 seq, int flags)
  514. {
  515. struct rtcanmsg *rtcan;
  516. struct nlmsghdr *nlh;
  517. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags);
  518. if (!nlh)
  519. return -EMSGSIZE;
  520. rtcan = nlmsg_data(nlh);
  521. rtcan->can_family = AF_CAN;
  522. rtcan->gwtype = gwj->gwtype;
  523. rtcan->flags = gwj->flags;
  524. /* add statistics if available */
  525. if (gwj->handled_frames) {
  526. if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
  527. goto cancel;
  528. }
  529. if (gwj->dropped_frames) {
  530. if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
  531. goto cancel;
  532. }
  533. if (gwj->deleted_frames) {
  534. if (nla_put_u32(skb, CGW_DELETED, gwj->deleted_frames) < 0)
  535. goto cancel;
  536. }
  537. /* check non default settings of attributes */
  538. if (gwj->limit_hops) {
  539. if (nla_put_u8(skb, CGW_LIM_HOPS, gwj->limit_hops) < 0)
  540. goto cancel;
  541. }
  542. if (gwj->flags & CGW_FLAGS_CAN_FD) {
  543. struct cgw_fdframe_mod mb;
  544. if (gwj->mod.modtype.and) {
  545. memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
  546. mb.modtype = gwj->mod.modtype.and;
  547. if (nla_put(skb, CGW_FDMOD_AND, sizeof(mb), &mb) < 0)
  548. goto cancel;
  549. }
  550. if (gwj->mod.modtype.or) {
  551. memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
  552. mb.modtype = gwj->mod.modtype.or;
  553. if (nla_put(skb, CGW_FDMOD_OR, sizeof(mb), &mb) < 0)
  554. goto cancel;
  555. }
  556. if (gwj->mod.modtype.xor) {
  557. memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
  558. mb.modtype = gwj->mod.modtype.xor;
  559. if (nla_put(skb, CGW_FDMOD_XOR, sizeof(mb), &mb) < 0)
  560. goto cancel;
  561. }
  562. if (gwj->mod.modtype.set) {
  563. memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
  564. mb.modtype = gwj->mod.modtype.set;
  565. if (nla_put(skb, CGW_FDMOD_SET, sizeof(mb), &mb) < 0)
  566. goto cancel;
  567. }
  568. } else {
  569. struct cgw_frame_mod mb;
  570. if (gwj->mod.modtype.and) {
  571. memcpy(&mb.cf, &gwj->mod.modframe.and, sizeof(mb.cf));
  572. mb.modtype = gwj->mod.modtype.and;
  573. if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
  574. goto cancel;
  575. }
  576. if (gwj->mod.modtype.or) {
  577. memcpy(&mb.cf, &gwj->mod.modframe.or, sizeof(mb.cf));
  578. mb.modtype = gwj->mod.modtype.or;
  579. if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
  580. goto cancel;
  581. }
  582. if (gwj->mod.modtype.xor) {
  583. memcpy(&mb.cf, &gwj->mod.modframe.xor, sizeof(mb.cf));
  584. mb.modtype = gwj->mod.modtype.xor;
  585. if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
  586. goto cancel;
  587. }
  588. if (gwj->mod.modtype.set) {
  589. memcpy(&mb.cf, &gwj->mod.modframe.set, sizeof(mb.cf));
  590. mb.modtype = gwj->mod.modtype.set;
  591. if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
  592. goto cancel;
  593. }
  594. }
  595. if (gwj->mod.uid) {
  596. if (nla_put_u32(skb, CGW_MOD_UID, gwj->mod.uid) < 0)
  597. goto cancel;
  598. }
  599. if (gwj->mod.csumfunc.crc8) {
  600. if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
  601. &gwj->mod.csum.crc8) < 0)
  602. goto cancel;
  603. }
  604. if (gwj->mod.csumfunc.xor) {
  605. if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
  606. &gwj->mod.csum.xor) < 0)
  607. goto cancel;
  608. }
  609. if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
  610. if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
  611. if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
  612. &gwj->ccgw.filter) < 0)
  613. goto cancel;
  614. }
  615. if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
  616. goto cancel;
  617. if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
  618. goto cancel;
  619. }
  620. nlmsg_end(skb, nlh);
  621. return 0;
  622. cancel:
  623. nlmsg_cancel(skb, nlh);
  624. return -EMSGSIZE;
  625. }
  626. /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
  627. static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
  628. {
  629. struct net *net = sock_net(skb->sk);
  630. struct cgw_job *gwj = NULL;
  631. int idx = 0;
  632. int s_idx = cb->args[0];
  633. rcu_read_lock();
  634. hlist_for_each_entry_rcu(gwj, &net->can.cgw_list, list) {
  635. if (idx < s_idx)
  636. goto cont;
  637. if (cgw_put_job(skb, gwj, RTM_NEWROUTE,
  638. NETLINK_CB(cb->skb).portid,
  639. cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0)
  640. break;
  641. cont:
  642. idx++;
  643. }
  644. rcu_read_unlock();
  645. cb->args[0] = idx;
  646. return skb->len;
  647. }
  648. static const struct nla_policy cgw_policy[CGW_MAX + 1] = {
  649. [CGW_MOD_AND] = { .len = sizeof(struct cgw_frame_mod) },
  650. [CGW_MOD_OR] = { .len = sizeof(struct cgw_frame_mod) },
  651. [CGW_MOD_XOR] = { .len = sizeof(struct cgw_frame_mod) },
  652. [CGW_MOD_SET] = { .len = sizeof(struct cgw_frame_mod) },
  653. [CGW_CS_XOR] = { .len = sizeof(struct cgw_csum_xor) },
  654. [CGW_CS_CRC8] = { .len = sizeof(struct cgw_csum_crc8) },
  655. [CGW_SRC_IF] = { .type = NLA_U32 },
  656. [CGW_DST_IF] = { .type = NLA_U32 },
  657. [CGW_FILTER] = { .len = sizeof(struct can_filter) },
  658. [CGW_LIM_HOPS] = { .type = NLA_U8 },
  659. [CGW_MOD_UID] = { .type = NLA_U32 },
  660. [CGW_FDMOD_AND] = { .len = sizeof(struct cgw_fdframe_mod) },
  661. [CGW_FDMOD_OR] = { .len = sizeof(struct cgw_fdframe_mod) },
  662. [CGW_FDMOD_XOR] = { .len = sizeof(struct cgw_fdframe_mod) },
  663. [CGW_FDMOD_SET] = { .len = sizeof(struct cgw_fdframe_mod) },
  664. };
  665. /* check for common and gwtype specific attributes */
  666. static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
  667. u8 gwtype, void *gwtypeattr, u8 *limhops)
  668. {
  669. struct nlattr *tb[CGW_MAX + 1];
  670. struct rtcanmsg *r = nlmsg_data(nlh);
  671. int modidx = 0;
  672. int err = 0;
  673. /* initialize modification & checksum data space */
  674. memset(mod, 0, sizeof(*mod));
  675. err = nlmsg_parse_deprecated(nlh, sizeof(struct rtcanmsg), tb,
  676. CGW_MAX, cgw_policy, NULL);
  677. if (err < 0)
  678. return err;
  679. if (tb[CGW_LIM_HOPS]) {
  680. *limhops = nla_get_u8(tb[CGW_LIM_HOPS]);
  681. if (*limhops < 1 || *limhops > max_hops)
  682. return -EINVAL;
  683. }
  684. /* check for AND/OR/XOR/SET modifications */
  685. if (r->flags & CGW_FLAGS_CAN_FD) {
  686. struct cgw_fdframe_mod mb;
  687. if (tb[CGW_FDMOD_AND]) {
  688. nla_memcpy(&mb, tb[CGW_FDMOD_AND], CGW_FDMODATTR_LEN);
  689. canfdframecpy(&mod->modframe.and, &mb.cf);
  690. mod->modtype.and = mb.modtype;
  691. if (mb.modtype & CGW_MOD_ID)
  692. mod->modfunc[modidx++] = mod_and_id;
  693. if (mb.modtype & CGW_MOD_LEN)
  694. mod->modfunc[modidx++] = mod_and_len;
  695. if (mb.modtype & CGW_MOD_FLAGS)
  696. mod->modfunc[modidx++] = mod_and_flags;
  697. if (mb.modtype & CGW_MOD_DATA)
  698. mod->modfunc[modidx++] = mod_and_fddata;
  699. }
  700. if (tb[CGW_FDMOD_OR]) {
  701. nla_memcpy(&mb, tb[CGW_FDMOD_OR], CGW_FDMODATTR_LEN);
  702. canfdframecpy(&mod->modframe.or, &mb.cf);
  703. mod->modtype.or = mb.modtype;
  704. if (mb.modtype & CGW_MOD_ID)
  705. mod->modfunc[modidx++] = mod_or_id;
  706. if (mb.modtype & CGW_MOD_LEN)
  707. mod->modfunc[modidx++] = mod_or_len;
  708. if (mb.modtype & CGW_MOD_FLAGS)
  709. mod->modfunc[modidx++] = mod_or_flags;
  710. if (mb.modtype & CGW_MOD_DATA)
  711. mod->modfunc[modidx++] = mod_or_fddata;
  712. }
  713. if (tb[CGW_FDMOD_XOR]) {
  714. nla_memcpy(&mb, tb[CGW_FDMOD_XOR], CGW_FDMODATTR_LEN);
  715. canfdframecpy(&mod->modframe.xor, &mb.cf);
  716. mod->modtype.xor = mb.modtype;
  717. if (mb.modtype & CGW_MOD_ID)
  718. mod->modfunc[modidx++] = mod_xor_id;
  719. if (mb.modtype & CGW_MOD_LEN)
  720. mod->modfunc[modidx++] = mod_xor_len;
  721. if (mb.modtype & CGW_MOD_FLAGS)
  722. mod->modfunc[modidx++] = mod_xor_flags;
  723. if (mb.modtype & CGW_MOD_DATA)
  724. mod->modfunc[modidx++] = mod_xor_fddata;
  725. }
  726. if (tb[CGW_FDMOD_SET]) {
  727. nla_memcpy(&mb, tb[CGW_FDMOD_SET], CGW_FDMODATTR_LEN);
  728. canfdframecpy(&mod->modframe.set, &mb.cf);
  729. mod->modtype.set = mb.modtype;
  730. if (mb.modtype & CGW_MOD_ID)
  731. mod->modfunc[modidx++] = mod_set_id;
  732. if (mb.modtype & CGW_MOD_LEN)
  733. mod->modfunc[modidx++] = mod_set_len;
  734. if (mb.modtype & CGW_MOD_FLAGS)
  735. mod->modfunc[modidx++] = mod_set_flags;
  736. if (mb.modtype & CGW_MOD_DATA)
  737. mod->modfunc[modidx++] = mod_set_fddata;
  738. }
  739. } else {
  740. struct cgw_frame_mod mb;
  741. if (tb[CGW_MOD_AND]) {
  742. nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
  743. canframecpy(&mod->modframe.and, &mb.cf);
  744. mod->modtype.and = mb.modtype;
  745. if (mb.modtype & CGW_MOD_ID)
  746. mod->modfunc[modidx++] = mod_and_id;
  747. if (mb.modtype & CGW_MOD_DLC)
  748. mod->modfunc[modidx++] = mod_and_ccdlc;
  749. if (mb.modtype & CGW_MOD_DATA)
  750. mod->modfunc[modidx++] = mod_and_data;
  751. }
  752. if (tb[CGW_MOD_OR]) {
  753. nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
  754. canframecpy(&mod->modframe.or, &mb.cf);
  755. mod->modtype.or = mb.modtype;
  756. if (mb.modtype & CGW_MOD_ID)
  757. mod->modfunc[modidx++] = mod_or_id;
  758. if (mb.modtype & CGW_MOD_DLC)
  759. mod->modfunc[modidx++] = mod_or_ccdlc;
  760. if (mb.modtype & CGW_MOD_DATA)
  761. mod->modfunc[modidx++] = mod_or_data;
  762. }
  763. if (tb[CGW_MOD_XOR]) {
  764. nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
  765. canframecpy(&mod->modframe.xor, &mb.cf);
  766. mod->modtype.xor = mb.modtype;
  767. if (mb.modtype & CGW_MOD_ID)
  768. mod->modfunc[modidx++] = mod_xor_id;
  769. if (mb.modtype & CGW_MOD_DLC)
  770. mod->modfunc[modidx++] = mod_xor_ccdlc;
  771. if (mb.modtype & CGW_MOD_DATA)
  772. mod->modfunc[modidx++] = mod_xor_data;
  773. }
  774. if (tb[CGW_MOD_SET]) {
  775. nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
  776. canframecpy(&mod->modframe.set, &mb.cf);
  777. mod->modtype.set = mb.modtype;
  778. if (mb.modtype & CGW_MOD_ID)
  779. mod->modfunc[modidx++] = mod_set_id;
  780. if (mb.modtype & CGW_MOD_DLC)
  781. mod->modfunc[modidx++] = mod_set_ccdlc;
  782. if (mb.modtype & CGW_MOD_DATA)
  783. mod->modfunc[modidx++] = mod_set_data;
  784. }
  785. }
  786. /* check for checksum operations after CAN frame modifications */
  787. if (modidx) {
  788. if (tb[CGW_CS_CRC8]) {
  789. struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]);
  790. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  791. c->result_idx, r);
  792. if (err)
  793. return err;
  794. nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
  795. CGW_CS_CRC8_LEN);
  796. /* select dedicated processing function to reduce
  797. * runtime operations in receive hot path.
  798. */
  799. if (c->from_idx < 0 || c->to_idx < 0 ||
  800. c->result_idx < 0)
  801. mod->csumfunc.crc8 = cgw_csum_crc8_rel;
  802. else if (c->from_idx <= c->to_idx)
  803. mod->csumfunc.crc8 = cgw_csum_crc8_pos;
  804. else
  805. mod->csumfunc.crc8 = cgw_csum_crc8_neg;
  806. }
  807. if (tb[CGW_CS_XOR]) {
  808. struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]);
  809. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  810. c->result_idx, r);
  811. if (err)
  812. return err;
  813. nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
  814. CGW_CS_XOR_LEN);
  815. /* select dedicated processing function to reduce
  816. * runtime operations in receive hot path.
  817. */
  818. if (c->from_idx < 0 || c->to_idx < 0 ||
  819. c->result_idx < 0)
  820. mod->csumfunc.xor = cgw_csum_xor_rel;
  821. else if (c->from_idx <= c->to_idx)
  822. mod->csumfunc.xor = cgw_csum_xor_pos;
  823. else
  824. mod->csumfunc.xor = cgw_csum_xor_neg;
  825. }
  826. if (tb[CGW_MOD_UID])
  827. nla_memcpy(&mod->uid, tb[CGW_MOD_UID], sizeof(u32));
  828. }
  829. if (gwtype == CGW_TYPE_CAN_CAN) {
  830. /* check CGW_TYPE_CAN_CAN specific attributes */
  831. struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
  832. memset(ccgw, 0, sizeof(*ccgw));
  833. /* check for can_filter in attributes */
  834. if (tb[CGW_FILTER])
  835. nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
  836. sizeof(struct can_filter));
  837. err = -ENODEV;
  838. /* specifying two interfaces is mandatory */
  839. if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
  840. return err;
  841. ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]);
  842. ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]);
  843. /* both indices set to 0 for flushing all routing entries */
  844. if (!ccgw->src_idx && !ccgw->dst_idx)
  845. return 0;
  846. /* only one index set to 0 is an error */
  847. if (!ccgw->src_idx || !ccgw->dst_idx)
  848. return err;
  849. }
  850. /* add the checks for other gwtypes here */
  851. return 0;
  852. }
  853. static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
  854. struct netlink_ext_ack *extack)
  855. {
  856. struct net *net = sock_net(skb->sk);
  857. struct rtcanmsg *r;
  858. struct cgw_job *gwj;
  859. struct cf_mod mod;
  860. struct can_can_gw ccgw;
  861. u8 limhops = 0;
  862. int err = 0;
  863. if (!netlink_capable(skb, CAP_NET_ADMIN))
  864. return -EPERM;
  865. if (nlmsg_len(nlh) < sizeof(*r))
  866. return -EINVAL;
  867. r = nlmsg_data(nlh);
  868. if (r->can_family != AF_CAN)
  869. return -EPFNOSUPPORT;
  870. /* so far we only support CAN -> CAN routings */
  871. if (r->gwtype != CGW_TYPE_CAN_CAN)
  872. return -EINVAL;
  873. err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
  874. if (err < 0)
  875. return err;
  876. if (mod.uid) {
  877. ASSERT_RTNL();
  878. /* check for updating an existing job with identical uid */
  879. hlist_for_each_entry(gwj, &net->can.cgw_list, list) {
  880. if (gwj->mod.uid != mod.uid)
  881. continue;
  882. /* interfaces & filters must be identical */
  883. if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
  884. return -EINVAL;
  885. /* update modifications with disabled softirq & quit */
  886. local_bh_disable();
  887. memcpy(&gwj->mod, &mod, sizeof(mod));
  888. local_bh_enable();
  889. return 0;
  890. }
  891. }
  892. /* ifindex == 0 is not allowed for job creation */
  893. if (!ccgw.src_idx || !ccgw.dst_idx)
  894. return -ENODEV;
  895. gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
  896. if (!gwj)
  897. return -ENOMEM;
  898. gwj->handled_frames = 0;
  899. gwj->dropped_frames = 0;
  900. gwj->deleted_frames = 0;
  901. gwj->flags = r->flags;
  902. gwj->gwtype = r->gwtype;
  903. gwj->limit_hops = limhops;
  904. /* insert already parsed information */
  905. memcpy(&gwj->mod, &mod, sizeof(mod));
  906. memcpy(&gwj->ccgw, &ccgw, sizeof(ccgw));
  907. err = -ENODEV;
  908. gwj->src.dev = __dev_get_by_index(net, gwj->ccgw.src_idx);
  909. if (!gwj->src.dev)
  910. goto out;
  911. if (gwj->src.dev->type != ARPHRD_CAN)
  912. goto out;
  913. gwj->dst.dev = __dev_get_by_index(net, gwj->ccgw.dst_idx);
  914. if (!gwj->dst.dev)
  915. goto out;
  916. if (gwj->dst.dev->type != ARPHRD_CAN)
  917. goto out;
  918. ASSERT_RTNL();
  919. err = cgw_register_filter(net, gwj);
  920. if (!err)
  921. hlist_add_head_rcu(&gwj->list, &net->can.cgw_list);
  922. out:
  923. if (err)
  924. kmem_cache_free(cgw_cache, gwj);
  925. return err;
  926. }
  927. static void cgw_remove_all_jobs(struct net *net)
  928. {
  929. struct cgw_job *gwj = NULL;
  930. struct hlist_node *nx;
  931. ASSERT_RTNL();
  932. hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
  933. hlist_del(&gwj->list);
  934. cgw_unregister_filter(net, gwj);
  935. call_rcu(&gwj->rcu, cgw_job_free_rcu);
  936. }
  937. }
  938. static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh,
  939. struct netlink_ext_ack *extack)
  940. {
  941. struct net *net = sock_net(skb->sk);
  942. struct cgw_job *gwj = NULL;
  943. struct hlist_node *nx;
  944. struct rtcanmsg *r;
  945. struct cf_mod mod;
  946. struct can_can_gw ccgw;
  947. u8 limhops = 0;
  948. int err = 0;
  949. if (!netlink_capable(skb, CAP_NET_ADMIN))
  950. return -EPERM;
  951. if (nlmsg_len(nlh) < sizeof(*r))
  952. return -EINVAL;
  953. r = nlmsg_data(nlh);
  954. if (r->can_family != AF_CAN)
  955. return -EPFNOSUPPORT;
  956. /* so far we only support CAN -> CAN routings */
  957. if (r->gwtype != CGW_TYPE_CAN_CAN)
  958. return -EINVAL;
  959. err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
  960. if (err < 0)
  961. return err;
  962. /* two interface indices both set to 0 => remove all entries */
  963. if (!ccgw.src_idx && !ccgw.dst_idx) {
  964. cgw_remove_all_jobs(net);
  965. return 0;
  966. }
  967. err = -EINVAL;
  968. ASSERT_RTNL();
  969. /* remove only the first matching entry */
  970. hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
  971. if (gwj->flags != r->flags)
  972. continue;
  973. if (gwj->limit_hops != limhops)
  974. continue;
  975. /* we have a match when uid is enabled and identical */
  976. if (gwj->mod.uid || mod.uid) {
  977. if (gwj->mod.uid != mod.uid)
  978. continue;
  979. } else {
  980. /* no uid => check for identical modifications */
  981. if (memcmp(&gwj->mod, &mod, sizeof(mod)))
  982. continue;
  983. }
  984. /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
  985. if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
  986. continue;
  987. hlist_del(&gwj->list);
  988. cgw_unregister_filter(net, gwj);
  989. call_rcu(&gwj->rcu, cgw_job_free_rcu);
  990. err = 0;
  991. break;
  992. }
  993. return err;
  994. }
  995. static int __net_init cangw_pernet_init(struct net *net)
  996. {
  997. INIT_HLIST_HEAD(&net->can.cgw_list);
  998. return 0;
  999. }
  1000. static void __net_exit cangw_pernet_exit_batch(struct list_head *net_list)
  1001. {
  1002. struct net *net;
  1003. rtnl_lock();
  1004. list_for_each_entry(net, net_list, exit_list)
  1005. cgw_remove_all_jobs(net);
  1006. rtnl_unlock();
  1007. }
  1008. static struct pernet_operations cangw_pernet_ops = {
  1009. .init = cangw_pernet_init,
  1010. .exit_batch = cangw_pernet_exit_batch,
  1011. };
  1012. static __init int cgw_module_init(void)
  1013. {
  1014. int ret;
  1015. /* sanitize given module parameter */
  1016. max_hops = clamp_t(unsigned int, max_hops, CGW_MIN_HOPS, CGW_MAX_HOPS);
  1017. pr_info("can: netlink gateway - max_hops=%d\n", max_hops);
  1018. ret = register_pernet_subsys(&cangw_pernet_ops);
  1019. if (ret)
  1020. return ret;
  1021. ret = -ENOMEM;
  1022. cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
  1023. 0, 0, NULL);
  1024. if (!cgw_cache)
  1025. goto out_cache_create;
  1026. /* set notifier */
  1027. notifier.notifier_call = cgw_notifier;
  1028. ret = register_netdevice_notifier(&notifier);
  1029. if (ret)
  1030. goto out_register_notifier;
  1031. ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
  1032. NULL, cgw_dump_jobs, 0);
  1033. if (ret)
  1034. goto out_rtnl_register1;
  1035. ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
  1036. cgw_create_job, NULL, 0);
  1037. if (ret)
  1038. goto out_rtnl_register2;
  1039. ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
  1040. cgw_remove_job, NULL, 0);
  1041. if (ret)
  1042. goto out_rtnl_register3;
  1043. return 0;
  1044. out_rtnl_register3:
  1045. rtnl_unregister(PF_CAN, RTM_NEWROUTE);
  1046. out_rtnl_register2:
  1047. rtnl_unregister(PF_CAN, RTM_GETROUTE);
  1048. out_rtnl_register1:
  1049. unregister_netdevice_notifier(&notifier);
  1050. out_register_notifier:
  1051. kmem_cache_destroy(cgw_cache);
  1052. out_cache_create:
  1053. unregister_pernet_subsys(&cangw_pernet_ops);
  1054. return ret;
  1055. }
  1056. static __exit void cgw_module_exit(void)
  1057. {
  1058. rtnl_unregister_all(PF_CAN);
  1059. unregister_netdevice_notifier(&notifier);
  1060. unregister_pernet_subsys(&cangw_pernet_ops);
  1061. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1062. kmem_cache_destroy(cgw_cache);
  1063. }
  1064. module_init(cgw_module_init);
  1065. module_exit(cgw_module_exit);