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