gw.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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 <oliver.hartkopp@volkswagen.de>");
  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 __rcu *cf_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. struct cf_mod *mod;
  392. int modidx = 0;
  393. /* process strictly Classic CAN or CAN FD frames */
  394. if (gwj->flags & CGW_FLAGS_CAN_FD) {
  395. if (!can_is_canfd_skb(skb))
  396. return;
  397. } else {
  398. if (!can_is_can_skb(skb))
  399. return;
  400. }
  401. /* Do not handle CAN frames routed more than 'max_hops' times.
  402. * In general we should never catch this delimiter which is intended
  403. * to cover a misconfiguration protection (e.g. circular CAN routes).
  404. *
  405. * The Controller Area Network controllers only accept CAN frames with
  406. * correct CRCs - which are not visible in the controller registers.
  407. * According to skbuff.h documentation the csum_start element for IP
  408. * checksums is undefined/unused when ip_summed == CHECKSUM_UNNECESSARY.
  409. * Only CAN skbs can be processed here which already have this property.
  410. */
  411. #define cgw_hops(skb) ((skb)->csum_start)
  412. BUG_ON(skb->ip_summed != CHECKSUM_UNNECESSARY);
  413. if (cgw_hops(skb) >= max_hops) {
  414. /* indicate deleted frames due to misconfiguration */
  415. gwj->deleted_frames++;
  416. return;
  417. }
  418. if (!(gwj->dst.dev->flags & IFF_UP)) {
  419. gwj->dropped_frames++;
  420. return;
  421. }
  422. /* is sending the skb back to the incoming interface not allowed? */
  423. if (!(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK) &&
  424. can_skb_prv(skb)->ifindex == gwj->dst.dev->ifindex)
  425. return;
  426. /* clone the given skb, which has not been done in can_rcv()
  427. *
  428. * When there is at least one modification function activated,
  429. * we need to copy the skb as we want to modify skb->data.
  430. */
  431. mod = rcu_dereference(gwj->cf_mod);
  432. if (mod->modfunc[0])
  433. nskb = skb_copy(skb, GFP_ATOMIC);
  434. else
  435. nskb = skb_clone(skb, GFP_ATOMIC);
  436. if (!nskb) {
  437. gwj->dropped_frames++;
  438. return;
  439. }
  440. /* put the incremented hop counter in the cloned skb */
  441. cgw_hops(nskb) = cgw_hops(skb) + 1;
  442. /* first processing of this CAN frame -> adjust to private hop limit */
  443. if (gwj->limit_hops && cgw_hops(nskb) == 1)
  444. cgw_hops(nskb) = max_hops - gwj->limit_hops + 1;
  445. nskb->dev = gwj->dst.dev;
  446. /* pointer to modifiable CAN frame */
  447. cf = (struct canfd_frame *)nskb->data;
  448. /* perform preprocessed modification functions if there are any */
  449. while (modidx < MAX_MODFUNCTIONS && mod->modfunc[modidx])
  450. (*mod->modfunc[modidx++])(cf, mod);
  451. /* Has the CAN frame been modified? */
  452. if (modidx) {
  453. /* get available space for the processed CAN frame type */
  454. int max_len = nskb->len - offsetof(struct canfd_frame, data);
  455. /* dlc may have changed, make sure it fits to the CAN frame */
  456. if (cf->len > max_len) {
  457. /* delete frame due to misconfiguration */
  458. gwj->deleted_frames++;
  459. kfree_skb(nskb);
  460. return;
  461. }
  462. /* check for checksum updates */
  463. if (mod->csumfunc.crc8)
  464. (*mod->csumfunc.crc8)(cf, &mod->csum.crc8);
  465. if (mod->csumfunc.xor)
  466. (*mod->csumfunc.xor)(cf, &mod->csum.xor);
  467. }
  468. /* clear the skb timestamp if not configured the other way */
  469. if (!(gwj->flags & CGW_FLAGS_CAN_SRC_TSTAMP))
  470. nskb->tstamp = 0;
  471. /* send to netdevice */
  472. if (can_send(nskb, gwj->flags & CGW_FLAGS_CAN_ECHO))
  473. gwj->dropped_frames++;
  474. else
  475. gwj->handled_frames++;
  476. }
  477. static inline int cgw_register_filter(struct net *net, struct cgw_job *gwj)
  478. {
  479. return can_rx_register(net, gwj->src.dev, gwj->ccgw.filter.can_id,
  480. gwj->ccgw.filter.can_mask, can_can_gw_rcv,
  481. gwj, "gw", NULL);
  482. }
  483. static inline void cgw_unregister_filter(struct net *net, struct cgw_job *gwj)
  484. {
  485. can_rx_unregister(net, gwj->src.dev, gwj->ccgw.filter.can_id,
  486. gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj);
  487. }
  488. static void cgw_job_free_rcu(struct rcu_head *rcu_head)
  489. {
  490. struct cgw_job *gwj = container_of(rcu_head, struct cgw_job, rcu);
  491. /* cgw_job::cf_mod is always accessed from the same cgw_job object within
  492. * the same RCU read section. Once cgw_job is scheduled for removal,
  493. * cf_mod can also be removed without mandating an additional grace period.
  494. */
  495. kfree(rcu_access_pointer(gwj->cf_mod));
  496. kmem_cache_free(cgw_cache, gwj);
  497. }
  498. /* Return cgw_job::cf_mod with RTNL protected section */
  499. static struct cf_mod *cgw_job_cf_mod(struct cgw_job *gwj)
  500. {
  501. return rcu_dereference_protected(gwj->cf_mod, rtnl_is_locked());
  502. }
  503. static int cgw_notifier(struct notifier_block *nb,
  504. unsigned long msg, void *ptr)
  505. {
  506. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  507. struct net *net = dev_net(dev);
  508. if (dev->type != ARPHRD_CAN)
  509. return NOTIFY_DONE;
  510. if (msg == NETDEV_UNREGISTER) {
  511. struct cgw_job *gwj = NULL;
  512. struct hlist_node *nx;
  513. ASSERT_RTNL();
  514. hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
  515. if (gwj->src.dev == dev || gwj->dst.dev == dev) {
  516. hlist_del(&gwj->list);
  517. cgw_unregister_filter(net, gwj);
  518. call_rcu(&gwj->rcu, cgw_job_free_rcu);
  519. }
  520. }
  521. }
  522. return NOTIFY_DONE;
  523. }
  524. static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
  525. u32 pid, u32 seq, int flags)
  526. {
  527. struct rtcanmsg *rtcan;
  528. struct nlmsghdr *nlh;
  529. struct cf_mod *mod;
  530. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtcan), flags);
  531. if (!nlh)
  532. return -EMSGSIZE;
  533. rtcan = nlmsg_data(nlh);
  534. rtcan->can_family = AF_CAN;
  535. rtcan->gwtype = gwj->gwtype;
  536. rtcan->flags = gwj->flags;
  537. /* add statistics if available */
  538. if (gwj->handled_frames) {
  539. if (nla_put_u32(skb, CGW_HANDLED, gwj->handled_frames) < 0)
  540. goto cancel;
  541. }
  542. if (gwj->dropped_frames) {
  543. if (nla_put_u32(skb, CGW_DROPPED, gwj->dropped_frames) < 0)
  544. goto cancel;
  545. }
  546. if (gwj->deleted_frames) {
  547. if (nla_put_u32(skb, CGW_DELETED, gwj->deleted_frames) < 0)
  548. goto cancel;
  549. }
  550. /* check non default settings of attributes */
  551. if (gwj->limit_hops) {
  552. if (nla_put_u8(skb, CGW_LIM_HOPS, gwj->limit_hops) < 0)
  553. goto cancel;
  554. }
  555. mod = cgw_job_cf_mod(gwj);
  556. if (gwj->flags & CGW_FLAGS_CAN_FD) {
  557. struct cgw_fdframe_mod mb;
  558. if (mod->modtype.and) {
  559. memcpy(&mb.cf, &mod->modframe.and, sizeof(mb.cf));
  560. mb.modtype = mod->modtype.and;
  561. if (nla_put(skb, CGW_FDMOD_AND, sizeof(mb), &mb) < 0)
  562. goto cancel;
  563. }
  564. if (mod->modtype.or) {
  565. memcpy(&mb.cf, &mod->modframe.or, sizeof(mb.cf));
  566. mb.modtype = mod->modtype.or;
  567. if (nla_put(skb, CGW_FDMOD_OR, sizeof(mb), &mb) < 0)
  568. goto cancel;
  569. }
  570. if (mod->modtype.xor) {
  571. memcpy(&mb.cf, &mod->modframe.xor, sizeof(mb.cf));
  572. mb.modtype = mod->modtype.xor;
  573. if (nla_put(skb, CGW_FDMOD_XOR, sizeof(mb), &mb) < 0)
  574. goto cancel;
  575. }
  576. if (mod->modtype.set) {
  577. memcpy(&mb.cf, &mod->modframe.set, sizeof(mb.cf));
  578. mb.modtype = mod->modtype.set;
  579. if (nla_put(skb, CGW_FDMOD_SET, sizeof(mb), &mb) < 0)
  580. goto cancel;
  581. }
  582. } else {
  583. struct cgw_frame_mod mb;
  584. if (mod->modtype.and) {
  585. memcpy(&mb.cf, &mod->modframe.and, sizeof(mb.cf));
  586. mb.modtype = mod->modtype.and;
  587. if (nla_put(skb, CGW_MOD_AND, sizeof(mb), &mb) < 0)
  588. goto cancel;
  589. }
  590. if (mod->modtype.or) {
  591. memcpy(&mb.cf, &mod->modframe.or, sizeof(mb.cf));
  592. mb.modtype = mod->modtype.or;
  593. if (nla_put(skb, CGW_MOD_OR, sizeof(mb), &mb) < 0)
  594. goto cancel;
  595. }
  596. if (mod->modtype.xor) {
  597. memcpy(&mb.cf, &mod->modframe.xor, sizeof(mb.cf));
  598. mb.modtype = mod->modtype.xor;
  599. if (nla_put(skb, CGW_MOD_XOR, sizeof(mb), &mb) < 0)
  600. goto cancel;
  601. }
  602. if (mod->modtype.set) {
  603. memcpy(&mb.cf, &mod->modframe.set, sizeof(mb.cf));
  604. mb.modtype = mod->modtype.set;
  605. if (nla_put(skb, CGW_MOD_SET, sizeof(mb), &mb) < 0)
  606. goto cancel;
  607. }
  608. }
  609. if (mod->uid) {
  610. if (nla_put_u32(skb, CGW_MOD_UID, mod->uid) < 0)
  611. goto cancel;
  612. }
  613. if (mod->csumfunc.crc8) {
  614. if (nla_put(skb, CGW_CS_CRC8, CGW_CS_CRC8_LEN,
  615. &mod->csum.crc8) < 0)
  616. goto cancel;
  617. }
  618. if (mod->csumfunc.xor) {
  619. if (nla_put(skb, CGW_CS_XOR, CGW_CS_XOR_LEN,
  620. &mod->csum.xor) < 0)
  621. goto cancel;
  622. }
  623. if (gwj->gwtype == CGW_TYPE_CAN_CAN) {
  624. if (gwj->ccgw.filter.can_id || gwj->ccgw.filter.can_mask) {
  625. if (nla_put(skb, CGW_FILTER, sizeof(struct can_filter),
  626. &gwj->ccgw.filter) < 0)
  627. goto cancel;
  628. }
  629. if (nla_put_u32(skb, CGW_SRC_IF, gwj->ccgw.src_idx) < 0)
  630. goto cancel;
  631. if (nla_put_u32(skb, CGW_DST_IF, gwj->ccgw.dst_idx) < 0)
  632. goto cancel;
  633. }
  634. nlmsg_end(skb, nlh);
  635. return 0;
  636. cancel:
  637. nlmsg_cancel(skb, nlh);
  638. return -EMSGSIZE;
  639. }
  640. /* Dump information about all CAN gateway jobs, in response to RTM_GETROUTE */
  641. static int cgw_dump_jobs(struct sk_buff *skb, struct netlink_callback *cb)
  642. {
  643. struct net *net = sock_net(skb->sk);
  644. struct cgw_job *gwj = NULL;
  645. int idx = 0;
  646. int s_idx = cb->args[0];
  647. rcu_read_lock();
  648. hlist_for_each_entry_rcu(gwj, &net->can.cgw_list, list) {
  649. if (idx < s_idx)
  650. goto cont;
  651. if (cgw_put_job(skb, gwj, RTM_NEWROUTE,
  652. NETLINK_CB(cb->skb).portid,
  653. cb->nlh->nlmsg_seq, NLM_F_MULTI) < 0)
  654. break;
  655. cont:
  656. idx++;
  657. }
  658. rcu_read_unlock();
  659. cb->args[0] = idx;
  660. return skb->len;
  661. }
  662. static const struct nla_policy cgw_policy[CGW_MAX + 1] = {
  663. [CGW_MOD_AND] = { .len = sizeof(struct cgw_frame_mod) },
  664. [CGW_MOD_OR] = { .len = sizeof(struct cgw_frame_mod) },
  665. [CGW_MOD_XOR] = { .len = sizeof(struct cgw_frame_mod) },
  666. [CGW_MOD_SET] = { .len = sizeof(struct cgw_frame_mod) },
  667. [CGW_CS_XOR] = { .len = sizeof(struct cgw_csum_xor) },
  668. [CGW_CS_CRC8] = { .len = sizeof(struct cgw_csum_crc8) },
  669. [CGW_SRC_IF] = { .type = NLA_U32 },
  670. [CGW_DST_IF] = { .type = NLA_U32 },
  671. [CGW_FILTER] = { .len = sizeof(struct can_filter) },
  672. [CGW_LIM_HOPS] = { .type = NLA_U8 },
  673. [CGW_MOD_UID] = { .type = NLA_U32 },
  674. [CGW_FDMOD_AND] = { .len = sizeof(struct cgw_fdframe_mod) },
  675. [CGW_FDMOD_OR] = { .len = sizeof(struct cgw_fdframe_mod) },
  676. [CGW_FDMOD_XOR] = { .len = sizeof(struct cgw_fdframe_mod) },
  677. [CGW_FDMOD_SET] = { .len = sizeof(struct cgw_fdframe_mod) },
  678. };
  679. /* check for common and gwtype specific attributes */
  680. static int cgw_parse_attr(struct nlmsghdr *nlh, struct cf_mod *mod,
  681. u8 gwtype, void *gwtypeattr, u8 *limhops)
  682. {
  683. struct nlattr *tb[CGW_MAX + 1];
  684. struct rtcanmsg *r = nlmsg_data(nlh);
  685. int modidx = 0;
  686. int err = 0;
  687. /* initialize modification & checksum data space */
  688. memset(mod, 0, sizeof(*mod));
  689. err = nlmsg_parse_deprecated(nlh, sizeof(struct rtcanmsg), tb,
  690. CGW_MAX, cgw_policy, NULL);
  691. if (err < 0)
  692. return err;
  693. if (tb[CGW_LIM_HOPS]) {
  694. *limhops = nla_get_u8(tb[CGW_LIM_HOPS]);
  695. if (*limhops < 1 || *limhops > max_hops)
  696. return -EINVAL;
  697. }
  698. /* check for AND/OR/XOR/SET modifications */
  699. if (r->flags & CGW_FLAGS_CAN_FD) {
  700. struct cgw_fdframe_mod mb;
  701. if (tb[CGW_FDMOD_AND]) {
  702. nla_memcpy(&mb, tb[CGW_FDMOD_AND], CGW_FDMODATTR_LEN);
  703. canfdframecpy(&mod->modframe.and, &mb.cf);
  704. mod->modtype.and = mb.modtype;
  705. if (mb.modtype & CGW_MOD_ID)
  706. mod->modfunc[modidx++] = mod_and_id;
  707. if (mb.modtype & CGW_MOD_LEN)
  708. mod->modfunc[modidx++] = mod_and_len;
  709. if (mb.modtype & CGW_MOD_FLAGS)
  710. mod->modfunc[modidx++] = mod_and_flags;
  711. if (mb.modtype & CGW_MOD_DATA)
  712. mod->modfunc[modidx++] = mod_and_fddata;
  713. }
  714. if (tb[CGW_FDMOD_OR]) {
  715. nla_memcpy(&mb, tb[CGW_FDMOD_OR], CGW_FDMODATTR_LEN);
  716. canfdframecpy(&mod->modframe.or, &mb.cf);
  717. mod->modtype.or = mb.modtype;
  718. if (mb.modtype & CGW_MOD_ID)
  719. mod->modfunc[modidx++] = mod_or_id;
  720. if (mb.modtype & CGW_MOD_LEN)
  721. mod->modfunc[modidx++] = mod_or_len;
  722. if (mb.modtype & CGW_MOD_FLAGS)
  723. mod->modfunc[modidx++] = mod_or_flags;
  724. if (mb.modtype & CGW_MOD_DATA)
  725. mod->modfunc[modidx++] = mod_or_fddata;
  726. }
  727. if (tb[CGW_FDMOD_XOR]) {
  728. nla_memcpy(&mb, tb[CGW_FDMOD_XOR], CGW_FDMODATTR_LEN);
  729. canfdframecpy(&mod->modframe.xor, &mb.cf);
  730. mod->modtype.xor = mb.modtype;
  731. if (mb.modtype & CGW_MOD_ID)
  732. mod->modfunc[modidx++] = mod_xor_id;
  733. if (mb.modtype & CGW_MOD_LEN)
  734. mod->modfunc[modidx++] = mod_xor_len;
  735. if (mb.modtype & CGW_MOD_FLAGS)
  736. mod->modfunc[modidx++] = mod_xor_flags;
  737. if (mb.modtype & CGW_MOD_DATA)
  738. mod->modfunc[modidx++] = mod_xor_fddata;
  739. }
  740. if (tb[CGW_FDMOD_SET]) {
  741. nla_memcpy(&mb, tb[CGW_FDMOD_SET], CGW_FDMODATTR_LEN);
  742. canfdframecpy(&mod->modframe.set, &mb.cf);
  743. mod->modtype.set = mb.modtype;
  744. if (mb.modtype & CGW_MOD_ID)
  745. mod->modfunc[modidx++] = mod_set_id;
  746. if (mb.modtype & CGW_MOD_LEN)
  747. mod->modfunc[modidx++] = mod_set_len;
  748. if (mb.modtype & CGW_MOD_FLAGS)
  749. mod->modfunc[modidx++] = mod_set_flags;
  750. if (mb.modtype & CGW_MOD_DATA)
  751. mod->modfunc[modidx++] = mod_set_fddata;
  752. }
  753. } else {
  754. struct cgw_frame_mod mb;
  755. if (tb[CGW_MOD_AND]) {
  756. nla_memcpy(&mb, tb[CGW_MOD_AND], CGW_MODATTR_LEN);
  757. canframecpy(&mod->modframe.and, &mb.cf);
  758. mod->modtype.and = mb.modtype;
  759. if (mb.modtype & CGW_MOD_ID)
  760. mod->modfunc[modidx++] = mod_and_id;
  761. if (mb.modtype & CGW_MOD_DLC)
  762. mod->modfunc[modidx++] = mod_and_ccdlc;
  763. if (mb.modtype & CGW_MOD_DATA)
  764. mod->modfunc[modidx++] = mod_and_data;
  765. }
  766. if (tb[CGW_MOD_OR]) {
  767. nla_memcpy(&mb, tb[CGW_MOD_OR], CGW_MODATTR_LEN);
  768. canframecpy(&mod->modframe.or, &mb.cf);
  769. mod->modtype.or = mb.modtype;
  770. if (mb.modtype & CGW_MOD_ID)
  771. mod->modfunc[modidx++] = mod_or_id;
  772. if (mb.modtype & CGW_MOD_DLC)
  773. mod->modfunc[modidx++] = mod_or_ccdlc;
  774. if (mb.modtype & CGW_MOD_DATA)
  775. mod->modfunc[modidx++] = mod_or_data;
  776. }
  777. if (tb[CGW_MOD_XOR]) {
  778. nla_memcpy(&mb, tb[CGW_MOD_XOR], CGW_MODATTR_LEN);
  779. canframecpy(&mod->modframe.xor, &mb.cf);
  780. mod->modtype.xor = mb.modtype;
  781. if (mb.modtype & CGW_MOD_ID)
  782. mod->modfunc[modidx++] = mod_xor_id;
  783. if (mb.modtype & CGW_MOD_DLC)
  784. mod->modfunc[modidx++] = mod_xor_ccdlc;
  785. if (mb.modtype & CGW_MOD_DATA)
  786. mod->modfunc[modidx++] = mod_xor_data;
  787. }
  788. if (tb[CGW_MOD_SET]) {
  789. nla_memcpy(&mb, tb[CGW_MOD_SET], CGW_MODATTR_LEN);
  790. canframecpy(&mod->modframe.set, &mb.cf);
  791. mod->modtype.set = mb.modtype;
  792. if (mb.modtype & CGW_MOD_ID)
  793. mod->modfunc[modidx++] = mod_set_id;
  794. if (mb.modtype & CGW_MOD_DLC)
  795. mod->modfunc[modidx++] = mod_set_ccdlc;
  796. if (mb.modtype & CGW_MOD_DATA)
  797. mod->modfunc[modidx++] = mod_set_data;
  798. }
  799. }
  800. /* check for checksum operations after CAN frame modifications */
  801. if (modidx) {
  802. if (tb[CGW_CS_CRC8]) {
  803. struct cgw_csum_crc8 *c = nla_data(tb[CGW_CS_CRC8]);
  804. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  805. c->result_idx, r);
  806. if (err)
  807. return err;
  808. nla_memcpy(&mod->csum.crc8, tb[CGW_CS_CRC8],
  809. CGW_CS_CRC8_LEN);
  810. /* select dedicated processing function to reduce
  811. * runtime operations in receive hot path.
  812. */
  813. if (c->from_idx < 0 || c->to_idx < 0 ||
  814. c->result_idx < 0)
  815. mod->csumfunc.crc8 = cgw_csum_crc8_rel;
  816. else if (c->from_idx <= c->to_idx)
  817. mod->csumfunc.crc8 = cgw_csum_crc8_pos;
  818. else
  819. mod->csumfunc.crc8 = cgw_csum_crc8_neg;
  820. }
  821. if (tb[CGW_CS_XOR]) {
  822. struct cgw_csum_xor *c = nla_data(tb[CGW_CS_XOR]);
  823. err = cgw_chk_csum_parms(c->from_idx, c->to_idx,
  824. c->result_idx, r);
  825. if (err)
  826. return err;
  827. nla_memcpy(&mod->csum.xor, tb[CGW_CS_XOR],
  828. CGW_CS_XOR_LEN);
  829. /* select dedicated processing function to reduce
  830. * runtime operations in receive hot path.
  831. */
  832. if (c->from_idx < 0 || c->to_idx < 0 ||
  833. c->result_idx < 0)
  834. mod->csumfunc.xor = cgw_csum_xor_rel;
  835. else if (c->from_idx <= c->to_idx)
  836. mod->csumfunc.xor = cgw_csum_xor_pos;
  837. else
  838. mod->csumfunc.xor = cgw_csum_xor_neg;
  839. }
  840. if (tb[CGW_MOD_UID])
  841. nla_memcpy(&mod->uid, tb[CGW_MOD_UID], sizeof(u32));
  842. }
  843. if (gwtype == CGW_TYPE_CAN_CAN) {
  844. /* check CGW_TYPE_CAN_CAN specific attributes */
  845. struct can_can_gw *ccgw = (struct can_can_gw *)gwtypeattr;
  846. memset(ccgw, 0, sizeof(*ccgw));
  847. /* check for can_filter in attributes */
  848. if (tb[CGW_FILTER])
  849. nla_memcpy(&ccgw->filter, tb[CGW_FILTER],
  850. sizeof(struct can_filter));
  851. err = -ENODEV;
  852. /* specifying two interfaces is mandatory */
  853. if (!tb[CGW_SRC_IF] || !tb[CGW_DST_IF])
  854. return err;
  855. ccgw->src_idx = nla_get_u32(tb[CGW_SRC_IF]);
  856. ccgw->dst_idx = nla_get_u32(tb[CGW_DST_IF]);
  857. /* both indices set to 0 for flushing all routing entries */
  858. if (!ccgw->src_idx && !ccgw->dst_idx)
  859. return 0;
  860. /* only one index set to 0 is an error */
  861. if (!ccgw->src_idx || !ccgw->dst_idx)
  862. return err;
  863. }
  864. /* add the checks for other gwtypes here */
  865. return 0;
  866. }
  867. static int cgw_create_job(struct sk_buff *skb, struct nlmsghdr *nlh,
  868. struct netlink_ext_ack *extack)
  869. {
  870. struct net *net = sock_net(skb->sk);
  871. struct rtcanmsg *r;
  872. struct cgw_job *gwj;
  873. struct cf_mod *mod;
  874. struct can_can_gw ccgw;
  875. u8 limhops = 0;
  876. int err = 0;
  877. if (!netlink_capable(skb, CAP_NET_ADMIN))
  878. return -EPERM;
  879. if (nlmsg_len(nlh) < sizeof(*r))
  880. return -EINVAL;
  881. r = nlmsg_data(nlh);
  882. if (r->can_family != AF_CAN)
  883. return -EPFNOSUPPORT;
  884. /* so far we only support CAN -> CAN routings */
  885. if (r->gwtype != CGW_TYPE_CAN_CAN)
  886. return -EINVAL;
  887. mod = kmalloc(sizeof(*mod), GFP_KERNEL);
  888. if (!mod)
  889. return -ENOMEM;
  890. err = cgw_parse_attr(nlh, mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
  891. if (err < 0)
  892. goto out_free_cf;
  893. if (mod->uid) {
  894. ASSERT_RTNL();
  895. /* check for updating an existing job with identical uid */
  896. hlist_for_each_entry(gwj, &net->can.cgw_list, list) {
  897. struct cf_mod *old_cf;
  898. old_cf = cgw_job_cf_mod(gwj);
  899. if (old_cf->uid != mod->uid)
  900. continue;
  901. /* interfaces & filters must be identical */
  902. if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw))) {
  903. err = -EINVAL;
  904. goto out_free_cf;
  905. }
  906. rcu_assign_pointer(gwj->cf_mod, mod);
  907. kfree_rcu_mightsleep(old_cf);
  908. return 0;
  909. }
  910. }
  911. /* ifindex == 0 is not allowed for job creation */
  912. if (!ccgw.src_idx || !ccgw.dst_idx) {
  913. err = -ENODEV;
  914. goto out_free_cf;
  915. }
  916. gwj = kmem_cache_alloc(cgw_cache, GFP_KERNEL);
  917. if (!gwj) {
  918. err = -ENOMEM;
  919. goto out_free_cf;
  920. }
  921. gwj->handled_frames = 0;
  922. gwj->dropped_frames = 0;
  923. gwj->deleted_frames = 0;
  924. gwj->flags = r->flags;
  925. gwj->gwtype = r->gwtype;
  926. gwj->limit_hops = limhops;
  927. /* insert already parsed information */
  928. RCU_INIT_POINTER(gwj->cf_mod, mod);
  929. memcpy(&gwj->ccgw, &ccgw, sizeof(ccgw));
  930. err = -ENODEV;
  931. gwj->src.dev = __dev_get_by_index(net, gwj->ccgw.src_idx);
  932. if (!gwj->src.dev)
  933. goto out;
  934. if (gwj->src.dev->type != ARPHRD_CAN)
  935. goto out;
  936. gwj->dst.dev = __dev_get_by_index(net, gwj->ccgw.dst_idx);
  937. if (!gwj->dst.dev)
  938. goto out;
  939. if (gwj->dst.dev->type != ARPHRD_CAN)
  940. goto out;
  941. /* is sending the skb back to the incoming interface intended? */
  942. if (gwj->src.dev == gwj->dst.dev &&
  943. !(gwj->flags & CGW_FLAGS_CAN_IIF_TX_OK)) {
  944. err = -EINVAL;
  945. goto out;
  946. }
  947. ASSERT_RTNL();
  948. err = cgw_register_filter(net, gwj);
  949. if (!err)
  950. hlist_add_head_rcu(&gwj->list, &net->can.cgw_list);
  951. out:
  952. if (err) {
  953. kmem_cache_free(cgw_cache, gwj);
  954. out_free_cf:
  955. kfree(mod);
  956. }
  957. return err;
  958. }
  959. static void cgw_remove_all_jobs(struct net *net)
  960. {
  961. struct cgw_job *gwj = NULL;
  962. struct hlist_node *nx;
  963. ASSERT_RTNL();
  964. hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
  965. hlist_del(&gwj->list);
  966. cgw_unregister_filter(net, gwj);
  967. call_rcu(&gwj->rcu, cgw_job_free_rcu);
  968. }
  969. }
  970. static int cgw_remove_job(struct sk_buff *skb, struct nlmsghdr *nlh,
  971. struct netlink_ext_ack *extack)
  972. {
  973. struct net *net = sock_net(skb->sk);
  974. struct cgw_job *gwj = NULL;
  975. struct hlist_node *nx;
  976. struct rtcanmsg *r;
  977. struct cf_mod mod;
  978. struct can_can_gw ccgw;
  979. u8 limhops = 0;
  980. int err = 0;
  981. if (!netlink_capable(skb, CAP_NET_ADMIN))
  982. return -EPERM;
  983. if (nlmsg_len(nlh) < sizeof(*r))
  984. return -EINVAL;
  985. r = nlmsg_data(nlh);
  986. if (r->can_family != AF_CAN)
  987. return -EPFNOSUPPORT;
  988. /* so far we only support CAN -> CAN routings */
  989. if (r->gwtype != CGW_TYPE_CAN_CAN)
  990. return -EINVAL;
  991. err = cgw_parse_attr(nlh, &mod, CGW_TYPE_CAN_CAN, &ccgw, &limhops);
  992. if (err < 0)
  993. return err;
  994. /* two interface indices both set to 0 => remove all entries */
  995. if (!ccgw.src_idx && !ccgw.dst_idx) {
  996. cgw_remove_all_jobs(net);
  997. return 0;
  998. }
  999. err = -EINVAL;
  1000. ASSERT_RTNL();
  1001. /* remove only the first matching entry */
  1002. hlist_for_each_entry_safe(gwj, nx, &net->can.cgw_list, list) {
  1003. struct cf_mod *cf_mod;
  1004. if (gwj->flags != r->flags)
  1005. continue;
  1006. if (gwj->limit_hops != limhops)
  1007. continue;
  1008. cf_mod = cgw_job_cf_mod(gwj);
  1009. /* we have a match when uid is enabled and identical */
  1010. if (cf_mod->uid || mod.uid) {
  1011. if (cf_mod->uid != mod.uid)
  1012. continue;
  1013. } else {
  1014. /* no uid => check for identical modifications */
  1015. if (memcmp(cf_mod, &mod, sizeof(mod)))
  1016. continue;
  1017. }
  1018. /* if (r->gwtype == CGW_TYPE_CAN_CAN) - is made sure here */
  1019. if (memcmp(&gwj->ccgw, &ccgw, sizeof(ccgw)))
  1020. continue;
  1021. hlist_del(&gwj->list);
  1022. cgw_unregister_filter(net, gwj);
  1023. call_rcu(&gwj->rcu, cgw_job_free_rcu);
  1024. err = 0;
  1025. break;
  1026. }
  1027. return err;
  1028. }
  1029. static int __net_init cangw_pernet_init(struct net *net)
  1030. {
  1031. INIT_HLIST_HEAD(&net->can.cgw_list);
  1032. return 0;
  1033. }
  1034. static void __net_exit cangw_pernet_exit_batch(struct list_head *net_list)
  1035. {
  1036. struct net *net;
  1037. rtnl_lock();
  1038. list_for_each_entry(net, net_list, exit_list)
  1039. cgw_remove_all_jobs(net);
  1040. rtnl_unlock();
  1041. }
  1042. static struct pernet_operations cangw_pernet_ops = {
  1043. .init = cangw_pernet_init,
  1044. .exit_batch = cangw_pernet_exit_batch,
  1045. };
  1046. static __init int cgw_module_init(void)
  1047. {
  1048. int ret;
  1049. /* sanitize given module parameter */
  1050. max_hops = clamp_t(unsigned int, max_hops, CGW_MIN_HOPS, CGW_MAX_HOPS);
  1051. pr_info("can: netlink gateway - max_hops=%d\n", max_hops);
  1052. ret = register_pernet_subsys(&cangw_pernet_ops);
  1053. if (ret)
  1054. return ret;
  1055. ret = -ENOMEM;
  1056. cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
  1057. 0, 0, NULL);
  1058. if (!cgw_cache)
  1059. goto out_cache_create;
  1060. /* set notifier */
  1061. notifier.notifier_call = cgw_notifier;
  1062. ret = register_netdevice_notifier(&notifier);
  1063. if (ret)
  1064. goto out_register_notifier;
  1065. ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
  1066. NULL, cgw_dump_jobs, 0);
  1067. if (ret)
  1068. goto out_rtnl_register1;
  1069. ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
  1070. cgw_create_job, NULL, 0);
  1071. if (ret)
  1072. goto out_rtnl_register2;
  1073. ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
  1074. cgw_remove_job, NULL, 0);
  1075. if (ret)
  1076. goto out_rtnl_register3;
  1077. return 0;
  1078. out_rtnl_register3:
  1079. rtnl_unregister(PF_CAN, RTM_NEWROUTE);
  1080. out_rtnl_register2:
  1081. rtnl_unregister(PF_CAN, RTM_GETROUTE);
  1082. out_rtnl_register1:
  1083. unregister_netdevice_notifier(&notifier);
  1084. out_register_notifier:
  1085. kmem_cache_destroy(cgw_cache);
  1086. out_cache_create:
  1087. unregister_pernet_subsys(&cangw_pernet_ops);
  1088. return ret;
  1089. }
  1090. static __exit void cgw_module_exit(void)
  1091. {
  1092. rtnl_unregister_all(PF_CAN);
  1093. unregister_netdevice_notifier(&notifier);
  1094. unregister_pernet_subsys(&cangw_pernet_ops);
  1095. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1096. kmem_cache_destroy(cgw_cache);
  1097. }
  1098. module_init(cgw_module_init);
  1099. module_exit(cgw_module_exit);